diff --git a/src/interface.f90 b/src/interface.f90 index d614bbd..a2f1ddd 100644 --- a/src/interface.f90 +++ b/src/interface.f90 @@ -224,7 +224,7 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu integer :: ier if (self%is_open) then - write(stderr,*) 'WARNING:nc4fortran: file handle already open to: '// filename + write(stderr,*) 'WARNING:nc4fortran:initialize file handle already open to: '// filename return endif @@ -287,6 +287,11 @@ subroutine nc_finalize(self, ierr) integer :: ier +if(.not. self%is_open) then + write(stderr,*) 'WARNING:nc4fortran:finalize file handle is not open' + return +endif + ier = nf90_close(self%ncid) if (present(ierr)) ierr = ier if (ier /= NF90_NOERR) then diff --git a/src/read.f90 b/src/read.f90 index bb88106..ce93a89 100644 --- a/src/read.f90 +++ b/src/read.f90 @@ -11,6 +11,9 @@ integer :: i, tempdims(NC_MAXDIM), N, ier character(NF90_MAX_NAME) :: tempnames(NC_MAXDIM) + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:read: file handle not open' + N = 0 do i = 1,NC_MAXDIM ier = nf90_inquire_dimension(self%ncid, dimid=i, name=tempnames(i), len=tempdims(i)) @@ -28,9 +31,17 @@ module procedure nc_check_exist integer :: varid, ierr -ierr = nf90_inq_varid(self%ncid, dname, varid) exists = .false. + +if(.not.self%is_open) then + write(stderr,*) 'WARNING:nc4fortran:read: file handle not open ' // self%filename + return +endif + +ierr = nf90_inq_varid(self%ncid, dname, varid) + + select case (ierr) case (NF90_NOERR) exists = .true. diff --git a/src/reader.f90 b/src/reader.f90 index 5c06cfe..9e34b34 100644 --- a/src/reader.f90 +++ b/src/reader.f90 @@ -7,6 +7,9 @@ module procedure nc_read_scalar integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -36,6 +39,9 @@ module procedure nc_read_1d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -65,6 +71,9 @@ module procedure nc_read_2d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -93,6 +102,9 @@ module procedure nc_read_3d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -121,6 +133,9 @@ module procedure nc_read_4d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -149,6 +164,9 @@ module procedure nc_read_5d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -177,6 +195,9 @@ module procedure nc_read_6d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then @@ -205,6 +226,9 @@ module procedure nc_read_7d integer :: varid, ier + +if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ier == NF90_NOERR) then diff --git a/src/write.f90 b/src/write.f90 index 8d883ec..28a0cf9 100644 --- a/src/write.f90 +++ b/src/write.f90 @@ -10,6 +10,8 @@ integer :: i, L character(NF90_MAX_NAME) :: name +if(.not.self%is_open) error stop 'ERROR:nc4fortran:write: file handle not open' + do i=1,size(dims) if (present(dimnames)) then ierr = nf90_inq_dimid(self%ncid, dimnames(i), dimids(i)) @@ -34,6 +36,8 @@ module procedure write_attribute integer :: varid, ier +if(.not.self%is_open) error stop 'ERROR:nc4fortran:write: file handle not open' + ier = nf90_inq_varid(self%ncid, dname, varid) if(ierr == nf90_noerr) ier = nf90_put_att(self%ncid, varid, attrname, value) diff --git a/src/writer.f90 b/src/writer.f90 index 5b7f020..e20579c 100644 --- a/src/writer.f90 +++ b/src/writer.f90 @@ -8,6 +8,8 @@ module procedure nc_write_scalar integer :: varid, ier +if(.not.self%is_open) error stop 'ERROR:nc4fortran:writer: file handle not open' + select type (value) type is (real(real64)) ier = nf90_def_var(self%ncid, dname, NF90_DOUBLE, varid=varid)