<?php
require_once "../../config/settings.inc.php";
define("IEM_APPID", 8);
require_once "../../include/myview.php";
require_once "../../include/database.inc.php";
require_once "../../include/forms.php";
require_once "../../include/iemprop.php";

$asos_archive_end = get_iemprop("asos2archive_last");
$t = new MyView();
$t->iem_resource = "ASOS";
$t->iemss = True;
$t->headextra = <<<EOM
<link rel="stylesheet" href="download.css">
EOM;

$network = isset($_GET['network']) ? xssafe($_GET['network']): "IA_ASOS";
$station = "";
$t->title = "Download ASOS/AWOS/METAR Data";

$netselect = selectNetworkType("ASOS", $network);

$y1select = yearSelect(1900, date("Y"), "year1");
$y2select = yearSelect(1900, date("Y"), "year2");

$m1select = monthSelect(1, "month1");
$m2select = monthSelect(date("m"), "month2");

$d1select = daySelect(1, "day1");
$d2select = daySelect(date("d"), "day2");

$ar = Array(
    "Etc/UTC" => "Coordinated Universal Time (UTC)",
    "America/New_York" => "America/New_York (EST/EDT)",
    "America/Chicago" => "America/Chicago (CST/CDT)",
    "America/Denver" => "America/Denver (MST/MDT)",
    "America/Los_Angeles" => "America/Los_Angeles (PST/PDT)",
    "America/Anchorage" => "America/Anchorage (AKST/AKDT)",
);

$tzselect = make_select("tz", "Etc/UTC", $ar);

$t->content = <<<EOM


<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="/ASOS">ASOS Network</a></li>
    <li class="breadcrumb-item active" aria-current="page">ASOS-AWOS-METAR Data Download</li>
  </ol>
</nav>

<div class="row">
<div class="col-lg-8 col-md-7">

<p>The IEM maintains an ever growing archive of automated airport weather
observations from around the world!  These observations are typically called
'ASOS' or sometimes 'AWOS' sensors.  A more generic term may be METAR data,
which is a term that describes the format the data is transmitted as. If you
don't get data for a request, please feel free to
<a href="/info/contacts.php">contact us</a> for help. The IEM also has a one
minute interval dataset for <a href="asos/1min.phtml">US ASOS</a> (2000-)
and <a href="awos/1min.php">Iowa AWOS</a> (1995-2011) sites.  This archive
simply provides the as-is collection of historical observations, very little
quality control is done.  More details on this dataset are
<a href="/info/datasets/metar.html">here</a>.</p>

<p><strong>Data Sources:</strong> The data made available on this page is sourced
from a number of places including: <a href="https://www.unidata.ucar.edu/projects/#idd">Unidata IDD</a>,
<a href="https://www.ncei.noaa.gov/products/land-based-station/integrated-surface-database">NCEI ISD</a>, and
<a href="https://madis.ncep.noaa.gov/madis_OMO.shtml">MADIS One Minute ASOS</a>.</p>

</div>
<div class="col-lg-4 col-md-5">

<p><strong>Tools/Libaries</strong></p>

<p><strong>Python Script Examples</strong>
<br /><a href="https://github.com/akrherz/iem/blob/main/scripts/asos/iem_scraper_example.py">fetch by network</a>
<br /><a href="https://github.com/akrherz/iem/blob/main/scripts/asos/iem_scraper_example2.py">selectively fetch</a>
</p>

<p><strong>R Script Examples</strong>
<br />A community user has contributed <a href="https://github.com/realmiketalbot/R-scripts/blob/master/iem_scraper_example.
r">R language</a> version of the python script.
There is also a <a href="https://docs.ropensci.org/riem/">riem R package</a>
 allowing for easy access to this archive.</p>

</div>
</div>

<p>This archive contains processed observations up until
<code>{$asos_archive_end}</code>. Data
is synced from the real-time ingest every 10 minutes.</p>

<p>
<a href="/cgi-bin/request/asos.py?help" class="btn btn-primary"><i class="bi bi-file-text" aria-hidden="true"></i> Backend documentation</a> exists for those that
wish to script against this service.
</p>

<form name="netselect" class="mb-3">
  <label for="network-select" class="form-label"><strong>Select Network</strong></label>
  {$netselect}
  <button type="submit" class="btn btn-outline-primary btn-sm ms-2">Switch to Network</button>
</form>

<form target="_blank" method="GET" action="/cgi-bin/request/asos.py" name="iemss">
  <input type="hidden" name="network" value="{$network}">
  <div class="form2url"></div>
  <div class="row">
    <div class="col-lg-7 col-md-6">

<h4>1) Select Station/Network by clicking on location: </h4>

<p>If you select no stations, you can download up to a
<strong>24 hour period</strong> of all
data available from this archive!  If you select stations, the aggregate
request limit is <strong>1,000 station years</strong> worth of data.</p>


<div id="iemss" data-network="{$network}" data-name="station" data-supports-all="0"></div>

</div>
<div class="col-lg-5 col-md-6">

<h4>2) Select From Available Data:</h4>

<p><strong>Note:</strong> Precipitation data is unavailable for non-US sites.
The Heat Index/Wind Chill value retroactively use current NWS equations.</p>

<select name="data" size="10" multiple class="form-select">
    <option value="all" SELECTED>All Available</option>
    <option value="tmpf">Air Temperature [F]</option>
    <option value="tmpc">Air Temperature [C]</option>
    <option value="dwpf">Dew Point [F]</option>
    <option value="dwpc">Dew Point [C]</option>
    <option value="relh">Relative Humidity [%]</option>
    <option value="feel">Heat Index/Wind Chill [F]</option>
    <option value="drct">Wind Direction </option>
    <option value="sknt">Wind Speed [knots]</option>
    <option value="sped">Wind Speed [mph]</option>
    <option value="alti">Altimeter [inches]</option>
    <option value="mslp">Sea Level Pressure [mb]</option>
    <option value="p01m">1 hour Precipitation [mm]</option>
    <option value="p01i">1 hour Precipitation [inch]</option>
    <option value="vsby">Visibility [miles]</option>
    <option value="gust">Wind Gust [knots]</option>
    <option value="gust_mph">Wind Gust [mph]</option>
    <option value="skyc1">Cloud Coverage Level 1</option>
    <option value="skyc2">Cloud Coverage Level 2</option>
    <option value="skyc3">Cloud Coverage Level 3</option>
    <option value="skyl1">Cloud Height Level 1 [ft]</option>
    <option value="skyl2">Cloud Height Level 2 [ft]</option>
    <option value="skyl3">Cloud Height Level 3 [ft]</option>
    <option value="wxcodes">Present Weather Code(s)</option>
    <option value="ice_accretion_1hr">Ice Accretion 1 Hour</option>
    <option value="ice_accretion_3hr">Ice Accretion 3 Hour</option>
    <option value="ice_accretion_6hr">Ice Accretion 6 Hour</option>
    <option value="peak_wind_gust">Peak Wind Gust [knots]</option>
    <option value="peak_wind_gust_mph">Peak Wind Gust [MPH]</option>
    <option value="peak_wind_drct">Peak Wind Direction [deg]</option>
    <option value="peak_wind_time">Peak Wind Time</option>
    <option value="snowdepth">Snow Depth (4-group) [inch]</option>
    <option value="metar">Raw METAR</option>
</select>


<br><br>
<h4>3) Select Date Range:</h4>
<p><strong>Note:</strong> These dates define timestamps starting at
midnight of the selected timezone.  The start date is inclusive and the
end date is exclusive.</p>

<table class="table table-bordered table-sm align-middle">
<tr><th>Start Date:</th><td>{$y1select} {$m1select} {$d1select}</td></tr>
<tr><th>End Date:</th><td>{$y2select} {$m2select} {$d2select}</td></tr>
</table>

<h4>4) Timezone of Observation Times:</h4>
<p><i>The following options are available for how the observation time is
    presented.</i></p>
{$tzselect}

<h4>5) Download Options:</h4>

<p><strong>Data Format:</strong>
<select name="format" class="form-select form-select-sm w-auto d-inline-block ms-2">
<option value="onlycomma">Comma Delimited</option>
<option value="onlytdf">Tab Delimited</option>
<option value="comma">Comma Delimited (With DEBUG headers)</option>
<option value="tdf">Tab Delimited (With DEBUG headers)</option>
</select></p>

<p><strong>Include Latitude + Longitude?</strong>
<select name="latlon" class="form-select form-select-sm w-auto d-inline-block ms-2">
  <option value="no">No
  <option value="yes">Yes
</select></p>

<p><strong>Include Elevation (meters)?</strong>
<select name="elev" class="form-select form-select-sm w-auto d-inline-block ms-2">
  <option value="no">No</option>
  <option value="yes">Yes</option>
</select></p>

<p><strong>How to represent missing data?</strong>
<select name="missing" class="form-select form-select-sm w-auto d-inline-block ms-2">
  <option value="M">Use 'M'</option>
  <option value="null">Use 'null'</option>
  <option value="empty">Use blank/empty string</option>
</select></p>

<p><strong>How to represent Trace reports?</strong>
<select name="trace" class="form-select form-select-sm w-auto d-inline-block ms-2">
  <option value="T">Use 'T'</option>
  <option value="null">Use 'null'</option>
  <option value="empty">Use blank/empty string</option>
  <option value="0.0001">Use float 0.0001</option>
</select></p>

<p>
<select name="direct" class="form-select form-select-sm w-auto d-inline-block ms-2">
  <option value="no">View result data in web browser</option>
  <option value="yes">Save result data to file on computer</option>
</select></p>

<h4>6) Limit Report Types</h4>

<p>See <a href="/onsite/news.phtml?id=1434">news item</a> on recent changes
made for report types.  When in doubt, pick both routine and specials.</p>

<div class="form-check">
  <input class="form-check-input" type="checkbox" name="report_type" value="1" id="rt1">
  <label class="form-check-label" for="rt1">MADIS HFMETAR / 5 Minute ASOS</label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" name="report_type" value="3" id="rt3" checked>
  <label class="form-check-label" for="rt3">Routine / Once Hourly</label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" name="report_type" value="4" id="rt4" checked>
  <label class="form-check-label" for="rt4">Specials</label>
</div>

<h4>7) Finally, get Data:</h4>

  <button type="submit" class="btn btn-success">Get Data</button>
  <button type="reset" class="btn btn-secondary ms-2">Reset</button>

         </div>
         </div>

</form>

<p><strong>Download Variable Description</strong>
    <br /><a href="https://www.weather.gov/media/asos/aum-toc.pdf">ASOS User's Guide</a>
        has detailed information about these data variables. The value "M" represents
        either value that was reported as missing or a value that was set to missing
        after meeting some general quality control check, or a value that was never
        reported by the sensor.  The METAR format makes it difficult to determine
        which of the three cases may have happened.</p>

<dl class="dl-horizontal">
<dt>station:</dt>
<dd>three or four character site identifier</dd>
<dt>valid:</dt>
<dd>timestamp of the observation</dd>
<dt>tmpf:</dt>
<dd>Air Temperature in Fahrenheit, typically @ 2 meters</dd>
<dt>dwpf:</dt>
<dd>Dew Point Temperature in Fahrenheit, typically @ 2 meters</dd>
<dt>relh:</dt>
<dd>Relative Humidity in %</dd>
<dt>drct:</dt>
<dd>Wind Direction in degrees from *true* north</dd>
<dt>sknt:</dt>
<dd>Wind Speed in knots	</dd>
<dt>p01i:</dt>
<dd>One hour precipitation for the period from the observation time
to the time of the previous hourly precipitation reset.  This varies slightly
by site. Values are in inches. This value may or may not contain frozen
        precipitation melted by some device on the sensor or estimated
        by some other means.  Unfortunately, we do not know of an
        authoritative database denoting which station has which sensor.</dd>
<dt>alti:</dt>
<dd>Pressure altimeter in inches</dd>
<dt>mslp:</dt>
<dd>Sea Level Pressure in millibar</dd>
<dt>vsby:</dt>
<dd>Visibility in miles</dd>
<dt>gust:</dt>
<dd>Wind Gust in knots</dd>
<dt>skyc1:</dt>
<dd>Sky Level 1 Coverage</dd>
<dt>skyc2:</dt>
<dd>Sky Level 2 Coverage</dd>
<dt>skyc3:</dt>
<dd>Sky Level 3 Coverage</dd>
<dt>skyc4:</dt>
<dd>Sky Level 4 Coverage</dd>
<dt>skyl1:</dt>
<dd>Sky Level 1 Altitude in feet</dd>
<dt>skyl2:</dt>
<dd>Sky Level 2 Altitude in feet</dd>
<dt>skyl3:</dt>
<dd>Sky Level 3 Altitude in feet</dd>
<dt>skyl4:</dt>
<dd>Sky Level 4 Altitude in feet</dd>
<dt>wxcodes:</dt>
<dd>Present Weather Codes (space seperated)</dd>
<dt>feel:</dt>
<dd>Apparent Temperature (Wind Chill or Heat Index) in Fahrenheit</dd>
<dt>ice_accretion_1hr:</dt>
<dd>Ice Accretion over 1 Hour (inches)</dd>
<dt>ice_accretion_3hr:</dt>
<dd>Ice Accretion over 3 Hours (inches)</dd>
<dt>ice_accretion_6hr:</dt>
<dd>Ice Accretion over 6 Hours (inches)</dd>
<dt>peak_wind_gust:</dt>
<dd>Peak Wind Gust (from PK WND METAR remark) (knots)</dd>
<dt>peak_wind_drct:</dt>
<dd>Peak Wind Gust Direction (from PK WND METAR remark) (deg)</dd>
<dt>peak_wind_time:</dt>
<dd>Peak Wind Gust Time (from PK WND METAR remark)</dd>
<dt>metar:</dt>
<dd>unprocessed reported observation in METAR format</dd>
</dl>

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