-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #591 from akrherz/231119
Omnibus
- Loading branch information
Showing
5 changed files
with
87 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Sync NWPS entries for flood stages! | ||
Called from windrose/daily_drive_network.py | ||
""" | ||
import sys | ||
|
||
import requests | ||
from pyiem.network import Table as NetworkTable | ||
from pyiem.util import get_dbconn, logger | ||
|
||
LOG = logger() | ||
|
||
|
||
def process_site(mcursor, nwsli, meta): | ||
"""Do our processing work""" | ||
|
||
url = f"https://preview-api.water.noaa.gov/v1/gauges/{nwsli}" | ||
|
||
try: | ||
res = requests.get(url, timeout=30).json() | ||
if res.get("code") == 5: | ||
LOG.info("No data found for %s", nwsli) | ||
return | ||
except Exception as exp: | ||
LOG.exception(exp) | ||
return | ||
|
||
msg = "" | ||
for name, entry in res.get("flood", {}).get("categories", {}).items(): | ||
key = f"sigstage_{name}" | ||
val = entry["stage"] | ||
if val < 0: | ||
continue | ||
# is this updated info? | ||
current = meta.get(key) | ||
if current is None or abs(current - val) > 0.01: | ||
msg += f"{name}: {current}->{val} " | ||
meta[key] = val | ||
if msg == "": | ||
return | ||
|
||
LOG.info("updating %s with %s", nwsli, msg) | ||
mcursor.execute( | ||
""" | ||
UPDATE stations SET sigstage_low = %s, | ||
sigstage_action = %s, | ||
sigstage_bankfull = %s, | ||
sigstage_flood = %s, | ||
sigstage_moderate = %s, | ||
sigstage_major = %s, | ||
sigstage_record = %s | ||
WHERE iemid = %s | ||
""", | ||
( | ||
meta.get("sigstage_low"), | ||
meta.get("sigstage_action"), | ||
meta.get("sigstage_bankfull"), | ||
meta.get("sigstage_flood"), | ||
meta.get("sigstage_moderate"), | ||
meta.get("sigstage_major"), | ||
meta.get("sigstage_record"), | ||
meta["iemid"], | ||
), | ||
) | ||
|
||
|
||
def main(argv): | ||
"""Go Main Go""" | ||
nt = NetworkTable(argv[1]) | ||
with get_dbconn("mesosite") as dbconn: | ||
for sid, meta in nt.sts.items(): | ||
mcursor = dbconn.cursor() | ||
process_site(mcursor, sid, meta) | ||
mcursor.close() | ||
dbconn.commit() | ||
|
||
|
||
if __name__ == "__main__": | ||
main(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters