V1_01.java [src/java/m/oms/ages] Revision: 7f4e13a95321fd34bfb19114c2d41bfede5490b4  Date: Wed Aug 16 15:50:26 MDT 2023
/*
 * 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.V1_0.*;
import oms.utils.Utils;
import static oms.utils.Utils.LOCATION_MNT_DATA;

/**
 * Ages service.
 *
 * @author od
 */
@Name("AGES model execution")
@Description("OMS based AGES model service")
@VersionInfo("1.01")
@Path("m/ages/1.0.1")
@Polling(first = 4000, next = 3500)
@Resource(file = "/bin/ages_1_0/ages.jar", type = JAR, id = Utils.ID_AGES_JAR)
@Resource(file = "/bin/ages_1_0/ages-lib.zip", type = ARCHIVE)
@Resource(file = "java-*-std*.txt output/csip_run/out/*.csv", type = OUTPUT)

public class V1_01 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 PAYLOAD = "payload";
    public static final String PROJECT = "project";

    public String data_folder = "";
    boolean payLoad = false;

    // required parameter
    String[] reqParams = {
        PAR_STARTTIME,
        PAR_ENDTIME
    };

    // optional parameter
    String[] optParams = {
        "ACAdaptation",
        "baseDepth",
        "BetaW",
        "Beta_min",
        "Beta_rsd",
        "DCAdaptation",
        "FCAdaptation",
        "Ksink",
        "LExCoef",
        "N_delay_RG1",
        "N_delay_RG2",
        "a_rain",
        "a_snow",
        "angstrom_a",
        "angstrom_b",
        "baseTemp",
        "calib_clat_fact",
        "ccf_factor",
        "cn_froz",
        "icn",
        "r2adj",
        "fcimp",
        "meanCNFactor",
        "meanKfFactor",
        "defaultCO2",
        "denitfac",
        "delayNitrification",
        "denitrificationRateCoefficient",
        "denitrificationSoilSaturationThreshold",
        "depdr",
        "deposition_factor",
        "drrad",
        "drspac",
        "f1",
        "f12",
        "f13",
        "f14",
        "f15",
        "f16",
        "f17",
        "f18",
        "f19",
        "f2",
        "f3",
        "f4",
        "f5",
        "f6",
        "f7",
        "f8",
        "flowRouteTA",
        "fmt_date",
        "fmt_double",
        "g_factor",
        "geoMaxPerc",
        "gwCapRise",
        "gwRG1Fact",
        "gwRG1RG2dist",
        "gwRG2Fact",
        "halflife_RG1",
        "halflife_RG2",
        "infil_conc_factor",
        "initLPS",
        "initMPS",
        "initN_concRG1",
        "initN_concRG2",
        "initRG1",
        "initRG2",
        "kdiff_layer",
        "kf_calib",
        "kf_fact",
        "lagSurfaceRunoff",
        "lagInterflow",
        "locGrw",
        "longTZ",
        "nitri_delay",
        "nitrificationSoilTemperatureThreshold",
        "nitrificationSurfaceTemperatureThreshold",
        "opti",
        "piadin",
        "r_factor",
        "rootfactor",
        "sceno",
        "skipRegression",
        "snowCritDens",
        "snowDensConst",
        "snowFactorA",
        "snowFactorB",
        "snowFactorC",
        "snow_trans",
        "snow_trs",
        "soilDiffMPSLPS",
        "soilDistMPSLPS",
        "soilImpGT80",
        "soilImpLT80",
        "soilLatVertLPS",
        "soilLinRed",
        "soilMaxDPS",
        "soilMaxInfSnow",
        "soilMaxInfSummer",
        "soilMaxInfWinter",
        "soilMaxPerc",
        "soilOutLPS",
        "soilPolRed",
        "t_factor",
        "tempRes",
        "temp_lag"
    };

    // simulation flags
    String[] flags = {
        "flagParallel",
        "flagRegionalization",
        "flagHRURouting",
        "flagReachRouting",
        "flagSort",
        "flagSplit",
        "flagInfiltration",
        "flagTillage",
        "flagTileDrain",
        "flagUPGM",
        "parallelismFactor",
        "parallelismThreads",
        "flagSaveState",
        "flagLoadState",
        "flagWB",
        "loadStateFilePath",
        "dataStartTime",
        "dataEndTime"
    };

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

        // 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")) {
        File dataFolder = !payLoad ? new File(LOCATION_MNT_DATA + "ages_projects/ages_1_0/" + project + "/data/") : 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.runAgesNew(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);
            // 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);
        }
    }
}