Skip to content

Commit

Permalink
Merge pull request #641 from akrherz/240119
Browse files Browse the repository at this point in the history
RWIS Fixups
  • Loading branch information
akrherz authored Jan 19, 2024
2 parents acf5df6 + 31bd51a commit cd34763
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 329 deletions.
184 changes: 59 additions & 125 deletions htdocs/plotting/rwis/SFtemps.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
require_once "../../../config/settings.inc.php";
require_once "../../../include/database.inc.php";
require_once "../../../include/forms.php";
require_once "../../../include/mlib.php";
require_once "../../../include/network.php";
require_once "../../../include/jpgraph/jpgraph.php";
require_once "../../../include/jpgraph/jpgraph_line.php";
require_once "../../../include/jpgraph/jpgraph_bar.php";
require_once "../../../include/jpgraph/jpgraph_date.php";
require_once "../../../include/jpgraph/jpgraph_led.php";

/** We need these vars to make this work */
$subc = isset($_GET["subc"]) ? $_GET["subc"] : "";
Expand All @@ -22,112 +24,51 @@
$sday = get_int404("sday", date("d"));
$days = get_int404("days", 2);
$station = isset($_GET['station']) ? xssafe($_GET["station"]) : "";
$mode = isset($_GET["mode"]) ? xssafe($_GET["mode"]) : "rt";
$network = isset($_GET["network"]) ? xssafe($_GET["network"]) : "IA_RWIS";

/** Lets assemble a time period if this plot is historical */
if (strlen($days) > 0) {
$sts = mktime(0, 0, 0, $smonth, $sday, $syear);
$dbDateString = "'" . date('Y-m-d', $sts) . "'";
$plotTitle = date('d M Y', $sts) . "\n";
for ($i = 1; $i < intval($days); $i++) {
$tts = $sts + ($i * 86400);
$dbDateString .= ",'" . date('Y-m-d', $tts) . "'";
$plotTitle .= date('d M Y', $tts) . "\n";
}
}
$sts = new DateTime("$syear-$smonth-$sday");
$ets = new DateTime("$syear-$smonth-$sday");
$ets->modify("+$days days");

$dbName = "iowa";
//$station = 'RAME';


$val = "> -50";
if (isset($_GET["limit"])) $val = "between 25 and 35";

if ($mode == "rt") {
$c1 = iemdb('rwis');
$c0 = iemdb("iem");
$q0 = "SELECT
valid, gvalid, max(tmpf) as tmpf, max(pday) as pcpn,
max(dwpf) as dwpf, max(tcs0) as tcs0, max(tcs1) as tcs1,
max(tcs2) as tcs2, max(tcs3) as tcs3, max(subc) as subc
FROM
(SELECT
to_char(valid, 'mm/dd HH PM') as valid,
newd || ':' || (case
when minute > 39 THEN '40'::text
WHEN minute > 19 THEN '20'::text
ELSE '00'::text END)::text as gvalid, pday,
CASE WHEN tmpf " . $val . " THEN tmpf ELSE NULL END as tmpf,
CASE WHEN dwpf " . $val . " THEN dwpf ELSE NULL END as dwpf,
CASE WHEN tsf0 " . $val . " THEN tsf0 ELSE NULL END as tcs0,
CASE WHEN tsf1 " . $val . " THEN tsf1 ELSE NULL END as tcs1,
CASE WHEN tsf2 " . $val . " THEN tsf2 ELSE NULL END as tcs2,
CASE WHEN tsf3 " . $val . " THEN tsf3 ELSE NULL END as tcs3,
CASE WHEN rwis_subf " . $val . " THEN rwis_subf ELSE NULL END as subc
FROM
(SELECT
*,
to_char(valid, 'YYYY-MM-DD HH24') as newd,
extract(minute from valid) as minute
FROM
current_log c JOIN stations s on (s.iemid = c.iemid)
WHERE
s.id = '$station'
ORDER by valid ASC) as foo) as bar
GROUP by valid, gvalid ORDER by gvalid ASC";
$minInterval = 20;
} else {
$c0 = iemdb('rwis');
$c1 = $c0;
$q0 = "SELECT
valid, gvalid, max(tmpf) as tmpf, max(pcpn) as pcpn,
max(dwpf) as dwpf, max(tcs0) as tcs0, max(tcs1) as tcs1,
max(tcs2) as tcs2, max(tcs3) as tcs3, max(subc) as subc
FROM
(SELECT
to_char(valid, 'mm/dd HH PM') as valid,
newd || ':' || (case
when minute > 39 THEN '40'::text
WHEN minute > 19 THEN '20'::text
ELSE '00'::text END)::text as gvalid, pcpn,
CASE WHEN tmpf " . $val . " THEN tmpf ELSE NULL END as tmpf,
CASE WHEN dwpf " . $val . " THEN dwpf ELSE NULL END as dwpf,
CASE WHEN tfs0 " . $val . " THEN tfs0 ELSE NULL END as tcs0,
CASE WHEN tfs1 " . $val . " THEN tfs1 ELSE NULL END as tcs1,
CASE WHEN tfs2 " . $val . " THEN tfs2 ELSE NULL END as tcs2,
CASE WHEN tfs3 " . $val . " THEN tfs3 ELSE NULL END as tcs3,
CASE WHEN subf " . $val . " THEN subf ELSE NULL END as subc
FROM
(SELECT
*,
to_char(valid, 'YYYY-MM-DD HH24') as newd,
extract(minute from valid) as minute
FROM
alldata
WHERE
station = '$station' and
date(valid) IN ($dbDateString)
ORDER by valid ASC) as foo) as bar
GROUP by valid, gvalid ORDER by gvalid ASC";
$minInterval = 20;
$rwisdb = iemdb('rwis');
$rs = pg_prepare($rwisdb, "OBS", "
SELECT * FROM alldata WHERE
station = $1 and valid >= $2 and valid < $3
ORDER by valid ASC");
$minInterval = 20;
$result = pg_execute($rwisdb, "OBS", array($station, $sts->format("Y-m-d H:i"), $ets->format("Y-m-d H:i")));

$minInterval = 20;

$rs = pg_prepare($rwisdb, "META", "SELECT * from sensors WHERE station = $1");
$r1 = pg_execute($rwisdb, "META", array($station));

$ns0 = "Sensor 1";
$ns1 = "Sensor 2";
$ns2 = "Sensor 3";
$ns3 = "Sensor 4";
if (pg_num_rows($r1) > 0) {
$row = pg_fetch_array($r1);
$ns0 = $row['sensor0'];
$ns1 = $row['sensor1'];
$ns2 = $row['sensor2'];
$ns3 = $row['sensor3'];
}

$q1 = "SELECT * from sensors WHERE station = '" . $station . "' ";

//echo $q0;
$result = pg_exec($c0, $q0);
$r1 = pg_exec($c1, $q1);
if (pg_num_rows($result) == 0) {
$led = new DigitalLED74();
$led->StrokeNumber('NO DATA AVAILABLE', LEDC_GREEN);
die();
}

$row = pg_fetch_array($r1);
$ns0 = $row['sensor0'];
$ns1 = $row['sensor1'];
$ns2 = $row['sensor2'];
$ns3 = $row['sensor3'];

$tcs0 = array();
$tcs1 = array();
$tcs2 = array();
$tcs3 = array();
$tfs0 = array();
$tfs1 = array();
$tfs2 = array();
$tfs3 = array();
$pcpn = array();
$Asubc = array();
$Atmpf = array();
Expand All @@ -148,12 +89,12 @@ function checker($v)

$lastp = 0;
for ($i = 0; $row = pg_fetch_array($result); $i++) {
$times[] = strtotime(substr($row["gvalid"], 0, 16));
$tcs0[] = checker($row["tcs0"]);
$tcs1[] = checker($row["tcs1"]);
$tcs2[] = checker($row["tcs2"]);
$tcs3[] = checker($row["tcs3"]);
$Asubc[] = checker($row["subc"]);
$times[] = strtotime(substr($row["valid"], 0, 16));
$tfs0[] = checker($row["tfs0"]);
$tfs1[] = checker($row["tfs1"]);
$tfs2[] = checker($row["tfs2"]);
$tfs3[] = checker($row["tfs3"]);
$Asubc[] = checker($row["subf"]);
$Atmpf[] = checker($row["tmpf"]);
$Adwpf[] = checker($row["dwpf"]);
$p = floatval($row["pcpn"]);
Expand All @@ -168,19 +109,19 @@ function checker($v)
$lastp = $p;
$freezing[] = 32;
}
pg_close($c0);
pg_close($rwisdb);

$nt = new NetworkTable("IA_RWIS");
$nt = new NetworkTable($network);
$cities = $nt->table;

// Create the graph. These two calls are always required
$graph = new Graph(650, 550, "example1");
$graph = new Graph(800, 600);
$graph->SetScale("datlin");
$graph->SetMarginColor("white");
$graph->SetColor("lightyellow");
if (max($pcpn) != "" && isset($_GET["pcpn"])) $graph->SetY2Scale("lin");
if (isset($limit)) $graph->SetScale("datlin", 25, 35);
$graph->img->SetMargin(40, 55, 105, 105);
$graph->img->SetMargin(40, 55, 105, 115);
//$graph->xaxis->SetFont(FS_FONT1,FS_BOLD);

if (max($pcpn) != "" && isset($_GET["pcpn"])) {
Expand All @@ -192,8 +133,7 @@ function checker($v)
$graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD, 12);

$graph->xaxis->SetTitle("Time Period: " . date('Y-m-d h:i A', $times[0]) . " thru " . date('Y-m-d h:i A', max($times)));
$graph->xaxis->SetTitleMargin(67);
$graph->xaxis->title->SetFont(FF_VERA, FS_BOLD, 12);
$graph->xaxis->SetTitleMargin(90);
$graph->xaxis->title->SetColor("brown");
$graph->xaxis->SetPos("min");
$graph->xaxis->SetLabelAngle(90);
Expand All @@ -203,25 +143,25 @@ function checker($v)
$graph->legend->SetLayout(LEGEND_VERT);

// Create the linear plot
$lineplot = new LinePlot($tcs0, $times);
$lineplot = new LinePlot($tfs0, $times);
$lineplot->SetLegend("0: " . $ns0);
$lineplot->SetColor("blue");
$lineplot->SetWeight(3);

// Create the linear plot
$lineplot2 = new LinePlot($tcs1, $times);
$lineplot2 = new LinePlot($tfs1, $times);
$lineplot2->SetLegend("1: " . $ns1);
$lineplot2->SetColor("pink");
$lineplot2->SetWeight(3);

// Create the linear plot
$lineplot3 = new LinePlot($tcs2, $times);
$lineplot3 = new LinePlot($tfs2, $times);
$lineplot3->SetLegend("2: " . $ns2);
$lineplot3->SetColor("gray");
$lineplot3->SetWeight(3);

// Create the linear plot
$lineplot4 = new LinePlot($tcs3, $times);
$lineplot4 = new LinePlot($tfs3, $times);
$lineplot4->SetLegend("3: " . $ns3);
$lineplot4->SetColor("purple");
$lineplot4->SetWeight(3);
Expand Down Expand Up @@ -264,26 +204,20 @@ function checker($v)
$tx2->SetPos(0.01, 0.11, 'left', 'top');
$tx2->SetFont(FF_FONT1, FS_NORMAL, 10);

require_once "../../../include/mlib.php";
$mySOb = array();


if ($mode == "hist") {
$ptext = "Historical Plot for dates:\n";
$tx3 = new Text($ptext . $plotTitle);
}
$ptext = "Historical Plot for dates:\n";
$tx3 = new Text($ptext);
$graph->AddText($tx1);
$graph->AddText($tx2);

// Add the plot to the graph
$graph->Add($fz);
if (max($tcs0) != "" && isset($_GET["s0"]))
if (max($tfs0) != "" && isset($_GET["s0"]))
$graph->Add($lineplot);
if (max($tcs1) != "" && isset($_GET["s1"]))
if (max($tfs1) != "" && isset($_GET["s1"]))
$graph->Add($lineplot2);
if (max($tcs2) != "" && isset($_GET["s2"]))
if (max($tfs2) != "" && isset($_GET["s2"]))
$graph->Add($lineplot3);
if (max($tcs3) != "" && isset($_GET["s3"]))
if (max($tfs3) != "" && isset($_GET["s3"]))
$graph->Add($lineplot4);
if (max($Asubc) != "" && isset($_GET["subc"]))
$graph->Add($lineplot5);
Expand Down
78 changes: 26 additions & 52 deletions htdocs/plotting/rwis/plot_soil.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,38 @@
$smonth = get_int404("smonth", date("m"));
$sday = get_int404("sday", date("d"));
$days = get_int404("days", 2);
$network = isset($_GET["network"]) ? xssafe($_GET["network"]) : "IA_RWIS";
$station = isset($_GET['station']) ? xssafe($_GET["station"]) : "";
$mode = isset($_GET["mode"]) ? xssafe($_GET["mode"]) : "rt";

$sts = time() - (3. * 86400.);
$ets = time();

$data = array();
$times = array();
for ($i = 0; $i < 15; $i++) {
$data["s{$i}temp"] = array();
$depths = Array(1, 3, 6, 9, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72);
$data = array();
// initialize the data array for each depth
foreach($depths as $depth){
$data["s{$depth}temp"] = array();
}

/** Lets assemble a time period if this plot is historical */
if ($mode != 'rt') {
$sts = mktime(0, 0, 0, $smonth, $sday, $syear);
$ets = $sts + ($days * 86400.0);

$dbconn = iemdb('rwis');
$rs = pg_prepare($dbconn, "SELECT", "SELECT * from alldata_soil
WHERE station = $1 and valid > $2 and valid < $3 ORDER by valid ASC");
$rs = pg_execute($dbconn, "SELECT", array(
$station,
date("Y-m-d H:i", $sts), date("Y-m-d H:i", $ets)
));

for ($i = 0; $row = pg_fetch_array($rs); $i++) {
$times[] = strtotime(substr($row["valid"], 0, 16));
for ($j = 0; $j < 15; $j++) {
$data["s{$j}temp"][] = $row["s{$j}temp"];
}
$sts = mktime(0, 0, 0, $smonth, $sday, $syear);
$ets = $sts + ($days * 86400.0);

$dbconn = iemdb('rwis');
$rs = pg_prepare($dbconn, "SELECT", "SELECT * from alldata_soil
WHERE station = $1 and valid > $2 and valid < $3 ORDER by valid ASC");
$rs = pg_execute($dbconn, "SELECT", array(
$station,
date("Y-m-d H:i", $sts), date("Y-m-d H:i", $ets)
));

for ($i = 0; $row = pg_fetch_array($rs); $i++) {
$times[] = strtotime(substr($row["valid"], 0, 16));
foreach ($depths as $j) {
$data["s{$j}temp"][] = $row["tmpf_{$j}in"];
}
pg_close($dbconn);
} else {
$dbconn = iemdb('iem');
$rs = pg_prepare($dbconn, "SELECT", "SELECT d.* from
rwis_soil_data_log d, rwis_locations l
WHERE l.id = d.location_id and l.nwsli = $1
ORDER by valid ASC");
$rs = pg_execute($dbconn, "SELECT", array($station));
$lts = 0;
for ($i = 0; $row = pg_fetch_array($rs); $i++) {
$ts = strtotime(substr($row["valid"], 0, 16));
if ($lts != $ts) {
$times[] = $ts;
$lts = $ts;
}
$data["s" . $row["sensor_id"] . "temp"][] = $row["temp"];
}
pg_close($dbconn);
}
pg_close($dbconn);

require_once "../../../include/jpgraph/jpgraph.php";
require_once "../../../include/jpgraph/jpgraph_line.php";
Expand All @@ -72,7 +54,7 @@
die();
}

$nt = new NetworkTable("IA_RWIS");
$nt = new NetworkTable($network);
$cities = $nt->table;

// Create the graph. These two calls are always required
Expand All @@ -91,27 +73,19 @@

//$graph->xaxis->SetTitle("Time Period:");
$graph->xaxis->SetTitleMargin(67);
$graph->xaxis->title->SetFont(FF_VERA, FS_BOLD, 12);
$graph->xaxis->title->SetColor("brown");
$graph->xaxis->SetPos("min");
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->SetLabelFormatString("M-j h A", true);

$graph->legend->Pos(0.01, 0.01);
$graph->legend->SetLayout(LEGEND_VERT);
$graph->legend->SetColumns(3);

$labels = array(1, 3, 6, 9, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72);
$colors = array(
"green", "black", "blue", "red",
"purple", "tan", "pink", "lavender", "teal", "maroon", "brown", "yellow",
"skyblue", "skyblue", "white"
);

for ($j = 0; $j < 15; $j++) {
foreach ($depths as $j) {
// Create the linear plot
$lineplot = new LinePlot($data["s{$j}temp"], $times);
$lineplot->SetLegend($labels[$j]);
$lineplot->SetColor($colors[$j]);
$lineplot->SetLegend($j);
$lineplot->SetWeight(3);
$graph->add($lineplot);
}
Expand Down
Loading

0 comments on commit cd34763

Please sign in to comment.