67#define REGDEBUG_FULL 0
78 cpl_frame *result_frame = NULL;
79 cpl_frame *abs_frame = NULL;
80 cpl_frame *sign_frame = NULL;
81 cpl_frame *rmcrh_frame = NULL;
82 const char* name = NULL;
83 cpl_propertylist *header = NULL;
88 check( name = cpl_frame_get_filename( sci_frame));
89 check( header = cpl_propertylist_load( name, 0));
97 xsh_msg(
"Not use remove crh single");
98 check( result_frame = cpl_frame_duplicate( sci_frame));
103 single_par, res_tag));
108 if (cpl_error_get_code() != CPL_ERROR_NONE){
137 const char * res_tag) {
139 double sigma_lim, f_lim;
141 cpl_frame* res_frame = NULL;
142 char* res_name = NULL;
144 cpl_image* laplacian_image = NULL;
145 cpl_image* laplacian_redu_image = NULL;
147 cpl_image* two_sub_sample = NULL;
148 cpl_image* noise_image = NULL;
149 cpl_image* s_median_image = NULL;
150 cpl_image* s_image = NULL;
151 cpl_image* sci_median3_image = NULL;
152 cpl_image* sci_median3_7_image = NULL;
153 cpl_image* f_image = NULL;
154 cpl_image* r_image = NULL;
155 int two_sub_sample_nx = 0;
156 int two_sub_sample_ny = 0;
160 cpl_image* sci_image = NULL;
161 cpl_image* sci_copy = NULL;
162 cpl_image* err_image = NULL;
163 cpl_image* qua_image = NULL;
166 float* sci_data = NULL;
167 float* err_data = NULL;
168 int* qua_data = NULL;
169 cpl_binary* msk_data = NULL;
171 float* two_sub_sample_data = NULL;
172 float* laplacian_data = NULL;
173 float* laplacian_redu_data = NULL;
175 float* sci_median3_data = NULL;
176 float* sci_median3_7_data = NULL;
178 float* s_data = NULL;
179 float* s_median_data = NULL;
181 float* f_data = NULL;
182 float* r_data = NULL;
185 cpl_matrix* laplacian_kernel = NULL;
186 cpl_matrix* median3_kernel = NULL;
187 cpl_matrix* median5_kernel = NULL;
188 cpl_matrix* median7_kernel = NULL;
189 int new_crh = 1, nb_crh = 0;
192 cpl_vector* median = NULL;
203 f_lim = single_par->
f_lim;
204 max_iter = single_par->
nb_iter;
206 " Params: sigma_lim %.2f f_lim %.2f, iter %d", sigma_lim, f_lim, max_iter);
212 check( sci_copy = cpl_image_duplicate( sci_image));
216 if (sky_map != NULL) {
217 cpl_image_reject_from_mask(sci_copy,sky_map);
218 msk_data = cpl_mask_get_data( sky_map);
229 check( laplacian_kernel = cpl_matrix_new(3,3));
230 cpl_matrix_set(laplacian_kernel, 0, 0, 0.0);
231 cpl_matrix_set(laplacian_kernel, 0, 1, -1.0);
232 cpl_matrix_set(laplacian_kernel, 0, 2, 0.0);
233 cpl_matrix_set(laplacian_kernel, 1, 0, -1.0);
234 cpl_matrix_set(laplacian_kernel, 1, 1, 4.0);
235 cpl_matrix_set(laplacian_kernel, 1, 2, -1.0);
236 cpl_matrix_set(laplacian_kernel, 2, 0, 0.0);
237 cpl_matrix_set(laplacian_kernel, 2, 1, -1.0);
238 cpl_matrix_set(laplacian_kernel, 2, 2, 0.0);
241 check( median3_kernel = cpl_matrix_new(3,3));
242 for (j = 0; j < 3; j++) {
243 for (i = 0; i < 3; i++) {
244 cpl_matrix_set(median3_kernel, i, j, 1.0);
249 check( median5_kernel = cpl_matrix_new(5,5));
250 for (j = 0; j < 5; j++) {
251 for (i = 0; i < 5; i++) {
252 cpl_matrix_set(median5_kernel, i, j, 1.0);
257 check( median7_kernel = cpl_matrix_new(7,7));
258 for (j = 0; j < 7; j++) {
259 for (i = 0; i < 7; i++) {
260 cpl_matrix_set(median7_kernel, i, j, 1.0);
265 check(sci_data = cpl_image_get_data_float( sci_copy));
266 check(err_data = cpl_image_get_data_float( err_image));
267 check(qua_data = cpl_image_get_data_int( qua_image));
269 two_sub_sample_nx =
nx * 2;
270 two_sub_sample_ny =
ny * 2;
272 two_sub_sample = cpl_image_new( two_sub_sample_nx, two_sub_sample_ny, CPL_TYPE_FLOAT));
273 check(two_sub_sample_data = cpl_image_get_data_float( two_sub_sample));
274 check( laplacian_redu_image = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT));
275 check(laplacian_redu_data = cpl_image_get_data_float( laplacian_redu_image));
276 check( noise_image = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT));
278 check( s_image = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT));
279 check( s_data = cpl_image_get_data_float( s_image));
282 check( f_image = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT));
283 check( f_data = cpl_image_get_data_float( f_image));
284 check( r_image = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT));
286 check( r_data = cpl_image_get_data_float( r_image));
289 while (new_crh > 0 && nbiter <= max_iter) {
300 int j_2_two_sub_sample_nx = 0;
302 for (j = 0; j <
ny; j++) {
303 j_2_two_sub_sample_nx = j * 2 * two_sub_sample_nx;
305 for (i = 0; i <
nx; i++) {
306 float val = sci_data[i + j_nx];
307 pix = i * 2 + j_2_two_sub_sample_nx;
308 two_sub_sample_data[pix] = val;
309 two_sub_sample_data[pix + two_sub_sample_nx] = val;
311 two_sub_sample_data[pix] = val;
312 two_sub_sample_data[pix + two_sub_sample_nx] = val;
327 check(laplacian_data = cpl_image_get_data_float( laplacian_image));
328 for (i = 0; i < two_sub_sample_nx * two_sub_sample_ny; i++) {
329 if (laplacian_data[i] < 0.0) {
330 laplacian_data[i] = 0.0;
332 laplacian_data[i] *= 8.0;
339 cpl_image_save(laplacian_image,
"Lpositive.fits", CPL_BPP_IEEE_FLOAT, NULL,
352 for (j = 0; j <
ny; j++) {
353 j_2_two_sub_sample_nx = j * 2 * two_sub_sample_nx;
355 for (i = 0; i <
nx; i++) {
356 pix = i * 2 + j_2_two_sub_sample_nx;
357 laplacian_redu_data[i + j_nx] = (laplacian_data[pix]
358 + laplacian_data[pix + 1] + laplacian_data[pix + two_sub_sample_nx]
359 + laplacian_data[pix + two_sub_sample_nx + 1]) * 0.25;
364 cpl_image_save(laplacian_redu_image,
"Lplus.fits", CPL_BPP_IEEE_FLOAT,
365 NULL, CPL_IO_DEFAULT);
371 for (i = 0; i <
nx *
ny; i++) {
372 s_data[i] = 0.5 * laplacian_redu_data[i] / err_data[i];
378 check( s_median_data = cpl_image_get_data_float( s_median_image));
383 for (i = 0; i <
nx *
ny; i++) {
384 s_data[i] -= s_median_data[i];
387 cpl_image_save( s_image,
"S2.fits", CPL_BPP_IEEE_FLOAT, NULL,
395 check( sci_median3_data = cpl_image_get_data_float( sci_median3_image));
401 check( sci_median3_7_data = cpl_image_get_data_float( sci_median3_7_image));
407 for (i = 0; i <
nx *
ny; i++) {
408 f_data[i] = sci_median3_data[i] - sci_median3_7_data[i];
409 if (f_data[i] < 0.01) {
414 cpl_image_save( f_image,
"F.fits", CPL_BPP_IEEE_FLOAT, NULL,
420 for (i = 0; i <
nx *
ny; i++) {
421 r_data[i] = laplacian_redu_data[i] / f_data[i];
424 cpl_image_save( r_image,
"R.fits", CPL_BPP_IEEE_FLOAT, NULL,
432 median = cpl_vector_new(24);
433 for (j = 0; j <
ny - 1; j++) {
436 cpl_vector* med_vect = NULL;
439 for (i = 0; i <
nx - 1 ; i++) {
440 int i_plus_j_nx = i + j_nx;
441 if ( (msk_data != NULL) && (msk_data[i + j_nx] != CPL_BINARY_1) ) {
448 if ( s_data[i_plus_j_nx] > sigma_lim ) {
449 if ( r_data[i_plus_j_nx] > f_lim ) {
464 if (li < 0) { li = 0; }
465 if (ui >=
nx) { ui =
nx - 1; }
466 if (lj < 0) { lj = 0; }
467 if (uj >=
ny) { uj =
ny - 1;}
469 for (k = lj; k <= uj; k++) {
471 for (l = li; l <= ui; l++) {
472 int l_plus_k_nx = l + k_nx;
473 if (s_data[l_plus_k_nx] <= sigma_lim) {
474 cpl_vector_set(median,
m, sci_data[l_plus_k_nx]);
478 if (r_data[l_plus_k_nx] <= f_lim) {
479 cpl_vector_set(median,
m, sci_data[l_plus_k_nx]);
489 check(
data = cpl_vector_get_data( median));
491 check( sci_data[i_plus_j_nx] = cpl_vector_get_median(med_vect));
492 cpl_vector_unwrap(med_vect);
505 " new cosmics %d, total %d [%d pixels]", new_crh, nb_crh,
nx*
ny);
513 cpl_image_save( qua_image,
"CRH_SINGLE.fits", CPL_BPP_32_SIGNED, NULL,
523 check( cpl_frame_set_tag( res_frame, res_tag ) );
static xsh_instrument * instrument
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
cpl_image * xsh_pre_get_errs(xsh_pre *pre)
Get errs.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define XSH_ASSURE_NOT_NULL(pointer)
#define xsh_msg_dbg_medium(...)
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
#define xsh_msg_dbg_high(...)
int xsh_pfits_get_qc_ncrh(const cpl_propertylist *plist)
Find out the QC NCRH value.
void xsh_add_qc_crh(xsh_pre *pre, int nbcrh, int nframes)
cpl_frame * xsh_abs_remove_crh_single(cpl_frame *sci_frame, xsh_instrument *instrument, xsh_remove_crh_single_param *single_par, const char *res_tag)
cpl_frame * xsh_remove_crh_single(cpl_frame *in_sci_frame, xsh_instrument *instrument, cpl_mask *sky_map, xsh_remove_crh_single_param *single_par, const char *res_tag)
Remove cosmic rays from a single frame.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
cpl_frame * xsh_frame_mult(cpl_frame *in, xsh_instrument *instr, cpl_frame *sign)
Computes product of two input frames.
cpl_frame * xsh_frame_abs(cpl_frame *in, xsh_instrument *instr, cpl_frame **sign)
Computes absolute value of a frame.
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
void xsh_free_matrix(cpl_matrix **m)
Deallocate a matrix and set the pointer to NULL.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
#define QFLAG_COSMIC_RAY_UNREMOVED
#define XSH_QC_CRH_NUMBER
#define XSH_FREE(POINTER)
cpl_image * xsh_image_filter_median(const cpl_image *img, const cpl_matrix *mx)
cpl_image * xsh_image_filter_linear(const cpl_image *img, const cpl_matrix *mx)