MPAS-JEDI
testInterpolate.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from basic_plot_functions import scatterMapFields
4 from copy import deepcopy
5 import Interpolate
6 import modelsp_utils as modelUtils
7 import numpy as np
8 
9 initDate = '2018041500'
10 mapCentralLongitude = 0.
11 
12 imageFileExtension = 'png'
13 
14 # choose the test variables/level
15 testVariables = ['theta', 'qv', 'uReconstructZonal']
16 testLevel = 0
17 
18 #referenceCase = 'barycentricScalar'
19 referenceCase = 'barycentric'
20 weightCases = {
21 # 'barycentricScalar': {
22 # 'weightMethod': 'barycentricScalar',
23 # 'nInterpPoints': 5,
24 # },
25  'barycentric': {
26  'weightMethod': 'barycentric',
27  'nInterpPoints': 6,
28  },
29  'pbarycentric': {
30  'weightMethod': 'pbarycentric',
31  'nInterpPoints': 4,
32  },
33 # 'unstinterpScalar3': {
34 # 'weightMethod': 'unstinterpScalar',
35 # 'nInterpPoints': 3,
36 # },
37  'unstinterp3': {
38  'weightMethod': 'unstinterp',
39  'nInterpPoints': 3,
40  },
41 # 'inverseD3': {
42 # 'weightMethod': 'inverseD',
43 # 'nInterpPoints': 3,
44 # },
45 }
46 
47 meshConfigs = {
48  'coarse':{
49  'directory': '/glade/p/mmm/parc/liuz/pandac_common/120km_GFSANA',
50  'nCells': 40962,
51  },
52  'fine':{
53  'directory': '/glade/p/mmm/parc/liuz/pandac_common/30km_GFSANA',
54  'nCells': 655362,
55  },
56 }
57 
58 gridFiles = {}
59 meshLatDeg = {}
60 meshLonDeg = {}
61 meshLat = {}
62 meshLon = {}
63 for meshName, conf in meshConfigs.items():
64  gridFiles[meshName] = modelUtils.getGridFile(initDate,
65  conf['directory'], conf['nCells'],
66  )
67  meshLatDeg[meshName], meshLonDeg[meshName] = modelUtils.readGrid(gridFile=gridFiles[meshName])
68  meshLat[meshName] = meshLatDeg[meshName] * np.pi / 180.0
69  meshLon[meshName] = meshLonDeg[meshName] * np.pi / 180.0
70 
71 lons = {
72  'coarse': meshLonDeg['coarse'],
73  'fine': meshLonDeg['fine'],
74 }
75 lats = {
76  'coarse': meshLatDeg['coarse'],
77  'fine': meshLatDeg['fine'],
78 }
79 markers = {
80  'coarse': '.',
81  'fine': '.',
82 }
83 
84 sizes = {
85  'coarse': 1.5,
86  'fine': 0.5,
87 }
88 
89 minAbsNormError = {}
90 maxAbsNormError = {}
91 meanAbsNormError = {}
92 
93 minAbsError = {}
94 maxAbsError = {}
95 meanAbsError = {}
96 
97 minBias = {}
98 maxBias = {}
99 meanBias = {}
100 
101 meshField = {}
102 for testVariable in testVariables:
103  meshField[testVariable] = {}
104  for meshName, conf in meshConfigs.items():
105  meshField[testVariable][meshName] = \
106  modelUtils.varRead(testVariable, gridFiles[meshName])[:,testLevel]
107 
108  fields = {
109  'coarse': meshField[testVariable]['coarse']
110  }
111 
112  print('Plotting coarse field: '+testVariable)
113  scatterMapFields(lons, lats, fields, 'original_coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
114  cLon = mapCentralLongitude,
115  markers = markers, sizes = sizes,
116  projection = 'moll',
117  )
118 
119  minAbsNormError[testVariable] = np.NaN
120  maxAbsNormError[testVariable] = np.NaN
121 
122  minAbsError[testVariable] = np.NaN
123  maxAbsError[testVariable] = np.NaN
124 
125  minBias[testVariable] = np.NaN
126  maxBias[testVariable] = np.NaN
127 
128 ## generate the double-interpolated field and calculate errors
129 absNormErrorFields = {}
130 absErrorFields = {}
131 biasFields = {}
132 for caseName, case in weightCases.items():
133  weightMethod = case['weightMethod']
134  print('Testing caseName = '+caseName)
135  print('generating weights for interpolating from coarse to fine mesh')
136  coarse2fine = Interpolate.InterpolateLonLat(meshLon['coarse'], meshLat['coarse'],
137  nInterpPoints = case['nInterpPoints'],
138  weightMethod = weightMethod,
139  calculateDiagnostics = True)
140  coarse2fine.initWeights(meshLon['fine'], meshLat['fine'])
141 
142  print('generating weights for interpolating from fine to coarse mesh')
143  fine2coarse = Interpolate.InterpolateLonLat(meshLon['fine'], meshLat['fine'],
144  nInterpPoints = case['nInterpPoints'],
145  weightMethod = weightMethod,
146  calculateDiagnostics = True)
147  fine2coarse.initWeights(meshLon['coarse'], meshLat['coarse'])
148 
149  if 'barycentric' in caseName:
150  print('plotting barycentric combination used, triangle area, and determinant')
151  fields['coarse'] = fine2coarse.combinationUsed
152  scatterMapFields(lons, lats, fields, caseName+'_combinationUsedCoarse.'+imageFileExtension,
153  cLon = mapCentralLongitude,
154  cmap = 'tab10',
155  markers = markers, sizes = sizes,
156  projection = 'moll',
157  )
158  fields['coarse'] = fine2coarse.triangleArea
159  scatterMapFields(lons, lats, fields, caseName+'_triangleAreaCoarse.'+imageFileExtension,
160  cLon = mapCentralLongitude,
161  cmap = 'gist_rainbow',
162  markers = markers, sizes = sizes,
163  projection = 'moll',
164  )
165  fields['coarse'] = fine2coarse.determinant
166  scatterMapFields(lons, lats, fields, caseName+'_determinantCoarse.'+imageFileExtension,
167  cLon = mapCentralLongitude,
168  markers = markers, sizes = sizes,
169  projection = 'moll',
170  )
171  fields['coarse'] = fine2coarse.ycoord
172  scatterMapFields(lons, lats, fields, caseName+'_ycoordCoarse.'+imageFileExtension,
173  cLon = mapCentralLongitude,
174  markers = markers, sizes = sizes, cbarType = 'Log',
175  projection = 'moll',
176  )
177  fields['coarse'] = fine2coarse.aspectRatio
178  scatterMapFields(lons, lats, fields, caseName+'_aspectRatioCoarse.'+imageFileExtension,
179  cLon = mapCentralLongitude,
180  markers = markers, sizes = sizes,
181  projection = 'moll',
182  )
183  del fields['coarse']
184 
185  fields['fine'] = coarse2fine.combinationUsed
186  scatterMapFields(lons, lats, fields, caseName+'_combinationUsedFine.'+imageFileExtension,
187  cLon = mapCentralLongitude,
188  cmap = 'tab10',
189  markers = markers, sizes = sizes,
190  projection = 'moll',
191  )
192  fields['fine'] = coarse2fine.triangleArea
193  scatterMapFields(lons, lats, fields, caseName+'_triangleAreaFine.'+imageFileExtension,
194  cLon = mapCentralLongitude,
195  cmap = 'gist_rainbow',
196  markers = markers, sizes = sizes,
197  projection = 'moll',
198  )
199  fields['fine'] = coarse2fine.determinant
200  scatterMapFields(lons, lats, fields, caseName+'_determinantFine.'+imageFileExtension,
201  cLon = mapCentralLongitude,
202  markers = markers, sizes = sizes,
203  projection = 'moll',
204  )
205  fields['fine'] = coarse2fine.ycoord
206  scatterMapFields(lons, lats, fields, caseName+'_ycoordFine.'+imageFileExtension,
207  cLon = mapCentralLongitude,
208  markers = markers, sizes = sizes, cbarType = 'Log',
209  projection = 'moll',
210  )
211  fields['fine'] = coarse2fine.aspectRatio
212  scatterMapFields(lons, lats, fields, caseName+'_aspectRatioFine.'+imageFileExtension,
213  cLon = mapCentralLongitude,
214  markers = markers, sizes = sizes,
215  projection = 'moll',
216  )
217  del fields['fine']
218 
219  absNormErrorFields[caseName] = {}
220  absErrorFields[caseName] = {}
221  biasFields[caseName] = {}
222 
223  meanAbsNormError[caseName] = {}
224  meanAbsError[caseName] = {}
225  meanBias[caseName] = {}
226 
227 
228  for testVariable in testVariables:
229  print('Interpolating and calculating errors for testVariable = '+testVariable)
230  coarse2fineField = coarse2fine.apply(meshField[testVariable]['coarse'])
231  coarse2fine2coarseField = fine2coarse.apply(coarse2fineField)
232 
233 # print('plotting double-interpolated field')
234 # fields['coarse'] = coarse2fine2coarseField
235 # scatterMapFields(lons, lats, fields, caseName+'_coarse2fine2coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
236 # cLon = mapCentralLongitude,
237 # markers = markers, sizes = sizes,
238 # projection = 'moll',
239 # )
240 
241  absNormErrorFields[caseName][testVariable] = \
242  np.divide(np.abs(coarse2fine2coarseField - meshField[testVariable]['coarse']), np.abs(meshField[testVariable]['coarse']))
243  minAbsNormError_ = np.min(absNormErrorFields[caseName][testVariable])
244  maxAbsNormError_ = np.max(absNormErrorFields[caseName][testVariable])
245  print(minAbsNormError_, maxAbsNormError_)
246  minAbsNormError[testVariable] = np.nanmin([minAbsNormError_, minAbsNormError[testVariable]])
247  maxAbsNormError[testVariable] = np.nanmax([maxAbsNormError_, maxAbsNormError[testVariable]])
248 
249  meanAbsNormError[caseName][testVariable] = np.nanmean(absNormErrorFields[caseName][testVariable])
250 
251  absErrorFields[caseName][testVariable] = \
252  np.abs(coarse2fine2coarseField - meshField[testVariable]['coarse'])
253  minAbsError_ = np.min(absErrorFields[caseName][testVariable])
254  maxAbsError_ = np.max(absErrorFields[caseName][testVariable])
255  print(minAbsError_, maxAbsError_)
256  minAbsError[testVariable] = np.nanmin([minAbsError_, minAbsError[testVariable]])
257  maxAbsError[testVariable] = np.nanmax([maxAbsError_, maxAbsError[testVariable]])
258 
259  meanAbsError[caseName][testVariable] = np.nanmean(absErrorFields[caseName][testVariable])
260 
261  biasFields[caseName][testVariable] = \
262  coarse2fine2coarseField - meshField[testVariable]['coarse']
263  minBias_ = np.min(biasFields[caseName][testVariable])
264  maxBias_ = np.max(biasFields[caseName][testVariable])
265  print(minBias_, maxBias_)
266  minBias[testVariable] = np.nanmin([minBias_, minBias[testVariable]])
267  maxBias[testVariable] = np.nanmax([maxBias_, maxBias[testVariable]])
268 
269  meanBias[caseName][testVariable] = np.nanmean(biasFields[caseName][testVariable])
270 
271 print('meanAbsNormError = ', meanAbsNormError)
272 print('meanAbsError = ', meanAbsError)
273 print('meanBias = ', meanBias)
274 
275 ## plot the errors
276 for caseName, case in weightCases.items():
277  for testVariable in testVariables:
278  print('Plotting error metrics for caseName = '+caseName+' and testVariable = '+testVariable)
279 
280  weightMethod = case['weightMethod']
281  fields['coarse'] = absNormErrorFields[caseName][testVariable]
282  scatterMapFields(lons, lats, fields, caseName+'_NormalizedAbsError_coarse2fine2coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
283  cLon = mapCentralLongitude,
284  # dmin = minAbsNormError[testVariable], dmax = maxAbsNormError[testVariable],
285  dmin = 1.e-8, dmax = 1.0,
286  cmap = 'gist_rainbow',
287  markers = markers, sizes = sizes, cbarType = 'Log',
288  projection = 'moll',
289  )
290 
291  fields['coarse'] = absErrorFields[caseName][testVariable]
292  scatterMapFields(lons, lats, fields, caseName+'_AbsError_coarse2fine2coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
293  cLon = mapCentralLongitude,
294  dmin = minAbsError[testVariable], dmax = maxAbsError[testVariable],
295  cmap = 'gist_rainbow',
296  markers = markers, sizes = sizes, cbarType = 'Log',
297  projection = 'moll',
298  )
299 
300  dd = np.max([minBias[testVariable], maxBias[testVariable]])
301  fields['coarse'] = biasFields[caseName][testVariable]
302  scatterMapFields(lons, lats, fields, caseName+'_Bias_coarse2fine2coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
303  cLon = mapCentralLongitude,
304  dmin = -dd, dmax = dd,
305  cmap = 'BrBG',
306  markers = markers, sizes = sizes, cbarType = 'SymLog',
307  projection = 'moll',
308  )
309 
310  if caseName != referenceCase:
311  fields['coarse'] = absNormErrorFields[caseName][testVariable] - absNormErrorFields[referenceCase][testVariable]
312  scatterMapFields(lons, lats, fields, caseName+'-'+referenceCase+'_NormalizedAbsError_coarse2fine2coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
313  cLon = mapCentralLongitude,
314  dmin = -0.1, dmax = 0.1,
315  cmap = 'BrBG',
316  markers = markers, sizes = sizes, cbarType = 'SymLog',
317  projection = 'moll',
318  )
319 
320  referenceAbsError = deepcopy(absErrorFields[referenceCase][testVariable])
321  referenceAbsError[referenceAbsError == 0.0] = 1.e-16*meshField[testVariable]['coarse'][referenceAbsError == 0.0]
322 
323  caseAbsError = deepcopy(absErrorFields[caseName][testVariable])
324  caseAbsError[caseAbsError == 0.0] = 1.e-16*meshField[testVariable]['coarse'][caseAbsError == 0.0]
325 
326  fields['coarse'] = np.divide(caseAbsError, referenceAbsError)
327  scatterMapFields(lons, lats, fields, caseName+'DIV'+referenceCase+'_AbsError_coarse2fine2coarse_'+testVariable+'_l'+str(testLevel)+'.'+imageFileExtension,
328  cLon = mapCentralLongitude,
329  dmin = 1.e-3, dmax = 1.e3,
330  cmap = 'BrBG',
331  markers = markers, sizes = sizes, cbarType = 'Log',
332  projection = 'moll',
333  )
def scatterMapFields(lons, lats, fields, filename, minLon=-180., maxLon=180., minLat=-90., maxLat=90., cLon=None, projection='default', dmin=None, dmax=None, markers={}, sizes={}, cmap='gist_ncar', cbarType=None, c={}, logVLim=1.e-12)