Improve the Weather with PHP
Ever wanted to turn a cloudy day into a sunny one? Well now you can, with the magic of PHP. If you use the Live Theme here at Mike Industries, you may have noticed that the weather conditions displayed in the header have improved markedly over the last couple of weeks. We’ve gone from a summer of “partly cloudy” and “fair” days to “sunny”, “beautiful”, and “spectacular” days. Here’s the lowdown on the PHP-induced warming trend:
When I first launched the Live Theme, it was merely a live shot of Puget Sound with no weather conditions readout. But then I saw Michael Simmons’ handy dandy weather readout and got jealous enough to set up a weather readout of my own (full instructions will follow later in this article). Problem was, I didn’t like what I was seeing on the weather report. We are in the middle of our third spectacularly sunny Seattle summer in a row and my weather readout still said things like “Partly Cloudy”.
Then one day a couple of weeks ago, something made me snap. I looked out the window of my office at the 360 degree view, and I saw nothing but blue sky and one tiny speck of a cloud in the distance. I then went to the web and saw that Mike Industries was reporting “Partly Cloudy and 80 degrees”. It was clear at this point that something had to be done.
Go straight to the source
As it turns out, the noaa.gov weather feeds are generated at various airports around the country. I chose Seattle’s Boeing Field, being that it is only about 10 miles away from me. Well, it turns out that the poor schmo who is in charge of watching clouds and reporting weather conditions at Boeing Field is some kind of pessimist. Either that or he’s trying to uphold Seattle’s reputation as a foul-weather city so no one else moves here. For all I know, he might even be choosing what weather conditions to report by throwing darts. One thing was clear — his input was not up to the Mike Industries Meteorological Reporting Standards, and thus, I had to banish him from the site.
With one cloudwatcher already dismissed, I moved on to other nearby airports. The next two closest were Sea-Tac and Renton. I monitored each airport’s reports a few times a day over the next week and Sea-Tac’s were still a little negative for my tastes, but I thought I might have a winner in Renton. I don’t know what they put in the coffee at Renton airport, but for whatever reason, the reports were quite a bit more accurate. So I settled on Renton.
So you’d think everything would be all solved now, right? Well, not quite. Apparently, the Renton schmo really likes the word “fair”. I like “fair” to describe skin tones, carnivals, and reasonable policies, but I just don’t like it to describe beautiful clear skies. “Fair and 80 degrees”? It just sounds very average to me. How was that chicken? “I don’t know. It was fair.”
Now that I had at least an accurate weather report to work with, I figured I’d rip a page out of The Wolf’s book and start replacing stuff with PHP. In general, I noticed the following things about Renton’s weather report:
“Mostly Cloudy” is actually partly cloudy
“Partly Cloudy” is actually mostly sunny
“Fair” is actually really sunny
Armed with these three conditions to change, I used PHP to perform the weather upgrade. The first two were fairly straightforward, but I really wanted to go over the top on the last one. When it’s nice out, I really want to let you know about it. So I set up a random number system in PHP whereby “fair” gets replaced randomly by one of the following:
Not a Cloud in the Sky
Great! So we’re done now, right? Not quite.
Sunglasses at night
Unless you’re Corey Hart, you can’t see solar rays under the cover of darkness. Therefore, it makes no sense to report things like “sunny” or “spectacular” when it’s nighttime. “Clear” is probably a better descriptor. So the next step was inserting logic into the PHP code which would decide what term to display depending on if it was dark or light in Seattle at the time. Since it’s no fun to just estimate it, I decided to use a Sunrise/Sunset function unearthed at this curious location in Liechtenstein. It’s a PHP function, wrapped in a class, which spits out the sunrise or sunset time at a set latitude and longitude for any day of the year. Armed with this piece of PHP, I created a conditional in my weather script which substitutes “clear” for “fair” at night, and one of the four reasonable embellishments during the day.
Incidentally, there was one other way I was thinking about doing this. That was to actually take a brightness reading of the current Live Cam shot and base it on that. Dark Live Cam equals night, Light Live Cam equals day. The only problem with that is that the chance for errors seems high. Why trust a DV cam when you can trust astronomy.
So there you have it. Better weather with PHP. I’m providing the code below in case you’d like to implement it on your own site. Here are the instructions:
- Save sun.php and weather.php to your server.
- Customize the variables in weather.php with your geographical location, local server path and any additional embellishments you wish. My weather feed pulls from KRNT.rss which is Renton airport. You’ll have to change that value to an airport closer to you. You can look up your airport here. And you can look up your latitude and longitude here. By the way, I’m using kind of a Busch League method to parse the RSS file. If any of you PHP people out there have a better way, I’ll swap it in there.
- Set up a cronjob to hit the weather.php file every half hour or hour.
- That php file will save the weather information (e.g. Sunny and 70) to a little file called “weatherslug.txt” which you can then pull into your pages with PHP or Flash. I use Flash and feed the file contents in via flashvars.