UFO
StringUtils.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018-2019 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
9 
10 #include <sstream>
11 #include <string>
12 
13 #include "eckit/exception/Exceptions.h"
14 
15 namespace ufo {
16 
17 // -----------------------------------------------------------------------------
18 // For now support both ioda v1 an ioda v2 syntax for specifying the combined
19 // variable and group name:
20 // ioda v1: variable@group
21 // ioda v2: group/variable
22 //
23 // We will eventually be obsoleting the ioda v1 syntax. For ioda v1 syntax, only
24 // allow for one "@" separator. For ioda v2 syntax, since it allows for nested groups,
25 // allow for multiple "/" separators, and the variable name is after the last "/".
26 //
27 void splitVarGroup(const std::string & vargrp, std::string & var, std::string & grp) {
28  const size_t at = vargrp.find("@");
29  const size_t slash = vargrp.find_last_of("/");
30 
31  if (at != std::string::npos) {
32  // ioda v1 syntax
33  var = vargrp.substr(0, at);
34  grp = vargrp.substr(at + 1, std::string::npos);
35  const size_t no_at = grp.find("@");
36  ASSERT(no_at == std::string::npos);
37  } else if (slash != std::string::npos) {
38  // ioda v2 syntax
39  grp = vargrp.substr(0, slash);
40  var = vargrp.substr(slash + 1, std::string::npos);
41  } else {
42  // vargrp has no "@" nor "/" then assume that vargrp is a variable
43  // name (with no group specified).
44  var = vargrp;
45  grp = "";
46  }
47 }
48 
49 // -----------------------------------------------------------------------------
50 
51 void splitInstSat(const std::string & instsat, std::string & inst, std::string & sat) {
52  const size_t at = instsat.find("_");
53  inst = instsat.substr(0, at);
54  sat = "";
55  if (at != std::string::npos) {
56  sat = instsat.substr(at + 1, std::string::npos);
57  const size_t no_at = sat.find("_");
58  ASSERT(no_at == std::string::npos);
59  }
60 }
61 
62 // -----------------------------------------------------------------------------
63 
64 bool isFloat(const std::string & str) {
65  std::istringstream iss(str);
66  float factor;
67  iss >> factor;
68  return (iss.eof() && !iss.fail());
69 }
70 
71 // -----------------------------------------------------------------------------
72 
73 bool readFloat(const std::string & str, float & num) {
74  std::istringstream iss(str);
75  iss >> num;
76  return (iss.eof() && !iss.fail());
77 }
78 
79 } // namespace ufo
Definition: RunCRTM.h:27
bool readFloat(const std::string &str, float &num)
Definition: StringUtils.cc:73
void splitInstSat(const std::string &instsat, std::string &inst, std::string &sat)
Definition: StringUtils.cc:51
void splitVarGroup(const std::string &vargrp, std::string &var, std::string &grp)
Definition: StringUtils.cc:27
bool isFloat(const std::string &str)
Definition: StringUtils.cc:64