-
Notifications
You must be signed in to change notification settings - Fork 0
/
eddy_prof_analysis.py
1259 lines (1117 loc) · 54.5 KB
/
eddy_prof_analysis.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
import xarray as xr
import numpy as np
from datetime import datetime
from datetime import timedelta
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cmocean
import pandas as pd
import matplotlib as mpl
from matplotlib import path
from scipy import interpolate
import warnings
from glob import glob
import os
import matplotlib.gridspec as gridspec
from geopy import distance
import geopy as gp
from matplotlib.animation import FuncAnimation
import matplotlib.collections as clt
import matplotlib.animation as animation
import gsw
from scipy.io import loadmat
import mat73
# Function
# ----------------------------------- Common -----------------------------------
def addParas(prof,eddy):
eddy_exist = len(list(eddy.dims.keys()))
month = np.array(pd.Series(prof.time.values).dt.month)
season = np.zeros_like(month)
sls = np.array([[9, 10,11],
[12,1, 2],
[3, 4, 5],
[6, 7, 8]])
for i in range(sls.shape[0]):
season[np.in1d(month,sls[i,:])] = i
if eddy_exist > 0:
longevity = np.ones_like(prof.n_prof.values).astype('float')
lat_birth = np.ones_like(prof.n_prof.values).astype('float')
lon_birth = np.ones_like(prof.n_prof.values).astype('float')
iage= np.ones_like(prof.n_prof.values).astype('float')
for i in range(len(prof.n_prof.values)):
ied = prof.eddyidx.values[i]
mask = eddy['eddyidx_obs'].values == ied
r,_ = np.where(mask)
longevity[i] = eddy.sel(n_eddy=r)['longevity']
lat_birth[i] = eddy.sel(n_eddy=r)['lat_birth']
lon_birth[i] = eddy.sel(n_eddy=r)['lon_birth']
iage[i] = eddy['age'].values[mask] / longevity[i]
prof = prof.assign_coords({'month':(['n_prof'],month),
'season':(['n_prof'],season),
'longevity':(['n_prof'],longevity),
'lat_birth':(['n_prof'],lat_birth),
'lon_birth':(['n_prof'],lon_birth),
'iage':(['n_prof'],iage)})
else:
prof = prof.assign_coords({'month':(['n_prof'],month),
'season':(['n_prof'],season)})
return prof
def combineEDDY(ds1,ds2):
track1 = ds1.track.values
track2 = ds2.track.values
ds2 = ds2.sel(n_eddy=~np.in1d( track2,track1 ))
l1 = ds1.eddyidx_obs.values.shape[1]
l2 = ds2.eddyidx_obs.values.shape[1]
dl = l1-l2
if dl < 0:
dl = abs(dl)
ds1 = ds1.pad(pad_width={'obs': (0,dl)},mode='constant',constant_values={'obs': (0,-999)})
elif dl > 0:
ds2 = ds2.pad(pad_width={'obs': (0,dl)},mode='constant',constant_values={'obs': (0,-999)})
DS = xr.concat([ds1,ds2],dim='n_eddy')
return DS
def ieddy_ds(ied,eddy):
eddyidx_obs = np.arange(0,len(eddy.amplitude.values))
eddy = eddy.assign_coords({'eddyidx_obs':(['obs'],eddyidx_obs)})
track = eddy.isel(obs=ied).track.values
keys = ['amplitude','time','effective_area','latitude','longitude']
eddy1 = eddy.sel(obs=eddy.track==track)[keys]
# coords
eddyidx_obs = eddy1['eddyidx_obs'].values
l = len(eddyidx_obs)
# data_vars, 2D
amplitude = eddy1['amplitude'].values * 0.0001
area = eddy1['effective_area'].values
time = days2dt64(eddy1['time'].values)
age = time - min(time)
lon = eddy1.longitude.values
lat = eddy1.latitude.values
# store time datetime64[ns] into int for padding
time = np.array(time).astype(int)
age = np.array(age.dt.days)
# data_vars, 1D
amplitude_t = np.round(max(amplitude),4)
area_t = max(area)
time_birth = min(time)
lat_birth = min(lat)
lon_birth = max(lon)
longevity = max(age)
ds = xr.Dataset(
data_vars = {
# 2D vars
'amplitude': ( ['n_eddy','obs'],amplitude.reshape(1,l) ),
'area': ( ['n_eddy','obs'],area.reshape(1,l) ),
'time': ( ['n_eddy','obs'],np.array(time).reshape(1,l) ),
'age': ( ['n_eddy','obs'],np.array(age).reshape(1,l) ),
'lon': ( ['n_eddy','obs'],lon.reshape(1,l) ),
'lat': ( ['n_eddy','obs'],lat.reshape(1,l) ),
# 1D vars
'amplitude_t':( ['n_eddy'],[amplitude_t] ),
'area_t': ( ['n_eddy'],[area_t] ),
'time_birth': ( ['n_eddy'],[time_birth] ),
'longevity': ( ['n_eddy'],[longevity] ),
'lat_birth': ( ['n_eddy'],[lat_birth] ),
'lon_birth': ( ['n_eddy'],[lon_birth] ),
'track': ( ['n_eddy'],[track] )},
coords = {'eddyidx_obs' : ( ['n_eddy','obs'],eddyidx_obs.reshape(1,l) )}
)
return ds
# def makeEddy
def inpaintnan1d(var_array,pres_old,res):
var_array = var_array.flatten()
pres_old = pres_old.flatten()
# interpolate pressure
pres0 = np.floor(np.nanmin(pres_old))
pres1 = np.ceil(np.nanmax(pres_old)+1)
pres_itp = np.arange(pres0,pres1+res/2,res)
# remove nan and infinite
mask_valid = np.isfinite(var_array) & np.isfinite(pres_old)
if mask_valid.sum() > 1:
variable_valid = var_array[mask_valid]
pres_old_valid = pres_old[mask_valid]
f = interpolate.interp1d(pres_old_valid,variable_valid,bounds_error=False,fill_value='extrapolate')
var_itp = f(pres_itp)
else:
var_itp = np.nan * np.ones_like(pres_itp)
return var_itp,pres_itp
def addSigma0(prof):
# load values
lon = prof['lon'].values
lat = prof['lat'].values
T = prof['temperature'].values
SP = prof['salinity'].values
p = prof['pressure'].values
# reshape
P = p.reshape(len(p),1)
LON = lon.reshape(1,len(lon))
LAT = lat.reshape(1,len(lat))
# tile
P = np.tile(P,(1,len(lon)))
LON = np.tile(LON,(len(p),1))
LAT = np.tile(LAT,(len(p),1))
# calculate
SA = gsw.conversions.SA_from_SP(SP,P,LON,LAT)
CT = gsw.conversions.CT_from_t(SA,T,P)
prof = prof.assign({'sigma0':(['pressure','n_prof'],gsw.density.sigma0(SA,CT))})
return prof
def findperipheral(ilon_cars,ilat_cars,ds_cars):
lons = ds_cars['lon'].values
lats = ds_cars['lat'].values
idx_lon = np.argwhere(np.isclose(lons,ilon_cars))[0][0] # index the value from an array
idx_lat = np.argwhere(np.isclose(lats,ilat_cars))[0][0] # index the value from an array
lons_sel = lons[idx_lon-1:idx_lon+2]
lats_sel = lats[idx_lat-1:idx_lat+2]
lons_mesh,lats_mesh = np.meshgrid(lons_sel,lats_sel)
return lons_mesh.flatten(order='C'),lats_mesh.flatten(order='C')
def iCARS(time,ilon,ilat,pressure,ds_cars):
# get the peripheral grid dots
ilon_1dot = ds_cars.sel({'lon':ilon,'lat':ilat},method='nearest')['lon'].values
ilat_1dot = ds_cars.sel({'lon':ilon,'lat':ilat},method='nearest')['lat'].values
ilon_9dots,ilat_9dots = findperipheral(ilon_1dot,ilat_1dot,ds_cars)
# get the climatology data at 1 dot or 9 dots
lons = ilon_9dots
lats = ilat_9dots
dat_mean = ds_cars.sel({'lon':lons,'lat':lats},method='nearest')['mean'].values
an_cos = ds_cars.sel({'lon':lons,'lat':lats},method='nearest')['an_cos'].values
an_sin = ds_cars.sel({'lon':lons,'lat':lats},method='nearest')['an_sin'].values
sa_cos = ds_cars.sel({'lon':lons,'lat':lats},method='nearest')['sa_cos'].values
sa_sin = ds_cars.sel({'lon':lons,'lat':lats},method='nearest')['sa_sin'].values
depth = ds_cars['depth'].values
# reshape for matrix multiplication
dat_mean = dat_mean.reshape(len(depth),len(lats),len(lons),1)
depth = depth
an_cos_ext = np.zeros((len(depth),len(lats),len(lons),1))
an_sin_ext = np.zeros((len(depth),len(lats),len(lons),1))
sa_cos_ext = np.zeros((len(depth),len(lats),len(lons),1))
sa_sin_ext = np.zeros((len(depth),len(lats),len(lons),1))
an_cos_ext[:len(an_cos),:,:,0] = an_cos
an_sin_ext[:len(an_sin),:,:,0] = an_sin
sa_cos_ext[:len(sa_cos),:,:,0] = sa_cos
sa_sin_ext[:len(sa_sin),:,:,0] = sa_sin
t0 = time.astype('datetime64[D]')-15
t1 = time.astype('datetime64[D]')+16
time_range = np.arange(t0,t1)
data_shape = dat_mean.shape[:3] + (len(time_range),) # add 1 time dimension to the data -> (depth,lat,lon,time)
time_range = time_range.reshape(1,1,1,data_shape[3])
t_range = 2*np.pi * (time_range - np.datetime64('2023-01-01'))/np.timedelta64(366,'D') # be aware that this only works for date that after 2023-01-16, and before 2023-12-15
# tile the data
mean_tile = np.tile(dat_mean,(1,1,1)+(data_shape[3],))
t_tile = np.tile(t_range,data_shape[:3]+(1,))
an_cos_tile = np.tile(an_cos_ext,(1,1,1)+(data_shape[3],))
an_sin_tile = np.tile(an_sin_ext,(1,1,1)+(data_shape[3],))
sa_cos_tile = np.tile(sa_cos_ext,(1,1,1)+(data_shape[3],))
sa_sin_tile = np.tile(sa_sin_ext,(1,1,1)+(data_shape[3],))
# calculation
climat_range = mean_tile + an_cos_tile*np.cos(t_tile) + an_sin_tile*np.sin(t_tile) + sa_cos_tile*np.cos(2*t_tile) + sa_sin_tile*np.sin(2*t_tile)
# make the negative value to zero if there was any
if climat_range.any() < 0:
print(f'on {time}, at (lon,lat) ({ilon},{ilat})')
climat_range = np.where(climat_range<0,0,climat_range)
# take average
iclimat_mean = np.nanmean(climat_range,axis=(1,2,3))
# interpolation
f = interpolate.interp1d(depth,iclimat_mean,bounds_error=False,fill_value='extrapolate')
iclimat_itp = f(pressure)
return iclimat_itp,lons,lats
def addCARS(prof,prof_type):
# lon_arr = np.zeros((9,len(prof.n_prof.values)))
# lat_arr = np.zeros((9,len(prof.n_prof.values)))
if prof_type == 'ctd':
var_list = ['temperature','salinity','oxygen']
elif prof_type == 'hydro':
var_list = ['nitrate','phosphate','silicate']
elif prof_type == 'argo':
var_list = ['temperature','salinity','oxygen','nitrate']
elif prof_type == 'v06' or prof_type == 'his':
var_list = ['temperature','salinity','oxygen','nitrate','phosphate','silicate']
else:
print('invalid inputs')
dir = '/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/CARS/'
for var in (var_list):
fn = (glob(f"{dir}{var}*"))
ds_cars = xr.open_dataset(fn[0])
for id in range(len(prof.n_prof.values)):
# get the ctd data at id
ilon = prof.lon[id].values
ilat = prof.lat[id].values
iprof = prof.n_prof[id].values
# return prof,lon_arr,lat_arr
# print(f'id={id}')
# print(f'{prof.x}')
time = prof.time[id].values
pressure = prof.pressure.values
iclimat_itp,lons,lats = iCARS(time,ilon,ilat,pressure,ds_cars)
# lon_arr[:,id] = lons
# lat_arr[:,id] = lats
if var == 'oxygen':
iclimat_itp = iclimat_itp * 44.661
da = xr.DataArray(data=iclimat_itp.reshape(len(pressure),1),
dims=['pressure','n_prof'],
coords={'n_prof':(['n_prof'],[iprof]),
'pressure':pressure})
ds = da.to_dataset(name=f'{var}_cars')
ds = ds.drop_duplicates(dim=...)
# print(ds)
prof = xr.merge([prof,ds])
# return prof,lon_arr,lat_arr
return prof
def calculateAnom(prof,prof_type):
var_list = list(prof.data_vars.keys())
var_ar = np.array(var_list)
if prof_type == 'ctd':
var_choice = ['temperature','salinity','oxygen']
elif prof_type == 'hydro':
var_choice = ['nitrate','phosphate','silicate']
elif prof_type == 'argo':
var_choice = ['temperature','salinity','oxygen','nitrate']
elif prof_type == 'v06' or prof_type == 'his':
var_choice = ['temperature','salinity','oxygen','nitrate','phosphate','silicate']
else:
print('invalid inputs')
var_cross = var_ar[np.in1d(var_ar,var_choice)]
for var in var_cross:
prof[var+'_anom'] = prof[var] - prof[var+'_cars']
return prof
def makeProf(prof_type,eddy_type,test=False):
if eddy_type == 'C' or eddy_type == 'A':
if prof_type == 'argo':
prof,eddy = makeArgoProf(eddy_type,test=test)
elif prof_type == 'his':
prof,eddy = makeHisProf(eddy_type,test=test)
elif prof_type == 'v06':
prof,eddy = makeV06Prof(eddy_type)
else:
print('invalid profile type')
elif eddy_type == 'O':
if prof_type == 'argo':
prof,eddy = makeArgoProf_OUT(eddy_type,test=test)
elif prof_type == 'his':
prof,eddy = makeHisProf_OUT(eddy_type,test=test)
elif prof_type == 'v06':
prof,eddy = makeV06Prof_OUT(eddy_type)
else:
print('invalid profile type')
return prof,eddy
# ----------------------------------- Argo -----------------------------------
def days2dt64(time):
days = time*1.1574074074074073e-05*timedelta(days=1)
dt = datetime(1950,1,1)+days
dt64 = pd.Series(dt,dtype='datetime64[ns]')
return dt64
def argoJULD2TS(argoJULD):
day0_ts = pd.Timestamp('1950-01-01T00:00:00')
day0_juld = pd.Timestamp.to_julian_date(day0_ts)
TS = pd.to_datetime(argoJULD+day0_juld,origin='julian',unit='D')
return TS
def cycidx2num():
dir = '/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/data/'
argodata_mat = mat73.loadmat(dir+'argo_data.mat')
argoineddy_mat = mat73.loadmat(dir+'argoineddy.mat')
argoineddy = argoineddy_mat['argoineddy']
cycidices = argoineddy['argoindex'][:,1].astype(int)
floatidices = argoineddy['argoindex'][:,0].astype(int)
cycidx = np.ones_like(floatidices).astype(int)
dir = '/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/aa_in2023_v06/Profiles/'
for icyc,cyc in enumerate(cycidices):
fl = floatidices[icyc]
fn = glob(dir+f'{fl}*.nc')
ds = xr.open_dataset(fn[0])
cycidx[icyc] = ds['CYCLE_NUMBER'][cyc-1] # the cyc was in MATLAB
argoineddy_new = np.concatenate([floatidices,cycidx],axis=1)
np.save('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/data/argoineddy_new.npy',argoineddy_new)
def changeName(prof):
argoName_selected = np.array(['TEMP_ADJUSTED','PSAL_ADJUSTED','DOXY_ADJUSTED','NITRATE_ADJUSTED','CHLA_ADJUSTED'])
argoName_ar = np.array(list(prof.data_vars.keys()))
argoName_cross = argoName_ar[np.in1d(argoName_ar,argoName_selected)]
newName = {}
newName['TEMP_ADJUSTED'] = 'temperature'
newName['PSAL_ADJUSTED'] = 'salinity'
newName['DOXY_ADJUSTED'] = 'oxygen'
newName['NITRATE_ADJUSTED'] = 'nitrate'
newName['CHLA_ADJUSTED'] = 'fluorescence'
newName['CHLA_FLUORESCENCE_ADJUSTED'] = 'fluorescence'
for old in argoName_cross:
new = newName[old]
prof[new] = prof[old]
prof = prof.drop(old)
return prof
def useADJUSTED(var_all,var_list):
var_adjusted = []
var_cross = var_all[np.in1d(var_all,var_list)]
for var in var_cross:
if var+'_ADJUSTED' in var_all:
var_adjusted.append(var+'_ADJUSTED')
else:
var_adjusted.append(var)
print(f'no adjusted for {var}')
return var_adjusted
def makeArgoProf(eddy_type,changename=True,test=False):
var_list = ['TEMP','PSAL','DOXY','CHLA','NITRATE','CHLA_FLUORESCENCE']
dir = '/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/data/'
argodata_mat = mat73.loadmat(dir+'argo_data.mat')
argoineddy_mat = mat73.loadmat(dir+'argoineddy.mat')
argoineddy = argoineddy_mat['argoineddy']
argodata = argodata_mat['Data']
# create masks
eddytype_split = np.array(list(argoineddy['eddytype'][:4121]))
mask_edy = (eddytype_split==eddy_type)
# extract index
eddyidx = argoineddy['eddyindex'][mask_edy].astype(int)
floatidx = argoineddy['argoindex'][mask_edy,0].astype(int)
cycidx = np.load('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/data/argoineddy_new.npy')[mask_edy,1]
if test:
l = 5
else:
l = len(eddyidx)
# import eddy
if eddy_type == 'C':
eddy = xr.open_dataset('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/cyclonic_eddy_traj_eac_days_more30_meta3p2_dt_2sat.nc', decode_cf=False)
elif eddy_type == 'A':
eddy = xr.open_dataset('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/anticyclonic_eddy_traj_eac_days_more30_meta3p2_dt_2sat.nc', decode_cf=False)
else:
print('invalid input')
eddy['effective_radius'] = eddy['effective_radius'].astype(float)
for i in range(l):
ied = eddyidx[i]-1
ifl = floatidx[i]
icyc = cycidx[i]
# make profile dataset
ifloat = argodata[f'F{ifl}']
var_all = np.array(list(ifloat.keys()))
var_adjusted = useADJUSTED(var_all,var_list)
mask_cyc = (ifloat['CYCLE_NUMBER']==icyc)
lat = np.mean(ifloat['LATITUDE'][mask_cyc])
lon = np.mean(ifloat['LONGITUDE'][mask_cyc])
coords = np.array([lat,lon])
juld = np.mean(ifloat['JULD'][mask_cyc])
time = argoJULD2TS(juld)
pres_name = useADJUSTED(var_all,['PRES'])[0]
pres_old = ifloat[pres_name][mask_cyc].flatten()
pres_valid = pres_old[np.isfinite(pres_old)]
lon_edd = eddy.isel(obs=ied).longitude.values
lat_edd = eddy.isel(obs=ied).latitude.values
lon_edd_eff = eddy.isel(obs=ied).longitude_max.values
lat_edd_eff = eddy.isel(obs=ied).latitude_max.values
rad = eddy.isel(obs=ied).effective_radius.values * 50
rad_deg = rad / 1000 * 0.009
lon_cont = eddy.isel(obs=ied).effective_contour_longitude.values.reshape(1,20)*0.01+180
lat_cont = eddy.isel(obs=ied).effective_contour_latitude.values.reshape(1,20)*0.01
time_edd = days2dt64(eddy.isel(obs=ied).time.values)[0]
# index the corresponding eddy and calculate x
coords_edd = np.array([lat_edd,lon_edd])
# coords_edd = np.array([lat_edd[i,0],lon_edd[i,0]])
dist = distance.distance(coords_edd,coords).m
x = dist/rad
# extract vars and interpolate when pressure level is larger than 1
if len(pres_valid) > 1:
ds = xr.Dataset({})
for ivar,var in enumerate(var_adjusted):
res = 1
var_array = ifloat[var][mask_cyc]
var_itp,pres_itp = inpaintnan1d(var_array,pres_old,res)
var_itp = var_itp.reshape(len(var_itp),1)
da = xr.DataArray(data=var_itp,
dims=['pressure','n_prof'],
coords={'pressure':(['pressure'],pres_itp),
'validity':(['n_prof'],['yes']),
'n_prof':(['n_prof'],[i]),
'eddyidx':(['n_prof'],[ied]),
'float':(['n_prof'],[ifl]),
'cycle_number':(['n_prof'],[icyc]),
'x':(['n_prof'],[x]),
'lon':(['n_prof'],[lon]),
'lat':(['n_prof'],[lat]),
'time':(['n_prof'],[time]),
'time_edd':(['n_prof'],[time_edd]),
'lon_edd':(['n_prof'],[lon_edd]),
'lat_edd':(['n_prof'],[lat_edd]),
'lon_edd_eff':(['n_prof'],[lon_edd_eff]),
'lat_edd_eff':(['n_prof'],[lat_edd_eff]),
'rad':(['n_prof'],[rad]),
'rad_deg':(['n_prof'],[rad_deg])})
ds[var] = da
ds = ds.assign_coords({'lon_cont':(['n_prof','Ncont'],lon_cont.data),'lat_cont':(['n_prof','Ncont'],lat_cont.data)})
elif len(pres_valid) == 1:
ds = xr.Dataset({})
for ivar,var in enumerate(var_adjusted):
da = xr.DataArray(data=ifloat[var][mask_cyc].reshape(1,1),
dims=['pressure','n_prof'],
coords={'pressure':(['pressure'],[0]),
'validity':(['n_prof'],['no']),
'n_prof':(['n_prof'],[i]),
'eddyidx':(['n_prof'],[ied]),
'float':(['n_prof'],[ifl]),
'cycle_number':(['n_prof'],[icyc]),
'x':(['n_prof'],[x]),
'lon':(['n_prof'],[lon]),
'lat':(['n_prof'],[lat]),
'time':(['n_prof'],[time]),
'time_edd':(['n_prof'],[time_edd]),
'lon_edd':(['n_prof'],[lon_edd]),
'lat_edd':(['n_prof'],[lat_edd]),
'lon_edd_eff':(['n_prof'],[lon_edd_eff]),
'lat_edd_eff':(['n_prof'],[lat_edd_eff]),
'rad':(['n_prof'],[rad]),
'rad_deg':(['n_prof'],[rad_deg])})
ds[var] = da
ds = ds.assign_coords({'lon_cont':(['n_prof','Ncont'],lon_cont.data),'lat_cont':(['n_prof','Ncont'],lat_cont.data)})
else:
print(f'PRES_ADJUSTED is all nan at F{ifl} - C{icyc}')
if i == 0:
DS = ds.copy()
# create eddy dataset
ds_ed = ieddy_ds(ied,eddy)
DS_ED = ds_ed.copy()
else:
DS = xr.merge([DS,ds])
# DS = xr.concat([DS,ds],dim='n_prof')
track = eddy.isel(obs=ied).track.values
if track not in DS_ED.track.values:
ds_ed = ieddy_ds(ied,eddy)
DS_ED = combineEDDY(DS_ED,ds_ed)
# DS = DS.sortby('x')
DS = DS.transpose('pressure','n_prof','Ncont')
DS_ED = DS_ED.transpose('n_eddy','obs')
mask = np.isnan(DS_ED.eddyidx_obs.values)
DS_ED['eddyidx_obs'] = (['n_eddy','obs'],np.where(mask,-999,DS_ED.eddyidx_obs.values.astype(int)))
for key in ['float','cycle_number','eddyidx']:
DS[key] = DS[key].astype(int)
if changename:
DS = changeName(DS)
return DS,DS_ED
def makeArgoProf_OUT(eddy_type='O',changename=True,test=False):
var_list = ['TEMP','PSAL','DOXY','CHLA','NITRATE','CHLA_FLUORESCENCE']
dir = '/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/data/'
argodata_mat = mat73.loadmat(dir+'argo_data.mat')
argoineddy_mat = mat73.loadmat(dir+'argoineddy.mat')
argoineddy = argoineddy_mat['argoineddy']
argodata = argodata_mat['Data']
# create masks
eddytype_split = np.array(list(argoineddy['eddytype'][:4121]))
mask_edy = (eddytype_split==eddy_type)
# extract index
floatidx = argoineddy['argoindex'][mask_edy,0].astype(int)
cycidx = np.load('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/data/argoineddy_new.npy')[mask_edy,1]
if test:
l = 5
else:
l = len(floatidx)
for i in range(l):
ifl = floatidx[i]
icyc = cycidx[i]
# make profile dataset
ifloat = argodata[f'F{ifl}']
var_all = np.array(list(ifloat.keys()))
var_adjusted = useADJUSTED(var_all,var_list)
mask_cyc = (ifloat['CYCLE_NUMBER']==icyc)
lat = np.mean(ifloat['LATITUDE'][mask_cyc])
lon = np.mean(ifloat['LONGITUDE'][mask_cyc])
coords = np.array([lat,lon])
juld = np.mean(ifloat['JULD'][mask_cyc])
time = argoJULD2TS(juld)
pres_name = useADJUSTED(var_all,['PRES'])[0]
pres_old = ifloat[pres_name][mask_cyc].flatten()
pres_valid = pres_old[np.isfinite(pres_old)]
# extract vars and interpolate when pressure level is larger than 1
if len(pres_valid) > 1:
ds = xr.Dataset({})
for ivar,var in enumerate(var_adjusted):
res = 1
var_array = ifloat[var][mask_cyc]
var_itp,pres_itp = inpaintnan1d(var_array,pres_old,res)
var_itp = var_itp.reshape(len(var_itp),1)
da = xr.DataArray(data=var_itp,
dims=['pressure','n_prof'],
coords={'pressure':(['pressure'],pres_itp),
'validity':(['n_prof'],['yes']),
'n_prof':(['n_prof'],[i]),
'float':(['n_prof'],[ifl]),
'cycle_number':(['n_prof'],[icyc]),
'lon':(['n_prof'],[lon]),
'lat':(['n_prof'],[lat]),
'time':(['n_prof'],[time])})
if var == 'CHLA_FLUORESCENCE_ADJUSTED':
var = 'CHLA_ADJUSTED'
ds[var] = da
elif len(pres_valid) == 1:
ds = xr.Dataset({})
for ivar,var in enumerate(var_adjusted):
da = xr.DataArray(data=ifloat[var][mask_cyc].reshape(1,1),
dims=['pressure','n_prof'],
coords={'pressure':(['pressure'],[0]),
'validity':(['n_prof'],['no']),
'n_prof':(['n_prof'],[i]),
'float':(['n_prof'],[ifl]),
'cycle_number':(['n_prof'],[icyc]),
'lon':(['n_prof'],[lon]),
'lat':(['n_prof'],[lat]),
'time':(['n_prof'],[time])})
if var == 'CHLA_FLUORESCENCE_ADJUSTED':
var = 'CHLA_ADJUSTED'
ds[var] = da
else:
print(f'PRES_ADJUSTED is all nan at F{ifl} - C{icyc}')
if i == 0:
DS = ds.copy()
else:
DS = xr.merge([DS,ds])
# DS = xr.concat([DS,ds],dim='n_prof')
# DS = DS.sortby('x')
DS = DS.transpose('pressure','n_prof')
for key in ['float','cycle_number']:
DS[key] = DS[key].astype(int)
if changename:
DS = changeName(DS)
else:
print('have to change var name to proceed the following processes: addSigma0, addCARS, calculateAnom')
DS_ED = xr.Dataset({})
return DS,DS_ED
# ----------------------------------- History -----------------------------------
def importIndices(ctd_type,eddy_type):
dir = '/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/'
if ctd_type == 'hydro':
inACEidx_mat = loadmat(dir+'inACEidx_hy.mat')
inCEidx_mat = loadmat(dir+'inCEidx_hy.mat')
ctdinACEidx_mat = loadmat(dir+'ctdinACEidx_hy.mat')
ctdinCEidx_mat = loadmat(dir+'ctdinCEidx_hy.mat')
inACEidx = inACEidx_mat['inACEidx_hy']-1 # due to matlab to python index
inCEidx = inCEidx_mat['inCEidx_hy']-1 # due to matlab to python index
ctdinACEidx = ctdinACEidx_mat['ctdinACEidx_hy']
ctdinCEidx = ctdinCEidx_mat['ctdinCEidx_hy']
elif ctd_type == 'ctd':
inACEidx_mat = loadmat(dir+'inACEidx.mat')
inCEidx_mat = loadmat(dir+'inCEidx.mat')
ctdinACEidx_mat = loadmat(dir+'ctdinACEidx.mat')
ctdinCEidx_mat = loadmat(dir+'ctdinCEidx.mat')
inACEidx = inACEidx_mat['inACEidx']-1 # due to matlab to python index
inCEidx = inCEidx_mat['inCEidx']-1 # due to matlab to python index
ctdinACEidx = ctdinACEidx_mat['ctdinACEidx']
ctdinCEidx = ctdinCEidx_mat['ctdinCEidx']
else:
print('invalid input')
if eddy_type == 'C':
profidx = ctdinCEidx
eddyidx = inCEidx
elif eddy_type == 'A':
profidx = ctdinACEidx
eddyidx = inACEidx
return profidx,eddyidx
def importHisProf(ctd_type):
# also include renameing n_prof, and calculating sigma0
# import prof
if ctd_type == 'ctd':
prof = xr.open_dataset('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/Python/extract_data/synchronized/prof_ctd_514prof.nc')
# rename fluorometer to fluorescence
prof = prof.rename({'fluorometer':'fluorescence',
'fluorometerFlag':'fluorescenceFlag'})
elif ctd_type == 'hydro':
prof = xr.open_dataset('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/Python/extract_data/synchronized/prof_hydro_362prof.nc')
prof['nitrate'] = prof['nox'] - prof['nitrite']
else:
print('invalid input')
# key_list = list(prof.data_vars.keys())
# var_list = [il for il in key_list if 'Flag' not in il]
# for ip in prof['n_prof'].values:
# for var in var_list:
# da = prof[var]
# res = 1
# pres0 = int(prof.pressure.min())
# pres1 = int(prof.pressure.max()+1)
# pres_interp = np.arange(pres0,pres1+res/2,res)
# value_interp = inpaintnan1d(da,pres0,pres1,res)
# prof = prof.interp(pressure=pres_interp)
prof = prof.transpose('pressure','n_prof')
prof = prof.sortby('time').drop_vars('n_prof').assign_coords({'n_prof':(['n_prof'],np.arange(0,len(prof.ctdindex.values)))})
# if prof_type == 'ctd':
# prof = addSigma0(prof)
return prof
def makeHisProf_type(ctd_type,eddy_type,test=False):
# prof_type: hydro, ctd
# eddy_type: CE, ACE
# import prof
prof = importHisProf(ctd_type)
# import eddy
if eddy_type == 'C':
eddy = xr.open_dataset('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/cyclonic_eddy_traj_eac_days_more30_meta3p2_dt_2sat.nc', decode_cf=False)
elif eddy_type == 'A':
eddy = xr.open_dataset('/Users/renjiongqiu/Library/CloudStorage/OneDrive-UniversityofTasmania/Documents/eddy_tracking/anticyclonic_eddy_traj_eac_days_more30_meta3p2_dt_2sat.nc', decode_cf=False)
else:
print('invalid input')
eddy['effective_radius'].values=eddy['effective_radius'].values.astype('float')
# import indices
profidx,eddyidx = importIndices(ctd_type,eddy_type)
if test:
l = 5
else:
l = len(profidx)
for ip in range(l):
# subset
ictdindex = profidx.flatten()[ip]
mask_ictd = np.in1d(prof.ctdindex.values,ictdindex)
prof1 = prof.sel(n_prof=mask_ictd).squeeze()
ied = eddyidx.flatten()[ip]
eddy1 = eddy.isel(obs=ied)
# extract prof parameters
time = prof1.time.values
cast = prof1.cast.values
cruise = prof1.cruise.values
ctdindex = prof1.ctdindex.values
lon = prof1.lon.values
lat = prof1.lat.values
# extract eddy parameters
lon_edd = eddy1.longitude.values
lat_edd = eddy1.latitude.values
lon_edd_eff = eddy1.longitude_max.values
lat_edd_eff = eddy1.latitude_max.values
rad = eddy1.effective_radius.values * 50
rad_deg = rad / 1000 * 0.009
lon_cont = eddy1.effective_contour_longitude.values.reshape(1,20)*0.01+180
lat_cont = eddy1.effective_contour_latitude.values.reshape(1,20)*0.01
time_edd = days2dt64(eddy1.time.values).values
coords_edd = np.array([lat_edd,lon_edd])
coords_ctd = np.array([lat,lon])
# calculate x
idist = distance.distance(coords_edd, coords_ctd).m
x = idist/rad
# interpolate
key_list = list(prof1.data_vars.keys())
var_list = [il for il in key_list if 'Flag' not in il]
pres_old = prof1.pressure.values
ds = xr.Dataset({})
for var in var_list:
res = 1
var_array = prof1[var].values
value_interp,pres_interp = inpaintnan1d(var_array,pres_old,res)
value_interp = value_interp.reshape(len(value_interp),1)
da = xr.DataArray(data = value_interp,
dims = ['pressure','n_prof'],
coords = {'n_prof':(['n_prof'],[ip]),
'cast':(['n_prof'],[cast]),
'cruise':(['n_prof'],[cruise]),
'ctdindex':(['n_prof'],[ctdindex]),
'x':(['n_prof'],[x]),
'pressure':(['pressure'],pres_interp),
'eddyidx':(['n_prof'],[ied]),
'lon':(['n_prof'],[lon]),
'lat':(['n_prof'],[lat]),
'time':(['n_prof'],[time]),
'time_edd':(['n_prof'],time_edd),
'lon_edd':(['n_prof'],[lon_edd]),
'lat_edd':(['n_prof'],[lat_edd]),
'lon_edd_eff':(['n_prof'],[lon_edd_eff]),
'lat_edd_eff':(['n_prof'],[lat_edd_eff]),
'rad':(['n_prof'],[rad]),
'rad_deg':(['n_prof'],[rad_deg])})
ds[var] = da
ds = ds.assign_coords({'lon_cont':(['n_prof','Ncont'],lon_cont.data),'lat_cont':(['n_prof','Ncont'],lat_cont.data)})
if ip == 0:
DS = ds.copy()
# create eddy dataset, use the origin eddy not eddy1
ds_ed = ieddy_ds(ied,eddy)
DS_ED = ds_ed.copy()
else:
DS = xr.merge([DS,ds])
# DS = xr.concat([DS,ds],dim='n_prof')
track = eddy.isel(obs=ied).track.values
if track not in DS_ED.track.values:
ds_ed = ieddy_ds(ied,eddy)
DS_ED = combineEDDY(DS_ED,ds_ed)
DS = DS.sortby('x')
# transpose
DS = DS.transpose('pressure','n_prof','Ncont')
DS_ED = DS_ED.transpose('n_eddy','obs')
# convert index to int
DS['eddyidx'] = DS['eddyidx'].astype(int)
mask = np.isnan(DS_ED.eddyidx_obs.values)
DS_ED['eddyidx_obs'] = (['n_eddy','obs'],np.where(mask,-999,DS_ED.eddyidx_obs.values.astype(int)))
return DS,DS_ED
def CTDaddHydro_his_old(prof_ctd,prof_hydro):
# check if there is identical 'x' value, proceed if there is not
if not np.in1d(prof_ctd['x'].values,prof_hydro['x'].values).any():
ds = xr.merge([prof_ctd.swap_dims({'n_prof':'x'}),prof_hydro.swap_dims({'n_prof':'x'})])
else:
print('there is identical x dim')
# drop the old 'n_prof'
ds = ds.drop_vars('n_prof')
# create a new 'n_prof' dim and swap with x
n_prof = np.arange(0,len(ds['x'].values))
ds = ds.sortby('x') # doesn't have to do this
ds = ds.assign_coords({'n_prof':(['x'],n_prof)}).swap_dims({'x':'n_prof'})
return ds
def CTDaddHydro_his(prof_ctd,prof_hydro):
# create new dim values
l1 = len(prof_ctd.n_prof.values)
l2 = len(prof_hydro.n_prof.values)
n_prof_ar1 = np.arange(0,l1)
n_prof_ar2 = np.arange(l1,l1+l2)
# first assign the new dim values to a new coords
prof_ctd = prof_ctd.assign_coords({'n_prof_new':(['n_prof'],n_prof_ar1)})
prof_hydro = prof_hydro.assign_coords({'n_prof_new':(['n_prof'],n_prof_ar2)})
# then swap with the old dim
prof_ctd = prof_ctd.swap_dims({'n_prof':'n_prof_new'})
prof_hydro = prof_hydro.swap_dims({'n_prof':'n_prof_new'})
# merge the two profs
prof_merge = xr.merge([prof_ctd,prof_hydro])
# drop the old dim 'n_prof'
prof_merge = prof_merge.drop_vars('n_prof')
prof_merge = prof_merge.rename({'n_prof_new':'n_prof'})
return prof_merge
def makeHisProf(eddy_type,test=False):
prof_ctd,eddy_ctd = makeHisProf_type('ctd',eddy_type,test=test)
prof_hydro,eddy_hydro = makeHisProf_type('hydro',eddy_type,test=test)
prof_his = CTDaddHydro_his(prof_ctd,prof_hydro)
prof_his = prof_his.assign({'cast':(['n_prof'],prof_his.cast.values.astype(int)),
'eddyidx':(['n_prof'],prof_his.eddyidx.values.astype(int))})
eddy_his = combineEDDY(eddy_ctd,eddy_hydro)
return prof_his,eddy_his
def makeHisProf_OUT(eddy_type='O',test=False):
prof = {}
for ctd_type in ['ctd','hydro']:
prof_all = importHisProf(ctd_type)
profidx_c, _ = importIndices(ctd_type,'C')
profidx_a, _ = importIndices(ctd_type,'A')
mask_c = np.in1d(prof_all.ctdindex.values,profidx_c.flatten())
mask_a = np.in1d(prof_all.ctdindex.values,profidx_a.flatten())
mask_o = ~( mask_c | mask_a )
prof_out = prof_all.sel({'n_prof':mask_o}).transpose('pressure','n_prof')
if test:
l = 5
else:
l = len(prof_out.n_prof.values)
for i in range(l):
prof1 = prof_out.isel(n_prof=i).squeeze()
# extract prof parameters
n_prof = prof1.n_prof.values
time = prof1.time.values
cast = prof1.cast.values
cruise = prof1.cruise.values
ctdindex = prof1.ctdindex.values
lon = prof1.lon.values
lat = prof1.lat.values
key_list = list(prof1.data_vars.keys())
var_list = [il for il in key_list if 'Flag' not in il]
pres_old = prof1.pressure.values
ds = xr.Dataset({})
for var in var_list:
res = 1
var_array = prof1[var].values
value_interp,pres_interp = inpaintnan1d(var_array,pres_old,res)
value_interp = value_interp.reshape(len(value_interp),1)
da = xr.DataArray(data = value_interp,
dims = ['pressure','n_prof'],
coords = {'n_prof':(['n_prof'],[n_prof]),
'cast':(['n_prof'],[cast]),
'cruise':(['n_prof'],[cruise]),
'ctdindex':(['n_prof'],[ctdindex]),
'pressure':(['pressure'],pres_interp),
'lon':(['n_prof'],[lon]),
'lat':(['n_prof'],[lat]),
'time':(['n_prof'],[time])})
ds[var] = da
if i == 0:
DS = ds.copy()
else:
DS = xr.merge([DS,ds])
# DS = xr.concat([DS,ds],dim='n_prof')
# transpose
DS = DS.transpose('pressure','n_prof')
prof[ctd_type] = DS
prof_merge = CTDaddHydro_his(prof['ctd'],prof['hydro'])
prof_merge = prof_merge.assign({'cast':(['n_prof'],prof_merge.cast.values.astype(int))})
eddy = xr.Dataset({})
return prof_merge,eddy
# ----------------------------------- V06 -----------------------------------
def inpolygon(xq, yq, xv, yv):
shape = xq.shape
xq = xq.reshape(-1)
yq = yq.reshape(-1)
xv = xv.reshape(-1)
yv = yv.reshape(-1)
q = [(xq[i], yq[i]) for i in range(xq.shape[0])]
p = path.Path([(xv[i], yv[i]) for i in range(xv.shape[0])])
return p.contains_points(q).reshape(shape)
def create_subset(ds, lon_min, lon_max, lat_min, lat_max, lifespan):
# demarkation of the study region
lon_min, lon_max, lat_min, lat_max = lon_min, lon_max, lat_min, lat_max
# Creating a mask of the study region
subset = ds.sel(obs=(ds.longitude > lon_min) & (ds.longitude < lon_max)
& (ds.latitude > lat_min) & (ds.latitude < lat_max))
# Creating a subset using the mask
subset = ds.isel(obs=np.in1d(ds.track, subset.track))
# Further applying lifespan filter to eddies
subset_life = subset.sel(obs=subset.observation_number > lifespan)
# saving the final output for further analyses
# Create the final subset
subset_final = subset.isel(obs=np.in1d(subset.track, subset_life.track))
return subset_final
def make_interp_dataset_ctd(ctd_files):
keys = ['pressure','temperature','salinity','oxygen','par','cdom','transmissometer','fluorometer','obs']
var_list = keys.copy()
var_list.remove('pressure')
flags = [var+'Flag' for var in var_list]
for i,fn in enumerate(ctd_files):
var_dict = {}
ds = xr.open_dataset(fn)
lon = ds.longitude.values[0]
lat = ds.latitude.values[0]
time = ds.time.values[0]
ds = ds[keys+flags].squeeze(dim=['time','latitude','longitude'])
pressure_old = ds.pressure.values
for iv,var in enumerate(var_list):
da = ds[var]
daflag = ds[var+'Flag']
da = da.where(daflag==0,np.nan) # flag the bad data
id = int(ds.attrs['Deployment'])
res = 1
value_interp,pressure_interp = inpaintnan1d(da.values,pressure_old,res)
var_dict[var] = {'data':value_interp,
'dims':'pressure',
'coords':{'pressure':{'dims':'pressure','data':pressure_interp}}}
ds = xr.Dataset.from_dict(var_dict)
ds = ds.expand_dims({'deployment':1})
ds['deployment'] = [id]
ds = ds.assign_coords( {'lat':(['deployment'],[lat]),
'lon':(['deployment'],[lon]),
'time':(['deployment'],[time]),
'pressure':(['pressure'],pressure_interp)} )
ds = ds.drop_duplicates(dim=...)
if i == 0:
DS = ds.copy()
else:
DS = xr.merge([DS,ds])
DS = DS.transpose('pressure','deployment')
return DS
def make_interp_dataset_hydro(hydro_file):