11 #include "ioda/core/IodaUtils.h"
12 #include "ioda/ObsSpaceParameters.h"
14 #include "oops/util/DateTime.h"
21 const std::vector<std::string> & StringVector) {
22 std::size_t MaxStrLen = 0;
23 for (std::size_t i = 0; i < StringVector.size(); i++) {
24 std::size_t StrSize = StringVector[i].size();
25 if (StrSize > MaxStrLen) {
30 std::vector<std::size_t> Shape{ StringVector.size(), MaxStrLen };
37 const std::vector<std::size_t> & CharShape) {
40 std::size_t Nstrings = CharShape[0];
41 std::size_t StrLength = CharShape[1];
43 std::vector<std::string> StringVector(Nstrings,
"");
44 for (std::size_t i = 0; i < Nstrings; i++) {
46 std::vector<char> CharVector(StrLength,
' ');
47 for (std::size_t j = 0; j < StrLength; j++) {
48 CharVector[j] = CharData[(i*StrLength) + j];
59 std::string WhiteSpace(
" \t\n\r\f\v\0", 7);
60 std::string
String(CharVector.begin(), CharVector.end());
61 String.erase(
String.find_last_not_of(WhiteSpace) + 1, std::string::npos);
71 const std::vector<std::size_t> & CharShape,
char * CharData) {
75 for (std::size_t i = 0; i < CharShape[0]; i++) {
76 for (std::size_t j = 0; j < CharShape[1]; j++) {
77 std::size_t ichar = (i * CharShape[1]) + j;
78 if (j < StringVector[i].size()) {
79 CharData[ichar] = StringVector[i].data()[j];
81 CharData[ichar] =
' ';
91 if (TypeId ==
typeid(
int)) {
93 }
else if (TypeId ==
typeid(
float)) {
95 }
else if (TypeId ==
typeid(
double)) {
97 }
else if (TypeId ==
typeid(std::string)) {
99 }
else if (TypeId ==
typeid(util::DateTime)) {
100 TypeName =
"DateTime";
102 TypeName = TypeId.name();
110 std::size_t MaxStringLength = 0;
111 for (std::size_t i = 0; i < StringVector.size(); ++i) {
112 if (StringVector[i].size() > MaxStringLength) {
113 MaxStringLength = StringVector[i].size();
116 return MaxStringLength;
120 std::string
fullVarName(
const std::string & groupName,
const std::string & varName) {
121 return groupName + std::string(
"/") + varName;
127 Dimensions_t & maxVarSize0) {
141 return (std::string::npos ==
name.find(
'@')) &&
142 (std::string::npos ==
name.find(
'/')) ?
true :
false;
147 std::vector<std::string> allVars = obsGroup.
listObjects<ObjectType::Variable>(
true);
154 std::list<std::string> sortedAllVars;
155 for (
const auto&
name : allVars) {
156 if (sortedAllVars.empty()) {
157 sortedAllVars.push_back(
name);
160 auto second = sortedAllVars.begin();
162 if (sortedAllVars.front() ==
"nlocs") {
163 sortedAllVars.insert(second,
name);
165 sortedAllVars.push_front(
name);
168 sortedAllVars.push_back(
name);
178 std::list<ioda::Named_Variable> dimension_scales;
180 varObjectList.reserve(allVars.size());
181 dimVarObjectList.reserve(allVars.size());
183 for (
const auto& vname : sortedAllVars) {
186 if (dims.dimensionality >= 1) {
187 maxVarSize0 = std::max(maxVarSize0, dims.dimsCur[0]);
197 dimVarObjectList.push_back(std::make_pair(vname, v));
204 varObjectList.push_back(std::make_pair(vname, v));
208 std::vector<std::string> dimVarNames;
209 dimVarNames.reserve(dims.dimensionality);
210 for (
const auto& dim_scales_along_axis : attached_dimensions) {
211 if (dim_scales_along_axis.empty()) {
214 dimVarNames.push_back(dim_scales_along_axis[0].
name);
216 dimsAttachedToVars.emplace(vname, dimVarNames);
221 std::type_index
varDtype(
const Group & group,
const std::string & varName) {
223 std::type_index varType(
typeid(std::string));
224 if (var.
isA<
int>()) {
225 varType =
typeid(int);
226 }
else if (var.
isA<
float>()) {
227 varType =
typeid(float);
241 std::size_t dtimeSize = dtStrings.size();
242 std::vector<util::DateTime> dateTimeValues(dtimeSize);
243 for (std::size_t i = 0; i < dtimeSize; ++i) {
244 util::DateTime dateTime(dtStrings[i]);
245 dateTimeValues[i] = dateTime;
247 return dateTimeValues;
252 const std::vector<float> & timeOffsets) {
254 int Year = refIntDtime / 1000000;
255 int TempInt = refIntDtime % 1000000;
256 int Month = TempInt / 10000;
257 TempInt = TempInt % 10000;
258 int Day = TempInt / 100;
259 int Hour = TempInt % 100;
260 util::DateTime refDtime(Year, Month, Day, Hour, 0, 0);
263 std::size_t dtimeSize = timeOffsets.size();
264 std::vector<util::DateTime> dateTimeValues(dtimeSize);
265 for (std::size_t i = 0; i < dtimeSize; ++i) {
266 util::DateTime dateTime =
267 refDtime + util::Duration(round(timeOffsets[i] * 3600));
268 dateTimeValues[i] = dateTime;
270 return dateTimeValues;
275 const std::vector<std::string> & arrayData,
276 const std::vector<Dimensions_t> & arrayShape) {
279 std::size_t nstrings = arrayShape[0];
280 std::size_t strLength = arrayShape[1];
283 std::vector<std::string> stringVector(nstrings,
"");
284 for (std::size_t i = 0; i < nstrings; i++) {
285 std::string oneString =
"";
286 for (std::size_t j = 0; j < strLength; j++) {
287 oneString += arrayData[(i*strLength) + j];
297 std::string WhiteSpace(
" \t\n\r\f\v\0", 7);
298 oneString.erase(oneString.find_last_not_of(WhiteSpace) + 1, std::string::npos);
299 stringVector[i] = oneString;
309 std::vector<eckit::LocalConfiguration> writeDimConfigs =
310 obsConfig.getSubConfigurations(
"write dimensions");
311 std::vector<eckit::LocalConfiguration> writeVarConfigs =
312 obsConfig.getSubConfigurations(
"write variables");
315 std::map<std::string, Dimensions_t> dimSizes;
316 for (std::size_t i = 0; i < writeDimConfigs.size(); ++i) {
317 std::string dimName = writeDimConfigs[i].getString(
"name");
318 Dimensions_t dimSize = writeDimConfigs[i].getInt(
"size");
319 bool isUnlimited = writeDimConfigs[i].getBool(
"unlimited",
false);
324 obsParams.
setDimScale(dimName, dimSize, dimSize, dimSize);
326 dimSizes.insert(std::pair<std::string, Dimensions_t>(dimName, dimSize));
330 Dimensions_t maxVarSize = 0;
331 for (std::size_t i = 0; i < writeVarConfigs.size(); ++i) {
332 std::vector<std::string> dimNames = writeVarConfigs[i].getStringVector(
"dims");
333 Dimensions_t varSize0 = dimSizes.at(dimNames[0]);
334 if (varSize0 > maxVarSize) {
335 maxVarSize = varSize0;
344 const int timeRankNum) {
347 std::string uniqueFileName = fileName;
351 std::size_t found = uniqueFileName.find_last_of(
".");
352 if (found == std::string::npos)
353 found = uniqueFileName.length();
356 std::ostringstream ss;
357 ss <<
"_" << std::setw(4) << std::setfill(
'0') << rankNum;
358 if (timeRankNum >= 0) ss <<
"_" << timeRankNum;
361 return uniqueFileName.insert(found, ss.str());
367 std::string oldFormat;
368 std::size_t pos = varName.find(
"/");
369 if (pos == std::string::npos) {
373 std::string gname = varName.substr(0, pos);
374 std::string vname = varName.substr(pos + 1);
375 oldFormat = vname + std::string(
"@") + gname;
Groups are a new implementation of ObsSpaces.
An ObsGroup is a specialization of a ioda::Group. It provides convenience functions and guarantees th...
void setMaxVarSize(const Dimensions_t maxVarSize)
set the maximum variable size
void setDimScale(const std::string &dimName, const Dimensions_t curSize, const Dimensions_t maxSize, const Dimensions_t chunkSize)
set a new dimension scale
Has_Variables vars
Use this to access variables.
virtual std::map< ObjectType, std::vector< std::string > > listObjects(ObjectType filter=ObjectType::Ignored, bool recurse=false) const
List all objects (groups + variables) within this group.
virtual Variable open(const std::string &name) const
Open a Variable by name.
bool isA() const
Convenience function to check a Variable's storage type.
virtual bool isDimensionScale() const
Is this Variable used as a dimension scale?
virtual Dimensions getDimensions() const
virtual std::vector< std::vector< Named_Variable > > getDimensionScaleMappings(const std::list< Named_Variable > &scalesToQueryAgainst, bool firstOnly=true) const
Which dimensions are attached at which positions? This function may offer improved performance on som...
void StringVectorToCharArray(const std::vector< std::string > &StringVector, const std::vector< std::size_t > &CharShape, char *CharData)
constexpr int Unlimited
Specifies that a dimension is resizable to infinity.
std::vector< std::size_t > CharShapeFromStringVector(const std::vector< std::string > &StringVector)
std::vector< std::pair< std::string, Variable > > VarNameObjectList
typedef for holding list of variable names with associated variable object
std::string fullVarName(const std::string &groupName, const std::string &varName)
form full variable name given individual group and variable names
std::map< std::string, std::vector< std::string > > VarDimMap
typedef for holding dim names attached to variables
std::type_index varDtype(const Group &group, const std::string &varName)
get variable data type
std::vector< std::string > CharArrayToStringVector(const char *CharData, const std::vector< std::size_t > &CharShape)
std::string uniquifyFileName(const std::string &fileName, const std::size_t rankNum, const int timeRankNum)
uniquify the output file name
void collectVarDimInfo(const ObsGroup &obsGroup, VarNameObjectList &varObjectList, VarNameObjectList &dimVarObjectList, VarDimMap &dimsAttachedToVars, Dimensions_t &maxVarSize0)
collect variable and dimension information from a ioda ObsGroup
std::vector< util::DateTime > convertRefOffsetToDtime(const int refIntDtime, const std::vector< float > &timeOffsets)
convert reference, time to DateTime object
bool varIsDimScale(const Group &group, const std::string &varName)
true if variable is a dimension scale
std::size_t FindMaxStringLength(const std::vector< std::string > &StringVector)
std::string TypeIdName(const std::type_info &TypeId)
void setOfileParamsFromTestConfig(const eckit::LocalConfiguration &obsConfig, ioda::ObsSpaceParameters &obsParams)
set params for output file construction from test YAML configuration
std::vector< util::DateTime > convertDtStringsToDtime(const std::vector< std::string > &dtStrings)
convert datetime strings to DateTime object
std::string convertNewVnameToOldVname(const std::string &varName)
convert the new format varible name to the old format
std::vector< std::string > StringArrayToStringVector(const std::vector< std::string > &arrayData, const std::vector< Dimensions_t > &arrayShape)
convert 2D string array to a vector of strings
A named pair of (variable_name, ioda::Variable).
bool isPossiblyScale(const std::string &name)
Convenience lambda to hint if a variable might be a scale.