Skip to content

Commit

Permalink
Merge branch 'stormtide' into 'master'
Browse files Browse the repository at this point in the history
Added filtered water level function

See merge request cmgp/stglib!4
  • Loading branch information
dnowacki-usgs committed Dec 11, 2024
2 parents 57cd785 + 91a2392 commit 45f2354
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
3 changes: 3 additions & 0 deletions doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ RBR instruments
Options specific to RBR instruments exported from the Ruskin software include:

- ``basefile``: the input filename without extension or data type. For example, if your exported text files are named ``055170_20190219_1547_burst.txt``, ``055170_20190219_1547_data.txt``, etc., ``basefile`` will be ``055170_20190219_1547``.
- ``filtered_wl``: "true" to turn on filtered water level variable (4th order lowpass butterworth filter with 6 min cutoff)
- ``wp_min``, ``wp_max``: min/max allowable wave period, in seconds
- ``wh_min``, ``wh_max``: min/max allowable wave height, in meters
- ``wp_ratio``: maximum allowable ratio between peak period (``wp_peak``) and mean period (``wp_4060``).
Expand Down Expand Up @@ -199,6 +200,7 @@ Onset Hobo
----------

- All the _min, _max, _bad_ens, etc. options available to the EXO.
- ``filtered_wl``: "true" to turn on filtered water level variable (4th order lowpass butterworth filter with 6 min cutoff)
- ``instrument_type``: can be ``hwl`` (water level), ``hwlb`` (water level as barometer), ``hdo`` (dissolved oxygen) or ``hcnd`` (conductivity) use these based on parameter measured by hobo logger
- ``skipfooter``: number of lines to skip in the CSV file at the end of the file
- ``ncols``: number of columns of data to read, starting at first
Expand Down Expand Up @@ -245,6 +247,7 @@ TruBlue
-------
- All the _min, _max, _bad_ens, etc. options available to the EXO
- ``skiprows``: number of header lines to skip in the txt file before the real data begins
- ``filtered_wl``: "true" to turn on filtered water level variable (4th order lowpass butterworth filter with 6 min cutoff)
- ``wave_interval``: interval in seconds for calculating wave bursts from continuous pressure data
- ``wp_min``, ``wp_max``: min/max allowable wave period, in seconds
- ``wh_min``, ``wh_max``: min/max allowable wave height, in meters
Expand Down
2 changes: 1 addition & 1 deletion stglib/core/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def apply_butter_filt(ds, var):

else:
raise ValueError(
f"sample_rate or sample _interval do not exits in global attributes, can not apply lowpass filter to {var}. "
f"sample_rate or sample _interval do not exist in global attributes, cannot apply lowpass filter to {var}. "
)

return ds
Expand Down
41 changes: 40 additions & 1 deletion stglib/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import stglib

from . import filter


def is_cf(ds):
if ("Conventions" in ds.attrs) and ("CF" in ds.attrs["Conventions"]):
Expand Down Expand Up @@ -1409,7 +1411,6 @@ def create_water_level_var(ds):
"P_1ac" in list(ds.data_vars)
and ds.z.attrs["geopotential_datum_name"] == "NAVD88"
):

if "sample" in ds.dims:
ds["water_level"] = xr.DataArray(
ds["P_1ac"].squeeze().mean(dim="sample") + ds["z"].values
Expand All @@ -1428,3 +1429,41 @@ def create_water_level_var(ds):
"Cannot create water_level variable without P_1ac and height_above_geopotential_datum relative to NAVD88 in global attributes file."
)
return ds


def create_filtered_water_level_var(ds):
"""
Create 4th order lowpass butterworth filtered water level with 6 min cutoff
"""

if "filtered_wl" in ds.attrs and ds.attrs["filtered_wl"].lower() == "true":

var = "water_level"
cutfreq = 1 / 360 # 6 min cutoff
ftype = "lowpass"
ford = 4

if "sample_rate" in ds.attrs:
sr = ds.attrs["sample_rate"]
elif "sample_interval" in ds.attrs:
sr = 1 / ds.attrs["sample_interval"]
else:
raise ValueError(
"Cannot create filtered_water_level without sample_rate or sample _interval in global attributes"
)

filtered_wl = filter.butter_filt(ds[var].values, sr, cutfreq, ftype, ford)

ds["water_level_filt"] = xr.DataArray(filtered_wl, dims="time")

ds["water_level_filt"].attrs["long_name"] = "Filtered water level NAVD88"
ds["water_level_filt"].attrs["units"] = "m"
ds["water_level_filt"].attrs[
"standard_name"
] = "sea_surface_height_above_geopotential_datum"
ds["water_level_filt"].attrs["geopotential_datum_name"] = "NAVD88"
ds["water_level_filt"].attrs[
"note"
] = "4th order lowpass butterworth filter with 6 min cutoff"

return ds
9 changes: 2 additions & 7 deletions stglib/hobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,12 @@ def cdf_to_nc(cdf_filename, atmpres=False):
ds = qaqc.drop_vars(ds)

ds = utils.create_z(ds) # added 7/31/2023

ds = utils.create_water_level_var(ds)
ds = utils.create_filtered_water_level_var(ds)
ds = ds_add_attrs(ds)

# assign min/max:
ds = utils.add_min_max(ds)

ds = utils.add_start_stop_time(ds)

ds = utils.add_delta_t(ds)

# add lat/lon coordinates
ds = utils.ds_add_lat_lon(ds)

if "vert_dim" in ds.attrs:
Expand Down
11 changes: 4 additions & 7 deletions stglib/rsk/cdf2nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,15 @@ def cdf_to_nc(cdf_filename, atmpres=None, writefile=True, format="NETCDF4"):
if not is_profile:
# add z coordinate dim
ds = utils.create_z(ds)

ds = utils.create_water_level_var(ds)
ds = utils.create_water_depth_var(ds)
ds = utils.create_filtered_water_level_var(ds)
ds = utils.add_min_max(ds)

ds = utils.add_start_stop_time(ds)

if not is_profile:
ds = utils.ds_add_lat_lon(ds)

ds = utils.add_start_stop_time(ds)
ds = utils.ds_coord_no_fillvalue(ds)

ds = utils.add_history(ds)

ds = dw_add_delta_t(ds)

if is_profile:
Expand Down
2 changes: 1 addition & 1 deletion stglib/rsk/nc2waves.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def nc_to_waves(nc_filename):
ds = utils.create_water_depth_var(ds)
ds = utils.create_water_level_var(ds)

for k in ["P_1", "P_1ac", "sample", "T_28"]:
for k in ["P_1", "P_1ac", "sample", "T_28", "water_level_filt"]:
if k in ds:
ds = ds.drop_vars(k)

Expand Down
1 change: 1 addition & 0 deletions stglib/tb.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def cdf_to_nc(cdf_filename, atmpres=None):
ds = utils.create_nominal_instrument_depth(ds)
ds = utils.create_z(ds)
ds = utils.create_water_level_var(ds)
ds = utils.create_filtered_water_level_var(ds)
ds = utils.create_water_depth_var(ds)
ds = utils.ds_add_lat_lon(ds)
ds = utils.add_start_stop_time(ds)
Expand Down

0 comments on commit 45f2354

Please sign in to comment.