import psycopg2
import pytz

PGCONN = psycopg2.connect(database='mec', host='127.0.0.1', port='5555')
cursor = PGCONN.cursor()
cursor2 = PGCONN.cursor()

cursor.execute("""SELECT id, unitnumber, ST_x(geom), ST_y(geom) 
    from turbines""")

o = open('turbines.csv', 'w')
o.write("TID,LON,LAT\n")

p = open('turbine_data.csv', 'w')
p.write("TID,VALID_UTC,VALID_LOCAL,POWER,YAW,WINDSPEED,PITCH\n")

for row in cursor:
    o.write("%s,%.6f,%.6f\n" % (row[0], row[2], row[3]))
    
    cursor2.execute("""SELECT valid at time zone 'UTC', power, yaw, 
    windspeed, pitch from sampled_data_%s WHERE
    valid between '2008-03-01' and '2008-09-01' 
    ORDER by valid ASC """ % (row[1],))
    
    for row2 in cursor2:
        ts = row2[0]
        ts = ts.replace(tzinfo=pytz.timezone("UTC"))
        p.write("%s,%s,%s,%s,%s,%s,%s\n" % (row[0], 
                    ts.strftime("%Y-%m-%d %H:%M:%S"), 
(ts.astimezone(pytz.timezone("America/Chicago"))).strftime("%Y-%m-%d %H:%M:%S"),                     
                    row2[1], row2[2], row2[3], row2[4]))
    print row[0], cursor2.rowcount
    
p.close()
o.close()
@@ -370,16 +370,21 @@
 WER,YAW,
+YAW2,
 WINDSPEE
@@ -545,16 +545,21 @@
 r, yaw, 
+yaw2,
 %0A    win
@@ -625,12 +625,12 @@
 '200
-8-03
+9-12
 -01'
@@ -641,13 +641,13 @@
  '20
+1
 0
-8
 -0
-9
+1
 -01'
@@ -829,16 +829,19 @@
 %25s,%25s,%25s
+,%25s
 %5Cn%22 %25 (r
@@ -1059,16 +1059,25 @@
  row2%5B4%5D
+, row2%5B5%5D
 ))%0A    p

add yaw2 to dump for seon