152 int nconfs,
float lthr,
float hthr,
153 cpl_propertylist **p,
const char *expkey,
154 cpl_image **out, cpl_image **outc,
int *status) {
156 int i,itx,iy,ccur,clast,ix,n,iline,icol,jy,jx,j,nxo,nyo,*ocdata;
157 long npts,ielm,iloc,index_y,index;
158 dstrct *fileptrs=NULL,*dd;
159 keeptabs *clipmon=NULL,*c;
160 float minxoff,minyoff,expref,sky,skynoise,clip1,clip2,outdata;
161 float outconf,avlev,avvar,renorm,exposure,sumweight,*odata,*owdata=NULL;
162 double crpix1,crpix2;
163 cpl_propertylist *ehu,*p2,*phu;
164 const char *fctid =
"casu_imdither";
172 if (*status != CASU_OK)
178 cpl_msg_error(fctid,
"No input files to combine");
179 tidy(0,fileptrs,clipmon,owdata);
185 fileptrs = cpl_malloc(nimages*
sizeof(dstrct));
192 expref = max(0.5,exposure);
195 for (i = 0; i < nimages; i++) {
201 }
else if (nconfs == 1) {
202 dd->conf = inconf[0];
205 dd->conf = inconf[i];
210 dd->xoff = cpl_propertylist_get_float(ehu,
"ESO DRS XOFFDITHER");
211 dd->yoff = cpl_propertylist_get_float(ehu,
"ESO DRS YOFFDITHER");
212 minxoff = min(dd->xoff,minxoff);
213 minyoff = min(dd->yoff,minyoff);
214 if (cpl_propertylist_has(phu,expkey)) {
215 exposure = (float)cpl_propertylist_get_double(phu,expkey);
216 exposure = max(0.5,exposure);
220 dd->expscale = exposure/expref;
226 npts = dd->nx*dd->ny;
227 skyest(dd->data,dd->cdata,npts,3.0,MINDATA,MAXDATA,&sky,&skynoise);
229 dd->noise = skynoise;
236 cpl_msg_error(fctid,
"Image %s and Confidence map %s don't match",
239 tidy(0,fileptrs,clipmon,owdata);
246 for (i = 0; i < nimages; i++) {
250 dd->ixoff = (int)(dd->xoff + 0.5);
251 dd->iyoff = (int)(dd->yoff + 0.5);
258 fileptrs->sky /= fileptrs->expscale;
259 fileptrs->skydiff = 0.0;
260 fileptrs->weight = 1.0;
262 for (i = 1; i < nimages; i++) {
264 dd->sky /= dd->expscale;
265 dd->skydiff = fileptrs->sky - dd->sky;
266 dd->noise /= (float)sqrt((
double)dd->expscale);
267 dd->weight = (float)(pow((
double)fileptrs->noise,2.0)/pow((
double)dd->noise,2.0));
268 sumweight += dd->weight;
273 for (i = 1; i < nimages; i++) {
275 npts = dd->nx*dd->ny;
276 for (j = 0; j < npts; j++)
277 dd->data[j] = dd->data[j]/dd->expscale + dd->skydiff;
282 clip1 = fileptrs->sky - lthr*fileptrs->noise;
283 clip2 = fileptrs->sky + hthr*fileptrs->noise;
290 for (i = 0; i < nimages; i++) {
292 itx = dd->nx + dd->ixoff;
294 itx = dd->ny + dd->iyoff;
300 *out = cpl_image_new((cpl_size)nxo,(cpl_size)nyo,CPL_TYPE_FLOAT);
305 *outc = cpl_image_new((cpl_size)nxo,(cpl_size)nyo,CPL_TYPE_INT);
312 odata = cpl_image_get_data_float(*out);
314 owdata = cpl_malloc(npts*
sizeof(
float));
315 clipmon = clip_open(nimages,nxo);
320 for (iy = 0; iy < nyo; iy++) {
323 for (ix = 0; ix < nxo; ix++) {
324 c = clipmon + ccur + ix;
328 for (i = 0; i < nimages; i++) {
330 iline = iy - dd->iyoff;
331 if (iline < 0 || iline >= dd->ny)
333 icol = ix - dd->ixoff;
334 if (icol < 0 || icol >= dd->nx)
340 ielm = dd->nx*iline + icol;
341 c->values[n] = dd->data[ielm];
342 c->confs[n] = (float)dd->cdata[ielm];
343 c->weights[n] = dd->weight;
344 c->iff[n] = (
short int)i;
347 c->outindex = nxo*iy + ix;
349 average(fileptrs,c,&outdata,&outconf,clip1,clip2,lthr,hthr,0.0,
351 odata[c->outindex] = outdata;
353 owdata[c->outindex] = outconf;
362 for (ix = 1; ix < nxo-1; ix++) {
363 c = clipmon + clast + ix;
372 for (jy = -1; jy <= 1; jy++) {
373 index_y = iloc + jy*nxo;
374 for (jx = -1; jx <= 1; jx++) {
375 index = index_y + jx;
376 avlev += odata[index];
381 for (jy = -1; jy <= 1; jy++) {
382 index_y = iloc + jy*nxo;
383 for (jx = -1; jx <= 1; jx++) {
384 index = index_y + jx;
385 avvar += fabs(odata[index] - avlev);
394 if (avlev < clip2 || avvar <= 2.0*fileptrs->noise)
399 average(fileptrs,c,&outdata,&outconf,clip1,clip2,lthr,hthr,
400 3.0*avvar,sumweight);
401 odata[c->outindex] = outdata;
403 owdata[c->outindex] = outconf;
409 if (owdata != NULL) {
410 skyest(owdata,NULL,npts,3.0,1.0,MAXDATA,&sky,&skynoise);
412 ocdata = cpl_image_get_data_int(*outc);
413 for (i = 0; i < npts; i++)
414 ocdata[i] = max(0,min(1000,(
int)(owdata[i]*renorm + 0.5)));
426 cpl_propertylist_update_string(p2,
"ESO CASU_TIME",timestamp);
427 cpl_propertylist_set_comment(p2,
"ESO CASU_TIME",
428 "Timestamp for matching to conf map");
432 if (cpl_propertylist_has(*p,
"CRPIX1") &&
433 cpl_propertylist_has(*p,
"CRPIX2")) {
434 crpix1 = cpl_propertylist_get_double(*p,
"CRPIX1");
435 crpix2 = cpl_propertylist_get_double(*p,
"CRPIX2");
436 crpix1 += fileptrs->xoff;
437 crpix2 += fileptrs->yoff;
438 cpl_propertylist_update_double(*p,
"CRPIX1",crpix1);
439 cpl_propertylist_update_double(*p,
"CRPIX2",crpix2);
444 tidy(nxo,fileptrs,clipmon,owdata);