/* This program creates a histogram of the last byte of a single channel. Prints output to stdout */ #include #include #include #include #include "FrLib/FrameL.h" /* for storing command line arguments */ typedef struct tagCmdArgs { char chanlist[500]; char filelist[500]; char destdir[500]; int debuglevel; } CmdArgs; /* for some link lists */ typedef struct tagFileList { char filename[500]; struct tagFileList *next; } FileList; typedef struct tagChanList { char channelname[500]; struct tagChanList *next; } ChanList; typedef struct tagHistoGram { unsigned long int databin[256]; struct tagHistoGram *next; } HistoGram; /* Function Prototypes */ int ExamineAdc(FrAdcData*,FILE*,int,char*); int ExamineTOC(FrTOC*,FILE*,int); int MyFrVectHistoGram(FrVect*,FILE*,int,HistoGram*); CmdArgs *ReadCommandLine(int,char**); void PrintUsage(void); FileList *GenerateFileList(char*); ChanList *GenerateChanList(char*); /* main */ int main(int argc, char **argv) { int i,histocount; char filename[500]; char channelname[500]; char destdir[500]; char histofile[504]; HistoGram *histo,*roothisto; FILE *inputfile,*outputfile; FrFile *frfile; FrameH *frame; FrAdcData *adc; FrTOCts *ts; FrTOC *toc; CmdArgs *cmdargs; FileList *filelist; ChanList *chanlist,*rootchanlist; cmdargs = ReadCommandLine(argc,argv); cmdargs->debuglevel = 1; strcpy(destdir,cmdargs->destdir); filelist = GenerateFileList(cmdargs->filelist); rootchanlist = GenerateChanList(cmdargs->chanlist); roothisto = (HistoGram*)calloc(1,sizeof(HistoGram)); roothisto->next=NULL; histo = roothisto; /* allocate histogram list */ chanlist = rootchanlist; while( chanlist != NULL ) { chanlist = chanlist -> next; if(chanlist != NULL) { histo->next = (HistoGram*)calloc(1,sizeof(HistoGram)); histo = histo->next; histo->next = NULL; } } chanlist = rootchanlist; histo = roothisto; while( filelist != NULL ) { strncpy(filename,filelist->filename,500); frfile = FrFileINew(filename); if(frfile == NULL) { fprintf(stderr,"Error _opening_ file, %s",filename); } frame = FrameRead(frfile); if(frame == NULL) { fprintf(stderr,"Error _reading_ frame file, %s",filename); exit(2); } printf("Examining file: %s\n",filename); chanlist = rootchanlist; histo = roothisto; while(chanlist != NULL) { strncpy(channelname,chanlist->channelname,500); printf("Examining channel: %s\n",channelname); /* ADC part */ adc = FrAdcDataFind(frame,channelname); if(adc == NULL) { fprintf(stderr,"%s: Cannot find ADC channel, %s\n",filename,channelname); } else { /* see if this channels contain numbers with fractions */ MyFrVectHistoGram( adc -> data,stdout,0,histo); } printf("\n"); chanlist = chanlist->next; histo = histo -> next; } printf("\n"); FrameFree(frame); FrFileIEnd(frfile); filelist = filelist->next; } /* print the info into one file per channel */ chanlist = rootchanlist; histo = roothisto; while(chanlist != NULL && histo != NULL) { sprintf(histofile,"%s/%s.his",destdir,chanlist->channelname); outputfile = fopen(histofile,"w"); if(outputfile == NULL) { fprintf(stderr,"Cannot open output file %s\n",histofile); exit(2); } for(i=0;i < 256;i++) { fprintf(outputfile,"%d\t%d\n",i,histo->databin[i]); } fclose(outputfile); fprintf(stderr,"Wrote %s\n",histofile); chanlist = chanlist -> next; histo = histo -> next; } exit(0); } /* End main */ CmdArgs* ReadCommandLine(int argc, char **argv) { int i,debuglvl; CmdArgs *cli; if(argc < 2 || argc > 7) { fprintf(stderr,"Invalid number of arguments.\n"); PrintUsage(); exit(1); } cli = (CmdArgs*)malloc(sizeof(CmdArgs)); for(i=1;i argc + 1) { fprintf(stderr,"Must specify a filelist.\n"); PrintUsage(); exit(1); } else { strncpy(cli->filelist,argv[i],500); if(strlen(cli->filelist) < 1) { fprintf(stderr,"Must specify a filelist.\n"); PrintUsage(); exit(1); } } } else if( strcmp(argv[i],"--chanlist") == 0 ) { i++; if(i > argc + 1) { fprintf(stderr,"Must specify a channel list.\n"); PrintUsage(); exit(1); } else { strncpy(cli->chanlist,argv[i],500); if(strlen(cli->chanlist) < 1) { fprintf(stderr,"Must specify a channel list.\n"); PrintUsage(); exit(1); } } } else if(strcmp(argv[i],"--destdir") == 0) { i++; if(i > argc + 1) { fprintf(stderr,"Must specify destination directory for histogram files.\n"); PrintUsage(); exit(1); } else { strncpy(cli->destdir,argv[i],500); if(strlen(cli->destdir) < 1) { fprintf(stderr,"Must specify a destination directory\n"); PrintUsage(); exit(1); } } } else { fprintf(stderr,"Invalid command line argument: %s\n",argv[i]); exit(1); } } return cli; } void PrintUsage(void) { printf("Usage: ./command --filelist FILELIST.txt --chanlist CHANLIST.txt \\\n--destdir DESTDIR\n"); printf(" FILELIST.txt: A file listing the filenames to examine, one file per line.\n"); printf(" Files will be examined in the order listed.\n\n"); printf(" CHANLIST.txt: A file containing a list of channels, one channel per line.\n\n"); printf(" DESTDIR: destination directory for histogram files\n\n"); } FileList* GenerateFileList(char*inputfile) { FILE* fin; int i=0; char curname[500]; FileList *filelist,*rootfilelist; fin = fopen(inputfile,"r"); if(fin == NULL) { fprintf(stderr,"Cannot open filelist, %s\n",inputfile); exit(2); } rootfilelist = (FileList*)malloc(sizeof(FileList)); fgets(curname,500,fin); /* eliminate \n in curname string */ for(i=0;curname[i] != '\0';i++) { if(curname[i] == '\n') { curname[i] = '\0'; break; } } strncpy(rootfilelist->filename,curname,500); rootfilelist->next = NULL; filelist = rootfilelist; i=0; while(fgets(curname,500,fin) != NULL) { /* eliminate \n in curname string */ for(i=0;curname[i] != '\0';i++) { if(curname[i] == '\n') { curname[i] = '\0'; break; } } filelist->next=(FileList*)malloc(sizeof(FileList)); filelist=filelist->next; strncpy(filelist->filename,curname,500); } filelist->next = NULL; return rootfilelist; } ChanList* GenerateChanList(char* inputfile) { FILE* fin; int i=0; char curname[500]; ChanList *chanlist,*rootchanlist; fin = fopen(inputfile,"r"); if(fin == NULL) { fprintf(stderr,"Cannot open chanlist, %s\n",inputfile); exit(2); } rootchanlist = (ChanList*)malloc(sizeof(ChanList)); fgets(curname,500,fin); /* eliminate \n in curname string */ for(i=0;curname[i] != '\0';i++) { if(curname[i] == '\n') { curname[i] = '\0'; break; } } strncpy(rootchanlist->channelname,curname,500); rootchanlist->next = NULL; chanlist = rootchanlist; while(fgets(curname,500,fin) != NULL) { /* eliminate \n in curname string */ for(i=0;curname[i] != '\0';i++) { if(curname[i] == '\n') { curname[i] = '\0'; break; } } chanlist->next=(ChanList*)malloc(sizeof(ChanList)); chanlist=chanlist->next; strncpy(chanlist->channelname,curname,500); } chanlist->next = NULL; return rootchanlist; } int MyFrVectHistoGram(FrVect *vect,FILE *fp,int debugLvl,HistoGram* histo) {FRULONG i, nData, inValid; char *dC, **dSt; short *dS; int *dI; FRLONG *dL; float *dF, ratio; double *dD; unsigned char *dU; unsigned short *dUS; unsigned int *dUI; FRULONG *dUL; union my_fp_union { float fppart; unsigned char bytepart[4]; } fp_union; union my_dp_union { double dppart; unsigned char bytepart[8]; } dp_union; if(fp == NULL) return; if(vect == NULL) return; //if(debugLvl < 1) return; nData = vect->nData; if(vect->name == NULL) {fprintf(fp," Vector:- ndata=%"FRLLD, nData);} else {fprintf(fp," Vector:%s ndata=%"FRLLD, vect->name, nData);} if(vect->GTime != 0) fprintf(fp," GTime=%.5f",vect->GTime); if(vect->unitY != NULL) {fprintf(fp," unitY=%s", vect->unitY);} if(vect->nDim == 1) {if(vect->unitX[0] != NULL) fprintf(fp," unitX=%s", vect->unitX[0]); fprintf(fp," startX=%g dx=%g\n", vect->startX[0], vect->dx[0]);} else {fprintf(fp," nDim=%d", vect->nDim); for(i=0; inDim; i++) {if(vect->unitX[i] != NULL) fprintf(fp," unit=%s", vect->unitX[i]); fprintf(fp," Dimension=%"FRLLD" nx=%10"FRLLD" startX=%.2g dx=%g\n", i,vect->nx[i], vect->startX[i], vect->dx[i]);}} /*-------------------------------------- data part ---------------*/ /* DEBUG */ printf("vect -> type == 0x%x\n",vect->type); /* END DEBUG*/ if(vect->compress == 0) { /* DEBUG */ printf("vector not compressed\n"); /* END DEBUG */ if (vect->type == FR_VECT_4R) /*----------------- float-------------*/ { dF = (float *) vect->data; dI = (int *) vect->data; if(1) { inValid = FrVectIsValid(vect); for(i=0; idatabin[ fp_union.bytepart[3]] ++; /* if LITTLE ENDIAN */ /*histo->databin[ fp_union.bytepart[0]] ++; */ } } } else if(vect->type == FR_VECT_8R) /*---------------- double ------------*/ {dD = (double *) vect->data; fprintf(fp,"(double) %s\n",FrVectStat(vect)); if(1) { for(i=0; i databin[ dp_union.bytepart[7] ] ++ ; /* if LITTLE ENDIAN */ /*histo -> databin[ dp_union.bytepart[0] ] ++ ; */ } } } else if(vect->type == FR_VECT_C) /*---------------one byte integer-----*/ {dC = vect->data; fprintf(fp,"(byte) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_2S) /*-------------- short integer -------*/ {dS = (short *) vect->data; fprintf(fp,"(short) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_8S) /*---------------long integer---------*/ {dL = (FRLONG *) vect->data; fprintf(fp,"(8S) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_4S) /*------------- signed integer -------*/ {dI = (int *) vect->data; fprintf(fp,"(int) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_1U) /*--------- unsigned character----*/ {dU = (unsigned char *)vect->dataU; fprintf(fp,"(1U) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_2U) /*-------------unsigned short -----*/ {dUS = (unsigned short *) vect->data; fprintf(fp,"(2U) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_8U) {dUL = (FRULONG *) vect->data; fprintf(fp,"(8U) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_4U) {dUI = (unsigned int *) vect->data; fprintf(fp,"(4U) %s\n",FrVectStat(vect)); if(debugLvl > 2) {for(i=0; itype == FR_VECT_8C) /*------------- complex float ------*/ {dF = (float *) vect->data; fprintf(fp,"(8C) %s\n", FrVectStat(vect)); if(debugLvl <3) {fprintf(fp," (%g,%g)", dF[0],dF[1]); if(nData > 1) fprintf(fp," (%g,%g) ...", dF[2],dF[3]); fprintf(fp,"\n");} else {for(i=0; itype == FR_VECT_16C) /*------------- complex double ------*/ {dD = (double *) vect->data; fprintf(fp,"(16C) %s\n", FrVectStat(vect)); if(debugLvl <3) {fprintf(fp," (%g,%g)",dD[0],dD[1]); if(nData > 1) fprintf(fp," (%g,%g) ...",dD[2],dD[3]); fprintf(fp,"\n");} else {for(i=0; itype == FR_VECT_8H) /*-------half complex float -------*/ {dF = (float *) vect->data; fprintf(fp,"(8H) %s\n", FrVectStat(vect)); if(debugLvl <3) {fprintf(fp," %g ", dF[0]); if(nData > 1) fprintf(fp,", (%g,%g) ...", dF[1],dF[nData-1]); fprintf(fp,"\n");} else {fprintf(fp,"%6d:(%12g %12g)", 0, dF[0], 0.); for(i=1; itype == FR_VECT_16H) /*----half complex double -------*/ {dD = (double *) vect->data; fprintf(fp,"(16H) %s\n", FrVectStat(vect)); if(debugLvl <3) {fprintf(fp," %g",dD[0]); if(nData > 1) fprintf(fp,", (%g,%g) ...\n",dD[1],dD[nData-1]); fprintf(fp,"\n");} else {fprintf(fp,"%6d:(%12g %12g)", 0, dD[0], 0.); for(i=1; itype == FR_VECT_STRING) {dSt = (char **) vect->data; fprintf(fp,"(STRING)"); if(dSt[0] != NULL) fprintf(fp," \"%s\"", dSt[0]); if(dSt[1] != NULL) fprintf(fp," \"%s\"", dSt[1]); fprintf(fp,"...\n");} else {fprintf(fp," unknown type: %d \n",vect->type );}} else {ratio = ((float) vect->nBytes) /(nData*vect->wSize); fprintf(fp,"\n the vector is %4.2f compressed (%x)" " nBytes=%"FRLLD" wSize=%d\n", ratio,vect->compress,vect->nBytes, vect->wSize);} if(vect->next != NULL) {fprintf(fp," Attached information:\n"); FrVectDump(vect->next, fp, debugLvl) ;} return 0; }