The netCDF_components_consolidator Module

Given a NetCDF file with absolute values (e.g., for PM25) and a set of NetCDF files containing percentage values for individual components, this tool consolidates all data into a single NetCDF file with both percentages and absolute values for all components.

See also:

https://unidata.github.io/netcdf4-python/ https://neetinayak.medium.com/combine-many-netcdf-files-into-a-single-file-with-python-469ba476fc14 https://docs.xarray.dev/en/stable/api.html

class NetCDFDataset[source]

Class to combine NetCDF dataset with absolute values with dependent datasets containing components

dataset: Optional[Dataset]

NetCDF Dataset that we will be modifying

rio_dataset

Rasterio dataset that holds metadata lost when writing NetCDF dataset

main_var: Optional[str]

The name of the main variable

percentages: List[str]

The names of the component variables containing percentages

abs_values: List[str]

The names of the component variables containing absolute values

open_original_dataset(infile)[source]
read_abs_values(filename: str, var: Optional[str] = None)[source]

Reads the NetCDF dataset from a *.nc file Assumes that this dataset contains absolute values of the variable with the name provided by var parameter.

Raises an exception if the variable is not None but is not present n the dataset. If the parameter “var” is None, checks that there is only one variable present beside “lat” and “lon”. Raises exception if there is more than one variable

Parameters:
  • var – The variable containing the absolute values of the feature of interest, e.g., “pm25” If None, defaults to a single variable present in teh dataset beside “lat” and “lon”

  • filename – A path to file to read. Can also be a python 3 pathlib instance or the URL of an OpenDAP dataset.

Raises:

ValueError if var is None and there is more than one variable in the dataset or, if var is not None and is not present in teh dataset

classmethod pct_var(var: str) str[source]
add_component(filename: str, var: Optional[str] = None)[source]

Reads the NetCDF dataset from a *.nc file Assumes that this dataset contains percentage of a component defined by the var parameter.

Can only be called after the dataset is initialized with absolute values.

Parameters:
  • var – The variable containing percentage of a certain component

  • filename – A path to file to read. Can also be a python 3 pathlib instance or the URL of an OpenDAP dataset.

Raises:

ValueError if var is None and there is more than one variable in the dataset or, if var is not None and is not present in the dataset

Raises:

ValueError if the grid of the component file is incompatible with the gird of the existing Dataset

Raises:

ValueError if the absolute values have not yet been read

add_components(filenames: List[str])[source]

Adds multiple components in a single call from multiple files. Assumes that every file given contains only one variable beside lat and lon

Can only be called after the dataset is initialized with absolute values.

Parameters:

filenames – A list of file paths to read. Elements of the list can also be a python 3 pathlib instance or the URL of an OpenDAP dataset.

Raises:

ValueError if there is more than one variable in any of the datasets

Raises:

ValueError if the grid of a component file is incompatible with the gird of the existing Dataset

Raises:

ValueError if the absolute values have not yet been read

compute_abs_values()[source]

Computes absolute values for every component present in the dataset by applying a formula.

Parameters:

components – Array of component names

Returns:

None

Raises:

ValueError if the absolute values have not yet been read

get_dataset() Dataset[source]
close(output_file_name)[source]

Writes all the data to the output file and closes all temporary objects

Returns:

None

main(infile: str, components: List[str], outfile: str)[source]