Skip to main content

Add meta data to netCDF files.

Project description

pytests CD

addmeta

Add meta data to netCDF files.

Metadata

The metadata is stored in attribute files in YAML format. The metadata is in key-value pairs and is a global attribute if defined in a global section, or applied to a specific named variable in the variables section.

If an attribute is listed with a missing value that attribute is deleted from the file.

For example the following is an example of an attribute file:

global:
    # Mandatory since it gives a key to all the other attributes
    Conventions: "CF-1.7, ACDD-1.3"
    # The url of the license applied to the data
    license: "http://creativecommons.org/licenses/by-nc-sa/4.0/"
variables:
    yt_ocean:
        _FillValue:
        long_name: "latitude in rotated pole grid"
        units: "degrees"
    geolat_t:
        long_name: "latitude coordinate"
        units: "degrees_north"
        standard_name: "latitude"

It will create (or replace) two global attributes: Conventions and license. It will also create (or replace) attributes for two variables, yt_ocean and geolat_t, and delete the _FillValue attribute of yt_ocean.

The information is read into a python dict. Multiple attribute files can be specified. If the same attribute is defined more than once, the last attribute file specified takes precedence. Like cascading style sheets this means default values can be given and overridden when necessary.

metadata.yaml support

ACCESS-NRI models produce, and intake catalogues consume, a metadata.yaml file that is a series of key/value pairs (see schema for details).

Simple key/value pairs are supported by addmeta and are assumed to define global metadata.

Dynamic templating

addmeta supports limited dynamic templating to allow injection of file specific metadata in a general way. This is done using Jinja templating and providing a number of pre-populated variables:

variable description
mtime Last modification time
size File size (in bytes)
parent Parent directory of the netCDF file
name Filename of the netCDF file
fullpath Full path of the netCDF file

These variables can be used in a metadata file like so:

global:
    Publisher: "ACCESS-NRI"
    directory: "{{ parent }}"
    Year: 2025
    filename: "{{ name }}"
    size: "{{ size }}"
    modification_time: "{{ mtime }}"

[!CAUTION] Jinja template variables must be quoted and as a consequence all are saved as string attributes in the netCDF variable

Filename based dynamic templating

Often important file level properties are encoded in filenames. This is not an optimal solution, but comes about because it is not possible to alter the model code to inject the metadata directly into the output files.

addmeta supports extracting this information and embedding it dynamically as an extension to dynamic templating.

Extracting the variable is done by specifying python regular expressions with named groups, and the group names become the metadata template variables. e.g.

For the filename

access-om3.mom6.3d.agessc.1day.mean.1900-01.nc'

the following regex:

r'.*\.(?P<frequency>.*)\.mean\.\d+-\d+\.nc$'

would match and set frequency=1day. It is possible to define more than one named group in a regex, as long as the names are unique. It is also possible to specify multiple regex expressions, only those that match will return variables that can be used as jinja template variables. Unused variables are ignored, and in the case of identical named groups in different regexs, later defined regexs override previous ones.

Invocation

addmeta provides a command line interface. Invoking with the -h flag prints a summay of how to invoke the program correctly.

$ addmeta -h
usage: cli.py [-h] [-c CMDLINEARGS] [-m METAFILES] [-l METALIST] [-f FN_REGEX] [-v] files [files ...]

Add meta data to one or more netCDF files

positional arguments:
files                 netCDF files

options:
-h, --help            show this help message and exit
-c CMDLINEARGS, --cmdlineargs CMDLINEARGS
                        File containing a list of command-line arguments
-m METAFILES, --metafiles METAFILES
                        One or more meta-data files in YAML format
-l METALIST, --metalist METALIST
                        File containing a list of meta-data files
-f FN_REGEX, --fn-regex FN_REGEX
                        Extract metadata from filename using regex
-v, --verbose         Verbose output

Multiple attribute files can be specified by passing more than one file with the -m option. For a large number of files this can be tedious. In that case use the -l option and pass it a text file with the names of attribute files, one per line.

Multiple meta list files and meta files can be specified on one command line.

To support scriptable invocation command line arguments can be saved into a file and consumed with -c <filename>. A good practice is to have a command line argument per line, to make it easy to read, and a diff of isolates the change. Whitespace and comments are stripped, so it is also possible to add useful comments. e.g.

# Re-use experiment level metadata
-m=../metadata.yaml
# Ocean model specific global metadata
-m=meta_ocean_global.yaml
# Ocean model specific variable metadata
-m=meta_ocean_variable.yaml
# Extract frequency from filename 
--fn-regex=.*\.(?P<frequency>.*)\.mean\.\d+-\d+\.nc$
# Apply to all ocean data in output subdirectory
output/ocean_*.nc

[!CAUTION] Do not quote regex strings in a command file as above. String quoting is still required when used on the command line.

The python argparse library does not allow mixing of command line options and positional arguments. So all the references to netCDF files need to come at the end of the argument list.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

addmeta-0.3.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

addmeta-0.3-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file addmeta-0.3.tar.gz.

File metadata

  • Download URL: addmeta-0.3.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for addmeta-0.3.tar.gz
Algorithm Hash digest
SHA256 1719fc57c45bc941d2d5ea09b1c5757ef05e625b12d660ec9545919b5f4a6c72
MD5 426bd93dd6fbca6700337a150ae94c9b
BLAKE2b-256 ccc33f2d3ed52732a7841b6f2f46187fa996b3d6c5fa80465e696cacdf88279f

See more details on using hashes here.

File details

Details for the file addmeta-0.3-py3-none-any.whl.

File metadata

  • Download URL: addmeta-0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for addmeta-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 10717d2115f9e7327194f674285313f85728e84d60458b750bb6c750fa07c40e
MD5 3fef35eeb35e229e83bc1a47d21e0a81
BLAKE2b-256 7cf77f66359c69687f8e0762f0e90542fb091f0646d650b83f76f2963aa1aef0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page