V1_0.java [src/java/m/windgen/interp_wdb] Revision: default  Date:
package m.windgen.interp_wdb;

import csip.Config;
import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import interpolation.Interpolation;
import java.io.File;

import javax.ws.rs.*;
import static m.wepsModelConstants.*;
import static m.windgen.V1_0.WindDBFileName;

/**
 * This implements the CSIP INTERP_WDB service : WINDGEN Generate interpolated
 * data support service.
 *
 * @author od, mh
 */
@Name("StationGenData")
@Description("Station interpolation data generation utility")
@VersionInfo("1.0")
@Path("m/windgen/interp_wdb/1.0")
@Polling(first = 500, next = 300)

@Resource(file = "/bin/lin-amd64/interp_wdb", id = "interp_wdb", type = EXECUTABLE)
//@Resource(file = "/data/windgenData052722.zip", id = "windgenDataID", type = ARCHIVE)
@Resource(file = "/data/windgenData111522.zip", id = "windgenDataID", type = ARCHIVE)
@Resource(file = "*stdout.txt *stderr.txt", type = OUTPUT)

/*
    windgenData052722.zip includes:
        interpolation_boundary.pol
        windgen.idx
        windgen_NRCS.idx
        windgen.wdb
*/

public class V1_0 extends ModelDataService {

  public static final String PARMITEMSTATIONDATA = "stationData";
  
  File parmWdbFile;
  String parmDBFileName;

  static final String serviceUpdateStr = "04/17/2022";

  @Override
  public void doProcess() throws Exception {

    Executable interp_wdb = resources().getExe("interp_wdb");
    // Ex. call syntax
    // bin/Windows/interp_wdb.exe test.wdb 724517 0.7576871450528201 724518 0.1762846085406619 724510 6.602824640651805E-02

    Interpolation interp = new Interpolation(null, interp_wdb, workspace().getDir(), LOG);

    String parmWdbFileName = parameter().getString(PARMITEMOUTPUTFILE, "test.wdb");
    String parmWeightsInFile = parameter().getString(PARMITEM_WDB_WEIGHTSFILE, "");

    String getStaDataIdx1 = parameter().getString(PARMITEM_WDB_IDX1, "");
    String parmIn1Weight = parameter().getString(PARMITEM_WDB_WEIGHT1, null);

    String getStaDataIdx2 = parameter().getString(PARMITEM_WDB_IDX2, "");
    String parmIn2Weight = parameter().getString(PARMITEM_WDB_WEIGHT2, null);

    String getStaDataIdx3 = parameter().getString(PARMITEM_WDB_IDX3, "");
    String parmIn3Weight = parameter().getString(PARMITEM_WDB_WEIGHT3, null);

    String parmIn1File = parameter().getString(PARMITEM_WDB_INFILE1, null);
    String parmIn2File = parameter().getString(PARMITEM_WDB_INFILE2, null);
    String parmIn3File = parameter().getString(PARMITEM_WDB_INFILE3, null);

    // The data can come from one of 3 sources:
    // 1. The internal database include with this endpoint
    // 2. A supplied data file included with this request
    // 3. Specified as individual file records with the inputFile1.2.3 parms
    //if ( hasParam(PARMITEMDATAFILE) ) {
    if (parameter().has(PARMITEMDATAFILE)) {
      if (parameter().getParamJSON(PARMITEMDATAFILE).getString(PARMITEMUSEINTERNAL).contentEquals("true")) {
        String parmDataInFile = parameter().getString(PARMITEMDATAFILE, "");
        // if useInternal, then get internal file
        if (parmDataInFile.length() == 0) {
          parmDataInFile = WindDBFileName;
        }
        parmDBFileName = Config.getString("csip.dir") + "/data/" + parmDataInFile;
      } else {
        throw new ServiceException("CSIP windgen interp_wdb:need to specify all 3 inputFile parms OR a datafile");
      }
    }
    parmWdbFile = interp.createInterpolatedWdb(parmWeightsInFile, parmDBFileName, getStaDataIdx1, 
        parmIn1Weight, getStaDataIdx2, parmIn2Weight, getStaDataIdx3, parmIn3Weight, 
        parmIn1File, parmIn2File, parmIn3File, parmWdbFileName);
  }


  @Override
  protected void postProcess() throws Exception {
    metainfo().put(PARMOUTUPDATED, serviceUpdateStr);
    results().put(parmWdbFile);
  }
}