46 grids_file_name, atmo_names, ATMO_COUNT, atmo_sizes_2d);
54 ocean_names[i], atmo_names, ATMO_COUNT, atmo_sizes_2d, threshold,
85 char const * grids_file_name,
86 char const *
grid_names[],
size_t count,
size_t (*grid_sizes)[2]) {
91 for (
size_t i = 0; i < count; ++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];
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);
117 char const * ocean_grid_name,
char const **atmos_grid_names,
118 size_t atmo_count,
size_t (*atmo_grid_sizes)[2],
double threshold,
121 char mask_file_name[128];
123 mask_file_name,
sizeof(mask_file_name),
"./output/masks_%s_yac.nc",
127 int dim_ids[atmo_count][2];
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];
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);
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])));
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];
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);
159 ncid, frac_var_name, NC_DOUBLE, 2, dim_ids[i], &(var_ids[i][0])));
162 ncid, var_ids[i][0],
"coherent_with_grid",
163 strlen(ocean_grid_name), ocean_grid_name));
166 ncid, mask_var_name, NC_INT, 2, dim_ids[i], &(var_ids[i][1])));
169 ncid, var_ids[i][1],
"coherent_with_grid",
170 strlen(ocean_grid_name), ocean_grid_name));
173 ncid, var_ids[i][1],
"threshold", NC_DOUBLE, 1, &threshold));
177 char str_description[] =
"Created by YAC";
178 char str_remapper[] =
"YAC";
181 ncid, NC_GLOBAL,
"description",
182 strlen(str_description), str_description));
185 ncid, NC_GLOBAL,
"remapper", strlen(str_remapper), str_remapper));
193 char const * path,
char const * ocean_grid_name,
194 char const * atmo_grid_name,
size_t atmo_size,
double const threshold) {
198 char field_name[1024];
199 snprintf(field_name, 1024,
"test_one_%s_CONSERV_DESTAREA", ocean_grid_name);
201 double * frac = malloc(atmo_size *
sizeof(*frac));
203 FILE * vtk_file = fopen(vtk_file_name,
"r");
205 if (vtk_file == NULL) {
206 for (
size_t i = 0; i < atmo_size; ++i) frac[i] = 1.0;
209 char line_buffer[1024];
213 while(((line = fgets(line_buffer, 1024, vtk_file))) != NULL)
214 if (strstr(line,
"CELLS") != NULL)
break;
217 fprintf(stderr,
"ERROR could not find CELLS declaration in file %s\n",
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",
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);
237 while(((line = fgets(line_buffer, 1024, vtk_file))) != NULL)
238 if (strstr(line, field_name) != NULL)
break;
240 fprintf(stderr,
"ERROR could not find field \"%s\" in file %s\n",
241 field_name, vtk_file_name);
245 line = fgets(line_buffer, 1024, vtk_file);
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);
253 if (frac[i] < threshold) frac[i] = 0.0;
254 if (frac[i] > 1.0) frac[i] = 1.0;
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])
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.