Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change DRS location in index to 8-byte value #691

Closed
wants to merge 9 commits into from
79 changes: 53 additions & 26 deletions src/g2index.F90
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ subroutine g2_write_index_headers(lugi, nlen, nnum, idxver, filename)

integer, intent(in) :: lugi, nlen, nnum, idxver
character, intent(in) :: filename*(*)

character cd8*8, ct10*10, hostname*15
#ifdef __GFORTRAN__
integer istat
Expand Down Expand Up @@ -270,7 +270,7 @@ subroutine getidx2(lugb, lugi, idxver, cindex, nlen, nnum, iret)
integer, intent(inout) :: idxver
character(len = 1), pointer, dimension(:) :: cindex
integer, intent(out) :: nlen, nnum, iret

integer, parameter :: maxidx = 10000
integer (kind = 8), parameter :: msk1 = 32000_8, msk2 = 4000_8
integer :: lux
Expand Down Expand Up @@ -428,7 +428,7 @@ end subroutine getidx2
!> @author Mark Iredell, Ed Hartnett @date 2000-05-26
subroutine getg2i(lugi, cbuf, nlen, nnum, iret)
implicit none

integer, intent(in) :: lugi
character(len=1), pointer, dimension(:) :: cbuf
integer, intent(out) :: nlen, nnum, iret
Expand All @@ -449,7 +449,7 @@ end subroutine getg2i2
if (idxver .eq. 2) iret = 5

end subroutine getg2i

!> Read a version 1 or 2 index file and return its contents.
!>
!> The index file may be generated by the grb2index utility of the
Expand Down Expand Up @@ -511,11 +511,11 @@ end subroutine getg2i
subroutine getg2i2(lugi, cbuf, idxver, nlen, nnum, iret)
use g2logging
implicit none

integer, intent(in) :: lugi
character(len=1), pointer, dimension(:) :: cbuf
integer, intent(out) :: idxver, nlen, nnum, iret

character chead*162
integer :: ios, istat, lbuf, lhead, nskp

Expand Down Expand Up @@ -588,7 +588,7 @@ subroutine getg2ir(lugb, msk1, msk2, mnum, cbuf, nlen, nnum, nmess, iret)
integer, intent(out) :: nlen, nnum, nmess, iret

integer (kind = 8) :: msk1_8, msk2_8

interface
subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, &
nlen, nnum, nmess, iret)
Expand All @@ -604,7 +604,7 @@ end subroutine getg2i2r
msk2_8 = msk2
call getg2i2r(lugb, msk1_8, msk2_8, mnum, 1, cbuf, nlen, nnum, nmess, iret)
end subroutine getg2ir

!> Generate a version 1 or 2 index record for each message in a GRIB2
!> file.
!>
Expand Down Expand Up @@ -648,7 +648,7 @@ subroutine getg2i2r(lugb, msk1, msk2, mnum, idxver, cbuf, nlen, nnum, nmess, ire
integer, intent(in) :: mnum, idxver
character(len = 1), pointer, dimension(:) :: cbuf
integer, intent(out) :: nlen, nnum, nmess, iret

character(len = 1), pointer, dimension(:) :: cbuftmp
integer :: nbytes, newsize, next, numfld, m, mbuf
integer (kind = 8) :: iseek, lskip, lgrib
Expand Down Expand Up @@ -1258,7 +1258,7 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret)
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
integer (kind = 8) :: loclus8, locgds8
Expand All @@ -1284,19 +1284,23 @@ subroutine ix2gb2(lugb, lskip8, idxver, lgrib8, cbuf, numfld, mlen, iret)
! Number of bytes of the BMS section put into index record.
integer :: MXBMS
parameter(MXBMS = 6)
integer :: IXDS
integer :: IXDS1, IXDS2
parameter(IXDS1 = 28, IXDS2 = 44)
! Bytes to skip in (version 1) index record to get to section 0.
integer :: IXIDS
parameter(IXIDS = 44)
integer :: IXSDR
parameter(IXSDR = 20, IXDS = 28)
! Bytes to skip in (version 1) index record to get to bms.
integer :: IXBMS
parameter(IXBMS = 24)
parameter(IXSDR = 20)
! Bytes to skip in (version 1 and 2) index record to get to bms.
integer :: IXBMS1, IXBMS2, ixbms
parameter(IXBMS1 = 24, IXBMS2 = 40)
! Sizes of integers in bits.
integer :: INT1_BITS, INT2_BITS, INT4_BITS, INT8_BITS
parameter(INT1_BITS = 8, INT2_BITS = 16, INT4_BITS = 32, INT8_BITS = 64)

! Location of bytes to drs field in index version 1 and 2.
integer :: IXDRS1, IXDRS2
parameter(IXDRS1 = 20, IXDRS2 = 36)

! Buffers.
character cbread(LINMAX), cindex(LINMAX)
character cids(LINMAX), cgds(LINMAX)
Expand Down Expand Up @@ -1516,12 +1520,25 @@ end subroutine g2_gbytec1
endif
!print *, 'pds:', lindex, lindex + ilnpds
lindex = lindex + ilnpds
mypos = mypos + ilnpds
#ifdef LOGGING
write(g2_log_msg, *) ' after writing pds location to index: mypos/8 ', mypos/8
call g2_log(3)
#endif
elseif (numsec .eq. 5) then
! Write the byte offset to the DRS section into the cindex buffer.
mypos = (IXSDR + inc) * INT1_BITS
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), mypos, INT4_BITS) ! location of drs
!mypos = (IXSDR + inc) * INT1_BITS
#ifdef LOGGING
write(g2_log_msg, *) ' before writing drs to index: mypos/8 ', mypos/8
call g2_log(3)
#endif
if (idxver .eq. 1) then
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), IXDRS1 * INT1_BITS, INT4_BITS) ! location of drs
else
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(8)), IXDRS2 * INT1_BITS, INT4_BITS) ! location of drs
endif
!print '(i3, a8, i5)', mypos/8, ' locdrs ', int(ibskip8 - lskip8, kind(4))

! Read the DRS section directly into the cindex buffer.
ilndrs = lensec
ilndrs8 = ilndrs
Expand All @@ -1532,19 +1549,25 @@ end subroutine g2_gbytec1
endif
!print *, 'drs:', lindex, lindex + ilndrs
lindex = lindex + ilndrs
elseif (numsec .eq. 6) then
elseif (numsec .eq. 6) then
! Based on the index version, determine where the BMS offset
! is in the index record.
if (idxver .eq. 1) then
ixbms = IXBMS1 * INT1_BITS
else
ixbms = IXBMS2 * INT1_BITS
endif
! Write the location of the BMS section in the message into
! the cindex buffer.
indbmp = g2_mova2i(cbread(6))
mypos = (IXBMS + inc) * INT1_BITS
if (indbmp .lt. 254) then
locbms = int(ibskip8 - lskip8, kind(4))
call g2_sbytec(cindex, locbms, mypos, INT4_BITS) ! loc. of bms
call g2_sbytec(cindex, locbms, ixbms, INT4_BITS) ! loc. of bms
!print '(i3, a8, i5)', mypos/8, ' locbms ', int(ibskip8 - lskip8, kind(4))
elseif (indbmp .eq. 254) then
call g2_sbytec(cindex, locbms, mypos, INT4_BITS) ! loc. of bms
call g2_sbytec(cindex, locbms, ixbms, INT4_BITS) ! loc. of bms
elseif (indbmp .eq. 255) then
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), mypos, INT4_BITS) ! loc. of bms
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), ixbms, INT4_BITS) ! loc. of bms
endif

! Copy 6 bytes of the BMS from data buffer to the cindex buffer.
Expand All @@ -1561,8 +1584,12 @@ end subroutine g2_gbytec1
!print '(i3, a8, i5)', 0, ' lindex ', lindex
elseif (numsec .eq. 7) then ! found data section
! Write the offset to the data section in the cindex buffer.
mypos = (IXDS + inc) * INT1_BITS
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), mypos, INT4_BITS) ! loc. of data sec.
if (idxver .eq. 1) then
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), IXDS1 * INT1_BITS, INT4_BITS) ! loc. of data sec.
else
call g2_sbytec(cindex, int(ibskip8 - lskip8, kind(4)), IXDS2 * INT1_BITS, INT4_BITS) ! loc. of data sec.
endif

!print '(i3, a8, i5)', mypos/8, ' locdata ', int(ibskip8 - lskip8, kind(4))

! Increment the field count.
Expand Down
Loading