Skip to content

Commit

Permalink
fix(array reader): store error for unexpected binary input file size (#…
Browse files Browse the repository at this point in the history
…2049)

* add error for unexpected binary input array size

* fprettify

* set error based on expected binary file size

* remove duplicate header write in binaryinput test

* make case consistent

* consolidate file size check

---------

Co-authored-by: mjreno <[email protected]>
  • Loading branch information
mjreno and mjreno authored Nov 21, 2024
1 parent c29763a commit f828bc6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
2 changes: 0 additions & 2 deletions autotest/test_gwf_utl01_binaryinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ def build_models(idx, test):
kstp=1,
kper=1,
)
header.tofile(f)
flopy.utils.Util2d.write_bin(
(nrow, ncol),
f,
Expand Down Expand Up @@ -372,7 +371,6 @@ def build_models(idx, test):
kstp=1,
kper=1,
)
header.tofile(f)
flopy.utils.Util2d.write_bin(
(nrow, ncol),
f,
Expand Down
8 changes: 7 additions & 1 deletion src/Utilities/ArrayRead/Double1dReader.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Double1dReaderModule
use BlockParserModule, only: BlockParserType
use SimVariablesModule, only: errmsg
use SimModule, only: store_error, store_error_unit
use ArrayReadersModule, only: read_binary_header
use ArrayReadersModule, only: read_binary_header, &
check_binary_filesize, &
BINARY_DOUBLE_BYTES, &
BINARY_HEADER_BYTES
use ArrayReaderBaseModule, only: ArrayReaderBaseType

implicit none
Expand Down Expand Up @@ -87,7 +90,10 @@ subroutine read_binary(this)
integer(I4B) :: i
integer(I4B) :: nvals
integer(I4B) :: istat
integer(I4B) :: expected_size
expected_size = BINARY_HEADER_BYTES + (size(this%dbl1d) * BINARY_DOUBLE_BYTES)
call read_binary_header(this%input_unit, this%iout, this%array_name, nvals)
call check_binary_filesize(this%input_unit, expected_size, this%array_name)
read (this%input_unit, iostat=istat, iomsg=errmsg) &
(this%dbl1d(i), i=1, size(this%dbl1d))
if (istat /= 0) then
Expand Down
8 changes: 7 additions & 1 deletion src/Utilities/ArrayRead/Double2dReader.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Double2dReaderModule
use BlockParserModule, only: BlockParserType
use SimVariablesModule, only: errmsg
use SimModule, only: store_error, store_error_unit
use ArrayReadersModule, only: read_binary_header
use ArrayReadersModule, only: read_binary_header, &
check_binary_filesize, &
BINARY_DOUBLE_BYTES, &
BINARY_HEADER_BYTES
use ArrayReaderBaseModule, only: ArrayReaderBaseType

implicit none
Expand Down Expand Up @@ -91,7 +94,10 @@ subroutine read_binary(this)
integer(I4B) :: i, j
integer(I4B) :: nvals
integer(I4B) :: istat
integer(I4B) :: expected_size
expected_size = BINARY_HEADER_BYTES + (size(this%dbl2d) * BINARY_DOUBLE_BYTES)
call read_binary_header(this%input_unit, this%iout, this%array_name, nvals)
call check_binary_filesize(this%input_unit, expected_size, this%array_name)
read (this%input_unit, iostat=istat, iomsg=errmsg) &
((this%dbl2d(j, i), j=1, size(this%dbl2d, dim=1)), &
i=1, size(this%dbl2d, dim=2))
Expand Down
8 changes: 7 additions & 1 deletion src/Utilities/ArrayRead/Integer1dReader.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module Integer1dReaderModule
use BlockParserModule, only: BlockParserType
use SimVariablesModule, only: errmsg
use SimModule, only: store_error, store_error_unit
use ArrayReadersModule, only: read_binary_header
use ArrayReadersModule, only: read_binary_header, &
check_binary_filesize, &
BINARY_INT_BYTES, &
BINARY_HEADER_BYTES
use ArrayReaderBaseModule, only: ArrayReaderBaseType

implicit none
Expand Down Expand Up @@ -119,7 +122,10 @@ subroutine read_binary(this)
integer(I4B) :: i
integer(I4B) :: nvals
integer(I4B) :: istat
integer(I4B) :: expected_size
expected_size = BINARY_HEADER_BYTES + (size(this%int1d) * BINARY_INT_BYTES)
call read_binary_header(this%input_unit, this%iout, this%array_name, nvals)
call check_binary_filesize(this%input_unit, expected_size, this%array_name)
read (this%input_unit, iostat=istat, iomsg=errmsg) &
(this%int1d(i), i=1, size(this%int1d))
if (istat /= 0) then
Expand Down
8 changes: 7 additions & 1 deletion src/Utilities/ArrayRead/Integer2dReader.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ module Integer2dReaderModule
use BlockParserModule, only: BlockParserType
use SimVariablesModule, only: errmsg
use SimModule, only: store_error, store_error_unit
use ArrayReadersModule, only: read_binary_header
use ArrayReadersModule, only: read_binary_header, &
check_binary_filesize, &
BINARY_INT_BYTES, &
BINARY_HEADER_BYTES
use ArrayReaderBaseModule, only: ArrayReaderBaseType

implicit none
Expand Down Expand Up @@ -91,7 +94,10 @@ subroutine read_binary(this)
integer(I4B) :: i, j
integer(I4B) :: nvals
integer(I4B) :: istat
integer(I4B) :: expected_size
expected_size = BINARY_HEADER_BYTES + (size(this%int2d) * BINARY_INT_BYTES)
call read_binary_header(this%input_unit, this%iout, this%array_name, nvals)
call check_binary_filesize(this%input_unit, expected_size, this%array_name)
read (this%input_unit, iostat=istat, iomsg=errmsg) &
((this%int2d(j, i), j=1, size(this%int2d, dim=1)), &
i=1, size(this%int2d, dim=2))
Expand Down
35 changes: 34 additions & 1 deletion src/Utilities/ArrayReaders.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ module ArrayReadersModule
private
public :: ReadArray
public :: read_binary_header
public :: check_binary_filesize
public :: BINARY_INT_BYTES
public :: BINARY_DOUBLE_BYTES
public :: BINARY_HEADER_BYTES

integer(I4B), parameter :: BINARY_CHAR_BYTES = 1
integer(I4B), parameter :: BINARY_INT_BYTES = 4
integer(I4B), parameter :: BINARY_DOUBLE_BYTES = 8
integer(I4B), parameter :: BINARY_STRLEN = 16
integer(I4B), parameter :: BINARY_HEADER_BYTES = &
(5 * BINARY_INT_BYTES) + & !< kstp, kper, msize1, msize2, msize3
(2 * BINARY_DOUBLE_BYTES) + & !< pertim, totim
(BINARY_STRLEN * BINARY_CHAR_BYTES) !< array text

interface ReadArray
module procedure &
Expand Down Expand Up @@ -1023,7 +1036,7 @@ subroutine read_binary_header(locat, iout, arrname, nval)
integer(I4B) :: istat
integer(I4B) :: kstp, kper, m1, m2, m3
real(DP) :: pertim, totim
character(len=16) :: text
character(len=BINARY_STRLEN) :: text
character(len=MAXCHARLEN) :: ermsgr
character(len=*), parameter :: fmthdr = &
"(/,1X,'HEADER FROM BINARY FILE HAS FOLLOWING ENTRIES',&
Expand Down Expand Up @@ -1053,6 +1066,26 @@ subroutine read_binary_header(locat, iout, arrname, nval)
nval = m1 * m2
end subroutine read_binary_header

subroutine check_binary_filesize(locat, expected_size, arrname)
! -- dummy
integer(I4B), intent(in) :: locat
integer(I4B), intent(in) :: expected_size
character(len=*), intent(in) :: arrname
! -- local
integer(I4B) :: file_size
!
inquire (unit=locat, size=file_size)
!
if (expected_size /= file_size) then
write (errmsg, '(a,i0,a,i0,a)') &
'Unexpected file size for binary input array '// &
trim(arrname)//'. Expected=', expected_size, &
'/Found=', file_size, ' bytes.'
call store_error(errmsg)
call store_error_unit(locat)
end if
end subroutine check_binary_filesize

!> @ brief Check the binary data size
!!
!! Check the size of the binary data that will be read
Expand Down

0 comments on commit f828bc6

Please sign in to comment.