-
Notifications
You must be signed in to change notification settings - Fork 9
/
_utils.py
executable file
·1583 lines (1259 loc) · 53.2 KB
/
_utils.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
############################################################
# Program is part of icams #
# Copyright 2019 Yunmeng Cao #
# Contact: ymcmrs@gmail.com #
############################################################
# Part of the program is modified from PyAPS
# Copyright 2012, by the California Institute of Technology
# Contact: earthdef@gps.caltech.edu
# Modified by A. Benoit and R. Jolivet 2019
# Contact: insar@geologie.ens.fr
import configparser as ConfigParser
import sys
import os.path
import cdsapi
# disable InsecureRequestWarning message from cdsapi
import urllib3
urllib3.disable_warnings()
import scipy.interpolate as intp
import scipy.integrate as intg
import scipy.spatial as ss
from scipy.optimize import leastsq
from scipy.stats.stats import pearsonr
from icams import elevation_models
from pykrige import OrdinaryKriging
from pykrige import variogram_models
from scipy.interpolate import RegularGridInterpolator as RGI
from scipy import interpolate
import pygrib
import numpy as np
import h5py
from skimage import io
import pyproj
from icams.geoid import GeoidHeight as gh # calculating geoid height
from math import radians, cos, sin, asin, sqrt
######Set up variables in model.cfg before using
dpath = os.path.dirname(__file__)
config = ConfigParser.RawConfigParser(delimiters='=')
config.read('%s/user.cfg'%(dpath))
variogram_dict = {'linear': variogram_models.linear_variogram_model,
'power': variogram_models.power_variogram_model,
'gaussian': variogram_models.gaussian_variogram_model,
'spherical': variogram_models.spherical_variogram_model,
'exponential': variogram_models.exponential_variogram_model,
'hole-effect': variogram_models.hole_effect_variogram_model}
def ECMWFdload(bdate,hr,filedir,model='ERA5',datatype='fc',humidity='Q',snwe=None,flist=None):
'''
ECMWF-ERA5 data downloading.
Args:
* bdate : date to download (str)
* hr : hour to download (str)
* filedir : files directory (str)
* model : weather model ('ERA5', 'ERAINT', 'HRES')
* datatype : reanalysis data type (an)
* snwe : area extent (tuple of int)
* humidity : humidity
'''
#-------------------------------------------
# Initialize
# Check data
assert model in ('ERA5', 'ERAINT', 'HRES'), 'Unknown model for ECMWF: {}'.format(model)
# Infos for downloading
if model in 'ERA5':
print('Download ERA-5 re-analysis-datasets from the latest ECMWF platform:')
print('https://cds.climate.copernicus.eu/api/v2')
else:
print('WARNING: you are downloading from the old ECMWF platform. '
'ERA-Interim is deprecated, use ERA-5 instead.')
#-------------------------------------------
# Define parameters
# Humidity
assert humidity in ('Q','R'), 'Unknown humidity field for ECMWF'
if humidity in 'Q':
humidparam = 'specific_humidity'
elif humidity in 'R':
humidparam = 'relative_humidity'
#-------------------------------------------
# file name
if not flist:
flist = []
for k in range(len(bdate)):
day = bdate[k]
# Only for ERA-5
fname = os.path.join(filedir, 'ERA-5_{}_{}.grb'.format(day, hr))
flist.append(fname)
# Iterate over dates
for k in range(len(bdate)):
day = bdate[k]
fname = flist[k]
#-------------------------------------------
# Request for CDS API client (new ECMWF platform, for ERA-5)
url = 'https://cds.climate.copernicus.eu/api/v2'
key = config.get('CDS', 'key')
# Contact the server
c = cdsapi.Client(url=url, key=key)
# Pressure levels
pressure_lvls = ['1','2','3','5','7','10','20','30','50',
'70','100','125','150','175','200','225',
'250','300','350','400','450','500','550',
'600','650','700','750','775','800','825',
'850','875','900','925','950','975','1000']
# Dictionary
indict = {'product_type' :'reanalysis',
'format' :'grib',
'variable' :['geopotential','temperature','{}'.format(humidparam)],
'pressure_level' : pressure_lvls,
'year' :'{}'.format(day[0:4]),
'month' :'{}'.format(day[4:6]),
'day' :'{}'.format(day[6:8]),
'time' :'{}:00'.format(hr)}
# download a geographical area subset
if snwe is not None:
s, n, w, e = snwe
indict['area'] = '/'.join(['{:.2f}'.format(x) for x in [n, w, s, e]])
# Assert grib file not yet downloaded
if not os.path.exists(fname):
print('Downloading %d of %d: %s '%(k+1,len(bdate), fname))
print(indict)
# Make the request
c.retrieve('reanalysis-{}-pressure-levels'.format(model.lower()),indict,target=fname)
return flist
######################## calculate delay #############################
def initconst():
'''Initialization of various constants needed for computing delay.
Args:
* None
Returns:
* constdict (dict): Dictionary of constants'''
constdict = {}
constdict['k1'] = 0.776 #(K/Pa)
constdict['k2'] = 0.716 #(K/Pa)
constdict['k3'] = 3750 #(K^2.Pa)
constdict['g'] = 9.81 #(m/s^2)
constdict['Rd'] = 287.05 #(J/Kg/K)
constdict['Rv'] = 461.495 #(J/Kg/K)
constdict['mma'] = 29.97 #(g/mol)
constdict['mmH'] = 2.0158 #(g/mol)
constdict['mmO'] = 16.0 #(g/mol)
constdict['Rho'] = 1000.0 #(kg/m^3)
constdict['a1w'] = 611.21 # hPa
constdict['a3w'] = 17.502 #
constdict['a4w'] = 32.19 # K
constdict['a1i'] = 611.21 # hPa
constdict['a3i'] = 22.587 #
constdict['a4i'] = -0.7 # K
constdict['T3'] = 273.16 # K
constdict['Ti'] = 250.16 # K
constdict['nhgt'] = 300 # Number of levels for interpolation (OLD 151)
constdict['minAlt'] = -200.0
constdict['maxAlt'] = 50000.0
constdict['minAltP'] = -200.0
constdict['Rearth'] = 6371 # km
return constdict
def cc_era(tmp,cdic):
'''Clausius Clayperon law used by ERA Interim.
Args:
* tmp (np.array) : Temperature.
* cdic (dict) : Dictionary of constants
Returns:
* esat (np.array) : Water vapor saturation partial pressure.'''
a1w = cdic['a1w']
a3w = cdic['a3w']
a4w = cdic['a4w']
a1i = cdic['a1i']
a3i = cdic['a3i']
a4i = cdic['a4i']
T3 = cdic['T3']
Ti = cdic['Ti']
esatw = a1w*np.exp(a3w*(tmp-T3)/(tmp-a4w))
esati = a1i*np.exp(a3i*(tmp-T3)/(tmp-a4i))
esat = esati.copy()
for k in range(len(tmp)):
if (tmp[k] >= T3):
esat[k] = esatw[k]
elif (tmp[k] <= Ti):
esat[k] = esati[k]
else:
wgt = (tmp[k]-Ti)/(T3-Ti)
esat[k] = esati[k] + (esatw[k]-esati[k])*wgt*wgt
return esat
########Read in ERA data from a given ERA Interim file##################
def get_ecmwf(model,fname,cdic, humidity='Q',minlat= -90,maxlat = 90,minlon = 0,maxlon = 360,verbose=False):
'''Read data from ERA Interim, ERA-5 or HRES grib file. Note that Lon values should be between [0-360].
Modified by A. Benoit, January 2019.
Args:
* model (str): Model used (ERA5, ERAINT or HRES)
* fname (str): Path to the grib file
* minlat (np.float): Minimum latitude
* maxlat (np.float): Maximum latitude
* minlon (np.float): Minimum longitude
* maxlon (np.float): Maximum longitude
* cdic (np.float): Dictionary of constants
Kwargs:
* humidity (str): Specific ('Q') or relative humidity ('R').
Returns:
* lvls (np.array): Pressure levels
* latlist(np.array): Latitudes of the stations
* lonlist(np.array): Longitudes of the stations
* gph (np.array): Geopotential height
* tmp (np.array): Temperature
* vpr (np.array): Vapor pressure
.. note::
Uses cc_era by default.
'''
assert humidity in ('Q','R'), 'Undefined humidity field in get_era.'
assert model in ('ERA5', 'ERAINT','HRES'), 'Model not recognized.'
if verbose:
print('PROGRESS: READING GRIB FILE')
if model in 'HRES':
if verbose:
print('INFO: USING PRESSURE LEVELS OF HRES DATA')
lvls = np.array([1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 150,
200, 250, 300, 400, 500, 600, 700,
800, 850, 900, 925, 950, 1000])
else:
if verbose:
print('INFO: USING PRESSURE LEVELS OF ERA-INT OR ERA-5 DATA')
lvls = np.array([1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 125, 150, 175,
200, 225, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 775,
800, 825, 850, 875, 900, 925, 950, 975, 1000])
nlvls = len(lvls)
alpha = cdic['Rv']/cdic['Rd']
gphind = np.arange(nlvls)*3
grbs = pygrib.open(fname)
grbs.seek(gphind[0])
grb = grbs.read(1)[0]
lats,lons = grb.latlons()
if model == 'ERA5':
lons[lons < 0.] += 360.
g = cdic['g']
mask = ((lats > minlat) & (lats < maxlat)) & ((lons > minlon) & (lons < maxlon))
#extrqct indices
uu = [i for i in list(range(np.shape(mask)[0])) if any(mask[i,:])]
vv = [j for j in list(range(np.shape(mask)[1])) if any(mask[:,j])]
latlist = lats[uu,:][:,vv]
lonlist = lons[uu,:][:,vv]
#nstn = len(lat.flatten())
nlat, nlon = latlist.shape
####Create arrays for 3D storage
gph = np.zeros((nlvls, nlat, nlon)) #Potential height
tmp = gph.copy() #Temperature
vpr = gph.copy() #Vapor pressure
if verbose:
print('INFO: IMAGE DIMENSIONS: {} LATITUDES AND {} LONGITUDES'.format(nlat, nlon))
lvls = 100.0*lvls #Conversion to absolute pressure
for i in range(nlvls):
grbs.seek(gphind[i]) #Reading potential height.
grb = grbs.read(3)
gph[i,:,:] = grb[0].values[uu,:][:,vv]/g
#Reading temperature
temp = grb[1].values[uu,:][:,vv]
tmp[i,:,:] = temp
if humidity in ('R'): # Relative humidity
esat = cc_era(temp,cdic)
temp = grb[2].values[uu,:][:,vv]/100.0
vpr[i,:,:] = temp*esat
elif humidity in ('Q'):
val = grb[2].values #Specific humidity
temp = grb[2].values[uu,:][:,vv]
vpr[i,:,:] = temp*lvls[i]*alpha/(1+(alpha - 1)*temp)
else:
assert 1==0, 'Undefined Humidity in get_ecmwf().'
return lvls,latlist,lonlist,gph,tmp,vpr
###############Completed GET_ECMWF########################################
###############Completed the list of constants################
##########Interpolating to heights from Pressure levels###########
def intP2H(lvls,hgt,gph,tmp,vpr,cdic,verbose=False):
'''Interpolates the pressure level data to altitude.
Args:
* lvls (np.array) : Pressure levels.
* hgt (np.array) : Height values for interpolation.
* gph (np.array) : Geopotential height.
* tmp (np.array) : Temperature.
* vpr (np.array) : Vapor pressure.
* cdic (dict) : Dictionary of constants.
.. note::
gph,tmp,vpr are of size (nstn,nlvls).
Returns:
* Presi (np.array) : Interpolated pressure.
* Tempi (np.array) : Interpolated temperature.
* Vpri (np.array) : Interpolated vapor pressure.
.. note::
Cubic splines are used to convert pressure level data to height level data.'''
minAlt = cdic['minAlt'] #Hardcoded parameter.
maxAlt = gph.max().round()
if verbose:
print('PROGRESS: INTERPOLATING FROM PRESSURE TO HEIGHT LEVELS')
nlat = gph.shape[1] #Number of stations
nlon = gph.shape[2]
nhgt = len(hgt) #Number of height points
Presi = np.zeros((nlat,nlon,nhgt))
Tempi = np.zeros((nlat,nlon,nhgt))
Vpri = np.zeros((nlat,nlon,nhgt))
for i in range(nlat):
for j in range(nlon):
temp = gph[:,i,j] #Obtaining height values
hx = temp.copy()
sFlag = False
eFlag = False
if (hx.min() > minAlt): #Add point at start
sFlag = True
hx = np.concatenate((hx,[minAlt-1]),axis = 0) #changed from 1 to 0 (-1 should also work), CL
if (hx.max() < maxAlt): #Add point at end
eFlag = True
hx = np.concatenate(([maxAlt+1],hx),axis=0) #changed from 1 to 0 (-1 should also work), CL
hx = -hx #Splines needs monotonically increasing.
hy = lvls.copy() #Interpolating pressure values
if (sFlag == True):
val = hy[-1] +(hx[-1] - hx[-2])* (hy[-1] - hy[-2])/(hx[-2]-hx[-3])
hy = np.concatenate((hy,[val]),axis=0) #changed from 1 to 0 (-1 should also work), CL
if (eFlag == True):
val = hy[0] - (hx[0] - hx[1]) * (hy[0] - hy[1])/(hx[1]-hx[2])
hy = np.concatenate(([val],hy),axis=0) #changed from 1 to 0 (-1 should also work), CL
tck = intp.interp1d(hx,hy,kind='cubic')
temp = tck(-hgt) #Again negative for consistency with hx
Presi[i,j,:] = temp.copy()
del temp
temp = tmp[:,i,j] #Interpolating temperature
hy = temp.copy()
if (sFlag == True):
val = hy[-1] +(hx[-1] - hx[-2])* (hy[-1] - hy[-2])/(hx[-2]-hx[-3])
hy = np.concatenate((hy,[val]),axis=0) #changed from 1 to 0 (-1 should also work), CL
if (eFlag == True):
val = hy[0] - (hx[0] - hx[1]) * (hy[0] - hy[1])/(hx[1]-hx[2])
hy = np.concatenate(([val],hy),axis=0) #changed from 1 to 0 (-1 should also work), CL
tck = intp.interp1d(hx,hy,kind='cubic')
temp = tck(-hgt)
Tempi[i,j,:] = temp.copy()
del temp
temp = vpr[:,i,j] #Interpolating vapor pressure
hy = temp.copy()
if (sFlag == True):
val = hy[-1] +(hx[-1] - hx[-2])* (hy[-1] - hy[-2])/(hx[-2]-hx[-3])
hy = np.concatenate((hy,[val]),axis=0) #changed from 1 to 0 (-1 should also work), CL
if (eFlag == True):
val = hy[0] - (hx[0] - hx[1]) * (hy[0] - hy[1])/(hx[1]-hx[2])
hy = np.concatenate(([val],hy),axis=0) #changed from 1 to 0 (-1 should also work), CL
tck = intp.interp1d(hx,hy,kind='cubic')
temp = tck(-hgt)
Vpri[i,j,:] = temp.copy()
del temp
## Save into files for plotting (test for era5 interpolation)
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/alt_20141023_20141210_era5.txt', gph, fmt=' '.join(['%s']*380)) # Save altitude (of lvls)
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/lvls_20141023_20141210_era5.txt', lvls.T, fmt='%s') # Save pressure
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/tmp_20141023_20141210_era5.txt', tmp, fmt=' '.join(['%s']*380)) # Save temperature
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/vpr_20141023_20141210_era5.txt', vpr, fmt=' '.join(['%s']*380)) # Save vapor
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/Alti_20141023_20141210_era5.txt', hgt.T, fmt='%s') # Save interpo altitude
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/Presi_20141023_20141210_era5.txt', Presi.T, fmt=' '.join(['%s']*380)) # Save interpo pressure
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/Tempi_20141023_20141210_era5.txt', Tempi.T, fmt=' '.join(['%s']*380)) # Save interpo temperature
#np.savetxt('/home/angel/Tests/test_era5interpolation/Outputs/Vpri_20141023_20141210_era5.txt', Vpri.T, fmt=' '.join(['%s']*380)) # Save interpo vapor
return Presi,Tempi,Vpri
###########Completed interpolation to height levels #####################
###########Computing the delay function ###############################
def PTV2del(Presi,Tempi,Vpri,hgt,cdict,verbose=False):
'''Computes the delay function given Pressure, Temperature and Vapor pressure.
Args:
* Presi (np.array) : Pressure at height levels.
* Tempi (np.array) : Temperature at height levels.
* Vpri (np.array) : Vapor pressure at height levels.
* hgt (np.array) : Height levels.
* cdict (np.array) : Dictionary of constants.
Returns:
* DDry2 (np.array) : Dry component of atmospheric delay.
* DWet2 (np.array) : Wet component of atmospheric delay.
.. note::
Computes refractive index at each altitude and integrates the delay using cumtrapz.'''
if verbose:
print('PROGRESS: COMPUTING DELAY FUNCTIONS')
nhgt = len(hgt) #Number of height points
nlat = Presi.shape[0] #Number of stations
nlon = Presi.shape[1]
WonT = Vpri/Tempi
WonT2 = WonT/Tempi
k1 = cdict['k1']
Rd = cdict['Rd']
Rv = cdict['Rv']
k2 = cdict['k2']
k3 = cdict['k3']
g = cdict['g']
#Dry delay
DDry2 = np.zeros((nlat,nlon,nhgt))
DDry2[:,:,:] = k1*Rd*(Presi[:,:,:] - Presi[:,:,-1][:,:,np.newaxis])*1.0e-6/g
#Wet delay
S1 = intg.cumtrapz(WonT,x=hgt,axis=-1)
val = 2*S1[:,:,-1]-S1[:,:,-2]
val = val[:,:,None]
S1 = np.concatenate((S1,val),axis=-1)
del WonT
S2 = intg.cumtrapz(WonT2,x=hgt,axis=-1)
val = 2*S2[:,:,-1]-S2[:,:,-2]
val = val[:,:,None]
S2 = np.concatenate((S2,val),axis=-1)
DWet2 = -1.0e-6*((k2-k1*Rd/Rv)*S1+k3*S2)
for i in range(nlat):
for j in range(nlon):
DWet2[i,j,:] = DWet2[i,j,:] - DWet2[i,j,-1]
return DDry2,DWet2
############# consider LOS direction & horizontal vartiation ##########
def PTV2del_Imp(Presi,Tempi,Vpri,hgt,cdict,verbose=False):
'''Computes the delay function given Pressure, Temperature and Vapor pressure.
Args:
* Presi (np.array) : Pressure at height levels.
* Tempi (np.array) : Temperature at height levels.
* Vpri (np.array) : Vapor pressure at height levels.
* hgt (np.array) : Height levels.
* cdict (np.array) : Dictionary of constants.
Returns:
* DDry2 (np.array) : Dry component of atmospheric delay.
* DWet2 (np.array) : Wet component of atmospheric delay.
.. note::
Computes refractive index at each altitude and integrates the delay using cumtrapz.'''
if verbose:
print('PROGRESS: COMPUTING DELAY FUNCTIONS')
nhgt = len(hgt) #Number of height points
nlat = Presi.shape[0] #Number of stations
nlon = Presi.shape[1]
WonT = Vpri/Tempi
WonT2 = WonT/Tempi
k1 = cdict['k1']
Rd = cdict['Rd']
Rv = cdict['Rv']
k2 = cdict['k2']
k3 = cdict['k3']
g = cdict['g']
#Dry delay
DDry2 = np.zeros((nlat,nlon,nhgt))
DDry2[:,:,:] = k1*Rd*(Presi[:,:,:] - Presi[:,:,-1][:,:,np.newaxis])*1.0e-6/g
#Wet delay
S1 = intg.cumtrapz(WonT,x=hgt,axis=-1)
val = 2*S1[:,:,-1]-S1[:,:,-2]
val = val[:,:,None]
S1 = np.concatenate((S1,val),axis=-1)
del WonT
S2 = intg.cumtrapz(WonT2,x=hgt,axis=-1)
val = 2*S2[:,:,-1]-S2[:,:,-2]
val = val[:,:,None]
S2 = np.concatenate((S2,val),axis=-1)
DWet2 = -1.0e-6*((k2-k1*Rd/Rv)*S1+k3*S2)
for i in range(nlat):
for j in range(nlon):
DWet2[i,j,:] = DWet2[i,j,:] - DWet2[i,j,-1]
return DDry2,DWet2
## 3D interpolate
def make3dintp(Delfn,lonlist,latlist,hgt,hgtscale):
'''Returns a 3D interpolation function that can be used to interpolate using llh coordinates.
Args:
* Delfn (np.array) : Array of delay values.
* lonlist (np.array) : Array of station longitudes.
* latlist (np.array) : Array of station latitudes.
* hgt (np.array) : Array of height levels.
* hgtscale (np.float) : Height scale factor for interpolator.
Returns:
* fnc (function) : 3D interpolation function.
.. note::
We currently use the LinearNDInterpolator from scipy.
'''
##Delfn = Ddry + Dwet. Delay function.
##lonlist = list of lons for stations. / x
##latlist = list of lats for stations. / y
nstn = Delfn.shape[0]
nhgt = Delfn.shape[1]
xyz = np.zeros((nstn*nhgt,3))
Delfn = np.reshape(Delfn,(nstn*nhgt,1))
print('3D interpolation')
count = 0
for m in range(nstn):
for n in range(nhgt):
xyz[count,0] = lonlist[m]
xyz[count,1] = latlist[m]
xyz[count,2] = hgt[n]/hgtscale #For same grid spacing as lat/lon
count += 1
#xyz[:,2] = xyz[:,2] #+ 1e-30*np.random.rand((nstn*nhgt))/hgtscale #For unique Delaunay
del latlist
del lonlist
del hgt
if verbose:
print('PROGRESS: BUILDING INTERPOLATION FUNCTION')
fnc = intp.LinearNDInterpolator(xyz,Delfn)
return fnc
################### Computing the delay samples: lats, lons, heights ##################
def get_delay_sample(height_level, delay_level, heigh_samples):
'''Computes the delay samples at a given altitude, given [heigh_level, delay_level]
Args:
* hgt 2D (np.array) : Height levels.
* delay 2D (np.array) : delay levels.
* hgts 1D (np.array) : altitude sample at each lon/lat
Returns:
* dels 1D (np.array) : delay samples at the corresponding hgts
'''
#nstn = delay_level.shape[0] # samples of horizontal level (lat, lon)
#nhgt = delay_level.shape[1] # samples of vertical level (altitude)
#delay_sample = np.zeros((nstn,), dtype = np.float32)
r0 = delay_level.shape[0]
c0 = delay_level.shape[1]
delay_sample = np.zeros((r0,c0), dtype = np.float32)
for i in range(r0):
for j in range(c0):
hy = delay_level[i,j,:]
hx = height_level
tck = intp.interp1d(hx,hy,kind='cubic')
delay_sample[i,j] = tck(heigh_samples[i,j])
#for i in range(nstn):
# hy = delay_level[i,:]
# hx = height_level[i,:]
# tck = intp.interp1d(hx,hy,kind='cubic')
# delay_sample[i] = tck(heigh_samples[i])
return delay_sample
def read_gamma_par(inFile, task, keyword):
if task == "read":
f = open(inFile, "r")
while 1:
line = f.readline()
if not line: break
if line.count(keyword) == 1:
strtemp = line.split(":")
value = strtemp[1].strip()
return value
print("Keyword " + keyword + " doesn't exist in " + inFile)
f.close
def read_attr(fname):
# read hdf5
with h5py.File(fname, 'r') as f:
atr = dict(f.attrs)
return atr
def read_hdf5(fname, datasetName=None, box=None):
# read hdf5
with h5py.File(fname, 'r') as f:
data = f[datasetName][:]
atr = dict(f.attrs)
return data, atr
def read_txt_line(txt):
# Open the file with read only permit
f = open(txt, "r")
# use readlines to read all lines in the file
# The variable "lines" is a list containing all lines in the file
lines = f.readlines()
lines0 = [line.strip() for line in lines]
f.close()
# remove empty lines
lines_out = []
for line in lines0:
if not len(line) ==0:
lines_out.append(line)
return lines_out
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def write_h5(datasetDict, out_file, metadata=None, ref_file=None, compression=None):
if os.path.isfile(out_file):
print('delete exsited file: {}'.format(out_file))
os.remove(out_file)
print('create HDF5 file: {} with w mode'.format(out_file))
dt = h5py.special_dtype(vlen=np.dtype('float64'))
with h5py.File(out_file, 'w') as f:
for dsName in datasetDict.keys():
data = datasetDict[dsName]
ds = f.create_dataset(dsName,
data=data,
compression=compression)
for key, value in metadata.items():
f.attrs[key] = str(value)
#print(key + ': ' + value)
print('finished writing to {}'.format(out_file))
return out_file
def remove_ramp(lat,lon,data):
# mod = a*x + b*y + c*x*y
lat = lat/180*np.pi
lon = lon/180*np.pi
lon = lon*np.cos(lat) # to get isometrics coordinates
p0 = [0.0001,0.0001,0.0001,0.0000001]
plsq = leastsq(residual_trend,p0,args = (lat,lon,data))
para = plsq[0]
data_trend = data - func_trend(lat,lon,para)
corr, _ = pearsonr(data, func_trend(lat,lon,para))
return data_trend, para, corr
def func_trend(lat,lon,p):
a0,b0,c0,d0 = p
return a0 + b0*lat + c0*lon +d0*lat*lon
def residual_trend(p,lat,lon,y0):
a0,b0,c0,d0 = p
return y0 - func_trend(lat,lon,p)
def func_trend_model(lat,lon,p):
lat = lat/180*np.pi
lon = lon/180*np.pi
lon = lon*np.cos(lat) # to get isometrics coordinates
a0,b0,c0,d0 = p
return a0 + b0*lat + c0*lon +d0*lat*lon
def print_progress(iteration, total, prefix='calculating:', suffix='complete', decimals=1, barLength=50, elapsed_time=None):
"""Print iterations progress - Greenstick from Stack Overflow
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : number of decimals in percent complete (Int)
barLength - Optional : character length of bar (Int)
elapsed_time- Optional : elapsed time in seconds (Int/Float)
Reference: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
"""
filledLength = int(round(barLength * iteration / float(total)))
percents = round(100.00 * (iteration / float(total)), decimals)
bar = '#' * filledLength + '-' * (barLength - filledLength)
if elapsed_time:
sys.stdout.write('%s [%s] %s%s %s %s secs\r' % (prefix, bar, percents, '%', suffix, int(elapsed_time)))
else:
sys.stdout.write('%s [%s] %s%s %s\r' % (prefix, bar, percents, '%', suffix))
sys.stdout.flush()
if iteration == total:
print("\n")
'''
Sample Useage:
for i in range(len(dateList)):
print_progress(i+1,len(dateList))
'''
return
def get_dataNames(FILE):
with h5py.File(FILE, 'r') as f:
dataNames = []
for k0 in f.keys():
dataNames.append(k0)
return dataNames
def get_bounding_box(meta):
"""Get lat/lon range (roughly), in the same order of data file
lat0/lon0 - starting latitude/longitude (first row/column)
lat1/lon1 - ending latitude/longitude (last row/column)
"""
length, width = int(meta['LENGTH']), int(meta['WIDTH'])
if 'Y_FIRST' in meta.keys():
# geo coordinates
lat0 = float(meta['Y_FIRST'])
lon0 = float(meta['X_FIRST'])
lat_step = float(meta['Y_STEP'])
lon_step = float(meta['X_STEP'])
lat1 = lat0 + lat_step * (length - 1)
lon1 = lon0 + lon_step * (width - 1)
else:
# radar coordinates
lats = [float(meta['LAT_REF{}'.format(i)]) for i in [1,2,3,4]]
lons = [float(meta['LON_REF{}'.format(i)]) for i in [1,2,3,4]]
lat0 = np.mean(lats[0:2])
lat1 = np.mean(lats[2:4])
lon0 = np.mean(lons[0:3:2])
lon1 = np.mean(lons[1:4:2])
return lat0, lat1, lon0, lon1
def get_lat_lon(meta):
"""Get 2D array of lat and lon from metadata"""
length, width = int(meta['LENGTH']), int(meta['WIDTH'])
lat0, lat1, lon0, lon1 = get_bounding_box(meta)
if 'Y_FIRST' in meta.keys():
lat, lon = np.mgrid[lat0:lat1:length*1j, lon0:lon1:width*1j]
else:
lat, lon = get_lat_lon_rdc(meta)
return lat, lon
def get_lat_lon_rdc(meta):
"""Get 2D array of lat and lon from metadata"""
length, width = int(meta['LENGTH']), int(meta['WIDTH'])
lats = [float(meta['LAT_REF{}'.format(i)]) for i in [1,2,3,4]]
lons = [float(meta['LON_REF{}'.format(i)]) for i in [1,2,3,4]]
lat = np.zeros((length,width),dtype = np.float32)
lon = np.zeros((length,width),dtype = np.float32)
for i in range(length):
for j in range(width):
lat[i,j] = lats[0] + j*(lats[1] - lats[0])/width + i*(lats[2] - lats[0])/length
lon[i,j] = lons[0] + j*(lons[1] - lons[0])/width + i*(lons[2] - lons[0])/length
return lat, lon
def get_geoid(lat,lon):
row,col = lat.shape
lat = np.asarray(lat)
lon = np.asarray(lon)
lat0 = lat.flatten()
lon0 = lon.flatten()
obj = gh()
geoidH = []
for i in range(len(lat0)):
N0 = gh.get(obj, lat0[i], lon0[i], cubic=True)
geoidH.append(N0)
geoidH = np.asarray(geoidH)
geoidH = geoidH.reshape(row,col)
return geoidH
def get_geoid_point(lat,lon):
obj = gh()
geoidH = []
N0 = gh.get(obj, lat, lon, cubic=True)
return N0
def lla_to_ecef(lat, lon, alt):
ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84')
lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84')
x, y, z = pyproj.transform(lla, ecef, lon, lat, alt, radians=False)
return x, y, z
def ecef_to_lla(X, Y, Z):
ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84')
lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84')
lon, lat, alt = pyproj.transform(ecef, lla, X, Y, Z, radians=False)
return lon, lat, alt
def get_uLOS(SatCoord,GroundLLH):
# GroundLLH = (lat, lon, alt)
lat, lon, alt = GroundLLH
sX,sY,sZ = SatCoord
gX,gY,gZ = lla_to_ecef(lat, lon, alt)
uLOS0 = ((sX - gX),(sY - gY),(sZ - gZ))
dLOS = np.sqrt((sX - gX)**2 + (sY - gY)**2 + (sZ - gZ)**2)
uLOS = uLOS0/dLOS
return uLOS,uLOS0
def latlon2rgaz_coarse(lat0,lon0,attr):
width = int(attr['WIDTH'])
length = int(attr['LENGTH'])
ref_lat1 = float(attr['LAT_REF1'])
ref_lat2 = float(attr['LAT_REF2'])
ref_lat3 = float(attr['LAT_REF3'])
ref_lon1 = float(attr['LON_REF1'])
ref_lon2 = float(attr['LON_REF2'])
ref_lon3 = float(attr['LON_REF3'])
dl_Lat =(ref_lat3 - ref_lat1)/length
dw_Lat = (ref_lat2 - ref_lat1)/width
dl_Lon = (ref_lon3 - ref_lon1)/length
dw_Lon = (ref_lon2 - ref_lon1)/width
A = np.zeros((2,2))
A[0,:] = [dl_Lat,dw_Lat]
A[1,:] = [dl_Lon,dw_Lon]
L = [(lat0 - ref_lat1), (lon0 - ref_lon1)]
L =np.asarray(L)
L = L.reshape((2,1))
kk = np.dot(np.linalg.inv(A),L)
#print(kk)
azimuth0 = round(kk[0,0] + 1)
range0 = round(kk[1,0] + 1)
return range0,azimuth0
def read_txt2array(txt):
A = np.loadtxt(txt,dtype=np.float32)
if np.array(A).size ==1:
A = [A]
if isinstance(A[0],bytes):
A = A.astype(str)
#A = list(A)
return A
def orb_state_lalo_uLOS(lat0,lon0,orb_data,attr):
width = int(attr['WIDTH'])
length = int(attr['LENGTH'])
ref_lat1 = float(attr['LAT_REF1'])
ref_lat2 = float(attr['LAT_REF2'])
ref_lat3 = float(attr['LAT_REF3'])
ref_lon1 = float(attr['LON_REF1'])
ref_lon2 = float(attr['LON_REF2'])
ref_lon3 = float(attr['LON_REF3'])
# orb_state_file from read_orb_state.py
ra0,az0 = latlon2rgaz_coarse(lat0,lon0,attr)
#Orb = read_txt2array(orb_state_file)
t_orb = orb_data[:,0]
X_Orb = orb_data[:,1]
Y_Orb = orb_data[:,2]
Z_Orb = orb_data[:,3]
t_tot = float(attr['END_TIME']) - float(attr['START_TIME'])
t0 = az0/length*t_tot
tck = intp.interp1d(t_orb,X_Orb,kind='cubic',bounds_error =False, fill_value='extrapolate')
X0 = tck(t0) #Again negative for consistency with hx
tck = intp.interp1d(t_orb,Y_Orb,kind='cubic',bounds_error =False, fill_value='extrapolate')
Y0 = tck(t0) #Again negative for consistency with hx
tck = intp.interp1d(t_orb,Z_Orb,kind='cubic',bounds_error =False, fill_value='extrapolate')
Z0 = tck(t0) #Again negative for consistency with hx
SatCoord = (X0,Y0,Z0)
GroundLLH = (lat0,lon0,0)
uLOS,uLOS0 = get_uLOS(SatCoord,GroundLLH)
return uLOS
def get_sar_area(attr):
meta = attr
length, width = int(attr['LENGTH']), int(attr['WIDTH'])
if 'Y_FIRST' in attr.keys():
# geo coordinates
lat0 = float(meta['Y_FIRST'])
lon0 = float(meta['X_FIRST'])
lat_step = float(meta['Y_STEP'])
lon_step = float(meta['X_STEP'])
lat1 = lat0 + lat_step * (length - 1)
lon1 = lon0 + lon_step * (width - 1)
maxlon = max(lon0,lon1)
minlon = min(lon0,lon1)
maxlat = max(lat0,lat1)
minlat = min(lat0,lat1)
else:
ref_lat1 = float(attr['LAT_REF1'])
ref_lat2 = float(attr['LAT_REF2'])
ref_lat3 = float(attr['LAT_REF3'])
ref_lat4 = float(attr['LAT_REF3'])
ref_lon1 = float(attr['LON_REF1'])
ref_lon2 = float(attr['LON_REF2'])
ref_lon3 = float(attr['LON_REF3'])
ref_lon4 = float(attr['LON_REF4'])
maxlon = max((ref_lon1,ref_lon2,ref_lon3,ref_lon4))
minlon = min((ref_lon1,ref_lon2,ref_lon3,ref_lon4))
maxlat = max((ref_lat1,ref_lat2,ref_lat3,ref_lat4))
minlat = min((ref_lat1,ref_lat2,ref_lat3,ref_lat4))
return minlon,maxlon,minlat,maxlat # wesn
def get_hgt_index(hgtlvs,mindem,maxdem):
idxmin = 0
nn = len(hgtlvs)
for i in range(nn-1):
if (hgtlvs[i] < mindem) & (mindem < hgtlvs[i+1]):
minkk = i
if (hgtlvs[i] < maxdem) & (maxdem < hgtlvs[i+1]):
maxkk = i+1