MPAS-JEDI
plot_inc_CellorEdge_level.py
Go to the documentation of this file.
1 import os
2 import sys
3 import numpy as np
4 from netCDF4 import Dataset
5 import matplotlib
6 matplotlib.use('pdf')
7 import matplotlib.cm as cm
8 import matplotlib.pyplot as plt
9 from mpl_toolkits.basemap import Basemap
10 from mpl_toolkits.axes_grid1 import make_axes_locatable
11 import datetime as dt
12 from datetime import *
13 import ast
14 import var_utils as vu
15 
16 #This script is for plotting increments in 2D.
17 #x-axis:Cells/edges, y-axis: verfical levels
18 #How to use it:
19 #python plot_inc_celloredge_level.py 2018041500 theta False
20 
21 DATE = str(sys.argv[1])
22 VAR_NAME = str(sys.argv[2])
23 
24 USE_GFSANA = ast.literal_eval(str(sys.argv[3])) #convert str to boolean
25 if (USE_GFSANA):
26  GFSANA_DIR = str(sys.argv[4])
27 
28 #nums used in 120km:
29 if (VAR_NAME =='u'):
30  nums = 122880
31 else:
32  nums = 40962
33 
34 def readdata():
35  xarray = np.arange(0,nums)
36  binsfory = np.arange(0,55)
37 
38  datein = datetime.strptime(DATE+'00','%Y%m%d%H%M%S')
39  datefile = datein.strftime('%Y-%m-%d_%H.%M.%S')
40 
41  bakfile = '../restart.'+datefile+'.nc_orig'
42  baknc = Dataset(bakfile, "r")
43 
44  anafile = '../analysis.'+datefile+'.nc'
45  ananc = Dataset(anafile, "r")
46 
47  #use gfsana
48  if (USE_GFSANA):
49  gfsfile = GFSANA_DIR+'/x1.40962.init.'+datefile+'.nc'
50  gfsnc = Dataset(gfsfile, "r")
51 
52  lats = np.array( baknc.variables['latCell'][:] ) * 180.0 / np.pi
53  lons = np.array( baknc.variables['lonCell'][:] ) * 180.0 / np.pi
54  xtime = np.array( baknc.variables['xtime'][:] )
55  nVertL = baknc.dimensions['nVertLevels']
56 
57  bakmpas = np.array( baknc.variables[VAR_NAME][0,:,:] )
58  if (VAR_NAME == 'pressure_p'):
59  pressure = np.array( ananc.variables['pressure'][0,:,:] )
60  pressure_base = np.array( ananc.variables['pressure_base'][0,:,:] )
61  anampas = pressure - pressure_base
62  else:
63  anampas = np.array( ananc.variables[VAR_NAME][0,:,:] )
64  ambmpas = anampas - bakmpas
65  #plot(xarray,binsfory,anampas,DATE,'MPASANA')
66  #plot(xarray,binsfory,bakmpas,DATE,'MPASBAK')
67  plot(xarray,binsfory,ambmpas,DATE,'MPASAMB')
68  if (USE_GFSANA):
69  gfs = np.array( gfsnc.variables[VAR_NAME][0,:,:] )
70  anamgfs = anampas - gfs
71  bakmgfs = bakmpas - gfs
72 
73  #compare with gfsana: plot()
74  plot(xarray,binsfory,anamgfs,DATE,'MPASANA-GFSANA')
75  plot(xarray,binsfory,bakmgfs,DATE,'MPASBAK-GFSANA')
76 def plot(xarray,binsfory,model,yyyymmddhh,source):
77  zgrid = np.loadtxt("/glade/work/jban/pandac/fix_input/graphics/zgrid_v55.txt")
78 
79  fig, ax1 = plt.subplots()
80  cmap = 'coolwarm' #'bwr' #'jet'# 'coolwarm'
81  model = model.reshape(len(xarray),len(binsfory)).T
82  valuemin = np.amin(model)
83  valuemax = np.amax(model)
84  norm = matplotlib.colors.DivergingNorm(vmin=valuemin, vcenter=0, vmax=valuemax)
85  plt.contourf(xarray,binsfory,model,10,vmin=valuemin,vmax=valuemax,norm=norm,cmap=cmap)
86  plt.title( source+' '+vu.varDictModel[VAR_NAME][1]+'('+ vu.varDictModel[VAR_NAME][0]+') max=' +str(round(valuemax,4))+' min='+str(round(valuemin,4)))
87  major_ticks = np.arange(0, 56, 5)
88  ax1.set_yticks(major_ticks)
89  ax1.set_ylim([0,54])
90  ax1.set_ylabel('Vertical level',fontsize=15)
91 
92  ax2 = ax1.twinx()
93  ax2.set_yticks(major_ticks-1)
94  ax2.set_yticklabels((zgrid[::5]).astype(int))
95 
96  ax2.set_ylabel('Height (m)',fontsize=13)
97 
98  if ( VAR_NAME == 'u'):
99  ax1.set_xlabel( 'Edge',fontsize=15)
100  else:
101  ax1.set_xlabel( 'Cell',fontsize=15)
102 
103  plt.colorbar(extend='both',orientation="horizontal")
104  plt.savefig('%s_%s_%s.png'%(vu.varDictModel[VAR_NAME][1],yyyymmddhh,source),dpi=200,bbox_inches='tight')
105 
106 def main():
107  readdata()
108 
109 if __name__ == '__main__': main()
def plot(xarray, binsfory, model, yyyymmddhh, source)