NetCDF utilities

NetCDF is a standard format you can use as input for NadirData objects.
Most of the time xarray load_dataset(), open_dataset() or open_mfdataset() functions will be enough to handle your NetCDf file or set of files.
Casys Toolbox provides two additional utility functions to explore and read NetCDF files containing groups and sub-groups of data.

nc_content

The nc_content() function allows you to display the content of a netCDF file.
List the content of netCDF files including its subgroups.

Parameters
----------
paths
    NetCDF file to explore (use the first file if a pattern is provided).
show_vars
    Whether to list variables or not.
show_dims
    Whether to list dimensions or not.
show_attrs
    Whether to list attributes or not.

Returns
-------
:
    List of groups and subgroups associated with their content.

Example showing the content of some netCDF files.

from casys.utilities import nc_content

files = "/path/to/JA3_GPN_2PfP057_*.nc"
nc_content(paths=files, show_vars=False, show_dims=True, show_attrs=False)

{'/': {'variables': 0, 'dimensions': [], 'attrs': 41},
 '/data_01': {'variables': 83, 'dimensions': ['time'], 'attrs': 0},
 '/data_01/ku': {'variables': 75, 'dimensions': [], 'attrs': 0},
 '/data_01/c': {'variables': 27, 'dimensions': [], 'attrs': 0},
 '/data_20': {'variables': 15, 'dimensions': ['time'], 'attrs': 0},
 '/data_20/ku': {'variables': 31, 'dimensions': [], 'attrs': 0},
 '/data_20/c': {'variables': 11, 'dimensions': [], 'attrs': 0}
}

open_grdataset

The open_grdataset() function allows you to open the content of a single or a set of NetCDF files including groups and sub-groups as well as dimensions which might be included in upper-groups.
open_grdataset() is build using open_mfdataset() and allows you to benefit from the delayed loading as well as Dask parallel loading.
Alternative function to xarray.open_mfdataset allowing to open a group
with its dimensions even if included in a parent group.

Subgroups might be included with their variables renamed by prefixing them with
their group path.

Parameters
----------
paths
    NetCDF file(s) to read.
group
    Name/path of the group to open.
sub_groups
    Whether to include subgroups or not.
separator
    Separator to use when renaming group fields (default to ".").
pre_kwargs
    Additional xarray.open_dataset parameters used during the preprocessing.
mf_kwargs
    Additional xarray.open_mfdataset parameters.

Returns
-------
:
    xarray.Dataset.
Examples illustrating open_grdataset() possibilities when working with a set of netCDF.
from casys.utilities import open_grdataset

files = "/path/to/JA3_GPN_2PfP057_*.nc"

# Opening a single subgroup
# time dimension, even if not included in the subgroup itself, will be included
# in the resulting dataset.
ds_01_ku = open_grdataset(paths=files, group="data_01/ku")

# Opening a subgroup with its subgroups (ku/c)
ds_01 = open_grdataset(paths=files, group="data_01")

# Opening a subgroup with its subgroups using a Dask cluster (if one is active)
ds_20 = open_grdataset(paths=files, group="data_20", mf_kwargs={"parallel": True})

# Opening the whole netCDF structure including data_01, data_20 and their respective
# subgroups
ds = open_grdataset(paths=files, mf_kwargs={"parallel": True})