Example #1
0
def main():
    """Go Main"""
    dbconn = get_dbconn('mesosite', user='******')
    cursor = dbconn.cursor()

    cursor.execute("""
        SELECT id, state, country, network from stations
        WHERE network ~* 'DCP' and length(id) = 5""")

    for row in cursor:
        nwsli = row[0]
        state = row[1]
        country = row[2]
        network = row[3]
        code = nwsli[-2:]

        country2 = nwsli2country.get(code)
        if country != country2:
            print(("ID:%s ST:%s C:%s NET:%s L_C:%s") %
                  (nwsli, state, country, network, country2))
        network2 = "%s_DCP" % (state, )
        if country in ["MX", "CA"]:
            network2 = "%s_%s_DCP" % (country, state)
        elif country not in ["MX", "CA", "US"]:
            network2 = "%s__DCP" % (country, )
        if network != network2:
            print(("ID:%s ST:%s C:%s NET:%s L_N:%s") %
                  (nwsli, state, country, network, network2))

        state2 = nwsli2state.get(code)
        if state is not None and state != state2:
            print(("ID:%s ST:%s C:%s NET:%s L_S:%s") %
                  (nwsli, state, country, network, state2))
Example #2
0
def main():
    """Go Main Go"""
    fh = open("insert.sql", "w")

    req = requests.get("http://www.weather.gov/source/aprfc/nrcs_swe.json")
    j = req.json()
    for feature in j["features"]:
        nwsli = feature["properties"]["lid"]
        name = feature["properties"]["name"]
        elev = distance(float(feature["properties"]["elev"]), "FT").value("M")
        lon, lat = feature["geometry"]["coordinates"]
        country = nwsli2country.get(nwsli[3:])
        state = nwsli2state.get(nwsli[3:])
        network = "%s_DCP" % (state, )

        sql = """INSERT into stations(id, name, network, country, state,
        plot_name, elevation, online, metasite, geom) VALUES ('%s', '%s', '%s',
        '%s', '%s', '%s', %s, 't', 'f', 'SRID=4326;POINT(%s %s)');
        """ % (
            nwsli,
            name,
            network,
            country,
            state,
            name,
            float(elev),
            float(lon),
            float(lat),
        )
        fh.write(sql)
    fh.close()
Example #3
0
def compute_network(nwsli):
    country = nwsli2country.get(nwsli[3:])
    state = nwsli2state.get(nwsli[3:])
    if country == "US" and state is not None:
        return "US", state, "%s_DCP" % (state,)
    if country != "US" and state is not None:
        return country, state, "%s_%s_DCP" % (country, state)
    print(("Failure to compute state for nwsli: %s [country:%s, state:%s]") % (nwsli, country, state))
    return None, None, None
Example #4
0
def compute_network(nwsli):
    country = nwsli2country.get(nwsli[3:])
    state = nwsli2state.get(nwsli[3:])
    if country == 'US' and state is not None:
        return "US", state, "%s_DCP" % (state,)
    if country != 'US' and state is not None:
        return country, state, "%s_%s_DCP" % (country, state)
    print(("Failure to compute state for nwsli: %s [country:%s, state:%s]"
           ) % (nwsli, country, state))
    return None, None, None
Example #5
0
def compute_network(nwsli):
    """Figure out the IEM network based on a NWSLI"""
    country = nwsli2country.get(nwsli[3:])
    state = nwsli2state.get(nwsli[3:])
    if country == "US" and state is not None:
        return "US", state, "%s_DCP" % (state, )
    if country != "US" and state is not None:
        return country, state, "%s_%s_DCP" % (country, state)
    print(("Failure to compute state for nwsli: %s [country:%s, state:%s]") %
          (nwsli, country, state))
    return None, None, None
Example #6
0
def main():
    """Go Main Go"""
    fh = open('insert.sql', 'w')

    req = requests.get('http://www.weather.gov/source/aprfc/nrcs_swe.json')
    j = req.json()
    for feature in j['features']:
        nwsli = feature['properties']['lid']
        name = feature['properties']['name']
        elev = distance(float(feature['properties']['elev']), 'FT').value('M')
        lon, lat = feature['geometry']['coordinates']
        country = nwsli2country.get(nwsli[3:])
        state = nwsli2state.get(nwsli[3:])
        network = "%s_DCP" % (state, )

        sql = """INSERT into stations(id, name, network, country, state,
        plot_name, elevation, online, metasite, geom) VALUES ('%s', '%s', '%s',
        '%s', '%s', '%s', %s, 't', 'f', 'SRID=4326;POINT(%s %s)');
        """ % (nwsli, name, network, country, state, name, float(elev),
               float(lon), float(lat))
        fh.write(sql)
    fh.close()
Example #7
0
def main():
    """Go Main Go"""
    hads_pgconn = get_dbconn('hads')
    mesosite_pgconn = get_dbconn('mesosite')
    mcursor = mesosite_pgconn.cursor()
    udf = read_sql("""
        select distinct nwsli from unknown where length(nwsli) = 5
        ORDER by nwsli
    """, hads_pgconn, index_col=None)
    udf['stcode'] = udf['nwsli'].str.slice(3, 5)
    for stcode, gdf in udf.groupby('stcode'):
        if stcode not in nwsli2country:
            continue
        country = nwsli2country[stcode]
        state = nwsli2state.get(stcode)
        hadscode = (country
                    if country != 'US'
                    else state)
        if country == 'US':
            network = "%s_DCP" % (state, )
        elif country == 'CA':
            hadscode = 'CN'
            network = "%s_%s_DCP" % (country, state)
        else:
            network = "%s__DCP" % (country, )
        print("Processing country:%s state:%s" % (country, state))
        uri = ("https://hads.ncep.noaa.gov/"
               "compressed_defs/%s_dcps_compressed.txt") % (hadscode, )
        req = requests.get(uri)
        if req.status_code != 200:
            print("uri: %s failed" % (uri, ))
            continue
        sio = StringIO()
        sio.write(req.content.decode('ascii', 'ignore'))
        sio.seek(0)
        hads = pd.read_csv(sio, sep='|', usecols=range(10), header=None)
        hads.columns = ["nesdis_id", "nwsli", "owner", "unsure",
                        "hsa", "lat", "lon", "dt", "int",
                        "location"]
        sio.close()
        if hads.empty or 'nwsli' not in hads.columns:
            continue
        for nwsli in gdf['nwsli']:
            df2 = hads[hads['nwsli'] == nwsli]
            if df2.empty:
                continue
            row = df2.iloc[0]
            tokens = row['lon'].split()
            lon = "%i.%04i" % (
                float(tokens[0]),
                (float(tokens[1]) + float(tokens[2]) / 60.) / 60. * 10000.
            )
            tokens = row['lat'].split()
            lat = "%i.%04i" % (
                float(tokens[0]),
                (float(tokens[1]) + float(tokens[2]) / 60.) / 60. * 10000.
            )
            state = state if country in ['US', 'CA'] else None
            print((" HADS adding NWSLI:%s network:%s country:%s state:%s"
                   ) % (nwsli, network, country, state))
            mcursor.execute("""
            SELECT online from stations where network = %s and id = %s
            """, (network, nwsli))
            if mcursor.rowcount == 1:
                dbrow = mcursor.fetchone()
                print((" %s already in database online:%s"
                       ) % (nwsli, dbrow[0]))
                continue
            mcursor.execute("""
            INSERT into stations(id, name, network, country, state,
            plot_name, elevation, online, metasite, geom)
            VALUES (%s, %s, %s, %s, %s, %s, %s, 't', 'f',
            'SRID=4326;POINT(%s %s)');
            """, (nwsli, row['location'].strip()[:64], network, country,
                  state, row['location'].strip()[:64], -999,
                  float(lon), float(lat)))
    mcursor.close()
    mesosite_pgconn.commit()
Example #8
0
cursor = dbconn.cursor()

cursor.execute("""
    SELECT id, state, country, network from stations
    WHERE network ~* 'DCP' and length(id) = 5""")

for row in cursor:
    nwsli = row[0]
    state = row[1]
    country = row[2]
    network = row[3]
    code = nwsli[-2:]

    country2 = nwsli2country.get(code)
    if country != country2:
        print "ID:%s ST:%s C:%s NET:%s L_C:%s" % (nwsli, state, country,
                                                  network, country2)
    network2 = "%s_DCP" % (state, )
    if country in ["MX", "CA"]:
        network2 = "%s_%s_DCP" % (country, state)
    elif country not in ["MX", "CA", "US"]:
        network2 = "%s__DCP" % (country, )
    if network != network2:
        print "ID:%s ST:%s C:%s NET:%s L_N:%s" % (nwsli, state, country,
                                                  network, network2)

    state2 = nwsli2state.get(code)
    if state is not None and state != state2:
        print "ID:%s ST:%s C:%s NET:%s L_S:%s" % (nwsli, state, country,
                                                  network, state2)
Example #9
0
import requests
from pyiem.reference import nwsli2country, nwsli2state
from pyiem.datatypes import distance

OUTPUT = open('insert.sql', 'w')

req = requests.get('http://www.weather.gov/source/aprfc/nrcs_swe.json')
j = req.json()
for feature in j['features']:
    nwsli = feature['properties']['lid']
    name = feature['properties']['name']
    elev = distance(float(feature['properties']['elev']), 'FT').value('M')
    lon, lat = feature['geometry']['coordinates']
    country = nwsli2country.get(nwsli[3:])
    state = nwsli2state.get(nwsli[3:])
    network = "%s_DCP" % (state, )

    sql = """INSERT into stations(id, name, network, country, state,
    plot_name, elevation, online, metasite, geom) VALUES ('%s', '%s', '%s',
    '%s', '%s', '%s', %s, 't', 'f', 'SRID=4326;POINT(%s %s)');
    """ % (nwsli, name, network, country, state, name, float(elev), float(lon),
           float(lat))
    OUTPUT.write(sql)
OUTPUT.close()
Example #10
0
import requests
from pyiem.reference import nwsli2country, nwsli2state
from pyiem.datatypes import distance

OUTPUT = open("insert.sql", "w")

req = requests.get("http://www.weather.gov/source/aprfc/nrcs_swe.json")
j = req.json()
for feature in j["features"]:
    nwsli = feature["properties"]["lid"]
    name = feature["properties"]["name"]
    elev = distance(float(feature["properties"]["elev"]), "FT").value("M")
    lon, lat = feature["geometry"]["coordinates"]
    country = nwsli2country.get(nwsli[3:])
    state = nwsli2state.get(nwsli[3:])
    network = "%s_DCP" % (state,)

    sql = """INSERT into stations(id, name, network, country, state,
    plot_name, elevation, online, metasite, geom) VALUES ('%s', '%s', '%s',
    '%s', '%s', '%s', %s, 't', 'f', 'SRID=4326;POINT(%s %s)');
    """ % (
        nwsli,
        name,
        network,
        country,
        state,
        name,
        float(elev),
        float(lon),
        float(lat),
    )
Example #11
0
cursor = dbconn.cursor()

cursor.execute("""
    SELECT id, state, country, network from stations
    WHERE network ~* 'DCP' and length(id) = 5""")

for row in cursor:
    nwsli = row[0]
    state = row[1]
    country = row[2]
    network = row[3]
    code = nwsli[-2:]

    country2 = nwsli2country.get(code)
    if country != country2:
        print "ID:%s ST:%s C:%s NET:%s L_C:%s" % (nwsli, state, country,
                                                  network, country2)
    network2 = "%s_DCP" % (state, )
    if country in ["MX", "CA"]:
        network2 = "%s_%s_DCP" % (country, state)
    elif country not in ["MX", "CA", "US"]:
        network2 = "%s__DCP" % (country,)
    if network != network2:
        print "ID:%s ST:%s C:%s NET:%s L_N:%s" % (nwsli, state, country,
                                                  network, network2)

    state2 = nwsli2state.get(code)
    if state is not None and state != state2:
        print "ID:%s ST:%s C:%s NET:%s L_S:%s" % (nwsli, state, country,
                                                  network, state2)