V1_0.java [src/java/m/cfa/drought] Revision: 4670874bcc79b6e0ba482761941592c3a739900e  Date: Wed Sep 01 14:39:54 MDT 2021
package m.cfa.drought;

import csip.ModelDataService;
import csip.PayloadParameter;
import csip.PayloadResults;
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 preProcess() throws Exception {
        model.setDirectory(getWorkspaceDir().toString());
        
        PayloadParameter inputPayload = parameter();
        model.setDatabase(inputPayload.getString("database"));
        model.setOrganizationID(inputPayload.getString("org_id"));
        model.setStationId(inputPayload.getString("station_id"));
        model.setStationName(inputPayload.getString("station_name"));
        model.setStartDate(inputPayload.getString("begin_date", ""));
        model.setEndDate(inputPayload.getString("end_date", ""));
        model.setLambdaString(inputPayload.getString("lambda_string"));
        model.setAction(inputPayload.getString("action"));
        model.setPhiValues(inputPayload.getString("phi_values"));
        model.setThetaValues(inputPayload.getString("theta_values"));
        model.setDroughtLimit(inputPayload.getDouble("drought_limit"));
        model.setMergeDatasets(inputPayload.getBoolean("merge_datasets", false));
        model.setMergeMethod(inputPayload.getString("merge_method", "user"));
        model.setUserData(inputPayload.getString("user_data", ""));
    }

    @Override
    protected void doProcess() throws Exception {
        model.run();
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        //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")){
            resultPayload.put(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
                resultPayload.put(new File(wd, model.getOptimizedModel_ARgraph()));
            }else{
                //The drought model used is ARMA(p,q) so only 2 graphs
                resultPayload.put(new File(wd, model.getOptimizedModel_ARMAgraphs()[0]), 
                          new File(wd, model.getOptimizedModel_ARMAgraphs()[1]));
            }
        }else{
            resultPayload.put(new File(wd, model.getFittedDataGraph()));
        }
        
        // values
        if (model.getAction().equalsIgnoreCase("all")) {
            resultPayload.put("len", model.getLen());
            resultPayload.put("start", model.getStart());
            resultPayload.put("end", model.getEnd());
            resultPayload.put("data_source", model.getDataSource());
            resultPayload.put("drought_limit", model.getDroughtLimit(), null, "acre-ft");
            //Get other results
            resultPayload.put("drought_summary",model.getResult().getName());
            resultPayload.put("graph1", model.getTimeseriesGraph());
            resultPayload.put("graph2", model.getColumnChart());
            resultPayload.put("graph3", model.getFittedDataGraph());
            resultPayload.put("graph4", model.getProjectedDataGraph());
            resultPayload.put("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();
            resultPayload.put("graph_data_files", graphDataFiles);
            
        } else if (model.getAction().equalsIgnoreCase("optimizeModel")) {
            if (model.getThetaValues().equalsIgnoreCase("")) {
                //The drought model is AR(p) so only 1 graph
                resultPayload.put("len", model.getLen());
                resultPayload.put("start", model.getStart());
                resultPayload.put("end", model.getEnd());
                resultPayload.put("data_source", model.getDataSource());
                resultPayload.put("drought_summary", FileUtils.readFileToString(model.getResult()));
                resultPayload.put("graph1", model.getOptimizedModel_ARgraph());
            } else {
                //The drought model used is ARMA(p,q) so only 2 graphs
                resultPayload.put("len", model.getLen());
                resultPayload.put("start", model.getStart());
                resultPayload.put("end", model.getEnd());
                resultPayload.put("data_source", model.getDataSource());
                resultPayload.put("drought_summary", FileUtils.readFileToString(model.getResult()));
                resultPayload.put("graph1", model.getOptimizedModel_ARMAgraphs()[0]);
                resultPayload.put("graph2", model.getOptimizedModel_ARMAgraphs()[1]);
            }
        } else {
            //Return only one graph and updated parameter info
            resultPayload.put("len", model.getLen());
            resultPayload.put("start", model.getStart());
            resultPayload.put("end", model.getEnd());
            resultPayload.put("data_source", model.getDataSource());
            resultPayload.put("drought_summary", FileUtils.readFileToString(model.getResult()));
            resultPayload.put("graph1", model.getFittedDataGraph());
        }
    }
}