V1_0.java [src/java/m/cfa/baseflow] Revision: default  Date:
package m.cfa.baseflow;

import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.api.server.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(workspace().getDir().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", false));
        model.setMergeMethod(inputPayload.getString("merge_method", "user"));
        model.setUserData(inputPayload.getString("user_data", ""));
    }
    
    @Override
    protected void doProcess() throws Exception {
        Executable e = resources().getExe("bf");  // looking up the resource by id.
        model.run(e);
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        // files
        File wd = workspace().getDir();
        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);
    }
}