Read specific raster file using python

1129
2
01-17-2012 12:26 PM
RameshGautam
New Contributor
I am trying to read a specific raster image file using python, however, getting some difficulties. Can you please help me?

Here is the details about the files:

import arcpy
import sys, os, string
from arcpy.sa import *
from arcpy import env
arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries\NDVI"
LstRst = arcpy.ListRasters("*", "img")
for Rst in LstRst:
    Img1 = string.find(Rst, "_stanislaus.img")
    if Img1 > -1:
        print Rst
     

when I print Rst, it prints all raster images with _stanislaus.img. Now, I need to read an specific image like "aug08_2010_stanislaus.img". Can you please tell me how to do that?

Thanks
Tags (2)
0 Kudos
2 Replies
DarrenWiens2
MVP Honored Contributor
I'm confused. If you know the file name, why not just substitute the file name for "_stanislaus.img"?
import arcpy
 import sys, os, string
 from arcpy.sa import *
 from arcpy import env
 arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries\NDVI"
 LstRst = arcpy.ListRasters("*", "img")
 for Rst in LstRst:
     Img1 = string.find(Rst, "aug08_2010_stanislaus.img")
     if Img1 > -1:
     print Rst

Or, are you looking for how to substitute a variable into the file name?
import arcpy
 import sys, os, string
 from arcpy.sa import *
 from arcpy import env
 arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries\NDVI"
 LstRst = arcpy.ListRasters("*", "img")
 fileprefix = "aug08_2010"
 for Rst in LstRst:
     Img1 = string.find(Rst, fileprefix + "_stanislaus.img")
     if Img1 > -1:
     print Rst
0 Kudos
RameshGautam
New Contributor
Thanks a lot for your prompt help. When I replace "_stanislaus.img" by "aug08_2010_stanislaus.img", it can't print the file, however, as you have mentioned in second option by using fileprefix, we can read this file. Do you know why?

Thanks again for your help. I apprecaite it.








I'm confused. If you know the file name, why not just substitute the file name for "_stanislaus.img"?
import arcpy
 import sys, os, string
 from arcpy.sa import *
 from arcpy import env
 arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries\NDVI"
 LstRst = arcpy.ListRasters("*", "img")
 for Rst in LstRst:
     Img1 = string.find(Rst, "aug08_2010_stanislaus.img")
     if Img1 > -1:
     print Rst

Or, are you looking for how to substitute a variable into the file name?
import arcpy
 import sys, os, string
 from arcpy.sa import *
 from arcpy import env
 arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries\NDVI"
 LstRst = arcpy.ListRasters("*", "img")
 fileprefix = "aug08_2010"
 for Rst in LstRst:
     Img1 = string.find(Rst, fileprefix + "_stanislaus.img")
     if Img1 > -1:
     print Rst
0 Kudos