FV3 Bundle
ncdw_varattr.F90
Go to the documentation of this file.
4  use ncdw_state, only: init_done, append_only, ncid, &
10  use netcdf, only: nf90_inq_dimid, nf90_def_dim, nf90_put_att, &
11  nf90_unlimited
12 
13  implicit none
14 
15  interface nc_diag_varattr
16  module procedure nc_diag_varattr_byte, &
23  end interface nc_diag_varattr
24 
25  contains
26  function nc_diag_varattr_check_var(var_name) result(found)
27  character(len=*), intent(in) :: var_name
28  integer :: i
29  logical :: found
30  found = .false.
31 
32  if (init_done .AND. allocated(diag_varattr_store)) then
33  do i = 1, diag_varattr_store%total
34  if (diag_varattr_store%names(i) == var_name) then
35  found = .true.
36  exit
37  end if
38  end do
39  end if
40  end function nc_diag_varattr_check_var
41 
42  function nc_diag_varattr_lookup_var(var_name) result(ind)
43  character(len=*), intent(in) :: var_name
44  integer :: i, ind
45 
46  ind = -1
47 
48  if (init_done .AND. allocated(diag_varattr_store)) then
49  do i = 1, diag_varattr_store%total
50  if (diag_varattr_store%names(i) == var_name) then
51  ind = i
52  exit
53  end if
54  end do
55  end if
56  end function nc_diag_varattr_lookup_var
57 
59  if (init_done .AND. allocated(diag_varattr_store)) then
60  if (diag_varattr_store%nobs_dim_id == -1) then
61  if (append_only) then
62  ! Fetch the nobs dimension ID instead!
63  call nclayer_check(nf90_inq_dimid(ncid, "nobs", diag_varattr_store%nobs_dim_id))
64  else
65  call nclayer_check(nf90_def_dim(ncid, "nobs", nf90_unlimited, diag_varattr_store%nobs_dim_id))
66  end if
67  end if
68  else
69  call nclayer_error("NetCDF4 layer not initialized yet!")
70  end if
71  end subroutine nc_diag_varattr_make_nobs_dim
72 
73  subroutine nc_diag_varattr_expand(addl_fields)
74  integer(i_llong), intent(in) :: addl_fields
75  integer(i_llong) :: size_add
76 
77  if (init_done .AND. allocated(diag_varattr_store)) then
78  if (allocated(diag_varattr_store%names)) then
79  if (diag_varattr_store%total >= size(diag_varattr_store%names)) then
80  size_add = (size(diag_varattr_store%names) * 0.5) + addl_fields
81  call nc_diag_realloc(diag_varattr_store%names, addl_fields)
82  end if
83  else
84  allocate(diag_varattr_store%names(nlayer_default_ent))
85  end if
86 
87  if (allocated(diag_varattr_store%types)) then
88  if (diag_varattr_store%total >= size(diag_varattr_store%types)) then
89  size_add = (size(diag_varattr_store%types) * 0.5) + addl_fields
90  call nc_diag_realloc(diag_varattr_store%types, size_add)
91  end if
92  else
93  allocate(diag_varattr_store%types(nlayer_default_ent))
94  diag_varattr_store%types = -1
95  end if
96 
97  if (allocated(diag_varattr_store%var_ids)) then
98  if (diag_varattr_store%total >= size(diag_varattr_store%var_ids)) then
99  size_add = (size(diag_varattr_store%var_ids) * 0.5) + addl_fields
100  call nc_diag_realloc(diag_varattr_store%var_ids, size_add)
101  end if
102  else
103  allocate(diag_varattr_store%var_ids(nlayer_default_ent))
104  diag_varattr_store%var_ids = -1
105  end if
106 
107  else
108  call nclayer_error("NetCDF4 layer not initialized yet!")
109  endif
110 
111  end subroutine nc_diag_varattr_expand
112 
113  subroutine nc_diag_varattr_add_var(var_name, var_type, var_id)
114  character(len=*), intent(in) :: var_name
115  integer(i_byte), intent(in) :: var_type
116  integer(i_long) :: var_id
117 
118  if (nc_diag_varattr_check_var(var_name)) then
119  call nclayer_error("Variable already exists for variable attributes!")
120  else
121 #ifdef _DEBUG_MEM_
122  print *, "adding var!"
123 #endif
124  call nc_diag_varattr_expand(1_i_llong)
125  diag_varattr_store%total = diag_varattr_store%total + 1
126  diag_varattr_store%names(diag_varattr_store%total) = var_name
128  diag_varattr_store%var_ids(diag_varattr_store%total) = var_id
129 #ifdef _DEBUG_MEM_
130  print *, "done adding var!"
131 #endif
132  end if
133  end subroutine nc_diag_varattr_add_var
134 
135  ! nc_diag_varattr - input integer(i_byte)
136  ! Corresponding NetCDF4 type: byte
137  subroutine nc_diag_varattr_byte(var_name, attr_name, attr_value)
138  character(len=*), intent(in) :: var_name
139  character(len=*), intent(in) :: attr_name
140  integer(i_byte), intent(in) :: attr_value
141 
142  integer(i_long) :: var_index
143 
144  if (nc_diag_varattr_check_var(var_name)) then
145  var_index = nc_diag_varattr_lookup_var(var_name)
146  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
147  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
148  else
149  call nclayer_error("Can't set attribute for a non-existent variable!" &
150  // char(10) &
151  // " (If you did add the variable, make sure you lock" &
152  // char(10) &
153  // " the definitions before calling varattr!) ")
154  end if
155  end subroutine nc_diag_varattr_byte
156 
157  ! nc_diag_varattr - input integer(i_short)
158  ! Corresponding NetCDF4 type: short
159  subroutine nc_diag_varattr_short(var_name, attr_name, attr_value)
160  character(len=*), intent(in) :: var_name
161  character(len=*), intent(in) :: attr_name
162  integer(i_short), intent(in) :: attr_value
163 
164  integer(i_long) :: var_index
165 
166  if (nc_diag_varattr_check_var(var_name)) then
167  var_index = nc_diag_varattr_lookup_var(var_name)
168  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
169  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
170  else
171  call nclayer_error("Can't set attribute for a non-existent variable!" &
172  // char(10) &
173  // " (If you did add the variable, make sure you lock" &
174  // char(10) &
175  // " the definitions before calling varattr!) ")
176  end if
177  end subroutine nc_diag_varattr_short
178 
179  ! nc_diag_varattr - input integer(i_long)
180  ! Corresponding NetCDF4 type: int (old: long)
181  subroutine nc_diag_varattr_long(var_name, attr_name, attr_value)
182  character(len=*), intent(in) :: var_name
183  character(len=*), intent(in) :: attr_name
184  integer(i_long), intent(in) :: attr_value
185 
186  integer(i_long) :: var_index
187 
188  if (nc_diag_varattr_check_var(var_name)) then
189  var_index = nc_diag_varattr_lookup_var(var_name)
190  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
191  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
192  else
193  call nclayer_error("Can't set attribute for a non-existent variable!" &
194  // char(10) &
195  // " (If you did add the variable, make sure you lock" &
196  // char(10) &
197  // " the definitions before calling varattr!) ")
198  end if
199  end subroutine nc_diag_varattr_long
200 
201  ! nc_diag_varattr - input real(r_single)
202  ! Corresponding NetCDF4 type: float (or real)
203  subroutine nc_diag_varattr_rsingle(var_name, attr_name, attr_value)
204  character(len=*), intent(in) :: var_name
205  character(len=*), intent(in) :: attr_name
206  real(r_single), intent(in) :: attr_value
207 
208  integer(i_long) :: var_index
209 
210  if (nc_diag_varattr_check_var(var_name)) then
211  var_index = nc_diag_varattr_lookup_var(var_name)
212  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
213  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
214  else
215  call nclayer_error("Can't set attribute for a non-existent variable!" &
216  // char(10) &
217  // " (If you did add the variable, make sure you lock" &
218  // char(10) &
219  // " the definitions before calling varattr!) ")
220  end if
221  end subroutine nc_diag_varattr_rsingle
222 
223  ! nc_diag_varattr - input real(r_double)
224  ! Corresponding NetCDF4 type: double
225  subroutine nc_diag_varattr_rdouble(var_name, attr_name, attr_value)
226  character(len=*), intent(in) :: var_name
227  character(len=*), intent(in) :: attr_name
228  real(r_double), intent(in) :: attr_value
229 
230  integer(i_long) :: var_index
231 
232  if (nc_diag_varattr_check_var(var_name)) then
233  var_index = nc_diag_varattr_lookup_var(var_name)
234  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
235  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
236  else
237  call nclayer_error("Can't set attribute for a non-existent variable!" &
238  // char(10) &
239  // " (If you did add the variable, make sure you lock" &
240  // char(10) &
241  // " the definitions before calling varattr!) ")
242  end if
243  end subroutine nc_diag_varattr_rdouble
244 
245  ! nc_diag_varattr - input character(len=*)
246  ! Corresponding NetCDF4 type: string? char?
247  subroutine nc_diag_varattr_string(var_name, attr_name, attr_value)
248  character(len=*), intent(in) :: var_name
249  character(len=*), intent(in) :: attr_name
250  character(len=*), intent(in) :: attr_value
251 
252  integer(i_long) :: var_index
253 
254  if (nc_diag_varattr_check_var(var_name)) then
255  var_index = nc_diag_varattr_lookup_var(var_name)
256  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
257  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
258  else
259  call nclayer_error("Can't set attribute for a non-existent variable!" &
260  // char(10) &
261  // " (If you did add the variable, make sure you lock" &
262  // char(10) &
263  // " the definitions before calling varattr!) ")
264  end if
265  end subroutine nc_diag_varattr_string
266 
267  !=============================================================
268  ! VECTOR TYPES
269  !=============================================================
270 
271  ! nc_diag_varattr - input integer(i_byte), dimension(:)
272  ! Corresponding NetCDF4 type: byte
273  subroutine nc_diag_varattr_byte_v(var_name, attr_name, attr_value)
274  character(len=*), intent(in) :: var_name
275  character(len=*), intent(in) :: attr_name
276  integer(i_byte), dimension(:), intent(in) :: attr_value
277 
278  integer(i_long) :: var_index
279 
280  if (nc_diag_varattr_check_var(var_name)) then
281  var_index = nc_diag_varattr_lookup_var(var_name)
282  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
283  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
284  else
285  call nclayer_error("Can't set attribute for a non-existent variable!" &
286  // char(10) &
287  // " (If you did add the variable, make sure you lock" &
288  // char(10) &
289  // " the definitions before calling varattr!) ")
290  end if
291  end subroutine nc_diag_varattr_byte_v
292 
293  ! nc_diag_varattr - input integer(i_short)
294  ! Corresponding NetCDF4 type: short
295  subroutine nc_diag_varattr_short_v(var_name, attr_name, attr_value)
296  character(len=*), intent(in) :: var_name
297  character(len=*), intent(in) :: attr_name
298  integer(i_short), dimension(:), intent(in) :: attr_value
299 
300  integer(i_long) :: var_index
301 
302  if (nc_diag_varattr_check_var(var_name)) then
303  var_index = nc_diag_varattr_lookup_var(var_name)
304  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
305  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
306  else
307  call nclayer_error("Can't set attribute for a non-existent variable!" &
308  // char(10) &
309  // " (If you did add the variable, make sure you lock" &
310  // char(10) &
311  // " the definitions before calling varattr!) ")
312  end if
313  end subroutine nc_diag_varattr_short_v
314 
315  ! nc_diag_varattr - input integer(i_long)
316  ! Corresponding NetCDF4 type: int (old: long)
317  subroutine nc_diag_varattr_long_v(var_name, attr_name, attr_value)
318  character(len=*), intent(in) :: var_name
319  character(len=*), intent(in) :: attr_name
320  integer(i_long), dimension(:), intent(in) :: attr_value
321 
322  integer(i_long) :: var_index
323 
324  if (nc_diag_varattr_check_var(var_name)) then
325  var_index = nc_diag_varattr_lookup_var(var_name)
326  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
327  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
328  else
329  call nclayer_error("Can't set attribute for a non-existent variable!" &
330  // char(10) &
331  // " (If you did add the variable, make sure you lock" &
332  // char(10) &
333  // " the definitions before calling varattr!) ")
334  end if
335  end subroutine nc_diag_varattr_long_v
336 
337  ! nc_diag_varattr - input real(r_single)
338  ! Corresponding NetCDF4 type: float (or real)
339  subroutine nc_diag_varattr_rsingle_v(var_name, attr_name, attr_value)
340  character(len=*), intent(in) :: var_name
341  character(len=*), intent(in) :: attr_name
342  real(r_single), dimension(:), intent(in) :: attr_value
343 
344  integer(i_long) :: var_index
345 
346  if (nc_diag_varattr_check_var(var_name)) then
347  var_index = nc_diag_varattr_lookup_var(var_name)
348  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
349  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
350  else
351  call nclayer_error("Can't set attribute for a non-existent variable!" &
352  // char(10) &
353  // " (If you did add the variable, make sure you lock" &
354  // char(10) &
355  // " the definitions before calling varattr!) ")
356  end if
357  end subroutine nc_diag_varattr_rsingle_v
358 
359  ! nc_diag_varattr - input real(r_double)
360  ! Corresponding NetCDF4 type: double
361  subroutine nc_diag_varattr_rdouble_v(var_name, attr_name, attr_value)
362  character(len=*), intent(in) :: var_name
363  character(len=*), intent(in) :: attr_name
364  real(r_double), dimension(:), intent(in) :: attr_value
365 
366  integer(i_long) :: var_index
367 
368  if (nc_diag_varattr_check_var(var_name)) then
369  var_index = nc_diag_varattr_lookup_var(var_name)
370  if (var_index == -1) call nclayer_error("Bug! Variable exists but could not lookup index for attr!")
371  call nclayer_check(nf90_put_att(ncid, diag_varattr_store%var_ids(var_index), attr_name, attr_value))
372  else
373  call nclayer_error("Can't set attribute for a non-existent variable!" &
374  // char(10) &
375  // " (If you did add the variable, make sure you lock" &
376  // char(10) &
377  // " the definitions before calling varattr!) ")
378  end if
379  end subroutine nc_diag_varattr_rdouble_v
380 end module ncdw_varattr
subroutine nc_diag_varattr_short_v(var_name, attr_name, attr_value)
logical function nc_diag_varattr_check_var(var_name)
subroutine nc_diag_varattr_expand(addl_fields)
subroutine nc_diag_varattr_long_v(var_name, attr_name, attr_value)
integer, parameter, public i_byte
Definition: ncd_kinds.F90:45
subroutine nc_diag_varattr_short(var_name, attr_name, attr_value)
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
logical init_done
Definition: ncdw_state.f90:9
subroutine nc_diag_varattr_rsingle_v(var_name, attr_name, attr_value)
type(diag_varattr), allocatable diag_varattr_store
Definition: ncdw_state.f90:19
subroutine nclayer_check(status)
subroutine nc_diag_varattr_rdouble(var_name, attr_name, attr_value)
integer(i_long) ncid
Definition: ncdw_state.f90:8
subroutine nc_diag_varattr_rsingle(var_name, attr_name, attr_value)
subroutine nc_diag_varattr_string(var_name, attr_name, attr_value)
logical append_only
Definition: ncdw_state.f90:10
subroutine nc_diag_varattr_long(var_name, attr_name, attr_value)
subroutine nclayer_warning(warn)
integer, parameter, public i_short
Definition: ncd_kinds.F90:46
subroutine nc_diag_varattr_add_var(var_name, var_type, var_id)
integer(i_short), parameter nlayer_default_ent
Definition: ncdw_types.F90:18
subroutine nc_diag_varattr_rdouble_v(var_name, attr_name, attr_value)
subroutine nclayer_error(err)
Definition: ncdw_climsg.F90:97
integer, parameter, public r_double
Definition: ncd_kinds.F90:80
subroutine nc_diag_varattr_byte_v(var_name, attr_name, attr_value)
integer, parameter, public r_single
Definition: ncd_kinds.F90:79
integer, parameter, public i_llong
Definition: ncd_kinds.F90:49
subroutine nc_diag_varattr_byte(var_name, attr_name, attr_value)
integer function nc_diag_varattr_lookup_var(var_name)
subroutine nc_diag_varattr_make_nobs_dim