<html>
<head>
<title>Illuminated Fraction of the Moon</title>
<link rel="stylesheet" href="default.css">
<link rel="stylesheet" href="highlight/styles/default.css">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body{}
</style>
</head>    
<body>
<h1>Illuminated Fraction of the Moon</h1>
This is an implementation of approximation aglorithm (48.4) in Astronomical Algorithms.

<pre id="output"></pre>
<pre id="sourceOutput">
<code class="JavaScript" id='code1'>

    /*
    Greg Miller gmiller@gregmiller.net 2021
    http://www.celestialprogramming.com/
    Released as public domain
    */
    
    function JulianDateFromUnixTime(t){
        //Not valid for dates before Oct 15, 1582
        return (t / 86400000) + 2440587.5;
    }
    
    function UnixTimeFromJulianDate(jd){
        //Not valid for dates before Oct 15, 1582
        return (jd-2440587.5)*86400000;
    }	
    
    function constrain(d){
        let t=d%360;
        if(t<0){t+=360;}
        return t;
    }
    
      function getIlluminatedFractionOfMoon(jd){
        const toRad=Math.PI/180.0;
        const T=(jd-2451545)/36525.0;
    
        const D = constrain(297.8501921 + 445267.1114034*T - 0.0018819*T*T + 1.0/545868.0*T*T*T - 1.0/113065000.0*T*T*T*T)*toRad; //47.2
        const M = constrain(357.5291092 + 35999.0502909*T - 0.0001536*T*T + 1.0/24490000.0*T*T*T)*toRad; //47.3
        const Mp = constrain(134.9633964 + 477198.8675055*T + 0.0087414*T*T + 1.0/69699.0*T*T*T - 1.0/14712000.0*T*T*T*T)*toRad; //47.4
    
        //48.4
        const i=constrain(180 - D*180/Math.PI - 6.289 * Math.sin(Mp) + 2.1 * Math.sin(M) -1.274 * Math.sin(2*D - Mp) -0.658 * Math.sin(2*D) -0.214 * Math.sin(2*Mp) -0.11 * Math.sin(D))*toRad;
    
        const k=(1+Math.cos(i))/2;
        return k;
    }
    

</code>    
</pre>

<script id='sourceCode'>
/*
Greg Miller gmiller@gregmiller.net 2021
http://www.celestialprogramming.com/
Released as public domain
*/

function JulianDateFromUnixTime(t){
	//Not valid for dates before Oct 15, 1582
	return (t / 86400000) + 2440587.5;
}

function UnixTimeFromJulianDate(jd){
	//Not valid for dates before Oct 15, 1582
	return (jd-2440587.5)*86400000;
}	

function constrain(d){
    let t=d%360;
    if(t<0){t+=360;}
    return t;
}

  function getIlluminatedFractionOfMoon(jd){
    const toRad=Math.PI/180.0;
    const T=(jd-2451545)/36525.0;

    const D = constrain(297.8501921 + 445267.1114034*T - 0.0018819*T*T + 1.0/545868.0*T*T*T - 1.0/113065000.0*T*T*T*T)*toRad; //47.2
    const M = constrain(357.5291092 + 35999.0502909*T - 0.0001536*T*T + 1.0/24490000.0*T*T*T)*toRad; //47.3
    const Mp = constrain(134.9633964 + 477198.8675055*T + 0.0087414*T*T + 1.0/69699.0*T*T*T - 1.0/14712000.0*T*T*T*T)*toRad; //47.4

    //48.4
    const i=constrain(180 - D*180/Math.PI - 6.289 * Math.sin(Mp) + 2.1 * Math.sin(M) -1.274 * Math.sin(2*D - Mp) -0.658 * Math.sin(2*D) -0.214 * Math.sin(2*Mp) -0.11 * Math.sin(D))*toRad;

    const k=(1+Math.cos(i))/2;
    return k;
}


</script>
<script>
show30Days();

function show30Days(){
    const t=new Date().getTime();
    const jd=JulianDateFromUnixTime(t);
    let text="";
    for(let i=0;i<30;i++){
        const f=Math.round(getIlluminatedFractionOfMoon(jd+i)*100);
        text+=new Date(UnixTimeFromJulianDate(jd+i))+": "+    f+"%\r\n";
    }
    document.getElementById("output").innerText=text;

}

//MeeusExample48a();
function MeeusExample48a(){
    const jd=JulianDateFromUnixTime(Date.UTC(1992,3,12));
    getIlluminatedFractionOfMoon(jd);
}

</script>
<script>
let t=document.getElementById("sourceCode").innerText;
t=t.replaceAll("<","&lt;")
t=t.replaceAll(">","&gt;")
document.getElementById("code1").innerHTML=t;
</script>
<script src="highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
    
</body>
</html>