How to deal with a .dat file containing characters

  • Thread starter Thread starter lilihoso
  • Start date Start date
  • Tags Tags
    File
Click For Summary

Discussion Overview

The discussion revolves around reading a .dat file containing character columns using Fortran 90. Participants are exploring methods to extract date and numerical data for mathematical calculations, addressing issues related to file reading and data formatting.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant seeks help to read a .dat file with specific formatting, particularly for the first three columns representing dates.
  • Another participant suggests using the 'read' statement with format specifiers to skip non-numeric characters, providing examples for both integer and real variables.
  • A participant confirms successful implementation of the suggested reading method.
  • A different participant describes an issue with reading a larger matrix from the file, indicating that the output is incorrect after a certain point.
  • One participant proposes debugging the reading process by adding a write statement to check the values being read and suggests commenting out the second loop to isolate the output from the first loop.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the issue with the larger matrix reading, as the problem remains unresolved and further debugging is suggested.

Contextual Notes

There may be limitations related to the input file's structure or the assumptions made in the reading format, which have not been fully explored or clarified.

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!
 
Technology 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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K