@@ -57,7 +57,6 @@ |
|
String cligen_db = Config.getString("rhem.cligen_db", "file:/Users/rumpal/Documents/Work/csipDocuments/RHEM/cligen"); |
|
- |
@Override |
public void preProcess() throws ServiceException { |
try { |
@@ -69,7 +68,6 @@ |
String stateId = getStringParam("stateid"); |
String climatestationId = getStringParam("climatestationid"); |
String soilTexture = getStringParam("soiltexture"); |
- double slopeLength = getDoubleParam("slopelength", 0.0); |
String slopeShape = getStringParam("slopeshape"); |
double slopeSteepness = getDoubleParam("slopesteepness", 0.0); |
double bunchGgrassCanopyCover = getDoubleParam("bunchgrasscanopycover", 0.0); |
@@ -87,20 +85,25 @@ |
LOG.log(Level.SEVERE, "RHEM-01: Invalid unit"); |
throw new ServiceException("Unit should be 1-metric or 2 - English. Digit " + unit + " is not valid."); |
} |
+ double slopeLength; |
+ if (unit == 1) { |
+ slopeLength = 50; |
+ } else { |
+ slopeLength = 164.04; |
+ } |
|
aoa = new AoA(aoaId, rhemSiteId, scenarioName, |
scenarioDescription, unit, stateId, climatestationId, |
soilTexture, slopeLength, slopeShape, slopeSteepness, |
bunchGgrassCanopyCover, forbsCanopyCover, shrubsCanopyCover, |
sodGrassCanopyCover, basalCover, rockCover, litterCover, |
- cryptogamsCover); |
+ cryptogamsCover, 25); |
} catch (ServiceException ex) { |
LOG.log(Level.SEVERE, "RHEM-01: Error in processing the request JSON.", ex); |
throw new ServiceException("Error in processing the request JSON.", ex); |
} |
} |
|
- |
@Override |
public void doProcess() throws ServiceException, IOException { |
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, YYYY, hh:mm a"); |
@@ -132,7 +135,7 @@ |
rhemModel.generateParamFile(parameter); |
rhemModel.generateStormFile(cligen_db, Double.parseDouble(parameter.getKe())); |
rhemModel.generateRunFile(); |
- |
+ |
runModel(); |
|
//If the run is successful then edit the summary file. |
@@ -159,7 +162,6 @@ |
} |
} |
|
- |
@Override |
public void postProcess() { |
putResult("AoAID", aoa.getAoaId(), "Area of Analysis Identifier"); |
@@ -49,8 +49,7 @@ |
private JSONObject rhemSiteGeometryPoint; |
|
//Result Payload |
- private ArrayList<Mapuint> mapunitList; |
- |
+ private ArrayList<Mapunit> mapunitList; |
|
@Override |
public void preProcess() throws ServiceException { |
@@ -64,7 +63,6 @@ |
} |
} |
|
- |
@Override |
public void doProcess() throws ServiceException { |
try (Connection sdmConnection = getResourceJDBC(RHEMResources.SDM_DB); |
@@ -78,7 +76,6 @@ |
} |
} |
|
- |
private void getMapunitValues(Connection sdmConnection) throws ServiceException { |
try (Statement statement = sdmConnection.createStatement();) { |
GISEngine gisEngine = createGISEngine(sdmConnection); |
@@ -106,7 +103,7 @@ |
resultSet.getInt("slope_h"))); |
} |
} |
- mapunitList.add(new Mapuint(musym, muname, componentList)); |
+ mapunitList.add(new Mapunit(musym, muname, componentList)); |
} |
} catch (SQLException | GISObjectException | JSONException | IOException ex) { |
LOG.log(Level.SEVERE, "RHEM-03: SQLException.", ex); |
@@ -114,11 +111,10 @@ |
} |
} |
|
- |
private void getComponentValues(Connection connection, Connection esdConnection) throws ServiceException { |
try (Statement statement = connection.createStatement();) { |
- for (Mapuint mapuint : mapunitList) { |
- for (Component component : mapuint.getComponentList()) { |
+ for (Mapunit mapunit : mapunitList) { |
+ for (Component component : mapunit.getComponentList()) { |
ArrayList<EcologicalSite> ecoList = new ArrayList<>(); |
String ecoclassid = null; |
try (ResultSet resultSet = statement.executeQuery(DBQueries.RHEM03Query03(component.getCokey()))) { |
@@ -145,11 +141,10 @@ |
} |
} |
|
- |
private void getSurfaceTextureClass(Connection connection, Connection rhemConnection) throws ServiceException { |
try (Statement statement = connection.createStatement();) { |
- for (Mapuint mapuint : this.mapunitList) { |
- for (Component component : mapuint.getComponentList()) { |
+ for (Mapunit mapunit : this.mapunitList) { |
+ for (Component component : mapunit.getComponentList()) { |
ArrayList<SurfaceTexture> surfaceTextureList = new ArrayList<>(); |
try (ResultSet resultSet = statement.executeQuery(DBQueries.RHEM03Query05(component.cokey));) { |
while (resultSet.next()) { |
@@ -175,7 +170,6 @@ |
} |
} |
|
- |
@Override |
public void postProcess() throws Exception { |
try { |
@@ -183,10 +177,10 @@ |
putResult("rhem_site_id", this.rhemSiteId, "RHEM Evaluation Site Identifier"); |
|
JSONArray mapUnitArray = new JSONArray(); |
- for (Mapuint mapunit : this.mapunitList) { |
- JSONArray mapuintArr = new JSONArray(); |
- mapuintArr.put(JSONUtils.dataDesc("musym", mapunit.getMusym(), "Mapunit Symbol")); |
- mapuintArr.put(JSONUtils.dataDesc("muname", mapunit.getMuname(), "Mapunit Name")); |
+ for (Mapunit mapunit : this.mapunitList) { |
+ JSONArray mapunitArr = new JSONArray(); |
+ mapunitArr.put(JSONUtils.dataDesc("musym", mapunit.getMusym(), "Mapunit Symbol")); |
+ mapunitArr.put(JSONUtils.dataDesc("muname", mapunit.getMuname(), "Mapunit Name")); |
|
JSONArray componentArray = new JSONArray(); |
for (Component component : mapunit.getComponentList()) { |
@@ -219,17 +213,16 @@ |
componentArr.put(JSONUtils.dataDesc("surface_texture_class_list", surfaceTestureArray, "Soil Component Surface Texture Class List")); |
componentArray.put(JSONUtils.dataDesc("component", componentArr, "Component")); |
} |
- mapuintArr.put(JSONUtils.dataDesc("component_list", componentArray, "Component List")); |
- mapUnitArray.put(JSONUtils.dataDesc("mapunit", mapuintArr, "Mapuint")); |
+ mapunitArr.put(JSONUtils.dataDesc("component_list", componentArray, "Component List")); |
+ mapUnitArray.put(JSONUtils.dataDesc("mapunit", mapunitArr, "Mapunit")); |
} |
- putResult("mapunit_list", mapUnitArray, "Mapuint List"); |
+ putResult("mapunit_list", mapUnitArray, "Mapunit List"); |
} catch (JSONException ex) { |
LOG.log(Level.SEVERE, "RHEM-03: Error in processing the reponse JSON.", ex); |
throw new ServiceException("Error in processing the reponse JSON.", ex); |
} |
} |
|
- |
/** |
* Get the full JSON parameter record. |
* |
@@ -244,31 +237,27 @@ |
return p; |
} |
|
- static class Mapuint { |
+ static class Mapunit { |
|
protected String musym; |
protected String muname; |
protected ArrayList<Component> componentList; |
|
- |
- public Mapuint(String musym, String muname, |
+ public Mapunit(String musym, String muname, |
ArrayList<Component> componentList) { |
this.musym = musym; |
this.muname = muname; |
this.componentList = componentList; |
} |
|
- |
public String getMusym() { |
return this.musym; |
} |
|
- |
public String getMuname() { |
return this.muname; |
} |
|
- |
public ArrayList<Component> getComponentList() { |
return this.componentList; |
} |
@@ -287,7 +276,6 @@ |
protected ArrayList<EcologicalSite> ecoSiteList; |
protected ArrayList<SurfaceTexture> surfaceTextureList; |
|
- |
public Component(String cokey, String compName, int comppctR, |
int slopePctL, int slopePctR, int slopePctH) { |
this.cokey = cokey; |
@@ -298,32 +286,26 @@ |
this.slopePctH = slopePctH; |
} |
|
- |
public String getCokey() { |
return this.cokey; |
} |
|
- |
public String getCompName() { |
return this.compName; |
} |
|
- |
public int getComppctR() { |
return this.comppctR; |
} |
|
- |
public int getSlopePctL() { |
return this.slopePctL; |
} |
|
- |
public int getSlopePctR() { |
return this.slopePctR; |
} |
|
- |
public int getSlopePctH() { |
return this.slopePctH; |
} |
@@ -332,17 +314,14 @@ |
return this.ecoSiteList; |
} |
|
- |
public ArrayList<SurfaceTexture> getSurfaceTextureList() { |
return this.surfaceTextureList; |
} |
|
- |
public void setEcoSiteList(ArrayList<EcologicalSite> list) { |
this.ecoSiteList = list; |
} |
|
- |
public void setSurfaceTextureList(ArrayList<SurfaceTexture> list) { |
this.surfaceTextureList = list; |
} |
@@ -354,18 +333,15 @@ |
protected String esId; |
protected String esRangeName; |
|
- |
public EcologicalSite(String esId, String esRangeName) { |
this.esId = esId; |
this.esRangeName = esRangeName; |
} |
|
- |
public String getEsId() { |
return esId; |
} |
|
- |
public String getEsRangeName() { |
return esRangeName; |
} |
@@ -378,7 +354,6 @@ |
protected String textureClass; |
protected String textureClassLabel; |
|
- |
public SurfaceTexture(String texcl, int textureId, String textureClass, |
String textureClassLabel) { |
this.texcl = texcl; |
@@ -387,22 +362,18 @@ |
this.textureClassLabel = textureClassLabel; |
} |
|
- |
public String getTexcl() { |
return this.texcl; |
} |
|
- |
public int getTextureId() { |
return this.textureId; |
} |
|
- |
public String getTextureClass() { |
return this.textureClass; |
} |
|
- |
public String getTextureClassLabel() { |
return this.textureClassLabel; |
} |
@@ -10,10 +10,11 @@ |
import csip.ServiceException; |
import csip.annotations.Polling; |
import csip.annotations.Resource; |
-import static csip.annotations.ResourceType.EXECUTABLE; |
import static csip.annotations.ResourceType.OUTPUT; |
+import java.io.BufferedReader; |
import java.io.File; |
import java.io.FileNotFoundException; |
+import java.io.FileReader; |
import java.io.IOException; |
import java.io.PrintWriter; |
import java.util.ArrayList; |
@@ -22,6 +23,8 @@ |
import javax.ws.rs.Path; |
import oms3.annotations.Description; |
import oms3.annotations.Name; |
+import rhem.utils.LinearInterpolate; |
+import rhem.utils.RHEMResources; |
import static rhem.utils.RHEMResources.RHEM_RA_EXE; |
|
/** |
@@ -33,11 +36,12 @@ |
@Path("m/rhem/riskassessment/1.0") |
@Polling(first = 10000, next = 2000) |
@Resource(file = "*.out *.run", type = OUTPUT) |
+@Resource(from = RHEMResources.class) |
public class V1_0 extends ModelDataService { |
|
private String baseLineScenarioFileName; |
private List<String> raScenarioFilenameList; |
- |
+ private ArrayList<String> scenarioNames; |
|
@Override |
public void preProcess() throws ServiceException { |
@@ -52,15 +56,17 @@ |
baseLineScenarioFileName = getStringParam("base_line_scenario_file"); |
} |
|
- |
@Override |
public void doProcess() throws ServiceException, IOException { |
writeOutFilesToWorkspace(); |
generateRARunFile(); |
runRiskAssessment(); |
+ |
+ double[][] outRAarray = readRiskAssessmentOutFile(); |
+ ArrayList<ArrayList> interpolatedResultsArray = calculateReturnPeriods(outRAarray); |
+ writeInterpolatedResultsArray(interpolatedResultsArray); |
} |
|
- |
private void writeOutFilesToWorkspace() throws ServiceException { |
getFileInput(baseLineScenarioFileName); |
for (String filename : raScenarioFilenameList) { |
@@ -68,7 +74,6 @@ |
} |
} |
|
- |
private void generateRARunFile() throws ServiceException { |
try (PrintWriter writer = new PrintWriter(new File(getWorkspaceDir(), "risk_assessment.run"))) { |
writer.println(baseLineScenarioFileName); |
@@ -82,7 +87,6 @@ |
} |
} |
|
- |
private void runRiskAssessment() throws ServiceException, IOException { |
Executable rh = getResourceExe(RHEM_RA_EXE); |
rh.setArguments("-b", getWorkspaceFile("risk_assessment.run").toPath()); |
@@ -91,4 +95,147 @@ |
throw new ServiceException("Problem in running risk assessment."); |
} |
} |
+ |
+ private double[][] readRiskAssessmentOutFile() throws ServiceException { |
+ scenarioNames = new ArrayList<>(); |
+ double outputRAArray[][] = new double[12][raScenarioFilenameList.size() + 2]; |
+ int count = -1; |
+ boolean check = false; |
+ try { |
+ FileReader fileReader = new FileReader(getWorkspaceFile("risk_assessment.OUT")); |
+ try (BufferedReader bufferedReader |
+ = new BufferedReader(fileReader)) { |
+ String line; |
+ |
+ while ((line = bufferedReader.readLine()) != null && !check) { |
+ if (line.contains("FREQUENCY ANALYSIS")) { |
+ check = true; |
+ for (int i = 0; i < 3; i++) { |
+ line = bufferedReader.readLine(); |
+ if (i == 2) { |
+ String[] test = line.trim().split("\\s+"); |
+ for (int j = 1; j < test.length; j++) { |
+ scenarioNames.add(test[j]); |
+ } |
+ } |
+ } |
+ } |
+ } |
+ |
+ do { |
+ count++; |
+ String[] test = line.trim().split("\\s+"); |
+ for (int i = 0; i < test.length; i++) { |
+ outputRAArray[count][i] = Double.parseDouble(test[i].trim()); |
+ } |
+ } while ((line = bufferedReader.readLine()) != null && check); |
+ |
+ } |
+ } catch (IOException exception) { |
+ throw new ServiceException("Problem in reading the risk assessment out file.", exception); |
+ } |
+ return outputRAArray; |
+ } |
+ |
+ public ArrayList<ArrayList> 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}}; |
+ */ |
+ ArrayList<ArrayList> interpolatedResultsArray = new ArrayList(); |
+ outputRAArray = roundValues(outputRAArray); |
+ double[][] trasposedMatrix = trasposeMatrix(outputRAArray); |
+ |
+ for (int x = 0; x < trasposedMatrix[0].length; x++) { |
+ double currentRP = trasposedMatrix[0][x]; |
+ double currentSoilLoss = trasposedMatrix[1][x]; |
+ double maxBaselineSoilLoss = trasposedMatrix[1][x]; |
+ ArrayList<Double> rpPeriodArray = new ArrayList<>(); |
+ rpPeriodArray.add(currentSoilLoss); |
+ rpPeriodArray.add(currentRP); |
+ |
+ for (int i = 2; i < trasposedMatrix.length; i++) { |
+ ArrayList<Double> alt_scenario = new ArrayList<>(); |
+ for (int k = 0; k < trasposedMatrix[i].length; k++) { |
+ if (trasposedMatrix[i][k] <= maxBaselineSoilLoss) { |
+ alt_scenario.add(trasposedMatrix[i][k]); |
+ } |
+ } |
+ // default the altenative scenario interpolated value to 1 |
+ double altScenarioInterp = 1; |
+ if (!alt_scenario.isEmpty()) { |
+ altScenarioInterp = LinearInterpolate.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 |
+ if (altScenarioInterp > 100 || Double.isNaN(altScenarioInterp)) { |
+ altScenarioInterp = 100; |
+ } |
+ } |
+ // round the interpolated year to the nearest 10th place |
+ altScenarioInterp = Math.round(altScenarioInterp * 10) / 10.0; |
+ rpPeriodArray.add(altScenarioInterp); |
+ } |
+ interpolatedResultsArray.add(rpPeriodArray); |
+ } |
+ return interpolatedResultsArray; |
+ } |
+ |
+ public static double[][] roundValues(double[][] matrix) { |
+ double[][] outputMatrix = new double[matrix.length][matrix[0].length]; |
+ for (int i = 0; i < matrix.length; i++) { |
+ for (int j = 0; j < matrix[i].length; j++) { |
+ outputMatrix[i][j] = Math.round(matrix[i][j] * 100.0) / 100.0; |
+ } |
+ } |
+ return outputMatrix; |
+ } |
+ |
+ public static double[][] trasposeMatrix(double[][] matrix) { |
+ int x = matrix.length; |
+ int y = matrix[0].length; |
+ |
+ double[][] trasposedMatrix = new double[y][x]; |
+ |
+ for (int i = 0; i < y; i++) { |
+ for (int j = 0; j < x; j++) { |
+ trasposedMatrix[i][j] = matrix[j][i]; |
+ } |
+ } |
+ return trasposedMatrix; |
+ } |
+ |
+ public void writeInterpolatedResultsArray(ArrayList<ArrayList> interpolatedResultsArray) throws ServiceException { |
+ |
+ try (PrintWriter writer = new PrintWriter(new File(getWorkspaceDir(), "frequencyAnalysisReturnPeriodTable.out"));) { |
+ writer.print(String.format("%-20s %-20s", "BASELINE SCENARIO", "RETURN PERIOD")); |
+ for (String name : scenarioNames) { |
+ writer.print(String.format("%-20s", name + "(years)")); |
+ } |
+ writer.println(); |
+ |
+ for (int i = 0; i < interpolatedResultsArray.size(); i++) { |
+ for (int j = 0; j < interpolatedResultsArray.get(i).size(); j++) { |
+ writer.print(String.format("%-20s", interpolatedResultsArray.get(i).get(j).toString())); |
+ } |
+ writer.println(); |
+ } |
+ writer.close(); |
+ } catch (FileNotFoundException ex) { |
+ throw new ServiceException("Problem in generating the parameter file.", ex); |
+ } |
+ |
+ } |
+ |
} |
@@ -28,6 +28,13 @@ |
return query; |
} |
|
+ public static String RHEM01Query02(String climateStationId) { |
+ String query = "SELECT avg_yearly_precip_mm " |
+ + "FROM rhem.d_rhem_climate_stations_avg_300yr_est_rain " |
+ + "WHERE station_id = '" + climateStationId + "';"; |
+ return query; |
+ } |
+ |
public static String RHEM02Query01(String stateId) { |
String query = "SELECT state_name, latitude, longitude, zoom " |
+ "FROM rhem.d_rhem_climate_station_states " |
@@ -53,21 +60,6 @@ |
} |
|
public static String RHEM03Query01(GISObject geometry) { |
-// String query = "SELECT mukey " |
-// + "FROM ssurgo.soilmu_a " |
-// + "WITH (index(geom_sidx)) " |
-// + "WHERE the_geom.STContains(geometry::STGeomFromText('" |
-// + geometry.getGeometry() + "', 0))=1 " |
-// + "AND (geometry::STGeomFromText('" |
-// + geometry.getGeometry() + "', 0).STIsValid())=1;"; |
- |
-// String query = "SELECT mupoly.mukey, mapunit.musym, mapunit.muname " |
-// + "FROM dbo.mupolygon AS mupoly " |
-// + "WITH (index(SI_mupolygon_24876)) " |
-// + "JOIN dbo.mapunit AS mapunit " |
-// + "ON mupoly.mukey = mapunit.mukey " |
-// + "WHERE mupoly.mupolygongeo.STIntersects( geometry::STGeomFromText('" |
-// + geometry.getGeometry() + "', 4326)) = 1;"; |
String query = "SELECT mupoly.mukey, mapunit.musym, mapunit.muname " |
+ "FROM dbo.mupolygon AS mupoly " |
+ "WITH (index(SI_mupolygon_24876)) " |
@@ -141,10 +133,10 @@ |
return query; |
} |
|
- public static String RHEM01Query02(String climateStationId) { |
+ public static String RHEM07Query01(String climatestationId) { |
String query = "SELECT avg_yearly_precip_mm " |
+ "FROM rhem.d_rhem_climate_stations_avg_300yr_est_rain " |
- + "WHERE station_id = '" + climateStationId + "';"; |
+ + "WHERE station_id = '" + climatestationId + "';"; |
return query; |
} |
|