35 #include "../vircam_fits.h"
36 #include "../vircam_pfits.h"
37 #include "../vircam_wcsutils.h"
42 #include "floatmath.h"
43 #include "imcore_version.h"
45 #define FATAL_ERR(_a) {freetable(tab); cpl_msg_error(fctid,_a); tidy(); return(VIR_FATAL);}
49 static float *smoothed = NULL;
50 static float *smoothedc = NULL;
51 static unsigned char *mflag = NULL;
52 static float *indata = NULL;
53 static int *confdata = NULL;
54 static float *confsqrt = NULL;
56 static int freeconf = 0;
58 static float weights[NW*NW];
59 static float weightc[NW*NW];
63 static void crweights(
float);
64 static void convolve(
int);
65 static void tidy(
void);
146 extern int imcore_conf(vir_fits *infile, vir_fits *conf,
int ipix,
147 float threshold,
int icrowd,
float rcore,
int nbsize,
148 int cattyp,
float filtfwhm, vir_tfits **outcat) {
150 int i,retval,mulpix,j,nw2,status;
151 float fconst,nullval,skymed,skysig,thresh,xintmin,offset;
152 float *current,isatbc,isat,junk,*currentc;
153 long npix,nxc,nyc,npts;
154 cpl_image *map,*cmap;
155 cpl_propertylist *plist,*extra;
157 const char *fctid =
"imcore_conf";
165 fconst = CPL_MATH_LOG2E;
173 if ((indata = cpl_image_get_data_float(map)) == NULL)
174 FATAL_ERR(
"Error getting image data");
175 nx = (long)cpl_image_get_size_x(map);
176 ny = (long)cpl_image_get_size_y(map);
185 if ((confdata = cpl_image_get_data(cmap)) == NULL)
186 FATAL_ERR(
"Error getting confidence map data");
187 nxc = (long)cpl_image_get_size_x(cmap);
188 nyc = (long)cpl_image_get_size_y(cmap);
189 if ((nx != nxc) || (ny != nyc))
190 FATAL_ERR(
"Input image and confidence dimensions don't match");
193 confdata = cpl_malloc(npts*
sizeof(*confdata));
194 for (i = 0; i < npts; i++)
199 confsqrt = cpl_malloc(npts*
sizeof(*confsqrt));
200 for (i = 0; i < npts; i++)
201 confsqrt[i] = sqrt(0.01*(
float)confdata[i]);
206 mflag = cpl_calloc(npix,
sizeof(*mflag));
217 ap.confdata = confdata;
222 ap.filtfwhm = filtfwhm;
232 for (i = 0; i < npix; i++)
233 if (confdata[i] == 0)
234 mflag[i] = MF_ZEROCONF;
235 else if (indata[i] < STUPID_VALUE)
236 mflag[i] = MF_STUPID_VALUE;
238 mflag[i] = MF_CLEANPIX;
243 if (retval != VIR_OK)
244 FATAL_ERR(
"Error calculating saturation level");
249 for (i = 0; i < npix ; i++)
250 if (mflag[i] == MF_CLEANPIX && indata[i] > isatbc)
251 mflag[i] = MF_SATURATED;
256 if (retval != VIR_OK)
257 FATAL_ERR(
"Error calculating background");
262 if (retval != VIR_OK)
263 FATAL_ERR(
"Error calculating saturation");
269 if (retval != VIR_OK)
270 FATAL_ERR(
"Error calculating background stats");
275 cpl_propertylist_update_float(plist,
"ESO DRS SKYLEVEL",skymed);
276 cpl_propertylist_set_comment(plist,
"ESO DRS SKYLEVEL",
277 "[adu] Median sky brightness");
278 cpl_propertylist_update_float(plist,
"ESO DRS SKYNOISE",skysig);
279 cpl_propertylist_set_comment(plist,
"ESO DRS SKYNOISE",
280 "[adu] Pixel noise at sky level");
286 for (i = 0; i < nx*ny; i++) {
295 thresh = threshold*skysig;
299 xintmin = 1.5*thresh*((float)ipix);
303 mulpix = MAX(8,2*ipix);
308 offset = logf(thresh)*fconst;
312 smoothed = cpl_malloc(nx*
sizeof(*smoothed));
313 smoothedc = cpl_malloc(nx*
sizeof(*smoothedc));
318 ap.areal_offset = offset;
320 ap.xintmin = xintmin;
322 ap.background = skymed;
323 ap.saturation = (float)isat;
333 for (j = nw2; j < ny-nw2; j++) {
334 current = indata + j*nx;
335 currentc = confsqrt + j*nx;
340 apline(&ap,current,currentc,smoothed,smoothedc,j,NULL);
344 if (ap.ibstack > (ap.maxbl - ap.lsiz))
346 if (ap.ipstack > (ap.maxpa*3/4))
347 for (i = 0; i < ap.maxpa*3/8; i++)
359 cpl_table_set_size(tab,nobjects);
361 if (retval != VIR_OK)
362 FATAL_ERR(
"Error working out seeing");
368 cpl_propertylist_update_float(extra,
"ESO QC SATURATION",ap.saturation);
369 cpl_propertylist_set_comment(extra,
"ESO QC SATURATION",
370 "[adu] Saturation level");
371 cpl_propertylist_update_float(extra,
"ESO QC MEAN_SKY",ap.background);
372 cpl_propertylist_set_comment(extra,
"ESO QC MEAN_SKY",
373 "[adu] Median sky brightness");
374 cpl_propertylist_update_float(extra,
"ESO QC SKY_NOISE",ap.sigma);
375 cpl_propertylist_set_comment(extra,
"ESO QC SKY_NOISE",
376 "[adu] Pixel noise at sky level");
380 cpl_propertylist_update_float(extra,
"ESO DRS THRESHOL",ap.thresh);
381 cpl_propertylist_set_comment(extra,
"ESO DRS THRESHOL",
382 "[adu] Isophotal analysis threshold");
383 cpl_propertylist_update_int(extra,
"ESO DRS MINPIX",ap.ipnop);
384 cpl_propertylist_set_comment(extra,
"ESO DRS MINPIX",
385 "[pixels] Minimum size for images");
386 cpl_propertylist_update_int(extra,
"ESO DRS CROWDED",ap.icrowd);
387 cpl_propertylist_set_comment(extra,
"ESO DRS CROWDED",
388 "Crowded field analysis flag");
389 cpl_propertylist_update_float(extra,
"ESO DRS RCORE",ap.rcore);
390 cpl_propertylist_set_comment(extra,
"ESO DRS RCORE",
391 "[pixels] Core radius for default profile fit");
392 cpl_propertylist_update_float(extra,
"ESO DRS SEEING",ap.fwhm);
393 cpl_propertylist_set_comment(extra,
"ESO DRS SEEING",
394 "[pixels] Average FWHM");
395 cpl_propertylist_update_float(extra,
"ESO DRS FILTFWHM",ap.filtfwhm);
396 cpl_propertylist_set_comment(extra,
"ESO DRS FILTFWHM",
397 "[pixels] FWHM of smoothing kernel");
398 cpl_propertylist_update_int(extra,
"ESO DRS XCOL",imcore_xcol);
399 cpl_propertylist_set_comment(extra,
"ESO DRS XCOL",
"Column for X position");
400 cpl_propertylist_update_int(extra,
"ESO DRS YCOL",imcore_ycol);
401 cpl_propertylist_set_comment(extra,
"ESO DRS YCOL",
"Column for Y position");
402 cpl_propertylist_update_int(extra,
"ESO DRS NXOUT",nx);
403 cpl_propertylist_set_comment(extra,
"ESO DRS NXOUT",
404 "X Dimension of input image");
405 cpl_propertylist_update_int(extra,
"ESO DRS NYOUT",ny);
406 cpl_propertylist_set_comment(extra,
"ESO DRS NYOUT",
407 "Y Dimension of input image");
408 snprintf(card,64,
"IMCORE version: %s",imcore_version);
409 cpl_propertylist_append_string(extra,
"HISTORY",card);
443 static void crweights(
float filtfwhm) {
454 gsigsq = 1.0/(2.0*pow(MAX(1.0,(
double)filtfwhm)/2.35,2.0));
460 for (i = -nw2; i <= nw2; i++) {
463 for (j = -nw2; j <= nw2; j++) {
467 weights[n] = (float)exp(-(di + dj));
468 renorm += weights[n];
475 for (i = -nw2; i <= nw2; i++) {
476 for (j = -nw2; j <= nw2; j++) {
478 weights[n] /= renorm;
480 weightc[n] = weights[n];
505 static void convolve(
int ir) {
506 int i,nw2,ix,jx,jy,n;
511 for (i = 0; i < nx; i++) {
522 for (ix = nw2; ix < nx-nw2; ix++) {
524 for (jy = ir-nw2; jy <= ir+nw2; jy++) {
525 idata = indata + jy*nx;
526 cdata = confsqrt + jy*nx;
527 for (jx = ix-nw2; jx <= ix+nw2; jx++) {
529 smoothed[ix] += weights[n]*idata[jx];
530 smoothedc[ix] += weightc[n]*idata[jx]*cdata[jx];
536 static void tidy(
void) {
542 freespace(smoothedc);