Skip to content

Commit

Permalink
🎨 Update to use new pyiem nav helper
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Dec 21, 2024
1 parent b15d409 commit 738972d
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 375 deletions.
1 change: 1 addition & 0 deletions htdocs/iemre/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require_once "../../config/settings.inc.php";
require_once "../../include/myview.php";
$t = new MyView();
$t->title = "Reanalysis Data";
$t->iem_resource = "IEMRE";

$s4link = sprintf("/archive/data/%s/stage4/", date("Y/m/d"));

Expand Down
2 changes: 1 addition & 1 deletion htdocs/wx/afos/p.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class="img img-responsive"></p>
<div class="row">
<div class="col-sm-7 col-md-7">
<p>Displaying AFOS PIL: <strong>$pil</strong>
Received: <strong>{$dstamp} UTC</strong>
Product Timestamp: <strong>{$dstamp} UTC</strong>
<p><a class="btn btn-primary"
href="p.php?dir=prev&pil=$pil&e=$newe"><i class="fa fa-arrow-left"></i>
Expand Down
22 changes: 12 additions & 10 deletions pylib/iemweb/autoplot/scripts/p4.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
from pyiem import iemre, reference
from pyiem import reference
from pyiem.database import get_sqlalchemy_conn
from pyiem.exceptions import NoDataFound
from pyiem.grid import nav
from pyiem.grid.zs import CachingZonalStats
from pyiem.iemre import daily_offset, get_daily_ncname
from pyiem.plot import figure_axes
from pyiem.util import get_autoplot_context, ncopen
from sqlalchemy import text
Expand Down Expand Up @@ -69,23 +71,23 @@ def plotter(fdict):
geom_col="the_geom",
)

ncfn = iemre.get_daily_ncname(year)
ncfn = get_daily_ncname(year)
if not os.path.isfile(ncfn):
raise NoDataFound("Data not available for year")
with ncopen(ncfn) as nc:
precip = nc.variables["p01d"]
czs = CachingZonalStats(iemre.DOMAINS[""]["affine"])
czs = CachingZonalStats(nav.IEMRE.affine_image)
hasdata = np.zeros(
(nc.dimensions["lat"].size, nc.dimensions["lon"].size)
)
czs.gen_stats(hasdata, states["the_geom"])
for nav in czs.gridnav:
if nav is None:
for gnav in czs.gridnav:
if gnav is None:
continue
grid = np.ones((nav.ysz, nav.xsz))
grid[nav.mask] = 0.0
jslice = slice(nav.y0, nav.y0 + nav.ysz)
islice = slice(nav.x0, nav.x0 + nav.xsz)
grid = np.ones((gnav.ysz, gnav.xsz))
grid[gnav.mask] = 0.0
jslice = slice(gnav.y0, gnav.y0 + gnav.ysz)
islice = slice(gnav.x0, gnav.x0 + gnav.xsz)
hasdata[jslice, islice] = np.where(
grid > 0, 1, hasdata[jslice, islice]
)
Expand All @@ -100,7 +102,7 @@ def plotter(fdict):
days = []
coverage = []
while now <= ets:
idx = iemre.daily_offset(now)
idx = daily_offset(now)
sevenday = np.sum(precip[(idx - period) : idx, :, :], 0)
pday = np.where(hasdata > 0, sevenday[:, :], -1)
tots = np.sum(np.where(pday >= (threshold * 25.4), 1, 0))
Expand Down
24 changes: 12 additions & 12 deletions pylib/iemweb/autoplot/scripts/p86.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

import numpy as np
from metpy.units import masked_array, units
from pyiem import iemre
from pyiem.exceptions import NoDataFound
from pyiem.grid.nav import get_nav
from pyiem.iemre import daily_offset, get_daily_ncname
from pyiem.plot import MapPlot, get_cmap, pretty_bins
from pyiem.util import get_autoplot_context, ncopen

Expand Down Expand Up @@ -140,6 +141,7 @@ def plotter(fdict):
"""Go"""
ctx = get_autoplot_context(fdict, get_description())
domain = ctx["domain"]
gridnav = get_nav("iemre", domain)
ptype = ctx["ptype"]
dt = ctx["date"]
varname = ctx["var"]
Expand All @@ -152,21 +154,19 @@ def plotter(fdict):
}
if domain != "":
ctx["csector"] = "custom"
mpargs["west"] = iemre.DOMAINS[domain]["west"]
mpargs["east"] = iemre.DOMAINS[domain]["east"]
mpargs["south"] = iemre.DOMAINS[domain]["south"]
mpargs["north"] = iemre.DOMAINS[domain]["north"]
mpargs["west"] = gridnav.left_edge
mpargs["east"] = gridnav.right_edge
mpargs["south"] = gridnav.bottom_edge
mpargs["north"] = gridnav.top_edge

mp = MapPlot(**mpargs)

plot_units = ""
idx0 = iemre.daily_offset(dt)
ncfn = iemre.get_daily_ncname(dt.year, domain=domain)
idx0 = daily_offset(dt)
ncfn = get_daily_ncname(dt.year, domain=domain)
if not os.path.isfile(ncfn):
raise NoDataFound("No Data Found.")
with ncopen(ncfn) as nc:
lats = nc.variables["lat"][:]
lons = nc.variables["lon"][:]
cmap = get_cmap(ctx["cmap"])
data = unit_convert(nc, varname, idx0)
if np.ma.is_masked(data) and data.mask.all():
Expand Down Expand Up @@ -207,7 +207,7 @@ def plotter(fdict):
clevs = pretty_bins(ptiles[0], ptiles[1])

if ptype == "c":
x, y = np.meshgrid(lons, lats)
x, y = np.meshgrid(gridnav.x_points, gridnav.y_points)
mp.contourf(
x,
y,
Expand All @@ -222,8 +222,8 @@ def plotter(fdict):
else:
mp.imshow(
data,
iemre.DOMAINS[domain]["affine_native"],
"EPSG:4326",
gridnav.affine,
gridnav.crs,
clevs=clevs,
cmap=cmap,
units=plot_units,
Expand Down
20 changes: 11 additions & 9 deletions pylib/iemweb/autoplot/scripts/p89.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import numpy as np
import pandas as pd
from metpy.units import units
from pyiem import iemre, reference
from pyiem import reference
from pyiem.database import get_sqlalchemy_conn
from pyiem.exceptions import NoDataFound
from pyiem.grid import nav
from pyiem.grid.zs import CachingZonalStats
from pyiem.iemre import daily_offset, get_daily_ncname
from pyiem.plot import figure
from pyiem.util import get_autoplot_context, ncopen

Expand Down Expand Up @@ -60,7 +62,7 @@ def get_description():

def do_date(ctx, now: datetime, precip, daythres, trailthres):
"""Do the local date and return a dict"""
idx = iemre.daily_offset(now)
idx = daily_offset(now)
ctx["days"].append(now)
sevenday = np.sum(precip[(idx - ctx["period"]) : idx, :, :], 0)
ptrail = np.where(ctx["iowa"] > 0, sevenday, -1)
Expand Down Expand Up @@ -94,21 +96,21 @@ def get_data(ctx):
if states.empty:
raise NoDataFound("No data was found.")

ncfn = iemre.get_daily_ncname(ctx["year"])
ncfn = get_daily_ncname(ctx["year"])
if not os.path.isfile(ncfn):
raise NoDataFound(f"Missing {ncfn}")
with ncopen(ncfn) as nc:
precip = nc.variables["p01d"]
czs = CachingZonalStats(iemre.DOMAINS[""]["affine"])
czs = CachingZonalStats(nav.IEMRE.affine_image)
hasdata = np.zeros(
(nc.dimensions["lat"].size, nc.dimensions["lon"].size)
)
czs.gen_stats(hasdata, states["the_geom"])
for nav in czs.gridnav:
grid = np.ones((nav.ysz, nav.xsz))
grid[nav.mask] = 0.0
jslice = slice(nav.y0, nav.y0 + nav.ysz)
islice = slice(nav.x0, nav.x0 + nav.xsz)
for gnav in czs.gridnav:
grid = np.ones((gnav.ysz, gnav.xsz))
grid[gnav.mask] = 0.0
jslice = slice(gnav.y0, gnav.y0 + gnav.ysz)
islice = slice(gnav.x0, gnav.x0 + gnav.xsz)
hasdata[jslice, islice] = np.where(
grid > 0, 1, hasdata[jslice, islice]
)
Expand Down
24 changes: 13 additions & 11 deletions pylib/iemweb/autoplot/scripts100/p175.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
from pyiem import iemre, reference
from pyiem import reference
from pyiem.database import get_sqlalchemy_conn
from pyiem.exceptions import NoDataFound
from pyiem.grid import nav
from pyiem.grid.zs import CachingZonalStats
from pyiem.iemre import daily_offset, get_daily_ncname
from pyiem.plot import figure_axes
from pyiem.util import convert_value, get_autoplot_context, ncopen

Expand Down Expand Up @@ -76,21 +78,21 @@ def plotter(fdict):
geom_col="the_geom",
)

sidx = iemre.daily_offset(sts)
ncfn = iemre.get_daily_ncname(sts.year)
sidx = daily_offset(sts)
ncfn = get_daily_ncname(sts.year)
if not os.path.isfile(ncfn):
raise NoDataFound(f"Data for year {sts.year} not found")
with ncopen(ncfn) as nc:
czs = CachingZonalStats(iemre.DOMAINS[""]["affine"])
czs = CachingZonalStats(nav.IEMRE.affine_image)
hasdata = np.zeros(
(nc.dimensions["lat"].size, nc.dimensions["lon"].size)
)
czs.gen_stats(hasdata, states["the_geom"])
for nav in czs.gridnav:
grid = np.ones((nav.ysz, nav.xsz))
grid[nav.mask] = 0.0
jslice = slice(nav.y0, nav.y0 + nav.ysz)
islice = slice(nav.x0, nav.x0 + nav.xsz)
for gnav in czs.gridnav:
grid = np.ones((gnav.ysz, gnav.xsz))
grid[gnav.mask] = 0.0
jslice = slice(gnav.y0, gnav.y0 + gnav.ysz)
islice = slice(gnav.x0, gnav.x0 + gnav.xsz)
hasdata[jslice, islice] = np.where(
grid > 0, 1, hasdata[jslice, islice]
)
Expand All @@ -106,8 +108,8 @@ def plotter(fdict):
}
)

eidx = iemre.daily_offset(ets)
ncfn = iemre.get_daily_ncname(ets.year)
eidx = daily_offset(ets)
ncfn = get_daily_ncname(ets.year)
if not os.path.isfile(ncfn):
raise NoDataFound(f"Data for year {ets.year} not found")
with ncopen(ncfn) as nc:
Expand Down
2 changes: 1 addition & 1 deletion pylib/iemweb/autoplot/scripts200/p242.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_image(filename):
return OffsetImage(plt.imread(fn, format=filename[-3:]), zoom=0.7)


def overlay_info(fig, df):
def overlay_info(fig, df: gpd.GeoDataFrame):
"""Add bling."""
if len(df.index) > 1:
msg = ""
Expand Down
48 changes: 20 additions & 28 deletions pylib/iemweb/autoplot/scripts200/p249.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import numpy as np
from metpy.units import masked_array, units
from pyiem import iemre
from pyiem.exceptions import NoDataFound
from pyiem.grid.nav import get_nav
from pyiem.iemre import get_hourly_ncname, hourly_offset
from pyiem.plot import MapPlot, get_cmap, pretty_bins
from pyiem.reference import LATLON
from pyiem.util import get_autoplot_context, ncopen

from iemweb.autoplot import ARG_IEMRE_DOMAIN
Expand Down Expand Up @@ -78,12 +78,11 @@ def get_description():
return desc


def unit_convert(nc, varname, idx0, jslice, islice):
def unit_convert(nc, varname, idx0):
"""Convert units."""
if varname == "wind_speed":
data = (
nc.variables["uwnd"][idx0, jslice, islice] ** 2
+ nc.variables["vwnd"][idx0, jslice, islice] ** 2
nc.variables["uwnd"][idx0] ** 2 + nc.variables["vwnd"][idx0] ** 2
) ** 0.5
data = (
masked_array(
Expand All @@ -94,7 +93,7 @@ def unit_convert(nc, varname, idx0, jslice, islice):
.m
)
else:
data = nc.variables[varname][idx0, jslice, islice]
data = nc.variables[varname][idx0]
if varname in [
"p01m",
]:
Expand All @@ -112,6 +111,7 @@ def plotter(fdict):
"""Go"""
ctx = get_autoplot_context(fdict, get_description())
domain = ctx["domain"]
gridnav = get_nav("iemre", domain)
ptype = ctx["ptype"]
valid = ctx["valid"].replace(tzinfo=timezone.utc)
varname = ctx["var"]
Expand All @@ -125,28 +125,21 @@ def plotter(fdict):
}
if domain != "":
ctx["csector"] = "custom"
mpargs["east"] = iemre.DOMAINS[domain]["east"]
mpargs["west"] = iemre.DOMAINS[domain]["west"]
mpargs["south"] = iemre.DOMAINS[domain]["south"]
mpargs["north"] = iemre.DOMAINS[domain]["north"]
mpargs["east"] = gridnav.right_edge
mpargs["west"] = gridnav.left_edge
mpargs["south"] = gridnav.bottom_edge
mpargs["north"] = gridnav.top_edge

mp = MapPlot(**mpargs)
(west, east, south, north) = mp.panels[0].get_extent(LATLON)
i0, j0 = iemre.find_ij(west, south, domain=domain)
i1, j1 = iemre.find_ij(east, north, domain=domain)
jslice = slice(j0, j1)
islice = slice(i0, i1)

plot_units = ""
idx0 = iemre.hourly_offset(valid)
ncfn = iemre.get_hourly_ncname(valid.year, domain=domain)
idx0 = hourly_offset(valid)
ncfn = get_hourly_ncname(valid.year, domain=domain)
if not os.path.isfile(ncfn):
raise NoDataFound("No Data Found.")
with ncopen(ncfn) as nc:
lats = nc.variables["lat"][jslice]
lons = nc.variables["lon"][islice]
cmap = get_cmap(ctx["cmap"])
data = unit_convert(nc, varname, idx0, jslice, islice)
data = unit_convert(nc, varname, idx0)
if np.ma.is_masked(np.max(data)):
raise NoDataFound("Data Unavailable")
ptiles = np.nanpercentile(data.filled(np.nan), [5, 95, 99.9])
Expand Down Expand Up @@ -176,12 +169,12 @@ def plotter(fdict):
plot_units = "W m**-2"
clevs = pretty_bins(0, ptiles[1])

x, y = np.meshgrid(lons, lats)
if ptype == "c":
x, y = np.meshgrid(gridnav.x_points, gridnav.y_points)
# in the case of contour, use the centroids on the grids
mp.contourf(
x + 0.125,
y + 0.125,
x,
y,
data,
clevs,
units=plot_units,
Expand All @@ -191,12 +184,11 @@ def plotter(fdict):
clip_on=ctx["clip"] == "yes",
)
else:
x, y = np.meshgrid(lons, lats)
mp.pcolormesh(
x,
y,
mp.imshow(
data,
clevs,
gridnav.affine,
gridnav.crs,
clevs=clevs,
cmap=cmap,
units=plot_units,
clip_on=ctx["clip"] == "yes",
Expand Down
Loading

0 comments on commit 738972d

Please sign in to comment.