Skip to content
Snippets Groups Projects
LoadRawHelper.cpp 52.1 KiB
Newer Older
#include "MantidDataHandling/LoadRawHelper.h"
#include "LoadRaw/isisraw2.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/SpectrumDetectorMapping.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/Glob.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidDataHandling/LoadAscii.h"
#include "MantidDataHandling/RawFileInfo.h"
Campbell, Stuart's avatar
Campbell, Stuart committed

#include <boost/algorithm/string/predicate.hpp>
Campbell, Stuart's avatar
Campbell, Stuart committed
#include <boost/date_time/gregorian/gregorian.hpp>
Campbell, Stuart's avatar
Campbell, Stuart committed
#include <Poco/Path.h>
#include <Poco/DirectoryIterator.h>
Campbell, Stuart's avatar
Campbell, Stuart committed
#include <Poco/DateTimeParser.h>
#include <Poco/DateTimeFormat.h>
#include <cmath>
#include <cstdio> //Required for gcc 4.4

namespace Mantid {
namespace DataHandling {

using namespace Kernel;
using namespace API;

/// Constructor
LoadRawHelper::LoadRawHelper()
    : isisRaw(new ISISRAW2), m_list(false), m_interval(false), m_spec_list(),
      m_spec_min(0), m_spec_max(EMPTY_INT()), m_numberOfPeriods(0),
      m_cache_options(), m_specTimeRegimes(), m_prog(0.0), m_numberOfSpectra(0),
      m_monitordetectorList(), m_bmspeclist(false), m_total_specs(0),
      m_logCreator() {}

/// Initialisation method.
void LoadRawHelper::init() {
  const std::vector<std::string> exts{".raw", ".s*", ".add"};
  declareProperty(Kernel::make_unique<FileProperty>("Filename", "",
                                                    FileProperty::Load, exts),
                  "The name of the RAW file to read, including its full or "
                  "relative path. The file extension must be .raw or .RAW "
                  "(N.B. case sensitive if running on Linux).");
  declareProperty(make_unique<WorkspaceProperty<Workspace>>(
                      "OutputWorkspace", "", Direction::Output),
                  "The name of the workspace that will be created, filled with "
                  "the read-in data and stored in the Analysis Data Service. "
                  "If the input RAW file contains multiple periods higher "
                  "periods will be stored in separate workspaces called "
                  "OutputWorkspace_PeriodNo.");

  m_cache_options.emplace_back("If Slow");
  m_cache_options.emplace_back("Always");
  m_cache_options.emplace_back("Never");
  declareProperty("Cache", "If Slow",
                  boost::make_shared<StringListValidator>(m_cache_options),
                  "An option allowing the algorithm to cache a remote file on "
                  "the local drive before loading. When \"If Slow\" is set the "
                  "download speed is estimated and if is deemed as slow the "
                  "file is cached. \"Always\" means always cache a remote file "
                  "and \"Never\" - never cache.");

  declareProperty("LoadLogFiles", true,
                  "Boolean option to load or skip log files. If this option is "
                  "set all the log files associated with the selected raw file "
                  "are loaded into workspace and can be displayed using right "
                  "click  menu item Sample Logs...on the selected "
                  "workspace.\nNote: If the log files contain motor positions, "
                  "etc. that would affect the instrument geometry this option "
                  "must be set to true for these adjustments to be applied to "
                  "the instrument geometry.");
}
/**opens the raw file and returns the file pointer
 *@param fileName :: name of the raw file
 *@return file pointer
 */
FILE *LoadRawHelper::openRawFile(const std::string &fileName) {
  FILE *file = fopen(fileName.c_str(), "rb");
    g_log.error("Unable to open file " + fileName);
    throw Exception::FileError("Unable to open File:", fileName);
  }
  // Need to check that the file is not a text file as the ISISRAW routines
  // don't deal with these very well, i.e
  // reading continues until a bad_alloc is encountered.
  if (isAscii(file)) {
    g_log.error() << "File \"" << fileName << "\" is not a valid RAW file.\n";
    fclose(file);
    throw std::invalid_argument("Incorrect file type encountered.");
  }

  return file;
}
/** Reads the run title and creates a string from it
 * @param file :: pointer to the raw file
 * @param title ::  An output parameter that will contain the workspace title
 */
void LoadRawHelper::readTitle(FILE *file, std::string &title) {
  ioRaw(file, true);
  title = std::string(isisRaw->r_title, 80);
  g_log.information("*** Run title: " + title + " ***");
}
/**skips the histogram from raw file
 *@param file :: pointer to the raw file
 *@param hist :: postion in the file to skip
 */
void LoadRawHelper::skipData(FILE *file, int hist) {
  isisRaw->skipData(file, hist);
}
void LoadRawHelper::skipData(FILE *file, int64_t hist) {
  skipData(file, static_cast<int>(hist));
}
/// calls isisRaw ioRaw.
/// @param file :: the file pointer
/// @param from_file :: unknown
void LoadRawHelper::ioRaw(FILE *file, bool from_file) {
  isisRaw->ioRAW(file, from_file);
}
int LoadRawHelper::getNumberofTimeRegimes() { return isisRaw->daep.n_tr_shift; }

void LoadRawHelper::reset() { isisRaw.reset(); }

/**reads the histogram from raw file
 * @param file :: pointer to the raw file
 * @param hist :: postion in the file to read
 * @return flag is data is read
 */
bool LoadRawHelper::readData(FILE *file, int hist) {
  return isisRaw->readData(file, hist);
}
bool LoadRawHelper::readData(FILE *file, int64_t hist) {
  return readData(file, static_cast<int>(hist));
}

float LoadRawHelper::getProtonCharge() const {
  return isisRaw->rpb.r_gd_prtn_chrg;
}

/**
 * Set the proton charge on the run object
 * @param run :: The run object
 */
void LoadRawHelper::setProtonCharge(API::Run &run) {
  run.setProtonCharge(getProtonCharge());
}
/** Stores the run number in the run logs
 *  @param run :: the workspace's run object
 */
void LoadRawHelper::setRunNumber(API::Run &run) {
  std::string run_num = std::to_string(isisRaw->r_number);
  run.addLogData(new PropertyWithValue<std::string>("run_number", run_num));
}
/**reads workspace dimensions,number of periods etc from raw data
 * @param numberOfSpectra :: number of spectra
 * @param numberOfPeriods :: number of periods
 * @param lengthIn :: size of workspace vectors
 * @param noTimeRegimes :: number of time regime.
 */
void LoadRawHelper::readworkspaceParameters(specnum_t &numberOfSpectra,
                                            int &numberOfPeriods,
                                            int64_t &lengthIn,
                                            int64_t &noTimeRegimes) {
  // Read in the number of spectra in the RAW file
  m_numberOfSpectra = numberOfSpectra = static_cast<specnum_t>(isisRaw->t_nsp1);
  // Read the number of periods in this file
  numberOfPeriods = isisRaw->t_nper;
  // Read the number of time channels (i.e. bins) from the RAW file
  const int64_t channelsPerSpectrum = isisRaw->t_ntc1;
  // Read in the time bin boundaries
  lengthIn = channelsPerSpectrum + 1;
  // Now check whether there is more than one time regime in use
  noTimeRegimes = isisRaw->daep.n_tr_shift;
}
/**This method creates shared pointer to a workspace
 * @param ws_sptr :: shared pointer to the parent workspace
 * @param nVectors :: number of histograms in the workspace
 * @param xLengthIn :: size of workspace X vector
 * @param yLengthIn :: size of workspace Y vector
 * @return an empty workspace of the given parameters
 */
DataObjects::Workspace2D_sptr
LoadRawHelper::createWorkspace(DataObjects::Workspace2D_sptr ws_sptr,
                               int64_t nVectors, int64_t xLengthIn,
                               int64_t yLengthIn) {
  DataObjects::Workspace2D_sptr empty;
  if (!ws_sptr)
    return empty;
  DataObjects::Workspace2D_sptr workspace =
      boost::dynamic_pointer_cast<DataObjects::Workspace2D>(
Loading
Loading full blame...