14#include "include/ModelArray.hpp"
30enum class StringName {
39static std::string hiceName =
"hice";
40static std::string ciceName =
"cice";
41static std::string hsnowName =
"hsnow";
42static std::string ticeName =
"tice";
44typedef std::map<StringName, std::string> NameMap;
46static const std::string metaName =
"meta";
47static const std::string dataName =
"data";
48static const std::string mdiName =
"missing_value";
51static void initModelMetaData(
const netCDF::NcGroup& metaGroup) { }
53static ModelState initModelData(
const netCDF::NcGroup& dataGroup)
57 std::vector<size_t> dim3(nDims);
58 std::vector<size_t> dim2(nDims - 1);
60 for (
int iDim = 0; iDim < nDims; ++iDim) {
61 dim3[iDim] = dataGroup.getVar(ticeName).getDim(iDim).getSize();
65 size_t fileZLevels = dim3[2];
66 dim3[2] = NZLevels::get();
74 std::multimap<std::string, netCDF::NcVar> varMap = dataGroup.getVars();
76 for (
const auto var : varMap) {
78 int nDims = var.second.getDimCount();
79 std::vector<netCDF::NcDim> dims = var.second.getDims();
81 for (
const auto dim : dims) {
82 totalSz *= dim.getSize();
85 std::string varName = var.first;
86 std::vector<double> buffer(totalSz);
87 var.second.getVar(buffer.data());
89 HField data = ModelArray::HField();
90 data.setData(buffer.data());
91 auto [i, y] = state.data.insert({ varName, data });
92 }
else if (nDims == 3) {
93 ZField data = ModelArray::ZField();
97 totalSz = dim3[0] * dim3[1] * dim3[2];
98 buffer.resize(totalSz);
99 std::vector<size_t> startVector = { 0, 0, 0 };
100 var.second.getVar(startVector, dim3, buffer.data());
101 data.setData(buffer.data());
102 state.data[varName] = data;
112 netCDF::NcFile ncFile(filePath, netCDF::NcFile::read);
114 netCDF::NcGroup metaGroup(ncFile.getGroup(metaName));
115 netCDF::NcGroup dataGroup(ncFile.getGroup(dataName));
116 initModelMetaData(metaGroup);
123void dumpModelMeta(
const ModelMetadata& metadata, netCDF::NcGroup& metaGroup)
128void dumpModelData(
const ModelState& state, netCDF::NcGroup& dataGroup)
130 int nx = DevGrid::nx;
133 netCDF::NcDim xDim = dataGroup.addDim(DevGrid::xDimName, nx);
134 netCDF::NcDim yDim = dataGroup.addDim(DevGrid::yDimName, nx);
135 std::vector<netCDF::NcDim> dims2 = { xDim, yDim };
137 netCDF::NcDim zDim = dataGroup.addDim(DevGrid::nIceLayersName, nLayers);
138 std::vector<netCDF::NcDim> dims3 = { xDim, yDim, zDim };
140 for (
const auto entry : state.data) {
141 const std::string& name = entry.first;
142 if (entry.second.getType() == ModelArray::Type::H) {
143 netCDF::NcVar var(dataGroup.addVar(name, netCDF::ncDouble, dims2));
144 var.putAtt(mdiName, netCDF::ncDouble, MissingData::value);
145 var.putVar(entry.second.getData());
146 }
else if (entry.second.getType() == ModelArray::Type::Z) {
147 netCDF::NcVar var(dataGroup.addVar(name, netCDF::ncDouble, dims3));
148 var.putAtt(mdiName, netCDF::ncDouble, MissingData::value);
149 var.putVar(entry.second.getData());
155 const std::string& filePath,
bool isRestart)
const
157 netCDF::NcFile ncFile(filePath, netCDF::NcFile::replace);
158 netCDF::NcGroup metaGroup = ncFile.addGroup(metaName);
159 netCDF::NcGroup dataGroup = ncFile.addGroup(dataName);
161 dumpModelMeta(metadata, metaGroup);
162 dumpModelData(state, dataGroup);
ModelState getModelState(const std::string &filePath) const override
Generates the ModelState based on the data in the given file.
void dumpModelState(const ModelState &state, const ModelMetadata &metadata, const std::string &filePath, bool isRestart) const override
Dumps the given ModelState to the given file path.
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.