FV3 Bundle
ncdw_realloc.F90
Go to the documentation of this file.
4  use ncdw_climsg, only: nclayer_error
5 #ifdef _DEBUG_MEM_
6  use ncdw_climsg, only: nclayer_debug
7 #endif
8 
9  implicit none
10 
11  ! This file provides the interface wrapper for the array
12  ! reallocation subroutines. This is so that others can simply
13  ! call nc_diag_realloc with the necessary arguments, instead of
14  ! having to call the specific nc_diag_realloc_* subroutines.
15 
16  interface nc_diag_realloc
17  module procedure nc_diag_realloc_byte, &
22  end interface nc_diag_realloc
23 
24  contains
25  ! nc_diag_realloc_byte(arr, addl_num_entries)
26  ! input:
27  ! integer(i_byte), dimension(:) :: arr
28  ! array to reallocate
29  ! integer(i_long), intent(in) :: addl_num_entries
30  ! additional number of elements to allocate to the
31  ! specified array
32  subroutine nc_diag_realloc_byte(arr, addl_num_entries)
33  integer(i_byte), dimension(:), allocatable, intent(inout) :: arr
34  integer(i_llong),intent(in) :: addl_num_entries
35 
36  integer(i_byte), dimension(:), allocatable :: tmp
37  integer(i_llong) :: new_size
38 
39  integer(i_byte) :: alloc_err
40  character(len=100) :: err_msg
41 
42  new_size = size(arr) + addl_num_entries
43 
44  allocate(tmp(new_size), stat=alloc_err)
45  if (alloc_err /= 0) then
46  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
47  call nclayer_error(trim(err_msg))
48  end if
49  tmp(1:size(arr)) = arr
50  deallocate(arr)
51  allocate(arr(new_size))
52  arr = tmp
53  end subroutine nc_diag_realloc_byte
54 
55  ! nc_diag_realloc_short(arr, addl_num_entries)
56  ! input:
57  ! integer(i_short), dimension(:) :: arr
58  ! array to reallocate
59  ! integer(i_long), intent(in) :: addl_num_entries
60  ! additional number of elements to allocate to the
61  ! specified array
62  subroutine nc_diag_realloc_short(arr, addl_num_entries)
63  integer(i_short), dimension(:), allocatable, intent(inout) :: arr
64  integer(i_llong),intent(in) :: addl_num_entries
65 
66  integer(i_short), dimension(:), allocatable :: tmp
67  integer(i_llong) :: new_size
68 
69  integer(i_byte) :: alloc_err
70  character(len=100) :: err_msg
71 
72  new_size = size(arr) + addl_num_entries
73 
74  allocate(tmp(new_size), stat=alloc_err)
75  if (alloc_err /= 0) then
76  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
77  call nclayer_error(trim(err_msg))
78  end if
79  tmp(1:size(arr)) = arr
80  deallocate(arr)
81  allocate(arr(new_size))
82  arr = tmp
83  end subroutine nc_diag_realloc_short
84 
85  ! nc_diag_realloc_long(arr, addl_num_entries)
86  ! input:
87  ! integer(i_long), dimension(:) :: arr
88  ! array to reallocate
89  ! integer(i_long), intent(in) :: addl_num_entries
90  ! additional number of elements to allocate to the
91  ! specified array
92  subroutine nc_diag_realloc_long(arr, addl_num_entries)
93  integer(i_long), dimension(:), allocatable, intent(inout) :: arr
94  integer(i_llong),intent(in) :: addl_num_entries
95 
96  integer(i_long), dimension(:), allocatable :: tmp
97  integer(i_llong) :: new_size
98 
99  integer(i_byte) :: alloc_err
100  character(len=100) :: err_msg
101 
102 #ifdef _DEBUG_MEM_
103  call nclayer_debug("Reallocating long array...")
104 #endif
105 
106  new_size = size(arr) + addl_num_entries
107 
108 #ifdef _DEBUG_MEM_
109  print *, "REALLOCATOR: new_size is ", new_size
110 #endif
111 
112  allocate(tmp(new_size), stat=alloc_err)
113  if (alloc_err /= 0) then
114  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
115  call nclayer_error(trim(err_msg))
116  end if
117 
118  tmp(1:size(arr)) = arr
119  deallocate(arr)
120  allocate(arr(new_size))
121  arr = tmp
122 
123 #ifdef _DEBUG_MEM_
124  print *, "REALLOCATOR: final actual size is ", size(arr)
125  call nclayer_debug("Realloc finished for long")
126 #endif
127  end subroutine nc_diag_realloc_long
128 
129  ! nc_diag_realloc_llong(arr, addl_num_entries)
130  ! input:
131  ! integer(i_llong), dimension(:) :: arr
132  ! array to reallocate
133  ! integer(i_llong), intent(in) :: addl_num_entries
134  ! additional number of elements to allocate to the
135  ! specified array
136  subroutine nc_diag_realloc_llong(arr, addl_num_entries)
137  integer(i_llong), dimension(:), allocatable, intent(inout) :: arr
138  integer(i_llong),intent(in) :: addl_num_entries
139 
140  integer(i_llong), dimension(:), allocatable :: tmp
141  integer(i_llong) :: new_size
142 
143  integer(i_byte) :: alloc_err
144  character(len=100) :: err_msg
145 
146 #ifdef _DEBUG_MEM_
147  call nclayer_debug("Reallocating long array...")
148 #endif
149 
150  new_size = size(arr) + addl_num_entries
151 
152  allocate(tmp(new_size), stat=alloc_err)
153  if (alloc_err /= 0) then
154  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
155  call nclayer_error(trim(err_msg))
156  end if
157 
158  tmp(1:size(arr)) = arr
159  deallocate(arr)
160  allocate(arr(new_size))
161  arr = tmp
162 
163 #ifdef _DEBUG_MEM_
164  call nclayer_debug("Realloc finished for long")
165 #endif
166  end subroutine nc_diag_realloc_llong
167 
168  ! nc_diag_realloc_rsingle(arr, addl_num_entries)
169  ! input:
170  ! real(r_single), 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_rsingle(arr, addl_num_entries)
176  real(r_single), dimension(:), allocatable, intent(inout) :: arr
177  integer(i_llong),intent(in) :: addl_num_entries
178 
179  real(r_single), dimension(:), allocatable :: tmp
180  integer(i_llong) :: 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 nclayer_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_rsingle
197 
198  ! nc_diag_realloc_rdouble(arr, addl_num_entries)
199  ! input:
200  ! real(r_double), 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_rdouble(arr, addl_num_entries)
206  real(r_double), dimension(:), allocatable, intent(inout) :: arr
207  integer(i_llong),intent(in) :: addl_num_entries
208 
209  real(r_double), dimension(:), allocatable :: tmp
210  integer(i_llong) :: new_size
211 
212  integer(i_byte) :: alloc_err
213  character(len=100) :: err_msg
214 
215  new_size = size(arr) + addl_num_entries
216 
217  allocate(tmp(new_size), stat=alloc_err)
218  if (alloc_err /= 0) then
219  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
220  call nclayer_error(trim(err_msg))
221  end if
222  tmp(1:size(arr)) = arr
223  deallocate(arr)
224  allocate(arr(new_size))
225  arr = tmp
226  end subroutine nc_diag_realloc_rdouble
227 
228  ! nc_diag_realloc_string(arr, addl_num_entries)
229  ! input:
230  ! character(len=*), dimension(:) :: arr
231  ! array to reallocate
232  ! integer(i_long), intent(in) :: addl_num_entries
233  ! additional number of elements to allocate to the
234  ! specified array
235  subroutine nc_diag_realloc_string(arr, addl_num_entries)
236  character(len=*), dimension(:), allocatable, intent(inout) :: arr
237  integer(i_llong),intent(in) :: addl_num_entries
238 
239  character(len=len(arr(1))), dimension(:), allocatable :: tmp
240  integer(i_llong) :: new_size
241 
242  integer(i_byte) :: alloc_err
243  character(len=100) :: err_msg
244 
245 #ifdef _DEBUG_MEM_
246  integer :: string_len, string_arr_size
247 
248  string_len = len(arr(1))
249  string_arr_size = size(arr)
250 
251  call nclayer_debug("[string] Length of string to allocate to:")
252  print *, string_len
253 
254  call nclayer_debug("[string] Allocating from...")
255  print *, string_arr_size
256 
257  call nclayer_debug("[string] ...to size...")
258  print *, (string_arr_size + addl_num_entries)
259 #endif
260 
261  new_size = size(arr) + addl_num_entries
262 
263  allocate(tmp(new_size), stat=alloc_err)
264  if (alloc_err /= 0) then
265  write(err_msg, "(A, I0)") "Reallocator was unable to reallocate memory! Error code: ", alloc_err
266  call nclayer_error(trim(err_msg))
267  end if
268  tmp(1:size(arr)) = arr
269  deallocate(arr)
270  allocate(arr(new_size))
271  arr = tmp
272  end subroutine nc_diag_realloc_string
273 
274  ! nc_diag_realloc_logical(arr, addl_num_entries)
275  ! input:
276  ! logical, dimension(:) :: arr
277  ! array to reallocate
278  ! integer(i_long), intent(in) :: addl_num_entries
279  ! additional number of elements to allocate to the
280  ! specified array
281  subroutine nc_diag_realloc_logical(arr, addl_num_entries)
282  logical, dimension(:), allocatable, intent(inout) :: arr
283  integer(i_llong),intent(in) :: addl_num_entries
284 
285  logical, dimension(:), allocatable :: tmp
286  integer(i_llong) :: new_size
287 
288  integer(i_llong) :: logical_arr_size
289  logical_arr_size = size(arr)
290 
291  new_size = logical_arr_size + addl_num_entries
292 
293 #ifdef _DEBUG_MEM_
294  call nclayer_debug("[logical] Allocating from...")
295  print *, logical_arr_size
296 
297  call nclayer_debug("[logical] ...to size...")
298  print *, (logical_arr_size + addl_num_entries)
299 #endif
300 
301  allocate(tmp(new_size))
302  tmp(1:logical_arr_size) = arr
303  deallocate(arr)
304  allocate(arr(new_size))
305  arr = tmp
306 #ifdef _DEBUG_MEM_
307  call nclayer_debug("[logical] Final size:")
308  print *, size(arr)
309 #endif
310  end subroutine nc_diag_realloc_logical
311 end module ncdw_realloc
subroutine nc_diag_realloc_short(arr, addl_num_entries)
integer, parameter, public i_byte
Definition: ncd_kinds.F90:45
subroutine nc_diag_realloc_string(arr, addl_num_entries)
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
subroutine nc_diag_realloc_llong(arr, addl_num_entries)
subroutine nc_diag_realloc_rdouble(arr, addl_num_entries)
subroutine nc_diag_realloc_logical(arr, addl_num_entries)
subroutine nc_diag_realloc_rsingle(arr, addl_num_entries)
integer, parameter, public i_short
Definition: ncd_kinds.F90:46
subroutine nc_diag_realloc_byte(arr, addl_num_entries)
subroutine nclayer_error(err)
Definition: ncdw_climsg.F90:97
subroutine nc_diag_realloc_long(arr, addl_num_entries)
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