HstMgr.php 4.65 KB
<?php
/**
 * Project   : AMDA-NG
 * Name      : HstMgr.php
 * @author Richard Hitier
*****************************************************************************
 * FT Id     :   Date   : Name - Description
 ******************************************************************************
 *           :14/05/2012: BRE  - file creation and modifications for AMDA-NG project
 */

class HstMgr {

    private $hst_votable;

    function __construct( $base_file = null ) {
        $this->hst_votable = Array(
            "saturn"=>Hst."hstVOtable-saturn.xml",
            "jupiter"=>Hst."hstVOtable-jupiter.xml",
        				"uranus"=>Hst."hstVOtable-uranus.xml");
    }


    /**
     * Return a Votable containing items between given dates.
     * Beware, input DOM's TR elements should already be date sorted.
     *
     * BTW, Group one TableData by day
     *
     * @param startTime
     * @param stopTime
     * @param stopTime
     * @return dom
     */
    function getImagesUrl( $startTime, $stopTime, $planet="saturn"){

        $startTime = $this->formatDate($startTime);
        $stopTime = $this->formatDate($stopTime);
        $imagesUrlList = array();

        // fetch whole data base
        $domOrig = $this->getHstBase($planet);

        // copy dom except tabledata children
        $domRes  = new Domdocument("1.0");
        $domRes->appendChild( $domRes->importNode( $domOrig->documentElement, true));

        // remove uniq TABLEDATA node
        $oldTableDataNode = $domRes->getElementsByTagName("TABLEDATA")->item(0);
        $oldTableDataNode->parentNode->removeChild( $oldTableDataNode );

        $dataNode = $domRes->getElementsByTagName("DATA")->item(0);

        // filter all rows (TR tags)
        // create one TABLEDATA per day
        // set xml result
        $oldDayString = "none";
        $trList = $domOrig->getElementsByTagName("TR");
        for( $i=0; $i < $trList->length ; $i++){
            $currItem = $trList->item($i);
            $currDateString = $this->formatDate( $currItem->getElementsByTagName("TD")->item(5)->nodeValue );
            $currDayString = substr( $currDateString, 0, 11);


            // not found already ? skip
            if( $currDateString < $startTime )
                continue;

            // to late ? quit loop
            if( $currDateString > $stopTime)
                break;

            // Create new TABLEDATA node if day changed
            // and immediatly insert in dom
            if( $currDayString != $oldDayString ){
                $oldDayString = $currDayString;
                $tableDataNode = $domRes->createElement("TABLEDATA");
                $tableDataNode->setAttribute( "name", $currDayString);
                $dataNode->appendChild( $tableDataNode );
            }

            //then append current element to node
            $tableDataNode->appendChild( $domRes->importNode( $currItem , true) );
        }
        
        //BRE result is now given as an array
        
        $res = array();
        
       	$tableDataNodes = $domRes->getElementsByTagName("TABLEDATA");
       	
       	foreach($tableDataNodes as $tableDataNode)
       	{
       		$data = $this->tableDataNode2Array($tableDataNode);
       		
       		$data["images"] = array();
       		
       		foreach($tableDataNode->childNodes as $child)
       			array_push($data["images"], $this->tableDataNode2Array($child));
       			
        	array_push($res,$data);
       	}
       	
        return $res;
    }
    
    function tableDataNode2Array($node)
    {
    	$tds = $node->getElementsByTagName("TD");
    	$data = array("name" => $tds->item(0)->nodeValue,
       				  "target" =>  $tds->item(1)->nodeValue,
       				  "ra" =>  $tds->item(2)->nodeValue,
       				  "dec" =>  $tds->item(3)->nodeValue,
       				  "ref" =>  $tds->item(4)->nodeValue,
       				  "start" =>  $tds->item(5)->nodeValue,
       				  "stop" =>  $tds->item(6)->nodeValue,
       				  "exposure" =>  $tds->item(7)->nodeValue,
       				  "instrument" =>  $tds->item(8)->nodeValue);
		return $data;
    }
    	
    /**
     * Standardize output dates
     * @param dateString inputdate
     * @return formated date
     */
    function formatDate($dateString){
        $arr = date_parse( $dateString);
        $format = "%4d-%02d-%02d %02d:%02d:%02d\n";
        return sprintf($format, $arr['year'], $arr['month'],$arr['day'], $arr['hour'], $arr['minute'], $arr['second']);
    }

    /**
     * Get the xml vot for given planet
     *
     * @param $planet string, either saturn or jupiter
     * @return dom votable
     */
    function getHstBase($planet="saturn") {
        $dom = new Domdocument("1.0");
        $dom->load($this->hst_votable[$planet]);
        return $dom;
    }

}

?>