ncdata.netcdf4 module#
Interface routines for converting data between ncdata and netCDF4.
Converts ncdata.NcData
to and from netCDF4.Dataset
objects.
- ncdata.netcdf4.from_nc4(nc4_dataset_or_file, dim_chunks=None)#
Load NcData from a
netCDF4.Dataset
or netCDF file.- Parameters:
nc4_dataset_or_file (Dataset | Group | Path | str) – source of load data. Can be either a
netCDF4.Dataset
, anetCDF4.Group
, apathlib.Path
or a string.dim_chunks (Dict[str, int | str]) – a dictionary of chunk sizes (number, or -1 or “auto”) for specific dimensions, specified by dimension name. Defaults to “auto” for all unspecified dimensions.
- Returns:
ncdata
- Return type:
Examples
For example, to avoid cases where a simple dask
from_array(chunks="auto")
will fail>>> from ncdata.netcdf4 import from_nc4 >>> from tests import testdata_dir >>> path = testdata_dir / "toa_brightness_temperature.nc" >>> ds = from_nc4(path, dim_chunks={"x": 15}) >>> ds.variables["data"].data.chunksize (160, 15) >>>
See also : Load a file containing variable-width string variables : This illustrates a particular case which does encounter an error with dask “auto” chunking, and therefore also fails with a plain “from_nc4” call. The
dim_chunks
keyword enables you to work around the problem.
- ncdata.netcdf4.to_nc4(ncdata, nc4_dataset_or_file, var_kwargs=None)#
Save NcData to a netCDF file.
- Parameters:
ncdata (NcData) – input data
nc4_dataset_or_file (
netCDF4.Dataset
orpathlib.Path
or str) – output filepath ornetCDF4.Dataset
to write intovar_kwargs (dict or None) –
additional keys for variable creation. If present, each entry
var_kwargs[<var_name>]
is itself a dictionary, containing entries of the form<kwarg_name>: <kwarg_value>
, which specify additional keyword controls passed tonetCDF4.Dataset.createVariable()
for this variable. This allows control of e.g. compression or chunking.Keyword controls for variables in a sub-group can be given in a
var_kwargs['/<group-name>']
entry, which is itself avar_kwargs
- like dictionary.Note: this must not include keywords controlled by the
to_nc4
operation itself. That is, any of : [“data”, “dimensions”, “datatype”, “fill_value”].
- Return type:
None
Notes
If a filepath is provided, a file is written and closed afterwards. If a dataset is provided, it must be writeable and remains open afterward.