def iefExample(): """update some key file paths in an ief file. Updates the .dat file, .tcf file, and results file paths referenced by the ief file and save it under a new ief file name. """ # Load the tuflow model with a tcf file ief_file = r'C:\path\to\an\isis\ieffile.ief' loader = fl.FileLoader() ief = loader.loadFile(ief_file) # Get the referenced fmp .dat and .tcf files dat_path = ief.getValue('Datafile') tcf_path = ief.getValue('2DFile') results_path = ief.getValue('Results') # Update the dat, results and tcf file names root, ext = os.path.splitext(dat_path) new_dat = root + '_Updated' + ext root, ext = os.path.splitext(results_path) new_results = root + '_Updated' + ext root, ext = os.path.splitext(tcf_path) new_tcf = root + '_Updated' + ext ief.setValue('Datafile', new_dat) ief.setValue('Results', new_results) ief.setValue('2DFile', new_tcf) # Update the filename and write contents to disk ief.path_holder.filename += '_Updated' ief_path = ief.path_holder.absolutePath() ief.write(ief_path)
def loadTuflowModel(self): print('Loading tuflow model...') path = "integration_tests/test_data/model1/tuflow/runs/test_run1.tcf" path = os.path.normpath(os.path.join(os.getcwd(), path)) loader = fileloader.FileLoader() self.tuflow = loader.loadFile(path) assert (self.tuflow.missing_model_files == []) print('Tuflow model load complete.')
def tuflowFileExample(): """Find all gis and bc database files referenced by a tuflow model. Uses a .tcf file to load a tuflow model and find all of the gis files and BC Database files referenced by it. Also identifies any additional files referenced by the BC Database files. """ # Load the tuflow model with a tcf file tcf_file = r'C:\path\to\a\tuflow\tcffile.tcf' loader = fl.FileLoader() tuflow_model = loader.loadFile(tcf_file) data_files = [] names = [] paths = [] # Loop through the different control files fetching the GIS and DATA files. for ckey, cfile in tuflow_model.control_files.items(): # Get file names and absolute paths of all gis files referenced by the model gis_files = cfile.files(filepart_type=ft.GIS) for g in gis_files: names.append(g.filenameAndExtension()) paths.append(g.absolutePath()) # Get the data files objs referenced by the model. # These are files that point to additional data (tmf, bcdbase, 1d_xs, etc) data_files = cfile.files(filepart_type=ft.DATA) gis_combined = dict(zip(names, paths)) # Loop through the data_objs and extract the names and file sources for # each of the BC Database type files bc_combined = [] for data in data_files: if data.command.upper() == 'BC DATABASE': bc = dfl.loadDataFile(data) names = bc.dataObjectAsList(bc.keys.NAME) sources = bc.dataObjectAsList(bc.keys.SOURCE) bc_combined.append((data.filenameAndExtension(), dict(zip(names, sources)))) print ('GIS files in model:') for name, path in gis_combined.items(): print (name + ':\n' + path) print ('\nBC Database files in model:') for b in bc_combined: print (b[0]) for x in b[1].values(): print (x)
def tuflowFileExample(): """Find all gis and bc database files referenced by a tuflow model. Uses a .tcf file to load a tuflow model and find all of the gis files and BC Database files referenced by it. Also identifies any additional files referenced by the BC Database files. """ # Load the tuflow model with a tcf file tcf_file = r'C:\path\to\a\tuflow\tcffile.tcf' loader = fl.FileLoader() tuflow_model = loader.loadFile(tcf_file) # Get file names and absolute paths of all gis files referenced by the model # First we get all GIS type files in the TuflowModel and to avoid getting the # same type twice, caused by the same GIS file referenced in two locations # in the model control files, we set no_duplicates=True. gis_files = tuflow_model.getFiles(file_type=ft.GIS, no_duplicates=True) names = [] paths = [] for g in gis_files: names.append(g.getFileNameAndExtension()) paths.append(g.getAbsolutePath()) gis_combined = dict(zip(names, paths)) # Get the data files objs referenced by the model. # These are files that point to additional data (tmf, bcdbase, 1d_xs, etc) bc_combined = [] data_objs = tuflow_model.getFiles(file_type=ft.DATA, no_duplicates=True) # Loop through the data_objs and extract the names and file sources for # each of the BC Database type files for data in data_objs: if data.command.upper() == 'BC DATABASE': bc = dfl.loadDataFile(data) names = bc.getDataEntryAsList(bc.keys.NAME) sources = bc.getDataEntryAsList(bc.keys.SOURCE) bc_combined.append((data.getFileNameAndExtension(), dict(zip(names, sources)))) print 'GIS files in model:' for name, path in gis_combined.items(): print name + ':\n' + path print '\nBC Database files in model:' for b in bc_combined: print b[0] for x in b[1].values(): print x
def trimRiverSections(): """Deactivates all parts of isis/fmp river sections outside bankmarkers. Searches through all of the river sections in an isis/fmp model and sets deactivation markers at the location of all bankmarkers. I.e. where a bankmarker was marked as left it will now have a deactivation marker there as well. Saves the updated file to disk with _Updated appended to the filename. """ # Load the dat file into a new DatCollection object (isis_model) dat_file = r'C:\path\to\an\isis-fmp\datfile.dat' loader = fl.FileLoader() isis_model = loader.loadFile(dat_file) # Get the river sections from the model and loop through them rivers = isis_model.getUnitsByCategory('River') for river in rivers: # Get the bankmarker locations as a list for this river section bvals = river.getRowDataAsList(rdt.BANKMARKER) # Get the DataObject for deactivation because we want to update it deactivation_data = river.getRowDataObject(rdt.DEACTIVATION) # Loop through the bankmarker values and each time we find one that's # set (not False) we set the deactivation value at that index equal to # the LEFT or RIGHT status of the bankmarker for i, b in enumerate(bvals): if b == 'LEFT': deactivation_data.setValue('LEFT', i) elif b == 'RIGHT': deactivation_data.setValue('RIGHT', i) # Update and get the filename of the isis_model fname = isis_model.path_holder.file_name isis_model.path_holder.setFileName(fname + '_Updated') dat_path = isis_model.path_holder.getAbsolutePath() # Get the contents of the updated isis model contents = isis_model.getPrintableContents() # Write the new isis model to file filetools.writeFile(contents, dat_path)
def trimRiverSections(): """Deactivates all parts of isis/fmp river sections outside bankmarkers. Searches through all of the river sections in an isis/fmp model and sets deactivation markers at the location of all bankmarkers. I.e. where a bankmarker was marked as left it will now have a deactivation marker there as well. Saves the updated file to disk with _Updated appended to the filename. """ # Load the dat file into a new DatCollection object (fmp_model) dat_path = r'C:\path\to\an\isis-fmp\datfile.dat' loader = fl.FileLoader() fmp_model = loader.loadFile(dat_path) # Get the river sections from the model and loop through them rivers = fmp_model.unitsByCategory('river') for river in rivers: # Get the bankmarker locations as a list for this river section bankmarkers = river.row_data['main'].dataObjectAsList(rdt.BANKMARKER) # Get the DataObject for deactivation because we want to update it deactivation = river.row_data['main'].dataObject(rdt.DEACTIVATION) # Loop through the bankmarker values and each time we find one that's # set (not False) we set the deactivation value at that index equal to # the LEFT or RIGHT status of the bankmarker for i, b in enumerate(bankmarkers): if b == 'LEFT': deactivation[i] = 'LEFT' elif b == 'RIGHT': deactivation[i] = 'RIGHT' # Update the filename and write contents to disk fmp_model.path_holder.filename = fmp_model.path_holder.filename + '_Updated' out_path = fmp_model.path_holder.absolutePath() fmp_model.write(out_path)
def iefExample(): """update some key file paths in an ief file. Updates the .dat file, .tcf file, and results file paths referenced by the ief file and save it under a new ief file name. """ # Load the tuflow model with a tcf file ief_file = r'C:\path\to\an\isis\ieffile.ief' loader = fl.FileLoader() ief = loader.loadFile(ief_file) # Get the referenced fmp .dat and .tcf files dat_path = ief.getValue('Datafile') tcf_path = ief.getValue('2DFile') results_path = ief.getValue('Results') # Update the dat, results and tcf file names root, ext = os.path.splitext(dat_path) new_dat = root + '_Updated' + ext root, ext = os.path.splitext(results_path) new_results = root + '_Updated' + ext root, ext = os.path.splitext(tcf_path) new_tcf = root + '_Updated' + ext ief.setValue('Datafile', new_dat) ief.setValue('Results', new_results) ief.setValue('2DFile', new_tcf) # Update and get the filename of the ief fname = ief.path_holder.file_name ief.path_holder.setFileName(fname + '_Updated') ief_path = ief.path_holder.getAbsolutePath() # Get the contents of the updated ief contents = ief.getPrintableContents() # Write the new ief to file filetools.writeFile(contents, ief_path)
def loadTuflowModel(self, path): print('Loading tuflow model...') loader = fileloader.FileLoader() self.tuflow = loader.loadFile(path) print('Tuflow model load complete.')
def loadDatModel(self, path): print('Loading FMP .dat model...') loader = fileloader.FileLoader() self.dat = loader.loadFile(path) print('FMP model load complete.')
def crossSectionWidth(): """Calculate river and bridge cross section widths. Populates a dictionary with the river unit name, full cross section width and active cross section width for all river units in a isis/fmp .dat model file. Then gets the width of all the bridge units in the model. Finally prints a summary of the calculations to the console. """ # Load the dat file into a new DatCollection object dat_file = r'C:\path\to\an\isis-fmp\datfile.dat' loader = fl.FileLoader() isis_model = loader.loadFile(dat_file) section_details = [] # Get the river sections from the model and loop through them rivers = isis_model.unitsByCategory('river') for river in rivers: # Get the width and deactivation values form the river section xvals = river.row_data['main'].dataObjectAsList(rdt.CHAINAGE) dvals = river.row_data['main'].dataObjectAsList(rdt.DEACTIVATION) x_start = xvals[0] x_end = xvals[-1] has_deactivation = False # loop through the section width values, check where any deactivation # markers are and set the active width start and end variables accordingly for i, x in enumerate(xvals, 0): if dvals[i] == 'LEFT': x_start = x has_deactivation = True if dvals[i] == 'RIGHT': x_end = x has_deactivation = True full_width = math.fabs(xvals[-1] - xvals[0]) active_width = math.fabs(x_end - x_start) section_details.append({'Name': river.name, 'Unit': river.UNIT_CATEGORY, 'Full Width': full_width, 'Active Width': active_width, 'Has deactivation': has_deactivation} ) # Get the widths of the bridges too bridges = isis_model.unitsByCategory('bridge') for bridge in bridges: xvals = bridge.row_data['main'].dataObjectAsList(rdt.CHAINAGE) full_width = math.fabs(xvals[-1] - xvals[0]) # Note that we set the 'Unit' value with UNIT_TYPE rather than CATEGORY # this time. It means that we will get what type of bridge it is # (Arch/Usbpr) rather than just that it's a bridge. section_details.append({'Name': bridge.name, 'Unit': bridge.UNIT_TYPE, 'Full Width': full_width, 'Active Width': full_width, 'Has deactivation': False} ) for section in section_details: print (section)