V1_0.java [src/java/m/cfa/baseflow] Revision: f159fdc696b78fa7bc3c940760935303b650dfe5  Date: Wed Jun 19 16:59:52 MDT 2019
package m.cfa.baseflow;

import csip.Executable;
import csip.ModelDataService;
import csip.PayloadParameter;
import csip.PayloadResults;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.annotations.ResourceType;
import csip.annotations.VersionInfo;
import java.io.File;
import javax.ws.rs.Path;

import org.apache.commons.io.FileUtils;

@Name("baseflow")
@Description("Time Series: Baseflow Separation")
@VersionInfo("1.0")
@Path("m/cfa/baseflow/1.0")
@Resource(file="/bin/win-x86/bflow.exe", wine=true, id="bf", type=ResourceType.EXECUTABLE)
public class V1_0 extends ModelDataService {

    guiBaseflow_Model model = new guiBaseflow_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.setDrainageArea(inputPayload.getDouble("drainage_area"));
        model.setModelType(inputPayload.getString("model_type"));
        model.setStartDate(inputPayload.getString("begin_date"));
        model.setEndDate(inputPayload.getString("end_date"));
        model.setNDMIN(inputPayload.getInt("ndmin"));
        model.setNDMAX(inputPayload.getInt("ndmax"));
        model.setMergeDatasets(inputPayload.getBoolean("merge_datasets"));
        model.setMergeMethod(inputPayload.getString("merge_method"));
        model.setUserData(inputPayload.getString("user_data"));
    }
    
    @Override
    protected void doProcess() throws Exception {
        Executable e = getResourceExe("bf");  // looking up the resource by id.
        model.run(e);
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        // files
        File wd = getWorkspaceDir();
        resultPayload.put(new File(wd, model.getGraph()),
                  model.getBaseflow_out(),
                  model.getTimeseriesOutput());

        // values
        resultPayload.put("len", model.getLen());
        resultPayload.put("start", model.getStart());
        resultPayload.put("end", model.getEnd());
        resultPayload.put("data_source", model.getDataSource());

        String baseflowSummary = FileUtils.readFileToString(model.getBaseflow_dat());
        resultPayload.put("baseflow_summary", baseflowSummary);
        resultPayload.put("baseflow_results", model.getBaseflow_out().getName());
        resultPayload.put("graph", model.getGraph());
        //Get BFLOW stream flow stats
        resultPayload.put("streamflow_max", model.getBFLOWstream_Max(), null, "cfs");
        resultPayload.put("streamflow_min", model.getBFLOWstream_Min(), null, "cfs");
        resultPayload.put("streamflow_median", model.getBFLOWstream_Median(), null, "cfs");
        resultPayload.put("streamflow_mean", model.getBFLOWstream_Mean(), null, "cfs");
        //Get BFLOW pass 1 stats
        resultPayload.put("bflowPass1_max", model.getBFLOWpass1_Max(), null, "cfs");
        resultPayload.put("bflowPass1_min", model.getBFLOWpass1_Min(), null, "cfs");
        resultPayload.put("bflowPass1_median", model.getBFLOWpass1_Median(), null, "cfs");
        resultPayload.put("bflowPass1_mean", model.getBFLOWpass1_Mean(), null, "cfs");
        //Get BFLOW pass 2 stats
        resultPayload.put("bflowPass2_max", model.getBFLOWpass2_Max(), null, "cfs");
        resultPayload.put("bflowPass2_min", model.getBFLOWpass2_Min(), null, "cfs");
        resultPayload.put("bflowPass2_median", model.getBFLOWpass2_Median(), null, "cfs");
        resultPayload.put("bflowPass2_mean", model.getBFLOWpass2_Mean(), null, "cfs");
        //Get BFLOW pass 3 stats
        resultPayload.put("bflowPass3_max", model.getBFLOWpass3_Max(), null, "cfs");
        resultPayload.put("bflowPass3_min", model.getBFLOWpass3_Min(), null, "cfs");
        resultPayload.put("bflowPass3_median", model.getBFLOWpass3_Median(), null, "cfs");
        resultPayload.put("bflowPass3_mean", model.getBFLOWpass3_Mean(), null, "cfs");

        //Get result files for JSHighCharts
        String graphDataFiles = model.getTimeseriesOutput().getName();
        resultPayload.put("graph_data_files", graphDataFiles);
    }
}