Select to view content in your preferred language

list values in a table or array???

1582
11
11-06-2012 02:29 AM
irinivozinaki
Deactivated User
Hello everyone!

I would like to ask you if there is a way in python to do the following:

I have 20 values and I want to list them in a 5x4 array. Or is it better to list them in a 5x4 table? Which is the difference between the table and array in python???

(Is this helpful?) I need to save this table/array as an ascii file, which will be continuously converted in a raster file!




Thank you very much in advance!!!
Tags (2)
0 Kudos
11 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,
In my considered opinion there is no reason to use either of this to for this purpose. Simple list slicing with defined number of columns will do the trick.

Simple example:

i = range(20)
cols = 4
f = open('D:\\tmp.csv', 'w')
for r in range(len(i)/cols):
    f.write(';'.join([str(v) for v in i[r*cols:(r+1)*cols]]) + '\n')


Regards
Arek
0 Kudos
RaphaelR
Deactivated User
Hi Irini,

for Arrays in python numpy is really handy.
There are also arcpy functions to convert numpy Arrays to rasters and the other way around.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000130000000

for example
import numpy as np

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

a = np.array(a)
a = np.reshape(a, (4,5))

will get you:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]]
0 Kudos
ArkadiuszMatoszka
Frequent Contributor

There are also arcpy functions to convert numpy Arrays to rasters and the other way around.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000130000000


Didn't know about that. Always something new to learn 🙂
Cheers.
A
0 Kudos
zhengniu
Deactivated User
it is depending on how you will use this table or array, what they are used for.
0 Kudos
irinivozinaki
Deactivated User
Thank you all very much!!!

More accurately,

I have created part of a code which

a) takes two specific raster files (of a 5x4 grid) following a path

b) creates two separate arrays of 5x4 dimension
    I have used the following:
    array_d=arcpy.RasterToNumPyArray(raster_d)
    array_v=arcpy.RasterToNumPyArray(raster_v)
    (height_d, width_d)=array_d.shape
    (height_v, width_v)=array_velocity.shape

c) Subsequently, the code reads every cell value of both arrays, obtaining two values h, v

    for row1 in range (0,height_d):

        row2=row1

        for col1 in range (0,width_d):

            col2=col1
      
            h=array_d.item(row1,col1)
            v=array_v.item(row2,col2)

d) according to several scenarios of these values, h and v, the code calculates a parameter, named "D", in every cell. That is, the D parameter depends on the h and v.


I need to save the calculated parameter, "D", for every cell in a new array of the same dimensions (5x4 dimension), in an ascii file. 
This ascii file will be converted subsequently to a raster file (I suppose this is the easy thing)
0 Kudos
RaphaelR
Deactivated User
not sure if i understand correctly but if you´re working on cell by cell basis, wouldn´t this be much easier with just using raster calculator?
anyways, code below creates a new array for the D parameter and writes it to an ascii file (no header, will need one if you want to use AsciiToRaster).
array_d=arcpy.RasterToNumPyArray(raster_d)
array_v=arcpy.RasterToNumPyArray(raster_v)
(height_d, width_d)=array_d.shape
(height_v, width_v)=array_v.shape

# create empty array to store the "D" parameter
newarr = np.zeros((5,4))


for row1 in range (0,height_d):

    row2=row1

    for col1 in range (0,width_d):
    
        col2=col1

        h=array_d.item(row1,col1)
        v=array_v.item(row2,col2)
        
        # calculate the "D" parameter
        expr = h+v
        
        newarr[row1,col1] =  expr

# write the new array to an ascii file
np.savetxt("e:/out.txt", newarr, fmt="%.2f", delimiter= " ")
0 Kudos
irinivozinaki
Deactivated User
@ rafaelr 

Your help was very important! My problem is solved for small grids! Let's try for bigger ones!!!

I am not good on python programming but I am trying...

I would like to ask you for the raster calculator.
As I can see it is an ArcGIS tool, but as long as the calculations among my cells are not the same every time, for all the cells.
For example I am checking whether the cell value of the first array is greater than one and the cell value of the second array is lower than 3 and if this happens the D value is calculated through a specific equation. But there are several scenarios for the read cell values...

So...It won't help me, will it?

Thank you again!!!
0 Kudos
RaphaelR
Deactivated User
you can build elaborate expressions with raster calculator as well:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009z000000z7000000.htm

however, going with python only is sometimes more straight forward (long/complex expressions in raster calculator can become a bit hard to read). i guess it´s a preference thing.
0 Kudos
irinivozinaki
Deactivated User
Yesterday I supposed that converting the ascii to raster file would be the easy thing. May be I was wrong 😞

Something goes wrong with the needed header of the ascii file.

I have created an ascii file (damageFile) which is the same with the one where the calculated values are saved.

I am writing the following...

damageFile=open('C:\\Python26\\ArcGIS10.0\\Python programming\\test2.ascii', "w+")
damageFile.write('NCOLS 5' + "\n")
damageFile.write('NROWS 4' + "\n")
damageFile.write('XLLCORNER 513589.931' + "\n")
damageFile.write('YLLCORNER 3923728.436' + "\n")
damageFile.write('CELLSIZE 10' + "\n")
damageFile.write('NODATA_VALUE -100' + "\n")

then the calculations follow under several scenarios...

and then the statement:

numpy.savetxt("C:\\Python26\\ArcGIS10.0\\Python programming\\test2.ascii", Damage, fmt="%14.11f", delimiter="w+")

and the damageFile is closed...


Can you find any mistake that I can see???

Thank you very much!!!
0 Kudos