MPAS-JEDI
plot_BUMP_diag.py
Go to the documentation of this file.
1 import os
2 import numpy
3 import matplotlib.pyplot as plt
4 from mpl_toolkits.axes_grid1 import make_axes_locatable
5 import matplotlib.axes as maxes
6 import fnmatch
7 
8 def readdata():
9  '''
10  BUMP log file name format:
11  mpas_*.info.0000
12  mpas_3denvar_bump.info.0000
13  mpas_3dvar_bumpcov.info.0000
14  mpas_parametersbump_cov.info.0000
15 
16  '''
17  bumplogfiles = []
18  for files in os.listdir('../'):
19  if fnmatch.fnmatch(files, 'mpas_*.info.0000'):
20  bumplogfiles.append('../'+files)
21  print(bumplogfiles)
22  for bumplogfile in bumplogfiles:
23  cmd = "sed -n -e '/--- Compute covariance/,/--- Compute correlation/p' \
24  " + bumplogfile + " > blocks.txt"
25  os.system(cmd)
26 
27  cmd = "sed -e '/--- Compute covariance/d' \
28  -e '/Ensemble 1/d' \
29  -e '/Block 01_01_01_01/d' \
30  -e '/Block 02_02_01_01/d' \
31  -e '/Block 03_03_01_01/d' \
32  -e '/Block 04_04_01_01/d' \
33  -e '/Block 05_05_01_01/d' \
34  -e '/----------------------/d' \
35  -e '/--- Compute correlation/d' blocks.txt > tmp.txt"
36  os.system(cmd)
37  file_name = 'covariance_'+bumplogfile[3:][:-10]+'.txt'
38  cmd = "awk '{print $2, $8}' tmp.txt > " + file_name + " ; rm blocks.txt tmp.txt"
39  os.system(cmd)
40 
41  if (os.stat(file_name).st_size == 0):
42  print(file_name+ " is empty.")
43  else:
44  lev = []
45  cov = []
46  with open(file_name,'r') as myfile:
47 
48  for line in myfile: #range(0, 54):
49  all1 = numpy.asarray([a for a in filter(None, line.split(' '))])
50  lev.append(all1[0])
51  cov.append(all1[1])
52  nlev = len(lev)/5
53  print('Vertical Levels=',nlev)
54  plot(cov[0:nlev-1],lev[0:nlev-1],'Variance', 'T','($K^2$)',bumplogfile[3:][:-10])
55  plot(cov[nlev:2*nlev-1],lev[nlev:2*nlev-1],'Variance', 'P', '($Pa^2$)',bumplogfile[3:][:-10])
56  plot(cov[2*nlev:3*nlev-1],lev[2*nlev:3*nlev-1],'Variance', 'Q','($kg^2/kg^2$)',bumplogfile[3:][:-10])
57  plot(cov[3*nlev:4*nlev-1],lev[3*nlev:4*nlev-1],'Variance', 'U','($m^2/s^2$)',bumplogfile[3:][:-10])
58  plot(cov[4*nlev:5*nlev-1],lev[4*nlev:5*nlev-1],'Variance', 'V','($m^2/s^2$)',bumplogfile[3:][:-10])
59 
60 def plot(var1,var2,var3,var4,var5,var6):
61  plt.plot(var1,var2,'g-*',markersize=5)
62  plt.ylabel('Vertical Levels')
63  plt.xlabel('%s %s'%(var3,var5))
64  plt.legend('%s'%var4, loc='upper left')
65 
66  plt.grid(True)
67  plt.savefig('cov_%s_%s.png'%(var4,var6),dpi=200,bbox_inches='tight')
68  plt.close()
69 
70 def main():
71  readdata()
72 
73 if __name__ == '__main__': main()
def plot(var1, var2, var3, var4, var5, var6)