Skip to content

Commit d1f181b

Browse files
authored
Merge pull request #400 from fsenf/bugfix-in-grid-spacing
Bugfix in grid spacing
2 parents defda4d + dac29e8 commit d1f181b

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

tobac/utils/general.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ def get_bounding_box(x, buffer=1):
366366
return bbox
367367

368368

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+
):
370372
"""Determine spatial and temporal grid spacing of the
371373
input data.
372374
@@ -383,6 +385,16 @@ def get_spacings(field_in, grid_spacing=None, time_spacing=None):
383385
Manually sets the time spacing if specified.
384386
Default is None.
385387
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+
386398
Returns
387399
-------
388400
dxy : float
@@ -411,11 +423,16 @@ def get_spacings(field_in, grid_spacing=None, time_spacing=None):
411423
) and (grid_spacing is None):
412424
x_coord = deepcopy(field_in.coord("projection_x_coordinate"))
413425
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]
415427
y_coord = deepcopy(field_in.coord("projection_y_coordinate"))
416428
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+
419436
elif grid_spacing is not None:
420437
dxy = grid_spacing
421438
else:
@@ -434,6 +451,10 @@ def get_spacings(field_in, grid_spacing=None, time_spacing=None):
434451
elif time_spacing is not None:
435452
# use value of time_spacing for dt:
436453
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+
)
437458
return dxy, dt
438459

439460

0 commit comments

Comments
 (0)