Skip to content

Commit

Permalink
fix metadata; suppress warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dionhaefner committed Mar 19, 2021
1 parent 4a53193 commit ff21ebc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
47 changes: 28 additions & 19 deletions fowd/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import math
import hashlib
import warnings

import numpy as np
import scipy.stats
Expand Down Expand Up @@ -81,25 +82,33 @@ def compute_dynamic_window_size(t, elevation, min_length, max_length, num_window
window_stationarity = []
window_offsets = np.linspace(0, window_length, num_samples, endpoint=False, dtype='int')

for window_offset in window_offsets:
if window_offset >= len(t):
continue

current_time_idx = window_offset
window_stds = []
while True:
next_time = t[current_time_idx] + np.timedelta64(window_length, 's')
if next_time > t[-1]:
break

next_time_idx = get_time_index(next_time, t)
window_stds.append(np.nanstd(elevation[current_time_idx:next_time_idx], ddof=1.5))
current_time_idx = next_time_idx

window_stds = np.asarray(window_stds)
window_stationarity.append(np.nanstd(window_stds[1:] / window_stds[:-1] - 1, ddof=1))

mean_window_stationarity = np.nanmean(window_stationarity)
# suppress nan warnings
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=RuntimeWarning)

for window_offset in window_offsets:
if window_offset >= len(t):
continue

current_time_idx = window_offset
window_stds = []
while True:
next_time = t[current_time_idx] + np.timedelta64(window_length, 's')
if next_time > t[-1]:
break

next_time_idx = get_time_index(next_time, t)
window_stds.append(
np.nanstd(elevation[current_time_idx:next_time_idx], ddof=1.5)
)
current_time_idx = next_time_idx

window_stds = np.asarray(window_stds)
window_stationarity.append(
np.nanstd(window_stds[1:] / window_stds[:-1] - 1, ddof=1)
)

mean_window_stationarity = np.nanmean(window_stationarity)

if mean_window_stationarity < best_window_stationarity:
best_window_length = window_length
Expand Down
17 changes: 14 additions & 3 deletions fowd/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
dtype='int64',
attrs=dict(
long_name='Length of dynamically computed sea state window',
units='seconds',
units='minutes',
)
)
})
Expand Down Expand Up @@ -622,9 +622,20 @@ def write_records(wave_record_iterator, filename, station_name, extra_metadata=N
if np.issubdtype(data.dtype, np.datetime64):
data = (data - np.datetime64(TIME_ORIGIN)) / np.timedelta64(1, 'ms')

# convert timedelta64 to seconds
# convert timedelta64 to target unit
if np.issubdtype(data.dtype, np.timedelta64):
data = data / np.timedelta64(1, 's')
unit = meta.get('attrs', {}).get('units')

if unit == 'seconds':
target_unit = 's'
elif unit == 'minutes':
target_unit = 'm'
elif unit == 'hours':
target_unit = 'h'
else:
raise ValueError(f'Got unrecognized time unit {unit}')

data = data / np.timedelta64(1, target_unit)

v[0, chunk_slice, ...] = data

Expand Down
2 changes: 1 addition & 1 deletion fowd/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_dynamic_window_size(current_window_size, current_time, time, elevation,
num_samples=NUM_DYNAMIC_WINDOW_SAMPLES,
)

return dynamic_sea_state_period // 60, current_time
return np.timedelta64(dynamic_sea_state_period, 's'), current_time


def compute_wave_records(time, elevation, elevation_normalized, outfile, statefile,
Expand Down

0 comments on commit ff21ebc

Please sign in to comment.