@@ -4,24 +4,16 @@ |
*/ |
package m.oms.ages; |
|
-import csip.Executable; |
import csip.ModelDataService; |
-import csip.ServiceException; |
import csip.annotations.*; |
import static csip.annotations.ResourceType.*; |
-import csip.utils.Binaries; |
import java.io.File; |
-import java.io.FilenameFilter; |
-import java.io.IOException; |
-import java.util.Arrays; |
import java.util.HashMap; |
import java.util.Map; |
import javax.ws.rs.Path; |
-import static m.oms.ages.V1_0.ID_AGES_JAR; |
-import org.apache.commons.io.FileUtils; |
-import org.apache.commons.io.IOCase; |
-import org.apache.commons.io.filefilter.WildcardFileFilter; |
-import org.apache.commons.lang.ArrayUtils; |
+import static m.oms.ages.V1_0.KEY_SCRIPT; |
+import oms.utils.Utils; |
+import oms3.util.Statistics; |
|
/** |
* Ages service. |
@@ -32,127 +24,62 @@ |
@Description("OMS based AGES model service") |
@VersionInfo("1.0") |
@Path("m/ages/1.0") |
-@Resource(file = "/bin/ages/ages.jar", type = JAR, id = ID_AGES_JAR) |
+@Resource(file = "/bin/ages/ages.jar", type = JAR, id = Utils.ID_AGES_JAR) |
@Resource(file = "/bin/ages/ages-lib.zip", type = ARCHIVE) |
@Resource(file = "/bin/ages/ages-static.zip", type = ARCHIVE) |
@Resource(file = "java-*-std*.txt output/csip_run/out/*.csv", type = OUTPUT) |
-@Resource(file = "${csip.dir}/bin/ages/simulation/ages.sim", type = REFERENCE, id = "ages.sim") |
+@Resource(file = "${csip.dir}/bin/ages/simulation/ages.sim", type = REFERENCE, id = KEY_SCRIPT) |
public class V1_0 extends ModelDataService { |
|
- // parameter keys |
- static final String KEY_SCRIPT = "ages.sim"; |
- static final String KEY_LOGLEVEL = "loglevel"; |
- static final String KEY_OPTIONS = "java.options"; |
+ public static final String KEY_SCRIPT = "ages.sim"; |
+ public static final String RUN_INC = "run.inc"; |
|
- static final String ID_AGES_JAR = "ages.jar"; |
- static final String RUN_INC = "run.inc"; |
+ public static final String PAR_STARTTIME = "startTime"; |
+ public static final String PAR_ENDTIME = "endTime"; |
|
|
@Override |
public void doProcess() throws Exception { |
- String dsl = parameter().getString(KEY_SCRIPT, resources().getFile(KEY_SCRIPT).toString()); |
+ String dsl = parameter().getString(KEY_SCRIPT, |
+ resources().getFile(KEY_SCRIPT).toString()); |
|
// pass request param to model runtime parameter -> run.inc |
- Map<String, String> runParam = new HashMap<>(); |
- passReqQuotedParam(runParam, "startTime"); |
- passReqQuotedParam(runParam, "endTime"); |
+ Map<String, String> agesParam = new HashMap<>(); |
+ Utils.passReqQuotedParam(agesParam, parameter(), PAR_STARTTIME, PAR_ENDTIME); |
+ Utils.passOptParam(agesParam, parameter(), "flowRouteTA", "SoilOutLPS"); |
+ Utils.createParamInclude(agesParam, getWorkspaceFile(RUN_INC)); |
|
- passOptParam(runParam, "flowRouteTA"); |
- passOptParam(runParam, "SoilOutLPS"); |
- |
- createParamInclude(runParam); |
+ String start = parameter().getString(PAR_STARTTIME); |
+ String end = parameter().getString(PAR_ENDTIME); |
|
File d = new File(dsl); |
if (!(d.isAbsolute() && d.exists())) { |
d = getWorkspaceFile(dsl); |
} |
- runAges(d); |
-// results().put(getWorkspaceFile("output")); |
- } |
- |
- |
- /** |
- * pass a required parameter, quoted (string). |
- */ |
- private void passReqQuotedParam(Map<String, String> p, String name) throws ServiceException { |
- p.put(name, "\"" + parameter().getString(name) + "\""); |
- } |
- |
- |
- /** |
- * pass optional parameter, no quotes. |
- */ |
- private void passOptParam(Map<String, String> p, String name) throws ServiceException { |
- if (parameter().has(name)) { |
- p.put(name, parameter().getString(name)); |
- } |
- } |
- |
- |
- /** |
- * create a 'sim' include file for the run part. |
- */ |
- private void createParamInclude(Map<String, String> p) throws IOException { |
- StringBuilder b = new StringBuilder(); |
- b.append("parameter {\n"); |
- p.keySet().forEach((name) -> { |
- b.append(" ").append(name).append(" ").append(p.get(name)).append("\n"); |
- }); |
- b.append("}\n"); |
- FileUtils.writeStringToFile(getWorkspaceFile(RUN_INC), b.toString()); |
- } |
- |
- |
- /** |
- * Run Ages |
- * |
- * @param dsl |
- * @param options |
- * @throws Exception |
- */ |
- private void runAges(File dsl) throws Exception { |
|
getWorkspaceFile("output").mkdirs(); |
getWorkspaceFile("logs").mkdirs(); |
|
- // Create/execute a Ages. |
- Executable p = createProcess(dsl); |
- int result = p.exec(); |
- if (result != 0) { |
- FilenameFilter ff = new WildcardFileFilter("java*stderr.txt", IOCase.INSENSITIVE); |
- File[] f = getWorkspaceDir().listFiles(ff); |
- if (f != null && f.length > 0) { |
- String err = FileUtils.readFileToString(f[0]); |
- LOG.info("Ages execution error. " + f[0] + ":\n" + err); |
- throw new ServiceException("Ages execution error. " + f[0] + ":\n" + err); |
- } |
- throw new ServiceException("Ages execution error." + result); |
+ Utils.runAges(d, getWorkspaceDir(), parameter(), resources(), LOG); |
+ |
+ if (parameter().has("obs") && parameter().has("sim")) { |
+ String obs = parameter().getString("obs"); |
+ String sim = parameter().getString("sim"); |
+ |
+ double missing = parameter().getDouble("missing", -9999d); |
+ |
+ // e.g. obs_data02_14.csv/obs/orun[1] |
+ double[] obsData = Utils.getData(obs, getWorkspaceDir(), start, end); |
+ |
+ // e.g. output/csip_run/out/Outlet.csv/output/catchmentSimRunoff |
+ double[] simData = Utils.getData(sim, getWorkspaceDir(), start, end); |
+ |
+ results().put("kge", Statistics.kge(obsData, simData, missing)); |
+ results().put("ns", Statistics.nashSutcliffe(obsData, simData, 2.0, missing)); |
+// results().put("nslog", Statistics.nashSutcliffeLog(obsData, simData, 2.0, missing)); |
} |
+ |
+// results().put(getWorkspaceFile("output")); |
} |
|
- |
- /** |
- * Create the external Ages process. |
- */ |
- private Executable createProcess(File dsl) throws Exception { |
- |
- Map<String, String> sysprops = new HashMap(); |
- sysprops.put("oms_prj", getWorkspaceDir().toString()); |
- sysprops.put("csip_ages", resources().getFile(ID_AGES_JAR).getParent()); |
- |
- String[] jvmOptions = Binaries.asSysProps(sysprops); |
- String options = parameter().getString(KEY_OPTIONS, ""); |
- if (options != null && !options.isEmpty()) { |
- jvmOptions = (String[]) ArrayUtils.addAll(jvmOptions, options.split("\\s+")); |
- } |
- |
- // java -Doms_prj=. -cp "dist/AgES.jar" oms3.CLI -l OFF -r "projects/sfir30/simulation/sfir30.sim" |
- return Binaries.getResourceOMSDSL( |
- dsl, // the dsl file to run |
- jvmOptions, // jvm options |
- getWorkspaceDir(), // workspace dir |
- Arrays.asList(resources().getFile(ID_AGES_JAR)), // the ages jar file |
- parameter().getString(KEY_LOGLEVEL, "INFO"), // The log level |
- LOG); // This session logger |
- } |
} |