Blame view

php/classes/BestRunsMgr.php 7.03 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
<?php
/**
 * @class BestRunsMgr
 * @version $Id: BestRunsMgr.php $
 * 
 */

bf74fc2d   Elena.Budnik   IMPEX
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class BestRunsMgr 
{ 
	protected $runs;
	protected $domRemoteData, $domTemplate;
	protected $xpRemoteData, $xpTemplate;
	protected $runsTemplate;
	protected $json;
	protected $factors = array("SW_Density"=>1e6,
			"SW_Utot"=>1e3,
	//		"SW_Temperature"=>1e3,
			"SW_Temperature"=>1.16e4, // ev => K
			"SW_Btot"=>1e-9,
			"SW_Bx"=>1e-9,
			"SW_By"=>1e-9,
			"SW_Bz"=>1e-9
	);
16035364   Benjamin Renard   First commit
24
 
bf74fc2d   Elena.Budnik   IMPEX
25
	function __construct() {}
16035364   Benjamin Renard   First commit
26

16035364   Benjamin Renard   First commit
27

bf74fc2d   Elena.Budnik   IMPEX
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	public function init() 
	{  
		// DOM of SIMU RUNs template
		$this->domTemplate = new DomDocument("1.0");
		if (!$this->domTemplate->load(DATAPATH.'SimuTemplate/templateGUMICS.xml')) 
				return false;
				
		$this->xpTemplate = new domxpath($this->domTemplate); 
		$this->runTemplate = $this->xpTemplate->query("//runID")->item(0);
		
		// DOM of user RemoteParams
		$this->domRemoteData = new DomDocument("1.0");				 
		
		if (!$this->domRemoteData->load(USERWSDIR.'RemoteParams.xml'))							 			
			return false;
16035364   Benjamin Renard   First commit
43
	
bf74fc2d   Elena.Budnik   IMPEX
44
45
46
47
48
49
		$this->xpRemoteData = new domxpath($this->domRemoteData); 
		
		$this->json = file_get_contents(USERWSDIR.'runs.json');
		
		return true;
	}
16035364   Benjamin Renard   First commit
50

bf74fc2d   Elena.Budnik   IMPEX
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	// $obj ={"Object":"Earth","RunCount":"2",
	// "SW_Density_value":"10","SW_Density_weight":"1","SW_Density_scale":"7","SW_Temperature_value":"100", ....}
	public function getRun($obj)
	{
		$params = $this->makeParams($obj);
		try 
		{ 
			$client = new SoapClient("http://impex-fp7.fmi.fi/ws/Methods_FMI.wsdl",
					array(
					'wsdl_cache' => 0,
					'trace' => 1,
					'exceptions' => 0,
					'soap_version'=>SOAP_1_2
					));
		}
		catch  (SoapFault $exception) 
		{				
			$res =  array('success' => false,'error' => $exception->getMessage());
		}		
		
		try 
		{
			$data_json = $client->getMostRelevantRun($params);
			$result = json_decode($data_json,true);
16035364   Benjamin Renard   First commit
75

bf74fc2d   Elena.Budnik   IMPEX
76
77
78
79
80
81
			if (count($result["runs"]) > 0) 
			{
				$result = $this->changeJson($result);
				$res =  array('success' => true,'runs' => $result["runs"]);
				file_put_contents(USERWSDIR.'runs.json',json_encode($res));		    
			}
16035364   Benjamin Renard   First commit
82
		}
bf74fc2d   Elena.Budnik   IMPEX
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		catch (Exception $e) 
		{
			//error_log ($e->getMessage(),1,email);
			$res =  array('success' => false,'error' => $e->getMessage());
		}	      
		return  $res; 
	}
             
	public function addRuns($obj)
	{      
		if (!$obj) return false;
		else 
		{
			$this->runsId = (array)$obj;
			$object = json_decode($this->json,true);
16035364   Benjamin Renard   First commit
98

bf74fc2d   Elena.Budnik   IMPEX
99
			$simuRegion =  $this->domRemoteData->getElementById('FMI_GUMICS_Earth_Magnetosphere');
16035364   Benjamin Renard   First commit
100
	  
bf74fc2d   Elena.Budnik   IMPEX
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
			foreach ($this->runsId as $runId)
			{
				if ($this->domRemoteData->getElementById($runId) == null)
				{				
					$newRun = $this->domRemoteData->importNode($this->runTemplate, true);
					foreach ($object['runs'] as $r)
					{
						if ($r['ResourceID'] == $runId) $run=$r;
					}
					$newRun->setAttribute('xml:id', $run['ResourceID']);
					$newRun->setAttribute('name', $run['ResourceName']);
					$desc = $this->runTemplate->getAttribute('start_desc'). "<b>Param_values: </b><br/>";
					foreach ($run['Param_values'] as $key => $value) 
					{
						$desc = $desc."<u>".$key." </u>".$value."<br/>";
					}
					$newRun->setAttribute('desc', $desc);
					$newRun->removeAttribute('start_desc'); 
					$datasets = $newRun->getElementsByTagName('dataset');
					
					$numericalOutputs = $run['NumericalOutput']; // array !					
					
					foreach ($datasets as $dataset)
					{
						$name = $dataset->getAttribute('name');
						$find = false;
						
						foreach ($numericalOutputs as $no)
						{						
							if (strpos($no, $name) > 0)
							{ 
							
								$datasetId = $no;
								$find = true;
								break;
							}
						}
						
						if (!$find)							
							throw new Exception($name." is not found in FMI_GUMICS response");
							
					//	$datasetId = str_replace('SimulationRun','NumericalOutput',$run['ResourceID'].'/'.$name);
						$dataset->setAttribute('xml:id', $datasetId); 
						$dataset->setAttribute('desc'); 
						$dataset->setAttribute('globalStart', 'depending on mission');
						$params = $dataset->getElementsByTagName('parameter');
						foreach($params as $param)
						{
							$paramName = $param->getAttribute('name');
				// 		    $datasetId = str_replace('SimulationRun','NumericalOutput',$datasetId);
							switch ($paramName) 
							{
								case "H+ number density" : $paramName = "Density"; break;
								case "H+ velocity" : $paramName = "Ux,Uy,Uz"; break;
								case "H+ total velocity" : $paramName = "Utot"; break;
								case "H+ thermal pressure" : $paramName = "Pressure"; break;
								case "H+ temperature" : $paramName = "Temperature"; break;
							}
16035364   Benjamin Renard   First commit
159
	    
bf74fc2d   Elena.Budnik   IMPEX
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
							$paramId = $this->param2ddSimu($datasetId.'/'.$paramName);
							$param->setAttribute('xml:id', str_replace('_EARTH___n_T_Vx_Bx_By_Bz','',str_replace('earth_synth_stationary_','',$paramId))); 
						//	$param->setAttribute('parentID',$datasetId); 
							$param->setAttribute('needsArgs','1');
							if ($param->hasChildNodes()) 
							{
								$components = $param->getElementsByTagName('component');
								
								foreach ($components as $component)
								{
									$compName = $component->getAttribute('name');
									$component->setAttribute('xml:id', str_replace('_EARTH___n_T_Vx_Bx_By_Bz','',str_replace('earth_synth_stationary_','',$paramId.'_'.$compName))); 
								//	$component->setAttribute('parentID',$datasetId); 
									$component->setAttribute('needsArgs','1');
								}
							}
						}
					}
					$simuRegion->appendChild($newRun);
					$this->domRemoteData->save(USERWSDIR.'RemoteParams.xml');
				}
			}  
		}
16035364   Benjamin Renard   First commit
183
	}
16035364   Benjamin Renard   First commit
184
      
bf74fc2d   Elena.Budnik   IMPEX
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
	protected function makeParams($json)
	{
		$obj = (array)$json;
		$SW_parameters = array();
		
		foreach ($obj as $k => $v) 
		{
			if (empty($v))
				unset($obj[$k]);
		}
		
		foreach ($obj as $k => $v) 
		{
			if (strpos($k,'_value') !== FALSE ) 
			{
				$sw= str_replace ('_value', '', $k);
				$SW_val = array();
				$SW_val['value'] = $obj[$sw.'_value'] * $this->factors[$sw];
				if ($obj[$sw.'_weight'] != '') $SW_val['weight'] = $obj[$sw.'_weight'];
				if ($obj[$sw.'_scale'] != '') $SW_val['scale'] = $obj[$sw.'_scale']* $this->factors[$sw];
				$SW_parameters[$sw] = $SW_val;
			}
16035364   Benjamin Renard   First commit
207
		}
bf74fc2d   Elena.Budnik   IMPEX
208
209
210
211
212
		$params = array (
			'Object'    => 'Earth',	// Mandatory
			'RunCount'  => $obj['RunCount'],	// Number of runs returned. Optional, default = 1
			'SW_parameters' => $SW_parameters);
		
16035364   Benjamin Renard   First commit
213
		return $params;
bf74fc2d   Elena.Budnik   IMPEX
214
	}
16035364   Benjamin Renard   First commit
215
        
bf74fc2d   Elena.Budnik   IMPEX
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
	protected function changeJson($object)
	{         
		$i=0;
		foreach ($object['runs'] as $run)
		{
			$run['S_diff'] = round($run['S_diff'],1);
			
			foreach ($run['S_diff_n'] as $k => $v)
			{
				$v = round($v,3);
				$run['S_diff_n'][$k] = $v;
			}
			
			foreach ($run['Param_values'] as $k => $v)
			{
				if ($this->factors[$k])
				{
					$newValue = round($v / $this->factors[$k], 3);
					$run['Param_values'][$k] = $newValue;
				}
			}
			$object['runs'][$i] = $run;
			$i++;
		}		
		return $object;
	}
16035364   Benjamin Renard   First commit
242
      
bf74fc2d   Elena.Budnik   IMPEX
243
244
245
246
247
248
249
250
	protected function param2ddSimu($paramID) 
	{
		$pairs = array("?" => "_", " " => "_","-" => "_","/" => "_","%" => "_","\\" => "_","$" => "_",":" => "_","+" =>"_",
							"#" => "_","@" => "_","." => "_", ">" => "_", "<" => "_", "," => "_");    
		return strtr($paramID,$pairs); 
	}
	
}
16035364   Benjamin Renard   First commit
251
?>