@@ -155,8 +155,6 @@ |
|
try (Connection connection = resources().getJDBC(DBResources.CRDB);) { |
parameter.computeParameters(connection); |
- } catch (ServiceException | SQLException se) { |
- throw new ServiceException(se); |
} |
|
rhemModel = new RhemModel(aoa.getStateId(), aoa.getClimateStationId(), |
@@ -166,14 +164,11 @@ |
rhemModel.generateParamFile(parameter); |
rhemModel.generateStormFile(cligen_db, Double.parseDouble(parameter.getKe())); |
rhemModel.generateRunFile(); |
- try { |
- runModel(); |
- } catch (IOException ex) { |
- throw new ServiceException(ex); |
- } |
+ |
+ runModel(); |
|
//If the run is successful then edit the summary file. |
- double avgYearlyPrecip = 0; |
+ double avgYearlyPrecip = 0.0; |
if (rhemModel.isIntl()) { |
avgYearlyPrecip = rhemModel.getYearlyPrecipIntl(); |
} else { |
@@ -184,8 +179,6 @@ |
avgYearlyPrecip = rs.getDouble("avg_yearly_precip_mm"); |
} |
} |
- } catch (SQLException e) { |
- throw new ServiceException(e); |
} |
} |
rhemModel.appendToSumFile(avgYearlyPrecip); |
@@ -28,9 +28,8 @@ |
import javax.ws.rs.Path; |
import csip.annotations.Description; |
import csip.annotations.Name; |
-import static csip.annotations.ResourceType.EXECUTABLE; |
-import rhem.utils.LinearInterpolate; |
import rhem.utils.DBResources; |
+import rhem.utils.RHEMUtils; |
import rhem.utils.RhemResources; |
import static rhem.utils.RhemResources.RHEM_RA_EXE; |
|
@@ -92,7 +91,7 @@ |
|
|
private void generateRARunFile() throws ServiceException { |
- try (PrintWriter writer = new PrintWriter(new File(getWorkspaceDir(), "risk_assessment.run"))) { |
+ try (PrintWriter writer = new PrintWriter(getWorkspaceFile("risk_assessment.run"))) { |
writer.println(baseLineScenarioFileName); |
for (String fileName : raScenarioFilenameList) { |
writer.println(fileName); |
@@ -191,7 +190,7 @@ |
// default the altenative scenario interpolated value to 1 |
double altScenarioInterp = 1; |
if (!alt_scenario.isEmpty()) { |
- altScenarioInterp = LinearInterpolate.interpolate(maxBaselineSoilLoss, trasposedMatrix[i], trasposedMatrix[0]); |
+ altScenarioInterp = RHEMUtils.interpolate(maxBaselineSoilLoss, trasposedMatrix[i], trasposedMatrix[0]); |
altScenarioInterp = Math.round(altScenarioInterp * 1000.0) / 1000.0; |
|
// set the return period to 100 if the interpolated value is greater than 100 |
@@ -16,14 +16,60 @@ |
* @author rumpal |
*/ |
public class RHEMUtils { |
- |
- public static final String[] MONTH_NAMES_LIST = {"January", "February", "March", "April", "May", |
- "June", "July", "August", "September", "October", "November", "December"}; |
|
- //Round the value of a float or double |
- public static double roundValues(double value, int places) { |
- double power = Math.pow(10, places); |
- return (Math.round(value * power) / power); |
+ public static final String[] MONTH_NAMES_LIST = {"January", "February", "March", "April", "May", |
+ "June", "July", "August", "September", "October", "November", "December"}; |
+ |
+ //Round the value of a float or double |
+ |
+ public static double roundValues(double value, int places) { |
+ double power = Math.pow(10, places); |
+ return (Math.round(value * power) / power); |
+ } |
+ |
+ |
+ public static double interpolate(double pointToEvaluate, double[] functionValuesX, double[] functionValuesY) { |
+ double result = 0; |
+ int index = findIntervalLeftBorderIndex(pointToEvaluate, functionValuesX); |
+ if (index == functionValuesX.length - 1) { |
+ index--; |
+ } |
+ result = linearInterpolation(pointToEvaluate, functionValuesX[index], functionValuesY[index], |
+ functionValuesX[index + 1], functionValuesY[index + 1]); |
+ return result; |
+ } |
+ |
+ |
+ public static int findIntervalLeftBorderIndex(double point, double[] intervals) { |
+ |
+ if (point < intervals[0]) { |
+ return 0; |
+ } |
+ if (point > intervals[intervals.length - 1]) { |
+ return intervals.length - 1; |
} |
|
+ int leftBorderIndex = 0; |
+ int indexOfNumberToCompare; |
+ int rightBorderIndex = intervals.length - 1; |
+ |
+ while ((rightBorderIndex - leftBorderIndex) != 1) { |
+ indexOfNumberToCompare = leftBorderIndex |
+ + (int) Math.floor(((rightBorderIndex - leftBorderIndex) / 2)); |
+ if (point >= intervals[indexOfNumberToCompare]) { |
+ leftBorderIndex = indexOfNumberToCompare; |
+ } else { |
+ rightBorderIndex = indexOfNumberToCompare; |
+ } |
+ } |
+ return leftBorderIndex; |
+ } |
+ |
+ |
+ public static double linearInterpolation(double x, double x0, double y0, double x1, double y1) { |
+ double a = (y1 - y0) / (x1 - x0); |
+ double b = -a * x0 + y0; |
+ return a * x + b; |
+ } |
+ |
} |