From 2becb6a1e3a3e6dae2f10ed87fdce3d6fca8377c Mon Sep 17 00:00:00 2001
From: Ronald Fowler <ronald.fowler@stfc.ac.uk>
Date: Tue, 7 Apr 2009 08:09:04 +0000
Subject: [PATCH] Updates to the Nexus reader to recognise workspace title -
 the Nexus writer uses this to label the data section, if available. Addresses
 issue reported by Aziz Daoud-Aladine. Re #325

---
 .../Nexus/inc/MantidNexus/NexusFileIO.h       |  2 +
 Code/Mantid/Nexus/src/NexusFileIO.cpp         | 64 +++++++++++++++++--
 2 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h b/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h
index 918ebdecd36..50340b20e20 100644
--- a/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h
+++ b/Code/Mantid/Nexus/inc/MantidNexus/NexusFileIO.h
@@ -126,6 +126,8 @@ namespace Mantid
       bool checkEntryAtLevel(const std::string& item) const;
       /// check if given attribute name is in currently opened entry
       bool checkAttributeName(const std::string& target) const;
+      /// Look for entry with given attribute (eg "signal")
+      bool checkEntryAtLevelByAttribute(const std::string& attribute, std::string& entry) const;
       /// write test field
       int writeNexusTextField( const NXhandle& h, const std::string& name, const std::string& value);
       /// search for exisiting MantidWorkpace_n entries in opened file
diff --git a/Code/Mantid/Nexus/src/NexusFileIO.cpp b/Code/Mantid/Nexus/src/NexusFileIO.cpp
index 53d0ee47202..65f5811d7ad 100644
--- a/Code/Mantid/Nexus/src/NexusFileIO.cpp
+++ b/Code/Mantid/Nexus/src/NexusFileIO.cpp
@@ -858,10 +858,20 @@ namespace NeXus
    status=NXopengroup(fileID,"workspace","NXdata");
    if(status==NX_ERROR)
        return(1);
-   // open "values" data
-   status=NXopendata(fileID, "values");
+   // open "values" data which is identified by attribute "signal", if it exists
+   std::string entry;
+   if(checkEntryAtLevelByAttribute("signal", entry))
+       status=NXopendata(fileID, entry.c_str());
+   else
+   {
+       status=NXclosegroup(fileID);
+       return(2);
+   }
    if(status==NX_ERROR)
+   {
+       status=NXclosegroup(fileID);
        return(2);
+   }
    // read workspace data size
    int rank,dim[2],type;
    status=NXgetinfo(fileID, &rank, dim, &type);
@@ -978,11 +988,23 @@ namespace NeXus
    status=NXopengroup(fileID,"workspace","NXdata");
    if(status==NX_ERROR)
        return(1);
-
-   // read values
-   status=NXopendata(fileID,"values");
+   std::string entry;
+   if(checkEntryAtLevelByAttribute("signal", entry))
+       status=NXopendata(fileID, entry.c_str());
+   else
+   {
+       status=NXclosegroup(fileID);
+       return(2);
+   }
    if(status==NX_ERROR)
+   {
+       status=NXclosegroup(fileID);
        return(2);
+   }
+   // read values
+   //status=NXopendata(fileID,"values");
+   //if(status==NX_ERROR)
+   //    return(2);
    status=NXgetinfo(fileID, &rank, dim, &type);
    // get buffer and block size
    double *buffer=new double[dim[1]];
@@ -1094,6 +1116,38 @@ namespace NeXus
   }
   
 
+  bool NexusFileIO::checkEntryAtLevelByAttribute(const std::string& attribute, std::string& entry) const
+  {
+   // Search the currently open level for a section with "attribute" and return entry name
+   NXstatus status;
+   char *nxname,*nxclass;
+   int nxdatatype;
+   nxname= new char[NX_MAXNAMELEN];
+   nxclass = new char[NX_MAXNAMELEN];
+   //
+   // read nexus fields at this level
+   status=NXinitgroupdir(fileID); // just in case
+   while( (status=NXgetnextentry(fileID,nxname,nxclass,&nxdatatype)) == NX_OK )
+   {
+      std::string nxName=nxname;
+      status=NXopendata(fileID,nxname);
+      if(checkAttributeName("signal"))
+      {
+         entry=nxname;
+         delete[] nxname;
+         delete[] nxclass;
+         status=NXclosedata(fileID);
+         return(true);
+      }
+      status=NXclosedata(fileID);
+   }
+   delete[] nxname;
+   delete[] nxclass;
+   return(false);
+
+  }
+  
+
   bool NexusFileIO::writeNexusProcessedSpectraMap(const API::SpectraDetectorMap& spectraMap,
                     const int& m_spec_min, const int& m_spec_max)
   {
-- 
GitLab