WSInputOutputParamsPlotClass.php 5.57 KB
<?php
/**
 * @class WSInputOutputParamsPlotClass
 * @brief Implementation of IHMInputOutputParamsAbstractClass to treat plot request
 * @details
*/
class WSInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass
{
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
		$this->paramManager     = new IHMParamManagerClass();
		$this->jobsManager      = new WSJobsManagerClass();
	}	

	/*
	 * @brief Get Task
	 */
	protected function getTask($input)
	{
		return WSInputOutputClass::getService();
	}
	
	/*
	 * @brief method to unmarshall a plot request
	*/
	protected function unmarshallRequest($input)
	{
		//Request
		$postProcessCmd = "";
	 
		$requestNode = $this->paramsData->addRequestNode();
		$outputsNode = $requestNode->getOutputsNode();
		$paramsNode  = $requestNode->getParamsNode();

		//unmarshall time definition
		$this->unmarshallTimeDefinition($input, 0);
	
		$plotOutputNode = $outputsNode->addNewOutput(RequestOutputTypeEnum::PLOT);
		$plotOutputNode->setWriteContextFile("false");
		
		$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE);
			
		//prefix

		$filePrefix = "plot";

		$plotOutputNode->setFilePrefix($filePrefix);
	
		//page
		$pageNode = $plotOutputNode->getPage();
		
		switch ($input->{'file-format'})
		{
			case 'PNG' :
				$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
				$extension = ".png";
				break;
			case 'PDF' :
				$fileFormat = RequestOutputPlotPageFormatEnum::PDF;
				$extension = ".pdf";
				break;
			case 'PS' :
				$fileFormat = RequestOutputPlotPageFormatEnum::PS;
				$extension = ".ps";
				break;
			case 'SVG' :
				$fileFormat = RequestOutputPlotPageFormatEnum::SVG;
				$extension = ".svg";
				break;
			default:
				throw new Exception('File format not implemented.');
		}
	
		$pageNode->setFormat($fileFormat);
		$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::LANDSCAPE);
		$pageNode->setDimension(RequestOutputPlotPageDimensionEnum::ISO_A4);
		$pageNode->setMode(RequestOutputPlotPageModeEnum::COLOR);
		
		// Layout 
	   $pageNode->getLayout()->setType(RequestOutputPlotLayoutTypeEnum::VERTICAL);
		$pageNode->getLayout()->setPanelHeight("0.5");
		$pageNode->getLayout()->setPanelSpacing("0");
		$pageNode->getLayout()->setExpand("false"); // true ?
		$pageNode->getLayout()->setOnlyLowerTimeAxesLegend("true");
		
		$parametersToPlot = $input->{'parameters'}; // one parameter on panel

		$panelN = 1;
		foreach ($parametersToPlot as $param)
		{
			$panelNode = $pageNode->addPanel();
			$panelNode->setId($panelN);
			$panelNode->setIndex($panelN -1);
			$plotNode = $panelNode->addPlotElement(RequestOutputPlotElementTypeEnum::TIMEPLOT);
			
			//Axes
			$timeAxisNode = $plotNode->getTimeAxis();
			$yAxisNode = $plotNode->addYAxis('y-left'); 

			//Params
			$this->unmarshallParams($param, $paramsNode, $plotNode, $panelNode);
			$panelN++;
		}

		$resultFile = $filePrefix."_*".$extension;
		$waitingResultFile = $input->{'result-file'}.$extension;
	
		$this->paramsData->addWaitingResult(WSInputOutputClass::getService(), $waitingResultFile);
		
		//Post process command to apply to the result file
		$postProcessCmd .= "mv ".$resultFile." ".WSConfigClass::getWsResultDir().$waitingResultFile;


		$this->paramsData->setBatchEnable(true);
		$this->paramsData->setPostCmd($postProcessCmd);

		$this->paramsData->setFromWS(true);
		
		return $this->paramsData;
	}

	
	protected function unmarshallParams($paramData, $requestParamsNode, $plotNode, $panelNode)
	{
		//Main drawing element parameter
		$paramInfo = $this->paramManager->addExistingParam($paramData->{'paramid'}, $this->paramsData, NULL);
					
		$requestParamsNode->addParam($paramInfo['id']);
		$paramNode = $plotNode->getParams()->getParamById($paramInfo['id']);
		
		$serieNode = $paramNode->addYSerie('y-left', -1);
		$serieNode->setId('1');
		
		$lineNode = $serieNode->getLine();
		$lineNode->setType(RequestOutputPlotLineTypeEnum::LINE); 
		
		if ($this->isVector($paramInfo['id'])) { // vector - activate legend
			$paramLegendNode = $plotNode->getLegends()->getParamsLegend();
			$paramLegendNode->setType(RequestOutputPlotParamsLegendTypeEnum::TEXTONLY);
			$paramLegendNode->setShowParamInfo("true");
			$paramLegendNode->setShowIntervalInfo("false");
			$paramLegendNode->setPosition("outside");
		}
	}
	
		
	protected function isVector($paramId)
	{
		$paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";
		
		$dom = new DomDocument("1.0");
		if (!@$dom->load($paramPath))
			return false;
		
		// TODO use tensor_order
		$components = $dom->getElementsByTagName("components")->item(0)->nodeValue;
		$arr = explode(",",$components);
		
		return (count($arr) == 3);
	}
	
	/*
	* @brief Add a job to the job manager
	*/
	protected function addToJobsFile($data,$resultKey)
	{
		$waitingResult = $data->getWaitingResult($resultKey);
		
		return $this->jobsManager->addJob(
				$this->input,
				$data->getId(),
				$this->getWorkingDirName(),
				$data->getStatus() == ProcessStatusEnumClass::RUNNING,
				$data->getStart(),
				$waitingResult,
				$data->getErrorCode(),
				$data->getExecTime()); 
	}
	
	/*
	 * @brief method to marshall the result of a download request
	*/
	protected function marshallResult($data)
	{
		if (!$data->getSuccess())
			return array(
					'success' => false,
					'message' => $data->getLastErrorMessage());

		switch ($data->getStatus())
		{
			case ProcessStatusEnumClass::ERROR :
			case ProcessStatusEnumClass::RUNNING :
			case ProcessStatusEnumClass::DONE :
				return $this->addToJobsFile($data, WSInputOutputClass::getService());
			default :
				return array(
				'success' => false,
				'message'   => 'Unknown Process Status');
		}
	}
}
?>