/***************************************************************************
 *   Copyright (C) 2005 by Armando Navarro-Vazquez                         *
 *   armando@linux                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "nmrspectrum.h"

NMRSpectrum::NMRSpectrum()
    : Spectrum()
{
  m_vendor = UNKNOWN;
  m_frequencyunits=PPM;
}


NMRSpectrum::~NMRSpectrum()
{}

float NMRSpectrum::PointToXUnits(size_t point) const
{


//   float hz= ( SW() * ( (m_data.size()-1-point)/static_cast<float>(m_data.size()-1) ) + O1()-SW()/2 )/m_field;
  switch(m_type)
  {
  case FID:
    return GetAQ() * ( point/static_cast<float>(m_data.size()-1) );
    break;
  case SPECTRUM:
    {
      float hz= ( SW() * ( (m_data.size()-1-point)/static_cast<float>(m_data.size()-1) ) + O1()-SW()/2 )/m_field;
      if( XUnits() == "ppm" )
        return hz/m_field;
      else return hz;
    }
    break;
  default:
    break;

  }
  return 0.;
}

void NMRSpectrum::SetDataType(datatype t)
{
  m_type=t;
  switch(m_type)
  {
    case FID:
    SetBaseLine(0.5);
    SetXUnits("s");
    break;
    case SPECTRUM:
    SetBaseLine(0.8);
    switch( m_frequencyunits)
    {
       case HZ:
       SetXUnits("Hz");
       break;
       case PPM:
       SetXUnits("PPM");
       break;
    }
   default:
   break;
  }
}

void NMRSpectrum::GetLimits(float& left,float& right)
{
  switch(m_type)
  {
  case FID:
    left=m_zoom.fleft*GetAQ();
    right=m_zoom.fright*GetAQ();
    break;
  case SPECTRUM:
    {

      float smin=(O1()+SW()/2)/m_field;
      float smax=(O1()-SW()/2)/m_field;
      left=(smin-smax)*(1-m_zoom.fleft)+smax;
      right=(smin-smax)*(1-m_zoom.fright)+smax;
    }
    break;
    /* case IRSPECTRUM:
       {
         float smin=(m_O1+m_sw/2);
         float smax=(m_O1-m_sw/2);
         min=(smin-smax)*(1-m_zoom.fleft)+smax;
         max=(smin-smax)*(1-m_zoom.fright)+smax;
       }
       break;*/
  }
}

QString NMRSpectrum::Info(const QRect& rect, const QPoint& pos) const
{
  if( m_data.empty() ) return "No data";

  size_t point=PosAt(pos,rect);

  QString info;
  switch( m_type)
  {
  case FID:
    info.sprintf("%.3f",1000*PointToXUnits(point));
    info += " ms";
    break;
  case SPECTRUM:
    if ( XUnits() == "ppm" )
    info.sprintf("%.3f ppm point[%d]=[%.3f,%.3f]",PointToXUnits(point),point,m_data[point].real(),m_data[point].imag());
    else
        info.sprintf("%.3f ppm point[%d]=[%.3f,%.3f]",PointToXUnits(point),point,m_data[point].real(),m_data[point].imag());
    break;

  }
  return info;

}

