Blame view

src/REMOTEDATA/THEMIS.php 6.76 KB
7d7445ea   Elena.Budnik   THEMIS remote center
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * @class THEMIS 
 * @brief THEMIS
 * @details
 */
class THEMIS extends RemoteDataCenterClass
{
	// /THEMIS_B1/themisdata
   
36b01ac2   Elena.Budnik   set utc time zone...
11
	protected $baseMgr;
7d7445ea   Elena.Budnik   THEMIS remote center
12
	protected $domTemplate;
36b01ac2   Elena.Budnik   set utc time zone...
13
	protected $xmlTemplate; 
7d7445ea   Elena.Budnik   THEMIS remote center
14
15
16
17
18
	
	public function init() 	
	{			
		error_log("THEMIS Proxy creation on ".date("Y-m-d\TH:i:s").PHP_EOL,3,log);
		error_log("THEMIS Proxy creation on ".date("Y-m-d\TH:i:s").PHP_EOL,3,err);
36b01ac2   Elena.Budnik   set utc time zone...
19
		$this->xmlTemplate = strval(RemoteData).ThemisConfigClass::$TemplateXml;
7d7445ea   Elena.Budnik   THEMIS remote center
20
21
22
23
	} 
	
	protected function getRemoteTree()	
	{
36b01ac2   Elena.Budnik   set utc time zone...
24
		if (!file_exists($this->xmlTemplate)) {
7d7445ea   Elena.Budnik   THEMIS remote center
25
26
27
28
29
			error_log('Cannot find THEMIS tree template'.PHP_EOL,3,err); 
			exit('Cannot find THEMIS tree template');
		}
		
		$this->domTemplate = new DomDocument("1.0");
36b01ac2   Elena.Budnik   set utc time zone...
30
		if (!$this->domTemplate->load($this->xmlTemplate)) {
7d7445ea   Elena.Budnik   THEMIS remote center
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
			error_log('Cannot load THEMIS tree template'.PHP_EOL,3,err); 
			exit('Cannot load THEMIS tree template');
		}
	}
	
	/* 
	*   create base.xml from themis template xml
	*/
	protected function createMissionNodes()
	{
		$missionNodes = array();
			
		$this->baseMgr = new DDBaseMgr();
		foreach (ThemisConfigClass::$themis as $suffix)
		{
			$mission = $this->domAmda->importNode($this->domTemplate->getElementsByTagName("mission")->item(0), true); 
			$template = $mission->getAttribute("name");
			$mission->setAttribute('name', str_replace("XXX", $suffix, $template));
			$mission->setAttribute('desc', str_replace("XXX", $suffix, $template));
			$template = $mission->getAttribute("xml:id");
			$mission->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
			$instruments = $mission->getElementsByTagName("instrument");
			foreach ($instruments as $instrument) {
				$template  = $instrument->getAttribute("xml:id");
				$instrument->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
				$datasets = $instrument->getElementsByTagName("dataset");
				foreach ($datasets as $dataset) {
					$template  = $dataset->getAttribute("xml:id");
					$dataset->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
					$template = $dataset->getAttribute("rem_id");
					$dataset->setAttribute('rem_id', str_replace("xxx", strtolower($suffix), $template));
					$this->updateGlobalStartStop($dataset);
					$this->createVi($dataset);
					
					$ncFiles = $this->getData($remSetID, $start, $stop);
					$params = $dataset->getElementsByTagName("parameter");
					foreach ($params as $param) {
						$template = $param->getAttribute("xml:id");
						$param->setAttribute('xml:id', str_replace("xxx", strtolower($suffix), $template));
					}
				}
			}
			$missionNodes[] = $mission;
		}
		return $missionNodes;
	}
	
	protected function updateGlobalStartStop($dataset)
	{
		$rem_id = $dataset->getAttribute('rem_id');
		$ViId = strtr(substr($dataset->getAttribute('xml:id'),7),":","_");

		$dataset->setAttribute('dataStart',$this->getGlobalStart($rem_id));
		$dataset->setAttribute('dataStop',$this->getGlobalStop($rem_id));
	}
   
	protected function getGlobalStart($id)
	{
		$startTime = "1970-01-01T00:00:00.000Z";
		$datasetDir = ThemisConfigClass::$NFS_DIR."/".strtr($id,"_" , "/" )."/".ThemisConfigClass::$START_YEAR;
		if (is_dir($datasetDir)) {
			$fileNames = glob($datasetDir."/".$id."*.".ThemisConfigClass::$format);
			//  Get Start  Time  from the First File Name 
			$pos = strpos($fileNames[0],$id) + strlen($id) + 1;
			$firstTime =  substr($fileNames[0], $pos, 8); 
			$startTime = substr($firstTime,0,4)."-".substr($firstTime,4,2)."-".substr($firstTime,6,2)."T00:00:00.000Z";
		}
	  
		return $startTime ;   
	}
	
	protected function getGlobalStop($id)
	{
		$stopTime = "1970-01-01T00:00:00.000Z";
		$datasetDir = ThemisConfigClass::$NFS_DIR."/".strtr($id,"_" , "/" )."/".date('Y');
		if (is_dir($datasetDir)) {
			$fileNames = glob($datasetDir."/".$id."*.".ThemisConfigClass::$format);
			//  Get Stop  Time  from the Last File Name
			$pos = strpos($fileNames[count($fileNames)-1],$id) + strlen($id) + 1;
			$lastTime =  substr($fileNames[count($fileNames)-1], $pos, 8);
			$stopTime = substr($lastTime,0,4)."-".substr($lastTime,4,2)."-".substr($lastTime,6,2)."T23:59:59.000Z";
		}
	  
		return $stopTime ;   
	}
  
	protected function createVi($dataset)
	{
		$ViId = strtr(substr($dataset->getAttribute('xml:id'),7),":","_");
		$globalStart = $dataset->getAttribute('dataStart');
		$globalStop = $dataset->getAttribute('dataStop');
		echo $globalStart." ".$globalStop.PHP_EOL;
		if ($this->baseMgr->viExists($ViId, $this->baseID)) {	
		   // error_log
		   // updateStartStop
		 
			return;
		}
		$info = explode("_", $ViId);

		$this->baseMgr->setViId($ViId);
		$rem_id = $dataset->getAttribute('rem_id').":".$info[2];
		$this->baseMgr->setRemoteViId($rem_id);
		$this->baseMgr->setViInfo($info[2]);
		$this->baseMgr->setViParentsInfo($this->baseID, $info[0], $info[1]);
 
		$this->baseMgr->setViLocation($this->baseID."/".$info[0], $info[1]."/".$info[2]."/");
		$min_sampling = $dataset->getAttribute('minSampling');
		$max_sampling = $dataset->hasAttribute('maxSampling') ? $dataset->getAttribute('maxSampling') : 0 ; 
		$this->baseMgr->setViSampling($min_sampling, $max_sampling);
		$this->baseMgr->globalStart = $globalStart;
		$this->baseMgr->globalStop = $globalStop;
		
		$this->baseMgr->createVi();
		
		$ncFiles = array();
		$start = $globalStart;

		while (count($ncFiles) == 0) {
			// get the very first data file
			$stop = $start;
			$ncFiles = $this->getData($rem_id, $start, $stop);
			
			$newStart = strtotime($start);
			$newStart += 24*60*60;
			$start = date("Y-m-d\TH:i:s",$newStart);
		}
		
		$this->baseMgr->addRemoteData($rem_id, $ncFiles, $globalStart, $globalStart, false);
		
		$this->baseMgr->updateRemoteStart();
	}
      
	protected function setDataCenterAttributes(){	
		$this->dataCenter->setAttribute('xml:id', $this->baseID);
		$this->dataCenter->setAttribute('name', $this->baseID."_iRAP");		
	}
	
	protected function makeArgumentsList(){}
	
	public function getData($remId, $start, $stop)
	{
		$info = explode(":", $remId);
		$rem_id = $info[0];
		$mode = $info[1];
		
		$info_1 =  explode("_", $rem_id);
		$mission = $info_1[0];
		
		$sourceDir = ThemisConfigClass::$NFS_DIR."/".strtr($rem_id,"_" , "/" );
		// Which files 
		$currTime = strtotime($start);
		$stopTime = strtotime($stop);

// 		$currTime -= 3600.0;
		
		while ($currTime <= $stopTime ) {
			$fileName = $sourceDir."/".date('Y', $currTime)."/".$rem_id."_".date('Ymd', $currTime)."_v01.cdf";
			if (file_exists($fileName)) {             
					system(ThemisConfigClass::$program[$mode]." ".$fileName." ".$mission." ".$mode); 
			}
			else {
					// fprintf($STDERR,"NO SUCH FILE  $fileName \n");
			}
			$currTime += 24*60*60;
		}  
		
		$files = glob($mode."*.nc");
	 
		return $files;
	}
	
	public function monitor()
	{
		// check if NFS connection ok
		if (glob(ThemisConfigClass::$NFS_DIR . "/*"))
					return true;
			 
		return false;
	}
} 
?>