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'

Line 74 has the answer.

I tried various permutations using this tip (of line 74 holding the secret).
First I replaced 64 with something almost idnetical to line 74 to no avail:
"with open(outfilename2,'w') as outfile2:"

I now ended up with a trial that has fewer steps and could be more efficient.... I'm just showing the last portion of the code I pasted at the top of this thread... I changed the bottom part as you can see

#############
#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
#########################
    os.chdir(resampleDIR)
    h=filex.replace( '.', '_' )
    outfilename=h+'.txt'
 
###################################
#WRITE NEW DATA WITH HEADER ON TOP
###################################

    
#try:
    with open(outfilename,'w') as outfile:
        outfile.write(header)
        for line in data4:
            line.replace('[',' ').replace(']', ' ')
            outfile.write(line)

But here is the error (below). Is there a way to convert Numarray lines back to a string for writing and using replace and such? I also tried line.replace('[','') by itself (not doubled up) and got the same problem.

Traceback (most recent call last):
  File "C:/PDSIwork/PYTHON_SCRIPTS/RESAMPLER2b.py", line 74, in <module>
    line.replace('[',' ').replace(']', ' ')
AttributeError: 'NumArray' object has no attribute 'replace'

Okay, it looks like a NumArray has to be converted to a list using the
tolist(), and then to a string using str.

listdata4 = data4.tolist()
    strdata4 = str(listdata4)
    strdata4 = strdata4.replace('[','').replace(']','')

and then

    outfile2=open(outfilename2,'w')
    for element in strdata4:
        outfile2.write(element)

The above code excerpt doe snot have correct indentation on the top line. Apologies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.