diff --git a/src/Model/GroundWaterFlow/gwf-lak.f90 b/src/Model/GroundWaterFlow/gwf-lak.f90 index 4a43f846399..419e8f7bc92 100644 --- a/src/Model/GroundWaterFlow/gwf-lak.f90 +++ b/src/Model/GroundWaterFlow/gwf-lak.f90 @@ -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, assign_iounit, openfile ! -- dummy class(LakType), intent(inout) :: this character(len=*), intent(inout) :: option @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(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)), & diff --git a/src/Model/GroundWaterFlow/gwf-maw.f90 b/src/Model/GroundWaterFlow/gwf-maw.f90 index f2dadcc1311..ec5e443ab40 100644 --- a/src/Model/GroundWaterFlow/gwf-maw.f90 +++ b/src/Model/GroundWaterFlow/gwf-maw.f90 @@ -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, assign_iounit, openfile ! -- dummy class(MawType), intent(inout) :: this character(len=*), intent(inout) :: option @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(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)), & diff --git a/src/Model/GroundWaterFlow/gwf-mvr.f90 b/src/Model/GroundWaterFlow/gwf-mvr.f90 index 39e61b06d17..f824b8d5822 100644 --- a/src/Model/GroundWaterFlow/gwf-mvr.f90 +++ b/src/Model/GroundWaterFlow/gwf-mvr.f90 @@ -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, assign_iounit, openfile ! -- dummy class(GwfMvrType) :: this ! -- local @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(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)), & diff --git a/src/Model/GroundWaterFlow/gwf-sfr.f90 b/src/Model/GroundWaterFlow/gwf-sfr.f90 index 93d21411b91..165ffe34032 100644 --- a/src/Model/GroundWaterFlow/gwf-sfr.f90 +++ b/src/Model/GroundWaterFlow/gwf-sfr.f90 @@ -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, assign_iounit, openfile ! -- dummy variables class(SfrType), intent(inout) :: this !< SfrType object character(len=*), intent(inout) :: option !< option keyword string @@ -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 assign_iounit(this%ibudgetout, this%inunit, "BUDGET fileout") call openfile(this%ibudgetout, this%iout, fname, 'DATA(BINARY)', & form, access, 'REPLACE', MNORMAL) write (this%iout, fmtsfrbin) & @@ -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 assign_iounit(this%ibudcsv, this%inunit, "BUDGETCSV fileout") call openfile(this%ibudcsv, this%iout, fname, 'CSV', & filstat_opt='REPLACE') write (this%iout, fmtsfrbin) & diff --git a/src/Model/GroundWaterFlow/gwf-uzf.f90 b/src/Model/GroundWaterFlow/gwf-uzf.f90 index 33f7ab1cd1e..184c925138a 100644 --- a/src/Model/GroundWaterFlow/gwf-uzf.f90 +++ b/src/Model/GroundWaterFlow/gwf-uzf.f90 @@ -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, assign_iounit, openfile implicit none ! -- dummy class(uzftype), intent(inout) :: this @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(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)), & diff --git a/src/Model/GroundWaterTransport/gwt-ist.f90 b/src/Model/GroundWaterTransport/gwt-ist.f90 index 16e834d4b8b..55d05fcdeaa 100644 --- a/src/Model/GroundWaterTransport/gwt-ist.f90 +++ b/src/Model/GroundWaterTransport/gwt-ist.f90 @@ -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, assign_iounit, openfile ! -- dummy class(GwtIstType), intent(inout) :: this !< GwtIstType object ! -- local @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(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)), & diff --git a/src/Model/TransportModel/tsp-apt.f90 b/src/Model/TransportModel/tsp-apt.f90 index ab9fdc81b1a..e8f03957c34 100644 --- a/src/Model/TransportModel/tsp-apt.f90 +++ b/src/Model/TransportModel/tsp-apt.f90 @@ -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, assign_iounit, openfile ! -- dummy class(TspAptType), intent(inout) :: this character(len=*), intent(inout) :: option @@ -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 assign_iounit(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', & @@ -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 assign_iounit(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', & diff --git a/src/Model/TransportModel/tsp-mvt.f90 b/src/Model/TransportModel/tsp-mvt.f90 index 86471b42fa0..7c7e9a56f22 100644 --- a/src/Model/TransportModel/tsp-mvt.f90 +++ b/src/Model/TransportModel/tsp-mvt.f90 @@ -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: assign_iounit, openfile ! -- dummy class(TspMvtType) :: this ! -- local @@ -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 assign_iounit(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)), & @@ -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 assign_iounit(this%ibudcsv, this%inunit, "BUDGETCSV fileout") call openfile(this%ibudcsv, this%iout, fname, 'CSV', & filstat_opt='REPLACE') write (this%iout, fmtflow) 'MVT', 'BUDGET CSV', & diff --git a/src/Utilities/InputOutput.f90 b/src/Utilities/InputOutput.f90 index aca425531aa..52e5ef86276 100644 --- a/src/Utilities/InputOutput.f90 +++ b/src/Utilities/InputOutput.f90 @@ -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, assign_iounit contains @@ -179,6 +179,25 @@ function getunit() getunit = iunit end function getunit + !> @ brief assign io unit number + !! + !! Generic method to assign io unit number to unassigned integer + !! variable (initialized less than or equal to 0). Assigns a valid + !! number if unassigned, otherwise sets a terminating error. + !< + subroutine assign_iounit(iounit, errunit, description) + integer(I4B), intent(inout) :: iounit !< iounit variable + 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 assign_iounit + !> @brief Convert to upper case !! !! Subroutine to convert a character string to upper case.