Skip to content

Commit b00180f

Browse files
committed
make mode handling more robust, correct test error
1 parent 1d13869 commit b00180f

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if(NOT CMAKE_BUILD_TYPE)
44
endif()
55
project(nc4fortran
66
LANGUAGES Fortran
7-
VERSION 0.4.4
7+
VERSION 0.5.0
88
DESCRIPTION "thin, light object-oriented NetCDF4 Fortran interface"
99
HOMEPAGE_URL https://github.com/geospace-code/nc4fortran)
1010

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project('nc4fortran', 'fortran',
22
meson_version : '>=0.52.0',
3-
version: '0.4.4',
3+
version: '0.5.0',
44
default_options : ['default_library=static', 'buildtype=release', 'warning_level=3'])
55

66
subdir('meson')

src/interface.f90

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ module nc4fortran
22
!! NetCDF4 object-oriented polymorphic interface
33
use, intrinsic :: iso_c_binding, only : c_ptr, c_loc
44
use, intrinsic :: iso_fortran_env, only : real32, real64, int32, int64, stderr=>error_unit
5-
use netcdf
5+
6+
use netcdf, only : nf90_create, nf90_open, NF90_CLOBBER, NF90_NETCDF4, NF90_MAX_NAME, &
7+
NF90_NOERR, NF90_EHDFERR, NF90_EBADNAME, NF90_EBADDIM, NF90_EBADTYPE, NF90_EBADGRPID, NF90_ENOTNC, NF90_ENOTVAR, &
8+
NF90_ECHAR, NF90_EEDGE, NF90_ENAMEINUSE, NF90_EBADID, NF90_EINDEFINE, NF90_NOWRITE, &
9+
nf90_open, nf90_close, nf90_estride, nf90_inq_varid, nf90_inq_dimid, nf90_inquire_dimension, &
10+
nf90_def_dim, nf90_put_att, nf90_def_var, nf90_get_var, nf90_put_var, nf90_float, nf90_double, nf90_int, nf90_int64, &
11+
nf90_inq_libvers
12+
613
use string_utils, only : toLower, strip_trailing_null, truncate_string_null
714

815
implicit none (type, external)
@@ -21,6 +28,7 @@ module nc4fortran
2128
integer :: comp_lvl = 0 !< compression level (1-9) 0: disable compression
2229
logical :: verbose = .false.
2330
logical :: debug = .false.
31+
character(80) :: libversion
2432

2533
contains
2634

@@ -217,6 +225,9 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
217225
if (present(verbose)) self%verbose = verbose
218226
if (present(debug)) self%debug = debug
219227

228+
!> get library version
229+
self%libversion = nf90_inq_libvers()
230+
220231
lstatus = 'old'
221232
if(present(status)) lstatus = toLower(status)
222233

@@ -226,20 +237,23 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
226237
select case(lstatus)
227238
case ('old', 'unknown')
228239
select case(laction)
229-
case('read','r') !< Open an existing file.
240+
case('read','r')
230241
ier = nf90_open(self%filename, NF90_NOWRITE, self%ncid)
231-
case('write','readwrite','w','rw', 'r+', 'append', 'a')
242+
case('r+')
232243
ier = nf90_open(self%filename, NF90_NETCDF4, self%ncid)
244+
case('readwrite', 'rw', 'append', 'a')
245+
ier = nf90_open(self%filename, NF90_NETCDF4, self%ncid)
246+
case('w','write')
247+
ier = nf90_create(self%filename, ior(NF90_CLOBBER, NF90_NETCDF4), self%ncid)
233248
case default
234-
write(stderr,*) 'ERROR:initialize: Unsupported action -> ' // laction
235-
ier = 128
249+
write(stderr,*) 'Unsupported action -> ' // laction
250+
error stop 128
236251
end select
237252
case('new','replace')
238-
ier = unlink(filename)
239-
ier = nf90_create(self%filename, NF90_NETCDF4, self%ncid)
253+
ier = nf90_create(self%filename, ior(NF90_CLOBBER, NF90_NETCDF4), self%ncid)
240254
case default
241-
write(stderr,*) 'ERROR:initialize: Unsupported status -> '// lstatus
242-
ier = 128
255+
write(stderr,*) 'Unsupported status -> '// lstatus
256+
error stop 128
243257
end select
244258

245259
if (present(ierr)) ierr = ier
@@ -314,23 +328,4 @@ logical function check_error(code, dname)
314328
end function check_error
315329

316330

317-
integer function unlink(path) result (ierr)
318-
!! the non-standard unlink present in some compilers can be unstable
319-
!! in particular ifort can hang, but not with this standard method
320-
character(*), intent(in) :: path
321-
logical :: exists
322-
integer :: u
323-
324-
ierr = 0
325-
326-
inquire(file=path, exist=exists)
327-
if(.not.exists) return
328-
329-
open(newunit=u, file=path, status='old', iostat=ierr)
330-
if(ierr/=0) return
331-
close(u, status='delete', iostat=ierr)
332-
333-
end function unlink
334-
335-
336331
end module nc4fortran

src/tests/test_io.f90

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ subroutine test_exist(path)
3838

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

41-
call hf%initialize(path // '/scalar.nc', status='replace', action='w')
41+
call hf%initialize(path // '/scalar.nc', status='replace')
4242
call hf%write('here', 12)
4343
if(.not. hf%exist('here')) error stop 'variable exists'
4444
if(hf%exist('nothere')) error stop 'variable does not actually exist'
45+
call hf%finalize()
4546

4647
end subroutine test_exist
4748

@@ -58,7 +59,7 @@ subroutine test_scalar(path)
5859

5960
!! write
6061

61-
call hf%initialize(path // '/scalar.nc', status='replace', action='rw')
62+
call hf%initialize(path // '/scalar.nc', status='replace')
6263
call hf%write('nan', nan)
6364
call hf%write('scalar_real', 42.)
6465
call hf%write('scalar_int', 42)
@@ -153,4 +154,4 @@ subroutine test_array(path)
153154
end subroutine test_array
154155

155156

156-
end program
157+
end program

0 commit comments

Comments
 (0)