netxsimdg
Loading...
Searching...
No Matches
StructureFactory.cpp
Go to the documentation of this file.
1
9
11
12#include "include/DevGrid.hpp"
13#include "include/DevGridIO.hpp"
14
17
20
21#include <ncFile.h>
22#include <ncGroup.h>
23#include <ncGroupAtt.h>
24#include <stdexcept>
25#include <string>
26
27namespace Nextsim {
28
29std::string structureNameFromFile(const std::string& filePath)
30{
31 netCDF::NcFile ncf(filePath, netCDF::NcFile::read);
32 netCDF::NcGroup metaGroup(ncf.getGroup(IStructure::structureNodeName()));
33 netCDF::NcGroupAtt att = metaGroup.getAtt(IStructure::typeNodeName());
34 int len = att.getAttLength();
35 // Initialize a std::string of len, filled with zeros
36 std::string structureName(len, '\0');
37 // &str[0] gives access to the buffer, guaranteed by C++11
38 att.getValues(&structureName[0]);
39 ncf.close();
40
41 return structureName;
42}
43
44ModelState StructureFactory::stateFromFile(const std::string& filePath)
45{
46 std::string structureName = structureNameFromFile(filePath);
47 // TODO There must be a better way
48 if (DevGrid::structureName == structureName) {
49 Module::setImplementation<IStructure>("DevGrid");
50 DevGrid gridIn;
51 gridIn.setIO(new DevGridIO(gridIn));
52 return gridIn.getModelState(filePath);
53 } else if (RectangularGrid::structureName == structureName) {
54 Module::setImplementation<IStructure>("RectangularGrid");
55 RectangularGrid gridIn;
56 gridIn.setIO(new RectGridIO(gridIn));
57 return gridIn.getModelState(filePath);
58 } else if (ParametricGrid::structureName == structureName) {
59 Module::setImplementation<IStructure>("ParametricGrid");
60 ParametricGrid gridIn;
61 gridIn.setIO(new ParaGridIO(gridIn));
62 return gridIn.getModelState(filePath);
63 } else {
64 throw std::invalid_argument(
65 std::string("fileFromName: structure not implemented: ") + structureName);
66 }
67 throw std::invalid_argument(std::string("fileFromName: structure not implemented: ")
68 + structureName + "\nAlso, how did you get here?");
69 return ModelState();
70}
71
73 const ModelState& state, const ModelMetadata& meta, const std::string& filePath, bool isRestart)
74{
75 std::string structureName = Module::getImplementation<IStructure>().structureType();
76
77 if (DevGrid::structureName == structureName) {
78 DevGrid gridOut;
79 gridOut.setIO(new DevGridIO(gridOut));
80 gridOut.dumpModelState(state, meta, filePath, isRestart);
81 } else if (RectangularGrid::structureName == structureName) {
82 RectangularGrid gridOut;
83 gridOut.setIO(new RectGridIO(gridOut));
84 gridOut.dumpModelState(state, meta, filePath, isRestart);
85 } else if (ParametricGrid::structureName == structureName) {
86 ParametricGrid gridOut;
87 gridOut.setIO(new ParaGridIO(gridOut));
88 gridOut.dumpModelState(state, meta, filePath, isRestart);
89 } else {
90 throw std::invalid_argument(
91 std::string("fileFromName: structure not implemented: ") + structureName);
92 }
93}
94
95} /* namespace Nextsim */
A class to hold a grid of ElementData instances in a fixed sized square grid.
Definition DevGrid.hpp:23
void dumpModelState(const ModelState &state, const ModelMetadata &metadata, const std::string &filePath, bool isRestart=false) const override
Dumps the data to a file path.
Definition DevGrid.hpp:47
void setIO(IDevGridIO *p)
Sets the pointer to the class that will perform the IO. Should be an instance of DevGridIO.
Definition DevGrid.hpp:58
ModelState getModelState(const std::string &filePath) override
Dumps the data to a file path.
Definition DevGrid.hpp:42
A class to implemented the actual IO for DevGrid, isolating the NetCDF libraries from the rest of the...
Definition DevGridIO.hpp:21
static const std::string typeNodeName()
The name of the node holding the name of the structure type processed by this class.
static const std::string structureNodeName()
The name of the group holding the definitive structure type.
A class to hold the grid data for parameterised rectangular grids.
void dumpModelState(const ModelState &state, const ModelMetadata &metadata, const std::string &filePath, bool isRestart=false) const override
Dumps the data to a file path.
ModelState getModelState(const std::string &filePath) override
Dumps the data to a file path.
void setIO(IParaGridIO *p)
ModelState getModelState(const std::string &filePath) override
Dumps the data to a file path.
void setIO(IRectGridIO *p)
Sets the pointer to the class that will perform the IO. Should be an instance of DevGridIO.
void dumpModelState(const ModelState &state, const ModelMetadata &metadata, const std::string &filePath, bool isRestart=false) const override
Dumps the data to a file path.
static void fileFromState(const ModelState &state, const ModelMetadata &meta, const std::string &filePath, bool isRestart=false)
Takes a ModelState and a template file name to write the state out to a target file path.
static ModelState stateFromFile(const std::string &filePath)
Returns the ModelState of the named restart file.