Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Added a flag to indicate whether a NetCDF file is open so that finali…
Browse files Browse the repository at this point in the history
…zers don't throw errors
  • Loading branch information
daminton committed Dec 21, 2022
1 parent 845354b commit d8be095
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/collision/collision_io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/encounter/encounter_io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/netcdf_io/netcdf_io_implementations.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/netcdf_io/netcdf_io_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/swiftest/swiftest_io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" )
Expand Down

0 comments on commit d8be095

Please sign in to comment.