DynExp
Highly flexible laboratory automation for dynamically changing experiments.
ArbitraryFunctionFromCSV.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
5 
6 namespace DynExpModule
7 {
8 
10  {
11  Init();
12  }
13 
15  {
16  }
17 
19  {
21  }
22 
24  {
25  }
26 
28  {
29  std::string CSVDataPath;
30  size_t SkipLines{};
31  double TimeStretch{}, TimeOffset{}, ValueStretch{}, ValueOffset{};
32 
33  {
34  auto ModuleParams = DynExp::dynamic_Params_cast<ArbitraryFunctionFromCSV>(Instance->ParamsGetter());
35  auto ModuleData = DynExp::dynamic_ModuleData_cast<ArbitraryFunctionFromCSV>(Instance->ModuleDataGetter());
36 
37  Instance->LockObject(ModuleParams->FunctionGenerator, ModuleData->FunctionGenerator);
38 
39  CSVDataPath = ModuleParams->CSVDataPath.GetPath().string();
40  SkipLines = ModuleParams->SkipLines;
41  TimeStretch = ModuleParams->TimeStretch;
42  TimeOffset = ModuleParams->TimeOffset;
43  ValueStretch = ModuleParams->ValueStretch;
44  ValueOffset = ModuleParams->ValueOffset;
45  } // ModuleParams and ModuleData unlocked here.
46 
47  // Reading and parsing potentially very heavy. So, unlock every mutex before.
48  auto CSVData = Util::ReadFromFile(CSVDataPath);
49 
50  std::vector<DynExpInstr::BasicSample> BasicSamples;
51  try
52  {
53  try
54  {
55  // Try to parse format time;value...
56  auto ParsedCSV = Util::ParseCSV<double, double>(CSVData, ';', SkipLines);
57 
58  for (const auto& ParsedTuple : ParsedCSV)
59  BasicSamples.emplace_back(std::get<1>(ParsedTuple) * ValueStretch + ValueOffset, std::get<0>(ParsedTuple) * TimeStretch + TimeOffset);
60  }
61  catch ([[maybe_unused]] std::ios_base::failure& e)
62  {
63  // ...did not work. So, try to parse list of values.
64  auto ParsedCSV = Util::ParseCSV<double>(CSVData, ';', SkipLines);
65 
66  for (const auto& ParsedTuple : ParsedCSV)
67  BasicSamples.emplace_back(std::get<0>(ParsedTuple) * ValueStretch + ValueOffset);
68  }
69  }
70  catch ([[maybe_unused]] std::ios_base::failure& e)
71  {
72  throw Util::InvalidDataException("Error parsing the CSV file \"" + CSVDataPath + "\".");
73  }
74 
75  auto ModuleData = DynExp::dynamic_ModuleData_cast<ArbitraryFunctionFromCSV>(Instance->ModuleDataGetter());
76  ModuleData->FunctionGenerator->Clear();
77  ModuleData->FunctionGenerator->SetArbitraryFunction(std::move(BasicSamples), true);
78  }
79 
81  {
82  auto ModuleData = DynExp::dynamic_ModuleData_cast<ArbitraryFunctionFromCSV>(Instance->ModuleDataGetter());
83 
84  ModuleData->FunctionGenerator->Stop();
85  Instance->UnlockObject(ModuleData->FunctionGenerator);
86  }
87 }
Implementation of a module to read a waveform from a CSV file and to store it in a data stream of a f...
void ResetImpl(dispatch_tag< ModuleDataBase >) override final
void ResetImpl(dispatch_tag< ModuleBase >) override final
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition: Module.h:743
Refer to ParamsBase::dispatch_tag.
Definition: Module.h:189
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition: Module.h:793
const ModuleBase::ModuleDataGetterType ModuleDataGetter
Getter for module's data. Refer to ModuleBase::ModuleDataGetterType.
Definition: Module.h:825
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
const Object::ParamsGetterType ParamsGetter
Invoke to obtain the parameters (derived from ParamsBase) of Owner.
Definition: Object.h:3671
void UnlockObject(LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer)
Unlocks an Object instance stored in the LinkedObjectWrapperContainer ObjectWrapperContainer....
Definition: Object.h:3570
void LockObject(const ParamsBase::Param< ObjectLink< ObjectT >> &LinkParam, LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer, std::chrono::milliseconds Timeout=ObjectLinkBase::LockObjectTimeoutDefault)
Locks an Object instance referenced by a parameter LinkParam of type ParamsBase::Param< ObjectLink< O...
Definition: Object.h:3554
Data to operate on is invalid for a specific purpose. This indicates a corrupted data structure or fu...
Definition: Exception.h:163
DynExp's module namespace contains the implementation of DynExp modules which extend DynExp's core fu...
DynExpErrorCodes
DynExp's error codes
Definition: Exception.h:22
std::string ReadFromFile(const QString &Filename)
Reads the entire content from a text file.
Definition: QtUtil.cpp:250
Accumulates include statements to provide a precompiled header.