FV3 Bundle
TLM_vs_NLM.py
Go to the documentation of this file.
1 from netCDF4 import Dataset
2 import numpy as np
3 import matplotlib
4 matplotlib.use('Agg')
5 import matplotlib.pyplot as plt
6 import re
7 
8 saveFigs = True
9 
10 date = '20181004'
11 time = '000000'
12 
13 fields_geos = ['ua','va','t','delp','q','qi','ql','o3mr']
14 fields_gfs = ['ua','va','T','Ps','sphum','ice_wat','liq_wat','o3mr']
15 
16 kind_gfs = ['fv_core','fv_core','fv_core','fv_core','fv_tracer','fv_tracer','fv_tracer','fv_tracer']
17 
18 nm = len(fields_geos)
19 
20 nl_type = 'geos'
21 nl1 = '../INPUTS/GEOS_c180/H21/GEOS.traj.c180.eta.'+date+'_'+time[0:4]+'z.nc4'
22 nl2 = '../INPUTS/GEOS_c180/H15/GEOS.traj.c180.eta.'+date+'_'+time[0:4]+'z.nc4'
23 
24 tlm_type = 'gfs'
25 tlm = date+'.'+time+'.linearforecast.final.KIND.res.tileXX.nc'
26 
27 
28 if (nl_type == 'gfs'):
29  tmp = re.sub("XX", str(1), nl1)
30  fh_nl1 = Dataset(tmp, mode='r')
31 elif (nl_type == 'geos'):
32  fh_nl1 = Dataset(nl1, mode='r')
33 
34 # Read grid
35 # ---------
36 print('Reading grid')
37 
38 if (nl_type == 'gfs'):
39  nl_xid = 'xaxis_1'
40  nl_yid = 'yaxis_1'
41  nl_zid = 'zaxis_1'
42 elif (nl_type == 'geos'):
43  nl_xid = 'lon'
44  nl_yid = 'lat'
45  nl_zid = 'lev'
46 
47 im = len(fh_nl1.dimensions[nl_xid])
48 jm = len(fh_nl1.dimensions[nl_yid])
49 lm = len(fh_nl1.dimensions[nl_zid])
50 
51 if (nl_type == 'gfs'):
52  jm = jm * 6
53 
54 jm_gfs = jm/6
55 
56 print('Reading variables')
57 
58 x_nlm = np.zeros(im*jm*lm*nm).reshape(nm,lm,jm,im)
59 x_tlm = np.zeros(im*jm*lm*nm).reshape(nm,lm,jm,im)
60 
61 if (nl_type == 'gfs'):
62 
63  fields = fields_gfs
64 
65 else:
66 
67  fields = fields_geos
68  fh_nl2 = Dataset(nl2, mode='r')
69 
70  for i in range(nm):
71  if (fields[i] != 'Ps'):
72  x_nlm[i,:,:,:] = fh_nl2.variables[fields[i]][:] - fh_nl1.variables[fields[i]][:]
73  if (fields[i] == 'delp'):
74  tmp = np.sum(x_nlm[i,:,:,:],axis=0)
75  for l in range(lm):
76  x_nlm[i,l,:,:] = tmp
77  elif (fields[i] == 'Ps'):
78  for l in range(lm):
79  x_nlm[i,l,:,:] = fh_nl2.variables[fields[i]][:] - fh_nl1.variables[fields[i]][:]
80 
81 
82 if (tlm_type == 'gfs'):
83 
84  fields = fields_gfs
85 
86  for i in range(nm):
87 
88  for tile in range(6):
89 
90  filetile1 = re.sub("XX", str(tile+1), tlm)
91  filetile = re.sub("KIND", kind_gfs[i], filetile1)
92 
93  fh_tlm = Dataset(filetile, mode = 'r')
94 
95  js = tile*jm_gfs
96  je = (tile+1)*jm_gfs
97 
98  if (fields[i] != 'Ps'):
99  x_tlm[i,:,js:je,:] = fh_tlm.variables[fields[i]][:]
100  if (fields[i] == 'delp'):
101  tmp = np.sum(x_nlm[i,:,js:je,:],axis=0)
102  for l in range(lm):
103  x_tlm[i,l,js:je,:] = tmp
104  elif (fields[i] == 'Ps'):
105  for l in range(lm):
106  x_tlm[i,l,js:je,:] = fh_tlm.variables[fields[i]][:]
107 
108 
109 # Compute and plot correlations
110 # -----------------------------
111 print('Computing Correlations')
112 f, ax = plt.subplots(1, 8, figsize=(18, 6))
113 
114 corr_tlm = np.zeros(lm*nm).reshape(nm,lm)
115 for i in range(nm):
116  for j in range(lm):
117  c1 = np.corrcoef(np.reshape(x_tlm[i,j,:,:],(im*jm,)),np.reshape(x_nlm[i,j,:,:],(im*jm,)))
118 
119  corr_tlm[i,j] = c1[0,1]
120 
121  rangeup1 = 0
122  rangeup2 = 0
123  for j in range(lm-1,-1,-1):
124  if np.isnan(corr_tlm[i,j]) == True:
125  rangeup1 = j+1
126  break
127 
128  ax[i].plot(corr_tlm[i,rangeup1:lm],range(rangeup1+1,lm+1),color='b')
129  ax[i].set_xlim(0, 1)
130  ax[i].set_ylim(1, 72)
131  ax[i].invert_yaxis()
132  ax[i].set_title(fields_geos[i])
133  if (i==0):
134  ax[i].set_ylabel('Model Level')
135 
136  print('Corelation:',fields_geos[i],np.mean(corr_tlm[i,rangeup1:lm]) )
137 
138 if saveFigs == True:
139  plt.savefig('Correlation.png')
140 
str
Definition: c2f.py:15