Skip to content

Commit baf807e

Browse files
committed
Short mode in default in openPMD >= 2.
1 parent 8736da8 commit baf807e

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

include/openPMD/IO/IOTask.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ struct OPENPMDAPI_EXPORT Parameter<Operation::CREATE_FILE>
142142

143143
std::string name = "";
144144
IterationEncoding encoding = IterationEncoding::groupBased;
145+
std::string openPMDversion;
145146
};
146147

147148
template <>

include/openPMD/RecordComponent.tpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#pragma once
2323

24+
#include "openPMD/Datatype.hpp"
25+
#include "openPMD/Error.hpp"
2426
#include "openPMD/RecordComponent.hpp"
2527
#include "openPMD/Span.hpp"
2628
#include "openPMD/auxiliary/Memory.hpp"
@@ -29,6 +31,7 @@
2931
#include "openPMD/auxiliary/UniquePtr.hpp"
3032

3133
#include <memory>
34+
#include <type_traits>
3235

3336
namespace openPMD
3437
{
@@ -92,12 +95,34 @@ inline std::shared_ptr<T> RecordComponent::loadChunk(Offset o, Extent e)
9295
#endif
9396
}
9497

98+
namespace detail
99+
{
100+
template <typename To>
101+
struct do_convert
102+
{
103+
template <typename From>
104+
static std::optional<To> call(Attribute &attr)
105+
{
106+
if constexpr (std::is_convertible_v<From, To>)
107+
{
108+
return std::make_optional<To>(attr.get<From>());
109+
}
110+
else
111+
{
112+
return std::nullopt;
113+
}
114+
}
115+
116+
static constexpr char const *errorMsg = "is_conversible";
117+
};
118+
} // namespace detail
119+
95120
template <typename T>
96121
inline void
97122
RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
98123
{
99124
Datatype dtype = determineDatatype(data);
100-
if (dtype != getDatatype())
125+
if (dtype != getDatatype() && !constant())
101126
if (!isSameInteger<T>(getDatatype()) &&
102127
!isSameFloatingPoint<T>(getDatatype()) &&
103128
!isSameComplexFloatingPoint<T>(getDatatype()) &&
@@ -159,10 +184,25 @@ RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
159184
for (auto const &dimensionSize : extent)
160185
numPoints *= dimensionSize;
161186

162-
T value = rc.m_constantValue.get<T>();
187+
std::optional<T> val =
188+
switchNonVectorType<detail::do_convert</* To = */ T>>(
189+
/* from = */ getDatatype(), rc.m_constantValue);
163190

164-
T *raw_ptr = data.get();
165-
std::fill(raw_ptr, raw_ptr + numPoints, value);
191+
if (val.has_value())
192+
{
193+
T *raw_ptr = data.get();
194+
std::fill(raw_ptr, raw_ptr + numPoints, *val);
195+
}
196+
else
197+
{
198+
std::string const data_type_str = datatypeToString(getDatatype());
199+
std::string const requ_type_str =
200+
datatypeToString(determineDatatype<T>());
201+
std::string err_msg =
202+
"Type conversion during chunk loading not possible! ";
203+
err_msg += "Data: " + data_type_str + "; Load as: " + requ_type_str;
204+
throw error::WrongAPIUsage(err_msg);
205+
}
166206
}
167207
else
168208
{
@@ -250,7 +290,7 @@ void RecordComponent::storeChunkRaw(T *ptr, Offset offset, Extent extent)
250290

251291
template <typename T_ContiguousContainer>
252292
inline typename std::enable_if_t<
253-
auxiliary::IsContiguousContainer_v<T_ContiguousContainer> >
293+
auxiliary::IsContiguousContainer_v<T_ContiguousContainer>>
254294
RecordComponent::storeChunk(T_ContiguousContainer &data, Offset o, Extent e)
255295
{
256296
uint8_t dim = getDimensionality();

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ void JSONIOHandlerImpl::createFile(
455455
access::write(m_handler->m_backendAccess),
456456
"[JSON] Creating a file in read-only mode is not possible.");
457457

458+
if (m_attributeModeSpecificationVia == SpecificationVia::DefaultValue)
459+
{
460+
m_attributeMode = parameters.openPMDversion >= "2."
461+
? AttributeMode::Short
462+
: AttributeMode::Long;
463+
}
464+
458465
if (!writable->written)
459466
{
460467
std::string name = parameters.name + m_originalExtension;

src/Iteration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ void Iteration::flushFileBased(
206206
/* create file */
207207
Parameter<Operation::CREATE_FILE> fCreate;
208208
fCreate.name = filename;
209+
fCreate.openPMDversion = s.openPMD();
209210
IOHandler()->enqueue(IOTask(&s.writable(), fCreate));
210211

211212
/* create basePath */

src/Series.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ void Series::flushGorVBased(
926926
Parameter<Operation::CREATE_FILE> fCreate;
927927
fCreate.name = series.m_name;
928928
fCreate.encoding = iterationEncoding();
929+
fCreate.openPMDversion = openPMD();
929930
IOHandler()->enqueue(IOTask(this, fCreate));
930931
}
931932

0 commit comments

Comments
 (0)