[portland] Array writing question from landscape ecologist

lintzh at science.oregonstate.edu lintzh at science.oregonstate.edu
Thu Mar 11 17:53:49 CET 2010


Hello,
I am new to this group. May I post a code-related question here? If  
so, here it goes.

I am in the process of importing and reading data, reshaping the data  
to be a 2D array, adding a header file on top (of the new 2D array),  
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 receive the  
following error when I attempt to write a data matrix created with  
NumArray. I just reviewed my two sources on Numarray, and I can't  
figure out why I can't write the data. Below is the code and below the  
code is the error message.

I'm not sure how to write NumArray lines ot a .txt file (see the the  
last lines of code).

CODE STARTS HERE:

from __future__ import with_statement

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)

##########################
#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)



---------------------------
Here's the error message:
---------------------------

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'

Thank you,
Heather



More information about the Portland mailing list