@@ -20,7 +20,6 @@ |
import java.util.Map; |
import java.util.Properties; |
import javax.ws.rs.Path; |
-import oms3.annotations.*; |
import org.apache.commons.io.FileUtils; |
import org.apache.velocity.VelocityContext; |
import org.apache.velocity.app.VelocityEngine; |
@@ -37,205 +36,204 @@ |
@Polling(first = 10000, next = 2000) |
public class V2_0 extends ModelDataService { |
|
- // parameter |
- static final String WATERSHED_NAME = "watershed_name"; |
- static final String DRAINAGE_AREA = "drainage_area"; |
- static final String CURVE_NUMBER = "curve_number"; |
- static final String WATERSHED_SLOPE = "watershed_slope"; |
- static final String WATERSHED_LENGTH = "watershed_length"; |
- static final String STORM_NAME = "storm_name"; |
- static final String RAIN_DEPTH = "rain_depth"; |
- static final String RAIN_DIST = "rain_dist"; |
+ // parameter |
+ static final String WATERSHED_NAME = "watershed_name"; |
+ static final String DRAINAGE_AREA = "drainage_area"; |
+ static final String CURVE_NUMBER = "curve_number"; |
+ static final String WATERSHED_SLOPE = "watershed_slope"; |
+ static final String WATERSHED_LENGTH = "watershed_length"; |
+ static final String STORM_NAME = "storm_name"; |
+ static final String RAIN_DEPTH = "rain_depth"; |
+ static final String RAIN_DIST = "rain_dist"; |
|
- // output |
- static final String RUNOFF_VOLUME = "runoff_volume"; |
- static final String PEAK_DISCHARGE = "peak_discharge"; |
- static final String PEAK_TIME = "peak_time"; |
- static final String TOC = "time_of_concentration"; |
+ // output |
+ static final String RUNOFF_VOLUME = "runoff_volume"; |
+ static final String PEAK_DISCHARGE = "peak_discharge"; |
+ static final String PEAK_TIME = "peak_time"; |
+ static final String TOC = "time_of_concentration"; |
|
- // internal result string. |
- private String[] res; |
- String st_name; |
- String toc; |
+ // internal result string. |
+ private String[] res; |
+ String st_name; |
+ String toc; |
|
|
- /** |
- * Inclusive range check. |
- * |
- * @param val |
- * @param name |
- * @param min |
- * @param max |
- * @throws ServiceException |
- */ |
- static void checkRange(double val, String name, double min, double max) throws ServiceException { |
- if (val >= min && val <= max) { |
- return; |
- } |
- throw new ServiceException(name + ": " + val + " not in range " + min + " ... " + max); |
+ /** |
+ * Inclusive range check. |
+ * |
+ * @param val |
+ * @param name |
+ * @param min |
+ * @param max |
+ * @throws ServiceException |
+ */ |
+ static void checkRange(double val, String name, double min, double max) throws ServiceException { |
+ if (val >= min && val <= max) { |
+ return; |
+ } |
+ throw new ServiceException(name + ": " + val + " not in range " + min + " ... " + max); |
+ } |
+ |
+ |
+ static void checkGT(double val, String name, double min) throws ServiceException { |
+ if (val > min) { |
+ return; |
+ } |
+ throw new ServiceException(name + ": " + val + " must be > " + min); |
+ } |
+ |
+ |
+ @Override |
+ protected void preProcess() throws Exception { |
+ // watershed name |
+ String ws_name = getStringParam(WATERSHED_NAME); |
+ st_name = getStringParam(STORM_NAME); |
+ |
+ double dr_area = getDoubleParam(DRAINAGE_AREA); |
+ checkRange(dr_area, DRAINAGE_AREA, 0, 2000); |
+ checkGT(dr_area, DRAINAGE_AREA, 0); |
+ |
+ double cu_number = getDoubleParam(CURVE_NUMBER); |
+ checkRange(cu_number, CURVE_NUMBER, 30, 100); |
+ |
+ double ws_slope = getDoubleParam(WATERSHED_SLOPE); |
+ checkRange(ws_slope, WATERSHED_SLOPE, 0.5, 64); |
+ |
+ double ws_length = getDoubleParam(WATERSHED_LENGTH); |
+ checkRange(ws_length, WATERSHED_LENGTH, 200, 26000); |
+ |
+ double ra_depth = getDoubleParam(RAIN_DEPTH); |
+ checkRange(ra_depth, RAIN_DEPTH, 0, 26); |
+ |
+ String ra_dist = getStringParam(RAIN_DIST); |
+ |
+ // populate the input file |
+ DecimalFormat f = new DecimalFormat("0.0####"); |
+ Map<String, Object> m = new HashMap<>(); |
+ m.put("wn", ws_name); |
+ m.put("da______", pad(f.format(dr_area), 10)); |
+ m.put("cn______", pad(f.format(cu_number), 10)); |
+ m.put("wl______", pad(f.format(ws_length), 10)); |
+ m.put("ws______", pad(f.format(ws_slope), 10)); |
+ m.put("rde_____", pad(f.format(ra_depth), 10)); |
+ m.put("sn______", pad(st_name, 10)); |
+ m.put("rdi_____", pad(ra_dist, 10)); |
+ createTR20Input(m, new File(getWorkspaceDir(), "TR20.inp")); |
+ } |
+ |
+ |
+ @Override |
+ protected void doProcess() throws Exception { |
+ Executable tr20 = getResourceExe("tr20"); |
+ int ret = tr20.exec(); |
+ if (ret != 0) { |
+ File err = tr20.stderr(); |
+ if (err.exists()) { |
+ new ServiceException(FileUtils.readFileToString(err)); |
+ } |
+ new ServiceException("Error executing TR20"); |
+ } |
+ File out = new File(getWorkspaceDir(), "TR20.out"); |
+ if (!out.exists()) { |
+ new ServiceException("Missing File : TR20.out"); |
+ } |
+ File dbg = new File(getWorkspaceDir(), "TR20.dbg"); |
+ if (!dbg.exists()) { |
+ new ServiceException("Missing File : TR20.dbg"); |
} |
|
- |
- static void checkGT(double val, String name, double min) throws ServiceException { |
- if (val > min) { |
- return; |
- } |
- throw new ServiceException(name + ": " + val + " must be > " + min); |
+ res = parseOutputFile(st_name, out); |
+ if (res == null || res.length != 3) { |
+ new ServiceException("Missing output."); |
} |
|
+ toc = parseDebugFile(dbg); |
+ if (toc == null) { |
+ new ServiceException("Missing tocs."); |
+ } |
+ } |
|
- @Override |
- protected void preProcess() throws Exception { |
- // watershed name |
- String ws_name = getStringParam(WATERSHED_NAME); |
- st_name = getStringParam(STORM_NAME); |
|
- double dr_area = getDoubleParam(DRAINAGE_AREA); |
- checkRange(dr_area, DRAINAGE_AREA, 0, 2000); |
- checkGT(dr_area, DRAINAGE_AREA, 0); |
+ @Override |
+ protected void postProcess() throws Exception { |
+ putResult(RUNOFF_VOLUME, res[0], "inches"); |
+ putResult(PEAK_TIME, res[1], "h"); |
+ putResult(PEAK_DISCHARGE, res[2], "cfs"); |
+ putResult(TOC, toc, "h"); |
+ } |
|
- double cu_number = getDoubleParam(CURVE_NUMBER); |
- checkRange(cu_number, CURVE_NUMBER, 30, 100); |
|
- double ws_slope = getDoubleParam(WATERSHED_SLOPE); |
- checkRange(ws_slope, WATERSHED_SLOPE, 0.5, 64); |
+ static private String parseDebugFile(File debugFile) throws IOException { |
+ List<String> out = FileUtils.readLines(debugFile); |
+ for (int i = 0; i < out.size(); i++) { |
+ String l = out.get(i); |
+ if (l.trim().toLowerCase().startsWith("time of concentration =")) { |
+ String[] vals = l.trim().split("\\s+"); |
+ return (vals.length == 5) ? vals[4] : null; |
+ } |
+ } |
+ return null; |
+ } |
|
- double ws_length = getDoubleParam(WATERSHED_LENGTH); |
- checkRange(ws_length, WATERSHED_LENGTH, 200, 26000); |
|
- double ra_depth = getDoubleParam(RAIN_DEPTH); |
- checkRange(ra_depth, RAIN_DEPTH, 0, 26); |
- |
- String ra_dist = getStringParam(RAIN_DIST); |
- |
- // populate the input file |
- DecimalFormat f = new DecimalFormat("0.0####"); |
- Map<String, Object> m = new HashMap<>(); |
- m.put("wn", ws_name); |
- m.put("da______", pad(f.format(dr_area), 10)); |
- m.put("cn______", pad(f.format(cu_number), 10)); |
- m.put("wl______", pad(f.format(ws_length), 10)); |
- m.put("ws______", pad(f.format(ws_slope), 10)); |
- m.put("rde_____", pad(f.format(ra_depth), 10)); |
- m.put("sn______", pad(st_name, 10)); |
- m.put("rdi_____", pad(ra_dist, 10)); |
- createTR20Input(m, new File(getWorkspaceDir(), "TR20.inp")); |
+ static private String[] parseOutputFile(String st_name, File outFile) throws IOException { |
+ List<String> out = FileUtils.readLines(outFile); |
+ int st_index = 0; |
+ for (int i = 0; i < out.size(); i++) { |
+ String l = out.get(i); |
+ if (l.trim().startsWith("STORM " + st_name)) { |
+ st_index = i; |
+ break; |
+ } |
} |
- |
- |
- @Override |
- protected String process() throws Exception { |
- Executable tr20 = getResourceExe("tr20"); |
- int ret = tr20.exec(); |
- if (ret != 0) { |
- File err = tr20.stderr(); |
- if (err.exists()) { |
- return FileUtils.readFileToString(err); |
- } |
- return "Error executing TR20"; |
- } |
- File out = new File(getWorkspaceDir(), "TR20.out"); |
- if (!out.exists()) { |
- return "Missing File : TR20.out"; |
- } |
- File dbg = new File(getWorkspaceDir(), "TR20.dbg"); |
- if (!dbg.exists()) { |
- return "Missing File : TR20.dbg"; |
- } |
- |
- res = parseOutputFile(st_name, out); |
- if (res == null || res.length != 3) { |
- return "Missing output."; |
- } |
- |
- toc = parseDebugFile(dbg); |
- if (toc == null) { |
- return "Missing tocs."; |
- } |
- return EXEC_OK; |
- } |
- |
- |
- @Override |
- protected void postProcess() throws Exception { |
- putResult(RUNOFF_VOLUME, res[0], "inches"); |
- putResult(PEAK_TIME, res[1], "h"); |
- putResult(PEAK_DISCHARGE, res[2], "cfs"); |
- putResult(TOC, toc, "h"); |
- } |
- |
- |
- static private String parseDebugFile(File debugFile) throws IOException { |
- List<String> out = FileUtils.readLines(debugFile); |
- for (int i = 0; i < out.size(); i++) { |
- String l = out.get(i); |
- if (l.trim().toLowerCase().startsWith("time of concentration =")) { |
- String[] vals = l.trim().split("\\s+"); |
- return (vals.length == 5) ? vals[4] : null; |
- } |
- } |
- return null; |
- } |
- |
- |
- static private String[] parseOutputFile(String st_name, File outFile) throws IOException { |
- List<String> out = FileUtils.readLines(outFile); |
- int st_index = 0; |
- for (int i = 0; i < out.size(); i++) { |
- String l = out.get(i); |
- if (l.trim().startsWith("STORM " + st_name)) { |
- st_index = i; |
- break; |
- } |
- } |
- if (st_index > 0) { |
- for (int i = st_index; i < out.size(); i++) { |
- String l = out.get(i); |
- if (l.trim().startsWith("sub 1")) { |
- String[] vals = l.split("\\s+"); |
- if (vals.length > 5) { |
+ if (st_index > 0) { |
+ for (int i = st_index; i < out.size(); i++) { |
+ String l = out.get(i); |
+ if (l.trim().startsWith("sub 1")) { |
+ String[] vals = l.split("\\s+"); |
+ if (vals.length > 5) { |
// System.out.println("runoff " + vals[3]); |
// System.out.println("time " + vals[4]); |
// System.out.println("rate " + vals[5]); |
- return new String[]{vals[3], vals[4], vals[5]}; |
- } |
- break; |
- } |
- } |
+ return new String[]{vals[3], vals[4], vals[5]}; |
+ } |
+ break; |
} |
- return null; |
+ } |
} |
+ return null; |
+ } |
|
|
- public static void createTR20Input(Map<String, Object> inp, File file) throws Exception { |
- Properties props = new Properties(); |
- props.put("resource.loader", "class"); |
- props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
+ public static void createTR20Input(Map<String, Object> inp, File file) throws Exception { |
+ Properties props = new Properties(); |
+ props.put("resource.loader", "class"); |
+ props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
- VelocityContext context = new VelocityContext(inp); |
- try (Writer writer = new FileWriter(file)) { |
- VelocityEngine ve = new VelocityEngine(); |
- ve.init(props); |
- ve.getTemplate("m/hydrotools/efh2/V2_0_input.vm").merge(context, writer); |
- } |
+ VelocityContext context = new VelocityContext(inp); |
+ try (Writer writer = new FileWriter(file)) { |
+ VelocityEngine ve = new VelocityEngine(); |
+ ve.init(props); |
+ ve.getTemplate("m/hydrotools/efh2/V2_0_input.vm").merge(context, writer); |
} |
+ } |
|
|
- /** |
- * |
- * @param s |
- * @param len |
- * @return |
- */ |
- private static String pad(String s, int len) { |
- if (s.length() == len) { |
- return s; |
- } |
- if (s.length() > len) { |
- return s.substring(0, len); |
- } |
- return String.format("%1$-" + len + "s", s); |
+ /** |
+ * |
+ * @param s |
+ * @param len |
+ * @return |
+ */ |
+ private static String pad(String s, int len) { |
+ if (s.length() == len) { |
+ return s; |
} |
+ if (s.length() > len) { |
+ return s.substring(0, len); |
+ } |
+ return String.format("%1$-" + len + "s", s); |
+ } |
|
// public static void main(String[] args) throws IOException, Exception { |
//// parseOutput("storm1", new File("/od/projects/csip-all/csip-hydrotools/src/java/bin/lin-amd64/TR20.out")); |
@@ -14,7 +14,6 @@ |
import java.io.File; |
import java.util.List; |
import javax.ws.rs.Path; |
-import oms3.annotations.*; |
import org.apache.commons.io.FileUtils; |
|
/** |
@@ -26,67 +25,59 @@ |
@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) |
-}) |
+@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; |
+ String conv; |
+ String tr20name; |
+ String convinput; |
|
|
- @Override |
- protected void preProcess() throws Exception { |
- conv = getStringParam("conv"); |
- tr20name = getStringParam("tr20_input"); |
- convinput = getStringParam("conv_input"); |
+ @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); |
} |
|
- // 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; |
+ 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); |
|
- @Override |
- protected void postProcess() throws Exception { |
- super.postProcess(); //To change body of generated methods, choose Tools | Templates. |
+ 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); |
} |
+ } |
+ |
} |