RhemModel.java [src/java/m/rhem/model] Revision: 3c2dc15593a0f02cb989710cf4496f9cd207e401 Date: Mon Jul 20 14:59:16 MDT 2020
package m.rhem.model;
import csip.ServiceException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import org.apache.commons.io.FileUtils;
/**
*
* @author rumpal
*/
public class RhemModel {
String stateId;
String climatestationId;
String scenarioName;
String today;
File workSpaceDir;
String parameterFileName;
String stormFileName;
String runFileName;
String summaryFileName;
double avgSoilLoss;
public RhemModel(String stateId, String climatestationId, String scenarioName,
String today, File workSpaceDir, String parameterFileName,
String stormFileName, String runFileName, String summaryFileName) {
this.stateId = stateId;
this.climatestationId = climatestationId;
this.scenarioName = scenarioName;
this.today = today;
this.workSpaceDir = workSpaceDir;
this.parameterFileName = parameterFileName;
this.stormFileName = stormFileName;
this.runFileName = runFileName;
this.summaryFileName = summaryFileName;
}
public void generateParamFile(Parameter param) throws ServiceException {
try (PrintWriter writer = new PrintWriter(new File(workSpaceDir, parameterFileName))) {
writer.println("! Parameter file for scenario: " + scenarioName);
writer.println("! Date built: " + today + " (Version 2.3)");
writer.println("! Parameter units: DIAMS(mm), DENSITY(g/cc),TEMP(deg C)");
writer.println("BEGIN GLOBAL");
writer.println(String.format(" %-8s= %s", "CLEN", param.clen));
writer.println(String.format(" %-8s= %s", "UNITS", "metric"));
writer.println(String.format(" %-8s= %s", "DIAMS", param.diams));
writer.println(String.format(" %-8s= %s", "DENSITY", param.density));
writer.println(String.format(" %-8s= %s", "TEMP", 40));
writer.println(String.format(" %-8s= %s", "NELE", 1));
writer.println("END GLOBAL");
writer.println("BEGIN PLANE");
writer.println(String.format(" %-8s= %s", "ID", 1));
writer.println(String.format(" %-8s= %s", "LEN", param.len));
writer.println(String.format(" %-8s= %s", "WIDTH", 1.0));
writer.println(String.format(" %-8s= %s", "CHEZY", param.chezy));
writer.println(String.format(" %-8s= %s", "RCHEZY", param.rchezy));
writer.println(String.format(" %-8s= %s", "SL", param.sl));
writer.println(String.format(" %-8s= %s", "SX", param.sx));
writer.println(String.format(" %-8s= %s", "CV", param.cv));
writer.println(String.format(" %-8s= %s", "SAT", param.sat));
writer.println(String.format(" %-8s= %s", "PR", 1));
writer.println(String.format(" %-8s= %s", "KSS", param.kss));
writer.println(String.format(" %-8s= %s", "KOMEGA", param.komega));
writer.println(String.format(" %-8s= %s", "KCM", param.kcm));
writer.println(String.format(" %-8s= %s", "CA", 1.0));
writer.println(String.format(" %-8s= %s", "IN", 0.0));
writer.println(String.format(" %-8s= %s", "KE", param.ke));
writer.println(String.format(" %-8s= %s", "G", param.g));
writer.println(String.format(" %-8s= %s", "DIST", param.dist));
writer.println(String.format(" %-8s= %s", "POR", param.por));
writer.println(String.format(" %-8s= %s", "ROCK", 0.00));
writer.println(String.format(" %-8s= %s", "SMAX", 1.0));
writer.println(String.format(" %-8s= %s", "ADF", param.adf));
writer.println(String.format(" %-8s= %s", "ALF", param.alf));
writer.println(String.format(" %-8s= %s", "BARE", param.bare));
writer.println(String.format(" %-8s= %s", "RSP", 1.0));
writer.println(String.format(" %-8s= %s", "SPACING", 1.0));
writer.println(String.format(" %-8s= %s", "FRACT", param.fract));
writer.println("END PLANE");
} catch (IOException E) {
throw new ServiceException("Problem in generating the parameter file.", E);
}
}
public void generateStormFile(String cligen_db, double Ke) throws ServiceException, URISyntaxException {
try {
if (isIntl()) {
try {
URL u = new URL(cligen_db + "/international/" + climatestationId + ".txt");
Files.createSymbolicLink(Paths.get(workSpaceDir.toString(), stormFileName), Paths.get(u.toURI()));
} catch (IOException E) {
throw new ServiceException("Climate Station not found: " + climatestationId);
}
return;
}
String cligenFileName = "/" + stateId.toLowerCase() + "/300yr/" + stateId + "_" + climatestationId + "_300yr.out";
URL u = new URL(cligen_db + cligenFileName);
long counter = 0;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(u.openStream()))) {
File tempFile = new File(workSpaceDir, "tempSummaryFile.pre");
try (PrintWriter writer = new PrintWriter(tempFile)) {
for (int i = 0; i <= 17; i++) {
bufferedReader.readLine();
}
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] test = line.split("\\s+");
double keComparison = Double.parseDouble(test[8]) * (Double.parseDouble(test[5]) / Double.parseDouble(test[6]));
if (Ke < keComparison && test.length > 8) {
counter++;
writer.println(String.format(" %-5s %-5s %-5s %-5s %-5s %-6s %-6s %-6s",
counter, test[2], test[3], test[4], test[5], test[6], test[7], test[8]));
}
}
}
appendInfo(tempFile, counter);
tempFile.delete();
}
} catch (IOException E) {
throw new ServiceException("Problem in generating the storm file.", E);
}
}
void appendInfo(File tempFile, long counter) throws ServiceException {
try (PrintWriter writer = new PrintWriter(new File(workSpaceDir, stormFileName))) {
writer.println("# Storm file for scenario: " + scenarioName);
writer.println("# Date built: " + today + " (Version 2.3)");
writer.println("# State: " + stateId);
writer.println("# Climate Station: " + climatestationId);
writer.println(counter + " # The number of rain events");
writer.println("0 # Breakpoint data? (0 for no, 1 for yes)");
writer.println("# id day month year Rain Dur Tp Ip");
writer.println("# (mm) (h)");
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(tempFile))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
writer.println(line);
}
}
} catch (IOException E) {
throw new ServiceException("Problem in generating the storm file.", E);
}
}
public void generateRunFile() throws ServiceException, IOException {
FileUtils.writeStringToFile(new File(workSpaceDir, runFileName),
parameterFileName + ", " + stormFileName + ", "
+ summaryFileName + ", \"" + scenarioName + "\", 0, 2, y, y, n, n, y");
}
public void appendToSumFile(double avgYearlyPrecip) throws ServiceException {
DecimalFormat df = new DecimalFormat("#.#####");
File summaryFile = new File(workSpaceDir, summaryFileName);
File tempSummaryFile = new File(workSpaceDir, "temp_" + summaryFileName);
try (PrintWriter writer = new PrintWriter(tempSummaryFile)) {
writer.println(" -ANNUAL-AVERAGES-");
writer.println();
writer.println("Avg-Precipitation(mm/year)= " + df.format(avgYearlyPrecip));
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(summaryFile))) {
for (int i = 0; i < 3; i++) {
bufferedReader.readLine();
}
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.toLowerCase().contains("variable")) {
int c = 0;
while (c < 6) {
if (c != 0) {
line = bufferedReader.readLine();
}
String[] test = line.split("\\s+");
if (c == 2) {
writer.print(String.format("%-25s", test[1] + test[2]));
for (int i = 3; i < test.length; i++) {
writer.print(String.format("%-25s", test[i]));
}
} else {
for (int i = 1; i < test.length; i++) {
writer.print(String.format("%-25s", test[i]));
}
}
writer.println();
c++;
}
} else {
if (line.contains("Avg-Soil-Loss")) {
String asl = line.substring(line.indexOf('=') + 1);
avgSoilLoss = Double.parseDouble(asl.trim());
}
writer.print(line);
writer.println();
}
}
}
} catch (IOException E) {
throw new ServiceException("Problem in editing the summary file.", E);
}
summaryFile.delete();
tempSummaryFile.renameTo(summaryFile);
}
public String getTDS() {
return Parameter.formatDouble((2.36 * (avgSoilLoss * 1000) + 0.99) / 1000.0);
}
public boolean isIntl() {
return stateId.toLowerCase().startsWith("intl_");
}
public double getYearlyPrecipIntl() throws Exception {
File sf = new File(workSpaceDir, stormFileName);
try (BufferedReader r = new BufferedReader(new FileReader(sf))) {
String line;
while ((line = r.readLine()) != null) {
if (line.contains("Observed monthly ave precipitation (mm)")) {
line = r.readLine();
String v[] = line.trim().split("\\s+");
if (v.length != 12) {
throw new ServiceException("cannot find monthly ave precip in " + sf.toString());
}
double sum = 0.0;
for (int i = 0; i < v.length; i++) {
sum += Double.parseDouble(v[i]);
}
return sum;
}
}
}
throw new ServiceException("cannot find monthly ave precip in " + sf.toString());
}
}