16 int const * tgt_id,
double const * weights,
19 unsigned num_src_fields,
20 int const * num_links_per_src_field,
21 int * tgt_id_fixed,
unsigned num_fixed_tgt,
22 double * fixed_values,
23 int * num_tgt_per_fixed_value,
24 unsigned num_fixed_values,
38 YAC_HANDLE_ERROR(nc_def_dim(ncid,
"num_links", num_links, &dim_weight_id[0]));
42 nc_def_dim(ncid,
"num_src_fields", num_src_fields, &dim_weight_id[2]));
45 if (num_fixed_values > 0) {
47 nc_def_dim(ncid,
"num_fixed_values", num_fixed_values, &dim_weight_id[4]));
49 nc_def_dim(ncid,
"num_fixed_dst", num_fixed_tgt, &dim_weight_id[5]));
52 int var_src_add_id, var_dst_add_id, var_weight_id, var_num_links_id,
53 src_var_locs_id, tgt_var_loc_id, var_fixed_values_id,
54 var_num_dst_per_fixed_value_id, var_dst_add_fixed_id;
60 ncid,
"src_address", NC_INT, 1, dim_weight_id, &var_src_add_id));
63 ncid,
"dst_address", NC_INT, 1, dim_weight_id, &var_dst_add_id));
66 ncid,
"remap_matrix", NC_DOUBLE, 2, dim_weight_id, &var_weight_id));
69 ncid,
"num_links_per_src_field", NC_INT, 1, dim_weight_id + 2,
74 ncid,
"src_locations", NC_CHAR, 2, &dim_weight_id[2], &src_var_locs_id));
77 ncid,
"dst_location", NC_CHAR, 1, &dim_weight_id[3], &tgt_var_loc_id));
78 if (num_fixed_values > 0) {
81 ncid,
"fixed_values", NC_DOUBLE, 1, &dim_weight_id[4],
82 &var_fixed_values_id));
85 ncid,
"num_dst_per_fixed_value", NC_INT, 1, &dim_weight_id[4],
86 &var_num_dst_per_fixed_value_id));
89 ncid,
"dst_address_fixed", NC_INT, 1, &dim_weight_id[5],
90 &var_dst_add_fixed_id));
117 int * src_address =
xmalloc(num_links *
sizeof(*src_address));
118 int * tgt_address =
xmalloc(num_links *
sizeof(*tgt_address));
120 for (
unsigned i = 0; i < num_links; i++) {
121 src_address[i] = src_id[i] + 1;
122 tgt_address[i] = tgt_id[i] + 1;
133 nc_put_var_int(ncid, var_num_links_id, num_links_per_src_field));
136 for (
unsigned i = 0; i < num_src_fields; ++i) {
137 char const * loc_str =
yac_loc2str(src_locations[i]);
138 size_t str_start[2] = {i, 0};
139 size_t str_count[2] = {1, strlen(loc_str)};
141 nc_put_vara_text(ncid, src_var_locs_id, str_start, str_count, loc_str));
146 size_t str_start[1] = {0};
147 size_t str_count[1] = {strlen(loc_str)};
149 nc_put_vara_text(ncid, tgt_var_loc_id, str_start, str_count, loc_str));
152 if (num_fixed_values > 0) {
154 int * tgt_address_fixed =
155 xmalloc(num_fixed_tgt *
sizeof(*tgt_address_fixed));
156 for (
unsigned i = 0; i < num_fixed_tgt; i++)
157 tgt_address_fixed[i] = tgt_id_fixed[i] + 1;
159 YAC_HANDLE_ERROR(nc_put_var_int(ncid, var_dst_add_fixed_id, tgt_address_fixed));
160 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_fixed_values_id, fixed_values));
162 num_tgt_per_fixed_value));
164 free(tgt_address_fixed);
171 int const * tgt_id,
double const * weights,
181 int dim_weight_id[6];
184 YAC_HANDLE_ERROR(nc_def_dim(ncid,
"num_links", num_links, &dim_weight_id[0]));
187 int var_src_add_id, var_dst_add_id, var_weight_id;
192 ncid,
"src_address", NC_INT, 1, dim_weight_id, &var_src_add_id));
195 ncid,
"dst_address", NC_INT, 1, dim_weight_id, &var_dst_add_id));
198 ncid,
"remap_matrix", NC_DOUBLE, 2, dim_weight_id, &var_weight_id));
201 char const title[] =
"CDO remapping";
203 strlen(title), title));
214 int * src_address =
xmalloc(num_links *
sizeof(*src_address));
215 int * tgt_address =
xmalloc(num_links *
sizeof(*tgt_address));
217 for (
unsigned i = 0; i < num_links; i++) {
218 src_address[i] = src_id[i] + 1;
219 tgt_address[i] = tgt_id[i] + 1;
237 unsigned ref_num_src_fields,
238 int const * ref_num_links_per_src_field,
244 char const * ref_src_grid_name,
245 char const * ref_tgt_grid_name) {
257 num_wgts == 1,
"ERROR(check_weight_file): test only supports num_wgts == 1")
260 size_t str_version_len;
264 str_version =
xmalloc(str_version_len + 1);
265 str_version[str_version_len] =
'\0';
268 size_t str_fixed_len;
271 nc_inq_attlen(ncid, var_id,
"contains_fixed_dst", &str_fixed_len));
272 str_fixed =
xmalloc(str_fixed_len + 1);
273 str_fixed[str_fixed_len] =
'\0';
274 YAC_HANDLE_ERROR(nc_get_att_text(ncid, var_id,
"contains_fixed_dst", str_fixed));
276 unsigned contains_fixed_dst =
277 ((strlen(
"TRUE") == str_fixed_len) &&
278 !strncmp(
"TRUE", str_fixed, str_fixed_len));
280 contains_fixed_dst ||
281 ((strlen(
"FALSE") == str_fixed_len) &&
282 !strncmp(
"FALSE", str_fixed, str_fixed_len)),
283 "ERROR(check_weight_file): invalid global attribute contains_fixed_dst")
286 size_t src_grid_name_len = 0, tgt_grid_name_len = 0;
288 YAC_HANDLE_ERROR(nc_inq_attlen(ncid, var_id,
"src_grid_name", &src_grid_name_len));
293 YAC_HANDLE_ERROR(nc_inq_attlen(ncid, var_id,
"dst_grid_name", &tgt_grid_name_len));
301 YAC_HANDLE_ERROR(nc_inq_attlen(ncid, var_id,
"contains_links", &str_link_len));
302 str_link =
xmalloc(str_link_len + 1);
303 str_link[str_link_len] =
'\0';
304 YAC_HANDLE_ERROR(nc_get_att_text(ncid, var_id,
"contains_links", str_link));
306 unsigned contains_links =
307 (strlen(
"TRUE") == str_link_len) &&
308 !strncmp(
"TRUE", str_link, str_link_len);
311 ((strlen(
"FALSE") == str_link_len) &&
312 !strncmp(
"FALSE", str_link, str_link_len)),
313 "ERROR(check_weight_file): invalid global attribute contains_links")
317 size_t num_links = 0;
318 if (contains_links) {
321 YAC_ASSERT(num_links != 0,
"ERROR(check_weight_file): no links defined")
324 PUT_ERR(
"wrong number of links in weight file\n");
327 size_t num_src_fields = 0;
328 if (contains_links) {
332 num_src_fields != 0,
"ERROR(check_weight_file): no source fields")
333 if (ref_num_src_fields != num_src_fields)
334 PUT_ERR(
"wrong number of source fields in weight file\n");
338 size_t max_loc_str_len;
343 "ERROR(check_weight_file): wrong max location string length")
345 size_t num_fixed_values = 0;
346 size_t num_fixed_tgt = 0;
347 if (contains_fixed_dst) {
358 int * src_address =
xmalloc(num_links *
sizeof(*src_address));
359 int * tgt_address =
xmalloc(num_links *
sizeof(*tgt_address));
360 double * weights =
xmalloc(num_links *
sizeof(*weights));
361 int * num_links_per_src_field =
362 xmalloc(num_src_fields *
sizeof(*num_links_per_src_field));
364 xmalloc(num_src_fields *
sizeof(*src_locations));
366 double * fixed_values =
xmalloc(num_fixed_values *
sizeof(*fixed_values));
367 int * num_tgt_per_fixed_value =
368 xmalloc(num_fixed_values *
sizeof(*num_tgt_per_fixed_value));
369 int * tgt_address_fixed =
xmalloc(num_fixed_tgt *
sizeof(*tgt_address_fixed));
372 if (contains_links) {
387 for (
unsigned i = 0; i < num_src_fields; ++i) {
389 size_t str_start[2] = {i, 0};
391 YAC_HANDLE_ERROR(nc_get_vara_text(ncid, var_id, str_start, str_count, loc_str));
402 if (contains_fixed_dst) {
417 PUT_ERR(
"wrong version string\n");
418 if ((strlen(ref_src_grid_name) != src_grid_name_len) ||
419 strncmp(ref_src_grid_name,
src_grid_name, src_grid_name_len))
420 PUT_ERR(
"wrong src_grid_name\n");
421 if ((strlen(ref_tgt_grid_name) != tgt_grid_name_len) ||
422 strncmp(ref_tgt_grid_name,
tgt_grid_name, tgt_grid_name_len))
423 PUT_ERR(
"wrong tgt_grid_name\n");
426 PUT_ERR(
"file contains links, but reference data does not\n");
427 }
else if (contains_links) {
430 PUT_ERR(
"wrong src_address\n");
431 if ((strlen(ref_src_grid_name) != src_grid_name_len) ||
432 strncmp(ref_src_grid_name,
src_grid_name, src_grid_name_len))
433 PUT_ERR(
"wrong src_grid_name\n");
436 PUT_ERR(
"wrong tgt_address\n");
437 if ((strlen(ref_tgt_grid_name) != tgt_grid_name_len) ||
438 strncmp(ref_tgt_grid_name,
tgt_grid_name, tgt_grid_name_len))
439 PUT_ERR(
"wrong src_grid_name\n");
443 for (
unsigned i = 0; i <
MIN(num_src_fields, ref_num_src_fields); ++i)
444 if (ref_num_links_per_src_field[i] != num_links_per_src_field[i])
445 PUT_ERR(
"wrong number of links per source field\n");
447 for (
unsigned i = 0; i <
MIN(num_src_fields, ref_num_src_fields); ++i)
448 if (ref_src_locations[i] != src_locations[i])
449 PUT_ERR(
"wrong source location\n");
450 if (ref_tgt_location != tgt_location)
PUT_ERR(
"wrong target location\n");
453 PUT_ERR(
"file contains fixed target data, but reference data does not\n");
454 }
else if (contains_fixed_dst) {
456 PUT_ERR(
"wrong number of fixed values\n");
458 unsigned * used_flag =
xcalloc(num_fixed_values,
sizeof(*used_flag));
459 unsigned match_count = 0;
462 for (
unsigned j = 0, offset = 0; j < num_fixed_values;
463 offset += num_tgt_per_fixed_value[j++]) {
464 if ((!used_flag[j]) &&
468 for (
int k = 0; k < num_tgt_per_fixed_value[j]; ++k) {
470 tgt_address_fixed[offset + k] - 1)
471 PUT_ERR(
"wrong fixed target address\n");
481 PUT_ERR(
"wrong fixed values data");
489 free(num_tgt_per_fixed_value);
490 free(tgt_address_fixed);
496 free(num_links_per_src_field);
void check_weight_file(char const *file_name, int const *ref_src_address, int const *ref_tgt_address, double const *ref_weights, unsigned ref_num_links, enum yac_location const *ref_src_locations, unsigned ref_num_src_fields, int const *ref_num_links_per_src_field, int const *ref_tgt_address_fixed, double const *ref_fixed_values, int const *ref_num_tgt_per_fixed_value, unsigned ref_num_fixed_values, enum yac_location ref_tgt_location, char const *ref_src_grid_name, char const *ref_tgt_grid_name)
void write_weight_file(char const *file_name, int const *src_id, int const *tgt_id, double const *weights, unsigned num_links, enum yac_location const *src_locations, unsigned num_src_fields, int const *num_links_per_src_field, int *tgt_id_fixed, unsigned num_fixed_tgt, double *fixed_values, int *num_tgt_per_fixed_value, unsigned num_fixed_values, enum yac_location tgt_location, char const *src_grid_name, char const *tgt_grid_name)