38 #include "catalogue/casu_utils.h"
39 #include "casu_filt.h"
55 static void docols(
float *data,
unsigned char *bpm,
int nx,
int ny,
56 int filter,
int stat);
57 static void dorows(
float *data,
unsigned char *bpm,
int nx,
int ny,
58 int filter,
int stat);
59 static void wraparound(
float *data,
unsigned char *bpm,
int npts,
int nfilt,
60 int whichstat,
float **ybuf,
unsigned char **ybbuf,
62 static void medavg(
float *array,
unsigned char *bpm,
int *ipoint,
int npix,
63 int whichstat,
int newl, nextlast *nl,
float *outval,
64 unsigned char *outbp);
65 static void quickie(
float *array,
unsigned char *iarray,
int *iarray2,
67 static void sortm(
float *a1,
unsigned char *a2,
int *a3,
int n);
68 static void plugholes(
float *data,
unsigned char *bpm,
int nx);
114 extern void casu_bfilt(
float *data,
unsigned char *bpm,
int nx,
int ny,
115 int filt,
int stat,
int axis) {
121 dorows(data,bpm,nx,ny,filt,stat);
122 docols(data,bpm,nx,ny,filt,stat);
124 docols(data,bpm,nx,ny,filt,stat);
125 dorows(data,bpm,nx,ny,filt,stat);
162 static void docols(
float *data,
unsigned char *bpm,
int nx,
int ny,
163 int filter,
int stat) {
165 unsigned char *goodval,*bbuf;
176 dbuf = cpl_malloc(nbuf*
sizeof(*dbuf));
177 bbuf = cpl_malloc(nbuf*
sizeof(*bbuf));
178 goodval = cpl_malloc(ny*
sizeof(*goodval));
182 for (k = 0; k < nx; k++) {
183 memset((
char *)goodval,0,ny);
184 for (j = 0; j < ny; j++) {
186 dbuf[j] = data[indx];
192 casu_dostat(dbuf,bbuf,goodval,ny,filter,stat);
193 plugholes(dbuf,goodval,ny);
197 for (j = 0; j < ny; j++) {
199 data[indx] = dbuf[j];
242 static void dorows(
float *data,
unsigned char *bpm,
int nx,
int ny,
243 int filter,
int stat) {
245 unsigned char *goodval,*bbuf;
256 dbuf = cpl_malloc(nbuf*
sizeof(*dbuf));
257 bbuf = cpl_malloc(nbuf*
sizeof(*bbuf));
258 goodval = cpl_malloc(nx*
sizeof(*goodval));
262 for (k = 0; k < ny; k++) {
263 memset((
char *)goodval,0,nx);
264 for (j = 0; j < nx; j++) {
266 dbuf[j] = data[indx];
272 casu_dostat(dbuf,bbuf,goodval,nx,filter,stat);
273 plugholes(dbuf,goodval,nx);
277 for (j = 0; j < nx; j++) {
279 data[indx] = dbuf[j];
322 extern void casu_dostat(
float *data,
unsigned char *bpm,
323 unsigned char *goodval,
int npts,
int nfilt,
325 int nbuf,jl,jh,j,*ipoint,ifree,i;
326 unsigned char *ybbuf,*barray,bval;
327 float *ybuf,*darray,val;
332 if ((nfilt/2)*2 == nfilt)
337 wraparound(data,bpm,npts,nfilt,whichstat,&ybuf,&ybbuf,&nbuf);
341 darray = cpl_malloc(nfilt*
sizeof(*darray));
342 barray = cpl_malloc(nfilt*
sizeof(*barray));
343 ipoint = cpl_malloc(nfilt*
sizeof(*ipoint));
344 memmove((
char *)darray,(
char *)ybuf,nfilt*
sizeof(*ybuf));
345 memmove((
char *)barray,(
char *)ybbuf,nfilt*
sizeof(*ybbuf));
346 for (j = 0; j < nfilt; j++)
352 medavg(darray,barray,ipoint,nfilt,whichstat,-1,&nl,&val,&bval);
361 jh = nfilt + npts - 2;
362 for (j = jl; j <= jh; j++) {
363 for (i = 0; i < nfilt; i++) {
364 if (ipoint[i] == 0) {
366 ipoint[i] = nfilt - 1;
367 nl.lastval = darray[ifree];
370 if (barray[ifree] == 0) {
374 darray[ifree] = ybuf[j];
375 barray[ifree] = ybbuf[j];
376 nl.nextval = darray[ifree];
379 if (barray[ifree] == 0) {
386 medavg(darray,barray,ipoint,nfilt,whichstat,ifree,&nl,&val,&bval);
389 goodval[j-jl+1] = bval;
438 static void wraparound(
float *data,
unsigned char *bpm,
int npts,
int nfilt,
439 int whichstat,
float **ybuf,
unsigned char **ybbuf,
int *nbuf) {
441 float *darray,xmns,xmnf;
442 int i1,ilow,i,*ipoint;
443 unsigned char *barray,bxmns,bxmnf;
449 ilow = max(3,nfilt/4);
450 ilow = (ilow/2)*2 + 1;
454 darray = cpl_malloc(nfilt*
sizeof(*darray));
455 barray = cpl_malloc(nfilt*
sizeof(*barray));
456 ipoint = cpl_calloc(nfilt,
sizeof(*ipoint));
458 *ybuf = cpl_malloc(*nbuf*
sizeof(
float));
459 *ybbuf = cpl_malloc(*nbuf*
sizeof(
unsigned char));
463 memmove((
char *)darray,(
char *)data,ilow*
sizeof(*data));
464 memmove((
char *)barray,(
char *)bpm,ilow*
sizeof(*bpm));
465 medavg(darray,barray,ipoint,ilow,whichstat,-1,&nl,&xmns,&bxmns);
466 memmove((
char *)darray,(
char *)(data+npts-ilow),ilow*
sizeof(*data));
467 memmove((
char *)barray,(
char *)(bpm+npts-ilow),ilow*
sizeof(*bpm));
468 medavg(darray,barray,ipoint,ilow,whichstat,-1,&nl,&xmnf,&bxmnf);
469 for (i = 0; i < i1; i++) {
471 (*ybuf)[i] = 2.0*xmns - data[i1+ilow-i-1];
472 (*ybbuf)[i] = bpm[i1+ilow-i-1];
474 (*ybuf)[i] = data[i1+ilow-i-1];
478 (*ybuf)[npts+i1+i] = 2.0*xmnf - data[npts-i-ilow-1];
479 (*ybbuf)[npts+i1+i] = bpm[npts-i-ilow-1];
481 (*ybuf)[npts+i1+i] = data[npts-i-ilow-1];
482 (*ybbuf)[npts+i1+i] = 1;
488 memmove((
char *)(*ybuf+i1),data,npts*
sizeof(*data));
489 memmove((
char *)(*ybbuf+i1),bpm,npts*
sizeof(*bpm));
537 static void medavg(
float *array,
unsigned char *bpm,
int *ipoint,
int npix,
538 int whichstat,
int newl, nextlast *nl,
float *outval,
539 unsigned char *outbp) {
549 if (whichstat == MEDIANCALC) {
551 sortm(array,bpm,ipoint,npix);
553 quickie(array,bpm,ipoint,newl,npix);
557 buf = cpl_malloc(npix*
sizeof(*buf));
562 for (i = 0; i < npix; i++) {
574 }
else if (whichstat == MEANCALC) {
579 for (i = 0; i < npix; i++) {
588 nl->sum += (nl->nextw*nl->nextval - nl->lastw*nl->lastval);
589 nl->sumw += (nl->nextw - nl->lastw);
590 nl->naver += (nl->nextc - nl->lastc);
600 if (whichstat == MEDIANCALC)
606 if (whichstat == MEDIANCALC) {
608 *outval = 0.5*(buf[(m/2)-1] + buf[m/2]);
612 }
else if (whichstat == MEANCALC)
613 *outval = (nl->sum)/(nl->sumw);
644 static void quickie(
float *array,
unsigned char *iarray,
int *iarray2,
645 int testloc,
int narray) {
651 test = array[testloc];
652 it = iarray[testloc];
653 it2 = iarray2[testloc];
655 for (i = 0; i < narray; i++) {
656 if (i != testloc && test <= array[i]) {
663 if (j - 1 == testloc)
666 if (j - testloc < 0) {
668 for (i = 0; i < npt; i++) {
669 array[testloc-i] = array[testloc-i-1];
670 iarray[testloc-i] = iarray[testloc-i-1];
671 iarray2[testloc-i] = iarray2[testloc-i-1];
680 for (i = 0; i < npt; i++) {
681 array[testloc+i] = array[testloc+i+1];
682 iarray[testloc+i] = iarray[testloc+i+1];
683 iarray2[testloc+i] = iarray2[testloc+i+1];
716 static void sortm(
float *a1,
unsigned char *a2,
int *a3,
int n) {
717 int iii,ii,i,ifin,j,b3;
724 iii = min(n,(3*iii)/4 - 1);
729 for (ii = 0; ii < ifin; ii++) {
742 if (i < 0 || a1[i] <= b1)
775 static void plugholes(
float *data,
unsigned char *bpm,
int nx) {
776 int i,ifirst,ilast,i1,i2,j;
777 float nc,d1,d2,t1,t2,slope;
782 while (i < nx && bpm[i] != 0)
794 while (i >= 0 && bpm[i] != 0)
811 nc = (float)(i2 - i1 + 1);
814 for (j = i1+1; j <= i2-1; j++) {
815 t1 = 1.0 - (float)(j - i1)/nc;
817 data[j] = t1*d1 + t2*d2;
824 slope = data[ifirst+1] - data[ifirst];
825 for (j = 0; j < ifirst; j++)
826 data[j] = slope*(j - ifirst) + data[ifirst];
831 if (ilast < nx - 1) {
832 slope = data[ilast] - data[ilast-1];
833 for (j = ilast; j < nx; j++)
834 data[j] = slope*(j - ilast) + data[ilast];