#ifdef __cplusplus extern "C" { #endif #include "sz.h" void exafelSZ_params_process(exafelSZ_params*pr, size_t panels, size_t rows, size_t cols){ pr->binnedRows=(rows+pr->binSize-1)/pr->binSize; pr->binnedCols=(cols+pr->binSize-1)/pr->binSize; pr->peakRadius=(pr->peakSize-1)/2; } void exafelSZ_params_checkDecomp(exafelSZ_params*pr, size_t panels, size_t rows, size_t cols){ if(pr->calibPanel==NULL){ printf("ERROR: calibPanel is NULL : calibPanel=%ld\n",(long)pr->calibPanel); assert(0); } if(pr->binSize<1 || pr->tolerance<0 || pr->szDim<1 || pr->szDim>3){ printf("ERROR: Something wrong with the following:\n"); printf("binSize=%d\n",(int)pr->binSize); printf("tolerance=%d\n",(int)pr->tolerance); printf("szDim=%d\n",(int)pr->szDim); assert(0); } if(!(pr->peakSize%2)){ printf("ERROR: peakSize = %d cannot be even. It must be odd!\n",(int)pr->peakSize); assert(0); } //if(nEvents<1 || panels<1 || rows<1 || cols<1){ if(panels<1 || rows<1 || cols<1){ printf("ERROR: Something wrong with the following:\n"); printf("panels=%d\n",(int)panels); printf("rows=%d\n",(int)rows); printf("cols=%d\n",(int)cols); assert(0); } } void exafelSZ_params_checkComp(exafelSZ_params*pr, size_t panels, size_t rows, size_t cols){ if(pr->peaksSegs==NULL || pr->peaksRows==NULL || pr->peaksCols==NULL){ printf("ERROR: One or more of the following are NULL : peaksSegs , peaksRows , peaksCols\n"); assert(0); } exafelSZ_params_checkDecomp(pr, panels, rows, cols); } void exafelSZ_params_print(exafelSZ_params*pr){ printf("Configuration (exafelSZ_params) :\n"); printf("binSize: %d\n",pr->binSize); printf("tolerance:%e\n",pr->tolerance); printf("szDim:%d\n",pr->szDim); printf("peakSize:%d\n",pr->peakSize); //printf("nEvents:%d\n",pr->nEvents); //printf("panels:%d\n",pr->panels); //printf("rows:%d\n",pr->rows); //printf("cols:%d\n",pr->cols); printf("\n"); printf("CALCULATED VARIABLES\n"); printf("binnedRows:%ld\n",pr->binnedRows); printf("binnedCols:%ld\n",pr->binnedCols); printf("peakRadius:%d\n",pr->peakRadius); printf("\n"); // outs<<"Configuration (exafelSZ_params) : "<binnedRows*pr->binnedCols*sizeof(float)) ; //float *binnedData=(float*)malloc(nEvents*panels*rows*cols*sizeof(float)) ; size_t e,p,r,c,pk,ri,ci,br,bc,roii,bi; /* printf("AMG : exafelSZ_Compress : pr->numPeaks = %d\n",pr->numPeaks); printf("S:\n"); for(e=0;enumPeaks;e++) printf("%d ",pr->peaksSegs[e]); printf("\nR:\n"); for(e=0;enumPeaks;e++) printf("%d ",pr->peaksRows[e]); printf("\nC:\n"); for(e=0;enumPeaks;e++) printf("%d ",pr->peaksCols[e]); printf("\n"); */ //Generate the ROI mask: NOTE: 0 means affirmative in ROI mask! This comes from the python scripts! //First, initialize with calibration panel: for(e=0;ecalibPanel[calcIdx_2D(r,c,cols)]; //calibPanel is a single segment copied over all the event(image) roiM[calcIdx_4D(e,p,r,c,panels,rows,cols)]=pr->calibPanel[calcIdx_3D(p,r,c,rows,cols)]; //calibPanel is as big as the event(image) itself } } } } //uint64_t peaksBytePos=0; //Position in the peaks buffer //Now process the peaks and generate the mask: uint64_t nPeaksTotal=0; //Total number of peaks for(e=0;epeaks[peaksBytePos]); //peaksBytePos+=8; //peaksBytePos+=8;//Skip the second one! This is due to the problem in Python. nPeaksTotal+=pr->numPeaks; for(pk=0;pknumPeaks;pk++){ //uint16_t p_=*(uint16_t*)(&pr->peaks[peaksBytePos]); //Panel for the current peak //peaksBytePos+=2; //uint16_t r_=*(uint16_t*)(&pr->peaks[peaksBytePos]); //Row for the current peak //peaksBytePos+=2; //uint16_t c_=*(uint16_t*)(&pr->peaks[peaksBytePos]); //Col for the current peak //peaksBytePos+=2; uint16_t p_=pr->peaksSegs[pk]; uint16_t r_=pr->peaksRows[pk]; uint16_t c_=pr->peaksCols[pk]; if(p_>=panels){ printf("ERROR: Peak coordinate out of bounds: Panel=%d, Valid range: 0,%d\n",(int)p_,(int)panels-1); assert(0); printf("Skipping this peak...\n"); continue; } if(r_>=rows){ printf("ERROR: Peak coordinate out of bounds: Row=%d, Valid range: 0,%d\n",(int)r_,(int)rows-1); assert(0); printf("Skipping this peak...\n"); continue; } if(c_>=cols){ printf("ERROR: Peak coordinate out of bounds: Col=%d, Valid range: 0,%d\n",(int)c_,(int)cols-1); assert(0); printf("Skipping this peak...\n"); continue; } for(ri=r_-pr->peakRadius;ri<=r_+pr->peakRadius;ri++){ //ri: row index. Just a temporary variable. for(ci=c_-pr->peakRadius;ci<=c_+pr->peakRadius;ci++){ //ci: column index. Just a temporary variable. if(ribinnedRows;r++){ //Row of the binnedData for(c=0;cbinnedCols;c++){ //Column of the binnedData float sum=0; int nPts=0; for(br=0;brbinSize;br++) //Bin Row (from origData) for(bc=0;bcbinSize;bc++) //Bin Column (from origData) if(r*pr->binSize+brbinSize+bcbinSize+br<<" "<binSize+bc<binSize+br,c*pr->binSize+bc,panels,rows,cols)]; nPts++; } // cout<<"p:"<binnedRows,pr->binnedCols)]=sum/nPts; } } } } //Additional compression using SZ: size_t szCompressedSize=0; unsigned char* szComp; switch(pr->szDim){ case 1: // szComp=sz_compress_3D(binnedData, 0, 0, nEvents * panels * pr->binnedRows * pr->binnedCols, pr->tolerance, szCompressedSize); //1D szComp=SZ_compress_args(SZ_FLOAT, binnedData, &szCompressedSize, SZ_ABS, pr->tolerance, 0, 0, 0, 0,0,0, nEvents * panels * pr->binnedRows * pr->binnedCols); break; case 2: // szComp=sz_compress_3D(binnedData, 0, nEvents * panels * pr->binnedRows, pr->binnedCols, pr->tolerance, szCompressedSize); //2D szComp=SZ_compress_args(SZ_FLOAT, binnedData, &szCompressedSize, SZ_ABS, pr->tolerance, 0, 0, 0, 0,0, nEvents * panels * pr->binnedRows, pr->binnedCols); break; case 3: // szComp=sz_compress_3D(binnedData, nEvents * panels, pr->binnedRows, pr->binnedCols, pr->tolerance, szCompressedSize); //3D szComp=SZ_compress_args(SZ_FLOAT, binnedData, &szCompressedSize, SZ_ABS, pr->tolerance, 0, 0, 0, 0, nEvents * panels, pr->binnedRows, pr->binnedCols); break; default: printf("ERROR: Wrong szDim : %d It must be 1,2 or 3.\n",(int)pr->szDim); assert(0); } /* Compressed buffer format: (Types are indicated in parenthesis) WRITE: nPeaksTotal(uint64_t) (Total number of peaks in this batch) for(e=0;epeaks[peaksBytePos]); //peaksBytePos+=8; ////peaksBytePos+=8;//Skip the second one. This is due to the error in Python! //*(uint64_t*)(&compressedBuffer[bytePos])=nPeaks; *(uint64_t*)(&compressedBuffer[bytePos])=pr->numPeaks; bytePos+=8; //for(pk=0;pknumPeaks;pk++){ //*(uint16_t*)(&compressedBuffer[bytePos])=*(uint16_t*)(&pr->peaks[peaksBytePos]); //Panel for the current peak //bytePos+=2; //peaksBytePos+=2; //*(uint16_t*)(&compressedBuffer[bytePos])=*(uint16_t*)(&pr->peaks[peaksBytePos]); //Row for the current peak //bytePos+=2; //peaksBytePos+=2; //*(uint16_t*)(&compressedBuffer[bytePos])=*(uint16_t*)(&pr->peaks[peaksBytePos]); //Column for the current peak //bytePos+=2; //peaksBytePos+=2; *(uint16_t*)(&compressedBuffer[bytePos])=pr->peaksSegs[pk]; //Panel for the current peak bytePos+=2; *(uint16_t*)(&compressedBuffer[bytePos])=pr->peaksRows[pk]; //Row for the current peak bytePos+=2; *(uint16_t*)(&compressedBuffer[bytePos])=pr->peaksCols[pk]; //Column for the current peak bytePos+=2; } } // cout<<"peaks"<=rows*cols){ printf("ERROR: calcIdx_2D(r,c,cols) = calcIdx_2D(%d,%d,%d) = %d",(int)r,(int)c,(int)cols,(int)calcIdx_2D(r,c,cols)); printf(" is NOT in the correct range: [0,%ld]",(int)rows*cols-1); assert(0); } if(calcIdx_4D(e,p,r,c,panels,rows,cols)<0 ||calcIdx_4D(e,p,r,c,panels,rows,cols)>=nEvents*panels*rows*cols){ printf("ERROR: calcIdx_4D(e,p,r,c,panels,rows,cols) = calcIdx_4D(%d,%d,%d,%d,%d,%d,%d) = %d",(int)e,(int)p,(int)r,(int)c,(int)panels,(int)rows,(int)cols,(int)calcIdx_4D(e,p,r,c,panels,rows,cols)); assert(0); } //roiM[calcIdx_4D(e,p,r,c,panels,rows,cols)]=pr->calibPanel[calcIdx_2D(r,c,cols)]; //calibPanel is a single segment copied over all the event(image) roiM[calcIdx_4D(e,p,r,c,panels,rows,cols)]=pr->calibPanel[calcIdx_3D(p,r,c,rows,cols)]; //calibPanel is as big as the event(image) itself } } } } uint64_t peaksBytePos=0; //Position in the peaks buffer //Now process the peaks and generate the mask: for(e=0;e=panels){ printf("ERROR: Peak coordinate out of bounds: Panel=%d, Valid range: 0,%d\n",(int)p_,(int)panels-1); assert(0); printf("Skipping this peak...\n"); continue; } if(r_>=rows){ printf("ERROR: Peak coordinate out of bounds: Row=%d, Valid range: 0,%d\n",(int)r_,(int)rows-1); assert(0); printf("Skipping this peak...\n"); continue; } if(c_>=cols){ printf("ERROR: Peak coordinate out of bounds: Col=%d, Valid range: 0,%d\n",(int)c_,(int)cols-1); assert(0); printf("Skipping this peak...\n"); continue; } for(ri=r_-pr->peakRadius;ri<=r_+pr->peakRadius;ri++){ //ri: row index. Just a temporary variable. for(ci=c_-pr->peakRadius;ci<=c_+pr->peakRadius;ci++){ //ci: column index. Just a temporary variable. if(ri>=0 && ri=0 && ciszDim){ case 1: szDecomp=SZ_decompress(SZ_FLOAT,szComp,_szCompressedSize,0,0,0,0, nEvents * panels * pr->binnedRows * pr->binnedCols); break; case 2: szDecomp=SZ_decompress(SZ_FLOAT,szComp,_szCompressedSize,0,0,0, nEvents * panels * pr->binnedRows, pr->binnedCols); break; case 3: szDecomp=SZ_decompress(SZ_FLOAT,szComp,_szCompressedSize,0,0,nEvents * panels, pr->binnedRows, pr->binnedCols); break; default: printf("ERROR: Wrong szDim : %d It must be 1,2 or 3.\n",(int)pr->szDim); assert(0); } //szDecomp=(void*)malloc(nEvents*panels*rows*cols*sizeof(float)); // double max_err = 0; // for(int i=0; ibinnedRows * pr->binnedCols; i++){ // double err = fabs(szDecomp[i]-binnedData[i]); // if(err > max_err) max_err = err; // } // cout << "Max err = \t\t\t" << max_err << endl; //De-binning: for(e=0;ebinnedRows;r++) //Row of the binnedData for(c=0;cbinnedCols;c++) //Column of the binnedData for(br=0;brbinSize;br++) //Bin Row (from origData) for(bc=0;bcbinSize;bc++) //Bin Column (from origData) if(r*pr->binSize+brbinSize+bcbinSize+br,c*pr->binSize+bc,panels,rows,cols)] = szDecomp[calcIdx_4D(e,p,r,c,panels,pr->binnedRows,pr->binnedCols)]; } //Restore ROI: uint64_t current=0; for(e=0;e