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

import cfa.guiDC_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.codehaus.jettison.json.*;
import csip.utils.JSONUtils;
import csip.utils.Services;
import java.io.File;

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

    guiDC_Model model = new guiDC_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.setWaterQualityTest(m.get("wq_test").getString(VALUE));
                model.setWaterQualityTarget(m.get("wq_target").getDouble(VALUE));
                model.setBeginDate(m.get("begin_date").getString(VALUE));
                model.setEndDate(m.get("end_date").getString(VALUE));
                model.setSeasonBegin(m.get("season_begin").getString(VALUE));
                model.setSeasonEnd(m.get("season_end").getString(VALUE));
                model.setSeasonalAnalysisOnlyTF(m.get("seasonal_only").getBoolean(VALUE));
                model.setPeriod1Begin(m.get("period1_begin").getString(VALUE));
                model.setPeriod1End(m.get("period1_end").getString(VALUE));
                model.setPeriod2Begin(m.get("period2_begin").getString(VALUE));
                model.setPeriod2End(m.get("period2_end").getString(VALUE));
                model.setPeriod3Begin(m.get("period3_begin").getString(VALUE));
                model.setPeriod3End(m.get("period3_end").getString(VALUE));
                model.setHighPercentile(m.get("highPercentile").getDouble(VALUE));
                model.setLowPercentile(m.get("lowPercentile").getDouble(VALUE));
                model.setShowMonthlyStatsTF(m.get("showMonthlyStatsTF").getBoolean(VALUE));
                model.setMQNperiod(m.get("mqn_period").getString(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.getDurationCurve_results().getName(),
                                model.getFlowStatistics_summary().getName(),
                                //Below are the result graph files for use by JHighCharts on eRAMS for duplicating the above graphs
                                model.getDCgraphOutput().getName());
    }

    @Override
    protected JSONArray createResults() throws Exception {
        JSONArray result = new JSONArray();
        result.put(JSONUtils.data("flow_len", model.getFlowLen()));
        result.put(JSONUtils.data("flow_median", model.getMedian()));
        result.put(JSONUtils.data("flow_mean", model.getMean()));
        result.put(JSONUtils.data("flow_len_period1", model.getFlowLen_period1()));
        result.put(JSONUtils.data("flow_len_period2", model.getFlowLen_period2()));
        result.put(JSONUtils.data("flow_len_period3", model.getFlowLen_period3()));
        result.put(JSONUtils.data("wq_len", model.getWQLen()));
        result.put(JSONUtils.data("wq_units", model.getWQUnits()));
        result.put(JSONUtils.data("start", model.getStart()));
        result.put(JSONUtils.data("end", model.getEnd()));
        result.put(JSONUtils.data("duration_curve_summary", model.getSummaryParagraph()));
        result.put(JSONUtils.data("duration_curve_table", model.getSummaryTable()));
        result.put(JSONUtils.data("data_source", model.getDataSource()));
        result.put(JSONUtils.data("mqn_val", model.getMQNval()));
        result.put(JSONUtils.data("low_flow_error_message", model.getLowFlowErrorMessage()));
        result.put(JSONUtils.data("graph", model.getGraph()));
        result.put(JSONUtils.data("result_file", model.getDurationCurve_results().getName()));
        result.put(JSONUtils.data("flow_statistics_summary",model.getFlowStatistics_summary().getName()));
        
        //Get result files for JSHighCharts
        String graphDataFiles = model.getDCgraphOutput().getName();
        result.put(JSONUtils.data("graph_data_files", graphDataFiles));
        return result;
    }

}