YAC 3.12.0
Yet Another Coupler
Loading...
Searching...
No Matches
grid_file_common.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <string.h>
6#include <math.h>
7#include <netcdf.h>
8
9#include "tests.h"
10#include "grid_file_common.h"
11#include "io_utils.h"
12#include "utils_common.h"
13#include "geometry.h"
14
15static int compare_cell_coords(double * a, double * b, int count) {
16
17 for (int order = -1; order <= 1; order += 2) {
18 for (int start = 0; start < count; ++start) {
19
20 int differences = 0;
21
22 for (int i = 0; i < count; ++i) {
23
24 int j = (count + start + order * i) % count;
25
26 if (fabs(a[i] - b[j]) > 1e-6) ++differences;
27 }
28
29 if (!differences) return 0;
30 }
31 }
32 return 1;
33}
34
35static int compare_ints(int * a, int * b, int count) {
36
37 for (int order = -1; order <= 1; order += 2) {
38 for (int start = 0; start < count; ++start) {
39
40 int differences = 0;
41
42 for (int i = 0; i < count; ++i) {
43
44 int j = (count + start + order * i) % count;
45
46 if (a[i] != b[j]) ++differences;
47 }
48
49 if (!differences) return 0;
50 }
51 }
52 return 1;
53}
54
56 char const * filename, char const * grid_name,
57 size_t ref_num_cells, size_t ref_num_corners_per_cell,
58 double * ref_cla, double * ref_clo, double * ref_lat, double * ref_lon,
59 int * ref_cell_global_ids,int * ref_core_cell_mask,
60 int * ref_vertex_global_ids,int * ref_core_vertex_mask,
61 int * ref_edge_global_ids,int * ref_core_edge_mask) {
62
63 if (!yac_file_exists(filename)) {
64 PUT_ERR("error file does not exist");
65 return;
66 }
67
68 size_t grid_name_len = strlen(grid_name) + 1;
69
70 char nv_dim_name[3 + grid_name_len];
71 char nc_dim_name[3 + grid_name_len];
72
73 char cla_var_name[4 + grid_name_len];
74 char clo_var_name[4 + grid_name_len];
75 char lat_var_name[4 + grid_name_len];
76 char lon_var_name[4 + grid_name_len];
77 char gid_var_name[4 + grid_name_len];
78 char cmk_var_name[4 + grid_name_len];
79 char vgid_var_name[5 + grid_name_len];
80 char vcmk_var_name[5 + grid_name_len];
81 char egid_var_name[5 + grid_name_len];
82 char ecmk_var_name[5 + grid_name_len];
83
84 snprintf(nv_dim_name, 3 + grid_name_len, "nv_%s", grid_name);
85 snprintf(nc_dim_name, 3 + grid_name_len, "nc_%s", grid_name);
86
87 snprintf(cla_var_name, 4 + grid_name_len, "%s.cla", grid_name);
88 snprintf(clo_var_name, 4 + grid_name_len, "%s.clo", grid_name);
89 snprintf(lat_var_name, 4 + grid_name_len, "%s.lat", grid_name);
90 snprintf(lon_var_name, 4 + grid_name_len, "%s.lon", grid_name);
91 snprintf(gid_var_name, 4 + grid_name_len, "%s.gid", grid_name);
92 snprintf(cmk_var_name, 4 + grid_name_len, "%s.cmk", grid_name);
93 snprintf(vgid_var_name, 5 + grid_name_len, "%s.vgid", grid_name);
94 snprintf(vcmk_var_name, 5 + grid_name_len, "%s.vcmk", grid_name);
95 snprintf(egid_var_name, 5 + grid_name_len, "%s.egid", grid_name);
96 snprintf(ecmk_var_name, 5 + grid_name_len, "%s.ecmk", grid_name);
97
98 int ncid;
99 yac_nc_open(filename, NC_NOWRITE, &ncid);
100
101 int nv_dim_id, nc_dim_id;
102 yac_nc_inq_dimid(ncid, nv_dim_name, &nv_dim_id);
103 yac_nc_inq_dimid(ncid, nc_dim_name, &nc_dim_id);
104
105 int cla_var_id, clo_var_id, lat_var_id, lon_var_id,
106 gid_var_id, cmk_var_id, vgid_var_id, vcmk_var_id,
107 egid_var_id, ecmk_var_id;
108 yac_nc_inq_varid(ncid, cla_var_name, &cla_var_id);
109 yac_nc_inq_varid(ncid, clo_var_name, &clo_var_id);
110 if (ref_lat)
111 yac_nc_inq_varid(ncid, lat_var_name, &lat_var_id);
112 if (ref_lon)
113 yac_nc_inq_varid(ncid, lon_var_name, &lon_var_id);
114 if (ref_cell_global_ids)
115 yac_nc_inq_varid(ncid, gid_var_name, &gid_var_id);
116 if (ref_core_cell_mask)
117 yac_nc_inq_varid(ncid, cmk_var_name, &cmk_var_id);
118 if (ref_vertex_global_ids)
119 yac_nc_inq_varid(ncid, vgid_var_name, &vgid_var_id);
120 if (ref_core_vertex_mask)
121 yac_nc_inq_varid(ncid, vcmk_var_name, &vcmk_var_id);
122 if (ref_edge_global_ids)
123 yac_nc_inq_varid(ncid, egid_var_name, &egid_var_id);
124 if (ref_core_edge_mask)
125 yac_nc_inq_varid(ncid, ecmk_var_name, &ecmk_var_id);
126
127 size_t nv_dim_len, nc_dim_len;
128 YAC_HANDLE_ERROR(nc_inq_dimlen(ncid, nv_dim_id, &nv_dim_len));
129 YAC_HANDLE_ERROR(nc_inq_dimlen(ncid, nc_dim_id, &nc_dim_len));
130
131 if (nv_dim_len != ref_num_corners_per_cell)
132 PUT_ERR("wrong nv dim size");
133 if (nc_dim_len < ref_num_cells) PUT_ERR("wrong nc dim size");
134
135 nc_type type;
136 int ndims;
137 int dimids[NC_MAX_VAR_DIMS];
138
140 nc_inq_var(ncid, cla_var_id, NULL, &type, &ndims, dimids, NULL));
141 if (type != NC_DOUBLE) PUT_ERR("wrong type for cla");
142 if (ndims != 2) PUT_ERR("wrong ndims for cla");
143 if ((dimids[0] != nc_dim_id) ||
144 (dimids[1] != nv_dim_id)) PUT_ERR("wrong dimensions for cla");
145
147 nc_inq_var(ncid, clo_var_id, NULL, &type, &ndims, dimids, NULL));
148 if (type != NC_DOUBLE) PUT_ERR("wrong type for clo");
149 if (ndims != 2) PUT_ERR("wrong ndims for clo");
150 if ((dimids[0] != nc_dim_id) ||
151 (dimids[1] != nv_dim_id)) PUT_ERR("wrong dimensions for clo");
152
153 if (ref_lat) {
155 nc_inq_var(ncid, lat_var_id, NULL, &type, &ndims, dimids, NULL));
156 if (type != NC_DOUBLE) PUT_ERR("wrong type for lat");
157 if (ndims != 1) PUT_ERR("wrong ndims for lat");
158 if (dimids[0] != nc_dim_id) PUT_ERR("wrong dimensions for lat");
159 }
160
161 if (ref_lon) {
163 nc_inq_var(ncid, lon_var_id, NULL, &type, &ndims, dimids, NULL));
164 if (type != NC_DOUBLE) PUT_ERR("wrong type for lon");
165 if (ndims != 1) PUT_ERR("wrong ndims for lon");
166 if (dimids[0] != nc_dim_id) PUT_ERR("wrong dimensions for lon");
167 }
168
169 if (ref_cell_global_ids) {
171 nc_inq_var(ncid, gid_var_id, NULL, &type, &ndims, dimids, NULL));
172 if (type != NC_INT) PUT_ERR("wrong type for gid");
173 if (ndims != 1) PUT_ERR("wrong ndims for gid");
174 if (dimids[0] != nc_dim_id) PUT_ERR("wrong dimensions for gid");
175 }
176
177 if (ref_core_cell_mask) {
179 nc_inq_var(ncid, cmk_var_id, NULL, &type, &ndims, dimids, NULL));
180 if (type != NC_INT) PUT_ERR("wrong type for cmk");
181 if (ndims != 1) PUT_ERR("wrong ndims for cmk");
182 if (dimids[0] != nc_dim_id) PUT_ERR("wrong dimensions for cmk");
183 }
184
185 if (ref_vertex_global_ids) {
187 nc_inq_var(ncid, vgid_var_id, NULL, &type, &ndims, dimids, NULL));
188 if (type != NC_INT) PUT_ERR("wrong type for vgid");
189 if (ndims != 2) PUT_ERR("wrong ndims for vgid");
190 if ((dimids[0] != nc_dim_id) ||
191 (dimids[1] != nv_dim_id)) PUT_ERR("wrong dimensions for vgid");
192 }
193
194 if (ref_core_vertex_mask) {
196 nc_inq_var(ncid, vcmk_var_id, NULL, &type, &ndims, dimids, NULL));
197 if (type != NC_INT) PUT_ERR("wrong type for vcmk");
198 if (ndims != 2) PUT_ERR("wrong ndims for vcmk");
199 if ((dimids[0] != nc_dim_id) ||
200 (dimids[1] != nv_dim_id)) PUT_ERR("wrong dimensions for vcmk");
201 }
202
203 if (ref_edge_global_ids) {
205 nc_inq_var(ncid, egid_var_id, NULL, &type, &ndims, dimids, NULL));
206 if (type != NC_INT) PUT_ERR("wrong type for egid");
207 if (ndims != 2) PUT_ERR("wrong ndims for egid");
208 if ((dimids[0] != nc_dim_id) ||
209 (dimids[1] != nv_dim_id)) PUT_ERR("wrong dimensions for egid");
210 }
211
212 if (ref_core_edge_mask) {
214 nc_inq_var(ncid, ecmk_var_id, NULL, &type, &ndims, dimids, NULL));
215 if (type != NC_INT) PUT_ERR("wrong type for ecmk");
216 if (ndims != 2) PUT_ERR("wrong ndims for ecmk");
217 if ((dimids[0] != nc_dim_id) ||
218 (dimids[1] != nv_dim_id)) PUT_ERR("wrong dimensions for ecmk");
219 }
220
221 double * clo = malloc(nc_dim_len * nv_dim_len * sizeof(*clo));
222 double * cla = malloc(nc_dim_len * nv_dim_len * sizeof(*cla));
223 double * lon = ref_lon?malloc(nc_dim_len * sizeof(*lon)):NULL;
224 double * lat = ref_lat?malloc(nc_dim_len * sizeof(*lat)):NULL;
225 int * gid = ref_cell_global_ids?malloc(nc_dim_len * sizeof(*gid)):NULL;
226 int * cmk = ref_core_cell_mask?malloc(nc_dim_len * sizeof(*cmk)):NULL;
227 int * vgid = ref_vertex_global_ids?malloc(nc_dim_len * nv_dim_len * sizeof(*vgid)):NULL;
228 int * vcmk = ref_core_vertex_mask?malloc(nc_dim_len * nv_dim_len * sizeof(*vcmk)):NULL;
229 int * egid = ref_edge_global_ids?malloc(nc_dim_len * nv_dim_len * sizeof(*egid)):NULL;
230 int * ecmk = ref_core_edge_mask?malloc(nc_dim_len * nv_dim_len * sizeof(*ecmk)):NULL;
231
232 YAC_HANDLE_ERROR(nc_get_var_double(ncid, cla_var_id, cla));
233 YAC_HANDLE_ERROR(nc_get_var_double(ncid, clo_var_id, clo));
234 if (ref_lat)
235 YAC_HANDLE_ERROR(nc_get_var_double(ncid, lat_var_id, lat));
236 if (ref_lon)
237 YAC_HANDLE_ERROR(nc_get_var_double(ncid, lon_var_id, lon));
238 if (ref_cell_global_ids)
239 YAC_HANDLE_ERROR(nc_get_var_int(ncid, gid_var_id, gid));
240 if (ref_core_cell_mask)
241 YAC_HANDLE_ERROR(nc_get_var_int(ncid, cmk_var_id, cmk));
242 if (ref_vertex_global_ids)
243 YAC_HANDLE_ERROR(nc_get_var_int(ncid, vgid_var_id, vgid));
244 if (ref_core_vertex_mask)
245 YAC_HANDLE_ERROR(nc_get_var_int(ncid, vcmk_var_id, vcmk));
246 if (ref_edge_global_ids)
247 YAC_HANDLE_ERROR(nc_get_var_int(ncid, egid_var_id, egid));
248 if (ref_core_edge_mask)
249 YAC_HANDLE_ERROR(nc_get_var_int(ncid, ecmk_var_id, ecmk));
250
251 int * ref_cell_flag = calloc(ref_num_cells, sizeof(*ref_cell_flag));
252
253 // for all cells in the grid file
254 for (size_t i = 0; i < nc_dim_len; ++i) {
255
256 // get the coordinates of the current cell
257 double * curr_cla = cla + i * nv_dim_len;
258 double * curr_clo = clo + i * nv_dim_len;
259
260 // match the current cell with the reference cells
261 size_t match_idx = SIZE_MAX;
262 {
263 if (ref_cell_global_ids) {
264 for (size_t j = 0; (j < ref_num_cells) && (match_idx == SIZE_MAX); ++j)
265 if (gid[i] == ref_cell_global_ids[j])
266 match_idx = j;
267 } else if (ref_lat && ref_lon) {
268 for (size_t j = 0; (j < ref_num_cells) && (match_idx == SIZE_MAX); ++j)
269 if ((fabs(lat[i] - ref_lat[j]) < 1e-3) &&
270 (fabs(lon[i] - ref_lon[j]) < 1e-3))
271 match_idx = j;
272 } else {
273 for (size_t j = 0; (j < ref_num_cells) && (match_idx == SIZE_MAX); ++j)
275 curr_cla, ref_cla + j * ref_num_corners_per_cell,
276 ref_num_corners_per_cell) &&
278 curr_clo, ref_clo + j * ref_num_corners_per_cell,
279 ref_num_corners_per_cell))
280 match_idx = j;
281 }
282 }
283
284 if (match_idx == SIZE_MAX) {
285
286 PUT_ERR("error no matching cell");
287
288 } else {
289
290 ref_cell_flag[match_idx] = 1;
291
293 curr_cla, ref_cla + match_idx * ref_num_corners_per_cell,
294 ref_num_corners_per_cell))
295 PUT_ERR("wrong cla");
297 curr_clo, ref_clo + match_idx * ref_num_corners_per_cell,
298 ref_num_corners_per_cell))
299 PUT_ERR("wrong clo");
300 if (ref_lat)
301 if (fabs(lat[i] - ref_lat[match_idx]) > 1e-3) PUT_ERR("wrong lat");
302 if (ref_lon)
303 if (fabs(lon[i] - ref_lon[match_idx]) > 1e-3) PUT_ERR("wrong lon");
304 if (ref_cell_global_ids)
305 if (gid[i] != (int)(ref_cell_global_ids[match_idx])) PUT_ERR("wrong gid");
306 if (ref_core_cell_mask)
307 if (cmk[i] != (ref_core_cell_mask[match_idx]))
308 PUT_ERR("wrong cmk");
309 if (ref_vertex_global_ids)
310 if (compare_ints(
311 vgid + i * nv_dim_len,
312 ref_vertex_global_ids + match_idx * ref_num_corners_per_cell,
313 ref_num_corners_per_cell))
314 PUT_ERR("wrong vgid");
315 if (ref_core_vertex_mask)
316 if (compare_ints(
317 vcmk + i * nv_dim_len,
318 ref_core_vertex_mask + match_idx * ref_num_corners_per_cell,
319 ref_num_corners_per_cell))
320 PUT_ERR("wrong vcmk");
321 if (ref_edge_global_ids)
322 if (compare_ints(
323 egid + i * nv_dim_len,
324 ref_edge_global_ids + match_idx * ref_num_corners_per_cell,
325 ref_num_corners_per_cell))
326 PUT_ERR("wrong egid");
327 if (ref_core_edge_mask)
328 if (compare_ints(
329 ecmk + i * nv_dim_len,
330 ref_core_edge_mask + match_idx * ref_num_corners_per_cell,
331 ref_num_corners_per_cell))
332 PUT_ERR("wrong ecmk");
333 }
334 }
335
336 size_t match_count = 0;
337 for (size_t i = 0; i < ref_num_cells; ++i)
338 if (ref_cell_flag[i]) match_count++;
339
340 if (match_count != ref_num_cells) PUT_ERR("missing cells");
341
342 free(ref_cell_flag);
343 free(ecmk);
344 free(egid);
345 free(vcmk);
346 free(vgid);
347 free(cmk);
348 free(gid);
349 free(lat);
350 free(lon);
351 free(clo);
352 free(cla);
353
354 YAC_HANDLE_ERROR(nc_close(ncid));
355}
356
358 char const * grid_filename, size_t num_lon, size_t num_lat,
359 double lon_range[2], double lat_range[2]) {
360
361 size_t num_nodes = num_lon * num_lat;
362 size_t num_elem = (num_lon - 1) * (num_lat - 1);
363
364 { // grid file
365
366 int ncid;
367
368 // create file
369 yac_nc_create(grid_filename, NC_CLOBBER, &ncid);
370
371 int coord_dim_id;
372 int node_dim_id;
373 int elem_dim_id;
374 int num_el_blk_dim_id;
375 int elem_blk1_dim_id;
376 int node_per_el1_dim_id;
377
378 // define dimensions
380 nc_def_dim(ncid, "num_dim", 3, &coord_dim_id));
382 nc_def_dim(ncid, "num_nodes", num_nodes, &node_dim_id));
384 nc_def_dim(ncid, "num_elem", num_elem, &elem_dim_id));
386 nc_def_dim(ncid, "num_el_blk", 1, &num_el_blk_dim_id));
388 nc_def_dim(ncid, "num_el_in_blk1", num_elem, &elem_blk1_dim_id));
390 nc_def_dim(ncid, "num_nod_per_el1", 4, &node_per_el1_dim_id));
391
392 int elem_dim_ids[2] = {elem_blk1_dim_id, node_per_el1_dim_id};
393 int coord_dim_ids[2] = {coord_dim_id, node_dim_id};
394
395 int var_elem_id;
396 int var_coord_id;
397
398 // define variable
400 nc_def_var(ncid, "connect1", NC_INT, 2, elem_dim_ids, &var_elem_id));
402 nc_def_var(ncid, "coord", NC_DOUBLE, 2, coord_dim_ids, &var_coord_id));
403
404 // end definition
405 YAC_HANDLE_ERROR(nc_enddef(ncid));
406
407 // write grid data
408 {
409 int elem[num_elem][4];
410
411 for (size_t i = 0, k = 0; i < (num_lat - 1); ++i) {
412 for (size_t j = 0; j < (num_lon - 1); ++j, ++k) {
413 elem[k][0] = (int)((i + 0) * num_lon + (j + 0) + 1);
414 elem[k][1] = (int)((i + 0) * num_lon + (j + 1) + 1);
415 elem[k][2] = (int)((i + 1) * num_lon + (j + 1) + 1);
416 elem[k][3] = (int)((i + 1) * num_lon + (j + 0) + 1);
417 }
418 }
419 YAC_HANDLE_ERROR(nc_put_var_int(ncid, var_elem_id, &elem[0][0]));
420 }
421 {
422 double coords[3][num_nodes];
423
424 for (size_t i = 0, k = 0; i < num_lat; ++i) {
425 double lat =
426 ((double)i * (lat_range[1] - lat_range[0])) / (double)(num_lat - 1) +
427 lat_range[0];
428 for (size_t j = 0; j < num_lon; ++j, ++k) {
429 double lon =
430 ((double)j * (lon_range[1] - lon_range[0])) / (double)(num_lon - 1) +
431 lon_range[0];
432 double temp_coord[3];
433 LLtoXYZ_deg(lon, lat, temp_coord);
434 coords[0][k] = temp_coord[0];
435 coords[1][k] = temp_coord[1];
436 coords[2][k] = temp_coord[2];
437 }
438 }
439 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_coord_id, &coords[0][0]));
440 }
441
442 YAC_HANDLE_ERROR(nc_close(ncid));
443 }
444}
445
447 char * grid_name, char * grid_filename, char * mask_filename,
448 int with_corners, size_t num_lon, size_t num_lat,
449 double lon_range[2], double lat_range[2]) {
450
451 { // grid file
452 int ncid;
453
454 // create file
455 yac_nc_create(grid_filename, NC_CLOBBER, &ncid);
456
457 char crn_dim_name[128];
458 char x_dim_name[128];
459 char y_dim_name[128];
460
461 sprintf(crn_dim_name, "crn_%s", grid_name);
462 sprintf(x_dim_name, "x_%s", grid_name);
463 sprintf(y_dim_name, "y_%s", grid_name);
464
465 int dim_crn_id = -1;
466 int dim_x_id;
467 int dim_y_id;
468
469 // define dimensions
470 if (with_corners)
471 YAC_HANDLE_ERROR(nc_def_dim(ncid, crn_dim_name, 4, &dim_crn_id));
472 YAC_HANDLE_ERROR(nc_def_dim(ncid, x_dim_name, num_lon, &dim_x_id));
473 YAC_HANDLE_ERROR(nc_def_dim(ncid, y_dim_name, num_lat, &dim_y_id));
474
475 char cla_var_name[128];
476 char clo_var_name[128];
477 char lat_var_name[128];
478 char lon_var_name[128];
479
480 sprintf(cla_var_name, "%s.cla", grid_name);
481 sprintf(clo_var_name, "%s.clo", grid_name);
482 sprintf(lat_var_name, "%s.lat", grid_name);
483 sprintf(lon_var_name, "%s.lon", grid_name);
484
485 int corner_dim_ids[3] = {dim_crn_id, dim_y_id, dim_x_id};
486 int cell_dim_ids[2] = {dim_y_id, dim_x_id};
487
488 int var_cla_id = -1;
489 int var_clo_id = -1;
490 int var_lat_id;
491 int var_lon_id;
492
493 char degree[] = "degree";
494 char title[] = "This is a reg lon-lat dummy grid";
495
496 // define variable
497 if (with_corners) {
499 nc_def_var(
500 ncid, cla_var_name, NC_DOUBLE, 3, corner_dim_ids, &var_cla_id));
502 nc_put_att_text(ncid, var_cla_id, "units", strlen(degree), degree));
504 nc_put_att_text(ncid, var_cla_id, "title", strlen(title), title));
505
507 nc_def_var(
508 ncid, clo_var_name, NC_DOUBLE, 3, corner_dim_ids, &var_clo_id));
510 nc_put_att_text(ncid, var_clo_id, "units", strlen(degree), degree));
512 nc_put_att_text(ncid, var_clo_id, "title", strlen(title), title));
513 }
514
516 nc_def_var(
517 ncid, lat_var_name, NC_DOUBLE, 2, cell_dim_ids, &var_lat_id));
519 nc_put_att_text(ncid, var_lat_id, "units", strlen(degree), degree));
521 nc_put_att_text(ncid, var_lat_id, "title", strlen(title), title));
522
524 nc_def_var(
525 ncid, lon_var_name, NC_DOUBLE, 2, cell_dim_ids, &var_lon_id));
527 nc_put_att_text(ncid, var_lon_id, "units", strlen(degree), degree));
529 nc_put_att_text(ncid, var_lon_id, "title", strlen(title), title));
530
531
532 // end definition
533 YAC_HANDLE_ERROR(nc_enddef(ncid));
534
535 // write grid data
536
537 double cla[4][num_lat][num_lon];
538 double clo[4][num_lat][num_lon];
539 double lat[num_lat][num_lon];
540 double lon[num_lat][num_lon];
541
542 for (size_t i = 0; i < num_lon; ++i) {
543 double vertex_lon[2] =
544 {((lon_range[1] - lon_range[0]) * (double)i)/(double)num_lon,
545 ((lon_range[1] - lon_range[0]) * (double)(i+1))/(double)num_lon};
546 for (size_t j = 0; j < num_lat; ++j) {
547 double vertex_lat[2] =
548 {((lat_range[1] - lat_range[0]) * (double)j)/(double)num_lat,
549 ((lat_range[1] - lat_range[0]) * (double)(j+1))/(double)num_lat};
550 cla[0][j][i] = lat_range[0] + vertex_lat[0];
551 cla[1][j][i] = lat_range[0] + vertex_lat[0];
552 cla[2][j][i] = lat_range[0] + vertex_lat[1];
553 cla[3][j][i] = lat_range[0] + vertex_lat[1];
554 clo[0][j][i] = lon_range[0] + vertex_lon[0];
555 clo[1][j][i] = lon_range[0] + vertex_lon[1];
556 clo[2][j][i] = lon_range[0] + vertex_lon[1];
557 clo[3][j][i] = lon_range[0] + vertex_lon[0];
558 lat[j][i] = lat_range[0] + (vertex_lat[0] + vertex_lat[1]) * 0.5;
559 lon[j][i] = lon_range[0] + (vertex_lon[0] + vertex_lon[1]) * 0.5;
560 }
561 }
562
563
564 if (with_corners) {
565 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_cla_id, &cla[0][0][0]));
566 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_clo_id, &clo[0][0][0]));
567 }
568 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_lat_id, &lat[0][0]));
569 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_lon_id, &lon[0][0]));
570
571 YAC_HANDLE_ERROR(nc_close(ncid));
572 }
573
574 { // mask file
575 int ncid;
576
577 // create file
578 yac_nc_create(mask_filename, NC_CLOBBER, &ncid);
579
580 char x_dim_name[128];
581 char y_dim_name[128];
582
583 sprintf(x_dim_name, "x_%s", grid_name);
584 sprintf(y_dim_name, "y_%s", grid_name);
585
586 int dim_x_id;
587 int dim_y_id;
588
589 // define dimensions
590 YAC_HANDLE_ERROR(nc_def_dim(ncid, x_dim_name, num_lon, &dim_x_id));
591 YAC_HANDLE_ERROR(nc_def_dim(ncid, y_dim_name, num_lat, &dim_y_id));
592
593 char frc_var_name[128];
594 char msk_var_name[128];
595
596 sprintf(frc_var_name, "%s.frc", grid_name);
597 sprintf(msk_var_name, "%s.msk", grid_name);
598
599 int dim_ids[2] = {dim_y_id, dim_x_id};
600
601 int var_frc_id;
602 int var_msk_id;
603
604 char adim[] = "adim";
605
606 // define variable
608 nc_def_var(
609 ncid, frc_var_name, NC_DOUBLE, 2, dim_ids, &var_frc_id));
611 nc_put_att_text(ncid, var_frc_id, "units", strlen(adim), adim));
612
614 nc_def_var(
615 ncid, msk_var_name, NC_INT, 2, dim_ids, &var_msk_id));
617 nc_put_att_text(ncid, var_msk_id, "units", strlen(adim), adim));
618
619
620 // end definition
621 YAC_HANDLE_ERROR(nc_enddef(ncid));
622
623 // write grid data
624
625 double frc[num_lat][num_lon];
626 int msk[num_lat][num_lon];
627
628 for (size_t i = 0; i < num_lon; ++i) {
629 for (size_t j = 0; j < num_lat; ++j) {
630 frc[j][i] = 1;
631 msk[j][i] = 0;
632 }
633 }
634
635 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_frc_id, &frc[0][0]));
636 YAC_HANDLE_ERROR(nc_put_var_int(ncid, var_msk_id, &msk[0][0]));
637
638 YAC_HANDLE_ERROR(nc_close(ncid));
639 }
640}
static void LLtoXYZ_deg(double lon, double lat, double p_out[])
Definition geometry.h:269
static int compare_ints(int *a, int *b, int count)
void write_dummy_exodus_grid_file(char const *grid_filename, size_t num_lon, size_t num_lat, double lon_range[2], double lat_range[2])
static int compare_cell_coords(double *a, double *b, int count)
void write_dummy_scrip_grid_file(char *grid_name, char *grid_filename, char *mask_filename, int with_corners, size_t num_lon, size_t num_lat, double lon_range[2], double lat_range[2])
void check_grid_file(char const *filename, char const *grid_name, size_t ref_num_cells, size_t ref_num_corners_per_cell, double *ref_cla, double *ref_clo, double *ref_lat, double *ref_lon, int *ref_cell_global_ids, int *ref_core_cell_mask, int *ref_vertex_global_ids, int *ref_core_vertex_mask, int *ref_edge_global_ids, int *ref_core_edge_mask)
enum callback_type type
void yac_nc_create(const char *path, int cmode, int *ncidp)
Definition io_utils.c:367
void yac_nc_inq_varid(int ncid, char const *name, int *varidp)
Definition io_utils.c:411
void yac_nc_open(const char *path, int omode, int *ncidp)
Definition io_utils.c:350
void yac_nc_inq_dimid(int ncid, char const *name, int *dimidp)
Definition io_utils.c:385
int yac_file_exists(const char *filename)
Definition utils_core.c:12
#define PUT_ERR(string)
Definition tests.h:10
#define YAC_HANDLE_ERROR(exp)
Definition toy_output.c:13