Geodata

class ncTools.geodata(filename, iotype='r', dtype='', dimensions='', time_unlimited=True, chk=None, format='NETCDF3_64BIT', init=True)[source]

The class geodate is designed to work with geophysical data stored in NetCDF file having the possible dimensions: ‘T’ : the time counter ‘Z’ : the vertical dimension ‘Y’ : the latitudinal dimension ‘X’ : the longitudinal dimension Dimension names and dimension variables names are stored for a lot of models and dataset. The class is able to recognize the appropriate type to use. It allows as easiest access to NetCDF file for which the dimension may be unkown and some functions are designed to rapidly create, duplicate and modify the NetCDF. The additional use of chunks may reveal very powerful when using it with the calculation class.

Attributes

filename (str) The name of the netcdf file.
iotype ({‘r’, ‘w’}) Read or Write mode.
dtype (str) The type of the geophysical data
dimensions (str) A series of dimension keys (‘T’, ‘Z’, ‘Y’, ‘X’)
time_unlimited (bool) A boolean that tells if the record dimension is unlimited or not
chk (chunk) The default chunk on which perform operations
format (str) The netCDF format (‘NETCDF3’, ‘NETCDF3_64BIT’, ‘NETCDF4’, ‘NETCDF4_CLASSIC’)

Methods

build_gvar(varname)[source]

Construct a geovar instance from the corresponding variable in the netCDF file

Parameters:

varname : str

Name of the variable in the netCDF file

Returns:

gvar : geovar

The geovar instance with all the attributes of the variable

Examples

>>> gdat = example_geodata()    
>>> gvar = gdat.build_gvar('exlon')             
>>> print gvar
exlon (float32): 
   fill_value: -9999.0
   axis: YX
changeIO(iotype)[source]

Change the read/write mode of the netCDF file.

Parameters:

iotype : {‘r’, ‘w’}

Read or write mode.

Examples

>>> gdat = example_geodata()
>>> print gdat.iotype
w
>>> gdat.changeIO('r')
>>> print gdat.iotype
r
check_chunk(chk)[source]

Check if the chunk is consistent with the dimensions of the netCDF file otherwise reset the value to the min/max.

Parameters:

chk : chunk

The chunk to check

copy_coord(gdat, chk_in=None, chk_out=None)[source]

Copy all the coordinate variables from the instance gdat into the current instance

Parameters:

gdata : geodata

The geodata instance from which the coordinate variables are copied

chk_in : chunk

Define a subset of the input instance

chk_out : chunk

The corresponding subset in which the coordinate variables are copied

copy_dim(gdat)[source]

Copy the dimensions from another geodata instance

Parameters:

gdat : geodata

The input instance from which the dimensions are copied

Examples

>>> gdat_in = example_geodata()
>>> gdat_out = geodata('../examples/example_copy.nc', 
... iotype='w', dtype='example2d')
>>> print gdat_out
================================================================
Filename                 : ../examples/example_copy.nc 
Format                   : NETCDF3_64BIT 
I/O access               : w 
Data type                : example2d 
Dimensions               : TZYX 
Unlimited time dimension : True 
================================================================
Default chunk: 
-------------- 
None

Dimensions: 
----------- 

Variables: 
---------- 
================================================================

>>> gdat_out.copy_dim(gdat_in)
>>> print gdat_out
================================================================
Filename                 : ../examples/example_copy.nc 
Format                   : NETCDF3_64BIT 
I/O access               : w 
Data type                : example2d 
Dimensions               : TZYX 
Unlimited time dimension : True 
================================================================
Default chunk: 
-------------- 
None

Dimensions: 
----------- 
T - t: 0
Z - z: 9
Y - y: 37
X - x: 73

Variables: 
---------- 
================================================================
copy_dimvar(gdat, dim, chk_in=None, chk_out=None)[source]

Copy the variables associated with the dimensions

copy_dimvarstruct(gdat, dim)[source]

Create and copy the structure of a dimension variable.

copy_varstruct(gdat, gvar_in, gvar_out=None)[source]

Copy the structure of the variable in the geodata instance geodat to a new variable in self

create_coord(dtype='f4', fill_value=-9999.0)[source]

Create all the dimension variables in the netCDF file

create_dim(chk=None)[source]

Create the dimensions in the netCDF file from a chunk

create_dimvar(dim, dtype='f4', fill_value=-9999.0)[source]

Create the dimension variables in the netCDF file

create_var(gvar)[source]

Create the variable with its attributes in the netCDF file

Parameters:

gvar : geovar or string

The variable to create in the netCDF file defined by a string or a complex object geovar

extract(gdat, list_gvar_in, list_gvar_out=None, chk_in=None, chk_out=None)[source]

Extract the structure and the variables of a netCDF file.

Parameters:

gdat : geodata

Input data from which the extraction will be made

list_gvar_in : list of string or geovar

Names of the variables to be copied

list_gvar_out: list of string or geovar, optional

Names of the variable that will be copied in the output file. The same names as the input variables are used by default.

chk_in : chunk, optional

Define the chunk to read in the input file for the extraction

chk_out : chunk, optional

Define the chunk to write the variable in the output file extraction

fill_value : float, file

The fill_value that will be used for the variables in the output netCDF file

extract_struct(gdat, list_gvar_in, list_gvar_out=None, chk_in=None, chk_out=None)[source]

Extract the structure of a netCDF file.

Parameters:

gdat : geodata

Input data from which the extraction will be made

list_gvar_in : list of string or geovar

Names of the variables of the input netCDF file for which the structure will be copied

list_gvar_out: list of string or geovar, optional

Names of the variables of the output netCDF file in which the structure will be copied

chk_in : chunk, optional

Define the chunk to read in the input file for the extraction

chk_out : chunk, optional

Define the chunk to write the variable in the output file extraction

fill_value : float, file

The fill_value that will be used for the variables in the output netCDF file

get_depth(chk=None)[source]

Return the vertical 1d array, optionally a subset defined by a chunk

Parameters:

chk : chunk, optional

Define the chunk to read in the NetCDF file

Returns:

depth : 1darray

The depth 1d array read in the NetCDF file

get_dim(dim)[source]

Get the size of a dimension int the netCDF4 instance nc_in along one dimension (T, Z, Y or X)

get_dim_depth()[source]

Return the dimension of the vertical axis

Returns:

n_z : int

The dimension of the vertical axis

get_dim_lat()[source]

Return the dimension of the latitudinal axis

Returns:

n_lat : int

The dimension of the latitudinal axis

get_dim_latlon()[source]

Return the dimension of the latitudinal and longitudinal axis

Returns:

n_lon : int

The dimension of the longitudinal axis

n_lat : int

The dimension of the latitudinal axis

get_dim_lon()[source]

Return the dimension of the longitudinal axis

Returns:

n_lon : int

The dimension of the longitudinal axis

get_dim_time()[source]

Get the dimension of the time axis

Returns:

n_t : int

The dimension of the time axis

get_dimensions()[source]

Return the dimensions

get_dimvar(dim, chk=None)[source]

Return the coordinates associated with the dimension dim from the netCDF4 instance nc_in or only a subset optionally defined by a chunk instance

get_dtype()[source]

Return the associated type of geodata

get_filename()[source]

Return the filename

get_lat(chk=None)[source]

Return the latitudinal 1d/2d array, optionally a subset defined by a chunk chk

Parameters:

chk : chunk, optional

Define the chunk to read in the NetCDF file

Returns:

lat : 1darray/2darray

The latitude 1d/2d array read in the NetCDF file

get_latlon(chk=None)[source]

Return the latitudinal and longitudinal 1d/2d array, optionally a subset defined by a chunk

Parameters:

chk : chunk, optional

Define the chunk to read in the NetCDF file

Returns:

lat : 1darray/2darray

The latitude 1d/2d array read in the NetCDF file

lon : 1darray/2darray

The longitude 1d/2d array read in the NetCDF file

get_lon(chk=None)[source]
Return the longitudinal 1d/2d array, optionally a subset
defined by a chunk
Parameters:

chk : chunk, optional

Define the chunk to read in the NetCDF file

Returns:

lon : 1darray/2darray

The longitude 1d/2d array read in the NetCDF file

get_nc()[source]

Return the associated netCDF4 instance

get_time(chk=None)[source]

Return the time record, optionally a subset defined by a chunk

Parameters:

chk : chunk, optional

Define the chunk to read in the NetCDF file

Returns:

time : 1darray

The time 1d/2d array read in the NetCDF file

get_var(gvar, chk=None, dtype='f8')[source]

Read a variable in the netCDF file

Parameters:

gvar : string or geovar

Input variable can only be described by its name or by a a more complex instance geovar

chk : chunk, optional

Specify a subset to read, all the data is read by default

dtype : data-type, optional

The desired data-type of the numpy array in which the data is stored. Default is double precision

Returns:

out : ndarray

A numpy array of the dimension of the variable, note that dimensions of length 1 are removed.

get_var_FillValue(gvar)[source]

Return the value of the FillValue attribute

Parameters:

gvar : string or geovar

Input variable can only be described by its name or by a a more complex instance geovar

attr_name : string

Name of the variable attribute in the NetCDF file

Returns:

out : unknown

The value of the attribute

get_var_attr(gvar, attr_name)[source]

Return the value of the variable attribute

Parameters:

gvar : string or geovar

Input variable can only be described by its name or by a a more complex instance geovar

attr_name : string

Name of the variable attribute in the NetCDF file

Returns:

out : unknown

The value of the attribute

get_var_axis(gvar)[source]

Return the axis attribute of the variable in the NetCDF file

Parameters:

gvar : string or geovar

Input variable can only be described by its name or by a a more complex instance geovar

Returns:

axis : string

The axis of the variable containing which is a combination of the available axis {‘T’, ‘Z’, ‘Y’, ‘X’}

get_var_dtype(gvar)[source]

Return the type of the variable in the netcdf file

Parameters:

gvar : string or geovar

Input variable can only be described by its name or by a a more complex instance geovar

Returns:

out : string

The type of the variable in the standard convention (ex: ‘f4’)

get_var_names()[source]

Return the list of the variable names without the dimension variables

Returns:

out : list of string

A list of string that contains the variable names

nc_create()[source]

Create the netCDF file

save_depth(depth, chk=None)[source]

Write the values of the vertical variable in the netCDF file

save_dimvar(dim, value, chk=None)[source]

Write the values of a dimension variable in the netCDF file

save_lat(lat, chk=None)[source]

Write the values of the latitudinal variable in the netCDF file

save_latlon(lat, lon, chk=None)[source]

Write the values of latidudinal and longitudinal variable in the netCDF file

save_lon(lon, chk=None)[source]

Write the values of the longitudinal variable in the netCDF file

save_time(time, chk=None)[source]

Write the values of the time variable in the netCDF file

save_var(gvar, values, chk=None)[source]

Write the variable values in the corresponding variable of the netCDF file

Previous topic

Geovar

Next topic

Calculation

This Page