NetCDF-Fortran  4.4.4
nf_genvar.f90
Go to the documentation of this file.
1 !------ Routines for defining and obtaining info about dataset variables ------
2 
3 ! Replacement for fort-genvar.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 checked from C API calls
29 ! Version 6.: Jan. 2016 - General code cleanup. Replaced automatic arrays
30 ! sized with NC_MAX_DIMS with allocatable arrays.
31 ! Change name processing to reflect change in
32 ! addCNullChar
33 
34 !-------------------------------- nf_def_var -------------------------------
35  Function nf_def_var(ncid, name, xtype, nvdims, vdims, varid) RESULT (status)
36 
37 ! Define name, datatype, number of dimensions, and dimension ids for a
38 ! dataset variable. Returns variable id
39 
41 
42  Implicit NONE
43 
44  Integer, Intent(IN) :: ncid, xtype, nvdims
45  Integer, Intent(IN) :: vdims(*)
46  Integer, Intent(OUT) :: varid
47  Character(LEN=*), Intent(IN) :: name
48 
49  Integer :: status
50 
51  Integer(C_INT) :: cncid, cnvdims, cvarid, cstatus, cxtype
52  Character(LEN=(LEN(name)+1)) :: cname
53  Integer :: ie
54 
55  Integer(C_INT), ALLOCATABLE :: cvdims(:)
56 
57  cncid = ncid
58  cnvdims = nvdims
59  cxtype = xtype
60 
61 ! Check if a C NULL character was appended to name in calling routine
62 
63  cname = addcnullchar(name, ie)
64 
65 ! Flip dimids to C order and subtract -1 to yield C ids prior
66 ! to calling nc_def_var. Replaces C utility f2c_dimids
67 
68  If (nvdims > 0) Then
69  ALLOCATE(cvdims(nvdims))
70  cvdims(1:nvdims) = vdims(nvdims:1:-1)-1
71  Else
72  ALLOCATE(cvdims(1))
73  cvdims(1) = 0
74  EndIf
75 
76  cstatus = nc_def_var(cncid, cname(1:ie), cxtype, cnvdims, cvdims, &
77  cvarid)
78 
79  If (cstatus == nc_noerr) Then
80  ! Add one to returned C varid to yield FORTRAN id
81  varid = cvarid + 1
82  EndIf
83 
84  status = cstatus
85 
86  If (ALLOCATED(cvdims)) DEALLOCATE(cvdims)
87 
88  End Function nf_def_var
89 !-------------------------------- nf_inq_varndims --------------------------
90  Function nf_inq_varndims(ncid, varid, vndims) RESULT (status)
91 
92 ! Get variable dimension for a given varid from NetCDF dataset ncid
93 
95 
96  Implicit NONE
97 
98  Integer, Intent(IN) :: ncid, varid
99  Integer, Intent(OUT) :: vndims
100 
101  Integer :: status
102 
103  Integer(C_INT) :: cncid, cvarid, cvndims, cstatus
104 
105  cncid = ncid
106  cvarid = varid - 1
107 
108  cstatus = nc_inq_varndims(cncid, cvarid, cvndims)
109 
110  If (cstatus == nc_noerr) Then
111  vndims = cvndims
112  EndIf
113  status = cstatus
114 
115  End Function nf_inq_varndims
116 !-------------------------------- nf_inq_var ----------------------------
117  Function nf_inq_var(ncid, varid, name, xtype, ndims, dimids, natts) &
118  result(status)
120 ! Get variable name, data type, dimension length, dimension ids, and
121 ! number of attributes for a given varid
122 
124 
125  Implicit NONE
126 
127  Integer, Intent(IN) :: ncid, varid
128  Character(LEN=*), Intent(OUT) :: name
129  Integer, Intent(OUT) :: dimids(*)
130  Integer, Intent(OUT) :: ndims, xtype, natts
131 
132  Integer :: status
133 
134  Integer(C_INT) :: cncid, cstatus, cndims, cvarid, &
135  cnatts, cstat1
136  Integer(C_INT) :: cxtype
137  Character(LEN=NC_MAX_NAME+1) :: tmpname
138  Integer :: nlen
139 
140  Integer(C_INT), ALLOCATABLE :: cdimids(:)
141 
142  cncid = ncid
143  cvarid = varid - 1 ! subtract -1 to yield cvarid
144 
145  nlen = len(name)
146  tmpname = repeat(" ", len(tmpname))
147  name = repeat(" ", nlen)
148 
149  cndims = 0
150  dimids(1) = 0
151  xtype = 0
152  natts = 0
153  ndims = 0
154 
155  cstat1 = nc_inq_varndims(cncid, cvarid, cndims)
156  status = cstat1
157 
158  If (cndims > 0) Then
159  ALLOCATE(cdimids(cndims))
160  Else
161  ALLOCATE(cdimids(1))
162  EndIf
163 
164  cdimids=0
165 
166  cstatus = nc_inq_var(cncid, cvarid, tmpname, cxtype, cndims, cdimids, cnatts)
167 
168  If (cstatus == nc_noerr) Then
169  xtype = cxtype
170  natts = cnatts
171  ndims = cndims
172 
173 ! Check tmpname for a C null character and strip it and trailing blanks
174 
175  name = stripcnullchar(tmpname, nlen)
176 
177 ! Reverse order of cdimids and add one to yield FORTRAN id numbers
178 ! Replaces c2f_dimids C utility
179 
180  If (ndims > 0) Then
181  dimids(1:ndims) = cdimids(ndims:1:-1)+1
182  EndIf
183  EndIf
184 
185  status = cstatus
186 
187  If (ALLOCATED(cdimids)) DEALLOCATE(cdimids)
188 
189  End Function nf_inq_var
190 !-------------------------------- nf_inq_vardimid -----------------------
191  Function nf_inq_vardimid(ncid, varid, dimids) RESULT (status)
193 ! Get variable dimension id for a dimension name
194 
196 
197  Implicit NONE
198 
199  Integer, Intent(IN) :: ncid, varid
200  Integer, Intent(OUT) :: dimids(*)
201 
202  Integer :: status
203 
204  Integer(C_INT) :: cncid, cstatus, cstat2, cndims, cvarid
205  Integer :: ndims
206 
207  Integer(C_INT), ALLOCATABLE :: cvdimids(:)
208 
209  cncid = ncid
210  cvarid = varid - 1 ! subtract -1 to get C id number
211  dimids(1) = 0
212  cndims = 0
213  ndims = 0
214 
215  cstat2 = nc_inq_varndims(cncid, cvarid, cndims)
216 
217  If (cndims > 0) Then
218  ALLOCATE(cvdimids(cndims))
219  Else
220  ALLOCATE(cvdimids(1))
221  EndIf
222 
223  cvdimids = 0
224 
225  cstatus = nc_inq_vardimid(cncid, cvarid, cvdimids)
226 
227 ! Reverse order of cdimids and add 1 to yield FORTRAN id numbers
228 ! Replaces c2f_dimids C utility
229 
230  If (cstatus == nc_noerr) Then
231  ndims = cndims
232  If (ndims > 0) Then
233  dimids(1:ndims) = cvdimids(ndims:1:-1)+1
234  EndIf
235  Else
236  ndims = 0
237  EndIf
238 
239  status = cstatus
240 
241  If (ALLOCATED(cvdimids)) DEALLOCATE(cvdimids)
242 
243  End Function nf_inq_vardimid
244 !-------------------------------- nf_inq_varid --------------------------
245  Function nf_inq_varid(ncid, name, varid) RESULT (status)
247 ! Get variable id for a variable name from NetCDF dataset ncid
248 
250 
251  Implicit NONE
252 
253  Integer, Intent(IN) :: ncid
254  Character(LEN=*), Intent(IN) :: name
255  Integer, Intent(OUT) :: varid
256 
257  Integer :: status
258 
259  Integer(C_INT) :: cncid, cvarid, cstatus
260  Character(LEN=(LEN(name)+1)) :: cname
261  Integer :: ie
262 
263  cncid = ncid
264 
265 ! Check name for a C NULL character added in calling routine
266 
267  cname = addcnullchar(name, ie)
268 
269  cstatus = nc_inq_varid(cncid, cname(1:ie), cvarid)
270 
271  If (cstatus == nc_noerr) Then
272  varid = cvarid + 1 ! add one to get Fortran id number
273  EndIf
274  status = cstatus
275 
276  End Function nf_inq_varid
277 !-------------------------------- nf_inq_varname ------------------------
278  Function nf_inq_varname (ncid, varid, name) RESULT (status)
280 ! Get variable name for a given varid from NetCDF dataset ncid
281 
283 
284  Implicit NONE
285 
286  Integer, Intent(IN) :: ncid, varid
287  Character(LEN=*), Intent(OUT) :: name
288 
289  Integer :: status
290 
291  Integer(C_INT) :: cncid, cvarid, cstatus
292  Character(LEN=NC_MAX_NAME+1) :: tmpname
293  Integer :: nlen
294 
295  cncid = ncid
296  cvarid = varid - 1 ! subtract one to get C id number
297 
298  nlen = len(name)
299  tmpname = repeat(" ", len(tmpname))
300  name = repeat(" ", nlen)
301 
302 ! Get tmpname from C interface
303 
304  cstatus = nc_inq_varname(cncid, cvarid, tmpname)
305 
306  ! Find first C null character in tmpname if present and set end of string
307 
308  If (cstatus == nc_noerr) Then
309  name = stripcnullchar(tmpname, nlen)
310  EndIf
311  status = cstatus
312 
313  End Function nf_inq_varname
314 !-------------------------------- nf_inq_vartype -------------------------
315  Function nf_inq_vartype(ncid, varid, xtype) RESULT(status)
317 ! Inquire about numeric type of variable attributes for NetCDF file id ncid
318 
320 
321  Implicit NONE
322 
323  Integer, Intent(IN) :: ncid, varid
324  Integer, Intent(OUT) :: xtype
325 
326  Integer :: status
327 
328  Integer(C_INT) :: cncid, cvarid, cstatus
329  Integer(C_INT) :: cxtype
330 
331  cncid = ncid
332  cvarid = varid - 1 ! Subtract one to get C id number
333 
334  cstatus = nc_inq_vartype(cncid, cvarid, cxtype)
335 
336  If (cstatus == nc_noerr) Then
337  xtype = cxtype
338  EndIf
339  status = cstatus
340 
341  End Function nf_inq_vartype
342 !-------------------------------- nf_inq_varnatts -----------------------
343  Function nf_inq_varnatts(ncid, varid, nvatts) RESULT(status)
345 ! Inquire about number of variable attributes for NetCDF file id ncid
346 
348 
349  Implicit NONE
350 
351  Integer, Intent(IN) :: ncid, varid
352  Integer, Intent(OUT) :: nvatts
353 
354  Integer :: status
355 
356  Integer(C_INT) :: cncid, cvarid, cnvatts, cstatus
357 
358  cncid = ncid
359  cvarid = varid - 1 ! subtract one to get C id number
360 
361  cstatus = nc_inq_varnatts(cncid, cvarid, cnvatts)
362 
363  If (cstatus == nc_noerr) Then
364  nvatts = cnvatts
365  EndIf
366 
367  status = cstatus
368 
369  End Function nf_inq_varnatts
370 !-------------------------------- nf_rename_var -------------------------
371  Function nf_rename_var(ncid, varid, name) RESULT (status)
373 ! Rename dimension name for a given dimension id
374 
376 
377  Implicit NONE
378 
379  Integer, Intent(IN) :: ncid, varid
380  Character(LEN=*), Intent(IN) :: name
381 
382  Integer :: status
383 
384  Integer(C_INT) :: cncid, cvarid, cstatus
385  Character(LEN=(LEN(name)+1)) :: cname
386  Integer :: ie
387 
388  cncid = ncid
389  cvarid = varid - 1 ! Subtract one to get C id number
390 
391 ! Check name for a C NULL character added in calling routine
392 
393  cname = addcnullchar(name, ie)
394 
395  cstatus = nc_rename_var(cncid, cvarid, cname(1:ie))
396 
397  status = cstatus
398 
399  End Function nf_rename_var
400 !-------------------------------- nf_copy_var ---------------------------
401  Function nf_copy_var(ncid_in, varid, ncid_out) RESULT(status)
403 ! Copies a given variable from dataset ncid_in to dataset ncid_out
404 
406 
407  Implicit NONE
408 
409  Integer, Intent(IN) :: ncid_in, varid, ncid_out
410  Integer :: status
411 
412  Integer(C_INT) :: cncidin, cvarid, cncidout, cstatus
413 
414  cncidin = ncid_in
415  cncidout = ncid_out
416  cvarid = varid - 1 ! Subtract one to get C id number
417 
418  cstatus = nc_copy_var(cncidin, cvarid, cncidout)
419 
420  status = cstatus
421 
422  End Function nf_copy_var
integer function nf_inq_var(ncid, varid, name, xtype, ndims, dimids, natts)
Definition: nf_genvar.f90:119
integer function nf_inq_varname(ncid, varid, name)
Definition: nf_genvar.f90:279
integer function nf_inq_varndims(ncid, varid, vndims)
Definition: nf_genvar.f90:91
integer function nf_copy_var(ncid_in, varid, ncid_out)
Definition: nf_genvar.f90:402
integer function nf_rename_var(ncid, varid, name)
Definition: nf_genvar.f90:372
integer function nf_inq_varnatts(ncid, varid, nvatts)
Definition: nf_genvar.f90:344
module procedure interfaces for utility routines
integer(c_int), parameter nc_noerr
integer function nf_def_var(ncid, name, xtype, nvdims, vdims, varid)
Definition: nf_genvar.f90:36
integer function nf_inq_vartype(ncid, varid, xtype)
Definition: nf_genvar.f90:316
integer function nf_inq_vardimid(ncid, varid, dimids)
Definition: nf_genvar.f90:192
integer function nf_inq_varid(ncid, name, varid)
Definition: nf_genvar.f90:246

Return to the Main Unidata NetCDF page.
Generated on Fri Oct 27 2017 08:12:17 for NetCDF-Fortran. NetCDF is a Unidata library.