YAC 3.14.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 "ERROR(yac_read_woa_dimensions): "
91 "unsupported datatype for variable \"%s\" "
92 "(has to be either NC_DOUBLE or NC_FLOAT)", fieldName)
93
94 // get number of dimensions
95 int varNdims;
96 YAC_HANDLE_ERROR(nc_inq_varndims(fileId, varId, &varNdims));
98 (varNdims > 0) && (varNdims <= 4),
99 "ERROR(read_woa_dimensions): invalid number of dimensions for "
100 "variable \"%s\" (has to be between 1 and 4)", fieldName)
101
102 // get dimensions of variable
103 int varDimids[NC_MAX_VAR_DIMS]; // dimension NetCDF IDs
104 YAC_HANDLE_ERROR(nc_inq_vardimid(fileId, varId, varDimids));
105
106 // check dimensions
107 for (int i = 0; i < varNdims; ++i) {
108
109 // get details of current dimension
110 char dim_name[NC_MAX_NAME + 1];
111 size_t dim_len;
112 YAC_HANDLE_ERROR(nc_inq_dim(fileId, varDimids[i], dim_name, &dim_len));
113
114 size_t * count = NULL;
115 int * idx = NULL;
116
117 if (!strcmp(dim_name, "time")) {
118 count = &(fieldInfo->nbrTimeSteps);
119 idx = &(fieldInfo->timeDimIdx);
120 } else if (!strcmp(dim_name, "depth")) {
121 count = &(fieldInfo->nbrLevels);
122 idx = &(fieldInfo->levelDimIdx);
123 } else if (!strcmp(dim_name, "lon")) {
124 count = &(fieldInfo->nbrLonPoints);
125 idx = &(fieldInfo->lonDimIdx);
126 } else if (!strcmp(dim_name, "lat")) {
127 count = &(fieldInfo->nbrLatPoints);
128 idx = &(fieldInfo->latDimIdx);
129 }
130
132 (count != NULL) && (idx != NULL),
133 "ERROR(read_woa_dimensions): "
134 "supported dimension \"%s\" of variable \"%s\"", dim_name, fieldName)
135
136 *count = dim_len;
137 *idx = i;
138 }
139#endif
140}
141
142/* ------------------------------------------------ */
143
144double * yac_get_woa_memory(struct yac_fieldMetadata fieldInfo) {
145
146 return
147 xmalloc(fieldInfo.nbrLatPoints * fieldInfo.nbrLonPoints * sizeof(double));
148}
149
150/* ------------------------------------------------ */
151
152void yac_free_woa_memory(double * data) {
153 free(data);
154}
155
156/* ------------------------------------------------ */
157
159 int fileId, double * cell_data, struct yac_fieldMetadata fieldInfo,
160 int timeStep, int level) {
161
162#ifndef YAC_NETCDF_ENABLED
163
164 UNUSED(fileId);
165 UNUSED(cell_data);
166 UNUSED(fieldInfo);
167 UNUSED(timeStep);
168 UNUSED(level);
169 die(
170 "ERROR(yac_read_woa_timestep_level): "
171 "YAC is built without the NetCDF support");
172#else
173
174 size_t start[4] = {0,0,0,0};
175 size_t count[4] = {1,1,1,1};
176
177 /* ... extract one level */
178 if ( fieldInfo.levelDimIdx > -1 ) {
180 ((size_t)level > 0) && ((size_t)level <= fieldInfo.nbrLevels),
181 "ERROR(yac_read_woa_timestep_level): "
182 "invalid level (has to be between 1 and %d)",
183 (int)(fieldInfo.nbrLevels));
184 start[fieldInfo.levelDimIdx] = level-1;
185 count[fieldInfo.levelDimIdx] = 1;
186 }
187
188 /* ... extract one timestep */
189 if ( fieldInfo.timeDimIdx > -1 ) {
191 ((size_t)timeStep > 0) && ((size_t)timeStep <= fieldInfo.nbrTimeSteps),
192 "ERROR(yac_read_woa_timestep_level): "
193 "invalid time step (has to be between 1 and %d)",
194 (int)(fieldInfo.nbrTimeSteps));
195 start[fieldInfo.timeDimIdx] = timeStep-1;
196 count[fieldInfo.timeDimIdx] = 1;
197 }
198
199 /* ... extract one horizontal data set */
200 if ( fieldInfo.latDimIdx > -1 ) {
201 count[fieldInfo.latDimIdx] = fieldInfo.nbrLatPoints;
202 }
203 if ( fieldInfo.lonDimIdx > -1 ) {
204 count[fieldInfo.lonDimIdx] = fieldInfo.nbrLonPoints;
205 }
206
207 /* ... get data from netcdf file */
209 nc_get_vara_double(fileId, fieldInfo.varId, start, count, cell_data));
210#endif
211}
#define UNUSED(x)
Definition core.h:72
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
const char * fieldName[]
#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
#define YAC_HANDLE_ERROR(exp)
Definition toy_output.c:13
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:30
#define die(msg)
Definition yac_assert.h:12