@@ -63,6 +63,24 @@ def preprocess_roms(
6363 for dim in dims :
6464 ds [dim ] = (dim , np .arange (ds .sizes [dim ]), {"axis" : "Y" })
6565
66+ # add attributes for lon/lat
67+ lon_attrs = {
68+ "standard_name" : "longitude" ,
69+ "units" : "degree_east" ,
70+ "field" : "longitude" ,
71+ }
72+ lat_attrs = {
73+ "standard_name" : "latitude" ,
74+ "units" : "degree_north" ,
75+ "field" : "latitude" ,
76+ }
77+ coords = [coord for coord in ds .coords if coord .startswith ("lon_" )]
78+ for coord in coords :
79+ ds [coord ].attrs = lon_attrs
80+ coords = [coord for coord in ds .coords if coord .startswith ("lat_" )]
81+ for coord in coords :
82+ ds [coord ].attrs = lat_attrs
83+
6684 # Fix standard_name for s_rho/s_w
6785 if "Vtransform" in ds .data_vars and "s_rho" in ds .coords :
6886 cond1 = (
@@ -177,17 +195,60 @@ def preprocess_roms(
177195 ds [zname ] = order (ds [zname ])
178196
179197 # replace s_rho with z_rho, etc, to make z_rho the vertical coord
180- for sname , zname in name_dict .items ():
181- for var in ds .data_vars :
182- if ds [var ].ndim == 4 :
183- if "coordinates" in ds [var ].encoding :
184- coords = ds [var ].encoding ["coordinates" ]
198+ for var in ds .data_vars :
199+ if ds [var ].ndim >= 3 :
200+ if "coordinates" in ds [var ].encoding :
201+ coords = ds [var ].encoding ["coordinates" ]
202+ # update s's to z's
203+ for sname , zname in name_dict .items ():
185204 if sname in coords : # replace if present
186205 coords = coords .replace (sname , zname )
187206 else : # still add z_rho or z_w
188207 if zname in ds [var ].coords and ds [zname ].shape == ds [var ].shape :
189208 coords += f" { zname } "
190- ds [var ].encoding ["coordinates" ] = coords
209+ # could have "x_rho" instead of "lon_rho", etc
210+ if "x_" in coords :
211+ xcoord = [element for element in coords .split () if "x_" in element ][
212+ 0
213+ ]
214+ coords = coords .replace (xcoord , xcoord .replace ("x" , "lon" ))
215+ # could have "x_rho" instead of "lon_rho", etc
216+ if "y_" in coords :
217+ ycoord = [element for element in coords .split () if "y_" in element ][
218+ 0
219+ ]
220+ coords = coords .replace (ycoord , ycoord .replace ("y" , "lat" ))
221+ ds [var ].encoding ["coordinates" ] = coords
222+ # same but coordinates not inside encoding. Do same processing
223+ # but also move coordinates from attrs to encoding.
224+ elif "coordinates" in ds [var ].attrs :
225+ coords_here = ds [var ].attrs ["coordinates" ]
226+ # update s's to z's
227+ for sname , zname in name_dict .items ():
228+ if sname in coords_here : # replace if present
229+ coords_here = coords_here .replace (sname , zname )
230+ else : # still add z_rho or z_w
231+ if zname in ds [var ].coords and ds [zname ].shape == ds [var ].shape :
232+ coords_here += f" { zname } "
233+ # could have "x_rho" instead of "lon_rho", etc
234+ if "x_" in coords_here :
235+ xcoord = [
236+ element for element in coords_here .split () if "x_" in element
237+ ][0 ]
238+ coords_here = coords_here .replace (
239+ xcoord , xcoord .replace ("x" , "lon" )
240+ )
241+ # could have "x_rho" instead of "lon_rho", etc
242+ if "y_" in coords_here :
243+ ycoord = [
244+ element for element in coords_here .split () if "y_" in element
245+ ][0 ]
246+ coords_here = coords_here .replace (
247+ ycoord , ycoord .replace ("y" , "lat" )
248+ )
249+ # move coords to encoding and delete from attrs
250+ ds [var ].encoding ["coordinates" ] = coords_here
251+ del ds [var ].attrs ["coordinates" ]
191252
192253 # # easier to remove "coordinates" attribute from any variables than add it to all
193254 # for var in ds.data_vars:
0 commit comments