Skip to content

Commit

Permalink
add wrapper with check around getunit
Browse files Browse the repository at this point in the history
  • Loading branch information
mjreno authored and mjreno committed Nov 22, 2024
1 parent c8f846c commit dca17a9
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/Model/GroundWaterFlow/gwf-lak.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3093,7 +3093,7 @@ subroutine lak_options(this, option, found)
use ConstantsModule, only: MAXCHARLEN, DZERO, MNORMAL
use OpenSpecModule, only: access, form
use SimModule, only: store_error
use InputOutputModule, only: urword, getunit, openfile
use InputOutputModule, only: urword, getunit, check_assign_unit, openfile
! -- dummy
class(LakType), intent(inout) :: this
character(len=*), intent(inout) :: option
Expand Down Expand Up @@ -3140,7 +3140,7 @@ subroutine lak_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE', mode_opt=MNORMAL)
write (this%iout, fmtlakbin) 'BUDGET', trim(adjustl(fname)), &
Expand All @@ -3152,7 +3152,7 @@ subroutine lak_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtlakbin) 'BUDGET CSV', trim(adjustl(fname)), &
Expand Down
8 changes: 4 additions & 4 deletions src/Model/GroundWaterFlow/gwf-maw.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ end subroutine maw_mc
subroutine maw_read_options(this, option, found)
use ConstantsModule, only: MAXCHARLEN, DZERO, MNORMAL
use OpenSpecModule, only: access, form
use InputOutputModule, only: urword, getunit, openfile
use InputOutputModule, only: urword, check_assign_unit, openfile
! -- dummy
class(MawType), intent(inout) :: this
character(len=*), intent(inout) :: option
Expand Down Expand Up @@ -1674,7 +1674,7 @@ subroutine maw_read_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%iheadout = getunit()
call check_assign_unit(this%iheadout, this%inunit, "HEAD fileout")
call openfile(this%iheadout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE', mode_opt=MNORMAL)
write (this%iout, fmtmawbin) 'HEAD', trim(adjustl(fname)), &
Expand All @@ -1687,7 +1687,7 @@ subroutine maw_read_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE', mode_opt=MNORMAL)
write (this%iout, fmtmawbin) 'BUDGET', trim(adjustl(fname)), &
Expand All @@ -1700,7 +1700,7 @@ subroutine maw_read_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtmawbin) 'BUDGET CSV', trim(adjustl(fname)), &
Expand Down
6 changes: 3 additions & 3 deletions src/Model/GroundWaterFlow/gwf-mvr.f90
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ subroutine read_options(this)
use ConstantsModule, only: LINELENGTH, DZERO, DONE
use OpenSpecModule, only: access, form
use SimModule, only: store_error, store_error_unit
use InputOutputModule, only: urword, getunit, openfile
use InputOutputModule, only: urword, check_assign_unit, openfile
! -- dummy
class(GwfMvrType) :: this
! -- local
Expand Down Expand Up @@ -718,7 +718,7 @@ subroutine read_options(this)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE')
write (this%iout, fmtmvrbin) 'BUDGET', trim(adjustl(fname)), &
Expand All @@ -732,7 +732,7 @@ subroutine read_options(this)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtmvrbin) 'BUDGET CSV', trim(adjustl(fname)), &
Expand Down
6 changes: 3 additions & 3 deletions src/Model/GroundWaterFlow/gwf-sfr.f90
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ end subroutine sfr_read_dimensions
subroutine sfr_options(this, option, found)
! -- modules
use OpenSpecModule, only: access, form
use InputOutputModule, only: getunit, openfile
use InputOutputModule, only: getunit, check_assign_unit, openfile
! -- dummy variables
class(SfrType), intent(inout) :: this !< SfrType object
character(len=*), intent(inout) :: option !< option keyword string
Expand Down Expand Up @@ -761,7 +761,7 @@ subroutine sfr_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE', MNORMAL)
write (this%iout, fmtsfrbin) &
Expand All @@ -774,7 +774,7 @@ subroutine sfr_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtsfrbin) &
Expand Down
6 changes: 3 additions & 3 deletions src/Model/GroundWaterFlow/gwf-uzf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ subroutine uzf_options(this, option, found)
use ConstantsModule, only: DZERO, MNORMAL
use OpenSpecModule, only: access, form
use SimModule, only: store_error
use InputOutputModule, only: urword, getunit, openfile
use InputOutputModule, only: urword, getunit, check_assign_unit, openfile
implicit none
! -- dummy
class(uzftype), intent(inout) :: this
Expand Down Expand Up @@ -432,7 +432,7 @@ subroutine uzf_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE', mode_opt=MNORMAL)
write (this%iout, fmtuzfbin) 'BUDGET', trim(adjustl(fname)), &
Expand All @@ -444,7 +444,7 @@ subroutine uzf_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtuzfbin) 'BUDGET CSV', trim(adjustl(fname)), &
Expand Down
6 changes: 3 additions & 3 deletions src/Model/GroundWaterTransport/gwt-ist.f90
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ subroutine read_options(this)
use ConstantsModule, only: LINELENGTH, MNORMAL
use SimModule, only: store_error
use OpenSpecModule, only: access, form
use InputOutputModule, only: getunit, openfile
use InputOutputModule, only: getunit, check_assign_unit, openfile
! -- dummy
class(GwtIstType), intent(inout) :: this !< GwtIstType object
! -- local
Expand Down Expand Up @@ -1027,7 +1027,7 @@ subroutine read_options(this)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE', mode_opt=MNORMAL)
write (this%iout, fmtistbin) 'BUDGET', trim(adjustl(fname)), &
Expand All @@ -1041,7 +1041,7 @@ subroutine read_options(this)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtistbin) 'BUDGET CSV', trim(adjustl(fname)), &
Expand Down
6 changes: 3 additions & 3 deletions src/Model/TransportModel/tsp-apt.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ end subroutine find_apt_package
subroutine apt_options(this, option, found)
use ConstantsModule, only: MAXCHARLEN, DZERO
use OpenSpecModule, only: access, form
use InputOutputModule, only: urword, getunit, openfile
use InputOutputModule, only: urword, getunit, check_assign_unit, openfile
! -- dummy
class(TspAptType), intent(inout) :: this
character(len=*), intent(inout) :: option
Expand Down Expand Up @@ -1365,7 +1365,7 @@ subroutine apt_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE')
write (this%iout, fmtaptbin) trim(adjustl(this%text)), 'BUDGET', &
Expand All @@ -1377,7 +1377,7 @@ subroutine apt_options(this, option, found)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtaptbin) trim(adjustl(this%text)), 'BUDGET CSV', &
Expand Down
6 changes: 3 additions & 3 deletions src/Model/TransportModel/tsp-mvt.f90
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ end subroutine allocate_scalars
subroutine read_options(this)
! -- modules
use OpenSpecModule, only: access, form
use InputOutputModule, only: getunit, openfile
use InputOutputModule, only: check_assign_unit, openfile
! -- dummy
class(TspMvtType) :: this
! -- local
Expand Down Expand Up @@ -611,7 +611,7 @@ subroutine read_options(this)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudgetout = getunit()
call check_assign_unit(this%ibudgetout, this%inunit, "BUDGET fileout")
call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', &
form, access, 'REPLACE')
write (this%iout, fmtflow) 'MVT', 'BUDGET', trim(adjustl(fname)), &
Expand All @@ -624,7 +624,7 @@ subroutine read_options(this)
call this%parser%GetStringCaps(keyword)
if (keyword == 'FILEOUT') then
call this%parser%GetString(fname)
this%ibudcsv = getunit()
call check_assign_unit(this%ibudcsv, this%inunit, "BUDGETCSV fileout")
call openfile(this%ibudcsv, this%iout, fname, 'CSV', &
filstat_opt='REPLACE')
write (this%iout, fmtflow) 'MVT', 'BUDGET CSV', &
Expand Down
23 changes: 22 additions & 1 deletion src/Utilities/InputOutput.f90
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module InputOutputModule
linear_interpolate, lowcase, read_line, GetFileFromPath, &
extract_idnum_or_bndname, urdaux, print_format, BuildFixedFormat, &
BuildFloatFormat, BuildIntFormat, fseek_stream, get_nwords, &
u9rdcom, append_processor_id
u9rdcom, append_processor_id, check_assign_unit

contains

Expand Down Expand Up @@ -179,6 +179,27 @@ function getunit()
getunit = iunit
end function getunit

!> @ brief check assign unit
!!
!! Generic method to verify that an integer variable for storing
!! a file io unit number is unassigned. Assumes unit number is
!! unassigned and initialized if less than or equal to 0. Assigns
!! a valid number if unassigned, otherwise throws an error and exits.
!!
!<
subroutine check_assign_unit(iounit, errunit, description)
integer(I4B), intent(inout) :: iounit !< iounit to assign
integer(I4B), intent(in) :: errunit !< input file inunit for error assignment
character(len=*), intent(in) :: description !< usage description for iounit
if (iounit > 0) then
write (errmsg, '(a,1x,i0)') &
trim(description)//' already assigned at unit: ', iounit
call store_error(errmsg)
call store_error_unit(errunit)
end if
iounit = getunit()
end subroutine check_assign_unit

!> @brief Convert to upper case
!!
!! Subroutine to convert a character string to upper case.
Expand Down

0 comments on commit dca17a9

Please sign in to comment.