/** * This file is part of the RAS Java Library Documentation, it shows how to run RAS using Java * Author: Richard Rachiele (RMA) */ package hec.ras.model; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import rma.util.RMAIO; import hec.heclib.dss.DSSPathname; import hec.heclib.util.HecTime; import hec.io.TimeSeriesContainer; import hec.heclib.util.HecTime; import hec.ras.io.DSSFactory; import hec.ras.io.DSSFactoryImpl; import hec.ras.io.GeometryFile; import hec.ras.io.UnsteadyFlowFile; import hec.ras.io.PlanFile; import hec.ras.io.RasProjectFile; /** * @author Mark Ackerman * */ public class RasStandaloneTester { /** * @param args */ public static void main(String[] args) { if (args.length < 4 ) { System.err.println("main:USAGE:" + RasStandaloneTester.class.getName() + " RasProjectFile PlanExtension \"01FEB1999, 1200\" \"21FEB1999, 2400\""); System.exit(1); } RasProjectFile prjFile = new RasProjectFile(args[0]); if (!prjFile.readProjectFile()) { System.err.println("main:failed to read file " + args[0]); System.exit(2); } List plans = prjFile .getManagerEntriesFor(ManagerEntry.PLAN); if (plans.size() > 0) { // ManagerEntry entry = plans.get(0); PlanData plan = prjFile.getPlan(args[1]); if (plan == null) { System.err.println("main:failed to get Plan for "+args[1]); System.exit(3); } GeometryData geoData = plan.getGeometry(); if (geoData == null) { System.err.println("main:failed to get Geometry for " + plan.getGeometryPath()); } if (plan.isUnsteadyFlow()) { String startTimeStr = args[2]; String endTimeStr = args[3]; unsteadyFlowCompute(plan, startTimeStr, endTimeStr); } else { steadyFlowCompute(plan); } } System.exit(0); } /** * @param plan */ private static void steadyFlowCompute(PlanData plan) { HecTime start = new HecTime("06NOV2008, 0000"); HecTime end = new HecTime("08NOV2008, 0000"); String planFile = ((PlanFile) plan).getFilename(); String planFileName = RMAIO.getFileNameNoExtension(planFile); String planDir = RMAIO.getDirectoryFromPath(planFile); String planExt = RMAIO.getFileExtension(planFile); String dssFile = planDir + "/forecast.dss"; FlowData flowData = plan.getFlow(); if (flowData == null) { System.err.println("main:failed to read flowfile " + plan.getFlowPath()); System.exit(7); } boolean doIntervalFlows = true; boolean doPeakFlows = true; boolean doLinearInterp = true; int timeStepMin = 720; DssImportOptions dssOpt = flowData.getDssImportOptions(); if (dssOpt != null) { timeStepMin = dssOpt.intervalMinutes; if (timeStepMin < 15 || timeStepMin > 30. * 24. * 60.) timeStepMin = 720; // default doIntervalFlows = dssOpt.doIntervals; doPeakFlows = dssOpt.doPeakFlows; // doLinearInterp = dssOpt.doLinearInterp } if (!flowData.importFromDss(flowData, doIntervalFlows, doPeakFlows, doLinearInterp, start, end, timeStepMin)) { System.err.println("main:Error in Time-Series Import for RAS"); System.exit(8); } // now launch RAS binaries OutputRasDss outDss = new OutputRasDss(); if ( !outDss.exportDss(plan, dssFile, planFileName, dssFile)) { System.err.println("main:Error in Time-Series Output for RAS"); System.exit(9); } } /** * @param plan */ private static void unsteadyFlowCompute(PlanData plan, String startTimeStr, String endTimeStr) { // HecTime start = new HecTime("01JAN1999, 1200"); // HecTime end = new HecTime("05JAN1999, 0000"); HecTime start = new HecTime(startTimeStr); HecTime end = new HecTime(endTimeStr); UnsteadyFlowData unsteadyFlowData = plan.getUnsteadyFlow(); if (unsteadyFlowData == null) { System.err.println("main:failed to read flowfile " + plan.getFlowPath()); System.exit(4); } String planFile = ((PlanFile) plan).getFilename(); String planFileName = RMAIO.getFileNameNoExtension(planFile); String planDir = RMAIO.getDirectoryFromPath(planFile); String planExt = RMAIO.getFileExtension(planFile); String dssFile = planDir + "/forecast.dss"; String bcExt = planExt.substring(2); bcExt = "b".concat(bcExt); String bcFile = planDir + "/" + planFileName + "." + bcExt; System.err.println("main:writing 'bc' file " + bcFile); String user = System.getProperty("user.name"); String geoFile = ((GeometryFile)plan.getGeometry()).getFilename(); String geoFilename = RMAIO.getFileNameNoExtension(geoFile); String geoExt = RMAIO.getFileExtension(geoFile); geoExt = geoExt.substring(2); geoExt = "x".concat(geoExt); String xFile = planDir+"/"+geoFilename+"."+geoExt; if (plan.isRunHTab()) { if (!plan.writeRunFile(user, plan.isRunHTab())) { // write .x file System.err.println("main:failed to write SNET file"); System.exit(5); } } Process htab = null; String cmd = getCommand("GeomPreprocessor.exe "); cmd = cmd.concat(xFile); cmd = cmd.replace('/', java.io.File.separatorChar); System.out.println(cmd); System.out.println((new Integer(cmd.length())).toString()); if ( !launchExe(cmd)) { System.exit(5); } // need to rename the *.t and *.c files (add number extension) int idx = xFile.lastIndexOf("."); String inpath = xFile.substring(0, idx+2); String outpath = xFile; StringBuffer inpathbuf = new StringBuffer(inpath); StringBuffer outpathbuf = new StringBuffer(outpath); // rename xx.c01.tmp file inpathbuf.setCharAt(idx+1,'c'); outpathbuf.setCharAt(idx+1,'c'); inpath = inpathbuf.toString(); outpath = outpathbuf.toString(); inpath = outpath + ".tmp"; File outfile = new File(outpath); if ( outfile.exists()) { outfile.delete(); } File inFile = new File(inpath); inFile.renameTo( outfile ); if (plan.isRunUNET()) { if (!plan.writeUnsteadyBC(user, bcFile, dssFile, "", "P0", start, end, 2)) { // write .b file System.err.println("main:failed to write BC file " + bcFile); System.exit(6); } } // write a modified unsteady flow file if (plan.isRunUNET()) { plan.writeForecastHydrographsToDss(user, dssFile, "", "P0", start, end); ((PlanFile)plan).writeForecastPlanFile( "otherplan.p01", start, end); // multiply flow by 2 as test RiverStationDataList ubcList = unsteadyFlowData.getUnsteadyBCList(); int nsize = ubcList.size(); UnsteadyBC bc; for ( int ibc=0; ibc 0 ) { unsteadyFlowData.clearBCList(); for ( int ibc=0; ibc reachList = new ArrayList(); for (int irvr=0; irvrprofileList = new ArrayList(profiles.length); for (int i=0; i>rowList = fillOpData(op, reachList, profileList, std1Table); op.close(); for (int i = 0;i < rowList.size(); i++ ) { System.out.println(rowList.get(i)); } } public static boolean exportHydrographToDss( Hydrograph hyd, String locationName, String dssFilename ) { double[] tims = hyd.bfile_Times; double[] vals = hyd.bfile_values; String startTime = hyd.bfile_startTime; String endTime = hyd.bfile_endTime; HecTime hecStartTime = new HecTime( startTime, HecTime.MINUTE_INCREMENT ); HecTime hecEndTime = new HecTime( startTime, HecTime.MINUTE_INCREMENT ); int intervalMinutes = (int)( 60.*(tims[1] - tims[0]) + .01 ); DSSPathname dsspathname = new DSSPathname( hyd.dssPathname ); dsspathname.setFPart( "bc check 1" ); exportHydrographToDss( locationName, dssFilename, dsspathname.getPathname(), hecStartTime, intervalMinutes, tims, vals ); return true; } public static boolean exportHydrographToDss( String locationName, String dssFilename, String dssPathname, HecTime startTime, int intervalMinutes, double[] tims, double[] vals ) { // get the dss file manager //DSSFactory dssmgr = getDSSFactory(); DSSFactory dssmgr = DSSFactoryImpl.getInstance(); if ( dssmgr == null ) return false; int npts = tims.length; int[] itimes = new int[npts]; try { // read dss data TimeSeriesContainer timeSeriesContainer = new TimeSeriesContainer(); // change times from double (hrs) to int minutes int istartTime = (int)startTime.getMinutes(); for ( int i=0; i> fillOpData(OP op, List selectedReaches, List selectedProfiles, String[] selectedParams) { int nprofiles = op.getNumberOfProfiles(); // make boolean array indicating selected profiles boolean[] profileSelected = new boolean[nprofiles]; Arrays.fill(profileSelected, false); String[] allProfiles = op.getProfileNames(); for ( int i=0; i headerList = new ArrayList(); List headerList2 = new ArrayList(); // Prepare table int col = 0; // Make some rows to start with. (Speeds up the table fill) int nSta = op.getAllRiverStations().size(); int nProf = selectedProfiles.size(); List rowList = new ArrayList(); // Create vector of vectors int totalRows = nSta*nProf+5; if (nProf > 1) { totalRows+= nSta + 1; // space between stations for nProf > 1 } RiverStationDataList riverStations = op.getAllRiverStations(); // Start filling rows int row = 0; int m = 0; OPVar[] opVarArr = new OPVar[selectedParams.length]; List columnDescriptions = new ArrayList(); double[] unitConversion = new double[14]; boolean isSI = false; // show in standard english units while (m < selectedParams.length) { OPVar opVar = op.getOPVar( selectedParams[m]); headerList.add(selectedParams[m]); headerList2.add(opVar.getUnits(isSI)); columnDescriptions.add(opVar.getDescription()); unitConversion[m] = opVar.getUnitConversion(isSI); // store opVar in an array for use in building table. // This saves lots of time when building table opVarArr[m] = opVar; // reset column width for long names String ctitle = selectedParams[m]; col++; m++; } row++; System.out.println("_headerVec2 " + headerList.size()); System.out.println("_riverStations " + riverStations.size()); System.out.println("selectedReaches " + selectedReaches.size()); NumberFormat nf = NumberFormat.getInstance(); RiverStationData rs; String reachTitle = ""; String riverTitle = ""; col = 0; // Reset column for( int l = 0; l < riverStations.size(); l++) { for ( int ireach=0; ireach rowData = new ArrayList(colMax); // multiple profile lines for normal xsection only if (rs.type != 1 && k > 0) { nprof++; continue; } if(op.getNumberOfRivers() > 1) { rowData.add(rs.riverName); col++; } // First or second column contains the reachName rowData.add(rs.reachName); col++; // Second (or third ) column contains the station name rowData.add(rs.stationName); col++; // add Profile name if ( rs.type == 1 ) { rowData.add(allProfiles[k]); } else { rowData.add(""); } col++; // Now fill in all the other columns m = 0; // Normal Cross-section node if (rs.type == 1) { while (m < selectedParams.length) { // Format the data that is written in the table OPVar opVar = opVarArr[m]; nf.setMaximumFractionDigits(opVar.getDecimalPlaces()); nf.setMinimumFractionDigits(opVar.getDecimalPlaces()); //System.out.println(opVar.getUnits_nDecs()); // float pval = _op.getParamValue(op_var[m], rs.riverName, rs.reachName, rs.stationName, k); float pval = op.getParamValue(opVar, rs.riverName, rs.reachName, rs.stationName, k); if (pval >= 3.0e+38) { rowData.add(""); } else { pval *= unitConversion[m]; rowData.add(nf.format(pval)); } col++; m++; } } // if not a normal cross-section, just print structure type in first column else if (rs.type == 2) { rowData.add("Culvert"); } else if (rs.type == 3) { rowData.add("Bridge"); } else if (rs.type == 4) { rowData.add("Mult Open"); } else if (rs.type == 5) { rowData.add("Weir"); } else if (rs.type == 6) { rowData.add("Lat. Weir"); } rowList.add(rowData); row++; // Move to next row col = 0; // Reinitialize the column nprof++; } if(nprof > 1) { rowList.add(new ArrayList()); } } } } String title; // Set the label across the top of the table if ( reachTitle.length() > 0 ) { title = "HEC-RAS Plan: " + op.getPlan() + " River: " + riverTitle + " Reach: " + reachTitle; } else { title = "HEC-RAS Plan: " + op.getPlan(); } System.out.println(title); System.out.println(headerList); System.out.println(headerList2); return rowList; } /** * @param cmd * @return */ private static boolean launchExe(String cmd) { String osname = System.getProperty("os.name"); if (osname.indexOf("Window") > -1) { Runtime rt = Runtime.getRuntime(); Process proc; try { proc = rt.exec("rmaStart " + cmd); } catch (IOException e) { System.err.println("launchExe:failed to launch "+cmd +" Error:"+e); e.printStackTrace(); return false; } if ( proc != null ) { int istatus = -1; try { istatus = proc.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); return false; } if ( istatus != 0 ) { return false; } return true; } } return false; } /** * @param string * @return */ private static String getCommand(String executable) { String rasDir = System.getProperty("RAS_DIR"); if ( rasDir != null ) { return rasDir.concat(RMAIO.separator).concat(executable); } File f = new File("bin".concat(RMAIO.separator).concat(executable)); if ( f.exists()) { return f.getAbsolutePath(); } // hope that its in the path return executable; } }