-
Notifications
You must be signed in to change notification settings - Fork 0
/
iasiCrisAirs_inBufrSubset_allStandard.py
413 lines (362 loc) · 18.4 KB
/
iasiCrisAirs_inBufrSubset_allStandard.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
#!/usr/bin/env python3
import configparser
pathInfo = configparser.ConfigParser()
pathInfo.read('rttov.cfg')
rttovPath = pathInfo['RTTOV']['rttovPath']
import matplotlib
matplotlib.use('Agg')
import numpy as np
import os
import sys
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
from matplotlib.mlab import bivariate_normal
import matplotlib
import matplotlib.cm as mplcm
import matplotlib.colors as colors
from cycler import cycler
import h5py
pyRttovPath = os.path.join(rttovPath,'wrapper')
if not pyRttovPath in sys.path:
sys.path.append(pyRttovPath)
import pyrttov
import example_data as ex
def calculateWeightingFunctions(chan_list, rttovInstance, myProfiles):
nlevels = np.asarray(rttovInstance.TauLevels).shape[2]
nprofiles = myProfiles.P.shape[0]
wf = np.zeros([nprofiles, nlevels-1, len(chan_list)])
for p in range(myProfiles.P.shape[0]):
iList = 0
for c in range(0,rttovInstance.Nchannels):
if c+1 in chan_list:
num = rttovInstance.TauLevels[p, c, 1::] - rttovInstance.TauLevels[p, c, 0:nlevels-1]
den = np.log(myProfiles.P[p, 0:nlevels-1]) - np.log(myProfiles.P[p, 1:nlevels])
wf[p,:,iList] = num/den
iList+=1
return wf
def plotWeightingFunctions(chan_list, profiles, wf, instrument, profileNumber, wavenumbers,corr=False):
matplotlib.rc('xtick', labelsize=10)
plt.figure()
NUM_COLORS = len(chan_list)
cm = plt.get_cmap('brg')
cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
plt.gca().set_prop_cycle(cycler('color', [scalarMap.to_rgba(i) for i in range(NUM_COLORS)]))
for i in range(wf.shape[1]):
plt.plot( wf[0:99,i], profiles.P[int(profileNumber)-1,0:99] )
plt.gca().set_yscale('log')
plt.gca().invert_yaxis()
plt.gca().set_xlim(0,1.0)
plt.yticks(np.array([1000.0, 100.0, 10.0, 1.0, 0.1]),['1000.0','100.0','10.0','1.0','0.1'])
legendList = []
for i,c in enumerate(chan_list):
legendList.append('{} {:4.3f} {:4.3f}'.format(c, wavenumbers[instrument][c-1], 10000.0/wavenumbers[instrument][c-1]))
plt.legend(legendList, fontsize=6, ncol=3)
plt.ylabel('Pressure [hPa]')
plt.xlabel('Weighting Function')
if(corr==False):
plt.savefig(instrument+'_profile_'+profileNumber+'_weighting_functions.png')
else:
plt.savefig(instrument+'_profile_'+profileNumber+'_weighting_functions_correlation.png')
if(len(chan_list)>30): matplotlib.rc('xtick', labelsize=6)
else: matplotlib.rc('xtick', labelsize=10)
plt.close()
plt.figure()
plt.title('Weighting Function by Instrument Channel')
wf = np.asarray(wf)
#plt.pcolor(np.arange(wf.shape[0]), myProfiles.P[0,0:99], wf[:,0:99].T, norm = LogNorm( vmin = wf[:,0:99].min(), vmax = 0.3 ))#vmax = wf[:,0:99].max() ) )
plt.pcolor(np.arange(wf.shape[1]), myProfiles.P[int(profileNumber)-1,0:99], wf[0:99,:],vmin=wf[0:99,:].min(), vmax=1 )#vmax = wf[:,0:99].max() ) )
plt.colorbar()
plt.ylabel('Pressure [hPa]')
plt.xlabel('Instrument Channel')
plt.gca().set_yscale('log')
plt.gca().invert_yaxis()
plt.xticks(np.arange(len(chan_list)), chan_list, rotation='vertical')
plt.yticks(np.array([1000.0, 100.0, 10.0, 1.0, 0.1]),['1000.0','100.0','10.0','1.0','0.1'])
plt.tight_layout()
if(corr==False):
plt.savefig(instrument+'_profile_'+profileNumber+'_weighting_functions_pcolor.png')
else:
plt.savefig(instrument+'_profile_'+profileNumber+'_weighting_functions_correlation_pcolor.png')
plt.close()
#print(profileNumber, myProfiles.O3[int(profileNumber)-1,:].sum())
def plotJacobians(chan_list, profiles, jacobians, instrument, profileNumber, wavenumbers, ofWhat):
matplotlib.rc('xtick', labelsize=10)
plt.figure()
NUM_COLORS = len(chan_list)
cm = plt.get_cmap('brg')
cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
plt.gca().set_prop_cycle(cycler('color', [scalarMap.to_rgba(i) for i in range(NUM_COLORS)]))
jacSub = []
for i in range(jacobians.shape[0]):
if(i+1 in chan_list):
plt.plot( jacobians[i,:], profiles.P[int(profileNumber)-1,:] )
jacSub.append(jacobians[i,:])
plt.gca().set_yscale('log')
plt.gca().invert_yaxis()
#plt.gca().set_xlim(0,0.5)
plt.yticks(np.array([1000.0, 100.0, 10.0, 1.0, 0.1]),['1000.0','100.0','10.0','1.0','0.1'])
legendList = []
for i,c in enumerate(chan_list):
legendList.append('{} {:4.3f} {:4.3f}'.format(c, wavenumbers[instrument][c-1], 10000.0/wavenumbers[instrument][c-1]))
plt.legend(legendList, fontsize=6, ncol=3)
plt.ylabel('Pressure [hPa]')
if(ofWhat.lower() =='temperature'): plt.xlabel('Jacobian [K/K]')
else: plt.xlabel('Jacobian [K/ppmv]')
plt.savefig(instrument+'_profile_'+profileNumber+'_jacobians_'+ofWhat+'.png')
if(len(chan_list)>30): matplotlib.rc('xtick', labelsize=6)
else: matplotlib.rc('xtick', labelsize=10)
plt.close()
plt.figure()
if(ofWhat.lower() == 'temperature'): plt.title(ofWhat.capitalize()+' Jacobian by Instrument Channel [K/K]')
else: plt.title(ofWhat.capitalize()+' Jacobian by Instrument Channel [K/ppmv]')
jacSub = np.asarray(jacSub)
#print(profileNumber)
#plt.pcolor(np.arange(wf.shape[0]), myProfiles.P[0,0:99], wf[:,0:99].T, norm = LogNorm( vmin = wf[:,0:99].min(), vmax = 0.3 ))#vmax = wf[:,0:99].max() ) )
plt.pcolor(np.arange(len(chan_list)), myProfiles.P[int(profileNumber)-1,:], jacSub[:,:].T )
plt.colorbar()
plt.ylabel('Pressure [hPa]')
plt.xlabel('Instrument Channel')
plt.gca().set_yscale('log')
plt.gca().invert_yaxis()
plt.xticks(np.arange(len(chan_list)), chan_list, rotation='vertical')
plt.yticks(np.array([1000.0, 100.0, 10.0, 1.0, 0.1]),['1000.0','100.0','10.0','1.0','0.1'])
plt.tight_layout()
plt.savefig(instrument+'_profile_'+profileNumber+'_jacobians_'+ofWhat+'_pcolor.png')
plt.close()
#print(profileNumber, myProfiles.O3[int(profileNumber)-1,:].sum())
def plotProfilesO3(profiles):
matplotlib.rc('xtick', labelsize=10)
plt.figure()
NUM_COLORS = profiles.P.shape[0]
cm = plt.get_cmap('brg')
cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
plt.gca().set_prop_cycle(cycler('color', [scalarMap.to_rgba(i) for i in range(NUM_COLORS)]))
for i in range(profiles.O3.shape[0]):
plt.plot( profiles.O3[i,0:99], profiles.P[i,0:99] )
plt.gca().set_yscale('log')
plt.gca().invert_yaxis()
plt.yticks(np.array([1000.0, 100.0, 10.0, 1.0, 0.1]),['1000.0','100.0','10.0','1.0','0.1'])
legendList = []
plt.legend(np.arange(1,7), fontsize=6, ncol=1)
plt.ylabel('Pressure [hPa]')
plt.xlabel('Ozone [ppmv]')
plt.savefig('ozoneProfiles.png')
plt.close()
def plotProfilesT(profiles):
matplotlib.rc('xtick', labelsize=10)
plt.figure()
NUM_COLORS = profiles.P.shape[0]
cm = plt.get_cmap('brg')
cNorm = colors.Normalize(vmin=0, vmax=NUM_COLORS-1)
scalarMap = mplcm.ScalarMappable(norm=cNorm, cmap=cm)
plt.gca().set_prop_cycle(cycler('color', [scalarMap.to_rgba(i) for i in range(NUM_COLORS)]))
for i in range(profiles.O3.shape[0]):
plt.plot( profiles.T[i,0:99], profiles.P[i,0:99] )
plt.gca().set_yscale('log')
plt.gca().invert_yaxis()
plt.yticks(np.array([1000.0, 100.0, 10.0, 1.0, 0.1]),['1000.0','100.0','10.0','1.0','0.1'])
legendList = []
plt.legend(np.arange(1,7), fontsize=6, ncol=1)
plt.ylabel('Pressure [hPa]')
plt.xlabel('Temperature [K]')
plt.savefig('temperatureProfiles.png')
plt.close()
def readProfileH5(filename,nprofiles):
h5 = h5py.File( filename )
groups = []
for i in range(1,nprofiles+1):
groups.append("{:04d}".format(i))
P = np.zeros([nprofiles,101])
T = np.zeros([nprofiles,101])
Q = np.zeros([nprofiles,101])
CO2 = np.zeros([nprofiles,101])
O3 = np.zeros([nprofiles,101])
for i,g in enumerate(groups):
P[i,:] = np.asarray(h5['PROFILES'][g]['P'])
Q[i,:] = np.asarray(h5['PROFILES'][g]['Q'])
T[i,:] = np.asarray(h5['PROFILES'][g]['T'])
CO2[i,:] = np.asarray(h5['PROFILES'][g]['CO2'])
O3[i,:] = np.asarray(h5['PROFILES'][g]['O3'])
GasUnits = int(np.asarray(h5['PROFILES'][g]['GAS_UNITS']))
return P, T, Q, CO2, O3, GasUnits
if __name__ == '__main__':
# This example program simulates two profiles for each of three instruments
# The example profile data are defined in example_data
# ------------------------------------------------------------------------
# Set up the profile data
# ------------------------------------------------------------------------
# Declare an instance of Profiles
nprofiles = 83
nlevels = 101
myProfiles = pyrttov.Profiles(nprofiles, nlevels)
h5ProfileFilename = os.path.join(rttovPath,'rttov_test','profile-datasets-hdf','div83.H5')
#h5ProfileFilename = os.path.join(rttovPath,'rttov_test','profile-datasets-hdf','standard101lev_allgas.H5')
myProfiles.P, myProfiles.T, myProfiles.Q, myProfiles.CO2, myProfiles.O3, myProfiles.GasUnits = readProfileH5(h5ProfileFilename, nprofiles)
# repeat first example parameter for all profiles
myProfiles.Angles = ex.angles.transpose()[0,:]*np.ones([nprofiles,ex.angles.transpose()[0,:].shape[0]])
myProfiles.S2m = ex.s2m.transpose()[0,:]*np.ones([nprofiles,ex.s2m.transpose()[0,:].shape[0]])
myProfiles.Skin = ex.skin.transpose()[0,:]*np.ones([nprofiles,ex.skin.transpose()[0,:].shape[0]])
myProfiles.SurfType = ex.surftype.transpose()[0,:]*np.ones([nprofiles,ex.surftype.transpose()[0,:].shape[0]])
myProfiles.SurfGeom = ex.surfgeom.transpose()[0,:]*np.ones([nprofiles,ex.surfgeom.transpose()[0,:].shape[0]])
myProfiles.DateTimes = ex.datetimes.transpose()[0,:]*np.ones([nprofiles,ex.datetimes.transpose()[0,:].shape[0]])
# ------------------------------------------------------------------------
# Set up Rttov instances for each instrument
# ------------------------------------------------------------------------
# Create three Rttov objects for Four instruments
iasiRttov = pyrttov.Rttov()
crisRttov = pyrttov.Rttov()
crisFsrRttov = pyrttov.Rttov()
airsRttov = pyrttov.Rttov()
fsrH5 = h5py.File('etc/cris-fsr_wavenumbers.h5','r')
crisH5 = h5py.File('etc/cris_wavenumbers.h5','r')
iasiH5 = h5py.File('etc/iasi_wavenumbers.h5','r')
airsH5 = h5py.File('etc/airs_wavenumbers.h5','r')
chan_list_fsr_bufr = np.asarray(fsrH5['idxBufrSubset'])
chan_list_cris_bufr = np.asarray(crisH5['idxBufrSubset'])
chan_list_iasi_bufr = np.asarray(iasiH5['idxBufrSubset'])
chan_list_airs_bufr = np.asarray(airsH5['idxBufrSubset'])
chan_list_fsr = chan_list_fsr_bufr[np.where( (chan_list_fsr_bufr>=592) & (chan_list_fsr_bufr<=659) )]
chan_list_cris = chan_list_cris_bufr[np.where( (chan_list_cris_bufr>=556) & (chan_list_cris_bufr<=670) )]
chan_list_iasi = chan_list_iasi_bufr[np.where( (chan_list_iasi_bufr>=1409) & (chan_list_iasi_bufr<=1710) )]
chan_list_airs = chan_list_airs_bufr[np.where( (chan_list_airs_bufr>=1003) & (chan_list_airs_bufr<=1142) )]
wavenumbers = {}
wavenumbers['cris-fsr'] = fsrH5['wavenumbers']
wavenumbers['cris'] = crisH5['wavenumbers']
wavenumbers['iasi'] = iasiH5['wavenumbers']
wavenumbers['airs'] = airsH5['wavenumbers']
# Set the options for each Rttov instance:
# - the path to the coefficient file must always be specified
# - turn RTTOV interpolation on (because input pressure levels differ from
# coefficient file levels)
# - set the verbose_wrapper flag to true so the wrapper provides more
# information
# - enable solar simulations for iasi
# - enable CO2 simulations for cris (the CO2 profiles are ignored for
# the iasi and airs simulations)
# - enable the store_trans wrapper option for airs to provide access to
# RTTOV transmission structure
iasiRttov.FileCoef = '{}/{}'.format(rttovPath,
"rtcoef_rttov12/rttov9pred101L/rtcoef_metop_2_iasi_so2.H5")
# "rtcoef_rttov12/rttov8pred101L/rtcoef_metop_2_iasi.H5")
iasiRttov.Options.AddInterp = True
iasiRttov.Options.AddSolar = True
iasiRttov.Options.CO2Data = True
iasiRttov.Options.OzoneData = True
iasiRttov.Options.StoreTrans = True
iasiRttov.Options.StoreRad = True
iasiRttov.Options.VerboseWrapper = True
crisRttov.FileCoef = '{}/{}'.format(rttovPath,
"rtcoef_rttov12/rttov9pred101L/rtcoef_jpss_0_cris_so2.H5")
crisRttov.Options.AddInterp = True
crisRttov.Options.AddSolar = True
crisRttov.Options.CO2Data = True
crisRttov.Options.OzoneData = True
crisRttov.Options.StoreTrans = True
crisRttov.Options.StoreRad = True
crisRttov.Options.VerboseWrapper = True
crisFsrRttov.FileCoef = '{}/{}'.format(rttovPath,
"rtcoef_rttov12/rttov9pred101L/rtcoef_jpss_0_cris-fsr_so2.H5")
crisFsrRttov.Options.AddInterp = True
crisFsrRttov.Options.AddSolar = True
crisFsrRttov.Options.CO2Data = True
crisFsrRttov.Options.OzoneData = True
crisFsrRttov.Options.StoreTrans = True
crisFsrRttov.Options.StoreRad = True
crisFsrRttov.Options.VerboseWrapper = True
airsRttov.FileCoef = '{}/{}'.format(rttovPath,
"rtcoef_rttov12/rttov9pred101L/rtcoef_eos_2_airs_so2.H5")
airsRttov.Options.AddInterp = True
airsRttov.Options.StoreTrans = True
airsRttov.Options.VerboseWrapper = True
airsRttov.Options.AddInterp = True
airsRttov.Options.AddSolar = True
airsRttov.Options.CO2Data = True
airsRttov.Options.OzoneData = True
airsRttov.Options.StoreTrans = True
airsRttov.Options.StoreRad = True
airsRttov.Options.VerboseWrapper = True
# Load the instruments: for cris and airs do not supply a channel list and
# so read all channels
try:
iasiRttov.loadInst()
crisRttov.loadInst()
crisFsrRttov.loadInst()
airsRttov.loadInst()
except pyrttov.RttovError as e:
sys.stderr.write("Error loading instrument(s): {!s}".format(e))
sys.exit(1)
# Associate the profiles with each Rttov instance
iasiRttov.Profiles = myProfiles
crisRttov.Profiles = myProfiles
crisFsrRttov.Profiles = myProfiles
airsRttov.Profiles = myProfiles
try:
#iasiRttov.printOptions()
#iasiRttov.runDirect()
#crisRttov.runDirect()
#crisFsrRttov.runDirect()
#airsRttov.runDirect()
iasiRttov.runK()
crisRttov.runK()
crisFsrRttov.runK()
airsRttov.runK()
airsRttov.printOptions()
except pyrttov.RttovError as e:
sys.stderr.write("Error running RTTOV direct model: {!s}".format(e))
sys.exit(1)
"""
print("calc weighting functions.")
iasi_wf = calculateWeightingFunctions(chan_list_iasi, iasiRttov, myProfiles)
cris_wf = calculateWeightingFunctions(chan_list_cris, crisRttov, myProfiles)
crisFsr_wf = calculateWeightingFunctions(chan_list_fsr, crisFsrRttov, myProfiles)
airs_wf = calculateWeightingFunctions(chan_list_airs, airsRttov, myProfiles)
print("Plot weighting Functions.")
correlated2Ozone = np.zeros([iasi_wf.shape[1],iasi_wf.shape[2]])
for c in range(iasi_wf.shape[2]):
for l in range(iasi_wf.shape[1]):
correlated2Ozone[l,c] = np.corrcoef(myProfiles.O3[:,l],iasi_wf[:,l,c])[0,1]
print(correlated2Ozone)
"""
#IPython.embed()
#sys.exit(1)
iasi_corr = np.corrcoef(iasiRttov.Bt)
airs_corr = np.corrcoef(airsRttov.Bt)
cris_corr = np.corrcoef(crisRttov.Bt)
fsr_corr = np.corrcoef(crisFsrRttov.Bt)
plt.matshow(iasi_corr)
plt.savefig('iasi_corr.png')
plt.matshow(airs_corr)
plt.savefig('airs_corr.png')
plt.matshow(cris_corr)
plt.savefig('cris_corr.png')
plt.matshow(fsr_corr)
plt.savefig('fsr_corr.png')
"""
for i in range(nprofiles):
print('Plotting IASI')
plotWeightingFunctions(chan_list_iasi, myProfiles, iasi_wf[i,:,:], 'iasi','{:04d}'.format(i+1), wavenumbers)
plotWeightingFunctions(chan_list_iasi, myProfiles, correlated2Ozone, 'iasi','{:04d}'.format(i+1), wavenumbers,corr=True)
plotJacobians(chan_list_iasi, myProfiles, iasiRttov.O3K[i,:,:], 'iasi', '{:04d}'.format(i+1), wavenumbers,'ozone')
plotJacobians(chan_list_iasi, myProfiles, iasiRttov.TK[i,:,:], 'iasi', '{:04d}'.format(i+1), wavenumbers,'temperature')
plotJacobians(chan_list_iasi, myProfiles, iasiRttov.QK[i,:,:], 'iasi', '{:04d}'.format(i+1), wavenumbers,'water')
print('Plotting CrIS')
plotWeightingFunctions(chan_list_cris, myProfiles, cris_wf[i,:,:], 'cris','{:04d}'.format(i+1),wavenumbers)
plotJacobians(chan_list_cris, myProfiles, crisRttov.O3K[i,:,:], 'cris', '{:04d}'.format(i+1), wavenumbers,'ozone')
plotJacobians(chan_list_cris, myProfiles, crisRttov.TK[i,:,:], 'cris', '{:04d}'.format(i+1), wavenumbers,'temperature')
plotJacobians(chan_list_cris, myProfiles, crisRttov.QK[i,:,:], 'cris', '{:04d}'.format(i+1), wavenumbers,'water')
print('Plotting CrIS-FSR')
plotWeightingFunctions(chan_list_fsr, myProfiles, crisFsr_wf[i,:,:], 'cris-fsr','{:04d}'.format(i+1),wavenumbers)
plotJacobians(chan_list_fsr, myProfiles, crisFsrRttov.O3K[i,:,:], 'cris-fsr', '{:04d}'.format(i+1), wavenumbers,'ozone')
plotJacobians(chan_list_fsr, myProfiles, crisFsrRttov.TK[i,:,:], 'cris-fsr', '{:04d}'.format(i+1), wavenumbers,'temperature')
plotJacobians(chan_list_fsr, myProfiles, crisFsrRttov.QK[i,:,:], 'cris-fsr', '{:04d}'.format(i+1), wavenumbers,'water')
print('Plotting AIRS')
plotWeightingFunctions(chan_list_airs, myProfiles, airs_wf[i,:,:], 'airs', '{:04d}'.format(i+1),wavenumbers)
plotJacobians(chan_list_airs, myProfiles, airsRttov.O3K[i,:,:], 'airs', '{:04d}'.format(i+1), wavenumbers,'ozone')
plotJacobians(chan_list_airs, myProfiles, airsRttov.TK[i,:,:], 'airs', '{:04d}'.format(i+1), wavenumbers,'temperature')
plotJacobians(chan_list_airs, myProfiles, airsRttov.QK[i,:,:], 'airs', '{:04d}'.format(i+1), wavenumbers,'water')
"""
plotProfilesO3(myProfiles)
plotProfilesT(myProfiles)