"""
Heating Degree Days
"""
import constants
import mx.DateTime

def chdd(high, low, base):
    hdd = 0
    try:
        a = float(int(high) + int(low)) / 2.00
    except:
        return 0
    if a < base:
        hdd = base - a

    return hdd  

def go(mydb, rs, stationID, updateAll=False):
    if updateAll:
        s = constants.startts(stationID)
    else:
        s = constants._ENDTS - mx.DateTime.RelativeDateTime(years=1)
    e = constants._ENDTS
    interval = mx.DateTime.RelativeDateTime(months=+1)

    now = s
    db = {}
    db60 = {}
    while (now < e):
        db[now] = 0
        db60[now] = 0
        now += interval

    for i in range(len(rs)):
        ts = mx.DateTime.strptime( rs[i]["day"] , "%Y-%m-%d")
        if ts < s:
            continue
        mo = ts + mx.DateTime.RelativeDateTime(day=1)
        db[mo] += chdd(rs[i]["high"], rs[i]["low"], 65.0)
        db60[mo] += chdd(rs[i]["high"], rs[i]["low"], 60.0)

    for mo in db.keys():
        mydb.query("""UPDATE r_monthly SET hdd = %s, hdd60 = %s WHERE 
          station = '%s' and monthdate = '%s' """ % (db[mo], db60[mo],
                                    stationID, mo.strftime("%Y-%m-%d") ) )

def write(mydb, out, station):
    YRCNT = constants.yrcnt(station)
    out.write("""# THESE ARE THE MONTHLY HEATING DEGREE DAYS (base=65) %s-%s FOR STATION  %s
YEAR    JAN    FEB    MAR    APR    MAY    JUN    JUL    AUG    SEP    OCT    NOV    DEC
""" % (constants.startyear(station), constants._ENDYEAR, station) )

    rs = mydb.query("""SELECT * from r_monthly WHERE station = '%s'""" % (
                                        station,) ).dictresult()
    db = {}
    db60 = {}
    for i in range(len(rs)):
        mo = mx.DateTime.strptime( rs[i]["monthdate"], "%Y-%m-%d")
        db[mo] = rs[i]["hdd"]
        db60[mo] = rs[i]["hdd60"]

    moTot = {}
    moTot60 = {}
    for mo in range(1,13):
        moTot[mo] = 0
        moTot60[mo] = 0

    second = """# THESE ARE THE MONTHLY HEATING DEGREE DAYS (base=60) %s-%s FOR STATION  %s
YEAR    JAN    FEB    MAR    APR    MAY    JUN    JUL    AUG    SEP    OCT    NOV    DEC\n""" % (
            constants.startyear(station), constants._ENDYEAR, station)
    yrCnt = 0
    for yr in range(constants.startyear(station), constants._ENDYEAR):
        yrCnt += 1
        out.write("%4i" % (yr,) )
        second += "%4i" % (yr,)
        for mo in range(1, 13):
            ts = mx.DateTime.DateTime(yr, mo, 1)
            if (ts >= constants._ARCHIVEENDTS):
                out.write("%7s" % ("M",) )
                second += "%7s" % ("M",)
                continue
            if (ts < constants._ARCHIVEENDTS):
                moTot[mo] += db[ts]
                moTot60[mo] += db60[ts]
            out.write("%7.0f" % (db[ts],) )
            second += "%7.0f" % (db60[ts],)
        out.write("\n")
        second += "\n"

    out.write("MEAN")
    second += "MEAN"
    for mo in range(1, 13):
        out.write("%7.0f" % ( float(moTot[mo]) / float( YRCNT[mo] ) ) )
        second += "%7.0f" % ( float(moTot60[mo]) / float( YRCNT[mo] ) ) 
    out.write("\n")
    second += "\n"
    out.write(second)

