Commit 85a1fcb5 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

add altitude argument to adamont studies. refactor a bit. add time series...

add altitude argument to adamont studies. refactor a bit. add time series display for adamont series
parent 146e4da1
No related merge requests found
Showing with 120 additions and 42 deletions
+120 -42
......@@ -43,8 +43,14 @@ class AbstractAdamontStudy(AbstractStudy):
for i, massif_name in enumerate(self.all_massif_names()):
assert massif_name == massif_number_to_massif_name[i + 1]
@property
def variable_name(self):
return scenario_to_str(self.scenario) + ' ' + super().variable_name
# Loading part
@cached_property
def ordered_years(self):
return list(range(self.year_min, self.year_max + 1))
......@@ -92,7 +98,7 @@ class AbstractAdamontStudy(AbstractStudy):
# PATHS
@property
def variable_name(self):
def variable_folder_name(self):
return self.variable_class.variable_name_for_folder_and_nc_file()
@property
......@@ -105,11 +111,11 @@ class AbstractAdamontStudy(AbstractStudy):
@property
def nc_files_path(self):
return op.join(ADAMONT_PATH, self.variable_name, self.scenario_name)
return op.join(ADAMONT_PATH, self.variable_folder_name, self.scenario_name)
@property
def nc_file_path(self):
suffix_nc_file = get_suffix_for_the_nc_file(self.scenario, self.gcm_rcm_couple)
nc_file = '{}_FORCING_{}_{}_{}_{}.nc'.format(self.variable_name, self.gcm_rcm_full_name, self.scenario_name,
nc_file = '{}_FORCING_{}_{}_{}_{}.nc'.format(self.variable_folder_name, self.gcm_rcm_full_name, self.scenario_name,
self.region_name, suffix_nc_file)
return op.join(self.nc_files_path, nc_file)
......@@ -6,6 +6,8 @@ from extreme_data.meteo_france_data.adamont_data.adamont.adamont_variables impor
SafranSnowfallSimulationVariable
from extreme_data.meteo_france_data.scm_models_data.abstract_study import YEAR_MIN, YEAR_MAX
from extreme_data.meteo_france_data.scm_models_data.abstract_variable import AbstractVariable
from extreme_data.meteo_france_data.scm_models_data.safran.safran import SafranSnowfall1Day
from extreme_data.meteo_france_data.scm_models_data.safran.safran_variable import SafranSnowfallVariable
from extreme_data.meteo_france_data.scm_models_data.utils import Season, FrenchRegion
......@@ -20,7 +22,6 @@ class AdamontSnowfall(AbstractAdamontStudy):
year_min, year_max,
multiprocessing, season, french_region, scenario, gcm_rcm_couple)
if __name__ == '__main__':
study = AdamontSnowfall(altitude=1800)
print(study.year_to_annual_maxima)
import numpy as np
from extreme_data.meteo_france_data.scm_models_data.abstract_variable import AbstractVariable
from extreme_data.meteo_france_data.scm_models_data.safran.safran_variable import SafranSnowfallVariable
class AbstractAdamontVariable(AbstractVariable):
......@@ -11,7 +12,8 @@ class AbstractAdamontVariable(AbstractVariable):
class SafranSnowfallSimulationVariable(AbstractAdamontVariable):
UNIT = 'kg $m^{-2}$'
UNIT = SafranSnowfallVariable.UNIT
NAME = SafranSnowfallVariable.NAME
@property
def daily_time_serie_array(self) -> np.ndarray:
......
......@@ -24,6 +24,16 @@ def get_year_min_and_year_max_from_scenario(adamont_scenario, gcm_rcm_couple):
else:
return 2006, 2100
def load_gcm_rcm_couples_for_year_min_and_year_max(year_min, year_max):
gcm_rcm_couples = []
for gcm_rcm_couple in gcm_rcm_couple_to_full_name.keys():
year_min_couple, year_max_couple = get_year_min_and_year_max_from_scenario(adamont_scenario=AdamontScenario.histo,
gcm_rcm_couple=gcm_rcm_couple)
if year_min_couple <= year_min and year_max <= year_max_couple:
gcm_rcm_couples.append(gcm_rcm_couple)
return gcm_rcm_couples
def get_suffix_for_the_nc_file(adamont_scenario, gcm_rcm_couple):
year_min, year_max = get_year_min_and_year_max_from_scenario(adamont_scenario, gcm_rcm_couple)
......@@ -34,6 +44,9 @@ def scenario_to_str(adamont_scenario):
return str(adamont_scenario).split('.')[-1].upper()
def gcm_rcm_couple_to_str(gcm_rcm_couple):
return ' / '.join(gcm_rcm_couple)
def get_color_from_gcm_rcm_couple(gcm_rcm_couple):
return gcm_rcm_couple_to_color[gcm_rcm_couple]
......
import matplotlib.pyplot as plt
from collections import OrderedDict
from cached_property import cached_property
from extreme_data.meteo_france_data.adamont_data.adamont_scenario import gcm_rcm_couple_to_full_name
from extreme_data.meteo_france_data.adamont_data.adamont_scenario import gcm_rcm_couple_to_full_name, \
gcm_rcm_couple_to_str, get_color_from_gcm_rcm_couple
from extreme_data.meteo_france_data.scm_models_data.abstract_study import AbstractStudy
from extreme_data.meteo_france_data.scm_models_data.visualization.main_study_visualizer import \
SCM_STUDY_CLASS_TO_ABBREVIATION
from extreme_data.meteo_france_data.scm_models_data.visualization.study_visualizer import StudyVisualizer
class AdamontStudies(object):
......@@ -13,15 +19,61 @@ class AdamontStudies(object):
if gcm_rcm_couples is None:
gcm_rcm_couples = list(gcm_rcm_couple_to_full_name.keys())
self.gcm_rcm_couples = gcm_rcm_couples
self.gcm_rcm_couples_to_study = OrderedDict() # type: OrderedDict[int, AbstractStudy]
self.gcm_rcm_couple_to_study = OrderedDict() # type: OrderedDict[int, AbstractStudy]
for gcm_rcm_couple in self.gcm_rcm_couples:
study = study_class(gcm_rcm_couple=gcm_rcm_couple, **kwargs_study)
self.gcm_rcm_couples_to_study[gcm_rcm_couple] = study
self.gcm_rcm_couple_to_study[gcm_rcm_couple] = study
@property
def study_list(self):
return list(self.gcm_rcm_couples_to_study.values())
return list(self.gcm_rcm_couple_to_study.values())
@cached_property
def study(self) -> AbstractStudy:
return self.study_list[0]
\ No newline at end of file
return self.study_list[0]
# Some plots
def show_or_save_to_file(self, plot_name, show=False, no_title=False, tight_layout=None):
study_visualizer = StudyVisualizer(study=self.study, show=show, save_to_file=not show)
study_visualizer.plot_name = plot_name
study_visualizer.show_or_save_to_file(add_classic_title=False, dpi=500, no_title=no_title,
tight_layout=tight_layout)
def plot_maxima_time_series(self, massif_names=None, scm_study=None):
massif_names = massif_names if massif_names is not None else self.study.all_massif_names()
for massif_names in massif_names:
self._plot_maxima_time_series(massif_names, scm_study)
def _plot_maxima_time_series(self, massif_name, scm_study=None):
ax = plt.gca()
x = self.study.ordered_years
linewidth = 2
for gcm_rcm_couple, study in list(self.gcm_rcm_couple_to_study.items())[::-1]:
if massif_name in study.massif_name_to_annual_maxima:
y = study.massif_name_to_annual_maxima[massif_name]
label = gcm_rcm_couple_to_str(gcm_rcm_couple)
color = get_color_from_gcm_rcm_couple(gcm_rcm_couple)
ax.plot(x, y, linewidth=linewidth, label=label, color=color)
if scm_study is not None:
try:
print(type(scm_study))
y = scm_study.massif_name_to_annual_maxima[massif_name]
label = 'Reanalysis'
color = 'black'
ax.plot(x, y, linewidth=linewidth * 2, label=label, color=color)
except KeyError:
pass
ax.xaxis.set_ticks(x[1::10])
ax.tick_params(axis='both', which='major', labelsize=13)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1])
plot_name = 'Annual maxima of {} in {}'.format(SCM_STUDY_CLASS_TO_ABBREVIATION[self.study_class],
massif_name.replace('_', ' '))
ax.set_ylabel('{} ({})'.format(plot_name, self.study.variable_unit), fontsize=15)
ax.set_xlabel('years', fontsize=15)
plot_name = 'time series/' + plot_name
self.show_or_save_to_file(plot_name=plot_name, show=False, no_title=True, tight_layout=True)
ax.clear()
plt.close()
......@@ -57,7 +57,7 @@ SCM_STUDY_CLASS_TO_ABBREVIATION = {
CrocusSnowDepthDifference: 'max HS - HS at max of GSL',
CrocusSnowDepthAtMaxofSwe: 'HS at max of GSL',
CrocusSnowDensity: 'Density',
SafranSnowfall: 'SF1 RCP85 projections',
AdamontSnowfall: 'daily snowfall',
SafranDateFirstSnowfall: 'SF1 first date'
}
......
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.lines import Line2D
from extreme_data.meteo_france_data.adamont_data.adamont_scenario import get_color_from_gcm_rcm_couple
from extreme_data.meteo_france_data.adamont_data.adamont_scenario import get_color_from_gcm_rcm_couple, \
gcm_rcm_couple_to_str
from extreme_data.meteo_france_data.adamont_data.adamont_studies import AdamontStudies
from extreme_data.meteo_france_data.scm_models_data.abstract_study import AbstractStudy
from extreme_data.meteo_france_data.scm_models_data.visualization.main_study_visualizer import \
......@@ -18,7 +21,7 @@ class ComparisonHistoricalVisualizer(StudyVisualizer):
show=False,
massif_names=None,
):
super().__init__(scm_study, show=show, save_to_file=not show)
super().__init__(adamont_studies.study, show=show, save_to_file=not show)
self.scm_study = scm_study
self.adamont_studies = adamont_studies
if massif_names is None:
......@@ -33,9 +36,14 @@ class ComparisonHistoricalVisualizer(StudyVisualizer):
:return:
"""
values = [self.scm_study.__getattribute__(study_method)[massif_name]]
for study in self.adamont_studies.study_list:
values.append(study.__getattribute__(study_method)[massif_name])
return np.array(values)
gcm_rcm_couples = []
for gcm_rcm_couple, study in self.adamont_studies.gcm_rcm_couple_to_study.items():
try:
values.append(study.__getattribute__(study_method)[massif_name])
gcm_rcm_couples.append(gcm_rcm_couple)
except KeyError:
pass
return np.array(values), gcm_rcm_couples
def plot_comparison(self, plot_maxima=True):
if plot_maxima:
......@@ -43,22 +51,19 @@ class ComparisonHistoricalVisualizer(StudyVisualizer):
else:
study_method = 'massif_name_to_daily_time_series'
value_name = study_method.split('to_')[1]
print('Nb massifs', len(self.massif_names))
for massif_name in self.massif_names:
values = self.get_values(study_method, massif_name)
values, gcm_rcm_couples = self.get_values(study_method, massif_name)
plot_name = value_name + ' for {}'.format(massif_name.replace('_', '-'))
self.shoe_plot_comparison(values, plot_name)
self.shoe_plot_comparison(values, gcm_rcm_couples, plot_name)
def shoe_plot_comparison(self, values, plot_name):
def shoe_plot_comparison(self, values, gcm_rcm_couples, plot_name):
ax = plt.gca()
width = 10
positions = [i * width * 2 for i in range(len(values))]
labels = ['Reanalysis'] + [' / '.join(couple) for couple in self.adamont_studies.gcm_rcm_couples]
colors = ['black'] + [get_color_from_gcm_rcm_couple(couple) for couple in self.adamont_studies.gcm_rcm_couples]
labels = ['Reanalysis'] + [gcm_rcm_couple_to_str(couple) for couple in gcm_rcm_couples]
colors = ['black'] + [get_color_from_gcm_rcm_couple(couple) for couple in gcm_rcm_couples]
# Permute values, labels & colors, based on the mean values
print(values.shape)
mean_values = np.mean(values, axis=1)
print(mean_values.shape)
index_to_sort = np.argsort(mean_values)
colors = [colors[i] for i in index_to_sort]
labels = [labels[i] for i in index_to_sort]
......@@ -76,7 +81,7 @@ class ComparisonHistoricalVisualizer(StudyVisualizer):
SCM_STUDY_CLASS_TO_ABBREVIATION[type(self.study)],
self.study.variable_unit))
self.plot_name = '{}'.format(plot_name)
self.plot_name = 'comparison/{}'.format(plot_name)
self.show_or_save_to_file(add_classic_title=False, no_title=True, tight_layout=True)
ax.clear()
plt.close()
......
import matplotlib as mpl
mpl.use('Agg')
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
from extreme_data.meteo_france_data.adamont_data.adamont.adamont_snowfall import AdamontSnowfall
from extreme_data.meteo_france_data.adamont_data.adamont_scenario import gcm_rcm_couple_to_full_name, \
get_year_min_and_year_max_from_scenario, AdamontScenario
get_year_min_and_year_max_from_scenario, AdamontScenario, load_gcm_rcm_couples_for_year_min_and_year_max
from extreme_data.meteo_france_data.adamont_data.adamont_studies import AdamontStudies
from extreme_data.meteo_france_data.scm_models_data.safran.safran import SafranSnowfall1Day
from extreme_data.meteo_france_data.scm_models_data.utils import Season
from projects.projected_snowfall.comparison_with_scm.comparison_historical_visualizer import \
ComparisonHistoricalVisualizer
def load_historical_adamont_studies(study_class, year_min, year_max):
gcm_rcm_couples = []
for gcm_rcm_couple in gcm_rcm_couple_to_full_name.keys():
year_min_couple, year_max_couple = get_year_min_and_year_max_from_scenario(adamont_scenario=AdamontScenario.histo,
gcm_rcm_couple=gcm_rcm_couple)
if year_min_couple <= year_min and year_max <= year_max_couple:
gcm_rcm_couples.append(gcm_rcm_couple)
return AdamontStudies(study_class, gcm_rcm_couples, year_min=year_min, year_max=year_max)
def main():
fast = [True, False][0]
fast = True
# Set the year_min and year_max for the comparison
if fast:
year_min = [1982, 1950][1]
......@@ -27,7 +23,7 @@ def main():
else:
massif_names = None
year_min = [1982, 1950][1]
altitudes = [1800, 2100]
altitudes = [900, 1800, 2700]
for altitude in altitudes:
plot(altitude, massif_names, year_min)
......@@ -38,11 +34,14 @@ def plot(altitude, massif_names, year_min):
year_max = 2005
study_class_couple = [(SafranSnowfall1Day, AdamontSnowfall)][0]
scm_study_class, adamont_study_class = study_class_couple
scm_study = scm_study_class(altitude=altitude, year_min=year_min, year_max=year_max)
adamont_studies = load_historical_adamont_studies(adamont_study_class, year_min, year_max)
season = Season.annual
scm_study = scm_study_class(altitude=altitude, year_min=year_min, year_max=year_max, season=season)
gcm_rcm_couples = load_gcm_rcm_couples_for_year_min_and_year_max(year_min, year_max)
adamont_studies = AdamontStudies(adamont_study_class, gcm_rcm_couples,
altitude=altitude, year_min=year_min, year_max=year_max, season=season)
adamont_studies.plot_maxima_time_series(massif_names, scm_study)
visualizer = ComparisonHistoricalVisualizer(scm_study, adamont_studies, massif_names=massif_names)
for plot_maxima in [True, False][1:]:
for plot_maxima in [True, False][:1]:
visualizer.plot_comparison(plot_maxima)
......
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