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

import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.api.server.PayloadResults;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.VersionInfo;
import java.io.File;
import javax.ws.rs.Path;

@Name("durationcurve")
@Description("Time Series: Flow and Load Duration Curve Analysis")
@VersionInfo("1.0")
@Path("m/cfa/durationcurve/1.0")
public class V1_0 extends ModelDataService {

    guiDC_Model model = new guiDC_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.setWaterQualityTest(inputPayload.getString("wq_test"));
        model.setWaterQualityTarget(inputPayload.getDouble("wq_target"));
        model.setStartDate(inputPayload.getString("begin_date", ""));
        model.setEndDate(inputPayload.getString("end_date", ""));
        model.setSeasonBegin(inputPayload.getString("season_begin"));
        model.setSeasonEnd(inputPayload.getString("season_end"));
        model.setSeasonalAnalysisOnlyTF(inputPayload.getBoolean("seasonal_only"));
        model.setPeriod1Begin(inputPayload.getString("period1_begin", ""));
        model.setPeriod1End(inputPayload.getString("period1_end", ""));
        model.setPeriod2Begin(inputPayload.getString("period2_begin", ""));
        model.setPeriod2End(inputPayload.getString("period2_end", ""));
        model.setPeriod3Begin(inputPayload.getString("period3_begin", ""));
        model.setPeriod3End(inputPayload.getString("period3_end", ""));
        model.setHighPercentile(inputPayload.getDouble("highPercentile"));
        model.setLowPercentile(inputPayload.getDouble("lowPercentile"));
        model.setShowMonthlyStatsTF(inputPayload.getBoolean("showMonthlyStatsTF"));
        model.setCalcFlowStatisticsFileTF(inputPayload.getBoolean("calcFlowStatisticsFileTF"));
        model.setMQNperiod(inputPayload.getString("mqn_period"));
        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 {
        model.run();
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        // files
        File wd = workspace().getDir();
        if(model.getCalcFlowStatisticsFileTF()){
            resultPayload.put(new File(wd, model.getGraph()),
                      model.getDurationCurve_results(),
                      model.getFlowStatistics_summary(),
                      //Below are the result graph files for use by JHighCharts on eRAMS for duplicating the above graphs
                      model.getDCgraphOutput());
        }else{
            resultPayload.put(new File(wd, model.getGraph()),
                      model.getDurationCurve_results(),
                      //Below are the result graph files for use by JHighCharts on eRAMS for duplicating the above graphs
                      model.getDCgraphOutput());
        }

        // values
        resultPayload.put("flow_len", model.getFlowLen());
        resultPayload.put("flow_median", model.getMedian(), null, "cfs");
        resultPayload.put("flow_mean", model.getMean(), null, "cfs");
        resultPayload.put("flow_len_period1", model.getFlowLen_period1());
        resultPayload.put("flow_len_period2", model.getFlowLen_period2());
        resultPayload.put("flow_len_period3", model.getFlowLen_period3());
        resultPayload.put("wq_len", model.getWQLen());
        resultPayload.put("wq_units", model.getWQUnits());
        resultPayload.put("start", model.getStart());
        resultPayload.put("end", model.getEnd());
        resultPayload.put("duration_curve_summary", model.getSummaryParagraph());
        resultPayload.put("duration_curve_table", model.getSummaryTable());
        resultPayload.put("data_source", model.getDataSource());
        resultPayload.put("mqn_val", model.getMQNval(), null, "cfs");
        resultPayload.put("low_flow_error_message", model.getLowFlowErrorMessage());
        resultPayload.put("graph", model.getGraph());
        resultPayload.put("result_file", model.getDurationCurve_results().getName());
        resultPayload.put("flow_statistics_summary",model.getFlowStatistics_summary().getName());
        
        //Get result files for JSHighCharts
        String graphDataFiles = model.getDCgraphOutput().getName();
        resultPayload.put("graph_data_files", graphDataFiles);
    }
}