NetCDF-Fortran  4.4.3
nf_dim.f90
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 checked from C API calls
29 
30 !-------------------------------- nf_def_dim -------------------------------
31  Function nf_def_dim(ncid, name, dlen, dimid) RESULT (status)
32 
33 ! Adds new dimensions to the NetCDF dataset given dimension name,
34 ! and length. Returns dimension id
35 
36  USE netcdf_nc_interfaces
37 
38  Implicit NONE
39 
40  Integer, Intent(IN) :: ncid, dlen
41  Integer, Intent(OUT) :: dimid
42  Character(LEN=*), Intent(IN) :: name
43 
44  Integer :: status
45 
46  Integer(KIND=C_INT) :: cncid, cdimid, cstatus
47  Integer(KIND=C_SIZE_T) :: cdlen
48  Character(LEN=(LEN(name)+1)) :: cname
49  Integer :: ie
50 
51  cncid = ncid
52  cdlen = dlen
53 
54  dimid = -1
55  cdimid = -1
56 
57 ! Check to see if a C null character was appended in FORTRAN
58 
59  cname = addcnullchar(name, ie)
60 
61  cstatus = nc_def_dim(cncid, cname(1:ie+1), cdlen, cdimid)
62 
63  If (cstatus == nc_ebaddim) Then ! Return dimid=-1
64  dimid = -1
65  Else ! Add 1 to get FORTRAN dimid
66  dimid = cdimid+1
67  EndIf
68  status = cstatus
69 
70  End Function nf_def_dim
71 !-------------------------------- nf_inq_dim -------------------------------
72  Function nf_inq_dim(ncid, dimid, name, dlen) RESULT (status)
73 
74 ! Get dimension name and length for a given dimid from NetCDF dataset ncid
75 
76  USE netcdf_nc_interfaces
77 
78  Implicit NONE
79 
80  Integer, Intent(IN) :: ncid, dimid
81  Integer, Intent(OUT) :: dlen
82  Character(LEN=*), Intent(OUT) :: name
83 
84  Integer :: status
85 
86  Integer(KIND=C_INT) :: cncid, cdimid, cstatus
87  Integer(KIND=C_SIZE_T) :: cdlen
88  Integer :: nlen
89  Character(LEN=NC_MAX_NAME) :: tmpname
90 
91  cncid = ncid
92  cdimid = dimid - 1 ! Subtract 1 to get C dimid
93  tmpname = repeat(" ", len(tmpname))
94  name = repeat(" ", len(name))
95  nlen = len(name)
96 
97 ! Get tmpname and cdlen from C interface
98 
99  cstatus = nc_inq_dim(cncid, cdimid, tmpname, cdlen)
100 
101  If (cstatus == nc_noerr) Then
102  ! Strip C null char from tmpname if present and set end of string
103  name = stripcnullchar(tmpname, nlen)
104  dlen = cdlen
105  Endif
106 
107  status = cstatus
108 
109  End Function nf_inq_dim
110 !-------------------------------- nf_inq_dimid -----------------------------
111  Function nf_inq_dimid(ncid, name, dimid) RESULT (status)
112 
113 ! Get dimension id for a given dimension name from dataset ncid
114 
115  USE netcdf_nc_interfaces
116 
117  Implicit NONE
118 
119  Integer, Intent(IN) :: ncid
120  Integer, Intent(OUT) :: dimid
121  Character(LEN=*), Intent(IN) :: name
122 
123  Integer :: status
124 
125  Integer(KIND=C_INT) :: cncid, cdimid, cstatus
126  Character(LEN=(LEN(name)+1)) :: cname
127  Integer :: ie
128 
129  cncid = ncid
130  dimid = 0
131  cdimid = -1
132 
133 ! Check to see if a C null character was appended in FORTRAN
134 
135  cname = addcnullchar(name, ie)
136 
137  cstatus = nc_inq_dimid(cncid, cname(1:ie+1), cdimid)
138 
139 ! add one to get FORTRAN dimid if not = -1
140 
141  If (cstatus == nc_ebaddim) Then
142  dimid = -1
143  Else
144  dimid = cdimid + 1
145  EndIf
146  status = cstatus
147 
148  End Function nf_inq_dimid
149 !-------------------------------- nf_inq_dimlen ----------------------------
150  Function nf_inq_dimlen(ncid, dimid, dlen) RESULT (status)
151 
152 ! Get dimension length for a given dimid from NetCDF dataset ncid
153 
154  USE netcdf_nc_interfaces
155 
156  Implicit NONE
157 
158  Integer, Intent(IN) :: ncid, dimid
159  Integer, Intent(OUT) :: dlen
160 
161  Integer :: status
162 
163  Integer(KIND=C_INT) :: cncid, cdimid, cstatus
164  Integer(KIND=C_SIZE_T) :: cdlen
165 
166  cncid = ncid
167  cdimid = dimid - 1 ! Subtract 1 to get C dimid
168  dlen = 0
169 
170  cstatus = nc_inq_dimlen(cncid, cdimid, cdlen)
171 
172  If (cstatus == nc_noerr) Then
173  dlen = cdlen
174  Endif
175  status = cstatus
176 
177  End Function nf_inq_dimlen
178 !-------------------------------- nf_inq_dimname ---------------------------
179  Function nf_inq_dimname (ncid, dimid, name) RESULT (status)
180 
181 ! Get dimension name for a given dimid from NetCDF dataset ncid
182 
183  USE netcdf_nc_interfaces
184 
185  Implicit NONE
186 
187  Integer, Intent(IN) :: ncid, dimid
188  Character(LEN=*), Intent(OUT) :: name
189 
190  Integer :: status
191 
192  Integer(KIND=C_INT) :: cncid, cdimid, cstatus
193  Integer :: nlen
194  Character(LEN=NC_MAX_NAME) :: tmpname
195 
196  cncid = ncid
197  cdimid = dimid - 1 ! Subtract 1 to get C dimid
198  tmpname = repeat(" ", len(tmpname))
199  name = repeat(" ", len(name))
200  nlen = len(name)
201 
202 ! Get tmpname and cdlen from C interface
203 
204  cstatus = nc_inq_dimname(cncid, cdimid, tmpname)
205 
206  If (cstatus == nc_noerr) Then
207  ! Strip C null character in tmpname if present and set end of string
208  name = stripcnullchar(tmpname, nlen)
209  Endif
210 
211  status = cstatus
212 
213  End Function nf_inq_dimname
214 !-------------------------------- nf_rename_dim ----------------------------
215  Function nf_rename_dim(ncid, dimid, name) RESULT (status)
216 
217 ! Rename dimension name for a given dimension id
218 
219  USE netcdf_nc_interfaces
220 
221  Implicit NONE
222 
223  Integer, Intent(IN) :: ncid, dimid
224  Character(LEN=*), Intent(IN) :: name
225 
226  Integer :: status
227 
228  Integer(KIND=C_INT) :: cncid, cdimid, cstatus
229  Character(LEN=(LEN(name)+1)) :: cname
230  Integer :: ie
231 
232  cncid = ncid
233  cdimid = dimid - 1 ! Subtract 1 to get C dimid
234 
235 ! Check to see if a C null character was appended in FORTRAN
236 
237  cname = addcnullchar(name, ie)
238 
239  cstatus = nc_rename_dim(cncid, cdimid, cname(1:ie+1))
240 
241  status = cstatus
242 
243  End Function nf_rename_dim
module procedure interfaces for utility routines

Return to the Main Unidata NetCDF page.
Generated on Tue Mar 1 2016 12:16:22 for NetCDF-Fortran. NetCDF is a Unidata library.