General Information
First a disclaimer. I am not a programmer. Anyone who is and looks at this code will instantly recognize that. This script was basically begun as a way to teach myself some PHP and image manipulation. I am well aware that there are probably numerous ways to do what I've done here much more efficiently and I encourage the real programmers out there who look at this script and see a way to make it better to provide some input. Now that that is out of the way....This script takes the National Hurricane Center (NHC) RSS feed and formats it for inclusion on a web page. The feed is processed into a series of unordered lists (<ul>) with headers for each storm and an <hr> divider between unrelated items. Output is XHTML 1.0 Strict compliant.
To utilize this script you should understand the following basic concepts:
- What file paths are, absolute and relative.
- How to include an image file on a web page.
Our scripts are free in both the license and monetary sense. However, if you would like show your appreciation for the time and effort that has gone into creating these scripts, you may do so by clicking the button below. It will take you to PayPal but you do not have to have a PayPal account to donate. You can use your regular Visa, MasterCard, etc.
Revision History
| 1.2 | 12/22/2006 |
Fixed problem with XHTML compliancy when no Statements/Warnings are active. Thanks to Ken True at http://saratoga-weather.org/ for the fix! |
| 1.1 | 08/29/2006 | Improved the handling of local statements. |
| 1.0 | 08/27/2006 | Initial Release |
Demos
A demo of the NHC Feed Parser can be seen at our St. James City, FL weather site
Restrictions and Requirements
- PHP release 4 or higher.
Installing the NHC Feed Parser Script
Installation is pretty simple:
- Download the script.
- Unpack/unzip the archive.
-
Set the following parameters in the file config.txt to match your specific configuration. Refer to the Configuration section for details:
$feed_source
$cache_directory
$cache_timer -
Copy the following file to the directory you want to run the script from:
nhcfeedparse.php
- If using the built-in cache funtion, create the cache directory and chmod it to "777".
Usage
Once installed you use the script by including it on your web page. The easiest way to do this is to use a PHP include that would look something like this:
<? include ("/your/path/to/nhcfeedparse.php"); ?>
Relative paths can be used instead of absolute as in the example above if you so desire.
Since the script is in an include, it can wrapped in a <table> or <div> or any other valid html markup.
Feed Source
This script is written specifically for the NHC feeds listed at:
http://www.nhc.noaa.gov/aboutrss.shtml
It has been verified to work with the two English aggregate feeds below as of the release date:
- Atlantic, Caribbean, and Gulf of Mexico tropical cyclones in English
- Eastern Pacific tropical cyclones in English
The spanish feed at will require modification to the script at this time. A future release will most likely incorporate the Spanish feed with this need for feed modification. See the section regarding Spanish feeds below for the required modifications.
To choose a feed set $feed_source to point to the RSS feed you want to use. Here are the settings for the feeds directly from the NHC site:
-
Atlantic (english):
$feed_source = 'http://www.nhc.noaa.gov/index-at.xml'; -
Atlantic (spanish):
$feed_source = 'http://www.nhc.noaa.gov/index-at-sp.xml'; -
Eastern Pacific (english):
$feed_source = 'http://www.nhc.noaa.gov/index-ep.xml';
The feed can be cached locally by setting up the transparent cache as described below. Additionally, you can use a cronjob to cache the file locally, and set the feed source to point at that file.
Cache
This script used the lastrss class created by Vojtech Semecky as it's main feed parsing engine. For more information on lastRSS please visit:
One of the nice things about lastrss is that it includes code for transparent caching. To use this built-in caching you must do the following:
- Create a directory to store the cache file.
- chmod that directory to "777".
- Set "$cache_directory" to the path to the cache directory.
- Set $cache_timer to the number of seconds that you want to wait before checking the NHC for an update.
If you do not want to use the built-in caching, leave $cache_directory blank:
$cache_directory = "";
If you have trouble getting the cache to work try using an absolute path instead of a relative one.
$cache_timer is a value in seconds. If you want to check the NHC feed every 20 minutes set it to 1200. I recommend that you choose a timer that is at least every 20 minutes to avoid unecessary queries of the NHC servers.
Output Format
Each item from the feed is processed and checked to see if it is related to the previously processed item. This allows grouping of items related to individual storms. Additionally, a heading is added with the name of the storm for each of these groupings. Outlook messages are separated from the storm items as well.
The output markup essentially looks like this:
### First a linked header is created to the NHC web site:
<div class="center">
<br />
<h3><a href="http://www.nhc.noaa.gov">National Hurricane Center (Atlantic)</a></h3>
<b>Active tropical cyclones in the Atlantic, Caribbean, and the Gulf of Mexico</b><br />
</div>
### Then a header and a list of the individual items for the first storm on the
### list is output if there is one. Each item is linked back to the NHC page it
### corresponds with along with the time it was issued. Note that each item is
### a list item <li> or an unordered list <ul>:
<h2>Tropical Depression DEBBY</h2>
<ul>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIATCPAT4+shtml/271458.shtml">Tropical Depression DEBBY Public Advisory Number 24</a><br />
Issued at 1100 AM AST SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIATCMAT4+shtml/271437.shtml">Tropical Depression DEBBY Forecast/Advisory Number 24</a><br />
Issued at 1500 UTC SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIATCDAT4+shtml/271459.shtml">Tropical Depression DEBBY Forecast Discussion Number 24</a><br />
Issued at 1100 AM EDT SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIAPWSAT4+shtml/DDHHMM.shtml">Tropical Depression DEBBY Wind Speed Probabilities Number 24</a><br />
Issued at 1500 UTC SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/refresh/graphics_at4+shtml/144642.shtml?3day">Tropical Depression DEBBY Graphics</a><br />
Issued at 1500Z
</li>
### The above is repeated for each additional storm in the feed along with an
### <hr /> to provide a separator for each storm:
<hr />
<h2>Hurricane ERNESTO</h2>
<ul>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIATCPAT5+shtml/271435.shtml">Hurricane ERNESTO Public Advisory Number 12</a><br />
Issued at 1100 AM EDT SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIATCMAT5+shtml/271435.shtml">Hurricane ERNESTO Forecast/Advisory Number 12</a><br />
Issued at 1500 UTC SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIATCDAT5+shtml/271459.shtml">Hurricane ERNESTO Forecast Discussion Number 12</a><br />
Issued at 1100 AM EDT SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/text/refresh/MIAPWSAT5+shtml/DDHHMM.shtml">Hurricane ERNESTO Wind Speed Probabilities Number 12</a><br />
Issued at 1500 UTC SUN AUG 27 2006
</li>
<li style="padding-top:0">
<a href="http://www.nhc.noaa.gov/refresh/graphics_at5+shtml/144732.shtml?3day">Hurricane ERNESTO Graphics</a><br />
Issued at 1500Z
</li>
</ul>
### If there are any additional items that are not associated with an
### individual storm, they are output separately as preformatted <pre> text.
### as with individual storms an <hr> is used as a separator between items.
<hr />
<a href="http://www.nhc.noaa.gov/text/refresh/MIATWOAT+shtml/271520.shtml">Atlantic Tropical Weather Outlook</a><br />
<pre>
000
ABNT20 KNHC 271520
TWOAT
TROPICAL WEATHER OUTLOOK
NWS TPC/NATIONAL HURRICANE CENTER MIAMI FL
1130 AM EDT SUN AUG 27 2006
FOR THE NORTH ATLANTIC...CARIBBEAN SEA AND THE GULF OF MEXICO...
THE NATIONAL HURRICANE CENTER IS ISSUING ADVISORIES ON HURRICANE
ERNESTO...LOCATED ABOUT 205 MILES SOUTH-SOUTHEAST OF GUANTANAMO
CUBA...AND ON TROPICAL DEPRESSION DEBBY...LOCATED OVER THE EASTERN
ATLANTIC ABOUT 1395 MILES WEST-SOUTHWEST OF THE AZORES.
ELSEWHERE...TROPICAL STORM FORMATION IS NOT EXPECTED THROUGH MONDAY.
$$
FORECASTER PASCH
</pre>
Spanish Feeds
If you are using the Spanish feed at http://www.nhc.noaa.gov/index-at-sp.xml, you will need to make some modifications to the script to make it output the data correctly. Specifically you must change:
- all occurences of "Tropical Depression" to "Depresion Tropical";
- all occurences of "Tropical Storm" to "Tormenta Tropical";
- all occurences of "Hurricane" to "Huracan";
There are currently two occurences of each.
lastrss
lastRSS.php is simple yet powerfull PHP class to parse RSS. It's features include:
- All RSS versions supported (0.9, 1.0 and 2.0)
- Transparent cache (doesn't cache RSS file directly, neither HTML output, but serialized variable; cached data isn't parsed every time, but you can still access individual fields)
- pubDate/lastBuildDate conversion to any date/time format
- Character encoding conversion (all encodings supported by iconv library)
- Returns an associative array with RSS fields
- Regular expression parsing (very quick)
- Strips HTML from description (both hard-coded and entity-coded)
- Optional limit the number of returned items
- Two ways how to proceed CDATA information (get/strip CDATA content)
lastRSS was written by Vojtech Semecky and can do much more than what its being used for in this script. If you are looking at trying to parse any RSS feed, I highly recommend taking a look at lastRSS to handle the actual feed parsing.
More information on lastRSS can be found at the lastRSS website: