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 main(): ''' The main section to determine is the script is executed within or outside of the CAVI environment ''' if cavi_env: # This is all the stuff to do if in the CAVI script_name = "{}.py".format(arg2) tw = cavistatus.get_timewindow() if tw == None: msg = "No forecast open on Modeling tab to get a timewindow." cumulus_logger.warning(msg) MessageBox.showWarning(msg, "No Forecast Open" ) sys.exit() db = os.path.join(cavistatus.get_database_directory(), "grid.dss") cwms_home = cavistatus.get_working_dir() common_exe = os.path.join(os.path.split(cwms_home)[0], "common", "exe") cumulus_logger.debug("DSS file default: {}".format(db)) cumulus_logger.debug("CWMS working directory: {}".format(cwms_home)) cumulus_logger.debug("Jython execution directory: {}".format(common_exe)) cumulus_config = os.path.join(APPDATA, "cumulus.config") this_script = os.path.join(cavistatus.get_project_directory(), "scripts", script_name) cmd = "@PUSHD {pushd}\n" cmd += '@Jython.bat "{script}" ' cmd += '"{start}" "{end}" "{dss}" "{home}" "{config}"' cmd = cmd.format(pushd=common_exe, script=this_script, start=tw[0], end=tw[1], dss=db, home=cwms_home, config=cumulus_config ) # Create a temporary file that will be executed outside the CAVI env batfile = tempfile.NamedTemporaryFile(mode='w', suffix='.cmd', delete=False) batfile.write(cmd) batfile.close() cumulus_logger.info("Initiated Subprocess") p = subprocess.Popen("start cmd /C " + batfile.name, shell=True) # os.remove(batfile.name) else: # This is all the stuff to do if initiated outside the CAVI environment args = sys.argv[1:] if len(args) < 5: MessageBox.showPlain( "Expecting five arguments. {} arguments given".format(len(args)), "Exception" ) raise Exception("Need more arguments") keys = ['start_time', 'end_time', 'dss_path', 'cwms_home', 'config'] ordered_dict = dict(zip(keys, args)) # Set the look and feel # Metal, Nimbus, CDE/Motif, Windows, or Windows Classic LookAndFeel("Nimbus") cui = CumulusUI(ordered_dict) cui.setVisible(True)
def postRun(self): """ Run additional tasks at the end of the core run. By default this method refreshes the HEC-DSSVue catalogue (if :attr:`.refreshCatalogue` is true) and displays any string in :attr:`.message`. """ if self.refreshCatalogue and self.mainWindow: self.mainWindow.updateCatalog() if self.message: MessageBox.showInformation(self.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
def json_build(self): '''Return JSON string or 'None' if failed The returning JSON string is from the UI and used when POSTing to the Cumulus API. ''' conf = { 'datetime_start': None, 'datetime_end': None, 'watershed_id': None, 'product_id': None, } try: tf = TimeFormatter() tz = tf.zid st = tf.parse_zoned_date_time(self.txt_start_time.getText(), tz) et = tf.parse_zoned_date_time(self.txt_end_time.getText(), tz) conf['datetime_start'] = st.format(tf.iso_instant()) conf['datetime_end'] = et.format(tf.iso_instant()) except DateTimeParseException as ex: MessageBox.showWarning(ex, "Exception") selected_watershed = self.lst_watershed.getSelectedValue() selected_products = self.lst_product.getSelectedValues() if selected_watershed is not None: watershed_id = self.basin_meta[selected_watershed]['id'] conf['watershed_id'] = watershed_id else: MessageBox.showWarning( "No Watershed Selected", "Exception" ) return None product_ids = list() if len(selected_products) > 0: for p in selected_products: product_ids.append(self.product_meta[p]['id']) conf['product_id'] = product_ids else: MessageBox.showWarning( "No Products Selected", "Exception" ) return None return json.dumps(conf)
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")
'-host=' + host, '-endpoint=' + endpoint, '-after=' + after, '-before=' + before, '-scheme=' + scheme, '-stdout' ]) print('Subprocess Command: {}'.format(CMD)) sp = subprocess.Popen( CMD, shell=True, cwd=os.path.join(RSGIS, 'rtsutils'), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) dss = HecDss.open(dbdss, DSSVERSION) byte_array = bytearray() for b in iter(partial(sp.stdout.read, 1), b''): byte_array.append(b) if b == '}': obj = json.loads(str(byte_array)) byte_array = bytearray() if 'message' in obj.keys(): raise Exception(obj['message']) msg = put_to_dss(obj, dss) if msg: print(msg) if dss: dss.close() print('Script Done!') _end = datetime.now() dur = _end - _start MessageBox.showInformation('Download to: {}\n\nAfter: {}\n\nBefore: {}\n\nTime Duration: {}'.format(dbdss, after, before, dur), script_name)
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()
import sys from hec.heclib.util import HecTime from hec.script import MessageBox script_name = arg2 try: # Add rtsutils package to sys.path before importing sys.path.append(os.path.join(os.environ['APPDATA'], "rsgis")) from rtsutils import cavistatus, usgs except ImportError, ex: raise tw = cavistatus.get_timewindow() if tw != None: st, et = tw print("Time window: {}".format(tw)) else: raise Exception('No Forecast open or in "Setup Tab"') rts_dss = os.path.join( cavistatus.get_database_directory(), '{}-usgs-data.dss'.format(cavistatus.get_watershed().getName())) retrieve = usgs.USGSDataRetrieve() retrieve.set_dssfilename(rts_dss) retrieve.set_begin_date(st) retrieve.set_end_date(et) retrieve.set_timezone('GMT') retrieve.set_tzdss('GMT') loc_file = os.path.join(cavistatus.get_shared_directory(), 'usgs_locations.csv') retrieve.set_locations_file(loc_file) retrieve.run() MessageBox.showInformation("Script '{}' done!".format(script_name), "End Process")
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))