axiom package
Contents
axiom package#
Subpackages#
Submodules#
axiom.config module#
Configuration.
axiom.converters module#
Conversion routines to convert between units automatically. EXPERIMENTAL.
- axiom.converters.ensure_units(da, expected_units)[source]#
Ensure that a given DataArray has the correct units, converting if needed.
- Args:
da (xarray.DataArray): Data. expected_units (ste): Units value.
- Returns:
xarray.DataArray: Data with units converted.
- Raises:
KeyError : When no unit attribute is present. KeyError : When no converter is registered between the old and new units. Exception: When the converter fails to convert the units.
axiom.decorators module#
Decorators.
axiom.drs module#
Main entrypoint for DRS processing.
- axiom.drs.consume(json_filepath)[source]#
Consume a json payload (for message passing)
- Args:
json_filepath (str): Path to the JSON file.
- axiom.drs.filter_years(filepaths, year, offset=0)[source]#
Filter filepaths based on a year, plus or minus an offset.
- Args:
filepaths (list): List of filepaths. year (int): Year. offset (int, optional): Number of years either side of YEAR to include. Defaults to 0.
- Returns:
list : List of filtered filepaths.
- axiom.drs.is_error_recoverable(exception)[source]#
Check if the error is recoverable.
- Args:
exception (Exception): Raised exception to check.
- Returns:
bool : True if recoverable, False otherwise.
- axiom.drs.load_variable_config(project_config)[source]#
Extract the variable configuration out of the project configuration.
- Args:
project_config (dict-like): Project configuration.
- Returns:
dict: Variable dictionary with name: [levels] (single level will have a list containing None.)
- axiom.drs.log_exception(message, ex)[source]#
Log the exception.
- Args:
message (str): Human-readable error message. ex (Exception): Stack trace.
- axiom.drs.process(input_files, output_directory, variable, project, model, domain, start_year, end_year, output_frequency, level=None, input_resolution=None, overwrite=True, preprocessor=None, postprocessor=None, **kwargs)[source]#
Method to process a single variable/domain/resolution combination.
- Args:
input_files (str or list): Globbable string or list of filepaths. output_directory (str) : Path from which to build DRS structure. variable (str): Variable to process. project (str): Project metadata to apply (loaded from user config). model (str): Model metadata to apply (loaded from user config). start_year (int): Start year. end_year (int): End year. output_frequency (str): Output frequency to process. input_resolution (float, optional): Input resolution in km. Leave black to auto-detect from filepaths. overwrite (bool): Overwrite the data at the destination. Defaults to True. preprocessor (str): Data preprocessor to activate on input data. Defaults to None. postprocesser (str): Data postprocess to activate before writing data. Defaults to None. **kwargs: Additional keyword arguments used in metadata interpolation.
- axiom.drs.process_multi(variables, domain, project, **kwargs)[source]#
Start a processing chain of multiple variables.
- Args:
variables (list): List of variables to process. domain (str): Domain to process from domains.json. project (str): Project metadata to use from projects.json. **kwargs: Additional keyword arguments to pass to the processing chain.
axiom.exceptions module#
Custom exception classes.
- exception axiom.exceptions.DRSContextInterpolationException(placeholders)[source]#
Bases:
ExceptionRaised when there are remaining placeholders in a template after DRS context interpolation.
- Args:
placeholders (list) : List of placeholders that have yet to be interpolated.
- exception axiom.exceptions.MalformedDRSJSONPayloadException[source]#
Bases:
ExceptionRaised when a DRS JSON payload can’t be processed.
axiom.report module#
Reporting functions.
axiom.utilities module#
General utilities.
- class axiom.utilities.ListAwareConfigParser[source]#
Bases:
configparser.ConfigParser- getlist(section, key, delimiter='\n', cast=<class 'str'>)[source]#
Get an option out of the configuration and automatically split into a list.
- Args:
section (str): Section name. key (str): Option key. delimiter (str, optional): Delimiter. Defaults to ‘
- ‘.
cast (callable): Typecast on the fly to this type.
- Returns:
list: Configuration option as a list.
- axiom.utilities.apply_schema(ds, schema)[source]#
Apply a metadata schema on a dataset.
- Args:
ds (xarray.Dataset): Dataset. schema (dict): Axiom schema dictionary.
- Returns:
xarray.Dataset : Dataset with schema-defined metadata applied.
- axiom.utilities.auto_glob(path)[source]#
Shorthand for sorted(glob.glob(path))
- Args:
mask (str): Globabble path
- Returns:
list : List of paths that match the globbable part.
- axiom.utilities.batch_split(iterable, n_batches)[source]#
Split iterable into n_batches.
- Args:
iterable (iterable) : Iterable object to split. n_batches (int) : Number of batches.
- Returns:
list : List of iterables.
- axiom.utilities.conditional_rename(ds, **kwargs)[source]#
Conditionally rename an object on a dataset, if it exists.
- Args:
ds (xarray.Dataset): Dataset. **kwargs : Key/value pairs of old=new names.
- Returns:
xarray.Dataset : Dataset with renamed variables/coords.
- axiom.utilities.diff_metadata(meta_a, meta_b, ignore_matches=True)[source]#
Difference the metadata between two metadata dictionaries.
Differences are encoded with values as tuples, where: - (None, None) = Attributes match. - (None, value2) = Attribute is missing from meta_a. - (value1, None) = Attribute is missing from meta_b. - (value1, value2) = Differing values between meta_a and meta_b.
- Args:
meta_a (dict): Metadata dictionary of the form from extract_metadata. meta_b (dict): Metadata dictionary of the form from extract_metadata.
- Returns:
dict : Dictionary of differences.
- axiom.utilities.extract_metadata(ds)[source]#
Extract metadata from an xarray dataset.
- Args:
ds (str or xarray.Dataset): Path to (netcdf) or dataset.
- Returns:
dict : Metadata dictionary.
- axiom.utilities.get_installed_data_root()[source]#
Get the root directory for installed data.
- Returns:
str : Root directory.
- axiom.utilities.get_lock_filepath(filepath)[source]#
Get a lock file path for filepath.
- Args:
filepath (str): Path.
- Returns:
str: Lock file path.
- axiom.utilities.get_logger(name, level='debug')[source]#
Get a logging object.
- Args:
name (str): Name of the module currently logging. level (str, optional): Level of logging to emit. Defaults to ‘debug’.
- Returns:
logging.Logger: Logging object.
- axiom.utilities.get_timestamp()[source]#
Generate a JSON-compliant timestamp.
- Returns:
str : Timestamp.
- axiom.utilities.get_user_data_root()[source]#
Get the root directory for user data.
- Returns:
str : Root directory.
- axiom.utilities.get_variables_and_coordinates(ds)[source]#
Get a list of variable and coordinate names.
- Args:
ds (xarray.Dataset): Dataset
- Returns:
list: List of variable and coordinate names
- axiom.utilities.has_attr(obj, attr)[source]#
Test if obj has the attribute attr.
- Args:
obj (xarray.DataArray or xarray.Dataset): xarray object. attr (str): Name of the attribute.
- Returns:
bool: True if obj has attribute attr, False otherwise.
- axiom.utilities.has_coord(dx, coord)[source]#
Check if a DataArray or Dataset contains a coordinate.
- Args:
dx (xarray.DataArray or xarray.Dataset): Data. coord (str): Coordinate name.
- Returns:
bool: True if coordinate in object, False otherwise.
- axiom.utilities.has_dim(dx, dim)[source]#
Check if a DataArray or Dataset contains a dimension.
- Args:
dx (xarray.DataArray or xarray.Dataset): Data. dim (str): Dimension name.
- Returns:
bool: True if dim on object, False otherwise.
- axiom.utilities.has_variable(ds, variable)[source]#
Check if a dataset contains a given variable.
- Args:
ds (xarray.Dataset): Dataset. variable (str): Variable name.
- Returns:
bool: True if ds contains variable, False otherwise.
- axiom.utilities.in_ds(variable, ds)[source]#
Test if variable is in the data file.
- Args:
variable (str): Variable name. ds (xarray.Dataset): Data.
- Returns:
bool : True if the variable exists, False otherwise.
- axiom.utilities.infer_dtype(value)[source]#
Infer the data type of the value passed.
- Args:
value (unknown): Value.
- Raises:
ValueError : When the type can’t be inferred.
- axiom.utilities.interpolate_template(template, **kwargs)[source]#
Interpolate onto a template string.
- Args:
string (str): Template string. **kwargs : Key/value pairs to interpolate.
- Returns:
str : Interpolated string
- axiom.utilities.is_locked(filepath)[source]#
Check if a file is locked.
- Args:
filepath (str): Path to the file.
- axiom.utilities.isolate_coordinate(obj, coordinate_name, drop=True)[source]#
Ensure that coordinate_name is the only coordinate on obj by selecting the first index of all other coordinates.
- Args:
obj (xarray.DataArray or xarray.Dataset): xarray object. coordinate_name (str): Name of the coordinate to retain. drop (bool, optional): Drop the coordinate_name from the object. Defaults to True.
- axiom.utilities.load_cf_standard_name_table(filepath)[source]#
Load and convert a CF standard name table XML into a metadata schema.
- Args:
filepath (str): Path to the file.
- Returns:
dict : Metadata dictionary (will be long).
- axiom.utilities.load_cordex_csv(filepath, **kwargs)[source]#
Load a cordex csv (i.e. from CCAM) and convert it into a metadata schema.
Note: There are no _global attributes.
- Args:
filepath (str): Path to file **kwargs : Additional key/value pairs to add to the schema header (i.e. contact etc.)
- Returns:
dict : Schema dictionary
- axiom.utilities.load_metadata_json(filepath)[source]#
Load a metadata.json file.
- Args:
filepath (str): Path to the json file.
- Returns:
dict: Metadata dictionary.
- axiom.utilities.load_package_data(slug, package_name='axiom', return_type='json')[source]#
Load data from the installed Axiom package.
- Args:
slug (str): Internal data path. package_name (str) : Name of the package. Defaults to ‘axiom’.
- Returns:
mixed : Dictionary of data (JSON only ATM).
- axiom.utilities.load_package_json(path)[source]#
Load package JSON data from the Axiom installation directory or from the user’s home directory.
- Args:
path (str): Path to the data.
- Returns:
dict : Data.
- axiom.utilities.load_schema_json(filepath)[source]#
Load a schema.json file.
- Args:
filepath (str): Path to the json file.
- Returns:
dict : Schema dictionary.
- axiom.utilities.load_user_config(name)[source]#
Load a configuration file from the user’s $HOME/.axiom directory.
- Args:
name (str): Name of the configuration file.
- Returns:
configparser.Config : Configuration.
- axiom.utilities.lock(filepath)[source]#
Place a lock on a filepath.
- Args:
filepath (str): Path to the file.
- axiom.utilities.pluralise(obj)[source]#
Automatically convert obj into a list object.
For use when an object can be defined singularly or plurally.
- Args:
obj (object): Object.
- axiom.utilities.save_schema(schema, filepath)[source]#
Save the schema to a json file at filepath.
- Args:
schema (dict): Schema dictionary. filepath (str): Path to which to save the schema.
- axiom.utilities.shell(cmd, shell=True, check=True, capture_output=True, **kwargs)[source]#
Execute a shell command.
- Args:
cmd (str): Command **kwargs : Passed to subprocess.run
- Returns:
[type]: [description]
Module contents#
Axiom main module.