diff --git a/src/collision/collision_io.f90 b/src/collision/collision_io.f90 index 04fac5524..5a7ead8c0 100644 --- a/src/collision/collision_io.f90 +++ b/src/collision/collision_io.f90 @@ -97,6 +97,7 @@ module subroutine collision_netcdf_io_initialize_output(self, param) end if call netcdf_io_check( nf90_create(nc%file_name, NF90_NETCDF4, nc%id), "collision_netcdf_io_initialize_output nf90_create" ) + nc%lfile_is_open = .true. ! Dimensions call netcdf_io_check( nf90_def_dim(nc%id, nc%event_dimname, nc%event_dimsize, nc%event_dimid), "collision_netcdf_io_initialize_output nf90_def_dim event_dimid" ) ! Dimension to store individual collision events diff --git a/src/encounter/encounter_io.f90 b/src/encounter/encounter_io.f90 index 2f7534b2e..46851cf2c 100644 --- a/src/encounter/encounter_io.f90 +++ b/src/encounter/encounter_io.f90 @@ -92,6 +92,7 @@ module subroutine encounter_netcdf_io_initialize_output(self, param) end if call netcdf_io_check( nf90_create(nc%file_name, NF90_NETCDF4, nc%id), "encounter_netcdf_io_initialize_output nf90_create" ) + nc%lfile_is_open = .true. ! Dimensions call netcdf_io_check( nf90_def_dim(nc%id, nc%time_dimname, nc%time_dimsize, nc%time_dimid), "encounter_netcdf_io_initialize_output nf90_def_dim time_dimid" ) ! Simulation time dimension diff --git a/src/netcdf_io/netcdf_io_implementations.f90 b/src/netcdf_io/netcdf_io_implementations.f90 index d93b3dfb6..9614e2a32 100644 --- a/src/netcdf_io/netcdf_io_implementations.f90 +++ b/src/netcdf_io/netcdf_io_implementations.f90 @@ -40,7 +40,10 @@ module subroutine netcdf_io_close(self) ! Arguments class(netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset - call netcdf_io_check( nf90_close(self%id), "netcdf_io_close" ) + if (self%lfile_is_open) then + call netcdf_io_check( nf90_close(self%id), "netcdf_io_close" ) + self%lfile_is_open = .false. + end if return end subroutine netcdf_io_close diff --git a/src/netcdf_io/netcdf_io_module.f90 b/src/netcdf_io/netcdf_io_module.f90 index bb3f098b8..cea8ca147 100644 --- a/src/netcdf_io/netcdf_io_module.f90 +++ b/src/netcdf_io/netcdf_io_module.f90 @@ -21,6 +21,7 @@ module netcdf_io !! This derived datatype stores the NetCDF ID values for each of the variables included in the NetCDF data file. This is used as the base class defined in base type, abstract :: netcdf_parameters character(STRMAX) :: file_name !! Name of the output file + logical :: lfile_is_open = .false. !! Flag indicating that the linked file is currently open integer(I4B) :: out_type !! output type (will be assigned either NF90_DOUBLE or NF90_FLOAT, depending on the user parameter) integer(I4B) :: id !! ID for the output file integer(I4B) :: name_chunk !! Chunk size for the id dimension variables diff --git a/src/swiftest/swiftest_io.f90 b/src/swiftest/swiftest_io.f90 index 0f6dec806..a3fa11e11 100644 --- a/src/swiftest/swiftest_io.f90 +++ b/src/swiftest/swiftest_io.f90 @@ -614,6 +614,7 @@ module subroutine swiftest_io_netcdf_initialize_output(self, param) ! Create the file call netcdf_io_check( nf90_create(nc%file_name, NF90_NETCDF4, nc%id), "netcdf_io_initialize_output nf90_create" ) + nc%lfile_is_open = .true. ! Dimensions call netcdf_io_check( nf90_def_dim(nc%id, nc%time_dimname, NF90_UNLIMITED, nc%time_dimid), "netcdf_io_initialize_output nf90_def_dim time_dimid" ) ! Simulation time dimension @@ -768,6 +769,7 @@ module subroutine swiftest_io_netcdf_open(self, param, readonly) write(errmsg,*) "netcdf_io_open nf90_open ",trim(adjustl(nc%file_name)) call netcdf_io_check( nf90_open(nc%file_name, mode, nc%id), errmsg) + self%lfile_is_open = .true. ! Dimensions call netcdf_io_check( nf90_inq_dimid(nc%id, nc%time_dimname, nc%time_dimid), "netcdf_io_open nf90_inq_dimid time_dimid" )