Package Scientific :: Package IO :: Module NetCDF :: Class NetCDFFile

[frames] | no frames]

# Class NetCDFFile

netCDF file

A NetCDFFile object has two standard attributes: 'dimensions' and 'variables'. The values of both are dictionaries, mapping dimension names to their associated lengths and variable names to variables, respectively. Application programs should never modify these dictionaries.

All other attributes correspond to global attributes defined in the netCDF file. Global file attributes are created by assigning to an attribute of the NetCDFFile object.

Instance Methods

__init__(self, filename, mode) |

close(self) Close the file. |

createDimension(self, name, length) |

NetCDFVariable

createVariable(self, name, type, dimensions) Returns: the object corresponding to the new variable |

flush(self) Write all buffered data to the disk file. |

sync(self) Write all buffered data to the disk file. |

Method Details

### __init__(self, filename, mode)
(Constructor)

Parameters:
  - `filename` (`str`) - name of the netCDF file. By convention, netCDF files have the extension ".nc", but this is not enforced. The filename may contain a home directory indication starting with "~".
  - `mode` (`str`) - access mode. "r" means read-only; no data can be modified. "w" means write; a new file is created, an existing file with the same name is deleted. "a" means append (in analogy with serial files); an existing file is opened for reading and writing, if the file does not exist it is created. "r+" is similar to "a", but the file must already exist. An "s" can be appended to any of the modes listed above; it indicates that the file will be opened or created in "share" mode, which reduces buffering in order to permit simultaneous read access by other processes to a file that is being written. When creating a file in write mode, an "l" or "4" can be appended to use the large-file format (introduced with netCDF 3.6) or the HDF5 format (introduced with netCDF 4).

### close(self)

Close the file. Any read or write access to the file or one of its variables after closing raises an exception.

### createDimension(self, name, length)

Parameters:
  - `name` (`str`) - the name of the dimension
  - `length` (`int` or `NoneType`) - the length of the new dimension. `None` stands for the unlimited dimension. Note that there can be only one unlimited dimension per file.

### createVariable(self, name, type, dimensions)

Parameters:
  - `name` (`str`) - the name of the new variable
  - `type` (`str`) - the data type of the elements; the same one-letter codes as in `Numeric` are used and the use of the pre-defined constants (Numeric.Float etc.) is strongly recommended.
  - `dimensions` (`tuple` of `str`) - a tuple of dimension names that have been defined earlier
Returns: NetCDFVariable
  the object corresponding to the new variable