Newer
Older
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
from datetime import datetime as dt
from datetime import timedelta as delta
import logging
import pandas as pd
from netCDF4 import MFDataset, MFTime
import numpy as np
import matplotlib.pyplot as plt
import math
CHOICES = ['air_temp', 'dewpoint',
'rh', 'pressure', 'wind_speed', 'wind_dir', 'accum_precip', 'solar_flux']
TITLES = {'air_temp': 'Temperature and Dewpoint(°C)',
'rh': 'Relative Humidity(%)', 'pressure': 'Pressure(hpa)',
'wind_speed': 'Wind Speed(m/s)', 'wind_dir': 'Wind Direction(°)',
'accum_precip': 'Accumulated Precipitation Since 00Z (mm)',
'solar_flux': 'Solar Flux(W/m^2)'}
IND_TITLES = {'air_temp': 'Temperature and Dewpoint',
'rh': 'Relative Humidity', 'pressure': 'Pressure',
'wind_speed': 'Wind Speed', 'wind_dir': 'Wind Direction',
'accum_precip': 'Accumulated Precipitation Since 00Z',
'solar_flux': 'Solar Flux'}
# The purpose of this method is to take a string in the format
# YYYY-MM-DDTHH:MM:SS and convert that to a datetime object
# or a string in the format YY-MM-DD and convert that to a datetime object
# used in coordination with argparse -s and -e params
# @param datetime string
# @return datetime object
def _dt_convert(datetime_str):
#parse datetime string, return datetime object
try:
return dt.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S')
except:
return dt.strptime(datetime_str, '%Y-%m-%d')
# The purpose of this method is to take a list of level_b1 filepaths
# and convert those into a pandas frame with only good data
# @param input_files - list of level_b1 filepaths
# @return frame - pandas dataframe with only good data
def get_data(input_files):
files = MFDataset(input_files)
#dictionary with var_name: good data pairs
dataDict = {}
#get the data from the files
for name in CHOICES:
data = files.variables[name][:]
qc_data = files.variables['qc_' + name][:]
dataDict[name] = data
dataDict['qc_' + name] = qc_data
#for some reason, time has floating point precision issues
#so I'm using base_time and offsets instead
#might need to fix later
base_time = files.variables['base_time'][:]
#convert base_time epoch format into date_time object
base_time_obj = dt(1970,1,1) + delta(seconds=int(base_time))
#get offsets from files
off_sets = files.variables['time_offset']
#convert that into offsets from the first file's base_time
off_sets = MFTime(off_sets)[:]
#for each offset, convert that into a datetime object
stamps = [base_time_obj + delta(seconds=int(s)) for s in off_sets]
#append all stamps into the data frame
dataDict['stamps'] = stamps
return pd.DataFrame(dataDict).set_index(['stamps'])
# The purpose of this method is to use the qc mask
# to only get the good data
# @param qc_data - np array with nan for good values and various set bits for others
# @param stamps - all stamps corresponding to data
# @param data - data
# @return good data and their corresponding stamps
def get_good_data(qc_data, stamps, data):
#all good data and stamps associated with them
good_data = []
good_stamps = []
#get good indices
good_indices = np.where(np.isnan(qc_data))[0]
for idx in good_indices:
#get good_data
good_data.append(data[idx])
good_stamps.append(stamps[idx])
return [good_data, good_stamps]
# The purpose of this method is to determines all the 12:00:00 days
# within a start and end date
# @param cur_dt - start time
# @param dates - end time
# @return dates in lists
def get_dates_in_range(cur_dt, end):
curr = cur_dt
while curr <= end:
yield curr
curr += delta(days=1)
# The purpose of this method is to get the min and max of
# an array of stamps and determines all the 12:00:00 days
# in that array
# @param dewpoint stamps - time stamps for valid dewpoint data. Only specified if
# air temp and dew point need to be ploted in the same plot
# @param dates - time stamps for data
# @return datesInRange - all days with 12:00:00
def find_half_days(dewpoint_stamps, dates):
date_max = np.amax(dates)
date_min = np.amin(dates)
if(dewpoint_stamps):
dew_max = np.amax(dewpoint_stamps)
dew_min = np.amin(dewpoint_stamps)
date_max = max(date_max, dew_max)
date_min = min(date_min, dew_min)
cur_dt = date_min.replace(hour=12, minute=0, second=0)
if cur_dt < date_min:
cur_dt += delta(days=1)
datesInRange = list(get_dates_in_range(cur_dt, date_max))
return datesInRange
# The purpose of this method is to create a thumbnail plot
# @param frame - all data I need in a data frame
# @param ymin - lower limit of the y axis if specified. set only when --daily is given
# @param ymax - upper limit of yaxis if specified. Set only when --daily is given
# @param dewpoint_stamps - timestamps for valid dewpoint data. Only specified if
# air temp and dew point need to be plotted in the same plot
# @param dewpoint_data - valid dewpoint data. Only specified if
# air temp and dew point need to be plotted in the same plot
#@param output - output filename pattern
def thumbnail_plot(dates, data, ymin, ymax,
dewpoint_stamps, dewpoint_data, output, wind_dir):
if(dewpoint_stamps):
plt.plot(dates, data, 'r', dewpoint_stamps, dewpoint_data, 'b')
plt.axis([min(dates), max(dates), ymin, ymax])
#plt.plot(dewpoint_stamps, dewpoint_data, 'b')
elif(wind_dir):
plt.plot(dates, data, 'ko', markersize=0.1)
plt.axis([min(dates), max(dates), 0, 360])
else:
plt.plot(dates, data, 'k')
plt.axis([min(dates), max(dates), ymin, ymax])
#scale
fig = plt.gcf()
dpi = fig.get_dpi()
fig.set_size_inches(80/float(dpi), 80/float(dpi))
#create axes
axes = plt.gca()
axes.axes.get_xaxis().set_ticklabels([])
axes.axes.get_xaxis().set_tick_params(length=7)
axes.axes.get_yaxis().set_ticklabels([])
axes.axes.get_yaxis().set_visible(False)
axes.spines['right'].set_visible(False)
axes.spines['bottom'].set_visible(False)
#got the basic shape of the plot
#still need to draw a line in the middle
datesInRange = find_half_days(dewpoint_stamps, dates)
for date in datesInRange:
plt.axvline(x=date, color='k')
# The purpose of this method is to create a thumbnail
# @param frame - all data I need in a data frame
# @param ymin - lower limit of the y axis if specified. set only when --daily is given
# @param ymax - upper limit of yaxis if specified. Set only when --daily is given
# @param create_air_dew_plot - boolean specifying if air temp and dewpoint
# @param output - output filename pattern
# should be in same plot
def create_thumbnail(frame, ymin, ymax, create_air_dew_plot, output):
#see if we're going to need subplots
#subplots needed when len is greater than 2 for one variable
#or greater than 4 for two variables
need_subplots = len(list(frame.columns.values)) > 2 and not create_air_dew_plot
need_subplots = need_subplots or len(list(frame.columns.values)) > 4 and create_air_dew_plot
if need_subplots:
plots_created = 1
if create_air_dew_plot:
numberPlots = (len(list(frame.columns.values)) - 2) / 2
plotNumber = numberPlots * 100 + 10
else:
numberPlots = len(list(frame.columns.values)) / 2
plotNumber = numberPlots * 100 + 10
stamps = list(frame.index)
#get dewpoint data
if create_air_dew_plot:
all_data = frame['dewpoint']
qc_data = frame['qc_dewpoint']
good_list = get_good_data(qc_data, stamps, all_data)
dewpoint_data = good_list[0]
dewpoint_stamps= good_list[1]
for name in CHOICES:
if name == 'dewpoint' and create_air_dew_plot:
continue
if name not in list(frame.columns.values):
continue
elif need_subplots:
plt.subplot(plotNumber + plots_created)
plots_created += 1
all_data = frame[name]
qc_data = frame['qc_' + name]
good_list = get_good_data(qc_data, stamps, all_data)
good_data = good_list[0]
good_stamps= good_list[1]
#if we don't need two lines in same plot, plot
if not (create_air_dew_plot and name == 'air_temp'):
if name != 'wind_dir':
thumbnail_plot(good_stamps, good_data, ymin[name], ymax[name],
None, None, output, False)
if name == 'wind_dir':
thumbnail_plot(good_stamps, good_data, ymin[name], ymax[name],
None, None, output, True)
else:
thumbnail_plot(good_stamps, good_data, ymin[name], ymax[name],
dewpoint_stamps, dewpoint_data, output, False)
plt.savefig(output + '.thumbnail.png')
def full_plot_stamps(stamps):
first_stamp = np.sort(stamps)[0]
first_stamp = first_stamp.replace(hour=0, minute=0, second=0)
#print([((s - first_stamp).total_seconds()/3600) for s in stamps])
#print([(int((s - first_stamp).total_seconds()) / 3600) for s in stamps])
return [((s - first_stamp).total_seconds()/3600) for s in stamps]
# The purpose of this method is to change how the y ticks are shown
# @param min - minimum of the y range right now
# @param max - maximum of the y range right now
# @return new tick labels
def get_new_labels(mini, maxi):
delta = math.ceil((maxi - mini) / 6)
return np.arange(mini, (mini + delta*6), delta)
# The purpose of this method is to create a full_size plot
# @param frame - all data I need in a data frame
# @param ymin - lower limit of the y axis if specified. set only when --daily is given
# @param ymax - upper limit of yaxis if specified. Set only when --daily is given
# @param dewpoint_stamps - timestamps for valid dewpoint data. Only specified if
# air temp and dew point need to be plotted in the same plot
# @param dewpoint_data - valid dewpoint data. Only specified if
# air temp and dew point need to be plotted in the same plot
# @param output - output filename pattern
# @param wind_direction - says different yaxis labels
# @param accum_precip - different yaxis handling
# @param see if we need subplots
def full_plot(dates, data, ymin, ymax,
dewpoint_stamps, dewpoint_data, output, wind_dir,
accum_precip, need_subplots):
dates = full_plot_stamps(dates)
if(dewpoint_stamps):
dewpoint_stamps = full_plot_stamps(dewpoint_stamps)
plt.plot(dates, data, 'r', dewpoint_stamps, dewpoint_data, 'b')
plt.axis([min(dates), max(dates), ymin, ymax])
#labels = [math.ceil(float(item.get_text())) for item in ticks]
#plt.plot(dewpoint_stamps, dewpoint_data, 'b')
elif(wind_dir):
plt.plot(dates, data, 'ko', markersize=1)
plt.axis([min(dates), max(dates), 0, 360])
axes = plt.gca()
axes.get_yaxis().set_ticks([0,90,180,270])
else:
plt.plot(dates, data, 'k')
plt.axis([min(dates), max(dates), ymin, ymax])
if not need_subplots:
return
#get axes
axes = plt.gca()
#if not wind_direction, reset yaxis ticks
if not wind_dir:
if not accum_precip:
mini, maxi = axes.get_ylim()
new_labels = get_new_labels(mini, maxi)
else:
mini, maxi = axes.get_ylim()
delta = ((maxi - mini)/6)
new_labels = np.arange(mini, (mini + delta*6), delta)
if len(new_labels) == 0:
new_labels = [0, 0.05, 0.1]
plt.yticks(new_labels)
axes.get_yaxis().get_major_ticks()[-1].set_visible(False)
#hid last tick because for subplots above the last one, it's peeping out...
# will show for the last subplot or when subplots are not needed
# in other words, this code should only affect plots above the last plot
# only when sublots are needed
axes.get_xaxis().get_major_ticks()[-1].set_visible(False)
axes.get_xaxis().get_major_ticks()[0].set_visible(False)
# The purpose of this method is to get the subtitle's y coordinate
# based upon how many subplots there are
# @param numSubPlots - number of subplots
# @return y-coordinate of subtitle
def get_subtitle_location(numSubPlots):
return 1 - 0.05*numSubPlots
# The purpose of this method is to create a full size quicklook
# @param frame - all data I need in a data frame
# @param ymin - lower limit of the y axis if specified. set only when --daily is given
# @param ymax - upper limit of yaxis if specified. Set only when --daily is given
# @param create_air_dew_plot - boolean specifying if air temp and dewpoint
# should be in same plot
def create_full_plot(frame, ymin, ymax, create_air_dew_plot, output):
#see if we're going to need subplots
#subplots needed when len is greater than 2 for one variable
#or greater than 4 for two variables
need_subplots = len(list(frame.columns.values)) > 2 and not create_air_dew_plot
need_subplots = need_subplots or len(list(frame.columns.values)) > 4 and create_air_dew_plot
#get need supplot vars
if need_subplots:
plots_created = 1
if create_air_dew_plot:
numberPlots = (len(list(frame.columns.values)) - 2) / 2
plotNumber = numberPlots * 100 + 10
else:
numberPlots = len(list(frame.columns.values)) / 2
plotNumber = numberPlots * 100 + 10
stamps = list(frame.index)
#get dewpoint data
if create_air_dew_plot:
all_data = frame['dewpoint']
qc_data = frame['qc_dewpoint']
good_list = get_good_data(qc_data, stamps, all_data)
dewpoint_data = good_list[0]
dewpoint_stamps= good_list[1]
for name in CHOICES:
#if dewpoint with temp or a name not in frame
# continue
if name == 'dewpoint' and create_air_dew_plot:
continue
if name not in list(frame.columns.values):
continue
all_data = frame[name]
qc_data = frame['qc_' + name]
good_list = get_good_data(qc_data, stamps, all_data)
good_data = good_list[0]
good_stamps= good_list[1]
dateString = good_stamps[0].strftime('%m/%d/%Y')
# create plots
if need_subplots:
if plots_created == 1:
plt.figure().suptitle('AO&SS Building Tower Meteorogram ' + dateString, fontsize=13)
ax1 = plt.subplot(plotNumber + plots_created)
ax1.set_title(TITLES[name], x=0.5, y=get_subtitle_location(numberPlots), fontsize=8)
else:
curr_plot = plt.subplot(plotNumber + plots_created, sharex=ax1)
curr_plot.set_title(TITLES[name], x=0.5, y=get_subtitle_location(numberPlots), fontsize=8)
plots_created += 1
else:
plt.figure().suptitle('AO&SS Building Tower ' + IND_TITLES[name] + ' ' + dateString, fontsize=13)
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
plt.ylabel(TITLES[name])
#if we don't need two lines in same plot, plot
if not (create_air_dew_plot and name == 'air_temp'):
full_plot(good_stamps, good_data, ymin[name], ymax[name],
None, None, output, name == 'wind_dir',
name == 'accum_precip', need_subplots)
else:
full_plot(good_stamps, good_data, ymin[name], ymax[name],
dewpoint_stamps, dewpoint_data, output, False, False,
need_subplots)
if need_subplots:
plt.subplots_adjust(hspace=0, bottom=0.125)
plt.xlabel('Time (UTC)')
#reverse code done in create_full_plot
axes = plt.gca()
axes.get_xaxis().get_major_ticks()[-1].set_visible(True)
axes.get_xaxis().get_major_ticks()[0].set_visible(True)
plt.savefig(output + '.png')
def main():
import argparse
#argparse description
parser = argparse.ArgumentParser(description="Use data from level_b1 netCDF files to create netCDF files")
#argparse verbosity info
parser.add_argument('-v', '--verbose', action='count',
default=int(os.environ.get("VERBOSITY", 2)),
dest='verbosity',
help=('each occurence increases verbosity 1 level through'
+ ' ERROR-WARNING-INFO-DEBUG (default INFO)'))
#argparse start and end times
parser.add_argument('-s', '--start-time', type=_dt_convert,
help="Start time of plot. If only -s is given, a plot of " +
"only that day is created. Formats allowed: \'YYYY-MM-DDTHH:MM:SS\', \'YYYY-MM-DD\'")
parser.add_argument('-e', '--end-time', type=_dt_convert,
help="End time of plot. If only -e is given, a plot of only that day is " +
"created. Formats allowed: \'YYYY-MM-DDTHH:MM:SS\', \'YYYY-MM-DD\'")
#netcdf file paths
parser.add_argument("input_files", nargs="+", help="aoss_tower_level_b1 paths")
#output filename pattern
parser.add_argument('-o', '--output', help="filename pattern")
#thumbnail or full
parser.add_argument('-t', '--thumb-nail', action='store_true', help="if specified, script creates a thumbnail")
#plot names
parser.add_argument('--varnames', nargs="+", choices=CHOICES,
required=True,
help="the variable names for the desired plot. Valid choices: air_temp, " +
"dewpoint, rh, pressure, wind_speed, wind_dir, accum_precip, solar_flux")
#--daily flag
parser.add_argument('-d', '--daily', action='store_true',
help="creates a plot for every day. Usually used to create plots " +
"that will line up for aoss tower quicklooks page")
args = parser.parse_args()
levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
level = levels[min(3, args.verbosity)]
logging.basicConfig(level=level)
var_names = args.varnames
create_air_dew_plot = False
#see if we need to plot air_temp and dewpoint on the same plot
if 'air_temp' in var_names and 'dewpoint' in var_names:
create_air_dew_plot = True
var_names.remove('dewpoint')
#get data
frame = get_data(args.input_files)
#only have the data we need
for name in CHOICES:
if name == 'dewpoint' and create_air_dew_plot:
continue
if name not in var_names:
del frame[name]
del frame['qc_' + name]
#frame only contains data from start-end times
if(args.start_time and args.end_time):
start_filter = args.start_time.strftime('%Y-%m-%d %H:%M:%S')
end_filter = args.end_time.strftime('%Y-%m-%d %H:%M:%S')
frame = frame[start_filter: end_filter]
#frame only contains data from start-end of that day
elif(args.start_time):
end_time = args.start_time.replace(hour=23, minute=59, second=59)
start_filter = args.start_time.strftime('%Y-%m-%d %H:%M:%S')
end_filter = end_time.strftime('%Y-%m-%d %H:%M:%S')
frame = frame[start_filter: end_filter]
ymin = {}
ymax = {}
if not args.daily:
for name in CHOICES:
if name == 'dewpoint' and create_air_dew_plot:
continue
if name not in frame:
continue
ymin[name] = None
ymax[name] = None
if(args.thumb_nail):
create_thumbnail(frame, ymin, ymax, create_air_dew_plot, args.output)
else:
create_full_plot(frame, ymin, ymax, create_air_dew_plot, args.output)
else:
for name in CHOICES:
if name == 'dewpoint' and create_air_dew_plot:
continue
if name not in frame:
continue
if name == 'accum_precip':
ymin[name] = frame[name].min()
ymax[name] = frame[name].max()
else:
ymin[name] = math.floor(frame[name].min())
ymax[name] = math.ceil(frame[name].max())
frameList = [(group[1]) for group in frame.groupby(frame.index.day)]
counter = 0
for frame in frameList:
if args.thumb_nail:
create_thumbnail(frame, ymin, ymax, create_air_dew_plot, args.output.format(str(counter)))
else:
create_full_plot(frame, ymin, ymax, create_air_dew_plot, args.output.format(str(counter)))
counter += 1
plt.gcf().clear()
if __name__ == "__main__":
main()