/* Convert JCAMP-DX spectrum to LISE
*/

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

float x,y,*spc,*err,*tim;
char EOL[4];

help()
{
  printf("jcampio spectrum [-l2j] [-j2l] [-nocr]\n");
  printf("  converts spectra from LISE to JCAMP and vice versa\n");
  printf("  options:\n");
  printf("     -l2j     convert from LISE to JCAMP\n");
  printf("     -j2l     convert from JCAMP to LISE\n");
  printf("     -nocr    suppress CR before LF\n");
}

main(argc,argv)
int argc;
char *argv[];
{
int n,m,i,max;
char z[80],comment[80];


   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);
   }
   EOL[0]=13; EOL[1]=10; EOL[2]=0;
   if(checkopt(argc,argv,"-nocr",z)) {
      EOL[0]=10; EOL[1]=0;
   }
   if(checkopt(argc,argv,"-l2j",z)) {
      max=readspec(argv[1],spc,err,tim,comment);
      strcpy(z,argv[1]); n = strlen(z);
      for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
      writejcamp(z,spc,err,tim,max,comment);
   }

   if(checkopt(argc,argv,"-j2l",z)) {
      max = readjcamp(argv[1],spc,err,tim,comment);
      strcpy(z,argv[1]); n = strlen(z);
      for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
      writespec(z,spc,err,max,2,comment);
      strcat(z,".tim");
      writespec(z,tim,err,max,2,comment);
   }
   free(err); free(spc); free(tim);
   exit(0);
}

float getnumber(s)
char *s;
{
char z[80];
int i,n,l;
float erg;

   n = 0; l = strlen(s);
   erg = 0.0;
   while(s[n++] != '=') if(n >= l) return(0.0);
   i = 0; while(s[n] > 31) z[i++] = s[n++];
   z[i] = 0;
   erg = atosf(z);
   return(erg);
}

writejcamp(name,spc,err,tim,max,comment)
char *name, *comment;
float *spc, *err, *tim;
int max;
{
FILE *fp;
int i,n,m;
char *s, *z;
float maxy, miny, yfactor;

   s = (char *) malloc(256);
   z = (char *) malloc(256);

   sprintf(s,"%s.jcamp",name);
   fp = fopen(s,"w");
   if(fp == NULL) {
      fprintf(stderr,"jcampio: could not open >%s< for write\n",s);
      free(s);
      return(0);
   }

   maxy = spc[0]; miny = spc[0];
   for(n = 0; n < max; n++) {
      if(spc[n] > maxy) maxy = spc[n];
      if(spc[n] < miny) miny = spc[n];
   }

   yfactor = 2.7737901E-009;

   fprintf(fp,"##TITLE=%s%s",comment,EOL);
   fprintf(fp,"##JCAMP-DX=4.24%s",EOL);
   fprintf(fp,"##DATA TYPE= LISE%s",EOL);
   fprintf(fp,"##ORIGIN= (C) 1993 by Rainer Kowallik%s",EOL);
   fprintf(fp,"##OWNER= Public Domain%s",EOL);
   fprintf(fp,"##XUNITS= arbitrary%s",EOL);
   fprintf(fp,"##YUNITS= arbitrary%s",EOL);
   fprintf(fp,"##XFACTOR=1%s",EOL);
   fprintf(fp,"##YFACTOR=%f%s",yfactor,EOL);
   fprintf(fp,"##FIRSTX=%f%s",tim[0],EOL);
   fprintf(fp,"##LASTX=%f%s",tim[max-1],EOL);
   fprintf(fp,"##NPOINTS=%d%s",max,EOL);
   fprintf(fp,"##FIRSTY=%f%s",spc[0],EOL);
   fprintf(fp,"##MAXY=%f%s",maxy,EOL);
   fprintf(fp,"##MINY=%f%s",miny,EOL);
   fprintf(fp,"##DELTAX=%f%s",_tica,EOL);
   fprintf(fp,"##XYDATA=(X++(Y..Y))%s",EOL);
   n = 0;
   while(n < max) {
      sprintf(z,"%d",(int)tim[n]);
      while(strlen(z) < 70) {
         sprintf(s,"%+d",(int)(spc[n] / yfactor));
         strcat(z,s);
         if((n++) >= max) break;
      }
      fprintf(fp,"%s%s",z,EOL);
   }
   fprintf(fp,"##END=%s",EOL);
   fclose(fp);

   free(z); free(s);
}

readjcamp(name,spc,err,tim,comment)
char *name, *comment;
float *spc, *err, *tim;
{
FILE *fp;
int i,n,l,max;
char *s, *z;
float firstx, deltax, yfactor;

   s = (char *) malloc(256); z = (char *) malloc(256);

   firstx = 0.0; deltax = 1.0;
   yfactor = 1.0;
   strcpy(comment,"no comment");

   strcpy(s,name);
   fp = fopen(s,"r");
   if(fp == NULL) {
      strcat(s,".jcamp");
      fp = fopen(s,"r");
      if(fp == NULL) {
         fprintf(stderr,"jcampio: could not open >%s< for read\n",name);
         free(s); free(z);
         return(0);
      }
   }
   while(!feof(fp)) {
      fgets(s,200,fp);
      if(instr("TITLE",s) > 0) {
         n = 0; i = 0;
         while(s[i++] != '=') if(i > 100) break;
         while(s[i] > 30) comment[n++] = s[i++];
         comment[n] = 0;
      }
      if(instr("FIRSTX",s) > 0) firstx = getnumber(s);
      if(instr("DELTAX",s) > 0) deltax = getnumber(s);
      if(instr("YFACTOR",s) > 0) yfactor = getnumber(s);
      if(instr("XYDATA",s) > 0) break;
   }
   max = 0;
   while(!feof(fp)) {
      fgets(s,200,fp);
      n = 0; while(s[n] > 20) n++;
      s[n] = 0;
      if(s[0] == '#') continue;
      if(strlen(s) < 3) break;
      if(instr("END",s) > 0) break;
      n = 0; l = strlen(s);
      while(s[n++] > 47) if(n >= l) break; /* first skip X */
      while( 1 == 1) {
         i = 0; if(n >= l) break;
         z[i++] = s[n - 1];
         while(s[n] > 47) z[i++] = s[n++];
         z[i] = 0; n++;
         spc[max] = ((float) atoi(z)) * yfactor;
         err[max] = 0.001 * spc[max]; /* you'll need an error for the fit */
         tim[max] = firstx + deltax * ((float) max);
         max = max + 1;
      }
   }
   fclose(fp);
   free(s); free(z);
   return(max);
}

