Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small fix for tests :'( #135

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions pastastore/extensions/hpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import hydropandas as hpd
import numpy as np
from hydropandas.io.knmi import _check_latest_measurement_date_de_bilt, get_stations
from hydropandas.io.knmi import get_stations
from pandas import DataFrame, Series, Timedelta, Timestamp
from pastas.timeseries_utils import timestep_weighted_resample
from tqdm.auto import tqdm
Expand Down Expand Up @@ -145,8 +145,8 @@ def add_observation(
return

if normalize_datetime_index and o.index.size > 1:
o = self._normalize_datetime_index(o)
else:
o = self._normalize_datetime_index(o).iloc[1:] # remove first nan
elif normalize_datetime_index and o.index.size <= 1:
raise ValueError(
"Must have minimum of 2 observations for timestep_weighted_resample."
)
Expand Down Expand Up @@ -202,7 +202,7 @@ def add_observation(
def download_knmi_precipitation(
self,
stns: Optional[list[int]] = None,
meteo_var: str = "RH",
meteo_var: str = "RD",
tmin: TimeType = None,
tmax: TimeType = None,
unit_multiplier: float = 1e3,
Expand All @@ -217,7 +217,7 @@ def download_knmi_precipitation(
stns : list of int/str, optional
list of station numbers to download data for, by default None
meteo_var : str, optional
variable to download, by default "RH", valid options are ["RD", "RH"].
variable to download, by default "RD", valid options are ["RD", "RH"].
tmin : TimeType, optional
start time, by default None
tmax : TimeType, optional
Expand Down Expand Up @@ -359,9 +359,9 @@ def update_knmi_meteo(
names: Optional[List[str]] = None,
tmin: TimeType = None,
tmax: TimeType = None,
fill_missing_obs=True,
normalize_datetime_index=True,
raise_on_error=False,
fill_missing_obs: bool = True,
normalize_datetime_index: bool = True,
raise_on_error: bool = False,
**kwargs,
):
"""Update meteorological data from KNMI in PastaStore.
Expand Down Expand Up @@ -398,24 +398,27 @@ def update_knmi_meteo(
logger.info(f"All KNMI stresses are up to date to {tmax}.")
return

maxtmax_rd = _check_latest_measurement_date_de_bilt("RD")
maxtmax_ev24 = _check_latest_measurement_date_de_bilt("EV24")
# NOTE: this check is very flaky (15 august 2024), perhaps I annoyed the
# KNMI server... Trying to skip this check and just attempt downloading data.
# maxtmax_rd = _check_latest_measurement_date_de_bilt("RD")
# maxtmax_ev24 = _check_latest_measurement_date_de_bilt("EV24")
maxtmax = Timestamp.today() - Timedelta(days=1)

for name in tqdm(names, desc="Updating KNMI meteo stresses"):
meteo_var = self._store.stresses.loc[name, "meteo_var"]
if meteo_var == "RD":
maxtmax = maxtmax_rd
elif meteo_var == "EV24":
maxtmax = maxtmax_ev24
else:
maxtmax = maxtmax_rd

# if meteo_var == "RD":
# maxtmax = maxtmax_rd
# elif meteo_var == "EV24":
# maxtmax = maxtmax_ev24
# else:
# maxtmax = maxtmax_rd

# 1 days extra to ensure computation of daily totals using
# timestep_weighted_resample
if tmin is None:
# 1 days extra to ensure computation of daily totals using
# timestep_weighted_resample
itmin = tmintmax.loc[name, "tmax"] - Timedelta(days=1)
else:
itmin = tmin
itmin = tmin - Timedelta(days=1)

# ensure 2 observations at least
if itmin >= (maxtmax + Timedelta(days=1)):
Expand All @@ -425,7 +428,7 @@ def update_knmi_meteo(
if tmax is None:
itmax = maxtmax
else:
itmax = tmax
itmax = Timestamp(tmax)

unit = self._store.stresses.loc[name, "unit"]
kind = self._store.stresses.loc[name, "kind"]
Expand Down Expand Up @@ -464,7 +467,7 @@ def update_knmi_meteo(
try:
self.add_observation(
"stresses",
obs.loc[tmintmax.loc[name, "tmax"] :],
obs,
name=name,
kind=kind,
data_column=meteo_var,
Expand Down
2 changes: 1 addition & 1 deletion pastastore/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PASTAS_LEQ_022 = PASTAS_VERSION <= parse_version("0.22.0")
PASTAS_GEQ_150 = PASTAS_VERSION >= parse_version("1.5.0")

__version__ = "1.6.0"
__version__ = "1.6.1"


def show_versions(optional=False) -> None:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_007_hpdextension.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_hpd_download_from_bro():
assert pstore.n_oseries == 3


@pytest.mark.xfail(reason="KNMI is being flaky, so allow this test to xfail/xpass.")
@pytest.mark.pastas150
def test_hpd_download_precipitation_from_knmi():
from pastastore.extensions import activate_hydropandas_extension
Expand All @@ -29,6 +30,7 @@ def test_hpd_download_precipitation_from_knmi():
assert pstore.n_stresses == 1


@pytest.mark.xfail(reason="KNMI is being flaky, so allow this test to xfail/xpass.")
@pytest.mark.pastas150
def test_hpd_download_evaporation_from_knmi():
from pastastore.extensions import activate_hydropandas_extension
Expand All @@ -54,13 +56,14 @@ def test_update_oseries():
assert tmintmax.loc["GMW000000036327_1", "tmax"] >= Timestamp("2024-01-20")


@pytest.mark.xfail(reason="KNMI is being flaky, so allow this test to xfail/xpass.")
@pytest.mark.pastas150
def test_update_stresses():
from pastastore.extensions import activate_hydropandas_extension

activate_hydropandas_extension()

pstore = pst.PastaStore.from_zip("tests/data/test_hpd_update.zip")
pstore.hpd.update_knmi_meteo(tmax="2024-01-31")
pstore.hpd.update_knmi_meteo(tmax="2024-01-31", normalize_datetime_index=False)
tmintmax = pstore.get_tmin_tmax("stresses")
assert (tmintmax["tmax"] >= Timestamp("2024-01-31")).all()
Loading