def go(mydb, rs, station, updateAll=False): if updateAll: s = constants.startts(station) 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] += ccdd(rs[i]["high"], rs[i]["low"], 65.0) db60[mo] += ccdd(rs[i]["high"], rs[i]["low"], 60.0) for mo in db.keys(): mydb.query("""UPDATE r_monthly SET cdd = %s, cdd60 = %s WHERE station = '%s' and monthdate = '%s' """ % (db[mo], db60[mo], station, mo.strftime("%Y-%m-%d") ) )
def modMonth(stationID, out, db, adb, mo1, mo2, mt1, mt2): out.write("""\n %-12s %-12s **************************** *************************** YEAR 40-86 48-86 50-86 52-86 40-86 48-86 50-86 52-86 **************************** *************************** \n""" \ % (mt1, mt2)) s = constants.startts(stationID) e = constants._ARCHIVEENDTS interval = mx.DateTime.RelativeDateTime(years=+1) now = s while (now < e): m1 = now + mx.DateTime.RelativeDateTime(month=mo1) m2 = now + mx.DateTime.RelativeDateTime(month=mo2) if (m1 >= constants._ARCHIVEENDTS): db[m1] = {40: 'M', 48: 'M', 50: 'M', 52: 'M'} if (m2 >= constants._ARCHIVEENDTS): db[m2] = {40: 'M', 48: 'M', 50: 'M', 52: 'M'} out.write("%5i%7s%7s%7s%7s%7s%7s%7s%7s\n" % (now.year, \ db[m1][40], db[m1][48], db[m1][50], db[m1][52], \ db[m2][40], db[m2][48], db[m2][50], db[m2][52]) ) now += interval out.write(" **************************** ****************************\n") out.write(" MEAN%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n" \ % (adb[mo1]["a40"], adb[mo1]["a48"], adb[mo1]["a50"], adb[mo1]["a52"], \ adb[mo2]["a40"], adb[mo2]["a48"], adb[mo2]["a50"], adb[mo2]["a52"]) ) out.write(" STDV%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n" \ % (adb[mo1]["s40"], adb[mo1]["s48"], adb[mo1]["s50"], adb[mo1]["s52"], \ adb[mo2]["s40"], adb[mo2]["s48"], adb[mo2]["s50"], adb[mo2]["s52"]) )
def go(mydb, rs, stationID, updateAll=False): """ Compute the monthly GDDs, but only do as much work as necessary """ 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 = {} while now < e: db[now] = {40: 0, 48: 0, 50: 0, 52: 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) # First of month for base in (40,48,50,52): db[mo][base] += cgdd(base, rs[i]["high"], rs[i]["low"]) for mo in db.keys(): mydb.query("""UPDATE r_monthly SET gdd40 = %s, gdd48 = %s, gdd50 = %s, gdd52 = %s WHERE station = '%s' and monthdate = '%s'""" % ( db[mo][40], db[mo][48], db[mo][50], db[mo][52], stationID, mo.strftime("%Y-%m-%d") ) )
def go(mydb, stationID,updateAll=False): """ Generate the monthly averages, but only do as much as necessary """ 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 = {} while (now < e): db[now] = {} now += interval rs = mydb.query("""SELECT year, month, avg(high) as avg_high, avg(low) as avg_low, sum(precip) as rain, sum( CASE WHEN precip >= 0.01 THEN 1 ELSE 0 END ) as rcount, sum( CASE WHEN snow >= 0.01 THEN 1 ELSE 0 END ) as scount from %s WHERE station = '%s' and day >= '%s-01-01' GROUP by year, month""" % ( constants.get_table(stationID), stationID, s.year ) ).dictresult() for i in range(len(rs)): ts = mx.DateTime.DateTime( int(rs[i]["year"]), int(rs[i]["month"]), 1) sql = """UPDATE r_monthly SET avg_high = %s, avg_low = %s, rain = %s, rain_days = %s, snow_days = %s WHERE station = '%s' and monthdate = '%s' """ % (rs[i]["avg_high"], rs[i]["avg_low"], rs[i]["rain"], rs[i]["rcount"], rs[i]['scount'], stationID, ts.strftime("%Y-%m-%d") ) mydb.query(sql)
def write(monthly_rows, out, out2, out3, out4, station): s = constants.startts(station) e = constants._ENDTS YRCNT = constants.yrcnt(station) YEARS = e.year - s.year + 1 interval = mx.DateTime.RelativeDateTime(months=+1) now = s db = {} while now < e: db[now] = {"avg_high": "M", "avg_low": "M", "rain": "M"} now += interval for row in monthly_rows: ts = mx.DateTime.DateTime(row["year"], row["month"], 1) db[ts] = {"avg_high": row["avg_high"], "avg_low": row["avg_low"], "rain": row["sum_precip"]} out.write( """# Monthly Average High Temperatures [F] YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN """ ) moTot = {} for mo in range(1, 13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out.write("%4i" % (yr,)) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if ts < constants._ARCHIVEENDTS: moTot[mo] += int(db[ts]["avg_high"]) yrSum += int(db[ts]["avg_high"]) out.write(safePrint(db[ts]["avg_high"], 6, 0)) if yr != constants._ARCHIVEENDTS.year: yrAvg += float(yrSum) / 12.0 out.write("%6.0f\n" % (float(yrSum) / 12.0,)) else: out.write(" \n") out.write("MEAN") for mo in range(1, 13): moAvg = moTot[mo] / float(YRCNT[mo]) out.write("%6.0f" % (moAvg,)) out.write("%6.0f\n" % (yrAvg / (float(YEARS)),)) out2.write( """# Monthly Average Low Temperatures [F] YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN """ ) moTot = {} for mo in range(1, 13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out2.write("%4i" % (yr,)) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if ts < constants._ARCHIVEENDTS: moTot[mo] += int(db[ts]["avg_low"]) yrSum += int(db[ts]["avg_low"]) out2.write(safePrint(db[ts]["avg_low"], 6, 0)) if yr != constants._ARCHIVEENDTS.year: yrAvg += float(yrSum) / 12.0 out2.write("%6.0f\n" % (float(yrSum) / 12.0,)) else: out2.write(" M\n") out2.write("MEAN") for mo in range(1, 13): moAvg = moTot[mo] / float(YRCNT[mo]) out2.write("%6.0f" % (moAvg,)) out2.write("%6.0f\n" % (yrAvg / float(YEARS),)) out3.write( """# Monthly Average Temperatures [F] (High + low)/2 YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN """ ) moTot = {} for mo in range(1, 13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out3.write("%4i" % (yr,)) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if ts >= constants._ARCHIVEENDTS: out3.write("%6s" % ("M",)) continue v = (float(db[ts]["avg_high"]) + float(db[ts]["avg_low"])) / 2.0 if ts < constants._ARCHIVEENDTS: moTot[mo] += v yrSum += v out3.write("%6.0f" % (v,)) if yr != constants._ARCHIVEENDTS.year: yrAvg += float(yrSum) / 12.0 out3.write("%6.0f\n" % (float(yrSum) / 12.0,)) else: out3.write(" M\n") out3.write("MEAN") for mo in range(1, 13): moAvg = moTot[mo] / float(YRCNT[mo]) out3.write("%6.0f" % (moAvg,)) out3.write("%6.0f\n" % (yrAvg / float(yrCnt),)) out4.write( """# Monthly Liquid Precip Totals [inches] (snow is melted) YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN\n""" ) moTot = {} for mo in range(1, 13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out4.write("%4i" % (yr,)) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if ts < constants._ARCHIVEENDTS: moTot[mo] += db[ts]["rain"] yrSum += db[ts]["rain"] out4.write(safePrint(db[ts]["rain"], 6, 2)) yrAvg += float(yrSum) out4.write("%6.2f\n" % (float(yrSum),)) out4.write("MEAN") for mo in range(1, 13): moAvg = moTot[mo] / float(YRCNT[mo]) out4.write("%6.2f" % (moAvg,)) out4.write("%6.2f\n" % (yrAvg / float(YEARS - 1),))
def write(cursor, out, station): out.write(("# THESE ARE THE HEAT STRESS VARIABLES FOR STATION # %s\n" ) % (station,)) s = constants.startts(station) e = constants._ENDTS interval = mx.DateTime.RelativeDateTime(months=+1) monthlyCount = {} monthlyIndex = {} now = s while now < e: monthlyCount[now] = 0 monthlyIndex[now] = 0 now += interval cursor.execute(""" SELECT year, month, high from %s WHERE station = '%s' and high > 86 and day >= '%s-01-01 '""" % (constants.get_table(station), station, constants.startyear(station))) for row in cursor: ts = mx.DateTime.DateTime(row["year"], row["month"], 1) monthlyCount[ts] += 1 monthlyIndex[ts] += int(row["high"]) - 86 monthlyAveCnt = {} monthlyAveIndex = {} for mo in range(5, 10): monthlyAveCnt[mo] = 0 monthlyAveIndex[mo] = 0 out.write(""" # OF DAYS MAXT >86 ACCUMULATED (MAXT - 86 ) YEAR MAY JUNE JULY AUG SEPT TOTAL MAY JUNE JULY AUG SEPT TOTAL\n""") yrCnt = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out.write("%5s" % (yr,)) totCnt = 0 for mo in range(5, 10): ts = mx.DateTime.DateTime(yr, mo, 1) if (ts >= constants._ARCHIVEENDTS): out.write("%6s" % ("M",)) continue totCnt += monthlyCount[ts] monthlyAveCnt[mo] += monthlyCount[ts] out.write("%6i" % (monthlyCount[ts], )) out.write("%6i " % (totCnt,)) totInd = 0 for mo in range(5, 10): ts = mx.DateTime.DateTime(yr, mo, 1) if (ts >= constants._ARCHIVEENDTS): out.write("%6s" % ("M",)) continue totInd += monthlyIndex[ts] monthlyAveIndex[mo] += monthlyIndex[ts] out.write("%6i" % (monthlyIndex[ts], )) out.write("%6i\n" % (totInd,)) out.write(" **************************************************************************************\n") out.write("MEANS") tot = 0 for mo in range(5, 10): val = float(monthlyAveCnt[mo]) / float(yrCnt) tot += val out.write("%6.1f" % (val, )) out.write("%6.1f " % (tot,)) tot = 0 for mo in range(5, 10): val = float(monthlyAveIndex[mo]) / float(yrCnt) tot += val out.write("%6.1f" % (val, )) out.write("%6.1f\n" % (tot, ))
def write(out, rs, station): s = constants.startts(station) e = constants._ENDTS YEARS = e.year - s.year + 1 out.write("""# SEASONAL TEMPERATURE CYCLES PER YEAR # 1 CYCLE IS A TEMPERATURE VARIATION FROM A VALUE BELOW A THRESHOLD # TO A VALUE EXCEEDING A THRESHOLD. THINK OF IT AS FREEZE/THAW CYCLES # FIRST DATA COLUMN WOULD BE FOR CYCLES EXCEEDING 26 AND 38 DEGREES F THRES 26-38 26-38 24-40 24-40 20-44 20-44 14-50 14-50 YEAR SPRING FALL SPRING FALL SPRING FALL SPRING FALL """) data = {} for yr in range(constants.startyear(station), constants._ENDYEAR): data[yr] = {'26s': 0, '26f': 0, '24s': 0, '24f': 0, '20s': 0, '20f': 0, '14s': 0, '14f': 0} prs = [[26, 38], [24, 40], [20, 44], [14, 50]] cycPos = {'26s': -1, '24s': -1, '20s': -1, '14s': -1} for i in range(len(rs)): ts = mx.DateTime.strptime(rs[i]["day"], "%Y-%m-%d") high = int(rs[i]['high']) low = int(rs[i]['low']) for pr in prs: l, u = pr key = '%ss' % (l, ) ckey = '%ss' % (l, ) if ts.month >= 7: ckey = '%sf' % (l, ) # cycles lower if cycPos[key] == 1 and low < l: # print 'Cycled lower', low, ts cycPos[key] = -1 data[ts.year][ckey] += 0.5 # cycled higher if cycPos[key] == -1 and high > u: # print 'Cycled higher', high, ts cycPos[key] = 1 data[ts.year][ckey] += 0.5 s26 = 0 f26 = 0 s24 = 0 f24 = 0 s20 = 0 f20 = 0 s14 = 0 f14 = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): s26 += data[yr]['26s'] f26 += data[yr]['26f'] s24 += data[yr]['24s'] f24 += data[yr]['24f'] s20 += data[yr]['20s'] f20 += data[yr]['20f'] s14 += data[yr]['14s'] f14 += data[yr]['14f'] out.write(("%s %-8i%-8i%-8i%-8i%-8i%-8i%-8i%-8i\n" "") % (yr, data[yr]['26s'], data[yr]['26f'], data[yr]['24s'], data[yr]['24f'], data[yr]['20s'], data[yr]['20f'], data[yr]['14s'], data[yr]['14f'])) out.write(("AVG %-8.1f%-8.1f%-8.1f%-8.1f%-8.1f%-8.1f%-8.1f%-8.1f\n" "") % (s26/YEARS, f26/YEARS, s24/YEARS, f24/YEARS, s20/YEARS, f20/YEARS, s14/YEARS, f14/YEARS))
def write(mydb, out, out2, out3, out4, station): s = constants.startts(station) e = constants._ENDTS YRCNT = constants.yrcnt(station) YEARS = e.year - s.year + 1 interval = mx.DateTime.RelativeDateTime(months=+1) now = s db = {} while (now < e): db[now] = {"avg_high": "M", "avg_low": "M", "rain": "M"} now += interval rs = mydb.query("SELECT * from r_monthly WHERE station = '%s'" % ( station,) ).dictresult() for i in range(len(rs)): ts = mx.DateTime.strptime(rs[i]["monthdate"], "%Y-%m-%d") if ts < constants._ARCHIVEENDTS: db[ts] = rs[i] out.write("""# Monthly Average High Temperatures [F] YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN """) moTot = {} for mo in range(1,13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out.write("%4i" % (yr,) ) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if (ts < constants._ARCHIVEENDTS): moTot[mo] += int(db[ts]["avg_high"]) yrSum += int(db[ts]["avg_high"]) out.write( safePrint( db[ts]["avg_high"], 6, 0) ) if yr != constants._ARCHIVEENDTS.year: yrAvg += float(yrSum) / 12.0 out.write("%6.0f\n" % ( float(yrSum) / 12.0, ) ) else: out.write(" \n") out.write("MEAN") for mo in range(1,13): moAvg = moTot[mo] / float( YRCNT[mo]) out.write("%6.0f" % (moAvg,) ) out.write("%6.0f\n" % (yrAvg / (float(YEARS)),) ) out2.write("""# Monthly Average Low Temperatures [F] YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN """) moTot = {} for mo in range(1,13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out2.write("%4i" % (yr,) ) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if (ts < constants._ARCHIVEENDTS): moTot[mo] += int(db[ts]["avg_low"]) yrSum += int(db[ts]["avg_low"]) out2.write( safePrint( db[ts]["avg_low"], 6, 0) ) if yr != constants._ARCHIVEENDTS.year: yrAvg += float(yrSum) / 12.0 out2.write("%6.0f\n" % ( float(yrSum) / 12.0, ) ) else: out2.write(" M\n") out2.write("MEAN") for mo in range(1,13): moAvg = moTot[mo] / float( YRCNT[mo]) out2.write("%6.0f" % (moAvg,) ) out2.write("%6.0f\n" % (yrAvg / float(YEARS),) ) out3.write("""# Monthly Average Temperatures [F] (High + low)/2 YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN """) moTot = {} for mo in range(1,13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out3.write("%4i" % (yr,) ) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if (ts >= constants._ARCHIVEENDTS): out.write("%6s" % ("M",)) continue v = (float(db[ts]["avg_high"]) + float(db[ts]["avg_low"])) / 2.0 if (ts < constants._ARCHIVEENDTS): moTot[mo] += v yrSum += v out3.write("%6.0f" % ( v, ) ) if yr != constants._ARCHIVEENDTS.year: yrAvg += float(yrSum) / 12.0 out3.write("%6.0f\n" % ( float(yrSum) / 12.0, ) ) else: out3.write(" M\n") out3.write("MEAN") for mo in range(1,13): moAvg = moTot[mo] / float( YRCNT[mo] ) out3.write("%6.0f" % (moAvg,) ) out3.write("%6.0f\n" % (yrAvg / float(yrCnt),) ) out4.write("""# Monthly Liquid Precip Totals [inches] (snow is melted) YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN\n""") moTot = {} for mo in range(1,13): moTot[mo] = 0 yrCnt = 0 yrAvg = 0 for yr in range(constants.startyear(station), constants._ENDYEAR): yrCnt += 1 out4.write("%4i" % (yr,) ) yrSum = 0 for mo in range(1, 13): ts = mx.DateTime.DateTime(yr, mo, 1) if (ts < constants._ARCHIVEENDTS): moTot[mo] += db[ts]["rain"] yrSum += db[ts]["rain"] out4.write( safePrint( db[ts]["rain"], 6, 2) ) yrAvg += float(yrSum) out4.write("%6.2f\n" % ( float(yrSum), ) ) out4.write("MEAN") for mo in range(1,13): moAvg = moTot[mo] / float( YRCNT[mo] ) out4.write("%6.2f" % (moAvg,) ) out4.write("%6.2f\n" % (yrAvg / float(YEARS -1) ,) )