1

I am having issues with continuously changing the values of pixels within a single band of a multiband raster using python 2.7. Essentially, what I am trying to do is continuously change (increment) the pixel values of a single band based on variables drawn from 2 lists that are used in the nested loop. I've included some code to help understand what I'm doing.

import arcpy
import os
from arcpy.sa import *
import math

arcpy.CheckOutExtension('spatial')

ConstantList = [2.1, 1.6]
TimeStep = ["4h", "8h", "12h", "16h", "20h", "24h"]

multiband = "Path:\\to\\my\\multibandraster.tif\\"

arcpy.env.workspace = multiband

bands = [Raster(os.path.join(multiband, b)) for b in arcpy.ListRasters()]
count = 1
for constant in ConstantList:
    for time in TimeStep:
        temperature = Raster("Path:\\to\\temperatureRasters\\" + time + "_1_1971.tif"

        bands[1] = Con(((temperature > 2.5) & (temperature < 35)), bands[1] + some equation, bands[1])

    bands[1].save("Path:\\to\\output" + str(count) + ".tif")
    count = count + 1

arcpy.CheckInExtension('spatial')

Using this code, I get a raster with all pixels = NoData. If I remove the 'time' for loop (for time in TimeStep:....) I can get the correct values, but only for one timestep. Why would including the 'time' for loop give me a raster with NoData, and how can I fix it?

Also, I should mention that when I remove the 'time' for loop, the values of bands[1] during the execution of the second constant in the constant list, are wrongly added to bands[1] values calculated during the execution of the first constant. At the start of the second constant, the values of bands[1] should be reset to the original values of the multiband raster.

1

1 Answer 1

1

I'm not an ArcGIS expert; but I am going to have a guess at what the issue is here as I have a bit of a suspicion on what the cause is.

The output of the raster calculator has been assigned to bands[1]; you haven't actually saved the output; it's just in memory at this stage. The next iteration of the loop, this memory data is now deleted and so each iteration is just working off nothing, so you save this .tif file that contains nodata.

Each iteration in the TimeStep loop, actually save the output raster and set bands[1] equal to that raster dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.