def http_get(self, url): '''Return java.lang.String JSON Input: java.lang.String URL ''' start_timer = System.currentTimeMillis() try: url = URL(url) urlconnect = url.openConnection() br = BufferedReader( InputStreamReader( urlconnect.getInputStream(), "UTF-8" ) ) s = br.readLine() br.close() except MalformedURLException() as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise except IOException as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise end_timer = System.currentTimeMillis() cumulus_logger.debug( "HTTP GET (milliseconds): {}".format( (end_timer - start_timer) ) ) return s
def _displayConfigErrors(self, errors): """ Display configuration errors in the HEC-DSSVue window. """ message = "The configuration file {} is not valid.\nPlease check the content and try again.".format(self.configFilePath) message += "\n" for error in errors: message += "\n - {}".format(error) MessageBox.showError(message, "HEC-DSSVue")
def http_post(self, json_string, url): '''Return java.lang.String JSON Input: java.lang.String JSON, java.lang.String URL ''' start_timer = System.currentTimeMillis() try: # Get a connection and set the request properties url = URL(url) urlconnect = url.openConnection() urlconnect.setDoOutput(True) urlconnect.setRequestMethod("POST") urlconnect.setRequestProperty("Content-Type", "application/json; UTF-8") urlconnect.setRequestProperty("Accept", "application/json") # Write to the body bw = BufferedWriter( OutputStreamWriter( urlconnect.getOutputStream() ) ) bw.write(json_string) bw.flush() bw.close() # Read the result from the POST br = BufferedReader( InputStreamReader( urlconnect.getInputStream(), "UTF-8" ) ) s = br.readLine() br.close() except MalformedURLException() as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise Exception(ex) except IOException as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise Exception(ex) end_timer = System.currentTimeMillis() cumulus_logger.debug( "HTTP GET (milliseconds): {}".format( (end_timer - start_timer) ) ) return s
def _toolIsValid(self): """ Check if the tool is configured correctly with a valid config file and HEC-DSS database. """ # Check if HEC-DSS db exists if not path.isfile(self.dssFilePath): error = ValidationError("Please open a HEC-DSS database first.") MessageBox.showError(error.message, "HEC-DSSVue") raise error # Check if config file exists if not path.isfile(self.configFilePath): error = ValidationError("The configuration file {} does not exist.\n\nPlease create this file and try again.".format(self.configFilePath)) MessageBox.showError(error.message, "HEC-DSSVue") raise error return 1
myDss = HecDss.open(DSS_OUTPUT_FILE) fileName = DISCHARGE_CSV_FILE.rsplit('.', 1) # str .format not working on this version fileName = '%s-%s%s.%s' % (fileName[0], date, '.' + tag if tag else '', fileName[1]) DISCHARGE_CSV_FILE_PATH = os.path.join(OUTPUT_DIR, fileName) print 'Open Discharge CSV ::', DISCHARGE_CSV_FILE_PATH csvWriter = csv.writer(open(DISCHARGE_CSV_FILE_PATH, 'w'), delimiter=',', quotechar='|') flow = myDss.get('//HANWELLA/FLOW//1HOUR/RUN:RUN 1/', 1) if flow.numberValues == 0: MessageBox.showError('No Data', 'Error') else: csvWriter.writerow(['Location Ids', 'Hanwella']) csvWriter.writerow(['Time', 'Flow']) print flow.values[:1], flow.times[:1] print flow.values[-1], flow.times[-1] csvList = [] for i in range(0, flow.numberValues): # print int(flow.times[i]) time = HecTime() time.set(int(flow.times[i])) d = [
# relative filename to VSCode workspace directory # , assuming running in vscode with .vscode/tasks.json and scripts/go.bat if len(sys.argv) == 2: filename = sys.argv[1] + "/data/sample.dss" print("filename = ", filename) # Open the file and get the data try: dssFile = HecDss.open(filename) airTemp = dssFile.get("/GREEN RIVER/OAKVILLE/AIRTEMP/01MAY1992/1HOUR/OBS/") outflow = dssFile.get( "/GREEN RIVER/OAKVILLE/FLOW-RES OUT/01MAY1992/1HOUR/OBS/") dssFile.done() except java.lang.Exception, e: # Take care of any missing data or errors MessageBox.showError(e.getMessage(), "Error reading data") # Initialize the plot and add data plot = Plot.newPlot("Oakville") plot.addData(airTemp) plot.addData(outflow) # Create plot plot.setSize(600, 600) plot.setLocation(100, 100) plot.showPlot() # Change the plot outCurve = plot.getCurve(outflow) outCurve.setLineColor("darkgreen")
from hec.script import Plot,MessageBox from hec.heclib.dss import HecDss import java try : myDss = HecDss.open("C:/temp/sample.dss") flow = myDss.get("/MISSISSIPPI/ST. LOUIS/FLOW//1DAY/OBS/", 1) if flow.numberValues == 0 : MessageBox.showError("No Data", "Error") else : plot = Plot.newPlot("Mississippi") plot.addData(flow) plot.showPlot() plot.stayOpen() except Exception, e : MessageBox.showError(' '.join(e.args), "Python Error") except java.lang.Exception, e : MessageBox.showError(e.getMessage(), "Error") finally : myDss.done()
from hec.heclib.dss import HecDss, HecTimeSeries from hec.io import TimeSeriesContainer from hec.heclib.util import HecTime import java try: myDss = HecDss.open("myFile.dss") tsc = TimeSeriesContainer() tsc.fullName = "/Basin/loc/FLOW/01NOV2002/1Hour//" start = HecTime("01Nov2022", "0800") tsc.interval = 60 flows = [0.0, 2.0, 1.0, 4.0, 3.0, 6.0, 5.0, 8.0, 7.0, 9.0] times = [] for value in flows: times.append(start.value()) start.add(tsc.interval) tsc.times = times tsc.values = flows tsc.numberValues = len(flows) tsc.units = "CFS" tsc.type = "PER-AVER" myDss.put(tsc) except Exception, e: MessageBox.showError(''.join(e.args), "Python Error") finally: myDss.close()
def submit(self, event): '''Collect user inputs and initiate download of DSS files to process. Event is a java.awt.event.ActionEvent ''' start_timer = end_timer = System.currentTimeMillis() # Build the JSON from the UI inputs and POST if we have JSON json_string = self.json_build() cumulus_logger.debug("JSON String Builder: {}".format(json_string)) if json_string is not None: cumulus_logger.info("*" * 50) cumulus_logger.info("Initiated Cumulus Product Request") cumulus_logger.info("*" * 50) post_result = self.http_post(json_string, url_downloads) json_post_result = json.loads(post_result) id = json_post_result['id'] max_timeout = 180 while max_timeout > 0: get_result = self.http_get("/".join([url_downloads, id])) if get_result is not None: json_get_result = json.loads(get_result) progress = json_get_result['progress'] #100% stat = json_get_result['status'] #SUCCESS fname = json_get_result['file'] # not null cumulus_logger.info("Status: {:>10}; Progress: {:>4.1f}%; Timeout: {:>4}".format(stat, progress, max_timeout)) if stat == 'FAILED': cumulus_logger.error("Failed to load grid products.") MessageBox.showError( "Failed to load grid products.", "Failed Download" ) break if int(progress) == 100 and stat == 'SUCCESS' and fname is not None: dest_dssfile = self.txt_select_file.getText() cumulus_logger.debug("DSS Download Filname: {}".format(fname)) downloaded_dssfile = download_dss(fname) if downloaded_dssfile is not None: cumulus_logger.info("DSS file downloaded.") merged_dssfiles = merge_dss(downloaded_dssfile, dest_dssfile) if len(merged_dssfiles) > 0: end_timer = System.currentTimeMillis() msg = "DSS file downloaded and merged to: {}".format( '\n'.join([f for f in merged_dssfiles]) ) cumulus_logger.info(msg) MessageBox.showInformation(msg, "Successful Processing" ) else: msg = "DSS file merge unsuccessful" cumulus_logger.warning(msg) MessageBox.showWarning(msg, "Unsuccessful Merge" ) else: msg = "Downloading and processing the DSS file failed!" cumulus_logger.error(msg) MessageBox.showError(msg, "Failed Processing" ) break else: Thread.sleep(2000) max_timeout -= 1 cumulus_logger. info( "Submit time duration (milliseconds): {}".format( (end_timer - start_timer) ) ) # Try to clean up any dss6 and dss7 files in the temp try: tempdir = tempfile.gettempdir() dss_temp_files = os.listdir(tempdir) for f in dss_temp_files: if (f.endswith(".dss") or f.endswith(".dss")): os.remove(os.path.join(tempdir, f)) except OSError as ex: cumulus_logger.warning(str(ex))