YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_read_woa_data.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 <unistd.h>
8#include <math.h>
9
10#include <netcdf.h>
11
12#include "tests.h"
13#include "test_common.h"
14#include "read_woa_data.h"
15#include "io_utils.h"
16
22#define LON (16)
23#define LAT (8)
24#define DEPTH (4)
25#define TIME (2)
26#define NV (2)
27
28static void write_test_data_file(char const * file_name);
29
30int main(void) {
31
32 char const * test_data_file_name = "test_read_woa_data.nc";
33
34 write_test_data_file(test_data_file_name);
35
36 {
37 // open woa data file
38 char * woa_field_name = "s_an";
39 int woa_file = yac_open_woa_output(test_data_file_name);
40
41 struct yac_fieldMetadata field_info;
42 yac_read_woa_dimensions(woa_file, woa_field_name, &field_info);
43
44 // check meta data
45 if (field_info.nbrTimeSteps != TIME)
46 PUT_ERR("ERROR in meta data (nbrTimeSteps)");
47 if (field_info.nbrLevels != DEPTH)
48 PUT_ERR("ERROR in meta data (nbrLevels)");
49 if (field_info.nbrLatPoints != LAT)
50 PUT_ERR("ERROR in meta data (nbrLatPoints)");
51 if (field_info.nbrLonPoints != LON)
52 PUT_ERR("ERROR in meta data (nbrLonPoints)");
53
54 double * global_salinity = yac_get_woa_memory(field_info);
55
56 for (int time = 0; time < TIME; ++time) {
57 for (int depth = 0; depth < DEPTH; ++depth) {
58
60 woa_file, global_salinity, field_info, time+1, depth+1);
61
62 // check retrieved field data
63 for (int i = 0; i < LON * LAT; ++i)
64 if (fabs(global_salinity[i] - (double)(time * 100 + depth)) > 1e-6)
65 PUT_ERR("ERROR in field data");
66 }
67 }
68
69 yac_free_woa_memory(global_salinity);
70
71 yac_close_woa_output(woa_file);
72 }
73
74 unlink(test_data_file_name);
75
76 return TEST_EXIT_CODE;
77}
78
79static void write_test_data_file(char const * file_name) {
80
81 int ncid;
82
83 // create file
84 yac_nc_create(file_name, NC_CLOBBER, &ncid);
85
86 int dim_lon_id, dim_lat_id, dim_depth_id, dim_time_id, dim_nv_id;
87
88 // define dimensions
89 YAC_HANDLE_ERROR(nc_def_dim(ncid, "lon", LON, &dim_lon_id));
90 YAC_HANDLE_ERROR(nc_def_dim(ncid, "lat", LAT, &dim_lat_id));
91 YAC_HANDLE_ERROR(nc_def_dim(ncid, "depth", DEPTH, &dim_depth_id));
92 YAC_HANDLE_ERROR(nc_def_dim(ncid, "time", TIME, &dim_time_id));
93 YAC_HANDLE_ERROR(nc_def_dim(ncid, "nv", NV, &dim_nv_id));
94
95 int var_s_an_id;
96
97 // define variables
99 nc_def_var(
100 ncid, "s_an", NC_FLOAT, 4,
101 (int[]){dim_time_id,dim_depth_id,dim_lat_id,dim_lon_id},
102 &var_s_an_id));
103
104 // end definition
105 YAC_HANDLE_ERROR(nc_enddef(ncid));
106
107 // write dummy data
108 float dummy_data[TIME][DEPTH][LAT][LON];
109 for (int time = 0; time < TIME; ++time)
110 for (int depth = 0; depth < DEPTH; ++depth)
111 for (int lat = 0; lat < LAT; ++lat)
112 for (int lon = 0; lon < LON; ++lon)
113 dummy_data[time][depth][lat][lon] = (float)(time * 100 + depth);
114 YAC_HANDLE_ERROR(nc_put_var_float(ncid, var_s_an_id, &dummy_data[0][0][0][0]));
115
116 // close file
117 YAC_HANDLE_ERROR(nc_close(ncid));
118}
char const * file_name
void yac_nc_create(const char *path, int cmode, int *ncidp)
Definition io_utils.c:367
void yac_read_woa_timestep_level(int fileId, double *cell_data, struct yac_fieldMetadata fieldInfo, int timeStep, int level)
void yac_close_woa_output(int ncid)
int yac_open_woa_output(char const *input_file)
void yac_read_woa_dimensions(int fileId, char const *fieldName, struct yac_fieldMetadata *fieldInfo)
double * yac_get_woa_memory(struct yac_fieldMetadata fieldInfo)
void yac_free_woa_memory(double *data)
general routines for reading WOA output NetCDF files
#define TIME
#define LAT
#define LON
#define DEPTH
static void write_test_data_file(char const *file_name)
#define NV
#define TEST_EXIT_CODE
Definition tests.h:14
#define PUT_ERR(string)
Definition tests.h:10
#define YAC_HANDLE_ERROR(exp)
Definition toy_output.c:13