Fortran list directed input reading logical variables

Fortran 90’s list directed input is powerful, flexible, but tricky. My function to read a keyword, boolean value pair from a file was not working when I reshuffled the order of the input parameters (blame Python’s orderless dictionaries for this, not that I’m complaining). Originally I just read the string and logical straight in, then tested the string for a match to the key. However when reading list directed logicals it seems that fortran was biting off the next record as well. New improved read_bool follows:

subroutine read_bool(iunit,key,val)
! Searches the input file for the key and reads the value
! to the supplied variable when it finds it.
implicit none
integer iunit
character (len=20) :: st
character (len=*) :: key
logical :: val
integer i,w
rewind(iunit)
w = len(key)
do while (st(1:len(key)) .ne. key)
print *,'reading value'
read(iunit,*,iostat=i) st
print *,i
print *,st,val
! print *,key,st(1:len(key)),i
if(i .eq. -1) then
write(*,*) key
stop 'fatal error: parameter not found'
endif
if (st(1:len(key)) .eq. key) then
! print *,'I found ',st
backspace(iunit)
read(iunit,*,iostat=i) st,val
write(*,*) st,val
return
endif
enddo
end subroutine read_bool

Respect to Nick Maclaren for his online powerpoint slides (which didn’t lead me to the answer, but were quite useful nonetheless.) Also see http://cimss.ssec.wisc.edu/~gumley/programmingtips.txt, and the QUB notes which I always seem to stumble back to.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: