39#include "catalogue/casu_utils.h"
56static void docols(
float *data,
unsigned char *bpm,
int nx,
int ny,
57 int filter,
int stat);
58static void dorows(
float *data,
unsigned char *bpm,
int nx,
int ny,
59 int filter,
int stat);
60static void wraparound(
float *data,
unsigned char *bpm,
int npts,
int nfilt,
61 int whichstat,
float **ybuf,
unsigned char **ybbuf,
63static void medavg(
float *array,
unsigned char *bpm,
int *ipoint,
int npix,
64 int whichstat,
int newl, nextlast *nl,
float *outval,
65 unsigned char *outbp);
66static void quickie(
float *array,
unsigned char *iarray,
int *iarray2,
68static void sortm(
float *a1,
unsigned char *a2,
int *a3,
int n);
69static void plugholes(
float *data,
unsigned char *bpm,
int nx);
115extern void casu_bfilt(
float *data,
unsigned char *bpm,
int nx,
int ny,
116 int filt,
int stat,
int axis) {
122 dorows(data,bpm,nx,ny,filt,stat);
123 docols(data,bpm,nx,ny,filt,stat);
125 docols(data,bpm,nx,ny,filt,stat);
126 dorows(data,bpm,nx,ny,filt,stat);
163static void docols(
float *data,
unsigned char *bpm,
int nx,
int ny,
164 int filter,
int stat) {
166 unsigned char *goodval,*bbuf;
175 assert(ny*
sizeof(*goodval) < PTRDIFF_MAX);
177 dbuf = cpl_malloc(nbuf*
sizeof(*dbuf));
178 bbuf = cpl_malloc(nbuf*
sizeof(*bbuf));
179 goodval = cpl_malloc((
size_t)ny*
sizeof(*goodval));
183 for (k = 0; k < nx; k++) {
184 memset((
char *)goodval,0,ny);
185 for (j = 0; j < ny; j++) {
187 dbuf[j] = data[indx];
193 casu_dostat(dbuf,bbuf,goodval,ny,filter,stat);
194 plugholes(dbuf,goodval,ny);
198 for (j = 0; j < ny; j++) {
200 data[indx] = dbuf[j];
243static void dorows(
float *data,
unsigned char *bpm,
int nx,
int ny,
244 int filter,
int stat) {
246 unsigned char *goodval,*bbuf;
257 dbuf = cpl_malloc(nbuf*
sizeof(*dbuf));
258 bbuf = cpl_malloc(nbuf*
sizeof(*bbuf));
259 goodval = cpl_malloc(nx*
sizeof(*goodval));
263 for (k = 0; k < ny; k++) {
264 memset((
char *)goodval,0,nx);
265 for (j = 0; j < nx; j++) {
267 dbuf[j] = data[indx];
273 casu_dostat(dbuf,bbuf,goodval,nx,filter,stat);
274 plugholes(dbuf,goodval,nx);
278 for (j = 0; j < nx; j++) {
280 data[indx] = dbuf[j];
323extern void casu_dostat(
float *data,
unsigned char *bpm,
324 unsigned char *goodval,
int npts,
int nfilt,
326 int nbuf,jl,jh,j,*ipoint,ifree,i;
327 unsigned char *ybbuf,*barray,bval;
328 float *ybuf,*darray,val;
333 if ((nfilt/2)*2 == nfilt)
338 wraparound(data,bpm,npts,nfilt,whichstat,&ybuf,&ybbuf,&nbuf);
342 darray = cpl_malloc(nfilt*
sizeof(*darray));
343 barray = cpl_malloc(nfilt*
sizeof(*barray));
344 ipoint = cpl_malloc(nfilt*
sizeof(*ipoint));
345 memmove((
char *)darray,(
char *)ybuf,nfilt*
sizeof(*ybuf));
346 memmove((
char *)barray,(
char *)ybbuf,nfilt*
sizeof(*ybbuf));
347 for (j = 0; j < nfilt; j++)
353 medavg(darray,barray,ipoint,nfilt,whichstat,-1,&nl,&val,&bval);
362 jh = nfilt + npts - 2;
363 for (j = jl; j <= jh; j++) {
364 for (i = 0; i < nfilt; i++) {
365 if (ipoint[i] == 0) {
367 ipoint[i] = nfilt - 1;
368 nl.lastval = darray[ifree];
371 if (barray[ifree] == 0) {
375 darray[ifree] = ybuf[j];
376 barray[ifree] = ybbuf[j];
377 nl.nextval = darray[ifree];
380 if (barray[ifree] == 0) {
387 medavg(darray,barray,ipoint,nfilt,whichstat,ifree,&nl,&val,&bval);
390 goodval[j-jl+1] = bval;
439static void wraparound(
float *data,
unsigned char *bpm,
int npts,
int nfilt,
440 int whichstat,
float **ybuf,
unsigned char **ybbuf,
int *nbuf) {
442 float *darray,xmns,xmnf;
443 int i1,ilow,i,*ipoint;
444 unsigned char *barray,bxmns,bxmnf;
450 ilow = max(3,nfilt/4);
451 ilow = (ilow/2)*2 + 1;
455 darray = cpl_malloc(nfilt*
sizeof(*darray));
456 barray = cpl_malloc(nfilt*
sizeof(*barray));
457 ipoint = cpl_calloc(nfilt,
sizeof(*ipoint));
459 *ybuf = cpl_malloc(*nbuf*
sizeof(
float));
460 *ybbuf = cpl_malloc(*nbuf*
sizeof(
unsigned char));
464 memmove((
char *)darray,(
char *)data,ilow*
sizeof(*data));
465 memmove((
char *)barray,(
char *)bpm,ilow*
sizeof(*bpm));
466 medavg(darray,barray,ipoint,ilow,whichstat,-1,&nl,&xmns,&bxmns);
467 memmove((
char *)darray,(
char *)(data+npts-ilow),ilow*
sizeof(*data));
468 memmove((
char *)barray,(
char *)(bpm+npts-ilow),ilow*
sizeof(*bpm));
469 medavg(darray,barray,ipoint,ilow,whichstat,-1,&nl,&xmnf,&bxmnf);
470 for (i = 0; i < i1; i++) {
472 (*ybuf)[i] = 2.0*xmns - data[i1+ilow-i-1];
473 (*ybbuf)[i] = bpm[i1+ilow-i-1];
475 (*ybuf)[i] = data[i1+ilow-i-1];
479 (*ybuf)[npts+i1+i] = 2.0*xmnf - data[npts-i-ilow-1];
480 (*ybbuf)[npts+i1+i] = bpm[npts-i-ilow-1];
482 (*ybuf)[npts+i1+i] = data[npts-i-ilow-1];
483 (*ybbuf)[npts+i1+i] = 1;
489 memmove((
char *)(*ybuf+i1),data,npts*
sizeof(*data));
490 memmove((
char *)(*ybbuf+i1),bpm,npts*
sizeof(*bpm));
538static void medavg(
float *array,
unsigned char *bpm,
int *ipoint,
int npix,
539 int whichstat,
int newl, nextlast *nl,
float *outval,
540 unsigned char *outbp) {
550 if (whichstat == MEDIANCALC) {
552 sortm(array,bpm,ipoint,npix);
554 quickie(array,bpm,ipoint,newl,npix);
557 assert((
size_t)npix*
sizeof(*buf) < PTRDIFF_MAX);
558 buf = cpl_malloc((
size_t)npix*
sizeof(*buf));
563 for (i = 0; i < npix; i++) {
575 }
else if (whichstat == MEANCALC) {
580 for (i = 0; i < npix; i++) {
589 nl->sum += (nl->nextw*nl->nextval - nl->lastw*nl->lastval);
590 nl->sumw += (nl->nextw - nl->lastw);
591 nl->naver += (nl->nextc - nl->lastc);
601 if (whichstat == MEDIANCALC)
607 if (whichstat == MEDIANCALC) {
609 *outval = 0.5*(buf[(m/2)-1] + buf[m/2]);
613 }
else if (whichstat == MEANCALC)
614 *outval = (nl->sum)/(nl->sumw);
645static void quickie(
float *array,
unsigned char *iarray,
int *iarray2,
646 int testloc,
int narray) {
652 test = array[testloc];
653 it = iarray[testloc];
654 it2 = iarray2[testloc];
656 for (i = 0; i < narray; i++) {
657 if (i != testloc && test <= array[i]) {
664 if (j - 1 == testloc)
667 if (j - testloc < 0) {
669 for (i = 0; i < npt; i++) {
670 array[testloc-i] = array[testloc-i-1];
671 iarray[testloc-i] = iarray[testloc-i-1];
672 iarray2[testloc-i] = iarray2[testloc-i-1];
681 for (i = 0; i < npt; i++) {
682 array[testloc+i] = array[testloc+i+1];
683 iarray[testloc+i] = iarray[testloc+i+1];
684 iarray2[testloc+i] = iarray2[testloc+i+1];
717static void sortm(
float *a1,
unsigned char *a2,
int *a3,
int n) {
718 int iii,ii,i,ifin,j,b3;
725 iii = min(n,(3*iii)/4 - 1);
730 for (ii = 0; ii < ifin; ii++) {
743 if (i < 0 || a1[i] <= b1)
776static void plugholes(
float *data,
unsigned char *bpm,
int nx) {
777 int i,ifirst,ilast,i1,i2,j;
778 float nc,d1,d2,t1,t2,slope;
783 while (i < nx && bpm[i] != 0)
795 while (i >= 0 && bpm[i] != 0)
812 nc = (float)(i2 - i1 + 1);
815 for (j = i1+1; j <= i2-1; j++) {
816 t1 = 1.0 - (float)(j - i1)/nc;
818 data[j] = t1*d1 + t2*d2;
825 slope = data[ifirst+1] - data[ifirst];
826 for (j = 0; j < ifirst; j++)
827 data[j] = slope*(j - ifirst) + data[ifirst];
832 if (ilast < nx - 1) {
833 slope = data[ilast] - data[ilast-1];
834 for (j = ilast; j < nx; j++)
835 data[j] = slope*(j - ilast) + data[ilast];