Skip to content

Commit

Permalink
address some TD tileflow plotting issues per isudatateam#472
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed May 24, 2022
1 parent 3292b95 commit c62b8b9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion config/datateam.inc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
RewriteRule ^agronomic__(.*)__(.*)__(.*)__(.*).js$ plot_agronomic.py?site=$1&varname=$2&ptype=$3&view=js&ungroup=$4
RewriteRule ^decagon__(.*)__(.*)__(.*)__(.*)__(.*).js$ plot_decagon.py?site=$1&date=$2&days=$3&ptype=$4&depth=$5&view=js
RewriteRule ^watertable__(.*)__(.*)__(.*)__(.*)_(.*).js$ plot_watertable.py?site=$1&date=$2&days=$3&ptype=$4&view=js&ungroup=$5
RewriteRule ^tileflow__(.*)__(.*)__(.*)__(.*)__([10]).js$ plot_tileflow.py?site=$1&date=$2&days=$3&ptype=$4&view=js&ungroup=$5
RewriteRule ^tileflow__(.*)__(.*)__(.*)__(.*)__([10]).js$ plot_tileflow.py?site=$1&date=$2&days=$3&by=$4&view=js&ungroup=$5
RewriteRule ^nitrateload__(.*)__(.*)__(.*)__(.*)__([10]).js$ plot_nitrateload.py?site=$1&date=$2&days=$3&by=$4&view=js&ungroup=$5
</Directory>

Expand Down
2 changes: 1 addition & 1 deletion htdocs/td/dl/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ located on farmer fields.</p>
<p>Measurements include:<br />
<ul>
<li><a href="/td/plot_tileflow.phtml" target="_blank">Tile Flow</a> (Still in Development)</li>
<li><a href="/td/plot_tileflow.phtml" target="_blank">Tile Flow</a></li>
<li><a href="/td/plot_nitrateload.phtml" target="_blank">Nitrate Load</a></li>
<li><a href="/td/plot_watertable.phtml" target="_blank">Water Table Depth</a> (Still in Development)</li>
<li>Water Quality (coming soon)</li>
Expand Down
26 changes: 11 additions & 15 deletions htdocs/td/plot_tileflow.phtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once "../../include/forms.php";
require_once "../../include/myview.php";
$HIGHCHARTS = "8.2.2";
$HIGHCHARTS = "9.3.1";

$site = isset($_GET["site"]) ? $_GET["site"]: 'IA_Boone';
$date = isset($_GET["date"]) ? $_GET["date"]: '2016-06-09';
Expand Down Expand Up @@ -63,13 +63,6 @@ $ar = Array(
);
$siteselect = td_site_select("site", $site, $ar);

$ar = Array(
'daily' => 'Daily (no aggregation)',
'monthly' => 'Monthly Totals',
'yearly' => 'Yearly Totals',
);
$byselect = make_select("by", $by, $ar);

$jsurl = sprintf("tileflow__%s__%s__%s__%s__%s.js",
$site, $date, $days, $by, $ungroup);
$jsextra = "<script src=\"{$jsurl}\"></script>";
Expand Down Expand Up @@ -108,6 +101,7 @@ $(document).ready(function(){
</script>
EOF;
$dby = ($by == "daily") ? " CHECKED ": "";
$wby = ($by == "weekly") ? " CHECKED ": "";
$mby = ($by == "monthly") ? " CHECKED ": "";
$yby = ($by == "yearly") ? " CHECKED ": "";
$ungroupselected = ($ungroup == "1") ? " checked=\"checked\"": "";
Expand Down Expand Up @@ -152,9 +146,11 @@ and aggregation tool. Data are from the USDA-NIFA funded project:
<br /><strong>Sum Values:</strong>
<br />
<input type="radio" name="by" value="daily"{$dby}> daily &nbsp;
<input type="radio" name="by" value="monthly"{$mby}> monthly &nbsp;
<input type="radio" name="by" value="yearly"{$yby}> yearly
<input type="radio" name="by" value="daily"{$dby} id="daily"> <label for="daily">daily</label> &nbsp;
<input type="radio" name="by" value="weekly"{$wby} id="weekly">
<label for="weekly">weekly (Mon-Sun)</label>&nbsp;
<input type="radio" name="by" value="monthly"{$mby} id="monthly"> <label for="monthy">monthly</label> &nbsp;
<input type="radio" name="by" value="yearly"{$yby} id="yearly"> <label for="yearly">yearly</label>
</div>
Expand All @@ -164,20 +160,20 @@ and aggregation tool. Data are from the USDA-NIFA funded project:
</form>
<p>Plotting of data may not occur if the time period selected is more than
~700 days. This is due to the amount of data overwhelming the users web
~700 days. This is due to the amount of data overwhelming the user's web
browser and failing to load. Select a shorter time span if you run into
this problem. Long time periods can be selected without problems if only
exporting the data.</p>
<p><strong>Viewing Options:</strong> Click the legend name to de/select data series.
To zoom in, click and drag your mouse within the plot frame.</p>
<p><strong>Download Options:</strong> Download the figure by right clicking
on the symbol in the upper right corner. Download the corresponding data
<p><strong>Download Options:</strong> Download the figure by clicking on the
three-bar menu symbol in the upper right corner of the plot.
Download the corresponding data
<a href="/td/dl/#tab_data" target="_blank">here</a>.</p>
<p>{$interface}</p>
EOF;
$t->render('full.phtml');
?>
66 changes: 37 additions & 29 deletions htdocs/td/plot_tileflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import datetime

import pandas as pd
from pandas.io.sql import read_sql
import numpy as np
from paste.request import parse_formvars
from pyiem.util import get_dbconn
from pyiem.util import get_sqlalchemy_conn

sys.path.append("/opt/datateam/htdocs/td")
from common import CODES, getColor, send_error, COPYWRITE
Expand Down Expand Up @@ -38,29 +37,35 @@
"-.",
"-.",
]
BYCOL = {"daily": "day", "monthly": "month", "yearly": "year"}
BYCOL = {
"daily": "day",
"weekly": "week",
"monthly": "month",
"yearly": "year",
}


def get_weather(pgconn, uniqueid, sts, ets):
def get_weather(uniqueid, sts, ets):
"""Retreive the daily precipitation"""
# Convert ids
df = read_sql(
"SELECT date, precipitation from weather_data "
"WHERE siteid = %s and date >= %s and date <= %s ORDER by date ASC",
pgconn,
index_col="date",
params=(uniqueid, sts.date(), ets.date()),
)
with get_sqlalchemy_conn("td") as conn:
df = pd.read_sql(
"SELECT date, precipitation from weather_data "
"WHERE siteid = %s and date >= %s and date <= %s "
"ORDER by date ASC",
conn,
index_col="date",
params=(uniqueid, sts.date(), ets.date()),
)
df.index = pd.DatetimeIndex(df.index.values)
df["ticks"] = (
df.index.values.astype("datetime64[ns]").astype(np.int64) // 10 ** 6
df.index.values.astype("datetime64[ns]").astype(np.int64) // 10**6
)
return df


def make_plot(form, start_response):
"""Make the plot"""
pgconn = get_dbconn("td")
uniqueid = form.get("site", "ISUAG")

sts = datetime.datetime.strptime(
Expand All @@ -69,27 +74,30 @@ def make_plot(form, start_response):
days = int(form.get("days", 1))
ungroup = int(form.get("ungroup", 0))
ets = sts + datetime.timedelta(days=days)
wxdf = get_weather(pgconn, uniqueid, sts, ets)
wxdf = get_weather(uniqueid, sts, ets)
by = form.get("by", "daily")
df = read_sql(
f"SELECT date_trunc('{BYCOL[by]}', date)::date as v, "
"coalesce(plotid, location) as datum, sum(discharge) as discharge "
"from tile_flow_and_n_loads_data WHERE siteid = %s "
"and date between %s and %s GROUP by v, datum ORDER by v ASC",
pgconn,
params=(uniqueid, sts.date(), ets.date()),
)
print(by)
with get_sqlalchemy_conn("td") as conn:
df = pd.read_sql(
f"SELECT date_trunc('{BYCOL[by]}', date)::date as v, "
"coalesce(plotid, location) as datum, sum(discharge) as discharge "
"from tile_flow_and_n_loads_data WHERE siteid = %s "
"and date between %s and %s GROUP by v, datum ORDER by v ASC",
conn,
params=(uniqueid, sts.date(), ets.date()),
)
if len(df.index) < 3:
send_error(start_response, "js", "No / Not Enough Data Found, sorry!")
linecol = "datum"
if ungroup == 0:
# Generate the plotid lookup table
plotdf = read_sql(
"SELECT * from meta_plot_identifier where siteid = %s",
pgconn,
params=(uniqueid,),
index_col="plotid",
)
with get_sqlalchemy_conn("td") as conn:
plotdf = pd.read_sql(
"SELECT * from meta_plot_identifier where siteid = %s",
conn,
params=(uniqueid,),
index_col="plotid",
)

def lookup(row):
"""Lookup value."""
Expand All @@ -112,7 +120,7 @@ def lookup(row):
plot_ids.sort()
if ungroup == "0":
plot_ids = plot_ids[::-1]
df["ticks"] = pd.to_datetime(df["v"]).astype(np.int64) // 10 ** 6
df["ticks"] = pd.to_datetime(df["v"]).astype(np.int64) // 10**6
seriestype = "line" if by in ["daily"] else "column"
for i, plotid in enumerate(plot_ids):
df2 = df[df[linecol] == plotid]
Expand Down

0 comments on commit c62b8b9

Please sign in to comment.