1

I have a nxm numpy array that either has positive values with 8 decimal points like 0.02113342 or NoValue data that is -9999. I am using the line below to make a text file from the numpy array

numpy.savetxt("result.asc", Numpy_Array, fmt="%.8f")#2D array to ASCII

However, I will have -9999.00000000 instead of -9999. I open the file and replace those numbers with -9999 using the following code:

with file("result.asc", 'r') as original: 
    data = original.read()
    new = data.replace(str(-9999)+".00000000", str(-9999))
with file("result.asc", 'w') as modified:
    modified.write(new)

Is there a more elegant way to write -9999 rather than -9999.00000000 from the beginning instead of opening the whole file again and replacing them?

ahoosh
  • 1,340
  • 3
  • 17
  • 31

1 Answers1

5

Try fmt="%.8g". It is not the same as "%.8f" for all floating point values (e.g. for very small values, it uses exponential notation), but it might work for the cases you have. (See the table at https://docs.python.org/2/library/string.html#format-specification-mini-language with the floating point types--scroll down a bit--for an explanation of the difference between the f and g formats.)

In [188]: x
Out[188]: 
array([[    0.20134635, -9999.        ],
       [    0.9287082 ,     0.00000123],
       [    0.77482316,     0.27246281],
       [    0.40529746,     0.41133371]])

In [189]: np.savetxt("xf.dat", x, fmt="%.8f")

In [190]: np.savetxt("xg.dat", x, fmt="%.8g")

In [191]: !cat xf.dat
0.20134635 -9999.00000000
0.92870820 0.00000123
0.77482316 0.27246281
0.40529746 0.41133371

In [192]: !cat xg.dat
0.20134635 -9999
0.9287082 1.2345679e-06
0.77482316 0.27246281
0.40529746 0.41133371

Here's the actual value of x[1,1]:

In [193]: x[1,1]
Out[193]: 1.23456789e-06

Alternatively, take a look at this answer to a similar question: How to format in numpy savetxt such that zeros are saved only as "0"

Community
  • 1
  • 1
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214