| Thread Closed |
Loading 2D array files to Fortran variables |
Share Thread | Thread Tools |
| Apr14-09, 04:20 AM | #1 |
|
|
Loading 2D array files to Fortran variables
Hi,
I have created data files by compiling and running a c++ code. Now I need to open and store the contents of these files in Fortran 77 variables. But I am not able to do this. The read command does not return the values stored in the file. The files are having some 2003 lines and 53 colomns. The values are in the form "2.885531e+002" (positive) and "-7.687902e+001" (negative) with 1 space seperating different values. Thank you in advance! /Z |
| Apr14-09, 06:51 AM | #2 |
|
Mentor
|
Can you show us the code that you tried to use for reading the file?
|
| Apr14-09, 07:11 AM | #3 |
|
|
The entire code is very long, so I will only write the part where the file is opened and read. I have tried many different formats, here is the last format I tried with:
REAL EVg(2003,53) open(1,file=''test.txt'') read(1,10,iostat=k) EVg 10 format(53(G7E3,1X)) close(1) the iostat has returned different values (-1, 0, 115, etc.) for different format cases I have tried. For the case above it returns 0, but the "EVg" array does not contain the array values. |
| Apr14-09, 04:32 PM | #4 |
|
Mentor
|
Loading 2D array files to Fortran variables
It's been a long time since I had to deal with this sort of thing, but as I recall, the basic principles are these: (1) When the number of values to be input exceeds the number of specifiers in the format statement, the format simply repeats itself. (2) All data in a single input statement is assumed to be on one line, unless the format specifies otherwise.
So I think your program is trying to read the entire array from the first line of the input file. Try adding a slash at the end of the format, which should skip to the next line: 10 format(53(G7E3,1X)/) This may still give you problems if the file doesn't have a blank space after the last number on each line. Also, I don't remember a format specifier like "G7E3", but my Fortran experience ended with Fortran 77. |
| Apr14-09, 09:31 PM | #5 |
|
|
I don't either.
In fact, the enhanced format specifiers (E,F,G) are mostly for displaying pretty output. For input, an E,F, or G specifier will accept both 123456.78 or 1.1234E6, as shown in the following example code. The input I gave was 1.123456E+012 1.123456e+013 . The output was printed correctly. Code:
C test fortran format
REAL*8 A,B
READ(5,1)A,B
C 1 FORMAT(2(F13.6,1X))
C 1 FORMAT(2(E13.6,1X))
1 FORMAT(2(G13.6,1X))
PRINT *,A,B
STOP
END
If you still have problems, post a few lines of your input, using the code option so we see exactly how the data are laid out. Also, check if the spaces between the data are aligned. The format above expects them to be aligned. If you have a problem with data alignment, write a c-program to read in and write them out aligned. That would be easier to do to figure out how to read it using free-format input. |
| Apr15-09, 10:58 AM | #6 |
|
|
Thank you for your response!
I should maybe add that I am working in Fortran 77 enviroment (due to limitations). The data in the files basically look like: Code:
+0.000000e+000 -7.281896e+002 -1.791944e+003 +0.000000e+000 -7.856508e+002 -1.824643e+003 -1.325604e+005 -8.411246e+002 -1.856220e+003 Code:
-1.045771e+005 +1.047517e+004 -1.180211e+003 -5.662817e+004 +1.152035e+005 -1.173054e+003 +1.187847e+004 +2.266384e+005 -1.165909e+003 The file reading procedure: Code:
REAL EVg(2003,53) open(1,file=''test.txt'') read(1,10,iostat=k) EVg 10 format(53(E13.6,1X)/) close(1) |
| Apr16-09, 07:05 PM | #7 |
|
|
Indeed, an error in the format will cause the numbers read in to become zeroes.
Using the following code: Code:
C testREAD fortran format
REAL*8 EVg(6,3)
open(1,file='testread.dat')
DO 5 I=1,6
5 READ(1,FMT='(3E15.6)',END=15)(EVg(I,J),J=1,3)
15 close(1)
DO 20 I=1,6
20 WRITE(6,FMT='(SP,3(1PE15.6E3))')(EVg(I,J),J=1,3)
END
Code:
+0.000000E+000 -7.281896E+002 -1.791944E+003 +0.000000E+000 -7.856508E+002 -1.824643E+003 -1.325604E+005 -8.411246E+002 -1.856220E+003 -1.045771E+005 +1.047517E+004 -1.180211E+003 -5.662817E+004 +1.152035E+005 -1.173054E+003 +1.187847E+004 +2.266384E+005 -1.165909E+003 |
| Apr17-09, 10:33 AM | #8 |
|
|
I have now tried with the following (on a somewhat smaller file [2001][12]),
Code:
open(1,file="test.txt") DO t=1,2001 READ(1,'(12E14.6)',iostat=ios)(EVg(t,x),x=1,12) ENDDO close(1) DO t=1,2001 WRITE(6,FMT='(SP,12(1PE15.6E3))')(EVg(5,x),x=1,12) ENDDO Code:
'12(E14.6,1X)' For both formats I still get a EVg to be an array of 0's. Can there be a problem with the compiler I am using? I am using the "mingw32-make.exe" which runs a "g77 -c -O2 fortran.f" and a "g77 -s -o file.exe dimdef.o newmods.o comtac.o fgnmod.o usernl.o analyt.o devt69.o usrfun.o hopcod.o user10.o tpbig.a dislin.a -luser32 -lgdi32 -lcomdlg32". |
| Apr17-09, 03:05 PM | #9 |
|
|
I cannot pronounce anything until you show the actual data you are using with 12 numbers on the same line. In Fortran, the size of each field is important. Even an extra space at the end of the line could be significant. The number of lines is not important in this case.
If you could post 4-5 lines of data, then I'll see how the data fits in with the format. If we don't find anything there, then we may have to worry about the compiler. |
| Apr18-09, 03:04 AM | #10 |
|
|
Here are 5 lines taken from the middle of the data file:
Code:
+1.670000e-006 +0.000000e+000 -8.540156e+006 -1.585399e+003 -2.243492e+003 -2.763622e+003 -3.142946e+003 -3.299538e+003 -3.304998e+003 -3.342794e+003 -3.706140e+003 -4.368216e+003 +1.680000e-006 -9.375357e+004 -8.037304e+006 -1.612638e+003 -2.255844e+003 -2.763778e+003 -3.130649e+003 -3.279551e+003 -3.282095e+003 -3.319731e+003 -3.677900e+003 -4.360687e+003 +1.690000e-006 -2.859594e+005 -7.588826e+006 -1.639665e+003 -2.269052e+003 -2.765124e+003 -3.119963e+003 -3.261429e+003 -3.261483e+003 -3.300265e+003 -3.655372e+003 -4.347075e+003 +1.700000e-006 -6.289417e+005 -7.239074e+006 -1.666989e+003 -2.283188e+003 -2.767609e+003 -3.110709e+003 -3.244538e+003 -3.242276e+003 -3.283470e+003 -3.638555e+003 -4.326224e+003 +1.710000e-006 -1.137889e+006 -7.009578e+006 -1.694831e+003 -2.298037e+003 -2.770898e+003 -3.102424e+003 -3.228056e+003 -3.223421e+003 -3.268063e+003 -3.626592e+003 -4.298185e+003 |
| Apr19-09, 09:24 AM | #11 |
|
|
Sorry it took a little more time than I thought would take, but I have remodelled the program to make it a little more general in order to read in a variable number of lines and fields. It seems to read in and write correctly.
You could try with this new version and make adaptations required for your purposes: Code:
C testREAD fortran format
REAL*8 EVg(5,12)
CHARACTER *20 READFMT
CHARACTER *25 WRITEFMT
NLINES=5
NFIELDS=12
WRITE(READFMT,'(1H(I2,5HE15.61H))')NFIELDS
WRITE(WRITEFMT,'(4H(SP,,I2,12H(1PE15.6E3)) )')NFIELDS
PRINT *,READFMT,WRITEFMT
C (12E15.6) (SP,12(1PE15.6E3))
OPEN(1,FILE='testread.512',FORM='FORMATTED',IOSTAT=IOS,RECL=181)
IF(IOS.NE.0) THEN
PRINT *,'ERROR IOS=',IOS
STOP
ENDIF
DO 5 I=1,NLINES
READ(1,FMT=READFMT,END=15)(EVg(I,J),J=1,NFIELDS)
5 CONTINUE
15 close(1)
DO 20 I=1,NLINES
WRITE(6,FMT=WRITEFMT)(EVg(I,J),J=1,NFIELDS)
20 CONTINUE
C FORMAT(SP,12(1PE15.6E3))
STOP
END
Code:
+1.670000E-006 +0.000000E+000 -8.540156E+006 -1.585399E+003 -2.243492E+003 -2.763622E+003 -3.142946E+003 -3.299538E+003 -3.304998E+003 -3.342794E+003 -3.706140E+003 -4.368216E+003 +1.680000E-006 -9.375357E+004 -8.037304E+006 -1.612638E+003 -2.255844E+003 -2.763778E+003 -3.130649E+003 -3.279551E+003 -3.282095E+003 -3.319731E+003 -3.677900E+003 -4.360687E+003 +1.690000E-006 -2.859594E+005 -7.588826E+006 -1.639665E+003 -2.269052E+003 -2.765124E+003 -3.119963E+003 -3.261429E+003 -3.261483E+003 -3.300265E+003 -3.655372E+003 -4.347075E+003 +1.700000E-006 -6.289417E+005 -7.239074E+006 -1.666989E+003 -2.283188E+003 -2.767609E+003 -3.110709E+003 -3.244538E+003 -3.242276E+003 -3.283470E+003 -3.638555E+003 -4.326224E+003 +1.710000E-006 -1.137889E+006 -7.009578E+006 -1.694831E+003 -2.298037E+003 -2.770898E+003 -3.102424E+003 -3.228056E+003 -3.223421E+003 -3.268063E+003 -3.626592E+003 -4.298185E+003 |
| Apr23-09, 05:59 AM | #12 |
|
|
Thank you, it is working now. I was stuck with these lines for more than 2 weeks.... but now everything is working.
|
| Apr23-09, 08:57 PM | #13 |
|
|
Glad that everything works out, come and visit us often!
|
| Thread Closed |
| Tags |
| format, fortran 77, read file |
| Thread Tools | |
Similar Threads for: Loading 2D array files to Fortran variables
|
||||
| Thread | Forum | Replies | ||
| array shuffling in fortran | Programming & Comp Sci | 1 | ||
| Fortran 90 (need to read in binary data files into arrays) | Math & Science Software | 1 | ||
| Accessing Fortran Modules within a Fortran library from Fortran | Programming & Comp Sci | 0 | ||
| Fortran 90 question about reading files with text | Programming & Comp Sci | 1 | ||
| Implicit Variables in FORTRAN | Programming & Comp Sci | 2 | ||