def prepare_discharge_file(self): for species in self.fish_applied.keys(): for ls in self.fish_applied[species]: fsn = str(species).lower()[0:2] + str(ls)[0:2] # copy spreadsheet with discharge dependencies (if not yet existent) spreadsheet_handle = cMT.MakeFlowTable(self.condition, "sharc", unit=self.unit) self.discharge_xlsx.append(spreadsheet_handle.make_aquatic_condition_xlsx(fsn)) # get discharge statistics and write them to workbook xlsx_flow_dur = config.dir2flows + self.condition + "\\flow_duration_" + str(fsn) + ".xlsx" Q_stats = cFl.FlowAssessment() Q_stats.get_flow_duration_data_from_xlsx(xlsx_flow_dur) exceedances = [] error_msg = [] for qq in spreadsheet_handle.discharges: try: exceedances.append(Q_stats.interpolate_flow_exceedance(float(qq))) # in percent except: error_msg.append("Invalid exceedances found for Q = " + str(qq) + ".") exceedances.append(0.0) spreadsheet_handle.write_data_column("E", 4, exceedances) spreadsheet_handle.save_close_wb() spreadsheet_handle.copy_wb(spreadsheet_handle.wb_out_name, spreadsheet_handle.wb_out_name.split(".xlsx")[0] + "_cov.xlsx") if error_msg.__len__() > 0: error_msg.insert(0, "VERIFY THAT RiverArchitect/00_Flows/{0}/flow_duration_{1}.xlsx exists\n".format(self.condition, str(fsn))) showinfo("ERRORS FOUND", "\n".join(error_msg))
def create_discharge_table(self): try: flow_table = cMT.MakeFlowTable( os.path.basename(self.dir2condition), "q_return") return flow_table.make_condition_xlsx() except: self.error = True
def select_condition(self): try: self.condition = self.combo_c.get() input_dir = config.dir2conditions + str(self.condition) if os.path.exists(input_dir): self.b_s_condition.config(fg="forest green", text="Selected: " + self.condition) # update flow reduction comboboxes mkt = cMkT.MakeFlowTable(self.condition, "", unit=self.unit) discharges = sorted(mkt.discharges) discharges = ["%i %s" % (q, self.q_unit) for q in discharges] self.c_q_high['state'] = 'readonly' self.c_q_high['values'] = discharges self.c_q_high.set('') self.c_q_low['state'] = 'readonly' self.c_q_low['values'] = discharges self.c_q_low.set('') if self.fish_applied != {}: self.b_apply_flow_red["state"] = "normal" return "" else: self.b_s_condition.config(fg="red", text="ERROR") self.errors = True self.verified = False return "Invalid file structure (non-existent directory /01_Conditions/CONDITION/)." except: self.errors = True self.verified = False return "Invalid entry for \'Condition\'."
def get_flow_model_data(self, condition): # condition = STR of CONDITION self.logger.info(" * retrieving 2D-modeled flows (%s) ..." % str(condition)) try: flow_data = cMT.MakeFlowTable(condition, "*") self.flows_2d = flow_data.discharges self.logger.info(" * OK") except: self.logger.info("Internal Error: Cannot access condition folder.")
def get_hydraulic_rasters(self): self.logger.info("Retrieving hydraulic rasters...") try: mkt = cMkT.MakeFlowTable(self.condition, "", unit=self.units) self.discharges = sorted(mkt.discharges) # use subset of flows for analysis if q_low and q_high are provided if (self.q_low is not None) and (self.q_high is not None): self.discharges = [ q for q in self.discharges if self.q_low <= q <= self.q_high ] self.Q_h_dict = { Q: self.dir2condition + mkt.dict_Q_h_ras[Q] for Q in self.discharges } self.Q_u_dict = { Q: self.dir2condition + mkt.dict_Q_u_ras[Q] for Q in self.discharges } try: self.Q_va_dict = { Q: self.dir2condition + mkt.dict_Q_va_ras[Q] for Q in self.discharges } except: proceed = askyesno( 'Cannot retrieve velocity angle data', 'Missing velocity angle data. Proceed without velocity barrier considerations?' ) self.analyze_v = not proceed if proceed: self.logger.info( "WARNING: Proceeding without velocity barrier considerations." ) else: return self.logger.info("OK") except: self.logger.info("ERROR: Could not retrieve hydraulic rasters.")
def make_disc_freq(self, condition): """ Computes frequency that each model discharge is exceeded then dropped within season """ self.logger.info("Computing disconnection frequencies...") disc_freqs = {} flow_data = cMT.MakeFlowTable(condition, "*") flows_2d = flow_data.discharges for fish in self.fish_seasons.keys(): disc_freqs[fish] = [] season_flows = self.season_flow_lists[fish] for q in flows_2d: count = 0 for season in season_flows: for q1, q2 in list(zip(season, season[1:])): if q2 <= q < q1: count += 1 disc_freqs[fish].append([q, count / len(season_flows)]) # output to xlsx self.logger.info("Writing disconnection frequencies to workbook...") self.write_disc_freq2xlsx(condition, disc_freqs)
def calculate_sha(self, sha_threshold, fish, apply_weighing=False): # SHArea_threshold = FLOAT -- value between 0.0 and 1.0 # fish = DICT -- fish.keys()==species_names; fish.values()==lifestages arcpy.CheckOutExtension('Spatial') arcpy.env.overwriteOutput = True arcpy.env.workspace = self.path_csi arcpy.env.extent = "MAXOF" self.logger.info(" >> Retrieving CHSI rasters ...") csi_list = arcpy.ListRasters() cc = 0 # appended in cache to avoid overwriting problems xlsx_result_list = [] for species in fish.keys(): for ls in fish[species]: self.logger.info(" -- Usable Area for " + str(species).upper() + " - " + str(ls).upper()) fish_shortname = str(species).lower()[0:2] + str(ls).lower()[0:2] if self.cover_applies: xsn = self.condition + "_sharea_" + fish_shortname + "_cov.xlsx" else: xsn = self.condition + "_sharea_" + fish_shortname + ".xlsx" xlsx_name = os.path.dirname(os.path.abspath(__file__)) + "\\SHArea\\" + xsn xlsx = cMkT.MakeFlowTable(self.condition, "sharc") xlsx.open_wb(xlsx_name, 0) Q = xlsx.read_float_column_short("B", 4) self.logger.info(" >> Reducing CHSI rasters to SHArea threshold (" + str(sha_threshold) + ") ...") for csi in csi_list: self.logger.info(" -- CHSI raster: " + str(csi)) if fish_shortname in str(csi): ras_csi = arcpy.Raster(self.path_csi + str(csi)) else: continue dsc = arcpy.Describe(ras_csi) coord_sys = dsc.SpatialReference rel_ras = Con(Float(ras_csi) > float(sha_threshold), Float(ras_csi)) self.logger.info(" * saving SHArea-CHSI raster: " + self.path_sha_ras + str(csi)) try: rel_ras.save(self.path_sha_ras + str(csi)) except: self.logger.info("ERROR: Could not save SHArea-CHSI raster.") ras4shp = Con(~IsNull(rel_ras), 1) self.logger.info(" * converting SHArea-CHSI raster to shapefile ...") try: shp_name = self.cache + str(cc) + "sharea.shp" arcpy.RasterToPolygon_conversion(ras4shp, shp_name, "NO_SIMPLIFY") arcpy.DefineProjection_management(shp_name, coord_sys) except arcpy.ExecuteError: self.logger.info("ExecuteERROR: (arcpy) in RasterToPolygon_conversion.") self.logger.info(arcpy.GetMessages(2)) arcpy.AddError(arcpy.GetMessages(2)) except Exception as e: self.logger.info("ExceptionERROR: (arcpy) in RasterToPolygon_conversion.") self.logger.info(e.args[0]) arcpy.AddError(e.args[0]) except: self.logger.info("ERROR: Shapefile conversion failed.") self.logger.info(" * calculating area ...") area = 0.0 try: arcpy.AddField_management(shp_name, "F_AREA", "FLOAT", 9) arcpy.CalculateGeometryAttributes_management(shp_name, geometry_property=[["F_AREA", "AREA"]], area_unit=self.area_unit) self.logger.info(" ... summing up area ...") if apply_weighing: mean_csi = float(arcpy.GetRasterProperties_management(rel_ras, property_type="MEAN")[0]) self.logger.info(" * weighing area with cHSI = %s ..." % str(mean_csi)) else: mean_csi = 1.0 with arcpy.da.UpdateCursor(shp_name, "F_AREA") as cursor: for row in cursor: try: area += float(row[0]) * mean_csi except: self.logger.info(" WARNING: Bad value (" + str(row) + ")") except arcpy.ExecuteError: self.logger.info("ExecuteERROR: (arcpy) in CalculateGeometryAttributes_management.") self.logger.info(arcpy.GetMessages(2)) arcpy.AddError(arcpy.GetMessages(2)) except Exception as e: self.logger.info("ExceptionERROR: (arcpy) in CalculateGeometryAttributes_management.") self.logger.info(e.args[0]) arcpy.AddError(e.args[0]) except: self.logger.info("ERROR: Area calculation failed.") self.logger.info(" * writing Usable Area to workbook ...") for q in Q.keys(): try: q_str = str(csi).split(fish_shortname)[1].split('.tif')[0] except: q_str = str(csi).split(fish_shortname)[1] if str(int(q)) == q_str: self.logger.info( " Discharge: " + str(q) + self.u_discharge + " and Usable Area: " + str( area) + " square " + self.u_length) row = int("".join([str(s) for s in str(Q[q]) if s.isdigit()])) xlsx.write_data_cell("F", row, (area * self.ft2ac)) cc += 1 try: xlsx.save_close_wb(xlsx_name) xlsx_result_list.append(xlsx_name) except: self.logger.info("ERROR: Failed to save " + str(xlsx_name)) arcpy.CheckInExtension('Spatial') if cc > 0: return xlsx_result_list else: return []