Skip to content

Commit

Permalink
sundry python code linting
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Jan 30, 2021
1 parent b711966 commit a2566d6
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 116 deletions.
10 changes: 5 additions & 5 deletions scripts/RT/env2csv.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Dump a days worth of output to file."""
from __future__ import print_function
# stdlib
import os
import datetime
from multiprocessing import Pool
import sys

# third party
import numpy as np
import pandas as pd
from tqdm import tqdm
Expand Down Expand Up @@ -140,10 +142,8 @@ def load_lengths(scenario):
icursor = idep.cursor()
res = {}
icursor.execute(
"""
SELECT huc_12, fpath, ST_Length(geom) from flowpaths where
scenario = %s
""",
"SELECT huc_12, fpath, ST_Length(geom) from flowpaths where "
"scenario = %s",
(int(sdf.loc[scenario, "flowpath_scenario"]),),
)
for row in icursor:
Expand Down
9 changes: 4 additions & 5 deletions scripts/RT/harvest2database.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Suck in the yield data!"""
from __future__ import print_function
# stdlib
import sys
import datetime
import os
import multiprocessing
from io import StringIO

# third party
import pandas as pd
from tqdm import tqdm
from pyiem.dep import read_yld
Expand Down Expand Up @@ -79,10 +80,8 @@ def main(argv):
pgconn = get_dbconn("idep")
cursor = pgconn.cursor()
cursor.execute(
"""
DELETE from harvest WHERE scenario = %s
and valid >= %s and valid <= %s
""",
"DELETE from harvest WHERE scenario = %s "
"and valid >= %s and valid <= %s",
(scenario, datetime.date(year, 1, 1), datetime.date(year, 12, 31)),
)
print(" + Deleted %s rows" % (cursor.rowcount,))
Expand Down
1 change: 0 additions & 1 deletion scripts/RT/harvest_wb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Collect the water balance for a given today"""
from __future__ import print_function
import os
import datetime
import multiprocessing
Expand Down
19 changes: 9 additions & 10 deletions scripts/cligen/daily_clifile_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
from scipy.interpolate import NearestNDInterpolator
from PIL import Image
from pyiem import iemre
from pyiem.datatypes import temperature
from pyiem.dep import SOUTH, WEST, NORTH, EAST, get_cli_fname
from pyiem.util import ncopen, logger
from pyiem.util import ncopen, logger, convert_value

LOG = logger()
XTILE = int(sys.argv[1])
Expand Down Expand Up @@ -108,24 +107,24 @@ def load_iemre():
)
SOLAR[:] = iemre_bounds_check("rsds", nn(xi, yi), 0, 1000)

data = temperature(nc.variables["high_tmpk"][offset, :, :], "K").value(
"C"
data = convert_value(
nc.variables["high_tmpk"][offset, :, :], "degK", "degC"
)
nn = NearestNDInterpolator(
(np.ravel(lons), np.ravel(lats)), np.ravel(data)
)
HIGH_TEMP[:] = iemre_bounds_check("high_tmpk", nn(xi, yi), -60, 60)

data = temperature(nc.variables["low_tmpk"][offset, :, :], "K").value(
"C"
data = convert_value(
nc.variables["low_tmpk"][offset, :, :], "degK", "degC"
)
nn = NearestNDInterpolator(
(np.ravel(lons), np.ravel(lats)), np.ravel(data)
)
LOW_TEMP[:] = iemre_bounds_check("low_tmpk", nn(xi, yi), -60, 60)

data = temperature(nc.variables["avg_dwpk"][offset, :, :], "K").value(
"C"
data = convert_value(
nc.variables["avg_dwpk"][offset, :, :], "degK", "degC"
)
nn = NearestNDInterpolator(
(np.ravel(lons), np.ravel(lats)), np.ravel(data)
Expand All @@ -141,7 +140,7 @@ def load_iemre():


def load_stage4():
""" It sucks, but we need to load the stage IV data to give us something
"""It sucks, but we need to load the stage IV data to give us something
to benchmark the MRMS data against, to account for two things:
1) Wind Farms
2) Over-estimates
Expand Down Expand Up @@ -402,7 +401,7 @@ def bpstr(ts, accum):


def compute_breakpoint(ar, accumThreshold=2.0, intensityThreshold=1.0):
""" Compute the breakpoint data based on this array of data!
"""Compute the breakpoint data based on this array of data!
To prevent massive ASCII text files, we do some simplification to the
precipitation dataset. We want to retain the significant rates though.
Expand Down
19 changes: 9 additions & 10 deletions scripts/cligen/qc_summarize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Need something that prints diagnostics of our climate file"""
from __future__ import print_function
import sys
import datetime

Expand All @@ -8,9 +7,9 @@
import pytz
import pandas as pd
import requests
from pyiem.datatypes import distance, temperature
from pyiem.dep import read_cli
from pyiem.iemre import hourly_offset
from pyiem.util import c2f, mm2inch


def compute_stage4(lon, lat, year):
Expand All @@ -24,7 +23,7 @@ def compute_stage4(lon, lat, year):
("Computed stage4 nclon:%.2f nclat:%.2f yidx:%s xidx:%s ")
% (lons[yidx, xidx], lats[yidx, xidx], yidx, xidx)
)
p01i = distance(nc.variables["p01m"][:, yidx, xidx], "MM").value("IN")
p01i = mm2inch(nc.variables["p01m"][:, yidx, xidx])
nc.close()
df = pd.DataFrame(
{"precip": 0.0},
Expand Down Expand Up @@ -72,9 +71,9 @@ def do_qc(fn, df, year):
("%s | %6.2f | %7.2f | %7.2f | %6i | %6i | %6.0f")
% (
_year,
distance(gdf["pcpn"].sum(), "MM").value("IN"),
distance(gdf["maxr"].max(), "MM").value("IN"),
distance(gdf["pcpn"].max(), "MM").value("IN"),
mm2inch(gdf["pcpn"].sum()),
mm2inch(gdf["maxr"].max()),
mm2inch(gdf["pcpn"].max()),
len(gdf[gdf["pcpn"] > 0].index),
len(gdf[gdf["maxr"] > 25.4].index),
gdf["rad"].mean(),
Expand All @@ -93,15 +92,15 @@ def do_qc(fn, df, year):
("%s | %6.2f | %6.2f | %3i")
% (
_year,
temperature(gdf["tmax"].mean(), "C").value("F"),
temperature(gdf["tmin"].mean(), "C").value("F"),
c2f(gdf["tmax"].mean()),
c2f(gdf["tmin"].mean()),
len(gdf[gdf["tmax"] > 37.7].index),
)
)

monthly = df[df.index.year == year]["pcpn"].resample("M").sum().copy()
monthly = pd.DataFrame(
{"dep": distance(monthly.values, "MM").value("IN")}, index=range(1, 13)
{"dep": mm2inch(monthly.values)}, index=range(1, 13)
)

# Get prism, for a bulk comparison
Expand Down Expand Up @@ -222,7 +221,7 @@ def main(argv):
fn = argv[1]
year = int(argv[2])
df = read_cli(fn)
df["pcpn_in"] = distance(df["pcpn"].values, "MM").value("IN")
df["pcpn_in"] = mm2inch(df["pcpn"].values)
do_qc(fn, df, year)


Expand Down
1 change: 0 additions & 1 deletion scripts/import/flowpath2prj.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"""
from __future__ import print_function
import copy
import sys
import os
Expand Down
20 changes: 6 additions & 14 deletions scripts/import/flowpath_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ def get_flowpath(cursor, huc12, fpath):
int the value of this huc12 flowpath
"""
cursor.execute(
"""
SELECT fid from flowpaths where huc_12 = %s and fpath = %s
and scenario = %s
""",
"SELECT fid from flowpaths where huc_12 = %s and fpath = %s "
"and scenario = %s",
(huc12, fpath, SCENARIO),
)
if cursor.rowcount == 0:
cursor.execute(
"""
INSERT into flowpaths(huc_12, fpath, scenario)
values (%s, %s, %s) RETURNING fid
""",
"INSERT into flowpaths(huc_12, fpath, scenario) "
"values (%s, %s, %s) RETURNING fid",
(huc12, fpath, SCENARIO),
)
return cursor.fetchone()[0]
Expand Down Expand Up @@ -110,8 +106,7 @@ def get_data(filename):


def delete_previous(cursor, huc12):
"""This file is the authority for the HUC12, so we cull previous content.
"""
"""This file is the authority for the HUC12, so we cull previous content."""
cursor.execute(
"""
DELETE from flowpath_points p USING flowpaths f WHERE
Expand All @@ -121,10 +116,7 @@ def delete_previous(cursor, huc12):
(SCENARIO, huc12, SCENARIO),
)
cursor.execute(
"""
DELETE from flowpaths WHERE
scenario = %s and huc_12 = %s
""",
"DELETE from flowpaths WHERE scenario = %s and huc_12 = %s",
(SCENARIO, huc12),
)

Expand Down
11 changes: 3 additions & 8 deletions scripts/switchgrass/mlra_summary.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""Summarize for MLRA, somehow"""
import sys

# third party
from pandas.io.sql import read_sql
import matplotlib

matplotlib.use("agg")
import matplotlib.pyplot as plt
from pyiem.plot.use_agg import plt
from pyiem.util import get_dbconn

LABELS = {
Expand All @@ -23,9 +20,7 @@ def main(argv):
fig, ax = plt.subplots(3, 3, figsize=(12, 6))

mlraxref = read_sql(
"""
select distinct mlra_id, mlra_name from mlra
""",
"select distinct mlra_id, mlra_name from mlra",
pgconn,
index_col="mlra_id",
)
Expand Down
22 changes: 6 additions & 16 deletions scripts/tillage_timing/huc12_overview_map.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
"""General HUC12 mapper"""
from __future__ import print_function

import datetime
import sys

from pyiem.plot.use_agg import plt
from pyiem.plot import MapPlot, nwsprecip
# third party
from pyiem.plot import MapPlot
from pyiem.util import get_dbconn
from geopandas import read_postgis
from shapely.wkb import loads
import numpy as np
import cartopy.crs as ccrs
from matplotlib.patches import Polygon
import matplotlib.colors as mpcolors

MYHUCS = [x.strip() for x in open("myhucs.txt").readlines()]


def main(argv):
def main():
"""Do Great Things"""
pgconn = get_dbconn("idep")
cursor = pgconn.cursor()

mp = MapPlot(
continentalcolor="#EEEEEE",
Expand Down Expand Up @@ -51,10 +43,8 @@ def main(argv):
mp.ax.add_patch(p)

df = read_postgis(
"""
select huc_12, ST_transform(geom, 4326) as geom from huc12
where huc_12 in %s and scenario = 0
""",
"select huc_12, ST_transform(geom, 4326) as geom from huc12 "
"where huc_12 in %s and scenario = 0",
pgconn,
params=(tuple(MYHUCS),),
geom_col="geom",
Expand All @@ -73,4 +63,4 @@ def main(argv):


if __name__ == "__main__":
main(sys.argv)
main()
68 changes: 40 additions & 28 deletions scripts/util/assign_county.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
from pyiem.util import get_dbconn
"""Compute the county a HUC12 belongs in."""
from pyiem.util import get_dbconn, logger

postgis = get_dbconn("postgis")
pcursor = postgis.cursor()
LOG = logger()

dep = get_dbconn("idep")
cursor = dep.cursor()
cursor2 = dep.cursor()

cursor.execute(
"""with data as (
SELECT ST_Transform(ST_Centroid(geom), 4326) as geo, gid
from huc12 where ugc is null)
def main():
"""Go Main Go."""
postgis = get_dbconn("postgis")
pcursor = postgis.cursor()

SELECT ST_x(geo), ST_y(geo), gid from data
"""
)
for row in cursor:
pcursor.execute(
"""
select ugc from ugcs where end_ts is null and
ST_Contains(geom, ST_SetSrid(ST_GeomFromText('POINT(%s %s)'), 4326))
and substr(ugc, 3, 1) = 'C'
dep = get_dbconn("idep")
cursor = dep.cursor()
cursor2 = dep.cursor()

cursor.execute(
"""with data as (
SELECT ST_Transform(ST_Centroid(geom), 4326) as geo, gid, huc_12
from huc12 where ugc is null)
SELECT ST_x(geo), ST_y(geo), gid, huc_12 from data
"""
% (row[0], row[1])
)
ugc = pcursor.fetchone()[0]
cursor2.execute(
"""UPDATE huc12 SET ugc = %s where gid = %s
""",
(ugc, row[2]),
)
for row in cursor:
pcursor.execute(
"""
select ugc from ugcs where end_ts is null and
ST_Contains(geom, ST_SetSrid(ST_GeomFromText('POINT(%s %s)'), 4326))
and substr(ugc, 3, 1) = 'C'
"""
% (row[0], row[1])
)
if pcursor.rowcount == 0:
LOG.info("failed to find a county for HUC12: %s", row[3])
continue
ugc = pcursor.fetchone()[0]
cursor2.execute(
"UPDATE huc12 SET ugc = %s where gid = %s",
(ugc, row[2]),
)

cursor2.close()
dep.commit()


cursor2.close()
dep.commit()
if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion scripts/util/compare_ofe_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Get some things straight in my head about these OFE files"""
from __future__ import print_function
from pyiem.dep import read_env, read_ofe


Expand Down
Loading

0 comments on commit a2566d6

Please sign in to comment.