36 #include "floatmath.h"
38 #include "../vircam_pfits.h"
39 #include "../vircam_fits.h"
41 #define FATAL_ERR(_a) {freetable(tab); cpl_msg_error(fctid,_a); tidy(); return(VIR_FATAL);}
45 static float *indata = NULL;
46 static int *confdata = NULL;
47 static float *confsqrt = NULL;
48 static float *smoothed = NULL;
49 static float *smoothedc = NULL;
50 static unsigned char *mflag = NULL;
52 static int freeconf = 0;
53 static float *incopy = NULL;
54 static int *ccopy = NULL;
56 static float weights[NW*NW];
57 static float weightc[NW*NW];
61 static void crweights(
float);
62 static void convolve(
int);
63 static void tidy(
void);
112 extern int imcore_opm(vir_fits *infile, vir_fits *conf,
int ipix,
113 float threshold,
int nbsize,
float filtfwhm,
116 int i,retval,j,nw2,iter,nclip,nxc,nyc;
117 float fconst,nullval,skymed,skysig,thresh,xintmin,offset;
118 float isat,isatbc,*current,junk,*currentc;
120 cpl_image *map,*cmap;
121 cpl_propertylist *plist;
123 const char *fctid =
"imcore_opm";
127 fconst = CPL_MATH_LOG2E;
134 if ((indata = cpl_image_get_data_float(map)) == NULL)
135 FATAL_ERR(
"Error getting image data");
136 nx = (long)cpl_image_get_size_x(map);
137 ny = (long)cpl_image_get_size_y(map);
146 if ((confdata = cpl_image_get_data(cmap)) == NULL)
147 FATAL_ERR(
"Error getting confidence map data");
148 nxc = (long)cpl_image_get_size_x(cmap);
149 nyc = (long)cpl_image_get_size_y(cmap);
150 if ((nx != nxc) || (ny != nyc))
151 FATAL_ERR(
"Input image and confidence dimensions don't match");
154 confdata = cpl_malloc(npix*
sizeof(*confdata));
155 for (i = 0; i < npix; i++)
160 confsqrt = cpl_malloc(npix*
sizeof(*confsqrt));
161 for (i = 0; i < npix; i++)
162 confsqrt[i] = sqrt(0.01*(
float)confdata[i]);
166 incopy = cpl_malloc(npix*
sizeof(*incopy));
167 ccopy = cpl_malloc(npix*
sizeof(*ccopy));
168 memmove(incopy,indata,npix*
sizeof(*incopy));
169 memmove(ccopy,confdata,npix*
sizeof(*ccopy));
173 mflag = cpl_calloc(npix,
sizeof(*mflag));
184 ap.confdata = confdata;
189 ap.filtfwhm = filtfwhm;
197 for (i = 0; i < npix ; i++)
198 if (confdata[i] == 0)
199 mflag[i] = MF_ZEROCONF;
200 else if (indata[i] < STUPID_VALUE)
201 mflag[i] = MF_STUPID_VALUE;
203 mflag[i] = MF_CLEANPIX;
208 if (retval != VIR_OK)
209 FATAL_ERR(
"Error calculating saturation level");
213 for (i = 0; i < npix ; i++)
214 if (mflag[i] == MF_CLEANPIX && indata[i] > isatbc)
215 mflag[i] = MF_SATURATED;
219 smoothed = cpl_malloc(nx*
sizeof(*smoothed));
220 smoothedc = cpl_malloc(nx*
sizeof(*smoothedc));
229 for (iter = 0; iter < niter; iter++) {
234 if (retval != VIR_OK)
235 FATAL_ERR(
"Error calculating background");
240 if (retval != VIR_OK)
241 FATAL_ERR(
"Error calculating saturation");
246 if (retval != VIR_OK)
247 FATAL_ERR(
"Error calculating background stats");
252 cpl_propertylist_update_float(plist,
"ESO DRS SKYLEVEL",skymed);
253 cpl_propertylist_set_comment(plist,
"ESO DRS SKYLEVEL",
254 "[adu] Median sky brightness");
255 cpl_propertylist_update_float(plist,
"ESO DRS SKYNOISE",skysig);
256 cpl_propertylist_set_comment(plist,
"ESO DRS SKYNOISE",
257 "[adu] Pixel noise at sky level");
261 for (i = 0; i < nx*ny; i++)
266 thresh = threshold*skysig;
270 xintmin = 1.5*thresh*((float)ipix);
275 offset = logf(thresh)*fconst;
279 ap.areal_offset = offset;
281 ap.xintmin = xintmin;
283 ap.background = skymed;
284 ap.saturation = isat;
289 for (j = nw2; j < ny-nw2; j++) {
290 current = indata + j*nx;
291 currentc = confsqrt + j*nx;
296 apline(&ap,current,currentc,smoothed,smoothedc,j,NULL);
300 if (ap.ibstack > (ap.maxbl - ap.lsiz))
302 if (ap.ipstack > (ap.maxpa*3/4))
303 for (i = 0; i < ap.maxpa*3/8; i++)
314 memmove(indata,incopy,npix*
sizeof(*indata));
316 opm = cpl_mask_get_data(ap.opmask);
317 for (i = 0; i < npix; i++) {
324 if (ap.backmap.bvals != NULL) {
325 for (i = 0; i < ap.backmap.nby; i++)
326 freespace(ap.backmap.bvals[i]);
327 freespace(ap.backmap.bvals);
332 opm = cpl_mask_get_data(ap.opmask);
333 for (i = 0; i < npix; i++)
334 opm[i] = (confdata[i] == 0);
335 memmove(confdata,ccopy,npix*
sizeof(*confdata));
363 static void crweights(
float filtfwhm) {
374 gsigsq = 1.0/(2.0*pow(MAX(1.0,(
double)filtfwhm)/2.35,2.0));
380 for (i = -nw2; i <= nw2; i++) {
383 for (j = -nw2; j <= nw2; j++) {
387 weights[n] = (float)exp(-(di + dj));
388 renorm += weights[n];
395 for (i = -nw2; i <= nw2; i++) {
396 for (j = -nw2; j <= nw2; j++) {
398 weights[n] /= renorm;
400 weightc[n] = weights[n];
425 static void convolve(
int ir) {
426 int i,nw2,ix,jx,jy,n;
431 for (i = 0; i < nx; i++) {
442 for (ix = nw2; ix < nx-nw2; ix++) {
444 for (jy = ir-nw2; jy <= ir+nw2; jy++) {
445 idata = indata + jy*nx;
446 cdata = confsqrt + jy*nx;
447 for (jx = ix-nw2; jx <= ix+nw2; jx++) {
449 smoothed[ix] += weights[n]*idata[jx];
450 smoothedc[ix] += weightc[n]*idata[jx]*cdata[jx];
456 static void tidy(
void) {
462 freespace(smoothedc);
int imcore_opm(vir_fits *infile, vir_fits *conf, int ipix, float threshold, int nbsize, float filtfwhm, int niter)
Create an object mask.
void terminate(ap_t *ap)
Check for objects that have terminated.
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.
int vircam_fits_get_nexten(vir_fits *p)
cpl_image * vircam_fits_get_image(vir_fits *p)
int vircam_pfits_get_gain(const cpl_propertylist *plist, float *gain)
Get the value of the detector gain.
void apclose(ap_t *ap)
Close ap structure.
void apline(ap_t *ap, float dat[], float conf[], float smoothed[], float smoothedc[], int j, unsigned char *bpm)
Detect objects on a line of data.
int tabclose(ap_t *ap)
Close the table structure.
void apfu(ap_t *ap)
Get rid of the largest contributor in an ap structure.
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
void apinit(ap_t *ap)
Initialise the ap structure.
void tabinit(ap_t *ap)
Initialise catalogues.
int imcore_background(ap_t *ap, int nbsize, float nullval)
Model and create background map.