Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure from cfdm.write when writing identical (auxiliary) coordinates to different data variables in different groups #177

Closed
davidhassell opened this issue Mar 4, 2022 · 0 comments · Fixed by #178
Assignees
Labels
bug Something isn't working netCDF write Relating to writing netCDF datasets
Milestone

Comments

@davidhassell
Copy link
Contributor

It should be possible to write to disk fields with identical dimension or auxiliary coordinates to different groups, but this causes a failure from cfdm.write, which complains that the netCDF data variable dimensions are not in the same group or a parent group as the data variable itself.

It is good that cfdm.write complains about this, as it is a valid restriction, but it shouldn't have ended up in that situation in the first place.

Many thanks to @ajelenak who noticed and diagnosed this problem, and provided a minimal reproducer, which I slightly adapted here to also cover auxiliary coordinates:

import numpy as np
import cfdm

# Define dimension and auxiliary coordinates
station_name = cfdm.AuxiliaryCoordinate()
station_name.set_data(["station1", "station2"])
station_name.set_properties(
    {"long_name": "sensor id", "cf_role": "timeseries_id"}
)
station_name.nc_set_variable("station_name")

times = cfdm.DimensionCoordinate()
times.set_data(range(5))
times.set_properties(
    {"standard_name": "time", "units": "days since 1967-04-01"}
)
times.nc_set_variable("time")

file_content = list()
for mt in ("Dew Point", "Wind Speed"):
    grp_name = mt.replace(" ", "_")
    var_name = mt.replace(" ", "_")

    # Define domain axes
    stations = cfdm.DomainAxis(2)
    stations.nc_set_dimension("stations")
    stations.nc_set_dimension_groups([grp_name])

    time = cfdm.DomainAxis(5)
    time.nc_set_dimension("time")
    time.nc_set_dimension_groups([grp_name])

    # Assign new groups to dimension and auxiliary coordinates
    station_name = station_name.copy()
    station_name.nc_set_variable_groups([grp_name])

    times = times.copy()
    times.nc_set_variable_groups([grp_name])

    # Define the field in the same group as the coordinates
    f = cfdm.Field()

    station_axis = f.set_construct(stations)
    time_axis = f.set_construct(time)

    f.set_construct(times, axes=time_axis)
    f.set_construct(station_name, axes=station_axis)

    f.set_data(
        np.arange(10).reshape(5, 2), axes=[time_axis, station_axis]
    )

    f.nc_set_variable(var_name)
    f.nc_set_variable_groups([grp_name])

    file_content.append(f)

# This should not raise an exception, but does at v1.9.0.2
cfdm.write(file_content, "result.nc")

produces:

Traceback (most recent call last):
  File "/home/david/alek.py", line 59, in <module>
    cfdm.write(file_content, "result.nc")
  File "/home/david/miniconda3.9/lib/python3.9/site-packages/cfdm/read_write/write.py", line 522, in write
    netcdf.write(
  File "/home/david/miniconda3.9/lib/python3.9/site-packages/cfdm/decorators.py", line 171, in verbose_override_wrapper
    return method_with_verbose_kwarg(*args, **kwargs)
  File "/home/david/miniconda3.9/lib/python3.9/site-packages/cfdm/read_write/netcdf/netcdfwrite.py", line 4809, in write
    self._file_io_iteration(
  File "/home/david/miniconda3.9/lib/python3.9/site-packages/cfdm/read_write/netcdf/netcdfwrite.py", line 5089, in _file_io_iteration
    self._write_field_or_domain(f)
  File "/home/david/miniconda3.9/lib/python3.9/site-packages/cfdm/read_write/netcdf/netcdfwrite.py", line 3972, in _write_field_or_domain
    self._write_netcdf_variable(
  File "/home/david/miniconda3.9/lib/python3.9/site-packages/cfdm/read_write/netcdf/netcdfwrite.py", line 2696, in _write_netcdf_variable
    raise ValueError(
ValueError: Can't create netCDF variable '/Wind_Speed/Wind_Speed' from <Field: ncvar%/Wind_Speed/Wind_Speed(time(5), cf_role=timeseries_id(2))> with dimension '/Dew_Point/time' that is not in the same group or a sub-group as the variable.
>>> cfdm.environment(paths=False)
Platform: Linux-5.4.0-100-generic-x86_64-with-glibc2.31
HDF5 library: 1.12.1
netcdf library: 4.8.1
Python: 3.9.5
netCDF4: 1.5.8
numpy: 1.22.2
cfdm.core: 1.9.0.2
cftime: 1.5.2
netcdf_flattener: 1.2.0
cfdm: 1.9.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working netCDF write Relating to writing netCDF datasets
Projects
None yet
1 participant