How to deal with a .dat file containing characters

  • Thread starter Thread starter lilihoso
  • Start date Start date
  • Tags Tags
    File
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
lilihoso
Messages
5
Reaction score
0
Hi everyone,

I'm programming witn Fortran 90.

Could anyone help me to read a .dat file who contains some columns of characters, it looks like the following:

2011/03/09 02:45:20.33 38.4350 142.8420 32.00 7.50 Mw 550 0.98 NEI 201103092006
2011/03/09 02:57:16.67 38.3610 142.9100 23.20 5.70 Mb 339 0.98 NEI 201103092008
2011/03/09 03:05:52.24 38.2770 142.8420 41.90 4.60 Mb 39 0.74 NEI 201103092011
2011/03/09 04:05:54.98 38.6550 142.7530 35.00 5.20 Mb 135 1.36 NEI 201103092017
2011/03/09 04:15:40.53 38.6890 142.9680 39.20 4.80 Mb 73 1.07 NEI 201103092019

I'd like to make it possible for mathematical calculations with the first 3 columns who are in date format.

Thank you very much in advance!
 
Physics news on Phys.org
If each line has the same format, you can specify a format in your 'read' statement and use the 'x' specifier to skip over the non-numeric characters and blank spaces. For example, if you read from unit 10 and your variables are integers:

Code:
read (10, '(i4,1x,i2,1x,i2,…)') year, month, day, …

You can probably fill in the rest of the statement. Or if the variables are real:

Code:
read (10, '(f4.0,1x,f2.0,1x,f2.0,…)') year, month, day, …
 
Thank you so much jtbell! It's done!
 
Hi jtbell,

I get another problem with my 'matrix', I tried to read my whole matrix which contains 1500 lines with the same form using the statement following:
open(10, file='eq_data.dat', form='formatted', status='old', action='read', iostat=ios)
if (ios .ne. 0) stop "Error!"
ios2=0
i=1
do while (ios2 .eq. 0)
read(10, '(i4,1x,i2,1x,i2,1x,i2,1x,i2,1x,f5.2,2x,f7.4,2x,f8.4,2x,f5.2,2x,f4.2)', iostat=ios2) year(i),month(i),&
day(i),hour(i), min(i), sec(i), lat(i), long(i), dep(i), mag(i)
i=i+1
enddo
close(10)

do i=1,15
write(*,*)year(i), month(i), day(i), hour(i), min(i), sec(i), lat(i), long(i), dep(i), mag(i)
enddo

But it can't never write the correct things from i=10.

Could you tell me where the error is?

Thank you so much again!
 
I would first suspect that the first loop is reading only the first ten (or so) lines of the data file. Try inserting (for testing purposes) the following statement immediately after the 'read' statement:

Code:
write(*,*) i, year(i), month(i), day(i), hour(i), min(i), sec(i), lat(i), long(i), dep(i), mag(i)

Note this also displays the value if 'i'. Also "comment out" the second do-loop so you see only the output from the first one (put a 'c' at the beginning of each line).

If indeed the program reads only to i = 10 (or so), I would examine the input file very carefully near that line.