V1_01cp.java [src/java/m/oms/ages] Revision: 24806d3b23f178f1e79f3afe29e5d88542b3d45c  Date: Tue Feb 06 09:21:06 MST 2024
/*
 * 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 csip.cosu.ObjFunc;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import static m.oms.Resources.LOCATION_MNT_DATA;
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, new ET-crop related override")
@VersionInfo("1.01cp")
@Path("m/ages/1.0.1cp")
@Polling(first = 4000, next = 3500)
@Resource(file = "/bin/ages_1_0_cropID/ages.jar", type = JAR, id = Utils.ID_AGES_JAR)
@Resource(file = "/bin/ages_1_0_cropID/ages-lib.zip", type = ARCHIVE)
@Resource(file = "java-*-std*.txt output/csip_run/out/*.csv", type = OUTPUT)

public class V1_01cp extends ModelDataService {

    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 EVAL_STARTTIME = "eva_startTime";
    public static final String EVAL_ENDTIME = "eva_endTime";

    public static final String PAYLOAD = "payload";
    public static final String PROJECT = "project";

    public static final String AGESCOMPILE = "ages_1_0";

    public String data_folder = "";
    boolean payLoad = false;
    public static File dataFolder = null;

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

        if (!payLoad) {
            if (!project.isEmpty()) {
                // /mnt/newCeph/csip-data/csip-oms/ages_projects/ages_1_0/
                // /mnt/newCeph/csip-data/csip-oms/ages_projects/ages_1_0/SFIR3/simulation/
                // dataFolder = !payLoad ? new File(LOCATION_MNT_DATA + "/ages_projects/ages_1_0/ages-static/data/" + data_folder) : workspace().getDir();
                // dsl = parameter().getString(KEY_SCRIPT_NP3, resources().getFile(KEY_SCRIPT_NP3).toString());
                dsl = (LOCATION_MNT_DATA + "/ages_projects/ages_1_0/" + project + "/simulation/ages.sim");
                // System.out.println(" +++++++++++ DSL :  " + dsl);
                // +++++++++++ DSL :  /mnt/newCeph/csip-data/csip-oms/ages_projects/ages_1_0/ages-static/simulation/ages_noPLSFIR3.sim
            }
        }

        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_1_0/data/" + data_folder) : workspace().getDir();
        //if (project.equals("SFIR3")) {
        dataFolder = !payLoad ? new File(LOCATION_MNT_DATA + "/ages_projects/ages_1_0/" + project + "/data/") : workspace().getDir();
        System.out.println(dataFolder);
        //}

        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.runAgesNew(d, workspace().getDir(), parameter(), resources(), AGESCOMPILE, 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);
            // e.g. output/csip_run/out/Outlet.csv/output/catchmentSimRunoff
            double[] simData = Utils.getData(data[1], workspace().getDir(), cal_start, cal_end);
            double result = of.eval(obsData, simData, parameter().getDouble("missing", -9999d));
            result = Double.isNaN(result) ? -999.0 : result;

            cosu().setValue(name, result);
        }
    }
}