NEDAS.utils.netcdf_lib module

NEDAS.utils.netcdf_lib.nc_open(filename: str, mode: Literal['r', 'w', 'a', 'r+'], comm: Comm | None = None) Dataset[source]

Open a netCDF file.

Parameters:
  • filename (str) – Path to the netCDF file.

  • mode (str) – Open mode, (e.g. 'r', 'w').

  • comm (Comm, optional) – MPI communicator object. If None, open the netCDF4.Dataset normally. If communicator is available, try parallel=True open when opening the file. If that’s not supported, acquire a file lock in communicator for blocking serial access of the file.

Returns:

netCDF file handle.

Return type:

netCDF4.Dataset

NEDAS.utils.netcdf_lib.nc_close(filename: str, f: Dataset, comm: Comm | None = None) None[source]

Close the netCDF file handle.

Parameters:
  • filename (str) – Path to the opened netCDF file.

  • f (netCDF4.Dataset) – netCDF4 Dataset file handle.

  • comm (Comm, optional) – MPI communicator object. If None, just close the file directly. If communicator is specified, release the file lock after closing it.

NEDAS.utils.netcdf_lib.nc_write_var(filename: str, dim: Mapping[str, int | None], varname: str, dat: ndarray, dtype: str | None = None, recno: dict[str, int] | None = None, attr: dict[str, str] | None = None, comm: Comm | None = None) None[source]

Write a variable to a netCDF file.

Parameters:
  • filename (str) – Path to the output netCDF file.

  • dim (dict) – Dictionary {dimension name (str): dimension size (int)} of each dimension. The dimension size can be None if it is unlimited dimension (can append more records afterwards)

  • varname (str) – Name of the output variable. Variable groups are supported, use 'group/varname' as varname.

  • dat (np.ndarray) – Data for output, number of its dimensions must match dim (excluding unlimited dimensions).

  • dtype (str, optional) – Data type to convert the input data to.

  • recno (dict, optional) – Dictionary {dimension name (str): record number (int)}, indicating the index in unlimited dimensions for the data to be written to. Each unlimited dimension defined in dim should have a corresponding recno entry.

  • attr (dict, optional) – Dictionary {name of attribute (str): value (str)}, additional attributes to be added.

  • comm (Comm, optional) – MPI communicator object, handling parallel I/O and make sure thread-safe writing of data.

NEDAS.utils.netcdf_lib.nc_read_var(filename: str, varname: str, comm: Comm | None = None) ndarray[source]

Read a variable from a netCDF file.

This function by default reads the entire variable, if you only want a slice, it is more efficient to use netCDF4.Dataset handle directly.

Parameters:
  • filename (str) – Path to the netCDF file for reading.

  • varname (str) – Name of the variable to read.

  • comm (Comm, optional) – MPI communicator object.

Returns:

Variable read from the file.

Return type:

np.ndarray