Transform#

Functions that can convert from underlying databases or model outputs into the standardised OpenGHG format. This could include, for example, creating a Flux file for a limited domain based on data from the EDGAR database. In constrast to standardisation functions, this will usually include some amount of transformation such as selection and/or regridding.

Regridding#

openghg.transform.regrid_uniform_cc(data, lat_out, lon_out, lat_in=None, lon_in=None, latlon=None, method='conservative')[source]#

Regrid data between two uniform, cell centered grids. All coordinates (lat_out, lon_out, lat_in, lon_in) should be for the centre of the representative cell and in degrees.

Adapted from code written by @DTHoare

Parameters:
  • data (TypeVar(ArrayLikeMatch, ndarray, DataArray)) – Data to be regridded. Data must have dimensions (lat, lon) if 2D or (time, lat, lon) if 3D.

  • lat_out (ndarray | DataArray) – 1D array for output latitude grid

  • lon_out (ndarray | DataArray) – 1D array for output longituide grid

  • lat_in (ndarray | DataArray | None) – 1D array for input latitude grid. Only used if data is a numpy array and not a DataArray

  • lon_in (ndarray | DataArray | None) – 1D array for input longitude grid. Only used if data is a numpy array and not a DataArray

  • latlon (list | None) – Names for latitude and longitude coordinates within data. Default = [“lat”, “lon”]

  • method (str) – Method to use for regridding. Mainly use: - “conservative” - “conservative_normed” (ignores NaN values) See xesmf documentation for full list of options.

Returns:

Regridded data using specified method

Return type:

ndarray / DataArray

Transform entry points#

Transform emissions or boundary-condition data and store the result.

openghg.transform.transform_flux_data(datapath=None, database=None, overwrite=False, store=None, data=None, **kwargs)[source]#

Transform raw flux data and write the result to an object store.

The database name selects a parser such as openghg.transform.flux.parse_edgar(). Exactly one of datapath and data must be supplied. Inputs are validated before the writable object store is acquired.

Parameters:
  • datapath (str | Path | None) – Path to a local database directory or archive.

  • database (str | None) – Name of the supported flux database to transform.

  • overwrite (bool) – Whether matching stored data should be overwritten.

  • store (str | None) – Name of the object store to write data to.

  • data (Dataset | None) – Raw in-memory dataset to transform instead of reading a path.

  • **kwargs (Any) – Inputs for underlying parser function for the database. Necessary inputs will depend on the database being parsed.

Return type:

list[dict]

Returns:

Metadata dictionaries identifying the stored datasources.

Raises:

ValueError – If database is unsupported or exactly one of datapath and data is not supplied.

openghg.transform.transform_bc_data(datapath=None, database=None, overwrite=False, store=None, data=None, **kwargs)[source]#

Transform raw boundary-condition data and store the result.

The database name selects a parser such as openghg.transform.boundary_conditions.parse_cams(). Exactly one of datapath and data must be supplied. Inputs are validated before the writable object store is acquired.

Parameters:
  • datapath (str | Path | None) – Path to local raw boundary-condition data.

  • database (str | None) – Name of the supported boundary-condition database.

  • overwrite (bool) – Whether matching stored data should be overwritten.

  • store (str | None) – Name of the object store to write data to.

  • data (Dataset | None) – Raw in-memory dataset to transform instead of reading a path.

  • **kwargs (Any) – Inputs for underlying parser function for the database. Necessary inputs will depend on the database being parsed.

Return type:

list[dict]

Returns:

Metadata dictionaries identifying the stored datasources.

Raises:

ValueError – If database is unsupported or exactly one of datapath and data is not supplied.

Database parsers#

openghg.transform.flux.parse_edgar(datapath=None, date=None, species=None, domain=None, lat_out=None, lon_out=None, source=None, edgar_version=None, data=None)[source]#

Read and transform raw EDGAR emissions data.

EDGAR data is global on a 0.1 x 0.1 grid. This function allows products to be created for a given year which cover specific regions (and matches to the OpenGHG data schema, including units and coordinate names).

Region information can be specified by using domain for a predefined domain, supplying domain, lat_out, and lon_out for a new domain, or omitting all three to use the globaledgar domain.

Pre-exisiting domains are defined within the openghg_defs “domain_info.json” file.

Stored metadata includes the domain, source, EDGAR database identifier, and database version.

Parameters:
  • datapath (Path | None) – Path to an EDGAR data folder or zip archive. Exactly one of datapath and data must be supplied.

  • date (str | int | None) – Year to extract as a four-digit string or integer. For direct data this is inferred from time when omitted and validated against time when provided.

  • species (str | None) – Species name being extracted.

  • domain (str | None) – Domain name for a new or pre-existing domain.

  • lat_out (ndarray | DataArray | None) – Latitude values for a new domain.

  • lon_out (ndarray | DataArray | None) – Longitude values for a new domain.

  • source (str | None) – Flux source to use; overrides the source extracted from the filename.

  • edgar_version (str | None) – EDGAR version in file. Will be inferred otherwise.

  • data (Dataset | None) – Raw EDGAR dataset containing emi_<species> for pre-v8 data or fluxes for v8-style data. Values are interpreted as kg/m2/s and converted to mol/m2/s.

Return type:

dict

Returns:

Dictionary containing transformed data, metadata, and attributes.

Raises:

ValueError – If exactly one input source is not supplied, required metadata cannot be determined, the date is invalid, or no supported raw EDGAR variable is present.

TODO: Allow date range to be extracted rather than year? TODO: Add sector stacking option

openghg.transform.boundary_conditions.parse_cams(bc_input, domain, datapath=None, species=None, period=None, cams_version=None, input_observations=None, get_footprint_kwargs=None, continuous=True, chunks=None, data=None)[source]#

Parse raw CAMS boundary conditions and add data and metadata.

Parameters:
  • bc_input (str) – Input used to create boundary conditions. For example: - a model name and version and period such as “cams_v24r1_daily” - a description such as “cams_uniform_mixedversion_daily” (uniform values based on CAMS average from a mix of version at daily resolution) Advice is to always put cams to state the model, as well as info on the cams version used and period, even though this will be put in the metadata.

  • domain (str) – Region for boundary conditions

  • datapath (str | Path | list[str] | list[Path] | None) – Path or paths containing raw boundary-condition data. Specify either datapath or data.

  • data (Dataset | None) – In-memory raw CAMS dataset. Specify either datapath or data.

  • species (str | None) – Species name

  • period (str | None) – period at which at resample and store the data

  • cams_version (str | None) – cams version to use. Put ‘mix’ if you want to use files from multiple cams versions.

  • input_observations (str | None) – input observations used to make the cams file (e.g. “surface_satellite_dm”). Put ‘mix’ if you want to use files from multiple input observations.

  • get_footprint_kwargs (dict | None) – arguments passed to openghg.retrieve.get_footprint to get the grid that that will be used to store the data

  • continuous (bool) – whether time stamps have to be continuous

  • chunks (dict | None) – Chunking schema to use when storing data. It expects a dictionary of dimension name and chunk size, for example {“time”: 100}. If None then a chunking schema will be set automatically by OpenGHG. See documentation for guidance on chunking: https://docs.openghg.org/tutorials/local/Adding_data/Adding_ancillary_data.html#chunking. To disable chunking pass in an empty dictionary.

Returns:

Dictionary of “species_bc_input_domain” : data, metadata, attributes

Return type:

Dict

Raises:

ValueError – If exactly one input is not supplied or the species or units cannot be determined from direct data.