Displaying differences for changeset
 
display as  

src/java/m/rhem/rhem02_getclimatestations/V1_0.java

@@ -23,6 +23,7 @@
 import javax.ws.rs.Path;
 import csip.annotations.Description;
 import csip.annotations.Name;
+import csip.utils.Numeric;
 import java.util.List;
 import org.codehaus.jettison.json.JSONArray;
 import rhem.utils.RHEMUtils;
@@ -58,10 +59,9 @@
 
   @Override
   public void doProcess() throws Exception {
-    try (Connection connection = resources().getJDBC(DBResources.CRDB);
-        Statement statement = connection.createStatement()) {
+    try ( Connection connection = resources().getJDBC(DBResources.CRDB);  Statement statement = connection.createStatement()) {
 
-      try (ResultSet resultSet = statement.executeQuery(DBResources.RHEM02Query01(stateId))) {
+      try ( ResultSet resultSet = statement.executeQuery(DBResources.RHEM02Query01(stateId))) {
         while (resultSet.next()) {
           climateStationStates = new ClimateStationStates(stateId,
               resultSet.getString("state_name"),
@@ -71,7 +71,7 @@
         }
       }
 
-      try (ResultSet resultSet = statement.executeQuery(DBResources.RHEM02Query02(stateId))) {
+      try ( ResultSet resultSet = statement.executeQuery(DBResources.RHEM02Query02(stateId))) {
         List<ClimateStation> climateStationsList = new ArrayList<>();
         while (resultSet.next()) {
           double[] monthlyPrecip = new double[12];
@@ -112,10 +112,10 @@
           .put(JSONUtils.dataDesc("long", cs.getStationLongitude(), "Climate station longitude"))
           .put(JSONUtils.dataDesc("year_recorded", cs.getYearRecorded(), "Number of years of recorded observations for climate station"))
           .put(JSONUtils.dataDesc("elevation_ft", cs.getElevation(), "Climate station elevation"))
-          .put(JSONUtils.dataDesc("avg_yearly_precip_mm", RHEMUtils.roundValues(cs.getAvgYearlyPrecip(), 2), "300 year average annual precipitation (mm)"));
+          .put(JSONUtils.dataDesc("avg_yearly_precip_mm", Numeric.round(cs.getAvgYearlyPrecip(), 2), "300 year average annual precipitation (mm)"));
       for (int i = 0; i < 12; i++) {
         innerArray.put(JSONUtils.dataDesc(MONTH_NAMES_LIST[i].substring(0, 3).toLowerCase() + "_precip_mm",
-            RHEMUtils.roundValues(cs.getMonthlyPrecip()[i], 2), MONTH_NAMES_LIST[i] + " estimated monthly average precipitation (mm)"));
+            Numeric.round(cs.getMonthlyPrecip()[i], 2), MONTH_NAMES_LIST[i] + " estimated monthly average precipitation (mm)"));
       }
       climateStationArray.put(JSONUtils.dataDesc("climate_stations", innerArray, "Climate station attributes"));
     }

src/java/rhem/utils/RHEMUtils.java

@@ -32,29 +32,22 @@
   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) {
     int index = findIntervalLeftBorderIndex(pointToEvaluate, functionValuesX);
-    if (index == functionValuesX.length - 1) 
+    if (index == functionValuesX.length - 1)
       index--;
-    
+
     return linearInterpolation(pointToEvaluate, functionValuesX[index], functionValuesY[index],
         functionValuesX[index + 1], functionValuesY[index + 1]);
   }
 
 
   public static int findIntervalLeftBorderIndex(double point, double[] intervals) {
-    if (point < intervals[0]) 
+    if (point < intervals[0])
       return 0;
-    
-    if (point > intervals[intervals.length - 1]) 
+
+    if (point > intervals[intervals.length - 1])
       return intervals.length - 1;
 
     int leftBorderIndex = 0;
@@ -63,7 +56,7 @@
 
     while ((rightBorderIndex - leftBorderIndex) != 1) {
       indexOfNumberToCompare = leftBorderIndex + (int) Math.floor(((rightBorderIndex - leftBorderIndex) / 2));
-      if (point >= intervals[indexOfNumberToCompare]) 
+      if (point >= intervals[indexOfNumberToCompare])
         leftBorderIndex = indexOfNumberToCompare;
       else
         rightBorderIndex = indexOfNumberToCompare;
@@ -126,20 +119,13 @@
 
 
   public static List<List> calculateReturnPeriods(double outputRAArray[][]) {
-    /* Sample outputRAArray
-        double[][] outputRAArray = {
-            {2, 0.15, 0.28, 1.09, 1.45},
-            {5, 0.3, 0.54, 2.03, 2.75},
-            {10, 0.4, 0.72, 2.75, 3.76},
-            {20, 0.53, 0.96, 3.65, 4.88},
-            {30, 0.58, 1.05, 3.9, 5.27},
-            {40, 0.59, 1.07, 3.91, 5.33},
-            {50, 0.63, 1.14, 4.22, 5.72},
-            {60, 0.64, 1.16, 4.34, 5.83},
-            {70, 0.66, 1.18, 4.37, 5.91},
-            {80, 0.68, 1.22, 4.49, 6.1},
-            {90, 0.7, 1.26, 4.67, 6.32},
-            {100, 0.71, 1.3, 4.84, 6.52}};
+    /*
+     * Sample outputRAArray double[][] outputRAArray = { {2, 0.15, 0.28, 1.09,
+     * 1.45}, {5, 0.3, 0.54, 2.03, 2.75}, {10, 0.4, 0.72, 2.75, 3.76}, {20,
+     * 0.53, 0.96, 3.65, 4.88}, {30, 0.58, 1.05, 3.9, 5.27}, {40, 0.59, 1.07,
+     * 3.91, 5.33}, {50, 0.63, 1.14, 4.22, 5.72}, {60, 0.64, 1.16, 4.34, 5.83},
+     * {70, 0.66, 1.18, 4.37, 5.91}, {80, 0.68, 1.22, 4.49, 6.1}, {90, 0.7,
+     * 1.26, 4.67, 6.32}, {100, 0.71, 1.3, 4.84, 6.52}};
      */
     List<List> interpolatedResultsArray = new ArrayList<>();
     outputRAArray = roundValues(outputRAArray);
@@ -156,7 +142,7 @@
       for (int i = 2; i < transposedMatrix.length; i++) {
         List<Double> alt_scenario = new ArrayList<>();
         for (int k = 0; k < transposedMatrix[i].length; k++) {
-          if (transposedMatrix[i][k] <= maxBaselineSoilLoss) 
+          if (transposedMatrix[i][k] <= maxBaselineSoilLoss)
             alt_scenario.add(transposedMatrix[i][k]);
         }
         // default the altenative scenario interpolated value to 1
@@ -166,7 +152,7 @@
           altScenarioInterp = Math.round(altScenarioInterp * 1000.0) / 1000.0;
 
           // set the return period to 100 if the interpolated value is greater than 100
-          if (altScenarioInterp > 100 || Double.isNaN(altScenarioInterp)) 
+          if (altScenarioInterp > 100 || Double.isNaN(altScenarioInterp))
             altScenarioInterp = 100;
         }
         // round the interpolated year to the nearest 10th place
@@ -193,9 +179,9 @@
           throw new ServiceException("Illegal unit for slopelength: " + p.getUnit("slopelength"));
       }
     }
-    if (slopeLength <= 0.0) 
+    if (slopeLength <= 0.0)
       throw new ServiceException("'slopelength' parameter must be greater than 0.");
-    
+
     return slopeLength;
   }