Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 Sundry review #1095

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions htdocs/ASOS/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ $t->content = <<< EOM
<div class="well pull-right">
<b><u>ASOS</u></b>
<br>Reports: Hourly
<br>Stations: 15
<br><a href="/sites/locate.php?network=IA_ASOS">Locations</a>
</div>

The Automated Surface Observing System (ASOS) is considered
to be the flagship automated observing network. Located at airports, the ASOS
stations provide essential observations for the National Weather Service (NWS), the Federal Aviation Administration (FAA), and the Department of Defense (DOD).
The primary function of the ASOS stations are to take minute-by-minute
observations and generate basic weather reports.<font class="ref">[1]</font>

<p class="intro">Observations from the ASOS network are nationally monitored
for quality 24 hours per day. Depending on the trouble, maintenance can
be performed on the same day.</p>
<p>Please see this <a href="/onsite/news.phtml?id=1469" class="btn btn-danger">
<i class="fa fa-info"></i>
Important News Item</a> regarding wagering on ASOS temperatures.</p>

<div class="row">
<div class="col-md-6">
Expand Down
17 changes: 11 additions & 6 deletions pylib/iemweb/autoplot/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,25 @@ def networkselect_handler(value, arg, res):
) + map_select_widget(arg["network"], arg["name"])


def station_handler(value, arg, fdict, res, typ):
def station_handler(value, arg: dict, fdict, res, typ: str):
"""Generate HTML."""
networks = {}
pgconn, cursor = get_dbconnc("mesosite")
netlim = ""
if typ == "zstation":
netlim = "WHERE id ~* 'ASOS'"
elif typ == "station":
netlim = "WHERE id ~* 'CLIMATE'"
elif typ == "sid" and not arg.get("include_climodat", False):
netlim = "WHERE id !~* 'CLIMATE'"
cursor.execute(f"SELECT id, name from networks {netlim} ORDER by name ASC")
for row in cursor:
networks[row["id"]] = row["name"]
pgconn.close()
with get_sqlalchemy_conn("mesosite") as conn:
dbres = conn.execute(
sql_helper(
"SELECT id, name from networks {netlim} ORDER by name ASC",
netlim=netlim,
)
)
for row in dbres:
networks[row[0]] = row[1]
# We could have two plus zstations
networkcgi = "network"
if arg["name"][-1].isdigit():
Expand Down Expand Up @@ -201,6 +205,7 @@ def ugc_select(state, ugc):
ar[row["ugc"]] = (
f"{row['name']} {'(Zone)' if row['ugc'][2] == 'Z' else ''}"
)
cursor.close()
pgconn.close()
return make_select("ugc", ugc, ar, cssclass="iemselect2")

Expand Down
5 changes: 2 additions & 3 deletions pylib/iemweb/autoplot/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import json

from pydantic import Field
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.reference import FIGSIZES_NAMES
from pyiem.webutil import CGIModel, iemapp
from sqlalchemy import text

from iemweb.autoplot import data as autoplot_data
from iemweb.autoplot import import_script
Expand All @@ -27,7 +26,7 @@ def get_timing(pidx: int) -> float:
"""Return an average plot generation time for this app"""
with get_sqlalchemy_conn("mesosite") as conn:
res = conn.execute(
text(
sql_helper(
"SELECT avg(timing) from autoplot_timing where appid = :id "
"and valid > (now() - '7 days'::interval)"
),
Expand Down
4 changes: 2 additions & 2 deletions pylib/iemweb/autoplot/scripts/p1.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def plotter(ctx: dict):
label=f"Slope={h_slope:.2f} R$^2$={r_value**2:.2f}",
)
ax.legend(fontsize=10)
xmonths = ", ".join([calendar.month_abbr[x] for x in months1])
ymonths = ", ".join([calendar.month_abbr[x] for x in months2])
xmonths = ", ".join(str(calendar.month_abbr[x]) for x in months1)
ymonths = ", ".join(str(calendar.month_abbr[x]) for x in months2)
t1 = "" if varname1 not in ["days_high_aoa"] else f" {threshold:.0f}"
t2 = "" if varname2 not in ["days_high_aoa"] else f" {threshold:.0f}"
x = xdata.mean()
Expand Down
13 changes: 8 additions & 5 deletions pylib/iemweb/autoplot/scripts/p11.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

import matplotlib.dates as mdates
import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure
from sqlalchemy import text

PDICT = {
"below": "Daily Range Below Emphasis",
Expand Down Expand Up @@ -84,14 +83,18 @@ def plotter(ctx: dict):
varname = ctx["var"]
with get_sqlalchemy_conn("iem") as conn:
df = pd.read_sql(
text(f"""
sql_helper(
"""
select day, max_{varname}, min_{varname}
from summary_{year} s JOIN stations t on (s.iemid = t.iemid)
from {table} s JOIN stations t on (s.iemid = t.iemid)
where t.id = :station and t.network = :network and
max_{varname} is not null and
min_{varname} is not null
ORDER by day ASC
"""),
""",
varname=varname,
table=f"summary_{year}",
),
conn,
params={"station": station, "network": ctx["network"]},
index_col="day",
Expand Down
39 changes: 22 additions & 17 deletions pylib/iemweb/autoplot/scripts/p12.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import numpy as np
import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure_axes

Expand Down Expand Up @@ -103,36 +103,41 @@ def plotter(ctx: dict):
(extrenum, varname, direction) = ctx["which"].split("_")
year = ctx["year"]

op = "%s %s" % (varname, ">=" if direction == "above" else "<")
op = ">=" if direction == "above" else "<"
with get_sqlalchemy_conn("coop") as conn:
df = pd.read_sql(
f"""
sql_helper(
"""
with data as (
SELECT extract(year from day + '%s months'::interval)
SELECT extract(year from day + ':months months'::interval)
as season,
high, low, day from alldata WHERE station = %s
high, low, day from alldata WHERE station = :station
and day >= '1893-01-01'),
agg1 as (
SELECT season - %s as season,
SELECT season - :soff as season,
count(*) as obs,
min(case when {op} %s then day else null end) as nday,
max(case when {op} %s then day else null end) as xday,
sum(case when {op} %s then 1 else 0 end) as count
min(case when {varname} {op} :thresh then day else null end)
as nday,
max(case when {varname} {op} :thresh then day else null end)
as xday,
sum(case when {varname} {op} :thresh then 1 else 0 end)
as count
from data GROUP by season)
SELECT season::int, count, obs, nday,
extract(doy from nday) as nday_doy,
xday, extract(doy from xday) as xday_doy from agg1
ORDER by season ASC
""",
conn,
params=(
6 if season == "winter" else 0,
station,
1 if season == "winter" else 0,
threshold,
threshold,
threshold,
op=op,
varname=varname,
),
conn,
params={
"months": 6 if season == "winter" else 0,
"station": station,
"soff": 1 if season == "winter" else 0,
"thresh": threshold,
},
index_col="season",
)
# We need to do some magic to julian dates straight
Expand Down
5 changes: 2 additions & 3 deletions pylib/iemweb/autoplot/scripts/p13.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
import numpy as np
import pandas as pd
from matplotlib import cm
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import centered_bins, figure_axes, get_cmap
from scipy import stats
from sqlalchemy import text

from iemweb.autoplot import ARG_STATION

Expand Down Expand Up @@ -49,7 +48,7 @@ def plotter(ctx: dict):

with get_sqlalchemy_conn("coop") as conn:
df = pd.read_sql(
text(
sql_helper(
"""
with obs as (
select day, year, avg((high+low)/2.) OVER
Expand Down
5 changes: 2 additions & 3 deletions pylib/iemweb/autoplot/scripts/p15.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import matplotlib.patheffects as PathEffects
import numpy as np
import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure_axes
from pyiem.util import utc
from sqlalchemy import text

from iemweb.autoplot import ARG_STATION

Expand Down Expand Up @@ -50,7 +49,7 @@ def plotter(ctx: dict):

with get_sqlalchemy_conn("coop") as conn:
df = pd.read_sql(
text("""
sql_helper("""
with obs as
(select month, year, high, lag(high) OVER (ORDER by day ASC) as lhigh,
low, lag(low) OVER (ORDER by day ASC) as llow
Expand Down
10 changes: 5 additions & 5 deletions pylib/iemweb/autoplot/scripts/p16.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import numpy as np
import pandas as pd
from metpy.units import units
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot.windrose import WindrosePlot, histogram
from pyiem.util import drct2text
from sqlalchemy import text

from iemweb.util import month2months

Expand Down Expand Up @@ -244,14 +243,15 @@ def add_ctx(ctx):
title = f"Relative Humidity below {ctx['threshold']}%"
with get_sqlalchemy_conn("asos") as conn:
ctx["df"] = pd.read_sql(
text(
f"""
sql_helper(
"""
SELECT valid at time zone 'UTC' as valid,
drct, sknt * 1.15 as smph from alldata
where station = :station and {limiter} and sknt > 0
and drct >= 0 and
drct <= 360 and extract(month from valid) = ANY(:months)
"""
""",
limiter=limiter,
),
conn,
params=params,
Expand Down
5 changes: 2 additions & 3 deletions pylib/iemweb/autoplot/scripts/p19.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

import numpy as np
import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure, get_cmap
from sqlalchemy import text

from iemweb.autoplot import ARG_STATION, get_monofont
from iemweb.util import month2months
Expand Down Expand Up @@ -92,7 +91,7 @@ def plotter(ctx: dict):
year = ctx.get("year")
with get_sqlalchemy_conn("coop") as conn:
ddf = pd.read_sql(
text(
sql_helper(
"SELECT high, low, year, month from alldata "
"WHERE station = :station "
"and high >= low and month = ANY(:months) "
Expand Down
29 changes: 18 additions & 11 deletions pylib/iemweb/autoplot/scripts/p2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import pandas as pd
from matplotlib.patches import Circle
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure_axes
from scipy import stats
Expand Down Expand Up @@ -64,27 +64,34 @@ def plotter(ctx: dict):

with get_sqlalchemy_conn("coop") as conn:
df = pd.read_sql(
"SELECT year, sum(precip) as total_precip, "
"sum(gddxx(%s, %s, high::numeric,low::numeric)) as gdd "
"from alldata WHERE station = %s and month = %s GROUP by year",
sql_helper("""
SELECT year, sum(precip) as total_precip,
sum(gddxx(:gddbase, :gddceil, high::numeric,low::numeric)) as gdd
from alldata WHERE station = :station and month = :month GROUP by year
"""),
conn,
params=(ctx["gddbase"], ctx["gddceil"], station, month),
params={
"gddbase": ctx["gddbase"],
"gddceil": ctx["gddceil"],
"station": station,
"month": month,
},
index_col="year",
)
if len(df.index) < 3:
raise NoDataFound("ERROR: No Data Found")

gstats = df.gdd.describe()
pstats = df.total_precip.describe()
gstats = df["gdd"].describe()
pstats = df["total_precip"].describe()
if "mean" not in pstats:
raise NoDataFound("ERROR: No Data Found")

df["precip_sigma"] = (df.total_precip - pstats["mean"]) / pstats["std"]
df["gdd_sigma"] = (df.gdd - gstats["mean"]) / gstats["std"]
df["distance"] = (df.precip_sigma**2 + df.gdd_sigma**2) ** 0.5
df["precip_sigma"] = (df["total_precip"] - pstats["mean"]) / pstats["std"]
df["gdd_sigma"] = (df["gdd"] - gstats["mean"]) / gstats["std"]
df["distance"] = (df["precip_sigma"] ** 2 + df["gdd_sigma"] ** 2) ** 0.5

h_slope, intercept, r_value, _, _ = stats.linregress(
df["gdd_sigma"], df["precip_sigma"]
df["gdd_sigma"].to_numpy(), df["precip_sigma"].to_numpy()
)

y1 = -4.0 * h_slope + intercept
Expand Down
5 changes: 2 additions & 3 deletions pylib/iemweb/autoplot/scripts/p20.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

import numpy as np
import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure_axes
from sqlalchemy import text


def get_description():
Expand Down Expand Up @@ -41,7 +40,7 @@ def plotter(ctx: dict):
# Oh, the pain of floating point comparison here.
with get_sqlalchemy_conn("asos") as conn:
df = pd.read_sql(
text("""
sql_helper("""
WITH obs as (
SELECT distinct date_trunc('hour', valid) as t from alldata
WHERE station = :station and p01i > 0.009
Expand Down
5 changes: 2 additions & 3 deletions pylib/iemweb/autoplot/scripts/p22.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import calendar

import pandas as pd
from pyiem.database import get_sqlalchemy_conn
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.exceptions import NoDataFound
from pyiem.plot import figure_axes
from sqlalchemy import text

from iemweb.autoplot import ARG_STATION

Expand Down Expand Up @@ -47,7 +46,7 @@ def plotter(ctx: dict):
station = ctx["station"]
with get_sqlalchemy_conn("coop") as conn:
df = pd.read_sql(
text("""
sql_helper("""
WITH climate as (
SELECT to_char(valid, 'mmdd') as sday, high, low from
ncei_climate91 where station = :ncei
Expand Down
Loading