Essentially I wanted the weather info in the same format as the rest of the site, and while Steve has done a great job of formatting the information I can't just use plain HTML due to the way Drupal (the CMS system I develop in) formats pages. So I needed to pull the info apart and reformat it the way I needed it.
Ideally, the following code would be in a 'module' (clip on application for Drupal), and eventually it probably will be... time and energy permitting... but for now it is effectively some PHP code pulling apart and reformatting some of the information from the realtime.txt file. I'm posting it here for a few reasons, not least of which is that it will be a safe backup of what I have written in case I break and/or lose it in the future But also because I believe in open source and sharing knowledge... and this seems like a decent way to help a community that has helped me (with software, ideas and entertainment - reading the forum over the last few weeks).
Note: this code is pretty basic, and the display is not nearly completed... but its a proof of concept and is a springboard for the next level... Also note that I have incorporated a windchill calculation, but I'm not actually using it as yet...
So, here is the code for the page at (and this is a temporary development site - mine, actually) http://steve.dunford.org.nz/weather (and note: you need to have the core-optional PHP evaluation module enabled, and be using the PHP input type in Drupal):
Code: Select all
<?php
/**
* Script to pull apart the realtime txt file and allow it to be
* used to show various weather items in Drupal
*/
$tempFile = "<path to the directory relative to the server root>/realtime.txt";
$tempContents = fopen($tempFile,'r');
$realtimeContents = fgets($tempContents,1000);
$realtime = explode(" ",$realtimeContents);
$weatherTime = explode(":",$realtime[1]);
$weatherTime = $weatherTime[0].$weatherTime[1];
$weatherDate = explode("/",$realtime[0]);
$weatherMonth = array ("January","February","March","April","May","June","July","August","September","October","November","December");
$weatherMonthNum = -1 + (int)$weatherDate[1];
$windKPH = round((1.852 * (double)$realtime[6]),1); //Wind is in Knots, convert to numeral, then KPH, then trim to 1dp
$tempC = (double)$realtime[2]; //convert temp from string to numeral.
$windChill=(13.12 + 0.6215 * $tempC - 11.37 * pow($windKPH,0.16) + 0.3965 * $tempC * pow($windKPH,0.16)); //windchill calculation
$windChill = round($windChill,1); //round to 1dp
if ($windKPH < 5 || $windKPH > 100 || $tempC < -50 || $tempC > 5) $windChill = 0; //constraints for windchill measurement
?>
<h3>
<?php echo 'Latest update: '.$weatherTime.' hours on the '.((int)$weatherDate[0]);
if ($weatherDate[0] == "1" || $weatherDate[0] == "21" || $weatherDate == "31") echo 'st';
else if ($weatherDate[0] == "2" || $weatherDate[0] == "22") echo 'nd';
else if ($weatherDate[0] == "3" || $weatherDate[0] == "23") echo 'rd';
else echo 'th';
echo ' of '.$weatherMonth[$weatherMonthNum].", 20".$weatherDate[2]; ?>
</h3>
<h4>Naseby, New Zealand - 45°01'47"S 170°08'19"E, Elevation 1968 ft</h4>
<br><br>
<p>Naseby weather, updated every 15 minutes when the local workstation is running</p>
<table style="width: 100%; max-width: 700px;">
<tr style="height: 15px;">
<td valign="top">Temperature: <?php echo $realtime[2]; ?><sup>o</sup><?php echo $realtime[14]; ?><br>
Humidity: <?php echo $realtime[3]; ?>%<br>
Windchill: <?php echo $realtime[24]; ?><sup>o</sup><?php echo $realtime[14]; ?><br>
Dew Point: <?php echo $realtime[4]; ?><sup>o</sup><?php echo $realtime[14]; ?><br>
Barometric Pressure: <?php echo $realtime[10].$realtime[15]; ?>
</td>
<td width="90px"><img src="/sites/default/files/weather/images/windgauge.png" alt="Wind Speed" title="Wind Speed" class="mceItem" width="95px" height="95px"><br><center>Wind Speed</center></td><td width="90px"><img src="/sites/default/files/weather/images/compass.png" alt="Weather Direction" title="Weather Direction" class="mceItem" width="95px" height="95px"><br><center>Wind Dir.</center></td>
</tr>
</table>
<p>Note: The weather station is a typical Chinese wireless model, available from many sources here in NZ and abroad - often referred to as the WS1083 and/or Fine Offset model. The back end weather software is Cumulus by <a href="http://www.sandaysoft.com" target="_blank">Sandaysoft</a> with some custom PHP software written by <a href="http://www.essentialtech.co.nz" target="_blank">Essential Technology</a> to format the information to suit the site.</p>
<p> </p><ul><li><span style="font-size: x-small;">The wind speed needle shows the current snapshot of the wind speed, as read from the weather station. The green arc shows the current ten-minute average wind speed, and the red arc shows the recent highest gust (in the last ten minutes).</span></li><li><span style="font-size: x-small;">The wind direction indicator shows the current snapshot of the wind speed, as read from the weather station, with a red pointer; it also shows the average wind direction over the last ten minutes, with a blue pointer (Note that when the wind speed is low, the wind direction can be quite erratic and unreliable)/</span></li></ul>
As a side note, with Drupal, a node.tpl.php file could also have been used to do the PHP stuff... and I did this first... but its an ugly way to do it (actually, the current way is ugly too). The proper (and next) way I will do it is using a Drupal module.
regards
Steve