UV forecast script issue

Hi folks

I am trying to set up the UV forecast script for the dashboard and a standalone page
cause my modified UV sensor has hit an all time low, i.e. LL.L pmsl…

I used to have this working on the odl site but then when i brought over the new site
it overwrote the existing scripts and i cannot for the life of me get it working again :frowning:

I have the uv-forecast.txt in the folder with 666 permission

Its not working on the dashboard and not working here in a direct page

<?php
include("get-euro-UV-forecast.php");

if ($ourUVicon <> '') {
  echo '<img src="./ajax-images/' . $ourUVicon . '" height="76" width="40" 
      alt="UV Index Forecast" title="UV Index Forecast" />';
  echo "<p>UV Forecast for $ourCity on $UVforecastdate 

    UV: $ourUV  (burn time $ourBurntime minutes)</p>\n";
} else {
  echo "No UV forecasted.";
}
?>

Oh yeah, problem is this http://www.northantsweather.com/uv-forecast.php

I have included my get-uv forecast script for you to take a gander at if you would please?

Any advice please folks, cause i dont remember having this trouble before?


get-UV-forecast-inc.zip (3.85 KB)

I think you were using an older script for the forecast … the one you attached is the current version which returns data in

// Returns Arrays:
//
// $UVfcstDate[n] – date of forecast in dd Mon yyyy format
// $UVfcstUVI[n] – forecast UVI in dd.d format
// n=0…8 (usually)
// $UVfcstDOW[n] – forecast DayOfWeek (‘Sunday’ … ‘Saturday’) from date(‘l’,time());
// $UVfcstISO[n] – forecast date in YYYYMMDD format.
// will return $UVfcstUVI[n] = ‘n/a’ if forecast is not available.
and this one works with your lat/long from www.temis.nl

The old script

<?php
//import UV Forecast
// Script by Ken True - saratoga-weather.org
// Version 1.00 - 13-Feb-2008 - Initial Release
// Version 1.01 - 20-Feb-2008 - Added error handling for data not available
// Version 1.02 - 21-Feb-2008 - changed default UV_URL to new address
//
$UV_URL = 'http://i115srv.vu-wien.ac.at/uv/uv-index/uvi_eue.txt';
$ourCity = 'Kopenhagen, Danemark'; // NOTE.. must be EXACT name in forecast page at URL above
//       with a ', ' separating the city and country names.
//$ourCity = 'Nikosia, Cyprus';
//
// create a 'uv-euro-forecast.txt' file in the same directory as the script.
// you may have to set the permissions on the file to '666' so it is writable
// by the webserver.
$UVcacheName = "./uv-euro-forecast.txt";
$UVrefetchSeconds = 3600;
// ---------- end of settings -----------------------

if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
   //--self downloader --
   $filenameReal = __FILE__;
   $download_size = filesize($filenameReal);
   header('Pragma: public');
   header('Cache-Control: private');
   header('Cache-Control: no-cache, must-revalidate');
   header("Content-type: text/plain");
   header("Accept-Ranges: bytes");
   header("Content-Length: $download_size");
   header('Connection: close');
   
   readfile($filenameReal);
   exit;
}
// You can now force the cache to update by adding ?force=1 to the end of the URL

if ( empty($_REQUEST['force']) ) 
        $_REQUEST['force']="0";

$Force = $_REQUEST['force'];

if ($Force==1) {
      $html = fetchUVUrlWithoutHanging($UV_URL,$UVcacheName); 
      print "<!-- force reload from URL $UV_URL -->\n";
      $fp = fopen($UVcacheName, "w"); 
      $write = fputs($fp, $html); 
      fclose($fp);  } 


// refresh cached copy of page if needed
// fetch/cache code by Tom at carterlake.org

if (file_exists($UVcacheName) and filemtime($UVcacheName) + $UVrefetchSeconds > time()) {
      $WhereLoaded = "from cache $UVcacheName";
      $html = implode('', file($UVcacheName));
    } else {
      $WhereLoaded = "from URL $UV_URL";
      $html = fetchUVUrlWithoutHanging($UV_URL);
      $fp = fopen($UVcacheName, "w");
      $write = fputs($fp, $html);
      fclose($fp);  
	}
print "<!-- UV data load from $WhereLoaded -->\n";

if(strlen($html) < 50 ) {
  print "<!-- data not available -->\n";
  $ourUV = 'N/A';
  $ourUVCloudy='N/A';
  $ourBurntime = 'N/A';
  $ourUVicon = '';
  return;
}
// now slice it up	
		preg_match_all("|clear sky\s+[\=]+\s+(.*)\s+Category of the|Uis", $html, $uvsets);

// $uvsets[1][n] contains the forecast text
//  print "<!-- uvsets \n" . print_r($uvsets,true) . " -->\n";

foreach ($uvsets[1] as $indx => $uvtext) { // take each forecast and slice it up

//   print "$indx set: ----------------------------\n" . $uvtext . "\n--- $indx end ---------------\n";

   preg_match('|valid for (.*)|i',$html,$matches);
   $UVforecastdate = trim($matches[1]);
   print "<!-- forecast date $UVforecastdate -->\n";   
// 0....+....1....+....2....+....3....+....4....+....5....+....6....+
// Amsterdam   Netherlands      0.8        0.4             148
// Ankara      Turkey           2.6        1.3              50
// Athen       Greece           2.7        1.3              48
// Baku        Aserbaidschan    2.1        1.0              63

   $lines = explode("\n",$uvtext);
   $nlines = 0;
   foreach ($lines as $n => $line) {
     $line = trim($line);
     $nlines++;
//	 print "$nlines : '$line'\n";
	 
    $city = trim(substr($line,0,12));
    $country = trim(substr($line,12,15));
    $uvClear = trim(substr($line,28,4));
    $uvCloudy = trim(substr($line,39,4));
	$burntime = trim(substr($line,55,4));
	
    $values["$city, $country"] = "$uvClear\t$uvCloudy\t$burntime";
   }
//   print "$nlines processed\n";

}

if (!isset($values["$ourCity"])) {
  return;
}
list($ourUV,$ourUVCloudy,$ourBurntime) = explode("\t",$values["$ourCity"]);

$ourUVicon = get_UVicon($ourUV);

print "<!-- for '$ourCity' ourUV=$ourUV ourUVicon=$ourUVicon ourUVCloudy=$ourUVCloudy ourBurntime=$ourBurntime -->\n";

return;
// ----------------------------functions ----------------------------------- 
 
 function fetchUVUrlWithoutHanging($url) // thanks to Tom at Carterlake.org for this script fragment
   {
   // Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
   $numberOfSeconds=4;   

   // Suppress error reporting so Web site visitors are unaware if the feed fails
   error_reporting(0);

   // Extract resource path and domain from URL ready for fsockopen

   $url = str_replace("http://","",$url);
   $urlComponents = explode("/",$url);
   $domain = $urlComponents[0];
   $resourcePath = str_replace($domain,"",$url);

   // Establish a connection
   $socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);

   if (!$socketConnection)
       {
       // You may wish to remove the following debugging line on a live Web site
        print("<!-- Network error: $errstr ($errno) -->\n");
       }    // end if
   else    {
       $xml = '';
       fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
   
       // Loop until end of file
       while (!feof($socketConnection))
           {
           $xml .= fgets($socketConnection, 128);
           }    // end while

       fclose ($socketConnection);

       }    // end else
	  

   return($xml);

   }    // end function
   
// ----------------------------------------------------------
function get_UVicon ( $uv ) {
  // figure out an icon for UV exposure
//  0 to 2  Low
//  3 to 5 	Moderate
//  6 to 7 	High
//  8 to 10 Very High
//  11+ 	Extreme
   switch (TRUE) {
     case ($uv == 0 or floor($uv) == 0):
       $uv = '';
     break;
     case (($uv >= 1) and ($uv < 11)):
       $uv = floor($uv);
	   $uv = 'UV' . sprintf('%02d',$uv) .  '.gif';
     break;
     case ($uv >= 11):
	   $uv = 'UV11.gif';
     break;
   } // end switch
   return $uv;
} // end get_uv_icon

      
?>

used a different source for UV, but you’re welcome to the old script above.

Best regards,
Ken

HI Ken

What would be the cause for the new script not working?
I would prefer to use the new one as latest normally means better lol

It is better to use the new script, as the old script provided forecasts only for selected cities around the world, while the newer script provides forecasts for your latitude and longitude.

The new script uses different variable names to store the results, and doesn’t include the code to pick an icon, but you could add it easily to your page like this

<?php
include("get-UV-forecast-inc.php");

$ourUVicon = '';
$UVforecastdate = '';
$ourUV = '';

if ($UVfcstUVI[0] <> 'n/a') {
  $ourUVicon = round($UVfcstUVI[0],0);
  $UVforecastdate = $UVfcstDate[0];
  $ourUV = $UVfcstUVI[0];
}

if ($ourUVicon <> '') {
 echo '<img src="./ajax-images/' . $ourUVicon . '" height="76" width="40" 
  alt="UV Index Forecast" title="UV Index Forecast" />';
   echo "<p>UV Forecast for $UVforecastdate 

   UV: $ourUV</p>\n";
} else {
  echo "No UV forecasted.";
}
?>

Note that the new script doesn’t have ‘burn time’ nor ‘city’, so your code was changed to omit those displays.

Best regards,
Ken

Hi Ken and thanks…

I have added that code to the uv-forecast.php to see how it looks and it just gives
No UV forecasted

I have my long and lat in the script and using it my long and lat on the temis page
produces a forecast so i kust have something wrong somewhere

My bad… forgot that the $ourUVicon was supposed to be a full image name UV00.gif to UV11.gif

Try this

<?php
include_once("get-UV-forecast-inc.php");

$ourUVicon = '';
$UVforecastdate = '';
$ourUV = '';

if ($UVfcstUVI[0] <> 'n/a') {
  $ourUVrounded = round($UVfcstUVI[0],0);
  if ($ourUVrounded > 11) {$ourUVrounded = 11; }
  $ourUVicon = "UV" . sprintf("%02d",$ourUVrounded) . ".gif";
  $UVforecastdate = $UVfcstDate[0];
  $ourUV = $UVfcstUVI[0];
}

if ($ourUVicon <> '') {
 echo '<img src="./ajax-images/' . $ourUVicon . '" height="76" width="40" 
  alt="UV Index Forecast" title="UV Index Forecast" />';
   echo "<p>UV Forecast for $UVforecastdate 

   UV: $ourUV</p>\n";
} else {
  echo "No UV forecasted.";
}
?>

See it work on my test page (with my forecast) at http://saratoga-weather.org/bashy-UV.php

Best regards,
Ken

Thanks a lot Ken, Its working now, I just need ot sort out some new images as the
ones i used before were specific for my old site…

Thanks again Ken, appreciate the time…

My pleasure, Simon :slight_smile:
Glad I could be of assistance.

Best regards,
Ken

I’m seeing no data for UV… even on Bashy’s link… a problem at ths source??

I’m having the same problems today.
I think they might have changed the website at http://www.temis.nl/uvradiation/nrt/uvindex.php? and now the preg_match_all() functions dont work in the UV script.
See mine here: Bashewa UV Forecast

Added: I see the site gives the following: Sorry: no data files found.

Same here :frowning:

The issue is that www.temis.nl server is now responding with a 301-Moved permanently to an ip address

HTTP/1.1 301 Moved Permanently Date: Tue, 10 Feb 2009 19:24:51 GMT Server: TUNIX-httpscreen/4.0 Location: http://145.23.253.72/uvradiation/nrt/uvindex.php?lon=-122.0227&lat=37.2715 Content-Length: 365 Connection: close Content-Type: text/html; charset=iso-8859-1

If you follow the redirect, no results appear so it’s the www.temis.nl website that is having problems … hopefully they’ll be back soon.

Best regards,
Ken

Seems to be working again now :smiley:

Edit, a bit later: Now seems not to be working :(. I really should keep my mouth shut!

Yes, it’s working again.

Reload your cache file by including ‘?force=1’ on the URL for the page that displays your UV info.

Best regards,
Ken

I’m not saying anything this time!!

Hi Ken

If I want to have 5 or 7 days UV displayed on one line - how do I get days under UV5 to show (uv04.gif) automatic?
I have 4,8 but show (uv05.gif)

http://www.herlev-vejret.dk/wxsun.php

Cheers
Steffen
Denmark

Currently, the script will round() the value so 4.8 → 5. If you’d rather it show as 4 then
Change the round() to floor() and it should return as 4

Ken

Hi Ken

MY SCRIPT:

<?php include_once("get-UV-forecast-inc.php"); $ourUVicon = ''; $UVforecastdate = ''; $ourUV = ''; if ($UVfcstUVI[0] <> 'n/a') { $ourUVrounded = [color=red]round[/color]($UVfcstUVI[0],0); if ($ourUVrounded > 11) {$ourUVrounded = 11; } $ourUVicon = "uv" . sprintf("%02d",$ourUVrounded) . ".gif"; $UVforecastdate = $UVfcstDate[0]; $ourUV = $UVfcstUVI[0]; } if ($ourUVicon <> '') { echo '
UV Index Prognose'; echo "

Prognose for Herlev: $UVforecastdate UV: $ourUV

\n"; } else { echo "Ingen UV Prognose."; } ?>

If I change too floor (in the red) I get this error:
Warning: Wrong parameter count for floor()

Cheers
Steffen

Yes, the floor() function only takes one parameter, so your

$ourUVrounded = round($UVfcstUVI[0],0);

needs to be

$ourUVrounded = floor($UVfcstUVI[0]);

Best regards,
Ken

Thanks Ken

I wounder if I only want to see one day instead of 9 day:
http://www.herlev-vejret.dk/wxsun.php
How do I do that?
(I want to show today icon in menu page.)

Cheers
Steffen, Denmark
http://www.herlev-vejret.dk