Select to view content in your preferred language

Batch process multiple ASCII to Raster files

18433
27
03-05-2012 11:46 AM
JenB
by
Emerging Contributor
I have multiple (~50) ASCII files that I need to convert to raster. I???m very new to Python but have managed to create a simple script to do this for one file (and it worked- so exciting!). How can I modify this to process ALL of the files? They are all located in the same directory. Any advice would be appreciated. Thanks very much!

# Import system modules
import arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Set local variables
InAsciiFile = "C:/Data/data_2001.asc"
OutRaster = "C:/Data/data_2001"
gp.outputCoordinateSystem = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

# Process: ASCIIToRaster_conversion
gp.ASCIIToRaster_conversion(InAsciiFile, OutRaster, "FLOAT")
Tags (2)
0 Kudos
27 Replies
RumanaReaz
Emerging Contributor
To change the output name you could do something like this. Changing the output to only take the first 13 characters of the input file name. If you want it to be more dynamic, splitting on a specific string etc then you could incorporate that also. At that point I would probably create an output raster variable to create the name. 
 gp.ASCIIToRaster_conversion(os.path.join(inDir, InAsciiFile), os.path.join(OutRaster, InAsciiFile.rsplit(".")[0][:13]), "FLOAT")


Thank you very much for reply.
I tried your code, only changed "FLOAT" to "INTEGER", cause I need integer values.
Got following error:

2011_001_glsea.asc 

Traceback (most recent call last): 
File "C:\Users\rumana\Desktop\My EFDC\Data\AVHRR\BatchAscii2Raster2.py", line 18, in <module> 
gp.ASCIIToRaster_conversion(os.path.join(inDir, InAsciiFile), os.path.join(OutRaster, InAsciiFile.rsplit(".")[0][:13]), "INTEGER") 
RuntimeError: NotInitialized 


I never got this error before and I tested your code with an ascii file which has name length less than 13 characters. It worked then.
0 Kudos
MathewCoyle
Honored Contributor
Try reducing the name splitting from 13 to 9 characters. It may be that it is a multi band raster and doesn't support 13 character file names. You can read about the formatting required for Esri Grids here.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009t0000000w000000

I seem to recall some issues when a file name starts with numeric characters, but that may be a different format I am thinking of.
0 Kudos
RumanaReaz
Emerging Contributor
Try reducing the name splitting from 13 to 9 characters. It may be that it is a multi band raster and doesn't support 13 character file names. You can read about the formatting required for Esri Grids here.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009t0000000w000000

I seem to recall some issues when a file name starts with numeric characters, but that may be a different format I am thinking of.


I checked with 9 but getting the same error.
I have attached a sample data file.

Thanks a lot for helping me out, please take your time.
0 Kudos
MathewCoyle
Honored Contributor
I checked with 9 but getting the same error.
I have attached a sample data file.

Thanks a lot for helping me out, please take your time.


Both worked fine for me. What version of ArcGIS are you using?
0 Kudos
RumanaReaz
Emerging Contributor
I am using ArcGIS 9.3 and Python 2.5 does it have a problem with version?
0 Kudos
MathewCoyle
Honored Contributor
There shouldn't be a problem, but I don't have 9.3 to check it on. Check to make sure your output and input directories are proper and exist.
0 Kudos
RumanaReaz
Emerging Contributor
I have found out two things:

Error1: Run Time Error Not Initialized was due to license problem, ArcGIS license server was down, now it is fixed.

Error2: when the ascii file starts with number say 2011_001.asc it doesn't work, whereas it works with glsea1.asc.

2011_001.asc

Traceback (most recent call last):
  File "C:/Users/rumana/Desktop/My EFDC/Data/AVHRR/BatchAscii2Raster3.py", line 18, in <module>
    gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFile), os.path.join(OutRaster,InAsciiFile.rsplit(".")[0][:9]), "INTEGER")
ExecuteError: ERROR 999999: Error executing function.
Failed to rename the specified file
Failed to execute (ASCIIToRaster).


What will be the most effective thing to do in this case?

File naming was done by some third party (from where I downloaded the files) and I have around 200 files which needed to be converted to Raster format.
0 Kudos
RumanaReaz
Emerging Contributor
I have found out two things:

Error: when the ascii file starts with number say 2011_001.asc it doesn't work, whereas it works with glsea1.asc.



I tried renaming the files in the code as below: But also getting error message.
# Import system modules
import arcgisscripting, os

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Set local variables
InAsciiFile = None
inDir = r"C:/Users/rumana/Desktop/My EFDC/Data/AVHRR/SST_Feb11"
OutRaster = "C:/Users/rumana/Desktop/My EFDC/Data/AVHRR/SST_Feb11/RasterUp_Feb11"

#gp.outputCoordinateSystem = r"Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone 17N.prj"

for InAsciiFile in os.listdir(inDir):
    if InAsciiFile.rsplit(".")[-1] == "asc":
        if InAsciiFile.startswith("2011"):
            InAsciiFilename = "SST" + InAsciiFile.replace("_glsea","")
            os.rename(InAsciiFile, InAsciiFilename)

            print InAsciiFilename
        # Process: ASCIIToRaster_conversion
        gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFilename), os.path.join(OutRaster,InAsciiFilename.rsplit(".")[0][:11]), "FLOAT")


Error Message:
Traceback (most recent call last):
  File "C:\Users\rumana\Desktop\My EFDC\Data\AVHRR\BatchAscii2Raster3.py", line 18, in <module>
    os.rename(InAsciiFile, InAsciiFilename)
WindowsError: [Error 2] The system cannot find the file specified


Can you help me out please?

Thanks.
0 Kudos
RaphaelR
Deactivated User
os.rename() needs a full path to the filename, or it won´t work.
Try this:
os.rename(os.path.join(inDir,InAsciiFile), os.path.join(inDir,InAsciiFilename)
0 Kudos
RumanaReaz
Emerging Contributor
os.rename() needs a full path to the filename, or it won´t work.
Try this:
os.rename(os.path.join(inDir,InAsciiFile), os.path.join(inDir,InAsciiFilename)


Thanks a lot for the help. I replaced os.rename with your code.
But I got Token Error: EOF in multi-line statement.

How do I fixed that?

# Import system modules
import arcgisscripting, os

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Set local variables
InAsciiFile = None
inDir = r"C:/Users/rumana/Desktop/My EFDC/Data/AVHRR/SST_Feb11"
OutRaster = "C:/Users/rumana/Desktop/My EFDC/Data/AVHRR/SST_Feb11/RasterUp_Feb11"

#gp.outputCoordinateSystem = r"Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone 17N.prj"

for InAsciiFile in os.listdir(inDir):
    if InAsciiFile.rsplit(".")[-1] == "asc":
        if InAsciiFile.startswith("2011"):
            InAsciiFilename = "SST" + InAsciiFile.replace("_glsea","")
            os.rename(os.path.join(inDir,InAsciiFile), os.path.join(inDir,InAsciiFilename)
            InAsciiFile = InAsciiFilename
            print InAsciiFile
            
        # Process: ASCIIToRaster_conversion
        gp.ASCIIToRaster_conversion(os.path.join(inDir,InAsciiFilename), os.path.join(OutRaster,InAsciiFilename.rsplit(".")[0][:11]), "FLOAT")



0 Kudos