FV3 Bundle
PlotCubeSphereField.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 #from mpl_toolkits.basemap import Basemap
7 
8 #User input required for the follwing:
9 plot_diff = 1 #Plot path1/file - path2/file
10 model = 'gfs'
11 cube = 48
12 filetype = 'png'
13 
14 path1 = './' #Path of first/only file
15 file_tplt_befr1 = '20180415.000000.3DVar.fv_core.res.tile' #Filename befor tile number
16 file_tplt_aftr = '.nc' #Filename after tile number
17 
18 if (cube == 48):
19  if (model == 'geos'):
20  path2 = '../INPUTS/GEOS_c48/'
21  file_tplt_befr2 = '20180415.000000.fv_core.res.tile'
22  elif (model == 'gfs'):
23  path2 = '../INPUTS/GFS_c48/ENSEMBLE/mem001/RESTART/'
24  file_tplt_befr2 = '20180415.000000.fv_core.res.tile'
25 elif (cube == 96):
26  path2 = ''
27  file_tplt_befr2 = 'fv_core.res.tile'
28 
29 xdimvar = 'xaxis_1' #What to read to get dimension
30 ydimvar = 'yaxis_2' #What to read to get dimension
31 zdimvar = 'zaxis_1' #What to read to get dimension
32 readvar = 'T' #Variable to plot
33 Dim2dor3d = '3D' #Is this 2D or 3D field?
34 plot_level = 50 #If 3D plot this level
35 
36 readvarlat = 'grid_lat' #Variable to plot
37 readvarlon = 'grid_lon' #Variable to plot
38 
39 #Tile 1 handle for getting dimension
40 fh1 = Dataset(path1 + file_tplt_befr1 + str(1) + file_tplt_aftr, mode='r')
41 fh2 = Dataset(path2 + file_tplt_befr2 + str(1) + file_tplt_aftr, mode='r')
42 
43 #Dimensions
44 npx = len(fh1.dimensions[xdimvar])
45 npy = len(fh1.dimensions[ydimvar])
46 npz = 0
47 
48 npxr = npx
49 npyr = npy
50 
51 if (readvar == 'u'):
52  npyr = npyr + 1
53 if (readvar == 'v'):
54  npxr = npxr + 1
55 
56 if Dim2dor3d == '3D':
57  npz = len(fh1.dimensions[zdimvar])
58  fr = np.zeros(6*npz*npyr*npxr).reshape(6,npz,npyr,npxr)
59 else:
60  fr = np.zeros(6*npyr*npxr).reshape(6,npyr,npxr)
61 
62 flat = np.zeros(6*npy*npx).reshape(6,npy,npx)
63 flon = np.zeros(6*npy*npx).reshape(6,npy,npx)
64 
65 #Read file
66 for tile in range(6):
67  file_tile1 = file_tplt_befr1 + str(tile+1) + file_tplt_aftr
68  file_tile2 = file_tplt_befr2 + str(tile+1) + file_tplt_aftr
69  pathfile1 = path1 + file_tile1
70  pathfile2 = path2 + file_tile2
71  print(pathfile1)
72  if plot_diff == 1:
73  print(' '+pathfile2)
74  fh1 = Dataset(pathfile1, mode='r')
75  fh2 = Dataset(pathfile2, mode='r')
76  if plot_diff == 1:
77  fr[tile,:,:,:] = fh1.variables[readvar][:] - fh2.variables[readvar][:]
78  else:
79  fr[tile,:,:,:] = fh1.variables[readvar][:]
80 
81 
82 #Get level to plot
83 f = np.zeros(6*npy*npx).reshape(6,npy,npx)
84 if Dim2dor3d == '3D':
85  f[:,:,:] = fr[:,plot_level,0:npy,0:npx]
86 else:
87  f = fr
88 
89 #Initial arrays
90 f12 = np.zeros(12*npy*npx).reshape(12,npy,npx)
91 fp = np.zeros(12*npy*npx).reshape(4*npy,3*npx)
92 
93 f12[0,:,:] = np.rot90(f[2,:,:],2)
94 f12[1,:,:] = np.rot90(f[2,:,:],3)
95 f12[2,:,:] = f[2,:,:]
96 f12[3,:,:] = np.rot90(f[2,:,:],1)
97 f12[4,:,:] = np.fliplr(np.transpose(f[0,:,:]))
98 f12[5,:,:] = np.fliplr(np.transpose(f[1,:,:]))
99 f12[6,:,:] = f[3,:,:]
100 f12[7,:,:] = f[4,:,:]
101 f12[8,:,:] = np.rot90(f[5,:,:],3)
102 f12[9,:,:] = np.rot90(f[5,:,:],2)
103 f12[10,:,:] = np.rot90(f[5,:,:],1)
104 f12[11,:,:] = f[5,:,:]
105 
106 #Cat to single array
107 fp[:,0*npx+0:npx] = np.concatenate([f12[0,:,:], f12[1,:,:], f12[2 ,:,:], f12[3 ,:,:]])
108 fp[:,1*npx:2*npx] = np.concatenate([f12[4,:,:], f12[5,:,:], f12[6 ,:,:], f12[7 ,:,:]])
109 fp[:,2*npx:3*npx] = np.concatenate([f12[8,:,:], f12[9,:,:], f12[10,:,:], f12[11,:,:]])
110 fp = np.flipud(np.transpose(fp))
111 
112 #Contour levels
113 maxf = np.nanmax(np.abs(fp))
114 minf = np.nanmin(fp)
115 
116 if minf < 0:
117  minf = -maxf
118 incf = (maxf - minf)/101
119 clev = np.arange(minf,maxf+incf,incf)
120 
121 incf = (maxf - minf)/10
122 ctic = np.arange(minf,maxf+incf,incf)
123 
124 #Colormap
125 cmap = plt.cm.seismic
126 
127 itit = ' '
128 if plot_diff == 1:
129  itit = ' increment '
130 
131 
132 # #Plot with coast lines
133 #
134 # for tile in range(6):
135 # flat[tile,:,:] = fh1.variables[readvarlat][:]
136 # flon[tile,:,:] = fh1.variables[readvarlon][:]
137 #
138 # f12lat = np.zeros(12*npy*npx).reshape(12,npy,npx)
139 # f12lon = np.zeros(12*npy*npx).reshape(12,npy,npx)
140 #
141 # #Transpose, rotate etc
142 # f12lat[0,:,:] = np.rot90(flat[2,:,:],2)
143 # f12lat[1,:,:] = np.rot90(flat[2,:,:],3)
144 # f12lat[2,:,:] = flat[2,:,:]
145 # f12lat[3,:,:] = np.rot90(flat[2,:,:],1)
146 # f12lat[4,:,:] = np.fliplr(np.transpose(flat[0,:,:]))
147 # f12lat[5,:,:] = np.fliplr(np.transpose(flat[1,:,:]))
148 # f12lat[6,:,:] = flat[3,:,:]
149 # f12lat[7,:,:] = flat[4,:,:]
150 # f12lat[8,:,:] = np.rot90(flat[5,:,:],3)
151 # f12lat[9,:,:] = np.rot90(flat[5,:,:],2)
152 # f12lat[10,:,:] = np.rot90(flat[5,:,:],1)
153 # f12lat[11,:,:] = flat[5,:,:]
154 #
155 # f12lon[0,:,:] = np.rot90(flon[2,:,:],2)
156 # f12lon[1,:,:] = np.rot90(flon[2,:,:],3)
157 # f12lon[2,:,:] = flon[2,:,:]
158 # f12lon[3,:,:] = np.rot90(flon[2,:,:],1)
159 # f12lon[4,:,:] = np.fliplr(np.transpose(flon[0,:,:]))
160 # f12lon[5,:,:] = np.fliplr(np.transpose(flon[1,:,:]))
161 # f12lon[6,:,:] = flon[3,:,:]
162 # f12lon[7,:,:] = flon[4,:,:]
163 # f12lon[8,:,:] = np.rot90(flon[5,:,:],3)
164 # f12lon[9,:,:] = np.rot90(flon[5,:,:],2)
165 # f12lon[10,:,:] = np.rot90(flon[5,:,:],1)
166 # f12lon[11,:,:] = flon[5,:,:]
167 #
168 #
169 # fig = plt.figure(figsize=(14,8))
170 #
171 # axN = fig.add_subplot(3,4,2)
172 # ax1 = fig.add_subplot(3,4,5)
173 # ax2 = fig.add_subplot(3,4,6)
174 # ax3 = fig.add_subplot(3,4,7)
175 # ax4 = fig.add_subplot(3,4,8)
176 # axS = fig.add_subplot(3,4,10)
177 #
178 # bl = 48.0 #np.min(f12lat[0,:,:])
179 #
180 # mapN = Basemap(projection='npstere', boundinglat= bl,lon_0 = np.min(0.5*(f12lon[5,npx/2-1,:] + f12lon[5,npx/2,:])), ax = axN)
181 # mapS = Basemap(projection='spstere', boundinglat=-bl,lon_0 = np.min(0.5*(f12lon[7,npx/2-1,:] + f12lon[7,npx/2,:])), ax = axS)
182 #
183 # map1 = Basemap(projection='merc', llcrnrlat = np.min(f12lat[4,:,:]), urcrnrlat = np.max(f12lat[4,:,:]),\
184 # llcrnrlon = -(360.0-np.min(f12lon[4,1,:])), urcrnrlon = np.max(f12lon[4,npx-1,:]), ax=ax1)
185 # map2 = Basemap(projection='merc', llcrnrlat = np.min(f12lat[5,:,:]), urcrnrlat = np.max(f12lat[5,:,:]),\
186 # llcrnrlon = np.min(f12lon[5,:,:]), urcrnrlon = np.max(f12lon[5,:,:]), ax=ax2)
187 # map3 = Basemap(projection='merc', llcrnrlat = np.min(f12lat[6,:,:]), urcrnrlat = np.max(f12lat[6,:,:]),\
188 # llcrnrlon = np.min(f12lon[6,:,:]), urcrnrlon = np.max(f12lon[6,:,:]), ax=ax3)
189 # map4 = Basemap(projection='merc', llcrnrlat = np.min(f12lat[7,:,:]), urcrnrlat = np.max(f12lat[7,:,:]),\
190 # llcrnrlon = np.min(f12lon[7,:,:]), urcrnrlon = np.max(f12lon[7,:,:]), ax=ax4)
191 #
192 # mapN.drawcoastlines()
193 # map1.drawcoastlines()
194 # map2.drawcoastlines()
195 # map3.drawcoastlines()
196 # map4.drawcoastlines()
197 # mapS.drawcoastlines()
198 #
199 # x,y = np.meshgrid(np.linspace(mapN.xmin,mapN.xmax,npx),np.linspace(mapN.ymin,mapN.ymax,npx))
200 # csN = mapN.contourf(x,y,np.rot90(f12[1,:,:]),clev,cmap=cmap)
201 #
202 # x,y = np.meshgrid(np.linspace(mapS.xmin,mapS.xmax,npx),np.linspace(mapS.ymin,mapS.ymax,npx))
203 # csS = mapS.contourf(x,y,np.rot90(f12[9,:,:],3),clev,cmap=cmap)
204 #
205 # latplt = np.linspace(np.min(f12lat[4,:,:]), np.max(f12lat[4,:,:]), num=npx)
206 # lonplt = np.linspace(-(360.0-np.min(f12lon[4,1,:])), np.max(f12lon[4,npx-1,:]), num=npx)
207 # lonpltm, latpltm = np.meshgrid(lonplt, latplt)
208 # x, y = map1(lonpltm,latpltm)
209 # cs1 = map1.contourf(x,y,np.rot90(f12[4,:,:]),clev,cmap=cmap)
210 #
211 # latplt = np.linspace(np.min(f12lat[5,:,:]), np.max(f12lat[5,:,:]), num=npx)
212 # lonplt = np.linspace(np.min(f12lon[5,:,:]), np.max(f12lon[5,:,:]), num=npx)
213 # lonpltm, latpltm = np.meshgrid(lonplt, latplt)
214 # x, y = map2(lonpltm,latpltm)
215 # cs2 = map2.contourf(x,y,np.rot90(f12[5,:,:]),clev,cmap=cmap)
216 #
217 # latplt = np.linspace(np.min(f12lat[6,:,:]), np.max(f12lat[6,:,:]), num=npx)
218 # lonplt = np.linspace(np.min(f12lon[6,:,:]), np.max(f12lon[6,:,:]), num=npx)
219 # lonpltm, latpltm = np.meshgrid(lonplt, latplt)
220 # x, y = map3(lonpltm,latpltm)
221 # cs3 = map3.contourf(x,y,np.rot90(f12[6,:,:]),clev,cmap=cmap)
222 #
223 # latplt = np.linspace(np.min(f12lat[7,:,:]), np.max(f12lat[7,:,:]), num=npx)
224 # lonplt = np.linspace(np.min(f12lon[7,:,:]), np.max(f12lon[7,:,:]), num=npx)
225 # lonpltm, latpltm = np.meshgrid(lonplt, latplt)
226 # x, y = map4(lonpltm,latpltm)
227 # cs4 = map4.contourf(x,y,np.rot90(f12[7,:,:]),clev,cmap=cmap)
228 #
229 # fig.patch.set_facecolor('grey')
230 #
231 # facemult = 1.115
232 # facedown = .016
233 #
234 # plt.tight_layout()
235 #
236 # pos1 = axN.get_position()
237 # pos2 = [pos1.x0, pos1.y0, pos1.width, pos1.height]
238 # axN.set_position(pos2)
239 #
240 # pos1 = ax1.get_position()
241 # pos2 = [pos1.x0+0.058, pos1.y0-facedown, facemult*pos1.width, facemult*pos1.height]
242 # ax1.set_position(pos2)
243 #
244 # pos1 = ax2.get_position()
245 # pos2 = [pos1.x0-0.014, pos1.y0-facedown, facemult*pos1.width, facemult*pos1.height]
246 # ax2.set_position(pos2)
247 #
248 # pos1 = ax3.get_position()
249 # pos2 = [pos1.x0-0.085, pos1.y0-facedown, facemult*pos1.width, facemult*pos1.height]
250 # ax3.set_position(pos2)
251 #
252 # pos1 = ax4.get_position()
253 # pos2 = [pos1.x0-0.155, pos1.y0-facedown, facemult*pos1.width, facemult*pos1.height]
254 # ax4.set_position(pos2)
255 #
256 # pos1 = axS.get_position()
257 # pos2 = [pos1.x0, pos1.y0, pos1.width, pos1.height]
258 # axS.set_position(pos2)
259 #
260 # plt.savefig('CubedSpherePlotCoasts_Field-'+readvar+'_Level-'+str(plot_level)+'.'+filetype, bbox_inches='tight',facecolor=fig.get_facecolor())
261 
262 #Nan out repeat locations
263 for tile in range(4):
264  for j in range(npy):
265  for i in range(npx):
266  if i<j:
267  f12[tile,j,i] = np.nan
268  if i<-j+npx:
269  f12[tile,j,i] = np.nan
270  if i>j:
271  f12[tile+8,j,i] = np.nan
272  if i>-j+npx:
273  f12[tile+8,j,i] = np.nan
274 
275 #Cat to single array with nans
276 fp = np.zeros(12*npy*npx).reshape(4*npy,3*npx)
277 fp[:,0*npx+0:npx] = np.concatenate([f12[0,:,:], f12[1,:,:], f12[2 ,:,:], f12[3 ,:,:]])
278 fp[:,1*npx:2*npx] = np.concatenate([f12[4,:,:], f12[5,:,:], f12[6 ,:,:], f12[7 ,:,:]])
279 fp[:,2*npx:3*npx] = np.concatenate([f12[8,:,:], f12[9,:,:], f12[10,:,:], f12[11,:,:]])
280 fp = np.flipud(np.transpose(fp))
281 
282 
283 #Plot
284 fig = plt.figure(figsize=(14,8))
285 cp = plt.contourf(fp,clev,cmap=cmap)
286 cbar = plt.colorbar(cp,ticks=ctic)
287 if Dim2dor3d == '3D':
288  plt.title('Cubed sphere plot of ' + readvar + itit + 'at level: ' + str(plot_level))
289 else:
290  plt.title('Cubed sphere plot of ' + readvar)
291 plt.axis('off')
292 plt.axis('equal')
293 
294 fig.patch.set_facecolor('grey')
295 
296 plt.savefig('CubedSpherePlot_Field-'+readvar+'_Level-'+str(plot_level)+'.'+filetype, bbox_inches='tight',facecolor=fig.get_facecolor())
str
Definition: c2f.py:15