FV3 Bundle
ncdr_dims.f90
Go to the documentation of this file.
1 module ncdr_dims
2  use ncd_kinds, only: i_long
4  use ncdr_climsg, only: ncdr_error
7  use netcdf, only: nf90_inquire_dimension, nf90_max_name
9 
10  implicit none
11 
13  module procedure nc_diag_read_id_lookup_dim, &
15  end interface nc_diag_read_lookup_dim
16 
18  module procedure nc_diag_read_id_assert_dim, &
20  end interface nc_diag_read_assert_dim
21 
23  module procedure nc_diag_read_id_check_dim, &
25  end interface nc_diag_read_check_dim
26 
28  module procedure nc_diag_read_id_get_dim, &
30  end interface nc_diag_read_get_dim
31 
33  module procedure nc_diag_read_id_check_dim_unlim, &
35  end interface nc_diag_read_check_dim_unlim
36 
38  module procedure nc_diag_read_id_get_dim_names
39  end interface nc_diag_read_get_dim_names
40 
42  module procedure nc_diag_read_noid_get_dim_names
44 
45  contains
46  subroutine nc_diag_read_parse_file_dims(file_ncid, file_index, num_dims)
47  integer(i_long), intent(in) :: file_ncid
48  integer(i_long), intent(in) :: file_index
49  integer(i_long), intent(in) :: num_dims
50 
51  integer(i_long), dimension(:), allocatable :: unlim_dims
52  integer(i_long) :: num_unlims
53  integer(i_long) :: i, j
54 
55  character(len=NF90_MAX_NAME) :: dim_name
56 
57  ncdr_files(file_index)%ndims = num_dims
58  allocate(ncdr_files(file_index)%dims(num_dims))
59 
60  ! Get unlimited dimension information
61  call ncdr_nc_check(pf_nf90_inq_unlimdims(file_ncid, num_unlims))
62 
63  allocate(unlim_dims(num_unlims))
64 
65  call ncdr_nc_check(pf_nf90_inq_unlimdims(file_ncid, num_unlims, unlim_dims))
66 
67  do i = 1, num_dims
68  ncdr_files(file_index)%dims(i)%dim_id = i
69 
70  call ncdr_nc_check(nf90_inquire_dimension(file_ncid, i, &
71  dim_name, &
72  ncdr_files(file_index)%dims(i)%dim_size))
73 
74  ncdr_files(file_index)%dims(i)%dim_name = trim(dim_name)
75  ncdr_files(file_index)%dims(i)%dim_unlim = .false.
76 
77  do j = 1, num_unlims
78  if (i == unlim_dims(j)) then
79  ncdr_files(file_index)%dims(i)%dim_unlim = .true.
80  exit
81  end if
82  end do
83  end do
84  end subroutine nc_diag_read_parse_file_dims
85 
86  function nc_diag_read_id_lookup_dim(file_ncdr_id, dim_name) result(dim_index)
87  integer(i_long), intent(in) :: file_ncdr_id
88  character(len=*), intent(in) :: dim_name
89 
90  integer(i_long) :: dim_index
91 
92  call ncdr_check_ncdr_id(file_ncdr_id)
93 
94  do dim_index = 1, ncdr_files(file_ncdr_id)%ndims
95  if (ncdr_files(file_ncdr_id)%dims(dim_index)%dim_name == dim_name) &
96  return
97  end do
98 
99  ! Otherwise, return -1!
100  dim_index = -1
101  end function nc_diag_read_id_lookup_dim
102 
103  function nc_diag_read_noid_lookup_dim(dim_name) result(dim_index)
104  character(len=*), intent(in) :: dim_name
105 
106  integer(i_long) :: dim_index
107 
109 
110  dim_index = nc_diag_read_id_lookup_dim(current_ncdr_id, dim_name)
111  end function nc_diag_read_noid_lookup_dim
112 
113  function nc_diag_read_id_assert_dim(file_ncdr_id, dim_name) result(dim_index)
114  integer(i_long), intent(in) :: file_ncdr_id
115  character(len=*), intent(in) :: dim_name
116 
117  integer(i_long) :: dim_index
118 
119  call ncdr_check_ncdr_id(file_ncdr_id)
120 
121  ! Otherwise, return -1!
122  dim_index = nc_diag_read_id_lookup_dim(file_ncdr_id, dim_name)
123 
124  ! ...except don't, since we're asserting!
125  if (dim_index == -1) &
126  call ncdr_error("The specified dimension '" // dim_name // "' does not exist!")
127  end function nc_diag_read_id_assert_dim
128 
129  function nc_diag_read_noid_assert_dim(dim_name) result(dim_index)
130  character(len=*), intent(in) :: dim_name
131 
132  integer(i_long) :: dim_index
133 
135 
136  dim_index = nc_diag_read_id_assert_dim(current_ncdr_id, dim_name)
137  end function nc_diag_read_noid_assert_dim
138 
139  function nc_diag_read_id_check_dim(file_ncdr_id, dim_name) result(dim_exists)
140  integer(i_long), intent(in) :: file_ncdr_id
141  character(len=*), intent(in) :: dim_name
142 
143  logical :: dim_exists
144 
145  call ncdr_check_ncdr_id(file_ncdr_id)
146 
147  if (nc_diag_read_id_lookup_dim(file_ncdr_id, dim_name) == -1) then
148  dim_exists = .false.
149  return
150  end if
151 
152  dim_exists = .true.
153  end function nc_diag_read_id_check_dim
154 
155  function nc_diag_read_noid_check_dim(dim_name) result(dim_exists)
156  character(len=*), intent(in) :: dim_name
157 
158  logical :: dim_exists
159 
161 
162  if (nc_diag_read_lookup_dim(dim_name) == -1) then
163  dim_exists = .false.
164  return
165  end if
166 
167  dim_exists = .true.
168  end function nc_diag_read_noid_check_dim
169 
170  function nc_diag_read_id_get_dim(file_ncdr_id, dim_name) result(dim_size)
171  integer(i_long), intent(in) :: file_ncdr_id
172  character(len=*), intent(in) :: dim_name
173 
174  integer(i_long) :: dim_index, dim_size
175 
176  call ncdr_check_ncdr_id(file_ncdr_id)
177 
178  dim_index = nc_diag_read_id_assert_dim(file_ncdr_id, dim_name)
179 
180  dim_size = ncdr_files(file_ncdr_id)%dims(dim_index)%dim_size
181  end function nc_diag_read_id_get_dim
182 
183  function nc_diag_read_noid_get_dim(dim_name) result(dim_size)
184  character(len=*), intent(in) :: dim_name
185 
186  integer(i_long) :: dim_size
187 
189 
190  dim_size = nc_diag_read_id_get_dim(current_ncdr_id, dim_name)
191  end function nc_diag_read_noid_get_dim
192 
193  function nc_diag_read_id_check_dim_unlim(file_ncdr_id, dim_name) result(dim_isunlim)
194  integer(i_long), intent(in) :: file_ncdr_id
195  character(len=*), intent(in) :: dim_name
196 
197  integer(i_long) :: dim_index
198  logical :: dim_isunlim
199 
200  call ncdr_check_ncdr_id(file_ncdr_id)
201 
202  dim_index = nc_diag_read_id_assert_dim(file_ncdr_id, dim_name)
203 
204  dim_isunlim = ncdr_files(file_ncdr_id)%dims(dim_index)%dim_unlim
206 
207  function nc_diag_read_noid_check_dim_unlim(dim_name) result(dim_isunlim)
208  character(len=*), intent(in) :: dim_name
209 
210  logical :: dim_isunlim
211 
213 
214  dim_isunlim = nc_diag_read_id_check_dim_unlim(current_ncdr_id, dim_name)
216 
217  subroutine nc_diag_read_id_get_dim_names(file_ncdr_id, num_dims, dim_name_mlen, dim_names)
218  integer(i_long), intent(in) :: file_ncdr_id
219  integer(i_long), intent(out), optional :: num_dims
220  integer(i_long), intent(out), optional :: dim_name_mlen
221  character(len=:), intent(inout), dimension(:), allocatable, optional:: dim_names
222 
223  integer(i_long) :: dim_index, ndims, max_dim_name_len
224 
225  max_dim_name_len = 0
226 
227  call ncdr_check_ncdr_id(file_ncdr_id)
228 
229  ndims = ncdr_files(file_ncdr_id)%ndims
230 
231  if (present(num_dims)) &
232  num_dims = ndims
233 
234  ! Figure out character max length
235  do dim_index = 1, ndims
236  if (len(ncdr_files(file_ncdr_id)%dims(dim_index)%dim_name) > max_dim_name_len) &
237  max_dim_name_len = len(ncdr_files(file_ncdr_id)%dims(dim_index)%dim_name)
238  end do
239 
240  if (present(dim_name_mlen)) &
241  dim_name_mlen = max_dim_name_len
242 
243  if (present(dim_names)) then
244  if (.NOT. allocated(dim_names)) then
245  allocate(character(max_dim_name_len) :: dim_names(ndims))
246  else
247  if (size(dim_names) /= ndims) &
248  call ncdr_error("Invalid allocated array size for dimension names storage!")
249  if (len(dim_names) < max_dim_name_len) &
250  call ncdr_error("Invalid allocated array size for dimension names storage! (String length does not match!)")
251  end if
252 
253  do dim_index = 1, ndims
254  dim_names(dim_index) = ncdr_files(file_ncdr_id)%dims(dim_index)%dim_name
255  end do
256  end if
257  end subroutine nc_diag_read_id_get_dim_names
258 
259  subroutine nc_diag_read_noid_get_dim_names(num_dims, dim_name_mlen, dim_names)
260  integer(i_long), intent(out), optional :: num_dims
261  integer(i_long), intent(out), optional :: dim_name_mlen
262  character(len=:), intent(inout), dimension(:), allocatable, optional:: dim_names
263 
265 
266  if (present(num_dims)) then
267  if (present(dim_name_mlen)) then
268  if (present(dim_names)) then
269  call nc_diag_read_id_get_dim_names(current_ncdr_id, num_dims, dim_name_mlen, dim_names)
270  else
271  call nc_diag_read_id_get_dim_names(current_ncdr_id, num_dims, dim_name_mlen)
272  end if
273  else
274  if (present(dim_names)) then
275  call nc_diag_read_id_get_dim_names(current_ncdr_id, num_dims, dim_names = dim_names)
276  else
278  end if
279  end if
280  else
281  if (present(dim_name_mlen)) then
282  if (present(dim_names)) then
283  call nc_diag_read_id_get_dim_names(current_ncdr_id, dim_name_mlen = dim_name_mlen, &
284  dim_names = dim_names)
285  else
286  call nc_diag_read_id_get_dim_names(current_ncdr_id, dim_name_mlen = dim_name_mlen)
287  end if
288  else
289  if (present(dim_names)) then
290  call nc_diag_read_id_get_dim_names(current_ncdr_id, dim_names = dim_names)
291  else
292  ! Why would you do this?
294  end if
295  end if
296  end if
297  end subroutine nc_diag_read_noid_get_dim_names
298 end module ncdr_dims
logical function nc_diag_read_id_check_dim(file_ncdr_id, dim_name)
Definition: ncdr_dims.f90:140
integer(c_int) function pf_nf90_inq_unlimdims(ncid, num_unlim_dims, unlim_dims)
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
integer(i_long) function nc_diag_read_noid_assert_dim(dim_name)
Definition: ncdr_dims.f90:130
subroutine ncdr_nc_check(status)
Definition: ncdr_check.f90:85
integer(i_long) function nc_diag_read_id_lookup_dim(file_ncdr_id, dim_name)
Definition: ncdr_dims.f90:87
integer(i_long) current_ncdr_id
Definition: ncdr_state.f90:7
type(ncdr_file), dimension(:), allocatable ncdr_files
Definition: ncdr_state.f90:14
subroutine ncdr_check_current_ncdr_id
Definition: ncdr_check.f90:22
subroutine nc_diag_read_id_get_dim_names(file_ncdr_id, num_dims, dim_name_mlen, dim_names)
Definition: ncdr_dims.f90:218
integer(i_long) function nc_diag_read_id_assert_dim(file_ncdr_id, dim_name)
Definition: ncdr_dims.f90:114
integer(i_long) function nc_diag_read_id_get_dim(file_ncdr_id, dim_name)
Definition: ncdr_dims.f90:171
logical function nc_diag_read_noid_check_dim(dim_name)
Definition: ncdr_dims.f90:156
logical function nc_diag_read_noid_check_dim_unlim(dim_name)
Definition: ncdr_dims.f90:208
subroutine ncdr_check_ncdr_id(file_ncdr_id)
Definition: ncdr_check.f90:12
integer(i_long) function nc_diag_read_noid_get_dim(dim_name)
Definition: ncdr_dims.f90:184
integer(i_long) function nc_diag_read_noid_lookup_dim(dim_name)
Definition: ncdr_dims.f90:104
subroutine nc_diag_read_parse_file_dims(file_ncid, file_index, num_dims)
Definition: ncdr_dims.f90:47
logical function nc_diag_read_id_check_dim_unlim(file_ncdr_id, dim_name)
Definition: ncdr_dims.f90:194
subroutine ncdr_error(err)
Definition: ncdr_climsg.F90:13