Select to view content in your preferred language

Trying to write data in an ascii file

1666
7
10-26-2012 04:19 AM
irinivozinaki
Deactivated User
Dear all,

I have written some Python code.

In this code I am separately reading the cell values from two grid files (cell by cell) and according to several scenarios (concerning the cell values), a parameter "D" is calculated for every grid cell. I need to save (write) the calculated "D" values in an ascii file, keeping the initial format of the grid files, in order to subsequently create a grid file of the calculated "D" values.

Can you help me???

Thank you in advance!!!

I should mention here that the two grid files have the same dimensions a x a.
Tags (2)
0 Kudos
7 Replies
T__WayneWhitley
Honored Contributor
And the 2 grids are numbered the same way or with the same orientation?...or do you mean to perform some kind of spatial intersection to determine common cell locations?

If you just need to iterate over the 2 grids by an identifier, you could open 2 update cursors (if you also need to write to the attribute tables) and open a txt file for writing.  Fetch the values from the 2 grids as necessary, perform your calculation row by row while writing results (your D values?) to the txt file.  Without more details, it's difficult to elaborate.
0 Kudos
irinivozinaki
Deactivated User
My basic concern is to write in an ascii file the values of several calculations.

For example I have a function D=32*a-b**2.

The values of the parameter "a" are obtained (read) by a grid file (dimension: k x k)
The values of the parameter "b" are obtained (read) by a grid file (dimension: k x k)

So I will have k x k calculations of the D parameter. I want to save this values in an ascii file in order to obtain continuously a grid file of the same dimensions (k x k dimension) with the former two grid files.

How can I save these values in an ascii file???
0 Kudos
T__WayneWhitley
Honored Contributor
If you mean you are working with raster data, there are the 'From Raster' and 'To Raster' toolsets, for starters.
You can convert to and from ascii.  The Spatial Analyst extension offers more capabilities, but I don't see for what you need to do why you can't use the conversion to ascii then search the file via python methods for your target values to perform and write out the results to a new file.
0 Kudos
irinivozinaki
Deactivated User
Thank you very much for your answers!!!

Actually this is what I have already done... but the only part I haven't manage yet is writing out the results to a new file.

How can I do it? Which is the python command that ensures the proper storage (in a k x k grid file) of the target values?


I can create a new ascii file, open it and close it but I haven't found yet the way to read (or print) inside the file the results.


Thank you again!!!
0 Kudos
T__WayneWhitley
Honored Contributor
Not that I have re-written a file of this kind before, but it looks possible with ordinary python read/write txt file commands.  Just from what I've seen on the documentation of these raster to ascii files, as long as you preserve the header formatting, you should be fine.  The other operations involving math you can do with python to, if you need it, with the standard math module.

According to the Raster to ASCII tool doc, the header lines are:
NCOLS
NROWS
XLLCORNER
YLLCORNER
CELLSIZE
NODATA_VALUE

So I'd test line by line for the 1st row after NODATA_VALUE, searching for the data strings you want to fetch, recalculate, and write anew.  Then of course see if valid format converting back the other direction (ASCII to Raster).

http://docs.python.org/library/stdtypes.html#bltin-file-objects
0 Kudos
RaphaelR
Deactivated User
if you have done your calculations with a raster and need to convert this to an ascii grid, arcpy.RasterToASCII_conversion should work.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000005000000
0 Kudos
irinivozinaki
Deactivated User
@rafaelr

Thank you too for your answer!!!

But don't want to convert the raster to an ascii grid.


Actually I have done some calculations with the grid values of two separate raster files and a resulting value has come up for each cell.
For the time being, I need to save the resulting values in an ascii file, which will be converted continuously in a raster file!
0 Kudos