Creating a python scrip to batch process .dat files

2062
1
04-09-2013 08:16 PM
KennethDudley
New Contributor II
I have been trying to create a python scrip to batch process .dat files (with .hdr files).

The data is PRISM Temperature, Precip etc... data which are stored as .dat files - for all the type of functions I need, ArcGIS spatial analyst tools can do it if I do them 1-by-1. I have 3,100 files that need processing. This is what I need to happen

Mask the PRISM data to the study area.
Convert PRISM .dat to ASCII


I tried adapting the code I found below

# Extracts the cells of a raster that correspond with the areas
# defined by a mask.
# Author: Michel
# Date: 10 de Janeiro de 2011
# -----------------------------

# Import system modules
import sys, string, os, arcgisscripting

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

try:
    # Check out any necessary licenses
    gp.CheckOutExtension("spatial")

    # Load required toolboxes...
    gp.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

    gp.workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISM_Binary/test"
    out_workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISMPrecip"
    mask = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PolygonStudyArea/polygonstudyarea.shp"

    # Get a list of the rasters in the workspace
    raster = gp.ListRaster()

    # Loop through the list of rasters
    raster.reset()
    raster = raster.next()

    while raster:
        # Set the outputname for each output to be the same as the input
        output = out_workspace + raster

        # Process: Extract by Mask...
        gp.ExtractByMask_sa(raster, mask, output)

        # Loop function...
        raster = raster.next()

except:
    # If an error occurred while running a tool, then print the messages.
    print "Error in script"
    print gp.GetMessages()


But I've read that the tool in python has a known error that won't process .dat
The python code I cobbled clumsily together for mass converting to ASCII had no luck
# Description: Converts a raster datasets to an ASCII file representing 
#    raster data from a foler.  
# Requirements: arcpy
# Author: Kenneth

# Import system modules
import arcpy
from arcpy import env

try:
# Set environment settings
    env.workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISM_Binary/test"
    out_workspace = "C:/Users/ken/Desktop/GIS Model/FinalProjectData/PRISMPrecip"

# Set local variables
    raster = env.ListRaster()

#loop through raster list
    raster.reset()
    raster = raster.next()

    while raster:
        #set output name for each to be same as input
        outASCII = out_workspace + rasterlist
    
        # Execute RasterToASCII
        arcpy.RasterToASCII_conversion(raster, outASCII)

        #loop function
        raster = raster.next()
    
except:
    # If an error occurred while running a tool, then print the messages.
    print "Error in script"


Is there any other way I can mass change these files? I appreciate any help.
Tags (2)
0 Kudos
1 Reply
PhilMorefield
Occasional Contributor III
Have you opened up one of the .dat files in Wordpad? It may already be in ASCII format, in which case you could just change the .dat extension to .asc, insert the header information, and proceed that way.

I've never seen the PRISM data distributed in .dat format. If you attach one of the files, I might be able to give better advice.
0 Kudos