<?php
require_once 'localtime2.php';
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
##  Whilst this source code is freely available to view, it is strictly for educational use only. 
##  All the code is written by myself, and most of the equations are derived from 'Astronomical Algorithms' by Jean Meeus. 
##  The code is therefore copyrighted and no part may be reproduced without permission.
##  David Campbell
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
  <link rel="stylesheet" type="text/css" href="printstyle.css" media="print" />  
  <link rel="stylesheet" type="text/css" href="astrowindow.css" media="screen" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

<div class="wholepage">

<div id="content">


<?php
$gtime 
microtime();
include(
"jde.php");
$t = ($JDE 2451545)/365250;
$T 10 $t;

if(isset(
$_COOKIE['latitude'])){
    
$userlat $_COOKIE['latitude']; 
$latlong 0;
}else{
    
$latlong 1;
}

if(isset(
$_COOKIE['longitude'])){
    
$userlong $_COOKIE['longitude']; 
$latlong 0;
}else{
    
$latlong 1;
}    

//////////////earth Data
$planet 'earth';
$R=r($planet,$t);

$l=l($planet,$t);

$B=b($planet,$t);
$ldegs rad2deg($l);
$l1quad($ldegs);
$Slon$l pi();

$Ldeg2rad($l1);

$x- ($R * (sin($L)));
$y$R * (cos($L));
$xearth= ($x/$au2)+570-2;
$yearth168.5-($y/$au2)-2;
$rearth=  round($R,9);
$r2earth number_format($R*$aul);
$Bdegs rad2deg($B);

$Slat $B;

$epsilon epsilon($T);

$Sra = (atan2((  ((cos(deg2rad($epsilon)))*(sin($Slon)))-((tan($Slat))*(sin(deg2rad($epsilon))))),(cos($Slon))));
$Sdec asin(((sin($Slat))*(cos(deg2rad($epsilon))))+((cos($Slat))*(sin(deg2rad($epsilon)))*(sin($Slon))));


//Moon angles

$Ld218.3164477 + (481267.88123421*$T) - (0.0015786*$T*$T)+(($T*$T*$T)/538841)-(($T*$T*$T*$T)/65194000);
$Ld quad($Ld);

$D =297.8501921 + (445267.1114034*$T) - (0.0018819*$T*$T)+(($T*$T*$T)/545868)-(($T*$T*$T*$T)/113065000);
$D quad($D);

$M  357.5291092 + (35999.0502909*$T) - (0.0001536*$T*$T)+(($T*$T*$T)/24490000);
$M quad($M);

$Md  134.9633964 + (477198.8675055*$T) + (0.0087414*$T*$T)+(($T*$T*$T)/69699)-(($T*$T*$T*$T)/14712000);
$Md quad($Md);

$F 93.2720950 + (483202.0175233*$T) - (0.0036539*$T*$T)+(($T*$T*$T)/3526000)-(($T*$T*$T*$T)/863310000);
$F quad($F);


$A1=119.75+(131.849*$T);
$A1 quad($A1);
$A2=53.09+(479264.290*$T);
$A2 quad($A2);
$A3=313.45+(481266.484*$T);
$A3 quad($A3);
$E=1- (0.002516*$T)-(0.0000074*$T*$T);

include(
"elp2000-82.php");

$lambda $Ld + ($long/3600);
$lambda22 $lambda;
$betadeg2rad($lat/3600);




$omega 125.04452 - (1934.136261*$T) + (0.0020708 *$T $T) + (($T*$T*$T)/450000);
$L 280.4665 + (36000.7698 $T);
$Ldash 218.3165 + (481267.8813 $T);
$psi = (((-17.20/3600)*(sin(deg2rad($omega))))-((1.32/3600)*(sin(deg2rad(2*$L))))-((0.23/3600)*(sin(deg2rad(2*$Ldash))))-((0.21/3600)*(sin(deg2rad(2*$omega)))));


$lambda deg2rad($lambda $psi);



// ra and dec
$ra = (quad(rad2deg(atan2((  ((cos(deg2rad($epsilon)))*(sin($lambda)))-((tan($beta))*(sin(deg2rad($epsilon))))),(cos($lambda))))))/15;
$rageo $ra;
$dec rad2deg(asin(((sin($beta))*(cos(deg2rad($epsilon))))+((cos($beta))*(sin(deg2rad($epsilon)))*(sin($lambda)))));
$decgeo $dec;
$delta=$dist/149597870.691;

$delta3=$delta;
$dist3=$dist;

///parallax
if ($latlong == 1){
}else{
$ra15 =$ra*15;
$sinpi = (sin(deg2rad(8.794/3600)))/$delta;
$ast  quad(280.46061837+(360.98564736629*($JDE-2451545))+(0.000387922*$T*$T)-(($T*$T*$T)/38710000)+(($psi*cos(rad2deg($epsilon)))/15));

$H 70;
$ruserlat =deg2rad($userlat);
$ruserlong=deg2rad($userlong);
$rra=deg2rad($ra15);
$rdec=deg2rad($dec);
$c=- (1/298.257);
$u =atan($c*tan($ruserlat));
$psin =$c*sin($u)+($H/6378140)*sin($ruserlat);
$pcos =cos($u)+($H/6378140)*cos($ruserlat);
$H =deg2rad(($ast)+$userlong-($ra*15));
$A= (cos($rdec))*(sin($H));
$B= (cos($rdec)*cos($H))-($pcos*$sinpi);
$C= (sin($rdec))-($psin*$sinpi);
$q sqrt(($A*$A)+($B*$B)+($C*$C));
$Hd rad2deg(atan2($A,$B));
$dec rad2deg(asin($C/$q));
$ra = (quad($ast $userlong $Hd))/15;}



$dra d($ra);
$mra m($ra);
$sra s($ra);
$ddec d($dec);
$mdec m($dec);
$sdec s($dec);
//angular diameter
$sinpi = (sin(deg2rad(8.794/3600)))/$delta;

$k=0.272481;
$s= (rad2deg(asin($k*$sinpi)))*120;
$delta=$dist;


//fraction of illuminated disk
$deltaau $delta/149597870.691;
$Psi acos((cos($beta))*(cos($lambda-$Slon)));

$i rad2deg(atan2($R*sin($Psi),$deltaau-($R*cos($Psi))));

$k= (1+(cos(deg2rad(($i)))))/2;


//position angle of the bright limb
$ra1 deg2rad($ra*15);
$dec1 deg2rad($dec);
$x quad(rad2deg(atan2(cos($Sdec)*sin($Sra-$ra1),(sin($Sdec)*cos($dec1))-(cos($Sdec)*sin($dec1)*cos($Sra-$ra1)))));



$phaseround(($k*100),1);
$delta number_format($delta);
$sround($s,2);

//libration - optical
$I deg2rad(1.5424166666);
$lambda rad2deg($lambda);
$omega $omega 125.0445479 - (1934.1362891*$T) + (0.0020754 *$T $T) + (($T*$T*$T)/467441) -(($T*$T*$T*$T)/60616000);
$W  deg2rad($lambda-$psi-$omega);
$lambda deg2rad($lambda);
$A rad2deg(atan2((sin($W)*cos($beta)*cos($I))-(sin($beta)*sin($I)),cos($W)*cos($beta)));
$l = ($A-$F);
if (
$l< -90){
$l $l - (( (round($l/360,0)))*360);
}else{
if (
$l 90){
$l $l - ((floor($l/360))*360);
}else{
$l $l;
}}
$b asin(((-sin($W))*cos($beta)*sin($I))-(sin($beta)*cos($I)));
//physical libration
$K1 119.75 + (131.849*$T);
$K2 72.56 + (20.186*$T);

$rho 
-
0.02752*(cos(deg2rad($Md)))
-
0.02245*(sin(deg2rad($F)))
+
0.00684*(cos(deg2rad($Md-2*$F)))
-
0.00293*(cos(deg2rad(2*$F)))
-
0.00085*(cos(deg2rad(2*$F-2*$D)))
-
0.00054*(cos(deg2rad($Md-2*$D)))
-
0.00020*(sin(deg2rad($Md+$F)))
-
0.00020*(cos(deg2rad($Md+2*$F)))
-
0.00020*(cos(deg2rad($Md-$F)))
+
0.00014*(cos(deg2rad($Md+2*$F-2*$D)));

$sigma =
-
0.02816*(sin(deg2rad($Md)))
+
0.02244*(cos(deg2rad($F)))
-
0.00682*(sin(deg2rad($Md-2*$F)))
-
0.00279*(sin(deg2rad(2*$F)))
-
0.00083*(sin(deg2rad(2*$F-2*$D)))
+
0.00069*(sin(deg2rad($Md-2*$D)))
+
0.00040*(cos(deg2rad($Md+$F)))
-
0.00025*(sin(deg2rad(2*$Md)))
-
0.00023*(sin(deg2rad($Md+2*$F)))
+
0.00020*(cos(deg2rad($Md-$F)))
+
0.00019*(sin(deg2rad($Md-$F)))
+
0.00013*(sin(deg2rad($Md+2*$F-2*$D)))
-
0.00010*(cos(deg2rad($Md-3*$F)));


$tau 
+
0.02520*$E*(sin(deg2rad($M)))
+
0.00473*(sin(deg2rad(2*$Md-2*$F)))
-
0.00467*(sin(deg2rad($Md)))
+
0.00396*(sin(deg2rad($K1)))
+
0.00276*(sin(deg2rad(2*$Md-2*$D)))
+
0.00196*(sin(deg2rad($omega)))
-
0.00183*(cos(deg2rad($Md-$F)))
+
0.00115*(sin(deg2rad($Md-2*$D)))
-
0.00096*(sin(deg2rad($Md-$D)))
+
0.00046*(sin(deg2rad(2*$F-2*$D)))
-
0.00039*(sin(deg2rad($Md-$F)))
-
0.00032*(sin(deg2rad($Md-$M-$D)))
+
0.00027*(sin(deg2rad(2*$Md-$M-2*$D)))
+
0.00023*(sin(deg2rad($K2)))
-
0.00014*(sin(deg2rad(2*$D)))
+
0.00014*(cos(deg2rad(2*$Md-2*$F)))
-
0.00012*(sin(deg2rad($Md-2*$F)))
-
0.00012*(sin(deg2rad(2*$Md)))
+
0.00011*(sin(deg2rad(2*$Md-2*$M-2*$D)));

$ld = -$tau + ($rho*cos(deg2rad($A))+$sigma*sin(deg2rad($A)))*tan($b);
$bd $sigma*cos(deg2rad($A))-$rho*sin(deg2rad($A));

$b1=(deg2rad($bd))+$b;


//position angle
$V $omega $psi + ($rho/(sin($I)));
$X=sin($I+deg2rad($rho))*sin(deg2rad($V));
$Y = (sin($I+deg2rad($rho))*cos(deg2rad($V))*cos(deg2rad($epsilon)))-(cos($I+deg2rad($rho))*sin(deg2rad($epsilon)));
$w atan2($X,$Y);
$alpha deg2rad($ra*15);

$P = (quad(rad2deg(asin(((sqrt(($X*$X)+($Y*$Y)))*cos($alpha-$w)) /(cos($b1))))));

//topocentric lib
if ($latlong == 1){
}else{
$phi =deg2rad($userlat);
$rra=deg2rad($rageo);
$rdec=deg2rad($decgeo);
$ast  quad(280.46061837+(360.98564736629*($JDE-2451545))+(0.000387922*$T*$T)-(($T*$T*$T)/38710000));
$H =deg2rad(($ast)+$userlong-($rageo*15));
$Q atan2((cos($phi)*sin($H)),(cos($rdec)*sin($phi)+cos($rdec)*cos($phi)*cos($H)));
$z asin(sin($rdec)*sin($phi)+cos($rdec)*cos($phi)*cos($H));
$delta2=$dist/149597870.691;
$pi rad2deg(asin((sin(deg2rad(8.794/3600)))/$delta2));
$pid $pi *(sin($z)+0.0084*sin(2*$z));
$deltal= (-$pid*sin($Q-$P))/(cos($b));
$deltab=$phi*cos($Q-$P);
$deltaP $deltal*sin($b+(deg2rad($deltab)))-$pid*sin($Q)*tan($rdec);
}
$b rad2deg($b);
$P = (quad(rad2deg(asin(((sqrt(($X*$X)+($Y*$Y)))*cos($alpha-$w)) /(cos($b1))))))+$deltaP;
$Pround round($P,1);
$l round($l +$ld+$deltal,4);
$b round($b+$bd+$deltab,4);

echo 
"
<div style=\"position: absolute; left: 10px; top: 40px; \">
<table style=\"border-width:thin; border-color:#333333; border-style:solid\" width=\"320\" height=\"180\">
<tr><td>Right Ascension:</td><td>{$dra}h {$mra}m {$sra}s</td></tr>
<tr><td>Declination:</td><td>{$ddec}&deg; {$mdec}' {$sdec}\"</td></tr>
<tr><td>Distance:</td><td>{$delta}km</td></tr>
<tr><td>Phase:</td><td>{$phase}%</td></tr>
<tr><td>Angular Diameter:</td><td>{$s}'</td></tr>
<tr><td>Libration in longitude:</td><td>{$l}&deg;</td></tr>
<tr><td>Libration in Latitude:</td><td>{$b}&deg;</td></tr>
<tr><td>Position angle:</td><td>{$Pround}&deg;</td></tr>
</table> "
;
include(
"tabletime.php");
echo 
"
</div>

<form action='moon.php' method='get'>
<table style=\"border-width:thin; border-color:#333333; border-style:solid; position: absolute; left: 10px; top: 280px; \" width=\"750\">

<td align=\"right\"><a href=\"" 
$_SERVER["PHP_SELF"] . "?day=$dayp\">-1 day</a>|<a href=\"" $_SERVER["PHP_SELF"] . "?day=$dayn\">+1 day</a></td>

<td align=\"right\"><a href=\"" 
$_SERVER["PHP_SELF"] . "?month=$monthp\">-1 month</a>|<a href=\"" $_SERVER["PHP_SELF"] . "?month=$monthn\">+1 month</a></td>

<td align=\"right\"><a href=\"" 
$_SERVER["PHP_SELF"] . "?year=$yearp\">-1 year</a>|<a href=\"" $_SERVER["PHP_SELF"] . "?year=$yearn\">+1 year</a></td>

<td align=\"right\"><a href=\"" 
$_SERVER["PHP_SELF"] . "?time=$timep\">-1 hour</a>|<a href=\"" $_SERVER["PHP_SELF"] . "?time=$timen\">+1 hour</a></td>
<t ></td><tr><td align=\"right\">Day (1-31):<input type=\"text\" name=\"day\" size=\"2\" value=\"$day\">&nbsp;&nbsp;</td>
<td width=\"170\" align=\"right\">Month (1-12):<input type=\"text\" name=\"month\" size=\"2\" value=\"$month\">&nbsp;&nbsp;</td>
<td width=\"170\" align=\"right\">Year (4-digit):<input type=\"text\" name=\"year\" size=\"4\" value=\"$year\">&nbsp;&nbsp;</td>
<td width=\"168\" align=\"right\">Time (24 hrs):<input type=\"text\" name=\"time\" size=\"4\" value=\"$time\">&nbsp;&nbsp;</td>
<td  align=\"right\"><input type=\"submit\" value=\"Submit\"></td></tr></table>"
;











$long = ($long/3600 )*1000000;
$lat = ($lat/3600 )*1000000;
$dist =$dist*1000;
echo
"<div style=\"position: absolute; left: 500px; top: 20px; \">
<img src=\"moonellipse.php?x={$x}&k={$k}&w=200\" ></div>

<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br>
<br>"
;


if (
$latlong == 1){
echo
"<div style=\"position: absolute; left: 20px; top: 240px; \"><font size=\"2\">Geocentric mode: To increase accuracy, 
<A HREF=\"javascript:void(0)\"
onclick=\"window.open('location2.htm','location','width=500,height=280',status='no')\">set your location </A></font></div>"
;
}else{
echo
"<div style=\"position: absolute; left: 20px; top: 240px; \"><font size=\"1.5\">Topocentric mode: <A HREF=\"javascript:void(0)\"
onclick=\"window.open('location2.htm','location','width=500,height=280',status='no')\">click to change your location </A></font></div>"
;
}




?>
<br><br><br>
<br><br><br><font size="1">Equations derived from 'Astronomical Algorithms' by Jean Meeus. <a href="moon.phps" target="_blank">View page source</a> <br>
Moon position calculated from the full ELP2000-82 theory from Chapront</font><br><br>


    </div>
</div> 




</div>
</body>
</html>