MODFLOW 6  version 6.7.0.dev2
USGS Modular Hydrologic Model
NetCDFCommon.f90
Go to the documentation of this file.
1 !> @brief This module contains the NetCDFCommonModule
2 !!
3 !! Common NetCDF interfaces and constants
4 !!
5 !<
7 
8  use kindmodule, only: dp, i4b, lgp
9  use constantsmodule, only: linelength
10  use simvariablesmodule, only: errmsg
12  use netcdf
13 
14  implicit none
15  private
16  public :: nc_fopen, nc_fclose
17  public :: netcdf_max_dim
18  public :: netcdf_attr_strlen
19  public :: nf_verify
20  public :: ixstp
21 
22  integer(I4B), parameter :: netcdf_max_dim = 6
23  integer(I4B), parameter :: netcdf_attr_strlen = 80
24 
25 contains
26 
27  !> @brief Open netcdf file
28  !<
29  function nc_fopen(nc_fname, iout) result(ncid)
30  character(len=*), intent(in) :: nc_fname
31  integer(I4B), intent(in) :: iout
32  integer(I4B) :: ncid
33  ! initialize
34  ncid = -1
35  ! open netcdf file
36  call nf_verify(nf90_open(nc_fname, nf90_nowrite, ncid), nc_fname)
37  end function nc_fopen
38 
39  !> @brief Close netcdf file
40  !<
41  subroutine nc_fclose(ncid, nc_fname)
42  integer(I4B), intent(in) :: ncid
43  character(len=*), intent(in) :: nc_fname
44  ! close netcdf file
45  call nf_verify(nf90_close(ncid), nc_fname)
46  end subroutine nc_fclose
47 
48  !> @brief error check a netcdf-fortran interface call
49  !<
50  subroutine nf_verify(res, nc_fname)
51  integer(I4B), intent(in) :: res
52  character(len=*), intent(in) :: nc_fname
53  character(len=LINELENGTH) :: errstr
54 
55  ! strings are set for a subset of errors
56  ! but the exit status will always be reported
57  if (res /= nf90_noerr) then
58  !
59  select case (res)
60  case (-33) ! (NC_EBADID)
61  errstr = 'Not a netcdf id'
62  case (nf90_einval) ! (-36)
63  errstr = 'Invalid Argument'
64  case (nf90_eperm) ! (-37)
65  errstr = 'Write to read only'
66  case (-38) ! (NC_ENOTINDEFINE)
67  errstr = 'Operation not allowed in data mode'
68  case (-39) ! (NC_EINDEFINE)
69  errstr = 'Operation not allowed in define mode'
70  case (nf90_einvalcoords) ! (-40)
71  errstr = 'Index exceeds dimension bound'
72  case (nf90_enameinuse) ! (-42)
73  errstr = 'String match to name in use'
74  case (nf90_enotatt) ! (-43)
75  errstr = 'Attribute not found'
76  case (-45) ! (NC_EBADTYPE)
77  errstr = 'Not a netcdf data type'
78  case (nf90_ebaddim) ! (-46)
79  errstr = 'Invalid dimension id or name'
80  case (nf90_enotvar) ! (-49)
81  errstr = 'Variable not found'
82  case (nf90_enotnc) ! (-51)
83  errstr = 'Not a netcdf file'
84  case (nf90_echar) ! (-56)
85  errstr = 'Attempt to convert between text & numbers'
86  case (nf90_eedge) ! (-57)
87  errstr = 'Edge+start exceeds dimension bound'
88  case (nf90_estride) ! (-58)
89  errstr = 'Illegal stride'
90  case (nf90_ebadname) ! (-59)
91  errstr = 'Attribute or variable name contains illegal characters'
92  case (-127) ! (NC_EBADCHUNK)
93  errstr = 'Bad chunksize.'
94  case default
95  errstr = ''
96  end select
97 
98  if (errstr /= '') then
99  write (errmsg, '(a,a,a,i0,a)') 'NetCDF library error [error="', &
100  trim(errstr), '", exit code=', res, '].'
101  else
102  write (errmsg, '(a,i0,a)') 'NetCDF library error [exit code=', &
103  res, '].'
104  end if
105 
106  call store_error(errmsg)
107  call store_error_filename(nc_fname)
108  end if
109  end subroutine nf_verify
110 
111  !> @brief step index for timeseries data
112  !<
113  function ixstp()
114  use tdismodule, only: kstp, kper, nstp
115  integer(I4B) :: n, ixstp
116  ixstp = kstp
117  if (kper > 1) then
118  do n = 1, kper - 1
119  ixstp = ixstp + nstp(n)
120  end do
121  end if
122  end function ixstp
123 
124 end module netcdfcommonmodule
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition: Constants.f90:45
This module defines variable data types.
Definition: kind.f90:8
This module contains the NetCDFCommonModule.
Definition: NetCDFCommon.f90:6
integer(i4b), parameter, public netcdf_attr_strlen
integer(i4b), parameter, public netcdf_max_dim
subroutine, public nc_fclose(ncid, nc_fname)
Close netcdf file.
integer(i4b) function, public ixstp()
step index for timeseries data
subroutine, public nf_verify(res, nc_fname)
error check a netcdf-fortran interface call
integer(i4b) function, public nc_fopen(nc_fname, iout)
Open netcdf file.
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition: Sim.f90:203
This module contains simulation variables.
Definition: SimVariables.f90:9
character(len=maxcharlen) errmsg
error message string
integer(i4b), dimension(:), pointer, public, contiguous nstp
number of time steps in each stress period
Definition: tdis.f90:39
integer(i4b), pointer, public kstp
current time step number
Definition: tdis.f90:24
integer(i4b), pointer, public kper
current stress period number
Definition: tdis.f90:23