1!------- Routines for defining, obtaining, etc. global dimension info ------
2
3! Replacement for fort-dim.c
4
5! Written by: Richard Weed, Ph.D.
6!             Center for Advanced Vehicular Systems
7!             Mississippi State University
8!             rweed@cavs.msstate.edu
9
10
11! License (and other Lawyer Language)
12
13! This software is released under the Apache 2.0 Open Source License. The
14! full text of the License can be viewed at :
15!
16!   http:www.apache.org/licenses/LICENSE-2.0.html
17!
18! The author grants to the University Corporation for Atmospheric Research
19! (UCAR), Boulder, CO, USA the right to revise and extend the software
20! without restriction. However, the author retains all copyrights and
21! intellectual property rights explicitly stated in or implied by the
22! Apache license
23
24! Version 1.: Sept. 2005 - Initial Cray X1 version
25! Version 2.: May   2006 - Updated to support g95
26! Version 3.: April 2009 - Updated for netCDF 4.0.1
27! Version 4.: April 2010 - Updated for netCDF 4.1.1
28! Version 5.: May   2014 - Ensure return error status
29!                          checked from C API calls
30! Version 6.: Jan.  2016 - General code cleanup. Changed name
31!                          processing to reflect change in addCNullChar
32
33!-------------------------------- nf_def_dim -------------------------------
34 Function nf_def_dim(ncid, name, dlen, dimid) RESULT (status)
35
36! Adds new dimensions to the NetCDF dataset given dimension name,
37! and length. Returns dimension id
38
39 USE netcdf_nc_interfaces
40
41 Implicit NONE
42
43 Integer,          Intent(IN)  :: ncid, dlen
44 Integer,          Intent(OUT) :: dimid
45 Character(LEN=*), Intent(IN)  :: name
46
47 Integer                       :: status
48
49 Integer(C_INT)               :: cncid, cdimid, cstatus
50 Integer(C_SIZE_T)            :: cdlen
51 Character(LEN=(LEN(name)+1)) :: cname
52 Integer                      :: ie
53
54 cncid = ncid
55 cdlen = dlen
56
57 dimid  = -1
58 cdimid = -1
59
60! Check to see if a C null character was appended in FORTRAN
61
62 cname = addCNullChar(name, ie)
63
64 cstatus = nc_def_dim(cncid, cname(1:ie), cdlen, cdimid)
65
66 If (cstatus == NC_EBADDIM) Then  ! Return dimid=-1
67   dimid = -1
68 Else                    ! Add 1 to get FORTRAN dimid
69   dimid = cdimid+1
70 EndIf
71 status = cstatus
72
73 End Function nf_def_dim
74!-------------------------------- nf_inq_dim -------------------------------
75 Function nf_inq_dim(ncid, dimid, name, dlen) RESULT (status)
76
77! Get dimension name and length for a given dimid from NetCDF dataset ncid
78
79 USE netcdf_nc_interfaces
80
81 Implicit NONE
82
83 Integer,          Intent(IN)  :: ncid, dimid
84 Integer,          Intent(OUT) :: dlen
85 Character(LEN=*), Intent(OUT) :: name
86
87 Integer                       :: status
88
89 Integer(C_INT)             :: cncid, cdimid, cstatus
90 Integer(C_SIZE_T)          :: cdlen
91 Integer                    :: nlen
92 Character(LEN=NC_MAX_NAME) :: tmpname
93
94 cncid   = ncid
95 cdimid  = dimid - 1   ! Subtract 1 to get C dimid
96 tmpname = REPEAT(" ", LEN(tmpname))
97 name    = REPEAT(" ", LEN(name))
98 nlen    = LEN(name)
99
100! Get tmpname and cdlen from C interface
101
102 cstatus = nc_inq_dim(cncid, cdimid, tmpname, cdlen)
103
104 If (cstatus == NC_NOERR) Then
105    ! Strip C null char from tmpname if present and set end of string
106    name = stripCNullChar(tmpname, nlen)
107    dlen   = int(cdlen)
108 Endif
109
110 status = cstatus
111
112 End Function nf_inq_dim
113!-------------------------------- nf_inq_dimid -----------------------------
114 Function nf_inq_dimid(ncid, name, dimid) RESULT (status)
115
116! Get dimension id for a given dimension name from dataset ncid
117
118 USE netcdf_nc_interfaces
119
120 Implicit NONE
121
122 Integer,          Intent(IN)  :: ncid
123 Integer,          Intent(OUT) :: dimid
124 Character(LEN=*), Intent(IN)  :: name
125
126 Integer                       :: status
127
128 Integer(C_INT)               :: cncid, cdimid, cstatus
129 Character(LEN=(LEN(name)+1)) :: cname
130 Integer                      :: ie
131
132 cncid = ncid
133 dimid  =  0
134 cdimid = -1
135
136! Check to see if a C null character was appended in FORTRAN
137
138 cname = addCNullChar(name, ie)
139
140 cstatus = nc_inq_dimid(cncid, cname(1:ie), cdimid)
141
142! add one to get FORTRAN dimid if not = -1
143
144 If (cstatus == NC_EBADDIM) Then
145   dimid = -1
146 Else
147   dimid = cdimid + 1
148 EndIf
149 status = cstatus
150
151 End Function nf_inq_dimid
152!-------------------------------- nf_inq_dimlen ----------------------------
153 Function nf_inq_dimlen(ncid, dimid, dlen) RESULT (status)
154
155! Get dimension length for a given dimid from NetCDF dataset ncid
156
157 USE netcdf_nc_interfaces
158
159 Implicit NONE
160
161 Integer, Intent(IN)  :: ncid, dimid
162 Integer, Intent(OUT) :: dlen
163
164 Integer              :: status
165
166 Integer(C_INT)    :: cncid, cdimid, cstatus
167 Integer(C_SIZE_T) :: cdlen
168
169 cncid   = ncid
170 cdimid  = dimid - 1 ! Subtract 1 to get C dimid
171 dlen    = 0
172
173 cstatus = nc_inq_dimlen(cncid, cdimid, cdlen)
174
175 If (cstatus == NC_NOERR) Then
176    dlen   = int(cdlen)
177 Endif
178 status = cstatus
179
180 End Function nf_inq_dimlen
181!-------------------------------- nf_inq_dimname ---------------------------
182 Function nf_inq_dimname (ncid, dimid, name) RESULT (status)
183
184! Get dimension name for a given dimid from NetCDF dataset ncid
185
186 USE netcdf_nc_interfaces
187
188 Implicit NONE
189
190 Integer,          Intent(IN)  :: ncid, dimid
191 Character(LEN=*), Intent(OUT) :: name
192
193 Integer                       :: status
194
195 Integer(C_INT)             :: cncid, cdimid, cstatus
196 Integer                    :: nlen
197 Character(LEN=NC_MAX_NAME) :: tmpname
198
199 cncid   = ncid
200 cdimid  = dimid - 1 ! Subtract 1 to get C dimid
201 tmpname = REPEAT(" ", LEN(tmpname))
202 name    = REPEAT(" ", LEN(name))
203 nlen    = LEN(name)
204
205! Get tmpname and cdlen from C interface
206
207 cstatus = nc_inq_dimname(cncid, cdimid, tmpname)
208
209 If (cstatus == NC_NOERR) Then
210    ! Strip C null character in tmpname if present and set end of string
211    name = stripCNullChar(tmpname, nlen)
212 Endif
213
214 status = cstatus
215
216 End Function nf_inq_dimname
217!-------------------------------- nf_rename_dim ----------------------------
218 Function nf_rename_dim(ncid, dimid, name) RESULT (status)
219
220! Rename dimension name for a given dimension id
221
222 USE netcdf_nc_interfaces
223
224 Implicit NONE
225
226 Integer,          Intent(IN) :: ncid, dimid
227 Character(LEN=*), Intent(IN) :: name
228
229 Integer                      :: status
230
231 Integer(C_INT)               :: cncid, cdimid, cstatus
232 Character(LEN=(LEN(name)+1)) :: cname
233 Integer                      :: ie
234
235 cncid  = ncid
236 cdimid = dimid - 1 ! Subtract 1 to get C dimid
237
238! Check to see if a C null character was appended in FORTRAN
239
240 cname = addCNullChar(name, ie)
241
242 cstatus = nc_rename_dim(cncid, cdimid, cname(1:ie))
243
244 status = cstatus
245
246 End Function nf_rename_dim
247