36 #include "floatmath.h"
38 #include "casu_fits.h"
40 #define FATAL_ERR(_a) {freetable(tab); cpl_msg_error(fctid,_a); tidy(); return(CASU_FATAL);}
44 static float *indata = NULL;
45 static int *confdata = NULL;
46 static float *confsqrt = NULL;
47 static float *smoothed = NULL;
48 static float *smoothedc = NULL;
49 static unsigned char *mflag = NULL;
51 static int freeconf = 0;
52 static float *incopy = NULL;
53 static int *ccopy = NULL;
55 static float weights[NW*NW];
56 static float weightc[NW*NW];
60 static void crweights(
float);
61 static void convolve(
int);
62 static void tidy(
void);
111 extern int imcore_opm(casu_fits *infile, casu_fits *conf,
int ipix,
112 float threshold,
int nbsize,
float filtfwhm,
115 int i,retval,j,nw2,iter,nclip,nxc,nyc,cattype,nobj,xcol,ycol;
116 float fconst,nullval,skymed,skysig,thresh,xintmin,offset;
117 float isat,isatbc,*current,junk,*currentc;
119 cpl_image *map,*cmap;
120 cpl_propertylist *plist;
123 const char *fctid =
"imcore_opm";
127 fconst = CPL_MATH_LOG2E;
135 if ((indata = cpl_image_get_data_float(map)) == NULL)
136 FATAL_ERR(
"Error getting image data");
137 nx = (long)cpl_image_get_size_x(map);
138 ny = (long)cpl_image_get_size_y(map);
145 if ((confdata = cpl_image_get_data(cmap)) == NULL)
146 FATAL_ERR(
"Error getting confidence map data");
147 nxc = (long)cpl_image_get_size_x(cmap);
148 nyc = (long)cpl_image_get_size_y(cmap);
149 if ((nx != nxc) || (ny != nyc))
150 FATAL_ERR(
"Input image and confidence dimensions don't match");
153 confdata = cpl_malloc(npix*
sizeof(*confdata));
154 for (i = 0; i < npix; i++)
159 confsqrt = cpl_malloc(npix*
sizeof(*confsqrt));
160 for (i = 0; i < npix; i++)
161 confsqrt[i] = sqrt(0.01*(
float)confdata[i]);
165 incopy = cpl_malloc(npix*
sizeof(*incopy));
166 ccopy = cpl_malloc(npix*
sizeof(*ccopy));
167 memmove(incopy,indata,npix*
sizeof(*incopy));
168 memmove(ccopy,confdata,npix*
sizeof(*ccopy));
172 mflag = cpl_calloc(npix,
sizeof(*mflag));
183 ap.confdata = confdata;
188 ap.filtfwhm = filtfwhm;
196 for (i = 0; i < npix ; i++)
197 if (confdata[i] == 0)
198 mflag[i] = MF_ZEROCONF;
199 else if (indata[i] < STUPID_VALUE)
200 mflag[i] = MF_STUPID_VALUE;
202 mflag[i] = MF_CLEANPIX;
207 if (retval != CASU_OK)
208 FATAL_ERR(
"Error calculating saturation level");
212 for (i = 0; i < npix ; i++)
213 if (mflag[i] == MF_CLEANPIX && indata[i] > isatbc)
214 mflag[i] = MF_SATURATED;
218 smoothed = cpl_malloc(nx*
sizeof(*smoothed));
219 smoothedc = cpl_malloc(nx*
sizeof(*smoothedc));
228 for (iter = 0; iter < niter; iter++) {
233 if (retval != CASU_OK)
234 FATAL_ERR(
"Error calculating background");
239 if (retval != CASU_OK)
240 FATAL_ERR(
"Error calculating saturation");
245 if (retval != CASU_OK)
246 FATAL_ERR(
"Error calculating background stats");
251 cpl_propertylist_update_float(plist,
"ESO DRS SKYLEVEL",skymed);
252 cpl_propertylist_set_comment(plist,
"ESO DRS SKYLEVEL",
253 "[adu] Median sky brightness");
254 cpl_propertylist_update_float(plist,
"ESO DRS SKYNOISE",skysig);
255 cpl_propertylist_set_comment(plist,
"ESO DRS SKYNOISE",
256 "[adu] Pixel noise at sky level");
260 for (i = 0; i < nx*ny; i++)
265 thresh = threshold*skysig;
269 xintmin = 1.5*thresh*((float)ipix);
274 offset = logf(thresh)*fconst;
278 ap.areal_offset = offset;
280 ap.xintmin = xintmin;
282 ap.background = skymed;
283 ap.saturation = isat;
288 for (j = nw2; j < ny-nw2; j++) {
289 current = indata + j*nx;
290 currentc = confsqrt + j*nx;
295 imcore_apline(&ap,current,currentc,smoothed,smoothedc,j,NULL);
299 if (ap.ibstack > (ap.maxbl - ap.lsiz))
301 if (ap.ipstack > (ap.maxpa*3/4))
302 for (i = 0; i < ap.maxpa*3/8; i++)
313 memmove(indata,incopy,npix*
sizeof(*indata));
315 opm = cpl_mask_get_data(ap.opmask);
316 for (i = 0; i < npix; i++) {
323 if (ap.backmap.bvals != NULL) {
324 for (i = 0; i < ap.backmap.nby; i++)
325 freespace(ap.backmap.bvals[i]);
326 freespace(ap.backmap.bvals);
331 opm = cpl_mask_get_data(ap.opmask);
332 for (i = 0; i < npix; i++)
333 opm[i] = (confdata[i] == 0);
334 memmove(confdata,ccopy,npix*
sizeof(*confdata));
362 static void crweights(
float filtfwhm) {
373 gsigsq = 1.0/(2.0*pow(MAX(1.0,(
double)filtfwhm)/2.35,2.0));
379 for (i = -nw2; i <= nw2; i++) {
382 for (j = -nw2; j <= nw2; j++) {
386 weights[n] = (float)exp(-(di + dj));
387 renorm += weights[n];
394 for (i = -nw2; i <= nw2; i++) {
395 for (j = -nw2; j <= nw2; j++) {
397 weights[n] /= renorm;
399 weightc[n] = weights[n];
424 static void convolve(
int ir) {
425 int i,nw2,ix,jx,jy,n;
430 for (i = 0; i < nx; i++) {
441 for (ix = nw2; ix < nx-nw2; ix++) {
443 for (jy = ir-nw2; jy <= ir+nw2; jy++) {
444 idata = indata + jy*nx;
445 cdata = confsqrt + jy*nx;
446 for (jx = ix-nw2; jx <= ix+nw2; jx++) {
448 smoothed[ix] += weights[n]*idata[jx];
449 smoothedc[ix] += weightc[n]*idata[jx]*cdata[jx];
455 static void tidy(
void) {
461 freespace(smoothedc);
cpl_image * casu_fits_get_image(casu_fits *p)
int casu_fits_get_nexten(casu_fits *p)
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
int imcore_background(ap_t *ap, int nbsize, float nullval)
Model and create background map.
int imcore_opm(casu_fits *infile, casu_fits *conf, int ipix, float threshold, int nbsize, float filtfwhm, int niter)
Create an object mask.
int imcore_backstats(ap_t *ap, float nullval, int satonly, float *skymed, float *skysig, float *sat)
Work out robust background estimate over a whole input image.
void imcore_tabinit(ap_t *ap, int *xcol, int *ycol, int cattype, cpl_table **tab)
Initialise catalogues.
void imcore_apfu(ap_t *ap)
Get rid of the largest contributor in an ap structure.
void imcore_terminate(ap_t *ap, int cattype, float gain, int *nobjects, cpl_table *tab)
Check for objects that have terminated.
void imcore_apline(ap_t *ap, float dat[], float conf[], float smoothed[], float smoothedc[], int j, unsigned char *bpm)
Detect objects on a line of data.
void imcore_apclose(ap_t *ap)
Close ap structure.
int imcore_tabclose(ap_t *ap, int cattype)
Close the table structure.
void imcore_apinit(ap_t *ap)
Initialise the ap structure.