1 |
#!/usr/bin/env python |
---|
2 |
# Last modified: Time-stamp: <2009-06-19 15:41:36 haines> |
---|
3 |
"""test add nan in time gap of data so plots lift pen at gap""" |
---|
4 |
|
---|
5 |
# plot each month |
---|
6 |
|
---|
7 |
import os, sys, glob, re |
---|
8 |
import datetime, time, dateutil, dateutil.tz |
---|
9 |
import pycdf |
---|
10 |
import numpy |
---|
11 |
sys.path.append('/home/haines/nccoos/raw2proc') |
---|
12 |
del(sys) |
---|
13 |
os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/" |
---|
14 |
from pylab import figure, twinx, savefig, setp, getp, cm, colorbar |
---|
15 |
from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date |
---|
16 |
import procutil |
---|
17 |
|
---|
18 |
proc_dir = '/seacoos/data/nccoos/level1/crow/wq' |
---|
19 |
fns = glob.glob((os.path.join(proc_dir, '*.nc'))) |
---|
20 |
fns.sort() |
---|
21 |
|
---|
22 |
fn = '/seacoos/data/nccoos/level1/crow/wq/crow_wq_2005_05.nc' |
---|
23 |
m=re.search('\d{4}_\d{2}', fn) |
---|
24 |
yyyy_mm = m.group() |
---|
25 |
prev_month, this_month, next_month = procutil.find_months(yyyy_mm) |
---|
26 |
|
---|
27 |
print ' ... ... read: ' + fn |
---|
28 |
nc = pycdf.CDFMF((fn,)) |
---|
29 |
ncvars = nc.variables() |
---|
30 |
es = nc.var('time')[:] |
---|
31 |
units = nc.var('time').units |
---|
32 |
dt = [procutil.es2dt(e) for e in es] |
---|
33 |
dn = date2num(dt) |
---|
34 |
batt = nc.var('battvolts')[:] |
---|
35 |
nc.close() |
---|
36 |
|
---|
37 |
data = batt |
---|
38 |
delta_dn = numpy.diff(dn) |
---|
39 |
delta_dt = numpy.diff(dt) |
---|
40 |
sample_interval = numpy.median(delta_dn) |
---|
41 |
maxdelta = 2*sample_interval |
---|
42 |
|
---|
43 |
igap = (delta > maxdelta).nonzero()[0] |
---|
44 |
ngap = len(igap) |
---|
45 |
sample_interval = datetime.timedelta(sample_interval) |
---|
46 |
|
---|
47 |
|
---|
48 |
# do some error handling |
---|
49 |
# if ngap: |
---|
50 |
# return (new_dt, new_data) |
---|
51 |
# else: return (dt, data) |
---|
52 |
|
---|
53 |
# for each gap in data create NaN |
---|
54 |
data_insert = [numpy.nan for gap in igap] |
---|
55 |
# for each gap in time create datetime value |
---|
56 |
dt_insert = [dt[gap]+sample_interval for gap in igap] |
---|
57 |
|
---|
58 |
# insert new sample times at indices of the gaps |
---|
59 |
new_dt = numpy.insert(dt, igap+1, dt_insert) |
---|
60 |
# insert NaN data at the indices that match the above times |
---|
61 |
new_data = numpy.insert(data, igap+1, data_insert) |
---|