/*** ^^A -*-C++-*- **********************************************/
/*      cal_cpd - calibrate cpd seqyuence            28.11.2022 */
/****************************************************************/
/****************************************************************/
/*      Author(s) :                                             */
/*      Name            : Eriks Kupce                           */
/*      Organisation    : Bruker BioSpin                        */
/*      Email           : eriks.kupce@bruker.com                */
/****************************************************************/

const char* args[3];
FILE *src, *trg;
char sourcefile[PATH_MAX], targetfile[PATH_MAX], cpd_name[PATH_MAX];
char *ch, xstr[256], str2[256], spx[32], s2[32];
double r1, r2, px, plx, scf = 1.0, old_pcpd = 350, plscf = 1.0, pltot = 0.0, ptot = 0.0, pltav=0.0;
int    ix = 0, jx = 0, kx = 0, ph = 0, ii = 0, jj=0, kk=0;   // pl3, plw2
float  p90, pl90, pcpd = 1000.0, pl_cpd = 0.0;

if(i_argv[2][6] == '2')
{
	ix = 2; jx = 12; kx = 3;
}
else if(i_argv[2][6] == '3')
{
  ix = 3; jx = 16; kx = 21;
}
else
{
  Proc_err(DEF_ERR_OPT, "Error - unrecognized channel, only cpdprg2 or cpdprg3 allowed.");
  ABORT
}

FETCHPAR(i_argv[2], cpd_name);

sprintf(spx, "P %d", kx);    // p3 or p21
FETCHPAR(spx, &p90)
printf("p90 = %.2f \n", p90);

sprintf(spx, "PCPD %d", ix);    // pcpd2 or pcpd3
FETCHPAR(spx, &pcpd)
scf = pcpd/old_pcpd;
printf("pcpd = %.2f, old pcpd2 = %.2f scf = %.2f\n", pcpd, old_pcpd, scf);

sprintf(spx, "PLW %d", ix);    // pl2 or pl3
FETCHPAR(spx, &pl90)

printf("pl90 = %.2f \n", pl90);

sprintf(spx, "PLW %d", jx);    // pl12 or pl16
FETCHPAR(spx, &pl_cpd)

printf("pl_cpd = %.2f \n", pl_cpd);

if((pcpd< 0.1) || (old_pcpd < 0.1))
{
  Proc_err(DEF_ERR_OPT, "Error - pcpd not set correctly.");
  ABORT
}

sprintf(sourcefile, "%s/exp/stan/nmr/lists/cpd/user/%s",PathXWinNMRInst(), cpd_name);
sprintf(targetfile, "%s/exp/stan/nmr/lists/cpd/user/%s",PathXWinNMRInst(), "randCPD-J");

if((src=fopen(sourcefile, "r"))==NULL)
{
  Proc_err(DEF_ERR_OPT, "cpd file not found:\n%s", sourcefile);
  ABORT
}

if((trg=fopen(targetfile, "w"))==NULL)
{
  Proc_err(DEF_ERR_OPT, "cannot open cpd file:\n%s", targetfile);
  ABORT
}

while(fgets(xstr, 256, src)) 
{
	jx=0;
	strcpy(str2, xstr);
  if(ch=strstr(str2,"pl=") != NULL)  
  { 
  	ch=strchr(str2,'=');
  	if(ch != NULL)
  	{
      jx = sscanf(ch+1, "%lf",  &r2);
      if(jx != 1) jx = 0;
  	}
  	else jx = 0;
  }
  
  ch=strchr(str2,':');
  if(ch != NULL) 
  {
  	kx = sscanf(ch+1, "%d",  &ph);
    ch -=2;
   *ch='\0';
    ix = sscanf(str2, "%d %d",  &ii, &jj);
    if(ix == 1) 
    {
      r1 = (double) ii/old_pcpd;
      if(jx == 1) 
      {
      	scf = p90/(pcpd*r1);
      	plx = scf*scf*pl90;
        fprintf(trg, "pcpd*%.4f:%d pl=%.4f [Watt]\n", r1, ph, plx);
        if(plx > pl_cpd) printf("WARNING: power %.4f not in the safe range \n", plx);
        ptot += 4.0*pcpd*r1;
        pltot += plx*4.0*pcpd*r1;
      }
      else fprintf(trg, "pcpd*%.4f:%d \n", r1, ph);
    }
    else if(ix == 2) 
    {
      r1 = (double) jj/old_pcpd;
      if (jx == 1) 
      {
      	scf = p90/(pcpd*r1);
      	plx = scf*scf*pl90;
        fprintf(trg, "1 pcpd*%.4f:%d pl=%.4f [Watt]\n", r1, ph, plx);
        if(plx > pl_cpd) printf("WARNING: power %.4f not in the safe range \n", plx);
        ptot += 4.0*pcpd*r1;
        pltot += plx*4.0*pcpd*r1;
      }
      else fprintf(trg, "1 pcpd*%.4f:%d \n", r1, ph);
    }
  }
  else fprintf(trg, "%s", xstr);           
}
pltav = pltot/ptot;
printf("cal_cpd: average decoupling power: %.4f \n", pltav);
printf("cal_cpd: new cpd saved in %s \n", targetfile);

fclose(src);
fclose(trg);  

QUIT
