From 8497a0375423694a26a1f0b76530ec050e2f2f1c Mon Sep 17 00:00:00 2001
From: Le Roux Erwan <erwan.le-roux@irstea.fr>
Date: Mon, 22 Mar 2021 11:39:03 +0100
Subject: [PATCH] [projection swe] add new version for the temperature
 covariate.

---
 .../cmip5/climate_explorer_cimp5.py           | 56 ++++++++++++-------
 .../adamont_data/cmip5/plot_temperatures.py   |  8 ++-
 .../{section_data => data}/__init__.py        |  0
 .../{section_data => data}/main_data.py       |  0
 .../discussion/__init__.py                    |  0
 .../{ => discussion}/main_sensitivity.py      |  0
 .../results/__init__.py                       |  0
 .../main_projections_ensemble.py              |  0
 8 files changed, 42 insertions(+), 22 deletions(-)
 rename projects/projected_extreme_snowfall/{section_data => data}/__init__.py (100%)
 rename projects/projected_extreme_snowfall/{section_data => data}/main_data.py (100%)
 create mode 100644 projects/projected_extreme_snowfall/discussion/__init__.py
 rename projects/projected_extreme_snowfall/{ => discussion}/main_sensitivity.py (100%)
 create mode 100644 projects/projected_extreme_snowfall/results/__init__.py
 rename projects/projected_extreme_snowfall/{ => results}/main_projections_ensemble.py (100%)

diff --git a/extreme_data/meteo_france_data/adamont_data/cmip5/climate_explorer_cimp5.py b/extreme_data/meteo_france_data/adamont_data/cmip5/climate_explorer_cimp5.py
index 4b5d8a8d..542d2b50 100644
--- a/extreme_data/meteo_france_data/adamont_data/cmip5/climate_explorer_cimp5.py
+++ b/extreme_data/meteo_france_data/adamont_data/cmip5/climate_explorer_cimp5.py
@@ -58,7 +58,10 @@ def years_and_global_mean_temps(gcm, scenario, year_min=None, year_max=None, ano
         download_dat(dat_filepath, txt_filepath)
     # Transform nc file into csv file
     if not op.exists(csv_filepath):
-        dat_to_csv(csv_filepath, txt_filepath, spline)
+        print('compute csv')
+        dat_to_csv(csv_filepath, txt_filepath, gcm)
+    else:
+        print('read from existing csv')
 
     # Load csv file
     df = pd.read_csv(csv_filepath, index_col=0)
@@ -75,7 +78,7 @@ def years_and_global_mean_temps(gcm, scenario, year_min=None, year_max=None, ano
     return years, global_mean_temp
 
 
-def dat_to_csv(csv_filepath, txt_filepath):
+def dat_to_csv(csv_filepath, txt_filepath, gcm):
     d = OrderedDict()
     with open(txt_filepath, 'r') as f:
         for i, l in enumerate(f):
@@ -96,29 +99,33 @@ def dat_to_csv(csv_filepath, txt_filepath):
     assert len(l) == len(df.index)
 
     # First we compute the standard column
-    mean_annual_column_name, anomaly_annual_column_name = [get_column_name(anomaly=anomaly, spline=False)
+    df = set_anomaly(df, mean_data=l, spline=False)
+
+    # Then we regress some cubic spline on the temperature columns
+    noisy_data = df[get_column_name(anomaly=False, spline=False)]
+    ind = noisy_data > -50
+    spline_data = noisy_data.copy()
+    spline_data.loc[ind] = apply_cubic_spline(noisy_data.loc[ind].index.values, noisy_data.loc[ind].values, gcm)
+    df = set_anomaly(df, mean_data=spline_data, spline=True)
+
+    df.to_csv(csv_filepath)
+
+
+def set_anomaly(df, mean_data, spline):
+    mean_annual_column_name, anomaly_annual_column_name = [get_column_name(anomaly=anomaly, spline=spline)
                                                            for anomaly in [False, True]]
-    df[mean_annual_column_name] = l
-    s_mean_for_reference_period_1850_to_1900 = df.loc[1850:1900, mean_annual_column_name]
+    df[get_column_name(anomaly=False, spline=spline)] = mean_data
+
     # Sometimes some initial global mean temperatures are negative for the first years,
     # we remove them for the computation of the mean
+    s_mean_for_reference_period_1850_to_1900 = df.loc[1850:1900, mean_annual_column_name]
     ind = s_mean_for_reference_period_1850_to_1900 > 0
     mean_for_reference_period_1850_to_1900 = s_mean_for_reference_period_1850_to_1900.loc[ind].mean()
     df[anomaly_annual_column_name] = df[mean_annual_column_name] - mean_for_reference_period_1850_to_1900
-
-    # Then we regress some cubic spline on these columns
-    for anomaly in [True, False]:
-        noisy_data = df[get_column_name(anomaly, spline=False)]
-        ind = noisy_data > -50
-        spline_data = noisy_data.copy()
-        spline_data.loc[ind] = apply_cubic_spline(noisy_data.loc[ind].index.values,
-                                                                       noisy_data.loc[ind].values)
-        df[get_column_name(anomaly, spline=True)] = spline_data
-
-    df.to_csv(csv_filepath)
+    return df
 
 
-def apply_cubic_spline(x, y):
+def apply_cubic_spline(x, y, gcm):
     """
     s is THE important parameter, that controls as how far the points of the spline are from the original points.
     w[i] corresponds to constant weight in our case.
@@ -126,13 +133,24 @@ def apply_cubic_spline(x, y):
     sum((w[i] * (y[i]-spl(x[i])))**2, axis=0) <= s
 
     """
-    # s = 3 # it was working well except for the blue one.
-    s = 5
+    gcm_to_s_parameter_for_univariate_spline = \
+        {
+            'MPI-ESM-LR': 8,
+            'CNRM-CM5': 3,
+            'IPSL-CM5A-MR': 3,
+            'EC-EARTH': 2,
+            'HadGEM2-ES': 5,
+            'NorESM1-M': 2.5
+        }
+    s = gcm_to_s_parameter_for_univariate_spline[gcm]
     f = UnivariateSpline(x, y, s=s, w=None)
     new_y = f(x)
     return new_y
 
 
+
+
+
 def download_dat(dat_filepath, txt_filepath):
     web_filepath = op.join(GLOBALTEMP_WEB_PATH, op.basename(dat_filepath))
     dirname = op.dirname(dat_filepath)
diff --git a/extreme_data/meteo_france_data/adamont_data/cmip5/plot_temperatures.py b/extreme_data/meteo_france_data/adamont_data/cmip5/plot_temperatures.py
index f2ffae0b..e8007d1b 100644
--- a/extreme_data/meteo_france_data/adamont_data/cmip5/plot_temperatures.py
+++ b/extreme_data/meteo_france_data/adamont_data/cmip5/plot_temperatures.py
@@ -36,7 +36,8 @@ def main_plot_temperature_with_spline_on_top(anomaly=True):
                                          label=label, spline=spline, anomaly=anomaly)
             plot_temperature_for_rcp_gcm(ax, gcm, scenario, year_min=2005, year_max=2100, spline=spline, anomaly=anomaly)
 
-        end_plot(anomaly, ax, spline, gcm)
+        title = '{} {} of temperatures'.format(gcm, 'anomaly' if anomaly else 'mean')
+        end_plot(anomaly, ax, spline, title)
 
 
 def end_plot(anomaly, ax, spline, title=None):
@@ -87,5 +88,6 @@ def plot_temperature_for_rcp_gcm(ax, gcm, scenario, year_min, year_max, linestyl
 
 
 if __name__ == '__main__':
-    # main_plot_temperature_with_spline_on_top(anomaly=True)
-    main_plot_temperature_all(anomaly=True, spline=True)
+    for anomaly in [True, False][:]:
+        main_plot_temperature_with_spline_on_top(anomaly=anomaly)
+        # main_plot_temperature_all(anomaly=True, spline=True)
diff --git a/projects/projected_extreme_snowfall/section_data/__init__.py b/projects/projected_extreme_snowfall/data/__init__.py
similarity index 100%
rename from projects/projected_extreme_snowfall/section_data/__init__.py
rename to projects/projected_extreme_snowfall/data/__init__.py
diff --git a/projects/projected_extreme_snowfall/section_data/main_data.py b/projects/projected_extreme_snowfall/data/main_data.py
similarity index 100%
rename from projects/projected_extreme_snowfall/section_data/main_data.py
rename to projects/projected_extreme_snowfall/data/main_data.py
diff --git a/projects/projected_extreme_snowfall/discussion/__init__.py b/projects/projected_extreme_snowfall/discussion/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/projects/projected_extreme_snowfall/main_sensitivity.py b/projects/projected_extreme_snowfall/discussion/main_sensitivity.py
similarity index 100%
rename from projects/projected_extreme_snowfall/main_sensitivity.py
rename to projects/projected_extreme_snowfall/discussion/main_sensitivity.py
diff --git a/projects/projected_extreme_snowfall/results/__init__.py b/projects/projected_extreme_snowfall/results/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/projects/projected_extreme_snowfall/main_projections_ensemble.py b/projects/projected_extreme_snowfall/results/main_projections_ensemble.py
similarity index 100%
rename from projects/projected_extreme_snowfall/main_projections_ensemble.py
rename to projects/projected_extreme_snowfall/results/main_projections_ensemble.py
-- 
GitLab