<?php
require_once ('geomag.php'); 
/**<p>
 *  Test values from
 *  <a href ="http://www.ngdc.noaa.gov/geomag/WMM/soft.shtml"> the National GeoPhysical Data Center.</a>.
 *  Click on the WMM2015testvalues.pdf link.
 *  </p><p>
 *  You have to run this test twice. Once with the WMM.COF
 *  file present, and then with it missing. Otherwise the
 *  setCoeff method is never tested.</p>
 *
 * @version 1.0 Apr 14, 2006
 * @author John St. Ledger
 * @version 1.1 Jan 28, 2009
 * <p>Added 2006 test values.</p>
 * @version 1.2 Jan 5, 2010
 * <p>Updated with the test values for the 2010 WMM.COF coefficients. From page 18 of
 * <i>The US/UK World Magnetic Model for 2010-2015, Preliminary online version containing
 * final WMM2010 model coefficients</i></p>
 * @version 1.3 Jan 15, 2015
 * <p>Updated with the test values for the 2015 WMM.COF coefficients. From the test values WMM2015testvalues.pdf
 * from the WMM web site.</p> * @version 1.4 May 26, 2015
 * <p>Fixed the East-West, North-South bug discovered by Martin Frassl.</p>
 
 */
class TSAGeoMagTest
{
    public  function __construct(){
        $this->failure = false;
        $this->magModel = new GeoMag();
    }
    
    public function all_tests(){
        $results = array();
        $results['getDeclination'] =         $this->testDeclination()?"pass":"fail";
        $results['getDipAngle'] =            $this->testDipAngle()?"pass":"fail";
        $results['getHorizontalIntensity'] = $this->testHorizontalIntensity()?"pass":"fail";
        $results['getNorthIntensity'] =      $this->testNorthIntensity()?"pass":"fail";
        $results['getEastIntensity'] =       $this->testEastIntensity()?"pass":"fail";
        $results['getVerticalIntensity'] =   $this->testVerticalIntensity()?"pass":"fail";
        $results['getIntensity'] =           $this->testIntensity()?"pass":"fail";
        $results['decimalYear'] =            $this->testdecimalYear()?"pass":"fail";
        print_r($results);
        $all = $this->magModel->getGeoMag(80, 0, 2015, 0);
        print_r($all);
        $all = $this->magModel->getGeoMag(0, 120, 2015, 0);
        print_r($all);
        $all = $this->magModel->getGeoMag(40.723458, -111.932889, 2015, 0);
        print_r($all);
        $all = $this->magModel->getGeoMag(40.723458, -111.932889, 2020, 0);
        print_r($all);
        $all = $this->magModel->getGeoMag(40.723458, -111.932889, 2020, 1.5);
        print_r($all);
        return $results;
    }
    
    /**
     * Test method for {@link d3.env.TSAGeoMag#getDeclination(double, double, double, double)}
     */
    public function testDeclination()
    {
        $this->newTest();
        $this->assertEquals(-3.85, $this->magModel->getDeclination(80, 0, 2015, 0) , 5.0E-03);
        $this->assertEquals(0.57, $this->magModel->getDeclination(0, 120, 2015, 0) , 5.0E-03);
        $this->assertEquals(69.81, $this->magModel->getDeclination(-80, 240, 2015, 0) , 5.0E-03);
        $this->assertEquals(-4.27, $this->magModel->getDeclination(80, 0, 2015, 100) , 5.0E-03);
        $this->assertEquals(0.56, $this->magModel->getDeclination(0, 120, 2015, 100) , 5.0E-03);
        $this->assertEquals(69.22, $this->magModel->getDeclination(-80, 240, 2015, 100) , 5.0E-03);
        
        $this->assertEquals(-2.75, $this->magModel->getDeclination(80, 0, 2017.5, 0) , 5.0E-03);
        $this->assertEquals(0.32, $this->magModel->getDeclination(0, 120, 2017.5, 0) , 5.0E-03);
        $this->assertEquals(69.58, $this->magModel->getDeclination(-80, 240, 2017.5, 0) , 5.0E-03);
        $this->assertEquals(-3.17, $this->magModel->getDeclination(80, 0, 2017.5, 100) , 5.0E-03);
        $this->assertEquals(0.32, $this->magModel->getDeclination(0, 120, 2017.5, 100) , 5.0E-03);
        $this->assertEquals(69.00, $this->magModel->getDeclination(-80, 240, 2017.5, 100) , 5.0E-03);
        
        $this->assertEquals(-2.75, $this->magModel->getDeclination(80, 0) , 5.0E-03);
        $this->assertEquals(0.32, $this->magModel->getDeclination(0, 120) , 5.0E-03);
        $this->assertEquals(69.58, $this->magModel->getDeclination(-80, 240) , 5.0E-03);
        return $this->getSuccess();
    }
    
    /**
     * Test method for {@link d3.env.TSAGeoMag#getDipAngle(double, double, double, double)}
     */
    public function testDipAngle()
    {
        $this->newTest();
        $this->assertEquals(83.04, $this->magModel->getDipAngle(80, 0, 2015, 0) , 5E-03);
        $this->assertEquals(-15.89, $this->magModel->getDipAngle(0, 120, 2015, 0) , 5.0E-03);
        $this->assertEquals(-72.39, $this->magModel->getDipAngle(-80, 240, 2015, 0) , 5.0E-03);
        $this->assertEquals(83.09, $this->magModel->getDipAngle(80, 0, 2015, 100) , 5.0E-03);
        $this->assertEquals(-16.01, $this->magModel->getDipAngle(0, 120, 2015, 100) , 5.0E-03);
        $this->assertEquals(-72.57, $this->magModel->getDipAngle(-80, 240, 2015, 100) , 5.0E-03);
        
        $this->assertEquals(83.08, $this->magModel->getDipAngle(80, 0, 2017.5, 0) , 5.0E-03);
        $this->assertEquals(-15.57, $this->magModel->getDipAngle(0, 120, 2017.5, 0) , 5.0E-03);
        $this->assertEquals(-72.28, $this->magModel->getDipAngle(-80, 240, 2017.5, 0) , 5.0E-03);
        $this->assertEquals(83.13, $this->magModel->getDipAngle(80, 0, 2017.5, 100) , 5.0E-03);
        $this->assertEquals(-15.70, $this->magModel->getDipAngle(0, 120, 2017.5, 100) , 5.0E-03);
        $this->assertEquals(-72.45, $this->magModel->getDipAngle(-80, 240, 2017.5, 100) , 5.0E-03);
        
        $this->assertEquals(83.08, $this->magModel->getDipAngle(80, 0) , 5.0E-03);
        $this->assertEquals(-15.57, $this->magModel->getDipAngle(0, 120) , 5.0E-03);
        $this->assertEquals(-72.28, $this->magModel->getDipAngle(-80, 240) , 5.0E-03);
        return $this->getSuccess();
    }
    /**
     * Test method for {@link d3.env.TSAGeoMag#getHorizontalIntensity(double, double, double, double)} in nT
     * and {@link d3.env.TSAGeoMag#getHorizontalIntensity(double, double)} in nT
     */
    public function testHorizontalIntensity()
    {
        $this->newTest();
        $this->assertEquals(6642.1, $this->magModel->getHorizontalIntensity(80, 0, 2015, 0) , 5.0E-02);
        $this->assertEquals(39520.2, $this->magModel->getHorizontalIntensity(0, 120, 2015, 0) , 5.0E-02);
        $this->assertEquals(16793.5, $this->magModel->getHorizontalIntensity(-80, 240, 2015, 0) , 5.0E-02);
        $this->assertEquals(6331.9, $this->magModel->getHorizontalIntensity(80, 0, 2015, 100) , 5.0E-02);
        $this->assertEquals(37537.3, $this->magModel->getHorizontalIntensity(0, 120, 2015, 100) , 5.0E-02);
        $this->assertEquals(15820.7, $this->magModel->getHorizontalIntensity(-80, 240, 2015, 100) , 5.0E-02);
        
        $this->assertEquals(6607.0, $this->magModel->getHorizontalIntensity(80, 0, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(39572.0, $this->magModel->getHorizontalIntensity(0, 120, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(16839.1, $this->magModel->getHorizontalIntensity(-80, 240, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(6300.1, $this->magModel->getHorizontalIntensity(80, 0, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(37586.1, $this->magModel->getHorizontalIntensity(0, 120, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(15862.0, $this->magModel->getHorizontalIntensity(-80, 240, 2017.5, 100) , 5.0E-02);
        
        $this->assertEquals(6607.0, $this->magModel->getHorizontalIntensity(80, 0) , 5.0E-02);
        $this->assertEquals(39572.0, $this->magModel->getHorizontalIntensity(0, 120) , 5.0E-02);
        $this->assertEquals(16839.1, $this->magModel->getHorizontalIntensity(-80, 240) , 5.0E-02);
        return $this->getSuccess();
    }
    /**
     * Test method for d3.env.TSAGeoMag.getEastIntensity() in nT
     */
    public function testNorthIntensity()
    {
        $this->newTest();
        $this->assertEquals(6627.1, $this->magModel->getNorthIntensity(80, 0, 2015, 0) , 5.0E-02);
        $this->assertEquals(39518.2, $this->magModel->getNorthIntensity(0, 120, 2015, 0) , 5.0E-02);
        $this->assertEquals(5797.3, $this->magModel->getNorthIntensity(-80, 240, 2015, 0) , 5.0E-02);
        $this->assertEquals(6314.3, $this->magModel->getNorthIntensity(80, 0, 2015, 100) , 5.0E-02);
        $this->assertEquals(37535.6, $this->magModel->getNorthIntensity(0, 120, 2015, 100) , 5.0E-02);
        $this->assertEquals(5613.1, $this->magModel->getNorthIntensity(-80, 240, 2015, 100) , 5.0E-02);
        
        $this->assertEquals(6599.4, $this->magModel->getNorthIntensity(80, 0, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(39571.4, $this->magModel->getNorthIntensity(0, 120, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(5873.8, $this->magModel->getNorthIntensity(-80, 240, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(6290.5, $this->magModel->getNorthIntensity(80, 0, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(37585.5, $this->magModel->getNorthIntensity(0, 120, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(5683.5, $this->magModel->getNorthIntensity(-80, 240, 2017.5, 100) , 5.0E-02);
        
        $this->assertEquals(6599.4, $this->magModel->getNorthIntensity(80, 0) , 5.0E-02);
        $this->assertEquals(39571.4, $this->magModel->getNorthIntensity(0, 120) , 5.0E-02);
        $this->assertEquals(5873.8, $this->magModel->getNorthIntensity(-80, 240) , 5.0E-02);
        return $this->getSuccess();
    }
    /**
     * Test method for d3.env.TSAGeoMag.getNorthIntensity() in nT
     */
    public function testEastIntensity()
    {
        $this->newTest();
        $this->assertEquals( -445.9, $this->magModel->getEastIntensity(80, 0, 2015, 0) , 5.0E-02);
        $this->assertEquals(  392.9, $this->magModel->getEastIntensity(0, 120, 2015, 0) , 5.0E-02);
        $this->assertEquals(15761.1, $this->magModel->getEastIntensity(-80, 240, 2015, 0) , 5.0E-02);
        $this->assertEquals( -471.6, $this->magModel->getEastIntensity(80, 0, 2015, 100) , 5.0E-02);
        $this->assertEquals(  364.4, $this->magModel->getEastIntensity(0, 120, 2015, 100) , 5.0E-02);
        $this->assertEquals(14791.5, $this->magModel->getEastIntensity(-80, 240, 2015, 100) , 5.0E-02);
        
        $this->assertEquals( -317.1, $this->magModel->getEastIntensity(80, 0, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(  222.5, $this->magModel->getEastIntensity(0, 120, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(15781.4, $this->magModel->getEastIntensity(-80, 240, 2017.5, 0) , 5.0E-02);
        $this->assertEquals( -348.5, $this->magModel->getEastIntensity(80, 0, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(  209.5, $this->magModel->getEastIntensity(0, 120, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(14808.8, $this->magModel->getEastIntensity(-80, 240, 2017.5, 100) , 5.0E-02);
        
        $this->assertEquals( -317.1, $this->magModel->getEastIntensity(80, 0) , 5.0E-02);
        $this->assertEquals(  222.5, $this->magModel->getEastIntensity(0, 120) , 5.0E-02);
        $this->assertEquals(15781.4, $this->magModel->getEastIntensity(-80, 240) , 5.0E-02);
        return $this->getSuccess();
    }
    /**
     * Test method for d3.env.TSAGeoMag.getVerticalIntensity()
     */
    public function testVerticalIntensity()
    {
        $this->newTest();
        $this->assertEquals( 54432.3, $this->magModel->getVerticalIntensity(80, 0, 2015, 0) , 5.0E-02);
        $this->assertEquals(-11252.4, $this->magModel->getVerticalIntensity(0, 120, 2015, 0) , 5.0E-02);
        $this->assertEquals(-52919.1, $this->magModel->getVerticalIntensity(-80, 240, 2015, 0) , 5.0E-02);
        $this->assertEquals( 52269.8, $this->magModel->getVerticalIntensity(80, 0, 2015, 100) , 5.0E-02);
        $this->assertEquals(-10773.4, $this->magModel->getVerticalIntensity(0, 120, 2015, 100) , 5.0E-02);
        $this->assertEquals(-50378.6, $this->magModel->getVerticalIntensity(-80, 240, 2015, 100) , 5.0E-02);
        
        $this->assertEquals( 54459.2, $this->magModel->getVerticalIntensity(80, 0, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(-11030.1, $this->magModel->getVerticalIntensity(0, 120, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(-52687.9, $this->magModel->getVerticalIntensity(-80, 240, 2017.5, 0) , 5.0E-02);
        $this->assertEquals( 52292.7, $this->magModel->getVerticalIntensity(80, 0, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(-10564.2, $this->magModel->getVerticalIntensity(0, 120, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(-50163.0, $this->magModel->getVerticalIntensity(-80, 240, 2017.5, 100) , 5.0E-02);
        
        $this->assertEquals( 54459.2, $this->magModel->getVerticalIntensity(80, 0) , 5.0E-02);
        $this->assertEquals(-11030.1, $this->magModel->getVerticalIntensity(0, 120) , 5.0E-02);
        $this->assertEquals(-52687.9, $this->magModel->getVerticalIntensity(-80, 240) , 5.0E-02);
        return $this->getSuccess();
    }
    /**
     * Test method for d3.env.TSAGeoMag.getIntensity()
     */
    public function testIntensity()
    {
        $this->newTest();
        $this->assertEquals(54836.0, $this->magModel->getIntensity(80, 0, 2015, 0) , 5.0E-02);
        $this->assertEquals(41090.9, $this->magModel->getIntensity(0, 120, 2015, 0) , 5.0E-02);
        $this->assertEquals(55519.8, $this->magModel->getIntensity(-80, 240, 2015, 0) , 5.0E-02);
        $this->assertEquals(52652.0, $this->magModel->getIntensity(80, 0, 2015, 100) , 5.0E-02);
        $this->assertEquals(39052.7, $this->magModel->getIntensity(0, 120, 2015, 100) , 5.0E-02);
        $this->assertEquals(52804.4, $this->magModel->getIntensity(-80, 240, 2015, 100) , 5.0E-02);
        
        $this->assertEquals(54858.5, $this->magModel->getIntensity(80, 0, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(41080.5, $this->magModel->getIntensity(0, 120, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(55313.4, $this->magModel->getIntensity(-80, 240, 2017.5, 0) , 5.0E-02);
        $this->assertEquals(52670.9, $this->magModel->getIntensity(80, 0, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(39042.5, $this->magModel->getIntensity(0, 120, 2017.5, 100) , 5.0E-02);
        $this->assertEquals(52611.1, $this->magModel->getIntensity(-80, 240, 2017.5, 100) , 5.0E-02);
        
        $this->assertEquals(54858.5, $this->magModel->getIntensity(80, 0) , 5.0E-02);
        $this->assertEquals(41080.5, $this->magModel->getIntensity(0, 120) , 5.0E-02);
        $this->assertEquals(55313.4, $this->magModel->getIntensity(-80, 240) , 5.0E-02);
        //assertEquals(52672.8, $this->magModel->getIntensity(40, -105, 2014, 0), 0.05);
        //assertEquals(52672.8, $this->magModel->getIntensity(40, -105, $this->magModel->decimalYear(new GregorianCalendar(2014, 0, 0)), 0), 0.05);
        //assertEquals(52672.5, $this->magModel->getIntensity(40, -105, $this->magModel->decimalYear(new GregorianCalendar(2014, 0, 1)), 0), 0.05);
        return $this->getSuccess();
    }
    
    /**
     *  test method for {@link d3.env.TSAGeoMag#decimalYear(GregorianCalendar)}
     */
    public function testdecimalYear()
    {
        $this->newTest();
        $mag = new GeoMag();
        
//        GregorianCalendar cal = new GregorianCalendar(2010, 0, 0);
//       assertEquals(2010.0, $mag->decimalYear(cal), 0.0);
        $ts = strtotime("2010/1/1");
        $this->assertEquals(2010.0, $mag->decimalYear($ts), 0.0);
//        GregorianCalendar cal2 = new GregorianCalendar(2012, 6, 1);  // the full day of July 1, 0 hours into 2 July
//        assertTrue(cal2.isLeapYear(2012));
//        assertEquals(2012.5, $mag->decimalYear(cal2), 0.0);
        $ts = strtotime("2012/7/2");
        $this->assertEquals(2012.5, $mag->decimalYear($ts), 0.0005);
//        cal2 = new GregorianCalendar(2013, 3, 13);
//        assertFalse(cal2.isLeapYear(2013));
//        assertEquals(2013.282, $mag->decimalYear(cal2), 0.0005);

        $ts = strtotime("2013/4/14");
        $this->assertEquals(2013.282, $mag->decimalYear($ts), 0.0005);
        return $this->getSuccess();
    }
    
    private function assertEquals ($a, $b, $tolerance) {
        
        $ok = abs($a - $b) <= $tolerance;
        $this->failure |= !$ok;
        printf("assertEquals (%f, %f, %f) -> %d (%d)\n",$a, $b, $tolerance, $ok, $this->failure);
        return $ok;
    }

    private function assertFalse ($f) {
        $this->failure &= $f;
        return !$f;
    }
    
    private function assertTrue ($t) {
        $this->failure &= !$t;
        return $t;
    }
    
    private function newTest(){
        $this->failure = false;
    }
    private function getSuccess(){
        return !$this->failure;
    }
}
$tests = new TSAGeoMagTest();
$tests->all_tests();

?>