Skip to content

Commit 2fbf81b

Browse files
committed
Better managment of units
1 parent 63640f0 commit 2fbf81b

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/py_eddy_tracker/dataset/grid.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ def is_circular(self):
353353
return False
354354

355355
def units(self, varname):
356+
stored_units = self.variables_description[varname]['attrs'].get('units', None)
357+
if stored_units is not None:
358+
return stored_units
356359
with Dataset(self.filename) as h:
357360
var = h.variables[varname]
358361
if hasattr(var, 'units'):
@@ -437,7 +440,7 @@ def bounds(self):
437440
return self.x_bounds.min(), self.x_bounds.max(), self.y_bounds.min(), self.y_bounds.max()
438441

439442
def eddy_identification(self, grid_height, uname, vname, date, step=0.005, shape_error=55,
440-
array_sampling=50, pixel_limit=None):
443+
array_sampling=50, pixel_limit=None, precision=None):
441444
"""
442445
443446
Args:
@@ -449,6 +452,7 @@ def eddy_identification(self, grid_height, uname, vname, date, step=0.005, shape
449452
shape_error: must be in percent (%)
450453
array_sampling:
451454
pixel_limit:
455+
precision: must be in meter(m)
452456
453457
Returns:
454458
@@ -468,11 +472,15 @@ def eddy_identification(self, grid_height, uname, vname, date, step=0.005, shape
468472
in_h_unit = units.parse_expression(h_units)
469473
if in_h_unit is not None:
470474
factor, _ = in_h_unit.to('m').to_tuple()
475+
logging.info('We will apply on step a factor to be coherent with grid : %f', 1 / factor)
471476
step /= factor
477+
if precision is not None:
478+
precision /= factor
472479

473480
# Get h grid
474-
data = self.grid(grid_height)
475-
481+
data = self.grid(grid_height).astype('f8')
482+
if precision is not None:
483+
data = (data / precision).round() * precision
476484
# Compute levels for ssh
477485
z_min, z_max = data.min(), data.max()
478486
d_z = z_max -z_min
@@ -599,10 +607,10 @@ def eddy_identification(self, grid_height, uname, vname, date, step=0.005, shape
599607
if speed_array.shape[0] == 1:
600608
properties.obs['uavg_profile'][:] = speed_array[0]
601609
else:
602-
properties.obs['uavg_profile'] = raw_resample(speed_array, array_sampling) * .01
610+
properties.obs['uavg_profile'] = raw_resample(speed_array, array_sampling)
603611
properties.obs['amplitude'] = amp.amplitude
604612
properties.obs['radius_s'] = eddy_radius_s
605-
properties.obs['speed_radius'] = max_average_speed * .01
613+
properties.obs['speed_radius'] = max_average_speed
606614
properties.obs['radius_e'] = eddy_radius_e
607615
properties.obs['shape_error_e'] = aerr
608616
properties.obs['shape_error_s'] = aerr_s
@@ -644,6 +652,13 @@ def eddy_identification(self, grid_height, uname, vname, date, step=0.005, shape
644652
factor, _ = in_h_unit.to(out_unit).to_tuple()
645653
a_and_c[0].obs[name] *= factor
646654
a_and_c[1].obs[name] *= factor
655+
in_u_units = units.parse_expression(self.units(uname))
656+
if in_u_units is not None:
657+
for name in ['speed_radius', 'uavg_profile']:
658+
out_unit = units.parse_expression(VAR_DESCR[name]['nc_attr']['units'])
659+
factor, _ = in_u_units.to(out_unit).to_tuple()
660+
a_and_c[0].obs[name] *= factor
661+
a_and_c[1].obs[name] *= factor
647662
return a_and_c
648663

649664
def get_uavg(self, all_contours, centlon_e, centlat_e, original_contour, anticyclonic_search, level_start,
@@ -652,6 +667,7 @@ def get_uavg(self, all_contours, centlon_e, centlat_e, original_contour, anticyc
652667
Calculate geostrophic speed around successive contours
653668
Returns the average
654669
"""
670+
# Init max speed to search maximum
655671
max_average_speed = self.speed_coef_mean(original_contour)
656672
speed_array = [max_average_speed]
657673

0 commit comments

Comments
 (0)