YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
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#ifdef HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9#include <stdio.h>
10#include <string.h>
11
12#ifdef YAC_NETCDF_ENABLED
13#include <netcdf.h>
14#endif
15
16#include "read_woa_data.h"
17#include "utils_common.h"
18#include "io_utils.h"
19
20/* ------------------------------------------------ */
21
22int yac_open_woa_output(char const * input_file) {
23
24#ifndef YAC_NETCDF_ENABLED
25
26 UNUSED(input_file);
27 die(
28 "ERROR(yac_open_woa_output): "
29 "YAC is built without the NetCDF support");
30 return -1;
31#else
32
33 int ncid;
34 yac_nc_open(input_file, NC_NOWRITE, &ncid);
35 return ncid;
36#endif
37}
38
39/* ------------------------------------------------ */
40
41void yac_close_woa_output(int ncid) {
42
43#ifndef YAC_NETCDF_ENABLED
44
45 UNUSED(ncid);
46 die(
47 "ERROR(yac_close_woa_output): "
48 "YAC is built without the NetCDF support");
49#else
50
51 YAC_HANDLE_ERROR(nc_close(ncid));
52#endif
53}
54
55/* ------------------------------------------------ */
56
57void yac_read_woa_dimensions ( int fileId, char const * fieldName,
58 struct yac_fieldMetadata * fieldInfo ) {
59
60#ifndef YAC_NETCDF_ENABLED
61
62 UNUSED(fileId);
64 UNUSED(fieldInfo);
65 die(
66 "ERROR(yac_read_woa_dimensions): "
67 "YAC is built without the NetCDF support");
68#else
69
70 // set defaults
71 fieldInfo->nbrLevels = 1;
72 fieldInfo->nbrTimeSteps = 1;
73 fieldInfo->nbrLatPoints = 1;
74 fieldInfo->nbrLonPoints = 1;
75 fieldInfo->timeDimIdx = -1;
76 fieldInfo->latDimIdx = -1;
77 fieldInfo->lonDimIdx = -1;
78 fieldInfo->levelDimIdx = -1;
79
80 // get id of variable
81 int varId;
82 yac_nc_inq_varid(fileId, fieldName, &varId);
83 fieldInfo->varId = varId;
84
85 // check type of variable
86 nc_type varType;
87 YAC_HANDLE_ERROR(nc_inq_vartype(fileId, varId, &varType));
89 (varType == NC_FLOAT) || (varType == NC_DOUBLE),
90 "unsupported datatype for variable \"%s\" "
91 "(has to be either NC_DOUBLE or NC_FLOAT)", fieldName)
92
93 // get number of dimensions
94 int varNdims;
95 YAC_HANDLE_ERROR(nc_inq_varndims(fileId, varId, &varNdims));
97 (varNdims > 0) && (varNdims <= 4),
98 "invalid number of dimensions for "
99 "variable \"%s\" (has to be between 1 and 4)", fieldName)
100
101 // get dimensions of variable
102 int varDimids[NC_MAX_VAR_DIMS]; // dimension NetCDF IDs
103 YAC_HANDLE_ERROR(nc_inq_vardimid(fileId, varId, varDimids));
104
105 // check dimensions
106 for (int i = 0; i < varNdims; ++i) {
107
108 // get details of current dimension
109 char dim_name[NC_MAX_NAME + 1];
110 size_t dim_len;
111 YAC_HANDLE_ERROR(nc_inq_dim(fileId, varDimids[i], dim_name, &dim_len));
112
113 size_t * count = NULL;
114 int * idx = NULL;
115
116 if (!strcmp(dim_name, "time")) {
117 count = &(fieldInfo->nbrTimeSteps);
118 idx = &(fieldInfo->timeDimIdx);
119 } else if (!strcmp(dim_name, "depth")) {
120 count = &(fieldInfo->nbrLevels);
121 idx = &(fieldInfo->levelDimIdx);
122 } else if (!strcmp(dim_name, "lon")) {
123 count = &(fieldInfo->nbrLonPoints);
124 idx = &(fieldInfo->lonDimIdx);
125 } else if (!strcmp(dim_name, "lat")) {
126 count = &(fieldInfo->nbrLatPoints);
127 idx = &(fieldInfo->latDimIdx);
128 }
129
131 (count != NULL) && (idx != NULL),
132 "supported dimension \"%s\" of variable \"%s\"", dim_name, fieldName)
133
134 *count = dim_len;
135 *idx = i;
136 }
137#endif
138}
139
140/* ------------------------------------------------ */
141
142double * yac_get_woa_memory(struct yac_fieldMetadata fieldInfo) {
143
144 return
145 xmalloc(fieldInfo.nbrLatPoints * fieldInfo.nbrLonPoints * sizeof(double));
146}
147
148/* ------------------------------------------------ */
149
150void yac_free_woa_memory(double * data) {
151 free(data);
152}
153
154/* ------------------------------------------------ */
155
157 int fileId, double * cell_data, struct yac_fieldMetadata fieldInfo,
158 int timeStep, int level) {
159
160#ifndef YAC_NETCDF_ENABLED
161
162 UNUSED(fileId);
163 UNUSED(cell_data);
164 UNUSED(fieldInfo);
165 UNUSED(timeStep);
166 UNUSED(level);
167 die(
168 "ERROR(yac_read_woa_timestep_level): "
169 "YAC is built without the NetCDF support");
170#else
171
172 size_t start[4] = {0,0,0,0};
173 size_t count[4] = {1,1,1,1};
174
175 /* ... extract one level */
176 if ( fieldInfo.levelDimIdx > -1 ) {
178 ((size_t)level > 0) && ((size_t)level <= fieldInfo.nbrLevels),
179 "invalid level (has to be between 1 and %d)",
180 (int)(fieldInfo.nbrLevels));
181 start[fieldInfo.levelDimIdx] = level-1;
182 count[fieldInfo.levelDimIdx] = 1;
183 }
184
185 /* ... extract one timestep */
186 if ( fieldInfo.timeDimIdx > -1 ) {
188 ((size_t)timeStep > 0) && ((size_t)timeStep <= fieldInfo.nbrTimeSteps),
189 "invalid time step (has to be between 1 and %d)",
190 (int)(fieldInfo.nbrTimeSteps));
191 start[fieldInfo.timeDimIdx] = timeStep-1;
192 count[fieldInfo.timeDimIdx] = 1;
193 }
194
195 /* ... extract one horizontal data set */
196 if ( fieldInfo.latDimIdx > -1 ) {
197 count[fieldInfo.latDimIdx] = fieldInfo.nbrLatPoints;
198 }
199 if ( fieldInfo.lonDimIdx > -1 ) {
200 count[fieldInfo.lonDimIdx] = fieldInfo.nbrLonPoints;
201 }
202
203 /* ... get data from netcdf file */
205 nc_get_vara_double(fileId, fieldInfo.varId, start, count, cell_data));
206#endif
207}
#define UNUSED(x)
Definition core.h:72
void yac_nc_inq_varid(int ncid, char const *name, int *varidp)
Definition io_utils.c:369
void yac_nc_open(const char *path, int omode, int *ncidp)
Definition io_utils.c:311
#define xmalloc(size)
Definition ppm_xfuncs.h:66
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
int latDimIdx
dimension index from NetCDF containing the latitude
int lonDimIdx
dimension index from NetCDF containing the longitude
size_t nbrLatPoints
number of latitude cells contained in the NetCDF file
int timeDimIdx
dimension index from NetCDF containing the time
size_t nbrTimeSteps
number of timesteps contained in the NetCDF file
int levelDimIdx
dimension index from NetCDF containing the vertical
size_t nbrLonPoints
number of longitude cells contained in the NetCDF file
size_t nbrLevels
number of vertical levels/layers contained in the NetCDF file
int varId
NetCDF variable ID.
double * data
const char * fieldName[]
Definition toy_common.h:5
#define YAC_HANDLE_ERROR(exp)
Definition toy_output.c:13
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:39
#define die(msg)
Definition yac_assert.h:14