Hello,
I am in the process of importing and reading data, reshaping the data, adding a header file on top (of the new data array or matrix), and writing a new file. I do this in a loop for many files. I'm using python 2.5 since that is the version distributed with ArcGIS, a software that interfaces with python 2.5. However, I recieve the following error when I attempt to write a data matrix created with NumArray. I just reveiwed my two sources on Numarray, and I can't figure out why I can't write the data. I thought pyhton writes in strings?
Below is the code and below the code is the output and the error message.
CODE:
from __future__ import with_statement
#Import Python Standard Library Modules
import arcgisscripting, sys, os, string, copy, glob,numarray
# Create the Geoprocessor Object
gp = arcgisscripting.create()
##############
#DIRECTORIES
##############
workDIR = "C:\\PDSIwork"
resampleDIR = workDIR + "\\resample\\"
ascDIR = workDIR + "\\PDSIdata\\"
##############
#HEADER
##############
header = """\
ncols 1386
nrows 595
xllcorner -124.7292
yllcorner 24.6024
cellsize 0.0417
NODATA_value -9999
"""
#############################
#GET RUNLIST AND IMPORT DATA
##############################
data=[]
os.chdir(ascDIR)
runlist=os.listdir(ascDIR)
#print runlist
for filex in runlist:
x=open(filex,'r')
for i in xrange(824670):
z=x.readline()
z=float(z)
data.append(z)
print filex
#############
#RESHAPE DATA
#############
data2 = numarray.array(data)
data3 = numarray.reshape(data2, 595, 1386)
data4=numarray.transpose(data3)
print len(data4)
#slice1=mmm(1:408,1:267);
##########################
#RENAME DATA FOR WRITING
#########################
h=filex.replace( '.', '_' )
outfilename=h+'.txt'
outfilename2=h+'NA.txt'
open(outfilename2,'w')
outfilename2.write(data4)
inputname=outfilename2
###################################
#WRITE NEW DATA WITH HEADER ON TOP
###################################
os.chdir(resampleDIR)
#try:
with open(outfilename,'w') as outfile:
outfile.write(header)
with open(inputname,'r') as datafile:
for line in datafile:
outfile.write(line)
OUTPUT AND ERROR MESSAGE:
pdsi_0.asc
1386
Traceback (most recent call last):
File "C:\PDSIwork\PYTHON_SCRIPTS\RESAMPLER2.py", line 65, in <module>
outfilename2.write(data4)
AttributeError: 'str' object has no attribute 'write'