V1_0.java [src/java/m/cfa/flood] Revision: b57fc7cc43a2bf83326f77afbe2341c6327520da  Date: Wed Feb 28 21:03:02 MST 2024
package m.cfa.flood;

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;
import org.apache.commons.io.FileUtils;

@Name("flood")
@Description("Time Series: Flood Frequency Analysis")
@VersionInfo("1.0")
@Path("m/cfa/flood/1.0")
public class V1_0 extends ModelDataService {

    guiFlood_Model model = new guiFlood_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.setAnalysisType(inputPayload.getString("analysis_type", "b17"));
        model.setStartDate(inputPayload.getString("begin_date", ""));
        model.setEndDate(inputPayload.getString("end_date", ""));
        model.setSkewness(inputPayload.getDouble("skewness"));
        model.setMeanSquareError(inputPayload.getDouble("mean_square_error"));
        model.setShowLargeFloods(inputPayload.getBoolean("show_large_floods", false));
        model.setPlotType(inputPayload.getBoolean("plot_type", true));
        model.setPlotReference(inputPayload.getBoolean("plot_ref", true));
        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();
        resultPayload.put(new File(wd, model.getGraph()));

        // values
        resultPayload.put("len", model.getLen());
        resultPayload.put("start", model.getStart());
        resultPayload.put("end", model.getEnd());
        resultPayload.put("data_source", model.getDataSource());
        resultPayload.put("skewErrorMessage", model.getSkewErrorMessage());
        String output = FileUtils.readFileToString(model.getOutputWeightedGenSkew(), "UTF-8");
        resultPayload.put("output", output);
        resultPayload.put("weightedGenSkew", model.getWeightedGenSkew());
        String output_stationSkew = FileUtils.readFileToString(model.getOutputStationSkew(), "UTF-8");
        resultPayload.put("output_stationSkew", output_stationSkew);
        resultPayload.put("stationSkew", model.getStationSkew());
        resultPayload.put("graph", model.getGraph());
    }
}