In [1]:
%run basics
In [48]:
def GetDateIndex(datetimeseries,date,ts=30,default=0,match='exact'):
'''
PURPOSE:
Return the index of a date/datetime string in an array of datetime objects
USAGE:
si = qcutils.GetDateIndex(datetimeseries,date_str,ts=30,default=0,match='exact')
where
datetimeseries - array of datetime objects
date_str - a date or date/time string in a format dateutils can parse
ts - time step for the data, optional (integer)
default - default value, optional (integer)
match - type of match (string) options are:
"exact" - finds the specified datetime and returns
the index
"startnextday" - returns the index of the first time period
in the next day
"endpreviousday" - returns the index of the last time period
in the previous day
"startnexthour" - returns the index of the first time period
in the next hour
"endprevioushour" - returns the index of the last time period
in the next hour
NOTE: "startnextday" and "endpreviousday" can be used to pick
out time periods with an integer number of days
AUTHOR: PRI
'''
try:
if len(date)!=0:
i = datetimeseries.index(dateutil.parser.parse(date))
else:
if default==-1:
i = len(datetimeseries)-1
else:
i = default
except ValueError:
if default==-1:
i = len(datetimeseries)-1
else:
i = default
if match=="exact":
# if an exact match is required, do nothing
pass
elif match=='startnextday':
while abs(datetimeseries[i].hour+float(datetimeseries[i].minute)/60-float(ts)/60)>c.eps:
i = i + 1
elif match=="startnexthour":
# check the time step value
if int(ts)!=60:
# if the time step is 60 then it is always the start of the next hour
# we assume here that the time period ends on the datetime stamp
print i
while datetimeseries[i].minute!=ts:
# iterate until the minutes equal the time step
i = i + 1
elif match=='endpreviousday':
while abs(datetimeseries[i].hour+float(datetimeseries[i].minute)/60)>c.eps:
i = i - 1
elif match=="endprevioushour":
# check the time step value
if int(ts)!=60:
# if the time step is 60 then it is always the end of the previous hour
# we assume here that the time period ends on the datetime stamp
while datetimeseries[i].minute!=0:
# iterate until the minutes equal 0
i = i - 1
else:
log.error("GetDateIndex: Unrecognised match option")
return i
In [2]:
ncname=qcio.get_filename_dialog()
In [41]:
ncfile=netCDF4.Dataset(ncname)
ts=30
In [42]:
time=ncfile.variables["time"][:]
time_units=getattr(ncfile.variables["time"],"units")
ldt=list(netCDF4.num2date(time,time_units))
ldt=qcutils.RoundDateTime(ldt,ts=ts)
In [43]:
si = GetDateIndex(ldt,"2000-01-01",default=0,ts=ts,match="startnexthour")
ei = GetDateIndex(ldt,"2015-01-01",default=-1,ts=ts,match="endprevioushour")
print ldt[si],ldt[ei]
In [44]:
ldt_30=ldt[si:ei+1]
ldt_60=[ldt_30[i] for i in range(len(ldt_30)) if ldt_30[i].minute==0]
print ldt_60[0],ldt_60[1]
In [45]:
var=ncfile.variables["SWdown"][:]
Fsd_30=numpy.squeeze(var[si:ei+1])
Fsd_30_2d=numpy.reshape(Fsd_30,(len(Fsd_30)/2,2))
Fsd_60=numpy.average(Fsd_30_2d,axis=1)
In [46]:
fig=plt.figure()
plt.plot(ldt_30,Fsd_30,'b.')
plt.plot(ldt_60,Fsd_60,'r+')
plt.show()
In [50]:
si_30=GetDateIndex(ldt_30,"2006-01-08",default=0,ts=ts,match="startnexthour")
ei_30=GetDateIndex(ldt_30,"2006-01-09",default=-1,ts=ts,match="endprevioushour")
si_60=GetDateIndex(ldt_60,"2006-01-08",default=0,ts=60,match="startnexthour")
ei_60=GetDateIndex(ldt_60,"2006-01-09",default=-1,ts=60,match="endprevioushour")
In [52]:
for i in range(si_30,ei_30+1):
print ldt_30[i],Fsd_30[i]
In [53]:
for i in range(si_60,ei_60+1):
print ldt_60[i],Fsd_60[i]
In [ ]: