From 2605e245e9098d1480c20da68cd78b0585a60e5e Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Wed, 12 Jul 2017 05:11:43 +0200
Subject: [PATCH] Add sync timestamp text dataset

---
 Source/Plugins/NWBFormat/NWBFormat.cpp    | 39 +++++++++++++++++++++++
 Source/Plugins/NWBFormat/NWBFormat.h      |  3 +-
 Source/Plugins/NWBFormat/NWBRecording.cpp |  2 +-
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/Source/Plugins/NWBFormat/NWBFormat.cpp b/Source/Plugins/NWBFormat/NWBFormat.cpp
index ecb504c57..dea14b246 100644
--- a/Source/Plugins/NWBFormat/NWBFormat.cpp
+++ b/Source/Plugins/NWBFormat/NWBFormat.cpp
@@ -284,6 +284,33 @@ bool NWBFile::startNewRecording(int recordingNumber, const Array<ContinuousGroup
 		 eventDataSets.add(tsStruct.release());
 
 	 }
+	 basePath = rootPath + "/events/sync_messages";
+	 ancestry.clearQuick();
+	 ancestry.add("Timeseries");
+	 ancestry.add("AnnotationSeries");
+	 String desc = "Stores recording start timestamps for each processor in text format";
+	 if (!createTimeSeriesBase(basePath, "Autogenerated messages", desc, desc, ancestry)) return false;
+	 tsStruct = new TimeSeries();
+	 tsStruct->basePath = basePath;
+	 dSet = createDataSet(BaseDataType::STR(100), 0, 1, basePath + "/data");
+	 if (dSet == nullptr)
+	 {
+		 std::cerr << "Error creating dataset for sync messages" << std::endl;
+		 return false;
+	 }
+	 else
+	 {
+		 createDataAttributes(basePath, NAN, NAN, "n/a");
+	 }
+	 tsStruct->baseDataSet = dSet;
+	 dSet = createTimestampDataSet(basePath, 1);
+	 if (dSet == nullptr) return false;
+	 tsStruct->timestampDataSet = dSet;
+
+	 dSet = createDataSet(BaseDataType::U8, 0, 1, basePath + "/control");
+	 if (dSet == nullptr) return false;
+	 tsStruct->controlDataSet = dSet;
+	 syncMsgDataSet = tsStruct;
 
 	 return true;
  }
@@ -309,10 +336,13 @@ bool NWBFile::startNewRecording(int recordingNumber, const Array<ContinuousGroup
 		 tsStruct = eventDataSets[i];
 		 CHECK_ERROR(setAttribute(BaseDataType::U64, &(tsStruct->numSamples), tsStruct->basePath, "num_samples"));
 	 }
+	 
+	 CHECK_ERROR(setAttribute(BaseDataType::U64, &(syncMsgDataSet->numSamples), syncMsgDataSet->basePath, "num_samples"));
 
 	 continuousDataSets.clear();
 	 spikeDataSets.clear();
 	 eventDataSets.clear();
+	 syncMsgDataSet = nullptr;
  }
  
  void NWBFile::writeData(int datasetID, int channel, int nSamples, const float* data, float bitVolts)
@@ -425,6 +455,15 @@ bool NWBFile::startNewRecording(int recordingNumber, const Array<ContinuousGroup
 	 eventDataSets[eventID]->numSamples += 1;
  }
 
+ void NWBFile::writeTimestampSyncText(uint16 sourceID, int64 timestamp, float sourceSampleRate, String text)
+ {
+	 CHECK_ERROR(syncMsgDataSet->baseDataSet->writeDataBlock(1, BaseDataType::STR(text.length()), text.toUTF8()));
+	 double timeSec = timestamp / sourceSampleRate;
+	 CHECK_ERROR(syncMsgDataSet->timestampDataSet->writeDataBlock(1, BaseDataType::F64, &timeSec));
+	 CHECK_ERROR(syncMsgDataSet->controlDataSet->writeDataBlock(1, BaseDataType::U8, &sourceID));
+	 syncMsgDataSet->numSamples += 1;
+ }
+
  
  String NWBFile::getFileName()
  {
diff --git a/Source/Plugins/NWBFormat/NWBFormat.h b/Source/Plugins/NWBFormat/NWBFormat.h
index 2ffb6d3b3..bf5fb79b2 100644
--- a/Source/Plugins/NWBFormat/NWBFormat.h
+++ b/Source/Plugins/NWBFormat/NWBFormat.h
@@ -55,6 +55,7 @@ namespace NWBRecording
 		void writeTimestamps(int datasetID, int nSamples, const double* data);
 		void writeSpike(int electrodeId, const SpikeChannel* channel, const SpikeEvent* event);
 		void writeEvent(int eventID, const EventChannel* channel, const Event* event);
+		void writeTimestampSyncText(uint16 sourceID, int64 timestamp, float sourceSampleRate, String text);
 		String getFileName() override;
 		void setXmlText(const String& xmlText);
 
@@ -84,7 +85,7 @@ namespace NWBRecording
 		OwnedArray<TimeSeries>  continuousDataSets;
 		OwnedArray<TimeSeries> spikeDataSets;
 		OwnedArray<TimeSeries> eventDataSets;
-
+		ScopedPointer<TimeSeries> syncMsgDataSet;
 
 		const String* xmlText;
 		const String identifierText;
diff --git a/Source/Plugins/NWBFormat/NWBRecording.cpp b/Source/Plugins/NWBFormat/NWBRecording.cpp
index 4737a8083..59473f86a 100644
--- a/Source/Plugins/NWBFormat/NWBRecording.cpp
+++ b/Source/Plugins/NWBFormat/NWBRecording.cpp
@@ -166,7 +166,7 @@ void NWBRecordEngine::writeEvent(int eventIndex, const MidiMessage& event)
 
 void NWBRecordEngine::writeTimestampSyncText(uint16 sourceID, uint16 sourceIdx, int64 timestamp, float sourceSampleRate, String text)
 {
-	
+	recordFile->writeTimestampSyncText(sourceID, timestamp, sourceSampleRate, text);
 }
 
 void NWBRecordEngine::addSpikeElectrode(int index,const  SpikeChannel* elec) 
-- 
GitLab