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

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 java.io.File;

import javax.ws.rs.*;
import static m.wepsModelConstants.*;

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

@Resource(file = "/bin/lin-amd64/wind_gen5", id = "windgen5", type = EXECUTABLE)
@Resource(file = "/data/windgenData111522.zip", id = "windgenDataID", type = ARCHIVE)

@Resource(file = "*stdout.txt *stderr.txt", type = OUTPUT)

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

public class V1_0 extends ModelDataService {

    public static final String WindDBFileName = "windgen.wdb";
    public static final String WindIdxFileName = "windgen.idx";
    public static final String WindIdxNrcsFileName = "windgen_NRCS.idx";
    
    
    Executable windgen;
    String parmOutputFile;
    
    // Reminder: add new date to json as well
  static final String serviceUpdateStr = "05/10/2023";


    @Override
    public void doProcess() throws Exception {
        
        String modelVersion = (parameter().has("modelVersion")) ? parameter().getString("modelVersion") : "";
        
        if (modelVersion.contentEquals("windgen4.win")) {
            windgen = resources().getExe("windgen4");
        } else if (modelVersion.contentEquals("windgen5.lin64")) {
            windgen = resources().getExe("windgen5");
        } else {
            windgen = resources().getExe("windgen5");
        }

        parmOutputFile = parameter().getString(PARMITEMOUTPUTFILE, "win_gen_d.win");
        String parmOutputFileParm = "-o" + workspace().getDir() + "/" + parmOutputFile;

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

        String parmWBAN = "-s" + parameter().getInt("WBANnum", 999999);
        String parmStartYear = "-b" + parameter().getInt("startyear", 1);
        String parmDuration = "-y" + parameter().getInt("duration", 1);
        String parmStationIndex = parameter().has("stationIndex") ? "-x" + parameter().getInt("stationIndex") : "";
        String parmRandSeed = parameter().has("randSeed") ? "-r" + parameter().getInt("randSeed") : "";
        String parmStormDuration = parameter().has("stormDuration") ? "-u" + parameter().getInt("stormDuration") : "";
        String parmStormBuild = parameter().has("stormBuild") ? "-d" + parameter().getInt("stormBuild") : "";
        String parmLongOutput = parameter().has("longOutput") && parameter().getBoolean("longOutput") ? "-l" : "";

        windgen.setArguments(
                parmOutputFileParm,
                parmDBFile,
                parmWBAN,
                parmStartYear,
                parmDuration,
                parmStationIndex,
                parmRandSeed,
                parmStormDuration,
                parmStormBuild,
                parmLongOutput
        );

        int ret = windgen.exec();
        if (ret != 0) {
            throw new ServiceException("Error executing: " + windgen);
        }
    }


    @Override
    protected void postProcess() throws Exception {
        metainfo().put(PARMOUTUPDATED, serviceUpdateStr);
        results().put(new File(workspace().getDir() + "/" + parmOutputFile));
    }
}