"""
 Grid the daily data onto a midwest grid for IEMRE
"""
import sys
import netCDF4
import numpy as np
import datetime
import psycopg2
from pyiem import iemre, datatypes
from psycopg2.extras import DictCursor
from scipy.interpolate import NearestNDInterpolator

COOP = psycopg2.connect(database='iem', host='iemdb', user='nobody')
icursor = COOP.cursor(cursor_factory=DictCursor)


def generic_gridder(rs, idx):
    """
    Generic gridding algorithm for easy variables
    """
    lats = []
    lons = []
    vals = []
    for row in rs:
        if row[idx] is not None:
            lats.append(  row['lat']  )
            lons.append(  row['lon'] )
            vals.append( row[idx]  )
    if len(vals) < 4:
        print "Only %s observations found for %s, won't grid" % (len(vals),
               idx)
        return None
    
    nn = NearestNDInterpolator((np.array(lons), np.array(lats)), np.array(vals))
    xi, yi = np.meshgrid(iemre.XAXIS, iemre.YAXIS)
    grid = nn(xi, yi)
    
    return grid
    

def grid_day(nc, ts):
    """
    """
    offset = iemre.daily_offset(ts)
    icursor.execute("""
       SELECT ST_x(s.geom) as lon, ST_y(s.geom) as lat, 
       (CASE WHEN pday >= 0 then pday else null end) as precipdata,
       (CASE WHEN max_tmpf > -50 and max_tmpf < 130 then max_tmpf else null end) as highdata,
       (CASE WHEN min_tmpf > -50 and min_tmpf < 95 then min_tmpf else null end) as lowdata 
       from summary_%s c, stations s WHERE day = '%s' and 
       s.network in ('IA_ASOS', 'MN_ASOS', 'WI_ASOS', 'IL_ASOS', 'MO_ASOS',
        'KS_ASOS', 'NE_ASOS', 'SD_ASOS', 'ND_ASOS', 'KY_ASOS', 'MI_ASOS',
        'OH_ASOS', 'AWOS') and c.iemid = s.iemid 
       and pday >= 0""" % (ts.year, ts.strftime("%Y-%m-%d")))

    if icursor.rowcount > 4:
        res = generic_gridder(icursor, 'highdata')
        nc.variables['high_tmpk'][offset] = datatypes.temperature(res, 'F').value('K')
        icursor.scroll(0, mode='absolute')
        res = generic_gridder(icursor, 'lowdata')
        nc.variables['low_tmpk'][offset] = datatypes.temperature(res, 'F').value('K')
        icursor.scroll(0, mode='absolute')
        res = generic_gridder(icursor, 'precipdata')
        nc.variables['p01d'][offset] = res * 25.4
    else:
        print "%s has %02i entries, FAIL" % (ts.strftime("%Y-%m-%d"), 
            icursor.rowcount)

def main(ts):

    # Load up our netcdf file!
    nc = netCDF4.Dataset("/mesonet/data/iemre/%s_mw_daily.nc" % (ts.year,), 'a')
    grid_day(nc , ts)
    nc.close()

if __name__ == "__main__":
    if len(sys.argv) == 4:
        ts = datetime.datetime(int(sys.argv[1]), int(sys.argv[2]),
                               int(sys.argv[3]))
    else:
        ts = datetime.datetime.now()
        ts = ts.replace(hour=0, minute=0, second=0)
    main(ts)

@@ -129,16 +129,28 @@
 sycopg2%0A
+import pytz%0A
 from pyi
@@ -1012,20 +1012,534 @@
 rn grid%0A
-    
+%0A%0Adef do_precip(nc, ts):%0A    %22%22%22Compute the precip totals based on the hourly analysis totals%22%22%22%0A    offset = iemre.daily_offset(ts)%0A    ets = ts.replace(hour=12, tzinfo=pytz.timezone(%22UTC%22))%0A    sts = ets - datetime.timedelta(hours=24)%0A    offset1 = iemre.hourly_offset(sts)%0A    offset2 = iemre.hourly_offset(ets)%0A    hnc = netCDF4.Dataset(%22/mesonet/data/iemre/%25s_mw_hourly.nc%22 %25 (ts.year,))%0A    phour = np.sum(hnc.variables%5B'p01m'%5D%5Boffset1:offset2, :, :%5D, 0)%0A    nc.variables%5B'p01d'%5D%5Boffset%5D = phour%0A    hnc.close()%0A
 %0A%0Adef gr
@@ -2198,25 +2198,24 @@
  s.iemid
- 
 %0A       
 and pday
@@ -2210,21 +2210,9 @@
     
-and pday %3E= 0
+ 
 %22%22%22 
@@ -2639,32 +2639,33 @@
 olute')%0A        
+#
 res = generic_gr
@@ -2693,32 +2693,33 @@
 pdata')%0A        
+#
 nc.variables%5B'p0
@@ -2856,16 +2856,17 @@
 count)%0A%0A
+%0A
 def main
@@ -2871,17 +2871,16 @@
 in(ts):%0A
-%0A
     # Lo
@@ -2981,30 +2981,76 @@
 r,),
- 'a')%0A    grid_day(nc 
+%0A                         'a')%0A    grid_day(nc, ts)%0A    do_precip(nc
 , ts

use IEMRE hourly analysis to build daily precip totals