Raster Calculator Function Does not Takes Predefined Variable

777
4
06-07-2018 10:07 AM
Md_Abdullah_All_Sourav
New Contributor

I have written a python script that finds the mean of all pixel values in a raster. Once I get the mean (suppose 0.3 in this case), I can use raster calculator and assign pixel value of 1 to each pixel higher than the mean and 0 to everything else. The equation is as follows -

Float(Con("Raster1"<0.3, 0, 1))

However, there are 30 raster files, thus, I want to avoid manually using the Raster Calculator. I have added the following codes to the script- 

Raster_mean = got from previous script [0.3 in this case]

arcpy.gp.RasterCalculator_sa('Float(Con("Raster1"<Raster_mean, 0, 1))' ,  "E:/ArcGIS Python/raster10")

I am getting the following errors-

NameError: name 'Raster_mean' is not defined.  Failed to execute (RasterCalculator). 

Seems like it does not take any predefined variable but only raw values. Is there any other option to conduct the task? 

Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

this line

Raster_mean = got from previous script [0.3 in this case]

throw a...

msg = "mean from script {}... type {}".format(Raster_mean, type(Raster_mean))

arcpy.AddMessage(msg)

print(msg) # if using standalone python

What do you get? and if the type isn't a number type (int, float etc) then something is wrong with the passing of the parameter to the script

Md_Abdullah_All_Sourav
New Contributor

I just used Con and it worked as a charm. Here are the codes- 

inRaster = Raster(NDVI_file)
outCon = Con(inRaster < mean, 0,1)

0 Kudos
DanPatterson_Retired
MVP Emeritus

Oh good... so Raster_mean had nothing to do with it

0 Kudos
Md_Abdullah_All_Sourav
New Contributor

Here, I assumed mean = Raster_mean. The code is pretty different (variables), I just tried to simplify that.

0 Kudos