V2_0.java [src/java/m/hydrotools/tr20] Revision: 29c38ae5409ce3dbcdca4488e55d192fb72fe11d  Date: Tue Aug 18 10:09:39 MDT 2015
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.hydrotools.tr20;

import csip.Executable;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.*;
import static csip.annotations.ResourceType.EXECUTABLE;
import static csip.annotations.ResourceType.OUTPUT;
import java.io.File;
import java.util.List;
import javax.ws.rs.Path;
import oms3.annotations.*;
import org.apache.commons.io.FileUtils;

/**
 * TR20 Service. Execution with converters.
 *
 * @author od
 */
@Name("Hydrotools")
@Description("TR20 with converters.")
@Path("m/tr20/2.0")
@Polling(first = 10000, next = 2000)
@Resources({
    @Resource(file = "/bin/lin-amd64/TR20.exe", type = ResourceType.EXECUTABLE, id = "tr20"),
    @Resource(file = "/bin/lin-amd64/HECRAS_Converter_V31.exe", type = EXECUTABLE, id = "hecras"),
    @Resource(file = "/bin/lin-amd64/NOAA_Converter_V31.exe", type = EXECUTABLE, id = "noaa"),
    @Resource(file = "/bin/lin-amd64/NOAA_Converter_V31_sm.exe", type = EXECUTABLE, id = "noaa_sm"),
    @Resource(file = "/bin/lin-amd64/NRCC_Converter_V31_non.exe", type = EXECUTABLE, id = "nrcc"),
    @Resource(file = "/bin/lin-amd64/NRCC_Converter_V31_sm.exe", type = EXECUTABLE, id = "nrcc_sm"),
    @Resource(file = "*.out *.hyd *.err *.dbg stdout.txt stderr.txt", type = OUTPUT)
})

public class V2_0 extends ModelDataService {

    String conv;
    String tr20name;
    String convinput;


    @Override
    protected void preProcess() throws Exception {
        conv = getStringParam("conv");
        tr20name = getStringParam("tr20_input");
        convinput = getStringParam("conv_input");
    }

    // hecras nrcc: txt, 
    // noaa csv
    // all produce inp.

    @Override
    protected String process() throws Exception {
        Executable e = getResourceExe(conv);
        int ret = e.exec();
        if (ret != 0) {
            return " Converter error " + conv;
        }

        File conv_output = new File(getWorkspaceDir(), convinput.substring(0, convinput.indexOf('.')) + ".inp");
        if (!conv_output.exists()) {
            throw new ServiceException("No converter output.");
        }

        List<String> inp = FileUtils.readLines(conv_output);
        inp.remove(0);
        inp.remove(0);
        inp.remove(0);

        FileUtils.writeLines(getFileInput(tr20name), inp, true);

        // merge the input files.
        Executable e1 = getResourceExe("tr20");
        e1.setArguments(tr20name);
        ret = e1.exec();
        if (ret != 0) {
            return " TR20 error " + conv;
        }
        return EXEC_OK;
    }


    @Override
    protected void postProcess() throws Exception {
        super.postProcess(); //To change body of generated methods, choose Tools | Templates.
    }
}