''' Read DSS-6 Grid record Copy DSS-6 Grid to DSS-7 file ''' from pydsstools.heclib.dss.HecDss import Open from pydsstools.heclib.utils import dss_logging dss_logging.config(level='Diagnostic') dss6_file = "example_dss6.dss" dss7_file = "example.dss" pathname_in = "/SHG/MAXTEMP/DAILY/08FEB1982:0000/08FEB1982:2400/PRISM/" pathname_out = "/SHG/MAXTEMP/DAILY/08FEB1982:0000/08FEB1982:2400/Ex11/" with Open(dss6_file) as fid: dataset = fid.read_grid(pathname_in) data = dataset.read() profile = dataset.profile with Open(dss6_file) as fidin, Open(dss7_file) as fidout: dataset = fidin.read_grid(pathname_in) fidout.put_grid(pathname_out,dataset,compute_range = True) # recomputing range limit table
Write Spatial Grid record Notes: type options: PER-AVER, PER-CUM, INST-VAL,INST-CUM, FREQ, INVALID flipud: 1 (default) - flips the numpy array upside down as dss array layout is opposite 0 - numpy array array is stored as it is ''' import numpy as np from pydsstools.heclib.dss.HecDss import Open dss_file = "example.dss" pathname_in = "/GRID/RECORD/DATA/01jan2001:1200/01jan2001:1300/Ex9/" pathname_out = "/GRID/RECORD/DATA/01jan2019:1200/01jan2019:1300/Ex10/" with Open(dss_file) as fid: # get shape and profile from example grid dss record dataset_in = fid.read_grid(pathname_in) profile_in = dataset_in.profile grid_in = dataset_in.read() # create random array, update profile and save as grid record profile_out = profile_in.copy() profile_out.update(type="PER-AVER",nodata=0,transform=dataset_in.transform) # required parameters profile_out.update(crs="UNDEFINED",units="ft",tzid="",tzoffset=0,datasource='') # optional parameters grid_out = np.random.rand(*grid_in.shape)*100 fid.put_grid(pathname_out,grid_out,profile_out,flipud=1)
# Experimental geospatial methods for grid # Not 100% sure about gridinfo that is computed for the cropped grid esp. for SHG and HRAP # Will apreaciate user feedbacks on this # This example code was tested using the following libraries # gdal 3.2.2 # matplotlib 3.4.4 # rasterio 1.2.1 # Potential rasterio issue with CRS # https://github.com/mapbox/rasterio/blob/master/docs/faq.rst#why-cant-rasterio-find-projdb # Unset PROJ_LIB environmental variable (i.e., SET PROJ_LIB= ) from pydsstools.heclib.dss.HecDss import Open from pydsstools.heclib.utils import BoundingBox dss_file = "example.dss" pathname = r"/SHG/LCOLORADO/PRECIP/02JAN2020:1500/02JAN2020:1600/Ex15/" pathname_out = r"/SHG/LCOLORADO/PRECIP/02JAN2020:1500/02JAN2020:1600/Ex15 OUT/" fid = Open(dss_file) ds0 = fid.read_grid(pathname) if not getattr(ds0,'raster',None) is None: ds0.raster.plot(mask_zeros = True, title = 'Original Spatial Grid') bbox = BoundingBox(-50000,6*10**5,50000,7*10**5) ds1 = ds0.raster.mask(bbox,crop = False) ds1.raster.plot(mask_zeros = True, title = 'Clipped Spatial Grid') ds2 = ds1.raster.mask(bbox,crop = True) ds2.raster.plot(mask_zeros = True, title = 'Cropped Spatial Grid') fid.put_grid(pathname_out,ds2)
''' HecTime and writing Irregular time-series safely ''' import sys from pydsstools.heclib.dss.HecDss import Open from pydsstools.core import TimeSeriesContainer from pydsstools.heclib.utils import HecTime, DssLastError dss_file = "example.dss" fid = Open(dss_file) def decor(func): def f(*arg, **kwargs): try: result = func(*arg, **kwargs) except: print(sys.exc_info()[1]) print(func.__name__ + ' !!!!!Failed!!!!!\n') else: print(func.__name__ + ' *****Passed******\n') return result return f @decor def test1(): # Pass but dss "warning" level error fid.read_ts('/a/b/c//e/f/')
''' Setting Dss Messaging/Logging level ''' import logging import sys from pydsstools.heclib.dss.HecDss import Open dss_file = "example.dss" pathname = "/REGULAR/TIMESERIES/FLOW//1HOUR/Ex1/" with Open(dss_file) as fid: logging.info('DSS Logging is DEFAULT') ts = fid.read_ts(pathname) with Open(dss_file, logging_level='Debug') as fid: logging.info('DSS Logging is DEBUG') ts = fid.read_ts(pathname) with Open(dss_file, logging_level='Diagnostic') as fid: logging.info('DSS Logging is DEBUG') ts = fid.read_ts(pathname)
def open_dss(self, dss_file): if not self.dss_fid is None: self.dss_fid.close() self.dss_fid = Open(dss_file) self._load_pathnames()
class DSSVue(QMainWindow): def __init__(self): super().__init__() uic.loadUi(os.path.join(os.path.dirname(__file__), 'ui', 'main.ui'), self) self.setWindowTitle('dssvue') self.add_gui_elements() self.init_toc() self.set_variables() self.add_actions() def add_gui_elements(self): pass def set_variables(self): self.dss_fid = None def add_actions(self): self.reset_row_action = self.createAction('Reset Rows', self.reset_rows, None, '', '', None) self.excel_action = self.createAction('Open in Excel', self.show_excel, None, 'excel.ico', '', None) self.table_action = self.createAction('Show table', self.show_table, None, 'table.ico', '', None) self.plot_action = self.createAction('Show Plot', self.show_plot, None, 'plot.ico', '', None) self.toolbar = self.addToolBar('Ribbon') self.toolbar.addAction(self.reset_row_action) self.toolbar.addAction(self.excel_action) self.toolbar.addAction(self.table_action) self.toolbar.addAction(self.plot_action) def createAction(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=None, signal="triggered"): action = QAction(text, self) if icon is not None: pass action.setIcon(QIcon(QtGui.QPixmap(os.path.join(ui_dir, icon)))) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: getattr(action, signal).connect(slot) if checkable is not None: action.setCheckable(True) return action def show_excel(self): try: file_prefix = os.path.join(tempfile.gettempdir(), 'dssvue') file_suffix = str(uuid1()) + '.xlsx' filename = file_prefix + file_suffix df = self.toc_tv.model().getData() writer = pd.ExcelWriter(filename, engine='xlsxwriter') df.to_excel(writer, sheet_name='Sheet1') writer.save() open_file(filename) except: logging.warn('Error opening data in excel.') def selected(self): index = self.toc_tv.currentIndex() pathname = self.toc_tv.model().get_pathname(index) typ = self.toc_tv.model().get_type(index) print(pathname) print(typ) return typ, pathname def show_table(self): typ, pathname = self.selected() if typ == 'TS': ts = self.dss_fid.read_ts(pathname) if not ts.empty: times = np.array(ts.pytimes) times = times[~ts.nodata] times = times.tolist() values = ts.values[~ts.nodata].tolist() data = [xy for xy in zip(times, values)] data = np.array(data, dtype=[('Date', object), ('Value', float)]) win = QtGui.QMainWindow(self) win.setWindowTitle('Time Series') t = pg.TableWidget() win.setCentralWidget(t) t.setData(data) win.resize(500, 500) win.show() elif typ == 'PD': df = self.dss_fid.read_pd(pathname) idx = df.index.tolist() df.insert(0, df.index.name, idx) df.index = range(1, df.shape[0] + 1) win = PairedDataWindow(df, parent=self) win.show() elif typ == 'GRID': pass else: pass def show_plot(self): typ, pathname = self.selected() if typ == 'TS': ts = self.dss_fid.read_ts(pathname) if not ts.empty: times = np.array(ts.pytimes) times = times[~ts.nodata].tolist() values = ts.values[~ts.nodata].tolist() win = MplPlotWindow(times, values, pathname, self) win.show() elif typ == 'PD': pass elif typ == 'GRID': dataset = self.dss_fid.read_grid(pathname) data = dataset.read(masked=True) pg.image(data, title=pathname) else: pass def reset_rows(self): self.toc_tv.model().reset_rows() def init_toc(self): df = create_toc_data(None) self.toc_tv = CustomTableView() self.toc_tv.setSortingEnabled(True) model = PathDataModel(df) self.toc_tv.setModel(model) self.setCentralWidget(self.toc_tv) def open_dss(self, dss_file): if not self.dss_fid is None: self.dss_fid.close() self.dss_fid = Open(dss_file) self._load_pathnames() def _load_pathnames(self, condense=True): if isinstance(self.dss_fid, Open): path_list = [] path_dict = self.dss_fid.getPathnameDict() print(path_dict) for typ, paths in path_dict.items(): for path in paths: pathobj = DssPathName(path) parts = [typ] + pathobj.getParts() path_list.append(parts) df = create_toc_data(path_list) if condense: df = self._condense_pathnames(df) self.toc_tv.model().set_data(df) def _condense_pathnames(self, df): ts_df = df[df.Type == 'TS'] ts_parts = [] for _, ts in ts_df.groupby(by=["A", "B", "C", "E", "F"]): d_parts = [datetime.strptime(x, '%d%b%Y') for x in ts.D.tolist()] _min = min(d_parts).strftime('%d%b%Y') _max = max(d_parts).strftime('%d%b%Y') d_part = '%s - %s' % (_min, _max) parts = [ 'TS', ts.A.iloc[0], ts.B.iloc[0], ts.C.iloc[0], d_part, ts.E.iloc[0], ts.F.iloc[0] ] ts_parts.append(parts) ts_df = create_toc_data(ts_parts) rest_df = df[df.Type != 'TS'] df = pd.concat([ts_df, rest_df], ignore_index=True) return df