Skip to content

Commit

Permalink
Add BioCro advanced edit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Shirk committed Dec 17, 2012
1 parent 91b5302 commit 4ab9ad5
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 405 deletions.
2 changes: 1 addition & 1 deletion scripts/autobuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TO="[email protected],[email protected],[email protected],mdietze@illin
LOCAL="yes"

# run check before install
CHECK="yes"
CHECK="no"

# location where to install packages
if [ $UID -eq 0 ]; then
Expand Down
12 changes: 11 additions & 1 deletion web/running_stage1.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}
$workflow = mysql_fetch_assoc($result);
$folder = $workflow['folder'];
$model_type = $workflow['model_type'];

// check result
$status=file($folder . DIRECTORY_SEPARATOR . "STATUS");
Expand All @@ -41,12 +42,20 @@

// check the global status
switch(checkStatus("CONFIG")) {
// No ERROR, and no endtime yet
case 0:
$nextenabled="disabled=\"disabled\"";header( "refresh:5" );
break;
// CONFIG is complete
case 1:
$nextenabled="disabled=\"disabled\"";
if ($workflow['advanced_edit']) {
if ($workflow['advanced_edit'] && $model_type == 'BIOCRO') {
if ($offline) {
header( "Location: sugarcane/index.php?workflowid=$workflowid&offline=offline");
} else {
header( "Location: sugarcane/index.php?workflowid=$workflowid");
}
} elseif ($workflow['advanced_edit'] ) {
if ($offline) {
header( "Location: advanced_edit.php?workflowid=$workflowid&offline=offline");
} else {
Expand All @@ -62,6 +71,7 @@
}
}
break;
// ERROR occurred
case 2:
$nextenabled="";
if ($offline) {
Expand Down
1 change: 1 addition & 0 deletions web/sugarcane/inc/model/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function get_default($path){
if(preg_match_all('#<([^<>/]*)>[ \n\r]*([^< >\n\r]+)[ \n\r]*</\1>#', $default_xml_raw, $arr, PREG_PATTERN_ORDER)){
foreach($arr[1] as $index=>$tag){
$value=trim($arr[2][$index]);
#print($value . "<br />");
if(preg_match('#<([^</!?\">]+)>[ \r\n]*(?:<([^<>]+)>.*</\2>[ \r\n]*)*[ \r\n]*<'.$tag.'>[ \r\n]*'.$value.'#', $default_xml_raw, $arr2)){
$parent_tag=trim($arr2[1]);
$var_name=$parent_tag.'_'.$tag;
Expand Down
68 changes: 14 additions & 54 deletions web/sugarcane/inc/view/main_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,45 @@
<script type="text/javascript" src="static/bootstrap-tab.js"></script>
<script type="text/javascript" src="static/bootstrap-button.js"></script>
<script type="text/javascript" src="static/script.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBUevuOVPkSqy35NkSqYIQ6UeqsrzYqS54&sensor=true"> </script>
</head>
<body>
<div id="zcontainer">
<div class="contain-left">
<h3 style="padding-bottom:10px;">Set BioCro Parameters</h3>
<form name="input" id="form" action="#" method="post">
<div class="tabbable tabs-left">

<ul class="nav nav-tabs fixed">
<? foreach ($items as $key=>$item) { ?>
<li<? if($key==0) echo ' class="active"';?>><a href="#<?=$item[0]?>"><?=$item[0]?></a></li>
<? foreach ($tabs as $key=>$tab) { ?>
<li<? if($key==0) echo ' class="active"';?>><a href="#<?=$tab[0]?>"><?=$tab[0]?></a></li>
<? } ?>
<li><a href="#graph_settings" style="margin-top:20px;">Settings</a></li>
<li><a href="#graph">Graph</a></li>
<li><a href="#maps-tab" id="maps-tab-button" style="margin-top:20px;">Map</a></li>
<div class="buttons">
<button class="btn" type="submit" id="default">Load Default</button><br />
<button class="btn" type="submit" id="clear">Clear</button><br />
<button class="btn" type="submit" id="create_xml">Generate XML</button><br />
<button class="btn" type="submit" id="run_btn">Run</button><br />
<button class="btn" type="submit" id="graph_settings_btn">Graph Settings</button><br />
<button class="btn" type="submit" id="plot_btn" disabled="diabaled">Plot</button><br />
<div id="feedback"></div>
<input type="hidden" name="command" value="continue"/>
<button class="btn" type="submit" id="create_xml">Continue</button><br />
</div>
</ul>

<div class="tab-content">
<? foreach ($items as $key=>$item) { ?>
<div class="tab-pane<? if($key==0) echo " active";?>" id="<?=$item[0]?>">
<? foreach ($tabs as $key=>$tab) { ?>
<div class="tab-pane<? if($key==0) echo " active";?>" id="<?=$tab[0]?>">
<div class="form-horizontal">
<fieldset>
<legend><?=$item[0]?></legend>
<? foreach ($item[1] as $var) {
$var_name=$item[0].'_'.$var; ?>
<legend><?=$tab[0]?></legend>
<? foreach ($tab[1] as $param) {
$param_name=$tab[0].'_'.$param->nodeName; ?>
<div class="control-group">
<label class="control-label" for="<?=$var_name?>"><?=$var?></label>
<label class="control-label" for="<?=$param_name?>"><?=$param->nodeName?></label>
<div class="controls">
<input type="text" name="<?=$var_name?>" />
<input type="text" name="<?=$param->getNodePath()?>" value="<?=$param->nodeValue?>"/>
</div>
</div>
<? } ?>
</fieldset>
</div>
</div>
<? } ?>

<div class="tab-pane" id="graph_settings">
<div class="plot_settings">
<h2>Graph Setings</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Variables</th>
<th>x-axis</th>
<th>y-axis</th>
</tr>
</thead>
<tbody>
<tr>

<? foreach($graph_variables as $gvar){ ?>
<td><?=$gvar;?></td>
<td><input type="radio" name="var_group_x" value="<?=trim($gvar);?>"></td>
<td><input type="radio" name="var_group_y" value="<?=trim($gvar);?>"></td>
</tr>
<tr>
<? } ?>
</tbody>
</table>
</div>
</div>
<div class="tab-pane" id="graph">
<div class="loading" style="display:none"><img src="static/ajax-loader.gif" /></div>
<div id="graph_box"></div>
</div>
<div class="tab-pane" id="maps-tab">
<div id="map_canvas" class="noselect"></div>
<div id="map_control"><button id="select_area_btn" class="btn" data-toggle="button" >Select Area</button><div id="infobox1"></div></div>
</div>
</div>

</div>
</form>
</div>
Expand Down
155 changes: 91 additions & 64 deletions web/sugarcane/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once("inc/model/functions.php");
#require_once("inc/model/functions.php");
require_once("config/file_locations.php");
require_once("config/graph_variables.php");

Expand All @@ -26,80 +26,107 @@
die('Invalid query: ' . mysql_error());
}
$workflow = mysql_fetch_assoc($result);
$folder = $workflow['folder'];

$runFolder = $folder . DIRECTORY_SEPARATOR . "run";
$runs = explode("\n", file_get_contents($runFolder . DIRECTORY_SEPARATOR . "runs.txt"));
// this is an advanced edit, so only one run is supported. use the last one in the file.
$lastRun = $runs[count($runs)-2];

// datafile
// TODO fix run folder name by looking at $workflow["folder"] . "/run/runs.txt"
$default_xml=array_shift(glob($workflow["folder"] . "/run/*/data.xml"));

// load default values
// TODO check with daeyun about get_default function
get_default($default_xml);

// set the datafile to be saved
$new_xml_file=str_replace("data.xml", "data_modified.xml", $default_xml);
#$dataXml="/home/pecan/pecan/web/sugarcane/default/default.xml";
$dataXml=array_shift(glob($workflow["folder"] . "/run/" . $lastRun . "/data.xml"));

close_database($connection);

// END PEcAn additions

if(isset($_POST["command"]) and strpos($_POST["command"],"create_xml")!==False){
error_reporting(0);
$xml_string=generate_xml($_POST,$xml_structure_file);
$f = fopen($new_xml_file, 'w');
if($f==False){
echo "$new_xml_file is not writable";
exit();
if(isset($_POST["command"]) and strpos($_POST["command"],"continue")!==False){

// prepare the datafile to be saved
$dataOrigXml=str_replace("data.xml", "data_orig.xml", $dataXml);

if (!copy($dataXml, $dataOrigXml)) {
die("Failed to copy parameters to new file, $dataOrigXml");
}
fwrite($f, $xml_string);
chmod("$new_xml_file", 0666);
fclose($f);
echo "successful";
// TODO check if file is modified and mark in database
// diff -b $default_xml $new_xml_file

// TODO save new configuration to run folder
// move $new_xml_file $default_xml

// TODO call R code to lauch stage 2 and redirect to running_stage2.php
// chdir($folder);
// pclose(popen('R_LIBS_USER="' . ${pecan_install} . '" R CMD BATCH workflow_stage2.R &', 'r'));
// if ($offline) {
// header( "Location: ../running_stage2.php?workflowid=$workflowid&offline=offline");
// } else {
// header( "Location: ../running_stage2.php?workflowid=$workflowid");
// }

exit();
}else if(isset($_POST["command"]) and $_POST["command"]=="run"){

$result=shell_exec("sh ".$sh_file);

if(strpos($result, "done")!==False){
echo "successful";
}else{
echo "$sh_file could not be executed. Try changing the persmission of the file to 755.";
$doc = new DOMDocument();
$doc->load($dataXml);
//$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath($doc);

// The name of most of the posted parameters will be an xpath to
// the same parameter in the data.xml file. Iterate through all the
// posted parameters and set the value of the parameter to the posted value.
foreach($_POST as $key=>$value) {
// All xpaths for this document will start with /config
if(strpos($key,"/config") !== false) {
$query = "/" . $key;
$nodeList = $xpath->query($query);
// The above query will only ever return 1 node
$node = $nodeList->item(0);
$node->nodeValue = $value;
}
}
exit();
}else if(isset($_GET["command"]) and $_GET["command"]=="default"){
echo get_default($default_xml);
exit();
}else if(isset($_POST["command"]) and $_POST["command"]=="plot"){
echo shell_exec("./python/png/delete_old.sh > /dev/null 2>/dev/null &");
$python_file="./python/plot.py";

$var1=strict_sanitize($_POST["var_group_x"]);
$var2=strict_sanitize($_POST["var_group_y"]);
$python_args=$var1." ".$var2;

$result=exec("python ".$python_file.' '.$python_args);
if(strpos($result, ".png")!==False){
echo trim($result);
}else{
echo "$python_file could not be executed. Try changing the persmission of the file to 755.";

if(!$doc->save($dataXml,LIBXML_NOEMPTYTAG)) {
die("$dataXml could not be saved");
}

$dataDiff=str_replace("data.xml", "data.diff", $dataXml);
exec("diff $dataOrigXml $dataXml > $dataDiff");
// TODO do something more intelligent with the diff, like save in the database

// call R code to lauch stage 2 and redirect to running_stage2.php
chdir($folder);
pclose(popen('R_LIBS_USER="' . ${pecan_install} . '" R CMD BATCH workflow_stage2.R &', 'r'));
if ($offline) {
header( "Location: ../running_stage2.php?workflowid=$workflowid&offline=offline");
} else {
header( "Location: ../running_stage2.php?workflowid=$workflowid");
}

}

$doc = new DOMDocument();
$doc->load($dataXml);
$rootNode=$doc->documentElement;

$tabs = array();

foreach ($rootNode->childNodes as $tabNode) {
// filter out top level text nodes
if ($tabNode->nodeType != 3) {
if ($tabNode->nodeName != "pft") {
$tabName = $tabNode->nodeName;
$childNodes = $tabNode->childNodes;
$paramNodes=array();
// filter out text nodes from children
foreach ($childNodes as $childNode) {
if ($childNode->nodeType != 3) {
$paramNodes[]=$childNode;
}
}
// add this tab and associated parameters to tabs array
$tabs[]=array($tabName,$paramNodes);
} else { // these are pft parameters, so we have to go down one level in the tree to create the rest of the tabs
foreach ($tabNode->childNodes as $pftTabNode) {
$nodeName = $pftTabNode->nodeName;
if ($pftTabNode->nodeType != 3 && $nodeName != "comment" && $nodeName != "num") {
$tabName = $pftTabNode->nodeName;
$childNodes = $pftTabNode->childNodes;
$paramNodes=array();
// filter out text nodes and comments nodes from children
foreach ($childNodes as $childNode) {
if ($childNode->nodeType != 3 && $childNode->nodeName != "comment") {
$paramNodes[]=$childNode;
}
}
$tabs[] = array($tabName,$paramNodes);
}
}
}
}
exit();
}

$items=read_xml_structure($xml_structure_file);
include_once("inc/view/main_view.php");
?>
Loading

0 comments on commit 4ab9ad5

Please sign in to comment.