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

import csip.Config;
import static csip.Config.CSIP_DATA_DIR;
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.WindIdxFileName;
import static m.windgen.V1_0.WindIdxNrcsFileName;

/**
 * This implements the CSIP WINDGEN station data interpolation utility service.
 *
 * @author od, mh
 */
@Name("Station Interpolation")
@Description("Station interpolation utility for Windgen")
@VersionInfo("1.0")
@Path("m/windgen/interpolate/1.0")
@Polling(first = 2000, next = 1000)

@Resource(file = "/bin/lin-amd64/interpolate", id = "interpolateExeLin64ID", 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 WindBoundaryFileName = "interpolation_boundary.pol";
    public static final String WindInterpDefOutputname = "weights.txt";

    File parmOutputFile;

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

  @Override
  public void doProcess() throws Exception {
    Executable interpolate;
    interpolate = resources().getExe("interpolateExeLin64ID");

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

    double lat = parameter().getDouble(PARMITEM_INTERP_LAT);
    double lon = parameter().getDouble(PARMITEM_INTERP_LON);
    
    Boolean isNRCS = parameter().getBoolean(PARMITEMISNRCS, false);
    String idxFileName = (isNRCS) ? WindIdxNrcsFileName : WindIdxFileName;

    // default to the internal file path
    String parmDBFilePath = Config.getString(CSIP_DATA_DIR);
    if (parameter().has(PARMITEMIDXFILE)) {
      // default, if parm PARMITEMDATAFILE is used, is external file
      parmDBFilePath = workspace().getDir().getAbsolutePath();
    }
    String parmIdxFile = parmDBFilePath + "/" + parameter().getString(PARMITEMIDXFILE, idxFileName);
    if (!(new File(parmIdxFile).exists())) {
      throw new ServiceException("File does not exist, filename=" + parmIdxFile);
    }

    parmDBFilePath = Config.getString(CSIP_DATA_DIR);
    if (parameter().has(PARMITEMPOLYGONFILE)) {
      // default, if parm PARMITEMDATAFILE is used, is external file
      parmDBFilePath = workspace().getDir().getAbsolutePath();
    }
    String parmPolygonFile = parmDBFilePath + "/" + parameter().getString(PARMITEMPOLYGONFILE, WindBoundaryFileName);
    if (!(new File(parmPolygonFile).exists())) {
      throw new ServiceException("File does not exist, filename=" + parmPolygonFile);
    }
    String parmOutputFileName = parameter().getString(PARMITEMOUTPUTFILE, WindInterpDefOutputname);
    parmOutputFile = interp.interpolate(lat, lon, parmIdxFile, parmPolygonFile, parmOutputFileName);
  }


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