@@ -29,6 +29,7 @@ module nc4fortran
2929integer :: comp_lvl = 0 ! < compression level (1-9) 0: disable compression
3030logical :: verbose = .false.
3131logical :: debug = .false.
32+ logical :: is_open = .false.
3233logical :: is_scratch = .false.
3334! ! will be auto-deleted on close
3435character (80 ) :: libversion
@@ -222,6 +223,11 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
222223character (:), allocatable :: lstatus, laction
223224integer :: ier
224225
226+ if (self% is_open) then
227+ write (stderr,* ) ' WARNING:nc4fortran: file handle already open to: ' // filename
228+ return
229+ endif
230+
225231self% filename = filename
226232
227233if (present (comp_lvl)) self% comp_lvl = comp_lvl
@@ -270,11 +276,13 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
270276 error stop
271277endif
272278
279+ self% is_open = .true.
280+
273281end subroutine nc_initialize
274282
275283
276284subroutine nc_finalize (self , ierr )
277- class(netcdf_file), intent (in ) :: self
285+ class(netcdf_file), intent (inout ) :: self
278286integer , intent (out ), optional :: ierr
279287
280288integer :: ier
@@ -291,6 +299,8 @@ subroutine nc_finalize(self, ierr)
291299 if (unlink(self% filename)) write (stderr,* ) ' WARNING: could not delete scratch file: ' // self% filename
292300endif
293301
302+ self% is_open = .false.
303+
294304end subroutine nc_finalize
295305
296306
0 commit comments