Skip to content

Commit

Permalink
more index work
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhartnett committed Feb 12, 2024
1 parent 78c4cd5 commit 7088a65
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/g2index.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib, cbuf, numfld, mlen, iret)
mypos = mypos + INT1_BITS
call g2_sbytec(cindex, numfld + 1, mypos, INT2_BITS) ! field num
mypos = mypos + INT2_BITS
cindex(ixids + 1:ixids + lensec1) = cids(1:lensec1)
cindex(ixids + 1 + inc:ixids + lensec1 + inc) = cids(1:lensec1)
lindex = ixids + lensec1 + inc
cindex(lindex + 1:lindex + lengds8) = cgds(1:lengds8)
lindex = lindex + int(lengds8, kind(lindex))
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ endfunction()
# Some very small test files may be committed to the repo. This
# function copies such a data file to the build directory.
function(copy_test_data name)
message(STATUS "Copying test file ${name}")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/${name}"
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
Expand Down Expand Up @@ -133,6 +134,7 @@ endif()
# Copy test data files that are in the repo to the build directory.
copy_test_data(testdata_g2grids)
copy_test_data(ref_gdaswave.t00z.wcoast.0p16.f000.grb2index)
copy_test_data(g1.grib2)
copy_test_data(gdaswave.t00z.wcoast.0p16.f000.grib2)
copy_test_data(ref_test_jpcpack_4_output.txt)

Expand All @@ -142,6 +144,7 @@ foreach(kind ${kinds})
# create_test(test_g2cf ${kind})
create_test(test_misc ${kind})
create_test(test_g2 ${kind})
create_test(test_g1 ${kind})
create_test(test_g2_encode ${kind})
create_test(test_g2_decode ${kind})
create_test(test_gridtemplates ${kind})
Expand Down
136 changes: 136 additions & 0 deletions tests/test_g1.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
! This is a test program for NCEPLIBS-g2.
!
! This program tests getidx().
!
! Ed Hartnett 7/26/22
program test_getidx
use bacio_module
implicit none

character(*) :: TEST_FILE_G1
parameter (TEST_FILE_G1 = 'g1.grib2')
integer :: lugi
character(len=1), pointer, dimension(:) :: cbuf(:)
integer :: lugb = 3
integer :: nlen, nnum, iret
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, i, idxver, j
integer (kind = 8) :: b2s_message8

integer :: lu_gdas = 4, lu_gdas_index = 5, mypos
integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS
parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64)

interface
subroutine getidx(lugb, lugi, cindex, nlen, nnum, iret)
integer, intent(in) :: lugb, lugi
integer, intent(out) :: nlen, nnum, iret
character(len = 1), pointer, dimension(:) :: cindex
end subroutine getidx
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
end interface

print *, 'Testing the getidx() subroutine with simple file...'

do i = 1, 2
! Open a real GRIB2 file.
print *, 'Indexing a real GRIB2 file: ', TEST_FILE_G1
call baopenr(lugb, TEST_FILE_G1, iret)
if (iret .ne. 0) stop 10

! Get the index info, telling getidx() to generate it directly from
! the GRIB2 file.
lugi = 0
idxver = i
call getidx2(lugb, lugi, idxver, cbuf, nlen, nnum, iret)
if (iret .ne. 0) stop 20
print *, 'nlen, nnum: ', nlen, nnum
if (nnum .ne. 1) stop 21
if (i .eq. 1) then
if (nlen .ne. 200) stop 22
else
if (nlen .ne. 204) stop 23
endif
do j = 1, nlen
print '(i3, x, z2.2)', j, cbuf(j)
end do

! Break out the index record into component values and check them for correctness.
mypos = 0
call g2_gbytec(cbuf, index_rec_len, mypos, INT4_BITS)
mypos = mypos + INT4_BITS
if (i .eq. 1) then
if (index_rec_len .ne. 200) stop 30
else
if (index_rec_len .ne. 204) stop 30
endif
if (i .eq. 1) then
call g2_gbytec(cbuf, b2s_message, mypos, INT4_BITS)
if (b2s_message .ne. 0) stop 31
mypos = mypos + INT4_BITS
b2s_message8 = b2s_message
else
call g2_gbytec8(cbuf, b2s_message8, mypos, INT8_BITS)
if (b2s_message8 .ne. 0) stop 32
mypos = mypos + INT8_BITS
endif
call g2_gbytec(cbuf, b2s_lus, mypos, INT4_BITS)
if (b2s_lus .ne. 0) stop 33
mypos = mypos + INT4_BITS
call g2_gbytec(cbuf, b2s_gds, mypos, INT4_BITS)
if (b2s_gds .ne. 37) stop 34
mypos = mypos + INT4_BITS
call g2_gbytec(cbuf, b2s_pds, mypos, INT4_BITS)
if (b2s_pds .ne. 109) stop 35
mypos = mypos + INT4_BITS
call g2_gbytec(cbuf, b2s_drs, mypos, INT4_BITS)
if (b2s_drs .ne. 143) stop 36
mypos = mypos + INT4_BITS
call g2_gbytec(cbuf, b2s_bms, mypos, INT4_BITS)
if (b2s_bms .ne. 166) stop 37
mypos = mypos + INT4_BITS
call g2_gbytec(cbuf, b2s_data, mypos, INT4_BITS)
if (b2s_data .ne. 4721) stop 38
mypos = mypos + INT4_BITS
print *, 'total_bytes mypos ', mypos
call g2_gbytec(cbuf, total_bytes, mypos, INT8_BITS)
print *, 'total_bytes ', total_bytes

if (total_bytes .ne. 15254) stop 39
mypos = mypos + INT8_BITS
print *, 'test_getidx expecting grib_version after byte, bit ', mypos, mypos/8
print '(z2.2)', cbuf(mypos/8 + 0)
print '(z2.2)', cbuf(mypos/8 + 1)
print '(z2.2)', cbuf(mypos/8 + 2)
print '(z2.2)', cbuf(mypos/8 + 3)
call g2_gbytec(cbuf, grib_version, mypos, INT1_BITS)
print *, grib_version, mypos
if (grib_version .ne. 2) stop 40
mypos = mypos + INT1_BITS
call g2_gbytec(cbuf, discipline, mypos, INT1_BITS)
print *, 'discipline', discipline
if (discipline .ne. 0) stop 41
mypos = mypos + INT1_BITS
call g2_gbytec(cbuf, field_number, mypos, INT2_BITS)
if (field_number .ne. 1) stop 42
print *, 'index_rec_len = ', index_rec_len, ' b2s_message8 = ', b2s_message8
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

! Clean up. Call gf_finalize or else index will be found in
!library buffer. Don't deallocate cbuf becuase gf_finalize() does
!that.
call gf_finalize(iret)
if (iret .ne. 0) stop 50
call baclose(lugb, iret)
if (iret .ne. 0) stop 51
end do

print *, 'SUCCESS!...'

end program test_getidx
3 changes: 1 addition & 2 deletions tests/test_getidx.F90
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ end subroutine getidx2

print *, 'Testing the getidx() subroutine - expect and ignore error messages during test...'

!do i = 1, 2
do i = 1, 1
do i = 1, 2
! Open a real GRIB2 file.
print *, 'Indexing a real GRIB2 file: ', TEST_FILE_WW3_WEST
call baopenr(lugb, TEST_FILE_WW3_WEST, iret)
Expand Down

0 comments on commit 7088a65

Please sign in to comment.