scipy.io.netcdf.netcdf_file¶
- class scipy.io.netcdf.netcdf_file(filename, mode='r', mmap=None, version=1)[source]¶
A file object for NetCDF data.
A netcdf_file 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 netcdf_file object.
Parameters: filename : string or file-like
string -> filename
mode : {‘r’, ‘w’}, optional
read-write mode, default is ‘r’
mmap : None or bool, optional
Whether to mmap filename when reading. Default is True when filename is a file name, False when filename is a file-like object
version : {1, 2}, optional
version of netcdf to read / write, where 1 means Classic format and 2 means 64-bit offset format. Default is 1. See here for more info.
Notes
The major advantage of this module over other modules is that it doesn’t require the code to be linked to the NetCDF libraries. This module is derived from pupynere.
NetCDF files are a self-describing binary data format. The file contains metadata that describes the dimensions and variables in the file. More details about NetCDF files can be found here. There are three main sections to a NetCDF data structure:
- Dimensions
- Variables
- Attributes
The dimensions section records the name and length of each dimension used by the variables. The variables would then indicate which dimensions it uses and any attributes such as data units, along with containing the data values for the variable. It is good practice to include a variable that is the same name as a dimension to provide the values for that axes. Lastly, the attributes section would contain additional information such as the name of the file creator or the instrument used to collect the data.
When writing data to a NetCDF file, there is often the need to indicate the ‘record dimension’. A record dimension is the unbounded dimension for a variable. For example, a temperature variable may have dimensions of latitude, longitude and time. If one wants to add more temperature data to the NetCDF file as time progresses, then the temperature variable should have the time dimension flagged as the record dimension.
In addition, the NetCDF file header contains the position of the data in the file, so access can be done in an efficient manner without loading unnecessary data into memory. It uses the mmap module to create Numpy arrays mapped to the data on disk, for the same purpose.
Examples
To create a NetCDF file:
>>> from scipy.io import netcdf >>> f = netcdf.netcdf_file('simple.nc', 'w') >>> f.history = 'Created for a test' >>> f.createDimension('time', 10) >>> time = f.createVariable('time', 'i', ('time',)) >>> time[:] = np.arange(10) >>> time.units = 'days since 2008-01-01' >>> f.close()
Note the assignment of range(10) to time[:]. Exposing the slice of the time variable allows for the data to be set in the object, rather than letting range(10) overwrite the time variable.
To read the NetCDF file we just created:
>>> from scipy.io import netcdf >>> f = netcdf.netcdf_file('simple.nc', 'r') >>> print(f.history) Created for a test >>> time = f.variables['time'] >>> print(time.units) days since 2008-01-01 >>> print(time.shape) (10,) >>> print(time[-1]) 9 >>> f.close()
A NetCDF file can also be used as context manager:
>>> from scipy.io import netcdf >>> with netcdf.netcdf_file('simple.nc', 'r') as f: >>> print(f.history) Created for a test
Methods
close() Closes the NetCDF file. createDimension(name, length) Adds a dimension to the Dimension section of the NetCDF data structure. createVariable(name, type, dimensions) Create an empty variable for the netcdf_file object, specifying its data type and the dimensions it uses. flush() Perform a sync-to-disk flush if the netcdf_file object is in write mode. sync() Perform a sync-to-disk flush if the netcdf_file object is in write mode.