From 0de571884ebcd35edf3eaeac56e447420557b5cb Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 02:19:23 -0600 Subject: [PATCH 01/22] adding logging capability --- src/CMakeLists.txt | 2 +- src/g2getgb2.F90 | 6 ++++++ src/g2index.F90 | 4 ++++ src/g2logging.F90 | 21 +++++++++++++++++++++ tests/test_getgb2rp.F90 | 3 +++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/g2logging.F90 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ddb9f0de..0b34a4b0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,7 +8,7 @@ set(fortran_src compack.F90 drstemplates.F90 g2bytes.F90 g2grids.F90 g2get.F90 g2getgb2.F90 g2index.F90 g2gf.F90 g2unpack.F90 g2create.F90 ${CMAKE_CURRENT_BINARY_DIR}/gribmod.F90 gridtemplates.F90 intmath.F90 g2jpc.F90 pack_gp.f params_ecmwf.F90 params.F90 pdstemplates.F90 -g2png.F90 realloc.F90 reduce.f g2sim.F90 skgb.F90 g2spec.F90) +g2png.F90 realloc.F90 reduce.f g2sim.F90 skgb.F90 g2spec.F90 g2logging.F90) # This function calls NCEPLIBS-w3emc. if (BUILD_WITH_W3EMC) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index b1fbf05e..04c876b8 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -995,6 +995,7 @@ end subroutine getgb2rp !> !> @author Edward Hartnett, Stephen Gilbert @date Feb 13, 2024 subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) + use g2logging implicit none integer, intent(in) :: lugb, idxver @@ -1015,6 +1016,11 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) integer :: mypos, inc = 0 integer (kind = 8) :: lread8, iskip8, leng8, len2_8, len7_8, len6_8 +#ifdef LOGGING + write(g2_log_msg, '(a)') 'howdy' + call g2_log(1, g2_log_msg) +#endif + iret = 0 ! Extract grib message from file. diff --git a/src/g2index.F90 b/src/g2index.F90 index 3234a90f..7869dce7 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -898,6 +898,7 @@ end subroutine getgb2s subroutine getgb2s2(cbuf, idxver, nlen, nnum, j, jdisc, jids, jpdtn, jpdt, jgdtn, & jgdt, k, gfld, lpos, iret) use grib_mod + use g2logging implicit none character(len = 1), intent(in) :: cbuf(nlen) @@ -953,6 +954,9 @@ subroutine gf_unpack5(cgrib, lcgrib, iofst, ndpts, idrsnum, & end subroutine gf_unpack5 end interface + ! Log results for debugging. + call g2_log(1, "*** logging getgb2s2") + ! Initialize. k = 0 lpos = 0 diff --git a/src/g2logging.F90 b/src/g2logging.F90 new file mode 100644 index 00000000..da647a8b --- /dev/null +++ b/src/g2logging.F90 @@ -0,0 +1,21 @@ +!> @file +!> @brief Logging for the g2 library. +!> @author Edward Hartnett @date 5/15/24 + +!> @brief Logging for the g2 library. +!> +!> @author Edward Hartnett @date 5/15/24 +module g2logging + integer g2_log_level !< 0 for no logging. + character* 120 g2_log_msg !< For messages. + +contains + subroutine g2_log(level, msg) + integer, intent(in) :: level + character(*), intent(in) :: msg + + if (level .le. g2_log_level) then + print *, msg + endif + end subroutine g2_log +end module g2logging diff --git a/tests/test_getgb2rp.F90 b/tests/test_getgb2rp.F90 index 6bccc005..322c36b1 100644 --- a/tests/test_getgb2rp.F90 +++ b/tests/test_getgb2rp.F90 @@ -5,6 +5,7 @@ ! Ed Hartnett 7/26/22 program test_getgb2rp use bacio_module + use g2logging implicit none integer :: lugi @@ -50,7 +51,9 @@ end subroutine getgb2rp ! Extract the whole message. extract = .false. nullify(gribm) + g2_log_level = 3 call getgb2rp(lugb, cbuf, extract, gribm, leng, iret) + g2_log_level = 0 print *, 'leng ', leng if (leng .ne. 11183) stop 110 ! Deallocate buffer that got GRIB message. From e88fe4b3f8e0ec2c8b55efc9d753dd4e8165f60a Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 02:50:40 -0600 Subject: [PATCH 02/22] adding logging --- src/g2getgb2.F90 | 5 +++-- src/g2index.F90 | 6 +++++- src/g2logging.F90 | 5 ++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index 04c876b8..92e25344 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -1017,8 +1017,9 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) integer (kind = 8) :: lread8, iskip8, leng8, len2_8, len7_8, len6_8 #ifdef LOGGING - write(g2_log_msg, '(a)') 'howdy' - call g2_log(1, g2_log_msg) + write(g2_log_msg, '(a, i2, a, i1, a, l)') 'getgb2rp2: lugb ', lugb, ' idxver ', idxver, & + ' extract ', extract + call g2_log(1) #endif iret = 0 diff --git a/src/g2index.F90 b/src/g2index.F90 index 7869dce7..cf9be022 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -955,7 +955,11 @@ end subroutine gf_unpack5 end interface ! Log results for debugging. - call g2_log(1, "*** logging getgb2s2") +#ifdef LOGGING + write(g2_log_msg, '(a, i1, a, i5, a, i7, a, i3, a, i3)') 'getgb2s2: idxver ', idxver, ' nlen ', nlen, & + ' nnum ', nnum, ' j ', j, ' jdisc ', jdisc + call g2_log(1) +#endif ! Initialize. k = 0 diff --git a/src/g2logging.F90 b/src/g2logging.F90 index da647a8b..88841c65 100644 --- a/src/g2logging.F90 +++ b/src/g2logging.F90 @@ -10,12 +10,11 @@ module g2logging character* 120 g2_log_msg !< For messages. contains - subroutine g2_log(level, msg) + subroutine g2_log(level) integer, intent(in) :: level - character(*), intent(in) :: msg if (level .le. g2_log_level) then - print *, msg + print *, g2_log_msg endif end subroutine g2_log end module g2logging From f47e3f9937a128c8ef3f1ab1d6cb1b3bd8baa1b9 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 02:55:07 -0600 Subject: [PATCH 03/22] more logging --- src/g2index.F90 | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/g2index.F90 b/src/g2index.F90 index cf9be022..58d3618b 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -263,6 +263,7 @@ end subroutine getidx !> !> @author Stephen Gilbert, Ed Hartnett @date Feb 9, 2024 subroutine getidx2(lugb, lugi, idxver, cindex, nlen, nnum, iret) + use g2logging implicit none integer, intent(in) :: lugb, lugi @@ -293,6 +294,8 @@ subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret) character(len=1), pointer, dimension(:) :: cbuf integer, intent(out) :: idxver, nlen, nnum, iret end subroutine getg2i2 + end interface + interface subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, & nlen, nnum, nmess, iret) integer, intent(in) :: lugb @@ -303,6 +306,13 @@ subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, & end subroutine getg2i2r end interface +#ifdef LOGGING + ! Log results for debugging. + write(g2_log_msg, '(a, i2, a, i2, a, i1)') 'getidx2: lugb ', lugb, ' lugi ', lugi, & + ' idxver ', idxver + call g2_log(1) +#endif + ! Free all associated memory and exit. if (lugb .eq. 0) then !print *, 'getidx: Freeing all memory' @@ -954,8 +964,8 @@ subroutine gf_unpack5(cgrib, lcgrib, iofst, ndpts, idrsnum, & end subroutine gf_unpack5 end interface - ! Log results for debugging. #ifdef LOGGING + ! Log results for debugging. write(g2_log_msg, '(a, i1, a, i5, a, i7, a, i3, a, i3)') 'getgb2s2: idxver ', idxver, ' nlen ', nlen, & ' nnum ', nnum, ' j ', j, ' jdisc ', jdisc call g2_log(1) @@ -1240,7 +1250,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) integer (kind = 8) :: ibread8, lbread8, ibskip8, lengds8 integer (kind = 8) :: ilnpds8, ilndrs8 integer :: lensec, lensec1 - integer :: mypos, inc, i + integer :: mypos, inc ! Parameters. ! Size of the internal char buffers used in this subroutine. From caa7f87a6dbb159057136d5839eea730bb19f4e7 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 02:59:11 -0600 Subject: [PATCH 04/22] more logging --- src/g2logging.F90 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/g2logging.F90 b/src/g2logging.F90 index 88841c65..a963220d 100644 --- a/src/g2logging.F90 +++ b/src/g2logging.F90 @@ -7,9 +7,15 @@ !> @author Edward Hartnett @date 5/15/24 module g2logging integer g2_log_level !< 0 for no logging. - character* 120 g2_log_msg !< For messages. + character* 120 g2_log_msg !< Fill this with the logging message. contains + !> Print a debug message for the g2 library. + !> + !> @param[in] level If this is lower or equal to the current setting + !> of g2_log_level, print this log message. + !> + !> @author Edward Hartnett @date 5/15/24 subroutine g2_log(level) integer, intent(in) :: level From 71ef6399d0cb90905f67e7cb66c0d32dc35caada Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 03:06:34 -0600 Subject: [PATCH 05/22] more logging --- src/g2index.F90 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/g2index.F90 b/src/g2index.F90 index 58d3618b..061e172c 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -511,6 +511,7 @@ end subroutine getg2i !> !> @author Ed Hartnett, Mark Iredell @date Feb 9, 2024 subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret) + use g2logging implicit none integer, intent(in) :: lugi @@ -520,6 +521,12 @@ subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret) character chead*162 integer :: ios, istat, lbuf, lhead, nskp +#ifdef LOGGING + ! Log results for debugging. + write(g2_log_msg, '(a, i2, a, i1)') 'getg2i2: lugi ', lugi, ' idxver ', idxver + call g2_log(1) +#endif + nullify(cbuf) nlen = 0 nnum = 0 @@ -634,6 +641,7 @@ end subroutine getg2ir !> !> @author Mark Iredell, Ed Hartnett @date 1995-10-31 subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, iret) + use g2logging use re_alloc ! needed for subroutine realloc implicit none @@ -660,6 +668,13 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) end subroutine ix2gb2 end interface +#ifdef LOGGING + ! Log results for debugging. + write(g2_log_msg, '(a, i2, a, i7, a, i7, a, i5, a, i1)') 'getg2i2r: lugb ', lugb, ' msk1 ', msk1, ' msk2 ', msk2, & + ' mnum ', mnum, ' idxver ', idxver + call g2_log(1) +#endif + ! Initialize. iret = 0 nullify(cbuf) @@ -1230,6 +1245,7 @@ end subroutine ixgb2 !> @author Ed Hartnett, Mark Iredell @date Feb 5, 2024 subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) use re_alloc ! needed for subroutine realloc + use g2logging implicit none ! Subroutine parameters. @@ -1282,6 +1298,13 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) character cbread(LINMAX), cindex(LINMAX) character cids(LINMAX), cgds(LINMAX) +#ifdef LOGGING + ! Log results for debugging. + write(g2_log_msg, '(a, i2, a, i7, a, i1)') 'ix2gb2: lugb ', lugb, ' lskip8 ', lskip8, & + ' idxver ', idxver + call g2_log(1) +#endif + ! Are we using index version 1 (legacy), or version 2 (introduced to ! handle files > 2 GB). if (idxver .eq. 1) then From 7445c4692e1bdb927778d4fea7eb2270a38b7d63 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 03:12:07 -0600 Subject: [PATCH 06/22] more logging --- src/g2getgb2.F90 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index 92e25344..f39fe35c 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -190,6 +190,7 @@ end subroutine getgb2 !> @author Ed Hartnett, Mark Iredell @date 2024-03-19 subroutine getgb2i2(lugb, lugi, j, jdisc, jids, jpdtn, jpdt, jgdtn, jgdt, & unpack, idxver, k, gfld, iret) + use g2logging use grib_mod implicit none @@ -245,6 +246,13 @@ subroutine getgb2r2(lugb, idxver, cindex, gfld, iret) end subroutine getgb2r2 end interface +#ifdef LOGGING + write(g2_log_msg, '(a, i2, a, i2, a, i5, a, i5, a, l, a, i1)') 'getgb2i2: lugb ', lugb, ' lugi ', lugi, & + ' j ', j, ' jdisc ', jdisc, ' unpack ', unpack, ' idxver ', idxver, & + ' unpack ', unpack + call g2_log(1) +#endif + ! Fill cbuf with the index records of this file, by recalling them ! from memory, reading them from the index file, or generating them ! from the data file. @@ -348,6 +356,7 @@ end subroutine getgb2l !> !> @author Stephen Gilbert @date 2002-05-07 subroutine getgb2l2(lugb, idxver, cindex, gfld, iret) + use g2logging use grib_mod implicit none @@ -376,6 +385,11 @@ subroutine gf_unpack2(cgrib, lcgrib, iofst, lencsec2, csec2, ierr) end subroutine gf_unpack2 end interface +#ifdef LOGGING + write(g2_log_msg, '(a, i2, a, i1)') 'getgb2l2: lugb ', lugb, ' idxver ', idxver + call g2_log(1) +#endif + ! Get info. nullify(gfld%local) iret = 0 @@ -775,6 +789,7 @@ end subroutine getgb2r !> !> @author Ed Hartnett, Stephen Gilbert @date Feb 14, 2024 subroutine getgb2r2(lugb, idxver, cindex, gfld, iret) + use g2logging use grib_mod implicit none @@ -813,6 +828,11 @@ subroutine gf_unpack7(cgrib, lcgrib, iofst, igdsnum, igdstmpl, & end subroutine gf_unpack7 end interface +#ifdef LOGGING + write(g2_log_msg, '(a, i2, a, i1)') 'getgb2r2: lugb ', lugb, ' idxver ', idxver + call g2_log(1) +#endif + ! Get info. nullify(gfld%bmap, gfld%fld) iret = 0 From 729968482ea354f4b2c6245ae18eec25ff4fcbfb Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 03:17:45 -0600 Subject: [PATCH 07/22] more logging --- src/g2getgb2.F90 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index f39fe35c..8cbd15fe 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -248,8 +248,7 @@ end subroutine getgb2r2 #ifdef LOGGING write(g2_log_msg, '(a, i2, a, i2, a, i5, a, i5, a, l, a, i1)') 'getgb2i2: lugb ', lugb, ' lugi ', lugi, & - ' j ', j, ' jdisc ', jdisc, ' unpack ', unpack, ' idxver ', idxver, & - ' unpack ', unpack + ' j ', j, ' jdisc ', jdisc, ' unpack ', unpack, ' idxver ', idxver call g2_log(1) #endif From 4d61171420c2b9211829f11abfe070914f9ac953 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 06:55:39 -0600 Subject: [PATCH 08/22] new test --- tests/CMakeLists.txt | 1 + tests/test_getgb2rp2.F90 | 88 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/test_getgb2rp2.F90 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8de931d3..08a1bdc2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -116,6 +116,7 @@ if(FTP_TEST_FILES) create_test(test_getg2i2r ${kind}) create_test(test_getidx ${kind}) create_test(test_getgb2rp ${kind}) + create_test(test_getgb2rp2 ${kind}) create_test(test_getgb2rp_2 ${kind}) create_test(test_getgb2s ${kind}) create_test(test_getgb2p ${kind}) diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 new file mode 100644 index 00000000..8d1beb2f --- /dev/null +++ b/tests/test_getgb2rp2.F90 @@ -0,0 +1,88 @@ +! This is a test program for NCEPLIBS-g2. +! +! This program tests getg2rp2.F90 +! +! Ed Hartnett 5/14/24 +program test_getgb2rp2 + use bacio_module + implicit none + + integer :: lugi + character(len=1), pointer, dimension(:) :: cbuf(:) + integer :: lugb = 3 + integer :: nlen, nnum, iret + logical :: extract + integer (kind = 8) :: leng8 + character(len=1), pointer, dimension(:) :: gribm + integer :: idxver + + ! Interfaces are needed due to pointers in the parameter lists. + interface + subroutine getidx2(lugb, lugi, idxver, cindex, nlen, nnum, iret) + integer, intent(in) :: lugb, lugi + integer, intent(inout) :: idxver + character(len = 1), pointer, dimension(:) :: cindex + integer, intent(out) :: nlen, nnum, iret + end subroutine getidx2 + subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng8, iret) + integer, intent(in) :: lugb, idxver + character(len = 1), intent(in) :: cindex(*) + logical, intent(in) :: extract + character(len = 1), pointer, dimension(:) :: gribm + integer(kind = 8), intent(out) :: leng8 + integer, intent(out) :: iret + end subroutine getgb2rp2 + end interface + + print *, 'Testing the getgb2rp() subroutine - expect and ignore error messages during test...' + + ! Open a real GRIB2 file. + print *, 'Indexing a real GRIB2 file WW3_Regional_US_West_Coast_20220718_0000.grib2...' + call baopenr(lugb, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) + if (iret .ne. 0) stop 100 + + idxver = 1 + lugi = 0 + !lugi = lugb ! Force regeneration of index from GRIB2 file. + call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, iret) + if (iret .ne. 0) stop 101 + if (nnum .ne. 688) stop 102 + if (idxver .eq. 1) then + if (nlen .ne. 137600) then + print *, nlen + stop 103 + endif + else + if (nlen .ne. 145856) then + print *, nlen + stop 103 + endif + endif + print *, 'nlen, nnum: ', nlen, nnum + + ! Extract the whole message. + extract = .false. + ! nullify(gribm) + call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) + print *, 'leng8 ', leng8 + if (leng8 .ne. 11183) stop 110 + ! Deallocate buffer that got GRIB message. + deallocate(gribm) + + ! Extract just the field (same result). + extract = .true. + call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) + print *, 'leng8 ', leng8 + if (leng8 .ne. 11183) stop 110 + ! Deallocate buffer that got GRIB message. + deallocate(gribm) + + ! Deallocate the buffer that holds index. + deallocate(cbuf) + + call baclose(lugb, iret) + if (iret .ne. 0) stop 199 + + print *, 'SUCCESS!...' + +end program test_getgb2rp2 From 5307c3e45586be7512cd493486d51bf01f210e74 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 09:19:20 -0600 Subject: [PATCH 09/22] improved logging --- src/g2getgb2.F90 | 7 +++++++ src/g2index.F90 | 2 +- tests/test_getgb2rp2.F90 | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index 8cbd15fe..7d1221dc 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -1180,6 +1180,13 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) endif mypos = mypos + 7 * INT4_BITS call g2_gbytec(cindex, leng, mypos, INT4_BITS) ! length of grib message +#ifdef LOGGING + write(g2_log_msg, *) ' iskip8 ', iskip8, ' mypos/8 ', mypos/8, & + ' leng ', leng + call g2_log(2) +#endif + + if (.not. associated(gribm)) allocate(gribm(leng)) leng8 = leng call bareadl(lugb, iskip8, leng8, lread8, gribm) diff --git a/src/g2index.F90 b/src/g2index.F90 index 061e172c..8655f9ab 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -1300,7 +1300,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) #ifdef LOGGING ! Log results for debugging. - write(g2_log_msg, '(a, i2, a, i7, a, i1)') 'ix2gb2: lugb ', lugb, ' lskip8 ', lskip8, & + write(g2_log_msg, *) 'ix2gb2: lugb ', lugb, ' lskip8 ', lskip8, & ' idxver ', idxver call g2_log(1) #endif diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 index 8d1beb2f..9baa95b9 100644 --- a/tests/test_getgb2rp2.F90 +++ b/tests/test_getgb2rp2.F90 @@ -4,6 +4,7 @@ ! ! Ed Hartnett 5/14/24 program test_getgb2rp2 + use g2logging use bacio_module implicit none @@ -43,6 +44,8 @@ end subroutine getgb2rp2 idxver = 1 lugi = 0 + g2_log_level = 3 + !lugi = lugb ! Force regeneration of index from GRIB2 file. call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, iret) if (iret .ne. 0) stop 101 From 325fdcb947121943cb180b5ac46d164713792f28 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 22:16:31 -0600 Subject: [PATCH 10/22] changed data cache key --- .github/workflows/developer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/developer.yml b/.github/workflows/developer.yml index 66d264c0..bf082314 100644 --- a/.github/workflows/developer.yml +++ b/.github/workflows/developer.yml @@ -103,7 +103,7 @@ jobs: uses: actions/cache@v4 with: path: ~/data - key: data-developer-3 + key: data-developer-4 - name: asan if: matrix.config == 'asan/code coverage' From 7642747435409e354cb1dbc4408ac790a55e81f3 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 22:34:36 -0600 Subject: [PATCH 11/22] fixed test --- src/g2getgb2.F90 | 10 ++++++---- tests/test_getgb2rp2.F90 | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index 7d1221dc..e71ffcd9 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -1055,6 +1055,7 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) call g2_gbytec(cindex, iskp2, mypos, INT4_BITS) ! bytes to skip for section 2 mypos = mypos + INT4_BITS iskp2_8 = iskp2 + mypos = mypos + 32 * INT1_BITS ! skip ahead in the cindex else inc = 12 call g2_gbytec8(cindex, iskip8, mypos, INT8_BITS) ! bytes to skip in file @@ -1062,6 +1063,7 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) iskip = int(iskip8, kind(4)) call g2_gbytec8(cindex, iskp2_8, mypos, INT8_BITS) ! bytes to skip for section 2 mypos = mypos + INT8_BITS + mypos = mypos + 36 * INT1_BITS ! skip ahead in the cindex endif if (iskp2_8 .gt. 0) then call bareadl(lugb, iskip8 + iskp2_8, 4_8, lread8, ctemp) @@ -1072,7 +1074,6 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) else len2 = 0 endif - mypos = mypos + 32 * INT1_BITS ! skip ahead in the cindex call g2_gbytec(cindex, len1, mypos, INT4_BITS) ! length of section 1 ipos = 44 + len1 mypos = mypos + len1 * INT1_BITS ! skip ahead in the cindex @@ -1173,20 +1174,21 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) if (idxver .eq. 1) then call g2_gbytec(cindex, iskip, mypos, INT4_BITS) ! bytes to skip in file mypos = mypos + INT4_BITS + mypos = mypos + 7 * INT4_BITS iskip8 = iskip else call g2_gbytec8(cindex, iskip8, mypos, INT8_BITS) ! bytes to skip in file mypos = mypos + INT8_BITS + mypos = mypos + 2 * INT8_BITS + 4 * INT4_BITS endif - mypos = mypos + 7 * INT4_BITS + call g2_gbytec(cindex, leng, mypos, INT4_BITS) ! length of grib message #ifdef LOGGING write(g2_log_msg, *) ' iskip8 ', iskip8, ' mypos/8 ', mypos/8, & ' leng ', leng call g2_log(2) #endif - - + if (.not. associated(gribm)) allocate(gribm(leng)) leng8 = leng call bareadl(lugb, iskip8, leng8, lread8, gribm) diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 index 9baa95b9..90ea2812 100644 --- a/tests/test_getgb2rp2.F90 +++ b/tests/test_getgb2rp2.F90 @@ -65,7 +65,7 @@ end subroutine getgb2rp2 ! Extract the whole message. extract = .false. - ! nullify(gribm) + nullify(gribm) call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) print *, 'leng8 ', leng8 if (leng8 .ne. 11183) stop 110 @@ -74,6 +74,7 @@ end subroutine getgb2rp2 ! Extract just the field (same result). extract = .true. + nullify(gribm) call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) print *, 'leng8 ', leng8 if (leng8 .ne. 11183) stop 110 From ea9342ced32997d586ade89f1c0c1a3a9208cf58 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 22:35:41 -0600 Subject: [PATCH 12/22] fixed test --- tests/test_getgb2rp2.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 index 90ea2812..d80b4944 100644 --- a/tests/test_getgb2rp2.F90 +++ b/tests/test_getgb2rp2.F90 @@ -72,7 +72,7 @@ end subroutine getgb2rp2 ! Deallocate buffer that got GRIB message. deallocate(gribm) - ! Extract just the field (same result). + ! Extract just the field (same result). extract = .true. nullify(gribm) call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) From 53a1cea167005b18c5bbcc3bcba5c832a7d67b32 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 22:52:23 -0600 Subject: [PATCH 13/22] now test both idxver --- src/g2getgb2.F90 | 5 ++- tests/test_getgb2rp2.F90 | 88 ++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index e71ffcd9..314887b3 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -1174,7 +1174,7 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) if (idxver .eq. 1) then call g2_gbytec(cindex, iskip, mypos, INT4_BITS) ! bytes to skip in file mypos = mypos + INT4_BITS - mypos = mypos + 7 * INT4_BITS + mypos = mypos + 6 * INT4_BITS iskip8 = iskip else call g2_gbytec8(cindex, iskip8, mypos, INT8_BITS) ! bytes to skip in file @@ -1182,7 +1182,8 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) mypos = mypos + 2 * INT8_BITS + 4 * INT4_BITS endif - call g2_gbytec(cindex, leng, mypos, INT4_BITS) ! length of grib message + call g2_gbytec8(cindex, leng8, mypos, INT8_BITS) ! length of grib message + leng = int(leng8, kind(4)) #ifdef LOGGING write(g2_log_msg, *) ' iskip8 ', iskip8, ' mypos/8 ', mypos/8, & ' leng ', leng diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 index d80b4944..4c0ab8db 100644 --- a/tests/test_getgb2rp2.F90 +++ b/tests/test_getgb2rp2.F90 @@ -15,7 +15,7 @@ program test_getgb2rp2 logical :: extract integer (kind = 8) :: leng8 character(len=1), pointer, dimension(:) :: gribm - integer :: idxver + integer :: idxver, i ! Interfaces are needed due to pointers in the parameter lists. interface @@ -37,56 +37,58 @@ end subroutine getgb2rp2 print *, 'Testing the getgb2rp() subroutine - expect and ignore error messages during test...' - ! Open a real GRIB2 file. - print *, 'Indexing a real GRIB2 file WW3_Regional_US_West_Coast_20220718_0000.grib2...' - call baopenr(lugb, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) - if (iret .ne. 0) stop 100 + do i = 1, 2 + ! Open a real GRIB2 file. + print *, 'Indexing a real GRIB2 file WW3_Regional_US_West_Coast_20220718_0000.grib2...' + call baopenr(lugb, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) + if (iret .ne. 0) stop 100 - idxver = 1 - lugi = 0 - g2_log_level = 3 - - !lugi = lugb ! Force regeneration of index from GRIB2 file. - call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, iret) - if (iret .ne. 0) stop 101 - if (nnum .ne. 688) stop 102 - if (idxver .eq. 1) then - if (nlen .ne. 137600) then - print *, nlen - stop 103 - endif - else - if (nlen .ne. 145856) then - print *, nlen - stop 103 + idxver = i + lugi = 0 + g2_log_level = 3 + + !lugi = lugb ! Force regeneration of index from GRIB2 file. + call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, iret) + if (iret .ne. 0) stop 101 + if (nnum .ne. 688) stop 102 + if (idxver .eq. 1) then + if (nlen .ne. 137600) then + print *, nlen + stop 103 + endif + else + if (nlen .ne. 145856) then + print *, nlen + stop 104 + endif endif - endif - print *, 'nlen, nnum: ', nlen, nnum + print *, 'nlen, nnum: ', nlen, nnum - ! Extract the whole message. - extract = .false. - nullify(gribm) - call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) - print *, 'leng8 ', leng8 - if (leng8 .ne. 11183) stop 110 - ! Deallocate buffer that got GRIB message. - deallocate(gribm) + ! Extract the whole message. + extract = .false. + nullify(gribm) + call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) + print *, 'leng8 ', leng8 + if (leng8 .ne. 11183) stop 110 + ! Deallocate buffer that got GRIB message. + deallocate(gribm) - ! Extract just the field (same result). - extract = .true. - nullify(gribm) - call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) - print *, 'leng8 ', leng8 - if (leng8 .ne. 11183) stop 110 - ! Deallocate buffer that got GRIB message. - deallocate(gribm) + ! Extract just the field (same result). + extract = .true. + nullify(gribm) + call getgb2rp2(lugb, idxver, cbuf, extract, gribm, leng8, iret) + print *, 'leng8 ', leng8 + if (leng8 .ne. 11183) stop 112 + ! Deallocate buffer that got GRIB message. + deallocate(gribm) + + call baclose(lugb, iret) + if (iret .ne. 0) stop 199 + end do ! Deallocate the buffer that holds index. deallocate(cbuf) - call baclose(lugb, iret) - if (iret .ne. 0) stop 199 - print *, 'SUCCESS!...' end program test_getgb2rp2 From ab2a327e0e0ea0f9a0907a457bb661d78814d8dc Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Wed, 15 May 2024 22:56:49 -0600 Subject: [PATCH 14/22] memory issues --- tests/test_getgb2rp2.F90 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 index 4c0ab8db..c267f3a3 100644 --- a/tests/test_getgb2rp2.F90 +++ b/tests/test_getgb2rp2.F90 @@ -45,7 +45,7 @@ end subroutine getgb2rp2 idxver = i lugi = 0 - g2_log_level = 3 + g2_log_level = 1 !lugi = lugb ! Force regeneration of index from GRIB2 file. call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, iret) @@ -89,6 +89,9 @@ end subroutine getgb2rp2 ! Deallocate the buffer that holds index. deallocate(cbuf) + ! Free library memory. + ! call gf_finalize() + print *, 'SUCCESS!...' end program test_getgb2rp2 From ff2c49484f7f65c486ac9bfedf7bf5169098d3db Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 00:05:48 -0600 Subject: [PATCH 15/22] initialize variable for memory test --- tests/test_getgb2rp2.F90 | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_getgb2rp2.F90 b/tests/test_getgb2rp2.F90 index c267f3a3..5a44eaf9 100644 --- a/tests/test_getgb2rp2.F90 +++ b/tests/test_getgb2rp2.F90 @@ -45,6 +45,7 @@ end subroutine getgb2rp2 idxver = i lugi = 0 + leng8 = 0 g2_log_level = 1 !lugi = lugb ! Force regeneration of index from GRIB2 file. From 5f9c3226d23bee8f041b6bc8e9ebe363f5fcff21 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 00:09:50 -0600 Subject: [PATCH 16/22] fixing some logging statements --- src/g2index.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/g2index.F90 b/src/g2index.F90 index 8655f9ab..76caa010 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -523,7 +523,7 @@ subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret) #ifdef LOGGING ! Log results for debugging. - write(g2_log_msg, '(a, i2, a, i1)') 'getg2i2: lugi ', lugi, ' idxver ', idxver + write(g2_log_msg, *) 'getg2i2: lugi ', lugi call g2_log(1) #endif From d682572638c74248845ff07a2c2ccffeb3b3ae79 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 00:41:31 -0600 Subject: [PATCH 17/22] changed argument of getgb2rp2() to 8-byte int --- src/g2getgb2.F90 | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index 314887b3..4e005082 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -967,18 +967,23 @@ subroutine getgb2rp(lugb, cindex, extract, gribm, leng, iret) logical, intent(in) :: extract character(len = 1), pointer, dimension(:) :: gribm integer, intent(out) :: leng, iret + integer (kind = 8) :: leng8 interface - subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) + subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng8, iret) integer, intent(in) :: lugb, idxver character(len = 1), intent(in) :: cindex(*) logical, intent(in) :: extract character(len = 1), pointer, dimension(:) :: gribm - integer, intent(out) :: leng, iret + integer (kind = 8), intent(out) :: leng8 + integer, intent(out) :: iret end subroutine getgb2rp2 end interface - call getgb2rp2(lugb, 1, cindex, extract, gribm, leng, iret) + ! Call the legacy version of this function. It will only work with + ! GRIB messages < 2 GB. + call getgb2rp2(lugb, 1, cindex, extract, gribm, leng8, iret) + leng = int(leng8, kind(4)) end subroutine getgb2rp @@ -1013,14 +1018,15 @@ end subroutine getgb2rp !> - 97 Error reading grib file. !> !> @author Edward Hartnett, Stephen Gilbert @date Feb 13, 2024 -subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) +subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng8, iret) use g2logging implicit none integer, intent(in) :: lugb, idxver character(len = 1), intent(in) :: cindex(*) logical, intent(in) :: extract - integer, intent(out) :: leng, iret + integer (kind = 8), intent(out) :: leng8 + integer, intent(out) :: iret character(len = 1), pointer, dimension(:) :: gribm integer, parameter :: zero = 0 @@ -1033,7 +1039,7 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64) integer :: mypos, inc = 0 - integer (kind = 8) :: lread8, iskip8, leng8, len2_8, len7_8, len6_8 + integer (kind = 8) :: lread8, iskip8, len2_8, len7_8, len6_8 #ifdef LOGGING write(g2_log_msg, '(a, i2, a, i1, a, l)') 'getgb2rp2: lugb ', lugb, ' idxver ', idxver, & @@ -1107,8 +1113,8 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) len7_8 = len7 call bareadl(lugb, iskip8 + iskp7, len7_8, lread8, csec7) - leng = len0 + len1 + len2 + len3 + len4 + len5 + len6 + len7 + len8 - if (.not. associated(gribm)) allocate(gribm(leng)) + leng8 = len0 + len1 + len2 + len3 + len4 + len5 + len6 + len7 + len8 + if (.not. associated(gribm)) allocate(gribm(leng8)) ! Create Section 0 gribm(1) = 'G' @@ -1117,13 +1123,9 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) gribm(4) = 'B' gribm(5) = char(0) gribm(6) = char(0) - gribm(7) = cindex(42 + inc) - gribm(8) = cindex(41 + inc) - gribm(9) = char(0) - gribm(10) = char(0) - gribm(11) = char(0) - gribm(12) = char(0) - call g2_sbytec(gribm, leng, 12*8, INT4_BITS) + gribm(7) = cindex(42 + inc) ! discipline + gribm(8) = cindex(41 + inc) ! GRIB version + call g2_sbytec8(gribm, leng8, 8 * INT1_BITS, INT8_BITS) ! Copy Section 1 gribm(17:16 + len1) = cindex(45 + inc:44 + inc + len1) @@ -1183,15 +1185,13 @@ subroutine getgb2rp2(lugb, idxver, cindex, extract, gribm, leng, iret) endif call g2_gbytec8(cindex, leng8, mypos, INT8_BITS) ! length of grib message - leng = int(leng8, kind(4)) #ifdef LOGGING write(g2_log_msg, *) ' iskip8 ', iskip8, ' mypos/8 ', mypos/8, & - ' leng ', leng + ' leng8 ', leng8 call g2_log(2) #endif - if (.not. associated(gribm)) allocate(gribm(leng)) - leng8 = leng + if (.not. associated(gribm)) allocate(gribm(leng8)) call bareadl(lugb, iskip8, leng8, lread8, gribm) if (leng8 .ne. lread8) then deallocate(gribm) From fb0b6e6463427b22e40562a334dc88ba3a3c1143 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 00:59:53 -0600 Subject: [PATCH 18/22] added intents to ix2gb2() parameters --- src/g2index.F90 | 50 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/g2index.F90 b/src/g2index.F90 index e09f0acd..d76fe9c4 100644 --- a/src/g2index.F90 +++ b/src/g2index.F90 @@ -659,12 +659,12 @@ subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, ire interface ! required for cbuf pointer subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) - integer :: lugb - integer (kind = 8) :: lskip8 - integer :: idxver - integer (kind = 8) :: lgrib8 - character(len = 1), pointer, dimension(:) :: cbuf - integer :: numfld, mlen, iret + integer, intent(in) :: lugb + integer (kind = 8), intent(in) :: lskip8 + integer, intent(in) :: idxver + integer (kind = 8), intent(in) :: lgrib8 + character(len = 1), pointer, intent(inout), dimension(:) :: cbuf + integer, intent(out) :: numfld, mlen, iret end subroutine ix2gb2 end interface @@ -1190,12 +1190,12 @@ subroutine ixgb2(lugb, lskip, lgrib, cbuf, numfld, mlen, iret) interface subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) - integer :: lugb - integer (kind = 8) :: lskip8 - integer :: idxver - integer (kind = 8) :: lgrib8 - character(len = 1), pointer, dimension(:) :: cbuf - integer :: numfld, mlen, iret + integer, intent(in) :: lugb + integer (kind = 8), intent(in) :: lskip8 + integer, intent(in) :: idxver + integer (kind = 8), intent(in) :: lgrib8 + character(len = 1), pointer, intent(inout), dimension(:) :: cbuf + integer, intent(out) :: numfld, mlen, iret end subroutine ix2gb2 end interface @@ -1217,24 +1217,24 @@ end subroutine ixgb2 !> !> See getg2i2() for thr format of the index buffer records. !> -!> @param lugb Unit of the unblocked GRIB file. Must +!> @param[in] lugb Unit of the unblocked GRIB file. Must !> be opened by [baopen() or baopenr()] !> (https://noaa-emc.github.io/NCEPLIBS-bacio/). -!> @param lskip8 Number of bytes to skip before GRIB message. -!> @param idxver Index version, use 1 for legacy, 2 for GRIB2 files > 2 GB. +!> @param[in] lskip8 Number of bytes to skip before GRIB message. +!> @param[in] idxver Index version, use 1 for legacy, 2 for GRIB2 files > 2 GB. !> @param lgrib8 Number of bytes in GRIB message. When subroutine is !> called, if this is < 5000, it will be used as the number of bytes !> initially read from the file. So for GRIB2 messages of < 5000 in !> size, this should be set to the size of the GRIB2 message. -!> @param cbuf Pointer to a buffer that will get the index +!> @param[inout] cbuf Pointer to a buffer that will get the index !> records. If any memory is associated with cbuf when this subroutine !> is called, cbuf will be nullified in the subroutine. Initially cbuf !> will get an allocation of 5000 bytes. realloc() will be used to !> increase the size if necessary. Users must free memory that cbuf !> points to when cbuf is no longer needed. -!> @param numfld Number of index records created. -!> @param mlen Total length of all index records. -!> @param iret Return code +!> @param[out] numfld Number of index records created. +!> @param[out] mlen Total length of all index records. +!> @param[out] iret Return code !> - 0 No error !> - 1 Not enough memory available to hold full index buffer. !> - 2 I/O error in read. @@ -1249,12 +1249,12 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret) implicit none ! Subroutine parameters. - integer :: lugb - integer (kind = 8) :: lskip8 - integer :: idxver - integer (kind = 8) :: lgrib8 - character(len = 1), pointer, dimension(:) :: cbuf - integer :: numfld, mlen, iret + integer, intent(in) :: lugb + integer (kind = 8), intent(in) :: lskip8 + integer, intent(in) :: idxver + integer (kind = 8), intent(in) :: lgrib8 + character(len = 1), pointer, intent(inout), dimension(:) :: cbuf + integer, intent(out) :: numfld, mlen, iret character cver, cdisc character(len = 4) :: ctemp From c6c94acc93483b02041f5fdcc86e051469a8052c Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 01:11:07 -0600 Subject: [PATCH 19/22] fixed doxygen --- src/g2getgb2.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/g2getgb2.F90 b/src/g2getgb2.F90 index 4e005082..bd997242 100644 --- a/src/g2getgb2.F90 +++ b/src/g2getgb2.F90 @@ -1012,7 +1012,7 @@ end subroutine getgb2rp !> - .true. = return grib2 message containing only the requested field. !> - .false. = return entire grib2 message containing the requested field. !> @param[out] gribm Returned grib message. -!> @param[out] leng Length of returned grib message in bytes. +!> @param[out] leng8 Length of returned grib message in bytes. !> @param[out] iret Return code: !> - 0 No error. !> - 97 Error reading grib file. From 13d8059e827234bf6823116183c0f814e7fd1086 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 03:57:21 -0600 Subject: [PATCH 20/22] more testing --- tests/CMakeLists.txt | 1 - tests/test_getg2i2r.F90 | 6 +- tests/test_getgb2ir.F90 | 125 ---------------------------------------- 3 files changed, 4 insertions(+), 128 deletions(-) delete mode 100644 tests/test_getgb2ir.F90 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 08a1bdc2..b9aa0b68 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -121,7 +121,6 @@ if(FTP_TEST_FILES) create_test(test_getgb2s ${kind}) create_test(test_getgb2p ${kind}) create_test(test_getgb2r ${kind}) - create_test(test_getgb2ir ${kind}) create_test(test_file_blend ${kind}) create_test(test_aqm ${kind}) create_test(test_create_index_gdas ${kind}) diff --git a/tests/test_getg2i2r.F90 b/tests/test_getg2i2r.F90 index a276aeca..8002f508 100644 --- a/tests/test_getg2i2r.F90 +++ b/tests/test_getg2i2r.F90 @@ -4,6 +4,7 @@ ! ! Ed Hartnett 5/9/24 program test_getg2ir2 + use g2logging use bacio_module implicit none @@ -12,7 +13,7 @@ program test_getg2ir2 integer (kind = 8) :: msk1, msk2 integer :: mnum integer :: nlen, nnum, nmess, iret - integer :: idxver + integer :: idxver, i interface subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, iret) @@ -34,7 +35,8 @@ end subroutine getg2i2r call baopenr(lugb, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) if (iret .ne. 0) stop 100 - do idxver = 1, 1 + do i = 1, 1 + idxver = 1 print *, ' testing with idxver', idxver msk1 = 1000 msk2 = 1000 diff --git a/tests/test_getgb2ir.F90 b/tests/test_getgb2ir.F90 deleted file mode 100644 index 5911c704..00000000 --- a/tests/test_getgb2ir.F90 +++ /dev/null @@ -1,125 +0,0 @@ -! This is a test program for NCEPLIBS-g2. -! -! This program tests ixgb2.F90 -! -! Ed Hartnett 7/22/22 -program test_ixgb2 - use bacio_module - implicit none - - integer :: lugi = 3 - character(len=1), pointer, dimension(:) :: cbuf(:) - integer :: lskip, lgrib, numfld, mlen, iret - integer :: i - - interface - subroutine ixgb2(lugb, lskip, lgrib, cbuf, numfld, mlen, iret) - integer lugb, lskip, lgrib, numfld, mlen, iret - character(len = 1),pointer,dimension(:) :: cbuf - end subroutine ixgb2 - end interface - - ! This is the index we expect to be created from the test file. - character :: expected_cbuf(200) = (/ char(0), char(0), char(0), char(200), char(0), char(0), char(0), & - char(202), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(37), char(0), & - char(0), char(0), char(109), char(0), char(0), char(0), char(143), char(0), char(0), char(0), & - char(166), char(0), char(0), char(18), char(113), char(0), char(0), char(0), char(0), char(0), & - char(0), char(0), char(95), char(2), char(10), char(0), char(1), char(0), char(0), char(0), & - char(21), char(1), char(0), char(7), char(0), char(0), char(2), char(1), char(1), char(7), & - char(230), char(7), char(18), char(0), char(0), char(0), char(0), char(1), char(0), char(0), & - char(0), char(72), char(3), char(0), char(0), char(0), char(142), char(39), char(0), char(0), & - char(0), char(0), char(6), char(0), char(0), char(0), char(0), char(0), char(0), char(0), & - char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), & - char(0), char(241), char(0), char(0), char(0), char(151), char(0), char(0), char(0), char(0), & - char(0), char(0), char(0), char(0), char(2), char(250), char(240), char(128), char(12), char(132), & - char(88), char(128), char(48), char(1), char(125), char(120), char(64), char(14), char(230), char(178), & - char(128), char(0), char(2), char(139), char(11), char(0), char(2), char(139), char(11), char(0), & - char(0), char(0), char(0), char(34), char(4), char(0), char(0), char(0), char(0), char(0), char(5), & - char(2), char(0), char(11), char(0), char(0), char(0), char(1), char(0), char(0), char(0), char(0), & - char(1), char(0), char(0), char(0), char(0), char(1), char(255), char(0), char(0), char(0), char(0), & - char(0), char(0), char(0), char(0), char(23), char(5), char(0), char(0), char(33), char(36), char(0), & - char(40), char(65), char(136), char(0), char(0), char(0), char(0), char(0), char(2), char(9), char(0), & - char(0), char(255), char(0), char(0), char(17), char(203), char(6), char(0) /) - - integer :: index_rec_len, b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data - integer :: total_bytes, grib_version, discipline, field_number - - ! This will not work, because we try to read more bytes than the file holds. - print *, 'Trying to read too many bytes...' - lgrib = 1000 - lskip = 0 - call baopenr(lugi, "testdata_g2grids", iret) - if (iret .ne. 0) stop 3 - call ixgb2(lugi, lskip, lgrib, cbuf, numfld, mlen, iret) - if (iret .ne. 2) stop 4 - call baclose(lugi, iret) - if (iret .ne. 0) stop 5 - deallocate(cbuf) - - ! This will not work, because it's not a GRIB2 file. - print *, 'Trying to index a non-GRIB2 file...' - lgrib = 95 - lskip = 0 - call baopenr(lugi, "testdata_g2grids", iret) - if (iret .ne. 0) stop 10 - call ixgb2(lugi, lskip, lgrib, cbuf, numfld, mlen, iret) - print *, 'iret = ', iret - if (iret .ne. 3) stop 11 - call baclose(lugi, iret) - if (iret .ne. 0) stop 12 - deallocate(cbuf) - - ! Now open a real GRIB2 file. - print *, 'Indexing a real GRIB2 file...' - call baopenr(lugi, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) - if (iret .ne. 0) stop 100 - - ! Skip the first 202 bytes of the test file. - lskip = 202 - call ixgb2(lugi, lskip, lgrib, cbuf, numfld, mlen, iret) - if (iret .ne. 0) stop 101 - if (numfld .ne. 1 .or. mlen .ne. 200) stop 102 - - ! Check every value. - do i = 1, mlen - !print *, 'char(', ichar(cbuf(i)), '), ' - if (cbuf(i) .ne. expected_cbuf(i)) stop 103 - end do - - ! Break out the index record into component values. - call g2_gbytec(cbuf, index_rec_len, 0, 8*4) - if (index_rec_len .ne. 200) stop 105 - call g2_gbytec(cbuf, b2s_message, 8*4, 8*4) - if (b2s_message .ne. 202) stop 106 - call g2_gbytec(cbuf, b2s_lus, 8*8, 8*4) - if (b2s_lus .ne. 0) stop 107 - call g2_gbytec(cbuf, b2s_gds, 8*12, 8*4) - if (b2s_gds .ne. 37) stop 108 - call g2_gbytec(cbuf, b2s_pds, 8*16, 8*4) - if (b2s_pds .ne. 109) stop 109 - call g2_gbytec(cbuf, b2s_drs, 8*20, 8*4) - if (b2s_drs .ne. 143) stop 110 - call g2_gbytec(cbuf, b2s_bms, 8*24, 8*4) - if (b2s_bms .ne. 166) stop 111 - call g2_gbytec(cbuf, b2s_data, 8*28, 8*4) - if (b2s_data .ne. 4721) stop 112 - call g2_gbytec(cbuf, total_bytes, 8*32, 8*8) - if (total_bytes .ne. 95) stop 113 - call g2_gbytec(cbuf, grib_version, 8*40, 8*1) - if (grib_version .ne. 2) stop 113 - call g2_gbytec(cbuf, discipline, 8*41, 8*1) - if (discipline .ne. 10) stop 113 - call g2_gbytec(cbuf, field_number, 8*42, 8*2) - if (field_number .ne. 1) stop 113 - print *, 'index_rec_len = ', index_rec_len, ' b2s_message = ', b2s_message - print *, 'b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data: ', b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data - print *, 'total_bytes, grib_version, discipline, field_number: ', total_bytes, grib_version, discipline, field_number - - deallocate(cbuf) - - call baclose(lugi, iret) - if (iret .ne. 0) stop 199 - - print *, 'SUCCESS!...' - -end program test_ixgb2 From 57abb001350ed25b92351edf0f46110692b6579d Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 04:09:01 -0600 Subject: [PATCH 21/22] test development --- tests/g2_test_util.F90 | 171 ++++++++++++++++++++++++++++++++++++++++ tests/test_getg2i2r.F90 | 112 ++++++++++++++------------ tests/test_ix2gb2.F90 | 170 --------------------------------------- 3 files changed, 233 insertions(+), 220 deletions(-) diff --git a/tests/g2_test_util.F90 b/tests/g2_test_util.F90 index 41a89264..06dc2146 100644 --- a/tests/g2_test_util.F90 +++ b/tests/g2_test_util.F90 @@ -112,3 +112,174 @@ subroutine write_grib2_file(filename) call baclose(1, ierr) end subroutine write_grib2_file + +! Pull the values out of an index record. +! +! Edward Hartnett, 5/11/24 +subroutine read_index(cbuf, idxver, index_rec_len, b2s_message8, b2s_lus8, & + b2s_gds8, b2s_pds8, b2s_drs8, b2s_bms8, b2s_data8, total_bytes8, & + grib_version, discipline, field_number, sec1, lengds, gds, lenpds, pds, & + lendrs, drs, bms, iret) + implicit none + + character(len=1), pointer, dimension(:), intent(in) :: cbuf(:) + integer, intent(in) :: idxver + integer, intent(out) :: index_rec_len + integer (kind = 8), intent(out) :: b2s_message8, b2s_lus8, b2s_gds8, b2s_pds8, b2s_drs8, b2s_bms8, b2s_data8 + integer (kind = 8), intent(out) :: total_bytes8 + integer, intent(out) :: grib_version, discipline, field_number + character, intent(out) :: sec1(21) + integer, intent(inout) :: lengds + character, intent(out) :: gds(:) + integer, intent(inout) :: lenpds + character, intent(out) :: pds(:) + integer, intent(inout) :: lendrs + character, intent(out) :: drs(:) + character, intent(out) :: bms(:) + integer, intent(out) :: iret + + integer :: lensec1 + + integer :: b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data + integer :: inc, mypos = 0 + integer :: i + integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS + parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64) + integer (kind = 8) :: INT8_BITS8 + parameter(INT8_BITS8 = 64_8) + integer :: BMS_LEN + parameter (BMS_LEN = 6) + + interface + subroutine g2_gbytec1(in, siout, iskip, nbits) + character*1, intent(in) :: in(*) + integer, intent(inout) :: siout + integer, intent(in) :: iskip, nbits + end subroutine g2_gbytec1 + subroutine g2_gbytec(in, iout, iskip, nbits) + character*1, intent(in) :: in(*) + integer, intent(inout) :: iout(*) + integer, intent(in) :: iskip, nbits + end subroutine g2_gbytec + subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n) + character*1, intent(in) :: in(*) + integer, intent(out) :: iout(*) + integer, intent(in) :: iskip, nbits, nskip, n + end subroutine g2_gbytesc + subroutine g2_gbytec8(in, iout, iskip, nbits) + character*1, intent(in) :: in(*) + integer (kind = 8), intent(inout) :: iout(*) + integer, intent(in) :: iskip, nbits + end subroutine g2_gbytec8 + end interface + + ! Get the index record len (4 byte int). + call g2_gbytec1(cbuf, index_rec_len, 0, INT4_BITS) + !print *, 'read_index(): index_rec_len', index_rec_len + mypos = INT4_BITS + + if (idxver .eq. 1) then + inc = 0 + call g2_gbytec1(cbuf, b2s_message, mypos, INT4_BITS) + !print '(i3, a12, z4)', mypos/8, ' b2s_message', b2s_message + mypos = mypos + INT4_BITS + b2s_message8 = b2s_message + call g2_gbytec1(cbuf, b2s_lus, mypos, INT4_BITS) + !print '(i3, a8, z4)', mypos/8, ' b2s_lus', b2s_lus + mypos = mypos + INT4_BITS + b2s_lus8 = b2s_lus + call g2_gbytec1(cbuf, b2s_gds, mypos, INT4_BITS) + !print '(i3, a8, z4)', mypos/8, ' b2s_gds', b2s_gds + mypos = mypos + INT4_BITS + b2s_gds8 = b2s_gds + else + inc = 12 + call g2_gbytec81(cbuf, b2s_message8, 8 * 4, INT8_BITS) + mypos = mypos + INT8_BITS + call g2_gbytec81(cbuf, b2s_lus8, 8 * 12, INT8_BITS) + mypos = mypos + INT8_BITS + call g2_gbytec81(cbuf, b2s_gds8, 8 * 20, INT8_BITS) + mypos = mypos + INT8_BITS + ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, INT8_BITS) + endif + call g2_gbytec1(cbuf, b2s_pds, mypos, INT4_BITS) + mypos = mypos + INT4_BITS + b2s_pds8 = b2s_pds + call g2_gbytec1(cbuf, b2s_drs, mypos, INT4_BITS) + mypos = mypos + INT4_BITS + b2s_drs8 = b2s_drs + call g2_gbytec1(cbuf, b2s_bms, mypos, INT4_BITS) + mypos = mypos + INT4_BITS + b2s_bms8 = b2s_bms + call g2_gbytec1(cbuf, b2s_data, mypos, INT4_BITS) + mypos = mypos + INT4_BITS + b2s_data8 = b2s_data + call g2_gbytec81(cbuf, total_bytes8, mypos, INT8_BITS) + mypos = mypos + INT8_BITS + call g2_gbytec1(cbuf, grib_version, mypos, INT1_BITS) + mypos = mypos + INT1_BITS + call g2_gbytec1(cbuf, discipline, mypos, INT1_BITS) + mypos = mypos + INT1_BITS + call g2_gbytec1(cbuf, field_number, mypos, INT2_BITS) + mypos = mypos + INT2_BITS + + ! Find the length of sec1. It should be 21. + call g2_gbytec1(cbuf, lensec1, mypos, INT4_BITS) + !mypos = mypos + INT4_BITS + + ! Copy section 1 from the index record to output parameter. (mypos + ! is in bits, but i is in bytes.) + !print *, 'copying sec1', mypos/8 + do i = 1, lensec1 + sec1(i) = cbuf(mypos/8 + 1) + mypos = mypos + INT1_BITS + end do + + ! Find the length of gds. It should be 72. + call g2_gbytec1(cbuf, lengds, mypos, INT4_BITS) + + ! Copy GDS from the index record to output parameter. (mypos + ! is in bits, but i is in bytes.) + !print *, 'copying gds', lengds, mypos/8 + do i = 1, lengds + gds(i) = cbuf(mypos/8 + 1) + mypos = mypos + INT1_BITS + end do + + ! Find the length of pds. It should be 72. + call g2_gbytec1(cbuf, lenpds, mypos, INT4_BITS) + + ! Copy PDS from the index record to output parameter. (mypos + ! is in bits, but i is in bytes.) + !print *, 'copying pds', lenpds, mypos/8 + do i = 1, lenpds + pds(i) = cbuf(mypos/8 + 1) + !print *, ichar(pds(i)), ',' + mypos = mypos + INT1_BITS + end do + + ! Find the length of drs. It should be 72. + call g2_gbytec1(cbuf, lendrs, mypos, INT4_BITS) + + ! Copy DRS from the index record to output parameter. (mypos + ! is in bits, but i is in bytes.) + !print *, 'copying drs', lendrs, mypos/8 + do i = 1, lendrs + drs(i) = cbuf(mypos/8 + 1) + !print *, ichar(drs(i)), ',' + mypos = mypos + INT1_BITS + end do + + ! Copy the 6 bytes of bms from the index record to output + ! parameter. (mypos is in bits, but i is in bytes.) + !print *, 'copying bms', mypos/8 + do i = 1, BMS_LEN + bms(i) = cbuf(mypos/8 + 1) + !print *, ichar(bms(i)), ',' + mypos = mypos + INT1_BITS + end do + + ! Return success. + iret = 0 +end subroutine read_index + diff --git a/tests/test_getg2i2r.F90 b/tests/test_getg2i2r.F90 index 8002f508..9b5a37de 100644 --- a/tests/test_getg2i2r.F90 +++ b/tests/test_getg2i2r.F90 @@ -32,70 +32,82 @@ end subroutine getg2i2r ! Open a real GRIB2 file. print *, 'Indexing a real GRIB2 file...' - call baopenr(lugb, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) - if (iret .ne. 0) stop 100 - - do i = 1, 1 - idxver = 1 + do i = 1, 2 + idxver = i print *, ' testing with idxver', idxver + + call baopenr(lugb, "data/WW3_Regional_US_West_Coast_20220718_0000.grib2", iret) + if (iret .ne. 0) stop 100 + msk1 = 1000 msk2 = 1000 mnum = 0 call getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, iret) if (iret .ne. 0) stop 101 print *, 'nlen, nnum, nmess: ', nlen, nnum, nmess - if (nlen .ne. 137600 .or. nnum .ne. 688 .or. nmess .ne. 688) stop 102 - - ! Break out the index record into component values. + if (nnum .ne. 688 .or. nmess .ne. 688) stop 102 if (idxver .eq. 1) then - inc = 0 - call g2_gbytec(cbuf, index_rec_len, 0, 8 * 4) - if (index_rec_len .ne. 200) stop 105 - print *, 'index_rec_len', index_rec_len - call g2_gbytec(cbuf, b2s_message, 8 * 4, 8 * 4) - if (b2s_message .ne. 202) stop 106 - call g2_gbytec(cbuf, b2s_lus, 8 * 8, 8 * 4) - if (b2s_lus .ne. 0) stop 107 - call g2_gbytec(cbuf, b2s_gds, 8 * 12, 8 * 4) - if (b2s_gds .ne. 37) stop 108 + if (nlen .ne. 137600) stop 103 else - inc = 16 - call g2_gbytec(cbuf, index_rec_len, 0, 8 * 8) - if (index_rec_len .ne. 200) stop 105 - print *, 'index_rec_len', index_rec_len - call g2_gbytec(cbuf, b2s_message, 8 * 8, 8 * 8) - if (b2s_message .ne. 202) stop 106 - call g2_gbytec(cbuf, b2s_lus, 8 * 8, 8 * 8) - if (b2s_lus .ne. 0) stop 107 - call g2_gbytec(cbuf, b2s_gds, 8 * 12, 8 * 8) - if (b2s_gds .ne. 37) stop 108 - ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, 8 * 8) - ! if (b2s_pds .ne. 109) stop 109 + if (nlen .ne. 145856) then + print *, nlen + stop 103 + endif endif - call g2_gbytec(cbuf, b2s_pds, 8 * 16, 8 * 4) - if (b2s_pds .ne. 109) stop 109 - call g2_gbytec(cbuf, b2s_drs, inc + 8 * 20, 8 * 4) - if (b2s_drs .ne. 143) stop 110 - call g2_gbytec(cbuf, b2s_bms, inc + 8 * 24, 8 * 4) - if (b2s_bms .ne. 166) stop 111 - call g2_gbytec(cbuf, b2s_data, inc + 8 * 28, 8 * 4) - if (b2s_data .ne. 4721) stop 112 - call g2_gbytec(cbuf, total_bytes, inc + 8 * 32, 8 * 8) - if (total_bytes .ne. 11183) stop 113 - call g2_gbytec(cbuf, grib_version, inc + 8 * 40, 8 * 1) - if (grib_version .ne. 2) stop 113 - call g2_gbytec(cbuf, discipline, inc + 8 * 41, 8 * 1) - if (discipline .ne. 10) stop 113 - call g2_gbytec(cbuf, field_number, inc + 8 * 42, 8 * 2) - if (field_number .ne. 1) stop 113 - print *, 'index_rec_len = ', index_rec_len, ' b2s_message = ', b2s_message - print *, 'b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data: ', b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data - print *, 'total_bytes, grib_version, discipline, field_number: ', total_bytes, grib_version, discipline, field_number + + ! Break out the index record into component values. + ! if (idxver .eq. 1) then + ! inc = 0 + ! call g2_gbytec(cbuf, index_rec_len, 0, 8 * 4) + ! if (index_rec_len .ne. 200) stop 105 + ! print *, 'index_rec_len', index_rec_len + ! call g2_gbytec(cbuf, b2s_message, 8 * 4, 8 * 4) + ! if (b2s_message .ne. 202) stop 106 + ! call g2_gbytec(cbuf, b2s_lus, 8 * 8, 8 * 4) + ! if (b2s_lus .ne. 0) stop 107 + ! call g2_gbytec(cbuf, b2s_gds, 8 * 12, 8 * 4) + ! if (b2s_gds .ne. 37) stop 108 + ! else + ! inc = 16 + ! call g2_gbytec(cbuf, index_rec_len, 0, 8 * 8) + ! if (index_rec_len .ne. 200) then + ! print *, 'index_rec_len', index_rec_len + ! stop 110 + ! endif + ! print *, 'index_rec_len', index_rec_len + ! call g2_gbytec(cbuf, b2s_message, 8 * 8, 8 * 8) + ! if (b2s_message .ne. 202) stop 111 + ! call g2_gbytec(cbuf, b2s_lus, 8 * 8, 8 * 8) + ! if (b2s_lus .ne. 0) stop 112 + ! call g2_gbytec(cbuf, b2s_gds, 8 * 12, 8 * 8) + ! if (b2s_gds .ne. 37) stop 113 + ! ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, 8 * 8) + ! ! if (b2s_pds .ne. 109) stop 114 + ! endif + ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, 8 * 4) + ! if (b2s_pds .ne. 109) stop 200 + ! call g2_gbytec(cbuf, b2s_drs, inc + 8 * 20, 8 * 4) + ! if (b2s_drs .ne. 143) stop 210 + ! call g2_gbytec(cbuf, b2s_bms, inc + 8 * 24, 8 * 4) + ! if (b2s_bms .ne. 166) stop 220 + ! call g2_gbytec(cbuf, b2s_data, inc + 8 * 28, 8 * 4) + ! if (b2s_data .ne. 4721) stop 230 + ! call g2_gbytec(cbuf, total_bytes, inc + 8 * 32, 8 * 8) + ! if (total_bytes .ne. 11183) stop 240 + ! call g2_gbytec(cbuf, grib_version, inc + 8 * 40, 8 * 1) + ! if (grib_version .ne. 2) stop 250 + ! call g2_gbytec(cbuf, discipline, inc + 8 * 41, 8 * 1) + ! if (discipline .ne. 10) stop 260 + ! call g2_gbytec(cbuf, field_number, inc + 8 * 42, 8 * 2) + ! if (field_number .ne. 1) stop 270 + ! print *, 'index_rec_len = ', index_rec_len, ' b2s_message = ', b2s_message + ! print *, 'b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data: ', b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data + ! print *, 'total_bytes, grib_version, discipline, field_number: ', total_bytes, grib_version, discipline, field_number deallocate(cbuf) call baclose(lugb, iret) - if (iret .ne. 0) stop 199 + if (iret .ne. 0) stop 500 print *, ' OK!' end do print *, 'SUCCESS!...' diff --git a/tests/test_ix2gb2.F90 b/tests/test_ix2gb2.F90 index 98a01ff6..c4b007bf 100644 --- a/tests/test_ix2gb2.F90 +++ b/tests/test_ix2gb2.F90 @@ -176,173 +176,3 @@ end subroutine read_index end program test_ix2gb2 -! Pull the values out of an index record. -! -! Edward Hartnett, 5/11/24 -subroutine read_index(cbuf, idxver, index_rec_len, b2s_message8, b2s_lus8, & - b2s_gds8, b2s_pds8, b2s_drs8, b2s_bms8, b2s_data8, total_bytes8, & - grib_version, discipline, field_number, sec1, lengds, gds, lenpds, pds, & - lendrs, drs, bms, iret) - implicit none - - character(len=1), pointer, dimension(:), intent(in) :: cbuf(:) - integer, intent(in) :: idxver - integer, intent(out) :: index_rec_len - integer (kind = 8), intent(out) :: b2s_message8, b2s_lus8, b2s_gds8, b2s_pds8, b2s_drs8, b2s_bms8, b2s_data8 - integer (kind = 8), intent(out) :: total_bytes8 - integer, intent(out) :: grib_version, discipline, field_number - character, intent(out) :: sec1(21) - integer, intent(inout) :: lengds - character, intent(out) :: gds(:) - integer, intent(inout) :: lenpds - character, intent(out) :: pds(:) - integer, intent(inout) :: lendrs - character, intent(out) :: drs(:) - character, intent(out) :: bms(:) - integer, intent(out) :: iret - - integer :: lensec1 - - integer :: b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data - integer :: inc, mypos = 0 - integer :: i - integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS - parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64) - integer (kind = 8) :: INT8_BITS8 - parameter(INT8_BITS8 = 64_8) - integer :: BMS_LEN - parameter (BMS_LEN = 6) - - interface - subroutine g2_gbytec1(in, siout, iskip, nbits) - character*1, intent(in) :: in(*) - integer, intent(inout) :: siout - integer, intent(in) :: iskip, nbits - end subroutine g2_gbytec1 - subroutine g2_gbytec(in, iout, iskip, nbits) - character*1, intent(in) :: in(*) - integer, intent(inout) :: iout(*) - integer, intent(in) :: iskip, nbits - end subroutine g2_gbytec - subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n) - character*1, intent(in) :: in(*) - integer, intent(out) :: iout(*) - integer, intent(in) :: iskip, nbits, nskip, n - end subroutine g2_gbytesc - subroutine g2_gbytec8(in, iout, iskip, nbits) - character*1, intent(in) :: in(*) - integer (kind = 8), intent(inout) :: iout(*) - integer, intent(in) :: iskip, nbits - end subroutine g2_gbytec8 - end interface - - ! Get the index record len (4 byte int). - call g2_gbytec1(cbuf, index_rec_len, 0, INT4_BITS) - !print *, 'read_index(): index_rec_len', index_rec_len - mypos = INT4_BITS - - if (idxver .eq. 1) then - inc = 0 - call g2_gbytec1(cbuf, b2s_message, mypos, INT4_BITS) - !print '(i3, a12, z4)', mypos/8, ' b2s_message', b2s_message - mypos = mypos + INT4_BITS - b2s_message8 = b2s_message - call g2_gbytec1(cbuf, b2s_lus, mypos, INT4_BITS) - !print '(i3, a8, z4)', mypos/8, ' b2s_lus', b2s_lus - mypos = mypos + INT4_BITS - b2s_lus8 = b2s_lus - call g2_gbytec1(cbuf, b2s_gds, mypos, INT4_BITS) - !print '(i3, a8, z4)', mypos/8, ' b2s_gds', b2s_gds - mypos = mypos + INT4_BITS - b2s_gds8 = b2s_gds - else - inc = 12 - call g2_gbytec81(cbuf, b2s_message8, 8 * 4, INT8_BITS) - mypos = mypos + INT8_BITS - call g2_gbytec81(cbuf, b2s_lus8, 8 * 12, INT8_BITS) - mypos = mypos + INT8_BITS - call g2_gbytec81(cbuf, b2s_gds8, 8 * 20, INT8_BITS) - mypos = mypos + INT8_BITS - ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, INT8_BITS) - endif - call g2_gbytec1(cbuf, b2s_pds, mypos, INT4_BITS) - mypos = mypos + INT4_BITS - b2s_pds8 = b2s_pds - call g2_gbytec1(cbuf, b2s_drs, mypos, INT4_BITS) - mypos = mypos + INT4_BITS - b2s_drs8 = b2s_drs - call g2_gbytec1(cbuf, b2s_bms, mypos, INT4_BITS) - mypos = mypos + INT4_BITS - b2s_bms8 = b2s_bms - call g2_gbytec1(cbuf, b2s_data, mypos, INT4_BITS) - mypos = mypos + INT4_BITS - b2s_data8 = b2s_data - call g2_gbytec81(cbuf, total_bytes8, mypos, INT8_BITS) - mypos = mypos + INT8_BITS - call g2_gbytec1(cbuf, grib_version, mypos, INT1_BITS) - mypos = mypos + INT1_BITS - call g2_gbytec1(cbuf, discipline, mypos, INT1_BITS) - mypos = mypos + INT1_BITS - call g2_gbytec1(cbuf, field_number, mypos, INT2_BITS) - mypos = mypos + INT2_BITS - - ! Find the length of sec1. It should be 21. - call g2_gbytec1(cbuf, lensec1, mypos, INT4_BITS) - !mypos = mypos + INT4_BITS - - ! Copy section 1 from the index record to output parameter. (mypos - ! is in bits, but i is in bytes.) - !print *, 'copying sec1', mypos/8 - do i = 1, lensec1 - sec1(i) = cbuf(mypos/8 + 1) - mypos = mypos + INT1_BITS - end do - - ! Find the length of gds. It should be 72. - call g2_gbytec1(cbuf, lengds, mypos, INT4_BITS) - - ! Copy GDS from the index record to output parameter. (mypos - ! is in bits, but i is in bytes.) - !print *, 'copying gds', lengds, mypos/8 - do i = 1, lengds - gds(i) = cbuf(mypos/8 + 1) - mypos = mypos + INT1_BITS - end do - - ! Find the length of pds. It should be 72. - call g2_gbytec1(cbuf, lenpds, mypos, INT4_BITS) - - ! Copy PDS from the index record to output parameter. (mypos - ! is in bits, but i is in bytes.) - !print *, 'copying pds', lenpds, mypos/8 - do i = 1, lenpds - pds(i) = cbuf(mypos/8 + 1) - !print *, ichar(pds(i)), ',' - mypos = mypos + INT1_BITS - end do - - ! Find the length of drs. It should be 72. - call g2_gbytec1(cbuf, lendrs, mypos, INT4_BITS) - - ! Copy DRS from the index record to output parameter. (mypos - ! is in bits, but i is in bytes.) - !print *, 'copying drs', lendrs, mypos/8 - do i = 1, lendrs - drs(i) = cbuf(mypos/8 + 1) - !print *, ichar(drs(i)), ',' - mypos = mypos + INT1_BITS - end do - - ! Copy the 6 bytes of bms from the index record to output - ! parameter. (mypos is in bits, but i is in bytes.) - !print *, 'copying bms', mypos/8 - do i = 1, BMS_LEN - bms(i) = cbuf(mypos/8 + 1) - !print *, ichar(bms(i)), ',' - mypos = mypos + INT1_BITS - end do - - ! Return success. - iret = 0 -end subroutine read_index - From d4f7d80933f41951b7c280d2bd1452418434863e Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Thu, 16 May 2024 04:27:59 -0600 Subject: [PATCH 22/22] improved test --- tests/g2_test_util.F90 | 6 ++ tests/test_getg2i2r.F90 | 161 +++++++++++++++++++++++++++------------- 2 files changed, 115 insertions(+), 52 deletions(-) diff --git a/tests/g2_test_util.F90 b/tests/g2_test_util.F90 index 06dc2146..2e94c5e0 100644 --- a/tests/g2_test_util.F90 +++ b/tests/g2_test_util.F90 @@ -156,16 +156,22 @@ subroutine g2_gbytec1(in, siout, iskip, nbits) integer, intent(inout) :: siout integer, intent(in) :: iskip, nbits end subroutine g2_gbytec1 + end interface + interface subroutine g2_gbytec(in, iout, iskip, nbits) character*1, intent(in) :: in(*) integer, intent(inout) :: iout(*) integer, intent(in) :: iskip, nbits end subroutine g2_gbytec + end interface + interface subroutine g2_gbytesc(in, iout, iskip, nbits, nskip, n) character*1, intent(in) :: in(*) integer, intent(out) :: iout(*) integer, intent(in) :: iskip, nbits, nskip, n end subroutine g2_gbytesc + end interface + interface subroutine g2_gbytec8(in, iout, iskip, nbits) character*1, intent(in) :: in(*) integer (kind = 8), intent(inout) :: iout(*) diff --git a/tests/test_getg2i2r.F90 b/tests/test_getg2i2r.F90 index 9b5a37de..95fab7c3 100644 --- a/tests/test_getg2i2r.F90 +++ b/tests/test_getg2i2r.F90 @@ -13,9 +13,64 @@ program test_getg2ir2 integer (kind = 8) :: msk1, msk2 integer :: mnum integer :: nlen, nnum, nmess, iret - integer :: idxver, i + integer :: idxver, i, j + integer :: index_rec_len + integer (kind = 8) :: b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data + integer (kind = 8) :: total_bytes + integer :: grib_version, discipline, field_number + integer :: SEC1_LEN + parameter (SEC1_LEN = 21) + integer :: GDS_LEN + parameter (GDS_LEN = 72) + integer :: PDS_LEN + parameter (PDS_LEN = 34) + integer :: DRS_LEN + parameter (DRS_LEN = 23) + integer :: BMS_LEN + parameter (BMS_LEN = 6) + integer :: lengds, lenpds, lendrs + character :: sec1(SEC1_LEN), gds(GDS_LEN), pds(PDS_LEN), drs(DRS_LEN), bms(BMS_LEN) + character :: expected_sec1(SEC1_LEN) = (/ char(0), char(0), char(0), char(21), char(1), char(0), & + char(7), char(0), char(0), char(2), char(1), & + char(1), char(7), char(229), char(11), char(30), char(0), char(0), char(0), char(0), char(1)/) + character :: expected_gds(GDS_LEN) = (/ char(0), char(0), char(0), char(72), char(3), char(0), char(0), & + char(0), char(142), char(39), char(0), char(0), char(0), char(0), char(6), char(0), char(0), & + char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), & + char(0), char(0), char(0), char(0), char(0), char(241), char(0), char(0), char(0), char(151), & + char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(0), char(2), char(250), & + char(240), char(128), char(12), char(132), char(88), char(128), char(48), char(1), char(125), & + char(120), char(64), char(14), char(230), char(178), char(128), char(0), char(2), char(139), & + char(11), char(0), char(2), char(139), char(11), char(0) /) + character :: expected_pds(PDS_LEN) = (/ char(0), char(0), char(0), char(34), char(4), char(0), char(0), & + char(0), char(0), char(2), char(1), char(2), char(0), char(11), char(0), char(0), char(0), char(1), & + char(0), char(0), char(0), char(0), char(1), char(0), char(0), char(0), char(0), char(1), char(255), & + char(0), char(0), char(0), char(0), char(0) /) + character :: expected_drs(DRS_LEN) = (/ char(0), char(0), char(0), char(23), char(5), char(0), char(0), & + char(43), char(33), char(0), char(40), char(65), char(32), char(0), char(0), char(0), char(0), char(0), & + char(2), char(11), char(0), char(0), char(255) /) + character :: expected_bms(BMS_LEN) = (/ char(0), char(0), char(17), char(203), char(6), char(0) /) + interface + subroutine read_index(cbuf, idxver, index_rec_len, b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, & + b2s_bms, b2s_data, total_bytes, grib_version, discipline, field_number, sec1, lengds, gds, lenpds, pds, & + lendrs, drs, bms, iret) + character(len=1), pointer, dimension(:), intent(in) :: cbuf(:) + integer, intent(in) :: idxver + integer, intent(out) :: index_rec_len + integer (kind = 8), intent(out) :: b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data + integer (kind = 8), intent(out) :: total_bytes + integer, intent(out) :: grib_version, discipline, field_number + character, intent(out) :: sec1(21) + integer, intent(inout) :: lengds + character, intent(out) :: gds(:) + integer, intent(inout) :: lenpds + character, intent(out) :: pds(:) + integer, intent(inout) :: lendrs + character, intent(out) :: drs(:) + character, intent(out) :: bms(:) + integer, intent(out) :: iret + end subroutine read_index subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, iret) integer, intent(in) :: lugb integer (kind = 8), intent(in) :: msk1, msk2 @@ -25,9 +80,6 @@ subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, ire end subroutine getg2i2r end interface - integer :: index_rec_len, b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data - integer :: total_bytes, grib_version, discipline, field_number, inc - print *, 'Testing the getg2ir2() subroutine - expect and ignore error messages during test...' ! Open a real GRIB2 file. @@ -55,55 +107,60 @@ end subroutine getg2i2r endif endif - ! Break out the index record into component values. - ! if (idxver .eq. 1) then - ! inc = 0 - ! call g2_gbytec(cbuf, index_rec_len, 0, 8 * 4) - ! if (index_rec_len .ne. 200) stop 105 - ! print *, 'index_rec_len', index_rec_len - ! call g2_gbytec(cbuf, b2s_message, 8 * 4, 8 * 4) - ! if (b2s_message .ne. 202) stop 106 - ! call g2_gbytec(cbuf, b2s_lus, 8 * 8, 8 * 4) - ! if (b2s_lus .ne. 0) stop 107 - ! call g2_gbytec(cbuf, b2s_gds, 8 * 12, 8 * 4) - ! if (b2s_gds .ne. 37) stop 108 - ! else - ! inc = 16 - ! call g2_gbytec(cbuf, index_rec_len, 0, 8 * 8) - ! if (index_rec_len .ne. 200) then - ! print *, 'index_rec_len', index_rec_len - ! stop 110 - ! endif - ! print *, 'index_rec_len', index_rec_len - ! call g2_gbytec(cbuf, b2s_message, 8 * 8, 8 * 8) - ! if (b2s_message .ne. 202) stop 111 - ! call g2_gbytec(cbuf, b2s_lus, 8 * 8, 8 * 8) - ! if (b2s_lus .ne. 0) stop 112 - ! call g2_gbytec(cbuf, b2s_gds, 8 * 12, 8 * 8) - ! if (b2s_gds .ne. 37) stop 113 - ! ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, 8 * 8) - ! ! if (b2s_pds .ne. 109) stop 114 - ! endif - ! call g2_gbytec(cbuf, b2s_pds, 8 * 16, 8 * 4) - ! if (b2s_pds .ne. 109) stop 200 - ! call g2_gbytec(cbuf, b2s_drs, inc + 8 * 20, 8 * 4) - ! if (b2s_drs .ne. 143) stop 210 - ! call g2_gbytec(cbuf, b2s_bms, inc + 8 * 24, 8 * 4) - ! if (b2s_bms .ne. 166) stop 220 - ! call g2_gbytec(cbuf, b2s_data, inc + 8 * 28, 8 * 4) - ! if (b2s_data .ne. 4721) stop 230 - ! call g2_gbytec(cbuf, total_bytes, inc + 8 * 32, 8 * 8) - ! if (total_bytes .ne. 11183) stop 240 - ! call g2_gbytec(cbuf, grib_version, inc + 8 * 40, 8 * 1) - ! if (grib_version .ne. 2) stop 250 - ! call g2_gbytec(cbuf, discipline, inc + 8 * 41, 8 * 1) - ! if (discipline .ne. 10) stop 260 - ! call g2_gbytec(cbuf, field_number, inc + 8 * 42, 8 * 2) - ! if (field_number .ne. 1) stop 270 - ! print *, 'index_rec_len = ', index_rec_len, ' b2s_message = ', b2s_message - ! print *, 'b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data: ', b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data - ! print *, 'total_bytes, grib_version, discipline, field_number: ', total_bytes, grib_version, discipline, field_number + ! Break out the first index record into component values. + call read_index(cbuf, idxver, index_rec_len, b2s_message, b2s_lus, b2s_gds, b2s_pds, b2s_drs, & + b2s_bms, b2s_data, total_bytes, grib_version, discipline, field_number, sec1, lengds, gds, & + lenpds, pds, lendrs, drs, bms, iret) + if (iret .ne. 0) stop 21 + + print *, ' index_rec_len = ', index_rec_len, ' b2s_message = ', b2s_message + print *, ' b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data: ', b2s_lus, b2s_gds, b2s_pds, b2s_drs, b2s_bms, b2s_data + print *, ' total_bytes, grib_version, discipline, field_number: ', total_bytes, grib_version, discipline, field_number + print *, ' lengds, lenpds, lendrs', lengds, lenpds, lendrs + if (idxver .eq. 1) then + if (index_rec_len .ne. 200) stop 105 + else + if (index_rec_len .ne. 212) then + print *, index_rec_len + stop 105 + endif + endif + if (b2s_message .ne. 202) stop 106 + if (b2s_lus .ne. 0) stop 107 + if (b2s_gds .ne. 37) stop 108 + if (b2s_pds .ne. 109) stop 109 + if (b2s_drs .ne. 143) stop 110 + if (b2s_bms .ne. 166) stop 111 + if (b2s_data .ne. 4721) stop 112 + if (total_bytes .ne. 11183) stop 113 + if (grib_version .ne. 2) stop 114 + if (discipline .ne. 10) stop 115 + if (field_number .ne. 1) stop 116 + do j = 1, SEC1_LEN + !print *, i, ichar(sec1(i)) + if (sec1(i) .ne. expected_sec1(i)) stop 200 + enddo + if (lengds .ne. GDS_LEN) stop 201 + do j = 1, GDS_LEN + !print *, i, ichar(gds(i)) + if (gds(i) .ne. expected_gds(i)) stop 201 + enddo + if (lenpds .ne. PDS_LEN) stop 201 + do j = 1, PDS_LEN + !print *, i, ichar(pds(i)) + if (pds(i) .ne. expected_pds(i)) stop 210 + enddo + if (lendrs .ne. DRS_LEN) stop 201 + do j = 1, DRS_LEN + !print *, i, ichar(drs(i)) + if (drs(i) .ne. expected_drs(i)) stop 210 + enddo + do j = 1, BMS_LEN + !print *, i, ichar(bms(i)) + if (bms(i) .ne. expected_bms(i)) stop 210 + enddo + deallocate(cbuf) call baclose(lugb, iret)