@@ -366,7 +366,9 @@ def get_bounding_box(x, buffer=1):
366
366
return bbox
367
367
368
368
369
- def get_spacings (field_in , grid_spacing = None , time_spacing = None ):
369
+ def get_spacings (
370
+ field_in , grid_spacing = None , time_spacing = None , average_method = "arithmetic"
371
+ ):
370
372
"""Determine spatial and temporal grid spacing of the
371
373
input data.
372
374
@@ -383,6 +385,16 @@ def get_spacings(field_in, grid_spacing=None, time_spacing=None):
383
385
Manually sets the time spacing if specified.
384
386
Default is None.
385
387
388
+ average_method : string, optional
389
+ Defines how spacings in x- and y-direction are
390
+ combined.
391
+
392
+ - 'arithmetic' : standard arithmetic mean like (dx+dy)/2
393
+ - 'geometric' : geometric mean; conserves gridbox area
394
+
395
+ Default is 'arithmetic'.
396
+
397
+
386
398
Returns
387
399
-------
388
400
dxy : float
@@ -411,11 +423,16 @@ def get_spacings(field_in, grid_spacing=None, time_spacing=None):
411
423
) and (grid_spacing is None ):
412
424
x_coord = deepcopy (field_in .coord ("projection_x_coordinate" ))
413
425
x_coord .convert_units ("metre" )
414
- dx = np .diff (field_in . coord ( "projection_y_coordinate" ) [0 :2 ].points )[0 ]
426
+ dx = np .diff (x_coord [0 :2 ].points )[0 ]
415
427
y_coord = deepcopy (field_in .coord ("projection_y_coordinate" ))
416
428
y_coord .convert_units ("metre" )
417
- dy = np .diff (field_in .coord ("projection_y_coordinate" )[0 :2 ].points )[0 ]
418
- dxy = 0.5 * (dx + dy )
429
+ dy = np .diff (y_coord [0 :2 ].points )[0 ]
430
+
431
+ if average_method == "arithmetic" :
432
+ dxy = 0.5 * (np .abs (dx ) + np .abs (dy ))
433
+ elif average_method == "geometric" :
434
+ dxy = np .sqrt (np .abs (dx ) * np .abs (dy ))
435
+
419
436
elif grid_spacing is not None :
420
437
dxy = grid_spacing
421
438
else :
@@ -434,6 +451,10 @@ def get_spacings(field_in, grid_spacing=None, time_spacing=None):
434
451
elif time_spacing is not None :
435
452
# use value of time_spacing for dt:
436
453
dt = time_spacing
454
+ else :
455
+ raise ValueError (
456
+ "no information about time spacing, need either input cube with time or keyword argument time_spacing"
457
+ )
437
458
return dxy , dt
438
459
439
460
0 commit comments