National Weather Service (NWS) new API

NWS API has a bunch of changes coming 2017. The documentation is a little light right now, but it sounds like the old DWML/XML feeds are going away.  We will have to pull the data using the new api.weather.gov API. Here is what I have learned about this new API so far while replacing the digitalDWML FcstType feed for my Illustrated Weather tool.

If you are using a Weather Service XML feed today and need someone to update it to api.weather.gov, drop me an email.

National Weather Service API Streaming Setup

To stream anything from https://api.weather.gov you must first set the stream header to include the Accept, Version and User-Agent. If you do not set the header the stream will return empty. NWS asks that you include your website and email address in the User-Agent so the they can contact you if need be.

$UserAgent = "YOURWEBSITE (your@emailaddress.com)";

// HTTP stream options:
$opts = array(
 'http'=>array(
 'method'=>"GET",
 'header'=>"Accept: application/geo+json;version=1\r\n" .
 "User-agent: $UserAgent\r\n"
 )
);
$context = stream_context_create($opts);

After the header is set you can stream the /points/lat,lng URL into a json object and pull forecastGridData, which is the /gridpoints/office/x/y URL. If you do not need a detailed gridded forecast take a look at the /points/lat,lng/forecast/ or /points/lat,lng/forecast/hourly in the documentation.

$lat = 43.0204;
$lng = -71.6002;
$pointMetaUrl = "https://api.weather.gov/points/$lat,$lng";
$pointMetaFile = file_get_contents($pointMetaUrl, false, $context);
$pointMetaArray = json_decode($pointMetaFile, true);
$forecastGridData = $pointMetaArray["properties"]["forecastGridData"];

Now stream the detailed gridded forecast using the forecastGridData /gridpoints/office/x/y URL. Then start pulling the data you want.

$gridDataFile = file_get_contents($forecastGridData, false, $context);
$gridDataArray = json_decode($gridDataFile, true);
$temp = $gridDataArray["properties"]["temperature"]["values"];

Just about all weather elements returned, like temperature, are an array of arrays including a time and value (e.g. temperature). The time format is ISO 8601, plus a PHP  DateInterval. “PT2H”  is a 2 hour interval.  Temperatures are in Celsius.

"values": [
                {
                    "validTime": "2017-02-17T08:00:00+00:00/PT2H",
                    "value": -6.1111111111111
                },

Now all I have to do is retool my Illustrated Weather functions to deal with this new format. UGH!

Published by

Profile photo of Kimball

Kimball

Kimball is a website designer and developer in Goffstown, NH.

One comment on:
“National Weather Service (NWS) new API”

Leave a Reply

Your email address will not be published. Required fields are marked *