V1_0.java [src/java/m/oms/ages] Revision: 143d3778c416433234280f8311b122676b40a217  Date: Fri Feb 28 21:28:04 MST 2020
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.oms.ages;

import csip.ModelDataService;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Path;
import static m.oms.ages.V1_0.KEY_SCRIPT;
import oms.utils.Utils;
import oms3.util.Statistics;

/**
 * Ages service.
 *
 * @author od
 */
@Name("AGES model execution")
@Description("OMS based AGES model service")
@VersionInfo("1.0")
@Path("m/ages/1.0")
@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 = KEY_SCRIPT)
public class V1_0 extends ModelDataService {

  public static final String KEY_SCRIPT = "ages.sim";
  public 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());

    // pass request param to model runtime parameter -> run.inc
    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));

    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);
    }

    getWorkspaceFile("output").mkdirs();
    getWorkspaceFile("logs").mkdirs();

    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"));
  }

}