V1_0.java [src/java/m/wqm/wqm09_pesticidesarp] Revision: Date:
package m.wqm.wqm09_pesticidesarp;
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
* @author Rumpal Sidhu
*/
@Name("WQM-09: Pesticide Soil Adsorbed Runoff Potential (PesticideSARP)")
@Description("This service computes pesticide soil adsorbed runoff potential "
+ "for soil components in an area of analysis, and then computes soil "
+ "adsorbed runoff potential representing the area of analysis. "
+ "The service primarily will consume data from the WQM-2 WQMSoilAttributes "
+ "service to compute pesticide soil adsorbed runoff potentials used "
+ "later by the WQM-13 to compute threshold treatment level scores.")
@Path("m/pesticide_sarp/1.0")
public class V1_0 extends ModelDataService {
String comp_sarp[] = new String[]{"", "LOW", "INTERMEDIATE", "HIGH"};
int comp_sarp_number;
String aoa_sarp;
//JSONArray getArray
ArrayList<m.wqm.wqm09_pesticidesarp.V1_0.Input> components;// store the set of all input soilcomponents as objects
ArrayList<m.wqm.wqm09_pesticidesarp.V1_0.Result1> result1; // store the result as objects
@Override
// reading the inputs from the json file into input object and placing it in the arraylist
protected void preProcess() throws ServiceException {
try {
components = new ArrayList<>();
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");
double aoa_comp_kfact = JSONUtils.getDoubleParam(group, "aoa_comp_kfact", 0);
boolean aoa_comp_slopegr15 = JSONUtils.getBooleanParam(group, "aoa_comp_slopegr15", false);
boolean aoa_comp_drained = JSONUtils.getBooleanParam(group, "aoa_comp_drained", false);
components.add(new m.wqm.wqm09_pesticidesarp.V1_0.Input(AoAId, cokey,
aoa_comp_area, aoa_comp_hsg, aoa_comp_kfact, aoa_comp_slopegr15,
aoa_comp_drained));
}
} catch (JSONException | ServiceException ex) {
LOG.log(Level.SEVERE, "Error in processing the request JSON for WQM-9!", ex);
throw new ServiceException("Error in processing the request JSON.", ex);
}
}
@Override
protected void doProcess() {
result1 = new ArrayList<>();
for (m.wqm.wqm09_pesticidesarp.V1_0.Input ip : components) {
if ((ip.aoa_comp_hsg.equals("C") && ip.aoa_comp_kfact >= 0.21)
|| (ip.aoa_comp_hsg.equals("D") && ip.aoa_comp_kfact >= 0.10)
|| (ip.aoa_comp_hsg.equals("C\\/D") && ip.aoa_comp_drained
&& ip.aoa_comp_kfact >= 0.21)) {
comp_sarp_number = 3;
} else if (ip.aoa_comp_hsg.equals("A")
|| (ip.aoa_comp_hsg.equals("B") && ip.aoa_comp_kfact <= 0.10)
|| (ip.aoa_comp_hsg.equals("C") && ip.aoa_comp_kfact <= 0.07)
|| (ip.aoa_comp_hsg.equals("D") && ip.aoa_comp_kfact <= 0.02)
|| (ip.aoa_comp_hsg.equals("A\\/D") && ip.aoa_comp_drained)
|| (ip.aoa_comp_hsg.equals("B\\/D") && ip.aoa_comp_kfact <= 0.10
&& ip.aoa_comp_drained)
|| (ip.aoa_comp_hsg.equals("C\\/D") && ip.aoa_comp_kfact <= 0.07
&& ip.aoa_comp_drained)
|| ((ip.aoa_comp_hsg.equals("A\\/D")
|| ip.aoa_comp_hsg.equals("B\\/D")
|| ip.aoa_comp_hsg.equals("C\\/D"))
&& ip.aoa_comp_kfact <= 0.02 && !ip.aoa_comp_drained)) {
if (!ip.aoa_comp_slopegr15) {
comp_sarp_number = 1;
} else {
comp_sarp_number = 2;
}
} else if (!ip.aoa_comp_slopegr15) {
comp_sarp_number = 2;
} else {
comp_sarp_number = 3;
}
result1.add(new m.wqm.wqm09_pesticidesarp.V1_0.Result1(ip.AoAId, ip.cokey,
ip.aoa_comp_area, comp_sarp[comp_sarp_number], comp_sarp_number));
}
calAoANutSLP(result1);
}
@Override
//writing the results back to JSON
protected void postProcess() {
for (m.wqm.wqm09_pesticidesarp.V1_0.Result1 temp : result1) {
results().put("AoAId", temp.AoAId, "Area of Analysis ID");
results().put("cokey", temp.cokey, "cokey");
results().put("aoa_comp_area", temp.aoa_comp_area, "Area of the soil component in the AoA");
results().put("comp_sarp", temp.comp_sarp, "Pesticide Soil Adsorbed Runoff Potential");
results().put("comp_sarp_number", temp.comp_sarp_number, "Integer Representinf Pesticide Soil Adsorbed Runoff Potential");
}
results().put("aoa_sarp", aoa_sarp, "Pesticide Soil Adsorbed Runoff Potential of AoA");
}
//#Compute weighted average pesticide soil adsorbed runoff potential for the AoA
void calAoANutSLP(ArrayList<m.wqm.wqm09_pesticidesarp.V1_0.Result1> source) {
double cum_sarp_product = 0;
double aoa_area = 0;
for (m.wqm.wqm09_pesticidesarp.V1_0.Result1 tmp : source) {
cum_sarp_product += (tmp.comp_sarp_number * tmp.aoa_comp_area);
aoa_area += tmp.aoa_comp_area;
}
double aoa_sarp_fract = cum_sarp_product / aoa_area;
if (aoa_sarp_fract <= 1.50) {
aoa_sarp = "LOW";
} else if (aoa_sarp_fract > 1.50 && aoa_sarp_fract <= 2.50) {
aoa_sarp = "INTERMEDIATE";
} else {
aoa_sarp = "HIGH";
}
}
static class Input {
int AoAId;
String cokey;
double aoa_comp_area;
String aoa_comp_hsg;
double aoa_comp_kfact;
boolean aoa_comp_slopegr15;
boolean aoa_comp_drained;
public Input(int AoAId, String cokey, double aoa_comp_area,
String aoa_comp_hsg, double aoa_comp_kfact,
boolean aoa_comp_slopegr15, 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_kfact = aoa_comp_kfact;
this.aoa_comp_slopegr15 = aoa_comp_slopegr15;
this.aoa_comp_drained = aoa_comp_drained;
}
}
static class Result1 {
int AoAId;
String cokey;
double aoa_comp_area;
String comp_sarp;
int comp_sarp_number;
public Result1(int AoAId, String cokey, double aoa_comp_area,
String comp_sarp, int comp_sarp_number) {
this.AoAId = AoAId;
this.cokey = cokey;
this.aoa_comp_area = aoa_comp_area;
this.comp_sarp = comp_sarp;
this.comp_sarp_number = comp_sarp_number;
}
}
}