Blame view

php/RemoteDataCenter/RemoteParamManager.php 11.9 KB
74b77f58   Elena.Budnik   init remote branch
1
2
3
4
5
6
7
8
<?php
/**
 * @class RemoteParamManager 
 * @brief Manage Remote Data Centers via DDServer  
 */

class RemoteParamManager
{ 
2b8ea2a7   Elena.Budnik   RemoteParamManage...
9
	public $Bases, $basesDom;	 	
74b77f58   Elena.Budnik   init remote branch
10
11
12
13
14
	protected $baseDom;
	
	protected $center;
	
	public $xmlDom, $xmlName;
0dc31ba8   Elena.Budnik   remoteParamManage...
15
	public $baseId, $paramId, $remoteViId, $localInfo, $paramDom, $paramXML;
74b77f58   Elena.Budnik   init remote branch
16
17
18
 
	function __construct() 
	{
2b8ea2a7   Elena.Budnik   RemoteParamManage...
19
20
21
22
		$this->basesDom = new DomDocument("1.0");

		if (!is_dir(RemoteData."/PARAMS"))
				mkdir(RemoteData."/PARAMS", 0775, true);
d444ba30   Elena.Budnik   mkdir...,0775 doe...
23
		chmod(RemoteData."/PARAMS",0775);
2b8ea2a7   Elena.Budnik   RemoteParamManage...
24
25
26
				
		if (!is_dir(RemoteData."/PARAMS_INFO"))
				mkdir(RemoteData."/PARAMS_INFO", 0775, true);
d444ba30   Elena.Budnik   mkdir...,0775 doe...
27
28
		chmod(RemoteData."/PARAMS_INFO",0775);
		
2b8ea2a7   Elena.Budnik   RemoteParamManage...
29
30
31
32
33
34
35
		$this->xmlName = USERWSDIR."RemoteParams.xml";
		$this->xmlDom = new DomDocument("1.0");
		
	}

	public function init() {
	
74b77f58   Elena.Budnik   init remote branch
36
37
		if (!file_exists(RemoteData."Bases.xml"))
				return array("err" => "No Remote Data Bases");
2b8ea2a7   Elena.Budnik   RemoteParamManage...
38
				
74b77f58   Elena.Budnik   init remote branch
39
40
		if (!$this->basesDom->load(RemoteData."Bases.xml"))
				return array("err" => "Can not load Remote Data Bases definitions");
2b8ea2a7   Elena.Budnik   RemoteParamManage...
41
		
74b77f58   Elena.Budnik   init remote branch
42
43
44
45
		$bases = $this->basesDom->getElementsByTagName('dataCenter');
		
		foreach ($bases as $base) 
			$this->Bases[] = $base->getAttribute('xml:id');
2b8ea2a7   Elena.Budnik   RemoteParamManage...
46
			
74b77f58   Elena.Budnik   init remote branch
47
		if (!file_exists(USERWSDIR."RemoteParams.xml"))
2b8ea2a7   Elena.Budnik   RemoteParamManage...
48
				return array("err" => "RemoteParams file error");
74b77f58   Elena.Budnik   init remote branch
49
				
b4fbe992   Benjamin Renard   Load XML files si...
50
		if (!@$this->xmlDom->load($this->xmlName))
2b8ea2a7   Elena.Budnik   RemoteParamManage...
51
52
53
			return array("err" => "RemoteParams file error");
		
		return array("success" => true);
74b77f58   Elena.Budnik   init remote branch
54
	}
2b8ea2a7   Elena.Budnik   RemoteParamManage...
55
	
74b77f58   Elena.Budnik   init remote branch
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	/*
	*  get baseId from parameter descriptor
	*/
	protected function setBaseId($id) 
	{ 
		foreach ($this->Bases as $base) {
			// Special Themis case
			if (substr($id,0,2) == "th") {
				$baseId = "THEMIS";
				break;
			}
			if (strncmp($id, $base, strlen($base)) === 0) 
			{
				$baseId = $base;
				break;
			}
		}
		
92edb3d1   Elena.Budnik   copy remote xml t...
74
		$this->center = new $baseId(); 	
74b77f58   Elena.Budnik   init remote branch
75
	}
25f87ff8   Elena.Budnik   info for remote
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	
// <param xml:id="imf">
//   <info>
//     <name>imf</name>
//     <short_name>b_gse</short_name>
//     <components>bx,by,bz</components>
//     <units>nT</units>
//     <coordinates_system>GSE</coordinates_system>
//     <tensor_order/>
//     <si_conversion>1e-9&gt;T</si_conversion>
//     <fill_value>-1.0E31</fill_value>
//     <ucd>phys.magField</ucd>
//     <dataset_id>ace-imf-all</dataset_id>
//     <instrument_id>ACE_MAG</instrument_id>
//   </info>
//   <get>
//      <vi name="ace:imf:all">
//         <baseParam name="IMF"/>
//     </vi>
//   </get>
//   <process/>
//   <output/>
// </param>

23b74e4b   Elena.Budnik   spectra in remote
100
	public function makeInternalParamXml($isSpectra)
74b77f58   Elena.Budnik   init remote branch
101
102
103
104
	{
		if (!$this->center->ViId) return false;
		if (!$this->center->ParamId) return false;
		
d3f8a6d0   Elena.Budnik   internal paramID
105
	//	$this->paramId = strtolower($this->center->baseID."_".$this->center->ViId."_".$this->center->ParamId);
23b74e4b   Elena.Budnik   spectra in remote
106

92edb3d1   Elena.Budnik   copy remote xml t...
107
108
109
		$xmlNameRemote = RemoteData."/PARAMS/".$this->paramId.".xml";
		$xmlNameTemp = PARAMS_LOCALDB_DIR."/".$this->paramId.".xml";
		
9c04c40e   Elena.Budnik   do not copy remot...
110
111
112
113
		if (file_exists($xmlNameTemp)) {
			return true;
		}
			
92edb3d1   Elena.Budnik   copy remote xml t...
114
		if (file_exists($xmlNameRemote)) {
9c04c40e   Elena.Budnik   do not copy remot...
115
			return copy($xmlNameRemote, $xmlNameTemp);
92edb3d1   Elena.Budnik   copy remote xml t...
116
		}
23b74e4b   Elena.Budnik   spectra in remote
117
118
119
		if (!$this->center->checkParamIsReal())
            return false;
         
74b77f58   Elena.Budnik   init remote branch
120
121
122
123
124
		$xml = new DomDocument("1.0");
		$paramNode = $xml->createElement("param");
		$xml->appendChild($paramNode);
    
		$paramNode->setAttribute("xml:id", $this->paramId);
25f87ff8   Elena.Budnik   info for remote
125
126
127
128
129
130
131
		
		$infoNode = $xml->createElement("info");
		$infoNode->appendChild($xml->createElement("name",strtolower($this->center->ParamId)));
		$infoNode->appendChild($xml->createElement("short_name",strtolower($this->center->ParamId)));
		
		$size = $this->center->getParamSize();
		//TODO spectra components 
23b74e4b   Elena.Budnik   spectra in remote
132
133

		if ($size > 1 && !$isSpectra) 
25f87ff8   Elena.Budnik   info for remote
134
135
136
137
138
139
140
141
		{	
			$components = strtolower($this->center->getParamComponents());	
		}
		else {
			$components = null;
		}
		
		$fillValue = $this->center->getParamFillValue();
fe74bf8e   Elena.Budnik   instrument id for...
142
		
25f87ff8   Elena.Budnik   info for remote
143
144
145
		if (!$fillValue) 
				$fillValue = null;
				
23b74e4b   Elena.Budnik   spectra in remote
146
147
148
		if ($components)
            $infoNode->appendChild($xml->createElement("components",$components));
            
25f87ff8   Elena.Budnik   info for remote
149
150
151
152
153
154
		$infoNode->appendChild($xml->createElement("units",$this->center->getParamUnits()));
		$infoNode->appendChild($xml->createElement("coordinates_system"));
		$infoNode->appendChild($xml->createElement("tensor_order"));
		$infoNode->appendChild($xml->createElement("si_conversion"));
		$infoNode->appendChild($xml->createElement("fill_value", $fillValue));
		$infoNode->appendChild($xml->createElement("ucd"));
fe74bf8e   Elena.Budnik   instrument id for...
155
156
		$infoNode->appendChild($xml->createElement("dataset_id",strtolower($this->center->ViId)));
		$infoNode->appendChild($xml->createElement("instrument_id",strtolower($this->center->baseID." ".$this->center->ViId)));
25f87ff8   Elena.Budnik   info for remote
157
158
159
		
		$getNode = $xml->createElement("get");
		$viNode = $xml->createElement("vi");
74b77f58   Elena.Budnik   init remote branch
160
161
162
163
		$baseParamNode = $xml->createElement("baseParam");
		$baseParamNode->setAttribute("name", $this->center->ParamId);
		$viNode->setAttribute("name", strtolower(strtr($this->center->ViId,"_", ":")));
		$viNode->appendChild($baseParamNode);
25f87ff8   Elena.Budnik   info for remote
164
165
166
		$getNode->appendChild($viNode);
		
		$paramNode->appendChild($infoNode);
74b77f58   Elena.Budnik   init remote branch
167
168
		$paramNode->appendChild($getNode);
		$paramNode->appendChild($xml->createElement("process"));
23b74e4b   Elena.Budnik   spectra in remote
169
170
171
172
173
174
175
		$output = $xml->createElement("output");
	
		if ($isSpectra) {
            $output->appendChild($this->makeSpectraNode($xml));                        
        }  
     	
		$paramNode->appendChild($output);
74b77f58   Elena.Budnik   init remote branch
176
		 
92edb3d1   Elena.Budnik   copy remote xml t...
177
		$res = $xml->save($xmlNameRemote);
05961422   Elena.Budnik   permissions on dirs
178

92edb3d1   Elena.Budnik   copy remote xml t...
179
180
		if ($res) 
				return copy($xmlNameRemote, $xmlNameTemp);
74b77f58   Elena.Budnik   init remote branch
181
		
92edb3d1   Elena.Budnik   copy remote xml t...
182
		return $res;
74b77f58   Elena.Budnik   init remote branch
183
184
	}
	
23b74e4b   Elena.Budnik   spectra in remote
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
	protected function makeSpectraNode($xml)
 	{
        $yAxis = $xml->createElement("yAxis"); 
        $digitalAxis = $xml->createElement("digitalAxis");
        $digitalAxis->setAttribute('id',"y-left");
//     $digitalAxis->setAttribute('scale',"logarithmic");
        $yAxis->appendChild($digitalAxis);
        
        $zAxis = $xml->createElement("zAxis"); 
        $colorAxis = $xml->createElement("colorAxis");
        $colorAxis->setAttribute('colorMapIndex',"1");
        $colorAxis->setAttribute('scale',"logarithmic");
        $zAxis->appendChild($colorAxis);
        
        $axes = $xml->createElement("axes");
        $axes->appendChild($yAxis);
        $axes->appendChild($zAxis);
        
        $spectro = $xml->createElement("spectro");
        $spectro->setAttribute('yAxis',"y-left");
        $param = $xml->createElement("param");
        $param->setAttribute('id',$this->paramId);
        $param->appendChild($spectro);
        
        $params = $xml->createElement("params");
        $params->appendChild($param);
        
        $timePlot = $xml->createElement("timePlot");
        $timePlot->appendChild($params);
        $timePlot->appendChild($axes);
        
        $plot = $xml->createElement("plot");
        $plot->appendChild($timePlot);
        
        return $plot;
 	}
 	
	protected function makeOurComponents($node, $size)
 	{
		for ($i = 0; $i < $size; $i++)
		{
			$compNode = $this->xmlDom->createElement("component");			
			$compNode->setAttribute('xml:id',$this->paramId."($i)");		
			$compNode->setAttribute('name',"comp_$i"); // LABEL

			$node->appendChild($compNode);
		}
 	}
 	
74b77f58   Elena.Budnik   init remote branch
234
235
	protected function makeComponents($node, $size, $components)
 	{
25f87ff8   Elena.Budnik   info for remote
236
		$compArray = explode(",",$components);
74b77f58   Elena.Budnik   init remote branch
237
238
239
240
241
242
243
244
245
246
247
248
249
250
		
		for ($i = 0; $i < $size; $i++)
		{
			$compNode = $this->xmlDom->createElement("component");			
			$compNode->setAttribute('xml:id',$this->paramId."($i)");		
			$compNode->setAttribute('name',strtolower($compArray[$i])); // LABEL

			$node->appendChild($compNode);
		}
 	}
 	
	protected function addNode($id)
	{
		// Node exists already 
d3f8a6d0   Elena.Budnik   internal paramID
251
252
253
		$this->paramId = strtr(strtolower($id),":","_");
		
		if ($this->xmlDom->getElementById($this->paramId)) return true;
74b77f58   Elena.Budnik   init remote branch
254
255
256

		// Node to be added
		$nodeRemote = $this->center->baseDom->getElementById($id);
74b77f58   Elena.Budnik   init remote branch
257
		// No such node in base.xml
23b74e4b   Elena.Budnik   spectra in remote
258
		 
74b77f58   Elena.Budnik   init remote branch
259
260
261
262
		if (!$nodeRemote) return false;

		if ($nodeRemote->tagName == 'dataset') 
		{
23b74e4b   Elena.Budnik   spectra in remote
263

74b77f58   Elena.Budnik   init remote branch
264
265
			$this->center->setViId($nodeRemote->getAttribute('name'));
			$status = $this->center->addViToDD();
23b74e4b   Elena.Budnik   spectra in remote
266
			
74b77f58   Elena.Budnik   init remote branch
267
			if (!$status) return false;
23b74e4b   Elena.Budnik   spectra in remote
268
			
4b22a046   Elena.Budnik   correct DDService...
269
			$remoteDatasetInfo = DDSERVICE."/BASE/INFO/bases/".$this->center->baseID."/".$this->center->infoFile; 
74b77f58   Elena.Budnik   init remote branch
270
			$localDatasetInfo = RemoteData.$this->center->baseID."/".$this->center->infoFile;
9c04c40e   Elena.Budnik   do not copy remot...
271

23b74e4b   Elena.Budnik   spectra in remote
272
			if (!copy($remoteDatasetInfo,$localDatasetInfo)) return false;	
9c04c40e   Elena.Budnik   do not copy remot...
273
		}			 	
74b77f58   Elena.Budnik   init remote branch
274
275
276
277
278
279
280
281

		$node = $this->xmlDom->importNode($nodeRemote);

		if ($nodeRemote->tagName == 'parameter') 
		{
			$this->center->setParamId($nodeRemote->getAttribute('name'));
			$this->center->setViId($nodeRemote->parentNode->getAttribute('name'));
			$this->center->setInfoFile();
23b74e4b   Elena.Budnik   spectra in remote
282
283
            $isSpectra = $this->center->checkIsSpectra();		          
			if (!$this->makeInternalParamXml($isSpectra)) return false;
05961422   Elena.Budnik   permissions on dirs
284

23b74e4b   Elena.Budnik   spectra in remote
285
286
287
288
289
290
291
292
293
294
			if ( (($size = $this->center->getParamSize()) > 1) && !$isSpectra )
			{
                // make components and args
                $components = $this->center->getParamComponents();	
                if ($components == -1) {
                    $this->makeOurComponents($node, $size);
                }
                else {
                    $this->makeComponents($node, $size, $components); // return false;
                }
74b77f58   Elena.Budnik   init remote branch
295
			}
92edb3d1   Elena.Budnik   copy remote xml t...
296
297
298
			
			// convert remote paramID into AMDA paramID			
			$node->setAttribute("xml:id", strtr(strtolower($node->getAttribute("xml:id")), ":","_"));
23b74e4b   Elena.Budnik   spectra in remote
299
300
			if ($isSpectra)
                $node->setAttribute("display_type","spectrogram");
74b77f58   Elena.Budnik   init remote branch
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
		}
	
		$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 necessary ???
			// 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) 
92edb3d1   Elena.Budnik   copy remote xml t...
352
	{	   
74b77f58   Elena.Budnik   init remote branch
353
354
355
356
357
358
		if (count($obj) == 1) 
		{	    
			$id = $obj->id;
			
			if ($id == 'root') return array('res' => 'ok');

d3f8a6d0   Elena.Budnik   internal paramID
359
360
			$this->setBaseId($id);
						
74b77f58   Elena.Budnik   init remote branch
361
362
363
364
365
366
367
368
369
370
371
372
373
			$res = $this->addNode($id);	
			
			if ($res === false) return array("err" => "Cannot add node : $id");
		}
		else 
		{ 
			foreach ($obj as $o) 
			{
				$id = $o->id;	
				
				if ($id == 'root') continue;
				
				if (!$this->baseId) $this->setBaseId($id);				
d3f8a6d0   Elena.Budnik   internal paramID
374
				
74b77f58   Elena.Budnik   init remote branch
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
				$res = $this->addNode($id);
				
				if ($res === false) return array("err" => "Cannot add node : $id");

			}
		}

		if (!$this->xmlDom->save($this->xmlName))
			return array("err" => "Cannot save RemoteParams.xml");
			
		return array('res' => 'ok');
	}
	
	public function deleteFromTree($obj) 
	{
		$id = $obj->id;
		$nodeToDelete = $this->xmlDom->getElementById($id);
		
		if (!$nodeToDelete) 
			return array("err" => "No such id : $id");
		
		$tagName = $nodeToDelete->tagName;
c344b37f   Elena.Budnik   process simuParam...
397
		$topTag = "dataCenter"; //"dataRoot";
74b77f58   Elena.Budnik   init remote branch
398
		
c344b37f   Elena.Budnik   process simuParam...
399
400
401
402
		if ($obj->nodeType && $obj->nodeType == "remoteSimuParam")  // FMI_GUMICS case
				$topTag = "simulationRegion";
		 
		while ( $tagName != $topTag ) // "dataCenter" ?
74b77f58   Elena.Budnik   init remote branch
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
		{
			$parentNode = $nodeToDelete->parentNode;
			$parentNode->removeChild($nodeToDelete);
			$otherChildren = $parentNode->getElementsByTagName($tagName);
			
			if ( $otherChildren->length > 0 ) 
					break;
					
			 $nodeToDelete = $parentNode;
			 $tagName = $nodeToDelete->tagName;
		}
		$this->xmlDom->save($this->xmlName);

		return array('res'=> $obj->id);
	}
0dc31ba8   Elena.Budnik   remoteParamManage...
418
419
420
421
422
423
424
425
	
	public function getInfoName($datasetId) {
	  
// 	      if ($this->baseId == 'CDAWEB')
// 				return  strtolower($datasetId)."_00000000_v01.cdf";
		return  $datasetId.'.xml';

	}
74b77f58   Elena.Budnik   init remote branch
426
}
b7741da9   Benjamin Renard   Define DDService ...
427
?>