X-shooter Pipeline Reference Manual 3.8.15
xsh_create_master.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2013-10-04 12:36:39 $
23 * $Revision: 1.173 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*----------------------------------------------------------------------------*/
39/*----------------------------------------------------------------------------*/
42/*-----------------------------------------------------------------------------
43 Includes
44 -----------------------------------------------------------------------------*/
45#include <xsh_drl.h>
46#include <xsh_dfs.h>
47//#include <xsh_pfits.h>
48#include <xsh_pfits_qc.h>
49#include <xsh_utils_wrappers.h>
50#include <xsh_utils_imagelist.h>
51#include <xsh_pfits.h>
52#include <xsh_pfits_qc.h>
53#include <xsh_error.h>
54#include <xsh_msg.h>
55#include <xsh_badpixelmap.h>
56#include <xsh_paf_save.h>
57#include <xsh_utils_image.h>
58#include <xsh_detmon.h>
59#include <irplib_utils.h> //For skip_if
60#include <xsh_utils_image.h>
61#include <xsh_irplib_mkmaster.h>
62#include <xsh_data_order.h>
63#include <math.h>
64
65/*-----------------------------------------------------------------------------
66 Typedefs
67 ---------------------------------------------------------------------------*/
68
69/*-----------------------------------------------------------------------------
70 Defines
71 ---------------------------------------------------------------------------*/
72
73#define RECIPE_ID "xsh_mbias"
74#define HIST_FACT 2.354820045
75/*-----------------------------------------------------------------------------
76 Functions prototypes
77 -----------------------------------------------------------------------------*/
78
79static cpl_error_code
80xsh_compute_ron_mbias(cpl_frameset* raws,
81 xsh_pre* master,
82 cpl_parameterlist* parameters);
83
84
85static cpl_error_code
86xsh_compute_fpn_mbias(cpl_frameset* raws,
87 xsh_pre* master,
89 cpl_parameterlist* parameters);
90
91static cpl_error_code
92xsh_mbias_get_fpn(const cpl_image* ima,
93 cpl_parameterlist* parameters,
94 double* fpn);
95
96
97/*-----------------------------------------------------------------------------
98 Implementation
99 -----------------------------------------------------------------------------*/
100
101
102static double
103xsh_mdark_get_contam(xsh_pre * pre,cpl_parameterlist* params,
104xsh_instrument* instr)
105{
106
107 double contam=0;
108
109 int llx_on=0;
110 int lly_on=0;
111 int urx_on=0;
112 int ury_on=0;
113
114 int llx_off=0;
115 int lly_off=0;
116 int urx_off=0;
117 int ury_off=0;
118 if(xsh_parameters_find(params,"xsh_mdark","contam_on_llx")!=NULL){
119 check(llx_on=xsh_parameters_get_int(params,"xsh_mdark","contam_on_llx"));
120 check(lly_on=xsh_parameters_get_int(params,"xsh_mdark","contam_on_lly"));
121 check(urx_on=xsh_parameters_get_int(params,"xsh_mdark","contam_on_urx"));
122 check(ury_on=xsh_parameters_get_int(params,"xsh_mdark","contam_on_ury"));
123
124
125 check(llx_off=xsh_parameters_get_int(params,"xsh_mdark","contam_off_llx"));
126 check(lly_off=xsh_parameters_get_int(params,"xsh_mdark","contam_off_lly"));
127 check(urx_off=xsh_parameters_get_int(params,"xsh_mdark","contam_off_urx"));
128 check(ury_off=xsh_parameters_get_int(params,"xsh_mdark","contam_off_ury"));
129
130 } else {
131 if(xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
132 llx_on =801; lly_on =1058;urx_on =810; ury_on =1067;
133 llx_off=855; lly_off=1066;urx_off=864; ury_off=1075;
134 } else if(xsh_instrument_get_arm(instr) == XSH_ARM_VIS) {
135 llx_on =1672; lly_on =2672;urx_on =1681;ury_on =2681;
136 llx_off=1926; lly_off=2941;urx_off=1935;ury_off=2950;
137 } else if(xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
138 llx_on =991; lly_on =1083;urx_on =1015;ury_on =1108;
139 llx_off=1029;lly_off=1075;urx_off=1053;ury_off=1110;
140 } else if(xsh_instrument_get_arm(instr) == XSH_ARM_AGC) {
141 llx_on =25; lly_on =10;urx_on =550;ury_on =520;
142 llx_off=25;lly_off=10;urx_off=550;ury_off=520;
143 }
144 }
145
146 llx_on=llx_on/pre->binx;
147 lly_on=lly_on/pre->biny;
148 urx_on=urx_on/pre->binx;
149 ury_on=ury_on/pre->biny;
150
151 llx_off=llx_off/pre->binx;
152 lly_off=lly_off/pre->biny;
153 urx_off=urx_off/pre->binx;
154 ury_off=ury_off/pre->biny;
155
156 check(contam=
157 cpl_image_get_median_window(pre->data,llx_on,lly_on,urx_on,ury_on)-
158 cpl_image_get_median_window(pre->data,llx_off,lly_off,urx_off,ury_off));
159
160
161cleanup:
162
163
164 return contam;
165
166
167}
168
169
174cpl_error_code
175xsh_mdark_get_median_stdev(xsh_pre* preFrame,cpl_parameterlist* parameters,
176 cpl_frame* crh_frm, cpl_frame* bpm_frm)
177{
178
179 int sx=0;
180 int sy=0;
181 int ref_llx=-1;
182 int ref_lly=-1;
183 int ref_urx=-1;
184 int ref_ury=-1;
185 xsh_pre* dup_frm=NULL;
186 const char* crh_name=NULL;
187 const char* bpm_name=NULL;
188 cpl_image* crh_ima=NULL;
189 cpl_image* bpm_ima=NULL;
190 int* pcrh=NULL;
191 int* pbpm=NULL;
192 int* pqua=NULL;
193 int i=0;
194 bool bpm_found=false;
195 bool crh_found=false;
196 double avg_value = 0.0;
197 double median_value = 0.0;
198 double stdev = 0.0;
199
200 check(ref_llx=xsh_parameters_get_int(parameters,"xsh_mdark","ref1_llx"));
201 check(ref_lly=xsh_parameters_get_int(parameters,"xsh_mdark","ref1_lly"));
202 check(ref_urx=xsh_parameters_get_int(parameters,"xsh_mdark","ref1_urx"));
203 check(ref_ury=xsh_parameters_get_int(parameters,"xsh_mdark","ref1_ury"));
204 sx=cpl_image_get_size_x(preFrame->data);
205 sy=cpl_image_get_size_y(preFrame->data);
206
207 /* QC parameters */
208
209 if(ref_llx == -1) ref_llx=1;
210 if(ref_lly == -1) ref_lly=1;
211 if(ref_urx == -1) ref_urx=sx;
212 if(ref_ury == -1) ref_ury=sy;
213
214 ref_llx = (ref_llx>0) ? ref_llx : 1;
215 ref_lly = (ref_lly>0) ? ref_lly : 1;
216 ref_urx = (ref_urx<sx) ? ref_urx : sx;
217 ref_ury = (ref_ury<sy) ? ref_ury : sy;
218
219 /*
220 Median and avg should ignore the bad pixels including the Cosmics.
221 But at this point the preframe->qual DOES NOT include the COSMIC RAYS !
222 (see xsh_remove_crh_multi function in xsh_remove_crh_multi.c)
223 Thus we flag CRHs and BP.
224 */
225
226 dup_frm=xsh_pre_duplicate(preFrame);
227
228 if(crh_frm!=NULL) {
229 check(crh_name=cpl_frame_get_filename(crh_frm));
230 check(crh_ima=cpl_image_load(crh_name,XSH_PRE_QUAL_TYPE,0,0));
231 check(pcrh=cpl_image_get_data_int(crh_ima));
232 crh_found=true;
233 }
234
235 if(bpm_frm != NULL) {
236 check(bpm_name=cpl_frame_get_filename(bpm_frm));
237 check(bpm_ima=cpl_image_load(bpm_name,XSH_PRE_QUAL_TYPE,0,0));
238 check(pbpm=cpl_image_get_data_int(bpm_ima));
239 bpm_found=true;
240 }
241
242 if (bpm_found || crh_found) {
243 int sx_sy=0;
244 check(pqua=cpl_image_get_data_int( preFrame->qual));
245 sx = preFrame->nx;
246 sy = preFrame->ny;
247 sx_sy=sx*sy;
248 if (bpm_found && crh_found) {
249 for (i = 0; i < sx_sy; i++) {
250 if (pcrh[i] != 0 || pbpm[i] != 0) {
251 pqua[i] = XSH_GOOD_PIXEL_LEVEL;
252 }
253 }
254 } else if (bpm_found) {
255 for (i = 0; i < sx_sy; i++) {
256 if (pbpm[i] != 0) {
257 pqua[i] = XSH_GOOD_PIXEL_LEVEL;
258 }
259 }
260 } else if (crh_found) {
261 for (i = 0; i < sx_sy; i++) {
262 if (pcrh[i] != 0) {
263 pqua[i] = XSH_GOOD_PIXEL_LEVEL;
264 }
265 }
266 }
267 }
268 xsh_free_image(&bpm_ima);
269 xsh_free_image(&crh_ima);
270 xsh_pre_free(&dup_frm);
271
273 &avg_value, &median_value,&stdev,
274 ref_llx,ref_lly,ref_urx,ref_ury) ) ;
275
276 xsh_pfits_set_qc_mdarkavg( preFrame->data_header,avg_value) ;
277 xsh_pfits_set_qc_mdarkmed( preFrame->data_header,median_value) ;
278 xsh_pfits_set_qc_mdarkrms( preFrame->data_header,stdev) ;
279
280 cleanup:
281 xsh_free_image(&bpm_ima);
282 xsh_free_image(&crh_ima);
283 xsh_pre_free(&dup_frm);
284 return cpl_error_get_code();
285}
286
287static cpl_error_code
288xsh_mdark_measure_fpn(xsh_pre* preFrame,cpl_parameterlist* parameters,xsh_instrument* instrument)
289{
290
291 /* fpn computation (DRS mode) */
292 cpl_size zone_fpn[4];
293 int fpn_llx=-1;
294 int fpn_lly=-1;
295 int fpn_urx=-1;
296 int fpn_ury=-1;
297 int fpn_nsamp=100;
298 int fpn_hsize=4;
299 double exptime=preFrame->exptime;
300 double qc_fpn_val=0;
301 double qc_fpn_err=0;
302 int sx=0;
303 int sy=0;
304 double median=0;
305
306 check(fpn_llx=xsh_parameters_get_int(parameters,"xsh_mdark","fpn_llx"));
307 check(fpn_lly=xsh_parameters_get_int(parameters,"xsh_mdark","fpn_lly"));
308 check(fpn_urx=xsh_parameters_get_int(parameters,"xsh_mdark","fpn_urx"));
309 check(fpn_ury=xsh_parameters_get_int(parameters,"xsh_mdark","fpn_ury"));
310 check(fpn_nsamp=xsh_parameters_get_int(parameters,"xsh_mdark","fpn_nsamples"));
311 check(fpn_hsize=xsh_parameters_get_int(parameters,"xsh_mdark","fpn_hsize"));
312 //xsh_msg("fpn=%d %d %d %d",fpn_llx,fpn_lly,fpn_urx,fpn_ury);
313 sx=cpl_image_get_size_x(preFrame->data);
314 sy=cpl_image_get_size_y(preFrame->data);
315
316 /* QC parameters */
317
318 if(fpn_llx == -1) fpn_llx=1;
319 if(fpn_lly == -1) fpn_lly=1;
320 if(fpn_urx == -1) fpn_urx=sx;
321 if(fpn_ury == -1) fpn_ury=sy;
322
323 fpn_llx = (fpn_llx>0) ? fpn_llx : 1;
324 fpn_lly = (fpn_lly>0) ? fpn_lly : 1;
325 fpn_urx = (fpn_urx<sx) ? fpn_urx : sx;
326 fpn_ury = (fpn_ury<sy) ? fpn_ury : sy;
327
328 assure(fpn_urx>fpn_llx,CPL_ERROR_ILLEGAL_INPUT,
329 "Must be fpn_urx (%d) > fpn_llx (%d)",
330 fpn_urx,fpn_llx);
331
332 assure(fpn_ury>fpn_lly,CPL_ERROR_ILLEGAL_INPUT,
333 "Must be fpn_ury (%d) > fpn_lly (%d)",
334 fpn_ury,fpn_lly);
335
336 zone_fpn[0]=fpn_llx;
337 zone_fpn[1]=fpn_urx;
338 zone_fpn[2]=fpn_lly;
339 zone_fpn[3]=fpn_ury;
340 xsh_image_flag_bp(preFrame->data,preFrame->qual,instrument);
341
342 //xsh_msg("fpn=%d %d %d %d hsize=%d nsamp=%d",fpn_llx,fpn_urx,fpn_lly,fpn_ury,
343 // fpn_hsize,fpn_nsamp);
344 check_msg(cpl_flux_get_noise_window(preFrame->data, zone_fpn, fpn_hsize,
345 fpn_nsamp, &qc_fpn_val, &qc_fpn_err),
346 "Error computing noise in a window");
347
348 if ( instrument->arm != XSH_ARM_NIR ) {
349 median=cpl_image_get_median_window(preFrame->data,fpn_llx,fpn_lly,fpn_urx,fpn_ury);
350 qc_fpn_val /= median;
351 qc_fpn_err /= median;
352 /*
353 xsh_msg("DRS norm fpn=%g",qc_fpn_val);
354 xsh_msg("DRS norm err fpn=%g",qc_fpn_err);
355 */
356 xsh_pfits_set_qc_norm_fpn(preFrame->data_header,qc_fpn_val);
357 xsh_pfits_set_qc_norm_fpn_err(preFrame->data_header,qc_fpn_err);
358 /* xsh_msg("DRS norm fpn=%g",qc_fpn_val); */
359
360 qc_fpn_val *= exptime;
361 qc_fpn_err *= exptime;
362
363 xsh_pfits_set_qc_fpn( preFrame->data_header,qc_fpn_val);
364 xsh_pfits_set_qc_fpn_err(preFrame->data_header,qc_fpn_err);
365
366 } else {
367 median=cpl_image_get_median_window(preFrame->data,fpn_llx,fpn_lly,fpn_urx,fpn_ury);
368
369 xsh_pfits_set_qc_fpn( preFrame->data_header,qc_fpn_val);
370 xsh_pfits_set_qc_fpn_err(preFrame->data_header,qc_fpn_err);
371 /*
372 xsh_msg("DRS norm fpn=%g",qc_fpn_val);
373 xsh_msg("DRS norm err fpn=%g",qc_fpn_err);
374 */
375 qc_fpn_val /= median;
376 qc_fpn_err /= median;
377
378 xsh_pfits_set_qc_norm_fpn(preFrame->data_header,qc_fpn_val);
379 xsh_pfits_set_qc_norm_fpn_err(preFrame->data_header,qc_fpn_err);
380 }
381 //abort();
382 /* xsh_msg("DRS fpn=%g",qc_fpn_val); */
383
384 cleanup:
385 return cpl_error_get_code();
386}
387
388
389/* ron region (not used) */
390cpl_error_code
391xsh_mdark_measure_ron(xsh_pre* preFrame,cpl_parameterlist* parameters)
392{
393
394 int ron_llx=-1;
395 int ron_lly=-1;
396 int ron_urx=-1;
397 int ron_ury=-1;
398 //int ron_nsamp=100;
399 //int ron_hsize=4;
400 int sx=0;
401 int sy=0;
402
403 check(ron_llx=xsh_parameters_get_int(parameters,"xsh_mdark","ron_llx"));
404 check(ron_lly=xsh_parameters_get_int(parameters,"xsh_mdark","ron_lly"));
405 check(ron_urx=xsh_parameters_get_int(parameters,"xsh_mdark","ron_urx"));
406 check(ron_ury=xsh_parameters_get_int(parameters,"xsh_mdark","ron_ury"));
407 //check(ron_hsize=xsh_parameters_get_int(parameters,"xsh_mdark","ron_hsize"));
408 //check(ron_nsamp=xsh_parameters_get_int(parameters,"xsh_mdark","ron_nsamples"));
409 sx=cpl_image_get_size_x(preFrame->data);
410 sy=cpl_image_get_size_y(preFrame->data);
411
412 /* QC parameters */
413 if(ron_llx == -1) ron_llx=1;
414 if(ron_lly == -1) ron_lly=1;
415 if(ron_urx == -1) ron_urx=sx;
416 if(ron_ury == -1) ron_ury=sy;
417
418 ron_llx = (ron_llx>0) ? ron_llx : 1;
419 ron_lly = (ron_lly>0) ? ron_lly : 1;
420 ron_urx = (ron_urx<sx) ? ron_urx : sx;
421 ron_ury = (ron_ury<sy) ? ron_ury : sy;
422
423 cleanup:
424 return cpl_error_get_code();
425}
426
437static void
440 cpl_parameterlist* parameters,
441 cpl_frame* crh_frm, cpl_frame* bpm_frm)
442{
443 int nx =0, ny = 0;
444
445 /* xsh_msg("set_masterdark_qc"); */
446 nx = xsh_pre_get_nx (preFrame);
447 assure (nx != 0, cpl_error_get_code (), "Cant get X size");
448 ny = xsh_pre_get_ny (preFrame);
449 assure (ny != 0, cpl_error_get_code (), "Cant get Y size");
450
451 check(xsh_mdark_get_median_stdev(preFrame,parameters,crh_frm,bpm_frm));
452
453 if ( instrument->arm != XSH_ARM_AGC ) {
454 check(xsh_mdark_measure_fpn(preFrame,parameters,instrument));
455 }
456
457 { /* get contamination */
458 double contam=0;
459 check(contam=xsh_mdark_get_contam(preFrame,parameters,instrument));
461 }
462 /* not used
463 check(xsh_mdark_measure_ron(preFrame,parameters));
464 */
465
466 /*
467 Should calculate the "slope", but how ?
468
469 double slope = 0.0 ;
470 slope = xsh_calculate_slope( preFrame->data ) ;
471
472 xsh_pfits_set_qc( preFrame->data_header, (void *)&slope,
473 XSH_QC_MDARKSLOPE, instrument ) ;
474 */
475
476 cleanup:
477
478 return;
479}
480
481
482static cpl_error_code
483xsh_mdark_get_ron(xsh_pre* pre,cpl_parameterlist* params,cpl_propertylist* qc_log,xsh_instrument* instr)
484{
485
486 int fpn_llx=0;
487 int fpn_lly=0;
488 int fpn_urx=0;
489 int fpn_ury=0;
490 cpl_size fpn_hsize=0;
491 cpl_size fpn_nsamp=0;
492 int sx=0;
493 int sy=0;
494 cpl_size zone[4];
495 double qc_ron_val=0;
496 double qc_ron_err=0;
497 //double median=0;
498
499 /* xsh_msg("xsh_mdark_get_fpn"); */
500
501 XSH_ASSURE_NOT_NULL_MSG(pre, "Null input pre");
502 XSH_ASSURE_NOT_NULL_MSG(params, "Null input parameters");
503 XSH_ASSURE_NOT_NULL_MSG(qc_log, "Null input QC propertylist");
504
505 sx=xsh_pre_get_nx(pre);
506 sy=xsh_pre_get_ny(pre);
507
508
509 check(fpn_llx=xsh_parameters_get_int(params,"xsh_mdark","fpn_llx"));
510 check(fpn_lly=xsh_parameters_get_int(params,"xsh_mdark","fpn_lly"));
511 check(fpn_urx=xsh_parameters_get_int(params,"xsh_mdark","fpn_urx"));
512 check(fpn_ury=xsh_parameters_get_int(params,"xsh_mdark","fpn_ury"));
513 check(fpn_hsize=xsh_parameters_get_int(params,"xsh_mdark","fpn_hsize"));
514 check(fpn_nsamp=xsh_parameters_get_int(params,"xsh_mdark","fpn_nsamples"));
515
516
517 /* QC parameters */
518
519 if(fpn_llx == -1) fpn_llx=1;
520 if(fpn_lly == -1) fpn_lly=1;
521 if(fpn_urx == -1) fpn_urx=sx;
522 if(fpn_ury == -1) fpn_ury=sy;
523
524 fpn_llx = (fpn_llx>0) ? fpn_llx : 1;
525 fpn_lly = (fpn_lly>0) ? fpn_lly : 1;
526 fpn_urx = (fpn_urx<sx) ? fpn_urx : sx;
527 fpn_ury = (fpn_ury<sy) ? fpn_ury : sy;
528
529 assure(fpn_urx>fpn_llx,CPL_ERROR_ILLEGAL_INPUT,
530 "Must be fpn_urx (%d) > fpn_llx (%d)",
531 fpn_urx,fpn_llx);
532
533 assure(fpn_ury>fpn_lly,CPL_ERROR_ILLEGAL_INPUT,
534 "Must be fpn_ury (%d) > fpn_lly (%d)",
535 fpn_ury,fpn_lly);
536
537
538 zone[0]=fpn_llx;
539 zone[1]=fpn_urx;
540 zone[2]=fpn_lly;
541 zone[3]=fpn_ury;
542
543 check(xsh_image_flag_bp(pre->data,pre->qual,instr));
544 check_msg(cpl_flux_get_noise_window(pre->data, zone, fpn_hsize,fpn_nsamp,
545 &qc_ron_val, &qc_ron_err),
546 "Error computing noise in a window");
547 /*
548 median=cpl_image_get_median_window(ima,fpn_llx,fpn_lly,fpn_urx,fpn_ury);
549 median=xsh_pfits_get_qc_mdarkmed(qc_log);
550 */
551 /* xsh_msg("fpn=%g,%g",qc_fpn_val,qc_fpn_err); */
552 double inv_sqrt2=1./sqrt(2);
553 xsh_pfits_set_qc_ron(qc_log,qc_ron_val*inv_sqrt2);
554 xsh_pfits_set_qc_ron_err(qc_log,qc_ron_err*inv_sqrt2) ;
555
556 /* as we are using input raw frames difference to get FPN, the noise
557 on the time normalized frame (master dark) has to be divided by
558 exptime */
559/*
560 qc_fpn_val /= exptime;
561 qc_fpn_err /= exptime;
562 qc_fpn_val /= median;
563 qc_fpn_err /= median;
564*/
565 //xsh_pfits_set_qc_norm_fpn(qc_log,qc_fpn_val) ;
566 //xsh_pfits_set_qc_norm_fpn_err(qc_log,qc_fpn_err) ;
567 /* xsh_msg("normalized fpn=%g,%g",qc_fpn_val,qc_fpn_err); */
568
569 cleanup:
570
571 return cpl_error_get_code();
572
573}
574
575
576static cpl_error_code
577xsh_mdark_compute_fpn(cpl_frameset* raws,cpl_parameterlist* params,
578 cpl_propertylist* qclog,xsh_instrument* instrument)
579{
580
581 int nraws=0;
582 cpl_frame* frm=NULL;
583 xsh_pre* pre1=NULL;
584 xsh_pre* pre2=NULL;
585 //double exptime=0;
586
587 XSH_ASSURE_NOT_NULL_MSG(raws, "Null input raw frames set");
588 XSH_ASSURE_NOT_NULL_MSG(params, "Null input parameter list");
589 XSH_ASSURE_NOT_NULL_MSG(qclog, "Null input qc property list");
590
591 nraws=cpl_frameset_get_size(raws);
592
593 XSH_ASSURE_NOT_ILLEGAL_MSG(nraws >1,"Min 2 raw darks needed to compute FPN");
594
595 frm=cpl_frameset_get_frame(raws,0);
596 pre1=xsh_pre_load(frm,instrument);
597
598 frm=cpl_frameset_get_frame(raws,1);
599 pre2=xsh_pre_load(frm,instrument);
600
601 xsh_pre_subtract(pre1,pre2);
602 //exptime=xsh_pfits_get_exptime(pre1->data_header);
603 check(xsh_mdark_get_ron(pre1,params,qclog,instrument));
604
605 cleanup:
606 xsh_pre_free(&pre1);
607 xsh_pre_free(&pre2);
608
609 return cpl_error_get_code();
610
611}
612
621cpl_frame *
622xsh_create_master_dark2(cpl_frameset * raws,
623 xsh_stack_param* stack_param,
624 cpl_parameterlist* params,
625 cpl_propertylist* qc_log,
626 xsh_instrument* instr)
627{
628
629 double mean_exptime=0;
630 cpl_imagelist* raw_data=NULL;
631 cpl_imagelist* raw_errs=NULL;
632 cpl_imagelist* iml_errs=NULL;
633 cpl_imagelist* iml_data=NULL;
634 cpl_image* qual_comb=NULL;
635
636 cpl_propertylist** raw_headers=NULL;
637 cpl_frame* mdark=NULL;
638 cpl_image* mbias=NULL; /* for xsh the bias has been already subtracted */
639 xsh_pre* pre=NULL;
640
641 cpl_frame* frm=NULL;
642 char mdark_name[256];
643 char mdark_tag[256];
644 const double kappa=5.;
645 const int nclip=5;
646 const double tolerance=1.e-5;
647
648 int sz=0;
649 int i=0;
650 int mode_and=0;
651
652 /* prepare image list and list of headers */
653 sz=cpl_frameset_get_size(raws);
654 raw_data=cpl_imagelist_new();
655 raw_errs=cpl_imagelist_new();
656 raw_headers=cpl_calloc(sz,sizeof(cpl_propertylist*));
657
658 for(i=0;i<sz;i++){
659 check(frm=cpl_frameset_get_frame(raws,i));
660 pre=xsh_pre_load(frm,instr);
661 /* inject bad pixels to change statistics */
662 check(xsh_image_flag_bp(pre->data,pre->qual,instr));
663 check(xsh_image_flag_bp(pre->errs,pre->qual,instr));
664 cpl_imagelist_set(raw_data,cpl_image_duplicate(pre->data),i);
665 cpl_imagelist_set(raw_errs,cpl_image_duplicate(pre->errs),i);
666 raw_headers[i]=cpl_propertylist_duplicate(pre->data_header);
667 if(i==0) {
668 qual_comb=cpl_image_duplicate(pre->qual);
669 } else {
670 xsh_badpixelmap_image_coadd(&qual_comb,pre->qual,mode_and);
671 }
672 /* All the input frames have the same bad pixel map, thus use the
673 last one as the "final" one */
674 if (i < (sz-1)) {
675 xsh_pre_free(&pre);
676 }
677 }
678
679 iml_data=xsh_irplib_mkmaster_dark_fill_imagelist(raw_data,raw_headers,mbias,&mean_exptime);
680
681 iml_errs=xsh_irplib_mkmaster_dark_fill_imagelist(raw_errs,raw_headers,mbias,&mean_exptime);
682
683 /*free memory on product extensions that will be re-allocated later on
684 (errs is only modified) */
685 xsh_free_image(&pre->data);
686 xsh_free_image(&pre->qual);
687
688 /* store combined bp map in qual extension of result */
689 pre->qual=cpl_image_duplicate(qual_comb);
690
691 /* Get median stack of input darks */
692 if (strcmp(stack_param->stack_method, "median") == 0) {
693 xsh_msg("Calculating stack median");
694
695 pre->data=xsh_irplib_mkmaster_median(iml_data,kappa,nclip,tolerance);
696 check( xsh_collapse_errs(pre->errs,iml_errs,0));
697
698 } else {
699 xsh_msg("Calculating stack mean");
700 pre->data=xsh_irplib_mkmaster_mean(iml_data,kappa,nclip,tolerance,stack_param->klow,stack_param->khigh);
701
702 check( xsh_collapse_errs(pre->errs,iml_errs,1));
703
704 }
705
706
707 /* If not NIR, Divide master data and errs by exposure time ansd rescale header EXPTIME,
708 * else just correct header exposure time */
709 if ( instr->arm != XSH_ARM_NIR ) {
710
711 check_msg (cpl_image_divide_scalar (pre->data, mean_exptime),
712 "Cant divide median image by exptime %lf", mean_exptime);
713 check_msg (cpl_image_divide_scalar (pre->errs, mean_exptime),
714 "Cant divide median image by exptime %lf", mean_exptime);
715
716 /* Set the EXPTIME at 1. */
717 xsh_msg_dbg_high ("Set EXPTIME = 1.");
718 check(xsh_pfits_set_exptime (pre->data_header, (double) 1.));
719 check(xsh_pfits_set_exptime (pre->errs_header, (double) 1.));
720
721 } else {
722
723 check(xsh_pfits_set_exptime(pre->data_header, mean_exptime ));
724 check(xsh_pfits_set_exptime(pre->errs_header, mean_exptime ));
725
726 }
727
728
729 if(qc_log != NULL ) {
730 if ( instr->arm != XSH_ARM_AGC ) {
731 if(sz>1) {
732 xsh_mdark_compute_fpn(raws,params,qc_log,instr);
733
734 }
735 }
736 }
737 if(qc_log != NULL ) {
738 cpl_propertylist_append(pre->data_header,qc_log);
739 }
740
741
742 sprintf(mdark_tag,"%s_%s",XSH_MASTER_DARK,xsh_instrument_arm_tostring(instr));
743 sprintf(mdark_name,"%s.fits",mdark_tag);
744 xsh_count_satpix(pre,instr,sz);
745 check(mdark = xsh_pre_save( pre,mdark_name,mdark_tag,0));
746 check(cpl_frame_set_tag(mdark,mdark_tag));
747
748cleanup:
749xsh_free_imagelist(&raw_data);
750xsh_free_imagelist(&raw_errs);
751xsh_free_imagelist(&iml_data);
752xsh_free_imagelist(&iml_errs);
753xsh_free_image(&qual_comb);
754xsh_pre_free(&pre);
755
756for(i=0;i<sz;i++){
757 xsh_free_propertylist(&raw_headers[i]);
758}
759cpl_free(raw_headers);
760return mdark;
761}
774cpl_frame *
775xsh_create_master_dark (cpl_frame * medFrame, xsh_instrument* instr,
776 cpl_parameterlist* parameters,
777 cpl_frame* crh_frm,cpl_frame* bpm_frm)
778{
779 /*
780 Load frame into PRE
781 Modify KW, etc
782 Save PRE and returns pointer to frame
783 */
784 xsh_pre *medPre = NULL;
785 double exptime = 0.0;
786 cpl_frame* resFrame = NULL;
787 const char* tag = XSH_GET_TAG_FROM_ARM (XSH_MASTER_DARK, instr);
788 char* finalName = NULL;
789
790 XSH_ASSURE_NOT_NULL(medFrame);
791 XSH_ASSURE_NOT_NULL(instr);
792
793 check(medPre = xsh_pre_load (medFrame,instr));
794 XSH_ASSURE_NOT_NULL(medPre);
795
796 exptime = medPre->exptime;
797
798 /* If not NIR, Divide medFrameand medErrs by exposure time */
799 if ( instr->arm != XSH_ARM_NIR ) {
800 check_msg (cpl_image_divide_scalar (medPre->data, exptime),
801 "Cant divide median image by exptime %lf", exptime);
802 check_msg (cpl_image_divide_scalar (medPre->errs, exptime),
803 "Cant divide median image by exptime %lf", exptime);
804
805 /* Set the EXPTIME at 1. */
806 xsh_msg_dbg_high ("Set EXPTIME = 1.");
807 check(xsh_pfits_set_exptime (medPre->data_header, (double) 1.));
808 check(xsh_pfits_set_exptime (medPre->errs_header, (double) 1.));
809 }
810
811
812 if ( xsh_instrument_get_arm(instr) == XSH_ARM_NIR ) {
813 check(finalName =cpl_sprintf("%s_%s.fits",XSH_MASTER_DARK,
815 } else {
816 /* Sabines doesn't like to have the binning in the file-name
817 check(binx=xsh_pfits_get_binx(medPre->data_header));
818 check(biny=xsh_pfits_get_biny(medPre->data_header));
819 check(finalName =cpl_sprintf("%s_%s_%dx%d.fits",XSH_MASTER_DARK,
820 xsh_instrument_arm_tostring(instr),
821 binx,biny));
822 */
823 check(finalName =cpl_sprintf("%s_%s.fits",XSH_MASTER_DARK,
825 }
826 /* Calculate and Save QC Keywords */
827 /* QC.MDARKMED
828 QC.MDARKRMS
829 QC.MDARKSLOGE (?)
830 */
831 check(set_masterdark_qc (medPre, instr,parameters,crh_frm,bpm_frm));
832
833 /* Save PRE */
834 check ( resFrame = xsh_pre_save (medPre, finalName, tag,0 ));
835
836 assure (resFrame != NULL, cpl_error_get_code (), "Cant save PRE");
837 xsh_msg_dbg_high ("PRE frame saved as file %s", finalName);
838
839 check(cpl_frame_set_level (resFrame, CPL_FRAME_LEVEL_FINAL));
840 check(cpl_frame_set_tag (resFrame, tag));
841
842 cleanup:
843 if (cpl_error_get_code() != CPL_ERROR_NONE){
844 xsh_free_frame(&resFrame);
845 }
846 cpl_free(finalName);
847 xsh_pre_free(&medPre);
848 return resFrame;
849}
850
851
852/*---------------------------------------------------------------------------*/
859/*---------------------------------------------------------------------------*/
860static void
861reject_lo_hi(cpl_image *image, double min, double max)
862{
863 cpl_mask *mask_lo = NULL;
864 cpl_mask *mask_hi = NULL;
865
866 mask_lo = cpl_mask_threshold_image_create(image, -DBL_MAX, min);
867 mask_hi = cpl_mask_threshold_image_create(image, max, DBL_MAX);
868 assure_mem( mask_lo );
869 assure_mem( mask_hi );
870
871 cpl_mask_or(mask_lo, mask_hi);
872
873 cpl_image_reject_from_mask(image, mask_lo);
874
875 cleanup:
876 xsh_free_mask(&mask_lo);
877 xsh_free_mask(&mask_hi);
878 return;
879}
880
881static int
882count_good(const cpl_image *image)
883{
884 return
885 cpl_image_get_size_x(image) * cpl_image_get_size_y(image) -
886 cpl_image_count_rejected(image);
887}
888
889static double
891{
892
893 cpl_image* avg_row=NULL;
894 double min=0;
895 double max=0;
896 double struct_row=0;
897
898
899
900 check(avg_row = cpl_image_collapse_create(tima,0));
901 check(cpl_image_divide_scalar(avg_row,cpl_image_get_size_y(tima)));
902
903 /* restricts statistics to +/- 2 ADU around mean */
904 min = cpl_image_get_mean(avg_row) - 2;
905 max = cpl_image_get_mean(avg_row) + 2;
906
907 /* replace with MIDAS
908 stat/ima avg_row + exc={min},{max};
909 */
910 check( reject_lo_hi(avg_row, min, max) );
911 if (count_good(avg_row) >= 2)
912 {
913 check(struct_row = cpl_image_get_stdev(avg_row));
914 }
915 else
916 {
917 struct_row = -1;
918 xsh_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
919 count_good(avg_row));
920 }
921
922 cleanup:
923 xsh_free_image(&avg_row);
924
925
926 return struct_row;
927
928}
929
930
931
932
933static double
935{
936
937
938 cpl_image* avg_col=NULL;
939 double min=0;
940 double max=0;
941 double struct_col=0;
942
943 check(avg_col = cpl_image_collapse_create(tima,1));
944 check(cpl_image_divide_scalar(avg_col,cpl_image_get_size_x(tima)));
945
946 /* restricts statistics to +/- 2 ADU around mean */
947 min = cpl_image_get_mean(avg_col) - 2;
948 max = cpl_image_get_mean(avg_col) + 2;
949
950 /* replace with MIDAS
951 stat/ima avg_col + exc={min},{max};
952 */
953 check( reject_lo_hi(avg_col, min, max) );
954 if (count_good(avg_col) >= 2)
955 {
956 check(struct_col = cpl_image_get_stdev(avg_col));
957 }
958 else
959 {
960 struct_col = -1;
961 xsh_msg_warning("Only %d good pixels in image. Setting QC parameter to -1",
962 count_good(avg_col));
963 }
964
965 cleanup:
966 xsh_free_image(&avg_col);
967
968
969 return struct_col;
970
971}
972
973
989static void
991 const int llx,
992 const int lly,
993 const int urx,
994 const int ury,
995 const int ref_x,
996 const int ref_y,
997 const int reg_id)
998{
999 /* UVES algorithm */
1000 cpl_image* xima=NULL;
1001 cpl_image* yima=NULL;
1002 double struct_row=0;
1003 double struct_col=0;
1004
1005 double mean = 0.0;
1006 double median = 0.0;
1007 double stdev = 0.0;
1008
1009 double structx = 0.0;
1010 double structy = 0.0;
1011 //int nb_images = 0;
1012
1013 check( xsh_pre_median_mean_stdev_window( master, &mean, &median,&stdev,
1014 llx,lly,urx,ury) ) ;
1015
1017 check(xsh_pfits_set_qc_mbiasmed (master->data_header, median));
1019
1020
1021
1022 if(reg_id==1){
1023 check(xima=cpl_image_extract(master->data,llx,lly,urx,ref_y));
1024 } else {
1025 check(xima=cpl_image_extract(master->data,llx,ref_y,urx,ury));
1026 }
1027 if (0) {
1028 /*
1029 replace/ima {mbia} {tmpfrm} 300,>=300.;
1030 */
1031 check(cpl_image_threshold(xima,
1032 -DBL_MAX,median+3*stdev,
1033 -DBL_MAX,median+3*stdev));
1034 }
1035
1036 if(reg_id==1){
1037 check(yima=cpl_image_extract(master->data,llx,lly,ref_x,ury));
1038 } else {
1039 check(yima=cpl_image_extract(master->data,ref_x,lly,urx,ury));
1040 }
1041 if (0) {
1042 /*
1043 replace/ima {mbia} {tmpfrm} 300,>=300.;
1044 */
1045 check(cpl_image_threshold(yima,
1046 -DBL_MAX,median+3*stdev,
1047 -DBL_MAX,median+3*stdev));
1048 }
1049
1051 structx=struct_row;
1052
1054 structy=struct_col;
1055
1056 /* end UVES based algorithm */
1057 if(reg_id==1) {
1060 } else {
1063 }
1064
1065 //check( nb_images = xsh_pfits_get_datancom( master->data_header));
1066 //AMo: this formula is inaccurate. RON is computed anyway elsewhere
1067 //check( xsh_pfits_set_qc_ron( master->data_header,
1068 // stdev*master->conad*sqrt(nb_images)));
1069
1070
1071 cleanup:
1072 xsh_free_image(&xima);
1073 xsh_free_image(&yima);
1074 return;
1075
1076}
1077
1092static void
1094 const int llx,
1095 const int lly,
1096 const int urx,
1097 const int ury,
1098 const int ref_x,
1099 const int ref_y,
1100 const int reg_id)
1101{
1102
1103
1105 ref_x,ref_y,reg_id));
1106
1107 cleanup:
1108 return;
1109
1110}
1111
1112
1120static void
1121set_masterbias_qc_structure(xsh_pre * master, cpl_parameterlist* parameters)
1122{
1123
1124 cpl_parameter* p=NULL;
1125 int ref_x=-1;
1126 int ref_y=-1;
1127
1128 int ref_llx=0;
1129 int ref_lly=0;
1130 int ref_urx=0;
1131 int ref_ury=0;
1132
1133 int sx=0;
1134 int sy=0;
1135 //int dlevel=0;
1136
1137 sx=cpl_image_get_size_x(master->data);
1138 sy=cpl_image_get_size_y(master->data);
1139 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.struct_refx"));
1140 check(ref_x = cpl_parameter_get_int(p));
1141
1142 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.struct_refy"));
1143 check(ref_y = cpl_parameter_get_int(p));
1144
1145 //check(dlevel=xsh_parameters_debug_level_get("xsh_mbias",parameters));
1146
1147 ref_llx=1;ref_urx=sx;
1148 ref_lly=1;ref_ury=sy;
1149
1150 if(ref_x == -1) {
1151 ref_x=sx/2;
1152 }
1153
1154 if(ref_y == -1) {
1155 ref_y=sy/2;
1156 }
1157
1158 ref_x = (ref_x>0) ? ref_x : 1;
1159 ref_y = (ref_y>0) ? ref_y : 1;
1160 ref_x = (ref_x<sx) ? ref_x : sx;
1161 ref_y = (ref_y<sy) ? ref_y : sy;
1162
1163 set_masterbias_qc_structure_region(master,ref_llx,ref_lly,ref_urx,ref_ury,
1164 ref_x,ref_y,1);
1165
1166
1167 set_masterbias_qc_structure_region(master,ref_llx,ref_lly,ref_urx,ref_ury,
1168 ref_x,ref_y,2);
1169
1170
1171 cleanup:
1172 return;
1173
1174}
1175
1176/*----------------------------------------------------------------------------*/
1177
1178
1179/*----------------------------------------------------------------------------*/
1180
1190cpl_frame*
1191xsh_create_master_bias2(cpl_frameset* rawFrames,xsh_stack_param* stack_par,
1192 xsh_instrument* instr,const char* result_tag,
1193 const int method_code)
1194{
1195 cpl_frame* medFrame = NULL;
1196 cpl_imagelist *dataList = NULL;
1197 cpl_imagelist *errsList = NULL;
1198 cpl_image* qual_comb=NULL;
1199 int i=0;
1200 int size=0;
1201 xsh_pre* pre = NULL;
1202 cpl_frame *current = NULL;
1203 char result_name[256];
1204 const int mode_and=0;
1205
1206 XSH_ASSURE_NOT_NULL( instr);
1207 XSH_ASSURE_NOT_NULL( rawFrames);
1208 check( size = cpl_frameset_get_size( rawFrames));
1209
1211
1212 check( dataList = cpl_imagelist_new());
1213 check( errsList = cpl_imagelist_new());
1214 /* Populate image lists with images in frame set */
1215 for (i=0;i < size;i++){
1216 check(current = cpl_frameset_get_frame(rawFrames,i));
1217 /* Load pre file */
1218 check(pre = xsh_pre_load(current,instr));
1219
1220 /* inject bad pixels to change statistics */
1221 xsh_image_flag_bp(pre->data,pre->qual,instr);
1222 xsh_image_flag_bp(pre->errs,pre->qual,instr);
1223 check_msg( cpl_imagelist_set(dataList,cpl_image_duplicate(pre->data),
1224 i),"Cant add Data Image %d to imagelist", i) ;
1225 check_msg( cpl_imagelist_set(errsList,cpl_image_duplicate(pre->errs),
1226 i),"Cant add Data Image %d to imagelist", i) ;
1227
1228 /* combining bad pixels */
1229 if(i==0) {
1230 qual_comb=cpl_image_duplicate(pre->qual);
1231 } else {
1232 xsh_badpixelmap_image_coadd(&qual_comb, pre->qual,mode_and);
1233 }
1234 /* Free memory */
1235 if (i < (size-1)) {
1236 xsh_pre_free(&pre);
1237 }
1238 }
1239
1240 /*free memory on product extensions that will be re-allocated later on
1241 (errs is only modified) */
1242 xsh_free_image(&pre->data);
1243 xsh_free_image(&pre->qual);
1244
1245 /* store combined bp map in qual extension of result */
1246 pre->qual=cpl_image_duplicate(qual_comb);
1247
1248 xsh_free_image(&qual_comb);
1249
1250 /* Verify uniformity of imagelists */
1251 assure (cpl_imagelist_is_uniform (dataList) == 0, CPL_ERROR_ILLEGAL_INPUT,
1252 "Data images are not uniform");
1253 assure (cpl_imagelist_is_uniform (errsList) == 0, CPL_ERROR_ILLEGAL_INPUT,
1254 "Errs images are not uniform");
1255
1256 /* computes the median of data and errs */
1257 if(method_code==0) {
1258
1259 pre->data=xsh_irplib_mkmaster_median(dataList,5.,5,1.e-5);
1260 check( xsh_collapse_errs(pre->errs,errsList,0));
1261
1262 } else if(method_code==1) {
1263 check(pre->data=xsh_irplib_mkmaster_mean(dataList,5.,5,1.e-5,
1264 stack_par->klow,
1265 stack_par->khigh));
1266 //Making a clean mean instead of a median
1267 check( xsh_collapse_errs(pre->errs,errsList,1));
1268
1269 }
1270
1271 /* Add PRO KW */
1273
1274 /* Now save the relevant frame of PRE
1275 With: median image, errs image and bad pixel map
1276 */
1277 sprintf(result_name,"%s.fits",result_tag);
1278 check(medFrame = xsh_pre_save( pre, result_name, result_tag,0));
1279
1280 check(cpl_frame_set_tag(medFrame, result_tag));
1281
1282
1283 cleanup:
1284
1285 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1286 xsh_free_frame(&medFrame);
1287 }
1288 xsh_free_imagelist(&dataList);
1289
1290 xsh_pre_free(&pre);
1291 xsh_free_imagelist(&errsList) ;
1292 xsh_free_image(&qual_comb);
1293
1294
1295 return medFrame;
1296
1297}
1298
1318cpl_frame *
1320 cpl_frame * frame,
1321 xsh_instrument* instr,
1322 cpl_parameterlist* params)
1323{
1324 xsh_pre *master = NULL;
1325 cpl_frame *masterFrame = NULL;
1326 const char* tag = XSH_GET_TAG_FROM_ARM( XSH_MASTER_BIAS, instr);
1327 char mastername[256];
1328 //int binx=0;
1329 //int biny=0;
1330 int nraws =0;
1331
1332 XSH_ASSURE_NOT_NULL(frame);
1333 check(master = xsh_pre_load (frame,instr));
1334 /* set the PROCATG KW */
1335 check(xsh_pfits_set_pcatg (master->data_header, tag));
1336
1337 //check(binx=xsh_pfits_get_binx(master->data_header));
1338 //check(biny=xsh_pfits_get_biny(master->data_header));
1339
1340 if ( params != NULL ) {
1341 check( set_masterbias_qc_structure(master,params));
1342 if(cpl_frameset_get_size(raws) > 1 ) {
1343 check(xsh_compute_ron_mbias(raws,master,params));
1344 check(xsh_compute_fpn_mbias(raws,master,instr,params));
1345 }
1346 }
1347
1348 sprintf(mastername,"MASTER_BIAS_%s.fits",
1350
1351 nraws = cpl_frameset_get_size(raws);
1352 xsh_msg("nraws=%d",nraws);
1353 xsh_count_satpix(master, instr, nraws);
1354
1355 xsh_msg ("Create master bias");
1356
1357 check(masterFrame = xsh_pre_save (master, mastername,tag,0 ));
1358 cpl_frame_set_tag (masterFrame, tag);
1359 cpl_frame_set_group(masterFrame,CPL_FRAME_GROUP_CALIB);
1360
1361 cleanup:
1362 xsh_pre_free(&master);
1363 return masterFrame;
1364}
1365
1366cpl_frame *
1367xsh_create_master_flat_with_mask (cpl_frame *frame, cpl_frame* edges, xsh_instrument *instr)
1368{
1369 xsh_pre *master = NULL;
1370 cpl_frame *masterFrame = NULL;
1371 const char *tag = NULL;
1372 char *name = NULL;
1373 xsh_order_list *orderlist = NULL;
1374 int nx = 0;
1375 int ny = 0;
1376 int ord=0;
1377 int y=0;
1378 int x=0;
1379 int x1=0;
1380 int x2=0;
1381 int y1=0;
1382 int y2=0;
1383 int endy=0;
1384 int starty=0;
1385 //int* qual=NULL;
1386 //int decode_bp=0;
1387 cpl_image* ima=NULL;
1388 //float* pima=NULL;
1389 //float* pdata=NULL;
1390 //cpl_mask* mask=NULL;
1391 double mean=0;
1392 int hbox=20;
1393
1394 // Check parameters
1395 XSH_ASSURE_NOT_NULL( frame);
1396 XSH_ASSURE_NOT_NULL( edges);
1397 XSH_ASSURE_NOT_NULL( instr);
1398 if(xsh_instrument_get_lamp(instr) == XSH_LAMP_D2) {
1399 ord=22;
1400 ord=2;
1401 } else if(xsh_instrument_get_lamp(instr) == XSH_LAMP_QTH) {
1402 ord=16;
1403 ord=4;
1404 } else if (xsh_instrument_get_arm(instr) == XSH_ARM_VIS ) {
1405 ord=24;
1406 ord=6;
1407 } else if (xsh_instrument_get_arm(instr) == XSH_ARM_NIR ) {
1408 ord=18;
1409 ord=8;
1410 }
1411 //decode_bp=instr->decode_bp;
1412 check( master = xsh_pre_load( frame, instr));
1413 check( orderlist = xsh_order_list_load( edges, instr));
1414
1415 nx = master->nx;
1416 ny = master->ny;
1417 //qual=cpl_image_get_data_int(xsh_pre_get_qual(master));
1418 ima=cpl_image_new(nx,ny,CPL_TYPE_FLOAT);
1419 xsh_image_flag_bp(ima,master->qual,instr);
1420 //mask=cpl_image_get_bpm( ima);
1421 //check(pima=cpl_image_get_data_float(ima));
1422 //check(pdata=cpl_image_get_data_float(master->data));
1423 xsh_msg("prepare mask");
1424
1425 starty = orderlist->list[ord-1].starty;
1426 endy = orderlist->list[ord-1].endy;
1427
1428 y=0.5*(starty+endy)/master->biny;
1429 check(x1 = xsh_order_list_eval(orderlist, orderlist->list[ord].edglopoly,y));
1430 check(x2 = xsh_order_list_eval(orderlist, orderlist->list[ord].edguppoly, y));
1431 x=0.5*(x1+x2);
1432 x1=x-hbox;
1433 x2=x+hbox;
1434 y1=y-hbox;
1435 y2=y+hbox;
1436 //xsh_msg("x1=%d x2=%d y1=%d y2=%d",x1,x2,y1,y2);
1437
1438 check(mean=cpl_image_get_median_window(master->data,x1,y1,x2,y2));
1439 xsh_msg("scaling factor mean flat=%g",mean);
1440 XSH_NAME_LAMP_MODE_ARM( name, "MASK_FLAT",".fits",instr);
1441 //cpl_image_save(ima,name, XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1442 xsh_free_image(&ima);
1443
1444 // set the PROCATG KW
1446 check( xsh_pfits_set_pcatg( master->data_header, tag));
1447
1448 //check( xsh_pre_normalize( master));
1449 cpl_image_divide_scalar( master->data, mean);
1450 cpl_image_divide_scalar( master->errs, mean);
1451
1452 XSH_FREE( name);
1453 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT",".fits",instr);
1454 xsh_msg ("Create master flat %s tag %s", name, tag);
1455
1457
1458
1459 check( masterFrame = xsh_pre_save( master, name, tag,0));
1460 check( cpl_frame_set_tag( masterFrame, tag));
1461 check( cpl_frame_set_group( masterFrame, CPL_FRAME_GROUP_CALIB));
1462
1463 cleanup:
1464 xsh_order_list_free(&orderlist);
1465 XSH_FREE( name);
1466 xsh_pre_free( &master);
1467 return masterFrame;
1468}
1469
1470
1471cpl_frame *
1472xsh_create_master_flat (cpl_frame *frame, xsh_instrument *instr)
1473{
1474 xsh_pre *master = NULL;
1475 cpl_frame *masterFrame = NULL;
1476 const char *tag = NULL;
1477 char *name = NULL;
1478
1479 /* Check parameters */
1480 XSH_ASSURE_NOT_NULL( frame);
1481 XSH_ASSURE_NOT_NULL( instr);
1482
1483 check( master = xsh_pre_load( frame, instr));
1484
1485 /* set the PROCATG KW */
1487 check( xsh_pfits_set_pcatg( master->data_header, tag));
1488
1489 check( xsh_pre_normalize( master));
1490
1491 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT",".fits",instr);
1492 xsh_msg ("Create master flat %s tag %s", name, tag);
1493
1494 check( masterFrame = xsh_pre_save( master, name, tag,0));
1495 check( cpl_frame_set_tag( masterFrame, tag));
1496 check( cpl_frame_set_group( masterFrame, CPL_FRAME_GROUP_CALIB));
1497
1498 cleanup:
1499 XSH_FREE( name);
1500 xsh_pre_free( &master);
1501 return masterFrame;
1502}
1503
1504
1505cpl_frame *
1506xsh_create_master_dark_bpmap (cpl_frame * mdarkFrame, xsh_instrument* instr)
1507{
1508 /*
1509 Load frame into PRE
1510 Create Frame with only the bpmap (qual from xsh_pre)
1511 Save and returns pointer to frame
1512 */
1513 xsh_pre * mdark_pre = NULL ;
1514 cpl_frame* resFrame = NULL;
1515 const char* tag = XSH_GET_TAG_FROM_ARM( XSH_MASTER_BP_MAP_DARK, instr);
1516 char* finalName = NULL;
1517 int i=0, j=0;
1518 int* qual_data = NULL;
1519 int nx,ny;
1520 int nb_flag_pixels = 0;
1521 int error_pixels[32];
1522
1523 /* check parameters input */
1524 XSH_ASSURE_NOT_NULL( mdarkFrame);
1525 XSH_ASSURE_NOT_NULL( instr);
1526
1527 /* load data */
1528 check( mdark_pre = xsh_pre_load( mdarkFrame, instr));
1529
1530 if ( xsh_instrument_get_arm(instr) == XSH_ARM_NIR ) {
1531 check(finalName=cpl_sprintf("%s_%s.fits",XSH_MASTER_BP_MAP_DARK,
1533
1534 } else {
1535 /* Sabine doesn't like the binning in the filename
1536 check(binx=xsh_pfits_get_binx(mdark_pre->data_header));
1537 check(biny=xsh_pfits_get_biny(mdark_pre->data_header));
1538 check(finalName=cpl_sprintf("MASTER_BP_MAP_%s_%dx%d.fits",
1539 xsh_instrument_arm_tostring(instr),binx,biny));
1540 */
1541 check(finalName=cpl_sprintf("%s_%s.fits",XSH_MASTER_BP_MAP_DARK,
1543
1544 }
1545
1546 check(resFrame=xsh_frame_product(finalName,tag,
1547 CPL_FRAME_TYPE_IMAGE,
1548 CPL_FRAME_GROUP_PRODUCT,
1549 CPL_FRAME_LEVEL_FINAL));
1550
1551 /* Compute some QC KW */
1552 check( qual_data = cpl_image_get_data_int( mdark_pre->qual));
1553 check( nx = cpl_image_get_size_x( mdark_pre->qual));
1554 check( ny = cpl_image_get_size_y( mdark_pre->qual));
1555
1556 /* count error in BP_MAP */
1557 for(j=0; j<32; j++){
1558 error_pixels[j] = 0;
1559 }
1560
1561 for(i=0; i<nx*ny; i++){
1562 if( qual_data[i] > 0){
1563 nb_flag_pixels++;
1564 for( j=0;j<32;j++){
1565 int cmp = pow(2,j);
1566 if ( qual_data[i] & cmp){
1567 error_pixels[j]++;
1568 }
1569 }
1570 }
1571 }
1572
1573
1574 /* Write QC */
1575 check( xsh_pfits_set_qc_bp_map_ntotal(mdark_pre->qual_header,nb_flag_pixels));
1576
1577 for(i=1; i< 33; i++){
1579 (void *)&(error_pixels[i-1]), XSH_QC_BP_MAP_NFLAGi, instr, i));
1580 }
1581 /* Save qual image by itself */
1582
1583 check_msg (cpl_image_save (mdark_pre->qual, finalName, XSH_PRE_DATA_BPP,
1584 mdark_pre->qual_header, CPL_IO_DEFAULT),
1585 "Could not save bpmap to %s extension", finalName);
1586
1587
1588 cleanup:
1589 if (cpl_error_get_code() != CPL_ERROR_NONE){
1590 xsh_free_frame(&resFrame);
1591 }
1592 XSH_FREE(finalName);
1593 xsh_pre_free( &mdark_pre);
1594 return resFrame;
1595}
1596
1597static cpl_error_code
1598xsh_compute_ron_mbias(cpl_frameset* raws,
1599 xsh_pre* master,
1600 cpl_parameterlist* parameters)
1601{
1602
1603 //const char* ron_method="ALL";
1604 int random_sizex=-1;
1605 int random_nsamples=-1;
1606 double ron=0;
1607 double ron_err=0;
1608
1609
1610 int ref_llx=-1;
1611 int ref_lly=-1;
1612 int ref_urx=-1;
1613 int ref_ury=-1;
1614
1615
1616 int ref2_llx=-1;
1617 int ref2_lly=-1;
1618 int ref2_urx=-1;
1619 int ref2_ury=-1;
1620
1621
1622 cpl_parameter* p=NULL;
1623
1624 const cpl_frame * fr = NULL;
1625
1626 cpl_propertylist * plist = NULL;
1627
1628 int naxis1 = 0;
1629 int naxis2 = 0;
1630/*
1631 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ron_method"));
1632 check(ron_method = cpl_parameter_get_string(p));
1633*/
1634
1635 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.random_sizex"));
1636 check(random_sizex = cpl_parameter_get_int(p));
1637
1638 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.random_nsamples"));
1639 check(random_nsamples = cpl_parameter_get_int(p));
1640
1641 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_llx"));
1642 check(ref_llx = cpl_parameter_get_int(p));
1643
1644 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_lly"));
1645 check(ref_lly = cpl_parameter_get_int(p));
1646
1647 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_urx"));
1648 check(ref_urx = cpl_parameter_get_int(p));
1649
1650 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref1_urx"));
1651 check(ref_ury = cpl_parameter_get_int(p));
1652
1653 check(fr=cpl_frameset_get_frame_const(raws,0));
1654
1655 check(plist=cpl_propertylist_load(cpl_frame_get_filename(fr),0));
1656 check(naxis1 = cpl_propertylist_get_int(plist, "NAXIS1"));
1657 check(naxis2 = cpl_propertylist_get_int(plist, "NAXIS2"));
1658
1659
1660 cpl_propertylist_delete(plist);
1661
1662 //checking for defaults
1663 if(ref_llx == -1) ref_llx = naxis1 / 8;
1664 if(ref_lly == -1) ref_lly = naxis2 / 8;
1665 if(ref_urx == -1) ref_urx = naxis1 * 7 / 8;
1666 if(ref_ury == -1) ref_ury = naxis2 * 7 / 8;
1667
1668 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_llx>0,"Must be ref_llx > 0");
1669 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_lly>0,"Must be ref_lly > 0");
1670 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_urx>0,"Must be ref_urx > 0");
1671 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_ury>0,"Must be ref_ury > 0");
1672
1673 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_llx<naxis1,"Must be ref_llx < frame X size");
1674 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_lly<naxis2,"Must be ref_lly < frame Y size");
1675 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_urx<=naxis1,"Must be ref_urx > frame X size");
1676 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_ury<=naxis2,"Must be ref_ury > frame Y size");
1677
1678 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_urx>ref_llx,"Must be ref_urx > ref_llx");
1679 XSH_ASSURE_NOT_ILLEGAL_MSG(ref_ury>ref_lly,"Must be ref_ury > ref_lly");
1680
1681 XSH_ASSURE_NOT_ILLEGAL_MSG(random_nsamples>0,"Must be random_nsamples > 0");
1682 XSH_ASSURE_NOT_ILLEGAL_MSG(random_nsamples<100000000,"Must be random_nsamples < 100000000");
1683 XSH_ASSURE_NOT_ILLEGAL_MSG(random_sizex>0,"Must be random_sizex > 0");
1684 XSH_ASSURE_NOT_ILLEGAL_MSG(random_sizex<naxis1,"Must be random_sizex < frame X size");
1685 XSH_ASSURE_NOT_ILLEGAL_MSG(random_sizex<naxis2,"Must be random_sizex < frame Y size");
1686
1687 check(xsh_compute_ron(raws,ref_llx,ref_lly,ref_urx,ref_ury,random_nsamples,random_sizex,1,&ron,&ron_err));
1688 xsh_pfits_set_qc_ron1(master->data_header,ron);
1689 xsh_pfits_set_qc_ron1_err(master->data_header,ron_err);
1690
1691 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_llx"));
1692 check(ref2_llx = cpl_parameter_get_int(p));
1693
1694 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_lly"));
1695 check(ref2_lly = cpl_parameter_get_int(p));
1696
1697 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_urx"));
1698 check(ref2_urx = cpl_parameter_get_int(p));
1699
1700 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.ref2_urx"));
1701 check(ref2_ury = cpl_parameter_get_int(p));
1702
1703
1704 //checking for defaults
1705 if(ref2_llx == -1) ref2_llx = naxis1 / 8;
1706 if(ref2_lly == -1) ref2_lly = naxis2 / 8;
1707 if(ref2_urx == -1) ref2_urx = naxis1 * 7 / 8;
1708 if(ref2_ury == -1) ref2_ury = naxis2 * 7 / 8;
1709
1710
1711 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_llx>0,"Must be ref2_llx > 0");
1712 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_lly>0,"Must be ref2_lly > 0");
1713 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_urx>0,"Must be ref2_urx > 0");
1714 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_ury>0,"Must be ref2_ury > 0");
1715
1716 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_llx<naxis1,"Must be ref2_llx < frame X size");
1717 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_lly<naxis2,"Must be ref2_lly < frame Y size");
1718
1719
1720 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_urx<=naxis1,"Must be ref2_urx <= frame X size");
1721 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_ury<=naxis2,"Must be ref2_ury <= frame Y size");
1722
1723 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_urx>ref2_llx,"Must be ref2_urx > ref2_llx");
1724 XSH_ASSURE_NOT_ILLEGAL_MSG(ref2_ury>ref2_lly,"Must be ref2_ury > ref2_lly");
1725
1726 if(
1727 (ref2_llx != ref_llx) ||
1728 (ref2_lly != ref_lly) ||
1729 (ref2_urx != ref_urx) ||
1730 (ref2_ury != ref_ury)
1731 ) {
1732
1733 check(xsh_compute_ron(raws,ref2_llx,ref2_lly,ref2_urx,ref2_ury,random_nsamples,random_sizex,2,&ron,&ron_err));
1734
1735 xsh_pfits_set_qc_ron2(master->data_header,ron);
1736 xsh_pfits_set_qc_ron2_err(master->data_header,ron_err);
1737
1738 }
1739
1740
1741 cleanup:
1742
1743 return cpl_error_get_code();
1744
1745}
1746
1747
1748static cpl_error_code
1749xsh_compute_fpn_mbias(cpl_frameset* raws,
1750 xsh_pre* master,
1751 xsh_instrument* instrument,
1752 cpl_parameterlist* parameters)
1753{
1754
1755 int nraws=0;
1756 double ron=0;
1757 double fpn=0;
1758 cpl_frame* frm1=NULL;
1759 cpl_frame* frm2=NULL;
1760 cpl_image* ima1=NULL;
1761 cpl_image* ima2=NULL;
1762 cpl_image* ima=NULL;
1763
1764
1765 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
1766 nraws=cpl_frameset_get_size(raws);
1767 if(nraws > 1) {
1768 frm1=cpl_frameset_get_frame(raws,0);
1769 frm2=cpl_frameset_get_frame(raws,1);
1770 ima1=cpl_image_load(cpl_frame_get_filename(frm1),CPL_TYPE_FLOAT,0,0);
1771 ima2=cpl_image_load(cpl_frame_get_filename(frm2),CPL_TYPE_FLOAT,0,0);
1772
1773 ima = cpl_image_duplicate(ima1);
1774 check(ron = xsh_image_get_stdev_clean(ima, NULL) / sqrt(2.0));
1775
1776 check(fpn = xsh_fixed_pattern_noise_bias(ima1,ima2,ron));
1777
1778 xsh_pfits_set_qc_ron_master(master->data_header,ron);
1779 xsh_pfits_set_qc_fpn_master(master->data_header,fpn);
1780
1781 xsh_free_image(&ima);
1782 xsh_free_image(&ima1);
1783 xsh_free_image(&ima2);
1784 }
1785 } else {
1786 check(xsh_mbias_get_fpn(master->data,parameters,&fpn));
1787 /* xsh_msg("Fixed Pattern Noise=%f",fpn); */
1788
1789 }
1790
1791 cleanup:
1792 xsh_free_image(&ima);
1793 xsh_free_image(&ima1);
1794 xsh_free_image(&ima2);
1795 return cpl_error_get_code();
1796
1797}
1798
1799
1800
1801static cpl_error_code
1802xsh_mbias_get_fpn(const cpl_image* ima,
1803 cpl_parameterlist* parameters,
1804 double* fpn)
1805{
1806
1807 int fpn_llx=0;
1808 int fpn_lly=0;
1809 int fpn_urx=0;
1810 int fpn_ury=0;
1811 int fpn_hsize=0;
1812 int fpn_nsamp=0;
1813 cpl_parameter* p=NULL;
1814
1815 cpl_size zone[4];
1816 int sx=0;
1817 int sy=0;
1818
1819 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_llx"));
1820 check(fpn_llx = cpl_parameter_get_int(p));
1821
1822 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_lly"));
1823 check(fpn_lly = cpl_parameter_get_int(p));
1824
1825 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_urx"));
1826 check(fpn_urx = cpl_parameter_get_int(p));
1827
1828 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_ury"));
1829 check(fpn_ury = cpl_parameter_get_int(p));
1830
1831 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_hsize"));
1832 check(fpn_hsize = cpl_parameter_get_int(p));
1833
1834 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_mbias.fpn_nsamples"));
1835 check(fpn_nsamp = cpl_parameter_get_int(p));
1836
1837 sx=cpl_image_get_size_x(ima);
1838 sy=cpl_image_get_size_y(ima);
1839
1840 fpn_llx=(fpn_llx>0) ? fpn_llx: 0;
1841 fpn_lly=(fpn_lly>0) ? fpn_lly: 0;
1842
1843 fpn_urx=(fpn_urx<sx) ? fpn_urx: sx;
1844 fpn_ury=(fpn_ury<sy) ? fpn_ury: sy;
1845
1846
1847 zone[0]=fpn_llx;
1848 zone[1]=fpn_urx;
1849 zone[2]=fpn_lly;
1850 zone[3]=fpn_ury;
1851
1852
1853 check_msg(cpl_flux_get_noise_window(ima,zone,fpn_hsize,fpn_nsamp,fpn,NULL),
1854 "Error computing noise in a window");
1855
1856 cleanup:
1857 return cpl_error_get_code();
1858
1859}
1860
1861
1862
1863
1864
1886static cpl_image *
1887xsh_flat_create_normalized_master(cpl_imagelist * flats,
1888 const cpl_table *ordertable,
1889 xsh_order_list* order_locations,
1890 xsh_stack_param* stack_param)
1891{
1892 int ni;
1893 cpl_image *image=NULL;
1894 cpl_image* master_flat=NULL;
1895 cpl_imagelist* flats_norm=NULL;
1896 int k=0;
1897 int ord_min=0;
1898 int ord_max=0;
1899 int nord=0;
1900 double flux_mean=0;
1901 int nsam=10;
1902 int y_space=10;
1903 int hbox_sx=10;
1904 int hbox_sy=0;
1905 int is=0;
1906 int pos_x=0;
1907 int pos_y=0;
1908 int llx=0;
1909 int lly=0;
1910 int urx=0;
1911 int ury=0;
1912
1913 double x=0;
1914 double y=0;
1915 int sx=0;
1916 int sy=0;
1917 cpl_vector* vec_flux_ord=NULL;
1918 cpl_vector* vec_flux_sam=NULL;
1919 double* pvec_flux_ord=NULL;
1920 double* pvec_flux_sam=NULL;
1921 double tolerance=1.e-5;
1922 //int absord=0;
1923 int ord=0;
1924
1925 XSH_ASSURE_NOT_NULL_MSG(order_locations, "Null input order locations polinomial!");
1926 XSH_ASSURE_NOT_NULL_MSG(flats, "Null input flats imagelist!");
1927
1928 ni = cpl_imagelist_get_size(flats);
1929
1930 image = cpl_image_duplicate(cpl_imagelist_get(flats, 0));
1931 sx = cpl_image_get_size_x(image);
1932 sy = cpl_image_get_size_y(image);
1933
1934 xsh_free_image(&image);
1935 ord_min=cpl_table_get_column_min(ordertable,"ORDER");
1936 ord_max=cpl_table_get_column_max(ordertable,"ORDER");
1937 nord=ord_max-ord_min+1;
1938 vec_flux_ord=cpl_vector_new(nord);
1939 vec_flux_sam=cpl_vector_new(nsam);
1940 pvec_flux_ord=cpl_vector_get_data(vec_flux_ord);
1941 pvec_flux_sam=cpl_vector_get_data(vec_flux_sam);
1942 hbox_sy=(int)((sy-2*y_space)/(2*nsam)+0.5);
1943 flats_norm=cpl_imagelist_new();
1944 int starty=0;
1945 int endy=0;
1946 for(k=0;k<ni;k++) { /* loop over input images */
1947 xsh_free_image(&image);
1948 image = cpl_image_duplicate(cpl_imagelist_get(flats, k));
1949 for(ord=0;ord<nord;ord++) {
1950 //absord=ord+ord_min;
1951 endy=order_locations->list[ord].endy;
1952 starty=order_locations->list[ord].starty;
1953 pos_y=starty-hbox_sy;
1954 hbox_sy=(int)((endy-starty+1-2*y_space)/(2*nsam)+0.5);
1955 for(is=0;is<nsam;is++) {
1956 /* sample flux on nsam rectangular boxes distributed uniformly over the order center traces
1957 * we use a median to be robust to bad pixels */
1958 pos_y+=(2*hbox_sy+y_space);
1959 y=(int)(pos_y+0.5);
1960
1961
1962 check( x=cpl_polynomial_eval_1d(order_locations->list[ord].cenpoly,
1963 (double)y,NULL));
1964
1965 pos_x=(int)(x+0.5);
1966
1967 llx=xsh_max_int(pos_x-hbox_sx,1);
1968 lly=xsh_max_int(pos_y-hbox_sy,1);
1969 llx=xsh_min_int(llx,sx);
1970 lly=xsh_min_int(lly,sy);
1971
1972 urx=xsh_min_int(pos_x+hbox_sx,sx);
1973 ury=xsh_min_int(pos_y+hbox_sy,sy);
1974 urx=xsh_max_int(urx,1);
1975 ury=xsh_max_int(ury,1);
1976
1977 llx=xsh_min_int(llx,urx);
1978 lly=xsh_min_int(lly,ury);
1979 //xsh_msg("ord=%d llx=%d lly=%d urx=%d ury=%d",ord,llx,lly,urx,ury);
1980 check(pvec_flux_sam[is]=cpl_image_get_median_window(image,llx,lly,urx,ury));
1981
1982 }
1983 /* compute mean sampled values for each order */
1984 pvec_flux_ord[ord]=cpl_vector_get_mean(vec_flux_sam);
1985 }
1986 /* compute mean of mean values measured on all orders. Normalizes each flat image to it */
1987 flux_mean=cpl_vector_get_mean(vec_flux_ord);
1988 xsh_msg("Flat %d normalize factor inter1: %g",k,flux_mean);
1989 cpl_image_divide_scalar(image,flux_mean);
1990 cpl_imagelist_set(flats_norm,cpl_image_duplicate(image),k);
1991 }
1992
1993 /* the first master flat is the median of all means */
1994 if (strcmp(stack_param->stack_method, "median") == 0) {
1995 master_flat=xsh_imagelist_collapse_median_create(flats_norm);
1996 } else {
1997 master_flat=cpl_imagelist_collapse_sigclip_create(flats_norm,stack_param->klow,stack_param->khigh,tolerance,CPL_COLLAPSE_MEAN,NULL);
1998 }
1999
2000
2001 cleanup:
2002
2003 xsh_free_vector(&vec_flux_ord);
2004 xsh_free_vector(&vec_flux_sam);
2005 xsh_free_image(&image);
2006 xsh_free_imagelist(&flats_norm);
2007
2008
2009 return master_flat;
2010
2011}
2012
2013
2014
2035static cpl_image *
2036xsh_flat_create_normalized_master2(cpl_imagelist * flats,
2037 const cpl_table *ordertable, xsh_order_list* order_locations,
2038 const cpl_image* mflat,xsh_stack_param* stack_param,cpl_vector** vec_flux_stack) {
2039
2040 cpl_imagelist* flats_norm = NULL;
2041 cpl_imagelist* flats_norm2 = NULL;
2042 cpl_image* master_flat = NULL;
2043
2044 cpl_image* flat = NULL;
2045 cpl_image* flat_mflat = NULL;
2046
2047
2048 cpl_vector* vec_flux = NULL;
2049 double* pvec_flux = NULL;
2050
2051 double* pvec_flux_stack = NULL;
2052
2053 int ni = 0;
2054 int i = 0;
2055 int sx = 0;
2056 int sy = 0;
2057 int ord_min = 0;
2058 int ord_max = 0;
2059 int nord = 0;
2060 int nsam = 10;
2061 int y_space = 10;
2062 int llx = 0;
2063 int lly = 0;
2064 int urx = 0;
2065 int ury = 0;
2066 int hbox_sx = 0;
2067 int hbox_sy = 0;
2068 int ord = 0;
2069 //int absord = 0;
2070 int pos_x = 0;
2071 int pos_y = 0;
2072 double x = 0;
2073 double y = 0;
2074 double flux_median = 0;
2075 double mean_explevel=0;
2076 /* double exptime=0; */
2077 int is = 0;
2078 int k = 0;
2079
2080 int starty=0;
2081 int endy=0;
2082 double tolerance=1.e-5;
2083
2084 cpl_mask* mask;
2085 int nflagged=0;
2086
2087 ni = cpl_imagelist_get_size(flats);
2088
2089 /* evaluate median on many windows distributed all over orders of flats */
2090 sx = cpl_image_get_size_x(mflat);
2091 sy = cpl_image_get_size_y(mflat);
2092
2093 ord_min = cpl_table_get_column_min(ordertable, "ORDER");
2094 ord_max = cpl_table_get_column_max(ordertable, "ORDER");
2095 nord = ord_max - ord_min + 1;
2096
2097 hbox_sy = (int) ((sy - 2 * y_space) / (2 * nsam) + 0.5);
2098 flats_norm = cpl_imagelist_new();
2099 *vec_flux_stack = cpl_vector_new(ni);
2100 pvec_flux_stack=cpl_vector_get_data(*vec_flux_stack);
2101
2102 for (i = 0; i < ni; i++) {
2103 xsh_free_vector(&vec_flux);
2104 vec_flux = cpl_vector_new(nord * nsam);
2105 pvec_flux = cpl_vector_get_data(vec_flux);
2106
2107
2108
2109
2110
2111 check(flat = cpl_image_duplicate(cpl_imagelist_get(flats, i)));
2112 /* normalize flats by master flat previously computed */
2113 flat_mflat = cpl_image_duplicate(flat);
2114 cpl_image_divide(flat_mflat, mflat);
2115 mask = cpl_image_get_bpm(flat_mflat);
2116 k = 0;
2117 for (ord = 0; ord < nord; ord++) {
2118 //absord = ord + ord_min;
2119
2120 endy=order_locations->list[ord].endy;
2121 starty=order_locations->list[ord].starty;
2122 pos_y=starty-hbox_sy;
2123 hbox_sy=(int)((endy-starty+1-2*y_space)/(2*nsam)+0.5);
2124
2125 for (is = 0; is < nsam; is++) {
2126 /* sample flux on nsam rectangular boxes distributed uniformly over the order center traces
2127 * we use a median to be robust to bad pixels */
2128
2129 pos_y += (2 * hbox_sy + y_space);
2130 y = (int) (pos_y + 0.5);
2131
2132 check(
2133 x=cpl_polynomial_eval_1d(order_locations->list[ord].cenpoly, (double)y,NULL));
2134
2135 pos_x = (int) (x + 0.5);
2136
2137 check(llx=xsh_max_int(pos_x-hbox_sx,1));
2138 check(lly=xsh_max_int(pos_y-hbox_sy,1));
2139 check(llx=xsh_min_int(llx,sx));
2140 check(lly=xsh_min_int(lly,sy));
2141
2142 check(urx=xsh_min_int(pos_x+hbox_sx,sx));
2143 check(ury=xsh_min_int(pos_y+hbox_sy,sy));
2144 check(urx=xsh_max_int(urx,1));
2145 check(ury=xsh_max_int(ury,1));
2146
2147 check(llx=xsh_min_int(llx,urx));
2148 check(lly=xsh_min_int(lly,ury));
2149
2150 check(pvec_flux[k]=0);
2151 nflagged=cpl_mask_count_window(mask,llx,lly,urx,ury);
2152 if(nflagged < (urx-llx+1)*(ury-lly+1) ) {
2153 check(pvec_flux[k]=cpl_image_get_median_window(flat_mflat,llx,lly,urx,ury));
2154 }
2155 k++;
2156 }
2157
2158 }
2159 /* compute median of median values measured on all orders. Normalizes each flat image to it */
2160 flux_median = cpl_vector_get_median(vec_flux);
2161 pvec_flux_stack[i]= flux_median;
2162 xsh_msg("Flat %d level iter2: %g", i, flux_median);
2163 //xsh_msg("Flat %d level ingested: %g", i, cpl_vector_get(*vec_flux_stack,i));
2164 //cpl_image_divide_scalar(flat, flux_median);
2165 cpl_imagelist_set(flats_norm, cpl_image_duplicate(flat), i);
2166 //xsh_msg("mean flat1=%g",cpl_image_get_median(flat));
2167 mean_explevel+=flux_median;
2168
2169
2170 xsh_free_image(&flat_mflat);
2171 xsh_free_image(&flat);
2172
2173 }
2174
2175 mean_explevel /= ni;
2176 //xsh_msg("mean_exp level: %g",mean_explevel);
2177
2178 cpl_vector_divide_scalar(*vec_flux_stack, mean_explevel);
2179 pvec_flux_stack = cpl_vector_get_data(*vec_flux_stack);
2180 flats_norm2 = cpl_imagelist_new();
2181 for (i = 0; i < ni; i++) {
2182 flat = cpl_imagelist_get(flats_norm, i);
2183 cpl_image_multiply_scalar(flat, 1./pvec_flux_stack[i]);
2184 //xsh_msg("inv factor=%g",pvec_flux_stack[i]);
2185 //xsh_msg("factor=%g",1./pvec_flux_stack[i]);
2186 //xsh_msg("mean flat2=%g",cpl_image_get_median(flat));
2187 cpl_imagelist_set(flats_norm2, cpl_image_duplicate(flat), i);
2188 }
2189
2190 if (strcmp(stack_param->stack_method, "median") == 0) {
2191 master_flat=xsh_imagelist_collapse_median_create(flats_norm2);
2192 } else {
2193 master_flat=cpl_imagelist_collapse_sigclip_create(flats_norm2,stack_param->klow,stack_param->khigh,tolerance,CPL_COLLAPSE_MEAN,NULL);
2194 }
2195
2196 cleanup:
2197
2198 xsh_free_imagelist(&flats_norm);
2199 xsh_free_imagelist(&flats_norm2);
2200 xsh_free_vector(&vec_flux);
2201 xsh_free_image(&flat_mflat);
2202 //xsh_free_image(&flat);
2203
2204 return master_flat;
2205
2206}
2207
2208/*----------------------------------------------------------------------------*/
2246/*----------------------------------------------------------------------------*/
2247cpl_frame *
2248xsh_create_master_flat2(cpl_frameset *set,
2249 cpl_frame * order_tab_cen,
2250 xsh_stack_param* stack_par,
2251 xsh_instrument* inst)
2252
2253{
2254 cpl_imagelist *dataList = NULL;
2255 cpl_imagelist *errsList = NULL;
2256 cpl_imagelist *errsList2 = NULL;
2257 //cpl_imagelist *raw_images_local = NULL;
2258
2259 cpl_image* qual_comb = NULL;
2260 cpl_image* ima=NULL;
2261
2262 cpl_image *master_flat = NULL;
2263 cpl_image *master_flat_tmp = NULL;
2264 cpl_image *current_flat = NULL;
2265
2266 cpl_table* ordertable = NULL;
2267
2268 cpl_vector* vec_flux=NULL;
2269
2270 cpl_frame *current = NULL;
2271
2272 cpl_frame* mflat = NULL;
2273
2274 xsh_pre* pre = NULL;
2275 xsh_order_list *order_centres_list = NULL;
2276
2277 double* pvec_flux=NULL;
2278 const char* fname = NULL;
2279
2280 int i = 0;
2281 int size = 0;
2282
2283 const int mode_and = 0;
2284 char mflat_name[256];
2285 char mflat_tag[256];
2286
2287
2288 /* is input valid? */
2289 XSH_ASSURE_NOT_NULL( inst);
2290 XSH_ASSURE_NOT_NULL( set);
2291 check( size = cpl_frameset_get_size( set));
2292 XSH_ASSURE_NOT_ILLEGAL( size > 0);
2293
2294 fname = cpl_frame_get_filename(order_tab_cen);
2295 ordertable = cpl_table_load(fname, 1, 0);
2296 order_centres_list = xsh_order_list_load(order_tab_cen, inst);
2297
2298 check( dataList = cpl_imagelist_new());
2299 check( errsList = cpl_imagelist_new());
2300
2301
2302
2303 /* Populate image lists with images in frame set */
2304 for (i = 0; i < size; i++) {
2305 check(current = cpl_frameset_get_frame(set,i));
2306 //cpl_msg_info(__func__,"Looking at file %s with tag %s\n",cpl_frame_get_filename(current),cpl_frame_get_tag(current));
2307 /* Load pre file */
2308 check(pre = xsh_pre_load(current,inst));
2309
2310 /* Get number of saturated pixels and update qc_numsat_max if needed */
2311/*
2312 double* idata = NULL;
2313 current_flat = cpl_image_duplicate(pre->data);
2314 idata = cpl_image_get_data(current_flat);
2315 int nsat = 0;
2316 cpl_msg_info(__func__,"image: pre->nx %d pre->ny %d nx %d ny %d idata %d\n",pre->nx,pre->ny,cpl_image_get_size_x(pre->data),cpl_image_get_size_y(pre->data),(idata == NULL));
2317 for(int j=0;j<pre->nx*pre->ny;j++){
2318 if(idata[j] > thresh){
2319 nsat++;
2320 }
2321 }
2322 if(nsat > qc_numsat_max){
2323 qc_numsat_max = nsat;
2324 }
2325 xsh_free_image(&current_flat);
2326*/
2327 /* inject bad pixels to change statistics */
2328 xsh_image_flag_bp(pre->data, pre->qual, inst);
2329 xsh_image_flag_bp(pre->errs, pre->qual, inst);
2330 check_msg( cpl_imagelist_set(dataList,cpl_image_duplicate(pre->data),
2331 i),"Cant add Data Image %d to imagelist", i);
2332 check_msg( cpl_imagelist_set(errsList,cpl_image_duplicate(pre->errs),
2333 i),"Cant add Data Image %d to imagelist", i);
2334
2335 /* combining bad pixels */
2336 if (i == 0) {
2337 qual_comb = cpl_image_duplicate(pre->qual);
2338 } else {
2339 xsh_badpixelmap_image_coadd(&qual_comb, pre->qual, mode_and);
2340 }
2341 /* Free memory */
2342 if (i < (size - 1)) {
2343 xsh_pre_free(&pre);
2344 }
2345 }
2346
2347 /*free memory on product extensions that will be re-allocated later on (errs is only modified) */
2348 xsh_free_image(&pre->data);
2349 xsh_free_image(&pre->qual);
2350
2351 /* store combined bp map in qual extension of result */
2352 pre->qual = cpl_image_duplicate(qual_comb);
2353 xsh_free_image(&qual_comb);
2354
2355 /* Create initial master flat */
2356 xsh_msg("Calculating stack normalized master");
2357 check_msg(
2358 master_flat_tmp = xsh_flat_create_normalized_master(dataList, ordertable, order_centres_list,stack_par),
2359 "Error computing master flat with normalization");
2360 //check(cpl_image_save(master_flat_tmp,"master_flat_tmp.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT));
2361
2362
2363 /* Create final master flat */
2364
2365 //xsh_free_image(&(pre->data));
2366 check_msg(
2367 pre->data = xsh_flat_create_normalized_master2(dataList, ordertable, order_centres_list, master_flat_tmp,
2368 stack_par,&vec_flux),"Error computing master flat with normalization");
2369 //check(cpl_image_save(pre->data,"master_flat_final.fits",CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT));
2370
2371 xsh_free_image(&master_flat_tmp);
2372 check(pvec_flux=cpl_vector_get_data(vec_flux));
2373 check( errsList2 = cpl_imagelist_new());
2374
2375 for (i = 0; i < size; i++) {
2376 check(ima=cpl_imagelist_get(errsList,i));
2377 check(cpl_image_multiply_scalar(ima,pvec_flux[i]));
2378 check(cpl_imagelist_set(errsList2,cpl_image_duplicate(ima),i));
2379 }
2380
2381 /* Create initial master flat err */
2382 if (strcmp(stack_par->stack_method, "median") == 0) {
2383 check( xsh_collapse_errs(pre->errs,errsList2,0));
2384 } else {
2385 check( xsh_collapse_errs(pre->errs,errsList2,1));
2386 }
2387
2388 /*clean memory and reset local pointers to null */
2389 //raw_images_local = NULL;
2390 if (xsh_instrument_get_arm(inst) == XSH_ARM_UVB) {
2391 sprintf(mflat_tag, "%s_%s_%s", XSH_MASTER_FLAT,
2392 xsh_instrument_lamp_tostring(inst), xsh_instrument_arm_tostring(inst));
2393 } else {
2394 sprintf(mflat_tag, "%s_%s", XSH_MASTER_FLAT,xsh_instrument_arm_tostring(inst));
2395 }
2396 sprintf(mflat_name,"%s.fits",mflat_tag);
2397 xsh_count_satpix(pre, inst,size);
2398 check(mflat = xsh_pre_save( pre,mflat_name,mflat_tag,1));
2399 check(cpl_frame_set_tag(mflat,mflat_tag));
2400
2401 cleanup:
2402 xsh_free_imagelist(&dataList);
2403 xsh_free_imagelist(&errsList);
2404 xsh_free_imagelist(&errsList2);
2405 xsh_free_table(&ordertable);
2406 xsh_free_image(&current_flat);
2407 xsh_order_list_free(&order_centres_list);
2408 xsh_pre_free(&pre);
2409 xsh_free_vector(&vec_flux);
2410
2411 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2412 xsh_free_image(&master_flat);
2413 }
2414
2415 return mflat;
2416}
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
static void set_masterbias_qc_structure(xsh_pre *master, cpl_parameterlist *parameters)
static void set_masterdark_qc(xsh_pre *preFrame, xsh_instrument *instrument, cpl_parameterlist *parameters, cpl_frame *crh_frm, cpl_frame *bpm_frm)
static cpl_error_code xsh_mdark_compute_fpn(cpl_frameset *raws, cpl_parameterlist *params, cpl_propertylist *qclog, xsh_instrument *instrument)
static void set_masterbias_qc_xsh_structure_region(xsh_pre *master, const int llx, const int lly, const int urx, const int ury, const int ref_x, const int ref_y, const int reg_id)
cpl_frame * xsh_compute_qc_on_master_bias(cpl_frameset *raws, cpl_frame *frame, xsh_instrument *instr, cpl_parameterlist *params)
Computes QC on a master bias frame.
static int count_good(const cpl_image *image)
cpl_frame * xsh_create_master_flat_with_mask(cpl_frame *frame, cpl_frame *edges, xsh_instrument *instr)
cpl_error_code xsh_mdark_measure_ron(xsh_pre *preFrame, cpl_parameterlist *parameters)
static cpl_error_code xsh_mdark_get_ron(xsh_pre *pre, cpl_parameterlist *params, cpl_propertylist *qc_log, xsh_instrument *instr)
static double get_masterbias_qc_structure_col_region(cpl_image *tima)
static cpl_error_code xsh_compute_fpn_mbias(cpl_frameset *raws, xsh_pre *master, xsh_instrument *instrument, cpl_parameterlist *parameters)
static cpl_error_code xsh_mdark_measure_fpn(xsh_pre *preFrame, cpl_parameterlist *parameters, xsh_instrument *instrument)
static cpl_error_code xsh_mbias_get_fpn(const cpl_image *ima, cpl_parameterlist *parameters, double *fpn)
static void reject_lo_hi(cpl_image *image, double min, double max)
Reject outlier pixels.
static void set_masterbias_qc_structure_region(xsh_pre *master, const int llx, const int lly, const int urx, const int ury, const int ref_x, const int ref_y, const int reg_id)
static double xsh_mdark_get_contam(xsh_pre *pre, cpl_parameterlist *params, xsh_instrument *instr)
static cpl_error_code xsh_compute_ron_mbias(cpl_frameset *raws, xsh_pre *master, cpl_parameterlist *parameters)
cpl_frame * xsh_create_master_flat(cpl_frame *frame, xsh_instrument *instr)
static double get_masterbias_qc_structure_row_region(cpl_image *tima)
cpl_error_code xsh_mdark_get_median_stdev(xsh_pre *preFrame, cpl_parameterlist *parameters, cpl_frame *crh_frm, cpl_frame *bpm_frm)
cpl_frame * xsh_create_master_bias2(cpl_frameset *rawFrames, xsh_stack_param *stack_par, xsh_instrument *instr, const char *result_tag, const int method_code)
Creates master bias.
cpl_frame * xsh_create_master_dark(cpl_frame *medFrame, xsh_instrument *instr, cpl_parameterlist *parameters, cpl_frame *crh_frm, cpl_frame *bpm_frm)
cpl_frame * xsh_create_master_dark_bpmap(cpl_frame *mdarkFrame, xsh_instrument *instr)
cpl_frame * xsh_create_master_dark2(cpl_frameset *raws, xsh_stack_param *stack_param, cpl_parameterlist *params, cpl_propertylist *qc_log, xsh_instrument *instr)
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.
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 exptime
static int starty
static int endy
static xsh_instrument * instrument
void xsh_image_flag_bp(cpl_image *image, cpl_image *bpmap, xsh_instrument *inst)
cpl_error_code xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right, const int mode)
cpl_error_code xsh_count_satpix(xsh_pre *pre, xsh_instrument *instr, const int datancom)
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
double xsh_order_list_eval(xsh_order_list *list, cpl_polynomial *poly, double y)
Evaluate an order list poly.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
Definition: xsh_data_pre.c:953
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
int xsh_pre_get_ny(const xsh_pre *pre)
Get ny of pre structure.
void xsh_pre_normalize(xsh_pre *self)
Normalize data flux.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
int xsh_pre_get_nx(const xsh_pre *pre)
Get nx of pre structure.
void xsh_pre_subtract(xsh_pre *self, const xsh_pre *right)
Subtract one PRE image from another The data units are subtracted data = data1 - data2 The error imag...
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
void xsh_pre_median_mean_stdev_window(xsh_pre *preFrame, double *mean, double *median, double *stdev, const int llx, const int lly, const int urx, const int ury)
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
Definition: xsh_error.h:103
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define assure_mem(PTR)
Definition: xsh_error.h:79
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
Definition: xsh_error.h:111
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_LAMP xsh_instrument_get_lamp(xsh_instrument *i)
Get a lamp on instrument structure.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
int size
int * y
int * x
static SimAnneal s
Definition: xsh_model_sa.c:99
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
void xsh_pfits_set_datancom(cpl_propertylist *plist, int value)
Definition: xsh_pfits.c:1249
void xsh_pfits_set_exptime(cpl_propertylist *plist, double value)
Write the EXPTIME value.
Definition: xsh_pfits.c:2292
void xsh_pfits_set_qc_mbiasrms(cpl_propertylist *plist, double value)
Write the QC.MBIASRMS value.
Definition: xsh_pfits_qc.c:686
void xsh_pfits_set_qc_mdarkmed(cpl_propertylist *plist, double value)
Write the QC.MDARKMED value.
Definition: xsh_pfits_qc.c:822
void xsh_pfits_set_qc_mbiasmed(cpl_propertylist *plist, double value)
Write the QC.MBIASMED value.
Definition: xsh_pfits_qc.c:668
void xsh_pfits_set_qc_norm_fpn(cpl_propertylist *plist, double value)
Write the QC.NORMFPN value.
Definition: xsh_pfits_qc.c:357
void xsh_pfits_set_qc_multi(cpl_propertylist *plist, void *value, const char *kw, xsh_instrument *instrument, int idx)
void xsh_pfits_set_qc_ron_err(cpl_propertylist *plist, double value)
Definition: xsh_pfits_qc.c:488
void xsh_pfits_set_qc_mbiasavg(cpl_propertylist *plist, double value)
Write the QC.MBIASAVG value.
Definition: xsh_pfits_qc.c:651
void xsh_pfits_set_qc_contamination(cpl_propertylist *plist, double value)
Write the contamination value.
Definition: xsh_pfits_qc.c:913
void xsh_pfits_set_qc_reg1_structy(cpl_propertylist *plist, double value)
Write the QC.STRUCTY value.
Definition: xsh_pfits_qc.c:409
void xsh_pfits_set_qc_bp_map_ntotal(cpl_propertylist *plist, int value)
Write the contamination value.
Definition: xsh_pfits_qc.c:894
void xsh_pfits_set_qc_fpn_err(cpl_propertylist *plist, double value)
Write the QC.FPN value.
Definition: xsh_pfits_qc.c:339
void xsh_pfits_set_qc_mdarkavg(cpl_propertylist *plist, double value)
Write the QC.MDARKAVG value.
Definition: xsh_pfits_qc.c:933
void xsh_pfits_set_qc_ron(cpl_propertylist *plist, double value)
Definition: xsh_pfits_qc.c:466
void xsh_pfits_set_qc_fpn(cpl_propertylist *plist, double value)
Write the QC.FPN value.
Definition: xsh_pfits_qc.c:321
void xsh_pfits_set_qc_mdarkrms(cpl_propertylist *plist, double value)
Write the QC.MDARKRMS value.
Definition: xsh_pfits_qc.c:859
void xsh_pfits_set_qc_norm_fpn_err(cpl_propertylist *plist, double value)
Write the QC.NORMFPN.ERR value.
Definition: xsh_pfits_qc.c:374
void xsh_pfits_set_qc_reg2_structx(cpl_propertylist *plist, double value)
Write the QC.STRUCTX value.
Definition: xsh_pfits_qc.c:428
void xsh_pfits_set_qc_reg1_structx(cpl_propertylist *plist, double value)
Write the QC.STRUCTX value.
Definition: xsh_pfits_qc.c:392
void xsh_pfits_set_qc_slit_width(cpl_propertylist *source_list, cpl_propertylist *set_list, xsh_instrument *instrument)
void xsh_pfits_set_qc_reg2_structy(cpl_propertylist *plist, double value)
Write the QC.STRUCTY value.
Definition: xsh_pfits_qc.c:445
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_mask(cpl_mask **m)
Deallocate an image mask and set the pointer to NULL.
Definition: xsh_utils.c:2149
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
Definition: xsh_utils.c:2164
xsh_order * list
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_image * qual
Definition: xsh_data_pre.h:71
float exptime
Definition: xsh_data_pre.h:92
cpl_propertylist * qual_header
Definition: xsh_data_pre.h:72
cpl_image * data
Definition: xsh_data_pre.h:65
cpl_propertylist * errs_header
Definition: xsh_data_pre.h:69
cpl_propertylist * data_header
Definition: xsh_data_pre.h:66
int biny
Definition: xsh_data_pre.h:80
int binx
Definition: xsh_data_pre.h:80
cpl_image * errs
Definition: xsh_data_pre.h:68
const char * stack_method
#define XSH_GOOD_PIXEL_LEVEL
@ XSH_LAMP_D2
@ XSH_LAMP_QTH
@ XSH_ARM_AGC
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
#define XSH_NAME_LAMP_MODE_ARM(name, id, ext, instr)
#define XSH_PRE_QUAL_TYPE
Definition: xsh_data_pre.h:46
#define XSH_PRE_DATA_BPP
Definition: xsh_data_pre.h:43
int nx
double kappa
Definition: xsh_detmon_lg.c:81
int lly
Definition: xsh_detmon_lg.c:86
int llx
Definition: xsh_detmon_lg.c:85
int n
Definition: xsh_detmon_lg.c:92
int urx
Definition: xsh_detmon_lg.c:87
double tolerance
int ury
Definition: xsh_detmon_lg.c:88
int ny
int order
Definition: xsh_detmon_lg.c:80
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
#define XSH_MASTER_FLAT
Definition: xsh_dfs.h:569
#define XSH_MASTER_BIAS
Definition: xsh_dfs.h:549
#define XSH_MASTER_DARK
Definition: xsh_dfs.h:550
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_MASTER_BP_MAP_DARK
Definition: xsh_dfs.h:669
#define XSH_GET_TAG_FROM_LAMP(TAG, instr)
Definition: xsh_dfs.h:1608
#define max(a, b)
cpl_parameter * xsh_parameters_find(cpl_parameterlist *list, const char *recipe_id, const char *name)
find a parameter
int xsh_parameters_get_int(const cpl_parameterlist *list, const char *recipe_id, const char *name)
#define XSH_QC_BP_MAP_NFLAGi
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
cpl_error_code xsh_collapse_errs(cpl_image *errs, cpl_imagelist *list, const int mode)