Weather Forecast

<?php

/**
 * weather_forecast
 *
 * Display the weather for a location. Look up using the Google weather API.
 *
 * @version 1.4
 * @author Contributors at eXorithm
 * @link /algorithm/view/weather_forecast Listing at eXorithm
 * @link /algorithm/history/weather_forecast History at eXorithm
 * @license /home/show/license
 *
 * @param mixed $location The location to get the weather for. Can be a city, zip code, or postal code.
 * @param mixed $units The units to get the temperature in.
 * @param string $header_color (hex color code) Color of the header
 * @param string $day_header_color (hex color code) Color of the header for each of the days
 * @param string $day_color (hex color code) Color of the cells containing the forecasts
 * @return string HTML
 */
function weather_forecast($location='Sydney, Australia',$units='c',$header_color='f0f0f0',$day_header_color='d0ffd0',$day_color='f0f0f0')
{
	$weather = file_get_contents('http://www.google.com/ig/api?weather='.urlencode($location));
	
	$xml = simplexml_load_string($weather);
	
	if (!isset($xml->weather->forecast_conditions)) {
		throw new Exception('Data could not be retreived for location '.$location);
	}
	
	$location = $xml->weather->forecast_information->city['data'];
						                                          
	// four day outlook
	for ($i = 0; $i < 4; $i++){
		if ($xml->weather->forecast_conditions->$i) {
			$forecast_day[] = $xml->weather->forecast_conditions->$i->day_of_week['data'];
			$forecast_condition[] = $xml->weather->forecast_conditions->$i->condition['data'];
			$low = $xml->weather->forecast_conditions->$i->low['data'];
			if ($units=='k')
				$low = round((($low - 32)*5/9) + 273, 1);
			else if ($units=='c')
				$low = round(($low - 32)*5/9, 1);
			$forecast_low[] = $low;
			$high = $xml->weather->forecast_conditions->$i->high['data'];
			if ($units=='k')
				$high = round((($high - 32)*5/9) + 273, 1);
			else if ($units=='c')
				$high = round(($high - 32)*5/9, 1);
			$forecast_high[] = $high;
			$forecast_icon[] = $xml->weather->forecast_conditions->$i->icon['data'];
		}
	}
	
	// current
	$condition = $xml->weather->current_conditions->condition['data'];
	$temp = $xml->weather->current_conditions->temp_f['data'];
	if ($units=='k')
		$temp = round((($temp - 32)*5/9) + 273, 1);
	else if ($units=='c')
		$temp = round(($temp - 32)*5/9, 1);
	$icon = $xml->weather->current_conditions->icon['data'];
	
	// build the HTML
	$header = "<tr><td colspan=\"".count($forecast_day)."\" bgcolor=\"#$header_color\">";
	if ($icon!='')
		$header .= "<img align=\"left\" src=\"http://www.google.com$icon\">";
	$header .= "<b>$location</b><br>Currently <i>$condition</i> <b>$temp$units</b>";
	$header .= "</td></tr>\n<tr>";
	$data = "<tr>\n";
	
	for ($i = 0; $i < count($forecast_day); $i++){
		$header .= "<td width=\"130\" bgcolor=\"#$day_header_color\"><b>$forecast_day[$i]</b></td>";
		$data .= "<td bgcolor=\"#$day_color\">";
		$data .= "<img src=\"http://www.google.com$forecast_icon[$i]\">";
		$data .= "<br><i>$forecast_condition[$i]</i>";
		$data .= "<br>high <b>$forecast_high[$i]$units</b>";
		$data .= "<br>low <b>$forecast_low[$i]$units</b>";
		$data .= "</td>\n";
	}
	
	$header .= "</tr>";
	$data .= "</tr>";
	
	return "<table cellpadding=\"5\" cellspacing=\"3\">\n$header\n$data\n</table>";
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm ascii_art

function ascii_art ($img
{
  $width = imagesx$img);
  $height = imagesy$img);
  
  $ascii = '<pre style="font-size:8px;line-height:1px;letter-spacing:-2px;">'
  
  for$y=0;$y$height$y++)
  {
    for$x=0;$x$width$x++)
    {
      $c = imagecolorat$img$x$y);
      $r = ($c >> 16) & 0xFF;
      $g = ($c >> 8) & 0xFF;
      $b = $c & 0xFF;
      $t = ($c >> 24) & 0xFF;
      if ($t>=0)
      {
        /** transparency **/
        $r = $r+(255-$r)*$t/127;
        $g = $g+(255-$g)*$t/127;
        $b = $b+(255-$b)*$t/127;
      }
      $r = floor$r/16);
      $g = floor$g/16);
      $b = floor$b/16);
      $ascii .= '<span style="color:#'dechex$r).dechex$g).dechex$b).'">#</span>'
    }
    $ascii .= '<br>'
  }
  
  $ascii .= '</pre>'
  
  return $ascii

eXorithm – Execute Algorithm: View / Run Algorithm project_polygon

function project_polygon ($points, $degree_x, $degree_y, $degree_z, $center_x, $center_y, $center_z, $dist1, $dist2, $include_z
{
  // check points
  if ((count$points)%3)!=0) {
    throw new Exception'The points must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
  }
  
  $degree_x = deg2rad$degree_x);
  $degree_y = deg2rad$degree_y);
  $degree_z = deg2rad$degree_z);
  
  $cosx = cos$degree_x);
  $sinx = sin$degree_x);
  $cosy = cos$degree_y);
  $siny = sin$degree_y);
  $cosz = cos$degree_z);
  $sinz = sin$degree_z);
  
  $array = array();
  
  for ($i=0;$icount$points);$i$i+3) {
    $x0 = $points$i]-$center_x
    $y0 = $points$i+1]-$center_y
    $z0 = $points$i+2]-$center_z
    
    $x1 = $cosy*($sinz$y0 + $cosz$x0) - $siny$z0
    $y1 = $sinx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) + $cosx*($cosz$y0 - $sinz$x0);
    $z1 = $cosx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) - $sinx*($cosz$y0 - $sinz$x0);
  
    $x2 = $x1$dist1/($z1$dist1$dist2);
    $y2 = $y1$dist1/($z1$dist1$dist2);
    $z2 = $z1$dist1/($z1$dist1$dist2);
  
    $array[] = $x2
    $array[] = $y2
    if ($include_z) $array[] = $z2
  }
  
  return $array
} 

eXorithm – Execute Algorithm: History For Algorithm weather_forecast

weather_forecast     version 1.4     Display the weather for a location. Look up using the Google weather API.

Version Note Created Diff
1.4 [edit Reverted from version 1.1 Apr 23, 2012 09:40 am by Mike Campbell
1.3 [revert Mar 22, 2012 03:35 pm by jessiie
1.2 [revert Mar 22, 2012 03:24 pm by jessiie
1.1 [revert support bth celsius and kelvin Nov 23, 2011 01:43 am by Mike Campbell
1.0 [revert Celsius is now Kelvin Nov 20, 2011 11:32 pm by ekajjake
0.5 [revert Nov 20, 2011 11:32 pm by ekajjake
0.4 [revert Nov 20, 2011 11:31 pm by ekajjake
0.3 [revert Nov 20, 2011 11:30 pm by ekajjake
0.2 [revert Dec 1, 2010 02:34 pm by Mike Campbell
0.1 [revert Oct 27, 2010 02:02 pm by Mike Campbell

eXorithm – Execute Algorithm: View / Run Algorithm word_counts

function word_counts ($text, $noise
{
  $words = preg_split'/[^A-Za-z]+/', strtolower$text));
  $counts = array();
  
  foreach ($words as $word) {
    if (strlen$word)>1) { // 1-letter words are ignored
      if (array_search$word, $noise)===false) { // noise word?
        if (array_key_exists$word, $counts)) {
          $counts$word] = $counts$word]+1;
        } else {
          $counts$word] = 1;
        }
      }
    }
  }
  
  return $counts
} 

eXorithm – Execute Algorithm: View / Run Algorithm binary_search

function binary_search ($array$item$low$high
{
  if ($low==-1) $low = 0;
  if ($high==-1) $high = count$array)-1;
  
  if ($low > $high) {
    // item not found
    return -1;  
  }  
  
  // get the middle
  $middle = floor(($low$high)/2);
  if ( $array$middle] == $item ) {
    // found it
    return $middle
  } elseif ($item < $array$middle]) {
    // search left
    return binary_search$array$item$low$middle-1);
  } else {
    // search right
    return binary_search$array$item$middle+1, $high);
  }

eXorithm – Execute Algorithm: History For Algorithm draw_cube

draw_cube ย ย ย  version 0.8 ย ย ย  Draw a 3d cube.
Version Note Created Diff
0.8 [edit Oct 29, 2010 10:20 am by Mike Campbell
0.7 [revert add rainbow option Oct 26, 2010 02:21 pm by Mike Campbell
0.6 [revert Oct 15, 2010 12:37 am by Mike Campbell
0.5 [revert Oct 14, 2010 11:24 pm by Mike Campbell
0.4 [revert support for solid sides using painter’s algorithm Oct 14, 2010 09:20 am by Mike Campbell
0.3 [revert Oct 12, 2010 10:40 am by Mike Campbell
0.2 [revert Oct 12, 2010 10:31 am by Mike Campbell
0.1 [revert Oct 12, 2010 10:29 am by Mike Campbell