00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <math.h>
00037
00038 #include "xsh_irplib_mkmaster.h"
00039 #include "xsh_utils_imagelist.h"
00040 #include "xsh_pfits.h"
00041 #include "xsh_msg.h"
00042
00046
00047
00049
00056
00057
00058
00070
00071 cpl_vector *
00072 xsh_irplib_imagelist_get_clean_mean_levels(const cpl_imagelist* iml,
00073 const double kappa,
00074 const int nclip,
00075 const double tolerance)
00076 {
00077
00078 const cpl_image* img=NULL;
00079 int size=0;
00080 int i=0;
00081 cpl_vector* levels=NULL;
00082 double* pval=NULL;
00083 double mean=0;
00084 double stdev=0;
00085
00086
00087 cpl_error_ensure(iml != NULL, CPL_ERROR_NULL_INPUT, return(levels),
00088 "Null input image list");
00089 cpl_error_ensure(kappa >= 0, CPL_ERROR_ILLEGAL_INPUT, return(levels),
00090 "Must be kappa>0");
00091
00092 size=cpl_imagelist_get_size(iml);
00093 levels=cpl_vector_new(size);
00094 pval=cpl_vector_get_data(levels);
00095
00096 for(i=0;i<size;i++) {
00097 img=cpl_imagelist_get_const(iml,i);
00098 xsh_ksigma_clip(img,1,1, cpl_image_get_size_x(img),
00099 cpl_image_get_size_y(img),
00100 nclip,kappa,tolerance,&mean,&stdev);
00101
00102 pval[i]=mean;
00103 }
00104
00105
00106 return levels;
00107 }
00108
00109
00117
00118 static cpl_error_code
00119 irplib_imagelist_subtract_values(cpl_imagelist** iml, cpl_vector* values)
00120 {
00121
00122 cpl_image* img=NULL;
00123 int size=0;
00124 int i=0;
00125 double* pval=NULL;
00126
00127 size=cpl_imagelist_get_size(*iml);
00128 pval=cpl_vector_get_data(values);
00129
00130 for(i=0;i<size;i++) {
00131 img=cpl_imagelist_get(*iml,i);
00132 cpl_image_subtract_scalar(img,pval[i]);
00133 cpl_imagelist_set(*iml,img,i);
00134 }
00135
00136 return cpl_error_get_code();
00137 }
00138
00139
00152
00153 static double
00154 irplib_vector_ksigma(cpl_vector *values,
00155 const double klow, const double khigh, int kiter)
00156 {
00157 cpl_vector *accepted;
00158 double mean = 0.0;
00159 double sigma = 0.0;
00160 double *data = cpl_vector_get_data(values);
00161 int n = cpl_vector_get_size(values);
00162 int ngood = n;
00163 int count = 0;
00164 int i;
00165
00166
00167
00168
00169
00170
00171 mean = cpl_vector_get_median(values);
00172
00173 for (i = 0; i < n; i++) {
00174 sigma += (mean - data[i]) * (mean - data[i]);
00175 }
00176 sigma = sqrt(sigma / (n - 1));
00177
00178 while (kiter) {
00179 count = 0;
00180 for (i = 0; i < ngood; i++) {
00181 if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
00182 data[count] = data[i];
00183 ++count;
00184 }
00185 }
00186
00187 if (count == 0)
00188 break;
00189
00190
00191
00192
00193
00194
00195
00196 accepted = cpl_vector_wrap(count, data);
00197 mean = cpl_vector_get_mean(accepted);
00198 if(count>1) {
00199 sigma = cpl_vector_get_stdev(accepted);
00200 }
00201 cpl_vector_unwrap(accepted);
00202
00203 if (count == ngood) {
00204 break;
00205 }
00206 ngood = count;
00207 --kiter;
00208 }
00209
00210 return mean;
00211 }
00212
00213
00232 static cpl_image *
00233 irplib_imagelist_ksigma_stack(const cpl_imagelist *imlist,
00234 double klow, double khigh, int kiter)
00235 {
00236 int ni, nx, ny, npix;
00237 cpl_image *out_ima=NULL;
00238 cpl_imagelist *loc_iml=NULL;
00239 float *pout_ima=NULL;
00240 cpl_image *image=NULL;
00241 const float **data=NULL;
00242 double *med=NULL;
00243 cpl_vector *time_line=NULL;
00244
00245 double *ptime_line=NULL;
00246 int i, j;
00247 double mean_of_medians=0;
00248
00249 cpl_error_ensure(imlist != NULL, CPL_ERROR_NULL_INPUT, return(out_ima),
00250 "Null input image list");
00251
00252 ni = cpl_imagelist_get_size(imlist);
00253 loc_iml = cpl_imagelist_duplicate(imlist);
00254 image = cpl_imagelist_get(loc_iml, 0);
00255 nx = cpl_image_get_size_x(image);
00256 ny = cpl_image_get_size_y(image);
00257 npix = nx * ny;
00258
00259 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
00260 pout_ima = cpl_image_get_data_float(out_ima);
00261
00262 time_line = cpl_vector_new(ni);
00263
00264 ptime_line = cpl_vector_get_data(time_line);
00265
00266 data = cpl_calloc(sizeof(float *), ni);
00267 med = cpl_calloc(sizeof(double), ni);
00268
00269 for (i = 0; i < ni; i++) {
00270 image = cpl_imagelist_get(loc_iml, i);
00271 med[i]=cpl_image_get_median(image);
00272 cpl_image_subtract_scalar(image,med[i]);
00273 data[i] = cpl_image_get_data_float(image);
00274 mean_of_medians+=med[i];
00275
00276 }
00277 mean_of_medians/=ni;
00278
00279 for (i = 0; i < npix; i++) {
00280 for (j = 0; j < ni; j++) {
00281 ptime_line[j] = data[j][i];
00282 }
00283 pout_ima[i] = irplib_vector_ksigma(time_line, klow, khigh, kiter);
00284 }
00285
00286 cpl_image_add_scalar(out_ima,mean_of_medians);
00287
00288
00289 cpl_free(data);
00290 cpl_free(med);
00291 cpl_vector_delete(time_line);
00292 cpl_imagelist_delete(loc_iml);
00293
00294 return out_ima;
00295
00296 }
00297
00298
00299
00300
00301
00315
00316 cpl_image*
00317 xsh_irplib_mkmaster_mean(cpl_imagelist* images,const double kappa, const int nclip,
00318 const double tolerance,const double klow,const double khigh)
00319 {
00320
00321 cpl_image* master=NULL;
00322 cpl_vector* levels=NULL;
00323 double mean=0;
00324 cpl_imagelist* iml=NULL;
00325
00326
00327 iml=cpl_imagelist_duplicate(images);
00328 levels=xsh_irplib_imagelist_get_clean_mean_levels(iml,kappa,nclip,tolerance);
00329 mean=cpl_vector_get_mean(levels);
00330
00331
00332 irplib_imagelist_subtract_values(&iml,levels);
00333
00334
00335 master = cpl_imagelist_collapse_sigclip_create(iml,klow,khigh,.1,CPL_COLLAPSE_MEAN,NULL);
00336 cpl_image_add_scalar(master,mean);
00337
00338 cpl_vector_delete(levels);
00339 cpl_imagelist_delete(iml);
00340
00341 return master;
00342
00343 }
00344
00345
00346
00347
00359
00360 cpl_image*
00361 xsh_irplib_mkmaster_median(cpl_imagelist* images,const double kappa, const int nclip,
00362 const double tolerance)
00363 {
00364
00365 cpl_image* master=NULL;
00366 cpl_vector* levels=NULL;
00367 double mean=0;
00368 cpl_imagelist* iml=NULL;
00369
00370
00371 iml=cpl_imagelist_duplicate(images);
00372 levels=xsh_irplib_imagelist_get_clean_mean_levels(iml,kappa,nclip,tolerance);
00373
00374 mean=cpl_vector_get_mean(levels);
00375
00376 irplib_imagelist_subtract_values(&iml,levels);
00377
00378
00379
00380 master = xsh_imagelist_collapse_median_create(iml);
00381
00382
00383 cpl_image_add_scalar(master,mean);
00384
00385 cpl_vector_delete(levels);
00386 cpl_imagelist_delete(iml);
00387
00388 return master;
00389
00390 }
00391
00392 cpl_imagelist*
00393 xsh_irplib_mkmaster_dark_fill_imagelist(const cpl_imagelist* raw_images,
00394 cpl_propertylist** raw_headers, const cpl_image* master_bias,
00395 double* mean_exptime) {
00396
00397
00398
00399 cpl_imagelist* preproc_images = NULL;
00400 int i = 0;
00401 cpl_image* current_dark = NULL;
00402 double min_exptime = 0;
00403 double max_exptime = 0;
00404
00405 preproc_images = cpl_imagelist_new();
00406 for (i = 0; i < cpl_imagelist_get_size(raw_images); i++) {
00407 double exposure_time = 0.0;
00408 const cpl_propertylist *current_header;
00409
00410 current_dark = cpl_image_duplicate(cpl_imagelist_get_const(raw_images, i));
00411 current_header = raw_headers[i];
00412
00413
00414 if (master_bias != NULL) {
00415
00416 cpl_image_subtract(current_dark, master_bias);
00417 } else {
00418
00419 }
00420
00421 exposure_time = xsh_pfits_get_exptime(current_header);
00422
00423 if (i == 0 || exposure_time < min_exptime) {
00424 min_exptime = exposure_time;
00425 }
00426 if (i == 0 || exposure_time > max_exptime) {
00427 max_exptime = exposure_time;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 cpl_imagelist_set(preproc_images, current_dark, i);
00439
00440
00441
00442 current_dark = NULL;
00443 }
00444
00445
00446
00447 cpl_msg_info(cpl_func,
00448 "Exposure times range from %e s to %e s (%e %% variation)", min_exptime,
00449 max_exptime, 100 * (max_exptime - min_exptime) / min_exptime);
00450
00451 if ((max_exptime - min_exptime) / min_exptime > .001) {
00452 cpl_msg_warning(cpl_func, "Exposure times differ by %e %%",
00453 100 * (max_exptime - min_exptime) / min_exptime);
00454 }
00455
00456
00457 *mean_exptime=0.5 * (max_exptime + min_exptime);
00458 return preproc_images;
00459 }
00460