V0_3.java [src/java/m/oms/ages] Revision: default Date:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package m.oms.ages;
import csip.Config;
import csip.ModelDataService;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import csip.cosu.ObjFunc;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import static m.oms.ages.V0_3.*;
import static m.oms.Resources.flags;
import static m.oms.Resources.optParams;
import static m.oms.Resources.outputs;
import oms.utils.Utils;
/**
* Ages service.
*
* @author od
*/
@Name("AGES model execution")
@Description("OMS based AGES model service")
@VersionInfo("0.3")
@Path("m/ages/0.3.0")
@Polling(first = 5000, next = 4000)
@Resource(file = "/bin/ages_0_3/ages.jar", type = JAR, id = Utils.ID_AGES_JAR)
@Resource(file = "/bin/ages_0_3/ages-lib.zip", type = ARCHIVE)
@Resource(file = "/bin/ages_0_3/ages-static.zip", type = ARCHIVE)
@Resource(file = "java-*-std*.txt output/csip_run/out/*.csv", type = OUTPUT)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages.sim", type = REFERENCE, id = KEY_SCRIPT)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPL.sim", type = REFERENCE, id = KEY_SCRIPT_NP)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLSFIR3.sim", type = REFERENCE, id = KEY_SCRIPT_NP3)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLSFIR30.sim", type = REFERENCE, id = KEY_SCRIPT_NP30)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLSFIR300.sim", type = REFERENCE, id = KEY_SCRIPT_NP300)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLSFIR3000.sim", type = REFERENCE, id = KEY_SCRIPT_NP3000)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLBDCW.sim", type = REFERENCE, id = KEY_SCRIPT_NPBDCW)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLEAGLE.sim", type = REFERENCE, id = KEY_SCRIPT_NPEAGLE)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE58.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE58)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE51.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE51)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE27.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE27)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE27s1.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE27s1)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE27s4.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE27s4)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE27s5.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE27s5)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE51s1.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE51s1)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE58s1.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE58s1)
@Resource(file = "${csip.dir}/bin/ages_0_3/simulation/ages_noPLDRAKE58s5.sim", type = REFERENCE, id = KEY_SCRIPT_NPDRAKE58s5)
public class V0_3 extends ModelDataService {
public static final String KEY_SCRIPT = "ages.sim";
public static final String KEY_SCRIPT_NP = "ages_noPL.sim"; // no payload script
public static final String KEY_SCRIPT_NP3 = "ages_noPLSFIR3.sim"; // no payload script
public static final String KEY_SCRIPT_NP30 = "ages_noPLSFIR30.sim"; // no payload script
public static final String KEY_SCRIPT_NP300 = "ages_noPLSFIR300.sim"; // no payload script
public static final String KEY_SCRIPT_NP3000 = "ages_noPLSFIR3000.sim"; // no payload script
public static final String KEY_SCRIPT_NPBDCW = "ages_noPLBDCW.sim"; // no payload script
public static final String KEY_SCRIPT_NPEAGLE = "ages_noPLEAGLE.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE58 = "ages_noPLDRAKE58.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE51 = "ages_noPLDRAKE51.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE27 = "ages_noPLDRAKE27.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE27s1 = "ages_noPLDRAKE27s1.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE27s4 = "ages_noPLDRAKE27s4.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE27s5 = "ages_noPLDRAKE27s5.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE51s1 = "ages_noPLDRAKE51s1.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE58s1 = "ages_noPLDRAKE58s1.sim"; // no payload script
public static final String KEY_SCRIPT_NPDRAKE58s5 = "ages_noPLDRAKE58s5.sim"; // no payload script
public static final String RUN_INC = "run.inc";
public static final String PAR_STARTTIME = "startTime";
public static final String PAR_ENDTIME = "endTime";
public static final String CAL_STARTTIME = "cal_startTime";
public static final String CAL_ENDTIME = "cal_endTime";
public static final String PAYLOAD = "payload";
public static final String PROJECT = "project";
public String data_folder = "";
boolean payLoad = false;
// required parameter
String[] reqParams = {
PAR_STARTTIME,
PAR_ENDTIME
};
@Override
public void doProcess() throws Exception {
LOG.info("===> Running AgES ");
payLoad = parameter().getBoolean(PAYLOAD, false);
String project = parameter().getString(PROJECT, "SFIR3");
String dsl = parameter().getString(KEY_SCRIPT, resources().getFile(KEY_SCRIPT).toString());
if (!payLoad) {
dsl = parameter().getString(KEY_SCRIPT_NP, resources().getFile(KEY_SCRIPT_NP).toString());
if (!project.isEmpty()) {
if (project.equals("SFIR3")) {
dsl = parameter().getString(KEY_SCRIPT_NP3, resources().getFile(KEY_SCRIPT_NP3).toString());
}
if (project.equals("SFIR30")) {
dsl = parameter().getString(KEY_SCRIPT_NP30, resources().getFile(KEY_SCRIPT_NP30).toString());
}
if (project.equals("SFIR300")) {
dsl = parameter().getString(KEY_SCRIPT_NP300, resources().getFile(KEY_SCRIPT_NP300).toString());
}
if (project.equals("SFIR3000")) {
dsl = parameter().getString(KEY_SCRIPT_NP3000, resources().getFile(KEY_SCRIPT_NP3000).toString());
}
if (project.equals("BDCW")) {
dsl = parameter().getString(KEY_SCRIPT_NPBDCW, resources().getFile(KEY_SCRIPT_NPBDCW).toString());
}
if (project.equals("EAGLE")) {
dsl = parameter().getString(KEY_SCRIPT_NPEAGLE, resources().getFile(KEY_SCRIPT_NPEAGLE).toString());
}
if (project.equals("DRAKE58")) {
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE58, resources().getFile(KEY_SCRIPT_NPDRAKE58).toString());
}
if (project.equals("DRAKE51")) {
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE51, resources().getFile(KEY_SCRIPT_NPDRAKE51).toString());
}
if (project.equals("DRAKE27")) {
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE27, resources().getFile(KEY_SCRIPT_NPDRAKE27).toString());
}
if (project.equals("DRAKE27s1")) {
project = "DRAKE27";
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE27s1, resources().getFile(KEY_SCRIPT_NPDRAKE27s1).toString());
}
if (project.equals("DRAKE27s4")) {
project = "DRAKE27";
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE27s4, resources().getFile(KEY_SCRIPT_NPDRAKE27s4).toString());
}
if (project.equals("DRAKE27s5")) {
project = "DRAKE27";
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE27s5, resources().getFile(KEY_SCRIPT_NPDRAKE27s5).toString());
}
if (project.equals("DRAKE51s1")) {
project = "DRAKE51";
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE51s1, resources().getFile(KEY_SCRIPT_NPDRAKE51s1).toString());
}
if (project.equals("DRAKE58s1")) {
project = "DRAKE58";
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE58s1, resources().getFile(KEY_SCRIPT_NPDRAKE58s1).toString());
}
if (project.equals("DRAKE58s5")) {
project = "DRAKE58";
dsl = parameter().getString(KEY_SCRIPT_NPDRAKE58s5, resources().getFile(KEY_SCRIPT_NPDRAKE58s5).toString());
}
}
}
data_folder = project;
// pass request param to model runtime parameter -> run.inc
Map<String, String> agesParam = new LinkedHashMap<>();
Utils.passReqQuotedParam(agesParam, parameter(), reqParams);
// scalar parameter
Utils.passOptParam(agesParam, parameter(), optParams);
// optional flags
Utils.passOptQuotedParam(agesParam, parameter(), flags);
// optional flags
Utils.passOptQuotedParam(agesParam, parameter(), outputs);
// data file parameters
File dataFolder = !payLoad ? new File(Config.getString(Config.CSIP_BIN_DIR) + "/ages_0_3/data/" + data_folder) : workspace().getDir();
Utils.processFileParameters(parameter(), dataFolder, workspace().getDir(), agesParam);
// create parameter include file
Utils.createParamInclude(agesParam, workspace().getFile(RUN_INC));
String start = parameter().getString(PAR_STARTTIME);
String end = parameter().getString(PAR_ENDTIME);
// optional parameter
String cal_start = parameter().getString(CAL_STARTTIME, start);
String cal_end = parameter().getString(CAL_ENDTIME, end);
File d = new File(dsl);
if (!(d.isAbsolute() && d.exists())) {
d = workspace().getFile(dsl);
}
workspace().getFile("output").mkdirs();
workspace().getFile("logs").mkdirs();
Utils.runAges(d, workspace().getDir(), parameter(), resources(), LOG);
if (!cosu().isRequested()) {
return;
}
for (String name : cosu().getNames()) {
String[] data = cosu().getData(name);
ObjFunc of = cosu().getObjFunc(name);
double[] obsData = Utils.getData(data[0],
dataFolder, cal_start, cal_end, 0);
// e.g. output/csip_run/out/Outlet.csv/output/catchmentSimRunoff
double[] simData = Utils.getData(data[1], workspace().getDir(), cal_start, cal_end, 0);
double result = of.eval(obsData, simData, parameter().getDouble("missing", -9999d));
result = Double.isNaN(result) ? -999.0 : result;
cosu().setValue(name, result);
}
}
}