Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Creating and Setting NetCDF Variables #39

Merged
merged 6 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions obs2ioda-v2/src/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(obs2ioda_cxx_SOURCES
netcdf_file.cc
netcdf_group.cc
netcdf_dimension.cc
netcdf_variable.cc
)
set(obs2ioda_cxx_LIBRARIES
NetCDF::NetCDF_CXX
Expand Down
220 changes: 220 additions & 0 deletions obs2ioda-v2/src/cxx/netcdf_variable.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#include "netcdf_variable.h"
#include "netcdf_file.h"
#include "netcdf_error.h"

namespace Obs2Ioda {
int netcdfAddVar(
int netcdfID,
const char *groupName,
const char *varName,
nc_type netcdfDataType,
int numDims,
const char **dimNames
) {
try {
auto file = FileMap::getInstance().getFile(netcdfID);
const auto group = !groupName
? file
: std::make_shared<
netCDF::NcGroup>(
file->getGroup(
groupName));
std::vector<netCDF::NcDim> dims;
dims.reserve(numDims);
for (int i = 0; i < numDims; i++) {
dims.push_back(file->getDim(dimNames[i]));;
}
auto var = group->addVar(
varName,
netCDF::NcType(netcdfDataType),
dims
);
return 0;
} catch (netCDF::exceptions::NcException &e) {
return netcdfErrorMessage(
e,
__LINE__,
__FILE__
);
}
}

template<typename T>
int netcdfPutVar(
int netcdfID,
const char *groupName,
const char *varName,
const T *values
) {
try {
auto file = FileMap::getInstance().getFile(netcdfID);
const auto group = !groupName
? file
: std::make_shared<
netCDF::NcGroup>(
file->getGroup(
groupName));
auto var = group->getVar(varName);
var.putVar(values);
return 0;
} catch (netCDF::exceptions::NcException &e) {
return netcdfErrorMessage(
e,
__LINE__,
__FILE__
);
}
}

int netcdfPutVarInt(
int netcdfID,
const char *groupName,
const char *varName,
const int *values
) {
return netcdfPutVar(
netcdfID,
groupName,
varName,
values
);
}

int netcdfPutVarInt64(
int netcdfID,
const char *groupName,
const char *varName,
const long long *values
) {
return netcdfPutVar(
netcdfID,
groupName,
varName,
values
);
}

int netcdfPutVarReal(
int netcdfID,
const char *groupName,
const char *varName,
const float *values
) {
return netcdfPutVar(
netcdfID,
groupName,
varName,
values
);
}

int netcdfPutVarString(
int netcdfID,
const char *groupName,
const char *varName,
const char **values
) {
return netcdfPutVar(
netcdfID,
groupName,
varName,
values
);
}

template<typename T>
int netcdfSetFill(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
T fillValue
) {
try {
auto file = FileMap::getInstance().getFile(netcdfID);
const auto group = !groupName
? file
: std::make_shared<
netCDF::NcGroup>(
file->getGroup(
groupName));
auto var = group->getVar(varName);
var.setFill(
fillMode,
fillValue
);
return 0;
} catch (netCDF::exceptions::NcException &e) {
return netcdfErrorMessage(
e,
__LINE__,
__FILE__
);
}
}

int netcdfSetFillInt(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
int fillValue
) {
return netcdfSetFill(
netcdfID,
groupName,
varName,
fillMode,
fillValue
);
}

int netcdfSetFillReal(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
float fillValue
) {
return netcdfSetFill(
netcdfID,
groupName,
varName,
fillMode,
fillValue
);
}

int netcdfSetFillInt64(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
long long fillValue
) {
return netcdfSetFill(
netcdfID,
groupName,
varName,
fillMode,
fillValue
);
}

int netcdfSetFillString(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
const char *fillValue
) {
return netcdfSetFill(
netcdfID,
groupName,
varName,
fillMode,
fillValue

);
}
}
118 changes: 118 additions & 0 deletions obs2ioda-v2/src/cxx/netcdf_variable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#ifndef NETCDF_VARIABLE_H
#define NETCDF_VARIABLE_H
#include <netcdf>

namespace Obs2Ioda {

extern "C" {
/**
* @brief Adds a variable to a NetCDF file.
*
* @param netcdfID The identifier of the NetCDF file where the variable will be added.
* @param groupName The name of the group in which the variable should be created.
* If nullptr, the variable is added to the root group.
* @param varName The name of the variable to be created.
* @param netcdfDataType The NetCDF data type of the variable (e.g., NC_INT, NC_FLOAT).
* @param numDims The number of dimensions associated with the variable.
* @param dimNames An array of dimension names specifying the shape of the variable.
* @return int A status code indicating the outcome of the operation:
* - 0: Success.
* - Non-zero: Failure, with an error message logged.
*/
int netcdfAddVar(
int netcdfID,
const char *groupName,
const char *varName,
nc_type netcdfDataType,
int numDims,
const char **dimNames
);

/**
* @brief Writes data to a variable in a NetCDF file.
*
* @param netcdfID The identifier of the NetCDF file where the data will be written.
* @param groupName The name of the group containing the variable. If nullptr, the variable is assumed to be in the root group.
* @param varName The name of the variable to which data will be written.
* @param values A pointer to the data to be written to the variable.
* @return int A status code indicating the outcome of the operation:
* - 0: Success.
* - Non-zero: Failure, with an error message logged.
*/
int netcdfPutVarInt(
amstokely marked this conversation as resolved.
Show resolved Hide resolved
int netcdfID,
const char *groupName,
const char *varName,
const int *values
);

int netcdfPutVarInt64(
int netcdfID,
const char *groupName,
const char *varName,
const long long *values
);

int netcdfPutVarReal(
int netcdfID,
const char *groupName,
const char *varName,
const float *values
);

int netcdfPutVarString(
int netcdfID,
const char *groupName,
const char *varName,
const char **values
);

/**
* @brief Sets the fill mode and fill value for a variable in a NetCDF file.
*
* @param netcdfID The identifier of the NetCDF file containing the variable.
* @param groupName The name of the group containing the variable. If nullptr, the variable is assumed to be in the root group.
* @param varName The name of the variable for which the fill mode is set.
* @param fillMode The fill mode to be applied:
* - 0: Disable fill mode (use uninitialized values).
* - 1: Enable fill mode (use the specified fill value).
mgduda marked this conversation as resolved.
Show resolved Hide resolved
* @param fillValue The fill value to be applied when fill mode is enabled. Must match the data type of the variable.
* @return int A status code indicating the outcome of the operation:
* - 0: Success.
* - Non-zero: Failure, with an error message logged.
*/
int netcdfSetFillInt(
amstokely marked this conversation as resolved.
Show resolved Hide resolved
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
int fillValue
);

int netcdfSetFillReal(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
float fillValue
);

int netcdfSetFillInt64(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
long long fillValue
);

int netcdfSetFillString(
int netcdfID,
const char *groupName,
const char *varName,
int fillMode,
const char *fillValue
);
}
}

#endif //NETCDF_VARIABLE_H
Loading