YAXArrays.jl icon indicating copy to clipboard operation
YAXArrays.jl copied to clipboard

Add axis metadata

Open danlooo opened this issue 1 year ago • 1 comments

The NetCDF data models allows storing attributes of dimensions. This is helpful when it comes to store CF metadata of axis dimensions, e.g. unit and standard_name of axis air pressure. However, such attributes are currently ignored by open_dataset and savedataset:

using YAXArrays
using NetCDF
using Downloads

cf_example_file = Downloads.download("https://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3-example.nc", "example.nc")

ds_nc = NetCDF.open(cf_example_file)
ds_yax = open_dataset(cf_example_file)

ds_yax.plev # no metadata
# plev [100000.0, 92500.0, …, 2000.0, 1000.0]

ds_nc["plev"].atts # metadata
# Dict{Any, Any} with 5 entries:
#   "units"         => "Pa"
#   "long_name"     => "pressure"
#   "axis"          => "Z"
#   "standard_name" => "air_pressure"
#   "positive"      => "down"

This could be fixed by utilising the metadata field of the Lookup of the Dimension:

using DimensionalData.Lookups
plev_lookup = Sampled(ds_yax.axes[:plev].val; metadata=ds_nc["plev"].atts)
plev_dim = Dim{:plev}(plev_lookup)

ds_yax.axes[:plev] = plev_dim
metadata(plev_dim)

#Dict{Any, Any} with 5 entries:
#  "units"         => "Pa"
#  "long_name"     => "pressure"
#  "axis"          => "Z"
#  "standard_name" => "air_pressure"
#  "positive"      => "down"

danlooo avatar May 15 '24 12:05 danlooo

PR?

lazarusA avatar May 15 '24 12:05 lazarusA