FV3 Bundle
ncdc_realloc.F90
Go to the documentation of this file.
4 
5  implicit none
6 
7  !===============================================================
8  ! nc_diag_realloc - reallocation support (declaration)
9  !===============================================================
10  ! DO NOT COMPILE THIS DIRECTLY! THIS IS MEANT TO BE INCLUDED
11  ! INSIDE A LARGER F90 SOURCE!
12  ! If you compile this directly, you WILL face the WRATH of your
13  ! compiler!
14  !---------------------------------------------------------------
15  ! Depends on: nothing
16  !---------------------------------------------------------------
17  ! nc_diag_realloc subroutines provide reallocation functionality
18  ! for various inputs.
19  !---------------------------------------------------------------
20  ! This file provides the interface wrapper for the array
21  ! reallocation subroutines. This is so that others can simply
22  ! call nc_diag_realloc with the necessary arguments, instead of
23  ! having to call the specific nc_diag_realloc_* subroutines.
24 
25  interface nc_diag_realloc
26  module procedure nc_diag_realloc_byte, &
31  end interface nc_diag_realloc
32 
33  contains
34  ! nc_diag_realloc_byte(arr, addl_num_entries)
35  ! input:
36  ! integer(i_byte), dimension(:) :: arr
37  ! array to reallocate
38  ! integer(i_long), intent(in) :: addl_num_entries
39  ! additional number of elements to allocate to the
40  ! specified array
41  subroutine nc_diag_realloc_byte(arr, addl_num_entries)
42  integer(i_byte), dimension(:), allocatable, intent(inout) :: arr
43  integer(i_long),intent(in) :: addl_num_entries
44 
45  integer(i_byte), dimension(:), allocatable :: tmp
46  integer(i_long) :: new_size
47 
48  integer(i_byte) :: alloc_err
49  character(len=100) :: err_msg
50 
51  new_size = size(arr) + addl_num_entries
52 
53  allocate(tmp(new_size), stat=alloc_err)
54  if (alloc_err /= 0) then
55  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
56  call nc_diag_realloc_error(trim(err_msg))
57  end if
58  tmp(1:size(arr)) = arr
59  deallocate(arr)
60  allocate(arr(new_size))
61  arr = tmp
62  end subroutine nc_diag_realloc_byte
63 
64  ! nc_diag_realloc_short(arr, addl_num_entries)
65  ! input:
66  ! integer(i_short), dimension(:) :: arr
67  ! array to reallocate
68  ! integer(i_long), intent(in) :: addl_num_entries
69  ! additional number of elements to allocate to the
70  ! specified array
71  subroutine nc_diag_realloc_short(arr, addl_num_entries)
72  integer(i_short), dimension(:), allocatable, intent(inout) :: arr
73  integer(i_long),intent(in) :: addl_num_entries
74 
75  integer(i_short), dimension(:), allocatable :: tmp
76  integer(i_long) :: new_size
77 
78  integer(i_byte) :: alloc_err
79  character(len=100) :: err_msg
80 
81  new_size = size(arr) + addl_num_entries
82 
83  allocate(tmp(new_size), stat=alloc_err)
84  if (alloc_err /= 0) then
85  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
86  call nc_diag_realloc_error(trim(err_msg))
87  end if
88  tmp(1:size(arr)) = arr
89  deallocate(arr)
90  allocate(arr(new_size))
91  arr = tmp
92  end subroutine nc_diag_realloc_short
93 
94  ! nc_diag_realloc_long(arr, addl_num_entries)
95  ! input:
96  ! integer(i_long), dimension(:) :: arr
97  ! array to reallocate
98  ! integer(i_long), intent(in) :: addl_num_entries
99  ! additional number of elements to allocate to the
100  ! specified array
101  subroutine nc_diag_realloc_long(arr, addl_num_entries)
102  integer(i_long), dimension(:), allocatable, intent(inout) :: arr
103  integer(i_long),intent(in) :: addl_num_entries
104 
105  integer(i_long), dimension(:), allocatable :: tmp
106  integer(i_long) :: new_size
107 
108  integer(i_byte) :: alloc_err
109  character(len=100) :: err_msg
110 
111 #ifdef _DEBUG_MEM_
112  call debug("Reallocating long array...")
113 #endif
114 
115  new_size = size(arr) + addl_num_entries
116 
117 #ifdef _DEBUG_MEM_
118  print *, "REALLOCATOR: new_size is ", new_size
119 #endif
120 
121  allocate(tmp(new_size), stat=alloc_err)
122  if (alloc_err /= 0) then
123  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
124  call nc_diag_realloc_error(trim(err_msg))
125  end if
126 
127  tmp(1:size(arr)) = arr
128  deallocate(arr)
129  allocate(arr(new_size))
130  arr = tmp
131 
132 #ifdef _DEBUG_MEM_
133  print *, "REALLOCATOR: final actual size is ", size(arr)
134  call debug("Realloc finished for long")
135 #endif
136  end subroutine nc_diag_realloc_long
137 
138  ! nc_diag_realloc_rsingle(arr, addl_num_entries)
139  ! input:
140  ! real(r_single), dimension(:) :: arr
141  ! array to reallocate
142  ! integer(i_long), intent(in) :: addl_num_entries
143  ! additional number of elements to allocate to the
144  ! specified array
145  subroutine nc_diag_realloc_rsingle(arr, addl_num_entries)
146  real(r_single), dimension(:), allocatable, intent(inout) :: arr
147  integer(i_long),intent(in) :: addl_num_entries
148 
149  real(r_single), dimension(:), allocatable :: tmp
150  integer(i_long) :: new_size
151 
152  integer(i_byte) :: alloc_err
153  character(len=100) :: err_msg
154 
155  new_size = size(arr) + addl_num_entries
156 
157  allocate(tmp(new_size), stat=alloc_err)
158  if (alloc_err /= 0) then
159  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
160  call nc_diag_realloc_error(trim(err_msg))
161  end if
162  tmp(1:size(arr)) = arr
163  deallocate(arr)
164  allocate(arr(new_size))
165  arr = tmp
166  end subroutine nc_diag_realloc_rsingle
167 
168  ! nc_diag_realloc_rdouble(arr, addl_num_entries)
169  ! input:
170  ! real(r_double), dimension(:) :: arr
171  ! array to reallocate
172  ! integer(i_long), intent(in) :: addl_num_entries
173  ! additional number of elements to allocate to the
174  ! specified array
175  subroutine nc_diag_realloc_rdouble(arr, addl_num_entries)
176  real(r_double), dimension(:), allocatable, intent(inout) :: arr
177  integer(i_long),intent(in) :: addl_num_entries
178 
179  real(r_double), dimension(:), allocatable :: tmp
180  integer(i_long) :: new_size
181 
182  integer(i_byte) :: alloc_err
183  character(len=100) :: err_msg
184 
185  new_size = size(arr) + addl_num_entries
186 
187  allocate(tmp(new_size), stat=alloc_err)
188  if (alloc_err /= 0) then
189  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
190  call nc_diag_realloc_error(trim(err_msg))
191  end if
192  tmp(1:size(arr)) = arr
193  deallocate(arr)
194  allocate(arr(new_size))
195  arr = tmp
196  end subroutine nc_diag_realloc_rdouble
197 
198  ! nc_diag_realloc_string(arr, addl_num_entries)
199  ! input:
200  ! character(len=*), dimension(:) :: arr
201  ! array to reallocate
202  ! integer(i_long), intent(in) :: addl_num_entries
203  ! additional number of elements to allocate to the
204  ! specified array
205  subroutine nc_diag_realloc_string(arr, addl_num_entries)
206  character(len=*), dimension(:), allocatable, intent(inout) :: arr
207  integer(i_long),intent(in) :: addl_num_entries
208 
209  character(len=len(arr(1))), dimension(:), allocatable :: tmp
210  integer(i_long) :: new_size
211 
212  integer(i_byte) :: alloc_err
213  character(len=100) :: err_msg
214 
215 #ifdef _DEBUG_MEM_
216  integer(i_long) :: string_len, string_arr_size
217 
218  string_len = len(arr(1))
219  string_arr_size = size(arr)
220 
221  call debug("[string] Length of string to allocate to:")
222  print *, string_len
223 
224  call debug("[string] Allocating from...")
225  print *, string_arr_size
226 
227  call debug("[string] ...to size...")
228  print *, (string_arr_size + addl_num_entries)
229 #endif
230 
231  new_size = size(arr) + addl_num_entries
232 
233  allocate(tmp(new_size), stat=alloc_err)
234  if (alloc_err /= 0) then
235  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
236  call nc_diag_realloc_error(trim(err_msg))
237  end if
238  tmp(1:size(arr)) = arr
239  deallocate(arr)
240  allocate(arr(new_size))
241  arr = tmp
242  end subroutine nc_diag_realloc_string
243 
244  ! nc_diag_realloc_logical(arr, addl_num_entries)
245  ! input:
246  ! logical, dimension(:) :: arr
247  ! array to reallocate
248  ! integer(i_long), intent(in) :: addl_num_entries
249  ! additional number of elements to allocate to the
250  ! specified array
251  subroutine nc_diag_realloc_logical(arr, addl_num_entries)
252  logical, dimension(:), allocatable, intent(inout) :: arr
253  integer(i_long),intent(in) :: addl_num_entries
254 
255  logical, dimension(:), allocatable :: tmp
256  integer(i_long) :: new_size
257 
258  integer(i_long) :: logical_arr_size
259  logical_arr_size = size(arr)
260 
261  new_size = logical_arr_size + addl_num_entries
262 
263 #ifdef _DEBUG_MEM_
264  call debug("[logical] Allocating from...")
265  print *, logical_arr_size
266 
267  call debug("[logical] ...to size...")
268  print *, (logical_arr_size + addl_num_entries)
269 #endif
270 
271  allocate(tmp(new_size))
272  tmp(1:logical_arr_size) = arr
273  deallocate(arr)
274  allocate(arr(new_size))
275  arr = tmp
276 #ifdef _DEBUG_MEM_
277  call debug("[logical] Final size:")
278  print *, size(arr)
279 #endif
280  end subroutine nc_diag_realloc_logical
281 
282  ! nc_diag_realloc_ncdcdn(arr, addl_num_entries)
283  ! input:
284  ! type(nc_diag_cat_dim_names), dimension(:) :: arr
285  ! array to reallocate
286  ! integer(i_long), intent(in) :: addl_num_entries
287  ! additional number of elements to allocate to the
288  ! specified array
289  subroutine nc_diag_realloc_ncdcdn(arr, addl_num_entries)
290  type(nc_diag_cat_dim_names), dimension(:), allocatable, intent(inout) :: arr
291  integer(i_long),intent(in) :: addl_num_entries
292 
293  type(nc_diag_cat_dim_names), dimension(:), allocatable :: tmp
294  integer(i_long) :: new_size
295 
296  integer(i_byte) :: alloc_err
297  character(len=100) :: err_msg
298 
299  new_size = size(arr) + addl_num_entries
300 
301  allocate(tmp(new_size), stat=alloc_err)
302  if (alloc_err /= 0) then
303  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
304  call nc_diag_realloc_error(trim(err_msg))
305  end if
306  tmp(1:size(arr)) = arr
307  deallocate(arr)
308  allocate(arr(new_size))
309  arr = tmp
310  end subroutine nc_diag_realloc_ncdcdn
311 
312  subroutine nc_diag_realloc_error(err)
313  character(len=*), intent(in) :: err
314 #ifdef ERROR_TRACEBACK
315  integer(i_long) :: div0
316 #endif
317  write(*, "(A)") " ** ERROR: " // err
318 #ifdef ERROR_TRACEBACK
319  write(*, "(A)") " ** Failed to concatenate NetCDF4."
320  write(*, "(A)") " (Traceback requested, triggering div0 error...)"
321  div0 = 1 / 0
322  write(*, "(A)") " Couldn't trigger traceback, ending gracefully."
323  write(*, "(A)") " (Ensure floating point exceptions are enabled,"
324  write(*, "(A)") " and that you have debugging (-g) and tracebacks"
325  write(*, "(A)") " compiler flags enabled!)"
326  stop 1
327 #else
328  stop " ** Failed to concatenate NetCDF4."
329 #endif
330  end subroutine nc_diag_realloc_error
331 end module ncdc_realloc
integer, parameter, public i_byte
Definition: ncd_kinds.F90:45
subroutine nc_diag_realloc_logical(arr, addl_num_entries)
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
subroutine nc_diag_realloc_short(arr, addl_num_entries)
subroutine nc_diag_realloc_rdouble(arr, addl_num_entries)
subroutine nc_diag_realloc_long(arr, addl_num_entries)
subroutine nc_diag_realloc_string(arr, addl_num_entries)
integer, parameter, public i_short
Definition: ncd_kinds.F90:46
subroutine nc_diag_realloc_error(err)
subroutine nc_diag_realloc_rsingle(arr, addl_num_entries)
subroutine nc_diag_realloc_byte(arr, addl_num_entries)
subroutine nc_diag_realloc_ncdcdn(arr, addl_num_entries)
integer, parameter, public r_double
Definition: ncd_kinds.F90:80
integer, parameter, public r_single
Definition: ncd_kinds.F90:79