Now that we've solved Asterisk weather forecasting for the rest of the world, wouldn't you know our U.S. weather reports would come unglued. One of the problems with depending upon the web data of other organizations (such as the National Weather Service) is that, when they shift gears, you have to make adjustments to your applications as well. So this week is cleanup time at Nerd Vittles. In its infinite wisdom, the National Weather Service decided last week to add latitude, longitude, and elevation information to all of their weather reports. This caused some wrinkles in both the Asterisk® Weather Station by Airport Code and Zip Code applications which didn't know how to interpret this new data. Personally, we were always big supporters of more federal holidays because it meant the bureaucrats and politicians had less time to screw things up. These new "improvements" are a good example of what happens when federal workers have too much time on their hands.
All of the Nerd Vittles weather applications for Asterisk (except last week's Worldwide Weather Forecasts) depend upon NOAA web site information at a web link that looks like the following. You can enter either a zip code or a city and state.
Using MySQL databases, we convert telephone input of airport codes and zip codes into City and State information which then can be passed to NOAA's web link to collect the information that we use to convert the text to speech with the Flite application. If you look at one of the reports, you'll see that the main page display now looks like this:
Current Local Conditions at:
Charleston Air Force Base
Lat: 32.91 Lon: -80.03 Elev: 59.055
Last Update: 02/20/07, 08:56 AM EST
Fair: 48 °F
Forecast at a Glance
Detailed 7-day Forecast
Your Local Radar
Area Forecast Discussion
Changes in Latitudes. So, Problem #1 is that there's a new Line 4 on the web page with the Latitude, Longitude, and Elevation information. Problem #2 is what to do with it. The two options, of course, are to discard it and leave things the way they were in the Asterisk weather reports or to convert it into intelligible text and play it with the weather report. The addition is a little puzzling quite frankly because it's static information and it really doesn't pertain to the weather other than telling you how close to the equator you are or how close to the sun you live plus or minus 93 million miles. They could just as easily have added the State Bird or State Flower but, no, we have Latitude, Longitude, and Elevation to deal with instead. The good news is that, if you ever want to build an application that computes the distance between two cities, this new information combined with searches for two cities using the NOAA link above will give you everything you need to make the calculation without having to rely upon a ZIP code database with latitude and longitude information (which we've previously given you). So we'll leave that as your homework project. With a little Googling, you can find the formula. And we'll throw in one of our PBX-in-a-Flash drives to the first person that posts a comment with working PHP code that correctly calculates the distance between two U.S. cities using NOAA website data only.
Manually Updating the Weather Applications. We've made the management decision to throw the new data in the proverbial bit bucket, but we also want to show you how to convert the data into a usable format for the one or two folks that just can't live without it. Why are we going to such pains? Because we want to encourage others to build PHP applications for Asterisk that do something useful. And, if you walk through the Nerd Vittles code for these applications, it will give you a good idea how to get started. Looking through the code, you'll notice that we have to parse the web data to actually collect the information we need to "play" a weather report on the telephone. So, to strip out this line of text, there are a couple ways to approach it. One way would be to look for Lat: and then search for the next line break character: <br> and strip out the intervening text. We chose to actually count line breaks after the Current Local Conditions line (above) until we got to the desired string of text:
// new code to delete Latitude, Longitude, and Elevation info from report
$start = strpos($newvalue,"<br>") ;
$start2 = strpos($newvalue,"<br>",$start+1) ;
$start3 = strpos($newvalue,"<br>",$start2+1) ;
$newvalue = substr($newvalue,0,$start2) . " " . substr($newvalue,$start3) ;
// new code ends here
So the new code above needs to be inserted immediately after the line that looks like this:
$newvalue=substr($newvalue, 0, $finish);
This change should be made in both /var/lib/asterisk/agi-bin/nv-weather.php and /var/lib/asterisk/agi-bin/nv-weather-zip.php if you happen to be using both the Weather Station by Airport Code and by Zip Code. And, not to worry, we've fixed the original downloadable code so that newbie's will never know anything changed.
If you'd prefer to actually have the Latitude, Longitude, and Elevation information spoken to callers, then you'd obviously not include the new code above. Instead, we need to add three new parsing instructions after the following line:
$newvalue = str_replace( "NA:", " ", $newvalue );
Here are the new lines you'd insert to translate the NOAA abbreviations into Plain English:
$newvalue = str_replace( "Lat:", "Latitude: ", $newvalue );
$newvalue = str_replace( "Lon:", ". Longitude: ", $newvalue );
$newvalue = str_replace( "Elev:", ". Elevation: ", $newvalue );
Now that we've covered all the theory, it occurred to us that some of you might just like to download the new code. If you prefer that approach, here are the step-by-step instructions for each application. These instructions assume that you've already installed the Weather Applications previously. If not, just choose your desired installation from the Best of Nerd Vittles site.
Installing Weather by Airport Code Update. To install the Weather by Airport Code update, log into your Asterisk server as root and issue the following commands:
rm -f nv-weather2-zip.*
mv nv-weather.php nv-weather.old2.php
rm -f nv-weather2.zip
chmod 775 nv-weather.php
chown asterisk:asterisk nv-weather.php
Installing Weather by Zip Code Update. To install the Weather by Zip Code update, log into your Asterisk server as root and issue the following commands:
rm -f nv-weather-zip.zip
mv nv-weather-zip.php nv-weather-zip.old.php
rm -f nv-weather-zip.zip
chown asterisk:asterisk nv-weather-zip.php
chmod 775 nv-weather-zip.php
Test Driving the Latest Asterisk Weather Applications. To use the Asterisk Weather Station by Airport Code, pick up any phone connected to your Asterisk server and dial 611. When prompted, key in the three-character Airport Code for the weather report you wish to retrieve. Then sit back and listen to the latest weather report for your Airport Code from the National Weather Service.
To use the Asterisk Weather Station by Zip Code, pick up any phone connected to your Asterisk server and dial Z-I-P (947). When prompted, key in the five-digit Zip Code for the weather report you wish to retrieve. Then sit back and listen to the latest weather report for your Zip Code from the National Weather Service.
Nerd Vittles Demo Hot Line (courtesy of les.net). You now can take a number of Nerd Vittles projects for a test drive... by phone! The current demos include (1) MailCall for Asterisk with password 1111 (retrieve your email by phone), (2) NewsClips for Asterisk (latest news headlines in dozens of categories), (3) Weather Forecasts by U.S. Airport Code, and (4) Weather Forecasts by U.S. ZIP Code. You're not prompted for #4 yet, but it does work! Just call our number (shown in the left margin) and take any or all of them for a spin. The sound quality may not be perfect due to performance limitations of our ancient Intel 386 demo machine. But the price is right.
Nerd Vittles Fan Club Map. Thanks for visiting! We hope you'll take a second and add yourself to our Frappr World Map compliments of Google. In making your entry, you can choose an icon: guy, gal, nerd, or geek. For those that don't know the difference in the last two, here's the best definition we've found: "a nerd is very similar to a geek, but with more RAM and a faster modem." We're always looking for the best BBQ joints on the planet. So, if you know of one, add it to the map while you're visiting as well.