X-shooter Pipeline Reference Manual 3.8.15
xsh_irplib_mkmaster.c
Go to the documentation of this file.
1/* $Id: xsh_irplib_mkmaster.c,v 1.13 2012-06-13 09:25:32 amodigli Exp $
2 *
3 * This file is part of the irplib package
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: amodigli $
23 * $Date: 2012-06-13 09:25:32 $
24 * $Revision: 1.13 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include <math.h>
37
38#include "xsh_irplib_mkmaster.h"
39#include "xsh_utils_imagelist.h"
40#include "xsh_pfits.h"
41#include "xsh_ksigma_clip.h"
42#include "xsh_msg.h"
43/*----------------------------------------------------------------------------*/
47/*----------------------------------------------------------------------------*/
48
50/*---------------------------------------------------------------------------*/
57/*---------------------------------------------------------------------------*/
58
59/*-------------------------------------------------------------------------*/
71/*--------------------------------------------------------------------------*/
72cpl_vector *
74 const double kappa,
75 const int nclip,
76 const double tolerance)
77{
78
79 const cpl_image* img=NULL;
80 int size=0;
81 int i=0;
82 cpl_vector* levels=NULL;
83 double* pval=NULL;
84 double mean=0;
85 double stdev=0;
86
87
88 cpl_error_ensure(iml != NULL, CPL_ERROR_NULL_INPUT, return(levels),
89 "Null input image list");
90 cpl_error_ensure(kappa >= 0, CPL_ERROR_ILLEGAL_INPUT, return(levels),
91 "Must be kappa>0");
92
93 size=cpl_imagelist_get_size(iml);
94 levels=cpl_vector_new(size);
95 pval=cpl_vector_get_data(levels);
96
97 for(i=0;i<size;i++) {
98 img=cpl_imagelist_get_const(iml,i);
99 xsh_ksigma_clip(img,1,1, cpl_image_get_size_x(img),
100 cpl_image_get_size_y(img),
101 nclip,kappa,tolerance,&mean,&stdev);
102 //cpl_msg_info(cpl_func,"Ima %d mean level: %g",i+1,mean);
103 pval[i]=mean;
104 }
105
106
107 return levels;
108}
109
110/*-------------------------------------------------------------------------*/
118/*--------------------------------------------------------------------------*/
119static cpl_error_code
120irplib_imagelist_subtract_values(cpl_imagelist** iml, cpl_vector* values)
121{
122
123 cpl_image* img=NULL;
124 int size=0;
125 int i=0;
126 double* pval=NULL;
127
128 size=cpl_imagelist_get_size(*iml);
129 pval=cpl_vector_get_data(values);
130
131 for(i=0;i<size;i++) {
132 img=cpl_imagelist_get(*iml,i);
133 cpl_image_subtract_scalar(img,pval[i]);
134 cpl_imagelist_set(*iml,img,i);
135 }
136
137 return cpl_error_get_code();
138}
139#if 0
140/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154static double
155irplib_vector_ksigma(cpl_vector *values,
156 const double klow, const double khigh, int kiter)
157{
158 cpl_vector *accepted;
159 double mean = 0.0;
160 double sigma = 0.0;
161 double *data = cpl_vector_get_data(values);
162 int n = cpl_vector_get_size(values);
163 int ngood = n;
164 int count = 0;
165 int i;
166
167 /*
168 * At first iteration the mean is taken as the median, and the
169 * standard deviation relative to this value is computed.
170 */
171
172 mean = cpl_vector_get_median(values);
173
174 for (i = 0; i < n; i++) {
175 sigma += (mean - data[i]) * (mean - data[i]);
176 }
177 sigma = sqrt(sigma / (n - 1));
178
179 while (kiter) {
180 count = 0;
181 for (i = 0; i < ngood; i++) {
182 if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
183 data[count] = data[i];
184 ++count;
185 }
186 }
187
188 if (count == 0) // This cannot happen at first iteration.
189 break; // So we can break: we have already computed a mean.
190
191 /*
192 * The mean must be computed even if no element was rejected
193 * (count == ngood), because at first iteration median instead
194 * of mean was computed.
195 */
196
197 accepted = cpl_vector_wrap(count, data);
198 mean = cpl_vector_get_mean(accepted);
199 if(count>1) {
200 sigma = cpl_vector_get_stdev(accepted);
201 }
202 cpl_vector_unwrap(accepted);
203
204 if (count == ngood) {
205 break;
206 }
207 ngood = count;
208 --kiter;
209 }
210
211 return mean;
212}
213#endif
214#if 0
233static cpl_image *
234irplib_imagelist_ksigma_stack(const cpl_imagelist *imlist,
235 double klow, double khigh, int kiter)
236{
237 int ni, nx, ny, npix;
238 cpl_image *out_ima=NULL;
239 cpl_imagelist *loc_iml=NULL;
240 float *pout_ima=NULL;
241 cpl_image *image=NULL;
242 const float **data=NULL;
243 double *med=NULL;
244 cpl_vector *time_line=NULL;
245
246 double *ptime_line=NULL;
247 int i, j;
248 double mean_of_medians=0;
249
250 cpl_error_ensure(imlist != NULL, CPL_ERROR_NULL_INPUT, return(out_ima),
251 "Null input image list");
252
253 ni = cpl_imagelist_get_size(imlist);
254 loc_iml = cpl_imagelist_duplicate(imlist);
255 image = cpl_imagelist_get(loc_iml, 0);
256 nx = cpl_image_get_size_x(image);
257 ny = cpl_image_get_size_y(image);
258 npix = nx * ny;
259
260 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
261 pout_ima = cpl_image_get_data_float(out_ima);
262
263 time_line = cpl_vector_new(ni);
264
265 ptime_line = cpl_vector_get_data(time_line);
266
267 data = cpl_calloc(sizeof(float *), ni);
268 med = cpl_calloc(sizeof(double), ni);
269
270 for (i = 0; i < ni; i++) {
271 image = cpl_imagelist_get(loc_iml, i);
272 med[i]=cpl_image_get_median(image);
273 cpl_image_subtract_scalar(image,med[i]);
274 data[i] = cpl_image_get_data_float(image);
275 mean_of_medians+=med[i];
276
277 }
278 mean_of_medians/=ni;
279
280 for (i = 0; i < npix; i++) {
281 for (j = 0; j < ni; j++) {
282 ptime_line[j] = data[j][i];
283 }
284 pout_ima[i] = irplib_vector_ksigma(time_line, klow, khigh, kiter);
285 }
286
287 cpl_image_add_scalar(out_ima,mean_of_medians);
288
289
290 cpl_free(data);
291 cpl_free(med);
292 cpl_vector_delete(time_line);
293 cpl_imagelist_delete(loc_iml);
294
295 return out_ima;
296
297}
298#endif
299
300
301
302/*-------------------------------------------------------------------------*/
316/*--------------------------------------------------------------------------*/
317cpl_image*
318xsh_irplib_mkmaster_mean(cpl_imagelist* images,const double kappa, const int nclip,
319 const double tolerance,const double klow,const double khigh)
320{
321
322 cpl_image* master=NULL;
323 cpl_vector* levels=NULL;
324 double mean=0;
325 cpl_imagelist* iml=NULL;
326
327 //cpl_msg_info(cpl_func,"method mean");
328 iml=cpl_imagelist_duplicate(images);
330 mean=cpl_vector_get_mean(levels);
331 //cpl_msg_info(cpl_func,"Master mean level: %g",mean);
332
334
335 //master = irplib_imagelist_ksigma_stack(iml,klow,khigh,niter);
336 master = cpl_imagelist_collapse_sigclip_create(iml,klow,khigh,.1,CPL_COLLAPSE_MEAN,NULL);
337 cpl_image_add_scalar(master,mean);
338
339 cpl_vector_delete(levels);
340 cpl_imagelist_delete(iml);
341
342 return master;
343
344}
345
346
347
348/*-------------------------------------------------------------------------*/
360/*--------------------------------------------------------------------------*/
361cpl_image*
362xsh_irplib_mkmaster_median(cpl_imagelist* images,const double kappa, const int nclip,
363 const double tolerance)
364{
365
366 cpl_image* master=NULL;
367 cpl_vector* levels=NULL;
368 double mean=0;
369 cpl_imagelist* iml=NULL;
370
371 cpl_ensure(images != NULL, CPL_ERROR_NULL_INPUT,NULL);
372 //cpl_msg_info(cpl_func,"method median");
373 iml=cpl_imagelist_duplicate(images);
375
376 mean=cpl_vector_get_mean(levels);
377 //cpl_msg_info(cpl_func,"Master mean level: %g",mean);
379
380 //When DFS11855 is solved we can use cpl_imagelist_collapse_median_create
381 //master = cpl_imagelist_collapse_median_create(iml);
383
384
385 cpl_image_add_scalar(master,mean);
386 cpl_vector_delete(levels);
387 cpl_imagelist_delete(iml);
388
389 return master;
390
391}
392
393cpl_imagelist*
394xsh_irplib_mkmaster_dark_fill_imagelist(const cpl_imagelist* raw_images,
395 cpl_propertylist** raw_headers, const cpl_image* master_bias,
396 double* mean_exptime) {
397 /* First process each input image and store the results in a
398 new image list */
399
400 cpl_imagelist* preproc_images = NULL;
401 int i = 0;
402 cpl_image* current_dark = NULL;
403 double min_exptime = 0;
404 double max_exptime = 0;
405
406 preproc_images = cpl_imagelist_new();
407 for (i = 0; i < cpl_imagelist_get_size(raw_images); i++) {
408 double exposure_time = 0.0;
409 const cpl_propertylist *current_header;
410
411 current_dark = cpl_image_duplicate(cpl_imagelist_get_const(raw_images, i));
412 current_header = raw_headers[i];
413
414 /* Subtract master bias */
415 if (master_bias != NULL) {
416 /* cpl_msg_info(cpl_func, "Subtracting master bias"); */
417 cpl_image_subtract(current_dark, master_bias);
418 } else {
419 /* cpl_msg_info(cpl_func, "Skipping bias subtraction"); */
420 }
421
422 exposure_time = xsh_pfits_get_exptime(current_header);
423 /* Initialize/update min/max exposure time*/
424 if (i == 0 || exposure_time < min_exptime) {
425 min_exptime = exposure_time;
426 }
427 if (i == 0 || exposure_time > max_exptime) {
428 max_exptime = exposure_time;
429 }
430
431 /* Do not normalize to unit exposure time */
432 /* If this is uncommented, then remember to also calculate the
433 correct master dark exposure time below.
434 irplib_msg("Normalizing from %f s to unit exposure time", exposure_time);
435 check(cpl_image_divide_scalar(current_dark, exposure_time),
436 "Error normalizing dark frame"); */
437
438 /* Append to imagelist */
439 cpl_imagelist_set(preproc_images, current_dark, i);
440
441 /* Don't deallocate the image. It will be deallocated when
442 the image list is deallocated */
443 current_dark = NULL;
444 }
445
446
447 /* Check exposure times */
448 cpl_msg_info(cpl_func,
449 "Exposure times range from %e s to %e s (%e %% variation)", min_exptime,
450 max_exptime, 100 * (max_exptime - min_exptime) / min_exptime);
451
452 if ((max_exptime - min_exptime) / min_exptime > .001) {
453 cpl_msg_warning(cpl_func, "Exposure times differ by %e %%",
454 100 * (max_exptime - min_exptime) / min_exptime);
455 }
456
457 /* compute correct exposure time */
458 *mean_exptime=0.5 * (max_exptime + min_exptime);
459 return preproc_images;
460}
461
cpl_imagelist * xsh_irplib_mkmaster_dark_fill_imagelist(const cpl_imagelist *raw_images, cpl_propertylist **raw_headers, const cpl_image *master_bias, double *mean_exptime)
cpl_image * xsh_irplib_mkmaster_median(cpl_imagelist *images, const double kappa, const int nclip, const double tolerance)
Computes master frame by clean stack median of the input imagelist.
static cpl_error_code irplib_imagelist_subtract_values(cpl_imagelist **iml, cpl_vector *values)
Subtract from input imagelist values specified in input vector.
cpl_vector * xsh_irplib_imagelist_get_clean_mean_levels(const cpl_imagelist *iml, const double kappa, const int nclip, const double tolerance)
find out the character string associated to the DIT keyword in a propertylist
cpl_image * xsh_irplib_mkmaster_mean(cpl_imagelist *images, const double kappa, const int nclip, const double tolerance, const double klow, const double khigh)
Computes master frame by clean stack mean of the input imagelist.
static double sigma
int size
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
Definition: xsh_pfits.c:2254
static cpl_error_code TYPE_ADD() xsh_ksigma_clip(const CPL_TYPE *pi, cpl_binary *pm, int llx, int lly, int urx, int ury, int nx, double var_sum, int npixs, double kappa, int nclip, double tolerance, double *mean, double *stdev)
int nx
double kappa
Definition: xsh_detmon_lg.c:81
int n
Definition: xsh_detmon_lg.c:92
double tolerance
int ny
cpl_image * xsh_imagelist_collapse_median_create(cpl_imagelist *iml)
Compute median on imagelist.