V2_0.java [src/java/m/hydrotools/tr20] Revision: df57a55ea7dd4f9f640ae39adfb2b9404a2fdad8  Date: Wed Sep 26 10:53:45 MDT 2018
/*
 * 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 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)
@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 void doProcess() throws Exception {
    Executable e = getResourceExe(conv);
    int ret = e.exec();
    if (ret != 0) {
      throw new ServiceException(" 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) {
      throw new ServiceException(" TR20 error " + conv);
    }
  }

}