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

import csip.Config;
import csip.Executable;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import java.io.File;
import oms3.annotations.*;

import javax.ws.rs.*;
import static m.windgen.V1_0.PARMITEMOUTPUTFILE;
import static m.windgen.V1_0.PARMITEMUSEINTERNAL;

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

@Resource(file = "/bin/win-x86/interpolate.exe", wine = true, id = "interpolateExeWinID", type = EXECUTABLE)
@Resource(file = "/d/interpolateData/interpolateData.zip", id = "interpolateDataID", type = ARCHIVE)
@Resource(file = "*stdout.txt *stderr.txt", type = OUTPUT)

/*
    interpolate.zip includes:
        interpolation_boundary.pol
    which are automatically unzipped by CSIP and can be found at:
        Config.getString("csip.dir") + "/d/interpolate/interpolation_boundary.pol"
    Also need (possibly):
        wind_gen_his_upper_US.idx from /d/windgen/windgen.zip
  
*/
/*
    @Resource(file = "/d/windgenData/windgenData.zip", id = "windgenDataID", type = ARCHIVE),
    windgenData.zip includes:
        wind_gen_his_upper_US.idx
        wind_gen_his_upper_US.wdb
        wind_gen_his_upper_US_NRCS.idx
    which are automatically unzipped by CSIP and can be found at (ex.):
        Config.getString("csip.dir") + "/d/windgen/wind_gen_his_upper_US.idx"
  
*/

public class V1_0 extends ModelDataService {
        
    String parmOutputFile;
    String version = "CSIP weps: v 0.0.3";
        
    @Override
    public void doProcess() throws Exception {
        
        Executable interpolate = getResourceExe("interpolateExeWinID");

        String parmIdxFile = getStringParam("idxFile", "wind_gen_his_upper_US.idx");
        //if (getParamInternalVal("idxFile").contentEquals("true")) {
        boolean b;
        try {
            b = getParamMap().get("idxFile").getString(PARMITEMUSEINTERNAL).contentEquals("true");
        } catch (Exception ex) {
            b = false;
        }
        if (b) {
            // if this param object contains the key "useInternal" and its value is true, then use the internal file
            parmIdxFile = Config.getString("csip.dir") + "/d/windgenData/" + parmIdxFile;
        } else {
            parmIdxFile = getWorkspaceDir() + "/" + parmIdxFile;
        }
        
        String parmPolygonFile = getStringParam("polygonFile", "interpolation_boundary.pol");
        //if (getParamInternalVal("polygonFile").contentEquals("true")) {
        try {
            b = getParamMap().get("polygonFile").getString(PARMITEMUSEINTERNAL).contentEquals("true");
        } catch (Exception ex) {
            b = false;
        }
        if (b) {
            // if this param object contains the key "useInternal" and its value is true, then use the internal file
            parmPolygonFile = Config.getString("csip.dir") + "/d/interpolateData/" + parmPolygonFile;
        } else {
            parmPolygonFile = getWorkspaceDir() + "/" + parmPolygonFile;
        }
        
        parmOutputFile = getStringParam(PARMITEMOUTPUTFILE, "weights.txt");
        //checkParmExists("lat");
        if (!hasParam("lat")) {
            throw new ServiceException ("File does not exist, filename=" + "lat");
        }
        String parmLat = Double.toString(getDoubleParam("lat"));
        //checkParmExists("lon");
        if (!hasParam("lon")) {
            throw new ServiceException ("File does not exist, filename=" + "lon");
        }
        String parmLon = Double.toString(getDoubleParam("lon"));

        // NOTE: this service .exe is very precise w/ input formats.
        //       Below is the only working format that I could find. Caveat Lector.
        
        interpolate.setArguments(
                            "-f",
                            parmIdxFile,
                            "-p",
                            parmPolygonFile,
                            "-lat",
                            parmLat,
                            "-lon",
                            parmLon,
                            "-o",
                            parmOutputFile
        );
        
      
        interpolate.exec();
    }
    
    @Override
    protected void postProcess () throws Exception {
        putResult(new File(getWorkspaceDir() + "/" + parmOutputFile));
    }
}