Graphic sun position

Got the OFC going a little bit…I’m having obvious data plotting problems though:

http://www.relayweather.com/wxflashchart-test.php

Michael,

Could you send me OFC script that you are working on and I can play with it a bit.

Chuck

Yeah, please post the code, looks promising :smiley:

Code for OFC here…The log/lat/tz are for Chuck’s station. Let me know if you guys can figure this out!

<?php
#Script updated on 14 April 2009
#Thanks to input from Forum-members
#http://discourse.weather-watch.com/t/39881

// allow viewing of generated source

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;
}
//Start of script
###############################################################
#Settings                                                     #
###############################################################
include_once( 'ofc-library/open-flash-chart.php' );
$hostloc = "./";


/*  GLOBAL VARIABLES */
$lat = 47.11967;
$lon = 104.70314;//unlike other scripts, this is East negative
$SITE['TZ'] = "America/Denver";//set your timezone
###############################################################
#End of settings                                              #
###############################################################

set_tz( $SITE['TZ'] );
$diffUTC=date("O",time())/100;
$timeadj = $lon * (1/15);
$solarct = 1368;
$sunavgdist = 149600000;


/* FUNCTIONS */
function set_tz ($TZ){
    if (phpversion() >= "5.1.0") {
        date_default_timezone_set($TZ);
    } else {
        putenv("TZ=" . $TZ);
    }
}

function convertRadtoGrad ($angle) {		// Converts radians to sexagesimal degrees

	$angle = $angle * 57.29578;
	return $angle;
	}

function convertGradtoRad ($angle) {		// Converts sexagesimal degrees to radians

	$angle = $angle / 57.29578;
	return $angle;
	}
	
/*
function eccentricity ($day)			// Function to know sun eccentricity
{
	$angle = (2*M_PI*$day)/365;
	$ecc = 1 + (0.033 * cos($angle));
	return $ecc;
}
*/
function declination ($day)				// Function to know sun declination
{
	$dec = 23.45 * cos(2*M_PI*(($day-173)/365));
	return $dec;
}

function hourangle ($hour)				// Function to know hour angle
{
	$hangle = ((2*M_PI*$hour)/24)-M_PI;
	return $hangle;
}

function local_time ($hour)				// Function to know local hour
{
	$localhour = $hour[hours];
	$localminute = $hour[minutes];
	$localsecond = $hour[seconds];
	
	$local_time = $localhour + ($localminute/60) + ($localsecond/3600);
	return $local_time;
}

function solartime ($hour)				// Function to know solar hour from local hour
{
	global $diffUTC;
	global $timeadj;

	$solarhour = $hour[hours];
	$solarhour = $solarhour - $diffUTC;
	$solarminute = $hour[minutes] - ($timeadj*60);
	$solarsecond = $hour[seconds];
	
	$solartime = $solarhour + ($solarminute/60) + ($solarsecond/3600);
	return $solartime;
}

function sunheightsin ($hour)			// Function to know sun height sine
{
	global $lat;

	$angle1 = declination($hour[yday]);
	$angle1 = convertGradtoRad($angle1);
	$angle2 = convertGradtoRad($lat);
	
	$sin = (sin($angle1)*sin($angle2))+(cos($angle1)*cos($angle2)*cos(hourangle(solartime($hour))));
	return $sin;
}


/* CREATE SUN PATH */

	$dusk = 5;
	$dawn = 22;
	$az = array();
	$he = array();
	
	for($h=$dusk; $h<=$dawn; ++$h) {
		$time = mktime($h);
		$time = getdate($time);
		$azimuth = convertRadtoGrad(hourangle(solartime($time)));
		$azimuth = $azimuth+180;
		$height = convertRadtoGrad(asin(sunheightsin($time)));
		$az[] = round($azimuth,0);
		$he[] = round($height,0);
	}

//Hi position
$sun_info = date_sun_info(strtotime("21 June".date("Y",time())), $lat,$long);
$b = array();
  $a = ($sun_info[transit]-39600);
for ($i=0;$i<=24;$i++){
$a=$a+3600;
$b[] = $a;
}
foreach ($b as $v) {
		$time1 = getdate($v);
    $azimuth1 = convertRadtoGrad(hourangle(solartime($time1)));
    		$azimuth1 = $azimuth1+180;
		$height1 = convertRadtoGrad(asin(sunheightsin($time1)));
		$az0[] = round($azimuth1,0);
		$he0[] = round($height1,0);
}
//Low position
$sun_info = date_sun_info(strtotime("21 December".date("Y",time())), $lat,$long);
$b = array();
  $a = ($sun_info[transit]-39600);
for ($i=0;$i<=24;$i++){
$a=$a+3600;
$b[] = $a;
}
foreach ($b as $v) {
		$time2 = getdate($v);
    $azimuth2 = convertRadtoGrad(hourangle(solartime($time2)));
    		$azimuth2 = $azimuth2+180;
		$height2 = convertRadtoGrad(asin(sunheightsin($time2)));
		$az1[] = round($azimuth2,0);
		$he1[] = round($height2,0);
}

/* CREATE ACTUAL SUN POSITION */

	$az2 = array();
	$he2 = array ();
	$time2 = getdate();
	$azimuth2 = convertRadtoGrad(hourangle(solartime($time2)));
	$azimuth2 = ($azimuth2+180);
	$height2 = convertRadtoGrad(asin(sunheightsin($time2)));
	$az2[0] = round($azimuth2,0);
	$he2[0] = round($height2,0);



/* Start OFC Graph Code Here */

$g = new graph();
$g->title( 'Sun Position', '{font-size: 20px; color: #040400}' );
$g->bg_colour = '88B5B3';

// we add 3 sets of data:
$g->set_data( $he, $az );
$g->set_data( $he0, $az0 );
$g->set_data( $he1, $az1 );

// we add the 3 line types and key labels
$g->line( 2, '#F8FF46', 'Sun Path', 10 );
$g->line( 2, '#28A737', 'Hi', 10);    // <-- 3px thick + dots
$g->line( 2, '#FFFFFF', 'Lo', 10 );

$g->set_x_labels(array (60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190 ,200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300));
$g->set_x_label_style( 10, '0x000000', 0, 2 );
$g->set_x_legend( 'Azimuth', 12, '#040400' );

$g->set_y_min( 0 );
$g->set_y_max( 90 );
$g->y_label_steps( 9 );
$g->set_y_legend( 'Elevation', 12, '#040400' );
echo $g->render();
?>

Thanks a lot. That works now and I understood what you have meant.

Regards

Jose

Hi mth

I tried your OFC but failed miserably, i ented long and lat and time zone but sommat aint right still

http://www.northantsweather.com/ofc/sunpositionoutput.php

You should redo the calculation, or so.
The arrays are don’t have an equal number of values.
When I was playing with OFC, I thought I read somewhere they must have an equal number of values.

EDIT: screenshot in attachment


Sorry for being late at this thread, I see you’ve already solved all problems :slight_smile:

Pinto, I have a very early stage for a moon position script. Lots of calculations but I’ll try. Not just moon position, I’m working in moon phase as well.

:smiley: Very good news.

Hey there pinto!

The calculation is the exact same calculation as the JP Graph version. Does the JPGraph version have equal number of values?

yes, I know, but JPgraph can handle arrays with a different number of values

Pinto,

Ahhh, I see. :slight_smile: I will look into this.

Michael

I have the most recent version of JPGraph installed and this script was failing… I had to change the script to use the SetPos command rather than Pos() as shown below to get it working again.

FYI

$text =new Text(“Jun 21”);
$text->SetPos( 185,60);
$text->SetColor( “white”);
$graph->AddText($text);
$text1 =new Text(“Dec 21”);
$text1->SetPos( 185,170);
$text1->SetColor( “white”);
$graph->AddText($text1);

:smilecolros:

Thank you Jim :smiley:

and another Update

EDIT: Thank you Stuntman
updated

hi everybody

thank you for the latest update on the Sun script
it looks brilliant on my webpage next to the author UV/solar graph

mick

Hi All
I have just been reading through this thread, Being a novice at coding etc I don’t understand very much of whats being said, however undaunted I downloaded the latest update of the file from Pinto’s last post, I changed the Tz and lat and long position but having uploaded it to the server I am just seeing a blank page, I assume I have missed a vital part somewhere. Any (basic) help much appreciated.

Here’s the link to the page, I have changed the php file name to sunpos http://www.weybourneweather.co.uk/sunpos.php

Phil

Excellent job on the update, thank you
http://www.northantsweather.com/wxuv.php

Phil,

You should place the file in the directory where the jpgraph directory is in.

Is your scripts data true to the data found here??
http://jamesrbass.com/sun/sunform.aspx

My data is off significantly…
Graph states: 293 -12
Sunform states 303 -20

Mines off alot according to that page also.