2

I am using ERDAS Imagine 2013 and am trying to create a Python script that takes lists created in another script containing the path to each scene's raw data folder and base image ID's (for example LC80230282013183LGN00). I then use a for-loop to go to each data folder, selecting the layers to stack to produce my stacked images. Also, I'm working with ETM+ and OLI data so I have a conditional statement to determine which bands to stack (1-7 for OLI and 1-5 & 7 for ETM+). What I have so far will stack the layers for the first data folder in the list, but then I get an error when it attempts to stack layers in the next folder in the list. I'm fairly inexperienced with Python and coding in general and I feel like either my logic is wrong or I'm just way off with my code.

What I have:

from imagine import modeler

from getpath import imageName, imagePath, outputPathStack

m = modeler.Model()

numImages = len(imageName)

for i in range (0,numImages):

    #check for landsat 8 image
    if "LC8" in str(imageName[i]):
        ri1 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B1.tif")
        ri2 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B2.tif")
        ri3 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B3.tif")
        ri4 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B4.tif")
        ri5 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B5.tif")
        ri6 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B6.tif")
        ri7 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B7.tif")

        StackBands = m.StackLayers(ri1, ri2, ri3, ri4, ri5, ri6, ri7)
        ro = m.RasterOutput(StackBands, outputPathStack + \
                            imageName[i] + "_stack.img")
    #check for landsat 7 image   
    elif "LE7" in str(imageName[i]):
        ri1 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B1.tif")
        ri2 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B2.tif")
        ri3 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B3.tif")
        ri4 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B4.tif")
        ri5 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B5.tif")
        ri6 = m.RasterInput(imagePath[i] + "/" + imageName[i] + "_B7.tif")

        StackBands = m.StackLayers(ri1, ri2, ri3, ri4, ri5, ri6)
        ro = m.RasterOutput(StackBands, outputPathStack + \
                            imageName[i] + "_stack.img")
    m.Execute()

The error I get is:

File "C:\Workspace\stacklayers.py", line 35, in m.Execute() RuntimeError: erdas::sblib::Operator::SetErrorMessage failed Spatial Model failed in Raster Output 2. Error: erdas::sbsupportlib::LayerOperator::OnExecute failed erdas::sbsupportlib::LayerOpOnExecute::WriteBlock failed Bad response from exchange.

  • Could you print outputPathStack + imageName[i] + "_stack.img" to check what the output path looks like? – Aaron May 23 '14 at 18:44
  • 1
    I added this print line to the very end of both conditional statements, the first image path was displayed, then after the layers were done stacking, the next two image paths were displayed immediately (I have 3 image folders in a 'test' folder I'm using) but no more stacked images were created. The path looks like: C:/Workspace/Testing/Stacks/LC80230282013183LGN00_stack.img – Dan May 23 '14 at 18:56
  • Could it be placement of m.Execute()? When inside the for-loop I get the first stacked image but then the model exits without stacking anymore. When placed outside the for-loop, I get the same, and I can see the second stacked image being created in the folder, but after a few seconds the file disappears and I get the above mentioned error message. I also tried using ro as a list with the .append operator and got the same results. – Dan May 27 '14 at 13:37
  • For anyone that's interested, I was able to get this to work by instead using a python script to first generate parameter files, and then a batch file as documented at hexagongeospatial.com/Libraries/Support_Documents/…. It isn't what I initially intended to do, but it accomplishes the automation I am looking for in processing 100's of landsat images! I have also been using this method to create parameter files/batch files for some of my .gmd models to automate these as well..it's been quite useful. – Dan May 30 '14 at 13:53
0

As commented by the asker:

For anyone that's interested, I was able to get this to work by instead using a python script to first generate parameter files, and then a batch file as documented at http://www.hexagongeospatial.com/Libraries/Support_Documents/Howtobatchlayerstack.sflb.ashx. It isn't what I initially intended to do, but it accomplishes the automation I am looking for in processing 100's of landsat images! I have also been using this method to create parameter files/batch files for some of my .gmd models to automate these as well..it's been quite useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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