Hi Neil,
seek_to_namelist() seems to be interesting. This could shorten my code a
bit.
Attached, there is a sample code which does what I want just for one
group. I could simply copy it for all occurring groups. This, however,
is very redundant due to the fact that just the NAMELIST-group and '! do
some stuff' needs to be changed.
Cheers, Stefan
PROGRAM main
IMPLICIT NONE
write
INTEGER :: i, ios, n_groups
REAL, DIMENSION(3) :: coord
TYPE inpp
REAL, DIMENSION( 3 ) :: coord
END TYPE inpp
TYPE(inpp), DIMENSION(:), ALLOCATABLE :: inps
NAMELIST /inp/ coord
n_groups = 0
OPEN( 99, FILE='conf.nml' )
DO
READ( 99, IOSTAT=ios , NML=inp )
IF ( ios == 0 ) THEN
n_groups = n_groups + 1
ELSE
EXIT
END IF
END DO
ALLOCATE( inps( n_groups ) )
REWIND( 99 )
DO i=1, SIZE( inps )
READ( 99, NML=inp )
! do some stuff
END DO
CLOSE( 99 )
! do the same for a whole bunch of other groups
END PROGRAM main
On Fri, 2012-06-08 at 10:42 -0600, Neil Carlson wrote:
> I don't fully understand what you need to do (the answer to your
> specific question is no)
> but I can suggest another approach that might apply. One code I deal
> with uses namelists
> for its input. There are many of them, can appear in any order, and
> some can appear
> multiple times (each instance having a distinct effect). The key to
> parsing this input
> file is a subroutine
>
> SUBROUTINE SEEK_TO_NAMELIST (UNIT, NML, FOUND [, IOSTAT])
> INTEGER, INTENT(IN) :: UNIT
> CHARACTER(*), INTENT(IN) :: NML
> LOGICAL, INTENT(OUT) :: FOUND
> INTEGER, INTENT(OUT), OPTIONAL :: IOSTAT
>
> that scans forward through the input file looking for the next
> occurrence of the specified
> namelist (as a string). Using this together with the REWIND statement
> makes parsing
> the input file pretty easy, with stanzas of code like
>
> namelist /foo/ x, y, z
> rewind(unit)
> do
> call seek_to_namelist (unit, 'foo', found)
> if (.not.found) exit
> real(unit, nml=foo)
> ! do something with the namelist variable values
> end do
>
> I can send you the implementation of seek_to_namelist if you're
> interested.
> It's a bit tricky to ensure that you find a valid namelist opening,
> and only valid
> ones, and then you need to leave the input file properly positioned.
>
> -Neil
>
> On Fri, Jun 8, 2012 at 7:30 AM, Stefan Ano
> <[log in to unmask]> wrote:
> Hi There,
>
> right now I stuck with Fortran's NAMELISTs. My objective is to
> simply
> parse a config-file containing several groups, each multiple
> times.
> Well, this is not so very hard, however, I am looking for a
> sophisticated way iterating through all the NAMELISTs using
> Fortran 95.
>
> Sure, I start just by defining all my NAMELISTs. As a second
> Step - and
> this is where I stuck - I would like iterate through all those
> NAMELISTs. Is there a way to put NAMELISTs into an array or
> so? My idea
> is having a code like:
> DO i=1, SIZE( lists )
> READ( 99, NMT=lists(i) )
> ...
> END DO
> Any suggestions how to put all the NAMELISTs into one
> 'container' I
> could iterate through?
> (Another even more sophisticate approach might be to figure
> out how to
> pass a NAMELIST as a function argument. Any ideas? )
>
> Cheers,
> Stefan
>
> Btw.: I am using gfortran on Debian Lenny.
>
|