-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataloader_global.py
More file actions
executable file
·1079 lines (965 loc) · 48.8 KB
/
Copy pathdataloader_global.py
File metadata and controls
executable file
·1079 lines (965 loc) · 48.8 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
#author: Alex Sun
#date: 03262022
#purpose: load 5d data
#date: 04042022
# CSR5d has unit in cm for global
#date: 04102022, add GPM and ERA5 water vapor fluxes
#date: 07152022, revise for new effort
#date: 08042022, add a new datagenerator class
#date: 09020222, revisit and add documentation
# check code quality, check accuracy
#date: 06192023, switch to version 1
###################################################################################################
import abc
from typing import Tuple
import numpy as np
from sklearn.utils import shuffle
import xarray as xr
import matplotlib.pyplot as plt
import os, glob,sys
import pandas as pd
import torch
from torch.utils.data import TensorDataset,DataLoader
import datetime
from dateutil.relativedelta import relativedelta
import random
import geopandas as gpd
import rioxarray
#custom package
from csr5dloader import getTWSDataArrays, filterData
from climate5dloader import getPrecipData, getWaterVaporData,getMERRA2SoilMoistureData,getGLEAMData,getAirTempData,getSSTData,getSWEData
from myutils import scaleDA,reScale,toTensor,getGraceRoot,getGridCellArea
def detrendDA(da):
"""detrend da
"""
#convert dates to time elapsed
startDate = '2002-04-01'
csr5d_days = (pd.to_datetime(da['time'].values) - datetime.datetime.strptime(startDate, '%Y-%m-%d')).days
#save old dates
oldtimes = da.coords['time']
da.coords['time']=csr5d_days
da, datrend = filterData(da, removeSeason=False)
da.coords['time'] = oldtimes
return da
def load5ddatasets(region:Tuple, coarsen:bool =True, mask_ocean=True, reLoad=False,
removeSeason=False, csr5d_version=1,
startYear=2002,endYear=2020):
"""Load CSR5d data
Params
------
coarsen, true to coarsen the grid (currently default to 1x1)
crs5d_version, version of csr5d data
"""
daCSR5d, mask = getTWSDataArrays(region,
reLoad=reLoad,
maskOcean=mask_ocean,
removeSeason=removeSeason,
csr5d_version=csr5d_version) #this has already been detrended in csr5dloader.py
daCSR5d = daCSR5d.sel(time=slice(f'{startYear}/01/01', f'{endYear}/12/31'))
if coarsen:
print ('coarsening to 1 degree from 0.25 degree !!!!')
#landmask = mask.coarsen(lat=4,lon=4,boundary='exact').mean()
landmask = xr.open_dataset(os.path.join(getGraceRoot(), 'data/mylandmask.nc'))['LSM'].squeeze()
daCSR5d = daCSR5d.coarsen(lat=4,lon=4,boundary='exact').mean()
else:
landmask = xr.open_dataset(os.path.join(getGraceRoot(), 'data/mylandmask025deg.nc'))['LSM'].squeeze()
#04042022, for global using all cells
if mask_ocean:
mask = landmask
else:
#use all cells
arr = np.zeros(landmask.values.shape)+1
mask = xr.DataArray(arr, name='mask', dims= landmask.dims, coords=landmask.coords )
print (daCSR5d.shape, mask.shape)
return daCSR5d,mask,landmask
def doRemoveSWE(daCSR, region, reLoad=False, startYear=2002, endYear=2020):
daSWE = loadClimatedatasets(region, vartype='swe',
daCSR5d= daCSR,
aggregate5d=True,
reLoad=reLoad,
precipSource='era5',
startYear= startYear,
endYear= endYear)
#remove mean to form anomalies
daSWE_mean = daSWE.sel(time=slice("2004/01/01", "2009/12/31")).mean(dim="time", skipna=True)
daSWE = daSWE-daSWE_mean
#Note: need to replace nan in swe, otherwise, detrend would not work
daSWE = daSWE.fillna(0)
daCSR.values = daCSR.values - daSWE.values
return daCSR
def loadTWSDataArray_SWE(region, mask_ocean, reLoad=False, startYear=2002, endYear=2019,coarsen=False,
removeSeason=False, csr5d_version=1):
from csr5dloader import loadTWS_raw
if reLoad:
#time of daCSR5d0 is in days elapsed
daCSR5d0, oldtimes = loadTWS_raw(region, maskOcean=mask_ocean)
daCSR5d0['time'] = oldtimes
if coarsen:
print ('coarsening to 1 degree from 0.25 degree !!!!')
landmask = xr.open_dataset(os.path.join(getGraceRoot(), 'data/mylandmask.nc'))['LSM'].squeeze()
daCSR5d = daCSR5d0.coarsen(lat=4,lon=4,boundary='exact').mean()
else:
landmask = xr.open_dataset(os.path.join(getGraceRoot(), 'data/mylandmask025deg.nc'))['LSM'].squeeze()
daCSR5d = daCSR5d0
#04042022, for global using all cells
if mask_ocean:
mask = landmask
else:
#use all cells
arr = np.zeros(landmask.values.shape)+1
mask = xr.DataArray(arr, name='mask', dims= landmask.dims, coords=landmask.coords )
#truncate the dataarray
daCSR5d = daCSR5d.sel(time=slice(f'{startYear}/01/01', f'{endYear}/12/31'))
#!!!! turn on reLoad to True to reload SWE
daCSR5d = doRemoveSWE(daCSR5d, region, reLoad=True, startYear=startYear, endYear=endYear)
oldtimes = daCSR5d['time'].values #save the dates
#convert to days elapsed
csrdays = (pd.to_datetime(oldtimes) - datetime.datetime.strptime('2002-04-01', '%Y-%m-%d')).days
#remove trend
#change back to days elapsed
daCSR5d['time'] = csrdays
#now do detrend
daInterannual,_ = filterData(daCSR5d, oldtimes, removeSeason=removeSeason)
daInterannual['time'] = oldtimes
bigarr = daInterannual.values
if mask_ocean:
bigarr = np.einsum('ijk,jk->ijk', bigarr, mask)
da = xr.DataArray(bigarr,name="lwe_thickness",coords=daCSR5d.coords,dims=daCSR5d.dims)
da.coords['time'] = oldtimes
#upsampling
da = da.interp_like(daCSR5d0, method='nearest')
#cut time
da = da.sel(time=slice(f'{startYear}/01/01', f'{endYear}/12/31'))
print ("final tws-swe shape", da.shape)
#land only
if mask_ocean:
if removeSeason:
da.to_netcdf(os.path.join(getGraceRoot(), f'data/globalcsr5d_notrend_deseason_swe_v{csr5d_version}.nc'))
else:
da.to_netcdf(os.path.join(getGraceRoot(), f'data/globalcsr5d_notrend_swe_v{csr5d_version}.nc'))
else:
#land only
if mask_ocean:
if removeSeason:
da = xr.open_dataset(os.path.join(getGraceRoot(), f'data/globalcsr5d_notrend_deseason_swe_v{csr5d_version}.nc'))['lwe_thickness']
else:
da = xr.open_dataset(os.path.join(getGraceRoot(), f'data/globalcsr5d_notrend_swe_v{csr5d_version}.nc'))['lwe_thickness']
return da
def getCSR5dLikeArray(da, daCSR5d, **kwargs):
""" Make arrays that have the same length as the CSR5d
"""
#get CSR5d dates
timeaxis = daCSR5d['time'].values
kwargs.setdefault("method", "rx5d")
kwargs.setdefault("aggmethod", 'sum')
kwargs.setdefault("name", 'variable')
method = kwargs['method']
if method=='rx5d':
#maximum consequtive 5-day
#get the 5-day total amount for precip, centered at each CSR5d date
if kwargs['aggmethod'] == 'sum':
daR5d = da.rolling(time=5, center=True,min_periods=1).sum()
#get the rolling mean on daily data (this is suitable for state types), centered at each CSR5d date
elif kwargs['aggmethod'] == 'average':
daR5d = da.rolling(time=5, center=True,min_periods=1).mean()
#do not do anything
elif kwargs['aggmethod'] == 'max':
daR5d = da
else:
raise NotImplementedError()
halfwin = 2 #[day]
#form CSR5d like time series
bigarr = []
for aday in timeaxis:
aday = pd.to_datetime(aday,format='%Y-%m-%d').date()
#for a 5-day window centered at aday
offsetDateStart = datetime.date.strftime(aday - relativedelta(days=halfwin),'%Y-%m-%d')
offsetDateEnd = datetime.date.strftime(aday + relativedelta(days=halfwin), '%Y-%m-%d')
tmpda = daR5d.sel(time = slice(offsetDateStart,offsetDateEnd))
if not tmpda.time.size==0:
#take the max of the 5-day statistic (for mean, sum, and max)
tmpda = tmpda.max(dim='time',skipna=True)
bigarr.append(tmpda.values)
else:
bigarr.append(np.zeros((daR5d.shape[1:3]))+np.NaN)
bigarr = np.stack(bigarr,axis=0)
daNew = xr.DataArray(bigarr, dims=daCSR5d.dims, coords=daCSR5d.coords, name=kwargs['name'])
else:
raise Exception("method not implemented")
return daNew
def loadClimatedatasets(region:Tuple, vartype:str, daCSR5d, aggregate5d=False, reLoad:bool=True,
precipSource='ERA5',saving=True,startYear=2002, endYear=2019, fake5d=False):
"""
#get precip data and aggregate according to CSR5d time intervals
#!!! don't use the simple resample, use getCSR5dLikeArray() instead
asun06192023: add extra parameters startYear and endYear
Params
------
reLoad: if reLoad is True, re-generate the csr5d like data arrays; else, load from disk
"""
if vartype == 'precip':
assert (precipSource in ['GPM', 'ERA5', 'REGEN','GPCP'])
dirmap = {'GPM':'gpm', 'ERA5': 'era5', 'REGEN': 'regen', 'GPCP':'gpcp'}
if reLoad:
#regenerate 5-day data, for precipitation we want the accumulative 5 day value
daPrecip = getPrecipData(region, source=precipSource, aggregateTo5d=False,
startYear=startYear, endYear=endYear)
if aggregate5d:
kwargs = {"method": "rx5d", "aggmethod": 'sum', 'name':'tp'}
daPrecip = getCSR5dLikeArray(daPrecip, daCSR5d, **kwargs)
if saving:
if not fake5d:
daPrecip.to_netcdf(os.path.join(getGraceRoot(), f'data/{dirmap[precipSource]}/{dirmap[precipSource]}_P5d.nc'))
else:
daPrecip.to_netcdf(os.path.join(getGraceRoot(), f'data/{dirmap[precipSource]}/{dirmap[precipSource]}_P5d_fake5d.nc'))
else:
daPrecip['time'] =daPrecip.indexes['time'].to_datetimeindex()
if saving:
daPrecip.to_netcdf(os.path.join(getGraceRoot(), f'data/{dirmap[precipSource]}/{dirmap[precipSource]}_P1d.nc'))
else:
if aggregate5d:
if not fake5d:
daPrecip = xr.open_dataset(os.path.join(getGraceRoot(), f'data/{dirmap[precipSource]}/{dirmap[precipSource]}_P5d.nc'))
else:
daPrecip = xr.open_dataset(os.path.join(getGraceRoot(), f'data/{dirmap[precipSource]}/{dirmap[precipSource]}_P5d_fake5d.nc'))
else:
daPrecip = xr.open_dataset(os.path.join(getGraceRoot(), f'data/{dirmap[precipSource]}/{dirmap[precipSource]}_P1d.nc'))
daPrecip = daPrecip.rename({'precipitationCal':'tp'})
#need to convert to dataarray
daPrecip = daPrecip['tp']
return daPrecip
elif vartype == 'airtemp':
if reLoad:
daT = getAirTempData(region, aggregateTo5d= False)
if aggregate5d:
#for air temperature we want 5-day average values
kwargs = {"method": "rx5d", "aggmethod": 'average', 'name': 't2m'}
daT = getCSR5dLikeArray(daT, daCSR5d, **kwargs)
if saving:
daT.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/t2m_5d.nc'))
else:
if saving:
daT.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/t2m_1d.nc'))
else:
if aggregate5d:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/t2m_5d.nc'))
else:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/t2m_1d.nc'))
daT = ds['t2m']
return daT
elif vartype == 'sst':
if reLoad:
daSST = getSSTData(region, aggregateTo5d= False)
if aggregate5d:
#for air temperature we want 5-day average values
kwargs = {"method": "rx5d", "aggmethod": 'average', 'name': 't2m'}
daSST = getCSR5dLikeArray(daSST, daCSR5d, **kwargs)
if saving:
daSST.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/sst_5d.nc'))
else:
if saving:
daSST.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/sst_1d.nc'))
else:
if aggregate5d:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/sst_5d.nc'))
else:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/sst_1d.nc'))
daSST = ds['sst']
return daSST
elif vartype == 'swe':
if reLoad:
daSWE = getSWEData(region, source='era5', aggregateTo5d= False, startYear=2002, endYear=2020)
if aggregate5d:
#for air temperature we want 5-day average values
kwargs = {"method": "rx5d", "aggmethod": 'average', 'name': 'sd'}
daSWE = getCSR5dLikeArray(daSWE, daCSR5d, **kwargs)
if saving:
if not fake5d:
daSWE.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/swe_5d.nc'))
else:
daSWE.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/swe_fake5d.nc'))
else:
if saving:
if not fake5d:
daSWE.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/swe_1d.nc'))
else:
daSWE.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/swe_fake1d.nc'))
else:
if aggregate5d:
if not fake5d:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/swe_5d.nc'))
else:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/swe_fake5d.nc'))
else:
if not fake5d:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/swe_1d.nc'))
else:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/swe_fake1d.nc'))
daSWE = ds['sd']
return daSWE
elif vartype == 'watervapor':
if reLoad:
#for water vapor, we want 5-day average values
kwargs = {"method": "rx5d", "aggmethod": 'average'}
daU, daV = getWaterVaporData(region)
#calculate magnitudes on daily data
mag = np.sqrt(daU.values*daU.values + daV.values*daV.values)
daM = xr.DataArray(mag, coords=daU.coords, dims=daU.dims, name='mag')
if aggregate5d:
kwargs['name'] = 'U'
daU = getCSR5dLikeArray(daU, daCSR5d, **kwargs)
kwargs['name'] = 'V'
daV = getCSR5dLikeArray(daV, daCSR5d, **kwargs)
kwargs['name'] = 'mag'
daM = getCSR5dLikeArray(daM, daCSR5d, **kwargs)
daU.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_u5d.nc'))
daV.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_v5d.nc'))
daM.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_m5d.nc'))
else:
daM.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_m1d.nc'))
daU.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_u1d.nc'))
daV.to_netcdf(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_v1d.nc'))
else:
if aggregate5d:
daU = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_u5d.nc'))
daV = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_v5d.nc'))
daM = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_m5d.nc'))
else:
daU = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_u1d.nc'))
daV = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_v1d.nc'))
daM = xr.open_dataset(os.path.join(getGraceRoot(), 'data/era5/watervapor/wv_m1d.nc'))
daU = daU.rename({'p88.162':'U'})
daV = daV.rename({'p89.162':'V'})
#convert to data array
daU = daU['U']; daV=daV['V']; daM = daM['mag']
return daU, daV, daM
elif vartype == 'sm':
if reLoad:
daSM = getMERRA2SoilMoistureData(region) #the unit is [m3/m3]
if aggregate5d:
#for soil moisture we want 5-day average values
kwargs = {"method": "rx5d", "aggmethod": 'average'}
kwargs['name'] = 'RZMC'
daSM = getCSR5dLikeArray(daSM, daCSR5d, **kwargs)
daSM.to_netcdf(os.path.join(getGraceRoot(), 'data/merra2/merr2sm_5d.nc'))
else:
daSM.to_netcdf(os.path.join(getGraceRoot(), 'data/merra2/merr2sm_1d.nc'))
else:
if aggregate5d:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/merra2/merr2sm_5d.nc'))
else:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/merra2/merr2sm_1d.nc'))
daSM = ds['RZMC']
return daSM
elif vartype == 'gleam':
if reLoad:
daGleam = getGLEAMData(region) #the unit is [m3/m3]
if aggregate5d:
#for ET we want 5-day accumulative values
kwargs = {"method": "rx5d", "aggmethod": 'average'}
kwargs['name'] = 'E'
daGleam = getCSR5dLikeArray(daGleam, daCSR5d, **kwargs)
daGleam.to_netcdf(os.path.join(getGraceRoot(), 'data/gleam_5d.nc'))
else:
daGleam.to_netcdf(os.path.join(getGraceRoot(), 'data/gleam_1d.nc'))
else:
if aggregate5d:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/gleam_5d.nc'))
else:
ds = xr.open_dataset(os.path.join(getGraceRoot(), 'data/gleam_1d.nc'))
daGleam = ds.E
return daGleam
def selectConsecData(da, lookbackLen=4, lookforwardLen=1):
#find data with consequtive dates
dT = 5 #days for csr5d
prevDate = None
goodInd = []
allDates = da.coords['time'].values
for i in range(lookbackLen, len(allDates)-lookbackLen):
currDate = pd.to_datetime(allDates[i],format='%Y-%m-%d').date()
prevDate = pd.to_datetime(allDates[i-lookbackLen],format='%Y-%m-%d').date()
futuDate = pd.to_datetime(allDates[i+lookforwardLen],format='%Y-%m-%d').date()
deltaBT = (currDate - prevDate).days
deltaFT = (futuDate - currDate).days
if deltaBT == lookbackLen*dT and deltaFT == lookforwardLen*dT:
goodInd.append(i)
print ('number of good dates', len(goodInd))
return goodInd
class TWSDataNorm():
def __init__(self, region, seq_len=4, target_seq_len=1,
rescale_method='norm', trainvalRatio=(0.7,0.15), mask_ocean=False):
"""
seq_len, lookback period
target_seq_len, forecast period
mask_ocean, True to remove ocean area
rescale_method, method for normalization
trainvalRatio, ratio between train and validation
"""
self.seq_len = seq_len
self.target_seq_len = target_seq_len
#load TWS data
daCSR5d,mask,landmask = load5ddatasets(region, coarsen=True, mask_ocean=mask_ocean, reLoad=False, removeSeason=False)
self.weights = getGridCellArea(daCSR5d,weighttype='latbased')
#Find indices satisfy the consec requirement
goodInd = selectConsecData(daCSR5d, lookbackLen=seq_len, lookforwardLen=target_seq_len)
#Split the dataset using good indices
nData = len(goodInd)
nTrain = int(trainvalRatio[0]*nData)
nVal = int(trainvalRatio[1]*nData)
trainInd = goodInd[:nTrain]
valInd = goodInd[nTrain:nTrain+nVal]
testInd = goodInd[nTrain+nVal:]
#these dataarrays include all data between start and end date so we can prepare the datasets appropriately
daCSR5dTrain = daCSR5d.isel(time=slice(0, trainInd[-1]+target_seq_len))
daCSR5dVal = daCSR5d.isel(time=slice(trainInd[-1]+target_seq_len, valInd[-1]+target_seq_len))
daCSR5dTest = daCSR5d.isel(time=slice(valInd[-1]+target_seq_len, len(daCSR5d.coords['time'])))
assert( (len(daCSR5dTrain['time']) + len(daCSR5dVal['time']) + len(daCSR5dTest['time'])) == len(daCSR5d['time']))
self.mask = mask.values #convert da to numpy
#Do normalization
kwargs={}
if rescale_method == 'deseason':
trainMat, monthlyMean = scaleDA(daCSR5dTrain, mask, rescale_method, op='train', **kwargs)
kwargs['monthlymean'] = monthlyMean
valMat, _ = scaleDA(daCSR5dVal, mask, rescale_method, op='test', **kwargs)
testMat, _ = scaleDA(daCSR5dTest, mask, rescale_method, op='test', **kwargs)
elif rescale_method == 'minimax':
trainMat, arrmin,arrmax = scaleDA(daCSR5dTrain, mask, rescale_method, op='train', **kwargs)
kwargs['min'] = arrmin
kwargs['max'] = arrmax
valMat, _, _ = scaleDA(daCSR5dVal, mask, rescale_method, op='test', **kwargs)
testMat, _, _ = scaleDA(daCSR5dTest, mask, rescale_method, op='test', **kwargs)
elif rescale_method == 'norm':
#this will raise a runtime warning because all-zero value arrays
trainMat, mu, stdvar = scaleDA(daCSR5dTrain, mask, rescale_method, op='train', **kwargs)
kwargs['mu'] = mu
kwargs['stdvar'] = stdvar
valMat, _, _ = scaleDA(daCSR5dVal, mask, rescale_method, op='test', **kwargs)
testMat, _, _ = scaleDA(daCSR5dTest, mask, rescale_method, op='test', **kwargs)
elif rescale_method == 'global':
#do single global mean/std
trainMat, mu, stdvar = scaleDA(daCSR5dTrain, mask, rescale_method, op='train', **kwargs)
kwargs['mu'] = mu
kwargs['stdvar'] = stdvar
valMat, _, _ = scaleDA(daCSR5dVal, mask, rescale_method, op='test', **kwargs)
testMat, _, _ = scaleDA(daCSR5dTest, mask, rescale_method, op='test', **kwargs)
else:
raise ValueError("Invalid scaling method")
allMat = np.concatenate([trainMat,valMat,testMat], axis=0)
self.Xtrain, self.Ytrain = self.formData(trainInd, allMat)
self.Xval, self.Yval = self.formData(valInd, allMat)
self.Xtest,self.Ytest = self.formData(testInd, allMat)
self.goodInd = goodInd
self.trainvalRatio = trainvalRatio
self.split = {'train':trainInd, 'val':valInd, 'test':testInd, 'all':goodInd}
self.daCSR5d = daCSR5d
self.landmask = landmask
self.kwargs = kwargs
def formData(self, indarr, arr):
#assemble X,Y using valid indices
#this is set as a prediction problem, using previous tws to predict future tws
X = []; Y=[]
for item in indarr:
X.append(arr[item-self.seq_len:item])
Y.append(arr[item:item+self.target_seq_len])
return X, Y
def rescaleAndGenDataArray(self, arr, split='test',rescale_method='norm'):
"""This method inverse transform data in the good indices only
"""
rarr = reScale(arr, self.mask, method=rescale_method, **self.kwargs)
#form DA
#first form dates according to target_seq_len
allInd = []
for ind in self.split[split]:
for i in range(1, self.target_seq_len+1):
allInd.append(ind+i)
print ('len all ind', len(allInd))
daTemp = self.daCSR5d.isel(time=allInd)
da = xr.DataArray(rarr, name='lwe_thickness', dims= daTemp.dims, coords=daTemp.coords )
return da
class TWSDataSet(torch.utils.data.Dataset):
def __init__(self, X, Y, mask=None, seq_len=4, target_seq_len=1):
assert(len(X) == len(Y))
self.X = X
self.Y = Y
self.mask= mask
self.seq_len = seq_len
self.target_seq_len = target_seq_len
def __getitem__(self, index):
input = toTensor(self.X[index])
target= toTensor(self.Y[index])
mask = toTensor(self.mask)
return input,target,mask
def __len__(self):
return len(self.X)
class ClimateDataNorm():
def __init__(self, region, seq_len, target_seq_len, dA, goodInd, trainvalRatio, mask,
rescale_method='norm'):
"""
seq_len, lookback period
target_seeq_len, forecast period
goodInd and trainvalRatio: these need to be passed from twsnorm!
mask, this needs to be passed down from twsnorm
"""
self.seq_len = seq_len
self.target_seq_len = target_seq_len
self.mask = mask
#Split the dataset using good indices
nData = len(goodInd)
nTrain = int(trainvalRatio[0]*nData)
nVal = int(trainvalRatio[1]*nData)
trainInd = goodInd[:nTrain]
valInd = goodInd[nTrain:nTrain+nVal]
testInd = goodInd[nTrain+nVal:]
#these dataarrays include all data between start and end date so we can prepare the datasets appropriately
daTrain = dA.isel(time=slice(0, trainInd[-1]+target_seq_len))
daVal = dA.isel(time=slice(trainInd[-1]+target_seq_len, valInd[-1]+target_seq_len))
daTest = dA.isel(time=slice(valInd[-1]+target_seq_len, len(dA.coords['time'])))
assert( (len(daTrain['time']) + len(daVal['time']) + len(daTest['time'])) == len(dA['time']))
#Do normalization
kwargs={}
if rescale_method == 'minimax':
trainMat, arrmin,arrmax = scaleDA(daTrain, mask, rescale_method, op='train', **kwargs)
kwargs['min'] = arrmin
kwargs['max'] = arrmax
valMat, _, _ = scaleDA(daVal, mask, rescale_method, op='test', **kwargs)
testMat, _, _ = scaleDA(daTest, mask, rescale_method, op='test', **kwargs)
elif rescale_method == 'norm':
#this will raise a runtime warning because all-zero value arrays
trainMat, mu, stdvar = scaleDA(daTrain, mask, rescale_method, op='train', **kwargs)
kwargs['mu'] = mu
kwargs['stdvar'] = stdvar
valMat, _, _ = scaleDA(daVal, mask, rescale_method, op='test', **kwargs)
testMat, _, _ = scaleDA(daTest, mask, rescale_method, op='test', **kwargs)
allMat = np.concatenate([trainMat,valMat,testMat], axis=0)
#We only need predictors
self.Xtrain = self.formData(trainInd, allMat)
self.Xval = self.formData(valInd, allMat)
self.Xtest = self.formData(testInd, allMat)
self.goodInd = goodInd
self.split = {'train':trainInd, 'val':valInd, 'test':testInd, 'all':goodInd}
self.kwargs = kwargs
def formData(self, indarr, arr):
#assemble X using valid indices
X = [arr[item-self.seq_len:item] for item in indarr]
return X
class ClimateDataSet():
"""class for combining TWS and climate forcings
"""
def __init__(self, X, Y, X_clim, mask=None, seq_len=4, target_seq_len=1):
assert(len(X) == len(Y))
self.X = X
self.Y = Y
self.X_clim = X_clim
nClimateVar = len(X_clim)
self.mask= mask
if not mask is None:
for i in range(len(X)):
#dimensions are X & X_clim:[seq_len, lat, lon], Y:[target_seq_len, lat, lon]
self.X[i] = np.einsum('kij,ij->kij', self.X[i], self.mask)
self.Y[i] = np.einsum('kij,ij->kij', self.Y[i], self.mask)
for ivar in range(nClimateVar):
tempvar = np.einsum('kij,ij->kij', self.X_clim[ivar][i], self.mask)
#remove the nan values
tempvar[np.isnan(tempvar)]=0.0
self.X_clim[ivar][i] = tempvar
#do debugging
"""
fig,ax = plt.subplots(3,1)
ax[0].imshow(X[i][0,:,:])
ax[1].imshow(X_clim[0][i][0,:,:])
ax[2].imshow(X_clim[1][i][0,:,:])
plt.savefig('testinput.png')
plt.close()
print (np.min(X[i]), np.min(self.X_clim[0][i]), np.min(self.X_clim[1][i]))
"""
self.seq_len = seq_len
self.target_seq_len = target_seq_len
def __getitem__(self, index):
input = toTensor(self.X[index])
target= toTensor(self.Y[index])
mask = toTensor(self.mask)
P = toTensor(self.X_clim[0][index])
ET = toTensor(self.X_clim[1][index])
return input,target,P,ET,mask
def __len__(self):
return len(self.X)
class ARDataSet(torch.utils.data.Dataset):
def __init__(self, X, Y, mask=None, seq_len=4, target_seq_len=1):
assert(len(X) == len(Y))
self.X = X
self.Y = Y
self.mask= mask
self.seq_len = seq_len
self.target_seq_len = target_seq_len
def __getitem__(self, index):
input = toTensor(self.X[index])
target= toTensor(self.Y[index])
mask = toTensor(self.mask)
return input,target,mask
def __len__(self):
return len(self.X)
class DataGenerator(metaclass=abc.ABCMeta):
"""A general class for preparing data pairs"""
def __init__(self, region, varlist, context_len, target_len, mask_ocean=True):
"""
Params:
------
region, global
varlist, list of variables, 'TWS', 'P', 'T', 'cycle'
context_len,
target_len,
mask_ocean
"""
self.context_len = context_len
self.target_len = target_len
self.varlist = varlist
assert ('TWS' in varlist)
xds,mask,landmask = load5ddatasets(region, coarsen=True, mask_ocean=mask_ocean,
reLoad=False, removeSeason=False)
self.mask = mask.values
self.landmask = landmask
#===========loading data =============#
dataDict = {'TWS':xds}
for item in varlist:
if item == 'P':
#Precip
xds_Precip = loadClimatedatasets(region, vartype='precip', daCSR5d= xds, aggregate5d=True, reLoad=False, precipSource='ERA5')
print ('Precip', xds_Precip.shape)
dataDict['P'] = xds_Precip
elif item == 'T':
#air temp
xds_T = loadClimatedatasets(region, vartype='airtemp', daCSR5d= xds, aggregate5d=True,reLoad=False)
print ('airtemp', xds_T.shape)
dataDict['T'] = xds_T
elif item == 'SST':
# SST
"""
xds_SST = loadClimatedatasets(region, vartype='sst', daCSR5d= xds, aggregate5d=True,reLoad=False)
print ('SST', xds_SST.shape)
dataDict['SST'] = xds_SST
"""
pass
elif item == 'cycle':
# monthly sin/cos cycles
data_months = pd.to_datetime(xds['time'].values).month.values
#change to (nT, H,W) numpy arrays
monthsin = np.sin((data_months-1)*(2.*np.pi/12))[:, np.newaxis,np.newaxis]
monthcos = np.cos((data_months-1)*(2.*np.pi/12))[:, np.newaxis,np.newaxis]
monthsin = np.tile(monthsin, (180,360) )
monthcos = np.tile(monthcos, (180,360) )
dataDict['SIN'] = xr.DataArray(monthsin, coords=xds.coords,dims=xds.dims)
dataDict['COS'] = xr.DataArray(monthcos, coords=xds.coords,dims=xds.dims)
print ('sin cycle', monthsin.shape, 'cos cycle', monthcos.shape)
self.dataDict = dataDict
def scaleData(self, da, varname, scalerType, scalerInfo, op, mask):
"""Scale data array
Params
------
da,
varname,
scalerType,
scalerInfo,
op, 'train' or 'test'
mask, this can be land mask or use mask = ones for the whole dataset
"""
if op=='train':
if scalerType=='norm':
#cellwise normalization
arr = da.values
#taking temporal mean and std
mu = np.mean(arr, axis=0)
sigma = np.std(arr, axis=0)
print ('99 percentile of std', np.percentile(sigma.flatten(), 99))
scalerInfo[varname] = {'type': scalerType, 'mu': mu, 'sigma': sigma}
for i in range(arr.shape[0]):
arr[i][mask==1] = (arr[i][mask==1]-mu[mask==1])/sigma[mask==1]
elif scalerType == 'globalnorm':
#normalize with global mean/std
arr = da.values
#taking temporal mean and std
mu = np.nanmean(arr)
sigma = np.nanstd(arr)
scalerInfo[varname] = {'type': scalerType, 'mu': mu, 'sigma': sigma}
arr = (arr - mu) / sigma
elif scalerType=='lognorm':
arr = np.log(da.values+1e-3)-np.log(1e-3)
#taking temporal mean and std
mu = np.nanmean(arr)
sigma = np.nanmean(arr)
scalerInfo[varname]= {'type': scalerType, 'mu':mu, 'sigma':sigma}
arr = (arr - mu) / sigma
elif scalerType == 'minmax':
arr = da.values
arrmin = np.nanmin(arr,axis=0)
arrmax = np.nanmax(arr,axis=0)
scalerInfo[varname] = {'type': scalerType, 'min': arrmin, 'max': arrmax}
for i in range(arr.shape[0]):
arr[i][mask==1] = (arr[i][mask==1]-arrmin[mask==1])/(arrmax[mask==1]-arrmin[mask==1])
else:
raise ValueError("Invalid scalertype")
return arr, scalerInfo
else:
if scalerType=='norm':
#cellwise normalization
arr = da.values
#taking temporal mean and std
mu,sigma = scalerInfo[varname]['mu'], scalerInfo[varname]['sigma']
for i in range(arr.shape[0]):
arr[i][mask==1] = (arr[i][mask==1]-mu[mask==1])/sigma[mask==1]
elif scalerType=='lognorm':
arr = np.log(da.values+1e-3)-np.log(1e-3)
#taking temporal mean and std
mu, sigma = scalerInfo[varname]['mu'], scalerInfo[varname]['sigma']
arr = (arr - mu) / sigma
elif scalerType == 'globalnorm':
#normalize with global mean/std
arr = da.values
#taking temporal mean and std
mu, sigma = scalerInfo[varname]['mu'], scalerInfo[varname]['sigma']
arr = (arr - mu) / sigma
elif scalerType == 'minmax':
arr = da.values
arrmax,arrmin = scalerInfo[varname]['max'],scalerInfo[varname]['min']
scalerInfo[varname] = {'type':scalerType, 'min':arrmin, 'max':arrmax}
for i in range(arr.shape[0]):
arr[i][mask==1] = (arr[i][mask==1]-arrmin[mask==1])/(arrmax[mask==1]-arrmin[mask==1])
return arr, scalerInfo
@abc.abstractmethod
def formData(self):
"""
"""
def scaleBack(self, arr, varName, split):
"""This method inverse transform data in the good indices only
"""
mask = self.mask
scalerType = self.scalerInfo[varName]['type']
if scalerType=='norm':
#cellwise normalization
#taking temporal mean and std
mu,sigma = self.scalerInfo['TWS']['mu'], self.scalerInfo['TWS']['sigma']
for i in range(arr.shape[0]):
arr[i][mask==1] = (arr[i][mask==1]*sigma[mask==1])+mu[mask==1]
elif scalerType == 'globalnorm':
#normalize with global mean/std
mu, sigma = self.scalerInfo['TWS']['mu'], self.scalerInfo['TWS']['sigma']
arr = arr * sigma + mu
elif scalerType == 'minmax':
arrmax,arrmin = self.scalerInfo['TWS']['max'], self.scalerInfo['TWS']['min']
for i in range(arr.shape[0]):
arr[i][mask==1] = arr[i][mask==1]*(arrmax[mask==1]-arrmin[mask==1])+arrmin[mask==1]
#form DA
#first form all dates according to target_seq_len
allInd = []
da = self.dataDict['TWS']
for ind in self.scalerInfo['split'][split]:
for i in range(1, self.target_len +1):
allInd.append(ind+i)
daTemp = da.isel(time=allInd)
da = xr.DataArray(arr, name='lwe_thickness', dims= daTemp.dims, coords=daTemp.coords )
return da
class ARGenerator(DataGenerator):
"""class for autoregression
"""
def __init__(self, region, varlist, context_len, target_len, batch_size):
DataGenerator.__init__(self, region, varlist, context_len, target_len)
self.batch_size = batch_size
def formData(self, randomTrainVal=True):
def __findGoodConSecDays(tt, lookbackLen=5, lookforwardLen=1):
#tt should be in days elapsed
#find data with consequtive dates in TWS
#note: this will get sequenceLen+1 gaps
dT = 5 #days for csr5d
prevDate = None
consecInd = []
for i in range(lookbackLen, len(tt)-lookforwardLen):
currDate = tt[i]
prevDate = tt[i-lookbackLen]
futuDate = tt[i+lookforwardLen]
deltaBT = (currDate - prevDate)
deltaFT = (futuDate - currDate)
if deltaBT == lookbackLen*dT and deltaFT == lookforwardLen*dT:
consecInd.append(i)
print ('number of good dates', len(consecInd))
return consecInd
startDate = '2002-04-01'
daTWS = self.dataDict['TWS']
#the time axes must be the same for all data
tt = (pd.to_datetime(daTWS['time'].values) - datetime.datetime.strptime(startDate, '%Y-%m-%d')).days
#select consec dates
consecInd = __findGoodConSecDays(tt, self.context_len, self.target_len)
#input shape
#[batch, seq, inputDim, H, W]
self.__inputDim = len(self.dataDict.keys())
H,W = daTWS.shape[1:] #daTWS in (time, lat, lon)
nData = len(consecInd)
nTrain = int(0.7*nData)
nVal = int(0.15*nData)
#get train ind
indTrain = consecInd[:nTrain]
indVal = consecInd[nTrain:nTrain+nVal]
indTest = consecInd[nTrain+nVal:]
#do normalization on inputs/outputs
scalerTypes={'TWS':'globalnorm', 'P': "lognorm", "T": "norm", "SIN":"minmax", "COS":"minmax"}
scalerInfo = {}
matDict = {}
for item in self.dataDict.keys():
da = self.dataDict[item]
daTrain = da.isel(time=slice(0, indTrain[-1]+self.target_len))
trainMat,scalerInfo = self.scaleData(daTrain, varname=item, scalerType= scalerTypes[item], op="train",
scalerInfo=scalerInfo, mask=self.mask)
daVal = da.isel(time=slice(indTrain[-1]+self.target_len, indVal[-1]+self.target_len))
valMat, _ = self.scaleData(daVal, varname=item, scalerType= scalerTypes[item], op="test",
scalerInfo=scalerInfo, mask=self.mask)
daTest = da.isel(time=slice(indVal[-1]+self.target_len, len(tt)))
testMat, _ = self.scaleData(daTest, varname=item, scalerType= scalerTypes[item], op="test",
scalerInfo=scalerInfo, mask=self.mask)
#make sure the combined datasets covers all data
assert( (len(daTrain['time']) + len(daVal['time']) + len(daTest['time'])) == len(daTWS['time']))
matDict[item] = np.concatenate([trainMat,valMat,testMat], axis=0)
if item == 'TWS':
scalerInfo['split']={'train':indTrain,'val':indTest,'test':indTest}
self.scalerInfo = scalerInfo
#form data in such a way that uses past TWS to predict future TWS
X = np.zeros((nData, self.context_len, self.__inputDim, H, W)) #predictors
Y = np.zeros((nData, self.target_len, 1, H, W)) #predictand
predictors = ['TWS', 'P', 'T', 'SIN', 'COS']
for ix, id in enumerate(consecInd):
for ik, key in enumerate(predictors):
X[ix,:, ik, :, :] = matDict[key][id-self.context_len:id]
Y[ix,:, 0, :, :] = matDict['TWS'][id:id+self.target_len]
Xtrain = toTensor(X[:nTrain,...])
Ytrain = toTensor(Y[:nTrain,...])
Xval = toTensor(X[nTrain:nTrain+nVal,...])
Yval = toTensor(Y[nTrain:nTrain+nVal,...])
Xtest = toTensor(X[nTrain+nVal:,...])
Ytest = toTensor(Y[nTrain+nVal:,...])
mask = toTensor(self.mask)
trainDataset = ARDataSet(Xtrain, Ytrain, mask=self.mask, seq_len=self.context_len,
target_seq_len= self.target_len)
valDataset = ARDataSet(Xval, Yval, mask=self.mask, seq_len=self.context_len,
target_seq_len= self.target_len)
testDataset = ARDataSet(Xtest, Ytest, mask=self.mask, seq_len=self.context_len,
target_seq_len= self.target_len)
trainLoader = DataLoader(trainDataset,
batch_size=self.batch_size,
shuffle = True,
num_workers=4,
pin_memory=True
)
valLoader = DataLoader(valDataset,
batch_size=self.batch_size,
shuffle = False,
num_workers=4,
pin_memory=True
)
testLoader = DataLoader(testDataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=4,
pin_memory=True
)
return trainLoader,valLoader, testLoader
class SNPGenerator(DataGenerator):
"""class for sequential NP
[P, T, SIN, COS] -> [TWS]
"""
def __init__(self, **kw_args):
DataGenerator.__init__(self, **kw_args)
def formData(self):
def __findGoodConSecDays(tt, sequenceLen=5):
#find data with consequtive dates in TWS
#note: this will get sequenceLen+1 gaps
#because I'm not using TWS, so there's no
#need to future data here
dT = 5 #days for csr5d
prevDate = None
consecInd = []
for i in range(sequenceLen, len(tt)-sequenceLen):
currDate = tt[i]
prevDate = tt[i-sequenceLen]
if (currDate - prevDate) == sequenceLen*dT:
consecInd.append(i)
print ('number of good dates', len(consecInd))
return consecInd
def __formDataLoader(loaderType):
task = {
'x_context': [],
'y_context': [],
'x_target': [],
'y_target': [],
}
#split for training/valication
if loaderType == 'train':
rng = range(0, nTrain, 2)
else:
rng = range(nTrain, nData, 2)
for i in rng:
x_context = X[i]
y_context = Y[i]
x_target = X[i+1]
y_target = Y[i+1]
task['x_context'].append(x_context)
task['y_context'].append(y_context)
task['x_target'].append(x_target)