Reputation: 121
I want to write some information from a certain hdf5 file to a new txt file. The information include time,lat,lon,obs,covariance matrix, where each variable is array.
The time
is 1D array with a shape (t,)
, lat
and lon
are also 1D array with a shape (t,)
, which represent the location at certain time, the obs
is 2D array, with a shape (t,n)
, which mean we have t observation, each observation contains n state, the covariance matrix
is a 3D array , with a shape (t,n,n)
.What I want to do extract these information one by one.
with open('test.dat','w') as output_file:
for i in np.arange(10):
output_file.writelines(str(time[i])+' '+str(lat[i])+' '+str(lon[i])+'\n')
output_file.writelines('%s ' % l for l in obs[i])
output_file.write('\n')
output_file.writelines('%s ' % l for l in cov[i])
output_file.write('\n')
Let say we have time= np.array([1,2,3])
,
lat=np.array([20,30,40])
,lon=np.array([40,50,70])
,
obs=np.random.random((3,4))
,cov = np.random.random((3,4,4))
,
I want the output file writes like:
time[0] lat[0] lon[0]
obs[0,0] obs[0,1] obs[0,2] obs[0,3]
cov[0,0,0] cov[0,0,1] cov[0,0,2] cov[0,0,3]
....
....
cov[0,3,0] cov[0,3,1] cov[0,3,2] cov[0,3,3]
time[1] lat[1] lon[1]
.....
Each line in the example output is to be one text line in the output file. The fields in a line are to be separated by a single space. But it seems the code I wrote didn't work, how should I do?
Upvotes: 1
Views: 62
Reputation: 22544
This code seems to work. I avoided a few loops by using the print(*anarray)
trick to easily print the contents of a one-dimensional array in one line separated by spaces. I used print
rather than write
to easily get the end-of-line markers in the right places. I could have left out the sep=' '
parameters since they are the default but I thought that "explicit is better than implicit." I removed your str
conversions since print
does that conversion automatically. I also put in some assert
statements to guarantee consistency in the array shapes.
import numpy as np
t = 3
n = 4
time = np.array([1, 2, 3])
lat = np.array([20, 30, 40])
lon = np.array([40, 50, 70])
obs = np.random.random((3, 4))
cov = np.random.random((3, 4, 4))
assert time.shape == lat.shape == lon.shape == (t,)
assert obs.shape == (t, n)
assert cov.shape == (t, n, n)
with open('test.dat', 'w') as output_file:
for i in range(t):
print(time[i], lat[i], lon[i], sep=' ', file=output_file)
print(*obs[i], sep=' ', file=output_file)
for j in range(n):
print(*cov[i, j], sep=' ', file=output_file)
Here is the contents of the file test.dat
after that code is run:
1 20 40
0.3299447219312376 0.996996139803863 0.9551526837239497 0.5509582503806248
0.8819189869595838 0.9132355846068761 0.9665353304879704 0.46635220924415366
0.5181587118344747 0.8038185825031468 0.6745351776394765 0.5995935654205414
0.8619757623942051 0.482577455518059 0.7957648986891931 0.7407148158441567
0.09795020526664455 0.794707302185401 0.5293100793000466 0.3338543543683751
2 30 50
0.8035406182773407 0.1185746504181443 0.580297768900217 0.18777926684714497
0.532529800187838 0.12522453075786388 0.46300433798756324 0.22510518678645997
0.29260759039257944 0.3045692764639927 0.8085267868489262 0.6155070434401326
0.744762931777478 0.3722679713441319 0.395290426225012 0.3838002215325691
0.8049013701055735 0.4221372661820496 0.9607451508389756 0.7143917783338681
3 40 70
0.747699155972642 0.0016320243955539881 0.6063364532047132 0.8264067710516358
0.5795303829414947 0.49210530688638277 0.7499924344997647 0.601564975827258
0.7578052140931559 0.3514264858000006 0.7016988143157379 0.7628124804393464
0.3503877412670806 0.4020137310035564 0.688387279488728 0.46935179605446187
0.21964712005015719 0.8918053594781274 0.7908691508919491 0.006491011595309404
Upvotes: 1