Posts Tagged ‘weather’

Free Weather Forecast

Tuesday, April 8th, 2008
Posted in Web Development · Tags: , , , ,

I noticed on the National Weather Service’s website they now allow you to grab the forecast by REST, whereas before they only supported SOAP requests. This makes grabbing the forecast much simpler than it was before.

If you use SOAP there are many methods to grab the data, but if you use REST there are only 2. With REST you can either use DWMLgen which lets you get a little more specific information or NDFDgenByDay which is a little simpler and lets you pick either 12 hour or 24 hour increments (so you can get each day broken in half as in day and night or the full day). The response is sent back as XML for either method so you can format the data how you like.

For the example we are doing, we are going to keep it simple and use a single location, NDFDgenByDay and a 24 hourly period. We will also set it up so that we send the longitude and latitude for Austin, TX, the current date as the start date and request 7 days worth of data.
(more…)

Current Weather Conditions

Friday, February 1st, 2008
Posted in Web Development · Tags: , , ,

The National Weather Service provides Current Weather Conditions XML files for many cities in the US.

Here is some code for PHP5 (it uses SimpleXML) on doing this (assuming you’ve already grabbed the XML file from their site and have a way to cache it). It even has some code calculating the sunrise, sunset and civil twilight which PHP5 can do with date_sun_info() and since the XML file gives the date, latitude and longitude, you can calculate it dynamically.

//PHP5
if (file_exists('KADS.xml'))
{
$xmlstr = file_get_contents('KADS.xml');
$xml = simplexml_load_string($xmlstr);
$datetime = strtotime($xml->observation_time_rfc822);
$latitude = (float)$xml->latitude;
$longitude = (float)$xml->longitude;
$sun_info = date_sun_info(date('Y-n-j', $datetime), $latitude, $longitude);//calculate sunrise,sunset,...
echo '<ul id="current_conditions">';
echo '<li>' . date('l, F j, Y', $datetime) . '</li>';
echo '<li class="weather_loc">' . $xml->location . '</li>';
echo '<!--' . $xml->observation_time . '-->';
echo '<li><img src="' . $xml->icon_url_base . $xml->icon_url_name . '" alt="' . $xml->weather . '" /></li>';
echo '<li class="weather_temp"><strong>' . $xml->temp_f . '&deg;' . '</strong></li>';
echo '<li class="weather_sum"><strong>' . $xml->weather . '</strong></li>';
echo '<li>Wind: <strong>' . $xml->wind_dir . ' ' . round($xml->wind_mph) . ' mph</strong></li>';
echo '<li>Humidity: <strong>' . $xml->relative_humidity . '%</strong></li>';
if ($xml->heat_index_f != 'NA')
echo '<li>Heat Index: <strong>' . $xml->heat_index_f . '</strong></li>';
if ($xml->windchill_f != 'NA')
echo '<li>Windchill: <strong>' . $xml->windchill_f . '</strong></li>';
echo '<li>Barometer: <strong>' . $xml->pressure_in . '&quot;</strong></li>';
echo '<li>Visibility: <strong>' . $xml->visibility_mi . ' mi</strong></li>';
echo '<li>Dewpoint: <strong>' . $xml->dewpoint_f . '&deg;</strong></li>';
echo '<li class="weather_credit">weather by <a href="' . $xml->credit_URL . '" title="' . $xml->credit . '">NOAA</a></li>';
echo '</ul>';
echo '<ul id="sunrise_sunset">';
echo '<li>Sunrise: <strong>' . date('g:i a', $sun_info['sunrise']) . '</strong> <span class="civil_twilight">Civil Twilight: <strong>' . date('g:i a', $sun_info['civil_twilight_begin']) . '</strong></span></li>';
echo '<li>Sunrise: <strong>' . date('g:i a', $sun_info['sunset']) . '</strong> <span class="civil_twilight">Civil Twilight: <strong>' . date('g:i a', $sun_info['civil_twilight_end']) . '</strong></span></li>';
echo '</ul>';
}

You will probably want to cache that output once you have it formatted how you like.

Free Weather For Your Web Site

Friday, February 1st, 2008
Posted in Web Development · Tags: , ,

The NOAA’s National Weather Service provides free weather data that you can use on your web site, even if it is for commercial uses. You can’t claim to have copyright on the data, but it is public domain information. Accuweather tried a few years ago to get this service shut down because they wanted people to pay for weather information (which is part of their business). Fortunately people complained because our tax money is being spent on making this data available in the first place.

So why aren’t more people using it? It’s probably because the documentation is daunting, a little confusing and the icons aren’t that great looking. You might be wondering why not just use Yahoo’s Weather RSS feed? Well, because it’s only for non-commercial use and it only gives you current conditions, today’s forecast and tomorrow’s forecast. However once you know how to access NOAA’s data you can customize it to your liking.

Current Conditions
It used to be that you had to use the METAR service to get current conditions. METAR is a very unfriendly format and fortunately is no longer necessary. You can now get current conditions in a XML file or RSS file. I recommend using the XML file since you will be able to go through the nodes and format it to your liking. The only key here is that most require an Airport/Airfield nearby, since there are only about 1,800 locations across the United States and US Territories.

Getting Weather Forecasts
The first place to stop is the National Digital Forecast Database XML Web Service. (There are change notices the bottom of the page, but they don’t change very often; however it’s probably a good idea to subscribe to the RSS feed. Then you can make appropriate changes in the future and if there are any new features to the service you can take advantage of them). First thing you’ll notice is that there is a lot of information on this page. The service uses SOAP (basically you make requests with XML and receive XML) and there are two functions to grab the data, NDFDgen and NDFDgenByDay. Both will give you summarized forecasts over a 12 hour or 24 hour time period (your choice). NDFDgen will give you much more information than NDFDgenByDay and lets you pick what you want. You need to know the longitude and latitude of where you want the weather forecast and you can send the number of days you want forecasted. You probably won’t want more than a 7 day forecast. If you want you can even request old weather forecasts. They ask that you cache your results for an hour since that is how often they are updated (even Yahoo’s Weather RSS feed is only updated once an hour). They say they are updated 45 minutes after the hour but from what I’ve seen it’s usually around 52 minutes after the hour. If your web server is on Unix/Linux you can setup a CRON job to grab the files on the hour (although they recommend to do it 15 minutes after the hour, because there is a delay in processing and updating all their files on their servers).

I wish they would offer RSS and XML feeds for some of the bigger cities in the US, rather than just SOAP. Part of me wonders if it is due to pressure from Accuweather and the other US weather sites online, because they have big business in providing easier and prettier methods of weather information. Using SOAP in Coldfusion is pretty simple, especially if you are using Dreamweaver. If you are using PHP, take a look at NuSOAP.

For about a month I noticed the Yahoo Weather RSS feed was giving current conditions and a 5 day forecast, instead of 2. However, it seems Yahoo went back to using just current conditions, today’s forecast and tomorrow’s forecast. I suppose it was a bug they overlooked.

Get Yahoo Weather with SimplePie

Wednesday, July 25th, 2007
Posted in Web Development · Tags: , , ,

SimplePie is a new PHP library made to make parsing RSS feeds easy and fun. Yes I said fun, because its very easy and the docs are well written. RSS is more than just news, it can be images, video and podcasts. Mash up the data however you want, I’ve seen some innovative and unique things done so far. They also offer sample code on making a site like popurls/Original Signal.

Figure I’d share some code I used with SimplePie and a Yahoo Weather RSS feed. I can’t post it all on the homepage as its really long. It does transform and output the data nicely (not any CSS styling but I do calculate the wind direction and some other things). Please note that Yahoo only permits you to use their Weather RSS feed on non-commercial sites.

(more…)