-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuture_meteorology.py
More file actions
2141 lines (1634 loc) · 117 KB
/
future_meteorology.py
File metadata and controls
2141 lines (1634 loc) · 117 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 3 15:00:06 2024
@author: amounier
"""
import time
import os
from datetime import date
import pandas as pd
import tqdm
import numpy as np
import subprocess
import xarray as xr
import matplotlib.pyplot as plt
import pyproj
import cartopy.crs as ccrs
import matplotlib
import geopandas as gpd
from shapely.geometry import mapping
import cmocean
from pysolar.solar import get_altitude, get_altitude_fast, get_azimuth, get_azimuth_fast
import multiprocessing
import seaborn as sns
import pickle
from meteorology import (get_historical_weather_data,
aggregate_resolution,
get_direct_solar_irradiance_projection_ratio,
get_diffuse_solar_irradiance_projection_ratio,
get_meteo_data,
get_safran_weather_data,
get_safran_hourly_weather_data)
from administrative import Climat, get_coordinates, France, City, draw_climat_map, Climat_winter
from climate_zone_characterisation import map_xarray
from utils import blank_national_map, get_extent,plot_timeserie
def open_zcl_daily_temperature():
data = pd.read_csv(os.path.join('data','CORDEX','climat_region_daily_temperature.csv'))
data['date'] = pd.to_datetime(data.date, format='%Y-%m-%d %H:%M:%S')
data = data.set_index('date')
return data
def delta_daily_temperature(zcl_codint,nmod,ref_period=[2010,2020]):
data = open_zcl_daily_temperature()
columns = [c for c in data.columns if str(zcl_codint) in c]
columns = [c for c in columns if 'mod{}'.format(nmod) in c]
data = data[columns]
ref_years = list(range(ref_period[0],ref_period[1]+1))
days = list(range(1,32))
months = list(range(1,13))
dict_ref_temperature = dict()
for m in months:
for d in days:
ref_doy_temp = data[(data.index.day==d)&(data.index.month==m)&(data.index.year.isin(ref_years))]
dict_ref_temperature[(m,d)] = ref_doy_temp.mean()
data_delta_t = data.copy()
for m in months:
for d in days:
filter_day = (data_delta_t.index.day==d)&(data_delta_t.index.month==m)
data_delta_t[filter_day] = data_delta_t[filter_day] - dict_ref_temperature.get((m,d))
return data_delta_t
def get_projected_weather_data_old(city,zcl_codint,nmod,rcp,future_period,ref_year=2020):
future_period_list = list(range(future_period[0],future_period[1]+1))
dt = delta_daily_temperature(zcl_codint, nmod)
col = 'proj_temperature_{}_mod{}_rcp{}'.format(zcl_codint,nmod,rcp)
dt = dt[[col]]
dt = dt[dt.index.year.isin(future_period_list)]
# il faut que les deux periodes aient la même durée
# print(future_period_list)
ref_period = [ref_year+1-len(future_period_list),ref_year]
ref_period_list = list(range(ref_period[0],ref_period[1]+1))
# print(ref_period_list)
change_year_dict = {proj_year:ref_year for proj_year,ref_year in zip(future_period_list,ref_period_list)}
# print(change_year_dict)
new_index = [pd.to_datetime('{}-{}-{}'.format(change_year_dict.get(y),m,d),errors='coerce') for y,m,d in zip(dt.index.year,dt.index.month,dt.index.day)]
dt.index = new_index
weather_data = get_historical_weather_data(city,ref_period)
weather_data = weather_data.rename(columns={'temperature_2m':'ref_temperature_2m'})
weather_data['temperature_2m'] = [np.nan]*len(weather_data)
for y,m,d, delta_t in zip(dt.index.year.values, dt.index.month.values, dt.index.day.values, dt[col]):
filter_day = (weather_data.index.year==y)&(weather_data.index.month==m)&(weather_data.index.day==d)
projected_weather_data = pd.DataFrame(weather_data[filter_day]['ref_temperature_2m'] + delta_t).rename(columns={'ref_temperature_2m':'temperature_2m'})
weather_data = weather_data.join(projected_weather_data, lsuffix='_l', rsuffix='_r')
# coalesce cost column to get first non NA value
weather_data['temperature_2m'] = weather_data['temperature_2m_l'].combine_first(weather_data['temperature_2m_r']).astype(float)
# remove the cols
weather_data = weather_data.drop(columns=['temperature_2m_r', 'temperature_2m_l'])
return weather_data
def compute_projected_weather_data(zcl_code,nmod):
# Accéléré 72 fois grace aux bfill et ffill
zcl = Climat(zcl_code)
city = City(zcl.center_prefecture)
coordinates = city.coordinates
path = os.path.join('data','Explore2','daily')
daily_data = pd.read_csv(os.path.join(path,'tas_mod{}_{}.csv'.format(nmod,zcl_code))).rename(columns={'Unnamed: 0':'date'}).set_index('date')
for cv in ['tasmin','tasmax','rsds']:
daily_data = daily_data.join(pd.read_csv(os.path.join(path,'{}_mod{}_{}.csv'.format(cv,nmod,zcl_code))).rename(columns={'Unnamed: 0':'date'}).set_index('date'))
daily_data.index = pd.to_datetime(daily_data.index)
if '{}_mod{}.parquet'.format(zcl_code,nmod) not in os.listdir(os.path.join('data','Explore2','hourly')):
hourly_data = pd.DataFrame(index=pd.date_range(daily_data.index[0],'{}-01-01'.format(daily_data.index[-1].year+1),freq='h'))
hourly_data = hourly_data.drop(hourly_data.iloc[-1].name)
dates = hourly_data.copy().index
dates = dates.tz_localize(tz='CET',ambiguous='NaT',nonexistent='NaT')
dates = dates.to_pydatetime()
altitude = [get_altitude_fast(coordinates[1],coordinates[0],t) if t is not pd.NaT else np.nan for t in tqdm.tqdm(dates, desc='altitude')]
azimuth = [float(get_azimuth_fast(coordinates[1],coordinates[0],t)) if t is not pd.NaT else np.nan for t in tqdm.tqdm(dates, desc='azimuth')]
hourly_data['sun_azimuth'] = azimuth
hourly_data['sun_altitude'] = altitude
hourly_data['sunrise'] = hourly_data.sun_altitude.lt(0) & hourly_data.sun_altitude.shift(-1).ge(0)
daily_data['hour_sunrise'] = hourly_data.index[hourly_data['sunrise']].hour.values[:len(daily_data)]
# construction des données de temperature
hourly_data['temp'] = [np.nan]*len(hourly_data)
hour_min_rel_sunrise = 0
hour_max = 15
mask_max = hourly_data.index.hour == hour_max
hourly_data.loc[mask_max,'temp'] = list(daily_data.tasmax.values) + [np.nan]*(len(hourly_data.loc[mask_max,'temp'])-len(daily_data))
mask_min = hourly_data.sunrise.shift(hour_min_rel_sunrise).infer_objects(copy=False).fillna(False)
hourly_data.loc[mask_min,'temp'] = list(daily_data.tasmin.values) + [np.nan]*(len(hourly_data.loc[mask_min,'temp'])-len(daily_data))
# weather_data_modelled = weather_data_modelled[['temperature']]
temperature_sin14R1 = [np.nan]*len(hourly_data)
prev_T, prev_t, next_T, next_t = [np.nan]*4
flag = False
def get_previous_temperature(t, data):
try:
# d = data[data.index<t].dropna().iloc[-1]
d = data.loc[t]
t = d.date
T = d.temp
return t, T
except IndexError:
return np.nan, np.nan
def get_next_temperature(t, data):
try:
# d = data[data.index>t].dropna().iloc[0]
d = data.loc[t]
t = d.date
T = d.temp
return t, T
except IndexError:
return np.nan, np.nan
def compute_temperature_sin14R1(t,prev_T,prev_t,next_T,next_t):
T = (next_T + prev_T)/2 - ((next_T-prev_T)/2 * np.cos(np.pi*(t-prev_t)/(next_t-prev_t)))
return T
# define bfill with temp and date
hourly_bfill = hourly_data.copy()
hourly_bfill['date'] = [pd.NaT]*len(hourly_bfill)
hourly_bfill.loc[~hourly_bfill.temp.isnull(),'date'] = hourly_bfill.loc[~hourly_bfill.temp.isnull()].index
hourly_bfill = hourly_bfill.bfill()
# same for ffill
hourly_ffill = hourly_data.copy()
hourly_ffill['date'] = [pd.NaT]*len(hourly_ffill)
hourly_ffill.loc[~hourly_ffill.temp.isnull(),'date'] = hourly_ffill.loc[~hourly_ffill.temp.isnull()].index
hourly_ffill = hourly_ffill.ffill()
for idx,t in tqdm.tqdm(enumerate(hourly_data.index),total=len(hourly_data), desc='temperature'):
T = hourly_data.loc[t].temp
if flag:
prev_t, prev_T = get_previous_temperature(t, hourly_ffill)
next_t, next_T = get_next_temperature(t, hourly_bfill)
flag = False
if not pd.isnull(T):
flag = True
temperature_sin14R1[idx] = T
else:
if pd.isnull(prev_t) or pd.isnull(next_t):
continue
temperature_sin14R1[idx] = compute_temperature_sin14R1(t,prev_T,prev_t,next_T,next_t)
hourly_data['temperature_2m'] = temperature_sin14R1
hourly_data.to_parquet(os.path.join('data','Explore2','hourly','{}_mod{}.parquet'.format(zcl_code,nmod)))
hourly_data['rsds'] = np.cos(np.deg2rad(90-hourly_data.sun_altitude))
hourly_data['rsds'] = hourly_data['rsds'].clip(lower=0.)
daily_data['rsds_model'] = aggregate_resolution(hourly_data[['rsds']],resolution='D', agg_method='mean')
daily_data['rsds_factor'] = daily_data.rsds/daily_data.rsds_model # *1.09 # caution
rsds_factor = np.asarray([[e]*24 for e in daily_data['rsds_factor']]).flatten()
rsds_factor = list(rsds_factor) + [np.nan]*(len(hourly_data)-len(rsds_factor))
hourly_data['rsds'] = hourly_data['rsds']*np.asarray(rsds_factor)
direct_ratio = 0.749
hourly_data['rsds_direct'] = hourly_data['rsds']*direct_ratio
hourly_data['diffuse_radiation_instant'] = hourly_data['rsds']*(1-direct_ratio)
normal_ratio = np.sin(np.deg2rad(np.maximum(hourly_data['sun_altitude'],0)))
hourly_data['direct_normal_irradiance_instant'] = hourly_data['rsds_direct']/normal_ratio
orientations = ['N','NE','E','SE','S','SW','W','NW','H']
for ori in orientations:
col_coef_dri = 'coefficient_direct_{}_irradiance'.format(ori)
col_coef_dif = 'coefficient_diffuse_{}_irradiance'.format(ori)
col_dri = 'direct_sun_radiation_{}'.format(ori)
col_dif = 'diffuse_sun_radiation_{}'.format(ori)
hourly_data[col_coef_dri] = get_direct_solar_irradiance_projection_ratio(ori, hourly_data.sun_azimuth, hourly_data.sun_altitude)
hourly_data[col_coef_dif] = get_diffuse_solar_irradiance_projection_ratio(ori)
hourly_data[col_dri] = hourly_data.direct_normal_irradiance_instant * hourly_data[col_coef_dri]
hourly_data[col_dif] = hourly_data.diffuse_radiation_instant * hourly_data[col_coef_dif]
hourly_data = hourly_data[['temperature_2m', 'diffuse_radiation_instant',
'direct_normal_irradiance_instant', 'sun_altitude', 'sun_azimuth',
'direct_sun_radiation_N', 'diffuse_sun_radiation_N',
'direct_sun_radiation_NE', 'diffuse_sun_radiation_NE',
'direct_sun_radiation_E', 'diffuse_sun_radiation_E',
'direct_sun_radiation_SE', 'diffuse_sun_radiation_SE',
'direct_sun_radiation_S', 'diffuse_sun_radiation_S',
'direct_sun_radiation_SW', 'diffuse_sun_radiation_SW',
'direct_sun_radiation_W', 'diffuse_sun_radiation_W',
'direct_sun_radiation_NW', 'diffuse_sun_radiation_NW',
'direct_sun_radiation_H', 'diffuse_sun_radiation_H']]
hourly_data.to_parquet(os.path.join('data','Explore2','hourly','{}_mod{}.parquet'.format(zcl_code,nmod)))
hourly_data = pd.read_parquet(os.path.join('data','Explore2','hourly','{}_mod{}.parquet'.format(zcl_code,nmod)))
hourly_data = hourly_data.fillna(0.)
return hourly_data
def get_projected_weather_data(zcl_code,period,nmod=3):
"""
Récupération des données climatiques projetées
Parameters
----------
zcl_code : str
code de la zone climatique zcl8.
period : list
list de deux éléments : année de départ, année de fin (inclues). Comprises entre 1951 et 2100
nmod : int, optional
Numéro du modèle climatique. The default is 3.
Returns
-------
hourly : pandas DataFrame
données climatiques.
"""
hourly = compute_projected_weather_data(zcl_code, nmod=nmod)
hourly = hourly[hourly.index.year.isin(list(range(period[0],period[1]+1)))]
return hourly
#%% ===========================================================================
# script principal
# =============================================================================
def main():
tic = time.time()
# Défintion de la date du jour
today = pd.Timestamp(date.today()).strftime('%Y%m%d')
# Défintion des dossiers de sortie
output = 'output'
folder = '{}_future_meteorology'.format(today)
figs_folder = os.path.join(output, folder, 'figs')
# Création des dossiers de sortie
if folder not in os.listdir(output):
os.mkdir(os.path.join(output,folder))
if 'figs' not in os.listdir(os.path.join(output, folder)):
os.mkdir(figs_folder)
external_disk_connection = 'MPBE' in os.listdir('/media/amounier/')
#%% Téléchargement des données CORDEX sur CDS (ne marche pas)
if False:
import cdsapi
dataset = "projections-cordex-domains-single-levels"
request = {
"domain": "europe",
"experiment": "historical",
"horizontal_resolution": "0_11_degree_x_0_11_degree",
"temporal_resolution": "daily_mean",
"variable": [
"2m_air_temperature",
"maximum_2m_temperature_in_the_last_24_hours",
"minimum_2m_temperature_in_the_last_24_hours",
"surface_solar_radiation_downwards"
],
"gcm_model": "cnrm_cerfacs_cm5",
"rcm_model": "cnrm_aladin63",
"ensemble_member": "r1i1p1",
"start_year": ["1996"],
"end_year": ["2000"]
}
client = cdsapi.Client()
client.retrieve(dataset, request).download()
# projections cliamtiques d'open-meteo CMIP6 HighRes
if False:
import openmeteo_requests
import requests_cache
# import pandas as pd
from retry_requests import retry
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)
# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = "https://climate-api.open-meteo.com/v1/climate"
params = {
"latitude": 52.52,
"longitude": 13.41,
"start_date": "1950-01-01",
"end_date": "2050-12-31",
"models": ["CMCC_CM2_VHR4", "FGOALS_f3_H", "HiRAM_SIT_HR", "MRI_AGCM3_2_S", "EC_Earth3P_HR", "MPI_ESM1_2_XR", "NICAM16_8S"],
"daily": ["temperature_2m_mean", "temperature_2m_max", "temperature_2m_min", "shortwave_radiation_sum"]
}
responses = openmeteo.weather_api(url, params=params)
# Process first location. Add a for-loop for multiple locations or weather models
response = responses[0]
print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E")
print(f"Elevation {response.Elevation()} m asl")
print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")
# Process daily data. The order of variables needs to be the same as requested.
daily = response.Daily()
daily_temperature_2m_mean = daily.Variables(0).ValuesAsNumpy()
daily_temperature_2m_max = daily.Variables(1).ValuesAsNumpy()
daily_temperature_2m_min = daily.Variables(2).ValuesAsNumpy()
daily_shortwave_radiation_sum = daily.Variables(3).ValuesAsNumpy()
daily_data = {"date": pd.date_range(
start = pd.to_datetime(daily.Time(), unit = "s", utc = True),
end = pd.to_datetime(daily.TimeEnd(), unit = "s", utc = True),
freq = pd.Timedelta(seconds = daily.Interval()),
inclusive = "left"
)}
daily_data["temperature_2m_mean"] = daily_temperature_2m_mean
daily_data["temperature_2m_max"] = daily_temperature_2m_max
daily_data["temperature_2m_min"] = daily_temperature_2m_min
daily_data["shortwave_radiation_sum"] = daily_shortwave_radiation_sum
daily_dataframe = pd.DataFrame(data = daily_data)
print(daily_dataframe)
#%% Utilisation des projections de températures par zone climatique
if False:
zcl = Climat('H1a')
nmod = 1
data = delta_daily_temperature(zcl.codint,nmod)
data.plot()
data = get_projected_weather_data_old(city='Paris',
zcl_codint=Climat('H1a').codint,
nmod=0,
rcp=85,
future_period=[2085,2090],
principal_orientation='S')
print(data)
data[['ref_temperature_2m','temperature_2m']].plot()
for t in ['ref_temperature_2m','temperature_2m']:
print(data[t].mean())
# print(data)
#%% Gestion des données Explore2
if False and external_disk_connection:
data_folder = '/media/amounier/MPBE/heavy_data/Explore2'
# telechargement sur le site de la DRIAS
if False:
# with open(os.path.join('data','Explore2','download_links.txt')) as f:
# dl_links = f.read().splitlines()
with open(os.path.join('data','Explore2','download_links_adamont.txt')) as f:
dl_links = f.read().splitlines()
for url in dl_links:
subprocess.run('wget -P {} {}'.format(data_folder,url),shell=True)
# association des projections et modelisations historiques (temperature)
if False:
zcl = Climat('H3')
city = zcl.center_prefecture
coords = get_coordinates(city)
tas_Explore2_hist = '/media/amounier/MPBE/heavy_data/Explore2/tasAdjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19510101-20051231.nc'
tas_Explore2_proj = '/media/amounier/MPBE/heavy_data/Explore2/tasAdjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'
tas_array_hist = xr.open_dataset(tas_Explore2_hist).tasAdjust - 273.15
tas_array_proj = xr.open_dataset(tas_Explore2_proj).tasAdjust - 273.15
tas_array_hist.rio.write_crs('epsg:27572', inplace=True)
tas_array_proj.rio.write_crs('epsg:27572', inplace=True)
geom = pd.Series(France().geometry).apply(mapping)
tas_array_hist = tas_array_hist.rio.clip(geom, 'epsg:4326', drop=False)
tas_array_proj = tas_array_proj.rio.clip(geom, 'epsg:4326', drop=False)
transformer = pyproj.Transformer.from_crs("EPSG:4326", "EPSG:27572", always_xy=True)
coords_transformed = transformer.transform(*coords)
# carte des temperature
if True:
cmap = matplotlib.colormaps.get_cmap('viridis')
fig,ax = blank_national_map()
tas = tas_array_hist.isel(time=42)
img = tas.plot(ax=ax,transform=ccrs.epsg('27572'),add_colorbar=False,
cmap=cmap,vmin=tas.min(),vmax=tas.max())
ax.plot(*coords_transformed, transform=ccrs.epsg('27572'), marker='o',color='red',mec='k')
ax.set_title('')
ax_cb = fig.add_axes([0,0,0.1,0.1])
posn = ax.get_position()
ax_cb.set_position([posn.x0+posn.width+0.02, posn.y0, 0.04, posn.height])
fig.add_axes(ax_cb)
cbar = plt.colorbar(img,cax=ax_cb,extendfrac=0.02)
cbar.set_label('Daily mean temperature (°C)')
ax.set_extent(get_extent())
plt.show()
# détermination des périodes à +2 et +4 degrés
if True:
mean_yt_deg2 = 11.37
mean_yt_deg4 = 13.25
hist = tas_array_hist.mean(('x','y'))
# hist = tas_array_hist.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
hist = hist.groupby('time.year').mean('time')
proj = tas_array_proj.mean(('x','y'))
# proj = tas_array_proj.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
proj = proj.groupby('time.year').mean('time')
data_hist = pd.DataFrame(index=hist.year, data=hist)
data_proj = pd.DataFrame(index=proj.year, data=proj)
data = pd.concat([data_hist, data_proj]).rename(columns={0:'temperature'})
fig,ax = plt.subplots(figsize=(5,5),dpi=300)
data.plot(ax=ax,color='k')
ax.plot([data.index.min(),data.index.max()],[mean_yt_deg2]*2,color='tab:blue',label='+2°C')
ax.plot([data.index.min(),data.index.max()],[mean_yt_deg4]*2,color='tab:red',label='+4°C')
ax.legend()
ax.set_ylabel('Yearly mean temperature over France (°C)')
plt.show()
period_list = []
mean_20_years_list = []
for y0 in data.index[10:-10]:
data_20 = data.loc[y0-10:y0+10]
mean_20 = data_20.mean()
period = y0
period_list.append(period)
mean_20_years_list.append(mean_20)
mean_20_years_list = np.asarray(mean_20_years_list).T[0]
year_deg2 = period_list[next(x[0] for x in enumerate(mean_20_years_list) if x[1] > mean_yt_deg2)]
year_deg4 = period_list[next(x[0] for x in enumerate(mean_20_years_list) if x[1] > mean_yt_deg4)]
fig,ax = plt.subplots(figsize=(5,5),dpi=300)
ax.plot(period_list,mean_20_years_list,color='k')
ax.plot([period_list[0],year_deg2],[mean_yt_deg2]*2,color='tab:blue',label='+2°C ({}-{})'.format(year_deg2-10,year_deg2+10),)
ax.plot([period_list[0],year_deg4],[mean_yt_deg4]*2,color='tab:red',label='+4°C ({}-{})'.format(year_deg4-10,year_deg4+10))
ax.plot([year_deg2],[mean_yt_deg2],color='tab:blue',marker='o')
ax.plot([year_deg4],[mean_yt_deg4],color='tab:red',marker='o')
ax.legend()
ax.set_ylabel('Yearly mean temperature over 20 years (°C)')
ax.set_xlabel('Center year of mean period')
plt.show()
# comparaison avec les données ERA5
if False:
zcl = Climat('H3')
city = zcl.center_prefecture
coords = get_coordinates(city)
period = [1990,2000]
tas_Explore2_hist = '/media/amounier/MPBE/heavy_data/Explore2/tasAdjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19510101-20051231.nc'
tas_array_hist = xr.open_dataset(tas_Explore2_hist).tasAdjust - 273.15
tas_array_hist.rio.write_crs('epsg:27572', inplace=True)
# geom = pd.Series(France().geometry).apply(mapping)
# tas_array_hist = tas_array_hist.rio.clip(geom, 'epsg:4326', drop=False)
transformer = pyproj.Transformer.from_crs("EPSG:4326", "EPSG:27572", always_xy=True)
coords_transformed = transformer.transform(*coords)
explore2 = tas_array_hist.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
explore2 = explore2.sel(time=slice("{}-01-01".format(period[0]), "{}-12-31".format(period[1])))
era5 = get_historical_weather_data(city,period)
era5 = aggregate_resolution(era5,resolution='D')
if True:
fig,ax = plt.subplots(figsize=(5,5),dpi=300)
explore2.plot(ax=ax)
era5.temperature_2m.plot(ax=ax)
plt.show()
fig,ax = plt.subplots(figsize=(5,5),dpi=300)
ax.plot(era5.temperature_2m,explore2,marker='.',alpha=0.05,ls='')
ax.plot([explore2.min(),explore2.max()],[explore2.min(),explore2.max()],color='k')
plt.axis('equal')
plt.show()
# CDF temperature moyenne et solaire
if False:
if external_disk_connection:
data_folder = '/media/amounier/MPBE/heavy_data/Explore2'
else:
data_folder = os.path.join('data','Explore2')
# association des projections et modelisations historiques (temperature)
if True:
zcl = Climat('H3')
zcl = Climat('H1b')
city = zcl.center_prefecture
coords = get_coordinates(city)
nmod = 0
# calib_method = 'CDFt'
calib_method = 'ADAMONT'
variable = 'rsds' # tas
variable = 'tas'
season = True
if calib_method == 'CDFt':
models_dict = {0:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19510101-20051231.nc',
# 'rcp45':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp45_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc',
'rcp85':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'},
1:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_MOHC-HadREM3-GA7-05_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19520101-20051231.nc',
'rcp85':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001230.nc'},
2:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_KNMI-RACMO22E_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19500101-20051231.nc',
# 'rcp45':'Adjust_France_ICHEC-EC-EARTH_rcp45_r12i1p1_KNMI-RACMO22E_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc',
'rcp85':'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_KNMI-RACMO22E_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'},
3:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19520101-20051231.nc',
'rcp85':'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'},
4:{'historical':'Adjust_France_MOHC-HadGEM2-ES_historical_r1i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19520101-20051231.nc',
'rcp85':'Adjust_France_MOHC-HadGEM2-ES_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-20991219.nc'},
}
elif calib_method == 'ADAMONT':
models_dict = {0:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19510101-20051231.nc',
'rcp85': 'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001231.nc',
'historical_rcp85':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19510101-21001231.nc'},
1:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_MOHC-HadREM3-GA7-05_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20051231.nc',
'rcp85': 'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v2_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001230.nc',
'historical_rcp85': 'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-21001230.nc'},
2:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_KNMI-RACMO22E_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19500101-20051231.nc',
'rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_KNMI-RACMO22E_v1_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001231.nc',
'historical_rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_KNMI-RACMO22E_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19500101-21001231.nc'},
3:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20051231.nc',
'rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001231.nc',
'historical_rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-21001231.nc'},
4:{'historical':'Adjust_France_MOHC-HadGEM2-ES_historical_r1i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20051231.nc',
'rcp85': 'Adjust_France_MOHC-HadGEM2-ES_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-20991219.nc',
'historical_rcp85': 'Adjust_France_MOHC-HadGEM2-ES_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20991219.nc'},
}
rsds_Explore2_hist = os.path.join(data_folder,variable+models_dict.get(nmod).get('historical'))
# rlds_Explore2_hist = os.path.join(data_folder,"rldsAdjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19510101-20051231.nc")
# rsds_Explore2_proj = "/media/amounier/MPBE/heavy_data/Explore2/rsdsAdjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc"
if variable == 'rsds':
rsds_array_hist = xr.open_dataset(rsds_Explore2_hist).rsdsAdjust
elif variable == 'tas':
rsds_array_hist = xr.open_dataset(rsds_Explore2_hist).tasAdjust
# rlds_array_hist = xr.open_dataset(rlds_Explore2_hist).rldsAdjust
# rsds_array_proj = xr.open_dataset(rsds_Explore2_proj).rsdsAdjust
rsds_array_hist.rio.write_crs('epsg:27572', inplace=True)
# rlds_array_hist.rio.write_crs('epsg:27572', inplace=True)
# rsds_array_proj.rio.write_crs('epsg:27572', inplace=True)
transformer = pyproj.Transformer.from_crs("EPSG:4326", "EPSG:27572", always_xy=True)
coords_transformed = transformer.transform(*coords)
if False:
cmap = matplotlib.colormaps.get_cmap('viridis')
geom = pd.Series(France().geometry).apply(mapping)
rsds_array_hist = rsds_array_hist.rio.clip(geom, 'epsg:4326', drop=False)
# rsds_array_proj = rsds_array_proj.rio.clip(geom, 'epsg:4326', drop=False)
fig,ax = blank_national_map()
tas = rsds_array_hist.isel(time=42)
img = tas.plot(ax=ax,transform=ccrs.epsg('27572'),add_colorbar=False,
cmap=cmap,vmin=tas.min(),vmax=tas.max())
ax.plot(*coords_transformed, transform=ccrs.epsg('27572'), marker='o',color='red',mec='k')
ax.set_title('')
ax_cb = fig.add_axes([0,0,0.1,0.1])
posn = ax.get_position()
ax_cb.set_position([posn.x0+posn.width+0.02, posn.y0, 0.04, posn.height])
fig.add_axes(ax_cb)
cbar = plt.colorbar(img,cax=ax_cb,extendfrac=0.02)
cbar.set_label('rsdsAdjust')
ax.set_extent(get_extent())
plt.show()
if False:
# hist = rsds_array_hist.mean(('x','y'))
hist_rsds = rsds_array_hist.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
# hist = hist.groupby('time.year').mean('time')
# hist_rlds = rlds_array_hist.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
# proj = rsds_array_proj.mean(('x','y'))
# # proj = tas_array_proj.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
# proj = proj.groupby('time.year').mean('time')
# data_hist_rsds = pd.DataFrame(index=hist_rsds.time, data=hist_rsds).rename(columns={0:'rsds'})
# data_hist_rlds = pd.DataFrame(index=hist_rlds.time, data=hist_rlds).rename(columns={0:'rlds'})
# data_proj = pd.DataFrame(index=proj.year, data=proj)
# data = data_hist_rsds.join(data_hist_rlds)
# data['sum'] = data.rsds + data.rlds
# fig,ax = plt.subplots(figsize=(15,5),dpi=300)
# data_hist_rsds.plot(ax=ax)
# # data_hist_rlds.plot(ax=ax)
# # (data_hist_rsds.rsds+data_hist_rlds.rlds).plot(ax=ax)
# # ax.legend()
# ax.set_ylabel('rsdsAdjust')
# ax.set_xlim([pd.to_datetime('2001-01-01'), pd.to_datetime('2001-12-31')])
# plt.show()
test = 1
# comparaison des CDF entre ERA5 et explore2
if True:
# reanalysis = 'era5'
# reanalysis = 'safran'
data_hist_rsds = pd.DataFrame()
for nmod in range(5):
if variable == 'rsds':
rsds_Explore2_hist = os.path.join(data_folder,"rsds"+models_dict.get(nmod).get('historical'))
rsds_array_hist = xr.open_dataset(rsds_Explore2_hist).rsdsAdjust
elif variable == 'tas':
rsds_Explore2_hist = os.path.join(data_folder,"tas"+models_dict.get(nmod).get('historical'))
rsds_array_hist = xr.open_dataset(rsds_Explore2_hist).tasAdjust
# rsds_array_hist = xr.open_dataset(rsds_Explore2_hist).rsdsAdjust
rsds_array_hist.rio.write_crs('epsg:27572', inplace=True)
transformer = pyproj.Transformer.from_crs("EPSG:4326", "EPSG:27572", always_xy=True)
coords_transformed = transformer.transform(*coords)
hist_rsds = rsds_array_hist.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
if variable == 'tas':
hist_rsds = hist_rsds - 273.15
data_hist_rsds_mod = pd.DataFrame(index=hist_rsds.time, data=hist_rsds).rename(columns={0:'{}_{}'.format(variable,nmod)})
data_hist_rsds = data_hist_rsds.join(data_hist_rsds_mod,how='outer')
data_hist_rsds = data_hist_rsds.dropna()
period_calibration = [1976,2005]
# period_calibration = [2000,2020]
data_hist_rsds = data_hist_rsds[data_hist_rsds.index.year.isin(list(range(period_calibration[0],period_calibration[1]+1)))]
# if reanalysis == 'era5':
data_hist_era5 = get_meteo_data(city,[data_hist_rsds.index.year[0],data_hist_rsds.index.year[-1]],['shortwave_radiation_instant','temperature_2m'])
data_hist_era5 = aggregate_resolution(data_hist_era5,resolution='D',agg_method='mean')
# data_hist_mf =
# elif reanalysis == 'safran':
data_hist_safran = get_safran_weather_data(zcl.center_prefecture,[data_hist_rsds.index.year[0],data_hist_rsds.index.year[-1]],param=['SSI_Q','T_Q'])
data_hist_safran = data_hist_safran.rename(columns={'SSI_Q':'shortwave_radiation_instant','T_Q':'temperature_2m'})
data_hist_safran['shortwave_radiation_instant'] = data_hist_safran.shortwave_radiation_instant / 3600 / 1e-4 / 24 # from J.cm-2.day-1 to Wh.m-2.day-1
# # quantile001_era5 = np.quantile(data_hist,0.01)
# quantile05_era5 = np.quantile(data_hist,0.5)
# quantile099_era5 = np.quantile(data_hist,0.999)
# # quantile001_explore2 = np.quantile(data_hist_rsds,0.01)
# quantile05_explore2 = np.quantile(data_hist_rsds,0.5)
# quantile099_explore2 = np.quantile(data_hist_rsds,0.999)
# # val001 = 1 #quantile001_era5/quantile001_explore2
# val005 = quantile05_era5/quantile05_explore2
# val099 = quantile099_era5/quantile099_explore2
# # print(val001,val099)
# print(quantile05_explore2, val005, val099)
# fig,ax = plt.subplots(dpi=300,figsize=(5,5))
# for idx,c in enumerate(data_hist_rsds.columns):
# if idx != 0:
# continue
# X = np.linspace(data_hist_rsds[c].min(),data_hist_rsds[c].max(),100)
# corr = [min(1,((X[i]-quantile05_explore2)/(quantile099_explore2-quantile05_explore2)*(val099-val005)+val005)) if X[i] > quantile05_explore2 else 1. for i in range(len(X))]
# ax.plot(X, corr,color='k')
# ax.set_ylim(bottom=0.)
# ax.set_ylabel('Solar correction function')
# ax.set_xlabel('Daily mean Explore2 solar radiation (W.m$^{-2}$)')
# plt.savefig(os.path.join(figs_folder,'solar_coorection_function_{}.png'.format(city)), bbox_inches='tight')
# plt.show()
# for c in data_hist_rsds.columns:
# corr = [min(1,((data_hist_rsds[c].values[i]-quantile05_explore2)/(quantile099_explore2-quantile05_explore2)*(val099-val005)+val005)) if data_hist_rsds[c].values[i] > quantile05_explore2 else 1. for i in range(len(data_hist_rsds))]
# data_hist_rsds[c] = data_hist_rsds[c] * np.asarray(corr)
cmap = matplotlib.colormaps.get_cmap('viridis')
if season == False:
for month in range(1,13):
data_hist_rsds_month = data_hist_rsds[data_hist_rsds.index.month==month]
data_hist_safran_month = data_hist_safran[data_hist_safran.index.month==month]
data_hist_era5_month = data_hist_era5[data_hist_era5.index.month==month]
# print(month, data_hist_month.max())
x_var = {'rsds':'shortwave_radiation_instant','tas':'temperature_2m'}.get(variable)
xlabel = {'rsds':'Daily mean shortwave solar radiation (W.m$^{-2}$)',
'tas':'Daily mean temperature (°C)'}.get(variable)
fig,ax = plt.subplots(dpi=300,figsize=(5,5))
sns.ecdfplot(data=data_hist_safran_month, x=x_var,ax=ax,color='k',label='SAFRAN',zorder=2)
# sns.ecdfplot(data=data_hist_era5_month, x="shortwave_radiation_instant",ax=ax,color='tab:red',label='ERA5',zorder=2)
for nmod in range(5):
sns.ecdfplot(data=data_hist_rsds_month, x="{}_{}".format(variable, nmod),ax=ax,
color=cmap(nmod/5),label='Model {}'.format(nmod+1),ls=[':','--','-.',':','--'][nmod],zorder=1)
ax.set_title('{} - {} ({}-{})'.format(city, pd.to_datetime('2000-{:02d}-01'.format(month)).strftime('%B'),data_hist_rsds.index.year[0],data_hist_rsds.index.year[-1]))
ax.legend()
ax.set_xlabel(xlabel)
ax.set_ylabel('Cumulative distribution function')
plt.savefig(os.path.join(figs_folder,'CDF_{}_{}_month{}_{}.png'.format(variable,city,month,calib_method.lower())), bbox_inches='tight')
plt.show()
else:
for seas in ['DJF','MAM','JJA','SON']:
month_dict = {'DJF':[12,1,2],
'MAM':[3,4,5],
'JJA':[6,7,8],
'SON':[9,10,11],}
data_hist_rsds_month = data_hist_rsds[data_hist_rsds.index.month.isin(month_dict.get(seas))]
data_hist_safran_month = data_hist_safran[data_hist_safran.index.month.isin(month_dict.get(seas))]
data_hist_era5_month = data_hist_era5[data_hist_era5.index.month.isin(month_dict.get(seas))]
# print(month, data_hist_month.max())
x_var = {'rsds':'shortwave_radiation_instant','tas':'temperature_2m'}.get(variable)
xlabel = {'rsds':'Daily mean shortwave solar radiation (W.m$^{-2}$)',
'tas':'Daily mean temperature (°C)'}.get(variable)
fig,ax = plt.subplots(dpi=300,figsize=(5,5))
sns.ecdfplot(data=data_hist_safran_month, x=x_var,ax=ax,color='k',label='SAFRAN',zorder=2)
# sns.ecdfplot(data=data_hist_era5_month, x="shortwave_radiation_instant",ax=ax,color='tab:red',label='ERA5',zorder=2)
for nmod in range(5):
sns.ecdfplot(data=data_hist_rsds_month, x="{}_{}".format(variable, nmod),ax=ax,
color=cmap(nmod/5),label='Model {}'.format(nmod+1),ls=[':','--','-.',':','--'][nmod],zorder=1)
ax.set_title('{} - {} ({}-{})'.format(city, seas,data_hist_rsds.index.year[0],data_hist_rsds.index.year[-1]))
ax.legend()
ax.set_xlabel(xlabel)
ax.set_ylabel('Cumulative distribution function')
plt.savefig(os.path.join(figs_folder,'CDF_{}_{}_season{}_{}.png'.format(variable,city,seas,calib_method.lower())), bbox_inches='tight')
plt.show()
# data_hist_rsds_month = data_hist_rsds.copy()
# data_hist_month = data_hist.copy()
# fig,ax = plt.subplots(dpi=300,figsize=(5,5))
# sns.ecdfplot(data=data_hist_month, x="shortwave_radiation_instant",ax=ax,color='k',label='ERA5')
# for nmod in range(5):
# sns.ecdfplot(data=data_hist_rsds_month, x="rsds_{}".format(nmod),ax=ax,
# color=cmap(nmod/5),label='Model {}'.format(nmod),ls=[':','--','-.',':','--'][nmod])
# ax.set_title('{} ({}-{})'.format(city,data_hist_rsds.index.year[0],data_hist_rsds.index.year[-1]))
# ax.legend()
# ax.set_xlabel('Daily mean shortwave solar radiation (W.m$^{-2}$)')
# ax.set_ylabel('Cumulative distribution function')
# plt.savefig(os.path.join(figs_folder,'CDF_rsds_{}_year_adamont.png'.format(city)), bbox_inches='tight')
# plt.show()
# fig,ax = plt.subplots(dpi=300,figsize=(5,5))
# data_hist.plot(ax=ax,color='k')
# data_hist_rsds.plot(ax=ax)
# ax.set_xlim([pd.to_datetime('2001-{:02d}-01'.format(month)), pd.to_datetime('2001-{:02d}-28'.format(month))])
# ax.set_xlabel('')
# plt.show()
if False:
# hist = rsds_array_hist.mean(('x','y'))
hist = rsds_array_hist.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
hist = hist.groupby('time.year').mean('time')
# proj = rsds_array_proj.mean(('x','y'))
# proj = rsds_array_proj.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
# proj = proj.groupby('time.year').mean('time')
data_hist = pd.DataFrame(index=hist.year, data=hist)
data_proj = pd.DataFrame(index=proj.year, data=proj)
data = pd.concat([data_hist, data_proj]).rename(columns={0:'rsdsAdjust'})
fig,ax = plt.subplots(figsize=(5,5),dpi=300)
data.plot(ax=ax,color='k')
ax.legend()
ax.set_ylabel('rsdsAdjust')
plt.show()
#%% Formatage des données de modèles climatiques
if False:
# calib_method = 'CDFt'
calib_method = 'ADAMONT'
if calib_method == 'CDFt':
models_dict = {0:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19510101-20051231.nc',
# 'rcp45':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp45_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc',
'rcp85':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'},
1:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_MOHC-HadREM3-GA7-05_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19520101-20051231.nc',
'rcp85':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v2_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001230.nc'},
2:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_KNMI-RACMO22E_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19500101-20051231.nc',
# 'rcp45':'Adjust_France_ICHEC-EC-EARTH_rcp45_r12i1p1_KNMI-RACMO22E_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc',
'rcp85':'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_KNMI-RACMO22E_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'},
3:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19520101-20051231.nc',
'rcp85':'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-21001231.nc'},
4:{'historical':'Adjust_France_MOHC-HadGEM2-ES_historical_r1i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_19520101-20051231.nc',
'rcp85':'Adjust_France_MOHC-HadGEM2-ES_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v1_LSCE-IPSL-CDFt-L-1V-0L-1976-2005_day_20060101-20991219.nc'},
}
elif calib_method == 'ADAMONT':
models_dict = {0:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CNRM-ALADIN63_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19510101-20051231.nc',
'rcp85': 'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001231.nc',
'historical_rcp85':'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_CNRM-ALADIN63_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19510101-21001231.nc'},
1:{'historical':'Adjust_France_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_MOHC-HadREM3-GA7-05_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20051231.nc',
'rcp85': 'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v2_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001230.nc',
'historical_rcp85': 'Adjust_France_CNRM-CERFACS-CNRM-CM5_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v2_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-21001230.nc'},
2:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_KNMI-RACMO22E_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19500101-20051231.nc',
'rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_KNMI-RACMO22E_v1_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001231.nc',
'historical_rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_KNMI-RACMO22E_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19500101-21001231.nc'},
3:{'historical':'Adjust_France_ICHEC-EC-EARTH_historical_r12i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20051231.nc',
'rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-21001231.nc',
'historical_rcp85': 'Adjust_France_ICHEC-EC-EARTH_rcp85_r12i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-21001231.nc'},
4:{'historical':'Adjust_France_MOHC-HadGEM2-ES_historical_r1i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20051231.nc',
'rcp85': 'Adjust_France_MOHC-HadGEM2-ES_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_20060101-20991219.nc',
'historical_rcp85': 'Adjust_France_MOHC-HadGEM2-ES_rcp85_r1i1p1_MOHC-HadREM3-GA7-05_v1_MF-ADAMONT-SAFRAN-1980-2011_day_19520101-20991219.nc'},
}
if external_disk_connection:
data_folder = '/media/amounier/MPBE/heavy_data/Explore2'
else:
data_folder = os.path.join('data','Explore2')
climate_vars = ['tas','tasmax','tasmin','rsds']
climate_vars = ['tas']
# climate_vars = ['rsds']
for climate_var in climate_vars:
# print('climate var',climate_var)
# climate_var = 'rsds' #'tas','tasmax','tasmin','rsds'
for mod in range(0,5):
# print('\tmodel',mod)
# for mod in range(4,5):
# Explore2_hist = os.path.join(data_folder,climate_var+models_dict.get(mod).get('historical'))
# array_hist = xr.open_dataset(Explore2_hist)
# array_hist = array_hist[list(array_hist.data_vars)[-1]]
# if 'tas' in climate_var:
# array_hist = array_hist - 273.15
# array_hist.rio.write_crs('epsg:27572', inplace=True)
# Explore2_proj = os.path.join(data_folder,climate_var+models_dict.get(mod).get('rcp85'))
# array_proj = xr.open_dataset(Explore2_proj)
# array_proj = array_proj[list(array_proj.data_vars)[-1]]
# if 'tas' in climate_var:
# array_proj = array_proj - 273.15
# array_proj.rio.write_crs('epsg:27572', inplace=True)
Explore2 = os.path.join(data_folder,climate_var+models_dict.get(mod).get('historical_rcp85'))
array = xr.open_dataset(Explore2)
array = array[list(array.data_vars)[-1]]
array.rio.write_crs('epsg:27572', inplace=True)
# concatenation des valeurs journalières
if False:
climats = France().climats
for zcl in climats:
# TODO : changer les villes des zcl
# TODO : vérifier de que ça marche
zcl = Climat(zcl)
city = zcl.center_prefecture
coords = get_coordinates(city)
save_name = '{}_mod{}_{}.csv'.format(climate_var,mod,zcl.code)
if save_name in os.listdir(os.path.join(output, folder)):
continue
transformer = pyproj.Transformer.from_crs("EPSG:4326", "EPSG:27572", always_xy=True)
coords_transformed = transformer.transform(*coords)
point_data = array.sel(x=coords_transformed[0], y=coords_transformed[1], method='nearest')
data = pd.DataFrame(index=point_data.time, data=point_data).rename(columns={0:climate_var})
print('saving {}_mod{}_{}'.format(climate_var,mod,zcl.code))
data.to_csv(os.path.join(os.path.join(output, folder),save_name))
# affichage des données journalieres
if False: