forked from MPAS-Dev/MPAS-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh_comparison.py
executable file
·301 lines (233 loc) · 10.6 KB
/
mesh_comparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import numpy as np
from mpl_toolkits.basemap import Basemap
plt.switch_backend('agg')
rad2deg = 180/np.pi
# Pre-defined plotting boxes
Western_Atlantic = np.array([-98.186645, -59.832744, 7.791301 ,45.942453])
Contiguous_US = np.array([-132.0,-59.832744,7.791301,51.0])
North_America = np.array([-175.0,-60.0,7.5,72.0])
Entire_Globe = np.array([-180,180,-90,90])
##############################################################
# Functions
##############################################################
class comparison_plots:
def __init__(self,nmesh,var,savepaths):
# Initialize varaiables common to all methods
self.nmesh = nmesh
self.var = var
self.savepaths = savepaths
def plot_field(self,lon,lat,data,mesh_name,i,plot_box,bounds=''):
print " plotting field: " + self.var
# Get the data inside the plotting box
lon_plot,lat_plot,data_plot = self.get_data_inside_box(lon,lat,data,plot_box)
# Initialize the figure and create subplot
if i == 1:
self.field = plt.figure()
else:
plt.figure(self.field.number)
ax = self.field.add_subplot(nmesh,1,i)
# Set the color scale bounds
if bounds:
levels = np.linspace(bounds[0],bounds[1],100)
else:
levels = np.linspace(np.amin(data_plot),np.amax(data_plot),100)
# Plot the data contours
cplot = ax.tricontourf(lon_plot,lat_plot,data_plot,levels=levels)
# Plot the land, coastlines, and lat/lon lines
m = Basemap(projection='cyl',llcrnrlat=plot_box[2],urcrnrlat=plot_box[3],\
llcrnrlon=plot_box[0],urcrnrlon=plot_box[1],resolution='c')
m.fillcontinents(color='grey',lake_color='white')
m.drawcoastlines()
m.drawparallels(np.arange(plot_box[2],plot_box[3],20),labels=[True, False, False, False])
m.drawmeridians(np.arange(plot_box[0],plot_box[1],60),labels=[False,False, False, True])
# Add the colorbar
cb = self.field.colorbar(cplot,ax=ax)
cb.set_label(self.var.lower())
# Add axis and figure lables
ax.set_title(mesh_name)
ax.axis(plot_box)
self.field.suptitle(self.var,y=1.05,fontweight='bold')
self.field.tight_layout()
# Save the figure
for path in self.savepaths:
self.field.savefig(path+self.var.replace(' ','')+'_field.png',bbox_inches='tight')
##############################################################
def plot_hist(self,data,mesh_name,i,bounds='',bins=''):
print " plotting histogram: " + self.var
# Initialize the figure and create subplot
if i == 1:
self.hist = plt.figure()
else:
plt.figure(self.hist.number)
ax = self.hist.add_subplot(nmesh,1,i)
# Plot a histogram of the data
if bins:
ax.hist(data,bins)
else:
ax.hist(data,'auto')
# Add axis and figure labels
ax.set_title(mesh_name + '\n(Number of cells = '+str(data.size)+')')
ax.set_ylabel('count')
ax.set_xlabel(self.var.lower())
self.hist.suptitle(self.var,y=1.05,fontweight='bold')
self.hist.tight_layout()
# Adjust the axis limits
if bounds:
ax.set_xlim([bounds[0],bounds[1]])
ax.set_ylim([bounds[2],bounds[3]])
else:
self.make_axis_same(self.hist)
# Add percentage axis
ax2 = ax.twinx()
ax_ylim = ax.get_ylim()
ax2.set_ylim([0,ax_ylim[1]/data.size*100])
ax2.set_ylabel('percentage')
# Save the figure
for path in self.savepaths:
self.hist.savefig(path+self.var.replace(' ','')+'_hist.png',bbox_inches='tight')
##############################################################
def plot_latavg(self,lat,data,mesh_name,i,bounds='',binsize=1.0):
print " plotting lat average: " + self.var
# Initialize the latitude vectors
lat_bins = np.arange(-90.0,90.0,binsize)
lat_avg = np.zeros(lat_bins.shape)
lat_min = np.zeros(lat_bins.shape)
lat_max = np.zeros(lat_bins.shape)
for j in range(lat_bins.size-1):
# Find cells in this bin
idx = np.where((lat > lat_bins[j]) & (lat <= lat_bins[j+1]))
lat_data = data[idx]
# Find average, minimum, and maximum of the data in this bin
# (if no data is in the bin, set to NaN so it is ignored in the plot)
if lat_data.size > 0:
lat_avg[j] = np.mean(lat_data)
lat_min[j] = np.min(lat_data)
lat_max[j] = np.max(lat_data)
else:
lat_avg[j] = np.nan
lat_min[j] = np.nan
lat_max[j] = np.nan
# Initialize the figure and add subplot
if i == 1:
self.latavg = plt.figure()
else:
plt.figure(self.latavg.number)
ax = self.latavg.add_subplot(nmesh,1,i)
# Plot the average, minimum and maximum
ax.fill_between(lat_bins,lat_min,lat_max,alpha=.5)
ax.plot(lat_bins,lat_avg)
# Add axis and figure labels
ax.set_title(mesh_name)
ax.set_xlabel('latitude')
ax.set_ylabel('average '+self.var.lower())
self.latavg.suptitle(self.var,y=1.05,fontweight='bold')
self.latavg.tight_layout()
# Adjust the axis limits
if bounds:
ax.set_xlim([bounds[0],bounds[1]])
ax.set_ylim([bounds[2],bounds[3]])
else:
self.make_axis_same(self.latavg)
# Save the figure
for path in self.savepaths:
self.latavg.savefig(path+self.var.replace(' ','')+'_lat.png',bbox_inches='tight')
#############################################################
def get_data_inside_box(self,lon,lat,data,box):
# Find indicies of coordinates inside bounding box
lon_idx, = np.where((lon > box[0]) & (lon < box[1]))
lat_idx, = np.where((lat > box[2]) & (lat < box[3]))
# Get region data inside bounding box
latlon_idx = np.intersect1d(lat_idx,lon_idx)
lon_region = lon[latlon_idx]
lat_region = lat[latlon_idx]
z_region = data[latlon_idx]
return (lon_region,lat_region,z_region)
#############################################################
def make_axis_same(self,fig):
# Initialize the axis limits
xlim_all = [1e99,-1e99]
ylim_all = [1e99,-1e99]
# Find the lower and upper bounds of the subplot axes limits
axes = fig.get_axes()
for ax in axes:
if ax.get_ylabel() != 'percentage':
xlim = ax.get_xlim()
xlim_all[0] = min(xlim_all[0],xlim[0])
xlim_all[1] = max(xlim_all[1],xlim[1])
ylim = ax.get_ylim()
ylim_all[0] = min(ylim_all[0],ylim[0])
ylim_all[1] = max(ylim_all[1],ylim[1])
# Set all axes limits to the lower and upper bounds
for ax in axes:
if ax.get_ylabel() != 'percentage':
ax.set_xlim(xlim)
ax.set_ylim(ylim)
##############################################################
# Configuration Section
##############################################################
meshes = [
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/EC60to30/spin_up/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'EC60to30V3'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/EC60to30JS/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'EC60to30V4 (Jigsaw)'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USNAEC60to30cr10/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USNAEC60to30cr10'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USNARRS30to10cr10/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USNARRS30to10cr10'}
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USNARRS30to10cr5/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USNARRS30to10cr5'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USNARRS30to10cr1/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USNARRS30to10cr1'},
{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USDEQU120cr1/init/mesh_metrics/',
'file':'mesh_with_metrics.nc',
'name':'USDEQU120cr1'},
{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USDEQU240cr1/init/mesh_metrics/',
'file':'mesh_with_metrics.nc',
'name':'USDEQU240cr1'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USDERRS30to10cr1/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USDERRS30to10cr1'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USDERRS30to10cr500m/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USDERRS30to10cr500m'},
#{'path':'/users/sbrus/scratch1/MPAS-O_testing/ocean/global_ocean/USDERRS30to10cr100m/init/mesh_metrics/',
# 'file':'mesh_with_metrics.nc',
# 'name':'USDERRS30to10cr100m'}
]
plot_box = Entire_Globe
cell_width_bounds = [0.9,275.0]
##############################################################
# Main Program
##############################################################
# Initialize comparison_plots class instances for each mesh metric
nmesh = len(meshes)
savepaths = [mesh['path'] for mesh in meshes]
cell_size = comparison_plots(nmesh,'Cell Width',savepaths)
cell_qual = comparison_plots(nmesh,'Cell Quality',savepaths)
for i,mesh in enumerate(meshes):
print "Mesh: " + mesh['name']
# Open mesh and read in cell coordinates and data
ds = Dataset(mesh['path']+mesh['file'],'r')
loncell = (np.mod(ds.variables['lonCell'][:]+np.pi,2*np.pi)-np.pi)*rad2deg
latcell = ds.variables['latCell'][:]*rad2deg
areaCell = ds.variables['areaCell'][:]
cellQuality = ds.variables['cellQuality'][:]
# Estimate cell width based on cell area using equal-area circle diameter
cellWidth = 2.0*np.sqrt(areaCell/np.pi)/1000
# Plot fields on globe
cell_size.plot_field(loncell,latcell,cellWidth, mesh['name'],i+1,plot_box,cell_width_bounds)
cell_qual.plot_field(loncell,latcell,cellQuality,mesh['name'],i+1,plot_box,[0.0,1.0])
# Plot histograms
cell_size.plot_hist(cellWidth, mesh['name'],i+1,bins=100)
cell_qual.plot_hist(cellQuality,mesh['name'],i+1,bins=20)
# Plot latitude averages
cell_size.plot_latavg(latcell,cellWidth, mesh['name'],i+1)
cell_qual.plot_latavg(latcell,cellQuality,mesh['name'],i+1)