Drought_V1_0.java [src/java/m/cfa] Revision: e202b7ae4ad75482011d557e656f07e7f4be8158  Date: Fri Jul 25 09:57:02 MDT 2014
package m.cfa;

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

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

    guiDrought_Model model = new guiDrought_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.setBeginDate(m.get("begin_date").getString(VALUE));
                model.setEndDate(m.get("end_date").getString(VALUE));
                model.setLambdaString(m.get("lambda_string").getString(VALUE));
                model.setAction(m.get("action").getString(VALUE));
                model.setPhiValues(m.get("phi_values").getString(VALUE));
                model.setThetaValues(m.get("theta_values").getString(VALUE));
                model.setDroughtLimit(m.get("drought_limit").getDouble(VALUE));
                model.setUserData(m.get("user_data").getString(VALUE));
                model.setMergeDatasets(m.get("merge_datasets").getBoolean(VALUE));
                model.setMergeMethod(m.get("merge_method").getString(VALUE));

                model.run();
                return EXEC_OK;
            }
        };
    }

    @Override
    public File[] postprocess() throws Exception {
         //This result array is more complex because drought can pass back 1, 2, or 5 graphs

        if (model.getAction().equalsIgnoreCase("all")) {

            return Services.toFiles(model.getResult().getName(),
                                    model.getTimeseriesGraph(),
                                    model.getColumnChart(),
                                    model.getFittedDataGraph(),
                                    model.getProjectedDataGraph(),
                                    model.getDroughtRecurrenceGraph(),
                                    //Below are the result graph files for use by JSHighCharts on eRAMS for duplicating the above graphs
                                    model.getTimeseriesOutput().getName(),
                                    model.getColumnChartOutput().getName(),
                                    model.getFittedGraphOutput().getName(),
                                    model.getProjectedGraphOutput().getName(),
                                    model.getRecurrenceGraphOutput().getName());

        } else if (model.getAction().equalsIgnoreCase("optimizeModel")) {
            return Services.toFiles(model.get_optimizeModel_output());

        } else {
            return Services.toFiles(model.getFittedDataGraph());
        }
    }

    @Override
    protected JSONArray createResults() throws Exception {
        //This result array is more complex because drought analysis can pass back 1, 2, or 5 graphs
        if (model.getAction().equalsIgnoreCase("all")) {
            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("drought_limit", model.getDroughtLimit()));
            //Get other results
            result.put(JSONUtils.data("drought_summary",model.getResult().getName()));
            result.put(JSONUtils.data("graph1", model.getTimeseriesGraph()));
            result.put(JSONUtils.data("graph2", model.getColumnChart()));
            result.put(JSONUtils.data("graph3", model.getFittedDataGraph()));
            result.put(JSONUtils.data("graph4", model.getProjectedDataGraph()));
            result.put(JSONUtils.data("graph5", model.getDroughtRecurrenceGraph()));
            
            //Get result files for JSHighCharts
            String graphDataFiles = model.getTimeseriesOutput().getName() + "|" + 
                                    model.getColumnChartOutput().getName() + "|" +
                                    model.getFittedGraphOutput().getName() + "|" +
                                    model.getProjectedGraphOutput().getName() + "|" +
                                    model.getRecurrenceGraphOutput().getName();
            result.put(JSONUtils.data("graph_data_files", graphDataFiles));
            return result;
            
        } else if (model.getAction().equalsIgnoreCase("optimizeModel")) {
            String[] graphs = model.get_optimizeModel_output();
            if (model.getThetaValues().equalsIgnoreCase("")) {
                //The drought model is AR(p) so only 1 graph
                String graph1 = graphs[0];

                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("drought_summary", FileUtils.readFileToString(model.getResult())));
                result.put(JSONUtils.data("graph1", graph1));
                return result;
            } else {
                //The drought model used is ARMA(p,q) so only 2 graphs
                String graph1 = graphs[0];
                String graph2 = graphs[1];

                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("drought_summary", FileUtils.readFileToString(model.getResult())));
                result.put(JSONUtils.data("graph1", graph1));
                result.put(JSONUtils.data("graph2", graph2));
                return result;
            }
        } else {
            //Return only one graph and updated parameter info
            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("graph1", model.getFittedDataGraph()));
            result.put(JSONUtils.data("drought_summary", FileUtils.readFileToString(model.getResult())));
            return result;
        }
    }
}