I have a 2D numpy array. I want to make a ASCII file out of it. Here's the code I am using where a3
is the numpy array
f = open('ASCIIout.asc', 'w')
numpy.savetxt(ASCIIout, a3)
f.write("ncols " + str(ncols) + "\n")
f.write("nrows " + str(nrows) + "\n")
f.write("xllcorner " + str(xllcorner) + "\n")
f.write("yllcorner " + str(yllcorner) + "\n")
f.write("cellsize " + str(cellsize) + "\n")
f.write("NODATA_value " + str(noDATA) + "\n")
f.close()
Now I open it with append option and write the 2D array values:
f_handle = open(ASCIIout, 'a')
numpy.savetxt(f_handle, MyArray, fmt="%.3f")
f_handle.close()
However, I have a couple problems. First, the fmt
really does not work I get values like this:
9.999000000000000000e+03
If I JUST use the code below line of code, I get -9999.000 1.345
, but then, I haven't attached ncols, nrows, etc to the ASCII file.
numpy.savetxt(f_handle, MyArray, fmt="%.3f")
My data range from 0 to 6. What I really want to get is:
-9999 1.345 -9999 3.21 0.13 -9999
where I do NOT get decimals after -9999
and I do get decimals after real data such as 1.345
I made the ASCII file in R, I am wondering there should be a easy way to do it in Python. Thanks for your help in advance.
numpy.savetxt(ASCIIout, a3)
do? You haven't told us what's inASCIIout
(is it just the string "ASCIIout.asc"?), and you haven't told us what's in a3. – frank128791 Jul 22 at 2:33header
option tonumpy.savetxt
– Gabriel Jul 22 at 4:56a3
is the numpy aray andASCIIout
is just the name of the file I am going to write on. – bikhaab Jul 22 at 14:54