diff --git a/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp b/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp index a2efab2ccb89b2621614eb7fa734d1171865fcac..ab67097f2a700142fbca61a91eef78f5b263f802 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp @@ -874,6 +874,13 @@ API::Workspace_sptr LoadNexusProcessed::loadEntry(NXRoot & root, const std::stri try { local_workspace->getAxis(0)->unit() = UnitFactory::Instance().create(unit1); + if(unit1 == "Label") + { + auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(local_workspace->getAxis(0)->unit()); + auto ax = wksp_cls.openNXDouble("axis1"); + label->setLabel(ax.attributes("caption"), ax.attributes("label")); + } + //If this doesn't throw then it is a numeric access so grab the data so we can set it later axis2.load(); m_axis1vals = MantidVec(axis2(), axis2() + axis2.dim0()); @@ -896,6 +903,12 @@ API::Workspace_sptr LoadNexusProcessed::loadEntry(NXRoot & root, const std::stri Mantid::API::NumericAxis* newAxis = new Mantid::API::NumericAxis(nspectra); local_workspace->replaceAxis(1, newAxis); newAxis->unit() = UnitFactory::Instance().create(unit2); + if(unit2 == "Label") + { + auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(newAxis->unit()); + auto ax = wksp_cls.openNXDouble("axis2"); + label->setLabel(ax.attributes("caption"), ax.attributes("label")); + } } catch( std::runtime_error & ) { diff --git a/Code/Mantid/Framework/DataHandling/test/SaveNexusProcessedTest.h b/Code/Mantid/Framework/DataHandling/test/SaveNexusProcessedTest.h index 23d0c8416ae8fa4522438d9d6d20a237393ebdcd..df78013564aff2957173756150e052f6521f6a02 100644 --- a/Code/Mantid/Framework/DataHandling/test/SaveNexusProcessedTest.h +++ b/Code/Mantid/Framework/DataHandling/test/SaveNexusProcessedTest.h @@ -374,6 +374,56 @@ public: if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove(); } + void testExecSaveLabel() + { + SaveNexusProcessed alg; + if ( !alg.isInitialized() ) alg.initialize(); + + // create dummy 2D-workspace + Workspace2D_sptr localWorkspace2D = boost::dynamic_pointer_cast<Workspace2D> + (WorkspaceFactory::Instance().create("Workspace2D",1,10,10)); + + //set units to be a label + localWorkspace2D->getAxis(0)->unit() = UnitFactory::Instance().create("Label"); + auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(localWorkspace2D->getAxis(0)->unit()); + label->setLabel("Temperature","K"); + + double d = 0.0; + for(int i = 0; i<10; ++i,d+=0.1) + { + localWorkspace2D->dataX(0)[i] = d; + localWorkspace2D->dataY(0)[i] = d; + localWorkspace2D->dataE(0)[i] = d; + } + + AnalysisDataService::Instance().addOrReplace("testSpace", localWorkspace2D); + + // Now set it... + // specify name of file to save workspace to + alg.setPropertyValue("InputWorkspace", "testSpace"); + outputFile = "SaveNexusProcessedTest_testExec.nxs"; + //entryName = "test"; + dataName = "spectra"; + title = "A simple workspace saved in Processed Nexus format"; + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Filename", outputFile)); + outputFile = alg.getPropertyValue("Filename"); + //alg.setPropertyValue("EntryName", entryName); + alg.setPropertyValue("Title", title); + if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove(); + + std::string result; + TS_ASSERT_THROWS_NOTHING( result = alg.getPropertyValue("Filename") ); + TS_ASSERT( ! result.compare(outputFile)); + + // changed so that 1D workspaces are no longer written. + TS_ASSERT_THROWS_NOTHING(alg.execute()); + TS_ASSERT( alg.isExecuted() ); + + if(clearfiles) Poco::File(outputFile).remove(); + + AnalysisDataService::Instance().remove("testSpace"); + } + private: diff --git a/Code/Mantid/Framework/Nexus/src/NexusFileIO.cpp b/Code/Mantid/Framework/Nexus/src/NexusFileIO.cpp index 493483c137a4a8a6045416d1cde347d5fce70dce..dd80822844f031c12d2770eeadf69c8a875a14e0 100644 --- a/Code/Mantid/Framework/Nexus/src/NexusFileIO.cpp +++ b/Code/Mantid/Framework/Nexus/src/NexusFileIO.cpp @@ -13,6 +13,7 @@ #include "MantidKernel/TimeSeriesProperty.h" #include "MantidNexus/NexusFileIO.h" #include "MantidDataObjects/Workspace2D.h" +#include "MantidKernel/Unit.h" #include "MantidKernel/UnitFactory.h" #include "MantidKernel/DateAndTime.h" #include "MantidKernel/ConfigService.h" @@ -384,6 +385,14 @@ using namespace DataObjects; std::string dist=(localworkspace->isDistribution()) ? "1" : "0"; NXputattr(fileID, "distribution", reinterpret_cast<void*>(const_cast<char*>(dist.c_str())), 2, NX_CHAR); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>(xLabel.c_str())), static_cast<int>(xLabel.size()), NX_CHAR); + + auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(xAxis->unit()); + if(label) + { + NXputattr (fileID, "caption", reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR); + NXputattr (fileID, "label", reinterpret_cast<void*>(const_cast<char*>(label->label().c_str())), static_cast<int>(label->label().size()), NX_CHAR); + } + NXclosedata(fileID); if ( ! sAxis->isText() ) @@ -394,6 +403,14 @@ using namespace DataObjects; NXopendata(fileID, "axis2"); NXputdata(fileID, (void*)&(axis2[0])); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>(sLabel.c_str())), static_cast<int>(sLabel.size()), NX_CHAR); + + auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(sAxis->unit()); + if(label) + { + NXputattr (fileID, "caption", reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR); + NXputattr (fileID, "label", reinterpret_cast<void*>(const_cast<char*>(label->label().c_str())), static_cast<int>(label->label().size()), NX_CHAR); + } + NXclosedata(fileID); } else @@ -409,6 +426,14 @@ using namespace DataObjects; NXopendata(fileID, "axis2"); NXputdata(fileID, reinterpret_cast<void*>(const_cast<char*>(textAxis.c_str()))); NXputattr (fileID, "units", reinterpret_cast<void*>(const_cast<char*>("TextAxis")), 8, NX_CHAR); + + auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(sAxis->unit()); + if(label) + { + NXputattr (fileID, "caption", reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR); + NXputattr (fileID, "label", reinterpret_cast<void*>(const_cast<char*>(label->label().c_str())), static_cast<int>(label->label().size()), NX_CHAR); + } + NXclosedata(fileID); }