Baseflow_V1_0.java [src/java/m/cfa] Revision: 5f9df206c7579765eed0feaf8307ec8f46bd6981  Date: Fri Jan 23 14:22:57 MST 2015
package m.cfa;

import cfa.guiBaseflow_Model;
import csip.AbstractModelService;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import oms3.annotations.VersionInfo;
import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.*;
import csip.utils.JSONUtils;
import csip.utils.Services;
import java.io.File;

@Name("baseflow")
@Description("baseflow")
@VersionInfo("1.0")
@Path("m/cfa/baseflow/1.0")
public class Baseflow_V1_0 extends AbstractModelService {

    guiBaseflow_Model model = new guiBaseflow_Model();

    @Override
    protected Callable<String> createCallable() throws Exception {
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                Map<String, JSONObject> m = getParamMap();

                model.setMainFolder(getWorkspaceDir().toString());
                model.setDatabase(m.get("database").getString(VALUE));
                model.setOrganizationName(m.get("org").getString(VALUE));
                model.setStationID(m.get("station_id").getString(VALUE));
                model.setStationName(m.get("station_name").getString(VALUE));
                model.setDrainageArea(m.get("drainage_area").getDouble(VALUE));
                model.setModelType(m.get("model_type").getString(VALUE));
                model.setBeginDate(m.get("begin_date").getString(VALUE));
                model.setEndDate(m.get("end_date").getString(VALUE));
                model.setNDMIN(m.get("ndmin").getInt(VALUE));
                model.setNDMAX(m.get("ndmax").getInt(VALUE));
                model.setUseSTORETretrieval(m.get("useSTORETretrieval").getBoolean(VALUE));
                model.setSTORETdata(m.get("STORETdata").getString(VALUE));
                model.setMergeDatasets(m.get("merge_datasets").getBoolean(VALUE));
                model.setMergeMethod(m.get("merge_method").getString(VALUE));
                model.setUserData(m.get("user_data").getString(VALUE));
                
                model.run();
                return EXEC_OK;
            }
        };
    }
    
    @Override
    public File[] postprocess() throws Exception {
        return Services.toFiles(model.getGraph(),
                                model.getBaseflow_out().getName(),
                                //Below are the result graph files for use by JHighCharts on eRAMS for duplicating the above graphs
                                model.getTimeseriesOutput().getName());
    }
    
    @Override
    protected JSONArray createResults() throws Exception {
        String baseflowSummary = FileUtils.readFileToString(model.getBaseflow_dat());
        JSONArray result = new JSONArray();
        result.put(JSONUtils.data("len", model.getLen()));
        result.put(JSONUtils.data("start", model.getStart()));
        result.put(JSONUtils.data("end", model.getEnd()));
        result.put(JSONUtils.data("data_source", model.getDataSource()));
        result.put(JSONUtils.data("baseflow_summary", baseflowSummary));
        result.put(JSONUtils.data("baseflow_results", model.getBaseflow_out().getName()));
        result.put(JSONUtils.data("graph", model.getGraph()));
        //Get BFLOW stream flow stats
        result.put(JSONUtils.data("streamflow_max", model.getBFLOWstream_Max()));
        result.put(JSONUtils.data("streamflow_min", model.getBFLOWstream_Min()));
        result.put(JSONUtils.data("streamflow_median", model.getBFLOWstream_Median()));
        result.put(JSONUtils.data("streamflow_mean", model.getBFLOWstream_Mean()));
        //Get BFLOW pass 1 stats
        result.put(JSONUtils.data("bflowPass1_max", model.getBFLOWpass1_Max()));
        result.put(JSONUtils.data("bflowPass1_min", model.getBFLOWpass1_Min()));
        result.put(JSONUtils.data("bflowPass1_median", model.getBFLOWpass1_Median()));
        result.put(JSONUtils.data("bflowPass1_mean", model.getBFLOWpass1_Mean()));
        //Get BFLOW pass 2 stats
        result.put(JSONUtils.data("bflowPass2_max", model.getBFLOWpass2_Max()));
        result.put(JSONUtils.data("bflowPass2_min", model.getBFLOWpass2_Min()));
        result.put(JSONUtils.data("bflowPass2_median", model.getBFLOWpass2_Median()));
        result.put(JSONUtils.data("bflowPass2_mean", model.getBFLOWpass2_Mean()));
        //Get BFLOW pass 3 stats
        result.put(JSONUtils.data("bflowPass3_max", model.getBFLOWpass3_Max()));
        result.put(JSONUtils.data("bflowPass3_min", model.getBFLOWpass3_Min()));
        result.put(JSONUtils.data("bflowPass3_median", model.getBFLOWpass3_Median()));
        result.put(JSONUtils.data("bflowPass3_mean", model.getBFLOWpass3_Mean()));
        
        //Get result files for JSHighCharts
        String graphDataFiles = model.getTimeseriesOutput().getName();
        result.put(JSONUtils.data("graph_data_files", graphDataFiles));
        return result;
    }
}