HstMgr.php
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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;
}
}
?>