In [1]:
%load_ext load_style
%load_style talk.css
In [2]:
%matplotlib inline
import numpy as np
from netCDF4 import Dataset # http://unidata.github.io/netcdf4-python/
In [3]:
#!wget ftp://ftp.cdc.noaa.gov/Datasets/ncep.reanalysis.derived/surface_gauss/skt.sfc.mon.mean.nc
ncfile = 'data\skt.mon.mean.nc'
Open a NetCDF file and print the file handler, then you can find the information of its variales from last several lines.
Just like:
In [4]:
fh = Dataset(ncfile, mode='r') # file handle, open in read only mode
print(fh)
fh.close() # close the file
In [5]:
fh = Dataset(ncfile, mode='r') # file handle, open in read only mode
lon = fh.variables['lon'][:]
lat = fh.variables['lat'][:]
nctime = fh.variables['time'][:]
t_unit = fh.variables['time'].units
skt = fh.variables['skt'][:]
try :
t_cal = fh.variables['time'].calendar
except AttributeError : # Attribute doesn't exist
t_cal = u"gregorian" # or standard
fh.close() # close the file
In [6]:
lat[0] # Caution! Python’s indexing starts with zero
Out[6]:
In [7]:
lat[-1] # gives the last value of the vector
Out[7]:
In [8]:
lat_so = lat[-21:-1]
lon_so = lon
skt_so = skt[:,-21:-1,:]
In [9]:
np.savez('data/skt.so.mon.mean.npz', skt_so=skt_so, lat_so=lat_so, lon_so=lon_so)
Surely, you can load these data back.
In [10]:
npzfile = np.load('data/skt.so.mon.mean.npz')
npzfile.files
Out[10]:
http://unidata.github.io/netcdf4-python/
John D. Hunter. Matplotlib: A 2D Graphics Environment, Computing in Science & Engineering, 9, 90-95 (2007), DOI:10.1109/MCSE.2007.55
Stéfan van der Walt, S. Chris Colbert and Gaël Varoquaux. The NumPy Array: A Structure for Efficient Numerical Computation, Computing in Science & Engineering, 13, 22-30 (2011), DOI:10.1109/MCSE.2011.37
Kalnay et al.,The NCEP/NCAR 40-year reanalysis project, Bull. Amer. Meteor. Soc., 77, 437-470, 1996.
In [ ]: