diff --git a/src/io/io.f90 b/src/io/io.f90 index d6adf265d..89b0a5988 100644 --- a/src/io/io.f90 +++ b/src/io/io.f90 @@ -404,7 +404,7 @@ module function io_get_old_t_final_system(self, param) result(old_t_final) class(swiftest_nbody_system), allocatable :: tmpsys class(swiftest_parameters), allocatable :: tmpparam integer(I4B) :: ierr, iu = LUN - character(len=STRMAX) :: errmsg + character(len=STRMAX) :: errmsg old_t_final = 0.0_DP allocate(tmpsys, source=self) @@ -2105,26 +2105,17 @@ module subroutine io_write_frame_system(self, param) errmsg = param%outfile // " not found! You must specify OUT_STAT = NEW, REPLACE, or UNKNOWN" goto 667 end if + call param%nciu%open(param) case('NEW') if (fileExists) then - errmsg = param%outfile // " Alread Exists! You must specify OUT_STAT = OLD, REPLACE, or UNKNOWN" + errmsg = param%outfile // " Alread Exists! You must specify OUT_STAT = APPEND, REPLACE, or UNKNOWN" goto 667 end if + call param%nciu%initialize(param) case('REPLACE', 'UNKNOWN') - if (fileExists) then - open(file=param%outfile, unit=iu, status='OLD') - close (unit=BINUNIT, status="delete") - end if - end select - - select case(param%out_stat) - case('APPEND') - call param%nciu%open(param) - case('NEW', 'REPLACE', 'UNKNOWN') call param%nciu%initialize(param) - call param%nciu%close(param) - call param%nciu%open(param) end select + lfirst = .false. else call param%nciu%open(param) diff --git a/src/netcdf/netcdf.f90 b/src/netcdf/netcdf.f90 index ac512ed6a..d97f9169e 100644 --- a/src/netcdf/netcdf.f90 +++ b/src/netcdf/netcdf.f90 @@ -78,14 +78,17 @@ module subroutine netcdf_initialize_output(self, param) integer(I4B) :: old_mode, nvar, varid, vartype, old_unit real(DP) :: dfill real(SP) :: sfill + logical :: fileExists + character(len=STRMAX) :: errmsg dfill = ieee_value(dfill, IEEE_QUIET_NAN) sfill = ieee_value(sfill, IEEE_QUIET_NAN) - !! Create the new output file, deleting any previously existing output file of the same name - if (any(DUMP_NC_FILE == param%outfile)) then - open(file=param%outfile, unit=old_unit, status='OLD') - close(unit=old_unit, status='delete') + ! Check if the file exists, and if it does, delete it + inquire(file=param%outfile, exist=fileExists) + if (fileExists) then + open(unit=LUN, file=param%outfile, status="old", err=667, iomsg=errmsg) + close(unit=LUN, status="delete") end if call check( nf90_create(param%outfile, NF90_NETCDF4, self%ncid) ) @@ -198,6 +201,10 @@ module subroutine netcdf_initialize_output(self, param) call check( nf90_enddef(self%ncid) ) return + + 667 continue + write(*,*) "Error creating NetCDF output file. " // trim(adjustl(errmsg)) + call util_exit(FAILURE) end subroutine netcdf_initialize_output