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]


2000-01-01 00:30:00 2014-03-01 00:00:00

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]


2000-01-01 01:00:00 2000-01-01 02:00:00

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")


105551

In [52]:
for i in range(si_30,ei_30+1):
    print ldt_30[i],Fsd_30[i]


2006-01-08 00:30:00 0.0
2006-01-08 01:00:00 0.0
2006-01-08 01:30:00 0.0
2006-01-08 02:00:00 0.0
2006-01-08 02:30:00 0.0
2006-01-08 03:00:00 0.0
2006-01-08 03:30:00 0.0
2006-01-08 04:00:00 0.0
2006-01-08 04:30:00 0.0
2006-01-08 05:00:00 30.5816
2006-01-08 05:30:00 139.55
2006-01-08 06:00:00 250.415
2006-01-08 06:30:00 361.28
2006-01-08 07:00:00 470.247
2006-01-08 07:30:00 575.454
2006-01-08 08:00:00 675.099
2006-01-08 08:30:00 767.478
2006-01-08 09:00:00 851.009
2006-01-08 09:30:00 924.264
2006-01-08 10:00:00 985.99
2006-01-08 10:30:00 1035.13
2006-01-08 11:00:00 1070.84
2006-01-08 11:30:00 1092.52
2006-01-08 12:00:00 1099.78
2006-01-08 12:30:00 1092.52
2006-01-08 13:00:00 1070.84
2006-01-08 13:30:00 1035.13
2006-01-08 14:00:00 985.99
2006-01-08 14:30:00 924.264
2006-01-08 15:00:00 851.009
2006-01-08 15:30:00 767.478
2006-01-08 16:00:00 675.099
2006-01-08 16:30:00 575.454
2006-01-08 17:00:00 470.248
2006-01-08 17:30:00 361.279
2006-01-08 18:00:00 250.415
2006-01-08 18:30:00 139.55
2006-01-08 19:00:00 30.5816
2006-01-08 19:30:00 0.0
2006-01-08 20:00:00 0.0
2006-01-08 20:30:00 0.0
2006-01-08 21:00:00 0.0
2006-01-08 21:30:00 0.0
2006-01-08 22:00:00 0.0
2006-01-08 22:30:00 0.0
2006-01-08 23:00:00 0.0
2006-01-08 23:30:00 0.0
2006-01-09 00:00:00 0.0

In [53]:
for i in range(si_60,ei_60+1):
    print ldt_60[i],Fsd_60[i]


2006-01-08 00:00:00 0.0
2006-01-08 01:00:00 0.0
2006-01-08 02:00:00 0.0
2006-01-08 03:00:00 0.0
2006-01-08 04:00:00 0.0
2006-01-08 05:00:00 15.2908
2006-01-08 06:00:00 194.982
2006-01-08 07:00:00 415.764
2006-01-08 08:00:00 625.277
2006-01-08 09:00:00 809.243
2006-01-08 10:00:00 955.127
2006-01-08 11:00:00 1052.99
2006-01-08 12:00:00 1096.15
2006-01-08 13:00:00 1081.68
2006-01-08 14:00:00 1010.56
2006-01-08 15:00:00 887.637
2006-01-08 16:00:00 721.288
2006-01-08 17:00:00 522.851
2006-01-08 18:00:00 305.847
2006-01-08 19:00:00 85.0658
2006-01-08 20:00:00 0.0
2006-01-08 21:00:00 0.0
2006-01-08 22:00:00 0.0
2006-01-08 23:00:00 0.0
2006-01-09 00:00:00 0.0

In [ ]: