Weather script from APRS reports
global $image_format, $baseimg, $img;
$start_pressure = -1;
$end_pressure = -1;
//
// handle any script inputs
array_shift($argv);
if ($argv[0] != null ){
$IMAGEFILE = $argv[0];
} else {
$IMAGEFILE = "/home/stratuso/cgi-bin/wx_panel_white/white600x70.jpg";
}
if ($argv[1] != null ){
$OUTPUTIMAGEFILE = $argv[1];
} else {
$OUTPUTIMAGEFILE = "/home/stratuso/webroots/ratair.stratus.org.uk/wc/wx.jpg";
}
//
$result = get_web_page( "http://www.findu.com/cgi-bin/raw.cgi?call=m3nic&start=3&length=5&time=1" );
//
if ( $result == null ) {
/* ... error: bad url, timeout, redirect loop ... */
}
//
if ( $result['http_code'] != 200 ) {
/* ... error: no page, no permissions, no service ... */
}
//
$page = $result['content'];
//
/* Ok lets now parse ou tthe weather data */
//
$output = strip_html_tags( $page );
//
/* Ok, now we need to strip out the stuff at the end */
//
$pieces = explode( "\n", $output);
//
foreach($pieces as $left ){
if ( strlen($left) >5 ) {
$bit = strstr($left, "@");
$bit = ltrim($bit, "@");
$bit = trim($bit, ".Dvs");
// by now we just have the following ...
// 1 2 3 4 5 6
// 01234567890123456789012345678901234567890123456789012345678901
// 112118z5210.97N/00059.63E_229/003g...t049r...p...P000h83b10216
$day = substr($bit,0, 2);
$time = substr($bit,2, 2).":".substr($bit,4,2);
$lat = substr($bit,7, 8);
$long = substr($bit,16, 9);
//
$wind_dir = substr($bit,25, 4); // includes the _
if ( substr($wind_dir,0,1) == "_" ) {
$wind_dir = fnResolveWindDirection( substr($wind_dir,1,3) );
}else {
$wind_dir = "***";
}
//
$wind_speed = substr($bit,29, 4);
if ( substr($wind_speed,0,1) == "/" ) {
$wind_speed = fnProcessWindSpeed( substr($wind_speed,1,3) );
}else {
$wind_speed = "***";
}
//
$wind_gust = substr($bit,33, 4);
if ( substr($wind_gust,0,1) == "/" ) {
$wind_gust = fnProcessWindSpeed( substr($wind_gust,1,3) );
}else {
$wind_gust = "***";
}
//
$temp_C = substr($bit,37, 4);
if ( substr($temp_C,0,1) == "t" ) {
$farenheight = substr($temp_C,1,3); // this is kept for later too
$temp_C = fnProcessCurrentTemperature( $farenheight );
}else {
$temp_C = "***";
}
//
$rain = substr($bit,41, 4);
if ( substr($rain,0,1) == "r" ) {
$rain = fnProcessRainFall( substr($rain,1,3) );
}else {
$rain = "***";
}
//
$rain_24 = substr($bit,45, 4);
if ( substr($rain_24,0,1) == "p" ) {
$rain_24 = fnProcessRainFall( substr($rain_24,1,3) );
}else {
$rain_24 = "***";
}
//
$rain_since_mid = substr($bit,49, 4);
if ( substr($rain_since_mid,0,1) == "P" ) {
$rain_since_mid = fnProcessRainFall( substr($rain_since_mid,1,3) );
}else {
$rain_since_mid = "***";
}
//
$humidity = substr($bit,53, 3);
if ( substr($humidity,0,1) == "h" ) {
$humidity = fnProcessHumidity( substr($humidity,1,2) );
}else {
$humidity = "***";
}
//
$pressure = substr($bit,56, 6);
if ( substr($pressure,0,1) == "b" ) {
$pressure = fnProcessPressure( substr($pressure,1,5) );
}else {
$pressure = "***";
}
//
if ($start_pressure == -1) # then first time
{
}
//
$pressure = substr($bit,56, 6);
if ( substr($pressure,0,1) == "b" ) {
$pressure = fnProcessPressure( substr($pressure,1,5) );
}else {
$pressure = "***";
}
//
if ($start_pressure == -1) # then first time
{
$start_pressure = $pressure;
} else {
$end_pressure = $pressure;
}
}
//print ( "\n$bit");
//
}
// Summary here
if ( $start_pressure > $end_pressure ) {
$pressure = $pressure." (rise)";
//print ("\nPressure rising");
} elseif ( $start_pressure < $end_pressure ) {
$pressure = $pressure." (fall)";
//print ("\nPressure falling");
} else {
$pressure = $pressure." (steady)";
//print ("\nPressure steady");
}
$dewpoint_C = fnCalcDewPoint($farenheight, $humidity);
$cloud_base = fnCalcCloudBase($dewpoint_C, $temp_C);
//
//print ( "\n--------------------");
print ( "\n$day");
print ( "\n$time");
print ( "\n$lat");
print ( "\n$long");
print ( "\n$wind_dir");
print ( "\n$wind_speed");
print ( "\n$wind_gust");
print ( "\n$temp_C");
print ( "\n$rain");
print ( "\n$rain_24");
print ( "\n$rain_since_mid");
print ( "\n$humidity");
print ( "\n$dewpoint_C");
print ( "\n$pressure");
print ( "\n$cloud_base");
//
fnWriteImage( $day, $time, $lat, $long,
$wind_dir, $wind_speed, $wind_gust,
$temp_C, $humidity, $cloud_base, $dewpoint_C,
$pressure, $IMAGEFILE, $OUTPUTIMAGEFILE );
//
// End of main body of the script ---------------------------------------
//
/************************************************************************
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the header fields and content.
*/
function get_web_page( $url )
{
$options = array( 'http' => array(
'user_agent' => 'spider', // who am i
'max_redirects' => 10, // stop after 10 redirects
'timeout' => 120, // timeout on response
) );
$context = stream_context_create( $options );
$page = @file_get_contents( $url, false, $context );
//
$result = array( );
if ( $page != false )
$result['content'] = $page;
else if ( !isset( $http_response_header ) )
return null; // Bad url, timeout
//
// Save the header
$result['header'] = $http_response_header;
//
// Get the *last* HTTP status code
$nLines = count( $http_response_header );
for ( $i = $nLines-1; $i >= 0; $i-- )
{
$line = $http_response_header[$i];
if ( strncasecmp( "HTTP", $line, 4 ) == 0 )
{
$response = explode( ' ', $line );
$result['http_code'] = $response[1];
break;
}
}
//
return $result;
}
/* ********************************************************************** */
/**
* Remove HTML tags, including invisible text such as style and
* script code, and embedded objects. Add line breaks around
* block-level tags to prevent word joining after tag removal.
*/
function strip_html_tags( $text )
{
$text = preg_replace(
array(
// Remove invisible content
'@]*?>.*?@siu',
'@
@siu',
'@@siu',
'@]*?.*?@siu',
'@]*?.*?@siu',
'@]*?.*?@siu',
'@]*?.*?@siu',
'@@siu',
'@]*?.*?@siu',
// Add line breaks before and after blocks
'@ '@ '@ '@ '@ '@ '@ ),
array(
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
"\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
"\n\$0", "\n\$0",
),
$text );
return strip_tags( $text );
}
// ########################################################################
function fnResolveWindDirection( $sWindDegrees )
{
$wind_dir = $sWindDegrees;
# Cardinal Direction Degree Direction
#N 348.75 - 11.25
#NNE 11.25 - 33.75
#NE 33.75 - 56.25
#ENE 56.25 - 78.75
#E 78.75 - 101.25
#ESE 101.25 - 123.75
#SE 123.75 - 146.25
#SSE 146.25 - 168.75
#S 168.75 - 191.25
#SSW 191.25 - 213.75
#SW 213.75 - 236.25
#WSW 236.25 - 258.75
#W 258.75 - 281.25
#WNW 281.25 - 303.75
#NW 303.75 - 326.25
#NNW 326.25 - 348.75
//
if ( (($wind_dir >= 348) && ($wind_dir <= 359))||(($wind_dir >= 0) && ($wind_dir <= 11)) ) {
#N 348.75 - 11.25
$wind_ind = "N";
} elseif ( (($wind_dir >= 12) && ($wind_dir <= 33)) ) {
#NNE 11.25 - 33.75
$wind_ind = "NNE";
} elseif ( (($wind_dir >= 34) && ($wind_dir <= 56)) ) {
#NE 33.75 - 56.25
$wind_ind = "NE";
} elseif ( (($wind_dir >= 57) && ($wind_dir <= 78)) ) {
#ENE 56.25 - 78.75
$wind_ind = "ENE";
} elseif ( (($wind_dir >= 79) && ($wind_dir <= 101)) ) {
#E 78.75 - 101.25
$wind_ind = "E";
} elseif ( (($wind_dir >= 102) && ($wind_dir <= 146)) ) {
#SE 123.75 - 146.25
$wind_ind = "SE";
} elseif ( (($wind_dir >= 147) && ($wind_dir <= 168)) ) {
#SSE 146.25 - 168.75
$wind_ind = "SSE";
} elseif ( (($wind_dir >= 169) && ($wind_dir <= 191)) ) {
#S 168.75 - 191.25
$wind_ind = "S";
} elseif ( (($wind_dir >= 192) && ($wind_dir <= 213)) ) {
#SSW 191.25 - 213.75
$wind_ind = "SSW";
} elseif ( (($wind_dir >= 214) && ($wind_dir <= 258)) ) {
#WSW 236.25 - 258.75
$wind_ind = "WSW";
} elseif ( (($wind_dir >= 259) && ($wind_dir <= 281)) ) {
#W 258.75 - 281.25
$wind_ind = "W";
} elseif ( (($wind_dir >= 282) && ($wind_dir <= 303)) ) {
#WNW 281.25 - 303.75
$wind_ind = "WNW";
} elseif ( (($wind_dir >= 304) && ($wind_dir <= 326)) ) {
#NW 303.75 - 326.25
$wind_ind = "NW";
} elseif ( (($wind_dir >= 327) && ($wind_dir <= 347)) ) {
#NNW 326.25 - 348.75
$wind_ind = "NNW";
} else {
# do nothing
$wind_ind ="";
}
$wind_ind = $wind_ind." (".$wind_dir.")";
//
return $wind_ind;
}
// ########################################################################
// This turns it into Knots and sets the calm if zero
function fnProcessWindSpeed( $sWindspeed )
{
if ($sWindspeed == "000"){
$windspeed = "Calm";
//print "\nWindspeed is zero - set to calm";
} else {
$windspeed = sprintf("%d kt",$sWindspeed*0.8689755962687);
}
return $windspeed;
}
// ########################################################################
function fnProcessCurrentTemperature( $sTemp )
{
$faren = sprintf("%.1f", $sTemp);
$temp = sprintf("%.1f", ((5/9) *($faren - 32)));
$temp = $temp."C";
//print "\ntemp modified to ".$temp;
return $temp;
}
// ########################################################################
function fnProcessRainFall( $sRain )
{
$rain = sprintf("%3d", ($sRain*(25/100))). " mm";
return $rain;
}
// ########################################################################
function fnProcessHumidity( $sHumidity )
{
if ($sHumidity == "00"){
$humidity = "100%";
//print "\nHumidity modified to 100";
} else
$humidity = sprintf("%s", $sHumidity). "%";
//print "\nHumidity='".$humidity."'";
return $humidity;
}
// ########################################################################
function fnProcessPressure( $sPressure )
{
$press = $sPressure / 10;
$press = $press." mb";
//print "\nModified pressure = $press[0]";
return $press;
}
// ########################################################################
function fnWriteImage( $day, $time, $lat, $long, $wind_dir, $wind_speed,
$wind_gust, $temp, $humidity, $cloud_base, $dew_point, $pressure, $sInputFileName, $sOutputFileName )
{
// create the base image
global $image_format, $baseimg, $img;
//
$image_format = 'jpeg';
//
// create a truecolor image to deal avoid any pallete problems
print "\nReading $sInputFileName";
print "\nWriting $sOutputFileName";
$baseimg = imagecreatefromjpeg($sInputFileName);
$basex = imagesx($baseimg);
$basey = imagesy($baseimg);
print "\nSize is $basex by $basey\n";
//
$img = imagecreatetruecolor($basex,$basey);
imagecopy($img,$baseimg,0,0,0,0,$basex,$basey);
//
$color1 = imagecolorallocate($img,255,0,0); // RED
$color2 = imagecolorallocate($img,0,128,0); // GREEN
$color3 = imagecolorallocate($img,0,0,255); // BLUE
$BLACK = imagecolorallocate($img,0,0,0); // BLACK
$WHITE = imagecolorallocate($img,255,255,255); // WHITE
$color6 = imagecolorallocate($img,255,215,0); // GOLD
$color7 = imagecolorallocate($img,135,206,235); // sky blue
$ROYAL_BLUE = imagecolorallocate($img,065,105,225); //
//
$root_x = 9;
$root_y = 1;
//
# now put the text on top of the box
imagestring($img, 2, $root_x, $root_y, "Weather Report at $time ($day)", $ROYAL_BLUE);
imagestring($img, 2, $root_x + 10, $root_y + 12, "Temperature: $temp", $ROYAL_BLUE);
imagestring($img, 2, $root_x + 10, $root_y + 24, "Cloud Base : $cloud_base", $ROYAL_BLUE);
imagestring($img, 2, $root_x + 200, $root_y + 12, "Dew Point: $dew_point", $ROYAL_BLUE);
imagestring($img, 2, $root_x + 200, $root_y + 24, "Relative Humidity: $humidity", $ROYAL_BLUE);
imagestring($img, 2, $root_x + 10, $root_y + 36, "Local QFE: $pressure", $ROYAL_BLUE);
imagestring($img, 2, $root_x + 200, $root_y + 36, "Wind: $wind_speed $wind_dir (gust $wind_gust)", $ROYAL_BLUE);
//
// ok now write the file
imagejpeg($img, "$sOutputFileName");
//
// get rid of the image since we don't need it in memory any more.
// don't need any memory leaks taking down the server
imagedestroy($img);
imagedestroy($baseimg);
} // end function imagecenteredtext
// ########################################################################
function fnCalcDewPoint($faren, $humidity )
{
# lets calc the dew point
$dew_point_F = $faren -((100 - $humidity)/5); # in farenheight
$dew_point_C = sprintf("%.1fC",($dew_point_F - 32) * (5/9));
//
return $dew_point_C;
}
// ########################################################################
function fnCalcCloudBase($dew_point_C, $temp_C )
{
//
#estimate the cloud base
$est_cb = ($temp_C - $dew_point_C) * 1000;
//
if ( $est_cb < 600) {
$est_cloud_base = "<600ft";
} else {
$est_cloud_base = sprintf("%dft",$est_cb);
}
//
//print "\nTemp C = $temp_C";
//print "\nDew Point C = $dew_point_C";
//print "\nCloud Base = $est_cloud_base";
//
return $est_cloud_base;
}
// ########################################################################
?>