Blame view

src/REMOTEDATA/CDAWEB.php 21.6 KB
41792000   Elena.Budnik   new REMOTEDATA
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * @class CDAWEB 
 * @brief  CDAWeb 
 * @details
 */
class CDAWEB extends RemoteDataCenterClass
{
	private $ch, $res, $dataViewUR; 
	private $obsGroupsIds;
62372d94   Elena.Budnik   RemoveVi
11
	private $spase_res, $insXML;
41792000   Elena.Budnik   new REMOTEDATA
12
13
14
15
16
	
	private $observatoryGroups, $instrumentTypes;
	private $CDAWEB = array(), $masterCDF = array();
	
	// https://cdaweb.gsfc.nasa.gov/WS/cdasr/1/dataviews/sp_phys/instrumentTypes
62372d94   Elena.Budnik   RemoveVi
17
	private $validInstrumentTypes = [ "Activity%20Indices", "Electric%20Fields%20(space)","Gamma%20and%20X-Rays",
41792000   Elena.Budnik   new REMOTEDATA
18
                                     "Magnetic%20Fields%20(space)", "Particles%20(space)", "Plasma%20and%20Solar%20Wind", 
62372d94   Elena.Budnik   RemoveVi
19
20
                                     "Radio%20and%20Plasma%20Waves%20(space)" ];     
  //   private $validInstrumentTypes = [  "Magnetic%20Fields%20(space)" ];                               
155b6b3c   Elena.Budnik   other approach to...
21
                                     
41792000   Elena.Budnik   new REMOTEDATA
22
23
24
25
26
27
28
29
//   not used : $validInstrumentTypes = ["Ephemeris"];
    
	private $excludeIns = ["GIFWALK", "Unknown"], $excludeGroup = ["THEMIS", "ARTEMIS", "NOAA"];
	
	// not in "https://heliophysicsdata.sci.gsfc.nasa.gov/queries/CDAWeb_SPASE.xql" List;
	// FOR INFO : Excluded automatically
   private $excludeDatasets = ["DMSP_R0_SSJ4", "DMSP_R0_SSIES", "I7_R0_LEPEDEA", "I8_R0_LEPEDEA" ];
   
d4071e27   Elena.Budnik   makeProxy with en...
30
	protected $DDserverXml, $DDserverXmlName;
41792000   Elena.Budnik   new REMOTEDATA
31
	
41792000   Elena.Budnik   new REMOTEDATA
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	
	public function html_encode($param) 
	{
		$pairs = array(" " => "%20"); 
		
		return strtr($param,$pairs); 
	}
	
	public function init() 	
	{		   							
		$this->getAllSpaseDatasets();		
	}			 
		
   private function openConnection() 
   {	
		$this->ch = curl_init();			 
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
d4071e27   Elena.Budnik   makeProxy with en...
49
		curl_setopt($this->ch, CURLOPT_TIMEOUT, 60); 
41792000   Elena.Budnik   new REMOTEDATA
50
51
52
53
54
55
56
	} 
		  
	private function closeConnection()
	{
		curl_close($this->ch);	
	}
	
41792000   Elena.Budnik   new REMOTEDATA
57
58
59
	protected function setDataViewURL()
	{
		curl_setopt($this->ch, CURLOPT_URL, CDAWebConfigClass::$restUrl."/dataviews");
d4071e27   Elena.Budnik   makeProxy with en...
60
		
41792000   Elena.Budnik   new REMOTEDATA
61
62
63
64
		$this->res = new DomDocument();	
		$this->res->loadXML(curl_exec($this->ch));
		$this->dataViewURL = $this->res->getElementsByTagName('EndpointAddress')->item(0)->nodeValue;
	}
d4071e27   Elena.Budnik   makeProxy with en...
65
66
	
	// Using CDAS REST APIs get all CDAWEB dataset descriptions
41792000   Elena.Budnik   new REMOTEDATA
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
	protected function getRemoteTree()	
	{		
		$this->openConnection();
		
		$this->setDataViewURL();     
      $this->obsGroupsIds = array();           
           
      foreach ( $this->validInstrumentTypes as $insType)
      {		
			curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/observatoryGroupsAndInstruments?instrumentType=".$insType);			   
			echo $insType.PHP_EOL;
			$this->res->loadXML(curl_exec($this->ch));
 	//	   $this->res->save($insType.".xml");
 		   $observatoryGroups = $this->res->getElementsByTagName("ObservatoryGroupInstrumentDescription");
 		   
 		   foreach ( $observatoryGroups as $obsGroup )
 		   {
				$obsGroupId = $obsGroup->getElementsByTagName('Name')->item(0)->nodeValue;
				
				// Exclude some Missions
				if (in_array ($obsGroupId, $this->excludeGroup )) continue;
				
				echo "   ".$obsGroupId.PHP_EOL;
				$observatories =  $obsGroup->getElementsByTagName("ObservatoryInstruments");
				if ( $observatories->length == 0 )
				{
					echo '      No observatories '.PHP_EOL;
					if ($obsGroup->getElementsByTagName("InstrumentDescription")->length > 0)
							echo '      CHECK INSTRUMENTS '.PHP_EOL;				   
					continue;
				}
				$obsIds = array();
				foreach ( $observatories as $obs )
				{
					$obsId = $obs->getElementsByTagName('Name')->item(0)->nodeValue;
					echo "      ".$obsId.PHP_EOL;
// 				<InstrumentDescription>
// 				  <Name>MAG</Name>
// 				  <ShortDescription>ACE Magnetic Field Instrument</ShortDescription>
// 				  <LongDescription>ACE Magnetic Field Instrument</LongDescription>
// 			   </InstrumentDescription>
					$instruments =  $obs->getElementsByTagName("InstrumentDescription");
					
					$insIds = array();
					
					foreach ( $instruments as $ins )
					{
						$insId = $ins->getElementsByTagName('Name')->item(0)->nodeValue;
						
						// Exclude some 'non-AMDA' staff
						if (in_array ($insId, $this->excludeIns )) continue;
						
						$dsIds = $this->getDatasetByObservatoryAndInstrument($obsGroupId, $obsId, $insId);
					   
					   $insIds[$insId] = $dsIds;
  
					}
					
					$obsIds[$obsId] = $insIds;
				}
				
				$this->obsGroupsIds[$obsGroupId][$insType] = $obsIds;
			}
		}	
		
	//	print_r($this->obsGroupsIds);
	//	$this->closeConnection();		
	}
	
	protected function getDatasetByObservatoryAndInstrument($obsGroupId,  $obsId, $insId)
	{
	//	echo "In getDataset ".$obsId." ".$insId.PHP_EOL;
		
		$grpEncodedId = $this->html_encode($obsGroupId);
		$insEncodedId = $this->html_encode($insId);
		
		$res = new DomDocument("1.0");
		curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/datasets?observatoryGroup=$grpEncodedId&instrument=$insEncodedId");	
		
		$res->loadXML(curl_exec($this->ch));
// 		$this->res->save('dataset.xml');
// 		exit();
		$datasets = $res->getElementsByTagName('DatasetDescription');
		$dsIds = array();
		
		foreach ( $datasets as $ds)
		{
			$obs = $ds->getElementsByTagName('Observatory')->item(0)->nodeValue;
			if ($obs != $obsId ) continue;
			$dsIds[] = $ds->getElementsByTagName('Id')->item(0)->nodeValue;
		}
		
		return $dsIds;  
	}
	
	protected function createMissionNodes()
	{
	   $missionNodes = array();
	   $missionIds = array();
	   // unique values
	   // $this->obsGroupsIds = array_map("unserialize", array_unique(array_map("serialize", $this->obsGroupsIds)));
	   
		foreach ($this->obsGroupsIds as $groupId => $insTypes)
		{
			// echo '  GroupID '.$groupId.PHP_EOL;
			$missionId = $this->baseID.":".$this->param2dd($groupId);
			
			if (!in_array($missionId, $missionIds))
			{
				$missionNode = $this->domAmda->createElement('mission');
				$missionNode->setAttribute("xml:id",$missionId);
				$missionNode->setAttribute("name",$groupId);
				
				$missionIds[] = $missionId;
				$newNode = true;
			}
			else {
				$key = array_search($missionId, $missionIds);
				$missionNode = $missionNodes[$key];	
				$newNode = false;
			}
			
155b6b3c   Elena.Budnik   other approach to...
189
			foreach ($insTypes as $insType => $obss){
41792000   Elena.Budnik   new REMOTEDATA
190
191
				// echo $insType.PHP_EOL;
				
155b6b3c   Elena.Budnik   other approach to...
192
				foreach ( $obss as $obs => $inss){
41792000   Elena.Budnik   new REMOTEDATA
193
194
					// echo $obs.PHP_EOL;
					// OMNI ONLY ONCE
155b6b3c   Elena.Budnik   other approach to...
195
					if (substr($obs,0,4) == "OMNI" && substr($missionId,0,4) != "OMNI"){
41792000   Elena.Budnik   new REMOTEDATA
196
197
198
199
						//echo '       '.$obs.' '.$missionId.PHP_EOL;
						continue;
					}
					
155b6b3c   Elena.Budnik   other approach to...
200
201
202
203
204
205
206
207
					if (file_exists(RemoteData."/".CDAWebConfigClass::$mapXML)) {
						$obsAMDA = $this->alias(RemoteData."/".CDAWebConfigClass::$mapXML, $obs);
					}
					else {
						$obsAMDA = $obs;
					}
					
					$obsId = $this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obsAMDA);
41792000   Elena.Budnik   new REMOTEDATA
208
209
210
					$obsNodes = $missionNode->getElementsByTagName('observatory');
					$newObsNode = true;
					
155b6b3c   Elena.Budnik   other approach to...
211
					foreach ($obsNodes as $node){
41792000   Elena.Budnik   new REMOTEDATA
212
213
						$id = $node->getAttribute("xml:id");
						
155b6b3c   Elena.Budnik   other approach to...
214
						if ($id == $obsId) {
41792000   Elena.Budnik   new REMOTEDATA
215
216
217
218
219
							$obsNode = $node;
							$newObsNode = false;							
							break;
						}
					}
155b6b3c   Elena.Budnik   other approach to...
220
221
					
					if ( $newObsNode ){
41792000   Elena.Budnik   new REMOTEDATA
222
223
						$obsNode = $this->createObservatoryNode($obs, $groupId);						
					}
155b6b3c   Elena.Budnik   other approach to...
224
225
					
					foreach ($inss as $ins => $dss){
41792000   Elena.Budnik   new REMOTEDATA
226
227
228
229
						$insId = $this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obs).":".$this->param2dd($ins);
						$insNodes = $obsNode->getElementsByTagName('instrument');
						$newInsNode = true;
						
155b6b3c   Elena.Budnik   other approach to...
230
						foreach ($insNodes as $node){
41792000   Elena.Budnik   new REMOTEDATA
231
232
							$id = $node->getAttribute("xml:id");
							
155b6b3c   Elena.Budnik   other approach to...
233
							if ($id == $insId) {
41792000   Elena.Budnik   new REMOTEDATA
234
235
236
237
238
								$insNode = $node;
								$newInsNode = false;
								break;
							}
						}
155b6b3c   Elena.Budnik   other approach to...
239
						
41792000   Elena.Budnik   new REMOTEDATA
240
241
242
						if ( $newInsNode )					
							$insNode = $this->createInstrumentNode($ins, $obs, $groupId);							
						 
155b6b3c   Elena.Budnik   other approach to...
243
						foreach ($dss as $ds){	
41792000   Elena.Budnik   new REMOTEDATA
244
245
246
247
							$dsId = $this->baseID.":".$ds;
							$dsNodes = $insNode->getElementsByTagName('dataset');
						   $newDsNode = true;
						   
155b6b3c   Elena.Budnik   other approach to...
248
							foreach ($dsNodes as $node){
41792000   Elena.Budnik   new REMOTEDATA
249
250
								$id = $node->getAttribute("xml:id");
								
155b6b3c   Elena.Budnik   other approach to...
251
								if ($id == $dsId){
41792000   Elena.Budnik   new REMOTEDATA
252
253
254
255
256
257
									$dsNode = $node;
									$newDsNode = false;
									break;
								}
							}
							
155b6b3c   Elena.Budnik   other approach to...
258
							if ($newDsNode) {
41792000   Elena.Budnik   new REMOTEDATA
259
260
261
								$this->initDDServerXml($ds,$ins,$obs);
								
								$dsNode =  $this->createDatasetNode($ds);
155b6b3c   Elena.Budnik   other approach to...
262
								if ($dsNode){
41792000   Elena.Budnik   new REMOTEDATA
263
264
265
266
267
268
269
270
271
272
									$insNode->appendChild($dsNode);
									$this->saveDDServerXml();
								}
							}
						}
						
						if ($newInsNode && $insNode->hasChildNodes())
							$obsNode->appendChild($insNode);
					}	
					
155b6b3c   Elena.Budnik   other approach to...
273
					if ($newObsNode && $obsNode->hasChildNodes()){				  
41792000   Elena.Budnik   new REMOTEDATA
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
						$missionNode->appendChild($obsNode);
					}
				}				
				
			}
			
			if ($newNode) 
				$missionNodes[] = $missionNode;
		}
			 
		return $missionNodes;
		
	}
	
	protected function createObservatoryNode($id, $grpId)
	{	
	
155b6b3c   Elena.Budnik   other approach to...
291
		$obsNode = $this->domAmda->createElement('observatory');
41792000   Elena.Budnik   new REMOTEDATA
292
				
155b6b3c   Elena.Budnik   other approach to...
293
294
		$obsNode->setAttribute("xml:id",$this->baseID.":".$this->param2dd($grpId).":".$this->param2dd($id));
		$obsNode->setAttribute("name",$id);
41792000   Elena.Budnik   new REMOTEDATA
295
		
155b6b3c   Elena.Budnik   other approach to...
296
		return $obsNode;
41792000   Elena.Budnik   new REMOTEDATA
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
	}
	
	protected function createInstrumentNode($id, $obsId, $groupId)
	{	
	
		$insNode = $this->domAmda->createElement('instrument');
				
		$insNode->setAttribute("xml:id",$this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obsId).":".$id);
		$insNode->setAttribute("name",$id);
	  //	$insNode->setAttribute("description",$ins[1]);
		
		return $insNode;
	}
	
	protected function createDatasetNode($dsId)
	{		
		$dsNode = $this->domAmda->createElement('dataset');
		
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
		curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/datasets/?idPattern=".$dsId);			   
			
		$obj = json_decode(curl_exec($this->ch));
      $dataSet = $obj->DatasetDescription;
      
		$dsNode->setAttribute("xml:id",$this->baseID.":".$dsId);
		$dsNode->setAttribute("name", $dsId);
		$startTime = $dataSet[0]->TimeInterval->Start;
		$endTime =  $dataSet[0]->TimeInterval->End;
		$label = $dataSet[0]->Label; 
		$url = $dataSet[0]->DatasetLink[0]->Url;
		$notesUrl = $dataSet[0]->Notes; 
		$piAffilation = $dataSet[0]->PiAffiliation;
		$piName = $dataSet[0]->PiName; 
		$sampling = $this->getDatasetSpaseDescription($dsId);
		$this->updateDDServerXml("GlobalStart",$startTime);
		$this->updateDDServerXml("GlobalStop",$endTime);
	
		// no general description - strange dataset
		if ($sampling == -1)
			return null;
			
		if ($sampling < -1)	
			echo "       !!! $dsId $sampling".PHP_EOL;
		else
			$this->updateDDServerXml("MinSampling",$sampling);	
			
155b6b3c   Elena.Budnik   other approach to...
343
		$dsNode->setAttribute('spaseUrl',$this->CDAWEB[$dsId]);
41792000   Elena.Budnik   new REMOTEDATA
344
	//   $dsNode->setAttribute('masterCdf',$this->existsMasterCdf($dsId));
155b6b3c   Elena.Budnik   other approach to...
345
346
347
		$dsNode->setAttribute("desc", "$label; $startTime - $endTime");
		$dsNode->setAttribute("start", $startTime); 
		$dsNode->setAttribute("stop", $stopTime); 
41792000   Elena.Budnik   new REMOTEDATA
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
	   
		$parameterNodes = $this->createParameterNodes($dsId);
		foreach ( $parameterNodes as $parameterNode)
		{
			$dsNode->appendChild($parameterNode);
		}
		
		return $dsNode;
	}
	
	protected function createParameterNodes($dsId)
	{	
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
		curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/datasets/".$dsId."/variables");			   
			 
	 	$obj = json_decode(curl_exec($this->ch));
	 	$parameters = $obj->VariableDescription;
d4071e27   Elena.Budnik   makeProxy with en...
365

41792000   Elena.Budnik   new REMOTEDATA
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
		$paramNodes = array();
		
		foreach ($parameters as $param)
		{
			$paramNode = $this->domAmda->createElement('parameter');
			$paramNode->setAttribute("xml:id", $this->baseID.":".$dsId.":".$param->Name);
			$paramNode->setAttribute("name", $param->Name);
			
			$paramNodes[] = $paramNode;
		}	
		
		return $paramNodes;
	}
	
	protected function getDatasetSpaseDescription($dsID)
	{
	//	$this->openConnection();		 
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Accept: application/xml"));

		if ( !array_key_exists($dsID, $this->CDAWEB ))
			return -1; // no description in SpaseRegistry
			
		curl_setopt($this->ch, CURLOPT_URL, CDAWebConfigClass::$spaseResolver."id=".$this->CDAWEB[$dsID]);
155b6b3c   Elena.Budnik   other approach to...
389
390
391
392
393

		if (!$this->spase_res->loadXML(curl_exec($this->ch)))
			return -100;
 
		$messages = $this->spase_res->getElementsByTagName('Message');
41792000   Elena.Budnik   new REMOTEDATA
394
395
396
397
398
399
400
401
		
		if ($messages->length > 0)
		{
			foreach ($messages as $message)
					echo $message->nodeValue.PHP_EOL;
			return -2;   // no description in  www-spase
		}
		
155b6b3c   Elena.Budnik   other approach to...
402
403
// 		$instrument = $this->spase_res->getElementsByTagName('InstrumentID');
// 	   echo "     instrument ".$instrument->item(0)->nodeValue.PHP_EOL;
41792000   Elena.Budnik   new REMOTEDATA
404
		
155b6b3c   Elena.Budnik   other approach to...
405
		$cadence = $this->spase_res->getElementsByTagName('Cadence');
41792000   Elena.Budnik   new REMOTEDATA
406
		
155b6b3c   Elena.Budnik   other approach to...
407
		if ($cadence->length == 0)
41792000   Elena.Budnik   new REMOTEDATA
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
			return -3; // no cadence in spase xml
			
		$sampling = $this->cadence2sampling($cadence->item(0)->nodeValue);
		
		return $sampling; 	 
	}
	
	public function cadence2sampling($cadence)
	{
		$scale = array('S' => 1, 'M' => 60, 'H' => '3600');
	   
		$value = substr($cadence,2,strlen($cadence)-3);
 
		$units = substr($cadence,-1); 
	   
		$sampling = (int)$value*$scale[$units];
	
		return $sampling;
	}
	
d4071e27   Elena.Budnik   makeProxy with en...
428
	// Get IDs and SPASE URLs of all CDAWEB SPASE-defined datasets 
41792000   Elena.Budnik   new REMOTEDATA
429
430
431
432
	protected function getAllSpaseDatasets()
	{
		require_once "simple_html_dom.php";
		
d4071e27   Elena.Budnik   makeProxy with en...
433
434
		if (file_exists("NumericalData.html")) 
				rename("NumericalData.html","NumericalData.html.bak");
41792000   Elena.Budnik   new REMOTEDATA
435
		
d4071e27   Elena.Budnik   makeProxy with en...
436
437
438
439
		// if cannot reach CDAWEB Spase Registry use an old file
		if (!copy(CDAWebConfigClass::$spaseRegistry, "NumericalData.html"))
				copy("NumericalData.html.bak", "NumericalData.html");

41792000   Elena.Budnik   new REMOTEDATA
440
441
442
443
444
445
446
447
448
449
		$html = file_get_html('NumericalData.html');
		$ids = $html->find('td[class="Spase.URL.ProductID"]');

		foreach ($ids as $id)
		{		    
		   $ref = $id->next_sibling()->find('a');
		   $key = $id->find('a');
		   $this->CDAWEB[$key[0]->innertext] = $ref[0]->innertext;		   
		}
		
d4071e27   Elena.Budnik   makeProxy with en...
450
		echo "SPASE-defined CDAWEB datasets : ".count($this->CDAWEB).PHP_EOL;		
41792000   Elena.Budnik   new REMOTEDATA
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
	}
	
	protected function initDDServerXml($ds, $ins, $obs)
	{
		$this->DDserverXml = new DomDocument("1.0");
		$this->DDserverXmlName =  $this->DDserverDir."/".$ds.".xml";
		$rootNode = $this->DDserverXml->createElement('VI');
		$this->DDserverXml->appendChild($rootNode);
		$rootNode->appendChild($this->DDserverXml->createElement("DataBaseID", $this->baseID));
		$rootNode->appendChild($this->DDserverXml->createElement("Mission", $obs));
		$rootNode->appendChild($this->DDserverXml->createElement("Instrument", $ins));
		$rootNode->appendChild($this->DDserverXml->createElement("DataSetID", $ds));
		$rootNode->appendChild($this->DDserverXml->createElement("GlobalStart"));
		$rootNode->appendChild($this->DDserverXml->createElement("GlobalStop"));
		$rootNode->appendChild($this->DDserverXml->createElement("MinSampling"));	
	}
	
	protected function updateDDServerXml($target, $value)
	{
		$node = $this->DDserverXml->getElementsByTagName($target);
		$node->item(0)->nodeValue = $value;
	}
	
	protected function saveDDServerXml()
	{
		$this->DDserverXml->save($this->DDserverXmlName);
	}
	
	protected function existsMasterCdf($dsId)
	{
		$file = CDAWebConfigClass::$masterUrl.strtolower($dsId)."_00000000_v01.cdf";
		$file_headers = @get_headers($file);
		
		if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
			$exists = false;
		//	echo "           NO master cdf ".$dsId.PHP_EOL;
		}
		else {
			$exists = true;
		}
		
		return $exists;
	}
		 
	protected function getMasterCdf($dsId)
	{
		$file = CDAWebConfigClass::$masterUrl.strtolower($dsId)."_00000000_v01.cdf";
		$file_headers = @get_headers($file);
		
		if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
			 return false;
		}		 
		return $file;		 	 
	}
	
	public function getData($ds, $start, $stop)
	{
		$this->openConnection();
		$this->setDataViewURL(); 

	   curl_setopt($this->ch, CURLOPT_URL,$this->dataViewURL."/datasets/$ds/orig_data/$start,$stop/");
	   $res = new DomDocument("1.0");
	   
	   $res->loadXML(curl_exec($this->ch));
	   //TODO errors
	   if ($res->getElementsByTagName("html")->length > 0)
	   {
			echo $res->saveXML();
			exit();
	   }
	   
	   $fileNames = $res->getElementsByTagName("Name");
	   	   
		$nc_prefix = strlen($ds) > RemoteDataCenterClass::$MAX_VI_NAME_LENGTH ? 
				substr(strtolower($ds),0,RemoteDataCenterClass::$MAX_VI_NAME_LENGTH - 1): strtolower($ds);
		$files = array();
	   
		for ($i = 0; $i < $fileNames->length;  $i++)
	   {
			$fileName = $fileNames->item($i);
			$url = $fileName->nodeValue;
			$file_headers = @get_headers($url);
		
			if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
			//TODO  errors
				continue;
			}	
			$temp = explode('/',$url);
			$destination = $temp[count($temp)-1];
			
			if (copy($url, $destination))
			{
				$ncFile = $this->convert2nc($destination);
				if (strlen($ncFile) > RemoteDataCenterClass::$MAX_NAME_LENGTH)
				{
					$ncFileTrue = $nc_prefix."_".time().$i.".nc";
					rename($ncFile, $ncFileTrue);
					$files[] = $ncFileTrue;
				}
				else
				{
					$files[] = $ncFile;
				}
			}
			else
			{
			 //TODO errors
			}
	   }
	   
		$this->closeConnection();
		
		return $files;
	}
	
	protected function convert2nc($file)
	{
		//TODO errors
		system("cdf2nc $file");		 
		$ncFile = str_replace(".cdf", ".nc", $file);
		
0ec21281   Elena.Budnik   reorganization + ...
572
 		if (!file_exists($ncFile)) return false; 
41792000   Elena.Budnik   new REMOTEDATA
573
574
575
576
577
		unlink($file);	
		
		return $ncFile;
	}
	
0ec21281   Elena.Budnik   reorganization + ...
578
579
580
581
582
583
584
585
586
587
588
	public function getDatasetInfo($ds)
	{
		$masterCdf = strtolower($ds)."_00000000_v01.cdf";	
		$localCdf = strtolower($ds).".cdf";
		
		if (!copy(CDAWebConfigClass::$masterUrl."/".$masterCdf,  strtolower($ds).".cdf")) return false;
	 
		$infoFile = $this->convert2nc($localCdf);
	  
		return $infoFile;
	}
155b6b3c   Elena.Budnik   other approach to...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
	
	public function makeProxy()
	{	 
		$this->spase_res = new DomDocument("1.0");
		$this->domAmda = new DOMDocument('1.0', 'utf-8');
		$this->domAmda->formatOutput = TRUE;
		$this->domAmda->preserveWhiteSpace = FALSE;
		
		$dataRoot = $this->domAmda->createElement('dataRoot');
		$dataRoot->setAttribute('xml:id', 'myRemoteData-treeRootNode');
		$this->domAmda->appendChild($dataRoot);	
		
		$this->dataCenter=$this->domAmda->createElement('dataCenter');

		$this->dataCenter->setAttribute('xml:id', $this->baseID);
	//	$this->dataCenter->setIdAttribute('xml:id', true);
		$this->dataCenter->setAttribute('name', $this->baseID);		 
		
		$dataRoot->appendChild($this->dataCenter);
		
		$this->setDataCenterAttributes();	

		foreach ($this->obsGroupsIds as $groupId => $insTypes){
			foreach ($insTypes as $insType => $obss){
				$obsNodes = array();
				foreach ($obss as $obs => $inss){
					$insNodes = array();
					// Create instrument nodes
					foreach ($inss as $ins => $dss){
						$dsNodes = array();
						// Create datasets nodes
						foreach ($dss as $ds){	
							$dsId = $this->baseID.":".$ds;
155b6b3c   Elena.Budnik   other approach to...
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
							if (!$this->domAmda->getElementById($dsId)){
								$this->initDDServerXml($ds,$ins,$obs);
								$dsNode = $this->createDatasetNode($ds);
								if ($dsNode){
									$this->saveDDServerXml();									
									$dsNodes[] = $dsNode;
								}
							}	
						} // foreach ($dss as $ds)
						if ($dsNodes){
							// last $spase_res : instrument should be the same
							$insSpaseId = $this->getInstrumentSpase();
							if (!$insSpaseId ) 
								$insSpaseId = $ins;
								
62372d94   Elena.Budnik   RemoveVi
637
638
639
640
641
							$obsSpaseId = $this->getObservatorySpase();
							if (!$obsSpaseId ) 
								$obsSpaseId = $obs;
							
							$insId = $this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obsSpaseId).":".$this->param2dd($insSpaseId);
155b6b3c   Elena.Budnik   other approach to...
642
643
							
							if (!($insNode = $this->domAmda->getElementById($insId))){
62372d94   Elena.Budnik   RemoveVi
644
								$insNode = $this->createInstrumentNode($insSpaseId, $obsSpaseId, $groupId);
155b6b3c   Elena.Budnik   other approach to...
645
646
647
648
649
650
651
652
								$insNodes[] = $insNode;
							}
							foreach ($dsNodes as $dsNode){
								$insNode->appendChild($dsNode);
							}
						}
					} // foreach ($inss as $ins => $dss)
					if ($insNodes){
62372d94   Elena.Budnik   RemoveVi
653
654
					
						$obsId = $this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obsSpaseId);
155b6b3c   Elena.Budnik   other approach to...
655
656
						
						if (!($obsNode = $this->domAmda->getElementById($obsId))){
62372d94   Elena.Budnik   RemoveVi
657
							$obsNode = $this->createObservatoryNode($obsSpaseId, $groupId);
155b6b3c   Elena.Budnik   other approach to...
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
							$obsNodes[] = $obsNode;
						}
						
						foreach ($insNodes as $insNode){
							$obsNode->appendChild($insNode);
						}
					}
				} // foreach ($obss as $obs => $inss)
			}
			if ($obsNodes){
				$missionId = $this->baseID.":".$this->param2dd($groupId);
				
				if (!($missionNode = $this->domAmda->getElementById($missionId))){
					$missionNode = $this->createMissionNode($groupId);
					$this->dataCenter->appendChild($missionNode);
				}
				
				foreach ($obsNodes as $obsNode){
62372d94   Elena.Budnik   RemoveVi
676
677
678
679
680
681
682
683
					// observatory == mission
					if ($obsNode->getAttribute('name') == $missionNode->getAttribute('name')) {
						$insNodes = $obsNode->getElementsByTagName("instrument");
						foreach ($insNodes as $insNode)
							$missionNode->appendChild($insNode);
					}
					else 
						$missionNode->appendChild($obsNode);
155b6b3c   Elena.Budnik   other approach to...
684
				}
62372d94   Elena.Budnik   RemoveVi
685
686
687
688
689
690
			}	
			// getElemetByID doesn't work on newly created Document
			$this->domAmda->save('temp.xml');
			$this->domAmda->load('temp.xml');
			
			$this->dataCenter = $this->domAmda->getElementById($this->baseID);
155b6b3c   Elena.Budnik   other approach to...
691
692
693
694
695
696
697
698
699
700
701
702
703
		}
	}
	
	protected function createMissionNode($groupId){
		$missionId = $this->baseID.":".$this->param2dd($groupId);
		$missionNode = $this->domAmda->createElement('mission');
		$missionNode->setAttribute("xml:id",$missionId);
		$missionNode->setAttribute("name",$groupId);
		
		return $missionNode;
	}
	
	protected function getInstrumentSpase(){
62372d94   Elena.Budnik   RemoveVi
704
705
	
		$this->insXML = new DomDocument("1.0");
155b6b3c   Elena.Budnik   other approach to...
706
707
708
709
710
		
		if ($this->spase_res){
			$instrument = $this->spase_res->getElementsByTagName('InstrumentID');
			
			if ($instrument->length > 0) {
62372d94   Elena.Budnik   RemoveVi
711
712
713
714
715
				
				curl_setopt($this->ch, CURLOPT_URL, CDAWebConfigClass::$spaseResolver."id=".$instrument->item(0)->nodeValue);
				
				$this->insXML->loadXML(curl_exec($this->ch));
				
155b6b3c   Elena.Budnik   other approach to...
716
				return $this->getIdFromSpase($instrument->item(0)->nodeValue);
155b6b3c   Elena.Budnik   other approach to...
717
718
719
720
721
722
723
724
				//InstrumentType
				//spase://SMWG/Observatory/ACE
			} 
		}	
		
		return null;
	}
	
62372d94   Elena.Budnik   RemoveVi
725
726
727
728
729
730
731
732
733
734
735
736
737
	protected function getObservatorySpase(){
		
		if ($this->insXML){
			$observatory = $this->insXML->getElementsByTagName('ObservatoryID');
			
			if ($observatory->length > 0) {
				return $this->getIdFromSpase($observatory->item(0)->nodeValue); 			
			} 
		}	
		
		return null;
	}
	
41792000   Elena.Budnik   new REMOTEDATA
738
739
740
741
	protected function setDataCenterAttributes(){}
	protected function makeArgumentsList(){}
} 
?>