# 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")
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")
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
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.
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).
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.
# 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")
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
os.rename(os.path.join(inDir,InAsciiFile), os.path.join(inDir,InAsciiFilename)
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)
# 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")