Debian Codegraph @

src/atlas_f/util/atlas_KDTree_module.F90

raw
1 ! (C) Copyright 2013 ECMWF.
2 !
3 ! This software is licensed under the terms of the Apache Licence Version 2.0
4 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 ! In applying this licence, ECMWF does not waive the privileges and immunities
6 ! granted to it by virtue of its status as an intergovernmental organisation nor
7 ! does it submit to any jurisdiction.
8
9 #include "atlas/atlas_f.h"
10
11 module atlas_KDTree_module
12
13 use, intrinsic :: iso_c_binding
14 use fckit_owned_object_module, only : fckit_owned_object
15 use atlas_geometry_module
16
17 implicit none
18
19 private :: fckit_owned_object
20
21 public :: atlas_IndexKDTree
22
23 private
24
25 !------------------------------------------------------------------------------
26 TYPE, extends(fckit_owned_object) :: atlas_IndexKDTree
27
28 ! Purpose :
29 ! -------
30 ! *IndexKDTree* : Container of IndexKDTree
31
32 ! Methods :
33 ! -------
34
35 ! Author :
36 ! ------
37 ! April-2020 Benjamin Menetrier *IRIT-JCSDA*
38
39 !------------------------------------------------------------------------------
40 contains
41 procedure, public :: delete => atlas_IndexKDTree__delete
42 procedure, public :: empty => atlas_IndexKDTree__empty
43 procedure, public :: size => atlas_IndexKDTree__size
44 procedure :: reserve => IndexKDTree__reserve
45 procedure :: insert_separate_coords => IndexKDTree__insert_separate_coords
46 procedure :: insert_vectorized_coords => IndexKDTree__insert_vectorized_coords
47 generic :: insert => insert_separate_coords, insert_vectorized_coords
48 procedure :: build_only => IndexKDTree__build_only
49 procedure :: build_list_separate_coords => IndexKDTree__build_list_separate_coords
50 procedure :: build_list_vectorized_coords => IndexKDTree__build_list_vectorized_coords
51 generic :: build => build_only, build_list_separate_coords, build_list_vectorized_coords
52 procedure :: closestPoints_separate_coords => IndexKDTree__closestPoints_separate_coords
53 procedure :: closestPoints_vectorized_coords => IndexKDTree__closestPoints_vectorized_coords
54 generic :: closestPoints => closestPoints_separate_coords, closestPoints_vectorized_coords
55 procedure :: closestPoint_separate_coords => IndexKDTree__closestPoint_separate_coords
56 procedure :: closestPoint_vectorized_coords => IndexKDTree__closestPoint_vectorized_coords
57 generic :: closestPoint => closestPoint_separate_coords, closestPoint_vectorized_coords
58 procedure :: closestPointsWithinRadius_separate_coords => IndexKDTree__closestPointsWithinRadius_separate_coords
59 procedure :: closestPointsWithinRadius_vectorized_coords => IndexKDTree__closestPointsWithinRadius_vectorized_coords
60 generic :: closestPointsWithinRadius => closestPointsWithinRadius_separate_coords, closestPointsWithinRadius_vectorized_coords
61 procedure :: geometry => IndexKDTree__geometry
62 #if FCKIT_FINAL_NOT_INHERITING
63 final :: atlas_IndexKDTree__final_auto
64 #endif
65
66 END TYPE atlas_IndexKDTree
67
68 !------------------------------------------------------------------------------
69
70 interface atlas_IndexKDTree
71 module procedure atlas_IndexKDTree__ctor
72 module procedure atlas_IndexKDTree__ctor_geometry
73 end interface
74
75 !------------------------------------------------------------------------------
76
77
78 !========================================================
79 contains
80 !========================================================
81
82
83 ! -----------------------------------------------------------------------------
84 ! IndexKDTree routines
85
86 function atlas_IndexKDTree__ctor() result(this)
87 use atlas_KDTree_c_binding
88 use fckit_c_interop_module
89 type(atlas_IndexKDTree) :: this
90 call this%reset_c_ptr( atlas__IndexKDTree__new() )
91 call this%return()
92 end function atlas_IndexKDTree__ctor
93
94 function atlas_IndexKDTree__ctor_geometry(geometry) result(this)
95 use atlas_KDTree_c_binding
96 use fckit_c_interop_module
97 type(atlas_Geometry), intent(in) :: geometry
98 type(atlas_IndexKDTree) :: this
99 call this%reset_c_ptr( atlas__IndexKDTree__new_geometry( geometry%CPTR_PGIBUG_A ) )
100 call this%return()
101 end function atlas_IndexKDTree__ctor_geometry
102
103 subroutine atlas_IndexKDTree__delete(this)
104 use atlas_KDTree_c_binding
105 class(atlas_IndexKDTree), intent(inout) :: this
106 if ( .not. this%is_null() ) then
107 call atlas__IndexKDTree__delete(this%CPTR_PGIBUG_A)
108 end if
109 call this%reset_c_ptr()
110 end subroutine atlas_IndexKDTree__delete
111
112 function atlas_IndexKDTree__empty(this) result(empty)
113 use atlas_KDTree_c_binding
114 logical :: empty
115 class(atlas_IndexKDTree), intent(in) :: this
116 if( atlas__IndexKDTree__empty(this%CPTR_PGIBUG_A) == 0 ) then
117 empty = .False.
118 else
119 empty = .True.
120 endif
121 endfunction
122
123 function atlas_IndexKDTree__size(this) result(size)
124 use atlas_KDTree_c_binding
125 use atlas_kinds_module
126 integer(ATLAS_KIND_IDX) :: size
127 class(atlas_IndexKDTree), intent(in) :: this
128 size = atlas__IndexKDTree__size(this%CPTR_PGIBUG_A)
129 endfunction
130
131 subroutine IndexKDTree__reserve(this, size)
132 use atlas_KDTree_c_binding
133 use fckit_c_interop_module
134 use atlas_kinds_module
135 class(atlas_IndexKDTree), intent(in) :: this
136 integer(ATLAS_KIND_IDX), intent(in) :: size
137 call atlas__IndexKDTree__reserve(this%CPTR_PGIBUG_A, size)
138 end subroutine IndexKDTree__reserve
139
140 subroutine IndexKDTree__insert_separate_coords(this, lon, lat, index)
141 use atlas_KDTree_c_binding
142 use fckit_c_interop_module
143 use atlas_kinds_module
144 class(atlas_IndexKDTree), intent(in) :: this
145 real(c_double), intent(in) :: lon
146 real(c_double), intent(in) :: lat
147 integer(ATLAS_KIND_IDX), intent(in) :: index
148 call atlas__IndexKDTree__insert(this%CPTR_PGIBUG_A, lon, lat, index)
149 end subroutine IndexKDTree__insert_separate_coords
150
151 subroutine IndexKDTree__insert_vectorized_coords(this, lonlat, index)
152 use atlas_KDTree_c_binding
153 use fckit_c_interop_module
154 use atlas_kinds_module
155 class(atlas_IndexKDTree), intent(in) :: this
156 real(c_double), intent(in) :: lonlat(2)
157 integer(ATLAS_KIND_IDX), intent(in) :: index
158 call atlas__IndexKDTree__insert(this%CPTR_PGIBUG_A, lonlat(1), lonlat(2), index)
159 end subroutine IndexKDTree__insert_vectorized_coords
160
161 subroutine IndexKDTree__build_only(this)
162 use atlas_KDTree_c_binding
163 use fckit_c_interop_module
164 class(atlas_IndexKDTree), intent(in) :: this
165 call atlas__IndexKDTree__build(this%CPTR_PGIBUG_A)
166 end subroutine IndexKDTree__build_only
167
168 subroutine IndexKDTree__build_list_separate_coords(this, n, lons, lats, indices)
169 use atlas_KDTree_c_binding
170 use fckit_c_interop_module
171 use atlas_kinds_module
172 class(atlas_IndexKDTree), intent(in) :: this
173 integer(ATLAS_KIND_IDX), intent(in) :: n
174 real(c_double), intent(in) :: lons(n)
175 real(c_double), intent(in) :: lats(n)
176 integer(ATLAS_KIND_IDX), intent(in), optional :: indices(n)
177 integer(ATLAS_KIND_IDX) :: i
178 integer(ATLAS_KIND_IDX) :: index
179 do i = 1, n
180 if (present(indices)) then
181 index = indices(i)
182 else
183 index = i
184 end if
185 call this%insert(lons(i), lats(i), index)
186 end do
187 call this%build()
188 end subroutine IndexKDTree__build_list_separate_coords
189
190 subroutine IndexKDTree__build_list_vectorized_coords(this, n, lonlats, indices)
191 use atlas_KDTree_c_binding
192 use fckit_c_interop_module
193 use atlas_kinds_module
194 class(atlas_IndexKDTree), intent(in) :: this
195 integer(ATLAS_KIND_IDX), intent(in) :: n
196 real(c_double), intent(in) :: lonlats(n,2)
197 integer(ATLAS_KIND_IDX), intent(in), optional :: indices(n)
198 integer(ATLAS_KIND_IDX) :: i, index
199 do i = 1, n
200 if (present(indices)) then
201 index = indices(i)
202 else
203 index = i
204 end if
205 call this%insert(lonlats(i,:), index)
206 end do
207 call this%build()
208 end subroutine IndexKDTree__build_list_vectorized_coords
209
210 subroutine IndexKDTree__closestPoints_separate_coords(this, plon, plat, k, indices, distances, lons, lats)
211 use atlas_KDTree_c_binding
212 use, intrinsic :: iso_c_binding, only : c_f_pointer
213 use fckit_c_interop_module
214 use atlas_kinds_module
215 class(atlas_IndexKDTree), intent(in) :: this
216 real(c_double), intent(in) :: plon
217 real(c_double), intent(in) :: plat
218 integer(c_int), intent(in) :: k
219 integer(ATLAS_KIND_IDX), intent(out) :: indices(k)
220 real(c_double), intent(out), optional :: distances(k)
221 real(c_double), intent(out), optional :: lons(k)
222 real(c_double), intent(out), optional :: lats(k)
223 type(c_ptr) :: indices_cptr
224 type(c_ptr) :: distances_cptr
225 type(c_ptr) :: lons_cptr
226 type(c_ptr) :: lats_cptr
227 integer(ATLAS_KIND_IDX), pointer :: indices_fptr(:)
228 real(c_double), pointer :: distances_fptr(:)
229 real(c_double), pointer :: lons_fptr(:)
230 real(c_double), pointer :: lats_fptr(:)
231 if ( k > 0 ) then
232 call atlas__IndexKDTree__closestPoints(this%CPTR_PGIBUG_A, plon, plat, int(k, c_size_t), lons_cptr, lats_cptr, indices_cptr, &
233 & distances_cptr)
234 call c_f_pointer(indices_cptr, indices_fptr, (/k/))
235 indices(:) = indices_fptr(:)
236 if (present(distances)) then
237 call c_f_pointer(distances_cptr, distances_fptr, (/k/))
238 distances(:) = distances_fptr(:)
239 end if
240 if (present(lons)) then
241 call c_f_pointer(lons_cptr, lons_fptr, (/k/))
242 lons(:) = lons_fptr(:)
243 end if
244 if (present(lats)) then
245 call c_f_pointer(lats_cptr, lats_fptr, (/k/))
246 lats(:) = lats_fptr(:)
247 end if
248 call c_ptr_free(lons_cptr)
249 call c_ptr_free(lats_cptr)
250 call c_ptr_free(indices_cptr)
251 call c_ptr_free(distances_cptr)
252 end if
253 end subroutine IndexKDTree__closestPoints_separate_coords
254
255 subroutine IndexKDTree__closestPoints_vectorized_coords(this, point, k, indices, distances, lonlats)
256 use atlas_KDTree_c_binding
257 use, intrinsic :: iso_c_binding, only : c_f_pointer
258 use fckit_c_interop_module
259 use atlas_kinds_module
260 class(atlas_IndexKDTree), intent(in) :: this
261 real(c_double), intent(in) :: point(2)
262 integer(c_int), intent(in) :: k
263 integer(ATLAS_KIND_IDX), intent(out) :: indices(k)
264 real(c_double), intent(out), optional :: distances(k)
265 real(c_double), intent(out), optional :: lonlats(k,2)
266 type(c_ptr) :: indices_cptr
267 type(c_ptr) :: distances_cptr
268 type(c_ptr) :: lons_cptr
269 type(c_ptr) :: lats_cptr
270 integer(ATLAS_KIND_IDX), pointer :: indices_fptr(:)
271 real(c_double), pointer :: distances_fptr(:)
272 real(c_double), pointer :: lons_fptr(:)
273 real(c_double), pointer :: lats_fptr(:)
274 if ( k > 0 ) then
275 call atlas__IndexKDTree__closestPoints(this%CPTR_PGIBUG_A, point(1), point(2), int(k, c_size_t), lons_cptr, lats_cptr, &
276 & indices_cptr, distances_cptr)
277 call c_f_pointer(indices_cptr, indices_fptr, (/k/))
278 indices(:) = indices_fptr(:)
279 if (present(distances)) then
280 call c_f_pointer(distances_cptr, distances_fptr, (/k/))
281 distances(:) = distances_fptr(:)
282 end if
283 if (present(lonlats)) then
284 call c_f_pointer(lons_cptr, lons_fptr, (/k/))
285 call c_f_pointer(lats_cptr, lats_fptr, (/k/))
286 lonlats(:,1) = lons_fptr(:)
287 lonlats(:,2) = lats_fptr(:)
288 end if
289 call c_ptr_free(lons_cptr)
290 call c_ptr_free(lats_cptr)
291 call c_ptr_free(indices_cptr)
292 call c_ptr_free(distances_cptr)
293 end if
294 end subroutine IndexKDTree__closestPoints_vectorized_coords
295
296 subroutine IndexKDTree__closestPoint_separate_coords(this, plon, plat, index, distance, lon, lat)
297 use atlas_KDTree_c_binding
298 use fckit_c_interop_module
299 use atlas_kinds_module
300 class(atlas_IndexKDTree), intent(in) :: this
301 real(c_double), intent(in) :: plon
302 real(c_double), intent(in) :: plat
303 integer(ATLAS_KIND_IDX), intent(out) :: index
304 real(c_double), intent(out), optional :: distance
305 real(c_double), intent(out), optional :: lon
306 real(c_double), intent(out), optional :: lat
307 real(c_double) :: distance_tmp, lon_tmp, lat_tmp
308 call atlas__IndexKDTree__closestPoint(this%CPTR_PGIBUG_A, plon, plat, lon_tmp, lat_tmp, index, distance_tmp)
309 if (present(distance)) distance = distance_tmp
310 if (present(lon)) lon = lon_tmp
311 if (present(lat)) lat = lat_tmp
312 end subroutine IndexKDTree__closestPoint_separate_coords
313
314 subroutine IndexKDTree__closestPoint_vectorized_coords(this, point, index, distance, lonlat)
315 use atlas_KDTree_c_binding
316 use fckit_c_interop_module
317 use atlas_kinds_module
318 class(atlas_IndexKDTree), intent(in) :: this
319 real(c_double), intent(in) :: point(2)
320 integer(ATLAS_KIND_IDX), intent(out) :: index
321 real(c_double), intent(out), optional :: distance
322 real(c_double), intent(out), optional :: lonlat(2)
323 real(c_double) :: distance_tmp, lon_tmp, lat_tmp
324 call atlas__IndexKDTree__closestPoint(this%CPTR_PGIBUG_A, point(1), point(2), lon_tmp, lat_tmp, index, distance_tmp)
325 if (present(distance)) distance = distance_tmp
326 if (present(lonlat)) then
327 lonlat(1) = lon_tmp
328 lonlat(2) = lat_tmp
329 end if
330 end subroutine IndexKDTree__closestPoint_vectorized_coords
331
332 subroutine IndexKDTree__closestPointsWithinRadius_separate_coords(this, plon, plat, radius, k, indices, distances, lons, lats)
333 use atlas_KDTree_c_binding
334 use, intrinsic :: iso_c_binding, only : c_f_pointer
335 use fckit_c_interop_module
336 use atlas_kinds_module
337 class(atlas_IndexKDTree), intent(in) :: this
338 real(c_double), intent(in) :: plon
339 real(c_double), intent(in) :: plat
340 real(c_double), intent(in) :: radius
341 integer(c_int), intent(out) :: k
342 integer(ATLAS_KIND_IDX), allocatable, intent(inout), optional :: indices(:)
343 real(c_double), allocatable, intent(inout), optional :: distances(:)
344 real(c_double), allocatable, intent(inout), optional :: lons(:)
345 real(c_double), allocatable, intent(inout), optional :: lats(:)
346 integer(c_size_t) :: k_tmp
347 type(c_ptr) :: lons_cptr
348 type(c_ptr) :: lats_cptr
349 type(c_ptr) :: indices_cptr
350 type(c_ptr) :: distances_cptr
351 real(c_double), pointer :: lons_fptr(:)
352 real(c_double), pointer :: lats_fptr(:)
353 integer(ATLAS_KIND_IDX), pointer :: indices_fptr(:)
354 real(c_double), pointer :: distances_fptr(:)
355 call atlas__IndexKDTree__closestPointsWithinRadius(this%CPTR_PGIBUG_A, plon, plat, radius, &
356 & k_tmp, lons_cptr, lats_cptr, indices_cptr, distances_cptr)
357 k = int(k_tmp, c_int)
358 if (present(indices)) then
359 if (allocated(indices)) deallocate(indices)
360 allocate(indices(k))
361 end if
362 if (present(distances)) then
363 if (allocated(distances)) deallocate(distances)
364 allocate(distances(k))
365 end if
366 if (present(lons)) then
367 if (allocated(lons)) deallocate(lons)
368 allocate(lons(k))
369 end if
370 if (present(lats)) then
371 if (allocated(lats)) deallocate(lats)
372 allocate(lats(k))
373 end if
374 if ( k > 0 ) then
375 if (present(indices)) then
376 call c_f_pointer(indices_cptr, indices_fptr, (/k/))
377 indices(:) = indices_fptr(:)
378 end if
379 if (present(distances)) then
380 call c_f_pointer(distances_cptr, distances_fptr, (/k/))
381 distances(:) = distances_fptr(:)
382 end if
383 if (present(lons)) then
384 call c_f_pointer(lons_cptr, lons_fptr, (/k/))
385 lons(:) = lons_fptr(:)
386 end if
387 if (present(lats)) then
388 call c_f_pointer(lats_cptr, lats_fptr, (/k/))
389 lats(:) = lats_fptr(:)
390 end if
391 end if
392 call c_ptr_free(lons_cptr)
393 call c_ptr_free(lats_cptr)
394 call c_ptr_free(indices_cptr)
395 call c_ptr_free(distances_cptr)
396 end subroutine IndexKDTree__closestPointsWithinRadius_separate_coords
397
398 subroutine IndexKDTree__closestPointsWithinRadius_vectorized_coords(this, point, radius, k, indices, distances, lonlats)
399 use atlas_KDTree_c_binding
400 use, intrinsic :: iso_c_binding, only : c_f_pointer
401 use fckit_c_interop_module
402 use atlas_kinds_module
403 class(atlas_IndexKDTree), intent(in) :: this
404 real(c_double), intent(in) :: point(2)
405 real(c_double), intent(in) :: radius
406 integer(c_int), intent(out) :: k
407 integer(ATLAS_KIND_IDX), allocatable, intent(inout), optional :: indices(:)
408 real(c_double), allocatable, intent(inout), optional :: distances(:)
409 real(c_double), allocatable, intent(inout), optional :: lonlats(:,:)
410 integer(c_size_t) :: k_tmp
411 type(c_ptr) :: lons_cptr
412 type(c_ptr) :: lats_cptr
413 type(c_ptr) :: indices_cptr
414 type(c_ptr) :: distances_cptr
415 real(c_double), pointer :: lons_fptr(:)
416 real(c_double), pointer :: lats_fptr(:)
417 integer(ATLAS_KIND_IDX), pointer :: indices_fptr(:)
418 real(c_double), pointer :: distances_fptr(:)
419 call atlas__IndexKDTree__closestPointsWithinRadius(this%CPTR_PGIBUG_A, point(1), point(2), radius, &
420 & k_tmp, lons_cptr, lats_cptr, indices_cptr, distances_cptr)
421 k = int(k_tmp, c_int)
422 if (present(indices)) then
423 if (allocated(indices)) deallocate(indices)
424 allocate(indices(k))
425 end if
426 if (present(distances)) then
427 if (allocated(distances)) deallocate(distances)
428 allocate(distances(k))
429 end if
430 if (present(lonlats)) then
431 if (allocated(lonlats)) deallocate(lonlats)
432 allocate(lonlats(k,2))
433 end if
434 if ( k > 0 ) then
435 if (present(indices)) then
436 call c_f_pointer(indices_cptr, indices_fptr, (/k/))
437 indices(:) = indices_fptr(:)
438 end if
439 if (present(distances)) then
440 call c_f_pointer(distances_cptr, distances_fptr, (/k/))
441 distances(:) = distances_fptr(:)
442 end if
443 if (present(lonlats)) then
444 call c_f_pointer(lons_cptr, lons_fptr, (/k/))
445 call c_f_pointer(lats_cptr, lats_fptr, (/k/))
446 lonlats(:,1) = lons_fptr(:)
447 lonlats(:,2) = lats_fptr(:)
448 end if
449 end if
450 call c_ptr_free(lons_cptr)
451 call c_ptr_free(lats_cptr)
452 call c_ptr_free(indices_cptr)
453 call c_ptr_free(distances_cptr)
454 end subroutine IndexKDTree__closestPointsWithinRadius_vectorized_coords
455
456 function IndexKDTree__geometry(this) result(geometry)
457 use atlas_KDTree_c_binding
458 use fckit_c_interop_module
459 class(atlas_IndexKDTree), intent(in) :: this
460 type(atlas_Geometry) :: geometry
461 call geometry%reset_c_ptr( atlas__IndexKDTree__geometry( this%CPTR_PGIBUG_A ) )
462 call geometry%return()
463 end function IndexKDTree__geometry
464
465 !-------------------------------------------------------------------------------
466
467 #if FCKIT_FINAL_NOT_INHERITING
468 ATLAS_FINAL subroutine atlas_IndexKDTree__final_auto(this)
469 type(atlas_IndexKDTree), intent(inout) :: this
470 #if FCKIT_FINAL_DEBUGGING
471 write(0,*) "atlas_IndexKDTree__final_auto"
472 #endif
473 #if FCKIT_FINAL_NOT_PROPAGATING
474 call this%final()
475 #endif
476 FCKIT_SUPPRESS_UNUSED( this )
477 end subroutine
478 #endif
479
480 end module atlas_KDTree_module