[Numpy-discussion] numpy.mean slicing in a netCDF file

Stephan Hoyer shoyer at gmail.com
Tue Oct 14 14:09:47 EDT 2014


Hi Fadzil,

My strong recommendation is that you don't just use numpy/netCDF4 to
process your data, but rather use one of a multitude of packages that have
been developed specifically to facilitate working with labeled data from
netCDF files:
- Iris: http://scitools.org.uk/iris/
- CDAT: http://uvcdat.llnl.gov/
- xray (my project): http://xray.readthedocs.org

I can't answer your specific question without taking a careful look at your
data, but in very general terms, your code will have fewer bugs if you can
use meaningful labels to refer to your data rather than numeric ranges like
396:757:12.

Best,
Stephan


On Tue, Oct 14, 2014 at 3:50 AM, Fadzil Mnor <fadzilmnor84 at gmail.com> wrote:

> Hi all,
> I wrote a script and plot monthly mean zonal wind (from a netcdf file
> names uwnd.mon.mean.nc) and I'm not sure if I'm doing it correctly. What
> I have:
>
>
> ********************************************************************************
> *#this part calculates mean values for january only, from 1980-2010; thus
> the index looks like this 396:757:12*
>
> def ujan():
>     f = nc.Dataset('~/data/ncep/uwnd.mon.mean.nc')
>     u10_1 = f.variables['uwnd']
>     u10_2 = np.mean(u10_1[396:757:12,:,38,39:43],axis=0)
>     return u10_2
>
> uJan = ujan()* #calling function*
>
> *#this part is only to define lon, lat and level *
> q = nc.Dataset('~/data/ncep/uwnd.mon.mean.nc')
> lon=q.variables['lon']
> lat=q.variables['lat']
> lev=q.variables['level']
>
> *#for some reason I need to define this unless it gave error "length of x
> must be number of column in z"*
>
> lon=lon[39:43]
>
> *#begin plotting*
>
> clevs=np.arange(-10.,10.,0.5)
> fig = plt.figure(figsize=(11, 8))
> fig.clf()
> ax = fig.add_subplot(111)
> ax.axis([97.5, 105., 1000., 10.])
> ax.tick_params(direction='out', which='both')
> ax.set_xlabel('Lon (degrees)')
> ax.set_ylabel('Pressure (mb)')
> ax.set_xticks(np.arange(97.5, 105., .5))
> ax.set_yticks([1000, 700, 500, 300, 100, 10])
> cs=ax.contourf(lon, lev, uJan, clevs, extend='both',cmap='seismic')
> plt.title('Zonal winds average (Jan, 1981-2010)')
> cax = fig.add_axes([0.99, 0.1, 0.03, 0.8])
> aa=fig.colorbar(cs,cax=cax,orientation='vertical')
> aa.set_label('m/s')
> plt.savefig('~/uwind-crossection-test.png', bbox_inches='tight')
>
> *******************************************************************************
>
> the result is attached.
> I have no idea how to confirm the result (at least until this email is
> written) , but I believe the lower altitude values should be mostly
> negative, because over this region, the zonal wind are usually easterly
> (thus,negative values), but I got positive values.
>
> Put the information above aside, *I just want to know if my slicing in
> the ujan() function is correct*. If it is, then, there must be nothing
> wrong(except my above mentioned assumption).
> The data file dimension is:
> *[time,level,latitude,longitude]*
>
> This part:
> *u10_2 = np.mean(u10_1[396:757:12,:,38,39:43],axis=0)*
> The line above will calculate the mean of zonal wind (uwnd) in a range of
> time index 396 to 757 for each year (january only), for all vertical level,
> at latitude index 38 (5 N) and in between longitude index 39 to 43
> (97.5E-105E).
> I assume it will calculate a 30-year average of zonal wind for january
> only.
> Is this correct?
>
> Thank you.
>
> Fadzil
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141014/af583c61/attachment.html>


More information about the NumPy-Discussion mailing list