{"id":29,"date":"2004-08-17T23:20:04","date_gmt":"2004-08-18T07:20:04","guid":{"rendered":""},"modified":"2016-05-25T23:34:40","modified_gmt":"2016-05-26T06:34:40","slug":"php-weather","status":"publish","type":"post","link":"https:\/\/mikeindustries.com\/blog\/archive\/2004\/08\/php-weather","title":{"rendered":"Improve the Weather with PHP"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/mikeindustries.com\/blog\/images\/inline\/westseattleview.jpg\" width=\"122\" height=\"368\" border=\"0\" alt=\"\" class=\"rightinlineborder\" \/>Ever wanted to turn a cloudy day into a sunny one?  Well now you can, with the magic of PHP.  If you use the <a href=\"https:\/\/mikeindustries.com\/blog\/archive\/2004\/06\/making-of-the-live-cam\" target=\"_blank\">Live Theme<\/a> 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&#8217;ve gone from a summer of &#8220;partly cloudy&#8221; and &#8220;fair&#8221; days to &#8220;sunny&#8221;, &#8220;beautiful&#8221;, and &#8220;spectacular&#8221; days.  Here&#8217;s the lowdown on the PHP-induced warming trend:<\/p>\n<p>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 <a href=\"http:\/\/www.thoughtanomalies.com\" target=\"_blank\">Michael Simmons&#8217; handy dandy weather readout<\/a> 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&#8217;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 &#8220;Partly Cloudy&#8221;.<\/p>\n<p>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 &#8220;Partly Cloudy and 80 degrees&#8221;.  It was clear at this point that something had to be done.<br \/>\n<!--more--><\/p>\n<h3>Go straight to the source<\/h3>\n<p>As it turns out, the noaa.gov weather feeds are generated at various airports around the country.  I chose Seattle&#8217;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&#8217;s trying to uphold Seattle&#8217;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 &#8212; his input was not up to the Mike Industries Meteorological Reporting Standards, and thus, I had to banish him from the site.<\/p>\n<p>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&#8217;s reports a few times a day over the next week and Sea-Tac&#8217;s were still a little negative for my tastes, but I thought I might have a winner in Renton.  I don&#8217;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.<\/p>\n<h3>Tomato tomahto<\/h3>\n<p>So you&#8217;d think everything would be all solved now, right?  Well, not quite.  Apparently, the Renton schmo really likes the word &#8220;fair&#8221;.  I like &#8220;fair&#8221; to describe skin tones, carnivals, and reasonable policies, but I just don&#8217;t like it to describe beautiful clear skies.  &#8220;Fair and 80 degrees&#8221;?  It just sounds very average to me.  How was that chicken?  &#8220;I don&#8217;t know. It was fair.&#8221;<\/p>\n<p>Now that I had at least an accurate weather report to work with, I figured I&#8217;d rip a page out of <a href=\"http:\/\/www.shauninman.com\" target=\"_blank\">The Wolf&#8217;s<\/a> book and start replacing stuff with PHP.  In general, I noticed the following things about Renton&#8217;s weather report:<\/p>\n<p>&#8220;Mostly Cloudy&#8221; is actually partly cloudy<br \/>\n&#8220;Partly Cloudy&#8221; is actually mostly sunny<br \/>\n&#8220;Fair&#8221; is actually really sunny<\/p>\n<p>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&#8217;s nice out, I <strong>really<\/strong> want to let you know about it.  So I set up a random number system in PHP whereby &#8220;fair&#8221; gets replaced randomly by one of the following:<\/p>\n<p>Spectacular<br \/>\nBeautiful<br \/>\nSunny<br \/>\nNot a Cloud in the Sky<\/p>\n<p>Great!  So we&#8217;re done now, right?  Not quite.<\/p>\n<h3>Sunglasses at night<\/h3>\n<p>Unless you&#8217;re <a href=\"http:\/\/www.80smusiclyrics.com\/artists\/coreyhart.htm\" target=\"_blank\">Corey Hart<\/a>, you can&#8217;t see solar rays under the cover of darkness.  Therefore, it makes no sense to report things like &#8220;sunny&#8221; or &#8220;spectacular&#8221; when it&#8217;s nighttime.  &#8220;Clear&#8221; 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&#8217;s no fun to just estimate it, I decided to use a Sunrise\/Sunset function unearthed at this <a href=\"http:\/\/www.drbeat.li\/php\/source.php?src=php\/sunrise.inc.php\">curious location in Liechtenstein<\/a>.  It&#8217;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 &#8220;clear&#8221; for &#8220;fair&#8221; at night, and one of the four reasonable embellishments during the day.<\/p>\n<p>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.<\/p>\n<h3>The code<\/h3>\n<p>So there you have it.  Better weather with PHP.  I&#8217;m providing the code below in case you&#8217;d like to implement it on your own site.  Here are the instructions:<\/p>\n<ol>\n<li>Save <a href=\"\/blog\/files\/play-god-with-the-weather.zip\">sun.php and weather.php<\/a> to your server.<\/li>\n<li>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&#8217;ll have to change that value to an airport closer to you. You can <a href=\"http:\/\/www.nws.noaa.gov\/data\/current_obs\/\" target=\"_blank\">look up your airport here.<\/a> And you can <a href=\"http:\/\/www.bcca.org\/misc\/qiblih\/latlong.html\" target=\"_blank\">look up your latitude and longitude here<\/a>. By the way, I&#8217;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&#8217;ll swap it in there.<\/li>\n<li>Set up a <a href=\"http:\/\/www.aota.net\/Script_Installation_Tips\/cronhelp.php3\" target=\"_blank\">cronjob<\/a> to hit the weather.php file every half hour or hour.<\/li>\n<li>That php file will save the weather information (e.g. Sunny and 70) to a little file called &#8220;weatherslug.txt&#8221; which you can then pull into your pages with PHP or Flash.  I use Flash and feed the file contents in via flashvars.<\/li>\n<\/ol>\n<p>That&#8217;s it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ve gone from a summer of &#8220;partly cloudy&#8221; and &#8220;fair&#8221; days to &#8220;sunny&#8221;, &#8220;beautiful&#8221;, and &#8220;spectacular&#8221; days.  Here&#8217;s the lowdown on the PHP-induced warming trend:<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,282],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-code","category-original"],"_links":{"self":[{"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mikeindustries.com\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}