Ejemplo n.º 1
0
void LoadFlexiNexus::load2DWorkspace(NeXus::File *fin) {
  Mantid::DataObjects::Workspace2D_sptr ws;
  int nSpectra, spectraLength;
  std::vector<int> data;

  // read the data first
  fin->getDataCoerce(data);

  Info inf = fin->getInfo();
  if (inf.dims.size() == 1) {
    nSpectra = 1;
    spectraLength = static_cast<int>(inf.dims[0]);
  } else {
    nSpectra = static_cast<int>(inf.dims[0]);
    spectraLength = static_cast<int>(inf.dims[1]);
  }

  g_log.debug() << "Reading " << nSpectra << " spectra of length "
                << spectraLength << "." << std::endl;

  // need to locate x-axis data too.....
  std::map<std::string, std::string>::const_iterator it;
  std::vector<double> xData;
  if ((it = dictionary.find("x-axis")) == dictionary.end()) {
    xData.resize(spectraLength);
    for (int i = 0; i < spectraLength; i++) {
      xData[i] = (double)i;
    }
  } else {
    if (safeOpenpath(fin, it->second)) {
      fin->getDataCoerce(xData);
    }
  }

  // need to locate y-axis data too.....
  std::vector<double> yData;
  if ((it = dictionary.find("y-axis")) == dictionary.end()) {
    yData.resize(nSpectra);
    for (int i = 0; i < nSpectra; i++) {
      yData[i] = (double)i;
    }
  } else {
    if (safeOpenpath(fin, it->second)) {
      fin->getDataCoerce(yData);
    }
  }

  // fill the data.......
  ws = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>(
      WorkspaceFactory::Instance().create("Workspace2D", nSpectra,
                                          spectraLength, spectraLength));
  for (int wsIndex = 0; wsIndex < nSpectra; wsIndex++) {
    Mantid::MantidVec &Y = ws->dataY(wsIndex);
    for (int j = 0; j < spectraLength; j++) {
      Y[j] = data[spectraLength * wsIndex + j];
    }
    // Create and fill another vector for the errors, containing sqrt(count)
    Mantid::MantidVec &E = ws->dataE(wsIndex);
    std::transform(Y.begin(), Y.end(), E.begin(), dblSqrt);
    ws->setX(wsIndex, xData);
    // Xtof		ws->getAxis(1)->spectraNo(i)= i;
    ws->getSpectrum(wsIndex)
        ->setSpectrumNo(static_cast<specid_t>(yData[wsIndex]));
    ws->getSpectrum(wsIndex)
        ->setDetectorID(static_cast<detid_t>(yData[wsIndex]));
  }

  ws->setYUnit("Counts");

  // assign an x-axis-name
  if ((it = dictionary.find("x-axis-name")) == dictionary.end()) {
    const std::string xname("no axis name found");
    ws->getAxis(0)->title() = xname;
  } else {
    const std::string xname(it->second);
    ws->getAxis(0)->title() = xname;
    if (xname.compare("TOF") == 0) {
      g_log.debug() << "Setting X-unit to be TOF" << std::endl;
      ws->getAxis(0)->setUnit("TOF");
    }
  }

  addMetaData(fin, ws, (ExperimentInfo_sptr)ws);

  // assign the workspace
  setProperty("OutputWorkspace", boost::dynamic_pointer_cast<Workspace>(ws));
}