I am using the following code to write an array of statistic values into a text file (row wise):
data_in = np.loadtxt('input_file.asc')
with open('output_file.txt', 'a+') as outfile:
values = data_in[:,1]
min_data = np.min(values)
max_data = np.max(values)
avg_data = np.mean(values)
stats = np.array([min_data, max_data, avg_data])
np.savetxt(outfile, stats.reshape(1,stats.shape[0]), delimiter=' ', newline='\n')
I want to use this code to read multiple data_in files and append the statistics into a new row of 'output_file.txt'. I'm using the reshape function as suggested in a previous post here, so every batch of statistics is stored in a new row.
My problem is when I want to access the data stored in 'output_file.txt' for later plotting. If I use:
stats_out = np.loadtxt('output_file.txt')
min_values = stats_out[:,0]
max_values = stats_out[:,1]
avg_values = stats_out[:,2]
I get the Error message: IndexError: too many indices
Is this error caused because I'm using the reshape function?
An example of output_file.txt after running the script several times is:
0.077 -0.330 0.303
0.107 -0.506 0.350
0.092 -0.548 0.405
0.138 -2.358 0.445
0.100 -0.461 0.337
An example of input_file.asc:
-1492.292540 -87.984545 95.515548 NaN NaN 8.18
-1491.349103 -87.982864 93.908249 NaN NaN 10.55
-1490.405666 -87.979607 92.304869 NaN NaN 10.73
-1489.462227 -87.974784 90.707865 NaN 7.740 10.60
-1488.518791 -87.968404 89.119650 NaN NaN 10.18
-1487.575353 -87.960482 87.542539 NaN NaN 10.10
-1486.631915 -87.951037 85.978759 NaN NaN 10.11
min_values = stats_out[:,0]IndexError: too many indicesI'm running Python2.7.8 |Anaconda 2.0.1