@@ -37,8 +37,10 @@ def find_axis_from_dim_coord(
37
37
Returns ValueError if there are more than one matching dimension name or
38
38
if the dimension/coordinate isn't found.
39
39
"""
40
-
41
- dim_axis = find_axis_from_dim (in_da , dim_coord_name )
40
+ try :
41
+ dim_axis = find_axis_from_dim (in_da , dim_coord_name )
42
+ except ValueError :
43
+ dim_axis = None
42
44
43
45
try :
44
46
coord_axes = find_axis_from_coord (in_da , dim_coord_name )
@@ -96,7 +98,7 @@ def find_axis_from_dim(in_da: xr.DataArray, dim_name: str) -> Union[int, None]:
96
98
"More than one matching dimension. Need to specify which axis number or rename "
97
99
"your dimensions."
98
100
)
99
- return None
101
+ raise ValueError ( "Dimension not found. " )
100
102
101
103
102
104
def find_axis_from_coord (in_da : xr .DataArray , coord_name : str ) -> tuple [int ]:
@@ -135,18 +137,18 @@ def find_axis_from_coord(in_da: xr.DataArray, coord_name: str) -> tuple[int]:
135
137
136
138
if len (all_matching_coords ) > 1 :
137
139
raise ValueError ("Too many matching coords" )
138
- return tuple ( )
140
+ raise ValueError ( "No matching coords" )
139
141
140
142
141
143
def find_vertical_coord_name (
142
- variable_cube : xr .DataArray ,
144
+ variable_da : xr .DataArray ,
143
145
vertical_coord : Union [str , None ] = None ,
144
146
) -> str :
145
147
"""Function to find the vertical coordinate in the iris cube
146
148
147
149
Parameters
148
150
----------
149
- variable_cube : xarray.DataArray
151
+ variable_da : xarray.DataArray
150
152
Input variable cube, containing a vertical coordinate.
151
153
vertical_coord: str
152
154
Vertical coordinate name. If None, this function tries to auto-detect.
@@ -162,7 +164,7 @@ def find_vertical_coord_name(
162
164
Raised if the vertical coordinate isn't found in the cube.
163
165
"""
164
166
165
- list_coord_names = variable_cube .coords
167
+ list_coord_names = variable_da .coords
166
168
167
169
if vertical_coord is None or vertical_coord == "auto" :
168
170
# find the intersection
@@ -347,14 +349,20 @@ def add_coordinates_to_features(
347
349
hdim2_name_original = variable_da .dims [hdim2_axis ]
348
350
349
351
# generate random names for the new coordinates that are based on i, j, k values
350
- hdim1_name_new = "" .join (
351
- random .choice (string .ascii_uppercase + string .ascii_lowercase + string .digits )
352
- for _ in range (16 )
353
- )
354
- hdim2_name_new = "" .join (
355
- random .choice (string .ascii_uppercase + string .ascii_lowercase + string .digits )
356
- for _ in range (16 )
357
- )
352
+ hdim1_name_new = "__temp_hdim1_name"
353
+ hdim2_name_new = "__temp_hdim2_name"
354
+ vdim_name_new = "__temp_vdim_name"
355
+
356
+ if (
357
+ hdim1_name_new in variable_da .dims
358
+ or hdim2_name_new in variable_da .dims
359
+ or vdim_name_new in variable_da .dims
360
+ ):
361
+ raise ValueError (
362
+ "Cannot have dimensions named {0}, {1}, or {2}" .format (
363
+ hdim1_name_new , hdim2_name_new , vdim_name_new
364
+ )
365
+ )
358
366
359
367
dim_new_names = {
360
368
hdim1_name_original : hdim1_name_new ,
@@ -367,12 +375,6 @@ def add_coordinates_to_features(
367
375
368
376
if is_3d :
369
377
vdim_name_original = variable_da .dims [vertical_axis ]
370
- vdim_name_new = "" .join (
371
- random .choice (
372
- string .ascii_uppercase + string .ascii_lowercase + string .digits
373
- )
374
- for _ in range (16 )
375
- )
376
378
dim_interp_coords [vdim_name_new ] = xr .DataArray (
377
379
return_feat_df ["vdim" ].values , dims = "features"
378
380
)
@@ -383,9 +385,9 @@ def add_coordinates_to_features(
383
385
# dataset
384
386
renamed_dim_da = variable_da .swap_dims (dim_new_names )
385
387
interpolated_df = renamed_dim_da .interp (coords = dim_interp_coords )
386
- interpolated_df = interpolated_df .drop ([ hdim1_name_new , hdim2_name_new ])
387
- if is_3d :
388
- interpolated_df = interpolated_df . drop ([ vdim_name_new ] )
388
+ interpolated_df = interpolated_df .drop_vars (
389
+ [ hdim1_name_new , hdim2_name_new , vdim_name_new ], errors = "ignore"
390
+ )
389
391
return_feat_df [time_dim_name ] = variable_da [time_dim_name ].values [
390
392
return_feat_df ["frame" ]
391
393
]
0 commit comments