Index: /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdydta.F90
===================================================================
--- /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdydta.F90	(revision 11640)
+++ /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdydta.F90	(revision 11641)
@@ -373,4 +373,6 @@
       INTEGER ::   ierror, ios     ! 
       !
+      INTEGER ::   nbdy_rdstart, nbdy_loc
+      CHARACTER(LEN=50)                      ::   cerrmsg       ! error string
       CHARACTER(len=3)                       ::   cl3           ! 
       CHARACTER(len=100)                     ::   cn_dir        ! Root directory for location of data files
@@ -415,5 +417,5 @@
       ! Read namelists
       ! --------------
-      REWIND(numnam_cfg)
+      nbdy_rdstart = 1
       DO jbdy = 1, nb_bdy
 
@@ -421,6 +423,5 @@
          WRITE(ctmp2, '(a,i2)') 'block nambdy_dta number ', jbdy
 
-         ! There is only one nambdy_dta block in namelist_ref -> use it for each bdy so we do a rewind 
-         REWIND(numnam_ref)
+         ! There is only one nambdy_dta block in namelist_ref -> use it for each bdy so we read from the beginning
          READ  ( numnam_ref, nambdy_dta, IOSTAT = ios, ERR = 901)
 901      IF( ios /= 0 )   CALL ctl_nam ( ios , 'nambdy_dta in reference namelist' )
@@ -431,6 +432,20 @@
             & .OR. ( dta_bdy(jbdy)%lneed_tra   .AND.       nn_tra_dta(jbdy)    == 1 )   &
             & .OR. ( dta_bdy(jbdy)%lneed_ice   .AND.       nn_ice_dta(jbdy)    == 1 )   )   THEN
-            ! WARNING: we don't do a rewind here, each bdy reads its own nambdy_dta block one after another
-            READ  ( numnam_cfg, nambdy_dta, IOSTAT = ios, ERR = 902 )
+            !
+            ! Need to support possibility of reading more than one
+            ! nambdy_dta from the namelist_cfg internal file.
+            ! Do this by finding the jbdy'th occurence of nambdy_dta in the
+            ! character buffer as the starting point.
+            !
+            nbdy_loc = INDEX( numnam_cfg( nbdy_rdstart: ), 'nambdy_dta' )
+            IF( nbdy_loc .GT. 0 ) THEN
+               nbdy_rdstart = nbdy_rdstart + nbdy_loc
+            ELSE
+               WRITE(cerrmsg,'(A,I4,A)') 'Error: entry number ',jbdy,' of nambdy_dta not found'
+               ios = -1
+               CALL ctl_nam ( ios , cerrmsg )
+            ENDIF
+            nbdy_rdstart = MAX( 1, nbdy_rdstart - 2 )
+            READ  ( numnam_cfg( nbdy_rdstart: ), nambdy_dta, IOSTAT = ios, ERR = 902)
 902         IF( ios >  0 )   CALL ctl_nam ( ios , 'nambdy_dta in configuration namelist' )
             IF(lwm) WRITE( numond, nambdy_dta )           
Index: /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdyini.F90
===================================================================
--- /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdyini.F90	(revision 11640)
+++ /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdyini.F90	(revision 11641)
@@ -75,5 +75,4 @@
       ! Read namelist parameters
       ! ------------------------
-      REWIND( numnam_ref )              ! Namelist nambdy in reference namelist :Unstructured open boundaries
       READ  ( numnam_ref, nambdy, IOSTAT = ios, ERR = 901)
 901   IF( ios /= 0 )   CALL ctl_nam ( ios , 'nambdy in reference namelist' )
@@ -93,5 +92,4 @@
       cn_ice         (2:jp_bdy) = cn_ice         (1)
       nn_ice_dta     (2:jp_bdy) = nn_ice_dta     (1)
-      REWIND( numnam_cfg )              ! Namelist nambdy in configuration namelist :Unstructured open boundaries
       READ  ( numnam_cfg, nambdy, IOSTAT = ios, ERR = 902 )
 902   IF( ios >  0 )   CALL ctl_nam ( ios , 'nambdy in configuration namelist' )
@@ -364,5 +362,4 @@
       ! -------------------------------------------------
 
-      REWIND( numnam_cfg )     
       nblendta(:,:) = 0
       nbdysege = 0
@@ -1079,12 +1076,26 @@
       INTEGER          ::   ios                 ! Local integer output status for namelist read
       INTEGER          ::   nbdyind, nbdybeg, nbdyend
+      INTEGER          ::   nbdy_count, nbdy_rdstart, nbdy_loc
       CHARACTER(LEN=1) ::   ctypebdy   !     -        - 
+      CHARACTER(LEN=50)::   cerrmsg    !     -        - 
       NAMELIST/nambdy_index/ ctypebdy, nbdyind, nbdybeg, nbdyend
       !!----------------------------------------------------------------------
-
-      ! No REWIND here because may need to read more than one nambdy_index namelist.
-      ! Read only namelist_cfg to avoid unseccessfull overwrite 
-      ! keep full control of the configuration namelist
-      READ  ( numnam_cfg, nambdy_index, IOSTAT = ios, ERR = 904 )
+      ! Need to support possibility of reading more than one nambdy_index from
+      ! the namelist_cfg internal file.
+      ! Do this by finding the kb_bdy'th occurence of nambdy_index in the
+      ! character buffer as the starting point.
+      nbdy_rdstart = 1
+      DO nbdy_count = 1, kb_bdy
+       nbdy_loc = INDEX( numnam_cfg( nbdy_rdstart: ), 'nambdy_index' )
+       IF( nbdy_loc .GT. 0 ) THEN
+          nbdy_rdstart = nbdy_rdstart + nbdy_loc
+       ELSE
+          WRITE(cerrmsg,'(A,I4,A)') 'Error: entry number ',kb_bdy,' of nambdy_index not found'
+          ios = -1
+          CALL ctl_nam ( ios , cerrmsg )
+       ENDIF
+      END DO
+      nbdy_rdstart = MAX( 1, nbdy_rdstart - 2 )
+      READ  ( numnam_cfg( nbdy_rdstart: ), nambdy_index, IOSTAT = ios, ERR = 904)
 904   IF( ios /= 0 )   CALL ctl_nam ( ios , 'nambdy_index in configuration namelist' )
       IF(lwm) WRITE ( numond, nambdy_index )
Index: /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdytides.F90
===================================================================
--- /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdytides.F90	(revision 11640)
+++ /NEMO/branches/2019/dev_r11613_ENHANCE-04_namelists_as_internalfiles/src/OCE/BDY/bdytides.F90	(revision 11641)
@@ -71,4 +71,6 @@
       INTEGER, DIMENSION(3)                     ::   ilen0       !: length of boundary data (from OBC arrays)
       INTEGER                                   ::   ios                 ! Local integer output status for namelist read
+      INTEGER                                   ::   nbdy_rdstart, nbdy_loc
+      CHARACTER(LEN=50)                         ::   cerrmsg             !: error string
       CHARACTER(len=80)                         ::   clfile              !: full file name for tidal input file 
       REAL(wp),ALLOCATABLE, DIMENSION(:,:,:)    ::   dta_read            !: work space to read in tidal harmonics data
@@ -84,6 +86,6 @@
       IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~'
 
-      REWIND(numnam_cfg)
-
+
+      nbdy_rdstart = 1
       DO ib_bdy = 1, nb_bdy
          IF( nn_dyn2d_dta(ib_bdy) >= 2 ) THEN
@@ -94,9 +96,22 @@
             filtide(:) = ''
 
-            REWIND( numnam_ref )
             READ  ( numnam_ref, nambdy_tide, IOSTAT = ios, ERR = 901)
 901         IF( ios /= 0 )   CALL ctl_nam ( ios , 'nambdy_tide in reference namelist' )
-            ! Don't REWIND here - may need to read more than one of these namelists. 
-            READ  ( numnam_cfg, nambdy_tide, IOSTAT = ios, ERR = 902 )
+            !
+            ! Need to support possibility of reading more than one
+            ! nambdy_tide from the namelist_cfg internal file.
+            ! Do this by finding the ib_bdy'th occurence of nambdy_tide in the
+            ! character buffer as the starting point.
+            !
+            nbdy_loc = INDEX( numnam_cfg( nbdy_rdstart: ), 'nambdy_tide' )
+            IF( nbdy_loc .GT. 0 ) THEN
+               nbdy_rdstart = nbdy_rdstart + nbdy_loc
+            ELSE
+               WRITE(cerrmsg,'(A,I4,A)') 'Error: entry number ',ib_bdy,' of nambdy_tide not found'
+               ios = -1
+               CALL ctl_nam ( ios , cerrmsg )
+            ENDIF
+            nbdy_rdstart = MAX( 1, nbdy_rdstart - 2 )
+            READ  ( numnam_cfg( nbdy_rdstart: ), nambdy_tide, IOSTAT = ios, ERR = 902)
 902         IF( ios >  0 )   CALL ctl_nam ( ios , 'nambdy_tide in configuration namelist' )
             IF(lwm) WRITE ( numond, nambdy_tide )
