-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclimate_works.py
1434 lines (1331 loc) · 56.3 KB
/
climate_works.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
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 15 08:21:02 2020
@author: shlomi
"""
from sklearn_xarray import RegressorWrapper
from PW_paths import work_yuval
climate_path = work_yuval / 'climate'
era5_path = work_yuval / 'ERA5'
ims_path = work_yuval / 'IMS_T'
lat_box = [10, 50]
lon_box = [10, 60]
lat_box1 = [10, 60]
lon_box1 = [-10, 60]
lat_hemi_box = [0, 80]
lon_hemi_box = [-80, 80]
# what worked: z500 is OK, compare to other large scale cirulations ?
def prepare_diurnal_temperature_range(era5_path=era5_path, ims_path=ims_path):
import xarray as xr
from aux_gps import groupby_date_xr
from aux_gps import save_ncfile
import pandas as pd
t2 = xr.load_dataset(era5_path / 'ERA5_T2_hourly_israel_1996-2020.nc')
date = groupby_date_xr(t2)
t2_min = t2.groupby(date).min()
t2_max = t2.groupby(date).max()
dtr = t2_max['t2m'] - t2_min['t2m']
dtr = dtr.rename({'date': 'time'})
dtr['time'] = pd.to_datetime(dtr['time'].values)
ds = xr.Dataset()
ds['DTR_mm'] = dtr
ds['DTR_mm'].attrs['long_name'] = 'Diurnal Temperature Range'
ds['DTR_mm'].attrs['method'] = 'max-min'
ds['DTR_mm'].attrs['units'] = 'degC'
# maybe Local Time ?
t2_12 = t2.sel(time=t2['time.hour'] == 12).resample(time='1D').mean()
t2_00 = t2.sel(time=t2['time.hour'] == 00).resample(time='1D').mean()
dtr = t2_12['t2m'] - t2_00['t2m']
ds['DTR_1200'] = dtr
ds['DTR_1200'].attrs['long_name'] = 'Diurnal Temperature Range'
ds['DTR_1200'].attrs['method'] = '12UTC-00UTC'
ds['DTR_1200'].attrs['units'] = 'degC'
filename = 'ERA5_DTR_israel_1996-2020.nc'
save_ncfile(ds, era5_path, filename)
# do the same thing for IMS data (at gnss loc)
t = xr.load_dataset(ims_path / 'GNSS_5mins_TD_ALL_1996_2020.nc')
date = groupby_date_xr(t)
t2_min = t.groupby(date).min()
t2_max = t.groupby(date).max()
dtr = t2_max - t2_min
dtr = dtr.rename({'date': 'time'})
dtr['time'] = pd.to_datetime(dtr['time'].values)
# dtr.name = 'DTR_mm'
dtr.attrs['long_name'] = 'Diurnal Temperature Range'
dtr.attrs['method'] = 'max-min'
dtr.attrs['units'] = 'degC'
filename = 'GNSS_IMS_DTR_mm_israel_1996-2020.nc'
save_ncfile(dtr, ims_path, filename)
t2_12 = t.sel(time=t['time.hour'] == 12).resample(time='1D').mean()
t2_00 = t.sel(time=t['time.hour'] == 00).resample(time='1D').mean()
dtr = t2_12 - t2_00
# dtr.name = 'DTR_1200'
dtr.attrs['long_name'] = 'Diurnal Temperature Range'
dtr.attrs['method'] = '12UTC-00UTC'
dtr.attrs['units'] = 'degC'
filename = 'GNSS_IMS_DTR_1200_israel_1996-2020.nc'
save_ncfile(dtr, ims_path, filename)
return ds
def prepare_ERA5_single_var_EM(era5_path=era5_path, var='tcwv'):
import xarray as xr
from aux_gps import save_ncfile
ds = xr.open_dataset(
era5_path / 'ERA5_single_vars_mm_EM_area_1979-2020.nc')
da = ds[var]
da = da.sel(expver=1)
save_ncfile(da, era5_path, 'ERA5_{}_mm_EM_area_1979-2020.nc'.format(var))
return
def prepare_ERA5_moisture_flux_mm_using_dask(era5_path=era5_path):
import xarray as xr
from dask.diagnostics import ProgressBar
u = xr.open_dataset(
era5_path/'ERA5_U_mm_EM_area_1979-2020.nc', chunks={"time": 40})['u']
v = xr.open_dataset(
era5_path/'ERA5_V_mm_EM_area_1979-2020.nc', chunks={"time": 40})['v']
q = xr.open_dataset(
era5_path/'ERA5_Q_mm_EM_area_1979-2020.nc', chunks={"time": 40})['q']
u = u.sel(expver=1).reset_coords(drop=True)
v = v.sel(expver=1).reset_coords(drop=True)
q = q.sel(expver=1).reset_coords(drop=True)
qu = q * u
qu.name = 'qu'
qv = q * v
qv.name = 'qv'
qu.attrs['units'] = u.attrs['units']
qv.attrs['units'] = v.attrs['units']
qu.attrs['long_name'] = 'U component of moisture flux'
qu.attrs['standard_name'] = 'eastward moisture flux'
qv.attrs['long_name'] = 'V component moisture flux'
qv.attrs['standard_name'] = 'northward moisture flux'
# ds = ds.sortby('latitude')
# ds = ds.sortby('level', ascending=False)
comp = dict(zlib=True, complevel=9)
encoding_qu = {var: comp for var in qu.to_dataset()}
encoding_qv = {var: comp for var in qv.to_dataset()}
qu_filename = 'ERA5_QU_mm_EM_area_1979-2020.nc'
qv_filename = 'ERA5_QV_mm_EM_area_1979-2020.nc'
qu_delayed = qu.to_netcdf(era5_path / qu_filename,
'w', encoding=encoding_qu, compute=False)
qv_delayed = qv.to_netcdf(era5_path / qv_filename,
'w', encoding=encoding_qv, compute=False)
with ProgressBar():
results = qu_delayed.compute()
with ProgressBar():
results = qv_delayed.compute()
return
def prepare_ERA5_moisture_flux(era5_path=era5_path):
"""
loads 12UTC q, u and v ERA5 fields above Israel (pressure levels)
and produces q*u and q*v and save them to files, also produces mean
anomalies.
Parameters
----------
era5_path : TYPE, optional
save and load path. The default is era5_path.
Returns
-------
None.
"""
import xarray as xr
from aux_gps import save_ncfile
from aux_gps import anomalize_xr
import numpy as np
from aux_gps import convert_wind_direction
from dask.diagnostics import ProgressBar
ds = xr.open_dataset(
era5_path / 'ERA5_UVQ_4xdaily_israel_1996-2019.nc', chunks={'level': 5})
# ds = ds.resample(time='D', keep_attrs=True).mean(keep_attrs=True)
# ds.attrs['action'] = 'resampled to 1D from 12:00UTC data points'
mf = (ds['q'] * ds['u']).to_dataset(name='qu')
mf.attrs = ds.attrs
mf['qu'].attrs['units'] = ds['u'].attrs['units']
mf['qu'].attrs['long_name'] = 'U component of moisture flux'
mf['qu'].attrs['standard_name'] = 'eastward moisture flux'
mf['qv'] = ds['q'] * ds['v']
mf['qv'].attrs['units'] = ds['v'].attrs['units']
mf['qv'].attrs['long_name'] = 'V component moisture flux'
mf['qv'].attrs['standard_name'] = 'northward moisture flux'
mf['qf'], mf['qfdir'] = convert_wind_direction(u=mf['qu'], v=mf['qv'])
mf['qf'].attrs['units'] = ds['v'].attrs['units']
mf['qf'].attrs['long_name'] = 'moisture flux magnitude'
# mf['qfdir'] = 270 - np.rad2deg(np.arctan2(mf['qv'], mf['qu']))
mf['qfdir'].attrs['units'] = 'deg'
mf['qfdir'].attrs['long_name'] = 'moisture flux direction (meteorological)'
mf = mf.sortby('latitude')
mf = mf.sortby('level', ascending=False)
comp = dict(zlib=True, complevel=9)
encoding_mf = {var: comp for var in mf}
mf_delayed = mf.to_netcdf(era5_path / 'ERA5_MF_4xdaily_israel_1996-2019.nc',
'w', encoding=encoding_mf, compute=False)
mf_anoms = anomalize_xr(mf, freq='MS', time_dim='time')
mf_anoms_mean = mf_anoms.mean('latitude').mean('longitude')
encoding_mf_anoms = {var: comp for var in mf_anoms}
mf_anoms_delayed = mf_anoms_mean.to_netcdf(era5_path / 'ERA5_MF_anomalies_4xdaily_israel_mean_1996-2019.nc',
'w', encoding=encoding_mf_anoms, compute=False)
with ProgressBar():
results = mf_delayed.compute()
with ProgressBar():
results1 = mf_anoms_delayed.compute()
# save_ncfile(mf, era5_path, 'ERA5_MF_4xdaily_israel_1996-2019.nc')
# mf_anoms = anomalize_xr(mf, freq='MS', time_dim='time')
# mf_anoms_mean = mf_anoms.mean('latitude').mean('longitude')
# save_ncfile(mf_anoms_mean, era5_path,
# 'ERA5_MF_anomalies_4xdaily_israel_mean_1996-2019.nc')
return
def create_synoptic_mean_qflux_index(era5_path=era5_path, level=750,
syn_class='upper', savepath=None):
from synoptic_procedures import agg_month_syn_class_continous_variable
import xarray as xr
from aux_gps import save_ncfile
from aux_gps import rename_data_vars
ds = xr.load_dataset(
era5_path/'ERA5_MF_anomalies_4xdaily_israel_mean_1996-2019.nc')
if syn_class == 'upper':
syn_cat = 'RST'
elif syn_class == 'isabella':
syn_cat = 1
qf = ds['qf'].sel(level=level, method='nearest')
qf = qf.resample(time='D').mean()
level = qf.level.item()
qf = qf.reset_coords(drop=True)
da_agg = agg_month_syn_class_continous_variable(qf, syn_cat=syn_cat,
return_all_syn_cats=True)
syns = da_agg.fillna(0).to_dataset('syn_class')
syns = rename_data_vars(syns, suffix='')
if savepath is not None:
filename = 'qf_{}_{}_class_index.nc'.format(level, syn_class)
save_ncfile(syns, savepath, filename)
return syns
def plot_world_map_with_box(lat_bounds=lat_box, lon_bounds=lon_box, save=True):
import geopandas as gpd
from shapely.geometry import Point, LineString
import matplotlib.pyplot as plt
from PW_from_gps_figures import savefig_path
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
point1 = [lon_bounds[0], lat_bounds[0]]
point2 = [lon_bounds[0], lat_bounds[1]]
point3 = [lon_bounds[1], lat_bounds[1]]
point4 = [lon_bounds[1], lat_bounds[0]]
line1 = LineString([Point(*point1), Point(*point2)])
line2 = LineString([Point(*point2), Point(*point3)])
line3 = LineString([Point(*point3), Point(*point4)])
line4 = LineString([Point(*point4), Point(*point1)])
geo_df = gpd.GeoDataFrame(geometry=[line1, line2, line3, line4])
fig, ax = plt.subplots(figsize=(15, 10))
world.plot(ax=ax)
geo_df.plot(ax=ax, color='k')
fig.tight_layout()
if save:
filename = 'world_with_box.png'
plt.savefig(savefig_path / filename, bbox_inches='tight')
return
def plot_eof_from_ds(ds, var='v1000', mode=1, ax=None):
var_name_in_ds = '{}_eofs'.format(var)
eof = ds[var_name_in_ds].sel(mode=mode)
# def read_climate_classification(path=climate_path):
# import pandas as pd
# import numpy as np
# file = path / 'Koeppen-Geiger-ASCII.txt'
# df = pd.read_csv(file, delim_whitespace=True)
# df.columns = ['latitude', 'longitude', 'Climate_Class']
# ds = df.groupby(['latitude', 'longitude']).first().to_xarray()
# return ds
def read_climate_classification_legend(path=climate_path):
import pandas as pd
import numpy as np
file = path / 'koppen_legend.txt'
df = pd.read_csv(file, header=None, sep=':')
df.columns = [
'class_int',
'class_code',
'class_description',
'pixel_range']
df.drop(df.tail(6).index, inplace=True)
df['class_int'] = df['class_int'].astype(int)
df['class_code'] = df['class_code'].str.replace(' ', '')
df = df.set_index('class_int')
df['pixel_range'] = df['pixel_range'].str.lstrip()
df['pixel_range'] = df['pixel_range'].str.replace(' ', ',')
li = df['pixel_range'].str.split(',').tolist()
p1 = [int(x[0].replace('[', '')) for x in li]
p2 = [int(x[1]) for x in li]
p3 = [int(x[2].replace(']', '')) for x in li]
df['pixel1'] = p1
df['pixel2'] = p2
df['pixel3'] = p3
df['color'] = list(zip(df['pixel1'].astype(float) / 255,
df['pixel2'].astype(float) / 255,
df['pixel3'].astype(float) / 255))
df['color']
df.drop('pixel_range', axis=1, inplace=True)
df.drop('pixel1', axis=1, inplace=True)
df.drop('pixel2', axis=1, inplace=True)
df.drop('pixel3', axis=1, inplace=True)
return df
def assign_climate_classification_to_gnss(path=climate_path):
import xarray as xr
import pandas as pd
from PW_stations import produce_era5_field_at_gnss_coords
ras = xr.open_rasterio(climate_path / 'Beck_KG_V1_present_0p0083.tif')
ds = ras.isel(band=0)
ds = ds.rename({'x': 'longitude', 'y': 'latitude'})
cc = produce_era5_field_at_gnss_coords(ds)
cc = cc.astype(int)
# corrections:
cc['csar'] = 8
cc['yrcm'] = 6
cc['drag'] = 6
cc['ramo'] = 4
cc = cc.expand_dims('class_int')
cc_ser = cc.to_dataframe().T
# read classification legend:
df = read_climate_classification_legend(path=path)
d = df['class_code'].to_dict()
c_code_ser = cc_ser[0].map(d)
d = df['class_description'].to_dict()
c_desc_ser = cc_ser[0].map(d)
df = pd.concat([cc_ser, c_code_ser, c_desc_ser], axis=1)
df.columns = ['climate_int', 'code', 'description']
df.index.name = 'station'
df.to_csv(path / 'gnss_station_climate_code.csv')
return df
def create_index_from_ds_eofs(ds, var='v1000', savepath=climate_path):
from aux_gps import save_ncfile
var_name_in_ds = '{}_pcs'.format(var)
pc_ds = ds[var_name_in_ds].to_dataset('mode')
names = [x for x in pc_ds]
new_names = ['{}_{}'.format(var, x) for x in names]
nd = dict(zip(names, new_names))
pc_ds = pc_ds.rename(nd)
if savepath is not None:
filename = '{}_index.nc'.format(var)
save_ncfile(pc_ds, savepath, filename)
return pc_ds
def run_EOFs_on_level_field(da, level_bins=[1000, 700, 500, 300, 1], npcs=4,
level_mean=True, level_dim='level', savepath=None):
from aux_gps import save_ncfile
import xarray as xr
da = da.sortby(level_dim, ascending=False)
pc_list = []
eof_list = []
for previous, current in zip(level_bins, level_bins[1:]):
da_bin = da.sel({level_dim: slice(previous, current)})
da_bin.name = '{}{}'.format(da.name, previous)
if level_mean:
print('mean on {} to {} hPa'.format(previous, current))
da_bin = da_bin.mean(level_dim)
pc, eof = eof_analysis(da_bin, npcs=npcs, return_all=True, plot=False)
pc_list.append(pc)
eof_list.append(eof)
ds_eof = xr.merge(eof_list)
ds_pc = xr.merge(pc_list)
ds = xr.merge([ds_eof, ds_pc])
ds.attrs['level_bins'] = level_bins
ds.attrs['level_mean'] = int(level_mean)
if savepath is not None:
if level_mean:
filename = 'ERA5_pc_eofs_{}_plevels_mean.nc'.format(da.name)
else:
filename = 'ERA5_pc_eofs_{}_plevels.nc'.format(da.name)
save_ncfile(ds, savepath, filename)
return ds
def prepare_ERA5_field(da, lon_roll=True, expver=1, time_dim='time', name=None,
lat_dim='latitude', lon_dim='longitude',
scope='global', savepath=None):
from aux_gps import save_ncfile
if 'expver' in da.dims:
da = da.sel(expver=expver).reset_coords(drop=True)
if lat_dim in da.dims:
da = da.sortby(lat_dim)
if lon_dim in da.dims:
if lon_roll:
if min(da[lon_dim]) >= 0:
da = da.roll({lon_dim: -180}, roll_coords=False)
da = da.assign_coords({lon_dim: da[lon_dim] - 180})
else:
print('no need to lon_roll.')
da = da.dropna(time_dim)
if name is not None:
da.name = name
if savepath is not None:
yrmin = da[time_dim].min().dt.year.item()
yrmax = da[time_dim].max().dt.year.item()
filename = 'ERA5_{}_mm_{}_{}-{}.nc'.format(da.name, scope, yrmin, yrmax)
save_ncfile(da, savepath, filename)
return da
def create_single_vars_indices(path=era5_path, savepath=climate_path,
var='msl', lats=lat_box, lons=lon_box,
anomalize_before_eof=None, lon_dim='longitude',
lat_dim='latitude'):
"""anomalize_before_eof is None - no deseasoning, True: before EOF, False: after EOF"""
import xarray as xr
from aux_gps import path_glob
print('creating {} index.'.format(var))
files = path_glob(path, 'ERA5_{}_mm_global_*.nc'.format(var))
v = xr.open_dataset(files[0])[var]
v_box = v.sel({lat_dim: slice(*lats), lon_dim: slice(*lons)})
print('subsetting to lats: {}-{}, lons: {}-{}'.format(*lats, *lons))
pc_var = produce_local_index_from_eof_analysis(v_box, npcs=4,
with_mean=False,
savepath=savepath,
plot=True,
anomalize_before_eof=anomalize_before_eof)
return pc_var
def produce_local_stations_anomalies_index(da, savepath=climate_path,
plot=False):
from aux_gps import anomalize_xr
from aux_gps import save_ncfile
from PW_stations import produce_era5_field_at_gnss_coords
da_at_st = produce_era5_field_at_gnss_coords(da, savepath=None)
da_at_st = da_at_st.dropna('time')
da_anoms = anomalize_xr(da_at_st, 'MS', time_dim='time')
da_ind = da_anoms.to_array('st').mean('st')
da_ind.name = da.name
da_ind.attrs = da.attrs
if plot:
da_ind.plot()
filename = 'ERA5_{}_index.nc'.format(da.attrs['long_name'])
save_ncfile(da_ind, savepath, filename)
return da_ind
def produce_local_index_from_eof_analysis(da, npcs=2, with_mean=False,
savepath=None, plot=False,
anomalize_before_eof=True):
from aux_gps import anomalize_xr
from aux_gps import save_ncfile
from aux_gps import keep_iqr
if anomalize_before_eof is not None:
if anomalize_before_eof:
print('anomalizing before EOF')
da = anomalize_xr(da, 'MS', time_dim='time')
pc = eof_analysis(da, npcs=npcs, plot=plot)
pc_mean = pc.mean('mode')
pc_mean.name = pc.name + '_mean'
# pc = keep_iqr(pc)
pc_ds = pc.to_dataset('mode')
names = [x for x in pc_ds]
new_names = ['{}_{}'.format(da.name, x) for x in names]
# new_names = [x.replace('pc', da.name) for x in names]
nd = dict(zip(names, new_names))
pc_ds = pc_ds.rename(nd)
if anomalize_before_eof is not None:
if not anomalize_before_eof:
pc_ds = anomalize_xr(pc_ds, 'MS')
if with_mean:
pc_ds[pc_mean.name] = pc_mean
if savepath is not None:
filename = '{}_index.nc'.format(pc.name)
save_ncfile(pc_ds, savepath, filename)
return pc_ds
def eof_analysis(da, npcs=2, return_all=False, plot=True,
lat_weights_on='latitude'):
from eofs.xarray import Eof
import matplotlib.pyplot as plt
import numpy as np
if lat_weights_on is not None:
coslat = np.cos(np.deg2rad(
da.coords[lat_weights_on].values)).clip(0., 1.)
wgts = np.sqrt(coslat)[..., np.newaxis]
else:
wgts = None
solver = Eof(da, weights=wgts)
eof = solver.eofsAsCorrelation(neofs=npcs)
pc = solver.pcs(npcs=npcs, pcscaling=1)
pc.name = '{}_{}'.format(da.name, pc.name)
eof.name = '{}_{}'.format(da.name, eof.name)
pc['mode'] = [int('{}'.format(x + 1)) for x in pc.mode.values]
eof['mode'] = [int('{}'.format(x + 1)) for x in eof.mode.values]
vf = solver.varianceFraction(npcs)
errors = solver.northTest(npcs, vfscaled=True)
if plot:
plt.close('all')
# plt.figure(figsize=(8, 6))
# eof.plot(hue='mode')
plt.figure(figsize=(10, 4))
pc.plot(hue='mode')
plt.figure(figsize=(8, 6))
x = np.arange(1, len(vf.values) + 1)
y = vf.values
ax = plt.gca()
ax.errorbar(x, y, yerr=errors.values, color='b', linewidth=2, fmt='-o')
ax.set_xticks(np.arange(1, len(vf.values) + 1, 1))
ax.set_yticks(np.arange(0, 1, 0.1))
ax.grid()
ax.set_xlabel('Eigen Values')
plt.show()
if return_all:
return pc, eof
else:
return pc
def read_VN_table(path=climate_path, savepath=None):
import pandas as pd
from aux_gps import path_glob
from aux_gps import save_ncfile
file = path_glob(path, 'vonNeumannCV_Shlomi.xlsx')[0]
df = pd.read_excel(file, header=1)
df.columns = ['sample_size', 0.001, 0.01, 0.05, 0.95, 0.99, 0.999]
df.set_index('sample_size', inplace=True)
cv_da = df.to_xarray().to_array('pvalue')
cv_da.name = 'VN_CV'
cv_da.attrs['full_name'] = 'Von Nuemann ratio test critical values table'
if savepath is not None:
filename = 'VN_critical_values.nc'
save_ncfile(cv_da, savepath, filename)
return cv_da
def prepare_ORAS5_download_script(path=work_yuval, var='sossheig'):
from aux_gps import path_glob
files = path_glob(path, 'wget_oras5*.sh')
for file in files:
filename = file.as_posix().split('/')[-1].split('.')[0]
print('reading file {} file'.format(filename))
with open(file) as f:
content = f.readlines()
var_content = [x for x in content if var in x]
new_filename = filename + '_{}.sh'.format(var)
with open(path / new_filename, 'w') as fi:
for item in var_content:
fi.write("%s\n" % item)
return
def create_index_from_synoptics(path=climate_path, syn_cat='normal',
normalize='zscore'):
"""create a long term index from synoptics"""
from aux_gps import anomalize_xr
from aux_gps import annual_standertize
from aux_gps import Zscore_xr
from synoptic_procedures import agg_month_count_syn_class
da = agg_month_count_syn_class(path=path, syn_category=syn_cat,
freq=False)
ds = da.to_dataset('syn_cls')
ds = anomalize_xr(ds, 'MS')
ds = ds.fillna(0)
if normalize is not None:
if normalize == 'zscore':
return Zscore_xr(ds)
elif normalize == 'longterm':
ds = annual_standertize(ds)
ds = ds.fillna(0)
return ds
else:
return ds
def read_ea_index(path=climate_path):
from aux_gps import path_glob
from aux_gps import save_ncfile
import pandas as pd
file = path_glob(path, 'ea_index.txt')[0]
df = pd.read_csv(file, names=['year', 'month',
'ea'], delim_whitespace=True, header=9)
df['time'] = df['year'].astype(str) + '-' + df['month'].astype(str)
df['time'] = pd.to_datetime(df['time'])
df = df.set_index('time')
df = df.sort_index()
df = df.drop(['year', 'month'], axis=1)
da = df.to_xarray()
save_ncfile(da, path, 'ea_index.nc')
return da
def read_west_moi(path=climate_path):
from aux_gps import path_glob
from aux_gps import save_ncfile
import pandas as pd
file = path_glob(path, 'Western_MOI.txt')[0]
df = pd.read_csv(file, delim_whitespace=True)
df['year'] = df.index
df = pd.melt(df, id_vars='year', var_name='month', value_name='wemoi')
df['time'] = df['year'].astype(str) + '-' + df['month'].astype(str)
df['time'] = pd.to_datetime(df['time'])
df = df.set_index('time')
df = df.sort_index()
df = df.drop(['year', 'month'], axis=1)
da = df.to_xarray()
save_ncfile(da, path, 'wemo_index.nc')
return da
def read_iod(path=climate_path):
from aux_gps import path_glob
from aux_gps import save_ncfile
import pandas as pd
file = path_glob(path, 'iod.txt')[0]
df = pd.read_csv(
file,
delim_whitespace=True,
names=[
'year',
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12],
na_values=-
9999.0)
df = pd.melt(df, id_vars='year', var_name='month', value_name='iod')
df['time'] = df['year'].astype(str) + '-' + df['month'].astype(str)
df['time'] = pd.to_datetime(df['time'])
df = df.set_index('time')
df = df.sort_index()
df = df.drop(['year', 'month'], axis=1)
da = df.to_xarray()
save_ncfile(da, path, 'iod_index.nc')
return da
def read_scand_index(path=climate_path, savepath=climate_path):
from aux_gps import save_ncfile
from aux_gps import path_glob
import pandas as pd
file = path_glob(path, 'scand_index.tim')[0]
df = pd.read_csv(file, names=['year', 'month',
'scand'], delim_whitespace=True, header=8)
df['time'] = df['year'].astype(str) + '-' + df['month'].astype(str)
df['time'] = pd.to_datetime(df['time'])
df = df.set_index('time')
df = df.sort_index()
df = df.drop(['year', 'month'], axis=1)
da = df.to_xarray()
save_ncfile(da, path, 'scand_index.nc')
return da
def read_mo_indicies(path=climate_path, moi=1, resample_to_mm=True):
from aux_gps import path_glob
from aux_gps import save_ncfile
import pandas as pd
file = path_glob(path, 'moi{}.dat'.format(moi))[0]
df = pd.read_fwf(file,
names=['year', 'date', 'moi{}'.format(moi)],
widths=[4, 8, 5])
df['date'] = df['date'].str.strip('.')
df['date'] = df['date'].str.strip(' ')
df['date'] = df['date'].str.replace(' ', '0')
df['date'] = df['date'].str.replace('.', '-')
df['time'] = df['year'].astype(str) + '-' + df['date'].astype(str)
df['time'] = pd.to_datetime(df['time'], format='%Y-%m-%d')
df = df.set_index('time')
df = df.drop(['date', 'year'], axis=1)
da = df.to_xarray()
if resample_to_mm:
da = da.resample(time='MS').mean()
save_ncfile(da, path, 'moi{}_index.nc'.format(moi))
return da
def run_best_MLR(savepath=None, heatmap=True, plot=True, keep='lci',
add_trend=True):
from aux_gps import save_ncfile
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# check for correlation between synoptics and maybe
# agg some classes and leave everything else
df = produce_interannual_df(lags=1, smooth=4, corr_thresh=None, syn=None,
drop_worse_lags=False)
syn_class = np.arange(1, 20)
syn_class = [str(x) for x in syn_class]
lci = ['ea', 'iod', 'moi2', 'meiv2']
# can add 3rd EOF if dealing with smaller box:
eofi = ['z500_1', 'z500_2', 'z500_3', 'msl_1', 'msl_2', 'msl_3']
if keep == 'lci':
keep_inds = ['pwv', 'qf700'] + lci
elif keep == 'eofi':
keep_inds = ['pwv', 'qf700'] + eofi
elif keep == 'both':
keep_inds = ['pwv'] + lci + eofi
elif keep == 'syn+lci':
keep_inds = ['pwv'] + lci + syn_class
elif keep == 'qflux':
keep_inds = ['pwv', 'qf700']
elif keep == 'syn_upper':
keep_inds = ['pwv', 'PT', 'RST', 'H', 'CL', 'DS']
elif keep == 'syn_class':
keep_inds = ['pwv'] + [str(x) for x in np.arange(1, 20)]
elif keep == 'qf':
keep_inds = ['pwv', 'qf750']
# keep_inds = ['pwv', 'ea', 'MJO_20E+1','iod+1','moi2', 'u500_1', 'u500_2', 'v500_1','v500_3']
dff = df[keep_inds]
X, y = preprocess_interannual_df(dff, add_trend=add_trend)
model, rdf = run_MLR(X, y, plot=plot)
if heatmap:
corr = X.to_dataset('regressors').to_dataframe().corr()
plt.figure()
sns.heatmap(corr, annot=True, cmap='bwr', center=0.0, vmax=1, vmin=-1)
if savepath is not None:
save_ncfile(model.results_, savepath,
'best_MLR_interannual_gnss_pwv.nc')
return model, rdf
def create_qflux_index(era5_path=era5_path, climate_path=climate_path):
from aux_gps import save_ncfile
from aux_gps import anomalize_xr
from PW_stations import produce_PWV_flux_from_ERA5_UVQ
qflux = produce_PWV_flux_from_ERA5_UVQ(path=era5_path,
return_magnitude=True)
qflux = anomalize_xr(qflux, 'MS')
qflux_index = qflux.to_array('st').mean('st')
qflux_index.name = 'qflux'
save_ncfile(qflux_index, climate_path, 'qflux_index.nc')
return qflux_index
def create_moisture_convergence_index(era5_path=era5_path,
climate_path=climate_path):
from aux_gps import save_ncfile
from aux_gps import anomalize_xr
import xarray as xr
ds_wv = xr.load_dataset(
era5_path /
'ERA5_water_vapor_single_vars_israel_mm_1979-2020.nc')
ds_wv = ds_wv.sel(expver=1).reset_coords(drop=True)
vimd = anomalize_xr(ds_wv['mvimd'], 'MS', time_dim='time')
vimd = -1 * vimd.mean('longitude').mean('latitude')
vimd.name = 'vimd'
save_ncfile(vimd, climate_path, 'vimd_index.nc')
return vimd
def create_qflux_convergence_index(era5_path=era5_path,
climate_path=climate_path):
from aux_gps import save_ncfile
from aux_gps import anomalize_xr
import xarray as xr
ds_wv = xr.load_dataset(
era5_path /
'ERA5_water_vapor_single_vars_israel_mm_1979-2020.nc')
ds_wv = ds_wv.sel(expver=1).reset_coords(drop=True)
mc = anomalize_xr(ds_wv['p84.162'], 'MS', time_time='time')
mc = -1 * mc.mean('longitude').mean('latitude')
mc.name = 'mc'
save_ncfile(mc, climate_path, 'mc_index.nc')
return mc
def produce_interannual_df(climate_path=climate_path, work_path=work_yuval,
lags=1, corr_thresh=0.2, smooth=False,
syn='agg+class', drop_worse_lags=True,
replace_syn=None, times=None, pick_cols=None):
import xarray as xr
from aux_gps import smooth_xr
from synoptic_procedures import upper_class_dict
pw = xr.load_dataset(
work_path /
'GNSS_PW_monthly_anoms_thresh_50.nc')
pw_mean = pw.to_array('station').mean('station')
if times is not None:
pw_mean = pw_mean.sel(time=slice(times[0], times[1]))
if smooth is not None:
if isinstance(smooth, int):
pw_mean = pw_mean.rolling(time=smooth, center=True).mean()
elif isinstance(smooth, str):
pw_mean = smooth_xr(pw_mean)
df_pw = pw_mean.to_dataframe(name='pwv')
# load other large circulation indicies:
ds = load_all_indicies(path=climate_path, smooth=smooth)
df = ds.to_dataframe()
# add lags:
if lags is not None:
inds = [x for x in df.columns]
for ind in inds:
for lag in [x for x in range(1, lags+1)]:
df['{}+{}'.format(ind, lag)] = df[ind].shift(lag)
df['{}-{}'.format(ind, lag)] = df[ind].shift(-lag)
if drop_worse_lags:
df = df_pw.join(df)
# find the best corr for each ind and its lags:
best_inds = []
for ind in inds:
ind_cols = [x for x in df.columns if ind in x]
ind_cols.insert(0, 'pwv')
best_ind = df.corr().loc[ind_cols]['pwv'][1:].abs().idxmax()
best_inds.append(best_ind)
print('best index from its lags: {}'.format(best_ind))
best_inds.insert(0, 'pwv')
df = df[best_inds]
df = df.drop('pwv', axis=1)
# load synoptics:
ds = create_index_from_synoptics(path=climate_path, syn_cat='upper',
normalize=None)
ds_cls = create_index_from_synoptics(path=climate_path, syn_cat='normal',
normalize=None)
# if smooth:
# ds = smooth_xr(ds)
# ds_cls = smooth_xr(ds_cls)
df_syn = ds.to_dataframe()
df_syn_cls = ds_cls.to_dataframe()
if syn is not None:
print('adding synoptic monthly counts.')
if syn == 'agg+class':
df_syn = df_syn.join(df_syn_cls)
if replace_syn is not None:
for agg in replace_syn:
print('dropping {} and keeping {}'.format(
upper_class_dict[agg], agg))
df_syn = df_syn.drop(upper_class_dict[agg], axis=1)
other_to_drop = list(
set(upper_class_dict).difference(set(replace_syn)))
df_syn = df_syn.drop(other_to_drop, axis=1)
print('dropping {}.'.format(other_to_drop))
elif syn == 'agg':
df_syn = df_syn
elif syn == 'class':
df_syn = df_syn_cls
# implement mixed class and agg, e.g., RST (and remove 1, 2, 3)
df = df.join(df_syn)
# sort cols:
df.columns = [str(x) for x in df.columns]
cols = sorted([x for x in df.columns])
df = df[cols]
# df = df.dropna()
df = df_pw.join(df)
if corr_thresh is not None:
corr = df.corr()['pwv']
corr = corr[abs(corr) > corr_thresh]
inds = corr.index
df = df[inds]
if pick_cols is not None:
pwv = df['pwv']
cols = [x for x in df.columns if pick_cols in x]
df = df[cols]
df['pwv'] = pwv
return df
def preprocess_interannual_df(df, yname='pwv', standartize=True,
add_trend=True):
from aux_gps import Zscore_xr
import pandas as pd
import numpy as np
jul = pd.to_datetime(df.index).to_julian_date()
med = np.median(jul)
jul -= med
if add_trend:
df['trend'] = jul
df = df.dropna()
y = df[yname].to_xarray()
xnames = [x for x in df.columns if yname not in x]
X = df[xnames].to_xarray().to_array('regressors')
X = X.transpose('time', 'regressors')
if standartize:
X = Zscore_xr(X)
return X, y
def load_all_indicies(path=climate_path, smooth=None, zscore=False):
from aux_gps import path_glob
from aux_gps import smooth_xr
from aux_gps import Zscore_xr
import xarray as xr
files = path_glob(path, '*_index.nc')
ds_list = [xr.load_dataset(file) for file in files]
ds = xr.merge(ds_list)
if smooth is not None:
if isinstance(smooth, int):
ds = ds.rolling(time=smooth, center=True,
keep_attrs=True).mean(keep_attrs=True)
elif isinstance(smooth, str):
ds = smooth_xr(ds)
if zscore:
ds = Zscore_xr(ds)
return ds
def load_z_from_ERA5(savepath=climate_path):
import xarray as xr
from aux_gps import save_ncfile
ds = xr.load_dataset(savepath / 'ERA5_Z_500_hPa_for_NCPI_1979-2020.nc')
if 'expver' in ds.dims:
ds = ds.sel(expver=1)
z = ds['z']
z = z.rename({'latitude': 'lat', 'longitude': 'lon'})
z = z.sortby('lat')
save_ncfile(z, climate_path, 'ERA5_Z_500_hPa_NCP_1979-2020.nc')
return
def calculate_NCPI(savepath=climate_path):
import xarray as xr
from aux_gps import anomalize_xr
from aux_gps import save_ncfile
z = xr.load_dataarray(climate_path / 'ERA5_Z_500_hPa_NCP_1979-2020.nc')
# positive NCP pole:
pos = z.sel(lat=55, lon=slice(0, 10)).mean('lon')
neg = z.sel(lat=45, lon=slice(50, 60)).mean('lon')
ncp = pos - neg
ncp_anoms = anomalize_xr(ncp, 'MS')
ncpi = ncp_anoms.groupby('time.month') / ncp.groupby('time.month').std()
ncpi = ncpi.reset_coords(drop=True)
ncpi.name = 'NCPI'
ncpi.attrs['long_name'] = 'North sea Caspian Pattern Index'
save_ncfile(ncpi, savepath, 'ncp_index.nc')
return ncpi
def read_old_ncp(savepath=climate_path):
import pandas as pd
df = pd.read_csv(savepath / 'ncp.dat', delim_whitespace=True)
df.columns = ['year', 'month', 'ncpi']
df['dt'] = pd.to_datetime(df['year'].astype(
str) + '-' + df['month'].astype(str))
df.set_index('dt', inplace=True)
df = df.drop(['year', 'month'], axis=1)
df = df.sort_index()
df.index.name = 'time'
da = df.to_xarray()['ncpi']
da.name = 'Old_NCPI'
return da
def produce_DI_index(path=climate_path, savepath=climate_path):
from aux_gps import Zscore_xr
from aux_gps import save_ncfile
p3 = read_all_DIs(sample_rate='3H')
p3_mm = p3.resample(time='MS').mean()
p = Zscore_xr(p3_mm)
p.name = 'DI'
if savepath is not None:
filename = 'DI_index.nc'
save_ncfile(p, savepath, filename)
return p
def DI_and_PWV_lag_analysis(bin_di, path=work_yuval, station='tela',
hour_interval=48):
import xarray as xr
from aux_gps import xr_reindex_with_date_range
bin_di = xr_reindex_with_date_range(bin_di, freq='5min')
pw = xr.open_dataset(
path / 'GNSS_PW_thresh_50_for_diurnal_analysis.nc')[station]
print('loaded {} pwv station.'.format(station))
pw.load()
df = pw.to_dataframe()
df['bins'] = bin_di.to_dataframe()
cats = df['bins'].value_counts().index.values
pw_time_cat_list = []
# for di_cat in cats:
#
# return df
def bin_DIs(di, bins=[300, 500, 700, 900, 1030]):
import pandas as pd
import numpy as np
df = di.to_dataframe()
df = df.dropna()
labels = np.arange(1, len(bins))
df_bins = pd.cut(df[di.name], bins=bins, labels=labels)
da = df_bins.to_xarray()
return da
def read_all_DIs(path=climate_path, sample_rate='12H'):
from aux_gps import path_glob
import xarray as xr
if sample_rate == '12H':
files = path_glob(path, 'data_DIs_Bet_Dagan_*.mat')
elif sample_rate == '3H':
files = path_glob(path, 'data_DIs_Bet_Dagan_hr_*.mat')
da_list = [read_DIs_matfile(x, sample_rate=sample_rate) for x in files]
da = xr.concat(da_list, 'time')
da = da.sortby('time')
return da
def read_DIs_matfile(file, sample_rate='12H'):
from scipy.io import loadmat
import datetime
import pandas as pd
from aux_gps import xr_reindex_with_date_range
print('sample rate is {}'.format(sample_rate))
# file = path / 'data_DIs_Bet_Dagan_2015.mat'
# name = file.as_posix().split('/')[-1].split('.')[0]
mat = loadmat(file)
real_name = [x for x in mat.keys() if '__' not in x and 'None' not in x][0]
arr = mat[real_name]
startdate = datetime.datetime.strptime("0001-01-01", "%Y-%m-%d")
dts = [pd.to_datetime(startdate + datetime.timedelta(arr[x, 1])) -
pd.Timedelta(366, unit='D') for x in range(arr[:, 1].shape[0])]
vals = arr[:, 0]
df = pd.DataFrame(vals, index=dts)