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)
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_kwargswill 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.
- collect_cache_vars(cache_vars, obj)
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_vars (List[str]) – Names of data variables to be cached.
obj (netCDF4.Dataset) – Dataset object from which to read them.
- collect_dimensions(name, obj)
- collect_metadata(name, obj)
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)