YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
generate_OASIS_mask_file.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 <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include <netcdf.h>
9
10#include "yac_core.h"
11
12static void get_grid_dimensions(
13 char const * grids_file_name,
14 char const * grid_names[], size_t count, size_t (*grid_sizes)[2]);
15static int create_mask_file(
16 char const * ocean_grid_name, char const **atmos_grid_names,
17 size_t atmo_count, size_t (*atmo_grid_sizes)[2], double threshold,
18 int (*var_ids)[2]);
19static double * read_interp_field(
20 char const * path, char const * ocean_grid_name,
21 char const * atmo_grid_name, size_t atmo_size, double const threshold);
22static void write_mask(
23 int ncid, int var_ids[2], double * frac, int * mask);
24
25int main(void) {
26
27 char const vtk_path[] = "./output";
28 char const grids_file_name[] = "./input/grids.nc";
29
30 char const * ocean_names[] = {
31 "nogt", "torc"
32 };
33 char const * atmo_names[] = {
34 "bggd", "icos", "sse7", "icoh"
35 };
36 enum {
37 OCEAN_COUNT = sizeof(ocean_names) / sizeof(ocean_names[0]),
38 ATMO_COUNT = sizeof(atmo_names) / sizeof(atmo_names[0])
39 };
40
41 double const threshold = 0.001;
42
43 // get dimensions from atmosphere grid file
44 size_t atmo_sizes_2d[ATMO_COUNT][2];
46 grids_file_name, atmo_names, ATMO_COUNT, atmo_sizes_2d);
47
48 for (size_t i = 0; i < OCEAN_COUNT; ++i) {
49
50 // create mask file
51 int var_ids[ATMO_COUNT][2];
52 int mask_file_ncid =
54 ocean_names[i], atmo_names, ATMO_COUNT, atmo_sizes_2d, threshold,
55 var_ids);
56
57 for (size_t j = 0; j < ATMO_COUNT; ++j) {
58
59 size_t atmo_size = atmo_sizes_2d[j][0] * atmo_sizes_2d[j][1];
60
61 // read interpolated field
62 double * frac =
63 read_interp_field(vtk_path, ocean_names[i], atmo_names[j], atmo_size, threshold);
64
65 // generate integer mask
66 int * mask = malloc(atmo_size * sizeof(*mask));
67 for (size_t k = 0; k < atmo_size; ++k)
68 mask[k] = frac[k] > 0.0 ? 0 : 1;
69
70 // write masks
71 write_mask(mask_file_ncid, var_ids[j], frac, mask);
72
73 free(mask);
74 free(frac);
75 }
76
77 // close mask file
78 nc_close(mask_file_ncid);
79 }
80
81 return EXIT_SUCCESS;
82}
83
85 char const * grids_file_name,
86 char const * grid_names[], size_t count, size_t (*grid_sizes)[2]) {
87
88 int ncid;
89 yac_nc_open(grids_file_name, NC_NOWRITE, &ncid);
90
91 for (size_t i = 0; i < count; ++i) {
92
93 char const * grid_name = grid_names[i];
94 size_t * grid_size = grid_sizes[i];
95 size_t grid_name_len = strlen(grid_name) + 1;
96 char x_dim_name[2 + grid_name_len];
97 char y_dim_name[2 + grid_name_len];
98
99 snprintf(x_dim_name, 2 + grid_name_len, "x_%s", grid_name);
100 snprintf(y_dim_name, 2 + grid_name_len, "y_%s", grid_name);
101
102 // get dimension ids
103 int x_dim_id;
104 int y_dim_id;
105 yac_nc_inq_dimid(ncid, x_dim_name, &x_dim_id);
106 yac_nc_inq_dimid(ncid, y_dim_name, &y_dim_id);
107
108 // get dimension length
109 YAC_HANDLE_ERROR(nc_inq_dimlen(ncid, x_dim_id, grid_size + 0));
110 YAC_HANDLE_ERROR(nc_inq_dimlen(ncid, y_dim_id, grid_size + 1));
111 }
112
113 YAC_HANDLE_ERROR(nc_close(ncid));
114}
115
117 char const * ocean_grid_name, char const **atmos_grid_names,
118 size_t atmo_count, size_t (*atmo_grid_sizes)[2], double threshold,
119 int (*var_ids)[2]) {
120
121 char mask_file_name[128];
122 snprintf(
123 mask_file_name, sizeof(mask_file_name), "./output/masks_%s_yac.nc",
124 ocean_grid_name);
125
126 int ncid;
127 int dim_ids[atmo_count][2];
128
129 YAC_HANDLE_ERROR(nc_create(mask_file_name, NC_CLOBBER, &ncid));
130
131 // write dimensions
132 for (size_t i = 0; i < atmo_count; ++i) {
133 char const * grid_name = atmos_grid_names[i];
134 size_t grid_name_len = strlen(grid_name) + 1;
135 char y_dim_name[2 + grid_name_len];
136 char x_dim_name[2 + grid_name_len];
137
138 snprintf(y_dim_name, 2 + grid_name_len, "y_%s", grid_name);
139 snprintf(x_dim_name, 2 + grid_name_len, "x_%s", grid_name);
140
142 nc_def_dim(ncid, y_dim_name, atmo_grid_sizes[i][1], &(dim_ids[i][0])));
144 nc_def_dim(ncid, x_dim_name, atmo_grid_sizes[i][0], &(dim_ids[i][1])));
145 }
146
147 // define variables
148 for (size_t i = 0; i < atmo_count; ++i) {
149 char const * grid_name = atmos_grid_names[i];
150 size_t grid_name_len = strlen(grid_name) + 1;
151 char frac_var_name[5 + grid_name_len];
152 char mask_var_name[5 + grid_name_len];
153
154 snprintf(frac_var_name, 5 + grid_name_len, "%s.frc", grid_name);
155 snprintf(mask_var_name, 5 + grid_name_len, "%s.msk", grid_name);
156
158 nc_def_var(
159 ncid, frac_var_name, NC_DOUBLE, 2, dim_ids[i], &(var_ids[i][0])));
161 nc_put_att_text(
162 ncid, var_ids[i][0], "coherent_with_grid",
163 strlen(ocean_grid_name), ocean_grid_name));
165 nc_def_var(
166 ncid, mask_var_name, NC_INT, 2, dim_ids[i], &(var_ids[i][1])));
168 nc_put_att_text(
169 ncid, var_ids[i][1], "coherent_with_grid",
170 strlen(ocean_grid_name), ocean_grid_name));
172 nc_put_att_double(
173 ncid, var_ids[i][1], "threshold", NC_DOUBLE, 1, &threshold));
174 }
175
176 // write global attributes
177 char str_description[] = "Created by YAC";
178 char str_remapper[] = "YAC";
180 nc_put_att_text(
181 ncid, NC_GLOBAL, "description",
182 strlen(str_description), str_description));
184 nc_put_att_text(
185 ncid, NC_GLOBAL, "remapper", strlen(str_remapper), str_remapper));
186
187 YAC_HANDLE_ERROR(nc_enddef(ncid));
188
189 return ncid;
190}
191
192static double * read_interp_field(
193 char const * path, char const * ocean_grid_name,
194 char const * atmo_grid_name, size_t atmo_size, double const threshold) {
195
196 char * vtk_file_name = yac_file_path_join(path, atmo_grid_name, ".vtk");
197
198 char field_name[1024];
199 snprintf(field_name, 1024, "test_one_%s_CONSERV_DESTAREA", ocean_grid_name);
200
201 double * frac = malloc(atmo_size * sizeof(*frac));
202
203 FILE * vtk_file = fopen(vtk_file_name, "r");
204
205 if (vtk_file == NULL) {
206 for (size_t i = 0; i < atmo_size; ++i) frac[i] = 1.0;
207 return frac;
208 }
209 char line_buffer[1024];
210 char * line;
211
212 // search for cell declaration
213 while(((line = fgets(line_buffer, 1024, vtk_file))) != NULL)
214 if (strstr(line, "CELLS") != NULL) break;
215
216 if (line == NULL) {
217 fprintf(stderr, "ERROR could not find CELLS declaration in file %s\n",
218 vtk_file_name);
219 exit(EXIT_FAILURE);
220 }
221
222 unsigned num_cells, dummy;
223 if (sscanf(line, "CELLS %u %u\n", &num_cells, &dummy) != 2) {
224 fprintf(stderr, "ERROR could not read grid size from file %s\n",
225 vtk_file_name);
226 exit(EXIT_FAILURE);
227 }
228
229 if ((size_t)num_cells != atmo_size) {
230 fprintf(stderr, "ERROR grid dimension missmatch in file %s\n"
231 "value in file: %u\n"
232 "expected value: %zu\n", vtk_file_name, num_cells, atmo_size);
233 exit(EXIT_FAILURE);
234 }
235
236 // search for field declaration
237 while(((line = fgets(line_buffer, 1024, vtk_file))) != NULL)
238 if (strstr(line, field_name) != NULL) break;
239 if (line == NULL) {
240 fprintf(stderr, "ERROR could not find field \"%s\" in file %s\n",
241 field_name, vtk_file_name);
242 exit(EXIT_FAILURE);
243 }
244 // skip one line
245 line = fgets(line_buffer, 1024, vtk_file);
246
247 for (size_t i = 0; i < atmo_size; ++i) {
248 if (fscanf(vtk_file, "%lf\n", frac + i) != 1) {
249 fprintf(stderr, "ERROR while reading field %s from file %s\n",
250 field_name, vtk_file_name);
251 exit(EXIT_FAILURE);
252 }
253 if (frac[i] < threshold) frac[i] = 0.0;
254 if (frac[i] > 1.0) frac[i] = 1.0;
255 }
256
257 fclose(vtk_file);
258 free(vtk_file_name);
259
260 return frac;
261}
262
263
264static void write_mask(
265 int ncid, int var_ids[2], double * frac, int * mask) {
266
267 YAC_HANDLE_ERROR(nc_put_var_double(ncid, var_ids[0], frac));
268 YAC_HANDLE_ERROR(nc_put_var_int(ncid, var_ids[1], mask));
269}
char const * grid_names[]
static double * read_interp_field(char const *path, char const *ocean_grid_name, char const *atmo_grid_name, size_t atmo_size, double const threshold)
static int create_mask_file(char const *ocean_grid_name, char const **atmos_grid_names, size_t atmo_count, size_t(*atmo_grid_sizes)[2], double threshold, int(*var_ids)[2])
static void write_mask(int ncid, int var_ids[2], double *frac, int *mask)
static void get_grid_dimensions(char const *grids_file_name, char const *grid_names[], size_t count, size_t(*grid_sizes)[2])
char * yac_file_path_join(char const *path_prefix, char const *path_suffix, char const *extension)
Join two path components with a '/' and optional extension.
Definition io_utils.c:426
void yac_nc_open(const char *path, int omode, int *ncidp)
Definition io_utils.c:311
void yac_nc_inq_dimid(int ncid, char const *name, int *dimidp)
Definition io_utils.c:344
size_t num_cells[2]
static int mask[16]
#define YAC_HANDLE_ERROR(exp)
Definition toy_output.c:13