"""Extraction as requested by IA Public Health"""
import psycopg2
import sys


def main(argv):
    """Do Something"""
    pgconn = psycopg2.connect(database='postgis', host='iemdb', user='nobody')
    cursor = pgconn.cursor()
    cursor2 = pgconn.cursor()
    pgconn2 = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    cursor3 = pgconn2.cursor()
    cursor.execute("""SELECT ugc, ST_X(centroid), ST_Y(centroid)
    from ugcs where state = 'IA' and substr(ugc,3,1) = 'C' and end_ts is null
    ORDER by ugc ASC""")
    for row in cursor:
        fips = row[0][3:]
        # Get closest climodat site
        cursor2.execute("""SELECT id, ST_Distance(geom,
        ST_SetSRID(ST_GeomFromText('POINT(%s %s)'), 4326))
        from stations where network = 'IACLIMATE' and id != 'IA0000'
        and substr(id,3,1) != 'C' ORDER by st_distance ASC LIMIT 1
            """, (row[1], row[2]))
        sid = cursor2.fetchone()[0]

        cursor3.execute("""SELECT year, month,
        max(high), min(low), avg((high+low)/2.), sum(precip),
        sum(case when high >= 95 then 1 else 0 end),
        sum(case when low >= 70 then 1 else 0 end) from alldata_ia WHERE
        station = %s and year >= 2000 and year < 2015 GROUP by year, month
        """, (sid,))
        for row in cursor3:
            print ','.join([str(i) for i in [fips, row[0], row[1], row[2],
                                             row[3], row[4], row[5], row[6],
                                             row[7]]])
if __name__ == '__main__':
    main(sys.argv)

@@ -71,16 +71,41 @@
 rt sys%0A%0A
+YEAR = int(sys.argv%5B1%5D)%0A%0A
 %0Adef mai
@@ -184,38 +184,83 @@
 postgis', host='
-iemdb'
+localhost',%0A                              port=5555
 , user='nobody')
@@ -377,14 +377,60 @@
 st='
-iemdb'
+localhost',%0A                               port=5555
 , us
@@ -1318,31 +1318,12 @@
 ear 
-%3E
 = 
-2000 and year %3C 2015
+%25s
  GRO
@@ -1358,16 +1358,21 @@
 %22, (sid,
+ YEAR
 ))%0A     
@@ -1601,16 +1601,18 @@
 w%5B7%5D%5D%5D)%0A
+%0A%0A
 if __nam

generalize IDHS extraction script