Skip to content

Commit 7f57f1c

Browse files
committed
fix introduced bug due to previous modification: tide height at time_series has wrong unit scaling and without truncate mode
1 parent ba45c1e commit 7f57f1c

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

change_log.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@
6262

6363
#### ver 0.2.4 End of filling NA of tpxo9.zarr
6464
#### ver 0.2.5 Add a 'truncate' mode: lon/lat/tide to 5/5/3 decimal places. Fix tide-height unit in cm in 'map'.
65+
66+
-- fix introduced bug due to previous modification: tide height at time_series has wrong unit scaling and without truncate mode

src/model_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_tide_series(amp, ph, c, tide_time, format="netcdf", unit="cm", drop_mask
8686
tide.data[tide.mask] = np.nan
8787
out = tide.data
8888
return out
89-
89+
9090
except AttributeError as e:
9191
# Catch specific error when tide or minor prediction fails
9292
raise ValueError(

tide_app.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ def tide_to_output(tide, lon, lat, dtime, variables, mode="time", absmax=-1):
119119
out_dict = {
120120
'longitude': longitude_flat.tolist(),
121121
'latitude': latitude_flat,
122-
'time': dtime if mode == 'time' else [dtime[0]]
122+
'time': dtime if 'time' in mode else [dtime[0]]
123123
}
124124

125125
# Initialize a set to store valid indices
126-
if mode == 'time':
126+
if 'time' in mode:
127127
valid_indices = set(range(len(dtime)))
128128
var_rechk = ['time'] + variables
129129
else:
@@ -134,8 +134,8 @@ def tide_to_output(tide, lon, lat, dtime, variables, mode="time", absmax=-1):
134134
invalid_indices = set()
135135
for var in variables:
136136
if var in tide:
137-
if var == 'z':
138-
var_data = tide[var] * 100.0 # convert to cm
137+
if var == 'z' and 'time' not in mode:
138+
var_data = tide[var] * 100.0 # convert to cm (but time series is already cm)
139139
else:
140140
var_data = tide[var]
141141

@@ -151,7 +151,7 @@ def tide_to_output(tide, lon, lat, dtime, variables, mode="time", absmax=-1):
151151
out_dict[var] = [out_dict[var][i] for i in valid_indices]
152152

153153
# Apply flow_trunc truncation for output_mode="map" and special mode="flow_trunc"
154-
if 'map' in mode and 'truncate' in mode:
154+
if 'truncate' in mode: # allow both 'time' and 'map' all has truncate mode
155155
# Truncate longitude and latitude to 5 decimal places
156156
out_dict['longitude'] = [round(x, 5) for x in out_dict['longitude']]
157157
out_dict['latitude'] = [round(x, 5) for x in out_dict['latitude']]
@@ -193,7 +193,7 @@ async def get_tide(
193193
5, description="Re-sampling every N points(default 5)"),
194194
mode: Optional[str] = Query(
195195
None,
196-
description="Allowed modes: list,truncate. Optional can be none (default output is list). Multiple/special modes can be separated by comma. The mode 'truncate' will output longitude/latitude to 5 decimal places, tide variables to 3 decimal places."),
196+
description="Allowed modes: list, truncate. Optional can be none (default output is list). Multiple/special modes can be separated by comma. The mode 'truncate' will output longitude/latitude to 5 decimal places, tide variables to 3 decimal places."),
197197
tol: Optional[float] = Query(
198198
None,
199199
description="Tolerance for nearest method to locate points by giving tolerance value. Default tolerance is ±1/60 degree, and maximum is ±0.25 degree."),
@@ -282,21 +282,21 @@ async def get_tide(
282282
# Handle the edge case for longitude 0
283283
# We found nearest 0 point (but > 0) may encounter index error in xarray
284284
# The grid in dataset is -4.06e-6 - 0.0333
285-
zero_nearest_pt = np.round(0.5*config.gridSz, 3) #0.017
285+
zero_nearest_pt = np.round(0.5*config.gridSz, 3) #0.017
286286
if lon0 >= 0 and lon0 < zero_nearest_pt: # Consider values very close to 0 as 0
287287
lon0 = zero_nearest_pt
288288

289289
# Create the selection indexers including both spatial and constituent dimensions
290290
ds_cons = config.dz.sel(constituents=cons)
291-
291+
292292
# Then create a dataset with a single point
293293
point_ds = xr.Dataset(
294294
coords={
295295
'lon': [lon0],
296296
'lat': [lat0]
297297
}
298298
)
299-
299+
300300
# Use sel to find the nearest point
301301
dsub = ds_cons.sel(
302302
lon=point_ds.lon,
@@ -382,8 +382,10 @@ async def get_tide(
382382

383383
#if mode is None or mode != 'row':
384384
#print(tide)
385-
out = tide_to_output(tide, dsub.coords['lon'].values, dsub.coords['lat'].values, dtime, variables,
386-
output_mode + ',truncate' if 'truncate' in mode and output_mode == 'map' else output_mode,
385+
out = tide_to_output(tide, dsub.coords['lon'].values, dsub.coords['lat'].values, dtime, variables,
386+
# allow both 'map' and 'time' mode can have truncate mode, 202504
387+
output_mode + ',truncate' if 'truncate' in mode else output_mode,
388+
# output_mode + ',truncate' if 'truncate' in mode and output_mode == 'map' else output_mode,
387389
absmax=10000.0)
388390
return ORJSONResponse(content=jsonable_encoder(out))
389391
#else:

0 commit comments

Comments
 (0)