openghg_inversions.postprocessing.countries#
Module with code related to country maps.
- class openghg_inversions.postprocessing.countries.Countries(countries: Dataset, country_selections: list[str] | None = None, country_code: Literal['alpha2', 'alpha3'] | None = None, country_regions: dict[str, list[str]] | str | Path | None = None)#
Bases:
objectClass to load country files and create country traces.
A list of specifying a subset of the countries in the country file can be provided. Multiple Country objects can be merged together to use multiple country files.
- classmethod from_file(country_file: str | Path | None = None, domain: str | None = None, country_selections: list[str] | None = None, country_code: Literal['alpha2', 'alpha3'] | None = None, country_regions: dict[str, list[str]] | str | Path | None = None) Self#
Create Countries object given country map Dataset and optional list of countries to select.
- Parameters:
country_file – path to country file
domain – used to select country file from openghg_inversions/countries if country_file is None.
country_selections – optional list of country names to select.
country_code – if not None, convert country names to specified codes. These names or codes will be used in the country coordinate.
country_regions – dict mapping country region names (e.g. “BENELUX”) to a list of (country codes) of the countries comprising that regions (e.g. [“BEL”, “NLD”, “LUX”]). Alternatively, a path (or string representing a path) to a JSON file with a similar specification can be passed.
- get_country_trace(inv_out: InversionOutput) Dataset#
Calculate trace(s) for total country emissions.
- Parameters:
species – name of species, e.g. “co2”, “ch4”, “sf6”, etc.
inv_out – InversionOutput
- Returns:
xr.Dataset with coordinate dimensions (“country”, “draw”)
- TODO: there is a “country unit” conversion in the old code, but it seems to always product
1.0, based on how it is used in hbmcmc
- get_x_to_country_mat(inv_out: InversionOutput, sparse: bool = False) DataArray#
Construct a sparse matrix mapping from x sensitivities to country totals.
- Parameters:
inv_out – InversionOutput object, used to get basis functions and flux.
sparse – if True, values of returned DataArray are sparse.COO array.
- Returns:
xr.DataArray with coordinate dimensions (“country”, “basis_region”)
- class openghg_inversions.postprocessing.countries.CountryRegions(country_regions: Mapping[str, Sequence[str]] | None = None)#
Bases:
objectRegions defined by combining several countries.
- align(country_list: CountryInfoList) Self#
Return CountryRegions with values aligned to a given list of countries.
This is used to make sure that the region definitions have the same “input names” as the given country list, which is necessary if country codes are not being used.
- all_region_countries_present_in(country_list: CountryInfoList) bool#
Return True if all countries needed to define regions are in the given country list.
- classmethod from_file(filepath: str | Path) Self#
Load country regions from JSON file.
- Parameters:
filepath – path to JSON file containing country region definitions.
- Returns:
new CountryRegions object representing the regions on the given file.
- region_countries_missing_from(country_list: CountryInfoList | Iterable[str]) dict[str, CountryInfoList]#
Report countries from region definitions that are missing from the given country list.
- Parameters:
country_list – List of countries to compare with. This is usually the
file. (list of country names from a country)
- Returns:
dict mapping regions to a list of missing countries.
- to_dict(country_code: Literal['alpha2', 'alpha3'] | None = None) dict[str, CountryInfoList]#
Return dict mapping region name to CountryInfoList of countries comprising the region.
This method is used to apply country region definitions to be applied in cases where the country names from the country file aren’t in the same format as the country names/codes in the country region definitions.
- Parameters:
country_code – country code to set for countries comprising a region
output. (in the)
- Returns:
dict mapping country regions to CountryInfoLists of the countries comprising each region.
- openghg_inversions.postprocessing.countries.get_area_grid(lat: DataArray, lon: DataArray) DataArray#
Return array containing area of each grid cell centered on given coordinates.
This creates an xr.DataArray with coordinate dimensions (“lat”, “lon”) containing the area of each grid cell centered on the coordinates.
- Parameters:
lat – latitude values
lon – longitude values
- Returns:
xr.DataArray with grid cell areas.