Utils.java [src/java/oms/utils] Revision: 7f572734ec2cbb2b8622becca4dec8dba2f4ba1d  Date: Thu Mar 05 10:35:07 MST 2020
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package oms.utils;

import csip.Executable;
import csip.PayloadParameter;
import csip.ServiceException;
import csip.ServiceResources;
import csip.SessionLogger;
import csip.utils.Binaries;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import oms3.Conversions;
import oms3.io.CSTable;
import oms3.io.DataIO;
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;

/**
 *
 * @author od
 */
public class Utils {

  // parameter keys
  public static final String KEY_LOGLEVEL = "loglevel";
  public static final String KEY_OPTIONS = "java.options";

  public static final String ID_AGES_JAR = "ages.jar";


  /**
   * create a 'sim' include file for the run part.
   */
  public static void createParamInclude(Map<String, String> p,
      File file) 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(file, b.toString());
  }


  /**
   * pass a required parameter, quoted (string).
   */
  public static void passReqQuotedParam(Map<String, String> p,
      PayloadParameter param, String... names) throws ServiceException {
    for (String name : names) {
      p.put(name, "\"" + param.getString(name) + "\"");
    }
  }


  /**
   * pass optional parameter, no quotes.
   */
  public static void passOptParam(Map<String, String> p,
      PayloadParameter param, String... names) throws ServiceException {
    for (String name : names) {
      if (param.has(name)) {
        p.put(name, param.getString(name));
      }
    }
  }


  public static void passOptQuotedParam(Map<String, String> p,
      PayloadParameter param, String... names) throws ServiceException {
    for (String name : names) {
      if (param.has(name)) {
        p.put(name, "\"" + param.getString(name) + "\"");
      }
    }
  }


  /**
   * Run Ages
   *
   * @param dsl
   * @param options
   * @throws Exception
   */
  public static void runAges(File dsl, File ws, PayloadParameter param,
      ServiceResources res, SessionLogger LOG) throws Exception {

    // Create/execute a Ages.
    Executable p = createProcess(dsl, ws, param, res, LOG);
    int result = p.exec();
    if (result != 0) {
      FilenameFilter ff = new WildcardFileFilter("java*stderr.txt", IOCase.INSENSITIVE);
      File[] f = ws.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);
    }
  }


  /**
   * Create the external Ages process.
   */
  public static Executable createProcess(File dsl, File ws,
      PayloadParameter param, ServiceResources res, SessionLogger LOG) throws Exception {

    Map<String, String> sysprops = new HashMap();
    sysprops.put("oms_prj", ws.toString());
    sysprops.put("csip_ages", res.getFile(ID_AGES_JAR).getParent());

    String[] jvmOptions = Binaries.asSysProps(sysprops);
    String options = param.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
        ws, // workspace dir
        Arrays.asList(res.getFile(ID_AGES_JAR)), // the ages jar file 
        param.getString(KEY_LOGLEVEL, "INFO"), // The log level
        LOG); // This session logger
  }

  private static final int FILE = 0;
  private static final int TABLE = 1;
  private static final int COLUMN = 2;


  /**
   *
   * @param d
   * @param workspace
   * @param start
   * @param end
   * @return
   * @throws IOException
   */
  public static double[] getData(String d, File workspace,
      String start, String end) throws IOException {
    if (d == null) {
      throw new IllegalArgumentException("Missing data property: " + d);
    }
    String[] parts = DataIO.parseCsvFilename(d);
    if (parts.length != 3) {
      throw new IllegalArgumentException("invalid: " + d + " expected:: <file>/<table>/<column>");
    }

//    System.out.println(Arrays.toString(parts));
    CSTable t = DataIO.table(new File(workspace, parts[FILE]), parts[TABLE]);

    Date startDate = Conversions.convert(start, Date.class);
    Date endDate = Conversions.convert(end, Date.class);

//    System.out.println(startDate);
//    System.out.println(endDate);
    double[] vals = DataIO.getColumnDoubleValuesInterval(startDate, endDate, t,
        parts[COLUMN], DataIO.DAILY);

    return vals;
  }


  public static void main(String[] args) throws IOException {
    // small test
    double[] d = getData("obs_data02_14.csv/obs/orun[1]", new File("/tmp"), "2002-01-18", "2002-02-18");
    System.out.println(Arrays.toString(d));
  }

}