Incidentally, if you wanted to do this a little more expressively than indexing, you could look into using iris ( http://scitools.org.uk/iris/docs/latest/index.html). It doesn't currently support DAP, but if you had the NetCDF file (from * http://www.marine.csiro.au/dods-data/climatology-netcdf/levitus_monthly_temp_98.nc *) you would do:
import iris
import iris.quickplot as qplt
import matplotlib.pyplot as plt
temp_cube = iris.load_cube(*'levitus_monthly_temp_98.**nc**'*)
# Sort out some of the bad metadata. Firstly, set the unit,
# secondly rename the dimension 1 coordinate to 'depth'.
temp_cube.unit = *'C'*
temp_cube.coord(dimensions=1, dim_coords=True).rename(*'depth'*)
# Extract a spatial sub-domain.
sub_temp_cube = temp_cube.extract(iris.Constraint(latitude=0.5,
longitude=lambda v: 45 < v < 100))
# Iterate over all the depth-longitude sections (in this case it
# iterates over time)
for cross_sect_cube in sub_temp_cube.slices([*'depth'*, *'longitude'*]):
qplt.pcolormesh(cross_sect_cube)
plt.gca().invert_yaxis()
plt.show()
break
[image: Inline images 1]
Hope that helps!
Phil
On 2 March 2013 09:37, Phil Elson <[email protected]> wrote:
> Perhaps something like:
>
>
> from matplotlib import pyplot as plt
>
> from netCDF4 import Dataset
>
> import numpy as np
>
>
>
> url=*'
> http://www.marine.csiro.au/dods/nph-dods/dods-data/climatology-netcdf/levitus_monthly_temp_98.nc
> '*
>
> ds = Dataset(url)
>
>
>
> temp = ds.variables[*'TEMP'*]
>
> lats = ds.variables[*'**lat**'*]
>
> lons = ds.variables[*'**lon**'*]
>
> depths = ds.variables[*'z'*]
>
>
>
> # filter all but one latitude
>
> lat_index = np.where(lats[:] == 0.5)[0][0]
>
> lats = lats[lat_index]
>
>
> # filter a range of longitudes
>
> lon_lower_index = np.where(lons[:] == 44.5)[0][0]
>
> lon_upper_index = np.where(lons[:] == 100.5)[0][0]
>
> lons = lons[lon_lower_index:lon_upper_index]
>
>
> temp = temp[0, :, lat_index, lon_lower_index:lon_upper_index]
>
>
>
> plt.pcolormesh(lons, depths[:], temp)
>
> plt.gca().invert_yaxis()
>
>
> plt.show()
>
>
>
>
>
>
> The indexing approach used here is quite flakey, so I certainly wouldn't
> use this in anything operational.
>
> Hope this helps,
>
> Phil
>
>
>
<<figure_1.png>>
------------------------------------------------------------------------------ Everyone hates slow websites. So do we. Make your web apps faster with AppDynamics Download AppDynamics Lite for free today: http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________ Matplotlib-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/matplotlib-users
