IHMInputOutputParamsPlotClass.php
12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
213
214
215
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
define ("PLOT_RESULT_FILE_KEY","plot");
/**
* @class IHMInputOutputParamsPlotClass
* @brief Implementation of IHMInputOutputParamsAbstractClass to treat plot request
* @details
*/
class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass
{
private $currentTabId;
private $isInterractive;
private $startTime;
private $stopTime;
/*
* @brief method to unmarshall a plot request
*/
protected function unmarshallRequest($input)
{
if (isset($input->action))
{
//interactive action
$realInput = $input;
$input = $this->loadIHMRequest();
if (!isset($input))
throw new Exception('Cannot get original request data for interactive action.');
//modify request
date_default_timezone_set('UTC');
$intervalMove = 0.;
switch ($realInput->action->name)
{
case 'next' :
$intervalMove = strtotime($input->stopDate) - strtotime($input->startDate);
break;
case 'halfnext' :
$intervalMove = (strtotime($input->stopDate) - strtotime($input->startDate))/2;
break;
case 'back' :
$intervalMove = strtotime($input->startDate) - strtotime($input->stopDate);
break;
case 'halfback' :
$intervalMove = (strtotime($input->startDate) - strtotime($input->stopDate))/2;
break;
}
//apply move
$input->startDate = date("Y-m-d\TH:i:s",strtotime($input->startDate)+$intervalMove);
$input->stopDate = date("Y-m-d\TH:i:s",strtotime($input->stopDate)+$intervalMove);
}
//remove old result files
foreach (glob($this->getWorkingPath()."plot_*.png") as $oldfile)
{
unlink($oldfile);
}
//Full plot request
$this->startTime = $input->startDate;
$this->stopTime = $input->stopDate;
//save request
$this->saveIHMRequest($input);
//TimePlot
//{"id":"","name":"","tabId":"1","outputName":"","orientation":"LANDSCAPE","format":"PNG","title":"",
//"description":"","charSize":1.3,"thickness":1,"ppp":3000,"forcedLayout":false,"forcedMulti":false,
//"children":[{"name":"Panel 1","leaf":false,"text":"","width":1,"height":0.4,"xTitle":"",
//"xRangeMin":0,"xRangeMax":0,"y1Title":"","y1RangeMin":0,"y1RangeMax":0,"y2Title":"",
//"y2RangeMin":0,"y2RangeMax":0,"plotType":"TIME",
//"children":[{"name":"imf","leaf":true,"text":"imf","paramArgs":"select...","isScatter":false,
//"needsArgs":false,"plotOnly":false,"yaxe":false}]}],
//"timesrc":"Interval","startDate":"2008-03-17T14:07:43","stopDate":"2008-03-17T15:26:18"
//"durationDay":"0000","durationHour":"01","durationMin":"18","durationSec":"35","leaf":true,"nodeType":"request"}
//XYPlot
//{"id":"","name":"","tabId":"1","outputName":"","orientation":"LANDSCAPE","format":"PNG","title":"",
//"description":"","charSize":1.3,"thickness":1,"ppp":3000,"forcedLayout":false,"forcedMulti":false,
//"children":[{"name":"Panel 1","leaf":false,"text":"","width":0.4,"height":0.4,"xTitle":"",
//"xRangeMin":0,"xRangeMax":0,"y1Title":"","y1RangeMin":0,"y1RangeMax":0,"y2Title":"",
//"y2RangeMin":0,"y2RangeMax":0,"plotType":"SCATTER",
//"scatterParam":{"phantom":true,"internalId":"ext-record-71","raw":{"name":"imf(1)"},
//"data":{"id":"","name":"imf(1)"},"modified":{},"hasListeners":{},"events":{},"stores":[],
//"dirty":false,"id":"amdaModel.AmdaObject-ext-record-71"},
//"children":[{"name":"#xmfi","leaf":true,"text":"#xmfi","paramArgs":"select...","isScatter":true,
//"needsArgs":false,"plotOnly":false,"yaxe":false}]}],
//"timesrc":"Interval","startDate":"2008-03-17T14:07:43","stopDate":"2008-03-17T15:26:18",
//"durationDay":"0000","durationHour":"01","durationMin":"18","durationSec":"35","leaf":true,"nodeType":"request"}
$this->currentTabId = $input->tabId;
$paramsNode = $this->paramsData->getRequestNode()->getParamsNode();
//unmarshall time definition
$this->unmarshallTimeDefinition($input);
//unmarshall plot output definition
$outputsNode = $this->paramsData->getRequestNode()->getOutputsNode();
$plotOutputNode = $outputsNode->addNewOutput(RequestOutputTypeEnum::PLOT);
$pageNode = $plotOutputNode->getPage();
//title
$pageNode->setTitle($input->description);
//orientation = [['landscape','LANDSCAPE'], ['portrait', 'PORTRAIT']];
$forceRotation = false;
switch (strtolower($input->orientation))
{
case 'landscape' :
$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::LANDSCAPE);
break;
case 'portrait' :
$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::PORTRAIT);
$forceRotation = true;
break;
default :
$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::LANDSCAPE);
break;
}
//format = [['PNG','PNG'], ['PDF', 'PDF'], ['PS', 'PostScript']];
switch ($input->format)
{
case 'PNG' :
$pageNode->setFormat(RequestOutputPlotPageFormatEnum::PNG);
//interactive plot => disable batch mode
$this->paramsData->setBatchEnable(false);
$this->isInterractive = true;
break;
case 'PDF' :
$pageNode->setFormat(RequestOutputPlotPageFormatEnum::PDF);
$this->isInterractive = false;
break;
case 'PS' :
case 'PostScript' :
$pageNode->setFormat(RequestOutputPlotPageFormatEnum::PS);
$this->isInterractive = false;
break;
}
//auto layout
if (count($input->children) > 1)
{
if ($input->forcedLayout)
$pageNode->getLayout()->setType(RequestOutputPlotLayoutTypeEnum::AUTO);
else
$pageNode->getLayout()->setType(RequestOutputPlotLayoutTypeEnum::VERTICAL);
}
$pageNode->getLayout()->setExpand("true");
//panels
foreach ($input->children as $panel)
{
$panelNode = $pageNode->addPanel();
$panelNode->setTitle($panel->name);
//add plot element
switch ($panel->plotType)
{
case "TIME" :
//time plot definition
$plotElementNode = $panelNode->addPlotElement(RequestOutputPlotElementTypeEnum::TIMEPLOT);
//time axis definition
$timeAxisNode = $plotElementNode->getTimeAxis();
$isXYPlot = false;
break;
case "SCATTER" :
//XY plot definition
$plotElementNode = $panelNode->addPlotElement(RequestOutputPlotElementTypeEnum::XYPLOT);
//x axis definition
$xAxisNode = $plotElementNode->getXAxis();
$xAxisNode->getLegend()->setText($panel->xTitle);
if ((($panel->xRangeMin != 0) || ($panel->xRangeMax != 0)) && ($panel->xRangeMin < $panel->xRangeMax))
$xAxisNode->getRange()->setMinMax($panel->xRangeMin, $panel->xRangeMax);
$isXYPlot = true;
break;
}
if (!isset($plotElementNode))
//unknown plot type
continue;
//y1 axis definition
$y1AxisNode = $plotElementNode->addYAxis("y1");
$y1AxisNode->getLegend()->setText($panel->y1Title);
if ((($panel->y1RangeMin != 0) || ($panel->y1RangeMax != 0)) && ($panel->y1RangeMin < $panel->y1RangeMax))
$y1AxisNode->getRange()->setMinMax($panel->y1RangeMin, $panel->y1RangeMax);
//y2 axis definition - it will drawn only if at least one param is associated to this axis
$y2AxisNode = $plotElementNode->addYAxis("y2");
$y2AxisNode->getLegend()->setText($panel->y2Title);
if ((($panel->y2RangeMin != 0) || ($panel->y2RangeMax != 0)) && ($panel->y2RangeMin < $panel->y2RangeMax))
$y2AxisNode->getRange()->setMinMax($panel->y2RangeMin, $panel->y2RangeMax);
//add xserie if XYPlot
if ($panel->plotType == "SCATTER")
{
//"scatterParam":{"phantom":true,"internalId":"ext-record-71","raw":{"name":"imf(1)"},
//"data":{"id":"","name":"imf(1)"},"modified":{},"hasListeners":{},"events":{},"stores":[],
//"dirty":false,"id":"amdaModel.AmdaObject-ext-record-71"}
$paramInfo = $this->paramManager->addExistingParam($panel->scatterParam->data->name,$this->paramsData);
$paramsNode->addParam($paramInfo['id']);
$paramNode = $plotElementNode->getParams()->getParamById($paramInfo['id']);
if (count($paramInfo['indexes']) == 0)
$paramNode->addXSerie($xAxisNode->getId(), -1);
else
{
$paramNode->addXSerie($xAxisNode->getId(), $paramInfo['indexes'][0]);
}
}
//add yseries
foreach ($panel->children as $yserie)
{
//{"name":"imf","leaf":true,"text":"imf","paramArgs":"select...","isScatter":false,
//"needsArgs":false,"plotOnly":false,"yaxe":false}
//{"name":"#xmfi","leaf":true,"text":"#xmfi","paramArgs":"select...","isScatter":true,
//"needsArgs":false,"plotOnly":false,"yaxe":false}
$xAxis = "";
if ($isXYPlot)
$xAxis = $xAxisNode->getId();
$yAxis = $y1AxisNode->getId();
if ($yserie->yaxe)
$yAxis = $y2AxisNode->getId();
$paramInfo = $this->paramManager->addExistingParam($yserie->name,$this->paramsData);
$paramsNode->addParam($paramInfo['id']);
$paramNode = $plotElementNode->getParams()->getParamById($paramInfo['id']);
if (count($paramInfo['indexes']) == 0)
{
$isSpectro = ($paramInfo['plotType'] == 'Spectra');
if ($isSpectro && $panel->plotType == "TIME")
$paramNode->addSpectro($yAxis);
else
$paramNode->addYSerie($yAxis, -1,$xAxis);
}
else
{
foreach ($paramInfo['indexes'] as $index)
$paramNode->addYSerie($yAxis, $index,$xAxis);
}
}
}
//determine extension and add post processing if needed
$extension = "";
switch (strtolower($pageNode->getFormat()))
{
case RequestOutputPlotPageFormatEnum::PNG :
$extension = ".png";
break;
case RequestOutputPlotPageFormatEnum::PS :
$extension = ".ps";
break;
case RequestOutputPlotPageFormatEnum::PDF :
$extension = ".pdf";
break;
}
$compression = "";
if ($pageNode->getFormat() != RequestOutputPlotPageFormatEnum::PNG)
{
//one file per interval
$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE_PER_INTERVAL);
//auto compression if not PNG format
$compression = ".tar.gz";
$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::TAR);
$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::GZIP);
}
//Set post command to retrieve request result file
//Use a cache killer (ie - random string in file name)
$resultFile = "plot_".CommonClass::generateRandomString(6);
$this->paramsData->addWaitingResult(PLOT_RESULT_FILE_KEY, $resultFile);
if ($pageNode->getFormat() != RequestOutputPlotPageFormatEnum::PNG)
{
$postProcessCmd = "mv plot_*".$compression;
$postProcessCmd .= " ".$resultFile.$extension.$compression;
}
else
{
$postProcessCmd = "mv plot_*";
$postProcessCmd .= $extension;
$postProcessCmd .= " ".$resultFile.$extension;
if ($forceRotation)
$postProcessCmd .= " | convert ".$resultFile.$extension." -rotate -90 ".$resultFile.$extension;
}
$this->paramsData->setPostCmd($postProcessCmd);
return $this->paramsData;
}
/*
* @brief method to marshall the result of a download request
*/
protected function marshallResult($data)
{
if (!$this->isInterractive)
{
//add to job
$commonRes = $this->commonMarshallResult($data,PLOT_RESULT_FILE_KEY);
return $commonRes;
}
if (!$data->getSuccess())
return array(
'success' => false,
'message' => $data->getLastErrorMessage());
switch ($data->getStatus())
{
case ProcessStatusEnumClass::DONE :
return array(
'success' => true,
'id' => $data->getId(),
'folder' => $this->getWorkingDirName(),
'result' => $data->getWaitingResult(PLOT_RESULT_FILE_KEY),
'format' => 'PNG',
'compression' => 'unknown',
'status' => 'done',
'children' => array(
array(
"xmin" => "0",
"xmax" => "1",
"ymin" => "0",
"umax" => "1"
)
),
'tabId' => $this->currentTabId,
'startDate' => $this->startTime,
'stopDate' => $this->stopTime
);
default :
return array(
'success' => false,
'message' => 'Error to process the interractive request');
}
}
private function saveIHMRequest($input)
{
$path = $this->getWorkingPath()."ihm.request";
if (!is_dir($this->getWorkingPath()))
mkdir($this->getWorkingPath(),0777);
$file = fopen($path, 'w');
fwrite($file, json_encode($input));
fclose($file);
}
private function loadIHMRequest()
{
$path = $this->getWorkingPath()."ihm.request";
if (!file_exists($path))
return NULL;
return json_decode(file_get_contents($path));
}
}
?>