Skip to content

Commit

Permalink
Merge pull request #156 from ricardogsilva/155-api-error-when-retriev…
Browse files Browse the repository at this point in the history
…ing-related-station-data

Adding logic for processing yearly station measurements
  • Loading branch information
francbartoli authored Jun 20, 2024
2 parents d569e19 + 3cdc907 commit 72a7139
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 90 deletions.
48 changes: 29 additions & 19 deletions arpav_ppcv/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,15 @@ def get_coverage_time_series(
)
if station_data is not None:
raw_station_data, station = station_data
data_ = _process_seasonal_station_data(
data_ = _process_station_data(
raw_station_data,
observation_smoothing_strategies,
start,
end,
base_name=coverage.configuration.related_observation_variable.name,
aggregation_type=(
coverage.configuration.observation_variable_aggregation_type
),
)
station_data_series_key = "_".join(
(
Expand Down Expand Up @@ -325,32 +328,39 @@ def _get_station_data(
return result


def _process_seasonal_station_data(
def _process_station_data(
raw_data: list[observations.SeasonalMeasurement],
smoothing_strategies: list[base.ObservationDataSmoothingStrategy],
time_start: Optional[dt.datetime],
time_end: Optional[dt.datetime],
base_name: str,
) -> pd.DataFrame:
aggregation_type: base.ObservationAggregationType,
):
df = pd.DataFrame([i.model_dump() for i in raw_data])
df = df[["value", "season", "year"]]
df = df.rename(columns={"value": base_name})

df["season_month"] = (
df["season"]
.astype("string")
.replace(
{
"Season.WINTER": "01",
"Season.SPRING": "04",
"Season.SUMMER": "07",
"Season.AUTUMN": "10",
}
if aggregation_type == base.ObservationAggregationType.SEASONAL:
df["season_month"] = (
df["season"]
.astype("string")
.replace(
{
"Season.WINTER": "01",
"Season.SPRING": "04",
"Season.SUMMER": "07",
"Season.AUTUMN": "10",
}
)
)
)
df["time"] = pd.to_datetime(
df["year"].astype("string") + "-" + df["season_month"] + "-01", utc=True
)
df["time"] = pd.to_datetime(
df["year"].astype("string") + "-" + df["season_month"] + "-01", utc=True
)
elif aggregation_type == base.ObservationAggregationType.YEARLY:
df["time"] = pd.to_datetime(df["year"].astype("string"), utc=True)
else:
raise RuntimeError(
f"Unable to process station data with aggregation type {aggregation_type}"
)

df = df[[base_name, "time"]]
df.set_index("time", inplace=True)
if time_start is not None:
Expand Down
211 changes: 140 additions & 71 deletions tests/notebooks/timeseries-smoothing-demo.ipynb

Large diffs are not rendered by default.

0 comments on commit 72a7139

Please sign in to comment.