V1_0.java [src/java/m/oms/ages] Revision: 73908a55f02bbc055a05f3057e316d58fc42e9fe  Date: Thu Mar 05 10:19:42 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.ServiceException;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.Path;
import static m.oms.ages.V1_0.KEY_SCRIPT;
import ngmf.util.cosu.luca.of.KGE;
import ngmf.util.cosu.luca.of.NS;
import ngmf.util.cosu.luca.of.NS2LOG;
import ngmf.util.cosu.luca.of.RMSE;
import ngmf.util.cosu.luca.of.TRMSE;
import oms.utils.Utils;
import oms3.ObjectiveFunction;

/**
 * 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";

  static Map<String, ObjectiveFunction> ofs = new HashMap<>();


  static {
    ofs.put("kge", new KGE());
    ofs.put("ns", new NS());
    ofs.put("nslog", new NS2LOG());
    ofs.put("rmse", new RMSE());
    ofs.put("trmse", new TRMSE());
  }


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

    for (String ofName : getRequestedObjfunc(ofs.keySet())) {
      String[] data = parameter().getStringArray(ofName);
      double v = calc_of(ofs.get(ofName), data[0], data[1], start, end);
      results().put(ofName, v);
    }
//  results().put(getWorkspaceFile("output"));
  }


  private List<String> getRequestedObjfunc(Set<String> names) {
    List<String> l = new ArrayList<>();
    for (String ofName : names) {
      if (parameter().has(ofName)) {
        l.add(ofName);
      }
    }
    return l;
  }


  private double calc_of(ObjectiveFunction of, String obs, 
      String sim, String start, String end) throws IOException, ServiceException {
    // 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);
    return of.calculate(obsData, simData, parameter().getDouble("missing", -9999d));
  }

}