Blame view

src/REMOTEDATA/CDAWEB.php 19.9 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
	
	private $observatoryGroups, $instrumentTypes;
	private $CDAWEB = array(), $masterCDF = array();
05fbcab0   Elena.Budnik   DDServices reorga...
15
	private $header;
41792000   Elena.Budnik   new REMOTEDATA
16
17
	
	// https://cdaweb.gsfc.nasa.gov/WS/cdasr/1/dataviews/sp_phys/instrumentTypes
1b0e0631   Elena.Budnik   unique name for obs
18
	private $validInstrumentTypes = [ "Activity%20Indices", "Electric%20Fields%20(space)","Gamma%20and%20X-Rays",
41792000   Elena.Budnik   new REMOTEDATA
19
                                     "Magnetic%20Fields%20(space)", "Particles%20(space)", "Plasma%20and%20Solar%20Wind", 
1b0e0631   Elena.Budnik   unique name for obs
20
                                     "Radio%20and%20Plasma%20Waves%20(space)" ];     
775592ff   Elena.Budnik   bug in insNodes a...
21
//    private $validInstrumentTypes = [ "Radio%20and%20Plasma%20Waves%20(space)" ];                               
155b6b3c   Elena.Budnik   other approach to...
22
                                     
41792000   Elena.Budnik   new REMOTEDATA
23
24
25
26
27
28
//   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
775592ff   Elena.Budnik   bug in insNodes a...
29
   private $excludeDatasets = [ "DMSP_R0_SSJ4", "DMSP_R0_SSIES", "I7_R0_LEPEDEA", "I8_R0_LEPEDEA" ];
41792000   Elena.Budnik   new REMOTEDATA
30
   
d4071e27   Elena.Budnik   makeProxy with en...
31
	protected $DDserverXml, $DDserverXmlName;
41792000   Elena.Budnik   new REMOTEDATA
32
	
41792000   Elena.Budnik   new REMOTEDATA
33
34
35
36
37
38
39
40
41
	
	public function html_encode($param) 
	{
		$pairs = array(" " => "%20"); 
		
		return strtr($param,$pairs); 
	}
	
	public function init() 	
8c0d5a9f   Elena.Budnik   correct location ...
42
	{			
05fbcab0   Elena.Budnik   DDServices reorga...
43
		error_log("CDAWEB Proxy creation on ".date("Y-m-d").PHP_EOL,3,log);
8c0d5a9f   Elena.Budnik   correct location ...
44
		error_log("CDAWEB Proxy creation on ".date("Y-m-d").PHP_EOL,3,err);
05fbcab0   Elena.Budnik   DDServices reorga...
45
		
41792000   Elena.Budnik   new REMOTEDATA
46
47
48
49
50
51
52
		$this->getAllSpaseDatasets();		
	}			 
		
   private function openConnection() 
   {	
		$this->ch = curl_init();			 
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
d4071e27   Elena.Budnik   makeProxy with en...
53
		curl_setopt($this->ch, CURLOPT_TIMEOUT, 60); 
41792000   Elena.Budnik   new REMOTEDATA
54
55
56
57
58
59
60
	} 
		  
	private function closeConnection()
	{
		curl_close($this->ch);	
	}
	
41792000   Elena.Budnik   new REMOTEDATA
61
62
63
	protected function setDataViewURL()
	{
		curl_setopt($this->ch, CURLOPT_URL, CDAWebConfigClass::$restUrl."/dataviews");
d4071e27   Elena.Budnik   makeProxy with en...
64
		
41792000   Elena.Budnik   new REMOTEDATA
65
66
		$this->res = new DomDocument();	
		$this->res->loadXML(curl_exec($this->ch));
775592ff   Elena.Budnik   bug in insNodes a...
67
68
69
70
71
72
		
		if ($this->res->getElementsByTagName('EndpointAddress')->length == 0) {
				error_log("Problem connect to ".CDAWebConfigClass::$restUrl."/dataviews", 3, err);
				exit("Problem connect to ".CDAWebConfigClass::$restUrl."/dataviews".PHP_EOL);
		}
		
41792000   Elena.Budnik   new REMOTEDATA
73
74
		$this->dataViewURL = $this->res->getElementsByTagName('EndpointAddress')->item(0)->nodeValue;
	}
d4071e27   Elena.Budnik   makeProxy with en...
75
76
	
	// Using CDAS REST APIs get all CDAWEB dataset descriptions
41792000   Elena.Budnik   new REMOTEDATA
77
78
79
80
81
82
83
	protected function getRemoteTree()	
	{		
		$this->openConnection();
		
		$this->setDataViewURL();     
      $this->obsGroupsIds = array();           
           
775592ff   Elena.Budnik   bug in insNodes a...
84
      foreach ($this->validInstrumentTypes as $insType)
41792000   Elena.Budnik   new REMOTEDATA
85
86
87
88
      {		
			curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/observatoryGroupsAndInstruments?instrumentType=".$insType);			   
			echo $insType.PHP_EOL;
			$this->res->loadXML(curl_exec($this->ch));
775592ff   Elena.Budnik   bug in insNodes a...
89
90

			$observatoryGroups = $this->res->getElementsByTagName("ObservatoryGroupInstrumentDescription");
41792000   Elena.Budnik   new REMOTEDATA
91
 		   
775592ff   Elena.Budnik   bug in insNodes a...
92
93
			foreach ($observatoryGroups as $obsGroup)
			{
41792000   Elena.Budnik   new REMOTEDATA
94
95
96
97
98
99
100
101
102
103
				$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;
8c0d5a9f   Elena.Budnik   correct location ...
104
105
106
107
					if ($obsGroup->getElementsByTagName("InstrumentDescription")->length > 0) {
							echo '      CHECK INSTRUMENTS '.PHP_EOL;
							error_log(" CHECK INSTRUMENTS ".$obsGroup.PHP_EOL, 3, err);
					}	
41792000   Elena.Budnik   new REMOTEDATA
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
					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;
				}
				
1b0e0631   Elena.Budnik   unique name for obs
140
				$this->obsGroupsIds[$insType][$obsGroupId] = $obsIds;
41792000   Elena.Budnik   new REMOTEDATA
141
			}
05fbcab0   Elena.Budnik   DDServices reorga...
142
		}		
41792000   Elena.Budnik   new REMOTEDATA
143
144
145
	}
	
	protected function getDatasetByObservatoryAndInstrument($obsGroupId,  $obsId, $insId)
8c0d5a9f   Elena.Budnik   correct location ...
146
	{		
41792000   Elena.Budnik   new REMOTEDATA
147
148
		$grpEncodedId = $this->html_encode($obsGroupId);
		$insEncodedId = $this->html_encode($insId);
05fbcab0   Elena.Budnik   DDServices reorga...
149
		$obsEncodedId = $this->html_encode($obsId);
41792000   Elena.Budnik   new REMOTEDATA
150
151
		
		$res = new DomDocument("1.0");
05fbcab0   Elena.Budnik   DDServices reorga...
152
153
154
	//	curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/datasets?observatoryGroup=$grpEncodedId&instrument=$insEncodedId");
	
		curl_setopt($this->ch, CURLOPT_URL, $this->dataViewURL."/datasets?observatory=$obsEncodedId&instrument=$insEncodedId");
41792000   Elena.Budnik   new REMOTEDATA
155
156
		
		$res->loadXML(curl_exec($this->ch));
05fbcab0   Elena.Budnik   DDServices reorga...
157

41792000   Elena.Budnik   new REMOTEDATA
158
159
160
161
162
		$datasets = $res->getElementsByTagName('DatasetDescription');
		$dsIds = array();
		
		foreach ( $datasets as $ds)
		{
05fbcab0   Elena.Budnik   DDServices reorga...
163
		//	$obs = $ds->getElementsByTagName('Observatory')->item(0)->nodeValue;
8c0d5a9f   Elena.Budnik   correct location ...
164
		//	if ($obs != $obsId ) continue;
41792000   Elena.Budnik   new REMOTEDATA
165
166
167
168
169
170
			$dsIds[] = $ds->getElementsByTagName('Id')->item(0)->nodeValue;
		}
		
		return $dsIds;  
	}
	
41792000   Elena.Budnik   new REMOTEDATA
171
172
173
	protected function createObservatoryNode($id, $grpId)
	{	
	
155b6b3c   Elena.Budnik   other approach to...
174
		$obsNode = $this->domAmda->createElement('observatory');
41792000   Elena.Budnik   new REMOTEDATA
175
				
155b6b3c   Elena.Budnik   other approach to...
176
177
		$obsNode->setAttribute("xml:id",$this->baseID.":".$this->param2dd($grpId).":".$this->param2dd($id));
		$obsNode->setAttribute("name",$id);
41792000   Elena.Budnik   new REMOTEDATA
178
		
155b6b3c   Elena.Budnik   other approach to...
179
		return $obsNode;
41792000   Elena.Budnik   new REMOTEDATA
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
	}
	
	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);
1b0e0631   Elena.Budnik   unique name for obs
216
		
41792000   Elena.Budnik   new REMOTEDATA
217
		// no general description - strange dataset
8c0d5a9f   Elena.Budnik   correct location ...
218
219
		if ($sampling == -1) {
			error_log("! No general description : ".$dsId.PHP_EOL,3,err);
41792000   Elena.Budnik   new REMOTEDATA
220
			return null;
8c0d5a9f   Elena.Budnik   correct location ...
221
		}
41792000   Elena.Budnik   new REMOTEDATA
222
			
8c0d5a9f   Elena.Budnik   correct location ...
223
		if ($sampling < -1)	{
41792000   Elena.Budnik   new REMOTEDATA
224
			echo "       !!! $dsId $sampling".PHP_EOL;
8c0d5a9f   Elena.Budnik   correct location ...
225
226
			error_log("! No sampling : ".$dsId." : ".$sampling.PHP_EOL,3,err);
		}
41792000   Elena.Budnik   new REMOTEDATA
227
228
		else
			$this->updateDDServerXml("MinSampling",$sampling);	
1b0e0631   Elena.Budnik   unique name for obs
229
		
155b6b3c   Elena.Budnik   other approach to...
230
		$dsNode->setAttribute('spaseUrl',$this->CDAWEB[$dsId]);
41792000   Elena.Budnik   new REMOTEDATA
231
	//   $dsNode->setAttribute('masterCdf',$this->existsMasterCdf($dsId));
155b6b3c   Elena.Budnik   other approach to...
232
233
234
		$dsNode->setAttribute("desc", "$label; $startTime - $endTime");
		$dsNode->setAttribute("start", $startTime); 
		$dsNode->setAttribute("stop", $stopTime); 
41792000   Elena.Budnik   new REMOTEDATA
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
	   
		$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...
252

41792000   Elena.Budnik   new REMOTEDATA
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
		$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...
276
277
278
279
280

		if (!$this->spase_res->loadXML(curl_exec($this->ch)))
			return -100;
 
		$messages = $this->spase_res->getElementsByTagName('Message');
41792000   Elena.Budnik   new REMOTEDATA
281
282
283
284
285
286
287
288
		
		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...
289
290
// 		$instrument = $this->spase_res->getElementsByTagName('InstrumentID');
// 	   echo "     instrument ".$instrument->item(0)->nodeValue.PHP_EOL;
41792000   Elena.Budnik   new REMOTEDATA
291
		
155b6b3c   Elena.Budnik   other approach to...
292
		$cadence = $this->spase_res->getElementsByTagName('Cadence');
41792000   Elena.Budnik   new REMOTEDATA
293
		
155b6b3c   Elena.Budnik   other approach to...
294
		if ($cadence->length == 0)
41792000   Elena.Budnik   new REMOTEDATA
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
			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); 
	   
0fb01225   Elena.Budnik   float sampling
310
311
		$sampling = floatval($value)*$scale[$units];
		
41792000   Elena.Budnik   new REMOTEDATA
312
313
314
		return $sampling;
	}
	
d4071e27   Elena.Budnik   makeProxy with en...
315
	// Get IDs and SPASE URLs of all CDAWEB SPASE-defined datasets 
41792000   Elena.Budnik   new REMOTEDATA
316
317
318
319
	protected function getAllSpaseDatasets()
	{
		require_once "simple_html_dom.php";
		
8c0d5a9f   Elena.Budnik   correct location ...
320
321
		if (file_exists($this->location."/NumericalData.html")) 
				rename($this->location."/NumericalData.html",$this->location."/NumericalData.html.bak");
41792000   Elena.Budnik   new REMOTEDATA
322
		
d4071e27   Elena.Budnik   makeProxy with en...
323
		// if cannot reach CDAWEB Spase Registry use an old file
8c0d5a9f   Elena.Budnik   correct location ...
324
325
		if (!copy(CDAWebConfigClass::$spaseRegistry, $this->location."/NumericalData.html"))
				copy($this->location."/NumericalData.html.bak", $this->location."/NumericalData.html");
d4071e27   Elena.Budnik   makeProxy with en...
326

8c0d5a9f   Elena.Budnik   correct location ...
327
		$html = file_get_html($this->location.'/NumericalData.html');
41792000   Elena.Budnik   new REMOTEDATA
328
329
330
331
332
333
334
335
		$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;		   
		}
8c0d5a9f   Elena.Budnik   correct location ...
336
		error_log("SPASE-defined CDAWEB datasets from ".CDAWebConfigClass::$spaseRegistry." : ".count($this->CDAWEB).PHP_EOL,3,log);
05fbcab0   Elena.Budnik   DDServices reorga...
337
		echo "SPASE-defined CDAWEB datasets : ".count($this->CDAWEB).PHP_EOL;
41792000   Elena.Budnik   new REMOTEDATA
338
339
340
341
342
343
344
345
346
347
348
349
350
351
	}
	
	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"));
1b0e0631   Elena.Budnik   unique name for obs
352
353
		$rootNode->appendChild($this->DDserverXml->createElement("MinSampling"));
		// fill value by default - if different - add into param description
2f2f6cee   Elena.Budnik   FillValue by default
354
		$rootNode->appendChild($this->DDserverXml->createElement("FillValue", -1.0E31));
41792000   Elena.Budnik   new REMOTEDATA
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
	}
	
	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;
8c0d5a9f   Elena.Budnik   correct location ...
375
			error_log("INFO:   no master CDF ".$dsId.PHP_EOL,3,log);
41792000   Elena.Budnik   new REMOTEDATA
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
		}
		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));
8c0d5a9f   Elena.Budnik   correct location ...
404

41792000   Elena.Budnik   new REMOTEDATA
405
406
	   if ($res->getElementsByTagName("html")->length > 0)
	   {
8c0d5a9f   Elena.Budnik   correct location ...
407
408
			error_log("ERROR no response for : ".$ds." : ".$start." - ".$stop.PHP_EOL,3,err);
			error_log($res->saveXML(),3,err);
41792000   Elena.Budnik   new REMOTEDATA
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
			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') {
8c0d5a9f   Elena.Budnik   correct location ...
425
				error_log("ERROR  404 Not Found for : ".$ds." : ".$start." - ".$stop.PHP_EOL,3,err);
41792000   Elena.Budnik   new REMOTEDATA
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
				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
			{
8c0d5a9f   Elena.Budnik   correct location ...
447
				error_log("ERROR cannot copy files : ".$ds." : ".$start." - ".$stop.PHP_EOL,3,err);
41792000   Elena.Budnik   new REMOTEDATA
448
449
450
451
452
453
454
455
456
457
			}
	   }
	   
		$this->closeConnection();
		
		return $files;
	}
	
	protected function convert2nc($file)
	{
0fb01225   Elena.Budnik   float sampling
458
		system("cdfnew2nc $file");		 
41792000   Elena.Budnik   new REMOTEDATA
459
460
		$ncFile = str_replace(".cdf", ".nc", $file);
		
8c0d5a9f   Elena.Budnik   correct location ...
461
462
463
464
 		if (!file_exists($ncFile)) {
			error_log("ERROR while converting ".$file.PHP_EOL,3,err);
			return false; 
 		}
41792000   Elena.Budnik   new REMOTEDATA
465
466
467
468
469
		unlink($file);	
		
		return $ncFile;
	}
	
0ec21281   Elena.Budnik   reorganization + ...
470
471
472
473
474
475
476
477
478
479
480
	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...
481
482
483
484
485
486
487
488
489
490
491
492
493
	
	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');
8c0d5a9f   Elena.Budnik   correct location ...
494
495
	 	$this->setDataCenterAttributes();
	 	
155b6b3c   Elena.Budnik   other approach to...
496
		$dataRoot->appendChild($this->dataCenter);
8c0d5a9f   Elena.Budnik   correct location ...
497
				
1b0e0631   Elena.Budnik   unique name for obs
498
499
		$first = true;
		
8c0d5a9f   Elena.Budnik   correct location ...
500
501
		foreach ($this->obsGroupsIds as $insType => $groupIds)
		{		
1b0e0631   Elena.Budnik   unique name for obs
502
503
504
505
506
			// getElementByID doesn't work on newly created Document
			if (!$first) {
				$this->domAmda->loadXML($this->domAmda->saveXML());			
				$this->dataCenter = $this->domAmda->getElementById($this->baseID);
			}
1b0e0631   Elena.Budnik   unique name for obs
507
			
8c0d5a9f   Elena.Budnik   correct location ...
508
509
			foreach ($groupIds as $groupId => $obss)
			{			
155b6b3c   Elena.Budnik   other approach to...
510
				$obsNodes = array();
775592ff   Elena.Budnik   bug in insNodes a...
511
				
1b0e0631   Elena.Budnik   unique name for obs
512
				foreach ($obss as $obs => $inss)
8c0d5a9f   Elena.Budnik   correct location ...
513
514
				{	
					// ! observatory OMNI is taken for observatoriesGroup OMNI only !
1b0e0631   Elena.Budnik   unique name for obs
515
516
517
					if (substr($obs,0,4) == "OMNI" && substr($groupId,0,4) != "OMNI"){
						echo 'OMNI      '.$obs.' '.$missionId.PHP_EOL;
						continue;
775592ff   Elena.Budnik   bug in insNodes a...
518
					} 
155b6b3c   Elena.Budnik   other approach to...
519
520
					$insNodes = array();
					// Create instrument nodes
8c0d5a9f   Elena.Budnik   correct location ...
521
522
					foreach ($inss as $ins => $dss)
					{
155b6b3c   Elena.Budnik   other approach to...
523
524
						$dsNodes = array();
						// Create datasets nodes
8c0d5a9f   Elena.Budnik   correct location ...
525
526
						foreach ($dss as $ds)
						{	
155b6b3c   Elena.Budnik   other approach to...
527
							$dsId = $this->baseID.":".$ds;
155b6b3c   Elena.Budnik   other approach to...
528
529
530
531
532
533
534
535
536
							if (!$this->domAmda->getElementById($dsId)){
								$this->initDDServerXml($ds,$ins,$obs);
								$dsNode = $this->createDatasetNode($ds);
								if ($dsNode){
									$this->saveDDServerXml();									
									$dsNodes[] = $dsNode;
								}
							}	
						} // foreach ($dss as $ds)
8c0d5a9f   Elena.Budnik   correct location ...
537
538
						if ($dsNodes)
						{
155b6b3c   Elena.Budnik   other approach to...
539
540
541
542
543
							// last $spase_res : instrument should be the same
							$insSpaseId = $this->getInstrumentSpase();
							if (!$insSpaseId ) 
								$insSpaseId = $ins;
								
775592ff   Elena.Budnik   bug in insNodes a...
544
							$obsSpaseId = strtolower($this->getObservatorySpase());
62372d94   Elena.Budnik   RemoveVi
545
							if (!$obsSpaseId ) 
775592ff   Elena.Budnik   bug in insNodes a...
546
								$obsSpaseId = strtolower($obs);
62372d94   Elena.Budnik   RemoveVi
547
548
							
							$insId = $this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obsSpaseId).":".$this->param2dd($insSpaseId);
155b6b3c   Elena.Budnik   other approach to...
549
550
							
							if (!($insNode = $this->domAmda->getElementById($insId))){
62372d94   Elena.Budnik   RemoveVi
551
								$insNode = $this->createInstrumentNode($insSpaseId, $obsSpaseId, $groupId);
775592ff   Elena.Budnik   bug in insNodes a...
552
							} 
155b6b3c   Elena.Budnik   other approach to...
553
554
555
							foreach ($dsNodes as $dsNode){
								$insNode->appendChild($dsNode);
							}
775592ff   Elena.Budnik   bug in insNodes a...
556
							$insNodes[] = $insNode;
155b6b3c   Elena.Budnik   other approach to...
557
558
						}
					} // foreach ($inss as $ins => $dss)
8c0d5a9f   Elena.Budnik   correct location ...
559
560
					if ($insNodes)
					{					
62372d94   Elena.Budnik   RemoveVi
561
						$obsId = $this->baseID.":".$this->param2dd($groupId).":".$this->param2dd($obsSpaseId);
155b6b3c   Elena.Budnik   other approach to...
562
						
1b0e0631   Elena.Budnik   unique name for obs
563
						if (!($obsNode = $this->domAmda->getElementById($obsId))){   
62372d94   Elena.Budnik   RemoveVi
564
							$obsNode = $this->createObservatoryNode($obsSpaseId, $groupId);
155b6b3c   Elena.Budnik   other approach to...
565
566
567
568
569
						}
						
						foreach ($insNodes as $insNode){
							$obsNode->appendChild($insNode);
						}
775592ff   Elena.Budnik   bug in insNodes a...
570
571
						
						$obsNodes[] = $obsNode;
155b6b3c   Elena.Budnik   other approach to...
572
573
					}
				} // foreach ($obss as $obs => $inss)
155b6b3c   Elena.Budnik   other approach to...
574
				
8c0d5a9f   Elena.Budnik   correct location ...
575
576
				if ($obsNodes)
				{
1b0e0631   Elena.Budnik   unique name for obs
577
578
579
580
581
					$missionId = $this->baseID.":".$this->param2dd($groupId);
					
					if (!($missionNode = $this->domAmda->getElementById($missionId))){
						$missionNode = $this->createMissionNode($groupId);
						$this->dataCenter->appendChild($missionNode);
62372d94   Elena.Budnik   RemoveVi
582
					}
1b0e0631   Elena.Budnik   unique name for obs
583
					
8c0d5a9f   Elena.Budnik   correct location ...
584
585
					foreach ($obsNodes as $obsNode)
					{
1b0e0631   Elena.Budnik   unique name for obs
586
						// observatory == mission
8c0d5a9f   Elena.Budnik   correct location ...
587
						if (strtolower($obsNode->getAttribute('name')) == strtolower($missionNode->getAttribute('name'))) {
1b0e0631   Elena.Budnik   unique name for obs
588
589
590
591
592
593
594
							$insNodes = $obsNode->getElementsByTagName("instrument");
							foreach ($insNodes as $insNode)
								$missionNode->appendChild($insNode);
						}
						else 
							$missionNode->appendChild($obsNode);
					}	
155b6b3c   Elena.Budnik   other approach to...
595
				}
1b0e0631   Elena.Budnik   unique name for obs
596
597
			}
			$first = false;
155b6b3c   Elena.Budnik   other approach to...
598
		}
05fbcab0   Elena.Budnik   DDServices reorga...
599
600
		
		$this->closeConnection();
155b6b3c   Elena.Budnik   other approach to...
601
602
	}
	
8c0d5a9f   Elena.Budnik   correct location ...
603
604
	protected function createMissionNode($groupId)
	{
155b6b3c   Elena.Budnik   other approach to...
605
606
607
608
609
610
611
612
		$missionId = $this->baseID.":".$this->param2dd($groupId);
		$missionNode = $this->domAmda->createElement('mission');
		$missionNode->setAttribute("xml:id",$missionId);
		$missionNode->setAttribute("name",$groupId);
		
		return $missionNode;
	}
	
8c0d5a9f   Elena.Budnik   correct location ...
613
614
	protected function getInstrumentSpase()
	{	
62372d94   Elena.Budnik   RemoveVi
615
		$this->insXML = new DomDocument("1.0");
155b6b3c   Elena.Budnik   other approach to...
616
617
618
619
620
		
		if ($this->spase_res){
			$instrument = $this->spase_res->getElementsByTagName('InstrumentID');
			
			if ($instrument->length > 0) {
62372d94   Elena.Budnik   RemoveVi
621
622
623
624
625
				
				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...
626
				return $this->getIdFromSpase($instrument->item(0)->nodeValue);
155b6b3c   Elena.Budnik   other approach to...
627
628
629
630
631
632
633
634
				//InstrumentType
				//spase://SMWG/Observatory/ACE
			} 
		}	
		
		return null;
	}
	
8c0d5a9f   Elena.Budnik   correct location ...
635
636
	protected function getObservatorySpase()
	{
62372d94   Elena.Budnik   RemoveVi
637
638
639
640
641
642
643
644
645
646
647
		if ($this->insXML){
			$observatory = $this->insXML->getElementsByTagName('ObservatoryID');
			
			if ($observatory->length > 0) {
				return $this->getIdFromSpase($observatory->item(0)->nodeValue); 			
			} 
		}	
		
		return null;
	}
	
8c0d5a9f   Elena.Budnik   correct location ...
648
649
650
651
652
653
	protected function setDataCenterAttributes(){	
		$this->dataCenter->setAttribute('xml:id', $this->baseID);
	//	$this->dataCenter->setIdAttribute('xml:id', true);
		$this->dataCenter->setAttribute('name', $this->baseID);		
	}
	
41792000   Elena.Budnik   new REMOTEDATA
654
	protected function makeArgumentsList(){}
05fbcab0   Elena.Budnik   DDServices reorga...
655

41792000   Elena.Budnik   new REMOTEDATA
656
657
} 
?>