IODA
IodaUtils.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 
8 #include "ioda/core/IodaUtils.h"
9 
10 #include "oops/util/DateTime.h"
11 
12 
13 namespace ioda {
14 
15 // -----------------------------------------------------------------------------
16 
17 std::vector<std::size_t> CharShapeFromStringVector(
18  const std::vector<std::string> & StringVector) {
19  std::size_t MaxStrLen = 0;
20  for (std::size_t i = 0; i < StringVector.size(); i++) {
21  std::size_t StrSize = StringVector[i].size();
22  if (StrSize > MaxStrLen) {
23  MaxStrLen = StrSize;
24  }
25  }
26 
27  std::vector<std::size_t> Shape{ StringVector.size(), MaxStrLen };
28  return Shape;
29 }
30 
31 // -----------------------------------------------------------------------------
32 
33 std::vector<std::string> CharArrayToStringVector(const char * CharData,
34  const std::vector<std::size_t> & CharShape) {
35  // CharShape[0] is the number of strings
36  // CharShape[1] is the length of each string
37  std::size_t Nstrings = CharShape[0];
38  std::size_t StrLength = CharShape[1];
39 
40  std::vector<std::string> StringVector(Nstrings, "");
41  for (std::size_t i = 0; i < Nstrings; i++) {
42  // Copy characters for i-th string into a char vector
43  std::vector<char> CharVector(StrLength, ' ');
44  for (std::size_t j = 0; j < StrLength; j++) {
45  CharVector[j] = CharData[(i*StrLength) + j];
46  }
47 
48  // Convert the char vector to a single string. Any trailing white space will be
49  // included in the string, so strip off the trailing white space.
50  //
51  // In order to include null characters in the white space list, the (char *, size_t)
52  // form of the string constructor needs to be used. The size_t (2nd) argument says
53  // how many characters to use from the "buffer" (1st argument). If the (char *) form
54  // of the string constructor is use, the null character terminates the string and only
55  // those characters leading up to the null are used.
56  std::string WhiteSpace(" \t\n\r\f\v\0", 7);
57  std::string String(CharVector.begin(), CharVector.end());
58  String.erase(String.find_last_not_of(WhiteSpace) + 1, std::string::npos);
59  StringVector[i] = String;
60  }
61 
62  return StringVector;
63 }
64 
65 // -----------------------------------------------------------------------------
66 
67 void StringVectorToCharArray(const std::vector<std::string> & StringVector,
68  const std::vector<std::size_t> & CharShape, char * CharData) {
69  // CharShape[0] is the number of strings, and CharShape[1] is the maximum
70  // string lenghth. Walk through the string vector, copy the string and fill
71  // with white space at the ends of strings if necessary.
72  for (std::size_t i = 0; i < CharShape[0]; i++) {
73  for (std::size_t j = 0; j < CharShape[1]; j++) {
74  std::size_t ichar = (i * CharShape[1]) + j;
75  if (j < StringVector[i].size()) {
76  CharData[ichar] = StringVector[i].data()[j];
77  } else {
78  CharData[ichar] = ' ';
79  }
80  }
81  }
82 }
83 
84 // -----------------------------------------------------------------------------
85 
86 std::string TypeIdName(const std::type_info & TypeId) {
87  std::string TypeName;
88  if (TypeId == typeid(int)) {
89  TypeName = "integer";
90  } else if (TypeId == typeid(float)) {
91  TypeName = "float";
92  } else if (TypeId == typeid(double)) {
93  TypeName = "double";
94  } else if (TypeId == typeid(std::string)) {
95  TypeName = "string";
96  } else if (TypeId == typeid(util::DateTime)) {
97  TypeName = "DateTime";
98  } else {
99  TypeName = TypeId.name();
100  }
101 
102  return TypeName;
103 }
104 
105 // -----------------------------------------------------------------------------
106 std::size_t FindMaxStringLength(const std::vector<std::string> & StringVector) {
107  std::size_t MaxStringLength = 0;
108  for (std::size_t i = 0; i < StringVector.size(); ++i) {
109  if (StringVector[i].size() > MaxStringLength) {
110  MaxStringLength = StringVector[i].size();
111  }
112  }
113  return MaxStringLength;
114 }
115 
116 // -----------------------------------------------------------------------------
117 } // namespace ioda
ioda
Definition: IodaUtils.cc:13
ioda::CharShapeFromStringVector
std::vector< std::size_t > CharShapeFromStringVector(const std::vector< std::string > &StringVector)
Definition: IodaUtils.cc:17
ioda::ObsDtype::String
@ String
ioda::FindMaxStringLength
std::size_t FindMaxStringLength(const std::vector< std::string > &StringVector)
Definition: IodaUtils.cc:106
ioda::StringVectorToCharArray
void StringVectorToCharArray(const std::vector< std::string > &StringVector, const std::vector< std::size_t > &CharShape, char *CharData)
Definition: IodaUtils.cc:67
ioda::TypeIdName
std::string TypeIdName(const std::type_info &TypeId)
Definition: IodaUtils.cc:86
ioda::CharArrayToStringVector
std::vector< std::string > CharArrayToStringVector(const char *CharData, const std::vector< std::size_t > &CharShape)
Definition: IodaUtils.cc:33