New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
loadnml.F90 on Ticket #2394 – Attachment – NEMO

Ticket #2394: loadnml.F90

File loadnml.F90, 4.3 KB (added by acc, 4 years ago)

plain F90 program to test internal reads of nameless

Line 
1   PROGRAM load_nml
2      CHARACTER(LEN=:)    , ALLOCATABLE  :: cdnambuff
3      CHARACTER(LEN=20)                :: cdnamfile='./nambdy'
4      CHARACTER(LEN=256)                           :: chline
5      CHARACTER(LEN=1)                             :: chsep
6      INTEGER                          :: kout
7      LOGICAL                          :: ldwp =.TRUE. !: .true. only for the root broadcaster
8      INTEGER                          :: itot, iun, iltc, inl, ios, itotsav
9      INTEGER                          :: nbdyind
10      INTEGER                          :: nbdy_rdstart, nbdy_count, nbdy_loc 
11      NAMELIST /nambdy_index/nbdyind
12      !
13      ! Check if the namelist buffer has already been allocated. Return if it has.
14      !
15      !chsep = NEW_LINE('A')
16      chsep = ' '
17      IF ( ALLOCATED( cdnambuff ) ) STOP
18      IF( ldwp ) THEN
19         !
20         ! Open namelist file
21         !
22         iun=15
23         OPEN ( unit=iun, file=cdnamfile)
24         !
25         ! First pass: count characters excluding comments and trimable white space
26         !
27         itot=0
28     10  READ(iun,'(A256)',END=20,ERR=20) chline
29         iltc = LEN_TRIM(chline)
30         IF ( iltc.GT.0 ) THEN
31          inl = INDEX(chline, '!') 
32          IF( inl.eq.0 ) THEN
33           itot = itot + iltc + 1                                ! +1 for the newline character
34          ELSEIF( inl.GT.0 .AND. LEN_TRIM( chline(1:inl-1) ).GT.0 ) THEN
35           itot = itot + inl                                  !  includes +1 for the newline character
36          ENDIF
37         ENDIF
38         GOTO 10
39     20  CONTINUE
40         !
41         ! Allocate text cdnambuff for condensed namelist
42         !
43!$AGRIF_DO_NOT_TREAT
44         ALLOCATE( CHARACTER(LEN=itot) :: cdnambuff )
45!$AGRIF_END_DO_NOT_TREAT
46         itotsav = itot
47         !
48         ! Second pass: read and transfer pruned characters into cdnambuff
49         !
50         REWIND(iun)
51         itot=1
52     30  READ(iun,'(A256)',END=40,ERR=40) chline
53         iltc = LEN_TRIM(chline)
54         IF ( iltc.GT.0 ) THEN
55          inl = INDEX(chline, '!')
56          IF( inl.eq.0 ) THEN
57           inl = iltc
58          ELSE
59           inl = inl - 1
60          ENDIF
61          IF( inl.GT.0 .AND. LEN_TRIM( chline(1:inl) ).GT.0 ) THEN
62             cdnambuff(itot:itot+inl-1) = chline(1:inl)
63             WRITE( cdnambuff(itot+inl:itot+inl), '(a)' ) chsep
64             itot = itot + inl + 1
65          ENDIF
66         ENDIF
67         GOTO 30
68     40  CONTINUE
69         itot = itot - 1
70         IF( itotsav .NE. itot ) WRITE(*,*) 'WARNING in load_nml. Allocated ',itotsav,' for read buffer; but used ',itot
71         !
72         ! Test the contents of the internal file
73         !
74         ! Write it all out:
75         write(6,'(a)') 'The whole internal file: '
76         write(6,'(32A)') cdnambuff
77         write(6,'(a)')'----'
78         ! Write out a sub-string of it
79         write(6,'(a)') '25 characters from position 20: '
80         read(cdnambuff(20:),'(A25)') chline
81         write(6,'(a)') TRIM(chline)
82         write(6,'(a)')'----'
83         write(6,'(a,i5)') 'Length of the compressed internal file: ',LEN_TRIM(cdnambuff)
84         write(6,'(a)')'----'
85         write(6,'(a)') 'nbdyind in the 1st occurrence of nambdy_index: '
86         read(cdnambuff(:), nambdy_index, end=99, err=99)
87         write(6,'(a,i5)') 'nbdyind = ',nbdyind
88         write(6,'(a)')'----'
89         ! Now test reading the 4th occurrence of the namelist
90         nbdy_rdstart = 1
91         DO nbdy_count = 1, 4
92          nbdy_loc = INDEX( cdnambuff( nbdy_rdstart: ), 'nambdy_index' )
93          IF( nbdy_loc .GT. 0 ) THEN
94             nbdy_rdstart = nbdy_rdstart + nbdy_loc
95          ELSE
96             WRITE(*,'(A,I4,A)') 'Error: entry number ',nbdy_count,' of nambdy_index not found'
97          ENDIF
98         END DO
99         nbdy_rdstart = MAX( 1, nbdy_rdstart - 2 )
100         read(cdnambuff(nbdy_rdstart:), nambdy_index, end=99, err=99)
101         write(*,'(a,i5)') 'nbdyind in the 4th occurence of nbdyind = ',nbdyind
102         goto 101
103         !
104    99   write(6,'(a)') 'Not found'
105         !
106   101   REWIND(iun)
107         write(6,'(a)')'----'
108         write(6,'(a)')'Finally the first occurrence directly from the external namelist: '
109         read(iun,nambdy_index)
110         write(*,'(a,i5)') 'nbdyind = ',nbdyind
111         write(6,'(a)')'----'
112         !
113         ! Close namelist file
114         !
115         CLOSE(iun)
116      ENDIF
117  END PROGRAM load_nml