[portland] Array writing question from landscape ecologist

Ryan Arana ryan.arana at gmail.com
Thu Mar 11 19:48:35 CET 2010


I'm not familiar with numarray, but google told me that their homepage says
that you should be using numpy instead (
http://www.stsci.edu/resources/software_hardware/numarray/numarray.html)
including a pdf guide to converting (
http://www.stsci.edu/resources/software_hardware/numarray/numarray2numpy.pdf
).

The docs for numarray are still available though, and it looks like numarray
has a tostring() method, but I'm not sure what you're wanting your output to
look like exactly, but you might want to look at the tolist() method.
There's also a tofile() method, but that writes out the array as binary
data, and it looks like you're wanting strings. Anyway, all of these methods
are documented on this page:
http://stsdas.stsci.edu/numarray/numarray-1.5.html/node39.html

<http://stsdas.stsci.edu/numarray/numarray-1.5.html/node39.html>Hope that
helps, good luck!
Ryan A

On Thu, Mar 11, 2010 at 8:53 AM, <lintzh at science.oregonstate.edu> wrote:

> 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
>
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/portland/attachments/20100311/f3d205a4/attachment.html>


More information about the Portland mailing list