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

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.*;

/**
 * This implements the CSIP WINDGEN service.
 *
 * @author od, mh
 */
@Name("windgen")
@Description("wind data generation utility")
@VersionInfo("1.0")
@Path("m/windgen/1.0")
@Polling(first = 100, next = 100)

@Resource(file = "/bin/win-x86/wind_gen4.exe", wine = true, id = "windgen", type = EXECUTABLE)
@Resource(file = "/d/windgenData/windgenData.zip", id = "windgenDataID", type = ARCHIVE)
@Resource(file = "*stdout.txt *stderr.txt", type = OUTPUT)
/*
    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 {
    
    public static final String PARMITEMOUTPUTFILE = "outputFile";
    public static final String PARMITEMDATAFILE = "dataFile";
    public static final String PARMITEMUSEINTERNAL = "useInternal";

    String parmOutputFile;
    String version = "CSIP weps: v 0.0.3";
    
    @Override
    public void doProcess() throws Exception {
        
        Executable windgen = getResourceExe("windgen");
                
        parmOutputFile = getStringParam(PARMITEMOUTPUTFILE, "win_gen_d.win");
        String parmOutputFileParm = "-o" + getWorkspaceDir() + "\\" + parmOutputFile;
        
        String parmDBFile = getStringParam(PARMITEMDATAFILE, "wind_gen_his_upper_US.wdb");
//        if (getParamInternalVal(PARMITEMDATAFILE).contentEquals("true")) {
        boolean b;
        try {
            b = getParamMap().get(PARMITEMDATAFILE).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
            parmDBFile = Config.getString("csip.dir") + "/d/windgenData/" + parmDBFile;
        } else {
            parmDBFile = getWorkspaceDir() + "/" + parmDBFile;
        }
        //checkFileExists(parmDBFile);
        if (!(new File (parmDBFile).exists())) {
            throw new ServiceException ("File does not exist, filename=" + parmDBFile);
        }
        parmDBFile = "-f" + parmDBFile;
        
        String parmWBAN = "-s" + getIntParam("WBANnum",999999);
        String parmStartYear = "-b" + getIntParam("startyear",1);
        String parmDuration = "-y" + getIntParam("duration",1);
        String parmStationIndex = hasParam("stationIndex") ? "-x" + getIntParam("stationIndex") : "";
        String parmRandSeed = hasParam("randSeed") ? "-r" + getIntParam("randSeed") : "";
        String parmStormDuration = hasParam("stormDuration") ? "-u" + getIntParam("stormDuration") : "";
        String parmStormBuild = hasParam("stormBuild") ? "-d" + getIntParam("stormBuild") : "";
        String parmLongOutput = hasParam("longOutput") && getBooleanParam("longOutput") ? "-l" : "";
        
        windgen.setArguments(
                            parmOutputFileParm,
                            parmDBFile,
                            parmWBAN,
                            parmStartYear,
                            parmDuration,
                            parmStationIndex,
                            parmRandSeed,
                            parmStormDuration,
                            parmStormBuild,
                            parmLongOutput
        );
        windgen.exec();
    }
    
    @Override
    protected void postProcess () throws Exception {
        putResult(new File(getWorkspaceDir() + "/" + parmOutputFile));
    }
}