/*********-*-C++-*-**********************************************/
/*  proc_2dphf2hetpl            30.03.2017                      */
/****************************************************************/
/*  Short Description :                                         */
/*  Determines phase correction in F2 for heteronuclear spectra */
/*  and plots them.                                             */
/****************************************************************/
/*  Keywords :                                                  */
/*  2D inverse HMQC HSQC                                        */
/****************************************************************/
/*  Description/Usage :                                         */
/*  Processing AU program for 2D inverse type spectra.          */
/*  The phase in F2 is determined by 2D peak picking            */
/*  followed by APK.                                            */
/*  If no valid plot limits are given in the dataset, the whole */
/*  spectrum is used.                                           */
/****************************************************************/
/*  Author(s) :                                                 */
/*  Name            : Rainer Kerssebaum                         */
/*  Organisation    : Bruker BioSpin GmbH                       */
/*  Email           : nmr-support@bruker.com                    */
/****************************************************************/
/*  Name        Date    Modification:                           */
/*  rke       060111  created out of proc_2dinv                 */
/*  ge      20071212  readPeakList                              */
/*  wem     20150326  care of NUS data                          */
/*  rke     20161103  plotting added                            */
/*  wem     20170320  set nprocno peak picking limits to full   */
/*  rke     20170330  if plot limits not valid, set full        */
/****************************************************************/
/*
$Id:$
*/

/****************************************************************/
/* Declare variables                                            */
    int     digmod, mc2, fntype;
    int     irow, s_procno, nprocno;
    float   offs1, offs2, phc0, phc1;
    double  f1p2, f2p2, f1p1, f2p1, sw2, sw1, sf2, sf1, sum1, sum2;
    char    xwlay[PATH_MAX], portf[PATH_MAX];

    portf[0] = 0;
    nprocno  = 9999;
    s_procno = procno;

/****************************************************************/
/* Declare filenames etc. for the portfolio file handling       */
    DECLARE_PORTFOLIO


/****************************************************************/
/* processing                                                   */

    XFB;
    ERRORABORT;
    XF2M;

/****************************************************************/
/* use aquisition range for the peak picking limits             */
    FETCHPAR("F1P", &f1p2)
    FETCHPAR("F2P", &f2p2)
    FETCHPARS("OFFSET",&offs2)
    FETCHPARS("SW_p",&sw2)
    FETCHPARS("SF",&sf2)
    sum2 = offs2 - sw2 / sf2;
    FETCHPAR1("F1P", &f1p1)
    FETCHPAR1("F2P", &f2p1)
    FETCHPAR1S("OFFSET",&offs1)
    FETCHPAR1S("SW_p",&sw1)
    FETCHPAR1S("SF",&sf1)
    sum1 = offs1 - sw1 / sf1;

    RVPP(nprocno);
    ERRORABORT;
    XFB;
    ERRORABORT;
    FETCHPAR1S("MC2",&mc2);

    if (mc2 > 0  &&  access(PROCPATH("2ir"), F_OK))
    {
        FETCHPARS("FnTYPE",&fntype);

        if (fntype == 2)
        {
            DELETEIMAGINARYDATA(name, expno, procno, disk, user)
            XHT2
        }
    }


/****************************************************************/
/* create the pp file on pos peaks                              */

    DATASET(name,expno,nprocno,disk,user);
    STOREPAR("CY",100.);
    STOREPAR("MAXI",110.);
    STOREPAR("MI",10.);
    STOREPAR("PSIGN",0);
    STOREPAR("PC",2.0);
    STOREPAR("F1P",offs1);
    STOREPAR("F2P",sum1);

    PP;
    ERRORABORT;


/****************************************************************/
/* open and read the pp file                                    */

    irow = readPeakList(PROCPATH(0));

    if (irow < 0)
    {
        Proc_err(DEF_ERR_OPT, "%s, phase correction aborted", getPeakError());
        ABORT;
    }

    if (irow == 0)
    {
        Proc_err(DEF_ERR_OPT, "no peak was found in the 2D file, phase correction aborted");
        ABORT;
    }


/****************************************************************/
/* add the selected rows in the procno 9999                     */

    DATASET(name,expno,s_procno,disk,user);
    RSR(getPeakAddress(0),nprocno);
    ERRORABORT;
    DATASET(name,expno,nprocno,disk,user);
    DATASET2(name,expno,nprocno - 1,disk,user);
    DATASET3(name,expno,nprocno,disk,user);
    STOREPAR("DATMOD",1);
    STOREPAR("DC",1.0);

    for (i1 = 1; i1 < irow; i1++)
    {
        DATASET(name,expno,s_procno,disk,user);
        RSR(getPeakAddress(i1),nprocno - 1);
        ERRORABORT;
        DATASET(name,expno,nprocno,disk,user);
        ADD;
        ERRORABORT;
    }

    freePeakList();


/****************************************************************/
/* perform the phase correction                                 */

    FETCHPARS("DIGMOD", &digmod)

    if (digmod == 3)
        APK0
    else
        APK

    ERRORABORT;
    FETCHPAR("PHC0",&phc0);
    FETCHPAR("PHC1",&phc1);

    DATASET(name,expno,s_procno,disk,user);
    phc0 = fmod(phc0,360.);
    STOREPAR("PHC0",phc0);
    STOREPAR("PHC1",phc1);
    STOREPAR("PH_mod",1);


/****************************************************************/
/* processing                                                   */

    XFB;
    ERRORABORT;
    XCMD("clev")


/****************************************************************/
/* If no layout is defined the default layout will be used      */

    FETCHPAR("LAYOUT",&xwlay)
    if (xwlay[0] == 0)
        STOREPAR("LAYOUT","+/2D_inv.xwp")


/****************************************************************/
/* prepare plotting                                             */

    if (f2p2 >= f1p2)
    {
        STOREPAR("F1P", offs2);
        STOREPAR("F2P", sum2);
    }
    if (f2p1 >= f1p1)
    {
        STOREPAR1("F1P", offs1);
        STOREPAR1("F2P", sum1);
    }


/****************************************************************/
/* set F2 reference for plotting. If no dataset is              */
/* defined the positive F2 projection is used                   */

    GETCURDATA2

    if (AUERR < 0 || access(PROCPATH2("1r"),F_OK))
    {
        RHPP(98)
        DATASET2(name, expno, 98, disk, user)
        GETCURDATA2
    }


/****************************************************************/
/* set F1 reference for plotting. If no dataset is              */
/* defined the F1 projection is used                            */

    GETCURDATA3

    if (AUERR < 0 || access(PROCPATH3("1r"), F_OK))
    {
        RVPP(99)
        DATASET3(name, expno, 99, disk, user)
        GETCURDATA3
    }


/****************************************************************/
/* Create and open portfolio file portf                         */
    strcpy(portf, PROCPATH("portf"));
    CREATE_PORTFOLIO(portf)

/****************************************************************/
/* The 2D dataset is written first to the portfolio file        */
    ADD_CURDAT_TO_PORTFOLIO

/****************************************************************/
/* Add the F2 projection (2nd dataset) to the portfolio file    */
    ADD_TO_PORTFOLIO(disk2,user2,name2,expno2,procno2)

/****************************************************************/
/* Add the F1 projection (3rd dataset) to the portfolio file    */
    ADD_TO_PORTFOLIO(disk3,user3,name3,expno3,procno3)

/****************************************************************/
/* Close protfolio file                                         */
    CLOSE_PORTFOLIO

/****************************************************************/
/* Plot with two datasets defined in the portfolio. This        */
/* command uses the portfolio file specified in the             */
/* CREATE_PORTFOLIO command                                     */
    AUTOPLOT_WITH_PORTFOLIO


/****************************************************************/
/* Adds include file 'printtofile' that manage                  */
/* the 'plot_to_file' information                               */
#include <inc/printtofile>

QUIT
