NetCDF-Fortran  4.4.4
nf_genatt.f90
Go to the documentation of this file.
1 !---------- Routines for defining and obtaining info about attributes --------
2 
3 ! Replacement for fort-genatt.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 ! License (and other Lawyer Language)
11 
12 ! This software is released under the Apache 2.0 Open Source License. The
13 ! full text of the License can be viewed at :
14 !
15 ! http:www.apache.org/licenses/LICENSE-2.0.html
16 !
17 ! The author grants to the University Corporation for Atmospheric Research
18 ! (UCAR), Boulder, CO, USA the right to revise and extend the software
19 ! without restriction. However, the author retains all copyrights and
20 ! intellectual property rights explicitly stated in or implied by the
21 ! Apache license
22 
23 ! Version 1.: Sept. 2005 - Initial Cray X1 version
24 ! Version 2.: May 2006 - Updated to support g95
25 ! Version 3.: April 2009 - Updated for netCDF 4.0.1
26 ! Version 4.: April 2010 - Updated for netCDF 4.1.1
27 ! Version 5.: May 2014 - Ensure return error status checked from C API calls
28 ! Version 6.: Jan. 2016 - General code cleanup. Changed name processing to
29 ! reflect change in addCNullChar
30 
31 !-------------------------------- nf_inq_att ---------------------------------
32  Function nf_inq_att(ncid, varid, name, xtype, nlen) RESULT(status)
33 
34 ! Get attribute data type and length for a given varid and name
35 
37 
38  Implicit NONE
39 
40  Integer, Intent(IN) :: ncid, varid
41  Character(LEN=*), Intent(IN) :: name
42  Integer, Intent(OUT) :: nlen, xtype
43 
44  Integer :: status
45 
46  Integer(C_INT) :: cncid, cstatus, cvarid
47  Integer(C_SIZE_T) :: cnlen
48  Integer(C_INT) :: cxtype
49  Character(LEN=(LEN(name)+1)) :: cname
50  Integer :: ie
51 
52  cncid = ncid
53  cvarid = varid - 1 ! Subtract 1 to get C varid
54 
55 ! Check to see if a C null character was added to name in calling program
56 
57  cname = addcnullchar(name, ie)
58 
59  cstatus = nc_inq_att(cncid, cvarid, cname(1:ie), cxtype, cnlen)
60 
61  If (cstatus == nc_noerr) Then
62  xtype = cxtype
63  nlen = cnlen
64  EndIf
65  status = cstatus
66 
67  End Function nf_inq_att
68 !-------------------------------- nf_inq_atttype ---------------------------
69  Function nf_inq_atttype(ncid, varid, name, xtype) RESULT(status)
70 
71 ! Get attribute type for a given varid and name
72 
74 
75  Implicit NONE
76 
77  Integer, Intent(IN) :: ncid, varid
78  Character(LEN=*), Intent(IN) :: name
79  Integer, Intent(OUT) :: xtype
80 
81  Integer :: status
82 
83  Integer(C_INT) :: cncid, cstatus, cvarid
84  Integer(C_INT) :: cxtype
85  Character(LEN=(LEN(name)+1)) :: cname
86  Integer :: ie
87 
88  cncid = ncid
89  cvarid = varid - 1 ! Subtract 1 to get C varid
90 
91 ! Check to see if a C null character was added to name in calling program
92 
93  cname = addcnullchar(name, ie)
94 
95  cstatus = nc_inq_atttype(cncid, cvarid, cname(1:ie), cxtype)
96 
97  If (cstatus == nc_noerr) Then
98  xtype = cxtype
99  EndIf
100  status = cstatus
101 
102  End Function nf_inq_atttype
103 !-------------------------------- nf_inq_attlen ----------------------------
104  Function nf_inq_attlen(ncid, varid, name, nlen) RESULT(status)
106 ! Get attribute length for a given varid and name
107 
109 
110  Implicit NONE
111 
112  Integer, Intent(IN) :: ncid, varid
113  Character(LEN=*), Intent(IN) :: name
114  Integer, Intent(OUT) :: nlen
115 
116  Integer :: status
117 
118  Integer(C_INT) :: cncid, cstatus, cvarid
119  Integer(C_SIZE_T) :: cnlen
120  Character(LEN=(LEN(name)+1)) :: cname
121  Integer :: ie
122 
123  cncid = ncid
124  cvarid = varid - 1 ! Subtract 1 to get C varid
125 
126 ! Check to see if a C null character was added to name in calling program
127 
128  cname = addcnullchar(name, ie)
129 
130  cstatus = nc_inq_attlen(cncid, cvarid, cname(1:ie), cnlen)
131 
132  If (cstatus == nc_noerr) Then
133  nlen = cnlen
134  EndIf
135  status = cstatus
136 
137  End Function nf_inq_attlen
138 !-------------------------------- nf_inq_attid -----------------------------
139  Function nf_inq_attid(ncid, varid, name, attnum) RESULT(status)
141 ! Get attribute id for a given varid and name
142 
144 
145  Implicit NONE
146 
147  Integer, Intent(IN) :: ncid, varid
148  Character(LEN=*), Intent(IN) :: name
149  Integer, Intent(OUT) :: attnum
150 
151  Integer :: status
152 
153  Integer(C_INT) :: cncid, cstatus, cattnum, cvarid
154  Character(LEN=(LEN(name)+1)) :: cname
155  Integer :: ie
156 
157  cncid = ncid
158  cvarid = varid - 1 ! Subtract 1 to get C varid
159 
160 ! Check to see if a C null character was added to name in calling program
161 
162  cname = addcnullchar(name, ie)
163 
164  cstatus = nc_inq_attid(cncid, cvarid, cname(1:ie), cattnum)
165 
166  If (cstatus == nc_noerr) Then
167  attnum = cattnum + 1 ! add 1 to get FORTRAN att id
168  EndIf
169  status = cstatus
170 
171  End Function nf_inq_attid
172 !-------------------------------- nf_inq_attname ---------------------------
173  Function nf_inq_attname(ncid, varid, attnum, name) RESULT(status)
175 ! Get attribute name for a given varid and attribute number
176 
178 
179  Implicit NONE
180 
181  Integer, Intent(IN) :: ncid, varid, attnum
182  Character(LEN=*), Intent(OUT) :: name
183 
184  Integer :: status
185 
186  Integer(C_INT) :: cncid, cstatus, cattnum, cvarid
187  Character(LEN=(LEN(name)+1)) :: tmpname
188  Integer :: nlen
189 
190  cncid = ncid
191  cvarid = varid - 1 ! Subtract 1 to get C varid and att num
192  cattnum = attnum - 1
193  nlen = len(name)
194  name = repeat(" ",nlen)
195  tmpname = repeat(" ",len(tmpname)) ! init to blanks
196 
197  cstatus = nc_inq_attname(cncid, cvarid, cattnum, tmpname)
198 
199  If (cstatus == nc_noerr) Then
200  ! Strip of any C null characters and load only the part
201  ! of tmpname that will fit in name
202 
203  name = stripcnullchar(tmpname, nlen)
204  EndIf
205  status = cstatus
206 
207  End Function nf_inq_attname
208 !-------------------------------- nf_copy_att ------------------------------
209  Function nf_copy_att(ncid_in, varid_in, name, ncid_out, varid_out) &
210  result(status)
212 ! Copy attribute name with varid_in from one netcdf file to another
213 ! with new varid_out
214 
216 
217  Implicit NONE
218 
219  Integer, Intent(IN) :: ncid_in, varid_in, ncid_out, varid_out
220  Character(LEN=*), Intent(IN) :: name
221 
222  Integer :: status
223 
224  Integer(C_INT) :: cncidin, cncidout,cvaridin, cvaridout, cstatus
225  Character(LEN=(LEN(name)+1)) :: cname
226  Integer :: ie
227 
228  cncidin = ncid_in
229  cvaridin = varid_in - 1
230  cncidout = ncid_out
231  cvaridout = varid_out - 1
232 
233 ! Check to see if a C null character was added to name in calling program
234 
235  cname = addcnullchar(name, ie)
236 
237  cstatus = nc_copy_att(cncidin, cvaridin, cname(1:ie), &
238  cncidout, cvaridout)
239 
240  status = cstatus
241 
242  End Function nf_copy_att
243 !-------------------------------- nf_rename_att ----------------------------
244  Function nf_rename_att(ncid, varid, name, newname) RESULT(status)
246 ! Rename an attribute to newname givin varid
247 
249 
250  Implicit NONE
251 
252  Integer, Intent(IN) :: ncid, varid
253  Character(LEN=*), Intent(IN) :: name, newname
254 
255  Integer :: status
256 
257  Integer(C_INT) :: cncid, cvarid, cstatus
258  Character(LEN=(LEN(name)+1)) :: cname
259  Character(LEN=(LEN(newname)+1)) :: cnewname
260  Integer :: ie1, ie2, inull
261 
262  cncid = ncid
263  cvarid = varid - 1 ! Subtract 1 to get C varid
264 
265 ! Check to see if a C null character was added to name and newname
266 ! in calling program
267 
268  cname = addcnullchar(name, ie1)
269 
270  cnewname = addcnullchar(newname, ie2)
271 
272  cstatus = nc_rename_att(cncid, cvarid, cname(1:ie1), cnewname(1:ie2))
273 
274  status = cstatus
275 
276  End Function nf_rename_att
277 !-------------------------------- nf_del_att -------------------------------
278  Function nf_del_att(ncid, varid, name) RESULT(status)
280 ! Delete an attribute givne varid and name
281 
283 
284  Implicit NONE
285 
286  Integer, Intent(IN) :: ncid, varid
287  Character(LEN=*), Intent(IN) :: name
288 
289  Integer :: status
290 
291  Integer(C_INT) :: cncid, cvarid, cstatus
292  Character(LEN=(LEN(name)+1)) :: cname
293  Integer :: ie
294 
295  cncid = ncid
296  cvarid = varid - 1 ! Subtract 1 to get C varid
297 
298 ! Check to see if a C null character was added to name in calling program
299 
300  cname = addcnullchar(name, ie)
301 
302  cstatus = nc_del_att(cncid, cvarid, cname(1:ie))
303 
304  status = cstatus
305 
306  End Function nf_del_att
integer function nf_del_att(ncid, varid, name)
Definition: nf_genatt.f90:279
integer function nf_inq_atttype(ncid, varid, name, xtype)
Definition: nf_genatt.f90:70
integer function nf_inq_attname(ncid, varid, attnum, name)
Definition: nf_genatt.f90:174
integer function nf_inq_attid(ncid, varid, name, attnum)
Definition: nf_genatt.f90:140
integer function nf_rename_att(ncid, varid, name, newname)
Definition: nf_genatt.f90:245
module procedure interfaces for utility routines
integer function nf_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)
Definition: nf_genatt.f90:211
integer(c_int), parameter nc_noerr
integer function nf_inq_attlen(ncid, varid, name, nlen)
Definition: nf_genatt.f90:105
integer function nf_inq_att(ncid, varid, name, xtype, nlen)
Definition: nf_genatt.f90:33

Return to the Main Unidata NetCDF page.
Generated on Fri Aug 4 2017 17:20:58 for NetCDF-Fortran. NetCDF is a Unidata library.