Read and write metadata#

Tools to read and write DataArray and Dataset attributes

Easily dump a template to set up metadata for a given Dataset or DataArray that can subsequently be read-in and append attributes to instances.

Functions#

dump_config_template(fname, config_type)

Dump configuration file

assign_xr_attrs(obj, config_file)

Assign attributes to Dataset or DataArray

metadata.assign_xr_attrs(obj, config_file)[source]#

Assign attributes to Dataset or DataArray

The config_file should have only one-level of nesting.

Parameters:
  • obj ({xarray.Dataset, xarray.DataArray}) – Object to assign attributes to.

  • config_file (str) – A valid string path for input json file with metadata attributes.

Returns:

out

Return type:

{xarray.Dataset, xarray.DataArray}

Examples

>>> import pandas as pd
>>> import numpy as np
>>> import xarray as xr
>>> import skdiveMove.metadata as metadata

Synthetic dataset with depth and speed

>>> nsamples = 60 * 60 * 24
>>> times = pd.date_range("2000-01-01", freq="1s", periods=nsamples,
...                       name="time")
>>> cycles = np.sin(2 * np.pi * np.arange(nsamples) / (60 * 20))
>>> ds = xr.Dataset({"depth": (("time"), 1 + cycles),
...                  "speed": (("time"), 3 + cycles)},
...                 {"time": times})

Dump dataset and sensor templates

>>> metadata.dump_config_template("mydataset.json",
...                               "dataset")  
>>> metadata.dump_config_template("P_sensor.json",
...                               "sensor")  
>>> metadata.dump_config_template("S_sensor.json",
...                               "sensor")  

Edit the templates as appropriate, load and assign to objects

>>> assign_xr_attrs(ds, "mydataset.json")       
>>> assign_xr_attrs(ds.depth, "P_sensor.json")  
>>> assign_xr_attrs(ds.speed, "S_sensor.json")  
metadata.dump_config_template(fname, config_type)[source]#

Dump configuration file

Dump a json configuration template file to build metadata for a Dataset or DataArray.

Parameters:
  • fname (str) – A valid string path for output file.

  • config_type ({"dataset", "sensor"}) – The type of config to dump.

Examples

>>> import skdiveMove.metadata as metadata
>>> metadata.dump_config_template("mydataset.json",
...                               "dataset")  
>>> metadata.dump_config_template("mysensor.json",
...                               "sensor")  

edit the files to your specifications.