#!/usr/bin/env python # Find the local max/min from a weather data day file # Version date: November 1, 2011 # import sys import os import string import time # # Define some station-specific constants. # station_name = "Table Mesa - Boulder, CO Weather" # 1 for English units, 0 for Metric English = 1 # Directories and filenames datadir = "/home/dv/data/" webdir = "/var/www/" maxminfile = "maxmin.html" BADVAL = -99.9 # # Unit Conversion Functions. # def c2f (tempc): return ((tempc * 1.8) + 32.0) def mb2inHg (presmb): return (presmb * 0.02953) def kph2mph (windmps): return (windmps * 0.6214) def mm2in (rainmm): return (rainmm * 0.03937) wind_dirs =["BAD", "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] # Calculate the wind direction name. def wdir_name (wdir): wdirofs = wdir + 11.25 if (wdirofs > 360.0): wdirofs = wdirofs - 360.0 return (wind_dirs[(int)((wdirofs / 22.5) + 1)]) # One line in the data file is: #sample time (unix time) (long) #outside temperature (deg C) (float) #outside humidity (%) (float) #wind chill (deg C) (float) #dewpoint (deg C) (float) #pressure (millibars) (float) #dp/dt (millibars/??) (float) #altitude corrected pressure (millibars) (float) #wind speed (M/sec) (float) #wind direction (degrees) (float) #peak gust (M/sec) (float) #rain accumulation (mm) (float) #inside temperature (deg C) (float) #inside humidity (%) (float) # There are 14 time/data fields per sample. # There should be 1440 samples per day, 1 per minute unless the file is short. # Data field indices Ftime = 0 Fotemp = 1 Fohum = 2 Fwchil = 3 Fdewp = 4 Fpres = 5 Fdpdt = 6 Fcpres = 7 Fwspd = 8 Fwdir = 9 Fgust = 10 Fraina = 11 Fitemp = 12 Fihum = 13 # Find the last 2 ascii data file names (.asc) in the data directory. file1 = "" file2 = "" # Get a list of the files in datadir, sort the list. dirlist = os.listdir(datadir) dirlist.sort () for filename in dirlist: pos = string.find (filename, ".asc") if (pos > 0): file1 = file2 file2 = filename print "Reading data from:", file1, "and", file2 # Check for non existent files. if ((len (file1) == 0) | (len (file2) == 0)): print "Error: data file(s) not found." sys.exit (1) # Verify that the two data files are readable. f1a = os.access (datadir + file1, os.R_OK) f2a = os.access (datadir + file2, os.R_OK) if ((f1a +f2a) != 2): print "Error: no read permissions for data file(s)." sys.exit (1) if (os.access (webdir + maxminfile, os.W_OK) != 1): print "Error: no write permissions for maxmin file." sys.exit (1) # # Read yesterday's full file and today's usually partial file # into flines. # flines = [] file1=open(datadir + file1,'r') for line in file1.readlines(): flines.append (string.split (line)) file1.close() f1lines = len(flines) #print "File 1 line count:", f1lines file2=open(datadir + file2,'r') for line in file2.readlines(): flines.append (string.split (line)) file2.close() #print "File 2 line count:", len(flines)-f1lines # # To Do: # Calculate missing vals based on date gaps, insert badvals into data list. # For now, just process a short list if the files are short. # if (len(flines) < 1440): dlines = len(flines) else: dlines = 1440 # # Time and Data vector lists # vtime = [] votemp = [] vohum = [] vwchil = [] vdewp = [] vpres = [] vdpdt = [] vcpres = [] vwspd = [] vwdir = [] vgust = [] vraina = [] vitemp = [] vihum = [] # # Convert and fill up the individual time/data lists. # Still breaks with missing data in the middle (at least in the 2nd file). # #print "len flines:", len(flines) #print "dlines:", dlines #print flines for ind in range (0-dlines,0): # print "ind:", ind vtime.append (time.localtime (int(flines[ind][Ftime]))) votemp.append (float(flines[ind][Fotemp])) vohum.append (float(flines[ind][Fohum])) vwchil.append (float(flines[ind][Fwchil])) vdewp.append (float(flines[ind][Fdewp])) vpres.append (float(flines[ind][Fpres])) vdpdt.append (float(flines[ind][Fdpdt])) vcpres.append (float(flines[ind][Fcpres])) vwspd.append (float(flines[ind][Fwspd])) vwdir.append (float(flines[ind][Fwdir])) vgust.append (float(flines[ind][Fgust])) vraina.append (float(flines[ind][Fraina])) vitemp.append (float(flines[ind][Fitemp])) vihum.append (float(flines[ind][Fihum])) # We're done with the file lines. del flines # # Build a list of lists with all of the data, build a list of field names. # alldata = [vtime, votemp, vohum, vwchil, vdewp, vpres, vdpdt, vcpres, vwspd, vwdir, vgust, vraina, vitemp, vihum] allnames = ["Sample Time", "Outside Temperature", "Outside Humidity", "Wind Chill", "Dewpoint", "Pressure", "Dp/Dt", "Pressure", "Wind Speed", "Wind Direction", "Peak Gust", "Rain Accumulation", "Inside Temperature", "Inside Humidity"] if English: Munits = ['°F', '%', '°F', '°F', 'in Hg', 'in Hg', 'in Hg', 'mph', '°', 'mph', 'in', '°F', '%', ''] else: Munits = ['°C', '%', '°C', '°C', 'mb', 'mb', 'mb', 'kph', '°', 'kph', 'mm', '°C', '%', ''] mindata = [] tmin = [] maxdata = [] tmax = [] # Keep numeric wind directions for max speed and max gust. maxspeeddir = -99.9 maxgustdir = -99.9 # # Loop through all of the data lists and look for max/min info. # range 1,14 produces 1 to 13. # for ind1 in range (1,14): workingmax = -5000.0 workingmin = 5000.0 for ind2 in range (0,dlines): if (alldata [ind1][ind2] < workingmin): workingmin = alldata [ind1][ind2] mintime = alldata [0][ind2] if (alldata [ind1][ind2] > workingmax): workingmax = alldata [ind1][ind2] maxtime = alldata [0][ind2] # Record wind direction of max speed and gust. if (ind1 == 8): maxspeeddir = alldata [9][ind2] elif (ind1 == 10): maxgustdir = alldata [9][ind2] # for ind3 in range (1,14): # print "data:", ind1,ind3,":",alldata[ind3][ind2] # Stuff the max/min and time lists. mindata.append (workingmin) tmin.append (mintime) maxdata.append (workingmax) tmax.append (maxtime) # # Write the max/min results to an html file. # wfile=open(webdir + maxminfile, 'w') # Write the DOCTYPE header. (This turns off the table colors for some reason.) #wfile.write ("\n") # Write the html header and table beginning. wfile.write ("\n
\nSample Time: %d/%d/%d %02d:%02d | " %(ts[0],ts[1],ts[2],ts[3],ts[4])) wfile.write ("Max/Min values for the previous 24 hours | ||||
---|---|---|---|---|---|
Data Type | Current | Min | Time of Min | Max | Time of Max |
%s | %3.2f %s | Max Direction: %s | %3.2f %s | %d/%d %02d:%02d | |
%s | %3.2f %s | Gust Direction: %s | %3.2f %s | %d/%d %02d:%02d | |
%s | %3.2f %s | %3.2f %s | %d/%d %02d:%02d | %3.2f %s | %d/%d %02d:%02d |
%s | %3.2f %s | Wind From The %s | \n" %(allnames[9], alldata[9][dlines-1], Munits[8], wdir_name (alldata[9][dlines-1]))) # Write the end of the metric table wfile.write ("