/*
*/

#define _MAXSPCLEN  32768
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifndef TRUE
#define TRUE 1L
#endif
#ifndef FALSE
#define FALSE 0L
#endif

float x,y,*spc,*tim;

help()
{
  printf("BAM2plot spectrum [-b2p] [-p2b]\n");
  printf("  converts spectra from BAM to MULTIPLOT and vice versa\n");
  printf("  options:\n");
  printf("     -b2p     convert from BAM to MULTIPLOT\n");
  printf("     -p2b     convert from MULTIPLOT to BAM\n");
  return(0);
}

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


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

   if(argc < 3) {
      help();
      exit(0);
   }

   if(checkopt(argc,argv,"-b2p",z)) {
      max=readspec(argv[1],spc,tim);
      strcpy(z,argv[1]); n = strlen(z);
      for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
      writeplot(z,spc,tim,max);
   }

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

writeplot(name,spc,tim,max)
char *name;
float *spc, *tim;
int max;
{
FILE *fp;
int n;
char *s;

   s = (char *) malloc(256);

   sprintf(s,"%s.dat",name);
   fp = fopen(s,"w");
   if(fp == NULL) {
      fprintf(stderr,"BAM2plot: could not open >%s< for write\n",s);
      free(s);
      return(0);
   }
   fprintf(fp,"*XLABEL* Time\n*LEGEND* %s\n",name);
   for(n = 0; n < max; n++) {
      fprintf(fp,"%f %f\n",tim[n],spc[n]);
   }
   fclose(fp);

   free(s);
}

readplot(name,spc,tim)
char *name;
float *spc, *tim;
{
FILE *fp;
int max;
char *s, *z;

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

   strcpy(s,name);
   fp = fopen(s,"r");
   if(fp == NULL) {
      strcat(s,".dat");
      fp = fopen(s,"r");
      if(fp == NULL) {
         fprintf(stderr,"BAM2plot: could not open >%s< for read\n",name);
         free(s); free(z);
         return(0);
      }
   }
   while(!feof(fp)) {
      fgets(s,200,fp); sscanf(s,"%s",z);
      if(strcmp(z,"*LEGEND*") == 0) break;
   }
   max = 0;
   while(!feof(fp)) {
      fgets(s,200,fp);
      if(s[0] == '*') continue;
      if(strlen(s) < 3) break;
      sscanf(s,"%f %f\n",&tim[max],&spc[max]);
      max = max + 1;
   }
   fclose(fp);

   free(s); free(z);
}

readspec(name,spc,tim)
char *name;
float *spc, *tim;
{
FILE *fp;
int max;
double x;
char *s, *z;

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

   strcpy(s,name);
   fp = fopen(s,"r");
   if(fp == NULL) {
      fprintf(stderr,"BAM2plot: could not open >%s< for read\n",name);
      free(s); free(z);
      return(0);
   }
   max = 0;
   while(!feof(fp)) {
      fgets(s,200,fp);
      if(strlen(s) == 0) break;
      sscanf(s,"%lf",&x);
      tim[max] = x;
      fgets(s,200,fp);
      if(strlen(s) == 0) break;
      sscanf(s,"%lf",&x);
      spc[max] = x;
      max = max + 1;
   }
   fclose(fp);
   free(s); free(z);
   return(max);
}

writespec(name,spc,tim,max)
char *name;
float *spc, *tim;
int max;
{
FILE *fp;
int n;

   fp = fopen(name,"w");
   if(fp == NULL) {
      fprintf(stderr,"BAM2plot: could not open >%s< for write\n",name);
      return(0);
   }
   for(n = 0; n < max; n++) {
      fprintf(fp,"%f\n%f\n",tim[n],spc[n]);
   }
   fclose(fp);
}

checkopt(argc,argv,s,sv)
int argc;
char *argv[],s[],sv[];
{
int   n,erg;

   erg=FALSE;
   for(n=1;n<argc;n++) {
      if(strcmp(argv[n],s)==0) {
         erg=TRUE;
         if((n + 1) < argc) strcpy(sv,argv[n+1]);
      }
   }
   return(erg);
}
