Beispiel #1
0
def validateForecastDates(dates, last_obs, end_date):
    if 'fcast_start' in dates:
        fcast_start = asDatetimeDate(dates['fcast_start'])
        if fcast_start > last_obs:
            fcast_end = asDatetimeDate(dates['fcast_end'])
            if fcast_start < end_date:
                if fcast_end > end_date:
                    dates['fcast_end'] = dates['season_end']
                elif fcast_start == end_date:
                    dates['fcast_end'] = dates['fcast_start']
            else:
                del dates['fcast_start']
                del dates['fcast_end']
        else:
            del dates['fcast_start']
            del dates['fcast_end']

    elif 'fcast_start_date' in dates:
        fcast_start = asDatetimeDate(dates['fcast_start_date'])
        if fcast_start > last_obs:
            fcast_dates['fcast_start'] = dates['fcast_start_date']
            if fcast_start < end_date:
                fcast_end = asDatetimeDate(dates['fcast_end_date'])
                if fcast_end > end_date:
                    dates['fcast_end'] = dates['season_end']
                elif fcast_start == end_date:
                    dates['fcast_end'] = dates['fcast_start_date']
                else: dates['fcast_end'] = dates['fcast_end_date']
        del dates['fcast_start_date']
        del dates['fcast_end_date']

    return dates
 def _loadDataGridAttributes_(self):
     if hasattr(self, 'start_date') \
     and isinstance(self.start_date, basestring):
         self.start_date = asDatetimeDate(self.start_date)
     if hasattr(self, 'end_date') \
     and isinstance(self.end_date, basestring):
         self.end_date = asDatetimeDate(self.end_date)
Beispiel #3
0
    def accumulateGdd(self, model_name, lo_gdd_th, hi_gdd_th, start_date,
                      accumulated_chill, daily_gdd):
        # need GDD accumulation and chill mask from previous day
        prev_day = start_date - ONE_DAY

        # start with chill mask from the previous day ... a mask is necessary
        # so that GDD accumulation will continue even after chill hours begin
        # to decline in later months of the season
        prev_mask =\
        self.getChillMask(model_name, lo_gdd_th, hi_gdd_th, prev_day)

        # allow accumulation wherever chill is above
        # minimum chill hour threshold for variety
        above_min_chill = N.where(accumulated_chill >= self.min_chill_units)
        # chill accumulation is below min chill hour threshold at all nodes
        if len(above_min_chill[0]) == 0:
            return N.zeros_like(daily_gdd), N.zeros(daily_gdd.shape,
                                                    dtype=bool)

        # chill accumulation is cumulative, so we need accumulated GDD for
        # the previous date
        if asDatetimeDate(start_date) > asDatetimeDate(self.start_date):
            prev_gdd = self.getGdd(model_name, lo_gdd_th, hi_gdd_th, prev_day)
            prev_gdd = prev_gdd.astype(daily_gdd.dtype)
        else:
            prev_gdd = N.zeros_like(prev_mask.shape, dtype=daily_gdd.dtype)

        # accumulate GDD for this variety/model combination
        accumulated_gdd = N.zeros_like(daily_gdd)
        if daily_gdd.ndim == 2:
            # adjust previous day's mask to include any additional nodes where
            # minimum chill units have been reached on the current date
            prev_mask[above_min_chill] = True
            indexes = N.where(prev_mask == True)
            # update accumulated GDD array with valid node from daily_gdd
            accumulated_gdd[indexes] = daily_gdd[indexes]
            # add previous accumulation
            accumulated_gdd += prev_gdd

            return accumulated_gdd, prev_mask

        elif daily_gdd.ndim == 3:
            # create a 3D mask from the previous day's mask
            chill_mask = N.array(
                [prev_mask for i in range(daily_gdd.shape[0])], dtype=int)
            # adjust mask to include any additional dates/nodes where
            # minimum chill units have been reached
            chill_mask[above_min_chill] = True
            indexes = N.where(chill_mask == True)
            # insert input daily GDD only where new mask applies
            accumulated_gdd[indexes] = daily_gdd[indexes]
            # add the previous day's GDD accumlation to the first
            # day in the new accumulation
            accumulated_gdd[0] = prev_gdd + daily_gdd[0]
            # calculated the cumulative total GDD accumulated
            accumulated_gdd = N.cumsum(accumulated_gdd, axis=0)

            return accumulated_gdd, chill_mask
Beispiel #4
0
def validateSeasonDates(dates):
    season_end = dates['season_end']
    end_date = asDatetimeDate(season_end)
    last_obs = asDatetimeDate(dates['last_obs'])
    if last_obs > end_date:
        dates['last_obs'] = season_end
        last_obs = end_date
    dates = validateForecastDates(dates, last_obs, end_date)
    last_valid = asDatetimeDate(dates['last_valid'])
    if last_valid > end_date:
        dates['last_valid'] = season_end
    return dates
    def __call__(self, request):
        start_response = datetime.datetime.now()

        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)
        print '\n\nGrapeHardinessTempextHandler.__call__'

        # extract location coordinates
        location = self.extractLocationParameters(request_dict)
        lat, lon = location['coords']
        print '    location :\n', location

        target_year = request_dict.get('season', None)
        print '\n    target_year :', target_year

        # get the configured season limits
        dates = self.extractSeasonDates(request_dict)
        print '    dates :\n', dates

        season_start = asDatetimeDate(dates['season_start'])
        season_end = asDatetimeDate(dates['season_end'])
        target_year = dates['season']
        del dates['season']
        # initialize response string with season dates
        response = \
            '{"tempexts":{%s,"data":{' % self.tightJsonString(dates)[1:-1]

        reader = \
            self.tempextFileReader(target_year, self.source, self.region)
        if self.mode in ('dev', 'test'):
            print 'tempexts file :', reader.filepath
        # add recent averages
        data = \
        reader.getSliceAtNode('mint', season_start, season_end, lon, lat)
        response = \
            '%s"mint":%s' % (response, self.serializeData(data, '%.1f'))

        # add climate normal averages
        data = \
        reader.getSliceAtNode('maxt', season_start, season_end, lon, lat)
        response = \
            '%s,"maxt":%s' % (response, self.serializeData(data, '%.1f'))

        reader.close()
        del data
        if self.mode in ('dev', 'test'):
            print 'tempexts data retrieved in', elapsedTime(
                start_response, True)
        print '\n\nTEMPEXTS RESPONSE\n', response, '\n\n'

        self.respondWithJSON(request, '%s}}}' % response)
 def compareDates(self, date1, op, date2):
     if isinstance(date1, datetime.datetime):
         if isinstance(date2, datetime.datetime):
             return self._compareDates(date1, op, date2)
         else: return self._compareDates(date1, op, asDatetime(date2))
     elif isinstance(date1, datetime.date):
         if isinstance(date2, datetime.date):
             return self._compareDates(date1, op, date2)
         else: return self._compareDates(date1, op, asDatetimeDate(date2))
     elif isinstance(date1, (tuple, basestring)):
         return self._compareDates(asDatetimeDate(date1), op, date2)
     else:
         errmsg = 'Invalid type for argument "date1" : %s'
         raise TypeError, errmsg % str(type(date1))
Beispiel #7
0
    def __call__(self, request):
        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)

        # get the configured season limits
        dates = self.extractSeasonDates(request_dict)
        season_start = asDatetimeDate(dates['season_start'])
        season_end = asDatetimeDate(dates['season_end'])
        # create an array of days for X axis of data plots
        response_json = \
            '{"days":%s}' % self.serializeDates(season_start, season_end)

        # send the respnse
        self.respond(request, response_json)
    def __call__(self, request):
        print '\n\nGrapeHardinessSeasonDaysHandler.__call__'
        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)
        # extract season date limits from request
        dates = self.extractSeasonDates(request_dict)
        print '    dates :\n', dates
        season_start = asDatetimeDate(dates['season_start'])
        season_end = asDatetimeDate(dates['season_end'])
        # create an array of days for X axis of data plots
        response_json = \
            '{"days":%s}' % self.serializeDates(season_start, season_end)

        # send the respnse
        self.respondWithJSON(request, response_json)
Beispiel #9
0
    def estimateChill(self, model_name, **kwargs):
        """ estimate daily chill units and cumulative chill accumulation
        """
        debug = kwargs.get('debug', False)

        # look through possible date conbinations
        date_arg = kwargs.get('date', kwargs.get('dates', None))

        # not a single date or list of dates, look for start/end date
        if date_arg is None:
            start_date = kwargs.get('start_date', None)
            if start_date is None:
                raise KeyError, CHILL_DATE_ERR
            end_date = kwargs.get('end_date', None)
            if end_date is None:
                raise KeyError, CHILL_DATE_ERR
            # convert start/ end date into the list of dates that Linvill needs
            num_days = (end_date - start_date).days + 1
            date_arg = [ start_date + relativedelta(days=days)
                         for days in range(num_days) ]

        # may pass hourly temps when they are known
        hourly_temps = kwargs.get('hourly',None)
        if hourly_temps is None:
            # hourly temps were not passed ... need to iterpolate from mint/maxt
            mint = kwargs.get('mint', None)
            if mint is None:
                raise KeyError, CHILL_DATA_ERR
            maxt = kwargs.get('maxt', None)
            if maxt is None:
                raise KeyError, CHILL_DATA_ERR
            # get temperature units, default to Fahrenheit
            units = kwargs.get('units','F')

            # interpolate hourly temps from mint/maxt wit Linvill algorithm
            hourly_temps = self.linvill(date_arg, mint, maxt, units, debug)

        # set previous acummulation to zero - need 3D array even for 1 day
        previous = N.zeros((1,) + self.lats.shape, dtype=float)
        # attempt to override with actual previous day's accumulation
        if isinstance(date_arg, datetime): date = date_arg
        else: date = date_arg[0]
        if self.start_date is not None \
        and asDatetimeDate(date) > self.start_date:
            previous[0] = self.getChill(model_name, 'accumulated',
                                        start_date=date-ONE_DAY)

        # create an instance of the chill accumulator class
        chill = self.chillCalculator(model_name, previous, None)
        # calculate daily chill units
        daily = chill.calcDailyChillUnits(
                chill.calcHourlyChillUnits(hourly_temps, debug) , debug)

        # calculate daily accumulations
        accumulated = chill.accumulate(daily)

        if daily.shape[0] == 1: return daily[0], accumulated[0]
        else: return daily, accumulated
Beispiel #10
0
    def __call__(self, request):
        reader, location, dates, response = \
            self.initializeResponse(request, 'risk')

        lat = location['lat']
        lon = location['lon']

        start_date = asDatetimeDate(dates['season_start'])
        last_valid = asDatetimeDate(dates['last_valid'])

        # get the min temp data slice at the requested location
        data_slice = \
            reader.dataAtNode('mint', lon, lat, start_date, last_valid)
        reader.close()  # temporarily free up the file
        if self.verbose:
            print '\nmint array size =', len(data_slice)
            print data_slice
        # initialize response data with min temp slice
        data = [
            '"mint":%s' % self.serializeData(data_slice, '%d'),
        ]

        # get the data slice at the location for each kill temp dataset
        for dataset in ("T10", "T50", "T90"):
            reader.open()
            data_slice = \
                reader.dataAtNode(dataset, lon, lat, start_date, last_valid)
            reader.close()
            if self.verbose:
                print '\n', dataset, 'array size =', len(data_slice)
                print data_slice
            # add the kill data
            template = '"%s":%%s' % dataset
            data.append(template % self.serializeData(data_slice, '%d'))
        reader.close()
        del reader

        # insert the actual data into the "data_arrays" placeholder
        response = response.replace('"data_arrays"', "{%s}" % ','.join(data))
        if self.debug:
            print '\nAppleFrostRiskDataHandler response'
            print response

        # send the response
        self.respondWithJSON(request, response)
Beispiel #11
0
    def _generateEmptyDateProvenance(self, provenance, attrs):
        records = []
        record_tail = provenance.empty[1:]

        date = asDatetimeDate(attrs.get('start_date', self.start_date))
        while date <= self.end_date:
            record = (asAcisQueryDate(date), ) + record_tail
            records.append(record)
            date += ONE_DAY
        return records
Beispiel #12
0
 def dateAttributes(self, object_path, as_date_obj=False):
     attrs = self.objectAttributes(object_path)
     date_attrs = {}
     if as_date_obj:
         for key, value in attrs.items():
             if key.endswith('date'):
                 date_attrs[key] = asDatetimeDate(value)
     else:
         for key, value in attrs.items():
             if key.endswith('date'): date_attrs[key] = value
     return date_attrs
Beispiel #13
0
    def __call__(self, request):
        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)
        # get the configured season limits
        dates = self.extractSeasonDates(request_dict)
        if self.verbose:
            print 'season dates array size', len(dates)
            print dates
        response = '{"season":{"season_start":"%s","season_end":"%s"'
        response = response % (dates['season_start'], dates['season_end'])

        # create an array of days for X axis of data plots
        days = self.serializeDates(asDatetimeDate(dates['season_start']),
                                   asDatetimeDate(dates['season_end']))
        response = '%s,"dates":%s}}' % (response, days)
        if self.debug:
            print '\nAppleFrostSeasonDatesHandler response'
            print response

        # send the respnse
        self.respondWithJSON(request, response)
Beispiel #14
0
    def _datesToIndexes(self, dataset_path, start_time_obj, end_time_obj):
        attrs = self.getDatasetAttributes(dataset_path)
        start_date = attrs.get('start_date', None)
        if start_date is not None:
            if isinstance(start_date, basestring):
                start_date = asDatetimeDate(start_date)
                end_date = asDatetimeDate(attrs['end_date'])
            else:
                start_date = self._dateFromSequence(start_date)
                end_date = self._dateFromSequence(attrs['end_date'])
        else:
            start_day = attrs.get('start_day', None)
            if start_day is not None:
                start_date = self._dateFromSequence(start_day)
                end_date = self._dateFromSequence(attrs['end_day'])
            else:
                errmsg = '"%s" dataset is not indexable by date.'
                raise IndexError, errmsg % dataset_path

        if isinstance(start_time_obj, datetime.datetime):
            sdate = start_time_obj.date()
            edate = end_time_obj.date()
        elif isinstance(start_time_obj, datetime.date):
            sdate = start_time_obj
            edate = end_time_obj
        elif isinstance(start_time_obj, (tuple, list)):
            sdate = self._dateFromSequence(start_time_obj)
            edate = self._dateFromSequence(end_time_obj)
        elif isinstance(start_time_obj, basestring):
            sdate = asDatetimeDate(start_time_obj)
            edate = asDatetimeDate(end_time_obj)
        else:
            errmsg = 'Invalid data type for time object arguments : %s'
            raise TypeError, errmsg % str(type(start_time_obj))

        if sdate >= start_date and edate <= end_date:
            return ((sdate - start_date).days, (edate - start_date).days + 1)
        else:
            errmsg = 'Invalid value in time object arguments : %s, %s'
            raise ValueError, errmsg % (str(start_time_obj), str(end_time_obj))
Beispiel #15
0
    def _dateToIndex(self, dataset_path, time_obj):
        if isinstance(time_obj, datetime.datetime):
            date = datetime.date(*time_obj.timetuple()[:3])
        elif isinstance(time_obj, datetime.date):
            date = time_obj
        elif isinstance(time_obj, (tuple, list)):
            date = self._dateFromSequence(time_obj)
        else:
            errmsg = 'Invalid data type for "time_obj" argument : %s'
            raise TypeError, errmsg % str(type(time_obj))

        attrs = self.getDatasetAttributes(dataset_path)
        start_date = attrs.get('start_date', None)

        if start_date is not None:
            if isinstance(start_date, basestring):
                start_date = asDatetimeDate(start_date)
                end_date = asDatetimeDate(attrs['end_date'])
            else:
                start_date = self._dateFromSequence(start_date)
                end_date = self._dateFromSequence(attrs['end_date'])
        else:
            start_day = attrs.get('start_day', None)
            if start_day is not None:
                start_date = self._dateFromSequence(start_day)
                end_date = self._dateFromSequence(attrs['end_day'])
            else:
                errmsg = '"%s" dataset is not indexable by date.'
                raise IndexError, errmsg % dataset_path

        if date >= start_date and date <= end_date:
            return (date - start_date).days
        else:
            errmsg = '"time_obj" translates to date outside valid range'
            errmsg += ' (%s to %s) for dataset "%s"'
            errmsg = errmsg % (str(start_date), str(end_date), dataset_path)
            raise TypeError, errmsg
Beispiel #16
0
    def getLastValidDate(self, dataset_path, start_date, data, **kwargs):
        # always honor a user-specified last valid date
        last_valid_date = kwargs.get('last_valid_date', None)
        # last valid date not specified by user, figure it out from the data
        if last_valid_date is None:
            if data.ndim == 2:  # 2D rray is a single day
                # an array filled with N.nan is not valid
                missing, where = \
                    self._whereMissing(data, dataset=dataset_path)
                if len(where[0]) == data.size:
                    num_valid_days = 0
                else:
                    num_valid_days = 1

            else:  # 3D array
                # for now, the code assumes a zyx or zxy data view
                num_nodes = data[0].size
                num_valid_days = num_days = data.shape[0]
                # starting from the last day in the array
                # find first day with at least one node with a valid value
                day = num_days - 1
                missing, where = \
                    self._whereMissing(data[day], dataset=dataset_path)
                while day > 0 and len(where[0]) == num_nodes:
                    # day has no valid values, decrement day counters
                    day -= 1
                    num_valid_days -= 1
                    missing, where = \
                        self._whereMissing(data[day], missing=missing)

            if num_valid_days == 0:
                # last valid date will not be changed because there are
                # no valid values in the data
                prev_date_str = self.getDatasetAttribute(
                    dataset_path, 'last_valid_date', None)
                if prev_date_str is not None:
                    return asDatetimeDate(prev_date_str)
                else:
                    return None

            # last valid date will be start_date plus number of valid days
            elif num_valid_days == 1:
                last_valid_date = start_date
            else:  # num_valid_days > 1
                last_valid_date = \
                    start_date + relativedelta(days=num_valid_days-1)

        return last_valid_date
Beispiel #17
0
arg_0 = args[0]
if arg_0.isdigit():
    target_year = int(args[0])
    region = factory.regionConfig(options.region)
    source = factory.sourceConfig(options.source)
    filepath = factory.tempextsFilepath(target_year, source, region)
else:
    filepath = os.path.abspath(arg_0)
print 'testing in file :', filepath

from_dates = input("Enter dates to copy : ")
if debug: print 'from_dates =', from_dates
if ',' in from_dates:
    start_date, end_date = from_dates.split(',')
    start_date = asDatetimeDate(start_date)
    end_date = asDatetimeDate(end_date)
else:
    start_date = end_date = asDatetimeDate(from_dates)

print 'Copying data from', start_date, 'to', end_date
manager = TemperatureFileManager(filepath, factory.registry, mode='r')
mint = manager.dateSlice('temps.mint', start_date, end_date)
maxt = manager.dateSlice('temps.maxt', start_date, end_date)
if forecast: source = 'NDFD'
else: source = manager.fileAttribute('source')
manager.close()

to_date = asDatetimeDate(input("Enter date to copy data to : "))
if debug: print 'to_date :', to_date
print 'Inserting data at', to_date
Beispiel #18
0
 def _loadDateGridAttributes_(self):
     start_date = self.fileAttribute('start_date', None)
     if start_date is not None: self.start_date = asDatetimeDate(start_date)
     end_date = self.fileAttribute('end_date', None)
     if end_date is not None: self.end_date = asDatetimeDate(end_date)
Beispiel #19
0
 def getAttributeAsDate(self, path, attribute_name):
     """ Access a date attribute and return it as a datetime.date
     """
     date_str = self.getObjectAttribute(path, attribute_name, None)
     if date_str is not None: return asDatetimeDate(date_str)
     return None
Beispiel #20
0
last_obs_date = manager.datasetAttribute('temps.maxt', 'last_obs_date')
last_valid_date = manager.datasetAttribute('temps.maxt', 'last_valid_date')

if end_date is not None:
    # refresh start date cannot be earlier than first day in datasets
    # this is only relevant at the beginning of a new year
    data_limit = manager.dateAttribute('temps.maxt', 'start_date')
    start_date = max(start_date, data_limit)
    # refresh end date cannot be later the last day in the datasets
    # this is only relevant at the end of the year
    data_limit = manager.dateAttribute('temps.maxt', 'end_date')
    if end_date > data_limit: end_date = data_limit
    else:
        # refresh end date cannot be later than the last observation
        # otherwise, you risk stomping on the forecast
        end_date = min(end_date, asDatetimeDate(last_obs_date))

    # recalculate actual number of days to be refreshed
    num_days = (end_date - start_date).days + 1

    msg = '    refreshing data for %d days : %s thru %s'
    print msg % (num_days, str(start_date), str(end_date))
else:
    if start_date < datetime.date(target_date.year, 1, 1):
        errmsg = "%s is an invalid date for %d"
        raise ValueError, errmsg % (str(start_date), target_date.year)
    print '    refreshing data for', str(start_date)

manager.close()

# filter annoying numpy warnings
Beispiel #21
0
 def preInitDates(self, start_date, end_date):
     self.start_date = asDatetimeDate(start_date)
     self.end_date = asDatetimeDate(end_date)
Beispiel #22
0
 def dateAttribute(self, object_path, attribute_name, default=None):
     date = self.getObjectAttribute(object_path, attribute_name, default)
     if date != default: return asDatetimeDate(date)
     return default
 def getDateAttribute(self, object_path, attribute_name, default=None):
     date = self.getObjectAttribute(object_path, attribute_name, default)
     if date is not None: return asDatetimeDate(date)
     return None
Beispiel #24
0
    def __call__(self, request):
        start_response = datetime.datetime.now()

        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)

        # extract location coordinates
        location = request_dict['location']
        coords = location.get('coords', None)
        if coords is not None:
            lat = float(coords[0])
            lon = float(coords[1])
        else:
            lat = float(location['lat'])
            lon = float(location['lon'])

        # GDD threshold and target_year
        gdd_threshold = str(request_dict['gdd_threshold'])
        target_year = request_dict.get('season', None)
        #if target_year is None: target_year = datetime.date.today().year
        if target_year is None: target_year = self.maxAvailableYear()

        # get the configured season limits
        dates = self.extractSeasonDates(request_dict, target_year)

        # initialize the response
        response_json = \
            '{"season":{"gdd_threshold":"%s"' % gdd_threshold
        response_json = \
            '%s,"location":%s' % (response_json,self.tightJsonString(location))

        # create a POR file reader
        reader = \
        self.getTargetYearFileReader(target_year, self.source, self.region)
        if self.mode in ('dev', 'test'):
            print 'season data file :', reader.filepath

        # create path to GDD dataset
        dataset_path = reader.gddDatasetPath(gdd_threshold)

        # capture the significant dates for the dataset
        if 'dates' in self.mode_config \
        and target_year == self.mode_config.season:
            dates.update(self.mode_config.dates.attrs)
        else:
            dates.update(reader.getSignificantDates(dataset_path))
        season_end = dates['season_end']
        end_date = asDatetimeDate(season_end)
        start_date = asDatetimeDate(dates['season_start'])
        if 'fcast_start' in dates:
            fcast_start = asDatetimeDate(dates['fcast_start'])
            if fcast_start > end_date: del dates['fcast_start']
        if 'fcast_end' in dates:
            fcast_end = asDatetimeDate(dates['fcast_end'])
            if fcast_end > end_date:
                if 'fcast_start' in dates: dates['fcast_end'] = season_end
                else: del dates['fcast_end']
        last_obs = asDatetimeDate(dates['last_obs'])
        if last_obs > end_date: dates['last_obs'] = season_end
        last_valid = asDatetimeDate(dates['last_valid'])
        if last_valid > end_date:
            dates['last_valid'] = season_end
            last_valid = asDatetimeDate(season_end)

        # temporarily free up the POR file
        reader.close()

        # add season dates to response
        response_json = \
            '%s,"dates":%s' % (response_json, self.tightJsonString(dates))
        del dates

        # get the accumulated GDD for Y axis of data plots
        reader.open()
        response_json = '%s,"data":{"season":%s}}}' % (
            response_json,
            self.serializeData(
                reader.getDataAtNode(dataset_path, lon, lat, start_date,
                                     last_valid)))
        reader.close()
        if self.mode in ('dev', 'test'):
            print 'season data retrieved in ', elapsedTime(
                start_response, True)

        # send the response
        self.respond(request, response_json)
Beispiel #25
0
    def __call__(self, request):
        print '\n\GrapeHardinessTempDataHandler.__call__'
        start_response = datetime.datetime.now()

        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)

        varieties = self.extractVarietyParameters(request_dict)
        variety = varieties['variety']

        # extract location coordinates
        location = self.extractLocationParameters(request_dict)
        if 'coords' in location:
            lat, lon = location['coords']
        else:
            lat = location['lat']
            lon = location['lon']

        # extract season date limits from request
        dates = self.extractSeasonDates(request_dict)
        target_year = dates['season']
        print '    target_year :', target_year
        print '    dates :\n', dates

        # create a variety file reader
        reader = self.varietyFileReader(variety, target_year, self.source,
                                        self.region, 'season')
        if self.mode in ('dev', 'test'):
            print 'hardtemp data file :', reader.filepath
        # path to the hardiness temp dataset
        dataset_path = 'hardtemp'

        # capture the significant dates for the dataset
        if 'dates' in self.mode_config \
        and target_year == self.mode_config.season:
            dates.update(self.mode_config.dates.attrs)
        else:
            dates.update(reader.significantDates(dataset_path))
        # temporarily free up the file
        reader.close()

        season_end = dates['season_end']
        end_date = asDatetimeDate(season_end)
        start_date = asDatetimeDate(dates['season_start'])
        if 'fcast_start' in dates:
            fcast_start = asDatetimeDate(dates['fcast_start'])
            if fcast_start > end_date: del dates['fcast_start']
        if 'fcast_end' in dates:
            fcast_end = asDatetimeDate(dates['fcast_end'])
            if fcast_end > end_date:
                if 'fcast_start' in dates: dates['fcast_end'] = season_end
                else: del dates['fcast_end']
        last_obs = asDatetimeDate(dates['last_obs'])
        if last_obs > end_date: dates['last_obs'] = season_end
        last_valid = asDatetimeDate(dates['last_valid'])
        if last_valid > end_date:
            dates['last_valid'] = season_end
            last_valid = asDatetimeDate(season_end)
        #dates = self.tightJsonString(dates)

        # get the accumulated GDD for Y axis of data plots
        reader.open()
        data = \
            reader.dataAtNode(dataset_path, lon, lat, start_date, last_valid)
        reader.close()
        if self.mode in ('dev', 'test'):
            print 'hardtemp data retrieved in ', elapsedTime(
                start_response, True)

        # initialize the response
        response_dict = {
            "hardtemp": {
                "variety": variety,
                "location": location,
                "dates": dates,
                "data": "data_array"
            }
        }
        response = self.tightJsonString(response_dict).replace('\\"', '"')
        response = \
            response.replace('"data_array"', self.serializeData(data, '%.1f'))

        # send the response
        self.respondWithJSON(request, response)
del date_attrs['start_date']
del date_attrs['end_date']

fcast_start = temps.dateAttribute(maxt_path, 'fcast_start_date', None)
fcast_end = temps.dateAttribute(maxt_path, 'fcast_end_date', None)
temps.close()
if fcast_start is None:
    print 'No forecast available.'
    exit()
if fcast_end < datetime.date.today():
    print 'No forecast available beyond', datetime.date.today()
    exit()

if fcast_end > season_end_date: fcast_end = season_end_date

last_valid = asDatetimeDate(fcast_end)
date_attrs['last_valid_date'] = last_valid.strftime('%Y-%m-%d')
print '\n\n', fcast_start, fcast_end, last_valid

temps_last_obs = asDatetimeDate(date_attrs['last_obs_date'])

if debug:
    print 'temps last obs, last valid', temps_last_obs, last_valid
    print 'facast_start, fcast_end', fcast_start, fcast_end
    print 'num days to retrieve', (fcast_end - fcast_start).days + 1

#last_valid = max(last_valid, fcast_end)
#if debug: print 'usable last valid', last_valid

build_dirpath = os.path.join(dirpaths.build, region_dirpath, source_dirpath)
variety_template = CONFIG.filenames.build
Beispiel #27
0
    # build the groups and their datasets
    for group_name in groups_in_file:
        if verbose: print '    building group : %s' % group_name
        builder.buildGroup(group_name, True)

    # create a source file reader
    reader = factory.getSourceFileReader(source, target_year, region, 'temps')

    # create lat,lon grids in POR file
    if verbose: print '    creating lat/lon datasets'
    builder.initLonLatData(reader.lons, reader.lats)

    # don't get data past last valid date in current year
    if target_year == current_year:
        lvd = reader.getDatasetAttribute('temps.mint', 'last_valid_date')
        end_date = asDatetimeDate(lvd)

    # get minimum and maximum temperatures
    mint = reader.getTimeSlice('temps.mint', start_date, end_date)
    maxt = reader.getTimeSlice('temps.maxt', start_date, end_date)
    reader.close()
    del reader

    # build the GDD datasets for the first sequence of dates
    for threshold, th_string in gdd_thresholds:
        manager.open('a')
        manager.updateThresholdGroup(threshold, start_date, mint, maxt,
                                     source.tag, **kwargs)
        manager.close()

    #report performance
Beispiel #28
0
    def __call__(self, request):
        start_response = datetime.datetime.now()

        # decode request variables into a dictionary
        request_dict = self.requestAsDict(request)

        # extract location coordinates
        location = request_dict['location']
        coords = location.get('coords', None)
        if coords is not None:
            lat = float(coords[0])
            lon = float(coords[1])
        else:
            lat = float(location['lat'])
            lon = float(location['lon'])

        # GDD threshold and target_year
        gdd_threshold = request_dict['gdd_threshold']
        target_year = request_dict.get('season', None)

        # get the configured season limits
        dates = self.extractSeasonDates(request_dict, target_year)
        season_start = asDatetimeDate(dates['season_start'])
        season_end = asDatetimeDate(dates['season_end'])
        target_year = dates['season']
        del dates['season']
        # initialize response string with season dates
        response = \
            '{"history":{%s,"data":{' % self.tightJsonString(dates)[1:-1]

        reader = self.getHistoryFileReader(target_year, self.source,
                                           self.region, gdd_threshold)
        if self.mode in ('dev', 'test'):
            print 'history file :', reader.filepath
        # add recent averages
        data = \
        reader.getSliceAtNode('recent', season_start, season_end, lon, lat)
        response = '%s"recent":%s' % (response, self.serializeData(data))

        # add climate normal averages
        data = \
        reader.getSliceAtNode('normal', season_start, season_end, lon, lat)
        response = '%s,"normal":%s' % (response, self.serializeData(data))

        # add period of record averages
        data = \
        reader.getSliceAtNode('por.avg', season_start, season_end, lon, lat)
        response = '%s,"poravg":%s' % (response, self.serializeData(data, 4))

        # add period of record - percent diffrence max GDD to average GDD
        data = \
        reader.getSliceAtNode('por.max', season_start, season_end, lon, lat)
        response = '%s,"pormax":%s' % (response, self.serializeData(data, 4))

        # add period of record - percent diffrence min GDD to average GDD
        data = \
        reader.getSliceAtNode('por.min', season_start, season_end, lon, lat)
        response = '%s,"pormin":%s' % (response, self.serializeData(data, 4))

        reader.close()
        del data
        if self.mode in ('dev', 'test'):
            print 'history data retrieved in', elapsedTime(
                start_response, True)

        self.respond(request, '%s}}}' % response)
Beispiel #29
0
num_args = len(args)

# determine target year from number of arguments
if num_args in (0, 2, 4):
    target_year = UPDATE_START_TIME.year
elif num_args in (1, 3, 5):
    target_year = int(args[0])
else:
    raise SyntaxError, INPUT_ERROR

# get a file manager for the target year
reader = factory.getSourceFileManager(source_key, target_year, region, 'temps')
print 'source filepath', reader.filepath
# need last valid date for
last_valid_date = \
    asDatetimeDate(reader.getDatasetAttribute('temps.mint','last_valid_date'))

# reprocess all days in the current year
if num_args == 0:
    start_date = datetime.date(target_year, 1, 1)
    end_date = datetime.date(target_year, 12, 31)
# reprocess a single day in the current year
elif num_args == 2:
    end_date = start_date = \
        datetime.date(target_year, int(args[0]), int(args[1]))
# reprocess single day in a specifc year
elif num_args == 3:
    end_date = start_date = \
        datetime.date(target_year, int(args[1]), int(args[2]))
# reprocess range of dates in the current year
elif num_args == 4:
Beispiel #30
0
 def acisDateString(self, date):
     return asDatetimeDate(date).strftime('%Y-%m-%d')