ParamMgr.php
12.7 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* @class ParamMgr
* @version $Id: ParamMgr.php 1947 2013-11-28 15:10:46Z elena $
*
*/
class ParamMgr {
protected $Bases, $basesDom;
protected $baseDom;
public $xmlDom, $xmlName;
public $baseId, $paramId, $remoteViId, $localInfo, $paramDom, $paramXML;
function __construct() {
//TODO check if files exist....
$this->basesDom = new DomDocument("1.0");
$this->basesDom->load(RemoteData.'Bases.xml');
$bases = $this->basesDom->getElementsByTagName('dataCenter');
foreach ($bases as $base) $this->Bases[] = $base->getAttribute('xml:id');
$this->xmlName = USERWSDIR.'RemoteParams.xml';
$this->xmlDom = new DomDocument("1.0");
$this->xmlDom->load($this->xmlName);
}
/*
*
*/
protected function param2dd($paramID) {
$pairs = array("%" => "_","\\" => "_","$" => "_",":" => "_","+" =>"_","-" => "_","#" => "_","@" => "_","." => "_", ">" => "_", "<" => "_");
return strtr($paramID,$pairs);
}
/*
* Make Aliases - Remote data set ID -> DD data set ID (xxxx_xxxx_xxxx)
*/
public function dataset2dd($remVIID) {
// TODO method of ???? class
$ddVIID = $remVIID;
return $ddVIID;
}
/*
* get baseId from parameter descriptor
*/
protected function setBaseId($id) {
foreach ($this->Bases as $base) {
// Special Themis case
if (substr($id,0,2) == "th") {
$this->baseId = "THEMIS";
break;
}
if (strncmp($id, $base, strlen($base)) === 0) {
$this->baseId = $base;
break;
}
}
$this->baseDom = new DomDocument("1.0");
$this->baseDom->load(RemoteData.$this->baseId.'/base.xml');
}
/*
* get Info File name
*/
//TODO not to hardly code special stuff...
public function getInfoName($datasetId) {
if ($this->baseId == 'CDAWEB')
return strtolower($datasetId)."_00000000_v01.cdf";
return $datasetId.'.xml';
}
/*
*
*/
protected function getDatasetInfo() {
$infoFileName = $this->getInfoName($this->remoteViId);
$this->localInfo = RemoteData.$this->baseId.'/'.$infoFileName;
if (file_exists($this->localInfo)) return true;
// call to DD Server to create new VI
$command = OLD_CLASSPATH.'AddVI '.$this->dataset2dd($this->remoteViId).' '.$this->remoteViId.' '.$this->baseId;
system($command, $err);
$remoteInfo = INFOSITE.'INFO/'.$this->baseId.'/DATASETS/'.$infoFileName;
// Get dataset info from DD Server if it doesn't yet exists locally
//TODO may be not neccessary to copy file? load from URL?
$res = copy($remoteInfo, $this->localInfo);
return $res;
}
/*
*
*/
public function getParamInfo() {
if (!$this->localInfo) {
$infoFileName = $this->getInfoName($this->remoteViId);
$this->localInfo = RemoteData.$this->baseId.'/'.$infoFileName;
}
switch ($this->baseId) {
// CDAWEB specific function to parse master CDF - idl
case 'CDAWEB' :
$command = OLD_CLASSPATH.'cdfInfo '.$this->localInfo.' "'.$this->paramId.'"';
$info = exec($command);
break;
case 'VEXGRAZ' :
case 'MAPSKP' :
$info = $this->getInfoSpase();
break;
default:
}
if ($info == "%-9999") return false;
return $info;
}
/*
*
*/
protected function getInfoSpase() {
$xmlinfo = new DomDocument("1.0");
$xmlinfo->load($this->localInfo);
$mission = $xmlinfo->getElementsByTagName("PARENT_MISSION");
$instrument = $xmlinfo->getElementsByTagName("PARENT_INSTRUMENT");
$xp = new domxpath($xmlinfo);
$param = $xp->query("//PARAM_ID[.='".$this->paramId."']");
$paramNode = $param->item(0)->parentNode;
if (!$paramNode) return false;
$info = "paramID%" . $this->paramId . "&&";
// Data Type
$tag = $paramNode->getElementsByTagName("DATA_TYPE");
$value = $tag->item(0)->nodeValue;
switch ($value) {
case 'FLOAT' : $dataTypeS = '2'; break;
case 'DOUBLE': $dataTypeS = '3'; break;
case 'INT' : $dataTypeS = '1'; break;
case 'CHAR' : $dataTypeS = '0'; break;
default : $dataTypeS = '2' ;
}
$info = $info . "DATATYPE%" . $dataTypeS ."&&";
$tag = $paramNode->getElementsByTagName("SIZES");
$value = $tag->item(0)->nodeValue;
$info = $info . "DIMENSION%" . $value;
$tags=array("PARENT_MISSION", "PARENT_EXPERIMENT", "PARAMETER_SHORT_DESCRIPTION", "FILLVAL", "UNITS","DISPLAY_TYPE", "COORDINATE_SYSTEM", "TENSOR_ORDER_VALUE", "LABEL_I", "FIELDNAM");
foreach ($tags as $atag){
$tag = $paramNode->getElementsByTagName($atag);
$value = $tag->length > 0 ? $tag->item(0)->nodeValue : -9999;
switch ($atag) {
case "LABEL_I" : $name = "LABLAXIS";
$val_arr = array();
for ($j = 0; $j < $tag->length; $j++)
$val_arr[$j] = $tag->item($j)->nodeValue;
$value = implode($val_arr, '$').'$'; break;
case "COORDINATE_SYSTEM" : $name = "FRAME"; break;
case "TENSOR_ORDER_VALUE" : $name = "TENSOR"; break;
case "PARAMETER_SHORT_DESCRIPTION" : $name = "DESCRIPTOR"; break;
case "PARENT_MISSION" : $name = "SOURCE_NAME";
$value = $mission->item(0)->nodeValue;
break;
case "PARENT_EXPERIMENT" : $name = "DATA_TYPE";
$value = $instrument->item(0)->nodeValue;
break;
case "FILLVAL":
$value = $tag->length > 0 ? $tag->item(0)->nodeValue : -1.e31;
$name = "FILLVAL";
break;
default: $name = $atag;
}
$info = $info . "&&".$name."%". $value;
}
return $info;
}
/*
* Create XML parameter descriptor in the generic_data/RemoteData/PARAMS dir
*/
public function createParamXml($infoTotal) {
$this->paramDom = new DomDocument("1.0");
$this->paramDom->preserveWhiteSpace = false;
$this->paramDom->formatOutput = true;
$param = $this->paramDom->createElement('PARAM');
$param->appendChild($this->paramDom->createElement("baseID",$this->baseId));
$param->appendChild($this->paramDom->createElement("viID",$this->remoteViId));
$info = explode("&&",$infoTotal);
for ($i = 0; $i < count($info); $i++){
$attr = explode("%",$info[$i]);
try {
$param->appendChild($this->paramDom->createElement($attr[0],$attr[1]));
}
catch (Exception $e) {
return false;
}
}
$this->paramDom->appendChild($param);
if (!$this->paramDom->save($this->paramXML)) return false;
return true; // NO ERROR
}
/*
* special themis case: componets from base.xml
*/
public function makeThemisComponents($param) {
$sizes = $param->getAttribute('size');
if (!$sizes) return true; // scalar
$labels = $param->getAttribute('labels');
$id = $param->getAttribute('xml:id');
$name = $param->getAttribute('name');
if (!$labels) {
$param->setAttribute('needsArgs', true);
return true;
}
$labelArr = explode("$",$labels);
for ($i = 0; $i < count($labelArr); $i++)
{
$component = $this->xmlDom->createElement('component');
$component->setAttribute('xml:id',$id.'('.$i.')');
$component->setAttribute('name',$labelArr[$i]);
$param->appendChild($component);
}
return true;
}
/*
*
*/
public function makeComponents($param) {
$sizes = $this->paramDom->getElementsByTagName('DIMENSION')->item(0)->nodeValue;
$display = strtolower($this->paramDom->getElementsByTagName('DISPLAY_TYPE')->item(0)->nodeValue);
$units = $this->paramDom->getElementsByTagName('UNITS')->item(0)->nodeValue;
if (!$units || ($units == "-9999")) $units = "no";
$param->setAttribute("units", $units);
if ($sizes == "1") return true; // Scalar - Nothing to do
$id = $param->getAttribute('xml:id');
$name = $param->getAttribute('name');
// Not a Scalar - add components to user tree
$labels = $this->paramDom->getElementsByTagName('LABLAXIS')->item(0)->nodeValue;
// No labels
if (!$labels || ($labels == "$") || (substr($display,0,6) == "spectr")) {
if (!$param->getAttribute('needsArgs')) {
$param->setAttribute('needsArgs', true);
if (substr($display,0,6) == "spectr")
$param->setAttribute('display_type', 'spectrogram');
}
return true;
}
$labelArr = explode("$",$labels);
for ($i = 0; $i < count($labelArr)-1; $i++)
{
$component = $this->xmlDom->createElement('component');
$component->setAttribute('xml:id',$id.'('.$i.')');
$component->setAttribute('name',$labelArr[$i]);
$param->appendChild($component);
}
return true;
}
/*
*
*/
protected function addNode($id){
// NODE EXISTS
if ($this->xmlDom->getElementById($id)) return true;
// NODE TO BE ADD
$nodeRemote = $this->baseDom->getElementById($id);
if (!$nodeRemote) return false;
if ($nodeRemote->tagName == 'dataset') {
$this->remoteViId = $nodeRemote->getAttribute('name');
$datasetExists = $this->baseId != 'THEMIS' ? $this->getDatasetInfo() : true;
if (!$datasetExists) return false;
}
$node = $this->xmlDom->importNode($nodeRemote);
if ($nodeRemote->tagName == 'parameter') {
$this->paramId = $nodeRemote->getAttribute('name');
$this->remoteViId = $nodeRemote->parentNode->getAttribute('name');
$info = $this->baseId != 'THEMIS' ? $this->getParamInfo() : true;
if (!$info) return false;
// XML descriptor of the parameter; themis excluded
if ($this->baseId == 'THEMIS') {
if (!$this->makeThemisComponents($node)) return false;
}
else {
$paramGlobalId = $this->baseId.":".$this->dataset2dd($this->remoteViId).":".$this->param2dd($this->paramId);
$node->setAttribute('xml:id', $paramGlobalId);
$this->paramXML = RemoteData.'PARAMS/'.$paramGlobalId.'.xml';
if (!file_exists($this->paramXML) && !$this->createParamXml($info)) return false;
if (!$this->paramDom) {
$this->paramDom = new DomDocument("1.0");
}
$this->paramDom->load($this->paramXML);
if (!$this->makeComponents($node)) return false;
}
}
$parentRemote= $nodeRemote->parentNode;
$parentRemoteId = $parentRemote->getAttribute('xml:id');
$parent = $this->xmlDom->getElementById($parentRemoteId);
if (!$parent) {
$parent = $this->xmlDom->importNode($parentRemote);
}
$parent -> appendChild($node);
$toAddDataCentertToDoc = false;
while ($parent->tagName != 'dataCenter') {
$node = $parent;
$parentRemote = $parentRemote->parentNode;
$parentRemoteId = $parentRemote->getAttribute('xml:id');
$parent = $this->xmlDom->getElementById($parentRemoteId);
if (!$parent) {
if ($parentRemote->tagName == 'dataCenter') $toAddDataCenterToDoc = true;
$parent = $this->xmlDom->importNode($parentRemote);
$parent -> appendChild($node);
}
}
if ($toAddDataCenterToDoc) {
//TODO if this is neccessary ???
// special bases 'hand-made' descriptions
$basesDom = new DomDocument("1.0");
$basesDom -> load(RemoteData.'Bases.xml');
$theBase = $basesDom -> getElementById($parent->getAttribute('xml:id'));
if ($theBase) $parent -> setAttribute('name', $theBase->getAttribute('name'));
$this->xmlDom->documentElement->appendChild($parent);
}
return true;
}
/*
* PUBLIC FUNCTIONS
*/
public function saveTree($obj) {
if (count($obj) == 1) {
$id = $obj->id;
$this->setBaseId($id);
$res = $this->addNode($id);
}
else {
foreach ($obj as $o) {
$id = $o->id;
if ($id == 'root') continue;
if (!$this->baseId) $this->setBaseId($id);
$res = $this->addNode($id);
}
}
$this->xmlDom->save($this->xmlName);
return array('res' => 'ok');
}
public function deleteFromTree($obj) {
$id = $obj->id;
$nodeToDelete = $this->xmlDom->getElementById($id);
if (!$nodeToDelete) return array('err' => 'NO SUCH ID');
$nodeToDelete->parentNode->removeChild($nodeToDelete);
$this->xmlDom->save($this->xmlName);
return array('res'=> $obj->id);
}
}
?>