satpy.readers.netcdf_utils module

Helpers for reading netcdf-based files.

class satpy.readers.netcdf_utils.NetCDF4FileHandler(filename, filename_info, filetype_info, auto_maskandscale=False, xarray_kwargs=None, cache_var_size=0, cache_handle=False)[source]

Bases: BaseFileHandler

Small class for inspecting a NetCDF4 file and retrieving its metadata/header data.

File information can be accessed using bracket notation. Variables are accessed by using:




Attributes can be accessed by appending “/attr/attr_name” to the item string:


Or for global attributes:


Or for all of global attributes:


Note that loading datasets requires reopening the original file (unless those datasets are cached, see below), but to get just the shape of the dataset append “/shape” to the item string:


If your file has many small data variables that are frequently accessed, you may choose to cache some of them. You can do this by passing a number, any variable smaller than this number in bytes will be read into RAM. Warning, this part of the API is provisional and subject to change.

You may get an additional speedup by passing cache_handle=True. This will keep the netCDF4 dataset handles open throughout the lifetime of the object, and instead of using xarray.open_dataset to open every data variable, a dask array will be created “manually”. This may be useful if you have a dataset distributed over many files, such as for FCI. Note that the coordinates will be missing in this case. If you use this option, xarray_kwargs will have no effect.

  • filename (str) – File to read

  • filename_info (dict) – Dictionary with filename information

  • filetype_info (dict) – Dictionary with filetype information

  • auto_maskandscale (bool) – Apply mask and scale factors

  • xarray_kwargs (dict) – Addition arguments to xarray.open_dataset

  • cache_var_size (int) – Cache variables smaller than this size.

  • cache_handle (bool) – Keep files open for lifetime of filehandler.

Initialize object.

_collect_attrs(name, obj)[source]

Collect all the attributes for the provided file object.


Collect all the global attributes for the provided file object.

_collect_groups_info(base_name, obj)[source]
_collect_listed_variables(file_handle, listed_variables)[source]
_collect_variable_info(var_name, var_obj)[source]
_collect_variables_info(base_name, obj)[source]
_get_attr(obj, key)[source]
_get_attr_value(obj, key)[source]
_get_group(key, val)[source]

Get a group from the netcdf file.

static _get_required_variable_names(listed_variables, variable_name_replacements)[source]
_get_var_from_filehandle(group, key)[source]
_get_var_from_xr(group, key)[source]
_get_variable(key, val)[source]

Get a variable from the netcdf file.

static _set_file_handle_auto_maskandscale(file_handle, auto_maskandscale)[source]
_set_xarray_kwargs(xarray_kwargs, auto_maskandscale)[source]

Collect data variables for caching.

This method will collect some data variables and store them in RAM. This may be useful if some small variables are frequently accessed, to prevent needlessly frequently opening and closing the file, which in case of xarray is associated with some overhead.

Should be called later than collect_metadata.


cache_var_size (int) – Maximum size of the collected variables in bytes

collect_dimensions(name, obj)[source]

Collect dimensions.

collect_metadata(name, obj)[source]

Collect all file variables and attributes for the provided file object.

This method also iterates through subgroups of the provided object.

file_handle = None
get(item, default=None)[source]

Get item.


Get and cache variable as DataArray[numpy].

class satpy.readers.netcdf_utils.NetCDF4FsspecFileHandler(filename, filename_info, filetype_info, auto_maskandscale=False, xarray_kwargs=None, cache_var_size=0, cache_handle=False)[source]

Bases: NetCDF4FileHandler

NetCDF4 file handler using fsspec to read files remotely.

Initialize object.

_get_attr(obj, key)[source]
satpy.readers.netcdf_utils._compose_replacement_names(variable_name_replacements, var, variable_names)[source]

Get data in variable as xr.DataArray.