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

import csip.Executable;
import csip.ModelDataService;
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 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.setDrainageArea(getDoubleParam("drainage_area"));
        model.setModelType(getStringParam("model_type"));
        model.setStartDate(getStringParam("begin_date"));
        model.setEndDate(getStringParam("end_date"));
        model.setNDMIN(getIntParam("ndmin"));
        model.setNDMAX(getIntParam("ndmax"));
        model.setMergeDatasets(getBooleanParam("merge_datasets"));
        model.setMergeMethod(getStringParam("merge_method"));
        model.setUserData(getStringParam("user_data"));

        Executable e = getResourceExe("bf");  // looking up the resource by id.
        model.run(e);
    }

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

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

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

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