/***************************************************************************
 *   Copyright (C) 2004 by Armando Navarro-Vazquez                         *
 *   uscqoanv@cesga.es                                                     *
 *                                                                         *
 *   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 <fstream>
#include <iostream>

#include <kdebug.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qstringlist.h>

#include "varianparser.h"
#include "nmrfile.h"
#include "tokenizer.h"
#include "nmrspectrum.h"

VarianParser::VarianParser ( QString& file )
    : SpectrumParser ( file )
{}


VarianParser::~VarianParser()
{}

Spectrum* VarianParser::ParseFile()
{
  NMRSpectrum* spectrum=new NMRSpectrum();
  QString root=m_file;
  QFileInfo finfo ( m_file );
  if ( !finfo.isReadable() ) return 0;


  if ( finfo.isDir() )
  {
    QStringList flist=finfo.dir ( true ).entryList();
    QStringList::Iterator it;

    bool bprocfound=false;
    for ( it=flist.begin();it!=flist.end();it++ )
    {
      if ( ( *it ) == "procpar" )
        bprocfound=true;
      if ( bprocfound ) break;
    }

    if ( !bprocfound ) return 0;
  }
  else if ( finfo.isFile() )
  {
    if ( finfo.fileName() == "fid" )
    {
      int k=root.find ( "/fid" );
      root.setLength ( k );

    }
    else return 0;


  }
  else return 0;



  std::ifstream sfid;
  std::ifstream procpar;

  QString file;
  file=root+"/fid";
  sfid.open ( file.toLatin1() );
  file=root+"/procpar";
  procpar.open ( file.toLatin1() );
  fidarray& data=spectrum->Data();
  spectrum->SetVendor ( NMRSpectrum::VARIAN );
//    fid.SetType(eVarian);
  std::string line;

  bool bByteSwap=true;
  //First count the atoms
  float offset,sw;
  size_t npoints;
  while ( std::getline ( procpar,line ) )
  {
   
    StringTokenizer token ( line );
    if ( token.size() )
    {
      if ( token[0]=="np" )
      {
        std::getline ( procpar,line );
        StringTokenizer stoken ( line );
        npoints=atoi ( stoken[1].c_str() );
      }

      if ( token[0]=="sw" )
      {
        std::getline ( procpar,line );
        StringTokenizer stoken ( line );
        sw=atof ( stoken[1].c_str() );
      }

      if ( token[0]=="sfrq" )
      {
        std::getline ( procpar,line );
        StringTokenizer stoken ( line );
        spectrum->SetField ( atof ( stoken[1].c_str() ) );
      }

      if ( token[0]=="rfl" )
      {
        std::getline ( procpar,line );
        StringTokenizer stoken ( line );
        offset=atof ( stoken[1].c_str() );

      }



    }
  }
  spectrum->SetSW ( sw );
  spectrum->SetOffset ( offset );
  spectrum->SetO1 ( offset+sw/2 );
  spectrum->SetSequential(false);

  //fid.SetPoints(npoints);
//   fid.CalculateAQ();
  int nTemp[npoints];
  npoints/=2; //complex points
  data.resize ( npoints );
  
  sfid.clear();
  sfid.seekg ( 60 );
  sfid.read ( ( char* ) nTemp,2*npoints*4 );

  size_t i,j;

  for ( i = 0, j = 0; i < npoints; i++ )
  {

    if ( bByteSwap )
      ByteSwap<int> ( &nTemp[j] );
    data[i].real() = nTemp[j] ;
    
    j++;
    if ( bByteSwap )
      ByteSwap<int> ( &nTemp[j] );
    data[i].imag() =nTemp[j] ;
    j++;
   
  }
  spectrum->SetDataType ( NMRSpectrum::FID );
  //delete [] nTemp;
  return spectrum;
}


