From 51f114575ed39b88a187e1cadee15b6f08597a3e Mon Sep 17 00:00:00 2001
From: Edward Hartnett <38856240+edwardhartnett@users.noreply.github.com>
Date: Thu, 23 May 2024 15:34:29 +0200
Subject: [PATCH] fixed test (#698)

---
 src/g2getgb2.F90         |  25 +++++-
 tests/test_getgb2p_2.F90 | 187 +++++++++++++++++++++++----------------
 2 files changed, 135 insertions(+), 77 deletions(-)

diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90
index 5af377a9..133b19a0 100644
--- a/src/g2getgb2.F90
+++ b/src/g2getgb2.F90
@@ -1207,6 +1207,12 @@ end subroutine g2_gbytec81
         mypos = mypos + INT8_BITS
         mypos = mypos + 44 * INT1_BITS ! skip ahead in the cindex
      endif
+#ifdef LOGGING
+     write(g2_log_msg, *) 'iskip8', iskip8, 'iskip', iskip, 'mypos/8', mypos/8
+     call g2_log(2)
+#endif
+     
+     ! Determine length of local section (section 2).
      if (iskp2_8 .gt. 0) then
         call bareadl(lugb, iskip8 + iskp2_8, 4_8, lread8, ctemp)
         call g2_gbytec1(ctemp, len2, 0, INT4_BITS)      ! length of section 2
@@ -1216,6 +1222,12 @@ end subroutine g2_gbytec81
      else
         len2 = 0
      endif
+#ifdef LOGGING
+     write(g2_log_msg, *) 'iskip8 ', iskip8, ' iskp2_8 ', iskp2_8, 'len2', len2
+     call g2_log(2)
+#endif
+
+     ! Find the lengths of the sections 1, 3, 4, 5, and 6.
      call g2_gbytec1(cindex, len1, mypos, INT4_BITS)      ! length of section 1
      mypos = mypos + len1 * INT1_BITS ! skip ahead in the cindex
      call g2_gbytec1(cindex, len3, mypos, INT4_BITS)      ! length of section 3
@@ -1226,6 +1238,12 @@ end subroutine g2_gbytec81
      mypos = mypos + len5 * INT1_BITS ! skip ahead in the cindex
      call g2_gbytec1(cindex, len6, mypos, INT4_BITS)      ! length of section 6
      mypos = mypos + len6 * INT1_BITS ! skip ahead in the cindex
+#ifdef LOGGING
+     write(g2_log_msg, *) 'len1', len1, 'len3', len3, 'len4', len4, 'len5', len5, 'len6', len6
+     call g2_log(2)
+#endif
+
+     ! Handle the bitmap, if present.
      call g2_gbytec1(cindex, ibmap, mypos, INT1_BITS)      ! bitmap indicator
      if (ibmap .eq. 254) then
         ! Get the bytes to skip for section 6 from the index.
@@ -1235,7 +1253,7 @@ end subroutine g2_gbytec81
            call g2_gbytec1(cindex, iskp6, IXBMS2 * INT1_BITS, INT4_BITS)
         endif
 
-        ! Read the length of the bitmat section from the data file. (lu, byts to
+        ! Read the length of the bitmap section from the data file. (lu, byts to
         ! skip, bytes to read, bytes read, buffer for output)
         call bareadl(lugb, iskip8 + iskp6, 4_8, lread8, ctemp)
         call g2_gbytec1(ctemp, len6, 0, INT4_BITS)      ! length of section 6
@@ -1361,5 +1379,10 @@ end subroutine g2_gbytec81
         iret = 97
         return
      endif
+#ifdef LOGGING
+     write(g2_log_msg, *) ' read message into gribm, lread8', lread8
+     call g2_log(3)
+#endif
+     
   endif
 end subroutine getgb2rp2
diff --git a/tests/test_getgb2p_2.F90 b/tests/test_getgb2p_2.F90
index 90a1223f..546f26a0 100755
--- a/tests/test_getgb2p_2.F90
+++ b/tests/test_getgb2p_2.F90
@@ -3,119 +3,154 @@
 ! lengths of each message section by verifying that the last four octets are
 ! '7777'. 
 !
-! Alex Richert, May 2024
+! Alex Richert, Edward Hartnett, May, 2024
 PROGRAM test_getgb2p_2
+  use g2logging
   use grib_mod
   use pdstemplates
   use gridtemplates
-  integer,dimension(200) :: IDS,GDT,PDT
-  integer   ::    DSCPL,GDTN,PDTN
-  integer   ::    nrec
-  integer,parameter :: jrew=0
-  character * 1 :: a7,b7,c7,d7
+  implicit none
 
-  CHARACTER * 80  DESC,WMOHEAD
-  CHARACTER * 200  fileb,filei,fileo
-  character(len=1),pointer,dimension(:) :: gribm
+  integer, dimension(200) :: ids, gdt, pdt
+  integer :: dscpl, gdtn, pdtn
+  integer :: nrec
+  integer, parameter :: jrew = 0
+  character * 1 :: a7, b7, c7, d7
 
-  logical :: extract=.false.
+  character * 80 desc, wmohead
+  character * 200 fileb, filei, fileo
+  character(len = 1), pointer, dimension(:) :: gribm
+
+  logical :: extract = .false.
+  integer :: idxver = 2
+  integer (kind = 8) :: itot
+  integer :: ios, iret, iret1, j2, krew, lugb, lugi, lugo
 
   interface
-     SUBROUTINE GETGB2P(LUGB,LUGI,J,JDISC,JIDS,JPDTN,JPDT,JGDTN,JGDT, &
-          EXTRACT,K,GRIBM,LENG,IRET)
-       INTEGER,INTENT(IN) :: LUGB,LUGI,J,JDISC,JPDTN,JGDTN
-       INTEGER,DIMENSION(:) :: JIDS(*),JPDT(*),JGDT(*)
-       LOGICAL,INTENT(IN) :: EXTRACT
-       INTEGER,INTENT(OUT) :: K,IRET
-       CHARACTER(LEN=1),POINTER,DIMENSION(:) :: GRIBM
-     END SUBROUTINE GETGB2P
+     subroutine getgb2p2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt,  &
+          extract, idxver, k, gribm, leng8, iret)
+       integer, intent(in) :: lugb, lugi, j, jdisc
+       integer, dimension(:) :: jids(*)
+       integer, intent(in) :: jpdtn
+       integer, dimension(:) :: jpdt(*)
+       integer, intent(in) :: jgdtn
+       integer, dimension(:) :: jgdt(*)
+       logical, intent(in) :: extract
+       integer, intent(inout) :: idxver
+       integer, intent(out) :: k
+       character(len = 1), pointer, dimension(:) :: gribm
+       integer (kind = 8), intent(out) :: leng8
+       integer, intent(out) :: iret
+     end subroutine getgb2p2
   end interface
-  NAMELIST /GRIBIDS/DSCPL,IDS,GDTN,GDT,PDTN,PDT,DESC,WMOHEAD,EXTRACT
+  namelist /gribids/DSCPL, IDS, GDTN, GDT, PDTN, PDT, DESC, WMOHEAD, EXTRACT
 
-  lugb=11      ! Input GRIB2 File
-  lugi=0      ! Input GRIB2 INdex File
-  lugo=51      ! Output transmission file.
+  lugb = 11      ! Input GRIB2 File
+  lugi = 0      ! Input GRIB2 INdex File
+  lugo = 51      ! Output transmission file.
 
-  !        Read GRIB2 data and index file names from the FORT_nn
-  !        environment variables, and open the files.
-  fileb='data/rrfs.t12z.prslevfaa.f010.na3km.grib2'
-  filei=''
+  ! Read GRIB2 data and index file names from the FORT_nn
+  ! environment variables, and open the files.
+  fileb = 'data/rrfs.t12z.prslevfaa.f010.na3km.grib2'
+  filei = ''
+  
+  print *, 'Testing getgb2p2() on', fileb
 
-  call baopenr(lugb,fileb,iret1)
+  call baopenr(lugb, fileb, iret1)
   if (iret1  .ne. 0) then
-     write(6,fmt='(" Error opening GRIB file: ",A200)') fileb
-     write(6,fmt='(" baopenr error = ",I5)') iret1
+     print *, 'error opening data file, iret1', iret1
      stop 10
   endif
 
-  !        Read output GRIB bulletin file name from  FORTnn
-  !        environment variable, and open file.
-  fileo='test_tocgrib2.output.grib2'
-  call baopenw(lugo,fileo,iret1)
+  ! Read output GRIB bulletin file name from FORTnn
+  ! environment variable, and open file.
+  fileo = 'test_tocgrib2.output.grib2'
+  call baopenw(lugo, fileo, iret1)
   if (iret1  .ne. 0) then
-     write(6,fmt='(" Error opening output transmission file: ", A200)') fileo
-     write(6,fmt='(" baopenw error = ",I5)') iret1
+     print *, 'Error opening output transmission file, iret1', iret1
      stop 20
   endif
 
-  !        loop through input control records.
-  iret=0
+  ! Loop through input control records.
+  iret = 0
   nrec = 0
-  open(12, file='data/grib2.awips.rrfs.010')
+  open(12, file = 'data/grib2.awips.rrfs.010')
   foreachinputrecord: do
 
-     !  Set Namelist defaults
-     DSCPL=-1     ! Grib2 Discipline number
-     IDS=-9999    ! GRIB2 Identification Section
-     GDTN=-1      ! Grid Definition Template Number
-     GDT=-9999    ! Grid Definition Template
-     PDTN=-1      ! Product Definition Template Number
-     PDT=-9999    ! Product Definition Template
-     WMOHEAD='TTAAnn CCCC'
-     EXTRACT=.false.
+     ! Set Namelist defaults.
+     dscpl = -1     ! Grib2 Discipline number.
+     ids = -9999    ! GRIB2 Identification Section.
+     gdtn = -1      ! Grid Definition Template Number.
+     gdt = -9999    ! Grid Definition Template.
+     pdtn = -1      ! Product Definition Template Number.
+     pdt = -9999    ! Product Definition Template.
+     wmohead = 'TTAAnn CCCC'
+     extract = .false.
 
-     READ (12,GRIBIDS,iostat=ios)
+     read (12, gribids, iostat = ios)
      if (ios .ne. 0) then
-        write(6,fmt='(" Error reading PDS from input file. iostat = ",i5)') ios
+        if (nrec .eq. 337) then
+           print *, 'All input records processed!'
+           exit
+        endif
+        print *, 'Error reading PDS from input file. ios', ios, 'nrec', nrec
+        stop 500
         cycle
      endif
      nrec = nrec + 1
 
      !  Echo input record
-     write(6,'(A,I0)') ' Start new record no. =  ',nrec
-     write(6,'(73A)') ' DESC=',DESC(1:73)
-     write(6,'(11A)') ' WMOHEAD=',WMOHEAD(1:11)
-     write(6,'(A,I0)') ' GRIB2 DISCIPLINE= ',DSCPL
-     write(6,'(A,20(1x,I0))')' Section 1=', &
-          (IDS(j2),j2=1,13)
+     write(6, '(A, I0)') ' Start new record no. =  ', nrec
+     if (nrec .eq. 314) then
+        g2_log_level = 0
+     endif
+     write(6, '(73A)') ' DESC = ', DESC(1:73)
+     write(6, '(11A)') ' WMOHEAD = ', WMOHEAD(1:11)
+     write(6, '(A, I0)') ' GRIB2 DISCIPLINE =  ', DSCPL
+     write(6, '(A, 20(1x, I0))')' Section 1 = ', (IDS(j2), j2 = 1, 13)
      if (GDTN .ne. -1) then
-        write(6,'(A,I0,A,100(1x,I0))') ' GDT 3. ',GDTN,' =', &
-             (GDT(j2),j2=1,getgdtlen(GDTN))
+        write(6, '(A, I0, A, 100(1x, I0))') ' GDT 3. ', GDTN, '  = ', (GDT(j2), j2 = 1, getgdtlen(GDTN))
      endif
      if (PDTN .ne. -1) then
-        write(6,'(A,I0,A,100(1x,I0))') ' PDT 4. ',PDTN,' =', &
-             (PDT(j2),j2=1,getpdtlen(PDTN))
+        write(6, '(A, I0, A, 100(1x, I0))') ' PDT 4. ', PDTN, '  = ', (PDT(j2), j2 = 1, getpdtlen(PDTN))
      endif
 
-     !        Read and return packed GRIB field
-     CALL GETGB2P(lugb,lugi,jrew,DSCPL,IDS,PDTN,PDT, &
-          GDTN,GDT,extract,KREW,gribm,itot,iret)
-     IF (IRET.NE.0) THEN
-        IF (IRET.EQ.96)WRITE(6,'(A)')' GETGB2P: ERROR READING INDEX' &
-             //' FILE'
-        IF (IRET.EQ.97)WRITE(6,'(A)')' GETGB2P: ERROR READING GRIB' &
-             //' FILE'
-        IF (IRET.EQ.99)WRITE(6,'(A)')' GETGB2P: ERROR REQUEST NOT' &
-             //' FOUND'
+     ! Read and return packed GRIB field.
+     print *, '*** calling getgb2p2()'
+     call getgb2p2(lugb, lugi, jrew, dscpl, ids, pdtn, pdt, gdtn, gdt, &
+          extract, idxver, krew, gribm, itot, iret)
+     print *, '*** iret', iret
+     if (iret .ne. 0) then
+        if (iret .eq. 96) then
+           print *, ' test_getgb2p_2: error reading index file'
+           stop 505
+        endif
+        if (iret .eq. 97) then
+           print *, ' test_getgb2p_2: error reading grib file'
+           stop 510
+        endif
+        if (iret .eq. 99) then
+           print *, ' test_getgb2p_2: error request not found, nrec', nrec
+           write(6, '(A, I0, A, 100(1x, I0))') ' PDT 4. ', PDTN, '  = ', (PDT(j2), j2 = 1, getpdtlen(PDTN))
+           ! We expect this one won't be found.
+           if (nrec .ne. 314) then
+              stop 515
+           endif
+        endif
         cycle
-     END IF
-     a7=gribm(size(gribm)-3)
-     b7=gribm(size(gribm)-2)
-     c7=gribm(size(gribm)-1)
-     d7=gribm(size(gribm))
-     if(.not.all((/a7,b7,c7,d7/).eq.'7')) stop 77
+     end if
+     a7 = gribm(size(gribm) - 3)
+     b7 = gribm(size(gribm) - 2)
+     c7 = gribm(size(gribm) - 1)
+     d7 = gribm(size(gribm))
+     if (.not. all((/a7, b7, c7, d7/) .eq. '7')) then
+        print *, 'error with section 8'
+        stop 77
+     endif
      deallocate(gribm)
-    return
   enddo foreachinputrecord
 
+  print *, 'OK!'
+  print *,  'SUCCESS!'
+
 END PROGRAM test_getgb2p_2