/**
 * Loads a tomography parameterization file into a newly created table
 * workspace. The file must have the following syntax:
 *
 * @verbatim
 <NXentry name="entry1">
   <NXprocess name="processing">
     <NXnote name="id">
       <values id="ID VALUE" params="..." name="..." cite="...">
       </values>
     </NXnote>
   </NXprocess>
 </NXentry>
@endverbatim
 *
 * @param fname name of the parameterization file
 * @param wsName name of workspace where to load the file data
 *
 * @return table workspace with parameters (plugins) found in the
 * loaded file
 */
ITableWorkspace_sptr LoadSavuTomoConfig::loadFile(std::string& fname,
                                              std::string& wsName) {
  // Throws an exception if there is a problem with file access
  //Mantid::NeXus::NXRoot root(fname);
  boost::shared_ptr<NeXus::File> f;
  if (!checkOpenFile(fname, f)) {
    throw std::runtime_error(
        "Failed to recognize this file as a NeXus file, cannot continue.");
  }

  ITableWorkspace_sptr ws =
    API::WorkspaceFactory::Instance().createTable();
  if (!ws)
    throw std::runtime_error("Could not create TableWorkspace for "
                             "workspace with name '" + wsName + "'");

  // init workspace
  ws->setTitle("Table with tomography parameters from file " +
               fname);
  ws->addColumn("str", "ID");
  ws->addColumn("str", "Parameters");
  ws->addColumn("str", "Name");
  ws->addColumn("str", "Cite");

  // a bit of file consistency check, check at least there's a
  // 'entry1'
  // it could be more strict and demand entries.size()==1
  std::map<std::string, std::string> entries = f->getEntries();
  std::string mainEntryName = "entry";
  auto it = entries.find(mainEntryName);
  if (entries.end() == it) {
    throw std::runtime_error("Could not find the '" + mainEntryName + "' "
      "entry. Even though this file looks like a valid NeXus file, it is "
      "not in the correct format for tomography reconstruction "
      "parameterization files.");
  }

  // go through the input file plugin entries
  f->openGroup(mainEntryName, "NXentry");
  f->openGroup("process", "NXprocess");
  size_t pluginsLen = f->getEntries().size();
  for (size_t j=0; j<pluginsLen; j++) {
    API::TableRow table = ws->appendRow();

    std::string entryIdx = boost::lexical_cast<std::string>(j);
    try {
      f->openGroup(entryIdx, "NXnote");
    } catch(NeXus::Exception &e) {
      // detailed NeXus error message and throw...
      g_log.error() << "Failed to load plugin '" << j << "' from"
        "NeXus file. Error description: " << e.what() << std::endl;
      throw std::runtime_error("Could not load one or more plugin "
        "entries from the tomographic reconstruction parameterization "
        "file. Please check that the file is correct.");
    }

    // TODO: check final 'schema', get these 4 fields from the file
    std::string id = "";
    std::string params = "";
    std::string name = "";
    std::string cite = "";
    try {
      f->readData("data", params);
      f->readData("id", id);
      f->readData("name", name);
      // cite not available for now
      // f->readData("cite", cite);
      // This might be extended to an NXcite group that would be included
      // not here but inside an "intermediate" NXcollection group. That
      // NXcite would have 4 arrays: description, doi, endnote, bibtex.
      // But this is what we have so far.
      cite = "Not available";
    } catch(NeXus::Exception &e) {
      // permissive, just error message but carry on
      g_log.warning() << "Failed to read some fields in tomographic "
        "reconstruction plugin line. The file seems to be wrong. Error "
        "description: " << e.what() << std::endl;
    }

    table << id << params << name << cite;
    f->closeGroup();
    progress(static_cast<double>(j)/static_cast<double>(pluginsLen));
  }
  f->close();

  return ws;
}
Exemple #2
0
/** Execute the algorithm.
 */
void LoadCalFile::exec() {
  std::string CalFilename = getPropertyValue("CalFilename");
  std::string WorkspaceName = getPropertyValue("WorkspaceName");
  bool MakeGroupingWorkspace = getProperty("MakeGroupingWorkspace");
  bool MakeOffsetsWorkspace = getProperty("MakeOffsetsWorkspace");
  bool MakeMaskWorkspace = getProperty("MakeMaskWorkspace");

  if (WorkspaceName.empty())
    throw std::invalid_argument("Must specify WorkspaceName.");

  Instrument_const_sptr inst = LoadCalFile::getInstrument3Ways(this);

  GroupingWorkspace_sptr groupWS;
  OffsetsWorkspace_sptr offsetsWS;
  MaskWorkspace_sptr maskWS;

  // Title of all workspaces = the file without path
  std::string title = Poco::Path(CalFilename).getFileName();

  // Initialize all required workspaces.
  if (MakeGroupingWorkspace) {
    groupWS = GroupingWorkspace_sptr(new GroupingWorkspace(inst));
    groupWS->setTitle(title);
    declareProperty(Kernel::make_unique<WorkspaceProperty<GroupingWorkspace>>(
                        "OutputGroupingWorkspace", WorkspaceName + "_group",
                        Direction::Output),
                    "Set the the output GroupingWorkspace, if any.");
    groupWS->mutableRun().addProperty("Filename", CalFilename);
    setProperty("OutputGroupingWorkspace", groupWS);
  }

  if (MakeOffsetsWorkspace) {
    offsetsWS = OffsetsWorkspace_sptr(new OffsetsWorkspace(inst));
    offsetsWS->setTitle(title);
    declareProperty(Kernel::make_unique<WorkspaceProperty<OffsetsWorkspace>>(
                        "OutputOffsetsWorkspace", WorkspaceName + "_offsets",
                        Direction::Output),
                    "Set the the output OffsetsWorkspace, if any.");
    offsetsWS->mutableRun().addProperty("Filename", CalFilename);
    setProperty("OutputOffsetsWorkspace", offsetsWS);
  }

  if (MakeMaskWorkspace) {
    maskWS = MaskWorkspace_sptr(new MaskWorkspace(inst));
    maskWS->setTitle(title);
    declareProperty(
        Kernel::make_unique<WorkspaceProperty<MatrixWorkspace>>(
            "OutputMaskWorkspace", WorkspaceName + "_mask", Direction::Output),
        "Set the the output MaskWorkspace, if any.");
    maskWS->mutableRun().addProperty("Filename", CalFilename);
    setProperty("OutputMaskWorkspace", maskWS);
  }

  LoadCalFile::readCalFile(CalFilename, groupWS, offsetsWS, maskWS);

  if (MakeOffsetsWorkspace) {
    auto alg = createChildAlgorithm("ConvertDiffCal");
    alg->setProperty("OffsetsWorkspace", offsetsWS);
    alg->executeAsChildAlg();
    ITableWorkspace_sptr calWS = alg->getProperty("OutputWorkspace");
    calWS->setTitle(title);
    declareProperty(
        Kernel::make_unique<WorkspaceProperty<ITableWorkspace>>(
            "OutputCalWorkspace", WorkspaceName + "_cal", Direction::Output),
        "Set the output Diffraction Calibration workspace, if any.");
    setProperty("OutputCalWorkspace", calWS);
  }
}