@@ -493,6 +493,9 @@ def era5_pi_decade(single_level_path: str, pressure_level_path: str) -> None:
493493 client .close ()
494494
495495
496+ import os
497+ import netCDF4 as nc
498+
496499def assign_coordinate_attributes (file_path : str ):
497500 """Assigns CF-compliant 'coordinates' attributes to data variables.
498501
@@ -508,8 +511,9 @@ def assign_coordinate_attributes(file_path: str):
508511 Doctests:
509512 >>> # Setup a dummy NetCDF file for the test
510513 >>> file_name = 'test_coord.nc'
511- >>> with nc.Dataset(file_name, 'w') as ds:
512- ... _ = ds.createDimension('latitude', 10)
514+ >>> # ✅ Use the simpler NETCDF3 format to avoid HDF5 filesystem issues in CI
515+ >>> with nc.Dataset(file_name, 'w', format='NETCDF3_CLASSIC') as ds:
516+ ... _ = ds.createDimension('latitude', 10)
513517 ... _ = ds.createDimension('longitude', 20)
514518 ... _ = ds.createDimension('time', 2)
515519 ... _ = ds.createVariable('latitude', 'f4', ('latitude',))
@@ -536,12 +540,14 @@ def assign_coordinate_attributes(file_path: str):
536540
537541 try :
538542 with nc .Dataset (file_path , "a" ) as ds :
539- # Get the names of all coordinate variables
543+ # Get the names of all coordinate variables (variables that share a name with a dimension)
540544 coord_vars = list (ds .dimensions .keys ())
541545
542546 # Iterate over all variables in the file
543547 for var_name in ds .variables :
544- # Skip coordinate variables themselves
548+ # 🐛 Skip coordinate variables themselves
549+ if var_name in coord_vars :
550+ continue
545551
546552 var_obj = ds .variables [var_name ]
547553 # Check if the variable has the required dimensions
@@ -551,12 +557,15 @@ def assign_coordinate_attributes(file_path: str):
551557 var_obj .setncattr ("coordinates" , f"{ lon_name } { lat_name } " )
552558 print ("Added 'coordinates' attribute." )
553559
554- ds [lon_name ].setncattr ("standard_name" , "longitude" )
555- ds [lat_name ].setncattr ("standard_name" , "latitude" )
556- ds [lon_name ].setncattr ("axis" , "X" )
557- ds [lat_name ].setncattr ("axis" , "Y" )
558- ds [lon_name ].units = "degrees_east"
559- ds [lat_name ].units = "degrees_north"
560+ # Set standard attributes for coordinate variables
561+ if lon_name in ds .variables :
562+ ds [lon_name ].setncattr ("standard_name" , "longitude" )
563+ ds [lon_name ].setncattr ("axis" , "X" )
564+ ds [lon_name ].units = "degrees_east"
565+ if lat_name in ds .variables :
566+ ds [lat_name ].setncattr ("standard_name" , "latitude" )
567+ ds [lat_name ].setncattr ("axis" , "Y" )
568+ ds [lat_name ].units = "degrees_north"
560569
561570 except FileNotFoundError :
562571 print (f"Error: The file '{ file_path } ' was not found." )
0 commit comments