Select to view content in your preferred language

Error with focal statistics in a custom script - says kernel file is not provided even though it is

1627
8
07-11-2020 10:19 AM
ClaireAshcraft
Emerging Contributor

I'm trying to code my own script (well, modify an old ArcInfo Workstation program to work in ArcGIS pro) and am running into problems. Part of the script involves doing focal statistics where I calculate the mean based on a basic 3x3 laplacian kernel (a .txt file I provide). However, this part of the script fails to run and I get error 010391 as described below. The issue is that I can't find any explanation for this. I double-checked the path to the kernel file and it's correct. The text file itself is CRLF. I can't find any online documentation on how the kernel should be formatted for ArcGIS, but I have it formatted the standard way. I have some experience with MatLab and C++ but no Python experience, so there might be an obvious answer I'm missing. What am I doing wrong?

Here's the section of code (this is my first post; if there's a code formatting button for GeoNet I can't find it, sorry):

 

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

#bring the parameters into the program
classnumber = arcpy.GetParameter(0) #Used later in the program
demoriginal = arcpy.GetParameter(1) #DEM raster
laplacian = arcpy.GetParameter(2) #Laplacian filter text file

#turn the raster into a GRID format composite geodataset called "dem" so that focal statistics can be done on it
dem = arcpy.CompositeBands_management(demoriginal, "dem")

#making slope image
simg = Slope(dem,"DEGREE")

# making convex area image
img1 = Con(FocalStatistics(dem,NbrWeight(laplacian),"MEAN") > 0, 1, 0)  #This is the line that fails
convex = focalmean (img1,circle,10)
delete(img1)

(and so on)

---

The error:

Traceback (most recent call last):  File "C:\Users\User\Desktop\ArcGIS Files\GeographicClassification3\ProgramFiles\ACT.py", line 35, in <module>    img1 = Con(FocalStatistics(dem,NbrWeight(laplacian),"MEAN") > 0, 1, 0)

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py", line 5486, in FocalStatistics    percentile_value)

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Utils.py", line 53, in swapper    result = wrapper(*args, **kwargs)

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py", line 5479, in Wrapper    percentile_value) 

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>    return lambda *args: val(*gp_fixargs(args, True))arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.

ERROR 010391: Kernel file is not provided or does not exist.Failed to execute (FocalStatistics).

Failed to execute (Classification).

---

I attached the kernel file but this is what it contains:

3 3
0 -1 0
-1 4 -1
0 -1 0

0 Kudos
8 Replies
DanPatterson
MVP Esteemed Contributor

How Focal Statistics works—Help | Documentation 

your kernel format looks correct, however 'laplacian' needs to be defined as the full path to the *.txt kernel file.

r"c:\pathto\kernel.txt"


... sort of retired...
0 Kudos
ClaireAshcraft
Emerging Contributor

Thanks for the response!

laplacian = arcpy.GetParameter(2) does return the full path to the kernel file. However, in double-checking I realized that one of the folders in my path has a space in it. I changed the path to one with no spaces and now get a (different) kernel error, whether I do laplacian = arcpy.GetParameter(2) or laplacian = r"c:\pathto\kernel.txt" (with my own path, obviously):

Traceback (most recent call last):  File "C:\Users\User\Desktop\ArcGIS Files\GeographicClassification3\ProgramFiles\ACT.py", line 35, in <module>    img1=FocalStatistics(dem,NbrWeight(laplacian),"MEAN")

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py", line 5486, in FocalStatistics    percentile_value)

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Utils.py", line 53, in swapper    result = wrapper(*args, **kwargs)

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\sa\Functions.py", line 5479, in Wrapper    percentile_value) 

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>    return lambda *args: val(*gp_fixargs(args, True))arcgisscripting.

ExecuteError: ERROR 010176: Invalid neighborhood mask.Failed to execute (FocalStatistics).

Failed to execute (Classification).

This is the description of that error. It says to "check the documentation and make sure the neighborhood is being correctly defined."

Looking at the documentation for Focal Statistics it says, "The Irregular and Weight Neighborhood types require a Kernel file be specified. Kernel files should have a .txt file extension. See the Irregular and Weight sections of How Focal Statistics works for information on creating and using kernel files."

If you click that second link and scroll down to the documentation for Weight, it describes the kernel as basically being exactly like what I already have. So I still can't figure out why it won't accept my file.

0 Kudos
DanPatterson
MVP Esteemed Contributor

sometimes a blank line at the end of a text file is not good, check for the extra blank line


... sort of retired...
0 Kudos
ClaireAshcraft
Emerging Contributor

Yeah, it's got no extra blank line.

0 Kudos
DanPatterson
MVP Esteemed Contributor
0 Kudos
ClaireAshcraft
Emerging Contributor

Thanks for the links!

First link: I tried both methods (using text representation as well as creating that separate script to write a kernel file, and then calling the kernel script within my original script) but got the same error both times.

Second link: My raster does have a spatial reference, unfortunately.

Third link: When I try this it does the multiplications correctly, indicating that the python in general has no problem reading my kernel file even though Focal Statistics can't.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Nothing I have suggested has been helpful and I am out of ideas


... sort of retired...
0 Kudos
ClaireAshcraft
Emerging Contributor

Sorry!

0 Kudos