View Full Version : Reading in sections of files in Fortran
Spanky Deluxe
Apr 23, 2008, 10:56 AM
Is there any way to read in large chunks of a file piece by piece?
I.e. say a file consists of 16 numbers. I'd first want to read in the first 4, do some stuff to them, then load in the second 4 etc.
I'm using the READ command at the moment but I don't know how to do this. The problem isn't reading in the first batch but getting it to read the second batch etc starting at the respective value, i.e. the 5th value in this over-simplified example.
lee1210
Apr 23, 2008, 11:33 AM
the open function takes a recl parameter that can be set to the number of bytes per record. Then when you do a read you give a record number.
integer(4) :: z(4)
integer(2) :: io_status
open(unit=10,file="./testfile.in",action='read', &
& status='old',recl=16,access='direct',iostat=io_status)
if(io_status /= 0) call abort()
read(unit=10,rec=1,iostat=io_status) z(1:4)
if(io_status /= 0) call abort()
-Lee
Spanky Deluxe
Apr 23, 2008, 01:10 PM
the open function takes a recl parameter that can be set to the number of bytes per record. Then when you do a read you give a record number.
integer(4) :: z(4)
integer(2) :: io_status
open(unit=10,file="./testfile.in",action='read', &
& status='old',recl=16,access='direct',iostat=io_status)
if(io_status /= 0) call abort()
read(unit=10,rec=1,iostat=io_status) z(1:4)
if(io_status /= 0) call abort()
-Lee
Awesome! Thanks for that! Big help. :)
lee1210
Apr 23, 2008, 01:25 PM
Awesome! Thanks for that! Big help. :)
No problem. I'm glad working with Fortran the last 4 years came in handy SOMEWHERE else.
-Lee
Hello,
I am also trying to read from a file in fortran but so far unsuccessfully.
my file looks like this [stationID and associated characteristic]:
A2332110 PLUVI
A2352020 PLUVI
A3301010 PLUVI
A3422010 NIVAL
A3792010 PLUVI
A3832010 PLUVI
A4050620 TRANS
how can I make fortran read it? thank you in advance, Ig
I have tried with this but it gives me error (i am new to fortran...)
program readstregime
CHARACTER(LEN=8) :: STATION
CHARACTER(LEN=5) :: REGIME
OPEN (UNIT=1, FILE="STATIONVSREGIME.TXT", STATUS='OLD')
READ(1,*) STATION, REGIME
PRINT*, STATION, REGIME
END
lee1210
Jun 4, 2009, 10:46 AM
Hello,
I am also trying to read from a file in fortran but so far unsuccessfully.
my file looks like this [stationID and associated characteristic]:
A2332110 PLUVI
A2352020 PLUVI
A3301010 PLUVI
A3422010 NIVAL
A3792010 PLUVI
A3832010 PLUVI
A4050620 TRANS
how can I make fortran read it? thank you in advance, Ig
I have tried with this but it gives me error (i am new to fortran...)
program readstregime
CHARACTER(LEN=8) :: STATION
CHARACTER(LEN=5) :: REGIME
OPEN (UNIT=1, FILE="STATIONVSREGIME.TXT", STATUS='OLD')
READ(1,*) STATION, REGIME
PRINT*, STATION, REGIME
END
Give this a try:
program readstregime
character(8) :: station
character(5) :: regime
integer :: stat
logical :: reading = .true.
open (unit=1, file="STATIONVSREGIME.TXT", status='OLD',iostat=stat)
if(stat /= 0) then
write(7,'("Could not open file, exiting!")')
stop
end if
do while(reading)
read(1,'(a8,1x,a5)',iostat=stat,end=1) station, regime
if(stat /= 0) then
write(7,'("Could not read from file, exiting!")')
stop
endif
write(6,'("Station: ",a," Regime: ",a)') station, regime
cycle
1 reading=.false.
end do
close(1)
end program readstregime
It's well past 1977, so all caps are not necessary. I don't like to feel like my code is shouting at me =).
-Lee
I got a
Could not open file, exiting!
But thanks a lot for helping me
lee1210
Jun 4, 2009, 11:37 AM
Is STATIONVSREGIME.TXT in the directory you're running the program from? Are the permissions such that the user you're running as can read it?
-Lee
Is STATIONVSREGIME.TXT in the directory you're running the program from? Are the permissions such that the user you're running as can read it?
-Lee
yes the file is in the same directory in which i have run other fortran programs too so I have the permissions.
I attach the file STATIONVSREGIME.TXT maybe i'm doing something wrong ...
Ig
lee1210
Jun 4, 2009, 11:55 AM
Pull out the iostat= argument to open, and see what error the runtime gives you. The code worked as posted on my machine with a copy and paste of the data in your post.
-Lee
Pull out the iostat= argument to open, and see what error the runtime gives you. The code worked as posted on my machine with a copy and paste of the data in your post.
-Lee
here's what I got after pulling out the iostat argument:
At line 7 of file readstregime.f90
Fortran runtime error: No such file or directory
line 7 --> open (unit=1, file="stationvsregime.TXT", status='OLD')
what could that be?
It works now!
Here's what I did: saved the content of the text file (STATIONVSREGIME.TXT)
as text UTF-8 without the .txt extension
this way I don't get errors and it works. Maybe it depends on the compilers I don't know
I am using gfortran on Ubuntu Linux.
Thanks a lot Lee
By the way I was trying to install gfortran on my home machine (mbp) but ran into some errors. I had even installed the apple development tools... What is the best way to compile fortran under OSX?
lee1210
Jun 5, 2009, 10:29 AM
I use g95 most frequently... there was some discussion about getting a fortran compiler set up in this thread:
http://forums.macrumors.com/showthread.php?t=660181
As for the filename... string literals (anything enclosed in apostrophes or quotes) are pretty much the only thing where case will matter, so just make sure if a filename is all caps, that's how it appears in code, etc.
-Lee
ign
Jun 15, 2009, 05:11 AM
Hello,
I am running a fortran f90 program which writes several output files,
I would like these files to be saved to a specific folder (not to the
current default directory)
here's a write sentence:
open(unit=3, file=val(i,1)//'_Brutes_lowV.txt', status='replace')
do h=1, size(pmv)
write(3,*) YearList(h), pmv(h), lowV(h,:)
enddo
close(3)
where do I add instructions so that the file is written say in
an output folder located on the desktop (/home/ign/desktop/output)?
I have tried this (adding the path before the file name) but it hasn't worked!
open(unit=3, file='home/ign/desktop/output/val(i,1)//'_Brutes_lowV.txt'', status='replace')
thank you in advance,
ign
ign
Jun 15, 2009, 08:16 AM
solved the problem!!
and to make it slim I declared a path
CHARACTER*29 :: path='/home/....../output/'
which I then added to the sentence OPEN(....
open(unit=3, file=path//val(i,1)//'_Brutes_lowD.txt', status='unknown')
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.