Drought_V1_0.java [src/java/m/cfa] Revision: 43efd7e0f3b8fe3c64f8229934ef0a7c85a69c01  Date: Fri Feb 14 14:19:18 MST 2014
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.cfa;

import cfa.guiDrought_Model;
import csip.AbstractModelService;
import java.io.IOException;
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.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.*;
import csip.utils.JSONUtils;
import csip.ServiceConst;

@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<Integer> createCallable() throws Exception {
        return new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Map<String, JSONObject> m = getParamMap();

                model.setMainFolder(getWorkspaceDir().toString());
                model.setOrganizationName(m.get("org").getString(ServiceConst.VALUE));
                model.setStationID(m.get("station_id").getString(ServiceConst.VALUE));
                model.setStationName(m.get("station_name").getString(ServiceConst.VALUE));
                model.setBeginDate(m.get("begin_date").getString(ServiceConst.VALUE));
                model.setEndDate(m.get("end_date").getString(ServiceConst.VALUE));
                model.setLambdaString(m.get("lambda_string").getString(ServiceConst.VALUE));
                model.setAction(m.get("action").getString(ServiceConst.VALUE));
                model.setPhiValues(m.get("phi_values").getString(ServiceConst.VALUE));
                model.setThetaValues(m.get("theta_values").getString(ServiceConst.VALUE));
                model.setDroughtLimit(m.get("drought_limit").getDouble(ServiceConst.VALUE));
                model.setUserData(m.get("user_data").getString(ServiceConst.VALUE));
                model.setMergeDatasets(m.get("merge_datasets").getBoolean(ServiceConst.VALUE));
                model.setMergeMethod(m.get("merge_method").getString(ServiceConst.VALUE));
                
                model.run();
                return ServiceConst.EXEC_OK;
            }
        };
    }

    @Override
    public String[] 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 model.get_all_output();

        }else if(model.getAction().equalsIgnoreCase("optimizeModel")){
            return model.get_optimizeModel_output();
            
        }else{
            return new String[] {model.get_parameter_output()};
        }
    }
    
    @Override
    protected JSONArray createResults() throws Exception {
        //This result array is more complex because drought can pass back 1, 2, or 5 graphs
        if(model.getAction().equalsIgnoreCase("all")){
            String[] graphs = model.get_all_output();
            String graph1 = graphs[0];
            String graph2 = graphs[1];
            String graph3 = graphs[2];
            String graph4 = graphs[3];
            String graph5 = graphs[4];
            String output = FileUtils.readFileToString(model.getResult());

            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", graph1));
            result.put(JSONUtils.data("graph2", graph2));
            result.put(JSONUtils.data("graph3", graph3));
            result.put(JSONUtils.data("graph4", graph4));
            result.put(JSONUtils.data("graph5", graph5));
            result.put(JSONUtils.data("output", output));
            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];
                String output = FileUtils.readFileToString(model.getResult());

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

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

            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", graph1));
            result.put(JSONUtils.data("output", output));
            return result;
        }
    }

    @Override
    protected JSONObject describe() throws JSONException {
        try {
            String tmpl = IOUtils.toString(Download_V1_0.class.getResource("/m/cfa/Drought_V1_0Req.json"));
            return new JSONObject(tmpl);
        } catch (IOException ex) {
            throw new RuntimeException("Not found: /m/cfa/Drought_V1_0Req.json");
        }
    }
}