Commit 19400dc0 authored by Niklas Selke's avatar Niklas Selke
Browse files

Set the 'seasons_per_statistic' to the correct value in all cases.

parent aa8b852a
......@@ -4,9 +4,9 @@ This module contains the following function:
calculate_statistics - calculate the requested statistics
"""
from toarstats.defaults import DEFAULT_DATA_CAPTURE
from toarstats.defaults import DEFAULT_CROPS, DEFAULT_DATA_CAPTURE
from toarstats.input_checks import check_input_parameters
from toarstats.stats_utils import create_reference_data_frame
from toarstats.stats_utils import create_reference_data_frame, get_seasons
def calculate_statistics(
......@@ -93,8 +93,10 @@ def calculate_statistics(
:param station_climatic_zone: station's climatic zone, used if
missing in the ``metadata`` argument
:raises KeyError: raised if any key in the metadata parameter is not
recognized
:raises KeyError: raised if
- any key in the metadata parameter is not
recognized
- a growing season is unknown
:raises TypeError: raised if any parameter has the wrong type or any
required parameter is missing
:raises ValueError: raised if
......@@ -106,6 +108,13 @@ def calculate_statistics(
sampling, statistics, data, metadata, seasons, crops, data_capture,
datetimes, values, station_lat, station_lon, station_climatic_zone
)
seasons_per_statistic = get_seasons(
input_parameters.sampling, input_parameters.statistics,
input_parameters.metadata, input_parameters.seasons,
input_parameters.crops if input_parameters.crops is not None
else DEFAULT_CROPS, input_parameters.required.seasons,
input_parameters.required.crops
)
resample_rule = ("seasonal" if input_parameters.sampling == "vegseason"
else input_parameters.sampling)
if input_parameters.data_capture is not None:
......
......@@ -7,6 +7,7 @@ create_reference_data_frame - create a reference data frame
dma8_processor - calculate the daily maximum 8-hour running mean
get_elevation_angle - calculate the solar elevation angle
get_growing_season - get the correct season name
get_seasons - get the correct seasons for each statistic
harmonize_time - harmonize the datetime index of all given data frames
kth_highest - calculate the kth highest value of the given data frame
perc - calculate percentiles
......@@ -24,7 +25,8 @@ import datetime as dt
import numpy as np
import pandas as pd
from toarstats.constants import CLIMATIC_ZONE, RSTAGS, SEASON_DICT
from toarstats.constants import RSTAGS, SEASON_DICT
from toarstats.defaults import DEFAULT_SEASONS
from toarstats.solar_position import calc_zenith
......@@ -198,16 +200,7 @@ def get_growing_season(croptype, climatic_zone, latitude):
:return: The growing season matching the crop type, climatic zone
and latitude
"""
if isinstance(climatic_zone, str):
try:
myclimzone = climatic_zone[
climatic_zone.index("(")+1:climatic_zone.index(")")
]
except ValueError:
myclimzone = climatic_zone
else:
myclimzone = CLIMATIC_ZONE[climatic_zone]
myclimzone = myclimzone.replace(" ", "_")
myclimzone = climatic_zone.replace(" ", "_")
res = f"{croptype}-{myclimzone}-{'NH' if latitude >= 0. else 'SH'}"
if (croptype == "wheat" and "cool_temperate" in myclimzone
and latitude < 0.):
......@@ -218,6 +211,43 @@ def get_growing_season(croptype, climatic_zone, latitude):
return res
def get_seasons(sampling, statistics, metadata, seasons, crops,
seasons_required, crops_required):
"""Get the correct seasons for each statistic.
:param sampling: the given sampling parameter
:param statistics: the given statistics parameter
:param metadata: the given metadata parameter
:param seasons: the given seasons parameter
:param crops: the given crops parameter
:param seasons_required: boolean denoting whether the seasons
parameter is required
:param crops_required: boolean denoting whether the crops parameter
is required
:raises KeyError: raised if a growing season is unknown
:return: A list containing the seasons for each statistic
"""
if sampling in ["summer", "xsummer"]:
return len(statistics)*[[
f"{'N' if metadata.station_lat > 0 else 'S'}H-"
f"{'X' if sampling == 'xsummer' else ''}Summer"
]]
if not seasons_required:
return len(statistics)*[None]
if seasons is not None:
return len(statistics)*[seasons]
if not crops_required:
return len(statistics)*[DEFAULT_SEASONS]
crops_season = [get_growing_season(crop, metadata.station_climatic_zone,
metadata.station_lat) for crop in crops]
if sampling == "vegseason":
return len(statistics)*[crops_season]
return [crops_season if "aot40" in stat or "w126" in stat
else DEFAULT_SEASONS for stat in statistics]
def harmonize_time(dflist, mtype, rsfreq=None):
"""Harmonize the datetime index of all given data frames.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment