def __init__(self, params, calib_data, hourly_data, daily_data, monthly_data): self.logger = logging.getLogger('pywws.Tasks.RegularTasks') self.params = params self.calib_data = calib_data self.hourly_data = hourly_data self.daily_data = daily_data self.monthly_data = monthly_data # get directories self.work_dir = self.params.get('paths', 'work', '/tmp/weather') self.template_dir = self.params.get( 'paths', 'templates', os.path.expanduser('~/weather/templates/')) self.graph_template_dir = self.params.get( 'paths', 'graph_templates', os.path.expanduser('~/weather/graph_templates/')) # create calibration object self.calibrator = Calib(self.params) # create templater object self.templater = Template.Template(self.params, self.calib_data, self.hourly_data, self.daily_data, self.monthly_data) # create plotter objects self.plotter = Plot.GraphPlotter(self.params, self.calib_data, self.hourly_data, self.daily_data, self.monthly_data, self.work_dir) self.roseplotter = WindRose.RosePlotter(self.params, self.calib_data, self.hourly_data, self.daily_data, self.monthly_data, self.work_dir) # directory of service uploaders self.services = dict() # create a YoWindow object self.yowindow = YoWindow.YoWindow(self.calib_data) # get local time's offset from UTC, without DST now = self.calib_data.before(datetime.max) if not now: now = datetime.now() # was kenmc utcnow time_offset = Local.utcoffset(now) - Local.dst(now) print "Time Offset = %s" % time_offset # get daytime end hour, in UTC self.day_end_hour = eval(params.get('config', 'day end hour', '21')) self.day_end_hour = (self.day_end_hour - (time_offset.seconds / 3600)) % 24 # convert config from underground/metoffice to new services for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'): services = eval(self.params.get(section, 'services', '[]')) for svc in ('underground', 'metoffice'): if self.params.get(section, svc) == 'True': if svc not in services: services.append(svc) self.params._config.remove_option(section, svc) self.params.set(section, 'services', str(services)) # create service uploader objects for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'): for service in eval(self.params.get(section, 'services', '[]')): if service not in self.services: self.services[service] = ToService(self.params, self.calib_data, service_name=service)
def __init__(self, params, calib_data, hourly_data, daily_data, monthly_data): self.logger = logging.getLogger('pywws.Tasks.RegularTasks') self.params = params self.calib_data = calib_data self.hourly_data = hourly_data self.daily_data = daily_data self.monthly_data = monthly_data # get directories self.work_dir = self.params.get('paths', 'work', '/tmp/weather') self.template_dir = self.params.get( 'paths', 'templates', os.path.expanduser('~/weather/templates/')) self.graph_template_dir = self.params.get( 'paths', 'graph_templates', os.path.expanduser('~/weather/graph_templates/')) # create calibration object self.calibrator = Calib(self.params) # create templater object self.templater = Template.Template( self.params, self.calib_data, self.hourly_data, self.daily_data, self.monthly_data) # create plotter objects self.plotter = Plot.GraphPlotter( self.params, self.calib_data, self.hourly_data, self.daily_data, self.monthly_data, self.work_dir) self.roseplotter = WindRose.RosePlotter( self.params, self.calib_data, self.hourly_data, self.daily_data, self.monthly_data, self.work_dir) # directory of service uploaders self.services = dict() # create a YoWindow object self.yowindow = YoWindow.YoWindow(self.calib_data) # get local time's offset from UTC, without DST now = self.calib_data.before(datetime.max) if not now: now = datetime.now() # was kenmc utcnow time_offset = Local.utcoffset(now) - Local.dst(now) print "Time Offset = %s" % time_offset # get daytime end hour, in UTC self.day_end_hour = eval(params.get('config', 'day end hour', '21')) self.day_end_hour = (self.day_end_hour - (time_offset.seconds / 3600)) % 24 # convert config from underground/metoffice to new services for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'): services = eval(self.params.get(section, 'services', '[]')) for svc in ('underground', 'metoffice'): if self.params.get(section, svc) == 'True': if svc not in services: services.append(svc) self.params._config.remove_option(section, svc) self.params.set(section, 'services', str(services)) # create service uploader objects for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'): for service in eval(self.params.get(section, 'services', '[]')): if service not in self.services: self.services[service] = ToService( self.params, self.calib_data, service_name=service)
def Process(params, raw_data, calib_data, hourly_data, daily_data, monthly_data): """Generate summaries from raw weather station data. The meteorological day end (typically 2100 or 0900 local time) is set in the preferences file ``weather.ini``. The default value is 2100 (2200 during DST), following the historical convention for weather station readings. """ logger = logging.getLogger('pywws.Process') logger.info('Generating summary data') # get time of last record last_raw = raw_data.before(datetime.max) if last_raw is None: raise IOError('No data found. Check data directory parameter.') # get local time's offset from UTC, without DST time_offset = Local.utcoffset(last_raw) - Local.dst(last_raw) # set daytime end hour, in UTC day_end_hour = eval(params.get('config', 'day end hour', '21')) day_end_hour = (day_end_hour - (time_offset.seconds / 3600)) % 24 # divide 24 hours of UTC day into day and night daytime = [] for i in range(24): daytime.append(True) night_hour = (21 - (time_offset.seconds / 3600)) % 24 for i in range(12): daytime[night_hour] = False night_hour = (night_hour + 1) % 24 # calibrate raw data start = calibrate_data(logger, params, raw_data, calib_data) # generate hourly data start = generate_hourly(logger, calib_data, hourly_data, start) # generate daily data start = generate_daily(logger, day_end_hour, daytime, calib_data, hourly_data, daily_data, start) # generate monthly data generate_monthly(logger, day_end_hour, time_offset, daily_data, monthly_data, start) return 0
def DoPlot(self, input_file, output_file): # read XML graph description print input_file self.doc = xml.dom.minidom.parse(input_file) self.graph = self.doc.childNodes[0] # get list of plots plot_list = self.GetPlotList() self.plot_count = len(plot_list) if self.plot_count < 1: # nothing to plot self.doc.unlink() return 1 # get start and end datetimes self.x_lo = self.GetValue(self.graph, 'start', None) self.x_hi = self.GetValue(self.graph, 'stop', None) self.duration = self.GetValue(self.graph, 'duration', None) if self.duration == None: self.duration = timedelta(hours=24) else: self.duration = eval('timedelta(%s)' % self.duration) if self.x_lo != None: self.x_lo = eval('datetime(%s)' % self.x_lo) if self.x_hi != None: self.x_hi = eval('datetime(%s)' % self.x_hi) self.duration = self.x_hi - self.x_lo else: self.x_hi = self.x_lo + self.duration elif self.x_hi != None: self.x_hi = eval('datetime(%s)' % self.x_hi) self.x_lo = self.x_hi - self.duration else: self.x_hi = self.hourly_data.before(datetime.max) if self.x_hi == None: self.x_hi = datetime.utcnow() # only if no hourly data # set end of graph to start of the next hour after last item self.x_hi = self.x_hi + timedelta(minutes=55) self.x_hi = self.x_hi.replace(minute=0, second=0) self.x_lo = self.x_hi - self.duration self.x_hi = self.x_hi + Local.utcoffset(self.x_lo) self.x_lo = self.x_hi - self.duration self.utcoffset = Local.utcoffset(self.x_lo) # open gnuplot command file self.tmp_files = [] cmd_file = os.path.join(self.work_dir, 'plot.cmd') self.tmp_files.append(cmd_file) of = open(cmd_file, 'w') # write gnuplot set up self.rows = self.GetDefaultRows() self.cols = (self.plot_count + self.rows - 1) / self.rows self.rows, self.cols = eval(self.GetValue( self.graph, 'layout', '%d, %d' % (self.rows, self.cols))) w, h = self.GetDefaultPlotSize() w = w * self.cols h = h * self.rows w, h = eval(self.GetValue(self.graph, 'size', '(%d, %d)' % (w, h))) fileformat = self.GetValue(self.graph, 'fileformat', 'png') if fileformat == 'svg': terminal = '%s enhanced font "arial,9" size %d,%d dynamic rounded' % ( fileformat, w, h) else: terminal = '%s large size %d,%d' % (fileformat, w, h) terminal = self.GetValue(self.graph, 'terminal', terminal) of.write('set encoding %s\n' % (self.encoding)) of.write('set terminal %s\n' % (terminal)) of.write('set output "%s"\n' % (output_file)) # set overall title title = self.GetValue(self.graph, 'title', '') if title: title = title.encode(self.encoding) title = 'title "%s"' % title of.write('set multiplot layout %d, %d %s\n' % (self.rows, self.cols, title)) # do actual plots of.write(self.GetPreamble()) for plot_no in range(self.plot_count): plot = plot_list[plot_no] # set key / title location title = self.GetValue(plot, 'title', '') title = title.encode(self.encoding) of.write('set key horizontal title "%s"\n' % title) # optional yaxis labels ylabel = self.GetValue(plot, 'ylabel', '') if ylabel: ylabel = ylabel.encode(self.encoding) ylabelangle = self.GetValue(plot, 'ylabelangle', '') if ylabelangle: ylabelangle = ' rotate by %s' % (ylabelangle) of.write('set ylabel "%s"%s\n' % (ylabel, ylabelangle)) else: of.write('set ylabel\n') y2label = self.GetValue(plot, 'y2label', '') if y2label: y2label = y2label.encode(self.encoding) y2labelangle = self.GetValue(plot, 'y2labelangle', '') if y2labelangle: y2labelangle = ' rotate by %s' % (y2labelangle) of.write('set y2label "%s"%s\n' % (y2label, y2labelangle)) else: of.write('set y2label\n') # set data source source = self.GetValue(plot, 'source', 'raw') if source == 'raw': source = self.raw_data elif source == 'hourly': source = self.hourly_data elif source == 'monthly': source = self.monthly_data else: source = self.daily_data # do the plot of.write(self.PlotData(plot_no, plot, source)) of.close() self.doc.unlink() # run gnuplot on file os.system('gnuplot %s' % cmd_file) for file in self.tmp_files: os.unlink(file) return 0
def DoPlot(self, input_file, output_file): # read XML graph description print input_file self.doc = xml.dom.minidom.parse(input_file) self.graph = self.doc.childNodes[0] # get list of plots plot_list = self.GetPlotList() self.plot_count = len(plot_list) if self.plot_count < 1: # nothing to plot self.doc.unlink() return 1 # get start and end datetimes self.x_lo = self.GetValue(self.graph, 'start', None) self.x_hi = self.GetValue(self.graph, 'stop', None) self.duration = self.GetValue(self.graph, 'duration', None) if self.duration == None: self.duration = timedelta(hours=24) else: self.duration = eval('timedelta(%s)' % self.duration) if self.x_lo != None: self.x_lo = eval('datetime(%s)' % self.x_lo) if self.x_hi != None: self.x_hi = eval('datetime(%s)' % self.x_hi) self.duration = self.x_hi - self.x_lo else: self.x_hi = self.x_lo + self.duration elif self.x_hi != None: self.x_hi = eval('datetime(%s)' % self.x_hi) self.x_lo = self.x_hi - self.duration else: self.x_hi = self.hourly_data.before(datetime.max) if self.x_hi == None: self.x_hi = datetime.utcnow() # only if no hourly data # set end of graph to start of the next hour after last item self.x_hi = self.x_hi + timedelta(minutes=55) self.x_hi = self.x_hi.replace(minute=0, second=0) self.x_lo = self.x_hi - self.duration self.x_hi = self.x_hi + Local.utcoffset(self.x_lo) self.x_lo = self.x_hi - self.duration self.utcoffset = Local.utcoffset(self.x_lo) # open gnuplot command file self.tmp_files = [] cmd_file = os.path.join(self.work_dir, 'plot.cmd') self.tmp_files.append(cmd_file) of = open(cmd_file, 'w') # write gnuplot set up self.rows = self.GetDefaultRows() self.cols = (self.plot_count + self.rows - 1) / self.rows self.rows, self.cols = eval( self.GetValue(self.graph, 'layout', '%d, %d' % (self.rows, self.cols))) w, h = self.GetDefaultPlotSize() w = w * self.cols h = h * self.rows w, h = eval(self.GetValue(self.graph, 'size', '(%d, %d)' % (w, h))) fileformat = self.GetValue(self.graph, 'fileformat', 'png') if fileformat == 'svg': terminal = '%s enhanced font "arial,9" size %d,%d dynamic rounded' % ( fileformat, w, h) else: terminal = '%s large size %d,%d' % (fileformat, w, h) terminal = self.GetValue(self.graph, 'terminal', terminal) of.write('set encoding %s\n' % (self.encoding)) of.write('set terminal %s\n' % (terminal)) of.write('set output "%s"\n' % (output_file)) # set overall title title = self.GetValue(self.graph, 'title', '') if title: title = title.encode(self.encoding) title = 'title "%s"' % title of.write('set multiplot layout %d, %d %s\n' % (self.rows, self.cols, title)) # do actual plots of.write(self.GetPreamble()) for plot_no in range(self.plot_count): plot = plot_list[plot_no] # set key / title location title = self.GetValue(plot, 'title', '') title = title.encode(self.encoding) of.write('set key horizontal title "%s"\n' % title) # optional yaxis labels ylabel = self.GetValue(plot, 'ylabel', '') if ylabel: ylabel = ylabel.encode(self.encoding) ylabelangle = self.GetValue(plot, 'ylabelangle', '') if ylabelangle: ylabelangle = ' rotate by %s' % (ylabelangle) of.write('set ylabel "%s"%s\n' % (ylabel, ylabelangle)) else: of.write('set ylabel\n') y2label = self.GetValue(plot, 'y2label', '') if y2label: y2label = y2label.encode(self.encoding) y2labelangle = self.GetValue(plot, 'y2labelangle', '') if y2labelangle: y2labelangle = ' rotate by %s' % (y2labelangle) of.write('set y2label "%s"%s\n' % (y2label, y2labelangle)) else: of.write('set y2label\n') # set data source source = self.GetValue(plot, 'source', 'raw') if source == 'raw': source = self.raw_data elif source == 'hourly': source = self.hourly_data elif source == 'monthly': source = self.monthly_data else: source = self.daily_data # do the plot of.write(self.PlotData(plot_no, plot, source)) of.close() self.doc.unlink() # run gnuplot on file os.system('gnuplot %s' % cmd_file) for file in self.tmp_files: os.unlink(file) return 0