V1_0.java [src/java/m/cfa/drought] Revision: f04c8a2b1c221af01cc5e0cce3444f440b56affa  Date: Wed Nov 21 10:00:48 MST 2018
package m.cfa.drought;

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

@Name("drought")
@Description("Time Series: Drought Frequency Analysis")
@VersionInfo("1.0")
@Path("m/cfa/drought/1.0")
public class V1_0 extends ModelDataService {

    guiDrought_Model model = new guiDrought_Model();

    @Override
    protected void doProcess() throws Exception {
        model.setDirectory(getWorkspaceDir().toString());
        model.setDatabase(getStringParam("database"));
        model.setOrganizationID(getStringParam("org_id"));
        model.setStationId(getStringParam("station_id"));
        model.setStationName(getStringParam("station_name"));
        model.setStartDate(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.setMergeDatasets(getBooleanParam("merge_datasets"));
        model.setMergeMethod(getStringParam("merge_method"));
        model.setUserData(getStringParam("user_data"));

        model.run();
    }

    @Override
    protected void postProcess() throws Exception {
        //This result array is more complex because drought analysis can pass back 1, 2, or 5 graphs
        // files
        File wd = getWorkspaceDir();
        if(model.getAction().equalsIgnoreCase("all")){
            putResult(model.getResult(),
                      new File(wd, model.getTimeseriesGraph()),
                      new File(wd, model.getColumnChart()),
                      new File(wd, model.getFittedDataGraph()),
                      new File(wd, model.getProjectedDataGraph()),
                      new File(wd, 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(wd, model.getOptimizedModel_ARgraph()));
            }else{
                //The drought model used is ARMA(p,q) so only 2 graphs
                putResult(new File(wd, model.getOptimizedModel_ARMAgraphs()[0]), 
                          new File(wd, model.getOptimizedModel_ARMAgraphs()[1]));
            }
        }else{
            putResult(new File(wd, 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());
        }
    }
}