FV3 Bundle
ncdr_vars.f90
Go to the documentation of this file.
1 module ncdr_vars
2  use ncd_kinds, only: i_long
4  use ncdr_climsg, only: ncdr_error
8  use netcdf, only: nf90_inquire_variable, nf90_max_name
9 
10  implicit none
11 
13  module procedure nc_diag_read_id_lookup_var, &
15  end interface nc_diag_read_lookup_var
16 
18  module procedure nc_diag_read_id_check_var, &
20  end interface nc_diag_read_check_var
21 
23  module procedure nc_diag_read_id_get_var_ndims, &
25  end interface nc_diag_read_get_var_ndims
26 
28  module procedure nc_diag_read_id_get_var_type, &
30  end interface nc_diag_read_get_var_type
31 
33  module procedure nc_diag_read_id_ret_var_dims, &
35  end interface nc_diag_read_ret_var_dims
36 
38  module procedure nc_diag_read_id_get_var_dims, &
40  end interface nc_diag_read_get_var_dims
41 
43  module procedure nc_diag_read_id_get_var_names
44  end interface nc_diag_read_get_var_names
45 
47  module procedure nc_diag_read_noid_get_var_names
49 
50  contains
51  subroutine nc_diag_read_parse_file_vars(file_ncid, file_index, num_vars)
52  integer(i_long), intent(in) :: file_ncid
53  integer(i_long), intent(in) :: file_index
54  integer(i_long), intent(in) :: num_vars
55 
56  integer(i_long) :: i, j
57 
58  character(len=NF90_MAX_NAME) :: var_name
59 
60  ncdr_files(file_index)%nvars = num_vars
61  allocate(ncdr_files(file_index)%vars(num_vars))
62 
63  do i = 1, num_vars
64  ncdr_files(file_index)%vars(i)%var_id = i
65 
66  call ncdr_nc_check(nf90_inquire_variable(file_ncid, i, &
67  name = var_name, &
68  ndims = ncdr_files(file_index)%vars(i)%var_ndims, &
69  xtype = ncdr_files(file_index)%vars(i)%var_type))
70 
71  ncdr_files(file_index)%vars(i)%var_name = trim(var_name)
72 
73  allocate(ncdr_files(file_index)%vars(i)%var_dim_inds( &
74  ncdr_files(file_index)%vars(i)%var_ndims))
75 
76  call ncdr_nc_check(nf90_inquire_variable(file_ncid, i, &
77  dimids = ncdr_files(file_index)%vars(i)%var_dim_inds))
78 
79  ! Since the dimensions indicies are aligned to NetCDF's
80  ! indicies, we don't need to do any more analysis.
81  ! We're done with indices!
82 
83  ! Now, let's actually use them:
84  allocate(ncdr_files(file_index)%vars(i)%var_dim_sizes( &
85  ncdr_files(file_index)%vars(i)%var_ndims))
86 
87  do j = 1, ncdr_files(file_index)%vars(i)%var_ndims
88  ncdr_files(file_index)%vars(i)%var_dim_sizes(j) = &
89  ncdr_files(file_index)%dims( &
90  ncdr_files(file_index)%vars(i)%var_dim_inds(j) &
91  )%dim_size
92  end do
93  end do
94  end subroutine nc_diag_read_parse_file_vars
95 
96  function nc_diag_read_id_lookup_var(file_ncdr_id, var_name) result(var_index)
97  integer(i_long), intent(in) :: file_ncdr_id
98  character(len=*), intent(in) :: var_name
99 
100  integer(i_long) :: var_index
101 
102  call ncdr_check_ncdr_id(file_ncdr_id)
103 
104  do var_index = 1, ncdr_files(file_ncdr_id)%nvars
105  if (ncdr_files(file_ncdr_id)%vars(var_index)%var_name == var_name) &
106  return
107  end do
108 
109  ! Otherwise, return -1!
110  var_index = -1
111  end function nc_diag_read_id_lookup_var
112 
113  function nc_diag_read_noid_lookup_var(var_name) result(var_index)
114  character(len=*), intent(in) :: var_name
115 
116  integer(i_long) :: var_index
117 
119 
120  var_index = nc_diag_read_id_lookup_var(current_ncdr_id, var_name)
121  end function nc_diag_read_noid_lookup_var
122 
123  function nc_diag_read_id_check_var(file_ncdr_id, var_name) result(var_exists)
124  integer(i_long), intent(in) :: file_ncdr_id
125  character(len=*), intent(in) :: var_name
126 
127  logical :: var_exists
128 
129  call ncdr_check_ncdr_id(file_ncdr_id)
130 
131  if (nc_diag_read_id_lookup_var(file_ncdr_id, var_name) == -1) then
132  var_exists = .false.
133  return
134  end if
135 
136  var_exists = .true.
137  end function nc_diag_read_id_check_var
138 
139  function nc_diag_read_noid_check_var(var_name) result(var_exists)
140  character(len=*), intent(in) :: var_name
141 
142  logical :: var_exists
143 
145 
146  if (nc_diag_read_lookup_var(var_name) == -1) then
147  var_exists = .false.
148  return
149  end if
150 
151  var_exists = .true.
152  end function nc_diag_read_noid_check_var
153 
154  function nc_diag_read_id_get_var_ndims(file_ncdr_id, var_name) result(var_ndims)
155  integer(i_long), intent(in) :: file_ncdr_id
156  character(len=*), intent(in) :: var_name
157 
158  integer(i_long) :: var_index, var_ndims
159 
160  call ncdr_check_ncdr_id(file_ncdr_id)
161 
162  var_index = nc_diag_read_id_assert_var(file_ncdr_id, var_name)
163 
164  var_ndims = ncdr_files(file_ncdr_id)%vars(var_index)%var_ndims
165  end function nc_diag_read_id_get_var_ndims
166 
167  function nc_diag_read_noid_get_var_ndims(var_name) result(var_ndims)
168  character(len=*), intent(in) :: var_name
169 
170  integer(i_long) :: var_ndims
171 
173 
174  var_ndims = nc_diag_read_id_get_var_ndims(current_ncdr_id, var_name)
176 
177  function nc_diag_read_id_get_var_type(file_ncdr_id, var_name) result(var_type)
178  integer(i_long), intent(in) :: file_ncdr_id
179  character(len=*), intent(in) :: var_name
180 
181  integer(i_long) :: var_index, var_type
182 
183  call ncdr_check_ncdr_id(file_ncdr_id)
184 
185  var_index = nc_diag_read_id_assert_var(file_ncdr_id, var_name)
186 
187  var_type = ncdr_files(file_ncdr_id)%vars(var_index)%var_type
188  end function nc_diag_read_id_get_var_type
189 
190  function nc_diag_read_noid_get_var_type(var_name) result(var_type)
191  character(len=*), intent(in) :: var_name
192 
193  integer(i_long) :: var_type
194 
196 
198  end function nc_diag_read_noid_get_var_type
199 
200  function nc_diag_read_id_ret_var_dims(file_ncdr_id, var_name) result(var_dims)
201  integer(i_long), intent(in) :: file_ncdr_id
202  character(len=*), intent(in) :: var_name
203 
204  integer(i_long) :: var_index, var_ndims, i
205  integer(i_long), dimension(:), allocatable :: var_dims
206 
207  call ncdr_check_ncdr_id(file_ncdr_id)
208 
209  var_index = nc_diag_read_id_assert_var(file_ncdr_id, var_name)
210 
211  var_ndims = nc_diag_read_id_get_var_ndims(file_ncdr_id, var_name)
212 
213  allocate(var_dims(var_ndims))
214 
215  do i = 1, var_ndims
216  var_dims(i) = &
217  ncdr_files(file_ncdr_id)%dims( &
218  ncdr_files(file_ncdr_id)%vars(var_index)%var_dim_inds(i) &
219  )%dim_size
220  end do
221  end function nc_diag_read_id_ret_var_dims
222 
223  function nc_diag_read_noid_ret_var_dims(var_name) result(var_dims)
224  character(len=*), intent(in) :: var_name
225  integer(i_long), dimension(:), allocatable :: var_dims
226 
227  integer(i_long) :: var_ndims
228 
230 
231  var_ndims = nc_diag_read_id_get_var_ndims(current_ncdr_id, var_name)
232 
233  allocate(var_dims(var_ndims))
234 
235  var_dims = nc_diag_read_id_ret_var_dims(current_ncdr_id, var_name)
236  end function nc_diag_read_noid_ret_var_dims
237 
238  subroutine nc_diag_read_id_get_var_dims(file_ncdr_id, var_name, var_ndims, var_dims)
239  integer(i_long), intent(in) :: file_ncdr_id
240  character(len=*), intent(in) :: var_name
241  integer(i_long), intent(inout), optional :: var_ndims
242  integer(i_long), intent(inout), dimension(:), allocatable, optional :: var_dims
243 
244  integer(i_long) :: var_index, v_ndims, i
245 
246  call ncdr_check_ncdr_id(file_ncdr_id)
247 
248  var_index = nc_diag_read_id_assert_var(file_ncdr_id, var_name)
249 
250  v_ndims = nc_diag_read_id_get_var_ndims(file_ncdr_id, var_name)
251 
252  if (present(var_ndims)) &
253  var_ndims = v_ndims
254 
255  if (present(var_dims)) then
256  if (.NOT. allocated(var_dims)) then
257  allocate(var_dims(v_ndims))
258  else
259  if (size(var_dims) /= v_ndims) &
260  call ncdr_error("Invalid allocated array size for variable dimensions size storage!")
261  end if
262 
263  do i = 1, v_ndims
264  var_dims(i) = &
265  ncdr_files(file_ncdr_id)%dims( &
266  ncdr_files(file_ncdr_id)%vars(var_index)%var_dim_inds(i) &
267  )%dim_size
268  end do
269  end if
270  end subroutine nc_diag_read_id_get_var_dims
271 
272  subroutine nc_diag_read_noid_get_var_dims(var_name, var_ndims, var_dims)
273  character(len=*), intent(in) :: var_name
274  integer(i_long), intent(inout), optional :: var_ndims
275  integer(i_long), intent(inout), dimension(:), allocatable, optional :: var_dims
276 
278 
279  if (present(var_ndims)) then
280  if (present(var_dims)) then
281  call nc_diag_read_id_get_var_dims(current_ncdr_id, var_name, var_ndims, var_dims)
282  else
283  call nc_diag_read_id_get_var_dims(current_ncdr_id, var_name, var_ndims)
284  end if
285  else
286  if (present(var_dims)) then
287  call nc_diag_read_id_get_var_dims(current_ncdr_id, var_name, var_dims = var_dims)
288  else
289  ! Why you want to do this, I dunno...
291  end if
292  end if
293  end subroutine nc_diag_read_noid_get_var_dims
294 
295  subroutine nc_diag_read_id_get_var_names(file_ncdr_id, num_vars, var_name_mlen, var_names)
296  integer(i_long), intent(in) :: file_ncdr_id
297  integer(i_long), intent(out), optional :: num_vars
298  integer(i_long), intent(out), optional :: var_name_mlen
299  character(len=:), intent(inout), dimension(:), allocatable, optional:: var_names
300 
301  integer(i_long) :: var_index, nvars, max_var_name_len
302 
303  max_var_name_len = 0
304 
305  call ncdr_check_ncdr_id(file_ncdr_id)
306 
307  nvars = ncdr_files(file_ncdr_id)%nvars
308 
309  if (present(num_vars)) &
310  num_vars = nvars
311 
312  ! Figure out character max length
313  do var_index = 1, nvars
314  if (len(ncdr_files(file_ncdr_id)%vars(var_index)%var_name) > max_var_name_len) &
315  max_var_name_len = len(ncdr_files(file_ncdr_id)%vars(var_index)%var_name)
316  end do
317 
318  if (present(var_name_mlen)) &
319  var_name_mlen = max_var_name_len
320 
321  if (present(var_names)) then
322  if (.NOT. allocated(var_names)) then
323  allocate(character(max_var_name_len) :: var_names(nvars))
324  else
325  if (size(var_names) /= nvars) &
326  call ncdr_error("Invalid allocated array size for variable names storage!")
327  if (len(var_names) < max_var_name_len) &
328  call ncdr_error("Invalid allocated array size for variable names storage! (String length does not match!)")
329  end if
330 
331  do var_index = 1, nvars
332  var_names(var_index) = ncdr_files(file_ncdr_id)%vars(var_index)%var_name
333  end do
334  end if
335  end subroutine nc_diag_read_id_get_var_names
336 
337  subroutine nc_diag_read_noid_get_var_names(num_vars, var_name_mlen, var_names)
338  integer(i_long), intent(out), optional :: num_vars
339  integer(i_long), intent(out), optional :: var_name_mlen
340  character(len=:), intent(inout), dimension(:), allocatable, optional:: var_names
341 
343 
344  if (present(num_vars)) then
345  if (present(var_name_mlen)) then
346  if (present(var_names)) then
347  call nc_diag_read_id_get_var_names(current_ncdr_id, num_vars, var_name_mlen, var_names)
348  else
349  call nc_diag_read_id_get_var_names(current_ncdr_id, num_vars, var_name_mlen)
350  end if
351  else
352  if (present(var_names)) then
353  call nc_diag_read_id_get_var_names(current_ncdr_id, num_vars, var_names = var_names)
354  else
356  end if
357  end if
358  else
359  if (present(var_name_mlen)) then
360  if (present(var_names)) then
361  call nc_diag_read_id_get_var_names(current_ncdr_id, var_name_mlen = var_name_mlen, &
362  var_names = var_names)
363  else
364  call nc_diag_read_id_get_var_names(current_ncdr_id, var_name_mlen = var_name_mlen)
365  end if
366  else
367  if (present(var_names)) then
368  call nc_diag_read_id_get_var_names(current_ncdr_id, var_names = var_names)
369  else
370  ! Why would you do this?
372  end if
373  end if
374  end if
375  end subroutine nc_diag_read_noid_get_var_names
376 end module ncdr_vars
subroutine nc_diag_read_noid_get_var_dims(var_name, var_ndims, var_dims)
Definition: ncdr_vars.f90:273
integer(i_long) function, dimension(:), allocatable nc_diag_read_id_ret_var_dims(file_ncdr_id, var_name)
Definition: ncdr_vars.f90:201
integer(i_long) function nc_diag_read_id_get_var_type(file_ncdr_id, var_name)
Definition: ncdr_vars.f90:178
logical function nc_diag_read_noid_check_var(var_name)
Definition: ncdr_vars.f90:140
integer(i_long) function nc_diag_read_noid_get_var_type(var_name)
Definition: ncdr_vars.f90:191
subroutine nc_diag_read_id_get_var_dims(file_ncdr_id, var_name, var_ndims, var_dims)
Definition: ncdr_vars.f90:239
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
subroutine nc_diag_read_parse_file_vars(file_ncid, file_index, num_vars)
Definition: ncdr_vars.f90:52
subroutine ncdr_nc_check(status)
Definition: ncdr_check.f90:85
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
integer(i_long) function, dimension(:), allocatable nc_diag_read_noid_ret_var_dims(var_name)
Definition: ncdr_vars.f90:224
integer(i_long) function nc_diag_read_id_lookup_var(file_ncdr_id, var_name)
Definition: ncdr_vars.f90:97
integer(i_long) function nc_diag_read_noid_get_var_ndims(var_name)
Definition: ncdr_vars.f90:168
integer(i_long) function nc_diag_read_noid_lookup_var(var_name)
Definition: ncdr_vars.f90:114
integer(i_long) function nc_diag_read_id_assert_var(file_ncdr_id, var_name)
logical function nc_diag_read_id_check_var(file_ncdr_id, var_name)
Definition: ncdr_vars.f90:124
subroutine ncdr_check_ncdr_id(file_ncdr_id)
Definition: ncdr_check.f90:12
subroutine ncdr_error(err)
Definition: ncdr_climsg.F90:13
integer(i_long) function nc_diag_read_id_get_var_ndims(file_ncdr_id, var_name)
Definition: ncdr_vars.f90:155
subroutine nc_diag_read_id_get_var_names(file_ncdr_id, num_vars, var_name_mlen, var_names)
Definition: ncdr_vars.f90:296