YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
core.pyx
Go to the documentation of this file.
1# Copyright (c) 2024 The YAC Authors
2#
3# SPDX-License-Identifier: BSD-3-Clause
4
5import cython
6import numpy as _np
7from libc.stdint cimport SIZE_MAX
8from cython cimport view
9from libc.stdlib cimport malloc, free
10from warnings import warn
11from collections import namedtuple
12from types import coroutine
13import logging
14
15logger = logging.getLogger("yac.core")
16logger.addHandler(logging.NullHandler())
17
18cdef extern from "Python.h":
19 int Py_AtExit(void (*)())
20
21cdef extern from "<mpi.h>" nogil:
22 ctypedef void* MPI_Comm
23 MPI_Comm MPI_Comm_f2c(int)
24 MPI_Comm MPI_COMM_WORLD
25 MPI_Comm MPI_COMM_NULL
26
27cdef extern from *:
28 """
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif"""
32
33cdef extern from "yac_core.h" nogil:
35
36 ctypedef int yac_int;
37 ctypedef double(*yac_coordinate_pointer)[3];
38
39 cpdef enum yac_location:
40 YAC_LOC_CELL
41 YAC_LOC_CORNER
42 YAC_LOC_EDGE
43 YAC_LOC_UNDEFINED
44
45 cpdef enum yac_interp_avg_weight_type:
46 YAC_INTERP_AVG_ARITHMETIC
47 YAC_INTERP_AVG_DIST
48 YAC_INTERP_AVG_BARY
49 cdef yac_interp_avg_weight_type YAC_INTERP_AVG_WEIGHT_TYPE_DEFAULT
50 cdef int YAC_INTERP_AVG_PARTIAL_COVERAGE_DEFAULT
51
52 cpdef enum yac_interp_ncc_weight_type:
53 YAC_INTERP_NCC_AVG
54 YAC_INTERP_NCC_DIST
55 cdef yac_interp_ncc_weight_type YAC_INTERP_NCC_WEIGHT_TYPE_DEFAULT
56 cdef int YAC_INTERP_NCC_PARTIAL_COVERAGE_DEFAULT
57
58 cpdef enum yac_interp_dnn_weight_type:
59 YAC_INTERP_DNN_WEIGHT_AVG
60 YAC_INTERP_DNN_WEIGHT_DIST
61 YAC_INTERP_DNN_WEIGHT_GAUSS
62 YAC_INTERP_DNN_WEIGHT_RBF
63 cpdef enum yac_interp_dnn_search_distance_type:
64 YAC_INTERP_DNN_SEARCH_DISTANCE_FIXED
65 YAC_INTERP_DNN_SEARCH_DISTANCE_CELL_AREA
66 cdef yac_interp_dnn_weight_type YAC_INTERP_DNN_WEIGHT_DEFAULT
67 cdef yac_interp_dnn_search_distance_type YAC_INTERP_DNN_SEARCH_DISTANCE_TYPE_DEFAULT
68 cdef double YAC_INTERP_DNN_SEARCH_DISTANCE_DEFAULT
69 cdef double YAC_INTERP_DNN_SCALE_DEFAULT
70
71 cpdef enum yac_interp_method_conserv_normalisation:
72 YAC_INTERP_CONSERV_DESTAREA
73 YAC_INTERP_CONSERV_FRACAREA
74 cdef int YAC_INTERP_CONSERV_ORDER_DEFAULT
75 cdef int YAC_INTERP_CONSERV_ENFORCED_CONSERV_DEFAULT
76 cdef int YAC_INTERP_CONSERV_PARTIAL_COVERAGE_DEFAULT
77 cdef yac_interp_method_conserv_normalisation YAC_INTERP_CONSERV_NORMALISATION_DEFAULT
78
79 cpdef enum yac_interp_spmap_weight_type:
80 YAC_INTERP_SPMAP_AVG
81 YAC_INTERP_SPMAP_DIST
82 cpdef enum yac_interp_spmap_scale_type:
83 YAC_INTERP_SPMAP_NONE
84 YAC_INTERP_SPMAP_SRCAREA
85 YAC_INTERP_SPMAP_INVTGTAREA
86 YAC_INTERP_SPMAP_FRACAREA
87 cpdef enum yac_interp_spmap_cell_area_provider:
88 YAC_INTERP_SPMAP_CELL_AREA_FILE
89 YAC_INTERP_SPMAP_CELL_AREA_YAC
90 cdef double YAC_INTERP_SPMAP_SPREAD_DISTANCE_DEFAULT
91 cdef double YAC_INTERP_SPMAP_MAX_SEARCH_DISTANCE_DEFAULT
92 cdef yac_interp_spmap_weight_type YAC_INTERP_SPMAP_WEIGHTED_DEFAULT
93 cdef yac_interp_spmap_scale_type YAC_INTERP_SPMAP_SCALE_TYPE_DEFAULT
94 cdef double YAC_INTERP_SPMAP_SPHERE_RADIUS_DEFAULT
95 cdef const char* YAC_INTERP_SPMAP_FILENAME_DEFAULT
96 cdef const char* YAC_INTERP_SPMAP_VARNAME_DEFAULT
97 cdef int YAC_INTERP_SPMAP_MIN_GLOBAL_ID_DEFAULT
98
99 cpdef enum yac_interp_nnn_weight_type:
100 YAC_INTERP_NNN_AVG #< average of n source points
101 YAC_INTERP_NNN_DIST #< distance weighted average of n source points
102 YAC_INTERP_NNN_GAUSS #< distance with Gauss weights of n source points
103 YAC_INTERP_NNN_RBF #< radial basis functions
104 YAC_INTERP_NNN_ZERO #< all weights are set to zero
105 cdef yac_interp_nnn_weight_type YAC_INTERP_NNN_WEIGHTED_DEFAULT
106 cdef int YAC_INTERP_NNN_N_DEFAULT
107 cdef double YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT
108 cdef double YAC_INTERP_NNN_GAUSS_SCALE_DEFAULT
109 cdef int YAC_INTERP_RBF_N_DEFAULT
110 cdef double YAC_INTERP_RBF_MAX_SEARCH_DISTANCE_DEFAULT
111 cdef double YAC_INTERP_RBF_SCALE_DEFAULT
112 cdef int YAC_INTERP_RBF_KERNEL_DEFAULT
113
114 cpdef enum yac_interp_file_on_missing_file:
115 YAC_INTERP_FILE_MISSING_ERROR
116 YAC_INTERP_FILE_MISSING_CONT
117 cpdef enum yac_interp_file_on_success:
118 YAC_INTERP_FILE_SUCCESS_STOP
119 YAC_INTERP_FILE_SUCCESS_CONT
120 cdef yac_interp_file_on_missing_file YAC_INTERP_FILE_ON_MISSING_FILE_DEFAULT
121 cdef yac_interp_file_on_success YAC_INTERP_FILE_ON_SUCCESS_DEFAULT
122
123 cdef const char* YAC_INTERP_CHECK_CONSTRUCTOR_KEY_DEFAULT
124 cdef const char* YAC_INTERP_CHECK_DO_SEARCH_KEY_DEFAULT
125
126 cdef int YAC_INTERP_CREEP_DISTANCE_DEFAULT
127
128 struct yac_basic_grid_data:
129 yac_int * cell_ids;
130 yac_int * vertex_ids;
131 yac_int * edge_ids;
132 int * core_cell_mask;
133 int * core_vertex_mask;
134 int * core_edge_mask;
135
136 struct yac_basic_grid:
137 pass
138 yac_basic_grid_data * yac_basic_grid_get_data(
139 yac_basic_grid * grid);
140 struct yac_interp_field:
141 yac_location location
142 size_t coordinates_idx
143 size_t masks_idx
145 yac_basic_grid * grid,
146 const char * filename,
147 MPI_Comm comm)
148 const char * yac_basic_grid_get_name(yac_basic_grid * grid);
149 size_t yac_basic_grid_get_data_size(yac_basic_grid * grid, yac_location location);
150 void yac_basic_grid_delete(yac_basic_grid * grid);
152 yac_basic_grid * grid, yac_location location,
153 yac_coordinate_pointer coordinates, size_t count);
155 yac_basic_grid * grid, yac_location location,
156 const int * mask, size_t count, const char * mask_name);
157 yac_basic_grid * yac_basic_grid_reg_2d_new(
158 const char * name, size_t nbr_vertices[2], int cyclic[2],
159 double *lon_vertices, double *lat_vertices)
160 yac_basic_grid * yac_basic_grid_empty_new(const char* name)
161 yac_basic_grid * yac_basic_grid_unstruct_new(
162 const char * name, size_t nbr_vertices, size_t nbr_cells,
163 int *num_vertices_per_cell, double *x_vertices, double *y_vertices,
164 int *cell_to_vertex);
165 yac_basic_grid * yac_basic_grid_unstruct_ll_new(
166 const char * name, size_t nbr_vertices, size_t nbr_cells,
167 int *num_vertices_per_cell, double *x_vertices, double *y_vertices,
168 int *cell_to_vertex);
169 yac_basic_grid * yac_basic_grid_unstruct_edge_new(
170 const char * name, size_t nbr_vertices, size_t nbr_cells, size_t nbr_edges,
171 int *num_edges_per_cell, double *x_vertices, double *y_vertices,
172 int *cell_to_edge, int *edge_to_vertex);
174 const char * name, size_t nbr_vertices, size_t nbr_cells, size_t nbr_edges,
175 int *num_edges_per_cell, double *x_vertices, double *y_vertices,
176 int *cell_to_edge, int *edge_to_vertex);
178 yac_basic_grid * grid, int is_fatal);
179
180 struct yac_dist_grid_pair:
181 pass
182 yac_dist_grid_pair * yac_dist_grid_pair_new(
183 yac_basic_grid * grid_a, yac_basic_grid * grid_b,
184 MPI_Comm comm);
185 void yac_dist_grid_pair_delete(yac_dist_grid_pair * grid_pair);
186
187 struct yac_interp_grid:
188 pass
189 yac_interp_grid * yac_interp_grid_new(
190 yac_dist_grid_pair * grid_pair,
191 const char * src_grid_name, const char * tgt_grid_name,
192 size_t num_src_fields, const yac_interp_field * src_fields,
193 const yac_interp_field tgt_field);
194 void yac_interp_grid_delete(yac_interp_grid * interp_grid);
195
196 struct interp_method:
197 pass
198 yac_interp_weights * yac_interp_method_do_search(
199 interp_method ** method, yac_interp_grid * grid);
200 void yac_interp_method_delete(interp_method ** method);
201
202 struct yac_interp_stack_config:
203 pass
204 struct yac_interp_method:
205 pass
206 yac_interp_stack_config * yac_interp_stack_config_new();
208 yac_interp_stack_config * interp_stack_config);
209 interp_method ** yac_interp_stack_config_generate(
210 yac_interp_stack_config * interp_stack);
211
213 yac_interp_stack_config * interp_stack_config,
214 yac_interp_avg_weight_type reduction_type, int partial_coverage);
216 yac_interp_stack_config * interp_stack_config,
217 yac_interp_ncc_weight_type type, int partial_coverage);
219 yac_interp_stack_config * interp_stack_config,
220 yac_interp_dnn_weight_type type,
221 yac_interp_dnn_search_distance_type search_distance_type,
222 double search_distance, double scale);
224 yac_interp_stack_config * interp_stack_config,
225 yac_interp_nnn_weight_type type, size_t n,
226 double max_search_distance, double scale);
228 yac_interp_stack_config * interp_stack_config,
229 int order, int enforced_conserv, int partial_coverage,
230 yac_interp_method_conserv_normalisation normalisation);
232 yac_interp_stack_config * interp_stack_config,
233 double spread_distance, double max_search_distance,
234 yac_interp_spmap_weight_type weight_type,
235 yac_interp_spmap_scale_type scale_type,
236 double src_sphere_radius, const char * src_filename,
237 const char * src_varname, int src_min_global_id,
238 double tgt_sphere_radius, const char * tgt_filename,
239 const char * tgt_varname, int tgt_min_global_id);
241 yac_interp_stack_config * interp_stack_config, double value);
243 yac_interp_stack_config * interp_stack_config,
244 const char * filename, yac_interp_file_on_missing_file on_missing_file,
245 yac_interp_file_on_success on_success);
247 yac_interp_stack_config * interp_stack_config,
248 const char * constructor_key, const char * do_search_key);
250 yac_interp_stack_config * interp_stack_config);
252 yac_interp_stack_config * interp_stack_config,
253 size_t n, double max_search_distance, double scale);
255 yac_interp_stack_config * interp_stack_config, int creep_distance);
256
257 struct yac_interp_method_config:
258 pass
259 yac_interp_method_config * yac_interp_method_config_default_from_name_new(
260 const char * method_name);
262 yac_interp_method_config * config);
264 yac_interp_stack_config * interp_stack_config,
265 yac_interp_method_config * method_config);
267 yac_interp_method_config * config,
268 const char * path, const char * value);
270 yac_interp_method_config * config,
271 const char * path, int value);
273 yac_interp_method_config * config,
274 const char * path, double value);
276 const yac_interp_method_config * config,
277 const char * path);
279 const yac_interp_method_config * config,
280 const char * path);
282 const yac_interp_method_config * config,
283 const char * path);
285 const yac_interp_method_config * config,
286 const char * path);
288 yac_interp_method_config * config,
289 const char * path);
290
291 struct yac_interpolation:
292 pass
294 yac_interpolation * interp, double *** src_fields,
295 double *** src_frac_masks, double ** tgt_field);
297 yac_interpolation * interp, double *** src_fields, double *** src_frac_masks);
298 int yac_interpolation_execute_put_test(yac_interpolation * interp);
300 yac_interpolation * interp, double ** tgt_field);
301 int yac_interpolation_execute_get_test(yac_interpolation * interp);
302
303 struct yac_collection_selection:
304 pass
305 yac_collection_selection * yac_collection_selection_new(
306 size_t collection_size, const size_t * selection_indices);
308 yac_collection_selection * collection_selection);
309
310 cpdef enum yac_interp_weights_reorder_type:
311 YAC_MAPPING_ON_SRC
312 YAC_MAPPING_ON_TGT
313 struct yac_interpolation_gen_config:
314 pass
315 yac_interpolation_gen_config * yac_interpolation_gen_config_new();
316 void yac_interpolation_gen_config_delete(yac_interpolation_gen_config * config);
317 void yac_interpolation_gen_config_set_reorder(yac_interpolation_gen_config * config, yac_interp_weights_reorder_type reorder)
318 void yac_interpolation_gen_config_set_collection_size(yac_interpolation_gen_config * config, size_t collection_size);
320 yac_interpolation_gen_config * config,
321 const yac_collection_selection * collection_selection);
323 yac_interpolation_gen_config * config,
324 double frac_mask_fallback_value);
326 yac_interpolation_gen_config * config, double scaling_factor);
328 yac_interpolation_gen_config * config, double scaling_summand);
330 yac_interpolation_gen_config * config, const char * name);
331
332 struct yac_interp_weights:
333 pass
334 cpdef enum yac_weight_file_on_existing:
335 YAC_WEIGHT_FILE_ERROR
336 YAC_WEIGHT_FILE_KEEP
337 YAC_WEIGHT_FILE_OVERWRITE
338 YAC_WEIGHT_FILE_UNDEFINED
340 yac_interp_weights * weights, const char * filename,
341 const char * src_grid_name, const char * tgt_grid_name,
342 size_t src_grid_size, size_t tgt_grid_size,
343 yac_weight_file_on_existing on_existing);
345 const yac_interp_weights * weights,
346 const yac_interpolation_gen_config * interp_gen_config,
347 int is_source, int is_target);
348
349 void yac_yaxt_init(MPI_Comm comm);
350 void yac_mpi_init();
351 void yac_mpi_finalize();
352
353 void yac_get_io_ranks(
354 MPI_Comm comm, int * local_is_io, int ** io_ranks, int * num_io_ranks);
355
357 """
358 Returns the YAC core library version string.
359
360 @see yac_get_core_version
361 """
362 return bytes.decode(yac_get_core_version())
363
364cdef extern from * nogil:
365 """
366#ifndef YAC_UTILS_ENABLED
367// we cannot link libyac_utils.a so we define the function here
368#include "yac_assert.h"
369void yac_read_icon_basic_grid_parallel_2(
370 const char * filename, const char * gridname, MPI_Comm comm,
371 struct yac_basic_grid ** basic_grid, size_t * cell_coordinate_idx,
372 int ** cell_mask) {
373 die("To read an ICON grid YAC must be configured with --enable-utils.");
374}
375struct yac_basic_grid * yac_read_scrip_basic_grid(
376 const char * grid_filename, const char * mask_filename,
377 const char * grid_name, int valid_mask_value, const char * name,
378 int use_ll_edges, size_t * cell_coord_idx,
379 size_t ** duplicated_cell_idx, yac_int ** orig_cell_global_ids,
380 size_t * nbr_duplicated_cells) {
381 die("To read a SCRIP grid YAC must be configured with --enable-utils.");
382 return NULL;
383}
384#else
385 #include "yac_utils.h"
386#endif
387 """
389 const char * filename, const char * gridname, MPI_Comm comm,
390 yac_basic_grid ** basic_grid, size_t * cell_coordinate_idx,
391 int ** cell_mask)
392
393 yac_basic_grid * yac_read_scrip_basic_grid(
394 const char * grid_filename, const char * mask_filename,
395 const char * grid_name, int valid_mask_value, const char * name,
396 int use_ll_edges, size_t * cell_coord_idx,
397 size_t ** duplicated_cell_idx, yac_int ** orig_cell_global_ids,
398 size_t * nbr_duplicated_cells);
399
400
401def lonlat2xyz(lon, lat):
402 clat = _np.cos(lat)
403 slat = _np.sin(lat)
404 return clat * _np.cos(lon), clat * _np.sin(lon), slat
405
406cdef _decode(const char* c_str):
407 if c_str == NULL:
408 return None
409 return bytes.decode(c_str)
410
411cdef int get_c_comm(comm, MPI_Comm* c_comm) except -1:
412 """
413 Convert comm into an MPI_Comm.
414 Comm can be given as an fortran comm (int) or as an mpi4py.Comm
415 """
416 try:
417 from mpi4py import MPI
418 if isinstance(comm, MPI.Comm):
419 comm = comm.py2f()
420 except:
421 pass
422 if comm is None:
423 c_comm[0] = MPI_COMM_WORLD
424 elif isinstance(comm, int):
425 c_comm[0] = MPI_Comm_f2c(comm)
426 else:
427 raise ValueError("Invalid communicator")
428
429 return 0
430
432 cdef yac_int tmp;
433 dtype = _np.asarray(<yac_int[:1]>&tmp).dtype
434 assert dtype.itemsize == sizeof(yac_int), "dtype detected did not work"
435 info = _np.iinfo(dtype)
436 assert _np.min(arr) >= info.min and _np.max(arr) <= info.max, \
437 f"Indices must be within the valid range for yac_int ({info.min:_} to {info.max:_})"
438 return _np.ascontiguousarray(arr, dtype=dtype)
439
440
441logger.info("Initializing MPI")
443logger.info("Initializing yaxt")
444yac_yaxt_init(MPI_COMM_WORLD);
445
446Coordinates = namedtuple("Coordinates", ["basic_grid", "location", "coord_idx"])
447Mask = namedtuple("Mask", ["basic_grid", "location", "mask_idx"])
448
449cdef class BasicGrid:
450 cdef yac_basic_grid* _ptr;
451
452 def __init__(self, yac_basic_grid_ptr):
453 raise TypeError("This class cannot be instantiated directly.")
454
455 def __del__(self):
456 logger.info("calling yac_basic_grid_delete")
458
459 @staticmethod
460 cdef BasicGrid from_ptr(yac_basic_grid* basic_grid_ptr):
461 cdef BasicGrid basic_grid = BasicGrid.__new__(BasicGrid)
462 basic_grid._ptr = basic_grid_ptr
463 return basic_grid
464
465 def to_file(self, filename, comm=None):
466 filename_bytes = filename.encode()
467 cdef const char* _filename = filename_bytes
468 cdef MPI_Comm c_comm = MPI_COMM_NULL
469 get_c_comm(comm, &c_comm)
470 logger.info("calling yac_basic_grid_to_file_parallel")
471 with nogil:
473 _filename,
474 c_comm);
475
476 @property
477 def name(self):
478 cdef const char * _name = yac_basic_grid_get_name(self._ptr);
479 return _decode(_name)
480
481 def __str__(self):
482 return f"yac_basic_grid(name={self.name})"
483
484 def get_data_size(self, location : yac_location):
485 logger.info("calling yac_basic_grid_get_data_size")
486 return yac_basic_grid_get_data_size(self._ptr, location);
487
489 location : yac_location,
490 indices):
491 cdef yac_basic_grid_data* data
492 cdef yac_int** grid_global_ids;
493 cdef size_t count = self.get_data_size(location)
494 indices = _to_yac_int_np_array(indices)
495 data = yac_basic_grid_get_data(self._ptr)
496 assert indices.shape == (count,), "Wrong indices shape"
497 if location == YAC_LOC_CELL:
498 grid_global_ids = &(data.cell_ids)
499 elif location == YAC_LOC_CORNER:
500 grid_global_ids = &(data.vertex_ids)
501 elif location == YAC_LOC_EDGE:
502 grid_global_ids = &(data.edge_ids)
503 else:
504 raise RuntimeError("Invalid location")
505 if grid_global_ids[0] == NULL:
506 grid_global_ids[0] = <yac_int*>malloc(count * sizeof(yac_int));
507 logger.info("setting global index")
508 (<yac_int[:count]>(grid_global_ids[0]))[:] = indices
509
510 def set_core_mask(self, location :yac_location, mask):
511 cdef yac_basic_grid_data* data
512 cdef int** grid_core_mask;
513 cdef int count = self.get_data_size(location)
514 data = yac_basic_grid_get_data(self._ptr)
515 mask = _np.ascontiguousarray(mask, dtype=_np.intc)
516 assert mask.shape == (count,), "Wrong mask shape"
517 if location == YAC_LOC_CELL:
518 grid_core_mask = &(data.core_cell_mask)
519 elif location == YAC_LOC_CORNER:
520 grid_core_mask = &(data.core_vertex_mask)
521 elif location == YAC_LOC_EDGE:
522 grid_core_mask = &(data.core_edge_mask)
523 else:
524 raise RuntimeError("Invalid location")
525 if grid_core_mask[0] == NULL:
526 grid_core_mask[0] = <int*>malloc(count * sizeof(int));
527 logger.info("setting core mask")
528 (<int[:count]>(grid_core_mask[0]))[:] = mask
529
531 location : yac_location,
532 lon = None,
533 lat = None):
534 assert (lon is None) == (lat is None), "lon and lat must both be either set or unset."
535 cdef double[:,::1] coords_view
536 cdef size_t coords_len = 0
537 cdef yac_coordinate_pointer coords_ptr = NULL
538 cdef size_t idx = SIZE_MAX
539 if lon is not None:
540 coords_xyz = _np.stack(lonlat2xyz(lon, lat), axis=-1)
541 coords_view = _np.ascontiguousarray(coords_xyz, dtype=_np.double)
542 coords_ptr = <yac_coordinate_pointer>&coords_view[0,0]
543 coords_len = len(coords_view)
544 logger.info("calling yac_basic_grid_add_coordinates")
546 self._ptr,
547 location,
548 coords_ptr,
549 coords_len)
550 return Coordinates(basic_grid=self, location=location, coord_idx=idx)
551
552 def add_mask(self,
553 location : yac_location,
554 mask,
555 mask_name : str | None = None
556 ):
557 cdef int[:] mask_view = _np.ascontiguousarray(_np.asarray(mask), dtype=_np.intc)
558 cdef const char* _mask_name = NULL
559 if mask_name is not None:
560 mask_name_bytes = mask_name.encode()
561 _mask_name = mask_name_bytes
562 logger.info("calling yac_basic_grid_add_mask")
563 cdef size_t idx = yac_basic_grid_add_mask(
564 self._ptr,
565 location,
566 <int*>&mask_view[0], len(mask), _mask_name)
567 return Mask(basic_grid=self, location=location, mask_idx=idx)
568
569 def check_coordinates(self, is_fatal : bool = True):
570 logger.info("calling yac_basic_grid_check_coordinates")
571 return yac_basic_grid_check_coordinates(self._ptr, 1 if is_fatal else 0) != 0
572
573 @classmethod
574 def read_icon(cls,
575 filename :str,
576 gridname :str,
577 comm = None,
578 mask_valid_values=None):
579
580 filename_bytes = filename.encode()
581 cdef const char* _filename = filename_bytes
582 gridname_bytes = gridname.encode()
583 cdef const char* _gridname = gridname_bytes
584 cdef MPI_Comm c_comm = MPI_COMM_NULL
585 get_c_comm(comm, &c_comm)
586 cdef yac_basic_grid * _yac_basic_grid
587 cdef size_t cell_coordinate_idx
588 cdef int * cell_mask;
589
590 logger.info("calling yac_read_icon_basic_grid_parallel_2")
592 _filename, _gridname, c_comm,
593 &_yac_basic_grid,
594 &cell_coordinate_idx, &cell_mask)
595
596 basic_grid = BasicGrid.from_ptr(_yac_basic_grid)
597 cell_coords = Coordinates(basic_grid=basic_grid,
598 location=YAC_LOC_CELL,
599 coord_idx=cell_coordinate_idx)
600
601 _cell_mask = None
602 if cell_mask != NULL and mask_valid_values is not None:
603 num_cells = basic_grid.get_data_size(YAC_LOC_CELL)
604 _cell_mask = basic_grid.add_mask(YAC_LOC_CELL,
605 _np.isin(<int[:num_cells]>cell_mask, mask_valid_values))
606
607 return basic_grid, InterpField(cell_coords, _cell_mask)
608
609 @classmethod
610 def read_scrip(cls,
611 grid_filename,
612 mask_filename,
613 grid_name,
614 name,
615 valid_mask_value = 1,
616 use_ll_edges = False,
617 ):
618 cdef size_t cell_coord_idx;
619 cdef size_t* duplicated_cell_idx;
620 cdef yac_int* orig_cell_global_ids;
621 cdef size_t nbr_duplicated_cells;
622 cdef yac_basic_grid * yac_basic_grid;
623 grid_filename_bytes = grid_filename.encode()
624 cdef const char* _grid_filename = grid_filename_bytes
625 mask_filename_bytes = mask_filename.encode()
626 cdef const char* _mask_filename = mask_filename_bytes
627 grid_name_bytes = grid_name.encode()
628 cdef const char* _grid_name = grid_name_bytes
629 name_bytes = name.encode()
630 cdef const char* _name = name_bytes
631 cdef int _valid_mask_value = valid_mask_value
632 cdef int _use_ll_edges = use_ll_edges
633 logger.info("calling yac_read_scrip_basic_grid")
634 with nogil:
635 yac_basic_grid = yac_read_scrip_basic_grid(
636 _grid_filename,
637 _mask_filename,
638 _grid_name,
639 _valid_mask_value,
640 _name,
641 _use_ll_edges,
642 &cell_coord_idx,
643 &duplicated_cell_idx,
644 &orig_cell_global_ids,
645 &nbr_duplicated_cells);
646 basic_grid = BasicGrid.from_ptr(yac_basic_grid)
647 return (basic_grid,
648 InterpField(Coordinates(basic_grid=basic_grid,
649 location=YAC_LOC_CELL,
650 coord_idx=cell_coord_idx)),
651 )
652
653 @classmethod
654 @cython.boundscheck(False)
655 def reg_2d_new(cls, name, lon_vertices, lat_vertices, cyclic = [False, False]):
656 cdef const double[::1] x = _np.ascontiguousarray(lon_vertices, dtype=_np.double)
657 cdef const double[::1] y = _np.ascontiguousarray(lat_vertices, dtype=_np.double)
658 cdef int[2] cyclic_view = cyclic
659 name_bytes = name.encode()
660 cdef const char* _name = name_bytes
661 cdef size_t[2] _len = [len(x), len(y)]
662 cdef int[2] _cyclic = cyclic
663 logger.info("calling yac_basic_grid_reg_2d_new")
664 with nogil:
665 yac_basic_grid = yac_basic_grid_reg_2d_new(
666 _name, _len, _cyclic,
667 <double*>&x[0], <double*>&y[0])
668 return BasicGrid.from_ptr(yac_basic_grid)
669
670 @classmethod
671 @cython.boundscheck(False)
673 name : str,
674 num_vertices_per_cell,
675 x_vertices,
676 y_vertices,
677 cell_to_vertex,
678 lonlat_edges = False):
679 assert len(x_vertices) == len(y_vertices)
680 cdef int[:] _num_vertices_per_cell = _np.ascontiguousarray(num_vertices_per_cell, dtype=_np.intc)
681 cdef double[:] _x_vertices = _np.ascontiguousarray(x_vertices, dtype=_np.double)
682 cdef double[:] _y_vertices = _np.ascontiguousarray(y_vertices, dtype=_np.double)
683 cdef int[:] _cell_to_vertex = _np.ascontiguousarray(cell_to_vertex, dtype=_np.intc)
684 cdef int[:] _num_edges_per_cell
685 cdef int[:] _edge_to_vertex
686 cdef yac_basic_grid * ptr
687 name_bytes = name.encode()
688 cdef const char* _name = name_bytes
689 cdef size_t nbr_vertices = len(x_vertices)
690 cdef size_t nbr_cells = len(num_vertices_per_cell)
691 if lonlat_edges:
692 logger.info("calling yac_basic_grid_unstruct_ll_new")
693 with nogil:
695 _name,
696 nbr_vertices,
697 nbr_cells,
698 <int*>&_num_vertices_per_cell[0],
699 <double *>&_x_vertices[0],
700 <double *>&_y_vertices[0],
701 <int *>&_cell_to_vertex[0]);
702 else:
703 logger.info("calling yac_basic_grid_unstruct_new")
704 with nogil:
706 _name,
707 nbr_vertices,
708 nbr_cells,
709 <int*>&_num_vertices_per_cell[0],
710 <double *>&_x_vertices[0],
711 <double *>&_y_vertices[0],
712 <int *>&_cell_to_vertex[0]);
713 return BasicGrid.from_ptr(ptr)
714
715 @classmethod
716 @cython.boundscheck(False)
718 name : str,
719 num_edges_per_cell,
720 x_vertices,
721 y_vertices,
722 cell_to_edge,
723 edge_to_vertex,
724 lonlat_edges : bool = False):
725 assert len(x_vertices) == len(y_vertices)
726 cdef int[:] _num_edges_per_cell = _np.ascontiguousarray(num_edges_per_cell, dtype=_np.intc)
727 cdef double[:] _x_vertices = _np.ascontiguousarray(x_vertices, dtype=_np.double)
728 cdef double[:] _y_vertices = _np.ascontiguousarray(y_vertices, dtype=_np.double)
729 cdef int[:] _cell_to_edge = _np.ascontiguousarray(cell_to_edge, dtype=_np.intc)
730 cdef int[:] _edge_to_vertex = _np.ascontiguousarray(edge_to_vertex, dtype=_np.intc)
731 name_bytes = name.encode()
732 cdef const char* _name = name_bytes
733 cdef size_t nbr_vertices = len(x_vertices)
734 cdef size_t nbr_cells = len(num_edges_per_cell)
735 cdef size_t nbr_edges = _np.max(_cell_to_edge)+1
736 if lonlat_edges:
737 logger.info("calling yac_basic_grid_unstruct_edge_ll_new")
738 with nogil:
740 _name,
741 nbr_vertices,
742 nbr_cells,
743 nbr_edges,
744 <int*>&_num_edges_per_cell[0],
745 <double*>&_x_vertices[0],
746 <double*>&_y_vertices[0],
747 <int*>&_cell_to_edge[0],
748 <int*>&_edge_to_vertex[0]);
749 else:
750 logger.info("calling yac_basic_grid_unstruct_edge_new")
751 with nogil:
753 _name,
754 nbr_vertices,
755 nbr_cells,
756 nbr_edges,
757 <int*>&_num_edges_per_cell[0],
758 <double*>&_x_vertices[0],
759 <double*>&_y_vertices[0],
760 <int*>&_cell_to_edge[0],
761 <int*>&_edge_to_vertex[0]);
762
763 return BasicGrid.from_ptr(ptr)
764
765 @classmethod
766 def empty(cls, name : str):
767 logger.info("calling yac_basic_grid_empty_new")
768 cdef yac_basic_grid * ptr = yac_basic_grid_empty_new(name.encode())
769 return BasicGrid.from_ptr(ptr)
770
771 @classmethod
772 def from_uxgrid(cls,
773 name : str,
774 uxgrid,
775 def_edges : bool = False,
776 lonlat_edges : bool = False):
777 from uxarray import INT_FILL_VALUE
778 if def_edges:
779 cell_to_edge = _np.asarray(uxgrid.face_edge_connectivity).reshape((-1,), order="C")
780 cell_to_edge = cell_to_edge[cell_to_edge!=INT_FILL_VALUE]
781 edge_to_vertex = _np.asarray(uxgrid.edge_node_connectivity).reshape((-1,), order="C")
782 edge_to_vertex = edge_to_vertex[edge_to_vertex!=INT_FILL_VALUE]
783 grid = cls.unstruct_edge_new(
784 name,
785 uxgrid.n_nodes_per_face, # n_nodes_per_face == n_edges_per_face (2D)
786 _np.deg2rad(uxgrid.node_lon),
787 _np.deg2rad(uxgrid.node_lat),
788 cell_to_edge,
789 edge_to_vertex,
790 lonlat_edges = lonlat_edges)
791 else:
792 cell_to_vertex = _np.asarray(uxgrid.face_node_connectivity).reshape((-1,), order="C")
793 cell_to_vertex = cell_to_vertex[cell_to_vertex!=INT_FILL_VALUE]
794 grid = cls.unstruct_new(
795 name,
796 uxgrid.n_nodes_per_face, # n_nodes_per_face == n_edges_per_face (2D)
797 _np.deg2rad(uxgrid.node_lon),
798 _np.deg2rad(uxgrid.node_lat),
799 cell_to_vertex,
800 lonlat_edges = lonlat_edges)
801 grid.set_global_index(YAC_LOC_CELL,
802 range(uxgrid.n_face))
803 grid.set_global_index(YAC_LOC_EDGE,
804 range(uxgrid.n_edge))
805 grid.set_global_index(YAC_LOC_CORNER,
806 range(uxgrid.n_node))
807 return grid
808
809
810cdef class InterpField:
811 cdef yac_interp_field _interp_field
812 cdef BasicGrid _basic_grid
813
814 def __init__(self,
815 coordinates : Coordinates,
816 mask : Mask = None):
817 self._basic_grid = coordinates.basic_grid
818 self._interp_field.location = coordinates.location
819 self._interp_field.coordinates_idx = coordinates.coord_idx
820 cdef masks_idx = SIZE_MAX
821 if mask is not None:
822 assert mask.location == coordinates.location, \
823 "coordinates and mask have different location"
824 masks_idx = mask.mask_idx
825 self._interp_field.masks_idx = masks_idx
826
827 def __str__(self):
828 return f"yac_interp_field(grid={self._basic_grid}, location={self.location}, coord_idx={self._interp_field.coordinates_idx}, mask_idx={self._interp_field.masks_idx})"
829
830 @property
831 def location(self):
832 return self._interp_field.location
833
834 @property
835 def basic_grid(self):
836 return self._basic_grid
837
838cdef class DistGridPair:
839 cdef yac_dist_grid_pair* _ptr
840 _str :str
841
842 def __init__(self, grid_a : BasicGrid,
843 grid_b : BasicGrid,
844 comm = None):
845 cdef MPI_Comm c_comm = MPI_COMM_NULL
846 get_c_comm(comm, &c_comm)
847 logger.info("calling yac_dist_grid_pair_new")
849 grid_a._ptr, grid_b._ptr,
850 c_comm);
851 self._str = f"yac_dist_grid_pair(grid_a={grid_a}, grid_b={grid_b}"
852
853 def __str__(self):
854 return self._str
855
856 def __del__(self):
857 logger.info("calling yac_dist_grid_pair_delete")
859
860cdef class InterpGrid:
861 cdef yac_interp_grid* _ptr
862 cdef InterpField _src_field
863 cdef InterpField _tgt_field
864
865 def __init__(self,
866 grid_pair : DistGridPair,
867 src_field : InterpField, # TODO: support multiple src_fields
868 tgt_field : InterpField):
869 src_grid_name = src_field._basic_grid.name
870 tgt_grid_name = tgt_field._basic_grid.name
871 logger.info("calling yac_interp_grid_new")
872 self._ptr = yac_interp_grid_new(grid_pair._ptr,
873 src_grid_name.encode(), tgt_grid_name.encode(),
874 1, &src_field._interp_field,
875 tgt_field._interp_field);
876 self._src_field = src_field
877 self._tgt_field = tgt_field
878
879 def __str__(self):
880 return f"yac_interp_grid(src_field={self._src_field}, tgt_field={self._tgt_field})"
881
882 def __del__(self):
883 logger.info("calling yac_interp_grid_delete")
885
887 cdef yac_interp_stack_config* _ptr
888 interpolation_register = {}
889
890 def __init__(self):
891 logger.info("calling yac_interp_stack_config_new")
893
894 @classmethod
895 def from_list(cls, interp_list):
896 interp_stack = InterpolationStack()
897 interp_list = [(t, {}) if type(t) is str else t for t in interp_list]
898 for name, kwargs in interp_list:
899 interp_stack.add(name, **kwargs)
900 return interp_stack
901
902 def __del__(self):
903 logger.info("calling yac_interp_stack_config_delete")
905
907 def _register(method):
908 InterpolationStack.interpolation_register[name] = method
909 return method
910 return _register
911
912 def add(self, name, *args, **kwargs):
913 self.interpolation_register[name](self, *args, **kwargs)
914
915 @register_interpolation("average")
916 def add_average(self,
917 weight_type : yac_interp_avg_weight_type | str = YAC_INTERP_AVG_WEIGHT_TYPE_DEFAULT,
918 partial_coverage : bool = YAC_INTERP_AVG_PARTIAL_COVERAGE_DEFAULT):
919 if isinstance(weight_type, str):
920 weight_type = getattr(yac_interp_avg_weight_type, weight_type)
921 logger.info("calling yac_interp_stack_config_add_average")
923 self._ptr, weight_type, partial_coverage);
924
925 @register_interpolation("ncc")
926 def add_ncc(self,
927 weight_type : yac_interp_ncc_weight_type | str = YAC_INTERP_NCC_WEIGHT_TYPE_DEFAULT,
928 partial_coverage : bool = YAC_INTERP_NCC_PARTIAL_COVERAGE_DEFAULT):
929 if isinstance(weight_type, str):
930 weight_type = getattr(yac_interp_ncc_weight_type, weight_type)
931 logger.info("calling yac_interp_stack_config_add_ncc")
932 yac_interp_stack_config_add_ncc(self._ptr, weight_type, partial_coverage);
933
934 @register_interpolation("dnn")
935 def add_dnn(self,
936 weight_type : yac_interp_dnn_weight_type | str = YAC_INTERP_DNN_WEIGHT_DEFAULT,
937 search_distance_type : yac_interp_dnn_search_distance_type | str = YAC_INTERP_DNN_SEARCH_DISTANCE_TYPE_DEFAULT,
938 search_distance : float = YAC_INTERP_DNN_SEARCH_DISTANCE_DEFAULT,
939 scale : float = YAC_INTERP_DNN_SCALE_DEFAULT):
940 if isinstance(weight_type, str):
941 weight_type = getattr(yac_interp_dnn_weight_type, weight_type)
942 if isinstance(search_distance_type, str):
943 search_distance_type = getattr(yac_interp_dnn_search_distance_type, search_distance_type)
944 logger.info("calling yac_interp_stack_config_add_dnn")
946 self._ptr, weight_type, search_distance_type, search_distance, scale);
947
948 @register_interpolation("nnn")
949 def add_nnn(self,
950 nnn_type : yac_interp_nnn_weight_type | str = YAC_INTERP_NNN_WEIGHTED_DEFAULT,
951 n : int = YAC_INTERP_NNN_N_DEFAULT,
952 max_search_distance: float = YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT,
953 scale : float = YAC_INTERP_NNN_GAUSS_SCALE_DEFAULT,
954 ):
955 if isinstance(nnn_type, str):
956 nnn_type = getattr(yac_interp_nnn_weight_type, nnn_type)
957 logger.info("calling yac_interp_stack_config_add_nnn")
959 self._ptr, nnn_type, n, max_search_distance, scale);
960
961 @register_interpolation("conservative")
963 order : int = YAC_INTERP_CONSERV_ORDER_DEFAULT,
964 enforced_conserv : bool = YAC_INTERP_CONSERV_ENFORCED_CONSERV_DEFAULT,
965 partial_coverage : bool = YAC_INTERP_CONSERV_PARTIAL_COVERAGE_DEFAULT,
966 normalisation : yac_interp_method_conserv_normalisation | str = YAC_INTERP_CONSERV_NORMALISATION_DEFAULT
967 ):
968 assert order in {1,2}, "Invalid order"
969 if isinstance(normalisation, str):
970 normalisation = getattr(yac_interp_method_conserv_normalisation, normalisation)
971 logger.info("calling yac_interp_stack_config_add_conservative")
973 self._ptr, order, enforced_conserv, partial_coverage, normalisation);
974
975 @register_interpolation("source_to_target_map")
977 spread_distance : float = YAC_INTERP_SPMAP_SPREAD_DISTANCE_DEFAULT,
978 max_search_distance : float = YAC_INTERP_SPMAP_MAX_SEARCH_DISTANCE_DEFAULT,
979 weight_type : yac_interp_spmap_weight_type | str = YAC_INTERP_SPMAP_WEIGHTED_DEFAULT,
980 scale_type : yac_interp_spmap_scale_type | str = YAC_INTERP_SPMAP_SCALE_TYPE_DEFAULT,
981 src_sphere_radius : float = 0.0,
982 src_filename : str | None = None,
983 src_varname : str | None = None,
984 src_min_global_id : int = YAC_INTERP_SPMAP_MIN_GLOBAL_ID_DEFAULT,
985 tgt_sphere_radius : float = 0.0,
986 tgt_filename : str | None = None,
987 tgt_varname : str | None = None,
988 tgt_min_global_id : int = YAC_INTERP_SPMAP_MIN_GLOBAL_ID_DEFAULT,
989 ):
990 cdef const char* _src_filename = NULL
991 cdef const char* _src_varname = NULL
992 cdef const char* _tgt_filename = NULL
993 cdef const char* _tgt_varname = NULL
994 if src_filename is not None:
995 src_filename_bytes = bytes(src_filename)
996 _src_filename = src_filename_bytes
997 if src_varname is not None:
998 src_varname_bytes = bytes(src_varname)
999 _src_varname = src_varname_bytes
1000 if tgt_filename is not None:
1001 tgt_filename_bytes = bytes(tgt_filename)
1002 _tgt_filename = tgt_filename_bytes
1003 if src_varname is not None:
1004 src_varname_bytes = bytes(src_varname)
1005 _src_varname = src_varname_bytes
1006 if isinstance(weight_type, str):
1007 weight_type = getattr(yac_interp_spmap_weight_type, weight_type)
1008 if isinstance(scale_type, str):
1009 scale_type = getattr(yac_interp_spmap_scale_type, scale_type)
1010 logger.info("calling yac_interp_stack_config_add_spmap")
1012 self._ptr, spread_distance, max_search_distance, weight_type,
1013 scale_type, src_sphere_radius, _src_filename, _src_varname,
1014 src_min_global_id, tgt_sphere_radius, _tgt_filename, _tgt_varname,
1015 tgt_min_global_id);
1016
1017 @register_interpolation("fixed")
1018 def add_fixed(self, value : float):
1019 logger.info("calling yac_interp_stack_config_add_fixed")
1021 self._ptr, value);
1022
1023 @register_interpolation("user_file")
1025 filename : str | None = None,
1026 on_missing_file : yac_interp_file_on_missing_file | str = YAC_INTERP_FILE_ON_MISSING_FILE_DEFAULT,
1027 on_success : yac_interp_file_on_success | str = YAC_INTERP_FILE_ON_SUCCESS_DEFAULT):
1028 cdef const char* _filename = NULL
1029 if filename is not None:
1030 filename_bytes = filename.encode()
1031 _filename = filename_bytes
1032 if isinstance(on_missing_file, str):
1033 on_missing_file = getattr(yac_interp_file_on_missing_file, on_missing_file)
1034 if isinstance(on_success, str):
1035 on_success = getattr(yac_interp_file_on_success, on_success)
1036 logger.info("calling yac_interp_stack_config_add_user_file")
1038 self._ptr, _filename, on_missing_file, on_success);
1039
1040 @register_interpolation("check")
1041 def add_check(self,
1042 constructor_key : str = _decode(YAC_INTERP_CHECK_CONSTRUCTOR_KEY_DEFAULT),
1043 do_search_key : str = _decode(YAC_INTERP_CHECK_DO_SEARCH_KEY_DEFAULT)):
1044 logger.info("calling yac_interp_stack_config_add_check")
1046 constructor_key.encode(), do_search_key.encode());
1047
1048 @register_interpolation("bernstein_bezier")
1050 logger.info("calling yac_interp_stack_config_add_hcsbb")
1052
1053 @register_interpolation("rbf")
1054 def add_rbf(self,
1055 n : int = YAC_INTERP_RBF_N_DEFAULT,
1056 max_search_distance : float = YAC_INTERP_RBF_MAX_SEARCH_DISTANCE_DEFAULT,
1057 scale : float = YAC_INTERP_RBF_SCALE_DEFAULT,
1058 rbf_kernel : int = YAC_INTERP_RBF_KERNEL_DEFAULT
1059 ):
1060 logger.info("calling yac_interp_stack_config_add_rbf")
1061 yac_interp_stack_config_add_rbf(self._ptr, n, max_search_distance, scale);
1062
1063 @register_interpolation("creep")
1064 def add_creep(self,
1065 creep_distance : int = YAC_INTERP_CREEP_DISTANCE_DEFAULT):
1066 logger.info("calling yac_interp_stack_config_add_creep")
1067 yac_interp_stack_config_add_creep(self._ptr, creep_distance);
1068
1069 def add_from_config(self, config : InterpMethodConfig):
1070 """Add a method config object to this interpolation stack.
1071
1072 The stack takes ownership of the config; it must not be used
1073 (or deleted) after this call.
1074 """
1075 logger.info("calling yac_interp_stack_config_add")
1076 yac_interp_stack_config_add(self._ptr, config._ptr)
1077 config._ptr = NULL # prevent double-free
1078
1079 # This method should eventually replace `add`
1080 def add_method(self, method, **kwargs):
1081 cfg = InterpMethodConfig(method, **kwargs)
1082 self.add_from_config(cfg)
1083
1085 """User-oriented wrapper around a YAC interpolation method configuration.
1086
1087 Typical usage::
1088
1089 cfg = InterpMethodConfig("nnn")
1090 cfg.set_param_int("n", 8)
1091 cfg.set_param_str("weighted", "gauss_weighted")
1092 stack = InterpolationStack()
1093 stack.add_from_config(cfg)
1094 """
1095 cdef yac_interp_method_config* _ptr
1096
1097 def __init__(self, method_name : str, **kwargs):
1098 method_name_bytes = method_name.encode()
1099 logger.info("calling yac_interp_method_config_default_from_name_new")
1101 method_name_bytes)
1102 for k,v in kwargs.items():
1103 self.set_param(k,v)
1104
1105 def __del__(self):
1106 if self._ptr != NULL:
1107 logger.info("calling yac_interp_method_config_delete")
1109 self._ptr = NULL
1110
1111 def set_param_str(self, path : str, value : str):
1112 """Set a parameter by dot-separated path from a string value."""
1113 logger.info("calling yac_interp_method_config_set_param_str")
1115 self._ptr, path.encode(), value.encode())
1116
1117 def set_param_int(self, path : str, value : int):
1118 """Set a scalar integer parameter by dot-separated path."""
1119 logger.info("calling yac_interp_method_config_set_param_int")
1121 self._ptr, path.encode(), value)
1122
1123 def set_param_dble(self, path : str, value : float):
1124 """Set a scalar double parameter by dot-separated path."""
1125 logger.info("calling yac_interp_method_config_set_param_dble")
1127 self._ptr, path.encode(), value)
1128
1129 def set_param(self, path : str, value : str | int | float | dict | list):
1130 """Set a parameter by dot-separated path, dispatching on type."""
1131 if isinstance(value, str):
1132 self.set_param_str(path, value)
1133 elif isinstance(value, int):
1134 self.set_param_int(path, value)
1135 elif isinstance(value, float):
1136 self.set_param_dble(path, value)
1137 elif isinstance(value, dict):
1138 for k,v in value.items():
1139 self.set_param(path+"."+k, v)
1140 elif isinstance(value, list):
1141 for v in value:
1142 i = self.append_list_element(path)
1143 self.set_param(path+"."+str(i), v)
1144 else:
1145 raise TypeError(f"Unsupported type {type(value)} for set_param")
1146
1147 def get_param_int(self, path : str) -> int:
1148 """Get the integer value of a parameter by dot-separated path."""
1149 logger.info("calling yac_interp_method_config_get_param_int")
1151 self._ptr, path.encode())
1152
1153 def get_param_dble(self, path : str) -> float:
1154 """Get the double value of a parameter by dot-separated path."""
1155 logger.info("calling yac_interp_method_config_get_param_dble")
1157 self._ptr, path.encode())
1158
1159 def get_param_str(self, path : str) -> str:
1160 """Get the string value of a parameter by dot-separated path."""
1161 cdef char * raw
1162 logger.info("calling yac_interp_method_config_get_param_str")
1164 self._ptr, path.encode())
1165 result = raw.decode()
1166 free(raw)
1167 return result
1168
1169 def get_list_size(self, path : str) -> int:
1170 """Return the number of elements in the list parameter at *path*."""
1171 logger.info("calling yac_interp_method_config_get_list_size")
1173 self._ptr, path.encode())
1174
1175 def append_list_element(self, path : str) -> int:
1176 """Append a default element to the list parameter at *path*.
1177
1178 Returns the zero-based index of the newly appended element so that its
1179 sub-parameters can be set with paths like ``"listname.<index>.subparam"``.
1180 """
1181 logger.info("calling yac_interp_method_config_append_list_element")
1183 self._ptr, path.encode())
1184
1185cdef class InterpWeights:
1186 cdef yac_interp_weights* _ptr;
1187 cdef _num_src_points, _num_tgt_points;
1188
1189 def __init__(self):
1190 raise TypeError("This class cannot be instantiated directly.")
1191
1192 @staticmethod
1193 cdef InterpWeights from_ptr(yac_interp_weights* interp_weights_ptr,
1194 num_src_points : int,
1195 num_tgt_points : int):
1196 cdef InterpWeights interp_weights = InterpWeights.__new__(InterpWeights)
1197 interp_weights._ptr = interp_weights_ptr
1198 interp_weights._num_src_points = num_src_points
1199 interp_weights._num_tgt_points = num_tgt_points
1200 return interp_weights
1201
1203 filename : str,
1204 src_grid_name : str = "src_grid",
1205 tgt_grid_name : str = "tgt_grid",
1206 num_global_src_points : int = 0,
1207 num_global_tgt_points : int = 0,
1208 on_existing = YAC_WEIGHT_FILE_ERROR,
1209 ):
1210 filename_bytes = filename.encode()
1211 src_grid_name_bytes = src_grid_name.encode()
1212 tgt_grid_name_bytes = tgt_grid_name.encode()
1213 cdef const char* filename_cstr = filename_bytes
1214 cdef const char* src_grid_name_cstr = src_grid_name_bytes
1215 cdef const char* tgt_grid_name_cstr = tgt_grid_name_bytes
1216 cdef yac_interp_weights* _ptr = self._ptr;
1217 cdef int num_global_src_points_c = num_global_src_points
1218 cdef int num_global_tgt_points_c = num_global_tgt_points
1219 cdef yac_weight_file_on_existing on_existing_c = on_existing
1220 logger.info("calling yac_interp_weights_write_to_file")
1221 with nogil:
1223 _ptr,
1224 filename_cstr,
1225 src_grid_name_cstr,
1226 tgt_grid_name_cstr,
1227 num_global_src_points_c,
1228 num_global_tgt_points_c,
1229 on_existing_c);
1230
1232 reorder : yac_interp_weights_reorder_type = YAC_MAPPING_ON_SRC,
1233 collection_size : int | None = None,
1234 frac_mask_fallback_value : float | None = None,
1235 scaling_factor : float = 1.0,
1236 scaling_summand : float = 0.0,
1237 yaxt_exchanger_name : str | None = None,
1238 is_source : bool = True,
1239 is_target : bool = True,
1240 collection_selection : List[int] | None = None):
1241 assert (collection_size is not None) ^ (collection_selection is not None), \
1242 "Either collection_size or collection_selection must be provided"
1243 if collection_size is not None:
1244 collection_selection = list(range(collection_size))
1245 collection_size = len(collection_selection)
1246
1247 cdef yac_collection_selection* coll_sel;
1248 cdef yac_interpolation_gen_config * config = yac_interpolation_gen_config_new();
1249 cdef size_t[:] coll_sel_np = _np.ascontiguousarray(collection_selection, dtype=_np.uintp)
1251
1252 coll_sel = yac_collection_selection_new(collection_size, <size_t*>&coll_sel_np[0]);
1255
1256 if frac_mask_fallback_value is not None:
1257 yac_interpolation_gen_config_set_frac_mask_fallback_value(config, frac_mask_fallback_value);
1259 yac_interpolation_gen_config_set_scaling_summand(config, scaling_summand);
1260 if yaxt_exchanger_name is not None:
1261 yac_interpolation_gen_config_set_yaxt_exchanger_name(config, yaxt_exchanger_name.encode());
1262
1263 logger.info("calling yac_interp_weights_get_interpolation_ext")
1264 cdef yac_interpolation * interp_ptr = yac_interp_weights_get_interpolation_ext(
1265 self._ptr, config, is_source, is_target);
1266
1268 return Interpolation.from_ptr(interp_ptr,
1269 self._num_src_points,
1270 self._num_tgt_points,
1271 collection_size,
1272 )
1273
1274def do_search(interpolation_stack : InterpolationStack, interp_grid : InterpGrid):
1275 cdef interp_method** interpstack = yac_interp_stack_config_generate(interpolation_stack._ptr)
1276 cdef yac_interp_weights * w
1277 with nogil:
1279 interpstack, interp_grid._ptr);
1280 logger.info("calling yac_interp_method_delete")
1281 yac_interp_method_delete(interpstack);
1282 free(interpstack);
1283
1284 src_loc = interp_grid._src_field.location
1285 num_src_points = interp_grid._src_field._basic_grid.get_data_size(src_loc)
1286
1287 tgt_loc = interp_grid._tgt_field.location
1288 num_tgt_points = interp_grid._tgt_field._basic_grid.get_data_size(tgt_loc)
1289
1290 return InterpWeights.from_ptr(w, num_src_points, num_tgt_points)
1291
1292def compute_weights(interpolation_stack : InterpolationStack,
1293 src_field : InterpField | None,
1294 tgt_field : InterpField | None,
1295 comm = None):
1296 dgp = DistGridPair(src_field.basic_grid, tgt_field.basic_grid)
1297 ig = InterpGrid(dgp,
1298 src_field,
1299 tgt_field)
1300 return do_search(interpolation_stack,
1301 ig)
1302
1303cdef void* fill_pointer_array(array):
1304 cdef double[::1] view1d
1305 if len(array.shape) == 1:
1306 view1d = array
1307 return &view1d[0]
1308 cdef void** ptr = <void**> malloc(array.shape[0] * sizeof(ptr[0]))
1309 cdef int i
1310 for i in range(array.shape[0]):
1311 slic = array[i, ...]
1312 assert slic.base is not None
1313 ptr[i] = fill_pointer_array(slic)
1314 return ptr
1315
1316cdef void free_pointer_array(void* ptr, shape):
1317 cdef int i
1318 if len(shape) > 1:
1319 for i in range(shape[0]):
1320 free_pointer_array((<void**>ptr)[i], shape[1:])
1321 free(ptr)
1322
1323cdef class Interpolation:
1324 cdef yac_interpolation* _ptr;
1325 cdef int _collection_size;
1326 cdef _num_src_points, _num_tgt_points
1327
1328 def __init__(self):
1329 raise TypeError("This class cannot be instantiated directly.")
1330
1331 @staticmethod
1332 cdef Interpolation from_ptr(yac_interpolation* interpolation_ptr,
1333 num_src_points, num_tgt_points,
1334 collection_size):
1335 cdef Interpolation interp = Interpolation.__new__(Interpolation)
1336 interp._ptr = interpolation_ptr
1337 interp._num_src_points = num_src_points
1338 interp._num_tgt_points = num_tgt_points
1339 interp._collection_size = collection_size
1340 return interp
1341
1342 def __call__(self, src = None, *, tgt = None, frac_mask = None):
1343 cdef double*** _src = NULL
1344 cdef double*** _frac_masks = NULL
1345 cdef double** _tgt = NULL
1346 if src is not None:
1347 src = src.reshape((-1, 1, self._num_src_points))
1348 _src = <double***>fill_pointer_array(src)
1349 if frac_mask is not None:
1350 frac_mask = frac_mask.reshape((-1, 1, self._num_src_points))
1351 _frac_masks = <double***>fill_pointer_array(frac_mask)
1352
1353 if tgt is None and self._num_tgt_points > 0:
1354 tgt = _np.empty((self._collection_size, self._num_tgt_points), dtype=_np.double)
1355
1356 if tgt is not None:
1357 tgt = tgt.reshape(self._collection_size, self._num_tgt_points)
1358 _tgt = <double**>fill_pointer_array(tgt)
1359
1360 logger.info("calling yac_interpolation_execute_frac")
1361 with nogil:
1363 self._ptr, _src, _frac_masks, _tgt);
1364 if src is not None:
1365 free_pointer_array(_src, src.shape)
1366 if frac_mask is not None:
1367 free_pointer_array(_frac_masks, frac_mask.shape)
1368 if tgt is not None:
1369 free_pointer_array(_tgt, tgt.shape)
1370 return tgt
1371
1372 @coroutine
1373 def execute_async(self, src = None, *, tgt = None, frac_mask = None):
1374 cdef double*** _src = NULL
1375 cdef double*** _frac_masks = NULL
1376 cdef double** _tgt = NULL
1377 if src is not None:
1378 src = src.reshape((-1, 1, self._num_src_points))
1379 _src = <double***>fill_pointer_array(src)
1380 if frac_mask is not None:
1381 frac_mask = frac_mask.reshape((-1, 1, self._num_src_points))
1382 _frac_masks = <double***>fill_pointer_array(frac_mask)
1383
1384 if tgt is None and self._num_tgt_points > 0:
1385 tgt = _np.empty((self._collection_size, self._num_tgt_points), dtype=_np.double)
1386 if tgt is not None:
1387 tgt = tgt.reshape(self._collection_size, self._num_tgt_points)
1388 _tgt = <double**>fill_pointer_array(tgt)
1389
1390 # wait until put buffer is free
1391 while yac_interpolation_execute_put_test(self._ptr) == 0:
1392 yield
1393
1394 with nogil:
1395 yac_interpolation_execute_put_frac(self._ptr, _src, _frac_masks);
1396 yac_interpolation_execute_get_async(self._ptr, _tgt);
1397
1398 # wait for the data
1399 while yac_interpolation_execute_get_test(self._ptr) == 0:
1400 yield
1401
1402 if src is not None:
1403 free_pointer_array(_src, src.shape)
1404 if frac_mask is not None:
1405 free_pointer_array(_frac_masks, frac_mask.shape)
1406 if tgt is not None:
1407 free_pointer_array(_tgt, tgt.shape)
1408 return tgt
1409
1410def get_io_ranks(comm=None):
1411 """
1412 Returns a list of ranks designated for I/O operations in the given
1413 communicator.
1414 """
1415 cdef MPI_Comm c_comm = MPI_COMM_NULL
1416 get_c_comm(comm, &c_comm)
1417 cdef int local_is_io
1418 cdef int *io_ranks_ptr
1419 cdef int num_io_ranks
1420 logger.info("calling yac_get_io_ranks")
1421 with nogil:
1422 yac_get_io_ranks(c_comm, &local_is_io, &io_ranks_ptr, &num_io_ranks)
1423 io_ranks = [io_ranks_ptr[i] for i in range(num_io_ranks)]
1424 free(io_ranks_ptr)
1425 return bool(local_is_io), io_ranks
1426
1427if Py_AtExit(yac_mpi_finalize) < 0:
1428 warn("could not register yac_mpi_finalize with Py_AtExit()")
struct yac_basic_grid * yac_basic_grid_unstruct_edge_new(char const *name, size_t nbr_vertices, size_t nbr_cells, size_t nbr_edges, int *num_edges_per_cell, double *x_vertices, double *y_vertices, int *cell_to_edge, int *edge_to_vertex)
Definition basic_grid.c:396
struct yac_basic_grid * yac_basic_grid_unstruct_edge_ll_new(char const *name, size_t nbr_vertices, size_t nbr_cells, size_t nbr_edges, int *num_edges_per_cell, double *x_vertices, double *y_vertices, int *cell_to_edge, int *edge_to_vertex)
Definition basic_grid.c:422
void yac_basic_grid_to_file_parallel(struct yac_basic_grid *grid, char const *filename, MPI_Comm comm)
Definition basic_grid.c:747
struct yac_basic_grid * yac_basic_grid_unstruct_new(char const *name, size_t nbr_vertices, size_t nbr_cells, int *num_vertices_per_cell, double *x_vertices, double *y_vertices, int *cell_to_vertex)
Definition basic_grid.c:344
struct yac_basic_grid_data * yac_basic_grid_get_data(struct yac_basic_grid *grid)
Definition basic_grid.c:137
size_t yac_basic_grid_add_mask(struct yac_basic_grid *grid, enum yac_location location, int const *mask, size_t count, char const *mask_name)
Definition basic_grid.c:266
struct yac_basic_grid * yac_basic_grid_reg_2d_new(char const *name, size_t nbr_vertices[2], int cyclic[2], double *lon_vertices, double *lat_vertices)
Definition basic_grid.c:300
struct yac_basic_grid * yac_basic_grid_unstruct_ll_new(char const *name, size_t nbr_vertices, size_t nbr_cells, int *num_vertices_per_cell, double *x_vertices, double *y_vertices, int *cell_to_vertex)
Definition basic_grid.c:370
char const * yac_basic_grid_get_name(struct yac_basic_grid *grid)
Definition basic_grid.c:130
size_t yac_basic_grid_add_coordinates(struct yac_basic_grid *grid, enum yac_location location, yac_coordinate_pointer coordinates, size_t count)
Definition basic_grid.c:222
int yac_basic_grid_check_coordinates(struct yac_basic_grid *grid, int fatal)
size_t yac_basic_grid_get_data_size(struct yac_basic_grid *grid, enum yac_location location)
Definition basic_grid.c:145
struct yac_basic_grid * yac_basic_grid_empty_new(char const *name)
Definition basic_grid.c:70
void yac_basic_grid_delete(struct yac_basic_grid *grid)
Definition basic_grid.c:77
from_uxgrid(cls, str name, uxgrid, bool def_edges=False, bool lonlat_edges=False)
Definition core.pyx:776
check_coordinates(self, bool is_fatal=True)
Definition core.pyx:569
set_global_index(self, yac_location location, indices)
Definition core.pyx:490
__init__(self, yac_basic_grid_ptr)
Definition core.pyx:452
reg_2d_new(cls, name, lon_vertices, lat_vertices, cyclic=[False, False])
Definition core.pyx:655
unstruct_new(cls, str name, num_vertices_per_cell, x_vertices, y_vertices, cell_to_vertex, lonlat_edges=False)
Definition core.pyx:678
to_file(self, filename, comm=None)
Definition core.pyx:465
read_scrip(cls, grid_filename, mask_filename, grid_name, name, valid_mask_value=1, use_ll_edges=False)
Definition core.pyx:617
empty(cls, str name)
Definition core.pyx:766
unstruct_edge_new(cls, str name, num_edges_per_cell, x_vertices, y_vertices, cell_to_edge, edge_to_vertex, bool lonlat_edges=False)
Definition core.pyx:724
read_icon(cls, str filename, str gridname, comm=None, mask_valid_values=None)
Definition core.pyx:578
get_data_size(self, yac_location location)
Definition core.pyx:484
add_coordinates(self, yac_location location, lon=None, lat=None)
Definition core.pyx:533
add_mask(self, yac_location location, mask, str|None mask_name=None)
Definition core.pyx:556
set_core_mask(self, yac_location location, mask)
Definition core.pyx:510
__init__(self, BasicGrid grid_a, BasicGrid grid_b, comm=None)
Definition core.pyx:844
__init__(self, Coordinates coordinates, Mask mask=None)
Definition core.pyx:816
__init__(self, DistGridPair grid_pair, InterpField src_field, InterpField tgt_field)
Definition core.pyx:868
User-oriented wrapper around a YAC interpolation method configuration.
Definition core.pyx:1084
set_param_dble(self, str path, float value)
Set a scalar double parameter by dot-separated path.
Definition core.pyx:1123
int append_list_element(self, str path)
Append a default element to the list parameter at path.
Definition core.pyx:1175
set_param_str(self, str path, str value)
Set a parameter by dot-separated path from a string value.
Definition core.pyx:1111
__init__(self, str method_name, **kwargs)
Definition core.pyx:1097
int get_param_int(self, str path)
Get the integer value of a parameter by dot-separated path.
Definition core.pyx:1147
set_param_int(self, str path, int value)
Set a scalar integer parameter by dot-separated path.
Definition core.pyx:1117
int get_list_size(self, str path)
Return the number of elements in the list parameter at path.
Definition core.pyx:1169
float get_param_dble(self, str path)
Get the double value of a parameter by dot-separated path.
Definition core.pyx:1153
str get_param_str(self, str path)
Get the string value of a parameter by dot-separated path.
Definition core.pyx:1159
set_param(self, str path, str|int|float|dict|list value)
Set a parameter by dot-separated path, dispatching on type.
Definition core.pyx:1129
get_interpolation(self, yac_interp_weights_reorder_type reorder=YAC_MAPPING_ON_SRC, int|None collection_size=None, float|None frac_mask_fallback_value=None, float scaling_factor=1.0, float scaling_summand=0.0, str|None yaxt_exchanger_name=None, bool is_source=True, bool is_target=True, List[int]|None collection_selection=None)
Definition core.pyx:1240
write_to_file(self, str filename, str src_grid_name="src_grid", str tgt_grid_name="tgt_grid", int num_global_src_points=0, int num_global_tgt_points=0, on_existing=YAC_WEIGHT_FILE_ERROR)
Definition core.pyx:1209
add_user_file(self, str|None filename=None, yac_interp_file_on_missing_file|str on_missing_file=YAC_INTERP_FILE_ON_MISSING_FILE_DEFAULT, yac_interp_file_on_success|str on_success=YAC_INTERP_FILE_ON_SUCCESS_DEFAULT)
Definition core.pyx:1027
add_method(self, method, **kwargs)
Definition core.pyx:1080
add_conservative(self, int order=YAC_INTERP_CONSERV_ORDER_DEFAULT, bool enforced_conserv=YAC_INTERP_CONSERV_ENFORCED_CONSERV_DEFAULT, bool partial_coverage=YAC_INTERP_CONSERV_PARTIAL_COVERAGE_DEFAULT, yac_interp_method_conserv_normalisation|str normalisation=YAC_INTERP_CONSERV_NORMALISATION_DEFAULT)
Definition core.pyx:967
add_check(self, str constructor_key=_decode(YAC_INTERP_CHECK_CONSTRUCTOR_KEY_DEFAULT), str do_search_key=_decode(YAC_INTERP_CHECK_DO_SEARCH_KEY_DEFAULT))
Definition core.pyx:1043
add(self, name, *args, **kwargs)
Definition core.pyx:912
add_fixed(self, float value)
Definition core.pyx:1018
add_average(self, yac_interp_avg_weight_type|str weight_type=YAC_INTERP_AVG_WEIGHT_TYPE_DEFAULT, bool partial_coverage=YAC_INTERP_AVG_PARTIAL_COVERAGE_DEFAULT)
Definition core.pyx:918
add_rbf(self, int n=YAC_INTERP_RBF_N_DEFAULT, float max_search_distance=YAC_INTERP_RBF_MAX_SEARCH_DISTANCE_DEFAULT, float scale=YAC_INTERP_RBF_SCALE_DEFAULT, int rbf_kernel=YAC_INTERP_RBF_KERNEL_DEFAULT)
Definition core.pyx:1059
add_creep(self, int creep_distance=YAC_INTERP_CREEP_DISTANCE_DEFAULT)
Definition core.pyx:1065
add_source_to_target_map(self, float spread_distance=YAC_INTERP_SPMAP_SPREAD_DISTANCE_DEFAULT, float max_search_distance=YAC_INTERP_SPMAP_MAX_SEARCH_DISTANCE_DEFAULT, yac_interp_spmap_weight_type|str weight_type=YAC_INTERP_SPMAP_WEIGHTED_DEFAULT, yac_interp_spmap_scale_type|str scale_type=YAC_INTERP_SPMAP_SCALE_TYPE_DEFAULT, float src_sphere_radius=0.0, str|None src_filename=None, str|None src_varname=None, int src_min_global_id=YAC_INTERP_SPMAP_MIN_GLOBAL_ID_DEFAULT, float tgt_sphere_radius=0.0, str|None tgt_filename=None, str|None tgt_varname=None, int tgt_min_global_id=YAC_INTERP_SPMAP_MIN_GLOBAL_ID_DEFAULT)
Definition core.pyx:989
add_nnn(self, yac_interp_nnn_weight_type|str nnn_type=YAC_INTERP_NNN_WEIGHTED_DEFAULT, int n=YAC_INTERP_NNN_N_DEFAULT, float max_search_distance=YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT, float scale=YAC_INTERP_NNN_GAUSS_SCALE_DEFAULT)
Definition core.pyx:954
add_dnn(self, yac_interp_dnn_weight_type|str weight_type=YAC_INTERP_DNN_WEIGHT_DEFAULT, yac_interp_dnn_search_distance_type|str search_distance_type=YAC_INTERP_DNN_SEARCH_DISTANCE_TYPE_DEFAULT, float search_distance=YAC_INTERP_DNN_SEARCH_DISTANCE_DEFAULT, float scale=YAC_INTERP_DNN_SCALE_DEFAULT)
Definition core.pyx:939
add_from_config(self, InterpMethodConfig config)
Add a method config object to this interpolation stack.
Definition core.pyx:1069
from_list(cls, interp_list)
Definition core.pyx:895
add_ncc(self, yac_interp_ncc_weight_type|str weight_type=YAC_INTERP_NCC_WEIGHT_TYPE_DEFAULT, bool partial_coverage=YAC_INTERP_NCC_PARTIAL_COVERAGE_DEFAULT)
Definition core.pyx:928
char * yac_get_core_version(void)
void yac_dist_grid_pair_delete(struct yac_dist_grid_pair *grid_pair)
Definition dist_grid.c:2377
struct yac_dist_grid_pair * yac_dist_grid_pair_new(struct yac_basic_grid *grid_a, struct yac_basic_grid *grid_b, MPI_Comm comm)
Definition dist_grid.c:2089
void yac_collection_selection_delete(struct yac_collection_selection *collection_selection)
Delete a collection selection object.
struct yac_collection_selection * yac_collection_selection_new(size_t collection_size, size_t const *selection_indices)
Create a new collection selection.
void yac_interp_grid_delete(struct yac_interp_grid *interp_grid)
struct yac_interp_grid * yac_interp_grid_new(struct yac_dist_grid_pair *grid_pair, char const *src_grid_name, char const *tgt_grid_name, size_t num_src_fields, struct yac_interp_field const *src_fields, struct yac_interp_field const tgt_field)
Definition interp_grid.c:31
void yac_interp_method_delete(struct interp_method **method)
Delete an interpolation stack and free its resources (but not the pointer array).
struct yac_interp_weights * yac_interp_method_do_search(struct interp_method **method, struct yac_interp_grid *interp_grid)
Perform weight computation using given interpolation stack and grid.
enum callback_type type
void yac_interp_method_config_set_param_int(struct yac_interp_method_config *config, char const *path, int value)
double yac_interp_method_config_get_param_dble(struct yac_interp_method_config const *config, char const *path)
size_t yac_interp_method_config_append_list_element(struct yac_interp_method_config *config, char const *path)
int yac_interp_method_config_get_param_int(struct yac_interp_method_config const *config, char const *path)
void yac_interp_method_config_set_param_dble(struct yac_interp_method_config *config, char const *path, double value)
char * yac_interp_method_config_get_param_str(struct yac_interp_method_config const *config, char const *path)
struct yac_interp_method_config * yac_interp_method_config_default_from_name_new(char const *method_name)
void yac_interp_method_config_set_param_str(struct yac_interp_method_config *config, char const *path, char const *value)
size_t yac_interp_method_config_get_list_size(struct yac_interp_method_config const *config, char const *path)
void yac_interp_method_config_delete(struct yac_interp_method_config *config)
void yac_interp_stack_config_add_check(struct yac_interp_stack_config *interp_stack_config, char const *constructor_key, char const *do_search_key)
void yac_interp_stack_config_add_spmap(struct yac_interp_stack_config *interp_stack_config, double spread_distance, double max_search_distance, enum yac_interp_spmap_weight_type weight_type, enum yac_interp_spmap_scale_type scale_type, double src_sphere_radius, char const *src_filename, char const *src_varname, int src_min_global_id, double tgt_sphere_radius, char const *tgt_filename, char const *tgt_varname, int tgt_min_global_id)
void yac_interp_stack_config_add_fixed(struct yac_interp_stack_config *interp_stack_config, double value)
void yac_interp_stack_config_add_hcsbb(struct yac_interp_stack_config *interp_stack_config)
void yac_interp_stack_config_add_rbf(struct yac_interp_stack_config *interp_stack_config, size_t n, double max_search_distance, double scale)
void yac_interp_stack_config_add_average(struct yac_interp_stack_config *interp_stack_config, enum yac_interp_avg_weight_type reduction_type, int partial_coverage)
void yac_interp_stack_config_add_creep(struct yac_interp_stack_config *interp_stack_config, int creep_distance)
void yac_interp_stack_config_add_nnn(struct yac_interp_stack_config *interp_stack_config, enum yac_interp_nnn_weight_type type, size_t n, double max_search_distance, double scale)
void yac_interp_stack_config_delete(struct yac_interp_stack_config *interp_stack_config)
Free an interpolation stack configuration object.
void yac_interp_stack_config_add_conservative(struct yac_interp_stack_config *interp_stack_config, int order, int enforced_conserv, int partial_coverage, enum yac_interp_method_conserv_normalisation normalisation)
struct interp_method ** yac_interp_stack_config_generate(struct yac_interp_stack_config *interp_stack)
Generate a NULL-terminated array of interpolation methods from the stack configuration.
void yac_interp_stack_config_add_dnn(struct yac_interp_stack_config *interp_stack_config, enum yac_interp_dnn_weight_type type, enum yac_interp_dnn_search_distance_type search_distance_type, double search_distance, double scale)
void yac_interp_stack_config_add(struct yac_interp_stack_config *interp_stack_config, struct yac_interp_method_config *method_config)
Add an interpolation method configuration to the end of the stack.
void yac_interp_stack_config_add_ncc(struct yac_interp_stack_config *interp_stack_config, enum yac_interp_ncc_weight_type weight_type, int partial_coverage)
void yac_interp_stack_config_add_user_file(struct yac_interp_stack_config *interp_stack_config, char const *filename, enum yac_interp_file_on_missing_file on_missing_file, enum yac_interp_file_on_success on_success)
struct yac_interp_stack_config * yac_interp_stack_config_new()
Allocate a new empty interpolation stack configuration object.
struct yac_interpolation * yac_interp_weights_get_interpolation_ext(struct yac_interp_weights const *weights, struct yac_interpolation_gen_config const *config, int is_source, int is_target)
void yac_interp_weights_write_to_file(struct yac_interp_weights *weights, char const *filename, char const *src_grid_name, char const *tgt_grid_name, size_t src_grid_size, size_t tgt_grid_size, enum yac_weight_file_on_existing on_existing)
int yac_interpolation_execute_put_test(struct yac_interpolation *interp)
Test whether the asynchronous put phase has completed.
void yac_interpolation_execute_frac(struct yac_interpolation *interp, double ***src_fields, double ***src_frac_masks, double **tgt_field)
Execute interpolation with fractional masks and write results to the target field.
void yac_interpolation_execute_get_async(struct yac_interpolation *interp, double **tgt_field)
Complete interpolation asynchronously and write results to the target field (get phase).
void yac_interpolation_execute_put_frac(struct yac_interpolation *interp, double ***src_fields, double ***src_frac_masks)
Provide source field data with fractional masks and start asynchronous execution of interpolation (pu...
int yac_interpolation_execute_get_test(struct yac_interpolation *interp)
Test whether the asynchronous get phase has completed.
void yac_interpolation_gen_config_set_reorder(struct yac_interpolation_gen_config *config, enum yac_interp_weights_reorder_type reorder)
Set the reordering strategy for interpolation weights.
void yac_interpolation_gen_config_set_collection_size(struct yac_interpolation_gen_config *config, size_t collection_size)
Set the number of contiguous fields (starting at "0") in the field collection.
void yac_interpolation_gen_config_set_collection_selection(struct yac_interpolation_gen_config *config, struct yac_collection_selection const *collection_selection)
Set the collection selection of source field in the source field collection.
void yac_interpolation_gen_config_set_scaling_factor(struct yac_interpolation_gen_config *config, double scaling_factor)
Set the multiplicative scaling factor.
void yac_interpolation_gen_config_set_yaxt_exchanger_name(struct yac_interpolation_gen_config *config, char const *name)
Set the name of the Yaxt exchanger.
void yac_interpolation_gen_config_delete(struct yac_interpolation_gen_config *config)
Release a interpolation generation configuration structure allocated by yac_interpolation_gen_config_...
void yac_interpolation_gen_config_set_frac_mask_fallback_value(struct yac_interpolation_gen_config *config, double frac_mask_fallback_value)
Set the fractional mask fallback value.
struct yac_interpolation_gen_config * yac_interpolation_gen_config_new(void)
Allocate and initialise an interpolation generation configuration structure.
void yac_interpolation_gen_config_set_scaling_summand(struct yac_interpolation_gen_config *config, double scaling_summand)
Set the additive scaling summand.
void yac_get_io_ranks(MPI_Comm comm, int *local_is_io_, int **io_ranks_, int *num_io_ranks_)
Definition io_utils.c:271
execute_async(self, src=None, *, tgt=None, frac_mask=None)
Definition core.pyx:1373
_to_yac_int_np_array(arr)
Definition core.pyx:431
compute_weights(InterpolationStack interpolation_stack, InterpField|None src_field, InterpField|None tgt_field, comm=None)
Definition core.pyx:1295
get_io_ranks(comm=None)
Returns a list of ranks designated for I/O operations in the given communicator.
Definition core.pyx:1410
__init__(self)
Definition core.pyx:1328
version()
using int here is ok.
Definition core.pyx:356
do_search(InterpolationStack interpolation_stack, InterpGrid interp_grid)
Definition core.pyx:1274
Coordinates
Initialization.
Definition core.pyx:446
__call__(self, src=None, *, tgt=None, frac_mask=None)
Definition core.pyx:1342
lonlat2xyz(lon, lat)
Definition core.pyx:401
void yac_read_icon_basic_grid_parallel_2(char const *filename, char const *gridname, MPI_Comm comm, struct yac_basic_grid **basic_grid, size_t *cell_coordinate_idx, int **cell_mask)
struct yac_basic_grid * yac_read_scrip_basic_grid(char const *grid_filename, char const *mask_filename, char const *grid_name, int valid_mask_value, char const *name, int use_ll_edges, size_t *cell_coord_idx, size_t **duplicated_cell_idx, yac_int **orig_cell_global_ids, size_t *nbr_duplicated_cells)
int const * location
char const * name
Definition toy_scrip.c:114
void yac_mpi_finalize()
Definition yac_mpi.c:106
void yac_yaxt_init(MPI_Comm comm)
Definition yac_mpi.c:41
void yac_mpi_init()
Definition yac_mpi.c:77