Example #1
0
/**
 * Creates the output workspace for this algorithm
 * @param inputWorkspace A parent workspace to initialize from.
 * @return A pointer to the output workspace.
 */
API::MatrixWorkspace_sptr Transpose::createOutputWorkspace(
    API::MatrixWorkspace_const_sptr inputWorkspace) {
  Mantid::API::Axis *yAxis = getVerticalAxis(inputWorkspace);
  const size_t oldNhist = inputWorkspace->getNumberHistograms();
  const auto &inX = inputWorkspace->x(0);
  const size_t oldYlength = inputWorkspace->blocksize();
  const size_t oldVerticalAxislength = yAxis->length();

  // The input Y axis may be binned so the new X data should be too
  size_t newNhist(oldYlength), newXsize(oldVerticalAxislength),
      newYsize(oldNhist);
  MatrixWorkspace_sptr outputWorkspace = inputWorkspace->cloneEmpty();
  outputWorkspace->initialize(newNhist, newXsize, newYsize);
  outputWorkspace->setTitle(inputWorkspace->getTitle());
  outputWorkspace->setComment(inputWorkspace->getComment());
  outputWorkspace->copyExperimentInfoFrom(inputWorkspace.get());
  outputWorkspace->setYUnit(inputWorkspace->YUnit());
  outputWorkspace->setYUnitLabel(inputWorkspace->YUnitLabel());
  outputWorkspace->setDistribution(inputWorkspace->isDistribution());

  // Create a new numeric axis for Y the same length as the old X array
  // Values come from input X
  API::NumericAxis *newYAxis(nullptr);
  if (inputWorkspace->isHistogramData()) {
    newYAxis = new API::BinEdgeAxis(inX.rawData());
  } else {
    newYAxis = new API::NumericAxis(inX.rawData());
  }

  newYAxis->unit() = inputWorkspace->getAxis(0)->unit();
  outputWorkspace->getAxis(0)->unit() = inputWorkspace->getAxis(1)->unit();
  outputWorkspace->replaceAxis(1, newYAxis);
  setProperty("OutputWorkspace", outputWorkspace);
  return outputWorkspace;
}
Example #2
0
  /** Write out a MatrixWorkspace's data as a 2D matrix.
   * Use writeNexusProcessedDataEvent if writing an EventWorkspace.
   */
  int NexusFileIO::writeNexusProcessedData2D( const API::MatrixWorkspace_const_sptr& localworkspace,
      const bool& uniformSpectra, const std::vector<int>& spec,
      const char * group_name, bool write2Ddata) const
  {
    NXstatus status;

    //write data entry
    status=NXmakegroup(fileID,group_name,"NXdata");
    if(status==NX_ERROR)
      return(2);
    NXopengroup(fileID,group_name,"NXdata");
    // write workspace data
    const size_t nHist=localworkspace->getNumberHistograms();
    if(nHist<1)
      return(2);
    const size_t nSpectBins=localworkspace->readY(0).size();
    const size_t nSpect=spec.size();
    int dims_array[2] = { static_cast<int>(nSpect),static_cast<int>(nSpectBins) };


    // Set the axis labels and values
    Mantid::API::Axis *xAxis=localworkspace->getAxis(0);
    Mantid::API::Axis *sAxis=localworkspace->getAxis(1);
    std::string xLabel,sLabel;
    if ( xAxis->isSpectra() ) xLabel = "spectraNumber";
    else
    {
      if ( xAxis->unit() ) xLabel = xAxis->unit()->unitID();
      else xLabel = "unknown";
    }
    if ( sAxis->isSpectra() ) sLabel = "spectraNumber";
    else
    {
      if ( sAxis->unit() ) sLabel = sAxis->unit()->unitID();
      else sLabel = "unknown";
    }
    // Get the values on the vertical axis
    std::vector<double> axis2;
    if (nSpect < nHist)
      for (size_t i=0;i<nSpect;i++)
        axis2.push_back((*sAxis)(spec[i]));
    else
      for (size_t i=0;i<sAxis->length();i++)
        axis2.push_back((*sAxis)(i));

    int start[2]={0,0};
    int asize[2]={1,dims_array[1]};


    // -------------- Actually write the 2D data ----------------------------
    if (write2Ddata)
    {
      std::string name="values";
      NXcompmakedata(fileID, name.c_str(), NX_FLOAT64, 2, dims_array,m_nexuscompression,asize);
      NXopendata(fileID, name.c_str());
      for(size_t i=0;i<nSpect;i++)
      {
        int s = spec[i];
        NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readY(s)[0]))),start,asize);
        start[0]++;
      }
      if(m_progress != 0) m_progress->reportIncrement(1, "Writing data");
      int signal=1;
      NXputattr (fileID, "signal", &signal, 1, NX_INT32);
      // More properties
      const std::string axesNames="axis2,axis1";
      NXputattr (fileID, "axes",  reinterpret_cast<void*>(const_cast<char*>(axesNames.c_str())), static_cast<int>(axesNames.size()), NX_CHAR);
      std::string yUnits=localworkspace->YUnit();
      std::string yUnitLabel=localworkspace->YUnitLabel();
      NXputattr (fileID, "units",  reinterpret_cast<void*>(const_cast<char*>(yUnits.c_str())), static_cast<int>(yUnits.size()), NX_CHAR);
      NXputattr (fileID, "unit_label",  reinterpret_cast<void*>(const_cast<char*>(yUnitLabel.c_str())), static_cast<int>(yUnitLabel.size()), NX_CHAR);
      NXclosedata(fileID);

      // error
      name="errors";
      NXcompmakedata(fileID, name.c_str(), NX_FLOAT64, 2, dims_array,m_nexuscompression,asize);
      NXopendata(fileID, name.c_str());
      start[0]=0;
      for(size_t i=0;i<nSpect;i++)
      {
        int s = spec[i];
        NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readE(s)[0]))),start,asize);
        start[0]++;
      }
      if(m_progress != 0) m_progress->reportIncrement(1, "Writing data");

      // Fractional area for RebinnedOutput
      if (localworkspace->id() == "RebinnedOutput")
      {
        RebinnedOutput_const_sptr rebin_workspace = boost::dynamic_pointer_cast<const RebinnedOutput>(localworkspace);
        name="frac_area";
        NXcompmakedata(fileID, name.c_str(), NX_FLOAT64, 2,
                              dims_array,m_nexuscompression,asize);
        NXopendata(fileID, name.c_str());
        start[0]=0;
        for(size_t i=0;i<nSpect;i++)
        {
          int s = spec[i];
          NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(rebin_workspace->readF(s)[0]))),
                           start, asize);
          start[0]++;
        }
        if(m_progress != 0) m_progress->reportIncrement(1, "Writing data");
      }

      NXclosedata(fileID);
    }

    // write X data, as single array or all values if "ragged"
    if(uniformSpectra)
    {
      dims_array[0]=static_cast<int>(localworkspace->readX(0).size());
      NXmakedata(fileID, "axis1", NX_FLOAT64, 1, dims_array);
      NXopendata(fileID, "axis1");
      NXputdata(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readX(0)[0]))));
    }
    else
    {
      dims_array[0]=static_cast<int>(nSpect);
      dims_array[1]=static_cast<int>(localworkspace->readX(0).size());
      NXmakedata(fileID, "axis1", NX_FLOAT64, 2, dims_array);
      NXopendata(fileID, "axis1");
      start[0]=0; asize[1]=dims_array[1];
      for(size_t i=0;i<nSpect;i++)
      {
        NXputslab(fileID, reinterpret_cast<void*>(const_cast<double*>(&(localworkspace->readX(i)[0]))),start,asize);
        start[0]++;
      }
    }
    std::string dist=(localworkspace->isDistribution()) ? "1" : "0";
    NXputattr(fileID, "distribution",  reinterpret_cast<void*>(const_cast<char*>(dist.c_str())), 2, NX_CHAR);
    NXputattr (fileID, "units",  reinterpret_cast<void*>(const_cast<char*>(xLabel.c_str())), static_cast<int>(xLabel.size()), NX_CHAR);

    auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(xAxis->unit());
    if(label)
    {
      NXputattr (fileID, "caption",  reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR);
      auto unitLbl = label->label();
      NXputattr (fileID, "label",  reinterpret_cast<void*>(const_cast<char*>(unitLbl.ascii().c_str())), static_cast<int>(unitLbl.ascii().size()), NX_CHAR);
    }

    NXclosedata(fileID);

    if ( ! sAxis->isText() )
    {
      // write axis2, maybe just spectra number
      dims_array[0]=static_cast<int>(axis2.size());
      NXmakedata(fileID, "axis2", NX_FLOAT64, 1, dims_array);
      NXopendata(fileID, "axis2");
      NXputdata(fileID, (void*)&(axis2[0]));
      NXputattr (fileID, "units",  reinterpret_cast<void*>(const_cast<char*>(sLabel.c_str())), static_cast<int>(sLabel.size()), NX_CHAR);

      auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(sAxis->unit());
      if(label)
      {
        NXputattr (fileID, "caption",  reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR);
        auto unitLbl = label->label();
        NXputattr (fileID, "label",  reinterpret_cast<void*>(const_cast<char*>(unitLbl.ascii().c_str())), static_cast<int>(unitLbl.ascii().size()), NX_CHAR);
      }

      NXclosedata(fileID);
    }
    else
    {
      std::string textAxis;
      for ( size_t i = 0; i < sAxis->length(); i ++ )
      {
        std::string label = sAxis->label(i);
        textAxis += label + "\n";
      }
      dims_array[0] = static_cast<int>(textAxis.size());
      NXmakedata(fileID, "axis2", NX_CHAR, 2, dims_array);
      NXopendata(fileID, "axis2");
      NXputdata(fileID,  reinterpret_cast<void*>(const_cast<char*>(textAxis.c_str())));
      NXputattr (fileID, "units",  reinterpret_cast<void*>(const_cast<char*>("TextAxis")), 8, NX_CHAR);

      auto label = boost::dynamic_pointer_cast<Mantid::Kernel::Units::Label>(sAxis->unit());
      if(label)
      {
        NXputattr (fileID, "caption",  reinterpret_cast<void*>(const_cast<char*>(label->caption().c_str())), static_cast<int>(label->caption().size()), NX_CHAR);
        auto unitLbl = label->label();
        NXputattr (fileID, "label",  reinterpret_cast<void*>(const_cast<char*>(unitLbl.ascii().c_str())), static_cast<int>(unitLbl.ascii().size()), NX_CHAR);
      }

      NXclosedata(fileID);
    }

    writeNexusBinMasking(localworkspace);

    status=NXclosegroup(fileID);
    return((status==NX_ERROR)?3:0);
  }