@@ -69,14 +69,20 @@ def get_frequency_dimension(xr_obj):
69
69
70
70
def get_vertical_dimension (xr_obj ):
71
71
"""Return the name of the available vertical dimension."""
72
- return np .array (VERTICAL_DIMS )[np .isin (VERTICAL_DIMS , list (xr_obj .dims ))].tolist ()
72
+ vertical_dim = np .array (VERTICAL_DIMS )[np .isin (VERTICAL_DIMS , list (xr_obj .dims ))].tolist ()
73
+ if len (vertical_dim ) > 1 :
74
+ raise ValueError (f"Only one vertical dimension is allowed. Got { vertical_dim } ." )
75
+ return vertical_dim
73
76
74
77
75
78
def get_spatial_dimensions (xr_obj ):
76
79
"""Return the name of the available spatial dimensions."""
77
80
dims = list (xr_obj .dims )
78
81
flattened_spatial_dims = list (chain .from_iterable (SPATIAL_DIMS ))
79
- return np .array (flattened_spatial_dims )[np .isin (flattened_spatial_dims , dims )].tolist ()
82
+ spatial_dimensions = np .array (flattened_spatial_dims )[np .isin (flattened_spatial_dims , dims )].tolist ()
83
+ if len (spatial_dimensions ) > 2 :
84
+ raise ValueError (f"Only two horizontal spatial dimensions are allowed. Got { spatial_dimensions } ." )
85
+ return spatial_dimensions
80
86
81
87
82
88
def _has_spatial_dim_dataarray (da , strict ):
@@ -230,9 +236,8 @@ def _is_expected_spatial_dims(spatial_dims):
230
236
def is_orbit (xr_obj ):
231
237
"""Check whether the xarray object is a GPM ORBIT.
232
238
233
- An ORBIT transect or nadir view is considered ORBIT.
234
- An ORBIT object must have the coordinates available !
235
-
239
+ An ORBIT cross-section (nadir view) or transect is considered ORBIT.
240
+ An ORBIT object must have the coordinates available.
236
241
"""
237
242
from gpm .dataset .crs import _get_swath_dim_coords
238
243
@@ -304,8 +309,8 @@ def _is_spatial_3d_dataarray(da, strict):
304
309
return True
305
310
306
311
307
- def _is_transect_dataarray (da , strict ):
308
- """Check if the xarray.DataArray is a spatial 3D array."""
312
+ def _is_cross_section_dataarray (da , strict ):
313
+ """Check if the xarray.DataArray is a cross-section array."""
309
314
spatial_dims = list (get_spatial_dimensions (da ))
310
315
if len (spatial_dims ) != 1 :
311
316
return False
@@ -320,6 +325,16 @@ def _is_transect_dataarray(da, strict):
320
325
return True
321
326
322
327
328
+ def _is_transect_dataarray (da , strict ):
329
+ """Check if the xarray.DataArray is a transect array."""
330
+ spatial_dims = list (get_spatial_dimensions (da ))
331
+ if len (spatial_dims ) != 1 :
332
+ return False
333
+ if strict and len (da .dims ) != 1 : # noqa
334
+ return False
335
+ return True
336
+
337
+
323
338
def _check_dataarrays_condition (condition , ds , strict ):
324
339
if not ds : # Empty dataset (no variables)
325
340
return False
@@ -339,8 +354,13 @@ def _is_spatial_3d_dataset(ds, strict):
339
354
return _check_dataarrays_condition (_is_spatial_3d_dataarray , ds = ds , strict = strict )
340
355
341
356
357
+ def _is_cross_section_dataset (ds , strict ):
358
+ """Check if all xarray.DataArrays of a xarray.Dataset are cross-section objects."""
359
+ return _check_dataarrays_condition (_is_cross_section_dataarray , ds = ds , strict = strict )
360
+
361
+
342
362
def _is_transect_dataset (ds , strict ):
343
- """Check if all xarray.DataArrays of a xarray.Dataset are transect objects."""
363
+ """Check if all xarray.DataArrays of a xarray.Dataset are transects objects."""
344
364
return _check_dataarrays_condition (_is_transect_dataarray , ds = ds , strict = strict )
345
365
346
366
@@ -376,13 +396,31 @@ def is_spatial_3d(xr_obj, strict=True, squeeze=True):
376
396
)
377
397
378
398
379
- def is_transect (xr_obj , strict = True , squeeze = True ):
380
- """Check if the xarray.DataArray or xarray.Dataset is a transect object.
399
+ def is_cross_section (xr_obj , strict = True , squeeze = True ):
400
+ """Check if the xarray.DataArray or xarray.Dataset is a cross-section object.
381
401
382
402
If ``squeeze=True`` (default), dimensions of size=1 are removed prior testing.
383
403
If ``strict=True`` (default), the xarray.DataArray must have just the
384
404
vertical dimension and a horizontal dimension.
385
- If ``strict=False`` , the xarray.DataArray can also have additional dimensions.
405
+ If ``strict=False`` , the xarray.DataArray can have additional dimensions but only
406
+ a single horizontal and vertical dimension.
407
+ """
408
+ return _check_xarray_conditions (
409
+ _is_cross_section_dataarray ,
410
+ _is_cross_section_dataset ,
411
+ xr_obj = xr_obj ,
412
+ strict = strict ,
413
+ squeeze = squeeze ,
414
+ )
415
+
416
+
417
+ def is_transect (xr_obj , strict = True , squeeze = True ):
418
+ """Check if the xarray.DataArray or xarray.Dataset is a transect object.
419
+
420
+ If ``squeeze=True`` (default), dimensions of size=1 are removed prior testing.
421
+ If ``strict=True`` (default), the xarray.DataArray must have just an horizontal dimension.
422
+ If ``strict=False`` , the xarray.DataArray can have additional dimensions but only a single
423
+ horizontal dimension.
386
424
"""
387
425
return _check_xarray_conditions (
388
426
_is_transect_dataarray ,
@@ -449,16 +487,29 @@ def check_is_spatial_3d(xr_obj, strict=True, squeeze=True):
449
487
raise ValueError ("Expecting a 3D GPM field." )
450
488
451
489
452
- def check_is_transect (xr_obj , strict = True , squeeze = True ):
453
- """Check if the xarray.DataArray or xarray.Dataset is a transect .
490
+ def check_is_cross_section (xr_obj , strict = True , squeeze = True ):
491
+ """Check if the xarray.DataArray or xarray.Dataset is a cross-section .
454
492
455
493
If ``squeeze=True`` (default), dimensions of size=1 are removed prior testing.
456
494
If ``strict=True`` (default), the xarray.DataArray must have just the
457
495
vertical dimension and a horizontal dimension.
458
- If ``strict=False`` , the xarray.DataArray can also have additional dimensions.
496
+ If ``strict=False`` , the xarray.DataArray can also have additional dimensions,
497
+ but only a single vertical and horizontal dimension.
498
+ """
499
+ if not is_cross_section (xr_obj , strict = strict , squeeze = squeeze ):
500
+ raise ValueError ("Expecting a cross-section extracted from a 3D GPM field." )
501
+
502
+
503
+ def check_is_transect (xr_obj , strict = True , squeeze = True ):
504
+ """Check if the xarray.DataArray or xarray.Dataset is a transect.
505
+
506
+ If ``squeeze=True`` (default), dimensions of size=1 are removed prior testing.
507
+ If ``strict=True`` (default), the xarray.DataArray must have just an horizontal dimension.
508
+ If ``strict=False`` , the xarray.DataArray can also have additional dimensions,
509
+ but only an horizontal dimension.
459
510
"""
460
511
if not is_transect (xr_obj , strict = strict , squeeze = squeeze ):
461
- raise ValueError ("Expecting a transect of a 3D GPM field ." )
512
+ raise ValueError ("Expecting a transect object ." )
462
513
463
514
464
515
def check_has_vertical_dim (xr_obj , strict = False , squeeze = True ):
@@ -523,16 +574,26 @@ def get_spatial_3d_variables(ds, strict=False, squeeze=True):
523
574
return sorted (variables )
524
575
525
576
526
- def get_transect_variables (ds , strict = False , squeeze = True ):
527
- """Get list of xarray.Dataset trasect variables.
577
+ def get_cross_section_variables (ds , strict = False , squeeze = True ):
578
+ """Get list of xarray.Dataset cross-section variables.
528
579
529
- If ``strict=False`` (default), the potential variables for which a transect can be derived.
530
- If ``strict=True``, the variables that are already provide a transect .
580
+ If ``strict=False`` (default), the potential variables for which a strict cross-section can be derived.
581
+ If ``strict=True``, the variables that are already a cross-section .
531
582
"""
532
- variables = [var for var in get_dataset_variables (ds ) if is_transect (ds [var ], strict = strict , squeeze = squeeze )]
583
+ variables = [var for var in get_dataset_variables (ds ) if is_cross_section (ds [var ], strict = strict , squeeze = squeeze )]
533
584
return sorted (variables )
534
585
535
586
587
+ # def get_transect_variables(ds, strict=False, squeeze=True):
588
+ # """Get list of xarray.Dataset transect variables.
589
+
590
+ # If ``strict=False`` (default), the potential variables for which a strict transect can be derived.
591
+ # If ``strict=True``, the variables that are already a transect.
592
+ # """
593
+ # variables = [var for var in get_dataset_variables(ds) if is_transect(ds[var], strict=strict, squeeze=squeeze)]
594
+ # return sorted(variables)
595
+
596
+
536
597
def get_vertical_variables (ds ):
537
598
"""Get list of xarray.Dataset variables with vertical dimension."""
538
599
variables = [var for var in get_dataset_variables (ds ) if has_vertical_dim (ds [var ], strict = False , squeeze = True )]
0 commit comments