Exemple #1
0
def write(mydb, out, rs, station, base):
    TODAY = mx.DateTime.now()
    out.write("""# LENGTH OF SEASON FOR STATION NUMBER  %s   BASE TEMP=%s
# LAST SPRING OCCURENCE FIRST FALL OCCURENCE 
   YEAR MONTH DAY DOY         MONTH DAY DOY   LENGTH OF SEASON
""" % (station, base) )

    s = mx.DateTime.DateTime(constants.startyear(station), 1, 1)
    e = constants._ENDTS
    interval = mx.DateTime.RelativeDateTime(years=+1)

    now = s
    d = {}
    while now <= e:
        ep = now + mx.DateTime.RelativeDateTime(month=12, day=31)
        sp = now + mx.DateTime.RelativeDateTime(month=1, day=1)
        d[int(now.year)] = {'sts': sp, 'ets': ep}
        now += interval

    for i in range(len(rs)):
        low = int(rs[i]["low"])
        if low > base:
            continue
        ts = mx.DateTime.strptime(rs[i]["day"], "%Y-%m-%d")
        mp = ts + mx.DateTime.RelativeDateTime(month=7, day=1)
        if (ts > mp): # Fall
            if (ts < d[ int(ts.year) ]['ets'] ): 
                d[int(ts.year)]['ets'] = ts
        else:
            if (ts > d[ int(ts.year) ]['sts'] ): 
                d[int(ts.year)]['sts'] = ts

    sjdaytot = 0
    ejdaytot = 0
    yrs = 0
    for yr in range(constants.startyear(station), constants._ENDYEAR):
        if (yr == constants._THISYEAR):
            continue
        if d[yr]['ets'] > TODAY:
            continue
        yrs += 1
        sjdaytot += int(d[yr]['sts'].strftime("%j"))
        ejdaytot += int(d[yr]['ets'].strftime("%j"))
        out.write("%7i%4i%6i%4i        %4i%6i%4i          %3s\n" % (
        yr, d[yr]['sts'].month, d[yr]['sts'].day, 
         int(d[yr]['sts'].strftime("%j")),
         d[yr]['ets'].month, d[yr]['ets'].day, 
         int(d[yr]['ets'].strftime("%j") ), 
         (d[yr]['ets'] - d[yr]['sts']).strftime("%d") ) )

    smean = sjdaytot / yrs
    emean = ejdaytot / yrs
    sts = mx.DateTime.DateTime(2001,1,1) + mx.DateTime.RelativeDateTime(days=smean)
    ets = mx.DateTime.DateTime(2001,1,1) + mx.DateTime.RelativeDateTime(days=emean)

    out.write("%7s%4i%6i%4i        %4i%6i%4i          %3s\n" % (
            "MEAN", sts.month, sts.day, smean, ets.month, ets.day, 
            emean, emean - smean) )
Exemple #2
0
def write(monthly_rows, 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)
    )

    db = {}
    db60 = {}
    for row in monthly_rows:
        mo = mx.DateTime.DateTime(row["year"], row["month"], 1)
        db[mo] = row["hdd65"]
        db60[mo] = row["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)
Exemple #3
0
def write(mydb, out, station):
    YRCNT = constants.yrcnt(station)
    out.write("""# THESE ARE THE MONTHLY COOLING 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]["cdd"]
        db60[mo] = rs[i]["cdd60"]

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

    second = """# THESE ARE THE MONTHLY COOLING 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)
Exemple #4
0
def write(cursor, out, station):
    out.write("""# Number of days exceeding given temperature thresholds
# -20, -10, 0, 32 are days with low temperature at or below value
# 50, 70, 80, 93, 100 are days with high temperature at or above value
""")
    out.write(("%s %4s %4s %4s %4s %4s %4s %4s %4s %4s\n"
               "") % ('YEAR', -20, -10, 0, 32, 50, 70, 80, 93, 100))

    cursor.execute("""SELECT year,
       sum(case when low <= -20 THEN 1 ELSE 0 END) as m20,
       sum(case when low <= -10 THEN 1 ELSE 0 END) as m10,
       sum(case when low <=  0 THEN 1 ELSE 0 END) as m0,
       sum(case when low <=  32 THEN 1 ELSE 0 END) as m32,
       sum(case when high >= 50 THEN 1 ELSE 0 END) as e50,
       sum(case when high >= 70 THEN 1 ELSE 0 END) as e70,
       sum(case when high >= 80 THEN 1 ELSE 0 END) as e80,
       sum(case when high >= 93 THEN 1 ELSE 0 END) as e93,
       sum(case when high >= 100 THEN 1 ELSE 0 END) as e100
       from %s WHERE station = '%s' and day >= '%s-01-01'
       GROUP by year ORDER by year ASC
    """ % (constants.get_table(station), station,
           constants.startyear(station)))

    for row in cursor:
        out.write(("%(year)4i %(m20)4i %(m10)4i %(m0)4i %(m32)4i %(e50)4i "
                   "%(e70)4i %(e80)4i %(e93)4i %(e100)4i\n") % row)
Exemple #5
0
def write(mydb, out, station):
    r = {}
    out.write("""# DAILY RECORD HIGHS AND LOWS OCCURRING DURING %s-%s FOR STATION NUMBER  %s
     JAN     FEB     MAR     APR     MAY     JUN     JUL     AUG     SEP     OCT     NOV     DEC
 DY  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN  MX  MN
""" % (constants.startyear(station), constants._ENDYEAR, station) )
  
    rs = mydb.query("SELECT * from %s WHERE station = '%s'" % (
            constants.climatetable(station), station) ).dictresult()
    for i in range(len(rs)):
        day = mx.DateTime.strptime(rs[i]["valid"], "%Y-%m-%d")
        r[day] = rs[i]

    for day in range(1,32):
        out.write("%3i" % (day,) )
        for mo in range(1,13):
            try:
                ts = mx.DateTime.DateTime(2000, mo, day)
                if not r.has_key(ts):
                    print ("Records missing for table: %s station: %s "
                           +"date: %s") % (constants.climatetable(station),
                                           station, ts.strftime("%b %d"))
                    out.write(" *** ***")
                    continue
            except:
                out.write(" *** ***")
                continue
            if (r[ts]['max_high'] is None or
                r[ts]['min_low'] is None):
                out.write(" *** ***")
                continue
            out.write("%4i%4i" % (r[ts]["max_high"], r[ts]["min_low"]) )
        out.write("\n")
Exemple #6
0
def process(id, csv):
  # Fetch Yearly Totals
    sql = """SELECT year, round(avg(high)::numeric,1) as avg_high, 
    round(avg(low)::numeric,1) as avg_low, 
    round(sum(precip)::numeric,2) as rain from %s 
    WHERE station = '%s' and year >= %s 
    GROUP by year ORDER by year ASC""" % (constants.get_table(id), 
                                        id, constants.startyear(id) )
    rs = constants.mydb.query(sql).dictresult()
    data = {}
    for i in range(len(rs)):
        year = int(rs[i]["year"])
        data[year] = {'oHigh': rs[i]["avg_high"], 'oLow': rs[i]["avg_low"], 
                  'oRain': rs[i]["rain"]}

    for i in range(1893, constants._ENDYEAR):
        if (not data.has_key(i)):
            data[i] = {'oHigh': "M", 'oLow': "M", 'oRain': "M"}
        csv.write("%s,%s,%s,"%(data[i]['oLow'],data[i]['oHigh'],data[i]['oRain']))

  # Need to do climate stuff
  # Then climate
    sql = """SELECT round(avg(high)::numeric,1) as avg_high,
    round(avg(low)::numeric,1) as avg_low, 
    round(sum(precip)::numeric,2) as rain from %s WHERE station = '%s' """ % (
        constants.climatetable(id), id)
    rs = constants.mydb.query(sql).dictresult()
    aHigh = rs[0]["avg_high"]
    aLow = rs[0]["avg_low"]
    aRain = rs[0]["rain"]
    csv.write("%s,%s,%s," % (aLow,aHigh,aRain) )

    csv.write("\n")
    csv.flush()
Exemple #7
0
def write(mydb, out, rs, station):
    """standard iterative here...."""
    startyear = constants.startyear(station)
    years = constants._ENDYEAR - startyear
    # 0.01, 0.5, 1, 2, 3, 4
    data = np.zeros((13, years+1, 6), 'i')
    for i in range(len(rs)):
        precip = float(rs[i]["precip"])
        if precip <= 0:
            continue
        offset = int(rs[i]['year']) - startyear
        data[0, offset, :] += np.where(precip >= CATS, 1, 0)
        data[int(rs[i]['month']), offset, :] += np.where(precip >= CATS, 1, 0)

    out.write("""# Number of days per year with precipitation at or above threshold [inch]
# Partitioned by month of the year, 'ANN' represents the entire year
""")

    for c in range(len(CATS)):
        out.write("""YEAR %4.2f JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN
""" % (CATS[c],))
        for yr in range(startyear, constants._ENDYEAR):
            out.write("%s %4.2f " % (yr, CATS[c]))
            for mo in range(1, 13):
                out.write("%3.0f " % (data[mo, yr-startyear, c], ))
            out.write("%3.0f\n" % (data[0, yr-startyear, c], ))
Exemple #8
0
def write(mydb, out, station):
    out.write("""# NUMBER OF DAYS WITH PRECIPITATION PER MONTH PER YEAR
# Days with a trace accumulation are not included
YEAR   JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN   
""")

    rs = mydb.query("SELECT * from r_monthly WHERE station = '%s'" % (
                            station,) ).dictresult()

    db = {}
    for i in range(len(rs)):
        ts = rs[i]["monthdate"]
        db[ts] = int(rs[i]["rain_days"])

    moCnt = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for yr in range(constants.startyear(station), constants._ENDYEAR):
        tot = 0
        out.write("%s   " % (yr,) )
        for mo in range(1, 13):
            cnt = db["%s-%02i-01" % (yr, mo)]
            ts = mx.DateTime.DateTime(yr,mo,1)
            if (ts >= constants._ARCHIVEENDTS):
                out.write("%3s " % ("M",) )
                continue
      
            out.write("%3i " % (cnt,))
            tot += cnt
        out.write("%3i\n" % (tot,))

    rs = mydb.query("""select extract(month from monthdate) as month, 
    stddev(rain_days), avg(rain_days) from r_monthly 
    WHERE station = '%s' GROUP by month ORDER by month ASC""" % (
                                station,) ).dictresult()

    db = {}
    for i in range(len(rs)):
        db[ int(rs[i]["month"]) ] = rs[i]
  
    rs = mydb.query("""SELECT avg(cnt), stddev(cnt) from ( 
       select extract(year from monthdate) as year, 
       sum(rain_days) as cnt from r_monthly 
       WHERE station = '%s' GROUP by year) as foo""" % (station,) ).dictresult()
    yrStd = rs[0]['stddev']
    yrAvg = rs[0]['avg']

    out.write("STDDEV ")
    for mo in range(1, 13):
        out.write("%3.0f " % (db[mo]['stddev'],))
    out.write("%3.0f\n" % (yrStd,) )
    out.write("AVG    ")
    for mo in range(1, 13):
        out.write("%3.0f " % (db[mo]['avg'],))
    out.write("%3.0f\n" % (yrAvg,))
Exemple #9
0
def write(mydb, out, station):
    out.write("""# Top 30 single day rainfalls
 MONTH  DAY  YEAR   AMOUNT
""")

    rs = mydb.query("""SELECT precip, day from %s WHERE station = '%s' 
   and day >= '%s-01-01' ORDER by precip DESC LIMIT 30""" % (
        constants.get_table(station), station, 
        constants.startyear(station) ) ).dictresult()

    for i in range(len(rs)):
        ts = mx.DateTime.strptime(rs[i]["day"], "%Y-%m-%d")
        out.write("%4i%7i%6i%9.2f\n" % (ts.month, ts.day, ts.year, rs[i]["precip"]) )
Exemple #10
0
def write(cursor, out, station):
    out.write("""# OF DAYS EACH YEAR WHERE MIN >=32 F\n""")

    cursor.execute(
        """
        SELECT year, count(low) from %s WHERE
        station = '%s' and low >= 32 and day >= '%s-01-01'
        and year < %s GROUP by year
    """
        % (constants.get_table(station), station, constants.startyear(station), constants._THISYEAR)
    )
    tot = 0
    d = {}
    for row in cursor:
        tot += row["count"]
        d[row["year"]] = row["count"]

    mean = tot / float(cursor.rowcount)

    for year in range(constants.startyear(station), constants._THISYEAR):
        out.write("%s %3i\n" % (year, d.get(year, 0)))

    out.write("MEAN %3i\n" % (mean,))
Exemple #11
0
def write(mydb, out, station):
    out.write("""# OF DAYS EACH YEAR WHERE MIN >=32 F\n""")

    rs = mydb.query("""SELECT year, count(low) from %s WHERE 
    station = '%s' and low >= 32 and day >= '%s-01-01' 
    and year < %s GROUP by year""" % (constants.get_table(station), 
                                      station, 
                                      constants.startyear(station), 
                                      constants._THISYEAR) ).dictresult()
    tot = 0
    d = {}
    for yr in range(constants.startyear(station), constants._THISYEAR):
        d[yr] = 0
    for i in range(len(rs)):
        tot += int(rs[i]["count"])
        d[ int(rs[i]["year"]) ] = int(rs[i]["count"])

    mean = tot / len(rs)

    for yr in range(constants.startyear(station), constants._THISYEAR):
        out.write("%s %3i\n" % (yr, d[yr]))


    out.write("MEAN %3i\n" % (mean,) )
def write(cursor, out, station):
    # Load up dict of dates..
    cnt = {}
    for day in range(30, 183):
        cnt[day] = {32: 0.0, 28: 0.0, 26: 0.0, 22: 0.0}
    cnt_years = {32: 0.0, 28: 0.0, 26: 0.0, 22: 0.0}

    for base in (32, 28, 26, 22):
        # Query Last doy for each year in archive
        sql = """
            select year, max(extract(doy from day)) as doy from %s
            WHERE month < 7 and low <= %s and low > -40 and station = '%s'
            and year >= %s and year < %s and month > 1
            GROUP by year ORDER by doy ASC
        """ % (constants.get_table(station),
               base, station, constants.startyear(station), constants._ENDYEAR)
        cursor.execute(sql)
        cnt_years[base] = cursor.rowcount
        if cursor.rowcount == 0:
            return
        for row in cursor:
            cnt[row['doy']][base] += 1.0

    sts = mx.DateTime.DateTime(2000, 1, 1)
    running = {32: 0.0, 28: 0.0, 26: 0.0, 22: 0.0}
    out.write("""# Low Temperature exceedence probabilities
# (On a certain date, what is the chance a temperature below a certain
# threshold will be observed again that spring)
 DOY Date    <33  <29  <27  <23
""")
    ar = []
    for day in range(181, 29, -1):
        ts = sts + mx.DateTime.RelativeDateTime(days=day-1)
        for base in (32, 28, 26, 22):
            running[base] += cnt[day][base]
        if day % 2 == 0:
            ar.append((" %3s %s  %3i  %3i  %3i  %3i"
                       "") % (ts.strftime("%-j"),
                              ts.strftime("%b %d"),
                              running[32] / cnt_years[32] * 100.0,
                              running[28] / cnt_years[28] * 100.0,
                              running[26] / cnt_years[26] * 100.0,
                              running[22] / cnt_years[22] * 100.0))

    ar.reverse()
    out.write("\n".join(ar))
Exemple #13
0
def main():
    """Go Main"""
    for station in nt.sts:
        sts = mx.DateTime.DateTime(constants.startyear(station), 1, 1)
        ets = constants._ENDTS

        # Check for obs total
        now = sts
        interval = mx.DateTime.RelativeDateTime(years=1)
        while now < (ets - interval):
            days = int(((now + interval) - now).days)
            ccursor.execute("""SELECT count(*) from alldata_%s WHERE
            year = %s and station = '%s'""" % (state, now.year, station))
            row = ccursor.fetchone()
            if row[0] != days:
                print('Mismatch station: %s year: %s count: %s days: %s'
                      '') % (station, now.year, row[0], days)
                fix_year(station, now.year)
            now += interval

        # Check records database...
        sts = mx.DateTime.DateTime(2000, 1, 1)
        ets = mx.DateTime.DateTime(2001, 1, 1)
        interval = mx.DateTime.RelativeDateTime(days=1)
        for table in ['climate', 'climate51', 'climate71', 'climate81']:
            ccursor.execute("""SELECT count(*) from %s WHERE
                station = '%s'""" % (table, station))
            row = ccursor.fetchone()
            if row[0] == 366:
                continue
            now = sts
            while now < ets:
                ccursor.execute("""SELECT * from %s WHERE station = '%s'
                    and valid = '%s'
                    """ % (table, station, now.strftime("%Y-%m-%d")))
                if ccursor.rowcount == 0:
                    print(("Add %s station: %s day: %s") %
                          (table, station, now.strftime("%Y-%m-%d")))
                    ccursor.execute("""
                    INSERT into %s (station, valid) values ('%s', '%s')
                    """ % (table, station, now.strftime("%Y-%m-%d")))
                now += interval

    ccursor.close()
    COOP.commit()
Exemple #14
0
def process(sid, csv):
    # Fetch Yearly Totals
    cursor.execute("""
        SELECT year, round(avg(high)::numeric,1) as avg_high,
        round(avg(low)::numeric,1) as avg_low,
        round(sum(precip)::numeric,2) as rain from %s
        WHERE station = '%s' and year >= %s GROUP by year ORDER by year ASC
    """ % (constants.get_table(sid), sid, constants.startyear(sid)))

    data = {}
    for row in cursor:
        year = row["year"]
        data[year] = {
            'oHigh': row["avg_high"],
            'oLow': row["avg_low"],
            'oRain': row["rain"]
        }

    for i in range(1893, constants._ENDYEAR):
        if i not in data:
            data[i] = {'oHigh': "M", 'oLow': "M", 'oRain': "M"}
        csv.write("%s,%s,%s," %
                  (data[i]['oLow'], data[i]['oHigh'], data[i]['oRain']))

    # Need to do climate stuff
    # Then climate
    cursor.execute("""
        SELECT round(avg(high)::numeric,1) as avg_high,
        round(avg(low)::numeric,1) as avg_low,
        round(sum(precip)::numeric,2) as rain from %s WHERE station = '%s'
    """ % (constants.climatetable(sid), sid))
    row = cursor.fetchone()
    aHigh = row["avg_high"]
    aLow = row["avg_low"]
    aRain = row["rain"]
    csv.write("%s,%s,%s," % (aLow, aHigh, aRain))

    csv.write("\n")
    csv.flush()
Exemple #15
0
def write(monthly_rows, out, station):
    out.write("""# NUMBER OF SNOWY DAYS PER MONTH PER YEAR
# Days with a trace of snowfall reported are not included
YEAR   JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN
""")

    db = {}
    monthly = [0]*13
    for i in range(13):
        monthly[i] = []

    for row in monthly_rows:
        ts = datetime.datetime(row['year'], row['month'], 1)
        db[ts] = row["snow_days"]
        monthly[ts.month].append(row['snow_days'])

    for yr in range(constants.startyear(station), constants._ENDYEAR):
        tot = 0
        out.write("%s   " % (yr,))
        for mo in range(1, 13):
            ts = datetime.datetime(yr, mo, 1)
            cnt = db.get(ts, None)
            if ts >= constants._ARCHIVEENDTS or cnt is None:
                out.write("%3s " % ("M",))
                continue

            out.write("%3i " % (cnt,))
            tot += cnt
        out.write("%3i\n" % (tot,))

    out.write("STDDEV ")
    for mo in range(1, 13):
        out.write("%3.0f " % (np.std(monthly[mo]),))
    out.write("%3.0f\n" % (0,))
    out.write("AVG    ")
    for mo in range(1, 13):
        out.write("%3.0f " % (np.average(monthly[mo]),))
    out.write("%3.0f\n" % (0,))
Exemple #16
0
def write(monthly_rows, out, station):
    out.write("""# NUMBER OF DAYS WITH PRECIPITATION PER MONTH PER YEAR
# Days with a trace accumulation are not included
YEAR   JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ANN   
""")

    db = {}
    monthly = [0]*13
    for i in range(13):
        monthly[i] = []
        
    for row in monthly_rows:
        ts = mx.DateTime.DateTime(row['year'], row['month'], 1)
        db[ts] = row["rain_days"]
        monthly[ts.month].append( row['rain_days'] )

    for yr in range(constants.startyear(station), constants._ENDYEAR):
        tot = 0
        out.write("%s   " % (yr,) )
        for mo in range(1, 13):
            cnt = db.get("%s-%02i-01" % (yr, mo), None)
            ts = mx.DateTime.DateTime(yr,mo,1)
            if ts >= constants._ARCHIVEENDTS or cnt is None:
                out.write("%3s " % ("M",) )
                continue
      
            out.write("%3i " % (cnt,))
            tot += cnt
        out.write("%3i\n" % (tot,))

    out.write("STDDEV ")
    for mo in range(1, 13):
        out.write("%3.0f " % (np.std(monthly[mo]),))
    out.write("%3.0f\n" % (0,) )
    out.write("AVG    ")
    for mo in range(1, 13):
        out.write("%3.0f " % (np.average(monthly[mo]),))
    out.write("%3.0f\n" % (0,))
Exemple #17
0
def process(sid, csv):
    # Fetch Yearly Totals
    cursor.execute("""
        SELECT year, round(avg(high)::numeric,1) as avg_high,
        round(avg(low)::numeric,1) as avg_low,
        round(sum(precip)::numeric,2) as rain from %s
        WHERE station = '%s' and year >= %s GROUP by year ORDER by year ASC
    """ % (constants.get_table(sid), sid, constants.startyear(sid)))

    data = {}
    for row in cursor:
        year = row["year"]
        data[year] = {'oHigh': row["avg_high"], 'oLow': row["avg_low"],
                      'oRain': row["rain"]}

    for i in range(1893, constants._ENDYEAR):
        if i not in data:
            data[i] = {'oHigh': "M", 'oLow': "M", 'oRain': "M"}
        csv.write("%s,%s,%s," % (data[i]['oLow'], data[i]['oHigh'],
                                 data[i]['oRain']))

    # Need to do climate stuff
    # Then climate
    cursor.execute("""
        SELECT round(avg(high)::numeric,1) as avg_high,
        round(avg(low)::numeric,1) as avg_low,
        round(sum(precip)::numeric,2) as rain from %s WHERE station = '%s'
    """ % (constants.climatetable(sid), sid))
    row = cursor.fetchone()
    aHigh = row["avg_high"]
    aLow = row["avg_low"]
    aRain = row["rain"]
    csv.write("%s,%s,%s," % (aLow, aHigh, aRain))

    csv.write("\n")
    csv.flush()
Exemple #18
0
# This will drive the modules

import pg, string, constants
import network
nt = network.Table("IACLIMATE")
mydb = pg.connect("coop", 'iemdb',user='******')

import genPrecipEvents,  genGDD, genDailyRecords
import genDailyRecordsRain, genDailyRange, genDailyMeans, genCountLows32
import genSpringFall, genMonthly, genHDD, genCDD, genHeatStress
import genCountRain, genFrostProbabilities, genSpringProbabilities, genCycles
import genTempThresholds, genRecordPeriods, gen_precip_cats

updateAll = True
#for id in st.ids:
for id in ['IA0200',]:
    print "processing [%s] %s" % (id, nt.sts[id]["name"])
    dbid = string.upper(id)
    rs = mydb.query("""SELECT * from %s WHERE station = '%s' and 
    day >= '%s-01-01' ORDER by day ASC""" % (
    constants.get_table(dbid), dbid, constants.startyear(dbid) ) ).dictresult()
    #genSpringFall.write(mydb, rs, dbid, 32, "09")
    genHDD.go(mydb, rs, dbid, updateAll)
    genHDD.write(mydb, dbid)
Exemple #19
0
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),))
Exemple #20
0
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, ))
Exemple #21
0
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))
Exemple #22
0
def go(mydb, rs, stationID):
  db = {}
  for i in range(1,54): # Store climateweek values
    db[i] = {}
    db[i]["maxval"] = 0
    db[i]["maxyr"] = 0
    db[i]["total"] = 0
    db[i]["events"] = 0
    db[i]["cat1"] = 0
    db[i]["cat2"] = 0
    db[i]["cat3"] = 0
    db[i]["cat4"] = 0
    db[i]["cat5"] = 0
    db[i]["totprec"] = [0]* (constants._ENDYEAR - constants.startyear(stationID))


  for i in range(len(rs)):
    climoweek = int(rs[i]["climoweek"])
    precip = float(rs[i]["precip"])
    yr = int(rs[i]['year'])
     # First we do the CATS
    if (precip >= 0.01 and precip <= 0.25):
      db[climoweek]["cat1"] += 1
    elif (precip >= 0.26 and precip <= 0.50):
      db[climoweek]["cat2"] += 1
    elif (precip >= 0.51 and precip <= 1.00):
      db[climoweek]["cat3"] += 1
    elif (precip >= 1.01 and precip <= 2.00):
      db[climoweek]["cat4"] += 1
    elif (precip >= 2.01):
      db[climoweek]["cat5"] += 1
     # Work the Max
    if (precip > 0):
      db[climoweek]["totprec"][yr - constants.startyear(stationID)] += precip
     # Add in for the total
    db[climoweek]["total"] += precip

  annEvents = 0
  maxVal = 0
  totRain = 0
  cat1 = 0
  cat2 = 0
  cat3 = 0
  cat4 = 0
  cat5 = 0
  for i in range(1,54):
    totEvents = db[i]["cat1"] + db[i]["cat2"] + db[i]["cat3"] + \
                db[i]["cat4"] + db[i]["cat5"]
    meanRain = db[i]["total"] / totEvents
    annEvents += totEvents
    maxVal = max( db[i]['totprec'] )
    maxyr = db[i]['totprec'].index(maxVal) + constants.startyear(stationID)
    cat1 += db[i]["cat1"]
    cat2 += db[i]["cat2"]
    cat3 += db[i]["cat3"]
    cat4 += db[i]["cat4"]
    cat5 += db[i]["cat5"]
    totRain += db[i]["total"]

    mydb.query("""DELETE from r_precipevents WHERE station = '%s' and 
     climoweek = %s""" % (stationID, i) )

    mydb.query("""INSERT into r_precipevents(station, climoweek, maxval, maxyr, 
     meanval, cat1e, cat2e, cat3e, cat4e, cat5e) values ('%s', %s, %s, %s, 
     %4.2f, %s, %s, %s, %s, %s)""" % (stationID, i, maxVal, maxyr, meanRain,
     db[i]["cat1"], db[i]["cat2"], db[i]["cat3"], db[i]["cat4"], db[i]["cat5"]))
Exemple #23
0
def write(mydb, out, rs, station):
    out.write("""# First occurance of record consecutive number of days 
# above or below a temperature threshold
""")
    out.write("#   %-27s %-27s  %-27s %-27s\n" % (" Low Cooler Than", 
     " Low Warmer Than", " High Cooler Than", " High Warmer Than") )
    out.write("%3s %5s %10s %10s %5s %10s %10s  %5s %10s %10s %5s %10s %10s\n" % (
      'TMP', 'DAYS', 'BEGIN DATE', 'END DATE',
      'DAYS', 'BEGIN DATE', 'END DATE',
      'DAYS', 'BEGIN DATE', 'END DATE',
      'DAYS', 'BEGIN DATE', 'END DATE') )

    highs = np.zeros( (len(rs),), 'f')
    lows = np.zeros( (len(rs),), 'f')
    for i in range(len(rs)):
        highs[i] = rs[i]['high']
        lows[i] = rs[i]['low']

    for thres in range(-20,101,2):
        condition = lows < thres
        max_bl = 0
        max_bl_ts = mx.DateTime.now()
        for start, stop in contiguous_regions(condition):
            if (stop - start) > max_bl:
                max_bl = int(stop - start)
                max_bl_ts = mx.DateTime.DateTime(constants.startyear(station),
                            1, 1) + mx.DateTime.RelativeDateTime(
                                                            days=int(stop))
        condition = lows >= thres
        max_al = 0
        max_al_ts = mx.DateTime.now()
        for start, stop in contiguous_regions(condition):
            if (stop - start) > max_al:
                max_al = int(stop - start)
                max_al_ts = mx.DateTime.DateTime(constants.startyear(station),
                            1, 1) + mx.DateTime.RelativeDateTime(
                                                            days=int(stop))
        condition = highs < thres
        max_bh = 0
        max_bh_ts = mx.DateTime.now()
        for start, stop in contiguous_regions(condition):
            if (stop - start) > max_bh:
                max_bh = int(stop - start)
                max_bh_ts = mx.DateTime.DateTime(constants.startyear(station),
                            1, 1) + mx.DateTime.RelativeDateTime(
                                                            days=int(stop))
        condition = highs >= thres
        max_ah = 0
        max_ah_ts = mx.DateTime.now()
        for start, stop in contiguous_regions(condition):
            if (stop - start) > max_ah:
                max_ah = int(stop - start)
                max_ah_ts = mx.DateTime.DateTime(constants.startyear(station),
                            1, 1) + mx.DateTime.RelativeDateTime(
                                                            days=int(stop))

        out.write(("%3i %5s %10s %10s %5s %10s %10s  "
                      +"%5s %10s %10s %5s %10s %10s\n") % (
    thres, 
    wrap(max_bl),
    wrap(max_bl, (max_bl_ts -
            mx.DateTime.RelativeDateTime(days=max_bl)).strftime("%m/%d/%Y")), 
    wrap(max_bl, max_bl_ts.strftime("%m/%d/%Y") ),

    wrap(max_al),
    wrap(max_al, (max_al_ts -
            mx.DateTime.RelativeDateTime(days=max_al)).strftime("%m/%d/%Y")), 
    wrap(max_al, max_al_ts.strftime("%m/%d/%Y") ),

    wrap(max_bh),
    wrap(max_bh, (max_bh_ts -
            mx.DateTime.RelativeDateTime(days=max_bh)).strftime("%m/%d/%Y")), 
    wrap(max_bh, max_bh_ts.strftime("%m/%d/%Y") ),

    wrap(max_ah),
    wrap(max_ah, (max_ah_ts -
            mx.DateTime.RelativeDateTime(days=max_ah)).strftime("%m/%d/%Y")), 
    wrap(max_ah, max_ah_ts.strftime("%m/%d/%Y") )
  ) )
Exemple #24
0
def run_station(dbid):
    """Actually run for the given station"""
    table = constants.get_table(dbid)
    # print "processing [%s] %s" % (dbid, constants.nt.sts[dbid]["name"])
    sql = """
        SELECT d.*, c.climoweek from %s d, climoweek c
        WHERE station = '%s' and day >= '%s-01-01' and d.sday = c.sday
        and precip is not null
        ORDER by day ASC
        """ % (table, dbid, constants.startyear(dbid))
    rs = caller(mydb.query, sql).dictresult()

    # Compute monthly
    cursor.execute("""
    SELECT year, month, sum(precip) as sum_precip,
    avg(high) as avg_high,
    avg(low) as avg_low,
    sum(cdd(high,low,60)) as cdd60,
    sum(cdd(high,low,65)) as cdd65,
    sum(hdd(high,low,60)) as hdd60,
    sum(hdd(high,low,65)) as hdd65,
    sum(case when precip >= 0.01 then 1 else 0 end) as rain_days,
    sum(case when snow >= 0.1 then 1 else 0 end) as snow_days,
    sum(gddxx(40,86,high,low)) as gdd40,
    sum(gddxx(48,86,high,low)) as gdd48,
    sum(gddxx(50,86,high,low)) as gdd50,
    sum(gddxx(52,86,high,low)) as gdd52
     from """+table+""" WHERE station = %s GROUP by year, month
    """, (dbid, ))
    monthly_rows = cursor.fetchall()

    out = constants.make_output(constants.nt, dbid, "01")
    caller(genPrecipEvents.write, cursor, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "02")
    caller(gen30rains.write, mydb, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "03")
    caller(genGDD.write, monthly_rows, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "04")
    caller(genDailyRecords.write, mydb, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "05")
    caller(genDailyRecordsRain.write, mydb, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "06")
    caller(genDailyRange.write, mydb, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "07")
    caller(genDailyMeans.write, mydb, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "08")
    caller(genCountLows32.write, cursor, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "09")
    caller(genSpringFall.write, out, rs, dbid, 32)
    out.close()

    out = constants.make_output(constants.nt, dbid, "10")
    caller(genSpringFall.write, out, rs, dbid, 30)
    out.close()

    out = constants.make_output(constants.nt, dbid, "11")
    caller(genSpringFall.write, out, rs, dbid, 28)
    out.close()

    out = constants.make_output(constants.nt, dbid, "12")
    caller(genSpringFall.write, out, rs, dbid, 26)
    out.close()

    out = constants.make_output(constants.nt, dbid, "13")
    caller(genSpringFall.write, out, rs, dbid, 24)
    out.close()

    out = constants.make_output(constants.nt, dbid, "14")
    out2 = constants.make_output(constants.nt, dbid, "15")
    out3 = constants.make_output(constants.nt, dbid, "16")
    out4 = constants.make_output(constants.nt, dbid, "17")
    caller(genMonthly.write, monthly_rows, out, out2, out3, out4, dbid)
    out.close()
    out2.close()
    out3.close()
    out4.close()

    out = constants.make_output(constants.nt, dbid, "18")
    caller(genHDD.write, monthly_rows, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "19")
    caller(genCDD.write, monthly_rows, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "20")
    caller(genHeatStress.write, cursor, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "21")
    caller(genCountRain.write, monthly_rows, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "25")
    caller(genCountSnow.write, monthly_rows, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "22")
    caller(genFrostProbabilities.write, mydb, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "23")
    caller(genSpringProbabilities.write, cursor, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "24")
    caller(genCycles.write, out, rs, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "26")
    caller(genTempThresholds.write, cursor, out, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "27")
    caller(genRecordPeriods.write, mydb, out, rs, dbid)
    out.close()

    out = constants.make_output(constants.nt, dbid, "28")
    caller(gen_precip_cats.write, mydb, out, rs, dbid)
    out.close()
Exemple #25
0
    while now < ets:
        ccursor.execute("""SELECT count(*) from alldata_%s where
        station = '%s' and day = '%s' """ %
                        (state, station, now.strftime("%Y-%m-%d")))
        row = ccursor.fetchone()
        if row[0] == 0:
            print 'Adding Date: %s station: %s' % (now, station)
            ccursor.execute("""INSERT into alldata_%s (station, day, sday,
            year, month) VALUES ('%s', '%s', '%s', %s, %s)
            """ % (state, station, now.strftime("%Y-%m-%d"),
                   now.strftime("%m%d"), now.year, now.month))
        now += interval


for station in nt.sts.keys():
    sts = mx.DateTime.DateTime(constants.startyear(station), 1, 1)
    ets = constants._ENDTS

    # Check for obs total
    now = sts
    interval = mx.DateTime.RelativeDateTime(years=1)
    while now < (ets - interval):
        days = int(((now + interval) - now).days)
        ccursor.execute("""SELECT count(*) from alldata_%s WHERE
        year = %s and station = '%s'""" % (state, now.year, station))
        row = ccursor.fetchone()
        if row[0] != days:
            print('Mismatch station: %s year: %s count: %s days: %s'
                  '') % (station, now.year, row[0], days)
            fix_year(station, now.year)
        now += interval
Exemple #26
0
import datetime

def caller(func, *args):
    #start = datetime.datetime.now()
    ret = func(*args)
    #end = datetime.datetime.now()
    #print "%s %s took %s" % (func.__name__, args[-1], (end-start))
    return ret

updateAll= False
for dbid in nt.sts.keys():
    #print "processing [%s] %s" % (dbid, nt.sts[dbid]["name"])
    sql = """SELECT d.*, c.climoweek from %s d, climoweek c 
    WHERE station = '%s' and day >= '%s-01-01' and d.sday = c.sday 
    ORDER by day ASC""" % (constants.get_table(dbid),
                dbid, constants.startyear(dbid) ) 

    rs = caller(mydb.query, sql).dictresult()

    caller(genPrecipEvents.go, mydb, rs, dbid)
    out = caller(constants.make_output, nt, dbid, "01")
    caller(genPrecipEvents.write, mydb, out, dbid)
    out.close()

    out = caller(constants.make_output, nt, dbid, "02")
    caller(gen30rains.write, mydb, out, dbid)
    out.close()
    
    caller(genGDD.go, mydb, rs, dbid, updateAll)
    out = caller(constants.make_output, nt, dbid, "03")
    caller(genGDD.write, mydb, out, dbid)
Exemple #27
0
    now = sts
    while now < ets:
        ccursor.execute("""SELECT count(*) from alldata_%s where
        station = '%s' and day = '%s' """ % (state, station,
                                             now.strftime("%Y-%m-%d")))
        row = ccursor.fetchone()
        if row[0] == 0:
            print 'Adding Date: %s station: %s' % (now, station)
            ccursor.execute("""INSERT into alldata_%s (station, day, sday,
            year, month) VALUES ('%s', '%s', '%s', %s, %s)
            """ % (state, station, now.strftime("%Y-%m-%d"),
                   now.strftime("%m%d"), now.year, now.month))
        now += interval

for station in nt.sts.keys():
    sts = mx.DateTime.DateTime(constants.startyear(station), 1, 1)
    ets = constants._ENDTS

    # Check for obs total
    now = sts
    interval = mx.DateTime.RelativeDateTime(years=1)
    while now < (ets - interval):
        days = int(((now + interval) - now).days)
        ccursor.execute("""SELECT count(*) from alldata_%s WHERE
        year = %s and station = '%s'""" % (state, now.year, station))
        row = ccursor.fetchone()
        if row[0] != days:
            print ('Mismatch station: %s year: %s count: %s days: %s'
                   '') % (station, now.year, row[0], days)
            fix_year(station, now.year)
        now += interval
Exemple #28
0
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) ,) )