-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_var_boxplot.py
123 lines (115 loc) · 5.02 KB
/
plot_var_boxplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Plot Variable boxplot."""
import datetime as dt
from math import ceil
from pathlib import Path
import bgc_data_processing as bgc_dp
import matplotlib.pyplot as plt
CONFIG_FOLDER = Path("config")
if __name__ == "__main__":
config_filepath = CONFIG_FOLDER.joinpath(Path(__file__).stem)
CONFIG = bgc_dp.parsers.ConfigParser(
filepath=config_filepath.with_suffix(".toml"),
dates_vars_keys=["DATE_MIN", "DATE_MAX"],
dirs_vars_keys=["SAVING_DIR"],
existing_directory="raise",
)
LOADING_DIR: Path = Path(CONFIG["LOADING_DIR"])
SHOW: bool = CONFIG["SHOW"]
SAVE: bool = CONFIG["SAVE"]
PLOT_VARIABLE: str = CONFIG["PLOT_VARIABLE"]
BOXPLOT_PERIOD: str = CONFIG["BOXPLOT_PERIOD"]
DATE_MIN: dt.datetime = CONFIG["DATE_MIN"]
DATE_MAX: dt.datetime = CONFIG["DATE_MAX"]
LATITUDE_MIN: int | float = CONFIG["LATITUDE_MIN"]
LATITUDE_MAX: int | float = CONFIG["LATITUDE_MAX"]
LONGITUDE_MIN: int | float = CONFIG["LONGITUDE_MIN"]
LONGITUDE_MAX: int | float = CONFIG["LONGITUDE_MAX"]
ACRONYMS: list[str] = CONFIG["WATER_MASS_ACRONYMS"]
WATER_MASSES: list[bgc_dp.WaterMass] = [
bgc_dp.defaults.WATER_MASSES[acro] for acro in ACRONYMS
]
EXPOCODES_TO_LOAD: list[str] = CONFIG["EXPOCODES_TO_LOAD"]
PRIORITY: list[str] = CONFIG["PRIORITY"]
VERBOSE: int = CONFIG["VERBOSE"]
bgc_dp.set_verbose_level(VERBOSE)
SALINITY_DEFAULT = bgc_dp.defaults.VARS["salinity"]
TEMPERATURE_DEFAULT = bgc_dp.defaults.VARS["temperature"]
storer = bgc_dp.io.read_files(
filepath=list(LOADING_DIR.glob("*.txt")),
providers_column_label=bgc_dp.defaults.VARS["provider"].label,
expocode_column_label=bgc_dp.defaults.VARS["expocode"].label,
date_column_label=bgc_dp.defaults.VARS["date"].label,
year_column_label=bgc_dp.defaults.VARS["year"].label,
month_column_label=bgc_dp.defaults.VARS["month"].label,
day_column_label=bgc_dp.defaults.VARS["day"].label,
hour_column_label=bgc_dp.defaults.VARS["hour"].label,
latitude_column_label=bgc_dp.defaults.VARS["latitude"].label,
longitude_column_label=bgc_dp.defaults.VARS["longitude"].label,
depth_column_label=bgc_dp.defaults.VARS["depth"].label,
variables_reference=bgc_dp.defaults.VARS.to_list(),
category="in_situ",
unit_row_index=1,
delim_whitespace=True,
)
storer.remove_duplicates(PRIORITY)
variables = storer.variables
# Add relevant bgc_dp.features to the data: Pressure / potential temperature /sigmat
pres_feat = bgc_dp.features.Pressure(
depth_variable=variables.get(variables.depth_var_name),
latitude_variable=variables.get(variables.latitude_var_name),
)
if not storer.variables.has_name(pres_feat.variable.name):
pres_feat.insert_in_storer(storer)
ptemp_feat = bgc_dp.features.PotentialTemperature(
salinity_variable=SALINITY_DEFAULT,
temperature_variable=TEMPERATURE_DEFAULT,
pressure_variable=pres_feat.variable,
)
if not storer.variables.has_name(ptemp_feat.variable.name):
ptemp_feat.insert_in_storer(storer)
sigmat_feat = bgc_dp.features.SigmaT(
salinity_variable=SALINITY_DEFAULT,
temperature_variable=TEMPERATURE_DEFAULT,
)
if not storer.variables.has_name(sigmat_feat.variable.name):
sigmat_feat.insert_in_storer(storer)
constraints = bgc_dp.Constraints()
constraints.add_superset_constraint(
field_label=variables.get(variables.expocode_var_name).label,
values_superset=EXPOCODES_TO_LOAD,
)
constraints.add_boundary_constraint(
field_label=variables.get(variables.date_var_name).label,
minimal_value=DATE_MIN,
maximal_value=DATE_MAX,
)
constraints.add_boundary_constraint(
field_label=variables.get(variables.latitude_var_name).label,
minimal_value=LATITUDE_MIN,
maximal_value=LATITUDE_MAX,
)
constraints.add_boundary_constraint(
field_label=variables.get(variables.longitude_var_name).label,
minimal_value=LONGITUDE_MIN,
maximal_value=LONGITUDE_MAX,
)
nb_wmasses = len(WATER_MASSES)
figure = plt.figure(figsize=(15, 15), layout="tight")
for i, watermass in enumerate(WATER_MASSES):
placement = f"{ceil(nb_wmasses/min(nb_wmasses,3))}{min(nb_wmasses,3)}{i+1}"
axes = figure.add_subplot(int(placement))
storer_wm = watermass.extract_from_storer(
storer=storer,
ptemperature_name=ptemp_feat.variable.label,
salinity_name=SALINITY_DEFAULT.label,
sigma_t_name=sigmat_feat.variable.label,
)
plot = bgc_dp.tracers.VariableBoxPlot(storer_wm, constraints)
plot.plot_to_axes(PLOT_VARIABLE, period=BOXPLOT_PERIOD, ax=axes)
axes.set_title(f"{watermass.name} ({watermass.acronym})")
plt.suptitle(f"{PLOT_VARIABLE} Box Plots")
if SAVE:
filename = f"{PLOT_VARIABLE}_boxplots.png"
plt.savefig(Path(CONFIG["SAVING_DIR"]).joinpath(filename))
if SHOW:
plt.show()