Skip to content
Snippets Groups Projects
LoadNexusProcessed.cpp 54.2 KiB
Newer Older


The algorithm LoadNexusProcessed will read a Nexus data file created by [[SaveNexusProcessed]] and place the data
into the named workspace.
The file name can be an absolute or relative path and should have the extension
.nxs, .nx5 or .xml.
Warning - using XML format can be extremely slow for large data sets and generate very large files.
The optional parameters can be used to control which spectra are loaded into the workspace (not yet implemented).
If spectrum_min and spectrum_max are given, then only that range to data will be loaded.

A Mantid Nexus file may contain several workspace entries each labelled with an integer starting at 1.
By default the highest number workspace is read, earlier ones can be accessed by setting the EntryNumber.

If the saved data has a reference to an XML file defining instrument geometry this will be read.


===Time series data===
The log data in the Nexus file (NX_LOG sections) is loaded as TimeSeriesProperty data within the workspace.
Time is stored as seconds from the Unix epoch.
Only floating point logs are stored and loaded at present.

===Subalgorithms used===

The subalgorithms used by LoadMuonNexus are:
* LoadInstrument - this algorithm looks for an XML description of the instrument and if found reads it.





*WIKI*/
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraDetectorMap.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidDataHandling/LoadNexusProcessed.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/RebinnedOutput.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidNexus/NexusClasses.h"
#include "MantidNexus/NexusFileIO.h"
#include "MantidNexusCPP/NeXusFile.hpp"
#include <boost/shared_ptr.hpp>
#include <cmath>
#include <Poco/DateTimeParser.h>
#include <Poco/Path.h>
#include <Poco/StringTokenizer.h>
#include "MantidDataObjects/PeaksWorkspace.h"

namespace Mantid
{
namespace DataHandling
{

// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadNexusProcessed)
DECLARE_LOADALGORITHM(LoadNexusProcessed)

/// Sets documentation strings for this algorithm
void LoadNexusProcessed::initDocs()
{
  this->setWikiSummary("The LoadNexusProcessed algorithm will read the given Nexus Processed data file containing a Mantid Workspace. The data is placed in the named workspace. LoadNexusProcessed may be invoked by [[LoadNexus]] if it is given a Nexus file of this type. ");
  this->setOptionalMessage("The LoadNexusProcessed algorithm will read the given Nexus Processed data file containing a Mantid Workspace. The data is placed in the named workspace. LoadNexusProcessed may be invoked by LoadNexus if it is given a Nexus file of this type.");
}

using namespace Mantid::NeXus;
using namespace DataObjects;
using namespace Kernel;
using namespace API;
using Geometry::Instrument_const_sptr;

/// Default constructor
LoadNexusProcessed::LoadNexusProcessed() : m_shared_bins(false), m_xbins(),
    m_axis1vals(), m_list(false), m_interval(false),
    m_spec_list(), m_spec_min(0), m_spec_max(Mantid::EMPTY_INT()),m_cppFile(NULL)
{
}

/// Delete NexusFileIO in destructor
LoadNexusProcessed::~LoadNexusProcessed()
{
}

/** Initialisation method.
 *
 */
void LoadNexusProcessed::init()
{
  // Declare required input parameters for algorithm
  std::vector<std::string> exts;
  exts.push_back(".nxs");
  exts.push_back(".nx5");
  exts.push_back(".xml");
  declareProperty(new FileProperty("Filename", "", FileProperty::Load, exts),
      "The name of the processed Nexus file to load" );
  declareProperty(new WorkspaceProperty<Workspace> ("OutputWorkspace", "",
      Direction::Output),
      "The name of the workspace to be created as the output of the\n"
      "algorithm. For multiperiod files, one workspace may be\n"
      "generated for each period. Currently only one workspace can\n"
      "be saved at a time so multiperiod Mantid files are not\n"
      "generated");


  // optional
  auto mustBePositive = boost::make_shared<BoundedValidator<int64_t> >();
  mustBePositive->setLower(0);

  declareProperty("SpectrumMin", (int64_t)1, mustBePositive,
      "Index number of the first spectrum to read, only used if\n"
      "spectrum_max is set and only for single period data, not\n"
      " yet implemented (default 0)");
  declareProperty("SpectrumMax", (int64_t)Mantid::EMPTY_INT(), mustBePositive,
      "Index of last spectrum to read, only for single period data,\n"
      " not yet implemented (default the last spectrum).");
  declareProperty(new ArrayProperty<int64_t> ("SpectrumList"),
      "Array, or comma separated list, of indexes of spectra to\n"
      "load. Not yet implemented.");
  declareProperty("EntryNumber", (int64_t)0, mustBePositive,
      "The particular entry number to read (default: read all entries)" );
}


//-------------------------------------------------------------------------------------------------
/** Executes the algorithm. Reading in the file and creating and populating
 *  the output workspace
 *
 *  @throw runtime_error Thrown if algorithm cannot execute
 */
void LoadNexusProcessed::exec()
{
  progress(0,"Opening file...");

  //Throws an approriate exception if there is a problem with file access
  NXRoot root(getPropertyValue("Filename"));

  // "Open" the same file but with the C++ interface
  m_cppFile = new ::NeXus::File(root.m_fileID);

  //Find out how many first level entries there are
  int64_t nperiods = static_cast<int64_t>(root.groups().size());

  // Check for an entry number property
  int64_t entrynumber = static_cast<int64_t>(getProperty("EntryNumber"));

  if( entrynumber > 0 && entrynumber > nperiods )
  {
    g_log.error() << "Invalid entry number specified. File only contains " << nperiods << " entries.\n";
    throw std::invalid_argument("Invalid entry number specified.");
  }

  const std::string basename = "mantid_workspace_";
  if( nperiods == 1 || entrynumber > 0 )
  {  // Load one first level entry, specified if there are several
    if( entrynumber == 0 ) ++entrynumber;
    std::ostringstream os;
    os << entrynumber;
    API::Workspace_sptr workspace = loadEntry(root, basename + os.str(), 0, 1);
    //API::Workspace_sptr workspace = boost::static_pointer_cast<API::Workspace>(local_workspace);
    setProperty("OutputWorkspace", workspace);
  }
  else
  {  // Load all first level entries
    WorkspaceGroup_sptr wksp_group(new WorkspaceGroup);
    //This forms the name of the group
    std::string base_name = getPropertyValue("OutputWorkspace");
    // First member of group should be the group itself, for some reason!
    base_name += "_";
    const std::string prop_name = "OutputWorkspace_";
    double nperiods_d = static_cast<double>(nperiods);
    for( int64_t p = 1; p <= nperiods; ++p )
    {
      std::ostringstream os;
      os << p;
      Workspace_sptr local_workspace = loadEntry(root, basename + os.str(), static_cast<double>(p-1)/nperiods_d, 1./nperiods_d);
      declareProperty(new WorkspaceProperty<API::Workspace>(prop_name + os.str(), base_name + os.str(),
          Direction::Output));
      //wksp_group->add(base_name + os.str());
      wksp_group->addWorkspace(local_workspace);
      setProperty(prop_name + os.str(), local_workspace);
    }

    // The group is the root property value
    setProperty("OutputWorkspace", boost::static_pointer_cast<Workspace>(wksp_group));

  }

  m_axis1vals.clear();
}



//-------------------------------------------------------------------------------------------------
/** Load the event_workspace field
 *
 * @param wksp_cls
 * @param xbins
 * @param progressStart
 * @param progressRange
 * @return
 */
API::MatrixWorkspace_sptr LoadNexusProcessed::loadEventEntry(NXData & wksp_cls, NXDouble & xbins,
    const double& progressStart, const double& progressRange)
{
  NXDataSetTyped<int64_t> indices_data = wksp_cls.openNXDataSet<int64_t>("indices");
  indices_data.load();
  boost::shared_array<int64_t> indices = indices_data.sharedBuffer();
  int numspec = indices_data.dim0()-1;

  int num_xbins = xbins.dim0();
  if (num_xbins < 2) num_xbins = 2;
  EventWorkspace_sptr ws = boost::dynamic_pointer_cast<EventWorkspace>
  (WorkspaceFactory::Instance().create("EventWorkspace", numspec, num_xbins, num_xbins-1));

  // Set the YUnit label
  ws->setYUnit(indices_data.attributes("units"));
  std::string unitLabel = indices_data.attributes("unit_label");
  if (unitLabel.empty()) unitLabel = indices_data.attributes("units");
  ws->setYUnitLabel(unitLabel);

  //Handle optional fields.
  // TODO: Handle inconsistent sizes
  boost::shared_array<int64_t> pulsetimes;
  if (wksp_cls.isValid("pulsetime"))
  {
    NXDataSetTyped<int64_t> pulsetime = wksp_cls.openNXDataSet<int64_t>("pulsetime");
    pulsetime.load();
    pulsetimes = pulsetime.sharedBuffer();
  }

  boost::shared_array<double> tofs;
  if (wksp_cls.isValid("tof"))
  {
    NXDouble tof = wksp_cls.openNXDouble("tof");
    tof.load();
    tofs = tof.sharedBuffer();
  }

  boost::shared_array<float> error_squareds;
  if (wksp_cls.isValid("error_squared"))
  {
    NXFloat error_squared = wksp_cls.openNXFloat("error_squared");
    error_squared.load();
    error_squareds = error_squared.sharedBuffer();
  }

  boost::shared_array<float> weights;
  if (wksp_cls.isValid("weight"))
  {
    NXFloat weight = wksp_cls.openNXFloat("weight");
    weight.load();
    weights = weight.sharedBuffer();
  }

  // What type of event lists?
  EventType type = TOF;
  if (tofs && pulsetimes && weights && error_squareds)
    type = WEIGHTED;
  else if ((tofs && weights && error_squareds))
    type = WEIGHTED_NOTIME;
  else if (pulsetimes && tofs)
    type = TOF;
  else
    throw std::runtime_error("Could not figure out the type of event list!");

  // Create all the event lists
  PARALLEL_FOR_NO_WSP_CHECK()
  for (int wi=0; wi < numspec; wi++)
  {
    PARALLEL_START_INTERUPT_REGION
    int64_t index_start = indices[wi];
    int64_t index_end = indices[wi+1];
    if (index_end >= index_start)
    {
      EventList & el = ws->getEventList(wi);
      el.switchTo(type);

      // Allocate all the required memory
      el.reserve(index_end - index_start);
      el.clearDetectorIDs();

      for (int64_t i=index_start; i<index_end; i++)
      switch (type)
      {
      case TOF:
        el.addEventQuickly( TofEvent( tofs[i], DateAndTime(pulsetimes[i])) );
        break;
      case WEIGHTED:
        el.addEventQuickly( WeightedEvent( tofs[i], DateAndTime(pulsetimes[i]), weights[i], error_squareds[i]) );
        break;
      case WEIGHTED_NOTIME:
        el.addEventQuickly( WeightedEventNoTime( tofs[i], weights[i], error_squareds[i]) );
        break;
      }

      // Set the X axis
      if (this->m_shared_bins)
        el.setX(this->m_xbins);
      else
      {
        MantidVec x;
        x.resize(xbins.dim0());
        for (int i=0; i < xbins.dim0(); i++)
          x[i] = xbins(wi, i);
        el.setX(x);
      }
    }

    progress(progressStart + progressRange*(1.0/numspec));
    PARALLEL_END_INTERUPT_REGION
  }
  PARALLEL_CHECK_INTERUPT_REGION

  // Clean up some stuff
  ws->doneAddingEventLists();

  return ws;
}


//-------------------------------------------------------------------------------------------------
/**
 * Load a table
 */
API::Workspace_sptr LoadNexusProcessed::loadTableEntry(NXEntry & entry)
{
  API::ITableWorkspace_sptr workspace;
  workspace = Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace");

  NXData nx_tw = entry.openNXData("table_workspace");

  bool hasNumberOfRowBeenSet = false;
  //int numberOfRows = 0;

  int columnNumber = 1;
  do
  {
    std::string str = "column_" + boost::lexical_cast<std::string>(columnNumber);

    NXInfo info = nx_tw.getDataSetInfo(str.c_str());
    if (info.stat == NX_ERROR)
    {
	  // Assume we done last column of table
      break;
    }

    if ( info.type == NX_FLOAT64 )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      std::string columnTitle = nxDouble.attributes("name");
      if (!columnTitle.empty())
      {
        workspace->addColumn("double", columnTitle);
        nxDouble.load();
        int length = nxDouble.dim0();
        if ( !hasNumberOfRowBeenSet )
        { 
          workspace->setRowCount(length);
          hasNumberOfRowBeenSet = true;
        }
        for (int i = 0; i < length; i++)
          workspace->cell<double>(i,columnNumber-1) = *(nxDouble() + i);
      }
    }
	else if ( info.type == NX_INT32 )
    {
      NXInt nxInt = nx_tw.openNXInt(str.c_str());
      std::string columnTitle = nxInt.attributes("name");
      if (!columnTitle.empty())
      {
        workspace->addColumn("int", columnTitle);
        nxInt.load();
        int length = nxInt.dim0();
        if ( !hasNumberOfRowBeenSet )
        { 
          workspace->setRowCount(length);
          hasNumberOfRowBeenSet = true;
        }
        for (int i = 0; i < length; i++)
          workspace->cell<int>(i,columnNumber-1) = *(nxInt() + i);
      }
    }
    else if ( info.type == NX_CHAR )
    {
      NXChar data = nx_tw.openNXChar(str.c_str());
      std::string columnTitle = data.attributes("name");
      if (!columnTitle.empty())
      {
        workspace->addColumn("str", columnTitle);
        int nRows = info.dims[0];
        if ( !hasNumberOfRowBeenSet )
        {
          workspace->setRowCount(nRows);
          hasNumberOfRowBeenSet = true;
        }
        int maxStr = info.dims[1];

        std::string fromCrap(maxStr,' ');

        data.load();
        for (int iR = 0; iR < nRows; iR++)
        {
          for (int i = 0; i < maxStr; i++)
            fromCrap[i] = *(data()+i+maxStr*iR);
          workspace->cell<std::string>(iR,columnNumber-1) = fromCrap;
        }
      }
    } 

    columnNumber++;
  
  } while ( 1 );

  return boost::static_pointer_cast<API::Workspace>(workspace);
}

//-------------------------------------------------------------------------------------------------
/**
 * Load peaks
 */
API::Workspace_sptr LoadNexusProcessed::loadPeaksEntry(NXEntry & entry)
{   
	//API::IPeaksWorkspace_sptr workspace;
	API::ITableWorkspace_sptr tWorkspace;
	//PeaksWorkspace_sptr workspace;
  tWorkspace = Mantid::API::WorkspaceFactory::Instance().createTable("PeaksWorkspace");

  PeaksWorkspace_sptr peakWS = boost::dynamic_pointer_cast<PeaksWorkspace>(tWorkspace);

  NXData nx_tw = entry.openNXData("peaks_workspace");


  int columnNumber = 1;
  int numberPeaks = 0;
  std::vector<std::string> columnNames;
  do
  {
    std::string str = "column_" + boost::lexical_cast<std::string>(columnNumber);

    NXInfo info = nx_tw.getDataSetInfo(str.c_str());
    if (info.stat == NX_ERROR)
    {
	  // Assume we done last column of table
      break;
    }

	// store column names
	columnNames.push_back(str);


	// determine number of peaks
	// here we assume that a peaks_table has always one column of doubles
	
    if ( info.type == NX_FLOAT64 )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      std::string columnTitle = nxDouble.attributes("name");
      if (!columnTitle.empty() && numberPeaks==0)
      {
        numberPeaks = nxDouble.dim0();
      }
    }

	columnNumber++;

  } while ( 1 );


  //Get information from all but data group
  std::string parameterStr;
  // Hop to the right point
  m_cppFile->openPath(entry.path());
  try
  {
    // This loads logs, sample, and instrument.
    peakWS->loadExperimentInfoNexus(m_cppFile, parameterStr);
  }
  catch (std::exception & e)
  {
    g_log.information("Error loading Instrument section of nxs file");
    g_log.information(e.what());
  }


  // std::vector<API::IPeak*> p;
  for (int r = 0; r < numberPeaks; r++)
  {   
	  Kernel::V3D v3d;
	  v3d[2] = 1.0;
	  API::IPeak* p;
	  p = peakWS->createPeak(v3d);
	  peakWS->addPeak(*p);
  }



  for (size_t i = 0; i < columnNames.size(); i++)
    const std::string str = columnNames[i];
    if ( !str.compare("column_1") )
    {
      NXInt nxInt = nx_tw.openNXInt(str.c_str());
      nxInt.load();
      for (int r = 0; r < numberPeaks; r++) {
        int ival = nxInt[r];
        if( ival != -1) peakWS->getPeak(r).setDetectorID( ival );
    if ( !str.compare("column_2") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setH( val );
    if ( !str.compare("column_3") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setK( val );
    if ( !str.compare("column_4") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setL( val );
    if ( !str.compare("column_5") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setIntensity( val );
    if ( !str.compare("column_6") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setSigmaIntensity( val );
    if ( !str.compare("column_7") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setBinCount( val );
    if ( !str.compare("column_10") )
    {
      NXDouble nxDouble = nx_tw.openNXDouble(str.c_str());
      nxDouble.load();
      for (int r = 0; r < numberPeaks; r++) {
        double val = nxDouble[r];
        peakWS->getPeak(r).setWavelength( val );
    if ( !str.compare("column_14") )
    {
      NXInt nxInt = nx_tw.openNXInt(str.c_str());
      nxInt.load();
      for (int r = 0; r < numberPeaks; r++) {
        int ival = nxInt[r];
        if( ival != -1) peakWS->getPeak(r).setRunNumber( ival );
  return boost::static_pointer_cast<API::Workspace>(peakWS);

//-------------------------------------------------------------------------------------------------
/**
 * Load a single entry into a workspace
 * @param root :: The opened root node
 * @param entry_name :: The entry name
 * @param progressStart :: The percentage value to start the progress reporting for this entry
 * @param progressRange :: The percentage range that the progress reporting should cover
 * @returns A 2D workspace containing the loaded data
 */
API::Workspace_sptr LoadNexusProcessed::loadEntry(NXRoot & root, const std::string & entry_name,
    const double& progressStart, const double& progressRange)
{
  progress(progressStart,"Opening entry " + entry_name + "...");

  NXEntry mtd_entry = root.openEntry(entry_name);

  if (mtd_entry.containsGroup("table_workspace"))
  {
    return loadTableEntry(mtd_entry);
  } 

  if (mtd_entry.containsGroup("peaks_workspace"))
  {
    return loadPeaksEntry(mtd_entry);
  // Determine workspace type and name of group containing workspace characteristics
  std::string workspaceType = "Workspace2D";
  std::string group_name = "workspace";
  if (mtd_entry.containsGroup("event_workspace"))
  {
    isEvent = true;
    group_name = "event_workspace";
  }
  else if (mtd_entry.containsGroup("offsets_workspace"))
  {
    workspaceType = "OffsetsWorkspace";
    group_name = "offsets_workspace";
  }

  // Get workspace characteristics
  NXData wksp_cls = mtd_entry.openNXData(group_name);

  // Axis information
  // "X" axis
  NXDouble xbins = wksp_cls.openNXDouble("axis1");
  xbins.load();
  std::string unit1 = xbins.attributes("units");
  // Non-uniform x bins get saved as a 2D 'axis1' dataset
  int xlength(-1);
  if( xbins.rank() == 2 )
  {
    xlength = xbins.dim1();
    m_shared_bins = false;
  }
  else if( xbins.rank() == 1 )
  {
    xlength = xbins.dim0();
    m_shared_bins = true;
    xbins.load();
    m_xbins.access().assign(xbins(), xbins() + xlength);
  }
  else
  {
    throw std::runtime_error("Unknown axis1 dimension encountered.");
  }

  // MatrixWorkspace axis 1
  NXDouble axis2 = wksp_cls.openNXDouble("axis2");
  std::string unit2 = axis2.attributes("units");

  // The workspace being worked on
  API::MatrixWorkspace_sptr local_workspace;
  size_t nspectra;
  int64_t nchannels;

  // -------- Process as event ? --------------------
  if (isEvent)
  {
    local_workspace = loadEventEntry(wksp_cls, xbins, progressStart, progressRange);
    nspectra = local_workspace->getNumberHistograms();
    nchannels = local_workspace->blocksize();
  }
  else
  {
    NXDataSetTyped<double> data = wksp_cls.openDoubleData();
    nspectra = data.dim0();
    nchannels = data.dim1();
    //// validate the optional spectrum parameters, if set
    checkOptionalProperties(nspectra);
    // Actual number of spectra in output workspace (if only a range was going to be loaded)
    size_t total_specs=calculateWorkspacesize(nspectra);

    //// Create the 2D workspace for the output
    bool hasFracArea = false;
    if (wksp_cls.isValid("frac_area"))
    {
      // frac_area entry is the signal for a RebinnedOutput workspace
      hasFracArea = true;
      workspaceType.clear();
      workspaceType = "RebinnedOutput";
    }
    local_workspace = boost::dynamic_pointer_cast<API::MatrixWorkspace>
    (WorkspaceFactory::Instance().create(workspaceType, total_specs, xlength, nchannels));
    try
    {
      local_workspace->setTitle(mtd_entry.getString("title"));
    }
    catch (std::runtime_error&)
    {
      g_log.debug() << "No title was found in the input file, " << getPropertyValue("Filename") << std::endl;
    }

    // Set the YUnit label
    local_workspace->setYUnit(data.attributes("units"));
    std::string unitLabel = data.attributes("unit_label");
    if (unitLabel.empty()) unitLabel = data.attributes("units");
    local_workspace->setYUnitLabel(unitLabel);
    
    readBinMasking(wksp_cls, local_workspace);
    NXDataSetTyped<double> errors = wksp_cls.openNXDouble("errors");
    NXDataSetTyped<double> fracarea = wksp_cls.openNXDouble("errors");
    if (hasFracArea)
    {
      fracarea = wksp_cls.openNXDouble("frac_area");
    }

    int64_t blocksize(8);
    //const int fullblocks = nspectra / blocksize;
    //size of the workspace
    int64_t fullblocks = total_specs / blocksize;
    int64_t read_stop = (fullblocks * blocksize);
    const double progressBegin = progressStart+0.25*progressRange;
    const double progressScaler = 0.75*progressRange;
    int64_t hist_index = 0;
    int64_t wsIndex=0;
    if( m_shared_bins )
    {
      //if spectrum min,max,list properties are set
      if(m_interval||m_list)
      {
        //if spectrum max,min properties are set read the data as a block(multiple of 8) and
        //then read the remaining data as finalblock
        if(m_interval)
        {
          //specs at the min-max interval
          int interval_specs=static_cast<int>(m_spec_max-m_spec_min);
          fullblocks=(interval_specs)/blocksize;
          read_stop = (fullblocks * blocksize)+m_spec_min-1;

          if(interval_specs<blocksize)
          {
            blocksize=total_specs;
            read_stop=m_spec_max-1;
          }
          hist_index=m_spec_min-1;

          for( ; hist_index < read_stop; )
          {
            progress(progressBegin+progressScaler*static_cast<double>(hist_index)/static_cast<double>(read_stop),"Reading workspace data...");
            loadBlock(data, errors, fracarea, hasFracArea, blocksize, nchannels, hist_index,wsIndex, local_workspace);
          }
          int64_t finalblock = m_spec_max-1 - read_stop;
          if( finalblock > 0 )
          {
            loadBlock(data, errors, fracarea, hasFracArea, finalblock, nchannels, hist_index,wsIndex,local_workspace);
          }
        }
        // if spectrum list property is set read each spectrum separately by setting blocksize=1
        if(m_list)
        {
          std::vector<int64_t>::iterator itr=m_spec_list.begin();
          for(;itr!=m_spec_list.end();++itr)
          {
            int64_t specIndex=(*itr)-1;
            progress(progressBegin+progressScaler*static_cast<double>(specIndex)/static_cast<double>(m_spec_list.size()),"Reading workspace data...");
            loadBlock(data, errors, fracarea, hasFracArea, static_cast<int64_t>(1), nchannels, specIndex,wsIndex, local_workspace);
          }

        }
      }
      else
      {
        for( ; hist_index < read_stop; )
        {
          progress(progressBegin+progressScaler*static_cast<double>(hist_index)/static_cast<double>(read_stop),"Reading workspace data...");
          loadBlock(data, errors, fracarea, hasFracArea, blocksize, nchannels, hist_index,wsIndex, local_workspace);
        }
        int64_t finalblock = total_specs - read_stop;
        if( finalblock > 0 )
        {
          loadBlock(data, errors, fracarea, hasFracArea, finalblock, nchannels, hist_index,wsIndex,local_workspace);
        }
      }

    }
    else
    {
      if(m_interval||m_list)
      {
        if(m_interval)
        {
          int64_t interval_specs=m_spec_max-m_spec_min;
          fullblocks=(interval_specs)/blocksize;
          read_stop = (fullblocks * blocksize)+m_spec_min-1;

          if(interval_specs<blocksize)
          {
            blocksize=interval_specs;
            read_stop=m_spec_max-1;
          }
          hist_index=m_spec_min-1;

          for( ; hist_index < read_stop; )
          {
            progress(progressBegin+progressScaler*static_cast<double>(hist_index)/static_cast<double>(read_stop),"Reading workspace data...");
            loadBlock(data, errors, fracarea, hasFracArea, xbins, blocksize, nchannels, hist_index,wsIndex,local_workspace);
          }
          int64_t finalblock = m_spec_max-1 - read_stop;
          if( finalblock > 0 )
          {
            loadBlock(data, errors, fracarea, hasFracArea, xbins, finalblock, nchannels, hist_index,wsIndex, local_workspace);
          }
        }
        //
        if(m_list)
        {
          std::vector<int64_t>::iterator itr=m_spec_list.begin();
          for(;itr!=m_spec_list.end();++itr)
          {
            int64_t specIndex=(*itr)-1;
            progress(progressBegin+progressScaler*static_cast<double>(specIndex)/static_cast<double>(read_stop),"Reading workspace data...");
            loadBlock(data, errors, fracarea, hasFracArea, xbins, 1, nchannels, specIndex,wsIndex,local_workspace);
          }

        }
      }
      else
      {
        for( ; hist_index < read_stop; )
        {
          progress(progressBegin+progressScaler*static_cast<double>(hist_index)/static_cast<double>(read_stop),"Reading workspace data...");
          loadBlock(data, errors, fracarea, hasFracArea, xbins, blocksize, nchannels, hist_index,wsIndex,local_workspace);
        }
        int64_t finalblock = total_specs - read_stop;
        if( finalblock > 0 )
        {
          loadBlock(data, errors, fracarea, hasFracArea, xbins, finalblock, nchannels, hist_index,wsIndex, local_workspace);
        }
      }
    }
  } //end of NOT an event -------------------------------



  //Units
  try
  {
    local_workspace->getAxis(0)->unit() = UnitFactory::Instance().create(unit1);
    //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());
  }
  catch( std::runtime_error & )
  {
    g_log.information() << "Axis 0 set to unitless quantity \"" << unit1 << "\"\n";
  }

  // Setting a unit onto a SpectraAxis makes no sense.
  if ( unit2 == "TextAxis" )
  {
    Mantid::API::TextAxis* newAxis = new Mantid::API::TextAxis(nspectra);
    local_workspace->replaceAxis(1, newAxis);
  }
  else if ( unit2 != "spectraNumber" )
  {
    try
    {
      Mantid::API::NumericAxis* newAxis = new Mantid::API::NumericAxis(nspectra);
      local_workspace->replaceAxis(1, newAxis);
      newAxis->unit() = UnitFactory::Instance().create(unit2);
    }
    catch( std::runtime_error & )
    {
      g_log.information() << "Axis 1 set to unitless quantity \"" << unit2 << "\"\n";
    }
  }


  //Are we a distribution
  std::string dist = xbins.attributes("distribution");
  if( dist == "1" )
  {
    local_workspace->isDistribution(true);
  }
  else
  {
    local_workspace->isDistribution(false);
  }

  //Get information from all but data group
  std::string parameterStr;

  progress(progressStart+0.05*progressRange,"Reading the sample details...");

  // Hop to the right point
  m_cppFile->openPath(mtd_entry.path());
  try
  {
    // This loads logs, sample, and instrument.
    local_workspace->loadExperimentInfoNexus(m_cppFile, parameterStr);
  }
  catch (std::exception & e)
  {
    g_log.information("Error loading Instrument section of nxs file");
    g_log.information(e.what());
  }

  // Now assign the spectra-detector map
  readInstrumentGroup(mtd_entry, local_workspace);

  // Parameter map parsing
  progress(progressStart+0.11*progressRange,"Reading the parameter maps...");
  local_workspace->readParameterMap(parameterStr);


  if ( ! local_workspace->getAxis(1)->isSpectra() )
  { // If not a spectra axis, load the axis data into the workspace. (MW 25/11/10)
    loadNonSpectraAxis(local_workspace, wksp_cls);
  }

  progress(progressStart+0.15*progressRange,"Reading the workspace history...");
  m_cppFile->openPath(mtd_entry.path());
    local_workspace->history().loadNexus(m_cppFile);
  }
  catch (std::out_of_range&)
  {
    g_log.warning() << "Error in the workspaces algorithm list, its processing history is incomplete\n";
  }

  progress(progressStart+0.2*progressRange,"Reading the workspace history...");

  return boost::static_pointer_cast<API::Workspace>(local_workspace);
}



//-------------------------------------------------------------------------------------------------
/**
 * Read the instrument group
 * @param mtd_entry :: The node for the current workspace
 * @param local_workspace :: The workspace to attach the instrument
 */
void LoadNexusProcessed::readInstrumentGroup(NXEntry & mtd_entry, API::MatrixWorkspace_sptr local_workspace)
{
  //Instrument information
  NXInstrument inst = mtd_entry.openNXInstrument("instrument");
  if ( ! inst.containsGroup("detector") )
  {
    g_log.information() << "Detector block not found. The workspace will not contain any detector information.\n";
    return;
  }

  //Populate the spectra-detector map
  NXDetector detgroup = inst.openNXDetector("detector");

  //Read necessary arrays from the file
  // Detector list contains a list of all of the detector numbers. If it not present then we can't update the spectra
  // map
  int ndets(-1);
  boost::shared_array<int> det_list(NULL);
  try
  {
    NXInt detlist_group = detgroup.openNXInt("detector_list");
    ndets = detlist_group.dim0();
    detlist_group.load();
    det_list.swap(detlist_group.sharedBuffer());
  }