/* Labo MCA Data Convertor binary->text */ #include #include #include #include int maxchan=1024; // maximum of MCA channels int roil[100], roih[100]; // ROI table long databuf[8192]; /* Open files mode=0 open inputfile. mode=1 open outputfile (text mode). */ FILE *OpenFileFun(char *fname, int mode) { FILE *opf; if (mode==0){ if ((opf=fopen(fname,"rb"))==NULL){ printf("Can't open %s\n",fname); exit(1); } else return opf; } else { if ((opf=fopen(fname,"wt"))==NULL){ printf("Can't create %s\n",fname); exit(1); } else return opf; } } void initroi(void) { int i=0; for(i=0; i<100; i++){ roil[i]=0; roih[i]=0; } return ; } /* Convert 2 char -> int */ int ctoint(char a, char b) { int x=0, low=0, high=0; high=b; low=a; high=high<<8; high=high&0x0000ff00; low=low&0x000000ff; x=high+low; return x; } /* Convert 4 char -> long */ long ctolong(char a, char b, char c, char d) { int ll=0, lh=0, hl=0, hh=0; long x=0; hh=d; hl=c; lh=b; ll=a; hh=hh << 24; hh=hh&0xff000000; hl=hl << 16; hl=hl&0x00ff0000; lh=lh << 8; lh=lh&0x0000ff00; ll=ll&0x000000ff; x=(long)(lh+ll+hh+hl); return x; } /* read header from input datafile. */ int HeadRead(FILE *fph, char *buf1, float *buf2, char *buf3) { if (fread(buf1, sizeof(char), 64, fph)==0) return 1; else if (fread(buf2, sizeof(float), 3, fph)==0) return 2; else if (fread(buf3, sizeof(char), 948, fph)==0) return 3; else return 0; } /* scanning ROI table & count of ROI */ int ProcROI(char *buf3) { int i=0, j=0; initroi(); for(i=0; i<100; i++){ roil[i]=ctoint(buf3[18+2*i], buf3[18+1+2*i]); roih[i]=ctoint(buf3[218+2*i], buf3[218+1+2*i]); if(roil[i]>roih[i]){ roih[i]=maxchan-1; } } j=0; while(roil[j]!=0||roih[j]!=0){ j=j+1; } return j; } /* sumup inner ROI */ long SumROI(int k) { int j=0; long sum=0; sum=0; for(j=roil[k]; j0||c<-64) comment1[i]=c; } fprintf(fpp,"Comment1:\n"); fprintf(fpp,"%s\n",comment1); fprintf(fpp,"\n"); return 0; } /* check output file permission. */ char *OutFileName(char *oname) { int cflag=0, sflag=0; do{ if (access(oname, F_OK)!=-1){ printf("%s already exist.\n",oname); printf("Overwrite ? (Y/other) "); rewind(stdin); cflag=getchar(); if (cflag=='y'||cflag=='Y'){ if (access(oname, W_OK)==-1){ printf("permission deny about %s. \n",oname); exit(1); } else sflag=1; } else { printf("new file name = ? "); scanf("%s", oname); } } else sflag=1; } while(sflag==0); return oname; } int main(int argc, char *argv[]) /* Labo data -> text */ { FILE *fpi, *fpo, *fpl; // char *buf1 ,aa; char buf1[65]; float buf2[4]; char buf3[950]; // char name1[]=" "; char name1[256]; char name2[256]; char name3[256]; long *lbuf, bb; int i=0,j=0,k=0,nro=0,fnum=0,kk=0; // buf1=&aa; *buf1=0; buf1[64]='\0'; buf2[3]='\0'; buf3[949]='\0'; lbuf=&bb; *lbuf=0; if (argc<2) { printf("data file name = ? "); scanf("%s",name1); fnum=1; } else fnum=argc-1; for(kk=0; kk1){ strcpy(name1, argv[kk+1]); } for(i=0; i<8192; i++){ databuf[i]=0; } fpi=OpenFileFun(name1,0); strcpy(name2, name1); strcat(name2,".txt"); strcpy(name3, name1); strcat(name3,".log"); strcpy(name2,OutFileName(name2)); strcpy(name3,OutFileName(name3)); fpo=OpenFileFun(name2,1); fpl=OpenFileFun(name3,1); if (HeadRead(fpi, buf1, buf2, buf3)!=0){ printf("datafile is invalid!\n"); exit(1); } else printf("%s\n", buf1); maxchan=ctoint(buf1[54], buf1[55]); printf("This is a data of %d channels.\n", maxchan); printf("Please hit hey ! \n"); rewind(stdin); getchar(); nro=ProcROI(buf3); j=0; while(fread(lbuf, sizeof(long), 1, fpi)!=0){ fprintf(fpo, "%d, %ld\n", j, (*lbuf)); databuf[j]=(*lbuf); j=j+1; } fclose(fpo); fclose(fpi); if (ProcHead(fpl, buf2, buf3, nro)!=0){ printf("error !? \n"); exit(1); } fclose(fpl); } exit(0); }