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

Fix conflicts in netcdf var_name #3634

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/iris/fileformats/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,7 @@ def _get_dim_names(self, cube):
while (
dim_name in self._existing_dim
or dim_name in self._name_coord_map.names
or dim_name in self._dataset.variables
):
dim_name = self._increment_name(dim_name)

Expand All @@ -1505,6 +1506,7 @@ def _get_dim_names(self, cube):
dim_name in self._existing_dim
and self._existing_dim[dim_name] != cube.shape[dim]
or dim_name in self._name_coord_map.names
or dim_name in self._dataset.variables
):
dim_name = self._increment_name(dim_name)
# Update dictionary with new entry
Expand Down
16 changes: 16 additions & 0 deletions lib/iris/tests/results/netcdf/netcdf_save_conf_name_2.cdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
dimensions:
dim0 = 10 ;
temp_0 = 10 ;
variables:
double temp(dim0) ;
temp:standard_name = "air_temperature" ;
temp:units = "K" ;
double temp3(temp_0) ;
temp3:long_name = "air_temperature" ;
temp3:units = "K" ;
int64 temp_0(temp_0) ;
temp_0:units = "1" ;

// global attributes:
:Conventions = "CF-1.7" ;
}
14 changes: 14 additions & 0 deletions lib/iris/tests/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,20 @@ def test_netcdf_save_conflicting_names(self):
# Check the netCDF file against CDL expected output.
self.assertCDL(file_out, ("netcdf", "netcdf_save_conf_name.cdl"))

def test_netcdf_save_conflicting_names_cube_to_coord(self):
# Test saving CF-netCDF with a dimension name corresponding to
# an existing variable name (conflict).
self.cube6.add_dim_coord(
iris.coords.DimCoord(np.arange(10), var_name="temp"), 0
)

cubes = [self.cube4, self.cube6]
with self.temp_filename(suffix=".nc") as file_out:
iris.save(cubes, file_out)

# Check the netCDF file against CDL expected output.
self.assertCDL(file_out, ("netcdf", "netcdf_save_conf_name_2.cdl"))

@tests.skip_data
def test_trajectory(self):
file_in = tests.get_data_path(("PP", "aPPglob1", "global.pp"))
Expand Down