How to deal with a .dat file containing characters

  • Thread starter Thread starter lilihoso
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
The discussion centers around reading a .dat file in Fortran 90, specifically a file containing multiple columns of character data formatted with timestamps and numerical values. The initial inquiry seeks assistance in reading the first three columns, which represent dates, for mathematical calculations. A suggested solution involves using the 'read' statement with specific formatting to skip non-numeric characters. Subsequently, the user encounters an issue while attempting to read a larger dataset containing 1500 lines. The provided code snippet for reading the data is discussed, with the user reporting that it fails to write correct values beyond the tenth entry. The response advises inserting a debugging statement to verify the values being read and suggests checking the input file for potential formatting issues near the problematic lines. This approach aims to identify whether the reading loop is functioning correctly and to ensure the integrity of the data being processed.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
4
Views
2K
Back
Top