IODA
IodaUtils.h
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 #ifndef CORE_IODAUTILS_H_
9 #define CORE_IODAUTILS_H_
10 
11 #include <string>
12 #include <typeinfo>
13 #include <vector>
14 
15 #include "oops/util/abor1_cpp.h"
16 #include "oops/util/missingValues.h"
17 
18 namespace ioda {
19 
20  // Utilities for converting back and forth between vector of strings and
21  // a 2D character array.
22  std::vector<std::size_t> CharShapeFromStringVector(
23  const std::vector<std::string> & StringVector);
24 
25  std::vector<std::string> CharArrayToStringVector(const char * CharData,
26  const std::vector<std::size_t> & CharShape);
27 
28  void StringVectorToCharArray(const std::vector<std::string> & StringVector,
29  const std::vector<std::size_t> & CharShape, char * CharData);
30 
31  std::size_t FindMaxStringLength(const std::vector<std::string> & StringVector);
32 
33  std::string TypeIdName(const std::type_info & TypeId);
34 
35  // -----------------------------------------------------------------------------
36  /*!
37  * \details This method will perform numeric data type conversions. The caller needs
38  * to allocate memory for the converted data (ToVar). This method is aware
39  * of the IODA missing values and will convert these appropriately. For
40  * example when converting double to float, all double missing values will
41  * be replaced with float missing values during the conversion.
42  *
43  * \param[in] FromVar Vector of variable we are converting from
44  * \param[out] ToVar Vector of variable we are converting to
45  * \param[in] VarSize Total number of elements in FromVar and ToVar.
46  */
47  template<typename FromType, typename ToType>
48  void ConvertVarType(const std::vector<FromType> & FromVar, std::vector<ToType> & ToVar) {
49  std::string FromTypeName = TypeIdName(typeid(FromType));
50  std::string ToTypeName = TypeIdName(typeid(ToType));
51  const FromType FromMiss = util::missingValue(FromMiss);
52  const ToType ToMiss = util::missingValue(ToMiss);
53 
54  // It is assumed that the caller has allocated memory for both input and output
55  // variables.
56  //
57  // In any type change, the missing values need to be switched.
58  //
59  // Allow type changes between numeric types (int, float, double). These can
60  // be handled with the standard conversions.
61  bool FromTypeOkay = ((typeid(FromType) == typeid(int)) ||
62  (typeid(FromType) == typeid(float)) ||
63  (typeid(FromType) == typeid(double)));
64 
65  bool ToTypeOkay = ((typeid(ToType) == typeid(int)) ||
66  (typeid(ToType) == typeid(float)) ||
67  (typeid(ToType) == typeid(double)));
68 
69  if (FromTypeOkay && ToTypeOkay) {
70  for (std::size_t i = 0; i < FromVar.size(); i++) {
71  if (FromVar[i] == FromMiss) {
72  ToVar[i] = ToMiss;
73  } else {
74  ToVar[i] = static_cast<ToType>(FromVar[i]);
75  }
76  }
77  } else {
78  std::string ErrorMsg = "Unsupported variable data type conversion: " +
79  FromTypeName + " to " + ToTypeName;
80  ABORT(ErrorMsg);
81  }
82  }
83 } // namespace ioda
84 
85 #endif // CORE_IODAUTILS_H_
ioda::ConvertVarType
void ConvertVarType(const std::vector< FromType > &FromVar, std::vector< ToType > &ToVar)
Definition: IodaUtils.h:48
ioda
Definition: IodaUtils.cc:13
ioda::CharShapeFromStringVector
std::vector< std::size_t > CharShapeFromStringVector(const std::vector< std::string > &StringVector)
Definition: IodaUtils.cc:17
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