'''
 Generate the storage netcdf file for 1km MRMS data over the Midwest
'''


from pyiem import iemre
import netCDF4
import datetime
import numpy as np
import sys

def init_year(ts):
    """
    Create a new NetCDF file for a year of our specification!
    """

    fp = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (ts.year, )
    nc = netCDF4.Dataset(fp, 'w')
    nc.title         = "MRMS Daily Precipitation %s" % (ts.year,)
    nc.platform      = "Grided Estimates"
    nc.description   = "Midwest MRMS 0.01 degree grid"
    nc.institution   = "Iowa State University, Ames, IA, USA"
    nc.source        = "Iowa Environmental Mesonet"
    nc.project_id    = "IEM"
    nc.realization   = 1
    nc.Conventions   = 'CF-1.0'
    nc.contact       = "Daryl Herzmann, akrherz@iastate.edu, 515-294-5978"
    nc.history       = "%s Generated" % (datetime.datetime.now().strftime("%d %B %Y"),)
    nc.comment       = "No Comment at this time"



    # Setup Dimensions
    nc.createDimension('lat', (iemre.NORTH - iemre.SOUTH) * 100.0)
    nc.createDimension('lon', (iemre.EAST - iemre.WEST) * 100.0)
    days = ((ts.replace(year=ts.year+1)) - ts).days
    nc.createDimension('time', int(days) ) 

    # Setup Coordinate Variables
    lat = nc.createVariable('lat', np.float, ('lat',) )
    lat.units = "degrees_north"
    lat.long_name = "Latitude"
    lat.standard_name = "latitude"
    lat.axis = "Y"
    lat[:] = np.arange(iemre.SOUTH, iemre.NORTH, 0.01)

    lon = nc.createVariable('lon', np.float, ('lon',) )
    lon.units = "degrees_east"
    lon.long_name = "Longitude"
    lon.standard_name = "longitude"
    lon.axis = "X"
    lon[:] = np.arange(iemre.WEST, iemre.EAST, 0.01)

    tm = nc.createVariable('time', np.float, ('time',) )
    tm.units = "Days since %s-01-01 00:00:0.0" % (ts.year,)
    tm.long_name = "Time"
    tm.standard_name = "time"
    tm.axis = "T"
    tm.calendar = "gregorian"
    tm[:] = np.arange(0, int(days) )

    p01d = nc.createVariable('p01d', np.float, ('time','lat','lon'), 
                             fill_value=1.e20)
    p01d.units = 'mm'
    p01d.long_name = 'Precipitation'
    p01d.standard_name = 'Precipitation'
    p01d.coordinates = "lon lat"
    p01d.description = "Precipitation accumulation for the day"

    nc.close()

init_year(datetime.datetime(int(sys.argv[1]),1,1))

@@ -39,11 +39,15 @@
 for 
-1km
+0.01deg
  MRM
@@ -1187,16 +1187,48 @@
 days) ) 
+%0A    nc.createDimension('nv', 2)
 %0A%0A    # 
@@ -1420,18 +1420,65 @@
 lat.
-axis = %22Y%22
+bounds = %22lat_bnds%22%0A    lat.axis = %22Y%22%0A    # Grid centers
 %0A   
@@ -1508,16 +1508,24 @@
 re.SOUTH
+ + 0.005
 , iemre.
@@ -1534,24 +1534,232 @@
 RTH, 0.01)%0A%0A
+    lat_bnds = nc.createVariable('lat_bnds', np.float, ('lat', 'nv'))%0A    lat_bnds%5B:,0%5D = np.arange(iemre.SOUTH, iemre.NORTH, 0.01)%0A    lat_bnds%5B:,1%5D = np.arange(iemre.SOUTH + 0.01, iemre.NORTH +0.01, 0.01)%0A%0A
     lon = nc
@@ -1893,32 +1893,60 @@
 e = %22longitude%22%0A
+    lon.bounds = %22lon_bnds%22%0A
     lon.axis = %22
@@ -1998,24 +1998,228 @@
 AST, 0.01)%0A%0A
+    lon_bnds = nc.createVariable('lon_bnds', np.float, ('lon', 'nv'))%0A    lon_bnds%5B:,0%5D = np.arange(iemre.WEST, iemre.EAST, 0.01)%0A    lon_bnds%5B:,1%5D = np.arange(iemre.WEST + 0.01, iemre.EAST +0.01, 0.01)%0A%0A
     tm = nc.

add bnds to netcdf file