forked from ReedEspinosa/GSFC-GRASP-Python-Interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathORACLES_GRASP.py
More file actions
1571 lines (1255 loc) · 89.2 KB
/
Copy pathORACLES_GRASP.py
File metadata and controls
1571 lines (1255 loc) · 89.2 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
"""
# Greema Regmi, UMBC
# Date: Jan 31, 2023
This code reads Polarimetric data from the Campaigns and runs GRASP. This code was created to Validate the Aerosol retrivals performed using Non Spherical Kernels (Hexahedral from TAMU DUST 2020)
"""
# %load_ext autoreload
# %autoreload 2
# %reload_ext autoreload
# %run -d -b runGRASP.py:LINENUM scriptToRun.py
# %load_ext autoreload
# %autoreload 2
import sys
from CreateRsltsDict import Read_Data_RSP_Oracles
from CreateRsltsDict import Read_Data_HSRL_Oracles
import netCDF4 as nc
from runGRASP import graspDB, graspRun, pixel, graspYAML
from matplotlib import pyplot as plt
import os
if os.uname()[1]=='uranus': plt.switch_backend('agg')
import numpy as np
import datetime as dt
from numpy import nanmean
import h5py
sys.path.append("/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases")
from architectureMap import returnPixel
from Plot_ORACLES import PltGRASPoutput
import yaml
%matplotlib inline
# Path to the Polarimeter data (RSP, In this case)
file_path = "/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03/" #Path to the ORACLE data file
file_name = "/RSP1-P3_L1C-RSPCOL-CollocatedRadiances_20180922T151106Z_V003-20210421T233946Z.h5" #Name of the ORACLES file
#Paths to the Lidar Data
HSRLfile_path = "/home/gregmi/ORACLES/HSRL" #Path to the ORACLE data file
HSRLfile_name = "/HSRL2_P3_20180922_R2.h5" #Name of the ORACLES file
#Path to the gas absorption (tau) values for gas absorption correction
GasAbsFn = '/home/gregmi/ORACLES/UNL_VRTM/shortwave_gas.unlvrtm.nc'
#This is required if we want to configure the HSRL yaml file based on the GRASP output for the RSP
noMod =2 #number of aerosol mode, here 2 for fine+coarse mode configuration
maxr=1.05 #set max and min value : here max = 1% incease, min 1% decrease : this is a very narrow distribution
minr =0.95
a=1 #no of char # this is a varible added to char and modes to avoid char[0]/ mod[0] which doesnt exist
def update_HSRLyaml(YamlFileName, RSP_rslt, noMod, maxr, minr, a, Kernel_type):
#This function creates new yaml with initial conditions updated form microphysical properties of Polarimeter retrievals
# Load the YAML file for HSRL
with open(YamlFileName, 'r') as f:
data = yaml.safe_load(f)
YamlChar =[] #This list stores the name of the charater types in the yaml files
noYmlChar = np.arange(1,7) #No of aerosol characters types in the yaml file (This can be adjusted based on the parameters we want to change)
for i in noYmlChar:
YamlChar.append(data['retrieval']['constraints'][f'characteristic[{i}]']['type'])
# RSP_rslt = np.load('RSP_sph.npy',allow_pickle= True).item()
print(len(YamlChar))
#change the yaml intitial conditions using the RSP GRASP output
for i in range(len(YamlChar)): #loop over the character types in the list
for noMd in range(noMod): #loop over the aerosol modes (i.e 2 for fine and coarse)
# print(noMd,i)
initCond = data['retrieval']['constraints'][f'characteristic[{i+a}]'][f'mode[{noMd+a}]']['initial_guess']
if YamlChar[i] == 'aerosol_concentration':
initCond['value'] = float(RSP_rslt['vol'][noMd]) #value from the GRASP result for RSP
if YamlChar[i] == 'size_distribution_lognormal':
initCond['value'] = float(RSP_rslt['rv'][noMd]),float(RSP_rslt['sigma'][noMd])
initCond['max'] =float(RSP_rslt['rv'][noMd]*maxr),float(RSP_rslt['sigma'][noMd]*maxr)
initCond['min'] =float(RSP_rslt['rv'][noMd]*minr),float(RSP_rslt['sigma'][noMd]*minr)
print("done",YamlChar[i])
if YamlChar[i] == 'real_part_of_refractive_index_spectral_dependent':
initCond['index_of_wavelength_involved'] = [1,2,3]
initCond['value'] =float(RSP_rslt['n'][noMd][0]),float(RSP_rslt['n'][noMd][2]),float(RSP_rslt['n'][noMd][4])
initCond['max'] =float(RSP_rslt['n'][noMd][0]*maxr),float(RSP_rslt['n'][noMd][2]*maxr),float(RSP_rslt['n'][noMd][4]*maxr)
initCond['min'] =float(RSP_rslt['n'][noMd][0]*minr),float(RSP_rslt['n'][noMd][2]*minr),float(RSP_rslt['n'][noMd][4]*minr)
print("done",YamlChar[i])
if YamlChar[i] == 'imaginary_part_of_refractive_index_spectral_dependent':
initCond['index_of_wavelength_involved'] = [1,2,3]
initCond['value'] =float(RSP_rslt['k'][noMd][0]),float(RSP_rslt['k'][noMd][2]),float(RSP_rslt['k'][noMd][4])
initCond['max'] =float(RSP_rslt['k'][noMd][0]*maxr),float(RSP_rslt['k'][noMd][2]*maxr),float(RSP_rslt['k'][noMd][4]*maxr)
initCond['min'] = float(RSP_rslt['k'][noMd][0]*minr),float(RSP_rslt['k'][noMd][2]*minr),float(RSP_rslt['k'][noMd][4]*minr)
print("done",YamlChar[i])
if YamlChar[i] == 'sphere_fraction':
initCond['value'] = float(RSP_rslt['sph'][noMd]/100)
initCond['max'] =float(RSP_rslt['sph'][noMd]/100*maxr) #GARSP output is in %
initCond['min'] =float(RSP_rslt['sph'][noMd]/100*minr)
print("done",YamlChar[i])
if Kernel_type == "sphro":
UpKerFile = 'Settings_Sphd_RSP_HSRL.yaml' #for spheroidal kernel
if Kernel_type == "TAMU":
UpKerFile = 'Settings_TAMU_RSP_HSRL.yaml'#for hexahedral kernel
ymlPath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/'
with open(ymlPath+UpKerFile, 'w') as f: #write the chnages to new yaml file
yaml.safe_dump(data, f)
return
#Find the pixel index for nearest lat and lon for given LatH and LonH
def FindPix(LatH,LonH,Lat,Lon):
# Assuming Lat, latH, Lon, and LonM are all NumPy arrays
diffLat = np.abs(LatH - Lat) # Find the absolute difference between `Lat` and each element in `latH`
indexLat = np.argwhere(diffLat == diffLat.min())[0] # Find the indices of all elements that minimize the difference
diffLon = np.abs(LonH - Lon) # Find the absolute difference between `Lon` and each element in `LonM`
indexLon = np.argwhere(diffLon == diffLon.min())[0] # Find the indices of all elements that minimize the difference
return indexLat[0], indexLat[1]
def find_dust(HSRLfile_path, HSRLfile_name, plot=None):
# Open the HDF5 file in read mode
f1 = h5py.File(HSRLfile_path + HSRLfile_name, 'r+')
# Extract the Aerosol_ID data product
Dust_pix = f1['DataProducts']['Aerosol_ID']
# Create an empty list to store indices of dust pixels for each column
dust_pixel = []
# Loop over the columns in Dust_pix
for i in range(Dust_pix.shape[1]):
# Get the indices where the pixel value is 8 (dust)
dust_pixel.append(np.where(Dust_pix[:, i] == 8)[0])
# Concatenate the arrays along the first axis (rows)
concatenated_array = np.concatenate(dust_pixel, axis=0)
# Flatten the concatenated array to a 1D array
all_dust_pixels = concatenated_array.flatten()
# Find the unique values and their frequency counts in the flattened dust pixel array
unique_values, counts = np.unique(all_dust_pixels, return_counts=True)
# Filter out the dust pixel values where frequency count is less than 100
dust_pix = unique_values[counts > 100]
# Find the dust pixel value(s) with the highest frequency count
max_dust = unique_values[counts == counts.max()]
# If plot is True, create and display plots
if plot == True:
# Plot a bar diagram showing the frequency count of each dust pixel value
plt.figure(figsize=(15,5))
plt.bar(unique_values, counts)
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
# Create a contour plot of the Aerosol_ID data product and plot the dust pixel indices on it
fig, ax = plt.subplots()
c = ax.contourf(f1['DataProducts']['Aerosol_ID'][:].T, cmap='tab20b')
ax.scatter(dust_pix, np.repeat((0), len(dust_pix)), c="k")
plt.colorbar(c)
# Close the HDF5 file
f1.close()
# Return the filtered dust pixel values and the dust pixel value(s) with the highest frequency count
return dust_pix, max_dust
def RSP_Run(Kernel_type,PixNo,ang1,ang2,TelNo,nwl):
krnlPath='/home/shared/GRASP_GSFC/src/retrieval/internal_files'
# Kernel_type = sphro is for the GRASP spheriod kernal, while TAMU is to run with Hexahedral Kernal
if Kernel_type == "sphro":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP_2COARSE.yml'
binPathGRASP ='/home/shared/GRASP_GSFC/build_RSP_v112/bin/grasp_app'
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
if Kernel_type == "TAMU":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP_dust.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP_dust_2Coarse.yml'
binPathGRASP ='/home/shared/GRASP_GSFC/build_HEX_v112/bin/grasp_app' #GRASP Executable
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
#rslt is the GRASP rslt dictionary or contains GRASP Objects
rslt = Read_Data_RSP_Oracles(file_path,file_name,PixNo,ang1,ang2,TelNo, nwl,GasAbsFn)
print(rslt['OBS_hght'])
maxCPU = 3 #maximum CPU allocated to run GRASP on server
gRuns = []
yamlObj = graspYAML(baseYAMLpath=fwdModelYAMLpath)
#eventually have to adjust code for height, this works only for one pixel (single height value)
gRuns.append(graspRun(pathYAML=yamlObj, releaseYAML=True )) # This should copy to new YAML object
pix = pixel()
pix.populateFromRslt(rslt, radianceNoiseFun=None, dataStage='meas', verbose=False)
gRuns[-1].addPix(pix)
gDB = graspDB(graspRunObjs=gRuns, maxCPU=maxCPU)
#rslts contain all the results form the GRASP inverse run
rslts, failPix = gDB.processData(binPathGRASP=binPathGRASP, savePath=None, krnlPathGRASP=krnlPath)
return rslts
#Running the GRASP for spherical or hexahedral shape model for HSRL data
def HSLR_run(Kernel_type,HSRLfile_path,HSRLfile_name,PixNo, updateYaml= None):
#Path to the kernel files
krnlPath='/home/shared/GRASP_GSFC/src/retrieval/internal_files'
if Kernel_type == "sphro": #If spheroid model
#Path to the yaml file for sphreroid model
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_ORACLES.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_ORACLES_2Coarse.yml'
if updateYaml == True: # True if init conditions for Yaml file for HSRL is updated from the GRASP output from RSP
update_HSRLyaml(fwdModelYAMLpath, rslts_Sph[0], noMod, maxr, minr, a,Kernel_type)
fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/Settings_Sphd_RSP_HSRL.yaml'
# binPathGRASP = path toGRASP Executable for spheriod model
binPathGRASP ='/home/shared/GRASP_GSFC/build_RSP_v112/bin/grasp_app'
savePath=f"/home/gregmi/ORACLES/HSRL1_P3_20180922_R03_{Kernel_type}"
if Kernel_type == "TAMU":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_Tamu.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_Tamu_2Coarse.yml'
if updateYaml == True:# True if init conditions for Yaml file for HSRL is updated from the GRASP output from RSP
update_HSRLyaml(fwdModelYAMLpath, rslts_Tamu[0], noMod, maxr, minr, a,Kernel_type)
fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/Settings_TAMU_RSP_HSRL.yaml'
#Path to the GRASP Executable for TAMU
binPathGRASP ='/home/shared/GRASP_GSFC/build_HEX_v112/bin/grasp_app' #GRASP Executable
#Path to save output plot
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
#rslt is the GRASP rslt dictionary or contains GRASP Objects
rslt = Read_Data_HSRL_Oracles(HSRLfile_path,HSRLfile_name,PixNo)
max_alt = rslt['OBS_hght']
print(rslt['OBS_hght'])
maxCPU = 3 #maximum CPU allocated to run GRASP on server
gRuns = []
yamlObj = graspYAML(baseYAMLpath=fwdModelYAMLpath)
#eventually have to adjust code for height, this works only for one pixel (single height value)
gRuns.append(graspRun(pathYAML=yamlObj, releaseYAML=True )) # This should copy to new YAML object
pix = pixel()
pix.populateFromRslt(rslt, radianceNoiseFun=None, dataStage= 'meas', verbose=False)
gRuns[-1].addPix(pix)
gDB = graspDB(graspRunObjs=gRuns, maxCPU=maxCPU)
#rslts contain all the results form the GRASP inverse run
rslts, failPix = gDB.processData(binPathGRASP=binPathGRASP, savePath=None, krnlPathGRASP=krnlPath)
return rslts, max_alt
# height = 200
def LidarAndMAP(Kernel_type,HSRLfile_path,HSRLfile_name,HSRLPixNo,file_path,file_name,RSP_PixNo,ang1,ang2,TelNo, nwl,GasAbsFn, updateYaml= None):
krnlPath='/home/shared/GRASP_GSFC/src/retrieval/internal_files'
if Kernel_type == "sphro": #If spheriod model
#Path to the yaml file for sphriod model
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_LidarAndMAP_V.1.2.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_ORACLES_2Coarse.yml'
if updateYaml == True: # True if init conditions for Yaml file for HSRL is updated from the GRASP output from RSP
update_HSRLyaml(fwdModelYAMLpath, rslts_Sph[0], noMod, maxr, minr, a,Kernel_type)
fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/Settings_Sphd_RSP_HSRL.yaml'
# binPathGRASP = path toGRASP Executable for spheriod model
binPathGRASP ='/home/shared/GRASP_GSFC/build_RSP_v112/bin/grasp_app'
savePath=f"/home/gregmi/ORACLES/HSRL1_P3_20180922_R03_{Kernel_type}"
if Kernel_type == "TAMU":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_LidarAndMAP_V.1.2_TAMU.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_Tamu_2Coarse.yml'
if updateYaml == True:# True if init conditions for Yaml file for HSRL is updated from the GRASP output from RSP
update_HSRLyaml(fwdModelYAMLpath, rslts_Tamu[0], noMod, maxr, minr, a,Kernel_type)
fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/Settings_TAMU_RSP_HSRL.yaml'
#Path to the GRASP Executable for TAMU
binPathGRASP ='/home/shared/GRASP_GSFC/build_HEX_v112/bin/grasp_app' #GRASP Executable
#Path to save output plot
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
# /tmp/tmpn596k7u8$
rslt_HSRL = Read_Data_HSRL_Oracles(HSRLfile_path,HSRLfile_name,HSRLPixNo)
rslt_RSP = Read_Data_RSP_Oracles(file_path,file_name,RSP_PixNo,ang1,ang2,TelNo, nwl,GasAbsFn)
rslt= {} # Teh order of the data is First lidar(number of wl ) and then Polarimter data
rslt['lambda'] = np.concatenate((rslt_HSRL['lambda'],rslt_RSP['lambda']))
#Sort the index of the wavelength if arranged in ascending order, this is required by GRASP
sort = np.argsort(rslt['lambda'])
IndHSRL = rslt_HSRL['lambda'].shape[0]
sort_Lidar, sort_MAP = np.array([0,3,7]),np.array([1,2,4,5,6])
# The shape of the variables in RSPkeys and HSRLkeys should be equal to no of wavelength
# Setting np.nan in place of the measurements for wavelengths for which there is no data
RSPkeys = ['meas_I', 'meas_P','sza', 'vis', 'sca_ang', 'fis']
HSRLkeys = ['RangeLidar','meas_VExt','meas_VBS','meas_DP']
GenKeys= ['datetime','longitude', 'latitude', 'land_prct'] # Shape of these variables is not N wavelength
#MAP measurement variables
RSP_var = np.ones((rslt_RSP['meas_I'].shape[0],rslt['lambda'].shape[0])) * np.nan
for keys in RSPkeys:
#adding values to sort_MAP index positions
for a in range(rslt_RSP[keys][:,0].shape[0]):
RSP_var[a][sort_MAP] = rslt_RSP[keys][a]
rslt[keys] = RSP_var
RSP_var = np.ones((rslt_RSP['meas_I'].shape[0],rslt['lambda'].shape[0])) * np.nan
#Lidar Measurements
HSRL_var = np.ones((rslt_HSRL['meas_VExt'].shape[0],rslt['lambda'].shape[0]))* np.nan
for keys1 in HSRLkeys:
for a in range(rslt_HSRL[keys1][:,0].shape[0]):
HSRL_var[a][sort_Lidar] = rslt_HSRL[keys1][a]
# 'sza', 'vis','fis'
rslt[keys1] = HSRL_var
# Refresh the array by Creating numpy nan array with shape of height x wl, Basically deleting all values
HSRL_var = np.ones((rslt_HSRL['meas_VExt'].shape[0],rslt['lambda'].shape[0]))* np.nan
# rslt['sza'][0][sort_Lidar]= np.ones(IndHSRL)* 0.01
# # rslt['vis'][sort_Lidar] = rslt['RangeLidar'][sort_Lidar]
# for a in range(rslt_HSRL['RangeLidar'][:,0].shape[0]):
# # rslt['sza'][a][sort_Lidar]= np.ones(IndHSRL)* 0.01
# rslt['fis'][a][sort_Lidar]= np.zeros(IndHSRL)
# rslt['vis'][a][sort_Lidar] = rslt['RangeLidar'][a][sort_Lidar]
for keys in GenKeys:
rslt[keys] = rslt_RSP[keys] #Adding the information about lat, lon, datetime and so on from RSP
rslt['OBS_hght'] = rslt_RSP['OBS_hght']+5000 #adding the aircraft altitude
rslt['lambda'] = rslt['lambda'][sort]
# rslt['masl'] = 0 #height of the ground
# print(rslt)
maxCPU = 3 #maximum CPU allocated to run GRASP on server
gRuns = []
yamlObj = graspYAML(baseYAMLpath=fwdModelYAMLpath)
#eventually have to adjust code for height, this works only for one pixel (single height value)
gRuns.append(graspRun(pathYAML=yamlObj, releaseYAML=True )) # This should copy to new YAML object
pix = pixel()
pix.populateFromRslt(rslt, radianceNoiseFun=None, dataStage= 'meas', verbose=False)
gRuns[-1].addPix(pix)
gDB = graspDB(graspRunObjs=gRuns, maxCPU=maxCPU)
#rslts contain all the results form the GRASP inverse run
rslts, failPix = gDB.processData(binPathGRASP=binPathGRASP, savePath=None, krnlPathGRASP=krnlPath)
return rslts
#Plotting the values
for i in range(1):
#Reading the ORACLE data for given pixel no, Tel_no = aggregated altitude
#working pixels: 16800,16813 ,16814
# RSP_PixNo = 2776 #4368 #Clear pixel 8/01 Pixel no of Lat,Lon that we are interested
# RSP_PixNo = 13240
RSP_PixNo = 13200
#Dusty pixel on 9/22
# PixNo = find_dust(file_path,file_name)[1][0]
TelNo = 0 # aggregated altitude. To obtain geometries corresponding to data from the 1880 nm channel, aggregation altitude should be set to 1, while aggregation altitude =0 should be used for all other channels.
nwl = 5 # first nwl wavelengths
ang1 = 20
ang2 = 120 # :ang angles #Remove
# Kernel_type = Run(Kernel_type) for spheriod, Kernel_type = 'TAMU' for hexahedral
rslts_Sph = RSP_Run("sphro",RSP_PixNo,ang1,ang2,TelNo,nwl)
rslts_Tamu = RSP_Run("TAMU",RSP_PixNo,ang1,ang2,TelNo,nwl)
f1_MAP = h5py.File(file_path+file_name,'r+')
Data = f1_MAP['Data']
LatRSP = f1_MAP['Geometry']['Collocated_Latitude'][TelNo,RSP_PixNo]
LonRSP = f1_MAP['Geometry']['Collocated_Longitude'][TelNo,RSP_PixNo]
f1_MAP.close()
f1= h5py.File(HSRLfile_path + HSRLfile_name,'r+') #reading hdf5 file
#Lat and Lon values for that pixel
LatH = f1['Nav_Data']['gps_lat'][:]
LonH = f1['Nav_Data']['gps_lon'][:]
f1.close()
#Get the index of pixel taht corresponds to the RSP Lat Lon
HSRLPixNo = FindPix(LatH,LonH,LatRSP,LonRSP)[0] # Or can manually give the index of the pixel that you are intrested in
# HSRLPixNo = 1154
Retrieval_type = 'NosaltStrictConst_final'
#Running GRASP for HSRL, HSRL_sphrod = for spheriod kernels,HSRL_Tamu = Hexahedral kernels
HSRL_sphrod = HSLR_run("sphro",HSRLfile_path,HSRLfile_name,HSRLPixNo,updateYaml= False)
HSRL_Tamu = HSLR_run("TAMU",HSRLfile_path,HSRLfile_name,HSRLPixNo,updateYaml= False)
LidarPolSph = LidarAndMAP('sphro',HSRLfile_path,HSRLfile_name,HSRLPixNo,file_path,file_name,RSP_PixNo,ang1,ang2,TelNo, nwl,GasAbsFn, updateYaml= None)
LidarPolTAMU = LidarAndMAP('TAMU',HSRLfile_path,HSRLfile_name,HSRLPixNo,file_path,file_name,RSP_PixNo,ang1,ang2,TelNo, nwl,GasAbsFn, updateYaml= None)
print('SPH',"tam" )
print(HSRL_sphrod[0]['aod'],HSRL_Tamu[0]['aod'])
#Plotting the results
PltGRASPoutput(rslts_Sph, rslts_Tamu,file_name,PixNo = RSP_PixNo)
def Plot_HSRL(HSRL_sphrod,LidarPolSph,HSRL_Tamu,LidarPolTAMU):
plt.rcParams['font.size'] = '16'
fig, axs= plt.subplots(nrows = 3, ncols =3, figsize= (18,10))
for i in range(3):
wave = np.str(HSRL_sphrod[0]['lambda'][i]) +"μm \n Range(km)"
axs[i,0].plot(HSRL_sphrod[0]['meas_VBS'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i,0].plot(HSRL_sphrod[0]['fit_VBS'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$",label ="Sphd")
axs[i,0].plot(HSRL_Tamu[0]['fit_VBS'][:,i],HSRL_Tamu[0]['RangeLidar'][:,0]/1000,color = "#d24787",ls = "--", label="Hex", marker = "h")
axs[i,1].plot(HSRL_sphrod[0]['meas_DP'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i,1].plot(HSRL_sphrod[0]['fit_DP'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$")
axs[i,1].plot(HSRL_Tamu[0]['fit_DP'][:,i],HSRL_Tamu[0]['RangeLidar'][:,0]/1000,color = "#d24787", ls = "--",marker = "h")
axs[i,2].plot(HSRL_sphrod[0]['meas_VExt'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i,2].plot(HSRL_sphrod[0]['fit_VExt'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$")
axs[i,2].plot(HSRL_Tamu[0]['fit_VExt'][:,i],HSRL_Tamu[0]['RangeLidar']/1000,color = "#d24787",ls = "--", marker = "h")
axs[0,0].set_title('VBS')
axs[i,0].set_xlabel('VBS')
axs[i,0].set_ylabel(wave)
if i ==0:
axs[0,0].legend()
axs[i,1].plot(LidarPolSph[0]['meas_DP'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i,1].plot(LidarPolSph[0]['fit_DP'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$")
axs[0,1].set_title(f'DP')
axs[i,1].set_xlabel('DP')
# axs[i,1].set_ylabel('Range (km)')
axs[i,2].plot(LidarPolSph[0]['meas_VExt'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i,2].plot(LidarPolSph[0]['fit_VExt'][:,i],HSRL_sphrod[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$")
axs[0,2].set_title('VExt')
axs[i,2].set_xlabel('VExt')
# axs[i,2].set_ylabel('Range (km)')
# axs[i,0].plot(HSRL_Tamu[0]['meas_VBS'][:,i],HSRL_Tamu[0]['RangeLidar'][:,0]/1000, ".b", label ="Meas TAMU")
axs[i,0].plot(LidarPolTAMU[0]['fit_VBS'][:,i],HSRL_Tamu[0]['RangeLidar'][:,0]/1000,color = "#d24787",ls = "--", label="Hex", marker = "h")
if i ==0:
axs[0,0].legend()
# axs[i,1].plot(HSRL_Tamu[0]['meas_DP'][:,i],HSRL_Tamu[0]['RangeLidar'][:,0]/1000, ".b", label ="Meas")
axs[i,1].plot(LidarPolTAMU[0]['fit_DP'][:,i],HSRL_Tamu[0]['RangeLidar'][:,0]/1000,color = "#d24787", ls = "--",marker = "h")
# axs[i,2].plot(HSRL_Tamu[0]['meas_VExt'][:,i],HSRL_Tamu[0]['RangeLidar'], ".b", label ="Meas")
axs[i,2].plot(LidarPolTAMU[0]['fit_VExt'][:,i],HSRL_Tamu[0]['RangeLidar']/1000,color = "#d24787",ls = "--", marker = "h")
plt.suptitle(f"Lat: {HSRL_Tamu[0]['latitude']},Lon:{HSRL_Tamu[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
fig.savefig(f'/home/gregmi/ORACLES/HSRL_RSP/HSRL_{HSRLPixNo}_{RSP_PixNo}_{Retrieval_type}.png',dpi = 300)
fig, axs = plt.subplots()
axs.plot(HSRL_sphrod[0]['meas_DP'][:,2],HSRL_sphrod[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
axs.plot(HSRL_sphrod[0]['fit_DP'][:,2],HSRL_sphrod[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$")
axs.plot(HSRL_Tamu[0]['fit_DP'][:,2],HSRL_Tamu[0]['RangeLidar'][:,0]/1000,color = "#d24787", ls = "--",marker = "h")
axs.set_xlabel('DP')
# plt.suptitle(f" Initial conditions strictly\n constrainted by RSP retrievals ") #Initial condition constrainted by RSP retrievals
fig.savefig(f'/home/gregmi/ORACLES/HSRL_RSP/HSRLProfile_{HSRLPixNo}_{RSP_PixNo}_{Retrieval_type}.png',dpi = 300)
# fig2= plt.plot()
# plt.plot(rslts_Sph[0]['lambda'],rslts_Sph[0]['aod'],label = "RSP Sphd")
# plt.plot(rslts_Tamu[0]['lambda'],rslts_Tamu[0]['aod'],label = "RSP Tamu")
# plt.plot(HSRL_sphrod[0]['lambda'],HSRL_sphrod[0]['aod'],label = "HSRL Sphd")
# plt.plot(HSRL_Tamu[0]['lambda'],HSRL_Tamu[0]['aod'],label = "HSRL TAMU")
Spheriod = HSRL_sphrod[0]
Hex= HSRL_Tamu[0]
#Stokes Vectors Plot
date_latlon = ['datetime', 'longitude', 'latitude']
Xaxis = ['r','lambda','sca_ang','rv','height']
Retrival = ['dVdlnr','aodMode','ssaMode','n', 'k']
#['sigma', 'vol', 'aodMode','ssaMode', 'rEff', 'costVal']
Angles = ['sza', 'vis', 'fis','angle' ]
Stokes = ['meas_I', 'fit_I', 'meas_PoI', 'fit_PoI']
Pij = ['p11', 'p12', 'p22', 'p33'],
Lidar= ['heightStd','g','LidarRatio','LidarDepol', 'gMode', 'LidarRatioMode', 'LidarDepolMode']
# Plot the AOD data
y = [0,1,2,0,1,2,]
x = np.repeat((0,1),3)
mode_v = ["fine", "coarse"]
linestyle =[':', '-']
cm_sp = ['#008080',"#C1E1C1" ]
cm_t = ['#900C3F',"#FF5733" ]
color_sph = '#0c7683'
color_tamu = "#BC106F"
#Retrivals:
fig, axs = plt.subplots(nrows= 5, ncols=1, figsize=(7, 30))
for i in range(len(Retrival)):
for mode in range(Spheriod['r'].shape[0]): #for each modes
if i ==0:
axs[i].plot(Spheriod['r'][mode], Spheriod[Retrival[i]][mode], marker = "$O$",color = cm_sp[mode],ls = linestyle[mode], label=f"Sphrod_{mode_v[mode]}")
axs[i].plot(Hex['r'][mode],Hex[Retrival[i]][mode], marker = "H", color = cm_t[mode] , ls = linestyle[mode],label=f"Hex_{mode_v[mode]}")
axs[i].set_xlabel('Radius')
axs[i].set_xscale("log")
else:
axs[i].plot(Spheriod['lambda'], Spheriod[Retrival[i]][mode], marker = "$O$",color = cm_sp[mode],ls = linestyle[mode], label=f"Sphrod_{mode_v[mode]}")
axs[i].plot(Hex['lambda'],Hex[Retrival[i]][mode], marker = "H",color = cm_t[mode] , ls = linestyle[mode],label=f"Hex_{mode_v[mode]}")
axs[i].set_xticks(Spheriod['lambda'])
axs[i].set_xticklabels(['0.355', '0.532', '1.064'])
axs[i].xaxis.set_tick_params(labelbottom=False)
axs[4].xaxis.set_tick_params(labelbottom=True)
axs[i].set_ylabel(f'{Retrival[i]}')
axs[4].set_xlabel(r'$\lambda$')
axs[0].legend()
lat_t = Hex['latitude']
lon_t = Hex['longitude']
dt_t = Hex['datetime']
plt.suptitle(f'HSRL Retrievals\n Lat:{lat_t} Lon :{lon_t}\n Date: {dt_t} Pixel:{HSRLPixNo} \n Initial condition strictly constrainted by RSP retrievals ') #\n
fig.savefig(f'/home/gregmi/ORACLES/HSRL_RSP/microph_{HSRLPixNo}_{Retrieval_type}.png',dpi = 300)
print('HSRL: sph tamu; RSP: sph, tamu' )
print(HSRL_sphrod[0]['costVal'],HSRL_Tamu[0]['costVal'], rslts_Sph[0]['costVal'],rslts_Tamu[0]['costVal'])
return
# def PlotOutput_Separate():
# altd = (HSRL_sphrod[1][0]-HSRL_sphrod[0][0]['RangeLidar'][:,0])/1000
# HSRL_sphrod = HSRL_sphrod[0]
# HSRL_Tamu =HSRL_Tamu[0]
# fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
# for i in range(3):
# wave = np.str(HSRL_sphrod[0]['lambda'][i]) +"μm"
# axs[i].plot(HSRL_sphrod[0]['meas_VBS'][:,i],altd, marker =">",color = "#3B270C", label ="Meas")
# axs[i].plot(HSRL_sphrod[0]['fit_VBS'][:,i],altd,color = "#025043", marker = "$O$",label ="Sphd")
# axs[i].plot(HSRL_Tamu[0]['fit_VBS'][:,i],altd,color = "#d24787",ls = "--", label="Hex", marker = "h")
# axs[i].set_xlabel('VBS')
# axs[i].set_title(wave)
# if i ==0:
# axs[0].legend()
# plt.suptitle(f"Lat: {HSRL_Tamu[0]['latitude']},Lon:{HSRL_Tamu[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
# fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
# for i in range(3):
# axs[i].plot(HSRL_sphrod[0]['meas_DP'][:,i],altd, marker =">",color = "#3B270C", label ="Meas")
# axs[i].plot(HSRL_sphrod[0]['fit_DP'][:,i],altd,color = "#025043", marker = "$O$",label ="Sphd")
# axs[i].plot(HSRL_Tamu[0]['fit_DP'][:,i],altd,color = "#d24787", ls = "--",marker = "h")
# axs[i].set_xlabel('DP %')
# axs[i].set_title(wave)
# if i ==0:
# axs[0].legend()
# plt.suptitle(f"Lat: {HSRL_Tamu[0]['latitude']},Lon:{HSRL_Tamu[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
# fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
# for i in range(3):
# axs[i].plot(HSRL_sphrod[0]['meas_VExt'][:,i],altd, marker =">",color = "#3B270C", label ="Meas")
# axs[i].plot(HSRL_sphrod[0]['fit_VExt'][:,i],altd,color = "#025043", marker = "$O$",label ="Sphd")
# axs[i].plot(HSRL_Tamu[0]['fit_VExt'][:,i],altd,color = "#d24787",ls = "--", marker = "h")
# axs[i].set_xlabel('VExt')
# axs[i].set_title(wave)
# if i ==0:
# axs[0].legend()
# plt.suptitle(f"Lat: {HSRL_Tamu[0]['latitude']},Lon:{HSRL_Tamu[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
# fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
# IndexH = [0,3,7]
# for i in range(3):
# wave = np.str(LidarPolSph[0]['lambda'][IndexH[i]]) +"μm"
# axs[i].plot(LidarPolSph[0]['meas_VBS'][:,IndexH[i]],LidarPolSph[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
# axs[i].plot(LidarPolSph[0]['fit_VBS'][:,IndexH[i]],LidarPolSph[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$",label ="Sphd")
# axs[i].plot(LidarPolTAMU[0]['fit_VBS'][:,IndexH[i]],LidarPolTAMU[0]['RangeLidar'][:,0]/1000,color = "#d24787",ls = "--", label="Hex", marker = "h")
# axs[i].set_xlabel('VBS')
# axs[i].set_title(wave)
# if i ==0:
# axs[0].legend()
# plt.suptitle(f"Lat: {HSRL_Tamu[0]['latitude']},Lon:{HSRL_Tamu[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
# fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
# for i in range(3):
# axs[i].plot(LidarPolSph[0]['meas_DP'][:,IndexH[i]],LidarPolSph[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
# axs[i].plot(LidarPolSph[0]['fit_DP'][:,IndexH[i]],LidarPolSph[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$",label ="Sphd")
# axs[i].plot(LidarPolTAMU[0]['fit_DP'][:,IndexH[i]],LidarPolTAMU[0]['RangeLidar'][:,0]/1000,color = "#d24787", ls = "--",marker = "h", label="Hex")
# axs[i].set_xlabel('DP %')
# axs[i].set_title(wave)
# if i ==0:
# axs[0].legend()
# plt.suptitle(f"Lat: {LidarPolTAMU[0]['latitude']},Lon:{LidarPolTAMU[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n Lidar+polarimeter Retrievals ") #Initial condition strictly constrainted by RSP retrievals
# fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
# for i in range(3):
# axs[i].plot(LidarPolSph[0]['meas_VExt'][:,IndexH[i]],LidarPolSph[0]['RangeLidar'][:,0]/1000, marker =">",color = "#3B270C", label ="Meas")
# axs[i].plot(LidarPolSph[0]['fit_VExt'][:,IndexH[i]],LidarPolSph[0]['RangeLidar'][:,0]/1000,color = "#025043", marker = "$O$",label ="Sphd")
# axs[i].plot(LidarPolTAMU[0]['fit_VExt'][:,IndexH[i]],LidarPolTAMU[0]['RangeLidar']/1000,color = "#d24787",ls = "--", marker = "h", label="Hex")
# axs[i].set_xlabel('VExt')
# axs[i].set_title(wave)
# if i ==0:
# axs[0].legend()
# plt.suptitle(f"Lat: {LidarPolTAMU[0]['latitude']},Lon:{LidarPolTAMU[0]['longitude']} Date: {HSRL_Tamu[0]['datetime']}\n Lidar+polarimeter Retrievals ") #Initial condition strictly constrainted by RSP retrievals
def PlotOutput_Separate (HSRL_sphrod, HSRL_Tamu,LidarPolSph,LidarPolTAMU ):
plt.rcParams['font.size'] = '18'
Hsph = HSRL_sphrod[0][0]
HTam =HSRL_Tamu[0][0]
#Converting range to altitude
altd = (HSRL_sphrod[1][0]-Hsph['RangeLidar'][:,0])/1000 #altitude for spheriod
altT = (HSRL_sphrod[1][0]-Hsph['RangeLidar'][:,0])/1000 #altitude for hexahedra
fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
for i in range(3):
wave = np.str(Hsph['lambda'][i]) +"μm"
axs[i].plot(Hsph['meas_VBS'][:,i],altd, marker =">",color = "#3B270C", label ="Meas")
axs[i].plot(Hsph['fit_VBS'][:,i],altd,color = "#025043", marker = "$O$",label ="Sphd")
axs[i].plot(HTam['fit_VBS'][:,i],altd,color = "#d24787",ls = "--", label="Hex", marker = "h")
axs[i].set_xlabel('VBS')
axs[i].set_title(wave)
if i ==0:
axs[0].legend()
plt.suptitle(f"Lat: {HTam['latitude']},Lon:{HTam['longitude']} Date: {HTam['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
for i in range(3):
axs[i].plot(Hsph['meas_DP'][:,i],altd, marker =">",color = "#3B270C", label ="Meas")
axs[i].plot(Hsph['fit_DP'][:,i],altd,color = "#025043", marker = "$O$",label ="Sphd")
axs[i].plot(HTam['fit_DP'][:,i],altd,color = "#d24787", ls = "--",marker = "h")
axs[i].set_xlabel('DP %')
axs[i].set_title(wave)
if i ==0:
axs[0].legend()
plt.suptitle(f"Lat: {HTam['latitude']},Lon:{HTam['longitude']} Date: {HTam['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
for i in range(3):
axs[i].plot(Hsph['meas_VExt'][:,i],altd, marker =">",color = "#3B270C", label ="Meas")
axs[i].plot(Hsph['fit_VExt'][:,i],altd,color = "#025043", marker = "$O$",label ="Sphd")
axs[i].plot(HTam['fit_VExt'][:,i],altd,color = "#d24787",ls = "--", marker = "h")
axs[i].set_xlabel('VExt')
axs[i].set_title(wave)
if i ==0:
axs[0].legend()
plt.suptitle(f"Lat: {HTam['latitude']},Lon:{HTam['longitude']} Date: {HTam['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
Spheriod = rslts_Sph
Hex= rslts_Tamu
fig, axs= plt.subplots(nrows = 1, ncols =2, figsize= (18,6))
for i in range(5):
axs[0].plot(Spheriod[0]['sca_ang'][:,i],Spheriod[0]['meas_I'][:,i],marker =">",color = "#3B270C", label ="Meas")
axs[0].plot(Spheriod[0]['sca_ang'][:,i],Spheriod[0]['fit_I'][:,i],color = "#025043", marker = "$O$",label ="Sphd")
axs[0].plot(Hex[0]['sca_ang'][:,i],Hex[0]['fit_I'][:,i],color = "#d24787",ls = "--", marker = "h", label="Hex")
axs[0].set_ylabel('I')
axs[0].set_xlabel('Scattering angles')
plt.suptitle(f"Wl: {Spheriod[0]['lambda'][i]}, Lat: {Spheriod[0]['latitude']},Lon:{Spheriod[0]['longitude']} Date: {Spheriod[0]['datetime']}\n RSP Retrievals ") #Initial condition strictly constrainted by RSP retrievals
axs[1].plot(Spheriod[0]['sca_ang'][:,i],Spheriod[0]['meas_P_rel'][:,i], marker =">",color = "#3B270C", label ="Meas")
axs[1].plot(Spheriod[0]['sca_ang'][:,i],Spheriod[0]['fit_P_rel'][:,i],color = "#025043", marker = "$O$",label ="Sphd")
axs[1].plot(Hex[0]['sca_ang'][:,i],Hex[0]['fit_P_rel'][:,i],color = "#d24787",ls = "--", marker = "h", label="Hex")
axs[1].set_ylabel('P/I')
axs[1].set_xlabel('Scattering angles')
axs[0].legend()
# axs[1].set_title()
fig, axs= plt.subplots(nrows = 1, ncols =2, figsize= (18,6))
fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
IndexH = [0,3,7]
for i in range(3):
wave = np.str(LidarPolSph[0]['lambda'][IndexH[i]]) +"μm"
axs[i].plot(LidarPolSph[0]['meas_VBS'][:,IndexH[i]],(HSRL_sphrod[1][0]-LidarPolSph[0]['RangeLidar'][:,0])/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i].plot(LidarPolSph[0]['fit_VBS'][:,IndexH[i]],(HSRL_sphrod[1][0]-LidarPolSph[0]['RangeLidar'][:,0])/1000,color = "#025043", marker = "$O$",label ="Sphd")
axs[i].plot(LidarPolTAMU[0]['fit_VBS'][:,IndexH[i]],altd,color = "#d24787",ls = "--", label="Hex", marker = "h")
axs[i].set_xlabel('VBS')
axs[i].set_title(wave)
if i ==0:
axs[0].legend()
plt.suptitle(f"Lat: {HTam['latitude']},Lon:{HTam['longitude']} Date: {HTam['datetime']}\n ") #Initial condition strictly constrainted by RSP retrievals
fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
for i in range(3):
axs[i].plot(LidarPolSph[0]['meas_DP'][:,IndexH[i]],(HSRL_sphrod[1][0]-LidarPolSph[0]['RangeLidar'][:,0])/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i].plot(LidarPolSph[0]['fit_DP'][:,IndexH[i]],(HSRL_sphrod[1][0]-LidarPolSph[0]['RangeLidar'][:,0])/1000,color = "#025043", marker = "$O$",label ="Sphd")
axs[i].plot(LidarPolTAMU[0]['fit_DP'][:,IndexH[i]],altd,color = "#d24787", ls = "--",marker = "h", label="Hex")
axs[i].set_xlabel('DP %')
axs[i].set_title(wave)
if i ==0:
axs[0].legend()
plt.suptitle(f"Lat: {LidarPolTAMU[0]['latitude']},Lon:{LidarPolTAMU[0]['longitude']} Date: {HTam['datetime']}\n Lidar+polarimeter Retrievals ") #Initial condition strictly constrainted by RSP retrievals
fig, axs= plt.subplots(nrows = 1, ncols =3, figsize= (18,6))
for i in range(3):
axs[i].plot(LidarPolSph[0]['meas_VExt'][:,IndexH[i]],(HSRL_sphrod[1][0]-LidarPolSph[0]['RangeLidar'][:,0])/1000, marker =">",color = "#3B270C", label ="Meas")
axs[i].plot(LidarPolSph[0]['fit_VExt'][:,IndexH[i]],(HSRL_sphrod[1][0]-LidarPolSph[0]['RangeLidar'][:,0])/1000,color = "#025043", marker = "$O$",label ="Sphd")
axs[i].plot(LidarPolTAMU[0]['fit_VExt'][:,IndexH[i]],altd,color = "#d24787",ls = "--", marker = "h", label="Hex")
axs[i].set_xlabel('VExt')
axs[i].set_title(wave)
if i ==0:
axs[0].legend()
plt.suptitle(f"Lat: {LidarPolTAMU[0]['latitude']},Lon:{LidarPolTAMU[0]['longitude']} Date: {HTam['datetime']}\n Lidar+polarimeter Retrievals ") #Initial condition strictly constrainted by RSP retrievals
fig, axs= plt.subplots(nrows = 1, ncols =2, figsize= (18,6))
for i in [1,2,4,5,6]:
axs[0].plot(LidarPolSph[0]['sca_ang'][:,i], LidarPolSph[0]['meas_I'][:,i],marker =">",color = "#3B270C", label ="Meas")
axs[0].plot(LidarPolSph[0]['sca_ang'][:,i],LidarPolSph[0]['fit_I'][:,i],color = "#025043", marker = "$O$",label ="Sphd")
axs[0].plot(LidarPolSph[0]['sca_ang'][:,i],LidarPolTAMU[0]['fit_I'][:,i],color = "#d24787",ls = "--", marker = "h", label="Hex")
axs[0].set_ylabel('I')
axs[0].set_xlabel('Scattering angles')
plt.suptitle(f"Wl: {LidarPolSph[0]['lambda'][i]}, Lat: {LidarPolTAMU[0]['latitude']},Lon:{LidarPolTAMU[0]['longitude']} Date: {HTam['datetime']}\n Lidar+polarimeter Retrievals ") #Initial condition strictly constrainted by RSP retrievals
axs[1].plot(LidarPolSph[0]['sca_ang'][:,i],LidarPolSph[0]['meas_P_rel'][:,i], marker =">",color = "#3B270C", label ="Meas")
axs[1].plot(LidarPolSph[0]['sca_ang'][:,i],LidarPolSph[0]['fit_P_rel'][:,i],color = "#025043", marker = "$O$",label ="Sphd")
axs[1].plot(LidarPolSph[0]['sca_ang'][:,i],LidarPolTAMU[0]['fit_P_rel'][:,i],color = "#d24787",ls = "--", marker = "h", label="Hex")
axs[1].set_ylabel('P/I')
axs[1].set_xlabel('Scattering angles')
axs[0].legend()
# axs[1].set_title()
fig, axs= plt.subplots(nrows = 1, ncols =2, figsize= (18,6))
"""
# Greema Regmi, UMBC
# Date: Jan 31, 2023
This code reads Polarimetric data from the Campaigns and runs GRASP. This code was created to Validate the Aerosol retrivals performed using Non Spherical Kernels (Hexahedral from TAMU DUST 2020)
"""
# %load_ext autoreload
# %autoreload 2
# %reload_ext autoreload
# %run -d -b runGRASP.py:LINENUM scriptToRun.py
# %load_ext autoreload
# %autoreload 2
import sys
from CreateRsltsDict import Read_Data_RSP_Oracles
from CreateRsltsDict import Read_Data_HSRL_Oracles
import netCDF4 as nc
from runGRASP import graspDB, graspRun, pixel, graspYAML
from matplotlib import pyplot as plt
import os
if os.uname()[1]=='uranus': plt.switch_backend('agg')
import numpy as np
import datetime as dt
from numpy import nanmean
import h5py
sys.path.append("/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases")
from architectureMap import returnPixel
from Plot_ORACLES import PltGRASPoutput
import yaml
%matplotlib inline
# Path to the Polarimeter data (RSP, In this case)
file_path = "/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03/" #Path to the ORACLE data file
file_name = "/RSP1-P3_L1C-RSPCOL-CollocatedRadiances_20180922T151106Z_V003-20210421T233946Z.h5" #Name of the ORACLES file
#Paths to the Lidar Data
HSRLfile_path = "/home/gregmi/ORACLES/HSRL" #Path to the ORACLE data file
HSRLfile_name = "/HSRL2_P3_20180922_R2.h5" #Name of the ORACLES file
#Path to the gas absorption (tau) values for gas absorption correction
GasAbsFn = '/home/gregmi/ORACLES/UNL_VRTM/shortwave_gas.unlvrtm.nc'
#This is required if we want to configure the HSRL yaml file based on the GRASP output for the RSP
noMod =2 #number of aerosol mode, here 2 for fine+coarse mode configuration
maxr=1.05 #set max and min value : here max = 1% incease, min 1% decrease : this is a very narrow distribution
minr =0.95
a=1 #no of char # this is a varible added to char and modes to avoid char[0]/ mod[0] which doesnt exist
def update_HSRLyaml(YamlFileName, RSP_rslt, noMod, maxr, minr, a, Kernel_type):
#This function creates new yaml with initial conditions updated form microphysical properties of Polarimeter retrievals
# Load the YAML file for HSRL
with open(YamlFileName, 'r') as f:
data = yaml.safe_load(f)
YamlChar =[] #This list stores the name of the charater types in the yaml files
noYmlChar = np.arange(1,7) #No of aerosol characters types in the yaml file (This can be adjusted based on the parameters we want to change)
for i in noYmlChar:
YamlChar.append(data['retrieval']['constraints'][f'characteristic[{i}]']['type'])
# RSP_rslt = np.load('RSP_sph.npy',allow_pickle= True).item()
print(len(YamlChar))
#change the yaml intitial conditions using the RSP GRASP output
for i in range(len(YamlChar)): #loop over the character types in the list
for noMd in range(noMod): #loop over the aerosol modes (i.e 2 for fine and coarse)
# print(noMd,i)
initCond = data['retrieval']['constraints'][f'characteristic[{i+a}]'][f'mode[{noMd+a}]']['initial_guess']
if YamlChar[i] == 'aerosol_concentration':
initCond['value'] = float(RSP_rslt['vol'][noMd]) #value from the GRASP result for RSP
if YamlChar[i] == 'size_distribution_lognormal':
initCond['value'] = float(RSP_rslt['rv'][noMd]),float(RSP_rslt['sigma'][noMd])
initCond['max'] =float(RSP_rslt['rv'][noMd]*maxr),float(RSP_rslt['sigma'][noMd]*maxr)
initCond['min'] =float(RSP_rslt['rv'][noMd]*minr),float(RSP_rslt['sigma'][noMd]*minr)
print("done",YamlChar[i])
if YamlChar[i] == 'real_part_of_refractive_index_spectral_dependent':
initCond['index_of_wavelength_involved'] = [1,2,3]
initCond['value'] =float(RSP_rslt['n'][noMd][0]),float(RSP_rslt['n'][noMd][2]),float(RSP_rslt['n'][noMd][4])
initCond['max'] =float(RSP_rslt['n'][noMd][0]*maxr),float(RSP_rslt['n'][noMd][2]*maxr),float(RSP_rslt['n'][noMd][4]*maxr)
initCond['min'] =float(RSP_rslt['n'][noMd][0]*minr),float(RSP_rslt['n'][noMd][2]*minr),float(RSP_rslt['n'][noMd][4]*minr)
print("done",YamlChar[i])
if YamlChar[i] == 'imaginary_part_of_refractive_index_spectral_dependent':
initCond['index_of_wavelength_involved'] = [1,2,3]
initCond['value'] =float(RSP_rslt['k'][noMd][0]),float(RSP_rslt['k'][noMd][2]),float(RSP_rslt['k'][noMd][4])
initCond['max'] =float(RSP_rslt['k'][noMd][0]*maxr),float(RSP_rslt['k'][noMd][2]*maxr),float(RSP_rslt['k'][noMd][4]*maxr)
initCond['min'] = float(RSP_rslt['k'][noMd][0]*minr),float(RSP_rslt['k'][noMd][2]*minr),float(RSP_rslt['k'][noMd][4]*minr)
print("done",YamlChar[i])
if YamlChar[i] == 'sphere_fraction':
initCond['value'] = float(RSP_rslt['sph'][noMd]/100)
initCond['max'] =float(RSP_rslt['sph'][noMd]/100*maxr) #GARSP output is in %
initCond['min'] =float(RSP_rslt['sph'][noMd]/100*minr)
print("done",YamlChar[i])
if Kernel_type == "sphro":
UpKerFile = 'Settings_Sphd_RSP_HSRL.yaml' #for spheroidal kernel
if Kernel_type == "TAMU":
UpKerFile = 'Settings_TAMU_RSP_HSRL.yaml'#for hexahedral kernel
ymlPath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/'
with open(ymlPath+UpKerFile, 'w') as f: #write the chnages to new yaml file
yaml.safe_dump(data, f)
return
#Find the pixel index for nearest lat and lon for given LatH and LonH
def FindPix(LatH,LonH,Lat,Lon):
# Assuming Lat, latH, Lon, and LonM are all NumPy arrays
diffLat = np.abs(LatH - Lat) # Find the absolute difference between `Lat` and each element in `latH`
indexLat = np.argwhere(diffLat == diffLat.min())[0] # Find the indices of all elements that minimize the difference
diffLon = np.abs(LonH - Lon) # Find the absolute difference between `Lon` and each element in `LonM`
indexLon = np.argwhere(diffLon == diffLon.min())[0] # Find the indices of all elements that minimize the difference
return indexLat[0], indexLat[1]
def find_dust(HSRLfile_path, HSRLfile_name, plot=None):
# Open the HDF5 file in read mode
f1 = h5py.File(HSRLfile_path + HSRLfile_name, 'r+')
# Extract the Aerosol_ID data product
Dust_pix = f1['DataProducts']['Aerosol_ID']
# Create an empty list to store indices of dust pixels for each column
dust_pixel = []
# Loop over the columns in Dust_pix
for i in range(Dust_pix.shape[1]):
# Get the indices where the pixel value is 8 (dust)
dust_pixel.append(np.where(Dust_pix[:, i] == 8)[0])
# Concatenate the arrays along the first axis (rows)
concatenated_array = np.concatenate(dust_pixel, axis=0)
# Flatten the concatenated array to a 1D array
all_dust_pixels = concatenated_array.flatten()
# Find the unique values and their frequency counts in the flattened dust pixel array
unique_values, counts = np.unique(all_dust_pixels, return_counts=True)
# Filter out the dust pixel values where frequency count is less than 100
dust_pix = unique_values[counts > 100]
# Find the dust pixel value(s) with the highest frequency count
max_dust = unique_values[counts == counts.max()]
# If plot is True, create and display plots
if plot == True:
# Plot a bar diagram showing the frequency count of each dust pixel value
plt.figure(figsize=(15,5))
plt.bar(unique_values, counts)
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.show()
# Create a contour plot of the Aerosol_ID data product and plot the dust pixel indices on it
fig, ax = plt.subplots()
c = ax.contourf(f1['DataProducts']['Aerosol_ID'][:].T, cmap='tab20b')
ax.scatter(dust_pix, np.repeat((0), len(dust_pix)), c="k")
plt.colorbar(c)
# Close the HDF5 file
f1.close()
# Return the filtered dust pixel values and the dust pixel value(s) with the highest frequency count
return dust_pix, max_dust
def RSP_Run(Kernel_type,PixNo,ang1,ang2,TelNo,nwl):
krnlPath='/home/shared/GRASP_GSFC/src/retrieval/internal_files'
# Kernel_type = sphro is for the GRASP spheriod kernal, while TAMU is to run with Hexahedral Kernal
if Kernel_type == "sphro":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP_2COARSE.yml'
binPathGRASP ='/home/shared/GRASP_GSFC/build_RSP_v112/bin/grasp_app'
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
if Kernel_type == "TAMU":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP_dust.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLAR_2modes_Shape_ORACLE_DoLP_dust_2Coarse.yml'
binPathGRASP ='/home/shared/GRASP_GSFC/build_HEX_v112/bin/grasp_app' #GRASP Executable
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
#rslt is the GRASP rslt dictionary or contains GRASP Objects
rslt = Read_Data_RSP_Oracles(file_path,file_name,PixNo,ang1,ang2,TelNo, nwl,GasAbsFn)
print(rslt['OBS_hght'])
maxCPU = 3 #maximum CPU allocated to run GRASP on server
gRuns = []
yamlObj = graspYAML(baseYAMLpath=fwdModelYAMLpath)
#eventually have to adjust code for height, this works only for one pixel (single height value)
gRuns.append(graspRun(pathYAML=yamlObj, releaseYAML=True )) # This should copy to new YAML object
pix = pixel()
pix.populateFromRslt(rslt, radianceNoiseFun=None, dataStage='meas', verbose=False)
gRuns[-1].addPix(pix)
gDB = graspDB(graspRunObjs=gRuns, maxCPU=maxCPU)
#rslts contain all the results form the GRASP inverse run
rslts, failPix = gDB.processData(binPathGRASP=binPathGRASP, savePath=None, krnlPathGRASP=krnlPath)
return rslts
#Running the GRASP for spherical or hexahedral shape model for HSRL data
def HSLR_run(Kernel_type,HSRLfile_path,HSRLfile_name,PixNo, updateYaml= None):
#Path to the kernel files
krnlPath='/home/shared/GRASP_GSFC/src/retrieval/internal_files'
if Kernel_type == "sphro": #If spheroid model
#Path to the yaml file for sphreroid model
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_ORACLES.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_ORACLES_2Coarse.yml'
if updateYaml == True: # True if init conditions for Yaml file for HSRL is updated from the GRASP output from RSP
update_HSRLyaml(fwdModelYAMLpath, rslts_Sph[0], noMod, maxr, minr, a,Kernel_type)
fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/Settings_Sphd_RSP_HSRL.yaml'
# binPathGRASP = path toGRASP Executable for spheriod model
binPathGRASP ='/home/shared/GRASP_GSFC/build_RSP_v112/bin/grasp_app'
savePath=f"/home/gregmi/ORACLES/HSRL1_P3_20180922_R03_{Kernel_type}"
if Kernel_type == "TAMU":
fwdModelYAMLpath = '/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_Tamu.yml'
# fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/settings_BCK_POLARandLIDAR_10Vbins_2modes_Tamu_2Coarse.yml'
if updateYaml == True:# True if init conditions for Yaml file for HSRL is updated from the GRASP output from RSP
update_HSRLyaml(fwdModelYAMLpath, rslts_Tamu[0], noMod, maxr, minr, a,Kernel_type)
fwdModelYAMLpath ='/home/gregmi/git/GSFC-Retrieval-Simulators/ACCP_ArchitectureAndCanonicalCases/Settings_TAMU_RSP_HSRL.yaml'
#Path to the GRASP Executable for TAMU
binPathGRASP ='/home/shared/GRASP_GSFC/build_HEX_v112/bin/grasp_app' #GRASP Executable
#Path to save output plot
savePath=f"/home/gregmi/ORACLES/RSP1-L1C_P3_20180922_R03_{Kernel_type}"
#rslt is the GRASP rslt dictionary or contains GRASP Objects
rslt = Read_Data_HSRL_Oracles(HSRLfile_path,HSRLfile_name,PixNo)
max_alt = rslt['OBS_hght']
print(rslt['OBS_hght'])
maxCPU = 3 #maximum CPU allocated to run GRASP on server
gRuns = []
yamlObj = graspYAML(baseYAMLpath=fwdModelYAMLpath)
#eventually have to adjust code for height, this works only for one pixel (single height value)
gRuns.append(graspRun(pathYAML=yamlObj, releaseYAML=True )) # This should copy to new YAML object
pix = pixel()
pix.populateFromRslt(rslt, radianceNoiseFun=None, dataStage= 'meas', verbose=False)
gRuns[-1].addPix(pix)
gDB = graspDB(graspRunObjs=gRuns, maxCPU=maxCPU)
#rslts contain all the results form the GRASP inverse run
rslts, failPix = gDB.processData(binPathGRASP=binPathGRASP, savePath=None, krnlPathGRASP=krnlPath)
return rslts, max_alt
# height = 200
def LidarAndMAP(Kernel_type,HSRLfile_path,HSRLfile_name,HSRLPixNo,file_path,file_name,RSP_PixNo,ang1,ang2,TelNo, nwl,GasAbsFn, updateYaml= None):
krnlPath='/home/shared/GRASP_GSFC/src/retrieval/internal_files'