-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff2obs.py
259 lines (216 loc) · 8.73 KB
/
diff2obs.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
#########################################################################
#$Id: bld.py 28 2021-01-21 15:10:31Z whuang $
#$Revision: 28 $
#$HeadURL: file:///Users/whuang/.wei_svn_repository/trunk/jedi-build-tools/bld.py $
#$Date: 2021-01-21 08:10:31 -0700 (Thu, 21 Jan 2021) $
#$Author: whuang $
#########################################################################
import getopt
import os, sys
import types
import math
import time
import datetime
import subprocess
import netCDF4
import numpy as np
from readIODA2Obs import ReadIODA2Obs
#------------------------------------------------------------------
class Compare2ObsFiles():
def __init__(self, debug=0):
self.debug = debug
self.delt = 1.0e-10
#------------------------------------------------------------------
def setup(self, datadir=None, filename=None):
if(self.debug):
print('datadir = ', datadir)
print('filename = ', filename)
basefile = '%s/hofx.nl/%s' %(datadir, filename)
if(os.path.exists(basefile)):
self.baseio = ReadIODA2Obs(debug=debug, filename=basefile)
else:
print('basefile: <%s> does not exist. Stop.' %(basefile))
sys.exit(-1)
casefile = '%s/hofx.lo/%s' %(datadir, filename)
if(os.path.exists(casefile)):
self.caseio = ReadIODA2Obs(debug=debug, filename=casefile)
else:
print('casefile: <%s> does not exist. Stop.' %(casefile))
sys.exit(-1)
print('basefile:', basefile)
print('casefile:', casefile)
#------------------------------------------------------------------
def get_groups(self):
self.basegrps = self.baseio.get_groups()
self.casegrps = self.caseio.get_groups()
#print('self.baseio.groups=', self.basegrps)
#print('self.caseio.groups=', self.casegrps)
grps = []
for grp in self.casegrps:
if(grp in self.basegrps):
if(grp != 'MetaData'):
grps.append(grp)
return grps
#------------------------------------------------------------------
def get_variables(self, grp):
variables = self.caseio.get_varlistInGroup(grp)
#print('variables:', variables)
return variables
#------------------------------------------------------------------
def compare_int_variable(self, varname):
base_var = self.baseio.get_var(varname)
case_var = self.caseio.get_var(varname)
#print('len(case_var) = ', len(case_var))
#print('case_var) = ', case_var)
#print('len(base_var) = ', len(base_var))
#print('base_var) = ', base_var)
diff = case_var - base_var
nd = 0
for n in range(len(case_var)):
if(diff[n] != 0):
nd += 1
if(nd):
print('Difference of %s\n' %(varname))
print('\tLen(var): %d, nd: %d, mindif: %f, maxdif: %f' %(len(case_var), nd, np.min(diff), np.max(diff)))
for n in range(len(case_var)):
if(diff[n] != 0):
print('\tNo %d: c %d, b %d, diff: %d' %(n+1, case_var[n], base_var[n], diff[n]))
else:
if(self.debug):
print('var: %s has no difference' %(varname))
#------------------------------------------------------------------
def compare_float_variable(self, varname):
base_var = self.baseio.get_var(varname)
case_var = self.caseio.get_var(varname)
#print('len(case_var) = ', len(case_var))
#print('case_var) = ', case_var)
#print('len(base_var) = ', len(base_var))
#print('base_var) = ', base_var)
diff = case_var - base_var
nd = 0
for n in range(len(case_var)):
if(math.isnan(base_var[n]) or math.isnan(case_var[n])):
continue
if(abs(diff[n]) > self.delt):
nd += 1
if(nd):
print('Difference of %s\n' %(varname))
print('\tLen(var): %d, nd: %d, mindif: %f, maxdif: %f' %(len(case_var), nd, np.min(diff), np.max(diff)))
for n in range(len(case_var)):
if(math.isnan(base_var[n]) or math.isnan(case_var[n])):
continue
if(abs(diff[n]) > self.delt):
print('\tNo %d: c %f, b %f, diff: %f' %(n+1, case_var[n], base_var[n], diff[n]))
else:
if(self.debug):
print('var: %s has no difference' %(varname))
#------------------------------------------------------------------
def compare_2d_int_variable(self, varname):
base_var = self.baseio.get_2d_var(varname)
case_var = self.caseio.get_2d_var(varname)
#print('len(case_var) = ', len(case_var))
#print('case_var) = ', case_var)
#print('len(base_var) = ', len(base_var))
#print('base_var) = ', base_var)
diff = case_var - base_var
nd = 0
for j in range(len(case_var)):
for i in range(len(case_var[0])):
if(diff[j,i] != 0):
nd += 1
if(nd):
print('Difference of %s\n' %(varname))
print('\tLen(var): %d, nd: %d, mindif: %f, maxdif: %f' %(len(case_var), nd, np.min(diff), np.max(diff)))
for j in range(len(case_var)):
for i in range(len(case_var[0])):
if(diff[j,i] != 0):
print('\tJ: %d, I: %d: c %d, b %d, diff: %d' %(j, i, case_var[j,i], base_var[j,i], diff[j,i]))
else:
if(self.debug):
print('var: %s has no difference' %(varname))
#------------------------------------------------------------------
def compare_2d_float_variable(self, varname):
base_var = self.baseio.get_2d_var(varname)
case_var = self.caseio.get_2d_var(varname)
diff = case_var - base_var
#print('len(case_var) = ', len(case_var))
#print('case_var) = ', case_var)
#print('len(base_var) = ', len(base_var))
#print('base_var) = ', base_var)
#print('\tbase_var.max: %f, base_var.min: %f' %(np.max(base_var[:,0]), np.min(base_var[:,0])))
#print('\tcase_var.max: %f, case_var.min: %f' %(np.max(case_var[:,0]), np.min(case_var[:,0])))
#print('\diffvar.max: %f, diffvar.min: %f' %(np.max(diffvar), np.min(diffvar)))
nd = 0
for j in range(len(case_var)):
for i in range(len(case_var[0])):
if(math.isnan(base_var[j,i]) or math.isnan(case_var[j,i])):
continue
if(abs(diff[j,i]) > self.delt):
nd += 1
if(nd):
print('Difference of %s\n' %(varname))
print('\tbase_var.max: %f, base_var.min: %f' %(np.max(base_var[:,0]), np.min(base_var[:,0])))
print('\tcase_var.max: %f, case_var.min: %f' %(np.max(case_var[:,0]), np.min(case_var[:,0])))
print('\tLen(var): %d, nd: %d, mindif: %f, maxdif: %f' %(len(case_var), nd, np.min(diff), np.max(diff)))
for j in range(len(case_var)):
for i in range(len(case_var[0])):
if(math.isnan(base_var[j,i]) or math.isnan(case_var[j,i])):
continue
if(abs(diff[j,i]) > self.delt):
print('\tJ: %d, I: %d: c %d, b %d, diff: %d' %(j, i, case_var[j,i], base_var[j,i], diff[j,i]))
else:
if(self.debug):
print('var: %s has no difference' %(varname))
#------------------------------------------------------------------
if __name__== '__main__':
debug = 0
datadir = '/scratch2/BMC/gsienkf/Wei.Huang/jedi/dev/build/intel/fv3-jedi/test/Data'
#filename = 'sfc_letkf-gfs_2020121500_m.nc4'
filename = 'sfc_letkf-gfs_2020121500_m.nc4'
opts, args = getopt.getopt(sys.argv[1:], '', ['debug=', 'filename='])
for o, a in opts:
if o in ('--debug'):
debug = int(a)
elif o in ('--filename'):
filename = a
else:
print('option: ', a)
assert False, 'unhandled option'
#------------------------------------------------------------------
cof = Compare2ObsFiles()
#------------------------------------------------------------------
for filename in ['sfc_letkf-gfs_2020121500_m.nc4',
'aircraft_letkf-gfs_2020121500_m.nc4',
'scatwind_letkf-gfs_2020121500_m.nc4']:
cof.setup(datadir=datadir, filename=filename)
qclist = cof.get_variables('EffectiveQC0')
varlist = cof.get_variables('ombg')
for grp in cof.get_groups():
if(grp in ['GsiInputObsError']):
continue
if(grp in ['EffectiveQC0', 'EffectiveQC2', 'ObsType', 'PreQC', 'PreUseFlag']):
for var in varlist:
varname = '/%s/%s' %(grp, var)
if(var in qclist):
cof.compare_int_variable(varname)
else:
for var in varlist:
varname = '/%s/%s' %(grp, var)
cof.compare_float_variable(varname)
#------------------------------------------------------------------
filename = 'amsua_n19_letkf-gfs_2020121500_m.nc4'
cof.setup(datadir=datadir, filename=filename)
qclist = cof.get_variables('EffectiveQC0')
varlist = cof.get_variables('ombg')
for grp in cof.get_groups():
if(grp in ['GsiInputObsError']):
continue
if(grp in ['EffectiveQC0', 'EffectiveQC2', 'ObsType', 'PreQC', 'PreUseFlag']):
for var in varlist:
varname = '/%s/%s' %(grp, var)
if(var in qclist):
cof.compare_2d_int_variable(varname)
else:
for var in varlist:
varname = '/%s/%s' %(grp, var)
cof.compare_2d_float_variable(varname)