From 3148d5876814c2ddc77fdb86518b957408b3bd79 Mon Sep 17 00:00:00 2001
From: Le Roux Erwan <erwan.le-roux@irstea.fr>
Date: Wed, 18 Nov 2020 15:35:21 +0100
Subject: [PATCH] improve the script for Ribatet. add one method to
 abstract_study.py

---
 .../scm_models_data/abstract_study.py            |  8 ++++++++
 .../scm_models_data/case_studies/nico/nico2.py   | 16 +++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/extreme_data/meteo_france_data/scm_models_data/abstract_study.py b/extreme_data/meteo_france_data/scm_models_data/abstract_study.py
index 27a0ff49..548c81d7 100644
--- a/extreme_data/meteo_france_data/scm_models_data/abstract_study.py
+++ b/extreme_data/meteo_france_data/scm_models_data/abstract_study.py
@@ -283,6 +283,14 @@ class AbstractStudy(object):
             massif_name_to_annual_maxima_ordered_index[massif_name] = ordered_index
         return massif_name_to_annual_maxima_ordered_index
 
+    @cached_property
+    def massif_name_to_annual_maxima_index(self):
+        massif_name_to_annual_maxima_index = OrderedDict()
+        for i, massif_name in enumerate(self.study_massif_names):
+            index = [self.year_to_annual_maxima_index[year][i] for year in self.ordered_years]
+            massif_name_to_annual_maxima_index[massif_name] = index
+        return massif_name_to_annual_maxima_index
+
     @cached_property
     def massif_name_to_annual_maxima(self):
         massif_name_to_annual_maxima = OrderedDict()
diff --git a/extreme_data/meteo_france_data/scm_models_data/case_studies/nico/nico2.py b/extreme_data/meteo_france_data/scm_models_data/case_studies/nico/nico2.py
index 4354aaa1..f11d41fe 100644
--- a/extreme_data/meteo_france_data/scm_models_data/case_studies/nico/nico2.py
+++ b/extreme_data/meteo_france_data/scm_models_data/case_studies/nico/nico2.py
@@ -13,21 +13,23 @@ from extreme_fit.estimator.margin_estimator.utils import fitted_stationary_gev
 from projects.altitude_spatial_model.altitudes_fit.altitudes_studies import AltitudesStudies
 
 
-def generate_excel_with_annual_maxima(fast=True):
+def generate_excel_with_annual_maxima(fast=True, maxima_dates=False):
     if fast:
         altitudes = [600, 900]
     else:
         altitudes = [600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000, 3300, 3600]
     study_class = SafranSnowfall1Day
-    study_name = SCM_STUDY_CLASS_TO_ABBREVIATION[study_class]
+    study_name = 'annual maxima of ' + SCM_STUDY_CLASS_TO_ABBREVIATION[study_class]
+    if maxima_dates:
+        study_name += ' - number of days since 1st August, e.g. 1 represents the 2nd of August'
     writer = pd.ExcelWriter('{}.xlsx'.format(study_name), engine='xlsxwriter')
     altitude_studies = AltitudesStudies(study_class, altitudes)
     for massif_name in altitude_studies.study.all_massif_names():
-        write_df_with_annual_maxima(massif_name, writer, altitude_studies)
+        write_df_with_annual_maxima(massif_name, writer, altitude_studies, maxima_dates)
     writer.save()
 
 
-def write_df_with_annual_maxima(massif_name, writer, altitude_studies) -> pd.DataFrame:
+def write_df_with_annual_maxima(massif_name, writer, altitude_studies, maxima_dates=False) -> pd.DataFrame:
     columns = []
     altitudes = []
     for altitude, study in altitude_studies.altitude_to_study.items():
@@ -35,6 +37,10 @@ def write_df_with_annual_maxima(massif_name, writer, altitude_studies) -> pd.Dat
         if massif_name in study.study_massif_names:
             altitudes.append(altitude)
             s = df_maxima.loc[massif_name]
+            if maxima_dates:
+                values = study.massif_name_to_annual_maxima_index[massif_name]
+                s = pd.Series(index=s.index, data=values)
+                # s.values = np.array(values)
             # Fit the data and add the parameters as the first columns
             columns.append(s)
     df = pd.concat(columns, axis=1)
@@ -45,4 +51,4 @@ def write_df_with_annual_maxima(massif_name, writer, altitude_studies) -> pd.Dat
 
 
 if __name__ == '__main__':
-    generate_excel_with_annual_maxima(fast=False)
+    generate_excel_with_annual_maxima(fast=False, maxima_dates=True)
-- 
GitLab