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