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, format='NETCDF3_CLASSIC')[source]

create a netCDF

dypy.netcdf.read_dimensions(filename)[source]

Read dimensions

Unlimited dimensions size are return as None

Parameters:filename (string) –
Returns:a dictionary with name as key and the dimension as value
Return type:dictionary
dypy.netcdf.read_gattributes(filename)[source]

Read global attributes from a netCDF

Parameters:filename (string) – path to a netcdf file
Returns:global attributes
Return type:dictionary
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; netcdf time are return as datetime array if possible
Parameters:
  • filename (string) – path to a netCDF file
  • variables (list or string) – variables to read as a list of string; a single variable can also be given as a string
  • index (slice, optional) – A slice object; for example np.s_[0, :, 10]
  • kwargs (keyword arguments) – A list of arguments to pass to the MFDataset class. For example to aggregate the files on the <time> dimension use aggdim=’time’
Returns:

list of numpy array

Return type:

list

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 read only the data in the given bounding box.

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) – coordinates of the bounding box as: minlon, maxlon, minlat, maxlat
  • lon (string or np.ndarray, default lon) – name of the 2D longitude array in the data; or 2D longitude array
  • lat (string or np.ndarray, default lat) – name of the 2D latitude array in the data: or 2D latitude array
  • 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

Parameters:filename (string) –
Returns:a list of variables names
Return type:list