35 #include "casu_fits.h"
36 #include "casu_wcsutils.h"
41 #include "floatmath.h"
42 #include "imcore_version.h"
44 #define FATAL_ERR(_a) {freetable(tab); cpl_msg_error(fctid,_a); tidy(); return(CASU_FATAL);}
48 static float *smoothed = NULL;
49 static float *smoothedc = NULL;
50 static unsigned char *mflag = NULL;
51 static float *indata = NULL;
52 static int *confdata = NULL;
53 static float *confsqrt = NULL;
55 static int freeconf = 0;
57 static float weights[NW*NW];
58 static float weightc[NW*NW];
62 static void crweights(
float);
63 static void convolve(
int);
64 static void tidy(
void);
147 extern int imcore_conf(casu_fits *infile, casu_fits *conf,
int ipix,
148 float threshold,
int icrowd,
float rcore,
int nbsize,
149 int cattype,
float filtfwhm,
float gain,
150 casu_tfits **outcat) {
152 int i,retval,mulpix,j,nw2,status,nobjects,imcore_xcol,imcore_ycol;
153 float fconst,nullval,skymed,skysig,thresh,xintmin,offset;
154 float *current,isatbc,isat,junk,*currentc;
155 long npix,nxc,nyc,npts;
156 cpl_image *map,*cmap;
157 cpl_propertylist *plist,*extra;
160 const char *fctid =
"imcore_conf";
168 fconst = CPL_MATH_LOG2E;
176 if ((indata = cpl_image_get_data_float(map)) == NULL)
177 FATAL_ERR(
"Error getting image data");
178 nx = (long)cpl_image_get_size_x(map);
179 ny = (long)cpl_image_get_size_y(map);
186 if ((confdata = cpl_image_get_data(cmap)) == NULL)
187 FATAL_ERR(
"Error getting confidence map data");
188 nxc = (long)cpl_image_get_size_x(cmap);
189 nyc = (long)cpl_image_get_size_y(cmap);
190 if ((nx != nxc) || (ny != nyc))
191 FATAL_ERR(
"Input image and confidence dimensions don't match");
194 confdata = cpl_malloc(npts*
sizeof(*confdata));
195 for (i = 0; i < npts; i++)
200 confsqrt = cpl_malloc(npts*
sizeof(*confsqrt));
201 for (i = 0; i < npts; i++)
202 confsqrt[i] = sqrt(0.01*(
float)confdata[i]);
207 mflag = cpl_calloc(npix,
sizeof(*mflag));
218 ap.confdata = confdata;
223 ap.filtfwhm = filtfwhm;
233 for (i = 0; i < npix; i++)
234 if (confdata[i] == 0)
235 mflag[i] = MF_ZEROCONF;
236 else if (indata[i] < STUPID_VALUE)
237 mflag[i] = MF_STUPID_VALUE;
239 mflag[i] = MF_CLEANPIX;
244 if (retval != CASU_OK)
245 FATAL_ERR(
"Error calculating saturation level");
250 for (i = 0; i < npix ; i++)
251 if (mflag[i] == MF_CLEANPIX && indata[i] > isatbc)
252 mflag[i] = MF_SATURATED;
257 if (retval != CASU_OK)
258 FATAL_ERR(
"Error calculating background");
263 if (retval != CASU_OK)
264 FATAL_ERR(
"Error calculating saturation");
270 if (retval != CASU_OK)
271 FATAL_ERR(
"Error calculating background stats");
276 cpl_propertylist_update_float(plist,
"ESO DRS SKYLEVEL",skymed);
277 cpl_propertylist_set_comment(plist,
"ESO DRS SKYLEVEL",
278 "[adu] Median sky brightness");
279 cpl_propertylist_update_float(plist,
"ESO DRS SKYNOISE",skysig);
280 cpl_propertylist_set_comment(plist,
"ESO DRS SKYNOISE",
281 "[adu] Pixel noise at sky level");
287 for (i = 0; i < nx*ny; i++) {
296 thresh = threshold*skysig;
300 xintmin = 1.5*thresh*((float)ipix);
304 mulpix = MAX(8,2*ipix);
309 offset = logf(thresh)*fconst;
313 smoothed = cpl_malloc(nx*
sizeof(*smoothed));
314 smoothedc = cpl_malloc(nx*
sizeof(*smoothedc));
319 ap.areal_offset = offset;
321 ap.xintmin = xintmin;
323 ap.background = skymed;
324 ap.saturation = (float)isat;
334 for (j = nw2; j < ny-nw2; j++) {
335 current = indata + j*nx;
336 currentc = confsqrt + j*nx;
341 imcore_apline(&ap,current,currentc,smoothed,smoothedc,j,NULL);
345 if (ap.ibstack > (ap.maxbl - ap.lsiz))
347 if (ap.ipstack > (ap.maxpa*3/4))
348 for (i = 0; i < ap.maxpa*3/8; i++)
360 cpl_table_set_size(tab,nobjects);
362 if (retval != CASU_OK)
363 FATAL_ERR(
"Error working out seeing");
369 cpl_propertylist_update_float(extra,
"ESO QC SATURATION",ap.saturation);
370 cpl_propertylist_set_comment(extra,
"ESO QC SATURATION",
371 "[adu] Saturation level");
372 cpl_propertylist_update_float(extra,
"ESO QC MEAN_SKY",ap.background);
373 cpl_propertylist_set_comment(extra,
"ESO QC MEAN_SKY",
374 "[adu] Median sky brightness");
375 cpl_propertylist_update_float(extra,
"ESO QC SKY_NOISE",ap.sigma);
376 cpl_propertylist_set_comment(extra,
"ESO QC SKY_NOISE",
377 "[adu] Pixel noise at sky level");
381 cpl_propertylist_update_float(extra,
"ESO DRS THRESHOL",ap.thresh);
382 cpl_propertylist_set_comment(extra,
"ESO DRS THRESHOL",
383 "[adu] Isophotal analysis threshold");
384 cpl_propertylist_update_int(extra,
"ESO DRS MINPIX",ap.ipnop);
385 cpl_propertylist_set_comment(extra,
"ESO DRS MINPIX",
386 "[pixels] Minimum size for images");
387 cpl_propertylist_update_int(extra,
"ESO DRS CROWDED",ap.icrowd);
388 cpl_propertylist_set_comment(extra,
"ESO DRS CROWDED",
389 "Crowded field analysis flag");
390 cpl_propertylist_update_float(extra,
"ESO DRS RCORE",ap.rcore);
391 cpl_propertylist_set_comment(extra,
"ESO DRS RCORE",
392 "[pixels] Core radius for default profile fit");
393 cpl_propertylist_update_float(extra,
"ESO DRS SEEING",ap.fwhm);
394 cpl_propertylist_set_comment(extra,
"ESO DRS SEEING",
395 "[pixels] Average FWHM");
396 cpl_propertylist_update_float(extra,
"ESO DRS FILTFWHM",ap.filtfwhm);
397 cpl_propertylist_set_comment(extra,
"ESO DRS FILTFWHM",
398 "[pixels] FWHM of smoothing kernel");
399 cpl_propertylist_update_int(extra,
"ESO DRS XCOL",imcore_xcol);
400 cpl_propertylist_set_comment(extra,
"ESO DRS XCOL",
"Column for X position");
401 cpl_propertylist_update_int(extra,
"ESO DRS YCOL",imcore_ycol);
402 cpl_propertylist_set_comment(extra,
"ESO DRS YCOL",
"Column for Y position");
403 cpl_propertylist_update_int(extra,
"ESO DRS NXOUT",nx);
404 cpl_propertylist_set_comment(extra,
"ESO DRS NXOUT",
405 "X Dimension of input image");
406 cpl_propertylist_update_int(extra,
"ESO DRS NYOUT",ny);
407 cpl_propertylist_set_comment(extra,
"ESO DRS NYOUT",
408 "Y Dimension of input image");
409 snprintf(card,64,
"IMCORE version: %s",imcore_version);
410 cpl_propertylist_append_string(extra,
"HISTORY",card);
416 (void)
casu_tabwcs(extra,imcore_xcol,imcore_ycol,&status);
444 static void crweights(
float filtfwhm) {
455 gsigsq = 1.0/(2.0*pow(MAX(1.0,(
double)filtfwhm)/2.35,2.0));
461 for (i = -nw2; i <= nw2; i++) {
464 for (j = -nw2; j <= nw2; j++) {
468 weights[n] = (float)exp(-(di + dj));
469 renorm += weights[n];
476 for (i = -nw2; i <= nw2; i++) {
477 for (j = -nw2; j <= nw2; j++) {
479 weights[n] /= renorm;
481 weightc[n] = weights[n];
506 static void convolve(
int ir) {
507 int i,nw2,ix,jx,jy,n;
512 for (i = 0; i < nx; i++) {
523 for (ix = nw2; ix < nx-nw2; ix++) {
525 for (jy = ir-nw2; jy <= ir+nw2; jy++) {
526 idata = indata + jy*nx;
527 cdata = confsqrt + jy*nx;
528 for (jx = ix-nw2; jx <= ix+nw2; jx++) {
530 smoothed[ix] += weights[n]*idata[jx];
531 smoothedc[ix] += weightc[n]*idata[jx]*cdata[jx];
537 static void tidy(
void) {
543 freespace(smoothedc);
cpl_image * casu_fits_get_image(casu_fits *p)
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
int casu_fits_get_nexten(casu_fits *p)
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
casu_tfits * casu_tfits_wrap(cpl_table *tab, casu_tfits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
int casu_tabwcs(cpl_propertylist *p, int xcol, int ycol, int *status)
int imcore_background(ap_t *ap, int nbsize, float nullval)
Model and create background map.
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.
int imcore_conf(casu_fits *infile, casu_fits *conf, int ipix, float threshold, int icrowd, float rcore, int nbsize, int cattype, float filtfwhm, float gain, casu_tfits **outcat)
Do source extraction.
void imcore_apclose(ap_t *ap)
Close ap structure.
int imcore_tabclose(ap_t *ap, int cattype)
Close the table structure.
int imcore_do_seeing(ap_t *ap, int cattype, int nobjects, cpl_table *tab)
Do seeing estimate.
void imcore_apinit(ap_t *ap)
Initialise the ap structure.