Exemplo n.º 1
0
def computeTimeWindow(freqString,currentDate):

    if (freqString == 'daily'):   req = requestors.Daily_req(currentDate.year,currentDate.month,currentDate.day)
    if (freqString == 'weekly'):  req = requestors.Weekly_req(currentDate.year, currentDate.month,currentDate.day)
    if (freqString == 'monthly'): req = requestors.Monthly_req(currentDate.year, currentDate.month)
    if (freqString == 'yearly'):  req = requestors.Yearly_req(currentDate.year)
    return TimeInterval.fromdatetimes(req.time_interval.start_time, req.time_interval.end_time)
Exemplo n.º 2
0
 def __init__(self,year,month,day, deltastr):
     self.year   = year
     self.month  = month
     self.day    = day
     centertime     = datetime.datetime(self.year,self.month,self.day,12)
     delta = relativedelta(10)
     exec 'delta= relativedelta(' + deltastr + ')'
     self.time_interval = TimeInterval.fromdatetimes(centertime-delta/2, centertime+delta/2)
     self.string  = centertime.strftime("%Y%m%d")
     self.deltastr = deltastr
Exemplo n.º 3
0
    def __init__(self,datelist):
        '''
        TimeList object is created by providing a list of datetime objects
        (At least 2).
        '''
        nTimes = len(datelist)
        assert  nTimes > 1
        self.Timelist = datelist
        self.Timelist.sort()
        self.nTimes   = nTimes

        self.timeinterval = TimeInterval.fromdatetimes(self.Timelist[0], self.Timelist[-1])
        self.inputdir     = None
        self.searchstring = None
        self.filelist     = None
        self.filtervar    = None
        self.inputFrequency= self.__searchFrequency()
Exemplo n.º 4
0
    def get_season_dates(self,season_num):
        """
        Given season number, return the range of season dates (start and end)
        and the name of season.
        """

        assert season_num  < self.numbers_season
        start_date=self.SEASON_LIST[season_num]
        if (season_num + 1) == self.numbers_season:
            end_date = self.SEASON_LIST[0] + relativedelta(years = 1)
        else:
            end_date= self.SEASON_LIST[season_num+1]
        TI = TimeInterval.fromdatetimes(start_date, end_date)

        season_name = self.SEASON_LIST_NAME[season_num]

        return TI,season_name
Exemplo n.º 5
0
    def __generaltimeselector(self,requestor):
            SELECTION=[]
            weights  =[]

            if self.inputFrequency == "daily":
                for it, t in enumerate(self.Timelist):
                    if requestor.time_interval.contains(t):
                        SELECTION.append(it)
                        weights.append(1.)
            if self.inputFrequency in ['weekly','monthly','yearly']:
                for it, t in enumerate(self.Timelist):
                    t1 = computeTimeWindow(self.inputFrequency,t);
                    t2 = TimeInterval.fromdatetimes(requestor.time_interval.start_time, requestor.time_interval.end_time)
                    weight = t1.overlapTime(t2)
                    if (weight > 0. ) :
                        SELECTION.append(it)
                        weights.append(weight)
            return SELECTION , np.array(weights)
Exemplo n.º 6
0
    def select(self,requestor):
        '''
        Method for time aggregation
        indexes, weights = select(requestor)
        Returned values:
         - a list of indexes (integers) indicating to access selected times (or files)
         - a numpy array of weights


        '''

        if isinstance(requestor,requestors.Daily_req):
            # hourly values are treated as instantaneous values, not time averages
            assert self.inputFrequency in ["hourly","daily"] # it does not matter how many hours
            SELECTION=[]
            weights = []
            for it,t in enumerate(self.Timelist):
                if requestor.time_interval.contains(t):
                    SELECTION.append(it)
                    weights.append(1.)
            return SELECTION , np.array(weights)


        if isinstance(requestor, requestors.Monthly_req):
            SELECTION=[]
            weights = []

            if self.inputFrequency == 'daily':
                for it, t in enumerate(self.Timelist):
                    if (t.year==requestor.year) & (t.month==requestor.month):
                        SELECTION.append(it)
                        weights.append(1.)
            if self.inputFrequency == 'weekly':
                for it,t in enumerate(self.Timelist):
                    t1 = computeTimeWindow("weekly",t);
                    t2 = TimeInterval.fromdatetimes(requestor.time_interval.start_time, requestor.time_interval.end_time)
                    weight = t1.overlapTime(t2);
                    if (weight > 0. ) :
                        SELECTION.append(it)
                        weights.append(weight)
            if self.inputFrequency == 'monthly':
                #print "Not time aggregation"
                for it,t in enumerate(self.Timelist):
                    if requestor.time_interval.contains(t):
                        SELECTION.append(it)
                        weights.append(1.)
            return SELECTION , np.array(weights)


        if isinstance(requestor, requestors.Weekly_req):
            assert self.inputFrequency != "monthly"
            assert self.inputFrequency != "weekly"

            SELECTION=[]
            weights  =[]

            if self.inputFrequency == "daily":
                for it,t in enumerate(self.Timelist):
                    if requestor.time_interval.contains(t):
                        SELECTION.append(it)
                        weights.append(1.)
            return SELECTION , np.array(weights)

        if isinstance(requestor, requestors.Interval_req):
            return self.__generaltimeselector(requestor)

        if isinstance(requestor, requestors.Season_req):
            return self.__generaltimeselector(requestor)

        if isinstance(requestor, requestors.Yearly_req):
            return self.__generaltimeselector(requestor)

        if isinstance(requestor, requestors.Decadal_req):
            return self.__generaltimeselector(requestor)
        if isinstance(requestor, requestors.Generic_req):
            return self.__generaltimeselector(requestor)

        if isinstance(requestor, requestors.Clim_day):
            assert self.inputFrequency == 'daily'
            SELECTION=[]
            weights  =[]
            for it,t in enumerate(self.Timelist):
                if (t.month == requestor.month) & (t.day == requestor.day):
                    SELECTION.append(it)
                    weights.append(1.)
            return SELECTION , np.array(weights)


        if isinstance(requestor,requestors.Clim_month):
            SELECTION = []
            weights   = []
            YEARLIST=self.getYearlist()
            for year_req in YEARLIST:
                req = requestors.Monthly_req(year_req.year, requestor.month)
                s,w = self.select(req)
                SELECTION.extend(s)
                weights.extend(w)

            return SELECTION , np.array(weights)

        if isinstance(requestor,requestors.Clim_season):
            SELECTION = []
            weights   = []
            YEARLIST=self.getYearlist()
            for year_req in YEARLIST:
                req = requestors.Season_req(year_req.year, requestor.season,seasonobj)
                s,w = self.select(req)
                SELECTION.extend(s)
                weights.extend(w)
            return SELECTION , np.array(weights)