示例#1
0
    def checkHeaders(self):
        """Checks to make sure headers (with cell size, number of cols, etc) match for input rasters."""  
        if self.options.data_type == 'network':
            return
        match_files = []
        if self.options.use_polygons == True:
            match_files.append(self.options.polygon_file)
        if 'advanced' not in self.options.scenario.lower():
            filetype = CSIO._guess_file_type(self.options.point_file)
            if filetype == CSIO.FILE_TYPE_AAGRID:
                match_files.append(self.options.point_file)
        else:
            filetype = CSIO._guess_file_type(self.options.ground_file)
            if filetype == CSIO.FILE_TYPE_AAGRID:
                match_files.append(self.options.ground_file)
            filetype = CSIO._guess_file_type(self.options.source_file)
            if filetype == CSIO.FILE_TYPE_AAGRID:
                match_files.append(self.options.source_file)
        if self.options.use_mask == True:
            match_files.append(self.options.mask_file)
        headers_match = CSIO.match_headers(self.options.habitat_file, match_files)

        if headers_match == False:
            result = wx.MessageDialog(None, "Raster map headers do not match.  Circuitscape can try to resample maps to match the resistance map (Beta code, no guarantees). \n\nNote:all maps MUST be in the same projection.  Some focal nodes or short-circuit regions may be lost. \n\nUsing the 'Export to Circuitscape' tool or the Circuitscape for ArcGIS toolbox is a better bet.\n\nContinue?", "Warning", wx.YES_NO).ShowModal()  # @UndefinedVariable
            return (result == wx.ID_NO) # @UndefinedVariable
        return False
示例#2
0
    def on_calcButton_mouseClick(self, event):
        self.components.habitatFile.SetFocus() #Need to force loseFocus on text boxes to make sure they are updated.
        self.components.outFile.SetFocus()
        self.components.calcButton.SetFocus()
        
        out_base, _out_ext = os.path.splitext(self.options.output_file)

        #if (self.options.log_file is None) or (not os.path.exists(self.options.log_file)): 
        self.options.log_file = out_base + '.log' # For now, just write log file to output directory.

        if (self.options.print_rusages and not sys.platform.startswith('win')) or (self.options.print_timings): 
            self.options.profiler_log_file = out_base + '_rusages.log' #For now, always write log file to output directory.
        else:
            self.options.profiler_log_file = None

        #Check to see if all inputs are chosen
        (all_options_entered, message) = self.options.check()
        
        if not all_options_entered:
            dial = wx.MessageDialog(None, message, 'Not all options entered', wx.OK | wx.ICON_EXCLAMATION)  # @UndefinedVariable
            dial.ShowModal()
            self.reset_status_bar()
            return
                                        
        #save selected options in local directory
        configFile = 'circuitscape.ini'
        self.options.write(configFile)
        try:
            self.options.write(self.options.output_file, True)
        except RuntimeError as ex:
            dial = wx.MessageDialog(None, str(ex), 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
            dial.ShowModal()
            self.reset_status_bar()
            return  
                            
        GUI.logger.info('Calling Circuitscape...')
        startTime = time.strftime('%H:%M:%S')
        self.statusBar.SetStatusText('Job started ' + str(startTime), 0)
        try:
            cs = Compute('circuitscape.ini', GUI.log_handler)
        except RuntimeError as error:
            message = str(error)
            dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
            dial.ShowModal()
            self.reset_status_bar()
            return
        except:
            self.unknown_exception()
            self.reset_status_bar()
            return

        try:
            filetype = CSIO._guess_file_type(self.options.habitat_file)
            if self.options.data_type == 'network': 
                if filetype != CSIO.FILE_TYPE_TXTLIST:
                    raise RuntimeError('Error reading network file "' + self.options.habitat_file + '".\nPlease check file format.')
            elif filetype != CSIO.FILE_TYPE_AAGRID and filetype != CSIO.FILE_TYPE_NPY:
                    raise RuntimeError('File "' + self.options.habitat_file + '"\ndoes not appear to be a raster. Please check file format.')
            terminate = self.checkHeaders()
            if terminate == True:
                self.reset_status_bar()
                return
        except RuntimeError as error:
            message = str(error)
            dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
            dial.ShowModal()
            self.reset_status_bar()
            return
        except:
            self.unknown_exception()
            self.reset_status_bar()
            return
        if self.options.data_type == 'network':        
            GUI.logger.debug('Running in Network (Graph) Mode')
                            
        if self.options.scenario == 'pairwise':
            try:
                wx.BeginBusyCursor()  # @UndefinedVariable
                self.statusBar.SetStatusText('',1)
                self.statusBar.SetStatusText('',2)
                resistances, solver_failed = cs.compute()
                if solver_failed == True:
                    msg = '\nPairwise resistances (-1 indicates disconnected focal node pair, -777 indicates failed solve):'
                else:
                    msg = '\nPairwise resistances (-1 indicates disconnected node pair):\nNode1\tNode2\tResistance'
                
                resistances3ColumnsStr = self.formatResistanceOutput(resistances)
                GUI.logger.info(msg + "\n" + resistances3ColumnsStr)
                GUI.logger.info('Done.\n')
                if solver_failed == True:
                    message = 'At least one solve failed.  Failure is coded as -777 in output resistance matrix.'
                    dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_EXCLAMATION)  # @UndefinedVariable
                    dial.ShowModal()
                wx.EndBusyCursor()  # @UndefinedVariable
                self.components.calcButton.SetFocus()
            except MemoryError:
                wx.EndBusyCursor()  # @UndefinedVariable
                self.memory_error_feedback()
                self.reset_status_bar()
                return
            except RuntimeError as error:
                wx.EndBusyCursor()  # @UndefinedVariable
                message = str(error)
                dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
                dial.ShowModal()
                self.reset_status_bar()
                return
            except:
                wx.EndBusyCursor()  # @UndefinedVariable
                self.unknown_exception()
        elif self.options.scenario == 'advanced':
            if self.options.write_cur_maps == False and self.options.write_volt_maps == False:
                message = 'Advanced mode selected but no output maps checked.\nThere is nothing to do.'
                dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
                dial.ShowModal()
                return

            wx.BeginBusyCursor()  # @UndefinedVariable

            try:
                self.statusBar.SetStatusText('',1)
                self.statusBar.SetStatusText('',2) 
                _voltages, solver_failed = cs.compute()
                wx.EndBusyCursor()  # @UndefinedVariable
                
                self.components.calcButton.SetFocus()
                
                if solver_failed == True:
                    message = 'Solver failed!'
                    dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_EXCLAMATION)  # @UndefinedVariable
                    dial.ShowModal()
                
                GUI.logger.info('Done.\n')
            except RuntimeError as error:
                wx.EndBusyCursor()  # @UndefinedVariable
                message = str(error)
                dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
                dial.ShowModal()
                self.reset_status_bar()
                return
            except MemoryError:
                wx.EndBusyCursor()  # @UndefinedVariable
                self.memory_error_feedback()
                self.reset_status_bar()
                return
            except:
                wx.EndBusyCursor()  # @UndefinedVariable
                self.unknown_exception()
                self.reset_status_bar()
                return

        else:
            try:
                wx.BeginBusyCursor()  # @UndefinedVariable
                self.statusBar.SetStatusText('',1)
                self.statusBar.SetStatusText('',2)                                    
                resistances, solver_failed = cs.compute()
                wx.EndBusyCursor()  # @UndefinedVariable
                
                self.components.calcButton.SetFocus()
                
                if self.options.scenario == 'all-to-one':
                    if solver_failed == True:
                        msg = '\nResult for each focal node (0 indicates successful calculation, -1 indicates disconnected node, -777 indicates failed solve):'
                    else:
                        msg = '\nResult for each focal node (0 indicates successful calculation, -1 indicates disconnected node):'
                elif solver_failed == True:
                    msg = '\nResistances to ground(-1 indicates disconnected node, -777 indicates failed solve):\n      Node         Resistance'
                else:
                    msg = '\nResistances to ground (-1 indicates disconnected node):\nNode\tResistance'
                resistanceString = self.formatResistanceOutput(resistances)
                GUI.logger.info(msg + '\n' + resistanceString)
                GUI.logger.info('Done.\n')
                
                if solver_failed == True:
                    message = 'At least one solve failed.  Failure is coded as -777 in output node/resistance list.'
                    dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_EXCLAMATION)  # @UndefinedVariable
                    dial.ShowModal()
            except RuntimeError as error:
                wx.EndBusyCursor()  # @UndefinedVariable
                message = str(error)
                dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
                dial.ShowModal()
                self.reset_status_bar()
            except MemoryError:
                wx.EndBusyCursor()  # @UndefinedVariable
                self.memory_error_feedback()
                self.reset_status_bar()
                return
            except:
                wx.EndBusyCursor()  # @UndefinedVariable
                self.unknown_exception()  
                self.reset_status_bar()
                return