Python Forum
Reading a .dat file in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a .dat file in Python
#1
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.
Reply
#2
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
Reply
#3
(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.
Reply
#4
If you want to do that, it would be a good idea to print i and j on each line as well.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 829 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,234 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  reading tab file Mandiph 1 2,173 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  Reading time steps from nc file ankurk017 1 2,578 Jul-16-2018, 07:06 PM
Last Post: woooee
  reading, modifying and writing json file metalray 2 10,925 Jun-06-2018, 03:09 PM
Last Post: metalray
  Reading json file as pandas data frame? Alberto 1 8,360 Feb-05-2018, 12:43 AM
Last Post: snippsat
  matplotlib help with reading values from CSV file cps13 6 7,298 Aug-22-2017, 10:21 AM
Last Post: andre
  Need help reading in a binary file shaynehansen 2 3,940 Jun-22-2017, 10:04 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020