From bbd13628c4ba269fdcbdd4c01476dac2e6d28549 Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Wed, 24 Aug 2016 18:36:18 +0200
Subject: [PATCH] Add default datasets

---
 Source/Plugins/NWBFormat/NWBFormat.cpp    | 25 +++++++++++++++++++----
 Source/Plugins/NWBFormat/NWBFormat.h      |  5 +++--
 Source/Plugins/NWBFormat/NWBRecording.cpp | 10 ++++++++-
 Source/Plugins/NWBFormat/NWBRecording.h   |  3 +++
 4 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/Source/Plugins/NWBFormat/NWBFormat.cpp b/Source/Plugins/NWBFormat/NWBFormat.cpp
index d650c930d..8b1047fa4 100644
--- a/Source/Plugins/NWBFormat/NWBFormat.cpp
+++ b/Source/Plugins/NWBFormat/NWBFormat.cpp
@@ -36,7 +36,7 @@
 #define SPIKE_CHUNK_YSIZE 40
 #endif
 
- NWBFile::NWBFile(String fName, String ver) : HDF5FileBase(), filename(fName), GUIVersion(ver), spikeMaxSize(0)
+ NWBFile::NWBFile(String fName, String ver, String idText) : HDF5FileBase(), filename(fName), identifierText(idText), GUIVersion(ver), spikeMaxSize(0)
  {
 	 //Init stuff
 	 readyToOpen=true; //In KWIK this is in initFile, but the new recordEngine methods make it safe for it to be here
@@ -76,6 +76,13 @@ int NWBFile::createFileStructure()
 	CHECK_ERROR(setAttributeStr(*xmlText, "/general/data_collection", "configuration"));
 	
 	//TODO: Add default datasets
+	//Modify this one once we have JUCE4 to allow UTC time 
+	String time = Time::getCurrentTime().formatted("%Y-%m-%dT%H:%M:%S");
+	createTextDataSet("", "file_create_date", time);
+	createTextDataSet("", "identifier", identifierText);
+	createTextDataSet("", "nwb_version", "NWB-1.0.4_beta");
+	createTextDataSet("", "session_description", " ");
+	createTextDataSet("", "session_start_time", time);
 	
 	return 0;
 }
@@ -298,8 +305,8 @@ int NWBFile::createFileStructure()
  {
 	 return filename;
  }
- 
- HDF5RecordingData* NWBFile::createRecordingStructures(String basePath, NWBRecordingInfo& info, String helpText, int chunk_size, String ancestry)
+
+  HDF5RecordingData* NWBFile::createRecordingStructures(String basePath, NWBRecordingInfo& info, String helpText, int chunk_size, String ancestry)
  {
 	 StringArray ancestryStrings;
 	 ancestryStrings.add("TimeSeries");
@@ -322,4 +329,14 @@ int NWBFile::createFileStructure()
 	 return tsSet;
 
  }
- 
\ No newline at end of file
+ 
+  void NWBFile::createTextDataSet(String path, String name, String text)
+  {
+	  ScopedPointer<HDF5RecordingData> dSet;
+
+	  if (text.isEmpty()) text = " "; //to avoid 0-length strings, which cause errors
+
+	  dSet = createDataSet(STR, 1, 0, path + "/" + name);
+	  if (!dSet) return;
+	  dSet->writeDataBlock(1, STR, text.toUTF8());
+  }
\ No newline at end of file
diff --git a/Source/Plugins/NWBFormat/NWBFormat.h b/Source/Plugins/NWBFormat/NWBFormat.h
index e082babcb..fd3f488ec 100644
--- a/Source/Plugins/NWBFormat/NWBFormat.h
+++ b/Source/Plugins/NWBFormat/NWBFormat.h
@@ -44,7 +44,7 @@ namespace NWBRecording
 	class NWBFile : public HDF5FileBase
 	{
 	public:
-		NWBFile(String fName, String ver); //with whatever arguments it's necessary
+		NWBFile(String fName, String ver, String idText); //with whatever arguments it's necessary
 		~NWBFile();
 		bool startNewRecording(int recordingNumber, const Array<NWBRecordingInfo>& continuousArray, const Array<NWBRecordingInfo>& electrodeArray);
 		void stopRecording();
@@ -61,6 +61,7 @@ namespace NWBRecording
 
 	private:
 		HDF5RecordingData* createRecordingStructures(String basePath, NWBRecordingInfo& info, String helpText, int chunk_size, String ancestry);
+		void createTextDataSet(String path, String name, String text);
 
 		const String filename;
 		const String GUIVersion;
@@ -90,8 +91,8 @@ namespace NWBRecording
 		int spikeMaxSize;
 
 		const String* xmlText;
+		const String identifierText;
 
-		//whatever is needed
 		JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NWBFile);
 
 	};
diff --git a/Source/Plugins/NWBFormat/NWBRecording.cpp b/Source/Plugins/NWBFormat/NWBRecording.cpp
index 64497e99a..856189570 100644
--- a/Source/Plugins/NWBFormat/NWBRecording.cpp
+++ b/Source/Plugins/NWBFormat/NWBRecording.cpp
@@ -48,7 +48,7 @@
 	 //Called when acquisition starts, to open the files
 	 String basepath = rootFolder.getFullPathName() + rootFolder.separatorString + "experiment_" + String(experimentNumber) + ".nwb";
 	 
-	 recordFile = new NWBFile(basepath, CoreServices::getGUIVersion());
+	 recordFile = new NWBFile(basepath, CoreServices::getGUIVersion(), identifierText);
 	 recordFile->setXmlText(getLatestSettingsXml());
 
 	 int recProcs = getNumRecordedProcessors();
@@ -217,6 +217,14 @@ RecordEngineManager* NWBRecordEngine::getEngineManager()
 {
 	//static factory that instantiates the engine manager, which allows to configure recording options among other things. See OriginalRecording to see how to create options for a record engine
 	RecordEngineManager* man = new RecordEngineManager("NWB", "NWB", &(engineFactory<NWBRecordEngine>));
+	EngineParameter* param;
+	param = new EngineParameter(EngineParameter::STR, 0, "Identifier Text", String::empty);
+	man->addParameter(param);
 	return man;
 	
+}
+
+void NWBRecordEngine::setParameter(EngineParameter& parameter)
+{
+	strParameter(0, identifierText);
 }
\ No newline at end of file
diff --git a/Source/Plugins/NWBFormat/NWBRecording.h b/Source/Plugins/NWBFormat/NWBRecording.h
index 2c218d7b0..2364d8683 100644
--- a/Source/Plugins/NWBFormat/NWBRecording.h
+++ b/Source/Plugins/NWBFormat/NWBRecording.h
@@ -43,6 +43,7 @@
 			void addSpikeElectrode(int index,const  SpikeRecordInfo* elec) override;
 			void writeSpike(int electrodeIndex, const SpikeObject& spike, int64 timestamp) override;
 			void resetChannels() override;
+			void setParameter(EngineParameter& parameter) override;
 			
 			static RecordEngineManager* getEngineManager();
 			
@@ -61,6 +62,8 @@
 			HeapBlock<int16> intBuffer;
 			HeapBlock<double> tsBuffer;
 			int bufferSize;
+
+			String identifierText;
 			
 			JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NWBRecordEngine);
 
-- 
GitLab