Skip to content

Commit

Permalink
added tests (#351)
Browse files Browse the repository at this point in the history
* minor commented code cleanup

* added tests
  • Loading branch information
veenstrajelmer authored Sep 6, 2024
1 parent 91fd20d commit e262f72
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
11 changes: 7 additions & 4 deletions hatyan/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,17 @@ def calc_HWLW12345to12(data_HWLW_12345):
logger.info('starting HWLW 12345 to 12 correction')
times_LWmin = []
data_HW1 = data_HWLW_12345.loc[data_HWLW_12345['HWLWcode']==1]
#computing minimum waterlevels after each HW. Using hardcoded 12hour period instead of from one HW to next HW since then we can also assess last LW values
# computing minimum waterlevels after each HW. Using hardcoded 12hour period
# instead of from one HW to next HW since then we can also assess last LW values
for timeHW in data_HW1.index: #np.arange(0,len(data_HW1)-1):
if timeHW==data_HWLW_12345.index[-1]: #if last HW is last time of input dataframe
continue
#tide_afterHW = data_HWLW_12345.loc[data_HW1.index[iHW]:data_HW1.index[iHW+1]]
tide_afterHW = data_HWLW_12345.loc[timeHW:timeHW+dt.timedelta(hours=12)]
tide_afterHW = tide_afterHW.iloc[1:] #remove first HW to avoid issues if LW is higher than HW due to surge
if len(tide_afterHW)==0: #this happens if there is no LW defined between two HWs, for instance after SCHEVNGN HW at '1948-04-30 19:50:00'
# remove first HW to avoid issues if LW is higher than HW due to surge
tide_afterHW = tide_afterHW.iloc[1:]
if len(tide_afterHW)==0:
# this happens if there is no LW defined between two HWs
# for instance after SCHEVNGN HW at '1948-04-30 19:50:00'
continue
time_minimum = tide_afterHW['values'].idxmin()
times_LWmin.append(time_minimum)
Expand Down
85 changes: 84 additions & 1 deletion tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ def test_writenetcdf():

@pytest.mark.unittest
def test_writenetcdf_nosidx():

current_station = 'VLISSGN'

file_pred = os.path.join(dir_testdata,f'{current_station}_pre.txt')
Expand Down Expand Up @@ -525,3 +524,87 @@ def test_writenetcdf_nosidx():
data_nc.close()
os.remove(file_nc)


@pytest.mark.unittest
def test_calc_HWLW12345to12():
file_ext = os.path.join(dir_testdata,'hoek_har.dia')

df = hatyan.read_dia(file_ext, block_ids=0)
df_12 = hatyan.calc_HWLW12345to12(df)

assert len(df) == 3977
assert len(df_12) == 2825


@pytest.mark.unittest
def test_calc_HWLW12345to12_include_last_lw():
file_ext = os.path.join(dir_testdata,'hoek_har.dia')

df = hatyan.read_dia(file_ext, block_ids=0)

# get timeseries that ends with HWLWcode=2
df_sel = df.iloc[:-1]
df_12 = hatyan.calc_HWLW12345to12(df_sel)

assert len(df) == 3977
assert len(df_sel) == 3976
assert df_sel["HWLWcode"].iloc[0] == 1
assert df_sel["HWLWcode"].iloc[-1] == 2
assert len(df_12) == 2824
assert df_12["HWLWcode"].iloc[0] == 1
assert df_12["HWLWcode"].iloc[-1] == 2


@pytest.mark.unittest
def test_calc_HWLW12345to12_include_first_lw():
file_ext = os.path.join(dir_testdata,'hoek_har.dia')

df = hatyan.read_dia(file_ext, block_ids=0)

# get timeseries that starts with HWLWcode=2
df_sel = df.iloc[9:]
df_12 = hatyan.calc_HWLW12345to12(df_sel)

assert len(df) == 3977
assert len(df_sel) == 3968
assert df_sel["HWLWcode"].iloc[0] == 2
assert df_sel["HWLWcode"].iloc[-1] == 1
assert len(df_12) == 2820
assert df_12["HWLWcode"].iloc[0] == 2
assert df_12["HWLWcode"].iloc[-1] == 1


@pytest.mark.unittest
def test_calc_HWLW12345to12_skip_missing_lw():
file_ext = os.path.join(dir_testdata,'hoek_har.dia')

df = hatyan.read_dia(file_ext, block_ids=0)

# construct boolean to drop the first low waters (345 combination)
# and the last low water (2)
bool_drop = df["HWLWcode"] != 0
bool_drop.iloc[1:4] = False
bool_drop.iloc[-2:-1] = False

df_sel = df.loc[bool_drop]
df_12 = hatyan.calc_HWLW12345to12(df_sel)

assert len(df) == 3977
assert len(df_sel) == 3973
assert df_sel["HWLWcode"].iloc[0:2].tolist() == [1,1]
assert df_sel["HWLWcode"].iloc[-2:].tolist() == [1,1]
assert len(df_12) == 2823
assert df_12["HWLWcode"].iloc[0:2].tolist() == [1,1]
assert df_12["HWLWcode"].iloc[-2:].tolist() == [1,1]


@pytest.mark.unittest
def test_calc_HWLW12345to12_already_12():
file_ext = os.path.join(dir_testdata,'VLISSGN_ext.txt')

df = hatyan.read_dia(file_ext)

df_12 = hatyan.calc_HWLW12345to12(df)

assert len(df) == 1411
assert len(df_12) == 1411

0 comments on commit e262f72

Please sign in to comment.