FV3 Bundle
ncdw_dresize.F90
Go to the documentation of this file.
8  use ncdw_climsg, only: &
9 #ifdef enable_action_msgs
10  nclayer_enable_action, nclayer_actionm, &
11 #endif
12 #ifdef _DEBUG_MEM_
13  nclayer_debug, &
14 #endif
15  nclayer_error
16 
17  implicit none
18 
19  contains
20  ! For all subroutines: update_acount_in specifies wheter to
21  ! update acount or not. By default, this is true. This is useful
22  ! for preallocation, when you aren't actually adding entries,
23  ! so you're just allocating ahead of time and NOT adding
24  ! elements, thus not adding to acount.
25 
26  ! nc_diag_data2d_resize - input integer(i_byte)
27  ! Corresponding NetCDF4 type: byte
28  subroutine nc_diag_data2d_resize_byte(addl_num_entries, update_acount_in)
29  integer(i_llong), intent(in) :: addl_num_entries
30  logical, intent(in), optional :: update_acount_in
31 
32  ! This is the Size Count index (sc_index) - we'll just set
33  ! this and then just change the variable we're altering
34  ! every time.
35  integer(i_long) :: sc_index
36  integer(i_long) :: sc_index_vi
37 
38  logical :: update_acount
39 
40  ! Assume true by default
41  if (.NOT. present(update_acount_in)) then
42  update_acount = .true.
43  else
44  update_acount = update_acount_in
45  end if
46 
47  ! Here, we increment the count by the number of additional entries,
48  ! and the size by that amount as well.
49  !
50  ! If we didn't allocate yet, we simply set the count to the number of
51  ! initial entries, and then allocate that number + our default
52  ! initialization amount. Our initial size is that number + the initial
53  ! amount.
54 
55  ! NLAYER_BYTE is located at the first index, 1.
56  ! sc_index_vi is just sc_index + 6, 6 being the number of single types
57  sc_index = 1
58  sc_index_vi = sc_index + 6
59 
60  if (allocated(diag_data2d_store%m_byte)) then
61  if (update_acount) diag_data2d_store%acount(sc_index) = diag_data2d_store%acount(sc_index) + addl_num_entries
62  if (diag_data2d_store%acount(sc_index) >= diag_data2d_store%asize(sc_index)) then
63 #ifdef ENABLE_ACTION_MSGS
64  if (nclayer_enable_action) then
65  call nclayer_actionm("nc_diag_data2d_resize_byte: doing reallocation!")
66  end if
67 #endif
68  call nc_diag_realloc(diag_data2d_store%m_byte, int8(addl_num_entries + (nlayer_default_ent * (nlayer_multi_base ** diag_data2d_store%alloc_m_multi(sc_index)))))
69  diag_data2d_store%asize(sc_index) = size(diag_data2d_store%m_byte)
70 
71  diag_data2d_store%alloc_m_multi(sc_index) = diag_data2d_store%alloc_m_multi(sc_index) + 1
72  end if
73  else
74  if (update_acount) diag_data2d_store%acount(sc_index) = addl_num_entries
75  allocate(diag_data2d_store%m_byte(addl_num_entries + nlayer_default_ent))
76  diag_data2d_store%asize(sc_index) = addl_num_entries + nlayer_default_ent
77  end if
78  end subroutine nc_diag_data2d_resize_byte
79 
80  ! nc_diag_data2d_resize - input integer(i_short)
81  ! Corresponding NetCDF4 type: short
82  subroutine nc_diag_data2d_resize_short(addl_num_entries, update_acount_in)
83  integer(i_llong), intent(in) :: addl_num_entries
84  logical, intent(in), optional :: update_acount_in
85 
86  ! This is the Size Count index (sc_index) - we'll just set
87  ! this and then just change the variable we're altering
88  ! every time.
89  integer(i_long) :: sc_index
90  integer(i_long) :: sc_index_vi
91 
92  logical :: update_acount
93 
94  ! Assume true by default
95  if (.NOT. present(update_acount_in)) then
96  update_acount = .true.
97  else
98  update_acount = update_acount_in
99  end if
100 
101  ! Here, we increment the count by the number of additional entries,
102  ! and the size by that amount as well.
103  !
104  ! If we didn't allocate yet, we simply set the count to the number of
105  ! initial entries, and then allocate that number + our default
106  ! initialization amount. Our initial size is that number + the initial
107  ! amount.
108 
109  ! NLAYER_SHORT is located at the second index, 2.
110  ! sc_index_vi is just sc_index + 6, 6 being the number of single types
111  sc_index = 2
112  sc_index_vi = sc_index + 6
113 
114  if (allocated(diag_data2d_store%m_short)) then
115  if (update_acount) diag_data2d_store%acount(sc_index) = diag_data2d_store%acount(sc_index) + addl_num_entries
116  if (diag_data2d_store%acount(sc_index) >= diag_data2d_store%asize(sc_index)) then
117 #ifdef ENABLE_ACTION_MSGS
118  if (nclayer_enable_action) then
119  call nclayer_actionm("nc_diag_data2d_resize_short: doing reallocation!")
120  end if
121 #endif
122  call nc_diag_realloc(diag_data2d_store%m_short, int8(addl_num_entries + (nlayer_default_ent * (nlayer_multi_base ** diag_data2d_store%alloc_m_multi(sc_index)))))
123  diag_data2d_store%asize(sc_index) = size(diag_data2d_store%m_short)
124 
125  diag_data2d_store%alloc_m_multi(sc_index) = diag_data2d_store%alloc_m_multi(sc_index) + 1
126  end if
127  else
128 #ifdef _DEBUG_MEM_
129  print *, "nc_diag_data2d_resize_short: allocate NEW m_short"
130 #endif
131  if (update_acount) diag_data2d_store%acount(sc_index) = addl_num_entries
132  allocate(diag_data2d_store%m_short(addl_num_entries + nlayer_default_ent))
133  diag_data2d_store%asize(sc_index) = addl_num_entries + nlayer_default_ent
134  end if
135  end subroutine nc_diag_data2d_resize_short
136 
137  ! nc_diag_data2d_resize - input integer(i_long)
138  ! Corresponding NetCDF4 type: int (old: long)
139  subroutine nc_diag_data2d_resize_long(addl_num_entries, update_acount_in)
140  integer(i_llong), intent(in) :: addl_num_entries
141  logical, intent(in), optional :: update_acount_in
142 
143  ! Did we realloc at all?
144  !logical :: data2d_realloc
145 
146  ! This is the Size Count index (sc_index) - we'll just set
147  ! this and then just change the variable we're altering
148  ! every time.
149  integer(i_long) :: sc_index
150  integer(i_long) :: sc_index_vi
151 
152 #ifdef _DEBUG_MEM_
153  character(len=200) :: debugstr
154 #endif
155 
156  logical :: update_acount
157 
158  ! Assume true by default
159  if (.NOT. present(update_acount_in)) then
160  update_acount = .true.
161  else
162  update_acount = update_acount_in
163  end if
164 
165  ! Here, we increment the count by the number of additional entries,
166  ! and the size by that amount as well.
167  !
168  ! If we didn't allocate yet, we simply set the count to the number of
169  ! initial entries, and then allocate that number + our default
170  ! initialization amount. Our initial size is that number + the initial
171  ! amount.
172 
173  ! NLAYER_LONG is located at the third index, 3.
174  ! sc_index_vi is just sc_index + 6, 6 being the number of single types
175  sc_index = 3
176  sc_index_vi = sc_index + 6
177 
178  if (allocated(diag_data2d_store%m_long)) then
179  if (update_acount) diag_data2d_store%acount(sc_index) = diag_data2d_store%acount(sc_index) + addl_num_entries
180 
181 #ifdef _DEBUG_MEM_
182  write (debugstr, "(A, I1, A, I7, A, I7)") "In sc_index ", sc_index, ", the acount/asize is: ", diag_data2d_store%acount(sc_index), "/", diag_data2d_store%asize(sc_index)
183  call nclayer_debug(debugstr)
184 #endif
185 
186  if (diag_data2d_store%acount(sc_index) >= diag_data2d_store%asize(sc_index)) then
187 #ifdef _DEBUG_MEM_
188  call nclayer_debug("acount < asize, reallocating.")
189  print *, "Start long realloc..."
190 #endif
191 #ifdef ENABLE_ACTION_MSGS
192  if (nclayer_enable_action) then
193  call nclayer_actionm("nc_diag_data2d_resize_long: doing reallocation!")
194  end if
195 #endif
196  call nc_diag_realloc(diag_data2d_store%m_long, int8(addl_num_entries + (nlayer_default_ent * (nlayer_multi_base ** diag_data2d_store%alloc_m_multi(sc_index)))))
197  diag_data2d_store%asize(sc_index) = size(diag_data2d_store%m_long)
198 
199  diag_data2d_store%alloc_m_multi(sc_index) = diag_data2d_store%alloc_m_multi(sc_index) + 1
200 
201 #ifdef _DEBUG_MEM_
202  print *, "alloc_m_multi increased to:"
203  print *, diag_data2d_store%alloc_m_multi(sc_index)
204 #endif
205  end if
206  else
207 #ifdef _DEBUG_MEM_
208  print *, "nc_diag_data2d_resize_long: allocate NEW m_long"
209 #endif
210  if (update_acount) diag_data2d_store%acount(sc_index) = addl_num_entries
211  allocate(diag_data2d_store%m_long(addl_num_entries + nlayer_default_ent))
212  diag_data2d_store%asize(sc_index) = addl_num_entries + nlayer_default_ent
213  end if
214  end subroutine nc_diag_data2d_resize_long
215 
216  ! nc_diag_data2d_resize - input real(r_single)
217  ! Corresponding NetCDF4 type: float (or real)
218  subroutine nc_diag_data2d_resize_rsingle(addl_num_entries, update_acount_in)
219  integer(i_llong), intent(in) :: addl_num_entries
220  logical, intent(in), optional :: update_acount_in
221 
222  ! This is the Size Count index (sc_index) - we'll just set
223  ! this and then just change the variable we're altering
224  ! every time.
225  integer(i_long) :: sc_index
226  integer(i_long) :: sc_index_vi
227 
228  logical :: update_acount
229 
230  ! Assume true by default
231  if (.NOT. present(update_acount_in)) then
232  update_acount = .true.
233  else
234  update_acount = update_acount_in
235  end if
236 
237  ! Here, we increment the count by the number of additional entries,
238  ! and the size by that amount as well.
239  !
240  ! If we didn't allocate yet, we simply set the count to the number of
241  ! initial entries, and then allocate that number + our default
242  ! initialization amount. Our initial size is that number + the initial
243  ! amount.
244 
245  ! NLAYER_FLOAT is located at the fourth index, 4.
246  ! sc_index_vi is just sc_index + 6, 6 being the number of single types
247  sc_index = 4
248  sc_index_vi = sc_index + 6
249 
250  if (allocated(diag_data2d_store%m_rsingle)) then
251  if (update_acount) diag_data2d_store%acount(sc_index) = diag_data2d_store%acount(sc_index) + addl_num_entries
252  if (diag_data2d_store%acount(sc_index) >= diag_data2d_store%asize(sc_index)) then
253 #ifdef _DEBUG_MEM_
254  print *, "realloc needed for data2d rsingle!"
255  write (*, "(A, I0, A, I0, A)") "(size needed / size available: ", diag_data2d_store%acount(sc_index), " / ", diag_data2d_store%asize(sc_index), ")"
256 #endif
257 #ifdef ENABLE_ACTION_MSGS
258  if (nclayer_enable_action) then
259  call nclayer_actionm("nc_diag_data2d_resize_rsingle: doing reallocation!")
260  end if
261 #endif
262  call nc_diag_realloc(diag_data2d_store%m_rsingle, int8(addl_num_entries + (nlayer_default_ent * (nlayer_multi_base ** diag_data2d_store%alloc_m_multi(sc_index)))))
263  diag_data2d_store%asize(sc_index) = size(diag_data2d_store%m_rsingle)
264 
265  diag_data2d_store%alloc_m_multi(sc_index) = diag_data2d_store%alloc_m_multi(sc_index) + 1
266  end if
267  else
268  if (update_acount) diag_data2d_store%acount(sc_index) = addl_num_entries
269  allocate(diag_data2d_store%m_rsingle(addl_num_entries + nlayer_default_ent))
270  diag_data2d_store%asize(sc_index) = addl_num_entries + nlayer_default_ent
271  end if
272  end subroutine nc_diag_data2d_resize_rsingle
273 
274  ! nc_diag_data2d_resize - input real(r_double)
275  ! Corresponding NetCDF4 type: double
276  subroutine nc_diag_data2d_resize_rdouble(addl_num_entries, update_acount_in)
277  integer(i_llong), intent(in) :: addl_num_entries
278  logical, intent(in), optional :: update_acount_in
279 
280  ! This is the Size Count index (sc_index) - we'll just set
281  ! this and then just change the variable we're altering
282  ! every time.
283  integer(i_long) :: sc_index
284  integer(i_long) :: sc_index_vi
285 
286  logical :: update_acount
287 
288  ! Assume true by default
289  if (.NOT. present(update_acount_in)) then
290  update_acount = .true.
291  else
292  update_acount = update_acount_in
293  end if
294  ! Here, we increment the count by the number of additional entries,
295  ! and the size by that amount as well.
296  !
297  ! If we didn't allocate yet, we simply set the count to the number of
298  ! initial entries, and then allocate that number + our default
299  ! initialization amount. Our initial size is that number + the initial
300  ! amount.
301 
302  ! NLAYER_DOUBLE is located at the fifth index, 5.
303  ! sc_index_vi is just sc_index + 6, 6 being the number of single types
304  sc_index = 5
305  sc_index_vi = sc_index + 6
306 
307  if (allocated(diag_data2d_store%m_rdouble)) then
308  if (update_acount) diag_data2d_store%acount(sc_index) = diag_data2d_store%acount(sc_index) + addl_num_entries
309  if (diag_data2d_store%acount(sc_index) >= diag_data2d_store%asize(sc_index)) then
310 #ifdef ENABLE_ACTION_MSGS
311  if (nclayer_enable_action) then
312  call nclayer_actionm("nc_diag_data2d_resize_rdouble: doing reallocation!")
313  end if
314 #endif
315  call nc_diag_realloc(diag_data2d_store%m_rdouble, int8(addl_num_entries + (nlayer_default_ent * (nlayer_multi_base ** diag_data2d_store%alloc_m_multi(sc_index)))))
316  diag_data2d_store%asize(sc_index) = size(diag_data2d_store%m_rdouble)
317 
318  diag_data2d_store%alloc_m_multi(sc_index) = diag_data2d_store%alloc_m_multi(sc_index) + 1
319  end if
320  else
321  if (update_acount) diag_data2d_store%acount(sc_index) = addl_num_entries
322  allocate(diag_data2d_store%m_rdouble(addl_num_entries + nlayer_default_ent))
323  diag_data2d_store%asize(sc_index) = addl_num_entries + nlayer_default_ent
324  end if
325  end subroutine nc_diag_data2d_resize_rdouble
326 
327  ! nc_diag_data2d_resize - input character(len=*)
328  ! Corresponding NetCDF4 type: string? char?
329  subroutine nc_diag_data2d_resize_string(addl_num_entries, update_acount_in)
330  integer(i_llong), intent(in) :: addl_num_entries
331  logical, intent(in), optional :: update_acount_in
332 
333  ! This is the Size Count index (sc_index) - we'll just set
334  ! this and then just change the variable we're altering
335  ! every time.
336  integer(i_long) :: sc_index
337  integer(i_long) :: sc_index_vi
338 
339  logical :: update_acount
340 
341  ! Assume true by default
342  if (.NOT. present(update_acount_in)) then
343  update_acount = .true.
344  else
345  update_acount = update_acount_in
346  end if
347  ! Here, we increment the count by the number of additional entries,
348  ! and the size by that amount as well.
349  !
350  ! If we didn't allocate yet, we simply set the count to the number of
351  ! initial entries, and then allocate that number + our default
352  ! initialization amount. Our initial size is that number + the initial
353  ! amount.
354 
355  ! NLAYER_BYTE is located at the sixth index, 6.
356  ! sc_index_vi is just sc_index + 6, 6 being the number of single types
357  sc_index = 6
358  sc_index_vi = sc_index + 6
359 
360  if (allocated(diag_data2d_store%m_string)) then
361  if (update_acount) diag_data2d_store%acount(sc_index) = diag_data2d_store%acount(sc_index) + addl_num_entries
362  if (diag_data2d_store%acount(sc_index) >= diag_data2d_store%asize(sc_index)) then
363 #ifdef ENABLE_ACTION_MSGS
364  if (nclayer_enable_action) then
365  call nclayer_actionm("nc_diag_data2d_resize_string: doing reallocation!")
366  end if
367 #endif
368  call nc_diag_realloc(diag_data2d_store%m_string, int8(addl_num_entries + (nlayer_default_ent * (nlayer_multi_base ** diag_data2d_store%alloc_m_multi(sc_index)))))
369  diag_data2d_store%asize(sc_index) = size(diag_data2d_store%m_string)
370 
371  diag_data2d_store%alloc_m_multi(sc_index) = diag_data2d_store%alloc_m_multi(sc_index) + 1
372  end if
373  else
374  if (update_acount) diag_data2d_store%acount(sc_index) = addl_num_entries
375  allocate(diag_data2d_store%m_string(addl_num_entries + nlayer_default_ent))
376  diag_data2d_store%asize(sc_index) = addl_num_entries + nlayer_default_ent
377  end if
378  end subroutine nc_diag_data2d_resize_string
379 
380  subroutine nc_diag_data2d_resize_iarr_type(addl_num_entries)
381  integer(i_llong), intent(in) :: addl_num_entries
382 
383  type(diag_d2d_iarr), dimension(:), allocatable :: tmp_stor_i_arr
384 
385 #ifdef ENABLE_ACTION_MSGS
386  if (nclayer_enable_action) then
387  call nclayer_actionm("nc_diag_data2d_resize_iarr_type: doing reallocation!")
388  end if
389 #endif
390 
391  ! We need to realloc ourselves here...
392  allocate(tmp_stor_i_arr(size(diag_data2d_store%stor_i_arr) + addl_num_entries))
393  tmp_stor_i_arr(1:size(diag_data2d_store%stor_i_arr)) = diag_data2d_store%stor_i_arr
394  deallocate(diag_data2d_store%stor_i_arr)
395  allocate(diag_data2d_store%stor_i_arr(size(tmp_stor_i_arr)))
396  diag_data2d_store%stor_i_arr = tmp_stor_i_arr
397  deallocate(tmp_stor_i_arr)
398  end subroutine nc_diag_data2d_resize_iarr_type
399 
400  subroutine nc_diag_data2d_resize_iarr(iarr_index, addl_num_entries, update_icount_in)
401  integer(i_long), intent(in) :: iarr_index
402  integer(i_llong), intent(in) :: addl_num_entries
403  logical, intent(in), optional :: update_icount_in
404 
405  logical :: update_icount
406 
407  integer(i_llong) :: addl_num_entries_r
408 
409  ! Assume true by default
410  if (.NOT. present(update_icount_in)) then
411  update_icount = .true.
412  else
413  update_icount = update_icount_in
414  end if
415 
416  if (allocated(diag_data2d_store%stor_i_arr(iarr_index)%index_arr)) then
417  if (update_icount) diag_data2d_store%stor_i_arr(iarr_index)%icount = &
418  diag_data2d_store%stor_i_arr(iarr_index)%icount + addl_num_entries
419  if (diag_data2d_store%stor_i_arr(iarr_index)%icount >= diag_data2d_store%stor_i_arr(iarr_index)%isize) then
420 #ifdef _DEBUG_MEM_
421  print *, "realloc needed for data2d iarr!"
422  write (*, "(A, I0, A, I0, A)") "(size needed / size available: ", diag_data2d_store%stor_i_arr(iarr_index)%icount, " / ", diag_data2d_store%stor_i_arr(iarr_index)%isize, ")"
423  print *, diag_data2d_store%alloc_sia_multi(iarr_index)
424  print *, int8(nlayer_multi_base ** int8(diag_data2d_store%alloc_sia_multi(iarr_index)))
425 #endif
426 #ifdef ENABLE_ACTION_MSGS
427  if (nclayer_enable_action) then
428  call nclayer_actionm("nc_diag_data2d_resize_iarr: doing reallocation!")
429  end if
430 #endif
431 
432  if (update_icount) then
433  addl_num_entries_r = addl_num_entries + (int8(nlayer_default_ent) * (nlayer_multi_base ** int8(diag_data2d_store%alloc_sia_multi(iarr_index))))
434  else
435  addl_num_entries_r = addl_num_entries + nlayer_default_ent
436  end if
437 #ifdef _DEBUG_MEM_
438  print *, " ** addl_num_entries_r = "
439  print *, addl_num_entries_r
440 #endif
441  call nc_diag_realloc(diag_data2d_store%stor_i_arr(iarr_index)%index_arr, addl_num_entries_r)
442  call nc_diag_realloc(diag_data2d_store%stor_i_arr(iarr_index)%length_arr, addl_num_entries_r)
443 #ifdef _DEBUG_MEM_
444  print *, " ** realloc done"
445 #endif
446  diag_data2d_store%stor_i_arr(iarr_index)%isize = size(diag_data2d_store%stor_i_arr(iarr_index)%index_arr)
447 
448  if (update_icount) diag_data2d_store%alloc_sia_multi(iarr_index) = diag_data2d_store%alloc_sia_multi(iarr_index) + 1
449  end if
450  else
451  if (update_icount) diag_data2d_store%stor_i_arr(iarr_index)%icount = addl_num_entries
452  allocate(diag_data2d_store%stor_i_arr(iarr_index)%index_arr(addl_num_entries + nlayer_default_ent))
453  allocate(diag_data2d_store%stor_i_arr(iarr_index)%length_arr(addl_num_entries + nlayer_default_ent))
454  diag_data2d_store%stor_i_arr(iarr_index)%isize = addl_num_entries + nlayer_default_ent
455  end if
456  end subroutine nc_diag_data2d_resize_iarr
457 end module ncdw_dresize
type(diag_data2d), allocatable diag_data2d_store
Definition: ncdw_state.f90:18
integer(i_long), parameter nlayer_multi_base
Definition: ncdw_types.F90:27
integer, parameter, public i_byte
Definition: ncd_kinds.F90:45
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
subroutine nc_diag_data2d_resize_iarr_type(addl_num_entries)
subroutine nc_diag_data2d_resize_rdouble(addl_num_entries, update_acount_in)
subroutine nc_diag_data2d_resize_rsingle(addl_num_entries, update_acount_in)
subroutine nc_diag_data2d_resize_byte(addl_num_entries, update_acount_in)
subroutine nc_diag_data2d_resize_long(addl_num_entries, update_acount_in)
subroutine nc_diag_data2d_resize_iarr(iarr_index, addl_num_entries, update_icount_in)
subroutine nc_diag_data2d_resize_short(addl_num_entries, update_acount_in)
integer, parameter, public i_short
Definition: ncd_kinds.F90:46
integer(i_short), parameter nlayer_default_ent
Definition: ncdw_types.F90:18
subroutine nc_diag_data2d_resize_string(addl_num_entries, update_acount_in)
integer, parameter, public r_double
Definition: ncd_kinds.F90:80
integer, parameter, public r_single
Definition: ncd_kinds.F90:79
integer, parameter, public i_llong
Definition: ncd_kinds.F90:49