/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // calculates the sun position and path throughout the day // input params: LAT&LON // output json // not much commenting in code // ported from http://www.stjarnhimlen.se/comp/tutorial.html // MANY THANKS! // most var-names are identical to above tutorial // get current position getsunpos($LAT, $LON, $year, $month, $day, $hour, $azimuth, $altitude, $sunstate); // get 3 points during day to create circle getsunpos($LAT, $LON, $year, $month, $day, 1, $azm1, $alt1, $ignore); getsunpos($LAT, $LON, $year, $month, $day, 9, $azm2, $alt2, $ignore); getsunpos($LAT, $LON, $year, $month, $day, 13, $azm3, $alt3, $ignore); // transform altitude from radians to number from 0 to 1 // polar to cartesian findCentre($x1,$y1,$x2,$y2,$x3,$y3,$cx,$cy,$r); echo "{\"result\":\"OK\",\"sunstate\":\"$sunstate\",\"azimuth\":$azimuth,\"altitude\":$altitude,"; echo "\"centrex\":$cx,\"centrey\":$cy,\"radius\":$r}"; function getsunpos($LAT, $LON, $year, $month, $day, $hour, &$azimuth, &$altitude, &$sunstate) { // julian date $w = 4.9382415669097640822661983551248 + .00000082193663128794959930855831205818* $d; // (longitude of perihelion) $a = 1.000000 ;// (mean distance, a.u.) $e = 0.016709 - .000000001151 * $d ;// (eccentricity) $M = 6.2141924418482506287494932704807 + 0.017201969619332228715501852561964 * $d ;// (mean anomaly) $oblecl = 0.40909295936270689252387465029835 - .0000000062186081248557962825791102081249 * $d ;// obliquity of the ecliptic $L = $w + $M; // sun's mean longitude $lon = $v + $w; $z = 0.0; $xequat = $x; $RAhours = r2d($RA)/15; $yhor = $y; // check sunshine state if ($alt_d >= 0) $sunstate = "DAY"; else if ($alt_d >= -6) $sunstate = "CIVIL-TWILIGHT"; else if ($alt_d >= -12) $sunstate = "NAUTICAL-TWILIGHT"; else if ($alt_d >= -18) $sunstate = "ASTRONOMICAL-TWILIGHT"; else $sunstate = "NIGHT"; } function r2d($r) { while ($d<0) $d += 360; while ($d>360) $d -= 360; return $d; } // functions below are for the sun's path through the day function findCentre($x1,$y1,$x2,$y2,$x3,$y3,&$cx,&$cy,&$r) { if (!isPerpendicular($x1,$y1,$x2,$y2,$x3,$y3) ) CalcCircle($x1,$y1,$x2,$y2,$x3,$y3,$cx,$cy,$r); else if (!isPerpendicular($x1,$y1,$x3,$y3,$x2,$y2) ) CalcCircle($x1,$y1,$x3,$y3,$x2,$y2,$cx,$cy,$r); else if (!isPerpendicular($x2,$y2,$x1,$y1,$x3,$y3) ) CalcCircle($x2,$y2,$x1,$y1,$x3,$y3,$cx,$cy,$r); else if (!isPerpendicular($x2,$y2,$x3,$y3,$x1,$y1) ) CalcCircle($x2,$y2,$x3,$y3,$x1,$y1,$cx,$cy,$r); else if (!isPerpendicular($x3,$y3,$x2,$y2,$x1,$y1) ) CalcCircle($x3,$y3,$x2,$y2,$x1,$y1,$cx,$cy,$r); else if (!isPerpendicular($x3,$y3,$x1,$y1,$x2,$y2) ) CalcCircle($x3,$y3,$x1,$y1,$x2,$y2,$cx,$cy,$r); else { $cx=0; $cy=0; $r=0; } } function isPerpendicular($x1,$y1,$x2,$y2,$x3,$y3) { $m = 0.00000001; $dya = $y2 - $y1; $dxa = $x2 - $x1; $dyb = $y3 - $y2; $dxb = $x3 - $x2; // checking whether the line of the two pts are vertical //TRACE("The points are pependicular and parallel to x-y axis\n"); return false; } return true; } } function CalcCircle($x1,$y1,$x2,$y2,$x3,$y3,&$cx,&$cy,&$r) { $m = 0.00000001; $dya = $y2 - $y1; $dxa = $x2 - $x1; $dyb = $y3 - $y2; $dxb = $x3 - $x2; $cx= 0.5 * ($x2 + $x3); $cy= 0.5 * ($y1 + $y2); return; } // IsPerpendicular() assure that xDelta(s) are not zero $aSlope = $dya/$dxa; $bSlope = $dyb/$dxb; return; } // calc center $cx = ($aSlope*$bSlope*($y1 - $y3) + $bSlope*($x1 + $x2) - $aSlope*($x2+$x3) )/(2* ($bSlope-$aSlope) ); $cy = -1*($cx - ($x1+$x2)/2)/$aSlope + ($y1+$y2)/2; return; } ?>