def generate_distance_file():
    """Use ArcGIS to create Conefor distance file

    Requires ArcInfo license.

    """
    try:
        #gp.Extent = gp.Describe(cfg.COREFC).Extent
        gp.CellSize = gp.Describe(cfg.RESRAST).MeanCellHeight
        S2COREFC = cfg.COREFC
        if cfg.SIMPLIFY_CORES:
            try:
                gprint('Simplifying polygons for core pair distance calculations')
                COREFC_SIMP = path.join(cfg.SCRATCHDIR, "CoreFC_Simp.shp")
                tolerance = float(gp.CellSize) / 3

                try:
                    import arcpy
                    import arcpy.cartography as CA
                except:
                    arcpy = False
                if arcpy:
                    CA.SimplifyPolygon(cfg.COREFC, COREFC_SIMP, "POINT_REMOVE",
                                        tolerance, "#", "NO_CHECK")

                else:
                    gp.SimplifyPolygon(cfg.COREFC, COREFC_SIMP, "POINT_REMOVE",
                                        tolerance, "#", "NO_CHECK")

                S2COREFC = COREFC_SIMP
            except:
                pass # In case point geometry is entered for core area FC


        gp.workspace = cfg.SCRATCHDIR
        FS2COREFC = "fcores"
        FS2COREFC2 = "fcores2"
        gp.MakeFeatureLayer(S2COREFC, FS2COREFC)
        gp.MakeFeatureLayer(S2COREFC, FS2COREFC2)

        output = []
        csvseparator = "\t"
        

        adjList = get_full_adj_list()
        # sourceCores = npy.unique(adjList[:, 0])

        gprint('\nFinding distances between cores using Generate Near Table.')
#        gp.OutputCoordinateSystem = gp.describe(cfg.COREFC).SpatialReference
        near_tbl = path.join(cfg.SCRATCHDIR, "neartbl.dbf")
        # gprint('old method')
        # start_time = time.clock()
        # gp.generateneartable(S2COREFC, S2COREFC, near_tbl, "#",
                           # "NO_LOCATION", "NO_ANGLE", "ALL", "0")
        # start_time = lu.elapsed_time(start_time)

        gprint('There are ' + str(len(adjList)) + ' adjacent core pairs to '
               'process.')
        pctDone = 0
        start_time = time.clock()
        for x in range(0, len(adjList)):

            pctDone = lu.report_pct_done(x, len(adjList), pctDone)
            sourceCore = adjList[x, 0]
            targetCore = adjList[x, 1]
            expression = cfg.COREFN + " = " + str(sourceCore)
            gp.selectlayerbyattribute(FS2COREFC, "NEW_SELECTION", expression)
            expression = cfg.COREFN + " = " + str(targetCore)
            gp.selectlayerbyattribute(FS2COREFC2, "NEW_SELECTION", expression)

            gp.generateneartable(FS2COREFC, FS2COREFC2, near_tbl, "#",
                               "NO_LOCATION", "NO_ANGLE", "ALL", "0")

            rows = gp.searchcursor(near_tbl)
            row = rows.Next()
            minDist = 1e20
            if row:  # May be running on selected core areas in step 2
                while row:
                    dist = row.getvalue("NEAR_DIST")
                    if dist <= 0:  # In case simplified polygons abut one another
                        dist = float(gp.CellSize)
                    if dist < minDist:
                        minDist = dist
                        outputrow = []
                        outputrow.append(str(sourceCore))
                        outputrow.append(str(targetCore))
                        outputrow.append(str(dist))
                    del row
                    row = rows.Next()              
            del rows
            output.append(csvseparator.join(outputrow))  
              
        start_time = lu.elapsed_time(start_time)

        # In case coreFC is grouped in TOC, get coreFN for non-Arc statement
        group,coreFN = path.split(cfg.COREFC)

        dist_fname = path.join(cfg.PROJECTDIR, (coreFN + "_dists.txt"))
        dist_file = open(dist_fname, 'w')
        dist_file.write('\n'.join(output))
        dist_file.close()
        gprint('Distance file ' + dist_fname + ' generated.\n')

        return dist_fname

    except arcgisscripting.ExecuteError:
        lu.dashline(1)
        gprint('****Failed in step 2. Details follow.****')
        lu.exit_with_geoproc_error(_SCRIPT_NAME)

    # Return any PYTHON or system specific errors
    except:
        lu.dashline(1)
        gprint('****Failed in step 2. Details follow.****')
        lu.exit_with_python_error(_SCRIPT_NAME)
Example #2
0
            def doRadiusLoop():
                linkTable = linkTableTemp.copy()
                startTime = time.clock()
                randomerror()
                linkLoop = 0
                pctDone = 0
                gprint('\nMapping barriers at a radius of ' + str(radius) +
                       ' ' + str(mapUnits))
                if cfg.SUM_BARRIERS:
                    gprint('using SUM method')
                else:
                    gprint('using MAXIMUM method')
                if numCorridorLinks > 1:
                    gprint('0 percent done')
                lastMosaicRaster = None
                lastMosaicRasterPct = None
                for x in range(0, numLinks):
                    pctDone = lu.report_pct_done(linkLoop, numCorridorLinks,
                                                 pctDone)
                    linkId = str(int(linkTable[x, cfg.LTB_LINKID]))
                    if ((linkTable[x, cfg.LTB_LINKTYPE] > 0)
                            and (linkTable[x, cfg.LTB_LINKTYPE] < 1000)):
                        linkLoop = linkLoop + 1
                        # source and target cores
                        corex = int(coreList[x, 0])
                        corey = int(coreList[x, 1])

                        # Get cwd rasters for source and target cores
                        cwdRaster1 = lu.get_cwd_path(corex)
                        cwdRaster2 = lu.get_cwd_path(corey)

                        # Mask out areas above CWD threshold
                        cwdTemp1 = None
                        cwdTemp2 = None
                        if cfg.BARRIER_CWD_THRESH is not None:
                            if x == 1:
                                lu.dashline(1)
                                gprint('  Using CWD threshold of ' +
                                       str(cfg.BARRIER_CWD_THRESH) +
                                       ' map units.')
                            arcpy.env.extent = cfg.RESRAST
                            arcpy.env.cellSize = cfg.RESRAST
                            arcpy.env.snapRaster = cfg.RESRAST
                            cwdTemp1 = path.join(cfg.SCRATCHDIR,
                                                 "tmp" + str(corex))
                            outCon = arcpy.sa.Con(
                                cwdRaster1 < float(cfg.BARRIER_CWD_THRESH),
                                cwdRaster1)
                            outCon.save(cwdTemp1)
                            cwdRaster1 = cwdTemp1
                            cwdTemp2 = path.join(cfg.SCRATCHDIR,
                                                 "tmp" + str(corey))
                            outCon = arcpy.sa.Con(
                                cwdRaster2 < float(cfg.BARRIER_CWD_THRESH),
                                cwdRaster2)
                            outCon.save(cwdTemp2)
                            cwdRaster2 = cwdTemp2

                        focalRaster1 = lu.get_focal_path(corex, radius)
                        focalRaster2 = lu.get_focal_path(corey, radius)

                        link = lu.get_links_from_core_pairs(
                            linkTable, corex, corey)
                        lcDist = float(linkTable[link, cfg.LTB_CWDIST])

                        # Detect barriers at radius using neighborhood stats
                        # Create the Neighborhood Object
                        innerRadius = radius - 1
                        outerRadius = radius

                        dia = 2 * radius
                        InNeighborhood = ("ANNULUS " + str(innerRadius) + " " +
                                          str(outerRadius) + " MAP")

                        @retry(10)
                        def execFocal():
                            randomerror()
                            # Execute FocalStatistics
                            if not path.exists(focalRaster1):
                                arcpy.env.extent = cwdRaster1
                                outFocalStats = arcpy.sa.FocalStatistics(
                                    cwdRaster1, InNeighborhood, "MINIMUM",
                                    "DATA")
                                if setCoresToNull:
                                    outFocalStats2 = arcpy.sa.Con(
                                        outFocalStats > 0, outFocalStats
                                    )  # Set areas overlapping cores to NoData xxx
                                    outFocalStats2.save(focalRaster1)  #xxx
                                else:
                                    outFocalStats.save(focalRaster1)  #xxx
                                arcpy.env.extent = cfg.RESRAST

                            if not path.exists(focalRaster2):
                                arcpy.env.extent = cwdRaster2
                                outFocalStats = arcpy.sa.FocalStatistics(
                                    cwdRaster2, InNeighborhood, "MINIMUM",
                                    "DATA")
                                if setCoresToNull:
                                    outFocalStats2 = arcpy.sa.Con(
                                        outFocalStats > 0, outFocalStats
                                    )  # Set areas overlapping cores to NoData xxx
                                    outFocalStats2.save(focalRaster2)  #xxx
                                else:
                                    outFocalStats.save(focalRaster2)  #xxx

                                arcpy.env.extent = cfg.RESRAST

                        execFocal()

                        lu.delete_data(cwdTemp1)
                        lu.delete_data(cwdTemp2)

                        barrierRaster = path.join(
                            cbarrierdir, "b" + str(radius) + "_" + str(corex) +
                            "_" + str(corey) + '.tif')

                        if cfg.SUM_BARRIERS:  # Need to set nulls to 0, also
                            # create trim rasters as we go

                            outRas = ((lcDist - Raster(focalRaster1) -
                                       Raster(focalRaster2) - dia) / dia)
                            outCon = arcpy.sa.Con(IsNull(outRas), 0, outRas)
                            outCon2 = arcpy.sa.Con(outCon < 0, 0, outCon)
                            outCon2.save(barrierRaster)

                            # Execute FocalStatistics to fill out search radii
                            InNeighborhood = "CIRCLE " + str(
                                outerRadius) + " MAP"
                            fillRaster = path.join(
                                cbarrierdir, "b" + str(radius) + "_" +
                                str(corex) + "_" + str(corey) + "_fill.tif")
                            outFocalStats = arcpy.sa.FocalStatistics(
                                barrierRaster, InNeighborhood, "MAXIMUM",
                                "DATA")
                            outFocalStats.save(fillRaster)

                            if cfg.WRITE_TRIM_RASTERS:
                                trmRaster = path.join(
                                    cbarrierdir,
                                    "b" + str(radius) + "_" + str(corex) +
                                    "_" + str(corey) + "_trim.tif")
                                rasterList = [fillRaster, resistFillRaster]
                                outCellStatistics = arcpy.sa.CellStatistics(
                                    rasterList, "MINIMUM")
                                outCellStatistics.save(trmRaster)

                        else:
                            #Calculate potential benefit per map unit restored
                            @retry(10)
                            def calcBen():
                                randomerror()
                                outRas = ((lcDist - Raster(focalRaster1) -
                                           Raster(focalRaster2) - dia) / dia)
                                outRas.save(barrierRaster)

                            calcBen()

                        if cfg.WRITE_PCT_RASTERS:
                            #Calculate PERCENT potential benefit per unit restored
                            barrierRasterPct = path.join(
                                cbarrierdir, "b" + str(radius) + "_" +
                                str(corex) + "_" + str(corey) + '_pct.tif')

                            @retry(10)
                            def calcBenPct():
                                randomerror()
                                outras = (100 *
                                          (Raster(barrierRaster) / lcDist))
                                outras.save(barrierRasterPct)

                            calcBenPct()

                        # Mosaic barrier results across core area pairs
                        mosaicDir = path.join(
                            cfg.SCRATCHDIR,
                            'mos' + str(radId) + '_' + str(x + 1))
                        lu.create_dir(mosaicDir)

                        mosFN = 'mos_temp'
                        tempMosaicRaster = path.join(mosaicDir, mosFN)
                        tempMosaicRasterTrim = path.join(
                            mosaicDir, 'mos_temp_trm')
                        arcpy.env.workspace = mosaicDir
                        if linkLoop == 1:
                            #If this is the first grid then copy rather than mosaic
                            arcpy.CopyRaster_management(
                                barrierRaster, tempMosaicRaster)
                            if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                                arcpy.CopyRaster_management(
                                    trmRaster, tempMosaicRasterTrim)

                        else:
                            if cfg.SUM_BARRIERS:
                                outCon = arcpy.sa.Con(
                                    Raster(barrierRaster) < 0,
                                    lastMosaicRaster,
                                    Raster(barrierRaster) +
                                    Raster(lastMosaicRaster))
                                outCon.save(tempMosaicRaster)
                                if cfg.WRITE_TRIM_RASTERS:
                                    outCon = arcpy.sa.Con(
                                        Raster(trmRaster) < 0,
                                        lastMosaicRasterTrim,
                                        Raster(trmRaster) +
                                        Raster(lastMosaicRasterTrim))
                                    outCon.save(tempMosaicRasterTrim)

                            else:
                                rasterString = ('"' + barrierRaster + ";" +
                                                lastMosaicRaster + '"')

                                @retry(10)
                                def mosaicToNew():
                                    randomerror()
                                    arcpy.MosaicToNewRaster_management(
                                        rasterString, mosaicDir, mosFN, "",
                                        "32_BIT_FLOAT", arcpy.env.cellSize,
                                        "1", "MAXIMUM", "MATCH")

                                mosaicToNew()
                                # gprint(str(corex)+'0'+str(corey))

                        if linkLoop > 1:  #Clean up from previous loop
                            lu.delete_data(lastMosaicRaster)
                            lastMosaicDir = path.dirname(lastMosaicRaster)
                            lu.clean_out_workspace(lastMosaicDir)
                            lu.delete_dir(lastMosaicDir)

                        lastMosaicRaster = tempMosaicRaster
                        if cfg.WRITE_TRIM_RASTERS:
                            lastMosaicRasterTrim = tempMosaicRasterTrim
                        if cfg.WRITE_PCT_RASTERS:
                            mosPctFN = 'mos_temp_pct'
                            mosaicDirPct = path.join(
                                cfg.SCRATCHDIR,
                                'mosP' + str(radId) + '_' + str(x + 1))
                            lu.create_dir(mosaicDirPct)
                            tempMosaicRasterPct = path.join(
                                mosaicDirPct, mosPctFN)
                            if linkLoop == 1:
                                # If this is the first grid then copy
                                # rather than mosaic
                                if cfg.SUM_BARRIERS:
                                    outCon = arcpy.sa.Con(
                                        Raster(barrierRasterPct) < 0, 0,
                                        arcpy.sa.Con(IsNull(barrierRasterPct),
                                                     0, barrierRasterPct))
                                    outCon.save(tempMosaicRasterPct)
                                else:
                                    arcpy.CopyRaster_management(
                                        barrierRasterPct, tempMosaicRasterPct)

                            else:
                                if cfg.SUM_BARRIERS:

                                    @retry(10)
                                    def sumBarriers():
                                        randomerror()
                                        outCon = arcpy.sa.Con(
                                            Raster(barrierRasterPct) < 0,
                                            lastMosaicRasterPct,
                                            Raster(barrierRasterPct) +
                                            Raster(lastMosaicRasterPct))
                                        outCon.save(tempMosaicRasterPct)

                                    sumBarriers()
                                else:
                                    rasterString = ('"' + barrierRasterPct +
                                                    ";" + lastMosaicRasterPct +
                                                    '"')

                                    @retry(10)
                                    def maxBarriers():
                                        randomerror()
                                        arcpy.MosaicToNewRaster_management(
                                            rasterString, mosaicDirPct,
                                            mosPctFN, "", "32_BIT_FLOAT",
                                            arcpy.env.cellSize, "1", "MAXIMUM",
                                            "MATCH")

                                    maxBarriers()

                            if linkLoop > 1:  #Clean up from previous loop
                                lu.delete_data(lastMosaicRasterPct)
                                lastMosaicDirPct = path.dirname(
                                    lastMosaicRasterPct)
                                lu.clean_out_workspace(lastMosaicDirPct)
                                lu.delete_dir(lastMosaicDirPct)

                            # lu.delete_data(lastMosaicRasterPct)
                            lastMosaicRasterPct = tempMosaicRasterPct

                        if not cfg.SAVEBARRIERRASTERS:
                            lu.delete_data(barrierRaster)
                            if cfg.WRITE_PCT_RASTERS:
                                lu.delete_data(barrierRasterPct)
                            if cfg.WRITE_TRIM_RASTERS:
                                lu.delete_data(trmRaster)

                        # Temporarily disable links in linktable -
                        # don't want to mosaic them twice
                        for y in range(x + 1, numLinks):
                            corex1 = int(coreList[y, 0])
                            corey1 = int(coreList[y, 1])
                            if corex1 == corex and corey1 == corey:
                                linkTable[y, cfg.LTB_LINKTYPE] = (
                                    linkTable[y, cfg.LTB_LINKTYPE] + 1000)
                            elif corex1 == corey and corey1 == corex:
                                linkTable[y, cfg.LTB_LINKTYPE] = (
                                    linkTable[y, cfg.LTB_LINKTYPE] + 1000)

                if numCorridorLinks > 1 and pctDone < 100:
                    gprint('100 percent done')
                gprint('Summarizing barrier data for search radius.')
                #rows that were temporarily disabled
                rows = npy.where(linkTable[:, cfg.LTB_LINKTYPE] > 1000)
                linkTable[rows, cfg.LTB_LINKTYPE] = (
                    linkTable[rows, cfg.LTB_LINKTYPE] - 1000)

                # -----------------------------------------------------------------

                # Set negative values to null or zero and write geodatabase.
                mosaicFN = (PREFIX + "_BarrierCenters" + sumSuffix + "_Rad" +
                            str(radius))
                mosaicRaster = path.join(cfg.BARRIERGDB, mosaicFN)
                arcpy.env.extent = cfg.RESRAST

                # if setCoresToNull:
                # outCon = arcpy.sa.Con(Raster(tempMosaicRaster) < 0, 0,
                # tempMosaicRaster) #xxx
                # outCon.save(mosaicRaster) #xxx
                # else:
                outSetNull = arcpy.sa.SetNull(tempMosaicRaster,
                                              tempMosaicRaster,
                                              "VALUE < 0")  #xxx orig
                outSetNull.save(mosaicRaster)

                lu.delete_data(tempMosaicRaster)

                if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                    mosaicFN = (PREFIX + "_BarrierCircles_RBMin" + sumSuffix +
                                "_Rad" + str(radius))
                    mosaicRasterTrim = path.join(cfg.BARRIERGDB, mosaicFN)
                    arcpy.CopyRaster_management(tempMosaicRasterTrim,
                                                mosaicRasterTrim)
                    lu.delete_data(tempMosaicRaster)

                if cfg.WRITE_PCT_RASTERS:
                    # Do same for percent raster
                    mosaicPctFN = (PREFIX + "_BarrierCenters_Pct" + sumSuffix +
                                   "_Rad" + str(radius))
                    arcpy.env.extent = cfg.RESRAST
                    outSetNull = arcpy.sa.SetNull(tempMosaicRasterPct,
                                                  tempMosaicRasterPct,
                                                  "VALUE < 0")
                    mosaicRasterPct = path.join(cfg.BARRIERGDB, mosaicPctFN)
                    outSetNull.save(mosaicRasterPct)
                    lu.delete_data(tempMosaicRasterPct)

                # 'Grow out' maximum restoration gain to
                # neighborhood size for display
                InNeighborhood = "CIRCLE " + str(outerRadius) + " MAP"
                # Execute FocalStatistics
                fillRasterFN = "barriers_fill" + str(outerRadius) + tif
                fillRaster = path.join(cfg.BARRIERBASEDIR, fillRasterFN)
                outFocalStats = arcpy.sa.FocalStatistics(
                    mosaicRaster, InNeighborhood, "MAXIMUM", "DATA")
                outFocalStats.save(fillRaster)

                if cfg.WRITE_PCT_RASTERS:
                    # Do same for percent raster
                    fillRasterPctFN = "barriers_fill_pct" + str(
                        outerRadius) + tif
                    fillRasterPct = path.join(cfg.BARRIERBASEDIR,
                                              fillRasterPctFN)
                    outFocalStats = arcpy.sa.FocalStatistics(
                        mosaicRasterPct, InNeighborhood, "MAXIMUM", "DATA")
                    outFocalStats.save(fillRasterPct)

                #Place copies of filled rasters in output geodatabase
                arcpy.env.workspace = cfg.BARRIERGDB
                fillRasterFN = (PREFIX + "_BarrrierCircles" + sumSuffix +
                                "_Rad" + str(outerRadius))
                arcpy.CopyRaster_management(fillRaster, fillRasterFN)
                if cfg.WRITE_PCT_RASTERS:
                    fillRasterPctFN = (PREFIX + "_BarrrierCircles_Pct" +
                                       sumSuffix + "_Rad" + str(outerRadius))
                    arcpy.CopyRaster_management(fillRasterPct, fillRasterPctFN)

                if not cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                    # Create pared-down version of filled raster- remove pixels
                    # that don't need restoring by allowing a pixel to only
                    # contribute its resistance value to restoration gain
                    outRasterFN = "barriers_trm" + str(outerRadius) + tif
                    outRaster = path.join(cfg.BARRIERBASEDIR, outRasterFN)
                    rasterList = [fillRaster, resistFillRaster]
                    outCellStatistics = arcpy.sa.CellStatistics(
                        rasterList, "MINIMUM")
                    outCellStatistics.save(outRaster)

                    #SECOND ROUND TO CLIP BY DATA VALUES IN BARRIER RASTER
                    outRaster2FN = ("barriers_trm" + sumSuffix +
                                    str(outerRadius) + "_2" + tif)
                    outRaster2 = path.join(cfg.BARRIERBASEDIR, outRaster2FN)
                    output = arcpy.sa.Con(IsNull(fillRaster), fillRaster,
                                          outRaster)
                    output.save(outRaster2)
                    outRasterFN = (PREFIX + "_BarrierCircles_RBMin" +
                                   sumSuffix + "_Rad" + str(outerRadius))

                    outRasterPath = path.join(cfg.BARRIERGDB, outRasterFN)
                    arcpy.CopyRaster_management(outRaster2, outRasterFN)
                randomerror()
                startTime = lu.elapsed_time(startTime)
def generate_distance_file():
    """Use ArcGIS to create Conefor distance file

    For ArcGIS Desktop users an Advanced license is required.

    """
    try:
        arcpy.env.cellSize = arcpy.Raster(cfg.RESRAST).meanCellHeight
        S2COREFC = cfg.COREFC
        if cfg.SIMPLIFY_CORES:
            try:
                gprint(
                    'Simplifying polygons for core pair distance calculations')
                COREFC_SIMP = path.join(cfg.SCRATCHDIR, "CoreFC_Simp.shp")
                tolerance = float(arcpy.env.cellSize) / 3
                arcpy.cartography.SimplifyPolygon(cfg.COREFC, COREFC_SIMP,
                                                  "POINT_REMOVE", tolerance,
                                                  "#", "NO_CHECK")
                S2COREFC = COREFC_SIMP
            except Exception:
                pass  # In case point geometry is entered for core area FC

        arcpy.env.workspace = cfg.SCRATCHDIR
        FS2COREFC = "fscores"
        FS2COREFC2 = "fscores2"
        arcpy.MakeFeatureLayer_management(S2COREFC, FS2COREFC)
        arcpy.MakeFeatureLayer_management(S2COREFC, FS2COREFC2)

        output = []
        csvseparator = "\t"

        adjList = get_full_adj_list()
        gprint('\nFinding distances between cores using Generate Near Table.')
        near_tbl = path.join(cfg.SCRATCHDIR, "neartbl.dbf")
        gprint('There are ' + str(len(adjList)) + ' adjacent core pairs to '
               'process.')
        pctDone = 0
        start_time = time.clock()
        for x in range(0, len(adjList)):

            pctDone = lu.report_pct_done(x, len(adjList), pctDone)
            sourceCore = adjList[x, 0]
            targetCore = adjList[x, 1]
            expression = cfg.COREFN + " = " + str(sourceCore)
            arcpy.SelectLayerByAttribute_management(FS2COREFC, "NEW_SELECTION",
                                                    expression)
            expression = cfg.COREFN + " = " + str(targetCore)
            arcpy.SelectLayerByAttribute_management(FS2COREFC2,
                                                    "NEW_SELECTION",
                                                    expression)

            arcpy.GenerateNearTable_analysis(FS2COREFC, FS2COREFC2, near_tbl,
                                             "#", "NO_LOCATION", "NO_ANGLE",
                                             "ALL", "0")

            rows = arcpy.SearchCursor(near_tbl)
            row = next(rows)
            minDist = 1e20
            if row:  # May be running on selected core areas in step 2
                while row:
                    dist = row.getValue("NEAR_DIST")
                    if dist <= 0:  # In case simplified polygons abut one another
                        dist = float(arcpy.env.cellSize)
                    if dist < minDist:
                        minDist = dist
                        outputrow = []
                        outputrow.append(str(sourceCore))
                        outputrow.append(str(targetCore))
                        outputrow.append(str(dist))
                    del row
                    row = next(rows)
            del rows
            output.append(csvseparator.join(outputrow))

        start_time = lu.elapsed_time(start_time)

        # In case coreFC is grouped in TOC, get coreFN for non-Arc statement
        group, coreFN = path.split(cfg.COREFC)

        dist_fname = path.join(cfg.PROJECTDIR, (coreFN + "_dists.txt"))
        dist_file = open(dist_fname, 'w')
        dist_file.write('\n'.join(output))
        dist_file.close()
        gprint('Distance file ' + dist_fname + ' generated.\n')

        return dist_fname

    except arcpy.ExecuteError:
        lu.dashline(1)
        gprint('****Failed in step 2. Details follow.****')
        lu.exit_with_geoproc_error(_SCRIPT_NAME)

    # Return any PYTHON or system specific errors
    except Exception:
        lu.dashline(1)
        gprint('****Failed in step 2. Details follow.****')
        lu.exit_with_python_error(_SCRIPT_NAME)
Example #4
0
            def doRadiusLoop():
                linkTable = linkTableTemp.copy()
                startTime = time.clock()
                randomerror()
                linkLoop = 0
                pctDone = 0
                gprint('\nMapping barriers at a radius of ' + str(radius) +
                       ' ' + str(mapUnits))             
                if cfg.SUM_BARRIERS:  
                    gprint('using SUM method')
                else:
                    gprint('using MAXIMUM method')                   
                if numCorridorLinks > 1:
                    gprint('0 percent done')
                lastMosaicRaster = None
                lastMosaicRasterPct = None
                for x in range(0,numLinks):
                    pctDone = lu.report_pct_done(linkLoop, numCorridorLinks,
                                                pctDone)
                    linkId = str(int(linkTable[x,cfg.LTB_LINKID]))
                    if ((linkTable[x,cfg.LTB_LINKTYPE] > 0) and
                       (linkTable[x,cfg.LTB_LINKTYPE] < 1000)):
                        linkLoop = linkLoop + 1
                        # source and target cores
                        corex=int(coreList[x,0])
                        corey=int(coreList[x,1])

                        # Get cwd rasters for source and target cores
                        cwdRaster1 = lu.get_cwd_path(corex)
                        cwdRaster2 = lu.get_cwd_path(corey)
                        
                        # Mask out areas above CWD threshold
                        cwdTemp1 = None
                        cwdTemp2 = None
                        if cfg.BARRIER_CWD_THRESH is not None:
                            if x == 1:
                                lu.dashline(1)
                                gprint('  Using CWD threshold of ' + str(cfg.BARRIER_CWD_THRESH) + ' map units.')
                            arcpy.env.extent = cfg.RESRAST
                            arcpy.env.cellSize = cfg.RESRAST
                            arcpy.env.snapRaster = cfg.RESRAST
                            cwdTemp1 = path.join(cfg.SCRATCHDIR, "tmp"+str(corex))
                            outCon = arcpy.sa.Con(cwdRaster1 < float(cfg.BARRIER_CWD_THRESH),cwdRaster1)
                            outCon.save(cwdTemp1)
                            cwdRaster1 = cwdTemp1
                            cwdTemp2 = path.join(cfg.SCRATCHDIR, "tmp"+str(corey))
                            outCon = arcpy.sa.Con(cwdRaster2 < float(cfg.BARRIER_CWD_THRESH),cwdRaster2)
                            outCon.save(cwdTemp2)
                            cwdRaster2 = cwdTemp2                        
                        
                        focalRaster1 = lu.get_focal_path(corex,radius)
                        focalRaster2 = lu.get_focal_path(corey,radius)
                                                                     
                        link = lu.get_links_from_core_pairs(linkTable,
                                                            corex, corey)
                        lcDist = float(linkTable[link,cfg.LTB_CWDIST])
                        
                        # Detect barriers at radius using neighborhood stats
                        # Create the Neighborhood Object
                        innerRadius = radius - 1
                        outerRadius = radius

                        dia = 2 * radius
                        InNeighborhood = ("ANNULUS " + str(innerRadius) + " " +
                                         str(outerRadius) + " MAP")

                        @retry(10)
                        def execFocal():
                            randomerror()
                            # Execute FocalStatistics
                            if not path.exists(focalRaster1):
                                arcpy.env.extent = cwdRaster1
                                outFocalStats = arcpy.sa.FocalStatistics(cwdRaster1,
                                                    InNeighborhood, "MINIMUM","DATA")
                                if setCoresToNull:                    
                                    outFocalStats2 = arcpy.sa.Con(outFocalStats > 0, outFocalStats) # Set areas overlapping cores to NoData xxx
                                    outFocalStats2.save(focalRaster1) #xxx
                                else:
                                    outFocalStats.save(focalRaster1) #xxx
                                arcpy.env.extent = cfg.RESRAST

                            if not path.exists(focalRaster2):
                                arcpy.env.extent = cwdRaster2
                                outFocalStats = arcpy.sa.FocalStatistics(cwdRaster2,
                                                InNeighborhood, "MINIMUM","DATA")
                                if setCoresToNull:                    
                                    outFocalStats2 = arcpy.sa.Con(outFocalStats > 0, outFocalStats) # Set areas overlapping cores to NoData xxx
                                    outFocalStats2.save(focalRaster2)#xxx
                                else:
                                    outFocalStats.save(focalRaster2) #xxx

                                arcpy.env.extent = cfg.RESRAST
                        execFocal()
                                                
                        lu.delete_data(cwdTemp1)
                        lu.delete_data(cwdTemp2)
                        
                        barrierRaster = path.join(cbarrierdir, "b" + str(radius)
                              + "_" + str(corex) + "_" +
                              str(corey)+'.tif') 
                             
                        if cfg.SUM_BARRIERS: # Need to set nulls to 0, also 
                                             # create trim rasters as we go

                            outRas = ((lcDist - Raster(focalRaster1) - 
                                      Raster(focalRaster2) - dia) / dia)
                            outCon = arcpy.sa.Con(IsNull(outRas),0,outRas)
                            outCon2 = arcpy.sa.Con(outCon<0,0,outCon)                            
                            outCon2.save(barrierRaster)
                            
                            # Execute FocalStatistics to fill out search radii                            
                            InNeighborhood = "CIRCLE " + str(outerRadius) + " MAP"
                            fillRaster = path.join(cbarrierdir, "b" + str(radius)
                            + "_" + str(corex) + "_" + str(corey) +"_fill.tif")
                            outFocalStats = arcpy.sa.FocalStatistics(barrierRaster,
                                                  InNeighborhood, "MAXIMUM","DATA")
                            outFocalStats.save(fillRaster)                            

                            if cfg.WRITE_TRIM_RASTERS:                            
                                trmRaster = path.join(cbarrierdir, "b" + 
                                                      str(radius)
                                + "_" + str(corex) + "_" + str(corey) +"_trim.tif")
                                rasterList = [fillRaster, resistFillRaster]
                                outCellStatistics = arcpy.sa.CellStatistics(
                                                            rasterList, "MINIMUM")
                                outCellStatistics.save(trmRaster)
                               
                        else:
                            #Calculate potential benefit per map unit restored
                            @retry(10)
                            def calcBen():
                                randomerror()
                                outRas = ((lcDist - Raster(focalRaster1)
                                      - Raster(focalRaster2) - dia) / dia)
                                outRas.save(barrierRaster)
                            calcBen()

                        if cfg.WRITE_PCT_RASTERS:
                            #Calculate PERCENT potential benefit per unit restored                        
                            barrierRasterPct = path.join(cbarrierdir, "b" + 
                                                    str(radius)
                                                    + "_" + str(corex) + "_" +
                                                    str(corey)+'_pct.tif') 
                            @retry(10)
                            def calcBenPct():                            
                                randomerror()
                                outras = (100 * (Raster(barrierRaster) / lcDist))
                                outras.save(barrierRasterPct)
                            calcBenPct()
                            
                        # Mosaic barrier results across core area pairs                    
                        mosaicDir = path.join(cfg.SCRATCHDIR,'mos'+str(radId)+'_'+str(x+1)) 
                        lu.create_dir(mosaicDir)
                        
                        mosFN = 'mos_temp'
                        tempMosaicRaster = path.join(mosaicDir,mosFN)
                        tempMosaicRasterTrim = path.join(mosaicDir,'mos_temp_trm')
                        arcpy.env.workspace = mosaicDir            
                        if linkLoop == 1:
                            #If this is the first grid then copy rather than mosaic
                            arcpy.CopyRaster_management(barrierRaster, 
                                                            tempMosaicRaster)
                            if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                                arcpy.CopyRaster_management(trmRaster, 
                                                            tempMosaicRasterTrim)                       
                            
                        else:                    
                            if cfg.SUM_BARRIERS:
                                outCon = arcpy.sa.Con(Raster (barrierRaster) < 0, lastMosaicRaster, 
                                        Raster(barrierRaster) + Raster(lastMosaicRaster))
                                outCon.save(tempMosaicRaster)                      
                                if cfg.WRITE_TRIM_RASTERS:
                                    outCon = arcpy.sa.Con(Raster
                                    (trmRaster) < 0, lastMosaicRasterTrim, 
                                    Raster(trmRaster) + Raster(
                                    lastMosaicRasterTrim))
                                    outCon.save(tempMosaicRasterTrim) 
                                
                            else:
                                rasterString = ('"'+barrierRaster+";" + 
                                                lastMosaicRaster+'"')
                                @retry(10)
                                def mosaicToNew():
                                    randomerror()
                                    arcpy.MosaicToNewRaster_management(
                                        rasterString,mosaicDir,mosFN, "", 
                                        "32_BIT_FLOAT", arcpy.env.cellSize, "1", 
                                        "MAXIMUM", "MATCH")
                                mosaicToNew()
                                # gprint(str(corex)+'0'+str(corey))
                                
                                
                        if linkLoop>1: #Clean up from previous loop
                            lu.delete_data(lastMosaicRaster)
                            lastMosaicDir =path.dirname(lastMosaicRaster) 
                            lu.clean_out_workspace(lastMosaicDir)
                            lu.delete_dir(lastMosaicDir)
                            
                        lastMosaicRaster = tempMosaicRaster
                        if cfg.WRITE_TRIM_RASTERS:
                            lastMosaicRasterTrim = tempMosaicRasterTrim             
                        if cfg.WRITE_PCT_RASTERS:
                            mosPctFN = 'mos_temp_pct'
                            mosaicDirPct = path.join(cfg.SCRATCHDIR,'mosP'+str(radId)+'_'+str(x+1)) 
                            lu.create_dir(mosaicDirPct)                            
                            tempMosaicRasterPct = path.join(mosaicDirPct,mosPctFN)
                            if linkLoop == 1:
                                # If this is the first grid then copy 
                                # rather than mosaic
                                if cfg.SUM_BARRIERS:
                                    outCon = arcpy.sa.Con(Raster(barrierRasterPct) 
                                        < 0, 0, arcpy.sa.Con(IsNull(
                                        barrierRasterPct), 0, barrierRasterPct)) 
                                    outCon.save(tempMosaicRasterPct)
                                else:
                                    arcpy.CopyRaster_management(barrierRasterPct, 
                                                             tempMosaicRasterPct)
                                                
                            else:                
                                if cfg.SUM_BARRIERS:
                                    @retry(10)
                                    def sumBarriers():
                                        randomerror()
                                        outCon = arcpy.sa.Con(Raster(barrierRasterPct) < 0, 
                                            lastMosaicRasterPct, Raster(barrierRasterPct) + Raster(
                                            lastMosaicRasterPct))
                                        outCon.save(tempMosaicRasterPct)
                                    sumBarriers()
                                else:
                                    rasterString = ('"' + barrierRasterPct + ";" + 
                                                    lastMosaicRasterPct + '"')
                                    @retry(10)
                                    def maxBarriers():
                                        randomerror()
                                        arcpy.MosaicToNewRaster_management(
                                            rasterString,mosaicDirPct,mosPctFN, "", 
                                            "32_BIT_FLOAT", arcpy.env.cellSize, "1", 
                                            "MAXIMUM", "MATCH")
                                    maxBarriers()
                                    
                            if linkLoop>1: #Clean up from previous loop
                                lu.delete_data(lastMosaicRasterPct)
                                lastMosaicDirPct =path.dirname(lastMosaicRasterPct) 
                                lu.clean_out_workspace(lastMosaicDirPct)
                                lu.delete_dir(lastMosaicDirPct)
                            
                            # lu.delete_data(lastMosaicRasterPct)
                            lastMosaicRasterPct = tempMosaicRasterPct                    
                        
                        if not cfg.SAVEBARRIERRASTERS:
                            lu.delete_data(barrierRaster)
                            if cfg.WRITE_PCT_RASTERS:
                                lu.delete_data(barrierRasterPct)
                            if cfg.WRITE_TRIM_RASTERS:                                                    
                                lu.delete_data(trmRaster)                            
                            
                            
                        # Temporarily disable links in linktable -
                        # don't want to mosaic them twice
                        for y in range (x+1,numLinks):
                            corex1 = int(coreList[y,0])
                            corey1 = int(coreList[y,1])
                            if corex1 == corex and corey1 == corey:
                                linkTable[y,cfg.LTB_LINKTYPE] = (
                                    linkTable[y,cfg.LTB_LINKTYPE] + 1000)
                            elif corex1==corey and corey1==corex:
                                linkTable[y,cfg.LTB_LINKTYPE] = (
                                    linkTable[y,cfg.LTB_LINKTYPE] + 1000)               

                if numCorridorLinks > 1 and pctDone < 100:
                    gprint('100 percent done')
                gprint('Summarizing barrier data for search radius.')
                #rows that were temporarily disabled
                rows = npy.where(linkTable[:,cfg.LTB_LINKTYPE]>1000)
                linkTable[rows,cfg.LTB_LINKTYPE] = (
                    linkTable[rows,cfg.LTB_LINKTYPE] - 1000)

                # -----------------------------------------------------------------
                
                # Set negative values to null or zero and write geodatabase. 
                mosaicFN = (PREFIX + "_BarrierCenters" + sumSuffix + "_Rad" + 
                           str(radius))
                mosaicRaster = path.join(cfg.BARRIERGDB, mosaicFN) 
                arcpy.env.extent = cfg.RESRAST
                
                # if setCoresToNull:                
                    # outCon = arcpy.sa.Con(Raster(tempMosaicRaster) < 0, 0, 
                                   # tempMosaicRaster) #xxx
                    # outCon.save(mosaicRaster) #xxx                            
                # else:
                outSetNull = arcpy.sa.SetNull(tempMosaicRaster, tempMosaicRaster,
                                              "VALUE < 0") #xxx orig
                outSetNull.save(mosaicRaster)
                
                lu.delete_data(tempMosaicRaster)
                
                if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                    mosaicFN = (PREFIX + "_BarrierCircles_RBMin" + sumSuffix + 
                                "_Rad" + str(radius))
                    mosaicRasterTrim = path.join(cfg.BARRIERGDB, mosaicFN)
                    arcpy.CopyRaster_management(tempMosaicRasterTrim, 
                                                            mosaicRasterTrim)
                    lu.delete_data(tempMosaicRaster)
                            
                if cfg.WRITE_PCT_RASTERS:                        
                    # Do same for percent raster
                    mosaicPctFN = (PREFIX + "_BarrierCenters_Pct" + sumSuffix + 
                                   "_Rad" + str(radius))
                    arcpy.env.extent = cfg.RESRAST
                    outSetNull = arcpy.sa.SetNull(tempMosaicRasterPct, 
                                                  tempMosaicRasterPct, "VALUE < 0")
                    mosaicRasterPct = path.join(cfg.BARRIERGDB, mosaicPctFN)
                    outSetNull.save(mosaicRasterPct)
                    lu.delete_data(tempMosaicRasterPct)
                           
                
                # 'Grow out' maximum restoration gain to
                # neighborhood size for display
                InNeighborhood = "CIRCLE " + str(outerRadius) + " MAP"
                # Execute FocalStatistics
                fillRasterFN = "barriers_fill" + str(outerRadius) + tif
                fillRaster = path.join(cfg.BARRIERBASEDIR, fillRasterFN)
                outFocalStats = arcpy.sa.FocalStatistics(mosaicRaster,
                                                InNeighborhood, "MAXIMUM","DATA")
                outFocalStats.save(fillRaster)

                if cfg.WRITE_PCT_RASTERS:
                    # Do same for percent raster
                    fillRasterPctFN = "barriers_fill_pct" + str(outerRadius) + tif
                    fillRasterPct = path.join(cfg.BARRIERBASEDIR, fillRasterPctFN)
                    outFocalStats = arcpy.sa.FocalStatistics(mosaicRasterPct,
                                                InNeighborhood, "MAXIMUM","DATA")
                    outFocalStats.save(fillRasterPct)
                

                #Place copies of filled rasters in output geodatabase
                arcpy.env.workspace = cfg.BARRIERGDB
                fillRasterFN = (PREFIX + "_BarrrierCircles" + sumSuffix + "_Rad" + 
                                str(outerRadius))
                arcpy.CopyRaster_management(fillRaster, fillRasterFN) 
                if cfg.WRITE_PCT_RASTERS:
                    fillRasterPctFN = (PREFIX + "_BarrrierCircles_Pct" + sumSuffix + 
                                      "_Rad" + str(outerRadius))
                    arcpy.CopyRaster_management(fillRasterPct, fillRasterPctFN) 

                if not cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                    # Create pared-down version of filled raster- remove pixels 
                    # that don't need restoring by allowing a pixel to only 
                    # contribute its resistance value to restoration gain
                    outRasterFN = "barriers_trm" + str(outerRadius) + tif
                    outRaster = path.join(cfg.BARRIERBASEDIR,outRasterFN)
                    rasterList = [fillRaster, resistFillRaster]
                    outCellStatistics = arcpy.sa.CellStatistics(rasterList, 
                                                                "MINIMUM")
                    outCellStatistics.save(outRaster)

                    #SECOND ROUND TO CLIP BY DATA VALUES IN BARRIER RASTER
                    outRaster2FN = ("barriers_trm"  + sumSuffix + str(outerRadius) 
                                   + "_2" + tif)
                    outRaster2 = path.join(cfg.BARRIERBASEDIR,outRaster2FN)
                    output = arcpy.sa.Con(IsNull(fillRaster),fillRaster,outRaster)
                    output.save(outRaster2)
                    outRasterFN = (PREFIX + "_BarrierCircles_RBMin"  + sumSuffix + 
                                  "_Rad" + str(outerRadius))

                    outRasterPath= path.join(cfg.BARRIERGDB, outRasterFN)
                    arcpy.CopyRaster_management(outRaster2, outRasterFN)
                randomerror()
                startTime=lu.elapsed_time(startTime)
Example #5
0
            def do_radius_loop():
                """Do radius loop."""
                link_table = link_table_tmp.copy()
                start_time = time.clock()
                link_loop = 0
                pct_done = 0
                gprint('\nMapping barriers at a radius of ' + str(radius) +
                       ' ' + str(map_units))
                if cfg.SUM_BARRIERS:
                    gprint('using SUM method')
                else:
                    gprint('using MAXIMUM method')
                if num_corridor_links > 1:
                    gprint('0 percent done')
                last_mosaic_ras = None
                last_mosaic_ras_pct = None
                for x in range(0, num_links):
                    pct_done = lu.report_pct_done(
                        link_loop, num_corridor_links, pct_done)
                    if ((link_table[x, cfg.LTB_LINKTYPE] > 0) and
                            (link_table[x, cfg.LTB_LINKTYPE] < 1000)):
                        link_loop = link_loop + 1
                        # source and target cores
                        corex = int(core_list[x, 0])
                        corey = int(core_list[x, 1])

                        # Get cwd rasters for source and target cores
                        cwd_ras1 = lu.get_cwd_path(corex)
                        cwd_ras2 = lu.get_cwd_path(corey)

                        # Mask out areas above CWD threshold
                        cwd_tmp1 = None
                        cwd_tmp2 = None
                        if cfg.BARRIER_CWD_THRESH is not None:
                            if x == 1:
                                lu.dashline(1)
                                gprint('  Using CWD threshold of '
                                       + str(cfg.BARRIER_CWD_THRESH)
                                       + ' map units.')
                            arcpy.env.extent = cfg.RESRAST
                            arcpy.env.cellSize = cfg.RESRAST
                            arcpy.env.snapRaster = cfg.RESRAST
                            cwd_tmp1 = path.join(cfg.SCRATCHDIR,
                                                 "tmp" + str(corex))
                            out_con = arcpy.sa.Con(
                                cwd_ras1 < float(cfg.BARRIER_CWD_THRESH),
                                cwd_ras1)
                            out_con.save(cwd_tmp1)
                            cwd_ras1 = cwd_tmp1
                            cwd_tmp2 = path.join(cfg.SCRATCHDIR,
                                                 "tmp" + str(corey))
                            out_con = arcpy.sa.Con(
                                cwd_ras2 < float(cfg.BARRIER_CWD_THRESH),
                                cwd_ras2)
                            out_con.save(cwd_tmp2)
                            cwd_ras2 = cwd_tmp2

                        focal_ras1 = lu.get_focal_path(corex, radius)
                        focal_ras2 = lu.get_focal_path(corey, radius)

                        link = lu.get_links_from_core_pairs(link_table,
                                                            corex, corey)
                        lc_dist = float(link_table[link, cfg.LTB_CWDIST])

                        # Detect barriers at radius using neighborhood stats
                        # Create the Neighborhood Object
                        inner_radius = radius - 1
                        outer_radius = radius

                        dia = 2 * radius
                        in_neighborhood = ("ANNULUS " + str(inner_radius)
                                           + " " + str(outer_radius) + " MAP")

                        @Retry(10)
                        def exec_focal():
                            """Execute focal statistics."""
                            if not path.exists(focal_ras1):
                                arcpy.env.extent = cwd_ras1
                                out_focal_stats = arcpy.sa.FocalStatistics(
                                    cwd_ras1, in_neighborhood,
                                    "MINIMUM", "DATA")
                                if SET_CORES_TO_NULL:
                                    # Set areas overlapping cores to NoData xxx
                                    out_focal_stats2 = arcpy.sa.Con(
                                        out_focal_stats > 0, out_focal_stats)
                                    out_focal_stats2.save(focal_ras1)
                                else:
                                    out_focal_stats.save(focal_ras1)
                                arcpy.env.extent = cfg.RESRAST

                            if not path.exists(focal_ras2):
                                arcpy.env.extent = cwd_ras2
                                out_focal_stats = arcpy.sa.FocalStatistics(
                                    cwd_ras2, in_neighborhood,
                                    "MINIMUM", "DATA")
                                if SET_CORES_TO_NULL:
                                    # Set areas overlapping cores to NoData xxx
                                    out_focal_stats2 = arcpy.sa.Con(
                                        out_focal_stats > 0, out_focal_stats)
                                    out_focal_stats2.save(focal_ras2)
                                else:
                                    out_focal_stats.save(focal_ras2)
                                arcpy.env.extent = cfg.RESRAST
                        exec_focal()

                        lu.delete_data(cwd_tmp1)
                        lu.delete_data(cwd_tmp2)

                        barrier_ras = path.join(
                            cbarrierdir, "b" + str(radius) + "_" + str(corex)
                            + "_" + str(corey)+'.tif')

                        # Need to set nulls to 0,
                        # also create trim rasters as we go
                        if cfg.SUM_BARRIERS:
                            out_ras = ((lc_dist - arcpy.sa.Raster(focal_ras1) -
                                        arcpy.sa.Raster(focal_ras2) - dia)
                                       / dia)
                            out_con = arcpy.sa.Con(arcpy.sa.IsNull(out_ras),
                                                   0, out_ras)
                            out_con2 = arcpy.sa.Con(out_con < 0, 0, out_con)
                            out_con2.save(barrier_ras)

                            # Execute FocalStatistics to fill out search radii
                            in_neighborhood = ("CIRCLE " + str(outer_radius)
                                               + " MAP")
                            fill_ras = path.join(
                                cbarrierdir, "b" + str(radius) + "_"
                                + str(corex) + "_" + str(corey) + "_fill.tif")
                            out_focal_stats = arcpy.sa.FocalStatistics(
                                barrier_ras, in_neighborhood,
                                "MAXIMUM", "DATA")
                            out_focal_stats.save(fill_ras)

                            if cfg.WRITE_TRIM_RASTERS:
                                trm_ras = path.join(
                                    cbarrierdir, "b" + str(radius) + "_"
                                    + str(corex) + "_" + str(corey)
                                    + "_trim.tif")
                                ras_list = [fill_ras, resist_fill_ras]
                                out_cell_statistics = arcpy.sa.CellStatistics(
                                    ras_list, "MINIMUM")
                                out_cell_statistics.save(trm_ras)

                        else:

                            @Retry(10)
                            def clac_ben():
                                """Calculate potential benefit.

                                Calculate potential benefit per map unit
                                restored.
                                """
                                out_ras = (
                                    (lc_dist - arcpy.sa.Raster(focal_ras1)
                                     - arcpy.sa.Raster(focal_ras2) - dia)
                                    / dia)
                                out_ras.save(barrier_ras)
                            clac_ben()

                        if cfg.WRITE_PCT_RASTERS:
                            # Calculate % potential benefit per unit restored
                            barrier_ras_pct = path.join(
                                cbarrierdir, "b" + str(radius) + "_"
                                + str(corex) + "_" + str(corey)
                                + '_pct.tif')

                            @Retry(10)
                            def calc_ben_pct():
                                """Calc benefit percentage."""
                                outras = (100 * (arcpy.sa.Raster(barrier_ras)
                                                 / lc_dist))
                                outras.save(barrier_ras_pct)
                            calc_ben_pct()

                        # Mosaic barrier results across core area pairs
                        mosaic_dir = path.join(cfg.SCRATCHDIR, 'mos'
                                               + str(rad_id) + '_'
                                               + str(x + 1))
                        lu.create_dir(mosaic_dir)

                        mos_fn = 'mos_temp'
                        tmp_mosaic_ras = path.join(mosaic_dir, mos_fn)
                        tmp_mosaic_ras_trim = path.join(mosaic_dir,
                                                        'mos_temp_trm')
                        arcpy.env.workspace = mosaic_dir
                        if link_loop == 1:
                            last_mosaic_ras_trim = None
                            # For first grid copy rather than mosaic
                            arcpy.CopyRaster_management(barrier_ras,
                                                        tmp_mosaic_ras)
                            if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                                arcpy.CopyRaster_management(
                                    trm_ras, tmp_mosaic_ras_trim)
                        else:
                            if cfg.SUM_BARRIERS:
                                out_con = arcpy.sa.Con(
                                    arcpy.sa.Raster(barrier_ras) < 0,
                                    last_mosaic_ras,
                                    arcpy.sa.Raster(barrier_ras)
                                    + arcpy.sa.Raster(last_mosaic_ras))
                                out_con.save(tmp_mosaic_ras)
                                if cfg.WRITE_TRIM_RASTERS:
                                    out_con = arcpy.sa.Con(
                                        arcpy.sa.Raster(trm_ras) < 0,
                                        last_mosaic_ras_trim,
                                        arcpy.sa.Raster(trm_ras)
                                        + arcpy.sa.Raster(last_mosaic_ras_trim)
                                        )
                                    out_con.save(tmp_mosaic_ras_trim)

                            else:
                                in_rasters = (";".join([barrier_ras,
                                                        last_mosaic_ras]))

                                @Retry(10)
                                def mosaic_to_new():
                                    """Mosaic to new raster."""
                                    arcpy.MosaicToNewRaster_management(
                                        input_rasters=in_rasters,
                                        output_location=mosaic_dir,
                                        raster_dataset_name_with_extension\
                                        =mos_fn,
                                        pixel_type="32_BIT_FLOAT",
                                        cellsize=arcpy.env.cellSize,
                                        number_of_bands="1",
                                        mosaic_method="MAXIMUM")
                                mosaic_to_new()

                        if link_loop > 1:  # Clean up from previous loop
                            lu.delete_data(last_mosaic_ras)
                            last_mosaic_dir = path.dirname(last_mosaic_ras)
                            lu.clean_out_workspace(last_mosaic_dir)
                            lu.delete_dir(last_mosaic_dir)

                        last_mosaic_ras = tmp_mosaic_ras
                        if cfg.WRITE_TRIM_RASTERS:
                            last_mosaic_ras_trim = tmp_mosaic_ras_trim
                        if cfg.WRITE_PCT_RASTERS:
                            mos_pct_fn = 'mos_temp_pct'
                            mosaic_dir_pct = path.join(cfg.SCRATCHDIR, 'mosP'
                                                       + str(rad_id) + '_'
                                                       + str(x+1))
                            lu.create_dir(mosaic_dir_pct)
                            tmp_mosaic_ras_pct = path.join(mosaic_dir_pct,
                                                           mos_pct_fn)
                            if link_loop == 1:
                                # If this is the first grid then copy
                                # rather than mosaic
                                if cfg.SUM_BARRIERS:
                                    out_con = arcpy.sa.Con(
                                        arcpy.sa.Raster(barrier_ras_pct)
                                        < 0, 0,
                                        arcpy.sa.Con(arcpy.sa.IsNull
                                                     (barrier_ras_pct),
                                                     0, barrier_ras_pct))
                                    out_con.save(tmp_mosaic_ras_pct)
                                else:
                                    arcpy.CopyRaster_management(
                                        barrier_ras_pct, tmp_mosaic_ras_pct)

                            else:
                                if cfg.SUM_BARRIERS:

                                    @Retry(10)
                                    def sum_barriers():
                                        """Sum barriers."""
                                        out_con = arcpy.sa.Con(
                                            arcpy.sa.Raster(barrier_ras_pct)
                                            < 0,
                                            last_mosaic_ras_pct,
                                            arcpy.sa.Raster(barrier_ras_pct)
                                            + arcpy.sa.Raster(
                                                last_mosaic_ras_pct))
                                        out_con.save(tmp_mosaic_ras_pct)
                                    sum_barriers()
                                else:
                                    in_rasters = (";".join([barrier_ras_pct,
                                                  last_mosaic_ras_pct]))

                                    @Retry(10)
                                    def max_barriers():
                                        """Get max barriers."""
                                        arcpy.MosaicToNewRaster_management(
                                            input_rasters=in_rasters,
                                            output_location=mosaic_dir_pct,
                                            raster_dataset_name_with_extension
                                            =mos_pct_fn,
                                            pixel_type="32_BIT_FLOAT",
                                            cellsize=arcpy.env.cellSize,
                                            number_of_bands="1",
                                            mosaic_method="MAXIMUM")
                                    max_barriers()

                            if link_loop > 1:  # Clean up from previous loop
                                lu.delete_data(last_mosaic_ras_pct)
                                last_mosaic_dir_pct = path.dirname(
                                    last_mosaic_ras_pct)
                                lu.clean_out_workspace(last_mosaic_dir_pct)
                                lu.delete_dir(last_mosaic_dir_pct)

                            last_mosaic_ras_pct = tmp_mosaic_ras_pct

                        if not cfg.SAVEBARRIERRASTERS:
                            lu.delete_data(barrier_ras)
                            if cfg.WRITE_PCT_RASTERS:
                                lu.delete_data(barrier_ras_pct)
                            if cfg.WRITE_TRIM_RASTERS:
                                lu.delete_data(trm_ras)

                        # Temporarily disable links in linktable -
                        # don't want to mosaic them twice
                        for y in range(x + 1, num_links):
                            corex1 = int(core_list[y, 0])
                            corey1 = int(core_list[y, 1])
                            if corex1 == corex and corey1 == corey:
                                link_table[y, cfg.LTB_LINKTYPE] = (
                                    link_table[y, cfg.LTB_LINKTYPE] + 1000)
                            elif corex1 == corey and corey1 == corex:
                                link_table[y, cfg.LTB_LINKTYPE] = (
                                    link_table[y, cfg.LTB_LINKTYPE] + 1000)

                if num_corridor_links > 1 and pct_done < 100:
                    gprint('100 percent done')
                gprint('Summarizing barrier data for search radius.')
                # Rows that were temporarily disabled
                rows = npy.where(link_table[:, cfg.LTB_LINKTYPE] > 1000)
                link_table[rows, cfg.LTB_LINKTYPE] = (
                    link_table[rows, cfg.LTB_LINKTYPE] - 1000)
                # -----------------------------------------------------------------
                # Set negative values to null or zero and write geodatabase.
                mosaic_fn = (prefix + "_BarrierCenters" + sum_suffix + "_Rad" +
                             str(radius))
                mosaic_ras = path.join(cfg.BARRIERGDB, mosaic_fn)
                arcpy.env.extent = cfg.RESRAST

                out_set_null = arcpy.sa.SetNull(tmp_mosaic_ras,
                                                tmp_mosaic_ras,
                                                "VALUE < 0")  # xxx orig
                out_set_null.save(mosaic_ras)

                lu.delete_data(tmp_mosaic_ras)

                if cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                    mosaic_fn = (prefix + "_BarrierCircles_RBMin" + sum_suffix
                                 + "_Rad" + str(radius))
                    mosaic_ras_trim = path.join(cfg.BARRIERGDB, mosaic_fn)
                    arcpy.CopyRaster_management(tmp_mosaic_ras_trim,
                                                mosaic_ras_trim)
                    lu.delete_data(tmp_mosaic_ras)

                if cfg.WRITE_PCT_RASTERS:
                    # Do same for percent raster
                    mosaic_pct_fn = (prefix + "_BarrierCenters_Pct"
                                     + sum_suffix + "_Rad" + str(radius))
                    arcpy.env.extent = cfg.RESRAST
                    out_set_null = arcpy.sa.SetNull(tmp_mosaic_ras_pct,
                                                    tmp_mosaic_ras_pct,
                                                    "VALUE < 0")
                    mosaic_ras_pct = path.join(cfg.BARRIERGDB, mosaic_pct_fn)
                    out_set_null.save(mosaic_ras_pct)
                    lu.delete_data(tmp_mosaic_ras_pct)

                # 'Grow out' maximum restoration gain to
                # neighborhood size for display
                in_neighborhood = "CIRCLE " + str(outer_radius) + " MAP"
                # Execute FocalStatistics
                fill_ras_fn = "barriers_fill" + str(outer_radius) + TIF
                fill_ras = path.join(cfg.BARRIERBASEDIR, fill_ras_fn)
                out_focal_stats = arcpy.sa.FocalStatistics(
                    mosaic_ras, in_neighborhood, "MAXIMUM", "DATA")
                out_focal_stats.save(fill_ras)

                if cfg.WRITE_PCT_RASTERS:
                    # Do same for percent raster
                    fill_ras_pct_fn = (
                        "barriers_fill_pct" + str(outer_radius) + TIF)
                    fill_ras_pct = path.join(cfg.BARRIERBASEDIR,
                                             fill_ras_pct_fn)
                    out_focal_stats = arcpy.sa.FocalStatistics(
                        mosaic_ras_pct, in_neighborhood, "MAXIMUM", "DATA")
                    out_focal_stats.save(fill_ras_pct)

                # Place copies of filled rasters in output geodatabase
                arcpy.env.workspace = cfg.BARRIERGDB
                fill_ras_fn = (prefix + "_BarrrierCircles" + sum_suffix
                               + "_Rad" + str(outer_radius))
                arcpy.CopyRaster_management(fill_ras, fill_ras_fn)
                if cfg.WRITE_PCT_RASTERS:
                    fill_ras_pct_fn = (prefix + "_BarrrierCircles_Pct"
                                       + sum_suffix + "_Rad"
                                       + str(outer_radius))
                    arcpy.CopyRaster_management(fill_ras_pct,
                                                fill_ras_pct_fn)

                if not cfg.SUM_BARRIERS and cfg.WRITE_TRIM_RASTERS:
                    # Create pared-down version of filled raster- remove pixels
                    # that don't need restoring by allowing a pixel to only
                    # contribute its resistance value to restoration gain
                    out_ras_fn = "barriers_trm" + str(outer_radius) + TIF
                    out_ras = path.join(cfg.BARRIERBASEDIR, out_ras_fn)
                    ras_list = [fill_ras, resist_fill_ras]
                    out_cell_statistics = arcpy.sa.CellStatistics(ras_list,
                                                                  "MINIMUM")
                    out_cell_statistics.save(out_ras)

                    # SECOND ROUND TO CLIP BY DATA VALUES IN BARRIER RASTER
                    out_ras_2fn = ("barriers_trm" + sum_suffix
                                   + str(outer_radius) + "_2" + TIF)
                    out_ras2 = path.join(cfg.BARRIERBASEDIR, out_ras_2fn)
                    output = arcpy.sa.Con(arcpy.sa.IsNull(fill_ras),
                                          fill_ras, out_ras)
                    output.save(out_ras2)
                    out_ras_fn = (prefix + "_BarrierCircles_RBMin"
                                  + sum_suffix + "_Rad"
                                  + str(outer_radius))
                    arcpy.CopyRaster_management(out_ras2, out_ras_fn)
                start_time = lu.elapsed_time(start_time)