Commit 396b459d authored by Niklas Selke's avatar Niklas Selke
Browse files

Added 'constants.py' which contains constants used throughout the different...

Added 'constants.py' which contains constants used throughout the different modules. The 'check_statistics' function now returns a list.
parent 4c0b473f
"""Collection of constants.
This module contains the following constants:
ALLOWED_SAMPLING_VALUES - all possible values for the sampling argument
STATISTICS_LIST - list of all available statistics and metrics
CLIMATIC_ZONE - dictionary containing all available climatic zones
SEASON_DICT - mapping of season names to months
ALLOWED_CROPS_VALUES - all possible values for the crops argument
RSTAGS - mapping of aggregation type to resampling tag
"""
ALLOWED_SAMPLING_VALUES = [
"daily", "monthly", "seasonal", "vegseason", "summer", "xsummer", "annual"
]
STATISTICS_LIST = [
"aot40", "average_values", "avgdma8epax", "dark_aot40", "dark_avg",
"data_capture", "daylight_aot40", "daylight_avg", "daytime_avg",
"diurnal_cycle", "dma8epa", "dma8epa_strict", "dma8epax",
"dma8epax_strict", "dma8eu", "dma8eu_strict", "drmdmax1h", "m7_avg",
"max1h_values", "maximum", "median", "minimum", "nighttime_avg", "nvgt050",
"nvgt060", "nvgt070", "nvgt080", "nvgt090", "nvgt100", "nvgt120",
"nvgtall", "perc05", "perc10", "perc25", "perc75", "perc90", "perc95",
"perc98", "perc99", "percentiles1", "percentiles2", "somo10",
"somo10_strict", "somo35", "somo35_strict", "standard_deviation",
"value_count", "w126", "w126_24h", "w90"
]
CLIMATIC_ZONE = {
-1: "undefined", 0: "unclassified", 1: "warm temperate moist",
2: "warm temperate dry", 3: "cool temperate moist",
4: "cool temperate dry", 5: "polar moist", 6: "polar dry",
7: "boreal moist", 8: "boreal dry", 9: "tropical montane",
10: "tropical wet", 11: "tropical moist", 12: "tropical dry"
}
SEASON_DICT = {
"DJF": [12, 1, 2], "MAM": [3, 4, 5], "JJA": [6, 7, 8], "SON": [9, 10, 11],
"NH-Summer": [4, 5, 6, 7, 8, 9], "SH-Summer": [10, 11, 12, 1, 2, 3],
"NH-XSummer": [3, 4, 5, 6, 7, 8, 9],
"SH-XSummer": [9, 10, 11, 12, 1, 2, 3], "wheat-boreal_moist-NH": [6, 7, 8],
"wheat-boreal_dry-NH": [6, 7, 8],
"wheat-cool_temperate_moist-NH": [4, 5, 6],
"wheat-cool_temperate_dry-NH": [4, 5, 6],
"wheat-warm_temperate_moist-NH": [3, 4, 5],
"wheat-warm_temperate_dry-NH": [3, 4, 5],
"wheat-tropical_montane-NH": [1, 2, 3], "wheat-tropical_wet-NH": [1, 2, 3],
"wheat-tropical_moist-NH": [1, 2, 3], "wheat-tropical_dry-NH": [1, 2, 3],
"wheat-tropical_moist-SH": [7, 8, 9], "wheat-tropical_dry-SH": [8, 9, 10],
"wheat-warm_temperate_moist-SH": [8, 9, 10],
"wheat-warm_temperate_dry-SH": [8, 9, 10],
"wheat-cool_temperate_moist_le30-SH": [2, 3, 4],
"wheat-cool_temperate_dry_le30-SH": [2, 3, 4],
"wheat-cool_temperate_moist_gt30-SH": [11, 12, 1],
"wheat-cool_temperate_dry_gt30-SH": [11, 12, 1],
"rice-cool_temperate_moist-NH": [5, 6, 7],
"rice-cool_temperate_dry-NH": [5, 6, 7],
"rice-warm_temperate_moist-NH": [6, 7, 8],
"rice-warm_temperate_dry-NH": [6, 7, 8],
"rice-tropical_moist-NH": [7, 8, 9], "rice-tropical_montane-NH": [7, 8, 9],
"rice-tropical_wet-NH": [7, 8, 9], "rice-tropical_dry-NH": [8, 9, 10],
"rice-tropical_moist-SH": [12, 1, 2], "rice-tropical_wet-SH": [12, 1, 2],
"rice-tropical_dry-SH": [1, 2, 3],
"rice-warm_temperate_moist-SH": [11, 12, 1],
"rice-warm_temperate_dry-SH": [1, 2, 3],
"rice-cool_temperate_moist-SH": [12, 1, 2],
"rice-cool_temperate_dry-SH": [12, 1, 2], "Jan": [1], "Feb": [2],
"Mar": [3], "Apr": [4], "May": [5], "Jun": [6], "Jul": [7], "Aug": [8],
"Sep": [9], "Oct": [10], "Nov": [11], "Dec": [12]
}
ALLOWED_CROPS_VALUES = ["rice", "wheat"]
RSTAGS = {
"daily": "D", "monthly": "MS", "seasonal": "AS", "summer": "AS",
"xsummer": "AS", "annual": "AS"
}
"""Collection of default values for the package. """Collection of default values.
This module contains the following constants: This module contains the following constants:
DEFAULT_SEASONS - default list of seasons DEFAULT_SEASONS - default list of seasons
......
...@@ -16,12 +16,8 @@ from collections import namedtuple ...@@ -16,12 +16,8 @@ from collections import namedtuple
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from toarstats import ozone_metrics, stats from toarstats.constants import (ALLOWED_CROPS_VALUES, ALLOWED_SAMPLING_VALUES,
from toarstats.stats_utils import CLIMATIC_ZONE, RSTAGS, SEASON_DICT CLIMATIC_ZONE, SEASON_DICT, STATISTICS_LIST)
SAMPLING_METHODS = ",".join(RSTAGS).replace("seasonal",
"seasonal,vegseason").split(",")
def check_sampling(sampling_in): def check_sampling(sampling_in):
...@@ -38,9 +34,9 @@ def check_sampling(sampling_in): ...@@ -38,9 +34,9 @@ def check_sampling(sampling_in):
if not isinstance(sampling_in, str): if not isinstance(sampling_in, str):
raise ValueError("The sampling argument must be a string") raise ValueError("The sampling argument must be a string")
sampling_out = sampling_in.strip() sampling_out = sampling_in.strip()
if sampling_out not in SAMPLING_METHODS: if sampling_out not in ALLOWED_SAMPLING_VALUES:
raise ValueError("The sampling argument must be one of" raise ValueError("The sampling argument must be one of"
f" {SAMPLING_METHODS}") f" {ALLOWED_SAMPLING_VALUES}")
return sampling_out return sampling_out
...@@ -55,8 +51,7 @@ def check_statistics(statistics_in): ...@@ -55,8 +51,7 @@ def check_statistics(statistics_in):
recognized or if the ``diurnal_cycle`` statistic recognized or if the ``diurnal_cycle`` statistic
is not given alone is not given alone
:return: A dictionary of statistics to calculate matched to their :return: A list of statistics and metrics to calculate
respective function
""" """
if not isinstance(statistics_in, (list, set, tuple, str)): if not isinstance(statistics_in, (list, set, tuple, str)):
raise ValueError("The statistics argument must be a list, set, tuple" raise ValueError("The statistics argument must be a list, set, tuple"
...@@ -66,18 +61,18 @@ def check_statistics(statistics_in): ...@@ -66,18 +61,18 @@ def check_statistics(statistics_in):
statistics_in_list = ([statistics_in] statistics_in_list = ([statistics_in]
if isinstance(statistics_in, str) if isinstance(statistics_in, str)
else list(statistics_in)) else list(statistics_in))
statistics_out = {} statistics_out = []
for stat in statistics_in_list: for stat in statistics_in_list:
if not isinstance(stat, str): if not isinstance(stat, str):
raise ValueError("Statistics must be given as strings") raise ValueError("Statistics must be given as strings")
stat = stat.strip() stat = stat.strip()
if stat in statistics_out: if stat in statistics_out:
continue continue
func = getattr(stats, stat, getattr(ozone_metrics, stat, None)) if stat in STATISTICS_LIST:
if func is not None: statistics_out.append(stat)
statistics_out[stat] = func
else: else:
raise ValueError(f"{stat} is not a recognized statistic") raise ValueError(f"{stat} is not a recognized statistic, available"
f" are {STATISTICS_LIST}")
if "diurnal_cycle" in statistics_out and len(statistics_out) > 1: if "diurnal_cycle" in statistics_out and len(statistics_out) > 1:
raise ValueError("Diurnal cycle can only be evaluated alone") raise ValueError("Diurnal cycle can only be evaluated alone")
return statistics_out return statistics_out
...@@ -276,7 +271,8 @@ def check_seasons(seasons_in, seasons_req): ...@@ -276,7 +271,8 @@ def check_seasons(seasons_in, seasons_req):
if season in SEASON_DICT: if season in SEASON_DICT:
seasons_out.append(season) seasons_out.append(season)
else: else:
raise ValueError(f"{season} is not a recognized season") raise ValueError(f"{season} is not a recognized season, available"
f" are {SEASON_DICT}")
return seasons_out return seasons_out
...@@ -310,10 +306,11 @@ def check_crops(crops_in, crops_req): ...@@ -310,10 +306,11 @@ def check_crops(crops_in, crops_req):
crop = crop.strip() crop = crop.strip()
if crop in crops_out: if crop in crops_out:
continue continue
if crop in {"wheat", "rice"}: if crop in ALLOWED_CROPS_VALUES:
crops_out.append(crop) crops_out.append(crop)
else: else:
raise ValueError(f"{crop} is not a recognized crop") raise ValueError(f"{crop} is not a recognized crop, available are"
f" {ALLOWED_CROPS_VALUES}")
return crops_out return crops_out
......
"""Collection of dictionaries and helper functions for the package. """Collection of dictionaries and helper functions for the package.
This module contains the following constants:
CLIMATIC_ZONE - dictionary containing all available climatic zones
RSTAGS - mapping of aggregation type to resampling tag
SEASON_DICT - mapping of season names to months
This module contains the following functions: This module contains the following functions:
aot40stat - evaluate the sum of values above the given threshold aot40stat - evaluate the sum of values above the given threshold
calc_data_capture - calculate the data capture fraction calc_data_capture - calculate the data capture fraction
...@@ -28,88 +23,10 @@ import datetime as dt ...@@ -28,88 +23,10 @@ import datetime as dt
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from toarstats.constants import CLIMATIC_ZONE, RSTAGS, SEASON_DICT
from toarstats.solar_position import calc_zenith from toarstats.solar_position import calc_zenith
CLIMATIC_ZONE = {-1: "undefined",
0: "unclassified",
1: "warm temperate moist",
2: "warm temperate dry",
3: "cool temperate moist",
4: "cool temperate dry",
5: "polar moist",
6: "polar dry",
7: "boreal moist",
8: "boreal dry",
9: "tropical montane",
10: "tropical wet",
11: "tropical moist",
12: "tropical dry"}
RSTAGS = {"daily": "D",
"monthly": "MS",
"seasonal": "AS",
"summer": "AS",
"xsummer": "AS",
"annual": "AS"}
SEASON_DICT = {"DJF": [12, 1, 2],
"MAM": [3, 4, 5],
"JJA": [6, 7, 8],
"SON": [9, 10, 11],
"NH-Summer": [4, 5, 6, 7, 8, 9],
"SH-Summer": [10, 11, 12, 1, 2, 3],
"NH-XSummer": [3, 4, 5, 6, 7, 8, 9],
"SH-XSummer": [9, 10, 11, 12, 1, 2, 3],
"wheat-boreal_moist-NH": [6, 7, 8],
"wheat-boreal_dry-NH": [6, 7, 8],
"wheat-cool_temperate_moist-NH": [4, 5, 6],
"wheat-cool_temperate_dry-NH": [4, 5, 6],
"wheat-warm_temperate_moist-NH": [3, 4, 5],
"wheat-warm_temperate_dry-NH": [3, 4, 5],
"wheat-tropical_montane-NH": [1, 2, 3],
"wheat-tropical_wet-NH": [1, 2, 3],
"wheat-tropical_moist-NH": [1, 2, 3],
"wheat-tropical_dry-NH": [1, 2, 3],
"wheat-tropical_moist-SH": [7, 8, 9],
"wheat-tropical_dry-SH": [8, 9, 10],
"wheat-warm_temperate_moist-SH": [8, 9, 10],
"wheat-warm_temperate_dry-SH": [8, 9, 10],
"wheat-cool_temperate_moist_le30-SH": [2, 3, 4],
"wheat-cool_temperate_dry_le30-SH": [2, 3, 4],
"wheat-cool_temperate_moist_gt30-SH": [11, 12, 1],
"wheat-cool_temperate_dry_gt30-SH": [11, 12, 1],
"rice-cool_temperate_moist-NH": [5, 6, 7],
"rice-cool_temperate_dry-NH": [5, 6, 7],
"rice-warm_temperate_moist-NH": [6, 7, 8],
"rice-warm_temperate_dry-NH": [6, 7, 8],
"rice-tropical_moist-NH": [7, 8, 9],
"rice-tropical_montane-NH": [7, 8, 9],
"rice-tropical_wet-NH": [7, 8, 9],
"rice-tropical_dry-NH": [8, 9, 10],
"rice-tropical_moist-SH": [12, 1, 2],
"rice-tropical_wet-SH": [12, 1, 2],
"rice-tropical_dry-SH": [1, 2, 3],
"rice-warm_temperate_moist-SH": [11, 12, 1],
"rice-warm_temperate_dry-SH": [1, 2, 3],
"rice-cool_temperate_moist-SH": [12, 1, 2],
"rice-cool_temperate_dry-SH": [12, 1, 2],
"Jan": [1],
"Feb": [2],
"Mar": [3],
"Apr": [4],
"May": [5],
"Jun": [6],
"Jul": [7],
"Aug": [8],
"Sep": [9],
"Oct": [10],
"Nov": [11],
"Dec": [12]}
def aot40stat(df, dfref, sampling, how, mincount=None, minfrac=0.75, def aot40stat(df, dfref, sampling, how, mincount=None, minfrac=0.75,
month_list=None, threshold=0.): month_list=None, threshold=0.):
"""Evaluate the sum of values above the given threshold. """Evaluate the sum of values above the given threshold.
......
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