Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:ArtesiaWater/hydropandas into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
OnnoEbbens committed Jun 26, 2024
2 parents 9dd47dc + 61f844d commit 4b5a57f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
54 changes: 30 additions & 24 deletions hydropandas/io/knmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,24 @@ def get_knmi_timeseries_fname(
if "neerslaggeg" in fname:
# neerslagstation
meteo_var = "RD"
adjust_time = True
add_day = True
elif "etmgeg" in fname:
# meteo station
adjust_time = True
add_day = True
elif "uurgeg" in fname:
adjust_time = False
add_day = False
# hourly station
# if that doesn't work try to figure out by the meteo_var and settings
elif meteo_var is None or meteo_var == "RD":
# neerslagstation
meteo_var = "RD"
adjust_time = True
add_day = True
elif settings["interval"] == "daily":
# meteo station
adjust_time = True
add_day = True
elif settings["interval"] == "hourly":
# uurlijks station
adjust_time = False
add_day = False
else:
raise ValueError(
"please indicate how to read the file by specifying a meteo_var and"
Expand All @@ -175,7 +175,7 @@ def get_knmi_timeseries_fname(
meteo_var=meteo_var,
start=start,
end=end,
adjust_time=adjust_time,
add_day=add_day,
)

stn = meta["station"]
Expand Down Expand Up @@ -376,7 +376,8 @@ def get_stations(meteo_var: str) -> pd.DataFrame:
os.path.join(dir_path, "../data/knmi_neerslagstation.json")
)

stations = pd.concat([mstations, pstations], axis=0).fillna(False)
stations = pd.concat([mstations, pstations], axis=0)
stations = stations.where(~stations.isna(), False)
if meteo_var in ("makkink", "penman", "hargreaves"):
meteo_var = "EV24"
return stations.loc[
Expand Down Expand Up @@ -644,19 +645,19 @@ def download_knmi_data(
df, meta = get_knmi_hourly_meteo_api(
stn=stn, meteo_var=meteo_var, start=start, end=end
)
adjust_time = False
add_day = False
elif meteo_var == "RD":
# daily data from rainfall-stations
df, meta = get_knmi_daily_rainfall_api(
stn=stn, start=start, end=end
)
adjust_time = True
add_day = True
else:
# daily data from meteorological stations
df, meta = get_knmi_daily_meteo_api(
stn=stn, meteo_var=meteo_var, start=start, end=end
)
adjust_time = True
add_day = True
if df.empty:
logger.warning(
f"No data for {meteo_var=} at {stn=} between"
Expand All @@ -669,7 +670,7 @@ def download_knmi_data(
meteo_var=meteo_var,
start=start,
end=end,
adjust_time=adjust_time,
add_day=add_day,
)

except (RuntimeError, requests.ConnectionError) as e:
Expand Down Expand Up @@ -704,7 +705,7 @@ def download_knmi_data(
meteo_var=meteo_var,
start=start,
end=end,
adjust_time=True,
add_day=True,
)
except (ValueError, KeyError, pd.errors.EmptyDataError) as e:
logger.error(f"{e} {msg}")
Expand Down Expand Up @@ -1112,7 +1113,8 @@ def interpret_knmi_file(
meteo_var: str,
start: Union[pd.Timestamp, None] = None,
end: Union[pd.Timestamp, None] = None,
adjust_time: bool = True,
add_day: bool = True,
add_hour: bool = True,
) -> Tuple[pd.DataFrame, Dict[str, Any]]:
"""interpret data from knmi by selecting meteo_var data and meta
and transforming the variables
Expand All @@ -1129,9 +1131,11 @@ def interpret_knmi_file(
start time of observations.
end : pd.TimeStamp or None
end time of observations.
adjust_time : boolean, optional
add 1 day and 1 hour to convert from GMT to CET winter time
and make observation time over the previous time interval
add_day : boolean, optional
add 1 day so that the timestamp is at the end of the period the data describes
add_hour : boolean, optional
add 1 hour to convert from UT to UT+1 (standard-time in the Netherlands)
Returns
-------
Expand All @@ -1154,14 +1158,16 @@ def interpret_knmi_file(
else:
stn = df.at[df.index[0], "STN"]

if adjust_time:
# step 1: add a full day for meteorological data, so that the
# timestamp is at the end of the period in the data
# step 2: from UT to UT+1 (standard-time in the Netherlands)
if add_day or add_hour:
if add_day and add_hour:
timedelta = pd.Timedelta(1, "d") + pd.Timedelta(1, "h")
elif add_hour:
timedelta = pd.Timedelta(1, "h")
else:
timedelta = pd.Timedelta(1, "d")

df = df.copy()
df.index = (
df.index + pd.to_timedelta(1, unit="d") + pd.to_timedelta(1, unit="h")
)
df.index = df.index + timedelta

if df.index.has_duplicates:
df = df.loc[~df.index.duplicated(keep="first")]
Expand Down
16 changes: 10 additions & 6 deletions hydropandas/obs_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ def __init__(self, *args, **kwargs):
if len(args) == 0:
logger.debug("Create empty ObsCollection")
super().__init__(**kwargs)
elif isinstance(args[0], ObsCollection):
super().__init__(*args, **kwargs)
elif len(args) == 0:
logger.debug("Create empty ObsCollection")
super().__init__(**kwargs)
elif isinstance(args[0], (list, tuple)):
logger.debug("Convert list of observations to ObsCollection")
obs_df = util._obslist_to_frame(args[0])
Expand All @@ -856,12 +861,11 @@ def __init__(self, *args, **kwargs):
remaining_args = [o for o in args if not isinstance(o, obs.Obs)]
obs_df = util._obslist_to_frame(obs_list)
super().__init__(obs_df, *remaining_args, **kwargs)
elif isinstance(args[0], pd.DataFrame):
if "obs" not in args[0].columns:
df = self.from_dataframe(*args)
super().__init__(df, **kwargs)
else:
super().__init__(*args, **kwargs)
elif isinstance(args[0], pd.DataFrame) and (
"obs_list" in kwargs or "ObsClass" in kwargs
):
df = self.from_dataframe(*args, **kwargs)
super().__init__(df, **kwargs)
else:
super().__init__(*args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ maintainers = [
{ name = "M.A. Vonk", email = "[email protected]" },
]
requires-python = ">=3.9"
dependencies = ["scipy", "pandas <= 2.2.1", "matplotlib", "tqdm", "requests", "colorama"]
dependencies = ["scipy", "pandas", "matplotlib", "tqdm", "requests", "colorama"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Science/Research",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_003_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_get_ground_level_gwobs():
try:
from art_tools import hpd_extension # noqa: F401

gw = ttf.observation_gw_old()
gw = ttf.observation_gw_dino_old()
gw.art.geo_get_ground_level()
return
except ModuleNotFoundError as e:
Expand Down

0 comments on commit 4b5a57f

Please sign in to comment.