netxsimdg
Loading...
Searching...
No Matches
CommonRestartMetadata.cpp
Go to the documentation of this file.
1
9
10#include <cstdint>
11#include <cstring>
12#include <ncInt64.h>
13#include <ncString.h>
14#include <ncVar.h>
15
16namespace Nextsim {
17
19 netCDF::NcFile& rootGroup, const ModelMetadata& metadata)
20{
21 netCDF::NcGroup structGroup = rootGroup.addGroup(IStructure::structureNodeName());
22 structGroup.putAtt(IStructure::typeNodeName(), metadata.structureName());
23 return rootGroup;
24}
25
27 netCDF::NcGroup& metaGroup, const ModelMetadata& metadata)
28{
29 // Structure type
30 metaGroup.putAtt(IStructure::typeNodeName(), metadata.structureName());
31
32 // Current time
33 netCDF::NcGroup timeGroup = metaGroup.addGroup(timeNodeName());
34 // As Unix time
35 netCDF::NcVar unixVar = timeGroup.addVar(unformattedName(), netCDF::ncInt64);
36 Duration sinceEpoch = metadata.time() - TimePoint();
37 std::uint64_t secondsSinceEpoch = sinceEpoch.seconds();
38 unixVar.putVar(&secondsSinceEpoch);
39 unixVar.putAtt(std::string("units"), "seconds since 1970-01-01T00:00:00Z");
40
41 // Add formatted string as attribute as NetCDF4 does not support string variables
42 // in parallel mode
43 unixVar.putAtt(std::string("format"), TimePoint::ymdhmsFormat);
44 unixVar.putAtt(formattedName(), metadata.m_time.format());
45
46 // All other configuration data
47 netCDF::NcGroup configGroup = metaGroup.addGroup(configurationNode());
48
49 for (auto entry : metadata.m_config) {
50 switch (entry.second.index()) {
51 case (CONFIGMAP_DOUBLE): {
52 configGroup.putAtt(entry.first, netCDF::ncDouble, *std::get_if<double>(&entry.second));
53 break;
54 }
55 case (CONFIGMAP_UNSIGNED): {
56 configGroup.putAtt(entry.first, netCDF::ncUint, *std::get_if<unsigned>(&entry.second));
57 break;
58 }
59 case (CONFIGMAP_INT): {
60 configGroup.putAtt(entry.first, netCDF::ncInt, *std::get_if<int>(&entry.second));
61 break;
62 }
63 case (CONFIGMAP_STRING): {
64 std::string extring = std::get<std::string>(entry.second);
65 configGroup.putAtt(entry.first, std::get<std::string>(entry.second));
66 break;
67 }
68 }
69 }
70
71 return metaGroup;
72}
73
74} /* namespace Nextsim */
static netCDF::NcGroup & writeRestartMetadata(netCDF::NcGroup &metaGroup, const ModelMetadata &metadata)
Writes the standard restart file metadata to a metadata node.
static netCDF::NcGroup & writeStructureType(netCDF::NcFile &rootGroup, const ModelMetadata &metadata)
Writes the structure type to the root of the restart file for future retrieval.
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.
const std::string & structureName() const
Returns the string description of the model grid structure.
const TimePoint & time() const
Returns the current model time.