Differences between revisions 3 and 21 (spanning 18 versions)
Revision 3 as of 2009-11-23 12:00:10
Size: 519
Editor: anonymous
Comment:
Revision 21 as of 2010-11-22 12:45:29
Size: 4808
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
Python does not rely on one standard library for plotting and visualization, you have the choice to from several alternatives:

 * [[http://matplotlib.sourceforge.net/|matplotlib]] is becoming a quasi standard for plotting within the numpy/scipy/ipython environment and easy to use.
 * [[http://www.pyngl.ucar.edu/|PyNGL and PyNIO]] provide interfaces to the functionality existing in the NCAR Command Language (NCL).
 * [[http://code.enthought.com/projects/mayavi/|Mayavi Project - 3D Scientific Data Visualization and Plotting]]
 * [[http://www.pythonware.com/products/pil/|The Python Imaging Library (PIL) supports many file formats, and provides powerful image processing and graphics capabilities.]]
 * Interaction with other (non-Python) tools, e.g. [[http://www.soest.hawaii.edu/GMT/|The Generic Mapping Tools]].

== 2 D Plots with Pylab ==

{{attachment:density.png}}

{{{#!python
from pylab import *
# The seawater module is installed only for Python2.6.2:
# module load Python/2.6.2
import seawater

S=linspace(0,35)
T=linspace(-2,35.0)

rho=zeros((S.size,T.size))

for i,t in enumerate(T):
    rho[i,:]=seawater.dens(S,t)

figure()
CS = contour(S,T,rho,15,colors='k')
clabel(CS, fontsize=8, inline=1)
ylabel('Temperature [$^\circ$C]')
xlabel('Salinity')
title('Sea water density [kg/m$^3$]')
show()
savefig('density.png',dpi=75)
}}}
Line 13: Line 49:
[[attachment:Map1.png]] The [[http://matplotlib.sourceforge.net/basemap/doc/html/| matplotlib basemap toolkit]] is a library for plotting 2D data on maps in Python.

{{attachment:Map1.png}}
Line 25: Line 63:
m.plot(x,y,'r.') m.plot([x],[y],'r.')
Line 30: Line 68:

Don't forget to set the correct path for using Basemap on ZMAW systems
{{{
module load Python
}}}

== GMT ==

{{attachment:map.png}}

[[http://gmt.soest.hawaii.edu/|GMT]] is an open source collection of ~60 tools for manipulating geographic and Cartesian data sets (including filtering, trend fitting, gridding, projecting, etc.) and producing
publication quality scientific plots.

In particular useful are the [[http://gmt.soest.hawaii.edu/gmt/examples/ex14/gmt_example_14.html|gridding routines]] which can be used for irregular sampled oceanographic as well as for satellite data.

GMT can be easily applied together with Python using the [[http://docs.python.org/library/os.html|os module]].

{{{#!python
import os
os.system('pscoast -R4/12/52/57 -JM6i -P -B1g1 -Ggray -Df > map.ps')
os.system('gv map.ps')
}}}



== Google Earth ==

{{attachment:600px-Google_earth_chlorophyll.png}}

Documentation:

http://code.google.com/apis/kml/documentation/

Chlorophyll data:

http://oceancolor.gsfc.nasa.gov/cgi/climatologies.pl

Download png and make black transparent

{{{
convert -transparent black A20020012007273.L3m_CU_CHLO_4.png A20020012007273.L3m_CU_CHLO_4_trans.png
}}}


Create ''.kml'' file


{{{
<?xml version="1.0"?>
<kml xmlns="http://earth.google.com/kml/2.1"><Document><name>Chlorophyll Climatology</name>
  <Folder>
<Snippet maxLines="0"/>
<name>MODIS</name>
<ScreenOverlay>
  <name>UHH Logo</name>
  <color>ffffffff</color>
  <visibility>1</visibility>
  <Icon>
    <href>http://www.ifm.uni-hamburg.de/logo_uhh_neu.gif</href>
  </Icon>
      <overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
      <screenXY x="5" y="5" xunits="pixels" yunits="insetPixels"/>
      <size x="90" y="90" xunits="pixel" yunits="pixel"/>
</ScreenOverlay>
<LookAt>
 <longitude>8</longitude>
 <latitude>53</latitude>
  <altitude>2000000</altitude>
  <heading>0</heading>
</LookAt>
<Folder> <Snippet maxLines="0"/>
<name>Chlorophyll</name><GroundOverlay> <name>2002-2007</name>
<visibility>1</visibility><color>ffffffff</color>
<Icon><href>A20020012007273.L3m_CU_CHLO_4_trans.png</href></Icon>
<LatLonBox><north>90</north><south>-90</south><east>180</east><west>-180</west></LatLonBox>
</GroundOverlay></Folder></Folder></Document></kml>
}}}

== Grads ==

http://opengrads.org/wiki/index.php?title=PyGrADS_Interactive_Shell

= Exercise =

Add to the above Basemap the air temperature from NCEP reanalyis as isolines:

{{{#!python
fid=io.netcdf_file('air.mon.mean.nc','r')
lat=array(fid.variables['lat'])
lon=array(fid.variables['lon'])
air=array(fid.variables['air'])
LON,LAT=meshgrid(lon,lat)
x,y=m(LON,LAT)
m.contour(x,y,air[753,:,:])
}}}

Visualisation

Python does not rely on one standard library for plotting and visualization, you have the choice to from several alternatives:

2 D Plots with Pylab

density.png

   1 from pylab import *
   2 # The seawater module is installed only for Python2.6.2:
   3 # module load Python/2.6.2 
   4 import seawater
   5 
   6 S=linspace(0,35)
   7 T=linspace(-2,35.0)
   8 
   9 rho=zeros((S.size,T.size))
  10 
  11 for i,t in enumerate(T):
  12     rho[i,:]=seawater.dens(S,t)
  13 
  14 figure()
  15 CS = contour(S,T,rho,15,colors='k')
  16 clabel(CS, fontsize=8, inline=1)
  17 ylabel('Temperature [$^\circ$C]')
  18 xlabel('Salinity')
  19 title('Sea water density  [kg/m$^3$]')
  20 show()
  21 savefig('density.png',dpi=75)

Basemap

The matplotlib basemap toolkit is a library for plotting 2D data on maps in Python.

Map1.png

   1 from pylab import *
   2 from mpl_toolkits.basemap import Basemap
   3 
   4 m = Basemap(projection='ortho',lon_0=10.0,lat_0=45.0,resolution='l')
   5 m.bluemarble()
   6 
   7 lon,lat=10.0,53.5 # Hamburg
   8 
   9 x,y=m(lon,lat)
  10 m.plot([x],[y],'r.')
  11     
  12 show()
  13 savefig('Map1.png',dpi=75)

Don't forget to set the correct path for using Basemap on ZMAW systems

module load Python

GMT

map.png

GMT is an open source collection of ~60 tools for manipulating geographic and Cartesian data sets (including filtering, trend fitting, gridding, projecting, etc.) and producing publication quality scientific plots.

In particular useful are the gridding routines which can be used for irregular sampled oceanographic as well as for satellite data.

GMT can be easily applied together with Python using the os module.

   1 import os
   2 os.system('pscoast -R4/12/52/57 -JM6i -P -B1g1 -Ggray -Df > map.ps')
   3 os.system('gv map.ps')

Google Earth

600px-Google_earth_chlorophyll.png

Documentation:

http://code.google.com/apis/kml/documentation/

Chlorophyll data:

http://oceancolor.gsfc.nasa.gov/cgi/climatologies.pl

Download png and make black transparent

convert -transparent black A20020012007273.L3m_CU_CHLO_4.png A20020012007273.L3m_CU_CHLO_4_trans.png

Create .kml file

<?xml version="1.0"?>
<kml xmlns="http://earth.google.com/kml/2.1"><Document><name>Chlorophyll Climatology</name>
  <Folder>
<Snippet maxLines="0"/>
<name>MODIS</name>
<ScreenOverlay>
  <name>UHH Logo</name>
  <color>ffffffff</color>
  <visibility>1</visibility>
  <Icon>
    <href>http://www.ifm.uni-hamburg.de/logo_uhh_neu.gif</href>
  </Icon>
      <overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
      <screenXY x="5" y="5" xunits="pixels" yunits="insetPixels"/>
      <size x="90" y="90" xunits="pixel" yunits="pixel"/>
</ScreenOverlay>
<LookAt>
        <longitude>8</longitude>
        <latitude>53</latitude>
         <altitude>2000000</altitude>       
  <heading>0</heading>          
</LookAt>
<Folder> <Snippet maxLines="0"/>
<name>Chlorophyll</name><GroundOverlay> <name>2002-2007</name>
<visibility>1</visibility><color>ffffffff</color>
<Icon><href>A20020012007273.L3m_CU_CHLO_4_trans.png</href></Icon>
<LatLonBox><north>90</north><south>-90</south><east>180</east><west>-180</west></LatLonBox>
</GroundOverlay></Folder></Folder></Document></kml>

Grads

http://opengrads.org/wiki/index.php?title=PyGrADS_Interactive_Shell

Exercise

Add to the above Basemap the air temperature from NCEP reanalyis as isolines:

   1 fid=io.netcdf_file('air.mon.mean.nc','r')
   2 lat=array(fid.variables['lat'])
   3 lon=array(fid.variables['lon'])
   4 air=array(fid.variables['air'])
   5 LON,LAT=meshgrid(lon,lat)
   6 x,y=m(LON,LAT)
   7 m.contour(x,y,air[753,:,:])

LehreWiki: OpenSource2010/Lesson6 (last edited 2010-11-22 12:55:36 by anonymous)