/*** ^^A -*-C++-*- **********************************************/
/*	multizgs		18.05.2000			*/
/****************************************************************/
/*	Short Description :					*/
/*	Performs multiple acquisitions on a series of experiments.	*/
/****************************************************************/
/*	Keywords :						*/
/*	serial acquisitions					*/
/****************************************************************/
/*	Description/Usage :					*/
/*	This AU program performs multiple acquisitions on	*/
/*	increasing expnos. If datasets do not yet exist, the	*/
/*	current dataset and its parameters are copied. If the	*/
/*	data sets already exist, then the experiments are	*/
/*	executed as they are.					*/
/*	The total experiment time is estimated and printed out.	*/
/*	The number of experiments to be performed can be	*/
/*	specified on the command line :				*/
/*		xau multizg <no of experiments>			*/
/****************************************************************/
/*	Author(s) :						*/
/*	Name		: Rainer Kerssebaum			*/
/*	Organisation	: Bruker BioSpin GmbH			*/
/*	Email		: rainer.kerssebaum@bruker.de		*/
/****************************************************************/
/*	Name		Date	Modification:			*/
/*	rke		941207	created				*/
/*	eng		000327  switch ZGSAFETY off if on	*/
/*	eng		000518	PathSystemTemp instead of /tmp	*/
/*	wem		080904	ZG_OVERWRITE			*/
/*  rwa 	160812  made mulit-expt applicable */
/****************************************************************/
/*
$Id: multizg,v 1.14 2009/04/02 13:05:43 wem Exp $
*/

AUERR = local_au(curdat);
QUITMSG("--- multizgs finished ---")

#include <inc/exptUtil>

static char stopau[PATH_MAX];


static int multizg(const char* curdat)
{
  char statusline[128];
  FILE* fp;
  int expTime = 0;
  int i1 = 0;
  int i2 = 1;
  int i3 = 2; 
  int i4 = 3;
  char runmultizgs[20];

 
    i2 = expno;
    GETINT("Enter 1st experiment to copy :",i2)
  
    i3 = i2 + 1;
    GETINT("Enter last experiment to copy :",i3)

    i4 = i3 + 1;
    GETINT("In which experiment should the first experiment be run?",i4)
   
    i1 = 1;
    GETINT("Enter number of times to repeat the experiments :",i1)

	strcpy(runmultizgs,"c");
	GETSTRING("Do you want to (r)un the experiments, or just (c)reate them? ",runmultizgs)

  
  sprintf(stopau, "%s/%s/%s",
	  PathXWinNMRProg(), PathXWinNMRCurDir(), "poptaustop.acq");
  fp = fopen(stopau, "w");
  if (fp)
    fclose(fp);
				/* now perform the experiments  */

TIMES(i1)
 TIMES2(i3-i2+1)
	REXPNO(i2+loopcount2)
	SETCURDATA
	REXPNO((loopcount1*(i3-i2+1))+i4+loopcount2)
	SETCURDATA	
	if( (runmultizgs[0] == 'R') || (runmultizgs[0] == 'r') ) {
		if (fp  &&  access(stopau, F_OK)){
			return 0;
		}
		sprintf(statusline,"running experiment %d of %d (expno %d)",(loopcount1*(i3-i2+1))+1+loopcount2,i1*(i3-i2+1),(loopcount1*(i3-i2+1))+i4+loopcount2,i1*(i3-i2+1));
		Show_status(statusline);
		sleep(2);
		ZG_OVERWRITE
	}
	
	
 END	
END

  return 0;
}

int local_au(const char* curdat)
{
  int ret = multizg(curdat);

  if (stopau[0])
    unlink(stopau);

  return ret;
}
