V1_0.java [src/java/m/cfa/drought] Revision: 584e7fe964a63c82a57c1c2693e9bb6fadc00272  Date: Thu Feb 04 15:37:35 MST 2016
package m.cfa.drought;

import csip.ModelDataService;
import java.io.File;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import oms3.annotations.VersionInfo;
import org.apache.commons.io.FileUtils;

@Name("drought")
@Description("drought")
@VersionInfo("1.0")
@Path("m/cfa/drought/1.0")
public class V1_0 extends ModelDataService {

    guiDrought_Model model = new guiDrought_Model();

    @Override
    protected String process() throws Exception {
        model.setMainFolder(getWorkspaceDir().toString());
        model.setDatabase(getStringParam("database"));
        model.setOrganizationName(getStringParam("org"));
        model.setStationID(getStringParam("station_id"));
        model.setStationName(getStringParam("station_name"));
        model.setBeginDate(getStringParam("begin_date"));
        model.setEndDate(getStringParam("end_date"));
        model.setLambdaString(getStringParam("lambda_string"));
        model.setAction(getStringParam("action"));
        model.setPhiValues(getStringParam("phi_values"));
        model.setThetaValues(getStringParam("theta_values"));
        model.setDroughtLimit(getDoubleParam("drought_limit"));
        model.setUseSTORETretrieval(getBooleanParam("useSTORETretrieval"));
        model.setSTORETdata(getStringParam("STORETdata"));
        model.setMergeDatasets(getBooleanParam("merge_datasets"));
        model.setMergeMethod(getStringParam("merge_method"));
        model.setUserData(getStringParam("user_data"));

        model.run();
        return EXEC_OK;
    }

    @Override
    protected void postProcess() throws Exception {
        //This result array is more complex because drought analysis can pass back 1, 2, or 5 graphs
        // files
        if(model.getAction().equalsIgnoreCase("all")){
            putResult(model.getResult(),
                      new File(model.getTimeseriesGraph()),
                      new File(model.getColumnChart()),
                      new File(model.getFittedDataGraph()),
                      new File(model.getProjectedDataGraph()),
                      new File(model.getDroughtRecurrenceGraph()),
                      //Below are the result graph files for use by JSHighCharts on eRAMS for duplicating the above graphs
                      model.getTimeseriesOutput(),
                      model.getColumnChartOutput(),
                      model.getFittedGraphOutput(),
                      model.getProjectedGraphOutput(),
                      model.getRecurrenceGraphOutput());

        }else if(model.getAction().equalsIgnoreCase("optimizeModel")){
            if(model.getThetaValues().equalsIgnoreCase("")){
                //The drought model used is AR(p) so only 1 graph
                putResult(new File(model.getOptimizedModel_ARgraph()));
            }else{
                //The drought model used is ARMA(p,q) so only 2 graphs
                putResult(new File(model.getOptimizedModel_ARMAgraphs()[0]), 
                          new File(model.getOptimizedModel_ARMAgraphs()[1]));
            }
        }else{
            putResult(new File(model.getFittedDataGraph()));
        }
        
        // values
        if (model.getAction().equalsIgnoreCase("all")) {
            putResult("len", model.getLen());
            putResult("start", model.getStart());
            putResult("end", model.getEnd());
            putResult("data_source", model.getDataSource());
            putResult("drought_limit", model.getDroughtLimit(), null, "acre-ft");
            //Get other results
            putResult("drought_summary",model.getResult().getName());
            putResult("graph1", model.getTimeseriesGraph());
            putResult("graph2", model.getColumnChart());
            putResult("graph3", model.getFittedDataGraph());
            putResult("graph4", model.getProjectedDataGraph());
            putResult("graph5", model.getDroughtRecurrenceGraph());
            
            //Get result files for JSHighCharts
            String graphDataFiles = model.getTimeseriesOutput().getName() + "|" + 
                                    model.getColumnChartOutput().getName() + "|" +
                                    model.getFittedGraphOutput().getName() + "|" +
                                    model.getProjectedGraphOutput().getName() + "|" +
                                    model.getRecurrenceGraphOutput().getName();
            putResult("graph_data_files", graphDataFiles);
            
        } else if (model.getAction().equalsIgnoreCase("optimizeModel")) {
            if (model.getThetaValues().equalsIgnoreCase("")) {
                //The drought model is AR(p) so only 1 graph
                putResult("len", model.getLen());
                putResult("start", model.getStart());
                putResult("end", model.getEnd());
                putResult("data_source", model.getDataSource());
                putResult("drought_summary", FileUtils.readFileToString(model.getResult()));
                putResult("graph1", model.getOptimizedModel_ARgraph());
            } else {
                //The drought model used is ARMA(p,q) so only 2 graphs
                putResult("len", model.getLen());
                putResult("start", model.getStart());
                putResult("end", model.getEnd());
                putResult("data_source", model.getDataSource());
                putResult("drought_summary", FileUtils.readFileToString(model.getResult()));
                putResult("graph1", model.getOptimizedModel_ARMAgraphs()[0]);
                putResult("graph2", model.getOptimizedModel_ARMAgraphs()[1]);
            }
        } else {
            //Return only one graph and updated parameter info
            putResult("len", model.getLen());
            putResult("start", model.getStart());
            putResult("end", model.getEnd());
            putResult("data_source", model.getDataSource());
            putResult("drought_summary", FileUtils.readFileToString(model.getResult()));
            putResult("graph1", model.getFittedDataGraph());
        }
    }
}