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

Netcdf "var_name" save collisions not handled correctly for coordinates #3643

Closed
pp-mo opened this issue Jan 21, 2020 · 4 comments
Closed

Netcdf "var_name" save collisions not handled correctly for coordinates #3643

pp-mo opened this issue Jan 21, 2020 · 4 comments
Labels
Stale A stale issue/pull-request

Comments

@pp-mo
Copy link
Member

pp-mo commented Jan 21, 2020

From @pdearnshaw :
This code causes a failed save to NetCDF :

import iris

# Create a model level and pressure level coordinate:
#  Important bit is that pressure level coordinate has var_name="pressure"
pcoord = iris.coords.DimCoord([1000.],
                              standard_name=None,
                              long_name='pressure',
                              var_name='pressure',
                              units='hPa',
                              attributes={'positive': 'down'})

mcoord = iris.coords.DimCoord([1],
                              standard_name='model_level_number',
                              long_name='model_level_number',
                              var_name='model_level_number',
                              units='1',
                              attributes={'positive': 'up'})

# Create a model level and pressure level cube using above coordinates:
#  Important bit is that model level cube has var_name="pressure"
pcube = iris.cube.Cube([0.0],
                       standard_name='air_temperature',
                       long_name='air_temperature',
                       var_name='air_temperature',
                       units='K',
                       dim_coords_and_dims=[(pcoord, 0)])
mcube = iris.cube.Cube([0.0],
                       standard_name='air_pressure',
                       long_name='air_pressure',
                       var_name='pressure',
                       units='Pa',
                       dim_coords_and_dims=[(mcoord, 0)])

# Then save the cubes:
#  First save has pressure level cube first and works
iris.save([pcube, mcube], 'netcdf_save_work.nc')

#  Second save has model level cube first and fails
iris.save([mcube, pcube], 'netcdf_save_fail.nc')

I essentially create two cubes, one with a model level coordinate and one with a pressure level coordinate. The model level cube has var_name “pressure”, while the pressure level coordinate in the other cube also has var_name=”pressure”. If you save the cubes to a netcdf file in the order of pressure level cube first then the save works. If you reverse the cubes then the save fails in the same way we saw.

(See error trace in separate comment below : it's really long !)

@pp-mo
Copy link
Member Author

pp-mo commented Jan 21, 2020

The actual error traceback looks like this ...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.7/site-packages/iris/__init__.py", line 343, in load
    return _load_collection(uris, constraints, callback).merged().cubes()
  File ".../lib/python3.7/site-packages/iris/__init__.py", line 313, in _load_collection
    result = iris.cube._CubeFilterCollection.from_cubes(cubes, constraints)
  File ".../lib/python3.7/site-packages/iris/cube.py", line 145, in from_cubes
    for cube in cubes:
  File ".../lib/python3.7/site-packages/iris/__init__.py", line 300, in _generate_cubes
    for cube in iris.io.load_files(part_names, callback, constraints):
  File ".../lib/python3.7/site-packages/iris/io/__init__.py", line 210, in load_files
    for cube in handling_format_spec.handler(fnames, callback):
  File ".../lib/python3.7/site-packages/iris/fileformats/netcdf.py", line 712, in load_cubes
    cube = _load_cube(engine, cf, cf_var, filename)
  File ".../lib/python3.7/site-packages/iris/fileformats/netcdf.py", line 540, in _load_cube
    engine.activate(_PYKE_RULE_BASE)
  File ".../lib/python3.7/site-packages/pyke/knowledge_engine.py", line 297, in activate
    for rb_name in rb_names: self.get_rb(rb_name).activate()
  File ".../lib/python3.7/site-packages/pyke/rule_base.py", line 159, in activate
    self.run_fc_rules(current_rb)
  File ".../lib/python3.7/site-packages/pyke/rule_base.py", line 147, in run_fc_rules
    for fc_rule in rb.fc_rules: fc_rule.run()
  File ".../lib/python3.7/site-packages/pyke/fc_rule.py", line 90, in run
    self.rule_fn(self)
  File ".../lib/python3.7/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py", line 588, in fc_build_coordinate_latitude_nocs
    coord_system=None)
  File ".../lib/python3.7/site-packages/iris/fileformats/_pyke_rules/compiled_krb/fc_rules_cf_fc.py", line 2081, in build_dimension_coordinate
    cube.add_aux_coord(coord, data_dims)
  File ".../lib/python3.7/site-packages/iris/cube.py", line 949, in add_aux_coord
    self._add_unique_aux_coord(coord, data_dims)
  File ".../lib/python3.7/site-packages/iris/cube.py", line 981, in _add_unique_aux_coord
    data_dims = self._check_multi_dim_metadata(coord, data_dims)
  File ".../lib/python3.7/site-packages/iris/cube.py", line 977, in _check_multi_dim_metadata
    raise ValueError(msg)
ValueError: Missing data dimensions for multi-valued DimCoord 'latitude'

@pp-mo
Copy link
Member Author

pp-mo commented Jan 21, 2020

Hypothesis: possible mechanism
(1) When the save #1 works, saving the pressure level cube creates the "pressure"
variable associated with the coordinate. When the model level cube
called "pressure" is added it is given the variable name "pressure_0"

(2) When the save #2 fails, saving the model level cube creates the "pressure"
variable associated with the cube. When the pressure level cube is added it
tries to save the pressure coordinate as variable "pressure_0", as
"pressure" already exists, but gets tripped up because it may have already
named the NetCDF dimension "pressure", but subsequently is trying to
reference it as "pressure_0" and cannot find it.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2021

In order to maintain a backlog of relevant issues, we automatically label them as stale after 500 days of inactivity.

If this issue is still important to you, then please comment on this issue and the stale label will be removed.

Otherwise this issue will be automatically closed in 28 days time.

@github-actions github-actions bot added the Stale A stale issue/pull-request label Aug 4, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2021

This stale issue has been automatically closed due to a lack of community activity.

If you still care about this issue, then please either:

  • Re-open this issue, if you have sufficient permissions, or
  • Add a comment pinging @SciTools/iris-devs who will re-open on your behalf.

@github-actions github-actions bot closed this as completed Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale A stale issue/pull-request
Projects
None yet
Development

No branches or pull requests

1 participant