scmdata.netcdf

NetCDF4 file operations

Reading and writing ScmRun to disk as binary

run_to_nc

run_to_nc(run, fname, dimensions=('region',), extras=(), **kwargs)[source]

Write timeseries to disk as a netCDF4 file

Each unique variable will be written as a variable within the netCDF file. Choosing the dimensions and extras such that there are as few empty (or nan) values as possible will lead to the best compression on disk.

Parameters:
  • fname (str | pathlib.Path) – Path to write the file into

  • dimensions (iterable of str) – Dimensions to include in the netCDF file. The time dimension is always included (if not provided it will be the last dimension). An additional dimension (specifically a co-ordinate in xarray terms), “_id”, will be included if extras is provided and any of the metadata in extras is not uniquely defined by dimensions. “_id” maps the timeseries in each variable to their relevant metadata.

  • extras (iterable of str) – Metadata columns to write as variables in the netCDF file (specifically as “non-dimension co-ordinates” in xarray terms, see xarray terminology for more details). Where possible, these non-dimension co-ordinates will use dimension co-ordinates as their own co-ordinates. However, if the metadata in extras is not defined by a single dimension in dimensions, then the extras co-ordinates will have dimensions of “_id”. This “_id” co-ordinate maps the values in the extras co-ordinates to each timeseries in the serialised dataset. Where “_id” is required, an extra “_id” dimension will also be added to dimensions.

  • kwargs (Any) – Passed through to xarray.Dataset.to_netcdf()

Return type:

None

See also

scmdata.run.ScmRun.to_nc()

nc_to_run

nc_to_run(cls, fname)[source]

Read a netCDF4 file from disk

Parameters:

fname (str) – Filename to read

Return type:

BaseScmRun

See also

scmdata.run.ScmRun.from_nc()

inject_nc_methods

inject_nc_methods(cls)[source]

Add the to/from nc methods to a class

Parameters:

cls (type[scmdata.run.BaseScmRun]) – Class to add methods to

Return type:

None