Author Topic: PHP Sun Elevation script  (Read 3409 times)

0 Members and 1 Guest are viewing this topic.

Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
PHP Sun Elevation script
« on: May 31, 2017, 05:12:45 AM »
Hi Mates

I am searching for a PHP-script that can give me the current elevation of the sun when the script is feed with latitude, longitude and a timestamp.

Thanks
Kind Regards

Offline R_o_B

  • WxElement panel
  • Senior Member
  • *****
  • Posts: 85
Re: PHP Sun Elevation script
« Reply #1 on: May 31, 2017, 07:37:12 AM »
Have you checked the 'date_sun_info' PHP function...

Or are you looking for a function that would give you the elevation of the sun in degrees above the horizon?
R_o_B
---
eMail: weather@herray.net

Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: PHP Sun Elevation script
« Reply #2 on: May 31, 2017, 07:38:55 AM »
Have you checked the 'date_sun_info' PHP function...

Yes.

Or are you looking for a function that would give you the elevation of the sun in degrees above the horizon?

Yes, thats what I'm looking for.

Kind Regards

Offline jmcmurry

  • Jim McMurry
  • Forecaster
  • *****
  • Posts: 528
  • Davis Vantage Pro 2 Plus Wireless.
    • Juneau County Weather
Re: PHP Sun Elevation script
« Reply #3 on: May 31, 2017, 08:04:48 AM »
I've been using this

Code: [Select]
function sun_pos($UnixTimestamp, $latitude, $longitude) {
// This function from sunpath.php http://www.weather-watch.com/smf/index.php/topic,39197.0.html - Thanks Jozef (Pinto)
$year = date("Y",$UnixTimestamp);
$month = date("n",$UnixTimestamp);
$day = date("j",$UnixTimestamp);
$UT = gmdate("H",$UnixTimestamp)+gmdate("i",$UnixTimestamp)/60+gmdate("s",$UnixTimestamp)/3600;
$d = round(367 * $year - (7 * ($year + (($month + 9)/12)))/4 + (275 * $month)/9 + $day - 730530);
$w = 282.9404 + 0.0000470935 * $d;
$e = 0.016709 - 0.000000001151 * $d;
$M = 356.0470 + 0.9856002585 * $d;
$M = fmod($M , 360);
if ($M < 0){$M = $M+360;}
$L = $w + $M;
$L = fmod($L , 360);
$oblecl = 23.4393 - 0.0000003563 * $d ;
$E = rad2deg(deg2rad($M) + (180/M_PI) * deg2rad($e) * sin(deg2rad($M)) * (1 + $e * cos(deg2rad($M))));
$x = cos(deg2rad($E)) - $e;
$y = sin(deg2rad($E)) * sqrt(1 - $e*$e);
$r = sqrt($x*$x + $y*$y);
$v = rad2deg(atan2( deg2rad($y), deg2rad($x)) );
$lon = $v + $w;
$lon = fmod($lon, 360);
$x_rect = $r * cos(deg2rad($lon));
$y_rect = $r * sin(deg2rad($lon));
$z_rect = 0.0;
$x_equat = $x_rect;
$y_equat = $y_rect * cos(deg2rad($oblecl)) + $z * sin(deg2rad($oblecl));
$z_equat = $y_rect * sin(deg2rad($oblecl)) + $z * cos(deg2rad($oblecl));
$r    = sqrt( $x_equat*$x_equat + $y_equat*$y_equat + $z_equat*$z_equat );
$RA   = rad2deg(atan2( deg2rad($y_equat), deg2rad($x_equat) ));
$Decl = rad2deg(atan2( deg2rad($z_equat), deg2rad(sqrt( $x_equat*$x_equat + $y_equat*$y_equat ))) );
$GMST0 = $L/15 + 12;
$SIDTIME = $GMST0 + $UT + $longitude/15;
$HA = $SIDTIME*15 - $RA;
$x = cos(deg2rad($HA)) * cos(deg2rad($Decl));
$y = sin(deg2rad($HA)) * cos(deg2rad($Decl));
$z = sin(deg2rad($Decl));
$xhor = $x * cos(deg2rad(90) - deg2rad($latitude)) - $z * sin(deg2rad(90) - deg2rad($latitude));
$yhor = $y;
$zhor = $x * sin(deg2rad(90) - deg2rad($latitude)) + $z * cos(deg2rad(90) - deg2rad($latitude));
$azimuth  = round(rad2deg(atan2($yhor, $xhor)) + 180,2);
$altitude = round(rad2deg(asin($zhor )),2);      //
$pos = array($azimuth, $altitude);
return $pos;
}

$Sun = sun_pos(time(),$Lat,$Lon);
$SunElev = $Sun[1];

- Jim

Forum Search and Google Can be Your Best Friends

Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: PHP Sun Elevation script
« Reply #4 on: May 31, 2017, 09:47:11 AM »
I've been using this

Code: [Select]
function sun_pos($UnixTimestamp, $latitude, $longitude) {
// This function from sunpath.php http://www.weather-watch.com/smf/index.php/topic,39197.0.html - Thanks Jozef (Pinto)
$year = date("Y",$UnixTimestamp);
$month = date("n",$UnixTimestamp);
$day = date("j",$UnixTimestamp);
$UT = gmdate("H",$UnixTimestamp)+gmdate("i",$UnixTimestamp)/60+gmdate("s",$UnixTimestamp)/3600;
$d = round(367 * $year - (7 * ($year + (($month + 9)/12)))/4 + (275 * $month)/9 + $day - 730530);
$w = 282.9404 + 0.0000470935 * $d;
$e = 0.016709 - 0.000000001151 * $d;
$M = 356.0470 + 0.9856002585 * $d;
$M = fmod($M , 360);
if ($M < 0){$M = $M+360;}
$L = $w + $M;
$L = fmod($L , 360);
$oblecl = 23.4393 - 0.0000003563 * $d ;
$E = rad2deg(deg2rad($M) + (180/M_PI) * deg2rad($e) * sin(deg2rad($M)) * (1 + $e * cos(deg2rad($M))));
$x = cos(deg2rad($E)) - $e;
$y = sin(deg2rad($E)) * sqrt(1 - $e*$e);
$r = sqrt($x*$x + $y*$y);
$v = rad2deg(atan2( deg2rad($y), deg2rad($x)) );
$lon = $v + $w;
$lon = fmod($lon, 360);
$x_rect = $r * cos(deg2rad($lon));
$y_rect = $r * sin(deg2rad($lon));
$z_rect = 0.0;
$x_equat = $x_rect;
$y_equat = $y_rect * cos(deg2rad($oblecl)) + $z * sin(deg2rad($oblecl));
$z_equat = $y_rect * sin(deg2rad($oblecl)) + $z * cos(deg2rad($oblecl));
$r    = sqrt( $x_equat*$x_equat + $y_equat*$y_equat + $z_equat*$z_equat );
$RA   = rad2deg(atan2( deg2rad($y_equat), deg2rad($x_equat) ));
$Decl = rad2deg(atan2( deg2rad($z_equat), deg2rad(sqrt( $x_equat*$x_equat + $y_equat*$y_equat ))) );
$GMST0 = $L/15 + 12;
$SIDTIME = $GMST0 + $UT + $longitude/15;
$HA = $SIDTIME*15 - $RA;
$x = cos(deg2rad($HA)) * cos(deg2rad($Decl));
$y = sin(deg2rad($HA)) * cos(deg2rad($Decl));
$z = sin(deg2rad($Decl));
$xhor = $x * cos(deg2rad(90) - deg2rad($latitude)) - $z * sin(deg2rad(90) - deg2rad($latitude));
$yhor = $y;
$zhor = $x * sin(deg2rad(90) - deg2rad($latitude)) + $z * cos(deg2rad(90) - deg2rad($latitude));
$azimuth  = round(rad2deg(atan2($yhor, $xhor)) + 180,2);
$altitude = round(rad2deg(asin($zhor )),2);      //
$pos = array($azimuth, $altitude);
return $pos;
}

$Sun = sun_pos(time(),$Lat,$Lon);
$SunElev = $Sun[1];

- Jim

Thanks Jim

Working fine, except it gives two errors/notices:

Notice: Undefined variable: z in C:\xampp\htdocs\sun.php on line 30

Notice: Undefined variable: z in C:\xampp\htdocs\sun.php on line 31


It makes sense since z is defined at line 40, but moving it up before line 30 is just giving more notices.

Maybe our local PHP-expert Jáchym can fix up the script  :grin:

Kind Regards

« Last Edit: May 31, 2017, 12:11:08 PM by droiddk »

Offline jmcmurry

  • Jim McMurry
  • Forecaster
  • *****
  • Posts: 528
  • Davis Vantage Pro 2 Plus Wireless.
    • Juneau County Weather
Re: PHP Sun Elevation script
« Reply #5 on: June 01, 2017, 02:50:27 PM »
I think we'll need to see what you have on lines 30 & 31.  There shouldn't be any variable z ... the function uses $z.

Maybe post your whole test script.

- Jim

Forum Search and Google Can be Your Best Friends

Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: PHP Sun Elevation script
« Reply #6 on: June 01, 2017, 04:17:04 PM »
I think we'll need to see what you have on lines 30 & 31.  There shouldn't be any variable z ... the function uses $z.

Maybe post your whole test script.

- Jim

Your script = my whole script, nothing else.

z = $z, I forgot $

$z is being used here:    $y_equat = $y_rect * cos(deg2rad($oblecl)) + $z * sin(deg2rad($oblecl)); but $z is first defined later in the script ($z = sin(deg2rad($Decl)) ).

Regards
« Last Edit: June 01, 2017, 04:19:56 PM by droiddk »

Offline jmcmurry

  • Jim McMurry
  • Forecaster
  • *****
  • Posts: 528
  • Davis Vantage Pro 2 Plus Wireless.
    • Juneau County Weather
Re: PHP Sun Elevation script
« Reply #7 on: June 01, 2017, 09:26:35 PM »
What I provided wasn't a working script, but I too now wonder about $z.  I'll put together a working test script this weekend and we'll see what comes of that.

- Jim

Forum Search and Google Can be Your Best Friends

Offline jmcmurry

  • Jim McMurry
  • Forecaster
  • *****
  • Posts: 528
  • Davis Vantage Pro 2 Plus Wireless.
    • Juneau County Weather
Re: PHP Sun Elevation script
« Reply #8 on: June 02, 2017, 09:20:20 AM »
Please try the attached test script which initializes $z and should get rid of your error.  It turns out that the value of $z makes no difference at all.  It'll take a mathematician to tell us why that is and that certainly isn't me.

- Jim

Forum Search and Google Can be Your Best Friends

Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: PHP Sun Elevation script
« Reply #9 on: June 03, 2017, 10:09:40 AM »
Please try the attached test script which initializes $z and should get rid of your error.  It turns out that the value of $z makes no difference at all.  It'll take a mathematician to tell us why that is and that certainly isn't me.

- Jim

Thanks Jim, no notices now. I actually did the same to avoid the notices but also wonder how it would affect the calculation.

Kind Regards

Offline R_o_B

  • WxElement panel
  • Senior Member
  • *****
  • Posts: 85
Re: PHP Sun Elevation script
« Reply #10 on: June 04, 2017, 08:00:58 PM »
I have tried different values for $z - I have settled with 0.375.

I tested the function by comparing its calculated value with the one produced by the  NOAA Solar Calculator - I simply changed the variable $z until the value calculated by the function was close to the value provided on the NOAA page.  ;)

If interested, I also changed the line producing the azimuth angle to:
Code: [Select]
$azimuth = round( rad2deg( atan2( $yhor, $xhor )) + 160, 1 );This modification brings the value for the azimuth closer to the one produced by the NOAA Solar Calculator.
R_o_B
---
eMail: weather@herray.net