
#include <stdio.h>
#include <direct.h>

/* This is Russ Bowers' ifid program for mass conversion of Bruker fids into ascii text files. */
/* If necessary, the program converts from little endian to big endian format after reading the binary 1r file suitable for intel x86 processors */
/* Ability to interpolate using cubic spline interpolation is incorporated */
/* The program reads the acqus files to obtain the dwell time */
/* ifid should be executed in the same folder as the list.txt file (without any command line argument) */
/* or alternatively, folder containing the "list.txt" can be specified as a command line argument to bruk2igor */
/* Use the command unix find . -name "fid" > list.txt  to create the list of fid files to be converted*/


int main( int argc, char *argv[] )
{

FILE *fptr,*fout,*list;

int data[64000];
int i,j,k,l,pos,n,li,test,outform,interval,n_out,imultiple;
char filename[128][256],outpath[256],outname[256],expname[256],expname_short[256],series[256],procs_path[256],expdir[256],listname[256],acqs_path[256],filenum[256];
float sw,offset,freq,dfreq,te,t,dt,larmor;
int npts,bytorda;

if (argc==2) {
	sscanf(argv[1],"%s",expdir);
	strcpy(listname,expdir);
	strcat(listname,"/list.txt");
} else strcpy(listname,"list.txt");

list=fopen(listname,"r");
if (!list) {
	printf("no list.txt file found\n");
	exit(0);
}

li=0;
do {
	test=fscanf(list,"%s",filename[li++]);
} while (test==1);
li--;
fclose(list);

printf("program found %d filenames in list.txt\n",li);
if (li==0) exit(0);

/*
printf("\nenter output format [0=.txt,ppm; 1=.txt, Hz; 2=.dfp; 3=.itx,ppm (igor); 4=.itx,Hz] > ");
scanf("%d",&outform);
*/
outform=1;

/* make the output directory */

	i=0;
	do { 
		outpath[i]=filename[0][i];
		expname[i]=filename[0][i]; }
	while (filename[0][i++]!='/');
	
	expname[i-1]='\0';
	outpath[i-1]='\0';

	if (strlen(expname)>4) { 
		for (l=0;l<5;l++) expname_short[l]=expname[l];
		expname[5]='\0';
	} else strcpy(expname_short,expname);

	strcat(outpath,"/ifids");
	mkdir(outpath);

for (j=0;j<li;j++) {  /* LOOP OVER FILES */
	pos=strlen(filename[j]);
	strcpy(acqs_path,"\0");
	for (i=0;i<pos-3;i++) acqs_path[i]=filename[j][i];
	acqs_path[i]='\0';
	strcat(acqs_path,"acqus");
	i=0; l=0;

	while (filename[0][i++]!='/');
//	strcpy(series,"/");
	strcpy(series,expname);
	strcat(series,"_");
	k=strlen(series);
	while (filename[j][i]!='/') {
		series[k++]=filename[j][i];
		filenum[l++]=filename[j][i];
		i++;
	}
	series[k]='\0';
	filenum[l]='\0';

	if (strncmp(filenum,"999",3)==0) {
		printf("warning: encountered 999 file in list.\n");
	}

	get_TE(acqs_path,&sw,&npts,&te,&bytorda);

	if (j==0) {

		printf("enter point multiple [1=every point] >");
		scanf("%d",&imultiple);

		interval=1;
		n_out=npts/interval;
	}

	printf(" sw=%10.3e npts=%d TE=%f",sw,npts,te);

	if (npts>1) {


//		dt=round_dt(sw);  /* convert dwell time to microseconds */

		dt=1/sw;

		fptr=fopen(filename[j],"rb");

		if (!fptr) {
			printf("spectrum file %s not found\n",filename[j]);
			exit(0);
		}

		n=fread(&data[0],sizeof(int),npts,fptr);
		printf(" -> %d points read\n",n);
		fclose(fptr);
		
			strcpy(outname,outpath);
			strcat(outname,"/");
			strcat(outname,series);
			strcat(outname,".txt");
			fout=fopen(outname,"w");
			fprintf(fout,"%10.5e\n%d\n",sw,npts/2);

			t=0;
			for (i=0;i<npts;i+=2*interval) {
				if (bytorda==1) fprintf(fout,"%d\t%d\n",INT_little_endian_TO_big_endian(data[i]),INT_little_endian_TO_big_endian(data[i+1]));
				else fprintf(fout,"%d\t%d\n",data[i],data[i+1]);
				t+=dt;
			}
	
			fclose(fout);
	} else printf(" -> no data written\n");
} /* end loop over files */

}


get_TE(path,sw,npts,te,bytorda)
char *path;
float *sw;
int *npts;
float *te;
int *bytorda;
{
FILE *acqptr;
char str[256],str2[256],c;
int i,quit;

acqptr=fopen(path,"r");
if (!acqptr) {
	printf("acqus file not found\n");
	exit(0);
}

quit=0;
while ((fgets(str,80,acqptr)!=NULL)&&(quit==0)) {
  if (strncmp(str,"##$TE",5)==0) sscanf(str,"%s %f",str2,te);
  if (strncmp(str,"##$TD",5)==0) sscanf(str,"%s %d",str2,npts);
  if (strncmp(str,"##$SW_h",7)==0) sscanf(str,"%s %f",str2,sw);
  if (strncmp(str,"##$BYTORDA",10)==0) sscanf(str,"%s %d",str2,bytorda); 
  if (strncmp("##END=",str,6)==0) quit=1;
}

fclose(acqptr);
}

int INT_little_endian_TO_big_endian(int i)
{
    return((i&0xff)<<24)+((i&0xff00)<<8)+((i&0xff0000)>>8)+((i>>24)&0xff);
}
