/*** ^^A -*-C++-*- **********************************************/
/*      proc_std                       17.02.2016               */
/****************************************************************/
/*      Short Description :                                     */
/*      Splitting STD pseudo 2D datasets in automation.		*/
/****************************************************************/
/*      Keywords :                                              */
/*      STD                                                     */
/****************************************************************/
/*      Description/Usage :                                     */
/*      Splits STD pseudo 2D data into reference spectrum and   */
/*      difference(s). The au program generates experiment n+1	*/
/*	(off resonance 1D) and n+i difference spectra, where n	*/
/*	is the experiment number of the pseudo 2D and i is the	*/
/*	number of on resonance frequencies.			*/
/*	The phase information from the off resonance spectrum	*/
/*	is used to correct the phase of the STD spectra		*/
/****************************************************************/
/*      Author(s) :                                             */
/*      Name            : Wolfgang Bermel and Stefan Jehle	*/
/*      Organisation    : Bruker BioSpin			*/
/*      Email           : nmr-support@bruker.com		*/
/****************************************************************/
/*      Name            Date    Modification:                   */
/*      saj             160217  created				*/
/*      saj             160503  relax option			*/
/****************************************************************/
/*
$Id:$
*/


char Mol_src[PATH_MAX];
int td1;
int counter;
int digmod;
int nexpno, refexpno, parmode;
float phc0, phc1;


/***** get dataset and parameters *****/

FETCHPARS("PARMODE", &parmode)

if (parmode != 1)
  STOPMSG("Dataset is not a 2D")

FETCHPARS("DIGMOD", &digmod)

if (digmod == 0 || digmod == 2)
  STOPMSG("digmod analog not supported")

FETCHPAR1S("TD", &td1)

if (td1 < 2)
  STOPMSG("not enough increments")

/***** get input *****/

refexpno = expno + 1;
nexpno = refexpno + 1;

strcpy(Mol_src, ACQUPATH("structure.mol"));

/***** read rows *****/

RSER(1, refexpno, 1)
ERRORABORT

for (counter = 2; counter <= td1; counter++)
{
  RSER(counter, nexpno, 1)
  ERRORABORT
  nexpno++;
}

/***** calculate difference *****/

if (i_argc <= 2  ||  strcmp(i_argv[2], "relax"))
{
  nexpno = refexpno + 1;

  for (counter = 2; counter <= td1; counter++)
  {
    DATASET(name, nexpno, procno, disk, user)
    DATASET2(name, refexpno, procno, disk, user)
    DATASET3(name, nexpno, procno, disk, user)

    STOREPAR("DC", -1.0)
    ADDFID
    ERRORABORT
    nexpno++;
  }
}

/***** process spectra - reference *****/

DATASET(name, refexpno, procno, disk, user)

EF
ERRORABORT

if (digmod == 1)
  APK
else
  APK0

FETCHPARS("PHC0", &phc0)
FETCHPARS("PHC1", &phc1)

if (access(Mol_src, F_OK) == 0)
  dircp(Mol_src, ACQUPATH("structure.mol"));

/***** process spectra - difference *****/

nexpno = refexpno + 1;

for (counter = 2; counter <= td1; counter++)
{
  DATASET(name, nexpno, procno, disk, user)

  STOREPAR("PHC0", phc0)
  STOREPAR("PHC1", phc1)

  if (access(Mol_src, F_OK) == 0)
    dircp(Mol_src, ACQUPATH("structure.mol"));

  EFP
  ERRORABORT
  nexpno++;
}

QUIT
