Exemple #1
0
 def monthlygen(inputdata, start, local_start):
     """Internal generator function"""
     acc = MonthAcc(rain_day_threshold)
     month_start = start
     count = 0
     while month_start <= stop:
         count += 1
         if count % 12 == 0:
             logger.info("monthly: %s", month_start.isoformat(' '))
         else:
             logger.debug("monthly: %s", month_start.isoformat(' '))
         if local_start.month < 12:
             local_start = local_start.replace(month=local_start.month+1)
         else:
             local_start = local_start.replace(
                 month=1, year=local_start.year+1)
         month_end = time_zone.local_to_utc(local_start)
         month_end = time_zone.day_start(
             month_end, day_end_hour, use_dst=use_dst)
         acc.reset()
         for data in inputdata[month_start:month_end]:
             acc.add_daily(data)
         new_data = acc.result()
         if new_data:
             new_data['start'] = month_start
             yield new_data
         month_start = month_end
Exemple #2
0
 def rain_day(self, data):
     calib_data = self.context.calib_data
     midnight = time_zone.utc_to_local(data['idx'])
     midnight = midnight.replace(hour=0, minute=0, second=0)
     midnight = time_zone.local_to_utc(midnight)
     midnight_data = calib_data[calib_data.nearest(midnight)]
     return max(0.0, data['rain'] - midnight_data['rain'])
Exemple #3
0
def generate_monthly(rain_day_threshold, day_end_hour, use_dst,
                     daily_data, monthly_data, process_from):
    """Generate monthly summaries from daily data."""
    start = monthly_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = daily_data.after(start + SECOND)
    if process_from:
        if start:
            start = min(start, process_from)
        else:
            start = process_from
    if start is None:
        return start
    # set start to noon on start of first day of month (local time)
    local_start = time_zone.utc_to_local(start).replace(tzinfo=None)
    local_start = local_start.replace(day=1, hour=12, minute=0, second=0)
    # go back to UTC and get start of day (which might be previous day)
    start = time_zone.local_to_utc(local_start)
    start = time_zone.day_start(start, day_end_hour, use_dst=use_dst)
    del monthly_data[start:]
    stop = daily_data.before(datetime.max)
    if stop is None:
        return None
    def monthlygen(inputdata, start, local_start):
        """Internal generator function"""
        acc = MonthAcc(rain_day_threshold)
        month_start = start
        count = 0
        while month_start <= stop:
            count += 1
            if count % 12 == 0:
                logger.info("monthly: %s", month_start.isoformat(' '))
            else:
                logger.debug("monthly: %s", month_start.isoformat(' '))
            if local_start.month < 12:
                local_start = local_start.replace(month=local_start.month+1)
            else:
                local_start = local_start.replace(
                    month=1, year=local_start.year+1)
            month_end = time_zone.local_to_utc(local_start)
            month_end = time_zone.day_start(
                month_end, day_end_hour, use_dst=use_dst)
            acc.reset()
            for data in inputdata[month_start:month_end]:
                acc.add_daily(data)
            new_data = acc.result()
            if new_data:
                new_data['start'] = month_start
                yield new_data
            month_start = month_end
    monthly_data.update(monthlygen(daily_data, start, local_start))
    return start
Exemple #4
0
 def _eval_time(self, time_str):
     # get timestamp of last data item
     result = self.hourly_data.before(datetime.max)
     if not result:
         result = datetime.utcnow()  # only if no hourly data
     # convert to local time
     result = time_zone.utc_to_local(result)
     # set to start of the day
     result = result.replace(hour=0, minute=0, second=0, microsecond=0)
     # apply time string
     result = eval('result.replace(%s)' % time_str)
     # convert back to UTC
     return time_zone.local_to_utc(result)
Exemple #5
0
def main(argv=None):
    from pywws.timezone import time_zone
    if argv is None:
        argv = sys.argv
    try:
        opts, args = getopt.getopt(argv[1:], "h", ['help'])
    except getopt.error as msg:
        print('Error: %s\n' % msg, file=sys.stderr)
        print(__usage__.strip(), file=sys.stderr)
        return 1
    # process options
    for o, a in opts:
        if o in ('-h', '--help'):
            print(__usage__.strip())
            return 0
    # check arguments
    if len(args) != 1:
        print("Error: 1 argument required", file=sys.stderr)
        print(__usage__.strip(), file=sys.stderr)
        return 2
    data_dir = args[0]
    with pywws.storage.pywws_context(data_dir) as context:
        params = context.params
        pywws.localisation.set_application_language(params)
        hourly_data = context.hourly_data
        idx = hourly_data.before(datetime.max)
        print('Zambretti (current):', zambretti(params, hourly_data[idx]))
        idx = time_zone.utc_to_local(idx)
        if idx.hour < 8 or (idx.hour == 8 and idx.minute < 30):
            idx -= timedelta(hours=24)
        idx = idx.replace(hour=9, minute=0, second=0)
        idx = time_zone.local_to_utc(idx)
        idx = hourly_data.nearest(idx)
        lcl = time_zone.utc_to_local(idx)
        print('Zambretti (at %s):' % lcl.strftime('%H:%M %Z'), zambretti(
            params, hourly_data[idx]))
    return 0
Exemple #6
0
    def process(self, live_data, template_file):
        def jump(idx, count):
            while count > 0:
                new_idx = data_set.after(idx + SECOND)
                if new_idx == None:
                    break
                idx = new_idx
                count -= 1
            while count < 0:
                new_idx = data_set.before(idx)
                if new_idx == None:
                    break
                idx = new_idx
                count += 1
            return idx, count == 0

        params = self.params
        if not live_data:
            idx = self.calib_data.before(datetime.max)
            if not idx:
                logger.error("No calib data - run pywws.process first")
                return
            live_data = self.calib_data[idx]
        # get default character encoding of template input & output files
        self.encoding = params.get('config', 'template encoding', 'iso-8859-1')
        file_encoding = self.encoding
        if file_encoding == 'html':
            file_encoding = 'ascii'
        # get conversions module to create its 'private' wind dir text
        # array, then copy it to deprecated wind_dir_text variable
        winddir_text(0)
        wind_dir_text = conversions._winddir_text_array
        hour_diff = self.computations.hour_diff
        rain_hour = self.computations.rain_hour
        rain_day = self.computations.rain_day
        rain_24hr = self.computations.rain_24hr
        pressure_offset = float(self.params.get('config', 'pressure offset'))
        fixed_block = literal_eval(self.status.get('fixed', 'fixed block'))
        # start off with no time rounding
        round_time = None
        # start off in hourly data mode
        data_set = self.hourly_data
        # start off in utc
        local_time = False
        # start off with default use_locale setting
        use_locale = self.use_locale
        # jump to last item
        idx, valid_data = jump(datetime.max, -1)
        if not valid_data:
            logger.error("No summary data - run pywws.process first")
            return
        data = data_set[idx]
        # open template file, if not already a file(like) object
        if hasattr(template_file, 'readline'):
            tmplt = template_file
        else:
            tmplt = open(template_file, 'rb')
        # do the text processing
        line = ''
        while True:
            new_line = tmplt.readline()
            if not new_line:
                break
            if isinstance(new_line, bytes) or sys.version_info[0] < 3:
                new_line = new_line.decode(file_encoding)
            line += new_line
            parts = line.split('#')
            if len(parts) % 2 == 0:
                # odd number of '#'
                line = line.rstrip('\r\n')
                continue
            for i, part in enumerate(parts):
                if i % 2 == 0:
                    # not a processing directive
                    if i == 0 or part != '\n':
                        yield part
                    continue
                if part and part[0] == '!':
                    # comment
                    continue
                # Python 2 shlex can't handle unicode
                if sys.version_info[0] < 3:
                    part = part.encode(file_encoding)
                command = shlex.split(part)
                if sys.version_info[0] < 3:
                    command = map(lambda x: x.decode(file_encoding), command)
                if command == []:
                    # empty command == print a single '#'
                    yield u'#'
                elif command[0] in list(data.keys()) + ['calc']:
                    # output a value
                    if not valid_data:
                        continue
                    # format is: key fmt_string no_value_string conversion
                    # get value
                    if command[0] == 'calc':
                        x = eval(command[1])
                        del command[1]
                    else:
                        x = data[command[0]]
                    # adjust time
                    if isinstance(x, datetime):
                        if round_time:
                            x += round_time
                        if local_time:
                            x = time_zone.utc_to_local(x)
                        else:
                            x = x.replace(tzinfo=time_zone.utc)
                    # convert data
                    if x is not None and len(command) > 3:
                        x = eval(command[3])
                    # get format
                    fmt = u'%s'
                    if len(command) > 1:
                        fmt = command[1]
                    # write output
                    if x is None:
                        if len(command) > 2:
                            yield command[2]
                    elif isinstance(x, datetime):
                        if sys.version_info[0] < 3:
                            fmt = fmt.encode(file_encoding)
                        x = x.strftime(fmt)
                        if sys.version_info[0] < 3:
                            if self.encoding == 'html':
                                x = x.decode('ascii',
                                             errors='xmlcharrefreplace')
                            else:
                                x = x.decode(file_encoding)
                        yield x
                    elif not use_locale:
                        yield fmt % (x)
                    elif sys.version_info >= (2, 7) or '%%' not in fmt:
                        yield locale.format_string(fmt, x)
                    else:
                        yield locale.format_string(fmt.replace('%%', '##'),
                                                   x).replace('##', '%')
                elif command[0] == 'monthly':
                    data_set = self.monthly_data
                    idx, valid_data = jump(datetime.max, -1)
                    data = data_set[idx]
                elif command[0] == 'daily':
                    data_set = self.daily_data
                    idx, valid_data = jump(datetime.max, -1)
                    data = data_set[idx]
                elif command[0] == 'hourly':
                    data_set = self.hourly_data
                    idx, valid_data = jump(datetime.max, -1)
                    data = data_set[idx]
                elif command[0] == 'raw':
                    data_set = self.calib_data
                    idx, valid_data = jump(datetime.max, -1)
                    data = data_set[idx]
                elif command[0] == 'live':
                    data_set = self.calib_data
                    idx = live_data['idx']
                    valid_data = True
                    data = live_data
                elif command[0] == 'timezone':
                    if command[1] == 'utc':
                        local_time = False
                    elif command[1] == 'local':
                        local_time = True
                    else:
                        logger.error("Unknown time zone: %s", command[1])
                        return
                elif command[0] == 'locale':
                    use_locale = eval(command[1])
                elif command[0] == 'encoding':
                    self.encoding = command[1]
                    file_encoding = self.encoding
                    if file_encoding == 'html':
                        file_encoding = 'ascii'
                elif command[0] == 'roundtime':
                    if eval(command[1]):
                        round_time = timedelta(seconds=30)
                    else:
                        round_time = None
                elif command[0] == 'jump':
                    prevdata = data
                    idx, valid_data = jump(idx, int(command[1]))
                    data = data_set[idx]
                elif command[0] == 'goto':
                    prevdata = data
                    time_str = command[1]
                    if '%' in time_str:
                        if local_time:
                            lcl = time_zone.utc_to_local(idx)
                        else:
                            lcl = idx.replace(tzinfo=time_zone.utc)
                        time_str = lcl.strftime(time_str)
                    new_idx = pywws.weatherstation.WSDateTime.from_csv(
                        time_str)
                    if local_time:
                        new_idx = time_zone.local_to_utc(new_idx)
                    new_idx = data_set.after(new_idx)
                    if new_idx:
                        idx = new_idx
                        data = data_set[idx]
                        valid_data = True
                    else:
                        valid_data = False
                elif command[0] == 'loop':
                    loop_count = int(command[1])
                    loop_start = tmplt.tell()
                elif command[0] == 'endloop':
                    loop_count -= 1
                    if valid_data and loop_count > 0:
                        tmplt.seek(loop_start, 0)
                else:
                    logger.error("Unknown processing directive: #%s#", part)
                    return
            line = ''