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

Fadzil Mnor fadzilmnor84 at gmail.com
Tue Oct 14 06:50:34 EDT 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141014/afdd8aa1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: uwind-crossection-test.png
Type: image/png
Size: 42464 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141014/afdd8aa1/attachment.png>


More information about the NumPy-Discussion mailing list