111extern 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));