@@ -7,12 +7,14 @@ |
|
import csip.Executable; |
import csip.ModelDataService; |
+import csip.ServiceException; |
import csip.annotations.*; |
import static csip.annotations.ResourceType.EXECUTABLE; |
import java.io.File; |
import java.io.FileWriter; |
import java.io.IOException; |
import java.io.Writer; |
+import java.text.DecimalFormat; |
import java.util.HashMap; |
import java.util.List; |
import java.util.Map; |
@@ -56,30 +58,66 @@ |
String st_name; |
String toc; |
|
- static final String format = "%-10.3f"; |
+ |
+ /** |
+ * 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 { |
- // get the parameter |
+ // 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); |
- st_name = getStringParam(STORM_NAME); |
+ 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 |
+ // populate the input file |
+ DecimalFormat f = new DecimalFormat("0.0####"); |
Map<String, Object> m = new HashMap<>(); |
m.put("wn", ws_name); |
- m.put("da______", String.format(format, dr_area)); |
- m.put("cn______", String.format(format, cu_number)); |
- m.put("wl______", String.format(format, ws_length)); |
- m.put("ws______", String.format(format, ws_slope)); |
+ 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("rde_____", String.format(format, ra_depth)); |
m.put("rdi_____", pad(ra_dist, 10)); |
createTR20Input(m, new File(getWorkspaceDir(), "TR20.inp")); |
} |
@@ -94,26 +132,26 @@ |
if (err.exists()) { |
return FileUtils.readFileToString(err); |
} |
+ return "Error executing TR20"; |
} |
File out = new File(getWorkspaceDir(), "TR20.out"); |
- if (out.exists()) { |
+ if (!out.exists()) { |
return "Missing File : TR20.out"; |
} |
File dbg = new File(getWorkspaceDir(), "TR20.dbg"); |
- if (dbg.exists()) { |
+ if (!dbg.exists()) { |
return "Missing File : TR20.dbg"; |
} |
|
- res = parseOutput(st_name, out); |
+ res = parseOutputFile(st_name, out); |
if (res == null || res.length != 3) { |
return "Missing output."; |
} |
|
- toc = parseDebug(dbg); |
+ toc = parseDebugFile(dbg); |
if (toc == null) { |
return "Missing tocs."; |
} |
-// System.out.println("toc " + toc); |
return EXEC_OK; |
} |
|
@@ -127,7 +165,7 @@ |
} |
|
|
- static private String parseDebug(File debugFile) throws IOException { |
+ 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); |
@@ -140,7 +178,7 @@ |
} |
|
|
- static private String[] parseOutput(String st_name, File outFile) throws IOException { |
+ 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++) { |