/
Ozone_Dobson.py
276 lines (224 loc) · 7.31 KB
/
Ozone_Dobson.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
from bpch import bpch
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib import ticker
#import temp_map2D
import os
import datetime
import pygchem.diagnostics as gdiag
import numpy as np
import csv
import glob
import datetime as datetime
import sys
species = 'O3'
group='IJ-AVG-$'
debug=True
month = '200607'
def main(species,group,debug):
wd, Output_Name = get_arguments()
data, units, start_time, end_time = get_species_data_from_bpch(wd,species,group, month)
if debug:
print 'raw data dimenstion = ' + str(data.dimensions)
plot_the_data(data, units, start_time, end_time, Output_Name, debug=debug)
return;
# Ask for arguments
def get_arguments():
print 'Getting the arguments.'
try:
wd = sys.argv[1]
Output_Name= sys.argv[2]+'.png'
except:
sys.exit('Please input arguments of working dir and output png')
return wd, Output_Name;
def plot_the_data(data, units, start_time, end_time, Output_Name, debug=False):
print 'Plotting the data.'
#set up plot
# print data
# print data.dimensions
units = 'DU'
colourbar_tick_size = 20
colourbar_title = species + '('+units+')'
colourbar_title_size = 25
colourbar_number_ticks = 5
X_axis_title = 'Longitude'
X_axis_size = 25
Y_axis_title = 'Latitude'
Y_axis_size = 25
Plot_title = species + ' average between '+ str(start_time) + ' and ' + str(end_time)
# Chose the figure size
fig=plt.figure(figsize=(20,12))
ax = fig.add_axes([.05, .1, .9, .8])
fig.patch.set_facecolor('white')
# Add the basemap
lat = np.arange(-88,90,4)
lat = np.insert(lat,0,-90)
lat = np.append(lat,90)
lon = np.arange(-182.5,179,5)
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,\
llcrnrlon=-182.5,\
urcrnrlon=177.5,\
resolution='c')
m.drawcoastlines()
m.drawmapboundary()
parallels = np.arange(-90,91,30)
meridians = np.arange(-180,151,30)
plt.xticks(meridians)
plt.yticks(parallels)
m.drawparallels(parallels)
m.drawmeridians(meridians)
x, y = np.meshgrid(*m(lon, lat))
# #Add the map
poly = m.pcolor(lon, lat, data)
plt.xlabel(X_axis_title, fontsize = X_axis_size)
plt.ylabel(Y_axis_title, fontsize = Y_axis_size)
# Add colourbar title
cb = plt.colorbar(poly, shrink=0.7)#,#orientation = 'horizontal')
tick_locator = ticker.MaxNLocator(nbins=colourbar_number_ticks)
cb.locator = tick_locator
cb.ax.tick_params(labelsize=colourbar_tick_size)
cb.update_ticks()
plt.text(191,93,colourbar_title,fontsize='25')
# Add the title
print species
print units
print start_time
print end_time
print Plot_title
plt.title( Plot_title, fontsize='30' )
fig.savefig( Output_Name, transparent=True )
return ;
def get_species_data_from_bpch(wd,species,group,month, debug=True):
print 'Extracting data from the bpch.'
bpch_fname = wd + month + ".ctm.bpch"
# print months
# for month in months:
bpch_data = bpch(bpch_fname )
if debug:
print 'Data groups:'
print bpch_data.groups
print 'Group species:'
print bpch_data.groups[group].variables
species_data = bpch_data.groups[group].variables[species]
units = bpch_data.groups[group].variables[species].units
start_time = bpch_data.groups[group].variables['tau0']
end_time = bpch_data.groups[group].variables['tau1']
start_time, end_time = extract_month(start_time, end_time)
# #Turn species data from ppbv to moles
#Convert from ppbv to species_moles
air_mass = bpch_data.groups['BXHGHT-$'].variables['AD']
air_moles = (air_mass*1E3)# / ( 0.78*28.0 + 0.22*32.0 )
species_moles = air_moles * species_data
# Get tropopause information
Fraction_of_time_in_the_troposphere = bpch_data.groups["TIME-TPS"].variables['TIMETROP']
if debug:
print Fraction_of_time_in_the_troposphere
# Extract only moles in the troposphere
# by multipling the 4d species concentrations by the fraction of time they were in the troposphere ( 1 = aff trop, 0 = no trop, other = tropopause ).
species_moles = np.multiply(species_moles[:,:38,:,:] , Fraction_of_time_in_the_troposphere)
if debug:
print 'species_moles shape = ' + str(species_moles.shape)
# get the total column moles.
total_column = species_moles[0,:,:,:].sum( axis=0 ) # total_column[t,lat,lon]
if debug:
print' total_column shape = ' + str(total_column.shape)
# Get the surface area
print 'Getting the surface area.'
surface_area = bpch_data.groups['DXYP'].variables['AREA']
if debug:
print 'surface_area shape = ' + str(surface_area.shape)
# surface_area[ lat, long ]
#Convert from total mols to total Volume
conversion_factor = 1E-7#1E-12 * 6.02e23 / 2.69e20
column_volume = np.multiply( total_column, conversion_factor )
#Convert to Dobson units.
dobson_data = np.divide( column_volume , surface_area )
if debug:
print 'dobson_data shape = ' + str(dobson_data.shape)
return dobson_data, units, start_time, end_time;
def extract_month(start_time, end_time):
# Turn time from hours from the equinox to YYYY-MM-DD
epoch = datetime.datetime(1985,1,1)
start_time = epoch + datetime.timedelta(hours=start_time[0])
end_time = epoch + datetime.timedelta(hours=end_time[0])
# Obtain only the date
start_time = start_time.date()
end_time = end_time.date()
return start_time, end_time;
##lat = f.dimensions['lat']
#
##lat = f.variables['lat']
##lon = f.variables['lon']
#
#lat = np.arange(-88,90,4)
#lat = np.insert(lat,0,-90)
#lat = np.append(lat,90)
#
#lon = np.arange(-182.5,179,5)
#print lon
#tautime = f.variables['tau0']
#tautime_end = f.variables['tau1']
#
#print len(lat)
#print len(lon)
#
#group = f.groups['IJ-AVG-$']
#
#ozone = group.variables[species]
#units = ozone.units
#
#
##d = ozone.variables['tau0']
#print units
#print ozone.dimensions
#
#layer1 = ozone[:,0,:,:]
#layer1 = np.average(layer1,axis=0)
#
#reference=datetime.datetime(1985, 1, 1)
#
#time =[]
#end_time=[]
#for t in tautime:
# time.append(reference + datetime.timedelta(hours=t))
#
#for t in tautime_end:
# end_time.append(reference + datetime.timedelta(hours=t))
#
#m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,\
# llcrnrlon=-182.5,\
# urcrnrlon=177.5,\
# resolution='c')
#
#m.drawcoastlines()
#m.drawmapboundary()
#parallels = np.arange(-90,91,15)
#meridians = np.arange(-180,151,30)
#
#
#plt.xticks(meridians)
#plt.yticks(parallels)
#
#m.drawparallels(parallels)
#m.drawmeridians(meridians)
#
#x, y = np.meshgrid(*m(lon, lat))
#
#print layer1.shape
#poly = m.pcolor(lon, lat, layer1)
#cb = plt.colorbar(poly, ax = m.ax,shrink=0.8)#,#orientation = 'horizontal')
##cb.set_label('%s (%s)'%(species,units))
#plt.xlabel('Longitude',fontsize = 20)
#plt.ylabel('Latitude',fontsize = 20)
#plt.text(191,93,'%s (%s)'%(species,units),fontsize=20)
#
#plt.title('%s at Surface, %s to %s'%(species,time[0],end_time[-1]),fontsize=20)
#
##plt.tight_layout()
#plt.savefig(Output_Name, transparent=True )
##plt.show()
#
## create 2D map(s) by averaging over a level interval
##for diag in diagnostics:
# # temp_map2D.map_level_avg(diag, 0, 0)
main(species,group,debug)