import pytz
import datetime
import psycopg2
pgconn = psycopg2.connect(host='127.0.0.1', port=5555, user='mesonet', database='mec')
cursor = pgconn.cursor()

dates = """06-02-2008 00z - 06-07-2008 06z
06-09-2008 00z - 06-14-2008 06z
06-23-2008 00z - 06-25-2008 06z
07-04-2008 00z - 07-06-2008 06z
08-15-2008 00z - 08-23-2008 06z
02-19-2009 00z - 02-25-2009 06z
03-02-2009 00z - 03-07-2009 06z
03-28-2009 00z - 04-03-2009 06z"""

def c(val):
    if val is None:
        return 'M'
    return val

for line in dates.split("\n"):
    tokens = line.split(" - ")
    sts = datetime.datetime.strptime(tokens[0][:12], '%m-%d-%Y %H')
    sts = sts.replace(tzinfo=pytz.timezone("UTC"))
    ets = datetime.datetime.strptime(tokens[1][:12], '%m-%d-%Y %H')
    ets = ets.replace(tzinfo=pytz.timezone("UTC"))
    output = open('extract/%s-%s.txt' % (sts.strftime("%Y%m%d%H%M"),
                                         ets.strftime("%Y%m%d%H%M")), 'w')
    output.write("utcvalid,avg_power,avg_windspeed,stddev_windspeed,count\n")
    cursor.execute("""
      select valid, avg(power), avg(windspeed), stddev(windspeed),
      count(*) from sampled_data 
      WHERE valid >= %s and valid < %s 
      and extract(minute from valid)::int %% 10 = 0 and power is not null
      and windspeed is not null GROUP by valid ORDER by valid ASC
    """, (sts, ets))
    print sts, ets, cursor.rowcount
    for row in cursor:
        ts = row[0].astimezone(pytz.timezone("UTC"))
        output.write("%s,%s,%s,%s,%s\n"  % ( 
           row[0].strftime("%Y-%m-%d %H:%M:%S"), 
           c(row[1]), c(row[2]), c(row[3]), row[4] ))

    output.close()

add extraction tool as per request from Renee