<?php
define("IEM_APPID", 105);
require_once '../../config/settings.inc.php';
require_once '../../include/myview.php';

$daylink = sprintf("%s0000", date("Ymd"));
$mhourlink = gmdate("YmdH", time() - 3600) . "00";

$cfg = array();

$cfg["air_temperature"] = array(
    "name" => "Air Temperature",
    "prods" => array(
        "2m_air_temperature" => array(
            "label" => "2m Air Temperature",
            "timemachine" => "?product=115",
            "img" => "/data/iowa_tmpf.png",
            "desc" => "Analysis of air temperature readings from airport " .
                "weather stations."
        ),
        "2m_wetbulb_temperature" => array(
            "label" => "2m Wet Bulb Temperature",
            "timemachine" => "?product=129",
            "img" => "/data/iowa_wetbulb.png",
            "desc" => "Analysis of wet bulb temperature readings from airport " .
                "weather stations."
        ),
        "12z_low" => array(
            "label" => "12 UTC Low Temperature",
            "timemachine" => "?product=63",
            "img" => "/data/summary/iowa_asos_12z_low.png",
            "desc" => "Based on manually computed minimums and 12 hour " .
                "minimums, when available, the plot shows low temperatures at " .
                "12 UTC."
        ),
        "0z_high" => array(
            "label" => "00 UTC High Temperature",
            "timemachine" => null,
            "img" => "/data/summary/iowa_asos_0z_high.png",
            "desc" => <<<EOM
This plot presents a computed high temperature between the 12z and 0z time
period for the date.  Available 6-hour max values at 18 and 00 UTC are
included within the calculation.
EOM
        ),
        "today_high" => array(
            "label" => "Today's High Temperature",
            "timemachine" => "?product=116",
            "img" => "/data/summary/iowa_asos_high.png",
            "desc" => "IEM computed calendar day high temperature."
        ),
        "iowa_heat_index" => array(
            "label" => "Heat Index",
            "timemachine" => "?product=10",
            "img" => "/data/heat.gif",
            "desc" => "Current heat index values computed using present day " .
                "NWS equation."
        ),
        "iowa_wc_index" => array(
            "label" => "Wind Chill Index",
            "timemachine" => "?product=25",
            "img" => "/data/wcht.gif",
            "desc" => "Current wind chill index values computed using present " .
                "day NWS equation."
        ),
        "iowa_wc_min" => array(
            "label" => "Today's Min Wind Chill Index",
            "timemachine" => "?product=117",
            "img" => "/data/summary/iowa_min_windchill.png",
            "desc" => "This plot presents the minimum wind chill temperature" .
                " for the local calendar day."
        ),
        "iowa_tw" => array(
            "label" => "Surface Temps &amp; Winds",
            "timemachine" => "?product=23",
            "img" => "/data/surfaceTW0.gif",
            "desc" => "Legacy GEMPAK plot showing temp analysis and winds."
        ),
        "iowa_te" => array(
            "label" => "Surface Theta E &amp; Winds",
            "timemachine" => "?product=22",
            "img" => "/data/surfaceTE0.gif",
            "desc" => "Legacy GEMPAK plot showing surface theta E and winds."
        ),
        "iowa_divergence" => array(
            "label" => "Surface Divergence",
            "timemachine" => "?product=18",
            "img" => "/data/surfaceDIV0.gif",
            "desc" => "Legacy GEMPAK plot showing surface divergence."
        ),
        "iowa_annual" => array(
            "label" => "Annual Average Temperature",
            "timemachine" => null,
            "link" => "/plotting/coop/climate_fe.php",
            "desc" => ""
        ),
        "coop_freezing" => array(
            "label" => "COOP Freezing Statistics",
            "timemachine" => null,
            "link" => "/COOP/freezing.php",
            "desc" => ""
        ),
        "gdd48_jan1" => array(
            "label" => "Accumulated Growing Degree Days (base=48)",
            "timemachine" => null,
            "img" => "/data/summary/gdd48_jan1.png",
            "desc" => "Growing degree days (base 48, ceiling 86) since 1 Jan."
        ),
        "gdd50_jan1" => array(
            "label" => "Accumulated Growing Degree Days (base=50)",
            "timemachine" => null,
            "img" => "/data/summary/gdd_jan1.png",
            "desc" => "Growing degree days (base 50, ceiling 86) since 1 Jan."
        ),
        "gdd52_jan1" => array(
            "label" => "Accumulated Growing Degree Days (base=52)",
            "timemachine" => null,
            "img" => "/data/summary/gdd52_jan1.png",
            "desc" => "Growing degree days (base 52, ceiling 86) since 1 Jan."
        ),
        "gdd50_fx" => array(
            "label" => "Forecast 7 Day Growing Degree Days Departure (base=50)",
            "timemachine" => null,
            "img" => "/data/summary/cb_ndfd_7day_gdd.png",
            "desc" => <<<EOM
Growing degree days (base 50, ceiling 86) seven day forecast departure
based on NWS NDFD and a climatology of daily GDDs.
EOM
        ),
        "daily_departures" => array(
            "label" => "Yearly Departures from Average",
            "timemachine" => null,
            "link" => "/plotting/auto/?q=99",
            "img" => "/plotting/auto/plot/99/img.png",
            "desc" => ""
        ),
        "monthly_mean_t" => array(
            "label" => "Monthly Average Temperature",
            "timemachine" => null,
            "img" => "/data/summary/mon_mean_T.png",
            "desc" => "Monthly mean temperature (high+low)/2."
        ),
        "monthly_sdd" => array(
            "label" => "Accumulated Stress Degree Days (base=86)",
            "timemachine" => null,
            "img" => "/data/summary/sdd_mon.png",
            "desc" => "Stress degree days for this month.."
        ),
        "monthly_gdd" => array(
            "label" => "Accumulated Growing Degree Days (base=50)",
            "timemachine" => null,
            "img" => "/data/summary/gdd_mon.png",
            "desc" => "Growing degree days (base 50, ceiling 86) for this month"
        ),
    )
);

$cfg["humidity"] = array(
    "name" => "Air Humidity",
    "prods" => array(
        "relative_humidity" => array(
            "label" => "Relative Humidity",
            "timemachine" => null,
            "img" => "/data/relh.gif",
            "desc" => "Relative humidity is the percentage of capacity of the " .
                "air to hold water vapor."
        ),
        "iowa_md" => array(
            "label" => "Surface Moisture Divergence &amp; Winds",
            "timemachine" => "?product=21",
            "img" => "/data/surfaceMD0.gif",
            "desc" => "Legacy plot of surface moisture divergence."
        ),
        "iowa_dw" => array(
            "label" => "Surface Dew Points &amp; Winds",
            "timemachine" => "?product=19",
            "img" => "/data/surfaceDW0.gif",
            "desc" => "Legacy plot of surface winds and dew point."
        ),
    )
);

$cfg["precipitation"] = array(
    "name" => "Precipitation",
    "prods" => array(
        "today_mrms_iowa" => array(
            "label" => "MRMS Today's Precip",
            "timemachine" => "?product=45&timestamp={$daylink}",
            "img" => "/data/iowa_q2_1d.png",
            "desc" => <<<EOM
Multi-RADAR Multi-Sensor (MRMS) summary of hourly precipitation estimates for
a central time zone calendar date.
EOM
        ),
        "hour_mrms_iowa" => array(
            "label" => "MRMS Hourly Precip",
            "timemachine" => "?product=46&timestamp={$daylink}",
            "img" => "/data/iowa_q2_1h.png",
            "desc" => <<<EOM
Multi-RADAR Multi-Sensor (MRMS) hourly precipitation esimates.
EOM
        ),
        "3hour_mrms_iowa" => array(
            "label" => "MRMS Three Hourly Precip",
            "timemachine" => "?product=47&timestamp={$daylink}",
            "img" => "/data/iowa_q2_3h.png",
            "desc" => <<<EOM
Multi-RADAR Multi-Sensor (MRMS) 3 hour precipitation estimates computed by
combining hourly esimates.
EOM
        ),
        "6hour_mrms_iowa" => array(
            "label" => "MRMS Six Hourly Precip",
            "timemachine" => "?product=48&timestamp={$daylink}",
            "img" => "/data/iowa_q2_6h.png",
            "desc" => <<<EOM
Multi-RADAR Multi-Sensor (MRMS) 6 hour precipitation estimates computed by
combining hourly esimates.
EOM
        ),
        "month_mrms_iowa" => array(
            "label" => "MRMS This Month Precip",
            "timemachine" => "?product=62&timestamp={$daylink}",
            "img" => "/data/summary/iowa_mrms_q3_month.png",
            "desc" => <<<EOM
Multi-RADAR Multi-Sensor (MRMS) monthly precipitation estimates computed by
combining daily estimates.
EOM
        ),
        "today_asos_iowa" => array(
            "label" => "Iowa ASOS Today's Precip",
            "timemachine" => null,
            "img" => "/data/summary/today_prec.png",
            "desc" => <<<EOM
IEM computed calendar day precipitation total based on available hourly reports
EOM
        ),
        "7day_asos_iowa" => array(
            "label" => "Iowa ASOS/AWOS 7 Day Precip",
            "timemachine" => "?product=84",
            "img" => "/data/summary/7day/ia_precip.png",
            "desc" => "This plot presents a summary of calendar day precip" .
                " obs over the past 7 days."
        ),
        "31day_asos_iowa" => array(
            "label" => "Iowa ASOS/AWOS 31 Day Precip",
            "timemachine" => "?product=85",
            "img" => "/data/summary/31day/ia_precip.png",
            "desc" => "This plot presents a summary of calendar day precip" .
                " obs over the past 31 days."
        ),
        "91day_asos_iowa" => array(
            "label" => "Iowa ASOS/AWOS 91 Day Precip",
            "timemachine" => "?product=86",
            "img" => "/data/summary/91day/ia_precip.png",
            "desc" => "This plot presents a summary of calendar day precip" .
                " obs over the past 91 days."
        ),
        "today_stage4_us" => array(
            "label" => "Stage IV Today's Precip",
            "timemachine" => "?product=43&timestamp={$daylink}",
            "img" => "/data/conus_stage4_1d.png",
            "desc" => <<<EOM
IEM totaling of available NCEP Stage IV hourly precipitation estimates.
EOM
        ),
        "today_stage4_midwest" => array(
            "label" => "Stage IV Today's Precip",
            "timemachine" => "?product=42&timestamp={$daylink}",
            "img" => "/data/midwest_stage4_1d.png",
            "desc" => <<<EOM
IEM totaling of available NCEP Stage IV hourly precipitation for the central
time zone calendar day.
EOM
        ),
        "hour_stage4_midwest" => array(
            "label" => "Stage IV Hourly Precip",
            "timemachine" => "?product=40&timestamp={$mhourlink}",
            "img" => "/data/midwest_stage4_1h.png",
            "desc" => <<<EOM
IEM computed total of NCEP Stage IV precipitation over an hour.
EOM
        ),
        "today_stage4_iowa" => array(
            "label" => "Stage IV Hourly Precip",
            "timemachine" => "?product=39&timestamp={$mhourlink}",
            "img" => "/data/iowa_stage4_1h.png",
            "desc" => <<<EOM
IEM totaling of available NCEP Stage IV hourly precipitation for the central
time zone calendar day.
EOM
        ),
        "midwest_coop_contour" => array(
            "label" => "COOP Precipitation Analysis",
            "timemachine" => "?product=38",
            "img" => "/data/midwest_coop_12z_precip.png",
            "desc" => "Contour plot of available COOP reports."
        ),
        "iowa_coop_contour" => array(
            "label" => "COOP Precipitation Analysis",
            "timemachine" => "?product=37",
            "img" => "/data/iowa_coop_12z_precip.png",
            "desc" => "Contour plot of available COOP reports."
        ),
        "iowa_flood_center" => array(
            "label" => "Iowa Flood Center",
            "timemachine" => "?product=82",
            "img" => "/data/iowa_ifc_1d.png",
            "desc" => <<<EOM
IEM computed daily total of available Iowa Flood Center precipitation estimates.
EOM
        ),
        "year_departure" => array(
            "label" => "Departure from Average",
            "timemachine" => null,
            "img" => "/data/summary/year/diff.png",
            "desc" => ("This map presents the difference between the estimated precipitation " .
                "total for this year vs the NCEI Climatology for 1991-2020."
            )
        ),
        "year_stage4" => array(
            "label" => "Stage IV Yearly Total",
            "timemachine" => null,
            "img" => "/data/summary/year/stage4obs.png",
            "desc" => "Yearlong total of NCEP Stage IV Precipitation."
        ),
        "year_average" => array(
            "label" => "Yearly Average Total",
            "timemachine" => null,
            "img" => "/data/summary/year/normals.png",
            "desc" => "IEM computed precipitation average for the year."
        ),
        "4mon_accum" => array(
            "label" => "4 Month Estimated accumulation",
            "timemachine" => null,
            "img" => "/data/summary/4mon_stage4obs.png",
            "desc" => "IEM computed 4 month precipitation from NCEP Stage IV."
        ),
        "4mon_climate" => array(
            "label" => "4 Month Climatology",
            "timemachine" => null,
            "img" => "/data/summary/4mon_normals.png",
            "desc" => "IEM computed 4 month climatology based on long term climate sites."
        ),
        "4mon_diff" => array(
            "label" => "4 Month Departures",
            "timemachine" => null,
            "img" => "/data/summary/4mon_diff.png",
            "desc" => "IEM computed departure from average based on long term climate sites."
        ),
        "asos_month_accum" => array(
            "label" => "Monthly Accumulation ASOS",
            "timemachine" => null,
            "img" => "/data/summary/month_prec.png",
            "desc" => "IEM computed monthly precipitation total from ASOS."
        ),
        "coop_month_accum" => array(
            "label" => "Monthly Accumulation [COOP]",
            "timemachine" => null,
            "img" => "/data/coopMonthPlot.png",
            "desc" => "IEM computed monthly precipitation total from COOP."
        ),
    )
);

$cfg["pressure"] = array(
    "name" => "Pressure",
    "prods" => array(
        "iowa_altimeter" => array(
            "label" => "Iowa Altimeter",
            "timemachine" => null,
            "img" => "/data/mesonet_altm.gif",
            "desc" => "Pressure altimeter analysis."
        ),
    )
);

$cfg["snow"] = array(
    "name" => "Snow",
    "prods" => array(
        "seasonal_snowfall" => array(
            "label" => "Seasonal Snowfall",
            "timemachine" => null,
            "img" => "/data/summary/mw_season_snowfall.png",
            "desc" => "Seasonal snowfall total based on NWS CLI Sites."
        ),
        "monthly_snowfall" => array(
            "label" => "Monthly Snowfall",
            "timemachine" => null,
            "img" => "/data/coopMonthSPlot.gif",
            "desc" => "Monthly snowfall total from NWS COOP sites."
        ),
    )
);

$cfg["soil_temperature"] = array(
    "name" => "Soil Temperature",
    "prods" => array(
        "day1" => array(
            "label" => "Yesterday Average",
            "timemachine" => "?product=57",
            "img" => "/data/soilt_day1.png",
            "desc" => "IEM Soil Temperature analysis maps."
        ),
        "day2" => array(
            "label" => "Day 2 Average",
            "timemachine" => "?product=57",
            "img" => "/data/soilt_day2.png",
            "desc" => "IEM Soil Temperature analysis maps."
        ),
        "day3" => array(
            "label" => "Day 3 Average",
            "timemachine" => "?product=57",
            "img" => "/data/soilt_day3.png",
            "desc" => "IEM Soil Temperature analysis maps."
        ),
    )
);

$cfg["station_plot"] = array(
    "name" => "Station Plot",
    "prods" => array(
        "iem" => array(
            "label" => "IEM Mesoplot",
            "timemachine" => "?product=1",
            "img" => "/data/mesonet.gif",
            "desc" => "Iowa Mesonet station plot combining ASOS + RWIS."
        ),
        "mw" => array(
            "label" => "Midwest Mesoplot",
            "timemachine" => "?product=4",
            "img" => "/data/MWmesonet.gif",
            "desc" => "Midwest station plot of airport data."
        ),
    )
);

$cfg["sounding"] = array(
    "name" => "Sounding",
    "prods" => array(
        "dvn" => array(
            "label" => "Daveport RAOB",
            "timemachine" => null,
            "img" => "https://weather.ral.ucar.edu/upper/displayUpper.php?img=KDVN.png&endDate=-1&endTime=-1&duration=0",
            "desc" => "Recent sounding for Davenport."
        ),
        "oax" => array(
            "label" => "Omaha RAOB",
            "timemachine" => null,
            "img" => "https://weather.ral.ucar.edu/upper/displayUpper.php?img=KOAX.png&endDate=-1&endTime=-1&duration=0",
            "desc" => "Recent sounding for Omaha."
        ),
        "mpx" => array(
            "label" => "Minneapolis RAOB",
            "timemachine" => null,
            "img" => "https://weather.ral.ucar.edu/upper/displayUpper.php?img=KMPX.png&endDate=-1&endTime=-1&duration=0",
            "desc" => "Recent sounding for Minneapolis."
        ),
        "top" => array(
            "label" => "Topeka RAOB",
            "timemachine" => null,
            "img" => "https://weather.ral.ucar.edu/upper/displayUpper.php?img=KTOP.png&endDate=-1&endTime=-1&duration=0",
            "desc" => "Recent sounding for Topeka."
        ),
    )
);

$cfg["wind_speed"] = array(
    "name" => "Wind",
    "prods" => array(
        "10m_gust" => array(
            "label" => "10m Today's Peak Wind Gust",
            "timemachine" => "?product=16",
            "img" => "/data/summary/today_gust.png",
            "desc" => "Computed wind gust from ASOS stations."
        ),
        "wind_power" => array(
            "label" => "Wind Power Potential",
            "timemachine" => "?product=60",
            "img" => "/data/midwest/rtma_wind_power.png",
            "desc" => "An estimate of wind power based on NCEP RTMA."
        ),
        "iowa_frontogenesis" => array(
            "label" => "Surface Frontogenesis &amp; Winds",
            "timemachine" => "?product=18",
            "img" => "/data/surfaceFRNT0.gif",
            "desc" => "Legacy surface frontogenesis plot."
        ),
        "windroses" => array(
            "label" => "Wind Roses",
            "timemachine" => null,
            "link" => "/sites/windrose.phtml?station=DSM&network=IA_ASOS",
            "img" => "/onsite/windrose/IA_ASOS/DSM/DSM_yearly.png",
            "desc" => "These plots are a fancy way to show two dimensional " .
                "histograms of wind speed and direction.  The directions " .
                "shown are where the wind is blowing from and not toward. " .
                "This website contains tools that can generate these plots " .
                "on demand."
        ),
    )
);

$cfg["visibility"] = array(
    "name" => "Visibility",
    "prods" => array(
        "iowa_vsby" => array(
            "label" => "Iowa Visibility",
            "timemachine" => "?product=24",
            "img" => "/data/iowa_vsby.png",
            "desc" => "Analysis of visibility from ASOS stations."
        ),
    )
);

$pathinfo = isset($_SERVER["PATH_INFO"]) ? $_SERVER["PATH_INFO"] : null;
$maincontent = "";
$showmenu = TRUE;
$sectionlabel = "";
if ($pathinfo) {
    $arr = explode("/", $pathinfo);
    if (sizeof($arr) == 2) {
        $showmenu = FALSE;
        $sectionlabel = $arr[1];
        $section = $cfg[$arr[1]];
        $maincontent .= sprintf("<p><a href=\"/current/index.phtml\"" .
            " class=\"btn btn-outline-secondary\">Back to Current Weather</a></p>");
        $maincontent .= "<div class=\"row\">\n";
        $i = 0;
        foreach ($section["prods"] as $path => $product) {
            if ($i > 0 && $i % 3 == 0) {
                $maincontent .= "</div>\n<div class=\"row\">\n";
            }
            if (array_key_exists("link", $product)) {
                $maincontent .= sprintf(
                    "<div class=\"col-md-4 bg-light border rounded p-3\">" .
                        "<a href=\"%s\">" .
                        "<h4><i class=\"bi bi-link-45deg\"></i> %s</h4>" .
                        "<img src=\"%s\" class=\"img img-fluid\">" .
                        "</a></div>",
                    $product["link"],
                    $product["label"],
                    (array_key_exists("img", $product)) ? $product["img"] : "/images/iem_logo.svg"
                );
            } else {
                $maincontent .= sprintf(
                    "<div class=\"col-md-4 bg-light border rounded p-3\">" .
                        "<a href=\"/current/index.phtml/%s/%s\">" .
                        "<h4>%s</h4>" .
                        "<img src=\"%s\" class=\"img img-fluid\">" .
                        "</a></div>",
                    $arr[1],
                    $path,
                    $product["label"],
                    $product["img"]
                );
            }
            $i += 1;
        }
        $maincontent .= "</div>\n";
    } else if (sizeof($arr) == 3) {
        $showmenu = FALSE;
        $sectionlabel = $arr[1];
        $section = $cfg[$arr[1]];
        $maincontent .= sprintf(
            "<p><a href=\"/current/index.phtml/%s\"" .
                " class=\"btn btn-outline-secondary\">Back to %s</a></p>",
            $arr[1],
            $section["name"]
        );
        $product = $section["prods"][$arr[2]];
        $tm = "";
        if ($product["timemachine"]) {
            $tm = sprintf(
                "<p>An archive of this image is in the <a href=\"/timemachine/%s\" " .
                    "class=\"btn btn-success\"><i class=\"bi bi-clock\"></i> IEM Time Machine</a></p>",
                $product["timemachine"]
            );
        }
        $maincontent .= sprintf(
            "<h4>%s</h4>" .
                "%s" .
                "<img src=\"%s\" class=\"img img-fluid\">" .
                "<br /><p>%s</p>",
            $product["label"],
            $tm,
            $product["img"],
            $product["desc"]
        );
    }
}
// Build left hand menu
reset($cfg);
$menuhidden = ($showmenu) ? "" : " d-none d-md-block";
$menu = "";
foreach ($cfg as $path => $section) {
    $menu .= sprintf(
        "<li><a href=\"/current/index.phtml/%s\"" .
            " class=\"btn btn-outline-secondary w-100 mb-2%s\">%s</a></li>",
        $path,
        ($path == $sectionlabel) ? " btn-primary" : "",
        $section["name"]
    );
}

$t = new MyView();
$t->title = "Current Products";
$t->headextra = "<meta http-equiv=\"refresh\" content=\"60\">";

if ($maincontent == "") {
    $maincontent = <<<EOM
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Please select from the available data types to view IEM products.</p>
EOM;
}

$t->content = <<<EOM

<div class="row">
 <div class="col-md-3{$menuhidden}">

<div class="card">
  <div class="card-header">Current Weather</div>
  <div class="card-body">
    <ul class="list-unstyled">
     {$menu}
    </ul>
  </div>
</div>

</div><!-- ./col-md-3 -->

 <div class="col-md-9">
    {$maincontent}
 </div><!-- ./col-md-9 -->
</div><!-- ./row -->

EOM;
$t->render('full.phtml');
