[portland] Array writing question from landscape ecologist

Jonathan Karon monk at netjunky.com
Thu Mar 11 21:18:02 CET 2010


Heather,

> I'm not sure how to write NumArray lines ot a .txt file (see the the
> last lines of code).
>
[snip]
>          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'
>

I believe the problem is that you're trying to treat a numarray as if it
were the textual representation that python will print to the console.

If you issued this command:

  print data4

You might see this:
  
  [4, 3, 2, 5]

And you might conclude that if you want your file to contain

  4, 3, 2, 5

Then using string.replace() to remove the [] characters gets you there.  But
it doesn't, because data4 is not a string, and internally it doesn't look
anything like 

  [4, 3, 2, 5]


I believe your numarray class is array-like, so the following should work...


One approach (likely suboptimal but I'm thinking out loud) is to convert the
numeric contents of the numarray to an array of strings using map and str,
and then joining them with a separator, like this:

  data5 = map( str, data4 )
  result = ", ".join( data5 )

if you do this:

  print data5

you get this:

  ['4', '3', '2', '5']

And if you do this:

  print result

you get this:

  4, 3, 2, 5


Hope that helps.


~jonathan

> -----Original Message-----
> From: portland-bounces+monk=netjunky.com at python.org [mailto:portland-
> bounces+monk=netjunky.com at python.org] On Behalf Of
> lintzh at science.oregonstate.edu
> Sent: Thursday, March 11, 2010 8:54 AM
> To: portland at python.org
> Subject: [portland] Array writing question from landscape ecologist
> 
> 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.
> 
> 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



More information about the Portland mailing list