netcdf package

This module is a wrapper for the netCDF4 library. It aims at shorten the reading of netcdf file.

Examples

Let’s start with reading one or several variables:

>>> import dypy.netcdf as dn
>>> filename  = 'foo.nc'

To display the variables of the netcdf file:

>>> print(dn.read_variables(filename))
[u'time', u'lon', u'lat', u'p', u'QV', u'TH', u'BLH']

You can read a single variable (becarful, do not forget the ”,” after the variable name):

>>> qv, = dn.read_var(filename, 'QV')

Or you can read several variables in once:

>>> th, qv = dn.read_var(filename, ['TH', 'QV'])

DocStrings

Module to read and write netcdf file.

Interface to netCDF4

dypy.netcdf.addvar(ncfile, varname, vardata, dimensions, attributes=None)[source]

Add a variable to an opened netcdf file

dypy.netcdf.addvar_to_file(filename, varname, vardata, dimensions, attributes=None)[source]

Add a variable to a netcdf file

dypy.netcdf.create(outname, dimensions, gattributes)[source]

create a netCDF

dypy.netcdf.read_dimensions(filename)[source]

Read dimensions

dypy.netcdf.read_gattributes(filename)[source]

Read global attributes from a netCDF

dypy.netcdf.read_var(filename, variables, index=slice(None, None, None), **kwargs)[source]

Extract variables from a netCDF

Raise an IOerror if the file[s] is not found

Returns:a list of netCDF4 array with time as a datetime object if possible
Options:

index should be a slice object like np.s_[0, :, 10]

The <kwargs> arguments are passed to the MFDataset Class. For example to aggregate the files on the <time> dimension use aggdim=’time’

dypy.netcdf.read_var_attributes(filename, var)[source]

Return the attributes of the variables as a dictionary

dypy.netcdf.read_var_bbox(filename, variables, bbox, lon='lon', lat='lat', return_index=False)[source]

Read var only in the bbox

Similar to read_var but with bbox and return_index added

Parameters:
  • filename (list or string) – filename(s) where the data is located
  • variables (list or string) – variable(s) name of the data to read from file
  • bbox (list) – minlon, maxlon, minlat, maxlat
  • lon (string, default lon) – name of the 2D longitude array in the data
  • lat (string, default lat) – name of the 2D latitude array in the data
  • return_index (boolean, default False) –
    If True return the index used to reduce the variable data, with the
    dimension (time, height, lon, lat)
Returns:

  • bbox_lon (numpy array) – lon restricted to the bbox
  • bbox_lat (numpy array) – lat restricted to the bbox
  • bbox_dta (numpy arra) – data restricted to the bbox

dypy.netcdf.read_variables(filename)[source]

Read variables