Skip to content

Commit 0ab8d66

Browse files
committed
add is_open property
1 parent 458d45e commit 0ab8d66

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/interface.f90

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module nc4fortran
2929
integer :: comp_lvl = 0 !< compression level (1-9) 0: disable compression
3030
logical :: verbose = .false.
3131
logical :: debug = .false.
32+
logical :: is_open = .false.
3233
logical :: is_scratch = .false.
3334
!! will be auto-deleted on close
3435
character(80) :: libversion
@@ -222,6 +223,11 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
222223
character(:), allocatable :: lstatus, laction
223224
integer :: ier
224225

226+
if (self%is_open) then
227+
write(stderr,*) 'WARNING:nc4fortran: file handle already open to: '// filename
228+
return
229+
endif
230+
225231
self%filename = filename
226232

227233
if (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
271277
endif
272278

279+
self%is_open = .true.
280+
273281
end subroutine nc_initialize
274282

275283

276284
subroutine nc_finalize(self, ierr)
277-
class(netcdf_file), intent(in) :: self
285+
class(netcdf_file), intent(inout) :: self
278286
integer, intent(out), optional :: ierr
279287

280288
integer :: 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
292300
endif
293301

302+
self%is_open = .false.
303+
294304
end subroutine nc_finalize
295305

296306

src/tests/test_io.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ subroutine test_exist(path)
4141

4242
if (hf%exist('foovar')) error stop 'variable and file not exists and not opened'
4343

44+
if(hf%is_open) error stop 'file not opened yet'
4445
call hf%initialize(path // '/scalar.nc', status='replace')
46+
if (.not.hf%is_open) error stop 'file not detected as open'
4547
call hf%write('here', 12)
4648
if(.not. hf%exist('here')) error stop 'variable exists'
4749
if(hf%exist('nothere')) error stop 'variable does not actually exist'

0 commit comments

Comments
 (0)