V1_0.java [src/java/m/wqm/wqm08_pesticidessrp] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package m.wqm.wqm08_pesticidessrp;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import java.util.ArrayList;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import csip.utils.JSONUtils;
import java.util.Map;
import java.util.logging.Level;
import org.codehaus.jettison.json.JSONException;
/**
*
* @author Srinivas, rumpal
*/
@Name("WQM-08: Pesticide Soil Solution Runoff Potential (PesticideSSRP)")
@Description("This service computes pesticide soil solution runoff potential "
+ "for soil components in an area of analysis, and computes soil "
+ "solution runoff potential representing the area of analysis (AoA). "
+ "The service primarily will consume data from the WQM-2 WQMSoilAttributes "
+ "service to compute soil solution runoff potential used later by the "
+ "WQM-13 service to compute WQM threshold treatment level scores.")
@Path("m/pesticide_ssrp/1.0")
public class V1_0 extends ModelDataService {
String comp_ssrp[] = new String[]{"", "LOW", "INTERMEDIATE", "HIGH"};
int comp_ssrp_number;
String aoa_ssrp;
ArrayList<m.wqm.wqm08_pesticidessrp.V1_0.Input> components = new ArrayList<>();
ArrayList<m.wqm.wqm08_pesticidessrp.V1_0.Result1> result1 = new ArrayList<>();
@Override
// reading the inputs from the json file into input object and placing it in the arraylist
protected void preProcess() throws ServiceException {
try {
JSONArray groups = parameter().getJSONArray("soilcomponents");
for (int i = 0; i < groups.length(); i++) {
Map<String, JSONObject> group = JSONUtils.preprocess(groups.getJSONArray(i));
int AoAId = JSONUtils.getIntParam(group, "AoAId", 0);
String cokey = JSONUtils.getStringParam(group, "cokey", "err");
double aoa_comp_area = JSONUtils.getDoubleParam(group, "aoa_comp_area", 0);
String aoa_comp_hsg = JSONUtils.getStringParam(group, "aoa_comp_hsg", "err");
boolean aoa_comp_drained = JSONUtils.getBooleanParam(group, "aoa_comp_drained", false);
components.add(new m.wqm.wqm08_pesticidessrp.V1_0.Input(AoAId, cokey, aoa_comp_area, aoa_comp_hsg, aoa_comp_drained));
}
} catch (JSONException | ServiceException ex) {
LOG.log(Level.SEVERE, "Error in processing the request JSON for WQM-8!", ex);
throw new ServiceException("Error in processing the request JSON.", ex);
}
}
@Override
protected void doProcess() {
for (m.wqm.wqm08_pesticidessrp.V1_0.Input ip : components) {
if (ip.aoa_comp_hsg.equals("C") || ip.aoa_comp_hsg.equals("D") || ip.aoa_comp_hsg.equals("C/D")) {
comp_ssrp_number = 3;
} else if (ip.aoa_comp_hsg.equals("A") || ip.aoa_comp_hsg.equals("A/D")) {
if (ip.aoa_comp_drained) {
comp_ssrp_number = 1;
} else if (!ip.aoa_comp_drained) {
comp_ssrp_number = 3;
}
} else if (ip.aoa_comp_hsg.equals("B") || ip.aoa_comp_hsg.equals("B/D")) {
if (ip.aoa_comp_drained) {
comp_ssrp_number = 2;
} else if (!ip.aoa_comp_drained) {
comp_ssrp_number = 3;
}
}
result1.add(new Result1(ip.AoAId, ip.cokey, ip.aoa_comp_area,
comp_ssrp[comp_ssrp_number], comp_ssrp_number));
}
calAoANutSLP(result1);
}
@Override
//writing the results back to JSON
protected void postProcess() {
for (m.wqm.wqm08_pesticidessrp.V1_0.Result1 temp : result1) {
results().put("AoAId", temp.AoAId, "Area of Analysis ID");
results().put("cokey", temp.cokey, "Cokey of each soil component");
results().put("aoa_comp_area", temp.aoa_comp_area, "Soil Component Area");
results().put("comp_ssrp", temp.comp_ssrp, "Calculated SSRP for soil component");
results().put("comp_ssrp_number", temp.comp_ssrp_number, "Number representing the SSRP");
}
results().put("aoa_ssrp", aoa_ssrp, "Overall SSRP of the area of analysis");
}
// calculate the aoa_nslp
void calAoANutSLP(ArrayList<m.wqm.wqm08_pesticidessrp.V1_0.Result1> source) {
double cum_nslp_product = 0;
double aoa_area = 0;
for (m.wqm.wqm08_pesticidessrp.V1_0.Result1 tmp : source) {
cum_nslp_product += (tmp.comp_ssrp_number * tmp.aoa_comp_area);
aoa_area += tmp.aoa_comp_area;
}
double aoa_nslp_fract = cum_nslp_product / aoa_area;
if (aoa_nslp_fract <= 1.50) {
aoa_ssrp = "LOW";
} else if (aoa_nslp_fract > 1.50 && aoa_nslp_fract <= 2.50) {
aoa_ssrp = "INTERMEDIATE";
} else {
aoa_ssrp = "HIGH";
}
}
static class Input {
int AoAId;
String cokey;
double aoa_comp_area;
String aoa_comp_hsg;
boolean aoa_comp_drained;
Input(int AoAId, String cokey, double aoa_comp_area,
String aoa_comp_hsg, boolean aoa_comp_drained) {
this.AoAId = AoAId;
this.cokey = cokey;
this.aoa_comp_area = aoa_comp_area;
this.aoa_comp_hsg = aoa_comp_hsg;
this.aoa_comp_drained = aoa_comp_drained;
}
}
static class Result1 {
int AoAId;
String cokey;
double aoa_comp_area;
String comp_ssrp;
int comp_ssrp_number;
Result1(int AoAId, String cokey, double aoa_comp_area,
String comp_nslp, int comp_nslp_number) {
this.AoAId = AoAId;
this.cokey = cokey;
this.aoa_comp_area = aoa_comp_area;
this.comp_ssrp = comp_nslp;
this.comp_ssrp_number = comp_nslp_number;
}
}
}