@@ -2,7 +2,14 @@ module nc4fortran
22! ! NetCDF4 object-oriented polymorphic interface
33use , intrinsic :: iso_c_binding, only : c_ptr, c_loc
44use , 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+
613use string_utils, only : toLower, strip_trailing_null, truncate_string_null
714
815implicit none (type, external )
@@ -21,6 +28,7 @@ module nc4fortran
2128integer :: comp_lvl = 0 ! < compression level (1-9) 0: disable compression
2229logical :: verbose = .false.
2330logical :: debug = .false.
31+ character (80 ) :: libversion
2432
2533contains
2634
@@ -217,6 +225,9 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
217225if (present (verbose)) self% verbose = verbose
218226if (present (debug)) self% debug = debug
219227
228+ ! > get library version
229+ self% libversion = nf90_inq_libvers()
230+
220231lstatus = ' old'
221232if (present (status)) lstatus = toLower(status)
222233
@@ -226,20 +237,23 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
226237select case (lstatus)
227238case (' 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
237252case (' 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)
240254case default
241- write (stderr,* ) ' ERROR:initialize: Unsupported status -> ' // lstatus
242- ier = 128
255+ write (stderr,* ) ' Unsupported status -> ' // lstatus
256+ error stop 128
243257end select
244258
245259if (present (ierr)) ierr = ier
@@ -314,23 +328,4 @@ logical function check_error(code, dname)
314328end 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-
336331end module nc4fortran
0 commit comments