Python Forum
Reading a .dat file in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Reading a .dat file in Python (/thread-17797.html)



Reading a .dat file in Python - mohd_umair - Apr-24-2019

Good Morning,

I am trying to study the correlations between different field quantities in turbulent flows. I have written a code which computes everything that I need and it writes the 2D cross-correlations to a data file in 'ascii' format. The structure of the file goes like this:

# write correlation coefficient in ascii file
dz = z - (z[-1]-z[0])/2.0
rdth= (th- (th[-1]-th[0])/2.0) * r[k]
fnam = 'piCorr2dBoxF'+'{:08d}'.format(iFirst)+'to'+'{:08d}'.format(iLast)+'nt'+'{:04d}'.format(nt)+'.dat'
print('-------------------------------------------------------------------------')
print('Writing cross-correlation to file', fnam)
f = open(fnam, 'w')
f.write('# Two-dimensional two-point cross-correlations\n')
f.write('# For all three velocity components (u_r, u_th, u_z) with eFlux\n')
f.write("# At wall-normal location r = %f -> y+ = %f\n" % (r[k], (1-r[k])*180.4))
f.write('# Python post-processing on data set nsPipe/pipe0002 generated in a DNS using nsPipe\n')
f.write('# Temporal (ensemble) averaging over %d samples\n' % (nt))
f.write("# First snapshot: %08d\n" % (iFirst))
f.write("# Last snapshot:  %08d\n" % (iLast))
f.write("# 01st column: axial separation dz in units of pipe radii (R)\n")
f.write("# 02nd column: azimuthal separation rdth in units of pipe radii (R)\n")
f.write("# 03rd column: u_rPi  correlation in units of RMS\n")
f.write("# 04th column: u_thPi correlation in units of RMS\n")
f.write("# 05th column: u_zPi  correlation in units of RMS\n")
for i in range(nth-1):
 for j in range(nz-1):
  f.write("%23.16e %23.16e %23.16e %23.16e %23.16e\n" % (rdth[i], dz[j], acr[i,j], acth[i,j], acz[i,j]) )
f.close()
I want to generate some 2D plots and for that, I'm writing a new script. The problem is that I don't know how to read this file.
Any kind of help will be appreciated.

Thank you so much.
Have a nice day.


RE: Reading a .dat file in Python - Gribouillis - Apr-24-2019

You know how to write the file but you don't know how to read it? This is somewhat unusual. It is a fairly simple task:
with open(fnam) as f:
    for line in f:
        # do something with the line



RE: Reading a .dat file in Python - mohd_umair - Apr-24-2019

(Apr-24-2019, 10:46 AM)Gribouillis Wrote: You know how to write the file but you don't know how to read it? This is somewhat unusual. It is a fairly simple task:
with open(fnam) as f:
    for line in f:
        # do something with the line

I want to read the file and store all the values in the arrays as I had computed. For example, acr(i,j) is a two-dimensional array, so I want to read the file and store the value in acr(i,j). I'm sorry, I don't whether I explained it clearly what I want to do. And I'm pretty new to python.
It would be helpful if you can somehow tell me how to do it.

Thank you very much.


RE: Reading a .dat file in Python - Gribouillis - Apr-24-2019

If you want to do that, it would be a good idea to print i and j on each line as well.


RE: Reading a .dat file in Python - mohd_umair - Apr-24-2019

(Apr-24-2019, 12:02 PM)Gribouillis Wrote: If you want to do that, it would be a good idea to print i and j on each line as well.

That's the main problem, I don't know how to do that.
Anyways Thanks for your reply.