#include <stdio.h>
#include <math.h>
#include <spec.h>

int umax,ulen,ubeg,uend;
float   *spc,*err,*tim;

char z[80],comment[80],uname[80];


help()
{
printf("generate spectrum\n");
printf("genspc sp[{i:j}] {-lin a b} {-sin a b c} {-exp a b c}\n");
printf("contrary to the normal behaviour, the WHOLE spectrum (and corresponding\n");
printf("error array) is stored back \n\n");

printf("   -t    generate timespectrum only. \n");
printf("   -e    generate errorspectrum only. \n");
printf("   -m n  maximum of n elements \n\n");

printf("   -sin a b c     for all n: spc(n) = a*sin(b*n+c)) \n");
printf("   -exp a b c     for all n: spc(n) = a+b*exp(c*n) \n");
printf("   -lin a b       for all n: spc(n) = a+n*b\n");
}

main(argc,argv)
int argc;
char *argv[];
{
int   m,n,i,max;
char  s[80],z[80];
FILE  *stream,*fopen();
float a,b,c,reals[30];

   if(argc<3) _help0(); /* at least two parameters should be specified */
   if(checkopt(argc,argv,"-dummy",s)) printf("\n"); /* get standard options */

   spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
   err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
   tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
   if(tim==NULL) {
      printf("sorry, not enough memory\n");
      exit(-1);
   }

   _tica = 1.0;                        /* set time calibration */
   ubeg=0;
   uend=_MAXSPCLEN;
   max=_MAXSPCLEN;

   strcat(uname,argv[1]);              /* get spectrum name */
   i=instr("[",uname);            /* check for spectrum name  */
   if(i>0) {
      midstr(uname,argv[1],0,i-1);
      midstr(s,argv[1],i+1,instr("]",argv[1])-1);
   } else {
      strcpy(s,"");
   }
   n=instr(":",s);
   if(strlen(s)>0) {             /* handle short spectrum */
      if(n<0) {                  /* spc[n] */
         uend=atoi(s);
         ubeg=0;
         max=uend;
      }
      if(n>0) {                  /* spc[a:b] */
         midstr(z,s,0,n-1); ubeg=atoi(z);
         midstr(z,s,i+1,strlen(s)); uend=atoi(z);
         max=uend;
         for(i=0;i<=ubeg;i++) spc[i]=0.0;
      }
   }
   if(uname[0]=='-') strcpy(uname,"");

   if(checkopt(argc,argv,"-m",s)) max=atoi(s);


/* ----------------------------------------------------------------------
         generate the arrays
   ---------------------------------------------------------------------- */
   if(xcheckopt(argc,argv,"-lin",reals)) {
      a=reals[0]; b=reals[1];
      n=0;
      for(i=ubeg;i<=uend;i++) {
         spc[i] = a + (n * b);
         n=n+1;
      }
      sprintf(comment,"lin: %E + (%E * x)",a,b);
   }

   if(xcheckopt(argc,argv,"-sin",reals)) {
      a=reals[0]; b=reals[1]; c=reals[2];
      n=0;
      for(i=ubeg;i<=uend;i++) {
         spc[i] = a * sin((b * n) + c);
         n=n+1;
      }
      sprintf(comment,"sin: %E * sin((%E * x) + %E)",a,b,c);
   }

   if(xcheckopt(argc,argv,"-exp",reals)) {
      a=reals[0]; b=reals[1]; c=reals[2];
      n=0;
      for(i=ubeg;i<=uend;i++) {
         spc[i] = a + (b * exp(n*c));
         n=n+1;
      }
      sprintf(comment,"exp: %E + (%E * exp(%E * x))",a,b,c);
   }

/* ----------------------------------------------------------------------
         save spectra
   ---------------------------------------------------------------------- */
 

   if(checkopt(argc,argv,"-t",z)) { /* modify time spectrum only */
      strcat(uname,".tim");
   }
   if(checkopt(argc,argv,"-e",z)) { /* modify error spectrum only */
      strcat(uname,".err");
   }

   writespec(uname,spc,err,max,2,comment);
   free(err); free(spc); free(tim);
   exit(0);
}

xcheckopt(argc,argv,s,reals)
int argc;
char *argv[],s[];
float reals[];
{
int i,n,m,erg;
char c;

   erg=FALSE;
   i=1; m=0;
   while(i<=argc) {
      if(strcmp(argv[i++],s)==0) {
         erg=TRUE;
         for(n=i;n<=argc;n++) reals[m++]=atosf(argv[n]);
         break;
      }
   }
   return(erg);
}
         
