ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_CheckpointSurfaceTemperature.H
Go to the documentation of this file.
1 #ifndef ERF_CHECKPOINT_SURFACE_TEMPERATURE_H_
2 #define ERF_CHECKPOINT_SURFACE_TEMPERATURE_H_
3 
4 #include <cctype>
5 #include <istream>
6 #include <ostream>
7 #include <string>
8 
9 #include <AMReX_PlotFileUtil.H>
10 #include <AMReX_Utility.H>
11 
13 
14 constexpr int contract_version = 1;
15 
16 inline void
17 write_contract_version (std::ostream& output)
18 {
19  output << contract_version << "\n";
20 }
21 
22 /**
23  * Return the first AMR level containing a legacy SST or TSK surface array.
24  *
25  * The arrays are identified by their AMReX header files. A return value of
26  * -1 means that neither legacy array is present on any level.
27  */
28 inline int
29 first_legacy_surface_temperature_level (const std::string& checkpointname,
30  const int finest_level)
31 {
32  for (int lev = 0; lev <= finest_level; ++lev) {
33  const bool has_sst = amrex::FileExists(
34  amrex::MultiFabFileFullPrefix(lev, checkpointname, "Level_", "SST_0_H"));
35  const bool has_tsk = amrex::FileExists(
36  amrex::MultiFabFileFullPrefix(lev, checkpointname, "Level_", "TSK_0_H"));
37  if (has_sst || has_tsk) {
38  return lev;
39  }
40  }
41  return -1;
42 }
43 
44 enum class ContractReadStatus {
45  Valid,
46  Malformed,
48 };
49 
50 enum class LegacyInitType {
51  WRFInput,
52  Metgrid,
53  Unknown
54 };
55 
57  Compatible,
60 };
61 
62 inline LegacyInitType
63 parse_legacy_init_type_value (std::string value)
64 {
65  for (char& character : value) {
66  character = static_cast<char>(std::tolower(static_cast<unsigned char>(character)));
67  }
68  if (value == "wrfinput") {
70  }
71  if (value == "metgrid") {
73  }
75 }
76 
77 /**
78  * Parse the integer surface-temperature contract marker from a checkpoint.
79  */
80 inline ContractReadStatus
81 read_contract_version (std::istream& input, int& version)
82 {
83  if (!(input >> version)) {
85  }
86 
87  std::string extra;
88  if (input >> extra) {
90  }
91 
92  return (version == contract_version) ? ContractReadStatus::Valid
94 }
95 
96 /**
97  * Legacy Metgrid SST/TSK arrays are absolute temperature, whereas current
98  * Metgrid initialization stores the SurfaceLayer representation (theta).
99  * Without the marker there is no pressure in a checkpoint with which to
100  * reinterpret those legacy arrays safely.
101  */
102 inline LegacyInitType
104 {
105  constexpr const char* key = "erf.init_type";
106  constexpr std::size_t key_length = 13;
108  std::string line;
109 
110  while (std::getline(input, line)) {
111  std::size_t pos = 0;
112  while (pos < line.size() &&
113  std::isspace(static_cast<unsigned char>(line[pos]))) {
114  ++pos;
115  }
116  if (line.compare(pos, key_length, key) != 0) {
117  continue;
118  }
119  pos += key_length;
120  if (pos < line.size() && !std::isspace(static_cast<unsigned char>(line[pos])) &&
121  line[pos] != '=') {
122  continue;
123  }
124  while (pos < line.size() &&
125  std::isspace(static_cast<unsigned char>(line[pos]))) {
126  ++pos;
127  }
128  if (pos == line.size() || line[pos] != '=') {
129  result = LegacyInitType::Unknown;
130  continue;
131  }
132  ++pos;
133  while (pos < line.size() &&
134  std::isspace(static_cast<unsigned char>(line[pos]))) {
135  ++pos;
136  }
137 
138  std::string value;
139  bool malformed = (pos == line.size());
140  if (!malformed && line[pos] == '"') {
141  ++pos;
142  const std::size_t end_quote = line.find('"', pos);
143  if (end_quote == std::string::npos) {
144  malformed = true;
145  } else {
146  value = line.substr(pos, end_quote - pos);
147  pos = end_quote + 1;
148  }
149  } else if (!malformed) {
150  const std::size_t value_begin = pos;
151  while (pos < line.size() &&
152  !std::isspace(static_cast<unsigned char>(line[pos]))) {
153  ++pos;
154  }
155  value = line.substr(value_begin, pos - value_begin);
156  }
157 
158  while (pos < line.size() &&
159  std::isspace(static_cast<unsigned char>(line[pos]))) {
160  ++pos;
161  }
162  if (malformed || pos != line.size()) {
163  result = LegacyInitType::Unknown;
164  continue;
165  }
166 
167  result = parse_legacy_init_type_value(value);
168  }
169 
170  return result;
171 }
172 
175  const bool marker_present,
176  const bool has_surface_arrays,
177  const LegacyInitType legacy_init_type)
178 {
179  if (marker_present || !has_surface_arrays) {
181  }
182  if (legacy_init_type == LegacyInitType::WRFInput) {
184  }
185  if (legacy_init_type == LegacyInitType::Metgrid) {
187  }
189 }
190 
191 } // namespace erf_checkpoint_surface_temperature
192 
193 #endif
Definition: ERF_CheckpointSurfaceTemperature.H:12
ContractReadStatus read_contract_version(std::istream &input, int &version)
Definition: ERF_CheckpointSurfaceTemperature.H:81
LegacyInitType parse_legacy_init_type_value(std::string value)
Definition: ERF_CheckpointSurfaceTemperature.H:63
constexpr int contract_version
Definition: ERF_CheckpointSurfaceTemperature.H:14
int first_legacy_surface_temperature_level(const std::string &checkpointname, const int finest_level)
Definition: ERF_CheckpointSurfaceTemperature.H:29
LegacyInitType
Definition: ERF_CheckpointSurfaceTemperature.H:50
ContractReadStatus
Definition: ERF_CheckpointSurfaceTemperature.H:44
LegacySurfaceTemperatureCompatibility
Definition: ERF_CheckpointSurfaceTemperature.H:56
LegacyInitType parse_legacy_init_type_from_job_info(std::istream &input)
Definition: ERF_CheckpointSurfaceTemperature.H:103
void write_contract_version(std::ostream &output)
Definition: ERF_CheckpointSurfaceTemperature.H:17
LegacySurfaceTemperatureCompatibility classify_legacy_surface_temperature_checkpoint(const bool marker_present, const bool has_surface_arrays, const LegacyInitType legacy_init_type)
Definition: ERF_CheckpointSurfaceTemperature.H:174