WSStatsV1_0.java [src/java/m/weppws/results] 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.weppws.results;
import csip.ModelDataService;
import csip.annotations.*;
import java.io.File;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.ws.rs.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import util.Extract;
import util.Extract.Channel;
import util.Extract.Hillslope;
import util.Extract.Summary;
@Name("wsstats")
@Description("WEPPWS channel stats")
@Path("m/wsstats/1.0")
@Polling(first = 2000, next = 2000)
public class WSStatsV1_0 extends ModelDataService {
@Override
protected void doProcess() throws Exception {
String f_loss = parameter().getString("wsloss_file", "loss_pw0.txt");
String f_outp = parameter().getString("outputsummary", "outputsummary.json");
int years = parameter().getInt("years");
File s = workspace().getExistingFile(f_outp);
File file = workspace().getExistingFile(f_loss);
Map<Integer, JSONObject> cm = Extract.getChannelResultsMap(s);
Summary summary = Extract.getChannelStats(file, years);
Map<Integer, Integer> chIdMap = Extract.getChannelIdMap(s);
Map<Integer, Integer> hsIdMap = Extract.getHillslopeIdMap(s);
if (parameter().has("ch_id")) {
int chIds = parameter().getInt("ch_id");
JSONObject chResult = cm.get(chIds);
int weppid = chResult.getInt("weppID");
Channel ch = summary.getChannels().get(weppid - 1);
putStats("Discharge Volume (m^3)", ch.getDischVolStats());
putStats("Sediment Yield (tonne)", ch.getSedYieldStats());
putStats("Soil Loss (kg)", ch.getSoilLossStats());
putStats("Upland Charge (m^3)", ch.getUplandChargeStats());
putStats("Subsuface Flow Volume (m^3)", ch.getSsurfflowVolStats());
JSONArray names = chResult.names();
for (int i = 0; i < names.length(); i++) {
String name = names.getString(i);
if (name.equals("topazid"))
continue;
results().put(name, chResult.get(name));
}
} else if (parameter().has("ch_ids")) {
int[] chIds = parameter().getIntArray("ch_ids");
List<Integer> tdChIds = toIntList(chIds);
List<Integer> weChIds = Extract.getWeppIdList(chIdMap, tdChIds, "Channel");
putStats("AOI scaled WS Channel sediment yield (tonne)", summary.getChannelSedYield(weChIds)); // col 10
putStats("AOI scaled WS Channel Soil Loss (kg)", summary.getChannelSoilLoss(weChIds)); // col 8
} else if (parameter().has("hs_ids")) {
int[] hsIds = parameter().getIntArray("hs_ids");
List<Integer> tdHsIds = toIntList(hsIds);
List<Integer> weHsIds = Extract.getWeppIdList(hsIdMap, tdHsIds, "Hillslope");
putStats("AOI scaled WS Hillslope sediment yield (kg)", summary.getHillslopeSedYield(weHsIds)); // col 5
putStats("AOI scaled WS Hillslope Soil Loss (kg)", summary.getHillslopeSoilLoss(weHsIds)); // col 7
// col 11 = 10 + 5
// 15 = ac of the intersection
} else {
// complete output.
putStats("tot_contrib_area", summary.getTotContribAreaStats());
putStats("tot_precip_vol", summary.getTotPrecipVolStats());
putStats("tot_irr_vol", summary.getTotIrrVolStats());
putStats("tot_water_discharge", summary.getTotWaterDischargeStats());
putStats("tot_channel_sloss", summary.getTotChannelSlossStats());
putStats("tot_sed_discharge", summary.getTotSedDischargeStats());
putStats("sed_deliv_unit_area", summary.getSedDelivUnitAreaStats());
putStats("sed_deliv_ratio", summary.getSedDelivRatioStats());
List<Channel> chs = summary.getChannels();
results().put("channel_count", chs.size());
for (int i = 0; i < chs.size(); i++) {
Channel ch = chs.get(i);
putStats("channel_" + (i + 1) + "_disch_vol", ch.getDischVolStats());
putStats("channel_" + (i + 1) + "_sed_yield", ch.getSedYieldStats());
putStats("channel_" + (i + 1) + "_soil_loss", ch.getSoilLossStats());
putStats("channel_" + (i + 1) + "_upland_charge", ch.getUplandChargeStats());
putStats("channel_" + (i + 1) + "_ssurfflow_vol", ch.getSsurfflowVolStats());
}
List<Hillslope> hss = summary.getHillslopes();
results().put("hillslope_count", hss.size());
for (int i = 0; i < hss.size(); i++) {
Hillslope hs = hss.get(i);
putStats("hillslope_" + (i + 1) + "_runoff_vol", hs.getRunoffVolStats());
putStats("hillslope_" + (i + 1) + "_subrunoff_vol", hs.getSubRunoffVolStats());
putStats("hillslope_" + (i + 1) + "_soil_loss", hs.getSoilLossStats());
putStats("hillslope_" + (i + 1) + "_soil_dep", hs.getSedDepStats());
putStats("hillslope_" + (i + 1) + "_sed_yield", hs.getSedYieldStats());
}
}
}
static List<Integer> toIntList(int[] i) {
return Arrays.stream(i).boxed().collect(Collectors.toList());
}
private void putStats(String name, DoubleSummaryStatistics s) {
results().put(name, new double[]{s.getMin(), s.getAverage(), s.getMax()});
}
}