X-shooter Pipeline Reference Manual 3.8.15
xsh_data_pre.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-09-30 12:14:02 $
23 * $Revision: 1.213 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
41/*-----------------------------------------------------------------------------
42 Includes
43 ----------------------------------------------------------------------------*/
44#include <string.h>
45#include <stdio.h>
46#include <xsh_data_pre.h>
47#include <xsh_irplib_mkmaster.h>
48#include <xsh_dump.h>
49#include <xsh_utils.h>
50#include <xsh_error.h>
51#include <xsh_msg.h>
52#include <xsh_pfits.h>
53#include <xsh_dfs.h>
54#include <math.h>
55#include <time.h>
56#include <cpl.h>
57
58/*----------------------------------------------------------------------------
59 Function implementation
60 ----------------------------------------------------------------------------*/
61static cpl_error_code xsh_preoverscan_corr(cpl_frame* raw, const int corr_mode,
62 double* cor_val) {
63
64 const char* fname = NULL;
65 cpl_image* raw_ima = NULL;
66 cpl_image* tmp_ima = NULL;
67
68 cpl_propertylist* header = NULL;
69 int ovscx = 0;
70 //int ovscy = 0;
71 int prscx = 0;
72 //int prscy = 0;
73 int naxis1 = 0;
74 int naxis2 = 0;
75 double tmp_val = 0;
76 cpl_vector* cor_val_vect=NULL;
77 cpl_imagelist* iml=NULL;
78 cpl_image* ima_ext=NULL;
79
80 //double* praw=NULL;
81 float* ptmp = NULL;
82 int i = 0;
83 int j = 0;
84 char cor_fname[256];
85
86 fname = cpl_frame_get_filename(raw);
87 sprintf(cor_fname, "PREOVER_COR_%s", xsh_get_basename(fname));
88 header = cpl_propertylist_load(fname, 0);
89
90 raw_ima = cpl_image_load(fname, XSH_PRE_DATA_TYPE, 0, 0);
91 prscx = xsh_pfits_get_prscx(header);
92 ovscx = xsh_pfits_get_ovscx(header);
93
94 naxis1 = xsh_pfits_get_naxis1(header);
95 naxis2 = xsh_pfits_get_naxis2(header);
96 tmp_ima = cpl_image_new(naxis1, naxis2, XSH_PRE_DATA_TYPE);
97 ptmp = cpl_image_get_data_float(tmp_ima);
98
99 switch (corr_mode) {
100
101
102 case 0: {
103 *cor_val = 0;
104 break;
105 }
106 case 1: {
107 /* constant using only overscan */
108
109 ima_ext = cpl_image_extract(raw_ima, naxis1 - ovscx, 1, naxis1, naxis2);
110 iml = cpl_imagelist_new();
111 cpl_imagelist_set(iml, ima_ext,0);
112 cor_val_vect = xsh_irplib_imagelist_get_clean_mean_levels(iml, 3, 5, 0.001);
113 xsh_free_imagelist(&iml);
114
115 *cor_val = cpl_vector_get(cor_val_vect, 0);
116 xsh_free_vector(&cor_val_vect);
117
118 break;
119 }
120 case 2: {
121 /* constant using only prescan */
122
123 ima_ext = cpl_image_extract(raw_ima, 1, 1, prscx, naxis2);
124 iml = cpl_imagelist_new();
125 cpl_imagelist_set(iml, ima_ext,0);
126 cor_val_vect = xsh_irplib_imagelist_get_clean_mean_levels(iml, 3, 5, 0.001);
127 xsh_free_imagelist(&iml);
128
129 *cor_val = cpl_vector_get(cor_val_vect, 0);
130 xsh_free_vector(&cor_val_vect);
131
132 break;
133 }
134 case 3: {
135 /* constant using both prescan and overscan */
136
137 /* prescan contribute */
138 ima_ext = cpl_image_extract(raw_ima, 1, 1, prscx, naxis2);
139 iml = cpl_imagelist_new();
140 cpl_imagelist_set(iml, ima_ext,0);
141 cor_val_vect = xsh_irplib_imagelist_get_clean_mean_levels(iml, 3, 5, 0.001);
142 xsh_free_imagelist(&iml);
143
144 *cor_val = cpl_vector_get(cor_val_vect, 0);
145 xsh_free_vector(&cor_val_vect);
146
147 /* overscan contribute */
148 ima_ext = cpl_image_extract(raw_ima, naxis1 - ovscx, 1, naxis1, naxis2);
149 iml = cpl_imagelist_new();
150 cpl_imagelist_set(iml, ima_ext,0);
151 cor_val_vect = xsh_irplib_imagelist_get_clean_mean_levels(iml, 3, 5, 0.001);
152 xsh_free_imagelist(&iml);
153
154 *cor_val += cpl_vector_get(cor_val_vect, 0);
155 xsh_free_vector(&cor_val_vect);
156
157 /* mean of two contributes */
158 *cor_val *= 0.5;
159
160 break;
161 }
162 case 4: {
163 /* image row by row using only overscan */
164 /*
165 xsh_msg("ok1 naxis1=%d ovscx=%d",naxis1,ovscx);
166 xsh_msg("raw_ima sx=%d sy=%d",
167 cpl_image_get_size_x(raw_ima),
168 cpl_image_get_size_y(raw_ima));
169 */
170 for (j = 0; j < naxis2; j++) {
171 tmp_val = cpl_image_get_median_window(raw_ima, naxis1 - ovscx, j + 1,
172 naxis1, j + 1);
173 for (i = 0; i < naxis1; i++) {
174 ptmp[j * naxis1 + i] = tmp_val;
175 }
176 }
177 break;
178 }
179 case 5: {
180 /* image row by row using only prescan */
181 for (j = 0; j < naxis2; j++) {
182 tmp_val = cpl_image_get_median_window(raw_ima, 1, j + 1,prscx, j + 1);
183 for (i = 0; i < naxis1; i++) {
184 ptmp[j * naxis1 + i] = tmp_val;
185 }
186 }
187 break;
188 }
189 case 6: {
190 /* image row by row using both prescan and overscan */
191
192 for (j = 0; j < naxis2; j++) {
193 /* prescan contribute */
194 tmp_val = cpl_image_get_median_window(raw_ima, 1, j + 1,prscx, j + 1);
195 /* overscan contribute */
196 tmp_val += cpl_image_get_median_window(raw_ima, naxis1 - ovscx, j + 1, naxis1, j + 1);
197
198 /* mean of two contributes */
199 tmp_val *= 0.5;
200 for (i = 0; i < naxis1; i++) {
201 ptmp[j * naxis1 + i] = tmp_val;
202 }
203 }
204 break;
205 }
206 default:
207 xsh_msg_error("Not supported");
208 break;
209 }
210
211 if (corr_mode < 4) {
212 cpl_image_subtract_scalar(raw_ima, *cor_val);
213 } else {
214 *cor_val = cpl_image_get_median_window(tmp_ima, 1, 1, naxis1, naxis2);
215 cpl_image_subtract(raw_ima, tmp_ima);
216 }
217
218 xsh_msg("Apply pre-overscan correction %g", *cor_val);
219
220 if (corr_mode > 0 && corr_mode < 4) {
221 if (fabs(*cor_val) < 1.e-6) {
223 "The overscan correction is < 10^-6. Probably you have bad input");
224 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
225 goto cleanup;
226 }
227 }
228
230 check(cpl_image_save( raw_ima, cor_fname, CPL_BPP_IEEE_FLOAT, header,
231 CPL_IO_DEFAULT));
232 cpl_frame_set_filename(raw, cor_fname);
233 //check(cpl_image_save(raw_ima,fname,CPL_BPP_IEEE_FLOAT,header,CPL_IO_DEFAULT));
234 xsh_add_temporary_file(cor_fname);
235 cleanup:
236
237 xsh_free_vector(&cor_val_vect);
238 xsh_free_imagelist(&iml);
239 xsh_free_image(&raw_ima);
240 xsh_free_image(&tmp_ima);
241 xsh_free_propertylist(&header);
242
243 return cpl_error_get_code();
244}
245
246
247double xsh_compute_ron_nir(const double dit)
248{
249 double ron=0;
250 const int nsamples=10;
251 int i=0;
252 double dit_min=0;
253 double dit_max=0;
254 double ron_min=0;
255 double ron_max=0;
256 double slope=0;
257 int i_min=0;
258
259
260 static double dit_samples[10]={2,4,8,16,32,64,128,256,400,1024};
261
262 //static double ron_samples[10]={25.5,27.2,27.5,25.1,20.0,14.8,12.5,8.8,8.5,8.0};
263 static double ron_samples[10]={21.3,18.4,14.8,11.7,9.0,7.3,6.3,6.2,7.0,9.0};
264
265 for(i=0;i<nsamples-1;i++){
266 if(dit_samples[i] < dit) i_min=i;
267 }
268
269 dit_min=dit_samples[i_min];
270 ron_min=ron_samples[i_min];
271
272 dit_max=dit_samples[i_min+1];
273 ron_max=ron_samples[i_min+1];
274 slope=(ron_max-ron_min)/(dit_max-dit_min);
275
276 ron=ron_min+slope*(dit-dit_min);
277
278 return ron;
279
280}
281/*---------------------------------------------------------------------------*/
317/*---------------------------------------------------------------------------*/
318static void xsh_pre_init(xsh_pre* pre, cpl_propertylist* header,
320{
321 //XSH_ARM arm;
322 double dit;
323 /* check parameters input */
325 XSH_ASSURE_NOT_NULL( header);
327
328 /* set header to pre */
329 pre->data_header = header;
330
331 /* put arm */
332 //arm = xsh_pfits_get_arm( pre->data_header);
333 if ( cpl_error_get_code() != CPL_ERROR_NONE){
334 xsh_msg_warning("No ARM keyword found put default");
337 }
339
340 /* must be read */
343 if(cpl_propertylist_has(pre->data_header,"EXPTIME")) {
345 }
348 /*Temporarily hard-coded as missing in NIR FITS header */
349 /*pre->ron=10; this should come from a curve: 22 (DIT=2s)- 6 (DIT=100s) */
350 pre->ron=xsh_compute_ron_nir(dit);
351 //xsh_msg("pre->ron=%g",pre->ron);
352 pre->conad=2.12;
353 pre->gain=1./pre->conad;
354 }else {
358 }
360 //int chip_ny;
361
362 /* NO binning in NIR */
363 pre->binx = 1;
364 pre->biny = 1;
365
367 pre->data_header));
368
369 /* convert from meter to microns */
370 pre->pszx = pre->pszx * 1000000 ;
371 pre->pszy = pre->pszx;
372
373 //check( chip_ny = xsh_pfits_get_chip_ny (pre->data_header));
375 //AModigliani
376 //PLUTO
377 //pre->cuty = chip_ny / 2; //why this? Cut out half detector
378
379 // Paolo would like this:
382#if 0
383 pre->cutmy = (chip_ny / 2) - XSH_NIR_DEFAULT_CUT_Y ; //why this? Cut out half detector
384#else
385 /* Dont cut half detector, it's already cut in the raw frame */
387#endif
388
389 }
390 else {
391 pre->binx = xsh_pfits_get_binx( pre->data_header);
392 pre->biny = xsh_pfits_get_biny( pre->data_header);
393 if ( cpl_error_get_code() != CPL_ERROR_NONE){
394 xsh_msg("WARNING : No binning keywords found. Force binning to 1x1");
395 pre->binx = 1;
396 pre->biny = 1;
398 }
399
402 /* compute size of a image pixel */
403 pre->pszx = pre->pszx * pre->binx;
404 pre->pszy = pre->pszy * pre->biny;
405
406 /* could be read */
407 /*
408 ATENTION ! ATTENTION !
409 With real data from VLT, pre and over scan should NOT be divided
410 by binning
411 */
417 }
418 /* Add binning into instrument->config for later use */
419 instrument->binx = pre->binx ;
420 instrument->biny = pre->biny ;
421
422 cleanup:
423
424 return;
425}
426
427/*---------------------------------------------------------------------------*/
449/*---------------------------------------------------------------------------*/
450xsh_pre* xsh_pre_create(cpl_frame* raw, cpl_frame* bpmap, cpl_image* bias_data,
451 xsh_instrument* instr, const int pre_overscan_corr,
452 const bool flag_neg_and_thresh_pix) {
453 xsh_pre* result = NULL;
454 const char* framename = NULL;
455 const char* frame_tag = NULL;
456 int nextensions = 0;
457 int mode_or=1;
458
459 /*
460 double median_le = 0.0, median_ri = 0.0, median_up = 0.0, median_do = 0.0;
461 double stdev_le = 0.0, stdev_ri = 0.0, stdev_up = 0.0, stdev_do = 0.0;
462 float* pdata = NULL;
463 */
464
465 cpl_image *image = NULL;
466 float* errs = NULL;
467 float* data = NULL;
468 int i = 0;
469 const char *bpfilename = NULL;
470 cpl_propertylist* bplist = NULL, *data_header = NULL;
471 int bpnaxis1 = 0, bpnaxis2 = 0;
472
473 int nsat = 0;
474 double frac_sat = 0;
475 double cor_val = 0;
476
477 /* check parameters input */
479 XSH_ASSURE_NOT_NULL(instr);
480
481 /* allocate memory */
482 XSH_CALLOC(result, xsh_pre, 1);
483
484 /* fill the structure */
485 result->instrument = instr;
486 result->decode_bp = instr->decode_bp;
487
489 check(xsh_pfits_set_extname(result->errs_header, "ERRS"));
491 check(xsh_pfits_set_extname(result->qual_header, "QUAL"));
492
493 /* analyse raw frame */
494 check(framename = cpl_frame_get_filename(raw));
495 check(result->group = cpl_frame_get_group(raw));
496 check(frame_tag = cpl_frame_get_tag(raw));
497
498 //XSH_ASSURE_NOT_MISMATCH(result->group == CPL_FRAME_GROUP_RAW);
499 check(nextensions = cpl_frame_get_nextensions(raw));
500 //xsh_msg("nextensions=%d",nextensions);
501
502 XSH_ASSURE_NOT_ILLEGAL(nextensions == 0);
503
504 /* load the property list */
505 check_msg(data_header = cpl_propertylist_load(framename, 0),
506 "Could not load header of file '%s'", framename);
507
508 /* we start data manimpulation here */
509 if (pre_overscan_corr > 0) {
510 check(xsh_preoverscan_corr(raw, pre_overscan_corr, &cor_val));
511 check(framename = cpl_frame_get_filename(raw));
512 }
513
514 check(xsh_pre_init(result, data_header, instr));
515 /* special init for AFC frame */
516 if (strstr(frame_tag, "AFC") != NULL) {
518 if (xsh_instrument_get_arm(instr) != XSH_ARM_NIR) {
519 result->cutmx = 0;
520 result->cutmy = 0;
521 result->cutx = 0;
522 result->cuty = 0;
523 }
524 }
525
526 /* compute image size */
527 result->nx = result->naxis1 - result->cutmx - result->cutx;
528 result->ny = result->naxis2 - result->cutmy - result->cuty;
529
530 /* in NIR mode */
531 int is_flat = 0;
532 if(cpl_propertylist_has(result->data_header, XSH_DPR_TYPE)) {
533 is_flat = xsh_pfits_is_flat(result->data_header);
534 }
535
536 if (xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
537
538 XSH_ASSURE_NOT_ILLEGAL(result->nx > 0 && result->ny > 0);
539
540 /* load image */
541 check(image = cpl_image_load(framename, XSH_PRE_DATA_TYPE, 0, 0));
542
543 /* compute data part */
544
545 /* slice it in two equal part and apply a padding of XSH_NIR_DEFAULT_CUT
546 on top,left and right borders */
547
548 xsh_msg_dbg_medium("extracting region [%d,%d:%d,%d]", result->cutx + 1,
549 result->cuty + 1, result->naxis1 - XSH_NIR_DEFAULT_CUT,
550 result->naxis2 - result->cutmy - XSH_NIR_DEFAULT_CUT);
551
552 check(
553 result->data = cpl_image_extract(image, result->cutx + 1,
554 result->cuty + 1, result->naxis1 - XSH_NIR_DEFAULT_CUT,
555 result->naxis2 - result->cutmy - XSH_NIR_DEFAULT_CUT));
556 /* rotate 90° clockwise the result */
557 check(cpl_image_turn(result->data, 1));
558 result->nx = cpl_image_get_size_x(result->data);
559 result->ny = cpl_image_get_size_y(result->data);
560 xsh_msg_dbg_low("***** NX,NY: %d,%d", result->nx, result->ny);
561 /* compute ERRS part */
562 check(result->errs = cpl_image_duplicate(result->data));
563 check(errs = cpl_image_get_data_float(result->errs));
564 check(data = cpl_image_get_data_float(result->data));
565 double cdata = 0;
566
567 double ron2 = result->ron * result->ron;
568 int nx_ny = result->nx * result->ny;
569 double ron_div_conad = result->ron / result->conad;
570 double crpix1=1.;
571 double crpix2=1.;
572 double crval1=1.;
573 double crval2=1.;
574 double cdelt1=1.;
575 double cdelt2=1.;
576 for (i = 0; i < nx_ny; i++) {
577 /* convert in e- to calculate and return in ADU */
578 cdata = data[i];
579 if (cdata > 0) {
580 errs[i] = sqrt(cdata * result->conad + ron2) / result->conad;
581 } else {
582 errs[i] = ron_div_conad;
583 }
584 }
585
586 /* to flag bad pixel we need the qual extension initialized */
587 result->qual=cpl_image_new(result->nx,result->ny,XSH_PRE_QUAL_TYPE);
588 /* Flag negative saturated and (NIR) extrapolated pixels */
589
590 xsh_badpixelmap_flag_saturated_pixels(result, instr, cor_val,
591 flag_neg_and_thresh_pix, is_flat, &nsat);
592 frac_sat = (double) nsat / (nx_ny);
593
594 xsh_pfits_set_wcs(result->data_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
595 xsh_pfits_set_wcs(result->errs_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
596 xsh_pfits_set_wcs(result->qual_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
597 check(xsh_pfits_set_nsat(result->data_header, nsat));
598 check(xsh_pfits_set_frac_sat(result->data_header, frac_sat));
599
600 }
601 /* IN VIS and UVB mode */
602 else {
603 XSH_ASSURE_NOT_ILLEGAL(result->nx > 0 && result->ny > 0);
604 /* load image */
605 check(image = cpl_image_load(framename, XSH_PRE_DATA_TYPE, 0, 0));
606 /* TODO: the following computations seems to record overscan/prescan levels
607 * but are already done elsewhere and is not used later on.
608 * Should we remove this? We have commented out it!
609
610 // computes mean and median for 4 windows
611 if (result->cutx > 0) {
612 check(median_le = cpl_image_get_median_window(image, 1, 1,
613 result->cutx, result->naxis2));
614 check(stdev_le = cpl_image_get_stdev_window(image, 1, 1,
615 result->cutx, result->naxis2));
616 }
617 if (result->cuty > 0) {
618 check(median_do = cpl_image_get_median_window(image, 1, 1,
619 result->naxis1, result->cuty));
620 check(stdev_do = cpl_image_get_stdev_window(image, 1, 1,
621 result->naxis1, result->cuty));
622 }
623 if (result->cutmx > 0) {
624 check(median_ri =cpl_image_get_median_window(image,
625 result->naxis1 - result->cutmx + 1, 1, result->naxis1,
626 result->naxis2));
627 check(stdev_ri = cpl_image_get_stdev_window(image,
628 result->naxis1 - result->cutmx + 1, 1, result->naxis1,
629 result->naxis2));
630 }
631 if (result->cutmy > 0) {
632 check(median_up =cpl_image_get_median_window(image, 1,
633 result->naxis2 - result->cutmy + 1, result->naxis1, result->naxis2));
634 check(stdev_ri = cpl_image_get_stdev_window(image, 1,
635 result->naxis2 - result->cutmy + 1, result->naxis1, result->naxis2));
636 }
637
638 // update FITS header
639 check(xsh_pfits_set_bias_left_median(result->data_header, median_le));
640 check(xsh_pfits_set_bias_right_median(result->data_header, median_ri));
641 check(xsh_pfits_set_bias_left_stdev(result->data_header, stdev_le));
642 check(xsh_pfits_set_bias_right_stdev(result->data_header, stdev_ri));
643 check(xsh_pfits_set_bias_up_median(result->data_header, median_up));
644 check(xsh_pfits_set_bias_down_median(result->data_header, median_do));
645 check(xsh_pfits_set_bias_up_stdev(result->data_header, stdev_up));
646 check(xsh_pfits_set_bias_down_stdev(result->data_header, stdev_do));
647 */
648
649 /* compute data part */
650 check(
651 result->data = cpl_image_extract(image, result->cutx + 1,
652 result->cuty + 1, result->naxis1 - result->cutmx,
653 result->naxis2 - result->cutmy));
654
655 if (xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
656 /* rotate the image of 180 degrees */
657 check(cpl_image_turn(result->data, 2));
658 }
659
660 /* to flag bad pixel we need the qual extension initialized */
661 result->qual=cpl_image_new(result->nx,result->ny,XSH_PRE_QUAL_TYPE);
662
663 /* flag saturated pixels for QC */
664 check(xsh_badpixelmap_flag_saturated_pixels(result, instr, cor_val,
665 flag_neg_and_thresh_pix, is_flat, &nsat));
666 int nx_ny = result->nx * result->ny;
667 frac_sat = (double) nsat / (nx_ny);
668
669 check(xsh_pfits_set_nsat(result->data_header, nsat));
670 check(xsh_pfits_set_frac_sat(result->data_header, frac_sat));
671 if ( (xsh_instrument_get_arm(instr) == XSH_ARM_VIS) ) {
672 /*
673 && (strcmp(rec_id,"xsh_wavecal") == 0 ) ) {
674 */
675 check(xsh_badpixelmap_count_range_pixels(result, 4800,5200, cor_val,
676 &nsat,&frac_sat));
679
680 }
681 double crpix1=1.;
682 double crpix2=1.;
683 double crval1=1.;
684 double crval2=1.;
685 double cdelt1=xsh_instrument_get_binx(instr);;
686 double cdelt2=xsh_instrument_get_biny(instr);
687
688 xsh_pfits_set_wcs(result->data_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
689 xsh_pfits_set_wcs(result->errs_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
690 xsh_pfits_set_wcs(result->qual_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
691
692
693 /* compute ERRS part */
694 check(result->errs = cpl_image_duplicate(result->data));
695 check(errs = cpl_image_get_data_float(result->errs));
696 check(data = cpl_image_get_data_float(result->data));
697
698 /* in case of BIAS frame */
699 if ((strcmp(frame_tag, XSH_BIAS_UVB) == 0)
700 || (strcmp(frame_tag, XSH_BIAS_VIS) == 0)
701 || (strcmp(frame_tag, XSH_RAW_IMA_SLIT_UVB) == 0)
702 || (strcmp(frame_tag, XSH_RAW_IMA_SLIT_VIS) == 0)) {
703
704 /* RON (e-) / CONAD ==> ADU */
705 float ron_div_conad = result->ron / result->conad;
706
707 for (i = 0; i < nx_ny; i++) {
708 errs[i] = ron_div_conad;
709 }
710 }
711
712 /* in case of LINEARITY frame */
713 else if ((strcmp(frame_tag, XSH_LINEARITY_UVB) == 0)
714 || (strcmp(frame_tag, XSH_LINEARITY_VIS) == 0)
715 || (strcmp(frame_tag, XSH_RAW_BP_MAP_NL_UVB) == 0)
716 || (strcmp(frame_tag, XSH_RAW_BP_MAP_NL_VIS) == 0)
717 || (strcmp(frame_tag, XSH_BP_MAP_NL_UVB) == 0)
718 || (strcmp(frame_tag, XSH_BP_MAP_NL_VIS) == 0)) {
719
720 for (i = 0; i < nx_ny; i++) {
721 errs[i] = 0;
722 }
723 } else {
724 /* usual input data case */
725 int bias_x, bias_y;
726 float* mbias_data = NULL;
727 double ron2 = result->ron * result->ron;
728 double cdata = 0;
729 double ron_div_conad = result->ron / result->conad;
730
731 int flag_err_comp = 0;
732 if (bias_data != (cpl_image *)-1) {
733 /* if a master bias is provided */
734
735 xsh_msg("pre_overscan=%d",pre_overscan_corr);
736 if (pre_overscan_corr == 0) {
737 /* if no overscan correction */
738
739 check(bias_x = cpl_image_get_size_x(bias_data));
740 check(bias_y = cpl_image_get_size_y(bias_data));
742 "bias_x=%d nx=%d bias_y=%d ny=%d", bias_x, result->nx, bias_y, result->ny);
744 bias_x == result->nx,
745 "Master bias X size different from raw frame X size. Input image frames must match in arm and bin!");
747 bias_y == result->ny,
748 "Master bias Y size different from raw frame Y size. Input image frames must match in arm and bin!");
749
750 flag_err_comp = 1;
751 check( mbias_data = cpl_image_get_data_float( bias_data));
752 for (i = 0; i < nx_ny; i++) {
753 /* convert in e- to calculate and return in ADU */
754 cdata = data[i] - mbias_data[i];
755 if (cdata > 0) {
756 errs[i] = sqrt(cdata * result->conad + ron2) / result->conad;
757 } else {
758 errs[i] = ron_div_conad;
759 }
760 }
761
762 } /* end check on poverscan correction */
763 } /* end check on mbias input provision */
764
765 if (flag_err_comp == 0) {
766
767 for (i = 0; i < nx_ny; i++) {
768 /* convert in e- to calculate and return in ADU */
769 cdata = data[i];
770 if (cdata > 0) {
771 errs[i] = sqrt(cdata * result->conad + ron2) / result->conad;
772 } else {
773 errs[i] = ron_div_conad;
774 }
775 }
776
777 } /* end check on special case */
778
779 } /* end case usual input data */
780
781 } /* end check on arms */
782
783 /* combine with reference bad pixels if provided */
784 if (bpmap != NULL) {
785 check(bpfilename = cpl_frame_get_filename(bpmap));
786 xsh_msg("Use Bad pixel map file %s\n", bpfilename);
787 check(bplist = cpl_propertylist_load(bpfilename, 0));
788 /* read KW */
789 check(bpnaxis1 = xsh_pfits_get_naxis1(bplist));
790 check(bpnaxis2 = xsh_pfits_get_naxis2(bplist));
791 assure(bpnaxis1 == result->nx && bpnaxis2 == result->ny,
792 CPL_ERROR_ILLEGAL_INPUT,
793 "qual image %d %d must have same size of data\
794 image %d %d",
795 bpnaxis1, bpnaxis2, result->nx, result->ny);
796 cpl_image* ima_ref_bp = cpl_image_load(bpfilename, XSH_PRE_QUAL_TYPE, 0, 0);
797 xsh_badpixelmap_image_coadd(&(result->qual), ima_ref_bp,mode_or);
798 xsh_free_image(&ima_ref_bp);
799 }
800 /* Associate to data structure the flagged bad pixels after decoding using a
801 CPL standard bad pixel code in order to not use the in following
802 operations like statistics etc.. */
803
804 check(xsh_image_flag_bp(result->data, result->qual, instr));
805
806 cleanup: if (cpl_error_get_code() != CPL_ERROR_NONE) {
807 xsh_pre_free(&result);
808 result = NULL;
809 }
810 xsh_free_image(&image);
811 xsh_free_propertylist(&bplist);
812
813 return result;
814}
815
816/*---------------------------------------------------------------------------*/
822/*---------------------------------------------------------------------------*/
824{
825 if (pre && *pre) {
826 xsh_free_image(&((*pre)->data));
827 xsh_free_propertylist(&((*pre)->data_header));
828 xsh_free_image(&((*pre)->errs));
829 xsh_free_propertylist(&((*pre)->errs_header));
830 xsh_free_image(&((*pre)->qual));
831 xsh_free_propertylist(&((*pre)->qual_header));
832 cpl_free(*pre);
833 *pre = NULL;
834 }
835}
836
837/*---------------------------------------------------------------------------*/
848/*---------------------------------------------------------------------------*/
849xsh_pre* xsh_pre_load (cpl_frame * frame, xsh_instrument* instr)
850{
851 /* MUST BE DEALLOCATED in caller */
852 xsh_pre * result = NULL;
853 /* Others */
854 const char *filename = NULL;
855 int extension = 0;
856 cpl_propertylist* data_header = NULL;
857
858
859 /* check parameters input */
860 XSH_ASSURE_NOT_NULL(frame);
861 XSH_ASSURE_NOT_NULL(instr);
862 XSH_ASSURE_NOT_NULL(cpl_frame_get_tag (frame));
863 check (filename = cpl_frame_get_filename (frame));
864 /* allocate memory */
865 XSH_CALLOC(result, xsh_pre,1);
866 /* fill structure */
867 result->instrument = instr;
868 check(result->group = cpl_frame_get_group(frame));
869
870 /* get nextensions */
871 XSH_ASSURE_NOT_ILLEGAL(cpl_frame_get_nextensions(frame) == 2);
872
873 /* Load data, validate file structure */
874 extension = 0;
875 check_msg (data_header = cpl_propertylist_load (filename,extension),
876 "Cannot read the FITS header from '%s' extension %d",
877 filename, extension);
878 check(xsh_pre_init(result,data_header, instr));
879
880 result->nx = result->naxis1;
881 result->ny = result->naxis2;
882 result->decode_bp = instr->decode_bp;
883
884 /* load data */
885 check_msg (result->data = cpl_image_load (filename,XSH_PRE_DATA_TYPE,0,0),
886 "Error loading image from %s extension 0", filename);
887
888 /* load errs */
889 check_msg (result->errs_header = cpl_propertylist_load (filename,1),
890 "Cannot read the FITS header from '%s' extension 1",filename);
891 assure(strcmp(xsh_pfits_get_extname(result->errs_header),"ERRS") == 0,
892 CPL_ERROR_ILLEGAL_INPUT,"extension 1 must be a errs extension");
893 check_msg (result->errs = cpl_image_load (filename, XSH_PRE_ERRS_TYPE,0,1),
894 "Error loading image from %s extension 1", filename);
895 /* load qual */
896 check_msg (result->qual_header = cpl_propertylist_load (filename,2),
897 "Cannot read the FITS header from '%s' extension 2",filename);
898 assure(strcmp("QUAL",xsh_pfits_get_extname (result->qual_header)) == 0,
899 CPL_ERROR_ILLEGAL_INPUT,"extension 2 must be a qual extension");
900 check_msg (result->qual = cpl_image_load (filename, XSH_PRE_QUAL_TYPE,0,2),
901 "Error loading image from %s extension 2", filename);
902
903
904 //AMo: Flip UVB images
905 //AModigliani: for the moment commented out, we have to decide which
906 //is most appropriate
907 /*
908 if(xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
909 //flip the image around the horizontal
910 check(cpl_image_flip(result->data,2));
911 check(cpl_image_flip(result->errs,2));
912 check(cpl_image_flip(result->qual,2));
913 //flip the image around the vertical
914 check(cpl_image_flip(result->data,0));
915 check(cpl_image_flip(result->errs,0));
916 check(cpl_image_flip(result->qual,0));
917 }
918
919
920 //PIPPO
921 */
922 //AMo: Flip NIR images
923 /*
924 check(cpl_image_shift(result->data,1,(int)cpl_image_get_size_y(result->data)/2.));
925 if(xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
926 //turn the image 90 degree clockwise
927 check(cpl_image_turn(result->data,1));
928 check(cpl_image_turn(result->errs,1));
929 check(cpl_image_turn(result->qual,1));
930 //flip around the vertical
931 check(cpl_image_flip(result->data,0));
932 check(cpl_image_flip(result->errs,0));
933 check(cpl_image_flip(result->qual,0));
934 }
935
936 */
937
938 cleanup:
939 if (cpl_error_get_code () != CPL_ERROR_NONE) {
940 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
941 xsh_pre_free(&result);
942 }
943 return result;
944}
945
946/*--------------------------------------------------------------------------*/
952/*--------------------------------------------------------------------------*/
954{
955 xsh_pre *result = NULL;
956
957 assure(pre != NULL, CPL_ERROR_NULL_INPUT, "Null PRE");
958
959 /* allocate memory */
960 result = (xsh_pre*)(cpl_malloc (sizeof (xsh_pre)));
961 assure (result != NULL, CPL_ERROR_ILLEGAL_OUTPUT,
962 "Memory allocation failed!");
963
964 /* init */
965 result->data = NULL;
966 result->data_header = NULL;
967 result->errs = NULL;
968 result->errs_header = NULL;
969 result->qual = NULL;
970 result->qual_header = NULL;
971 result->instrument = pre->instrument;
972
973 /* copy attributes */
974 check(result->nx = xsh_pre_get_nx(pre));
975 check(result->ny = xsh_pre_get_ny(pre));
976 check(result->group = xsh_pre_get_group(pre));
977 result->pszx = pre->pszx;
978 result->pszy = pre->pszy;
979 result->gain = pre->gain;
980 result->exptime = pre->exptime;
981 /* copy data */
982 check_msg(result->data_header = cpl_propertylist_duplicate(
983 pre->data_header),"can't copy data header");
984 check_msg(result->data = cpl_image_duplicate(pre->data),
985 "can't copy data image");
986
987 /* copy errs */
988 check_msg(result->errs_header = cpl_propertylist_duplicate(
989 pre->errs_header),"can't copy errs header");
990 check_msg(result->errs = cpl_image_duplicate(pre->errs),
991 "can't copy errs image");
992
993 /* copy qual */
994 check_msg(result->qual_header = cpl_propertylist_duplicate(
995 pre->qual_header),"can't copy qual header");
996
997 check_msg(result->qual = cpl_image_duplicate(pre->qual),
998 "can't copy qual image");
999 result->decode_bp = pre->decode_bp;
1000
1001cleanup:
1002 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1003 xsh_pre_free(&result);
1004 result = NULL;
1005 }
1006 return result;
1007}
1008
1009/*---------------------------------------------------------------------------*/
1018/*----------------------------------------------------------------------------*/
1019cpl_frame* xsh_pre_save (const xsh_pre * pre,
1020 const char *filename,
1021 const char *tag,
1022 int temp )
1023{
1024 cpl_frame *product_frame = NULL;
1025
1027 XSH_ASSURE_NOT_NULL(filename);
1028
1029 /* Save the file */
1033
1035 check_msg (cpl_image_save (pre->data, filename, XSH_PRE_DATA_BPP,
1036 pre->data_header, CPL_IO_DEFAULT),
1037 "Could not save data to %s extension 0", filename);
1038 check_msg (cpl_image_save (pre->errs, filename, XSH_PRE_ERRS_BPP,
1039 pre->errs_header, CPL_IO_EXTEND),
1040 "Could not save errs to %s extension 1", filename);
1041 check_msg (cpl_image_save (pre->qual, filename, XSH_PRE_QUAL_BPP,
1042 pre->qual_header, CPL_IO_EXTEND),
1043 "Could not save qual to %s extension 2", filename);
1044 /* Create product frame */
1045 product_frame = cpl_frame_new ();
1046 XSH_ASSURE_NOT_NULL( product_frame);
1047
1048 check ((cpl_frame_set_filename (product_frame, filename),
1049 cpl_frame_set_tag(product_frame,tag),
1050 cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_IMAGE),
1051 cpl_frame_set_group (product_frame, xsh_pre_get_group(pre)) )) ;
1052
1053
1054 if ( temp != 0 ) {
1055 check( cpl_frame_set_level(product_frame,
1056 CPL_FRAME_LEVEL_TEMPORARY));
1057 xsh_add_temporary_file( filename ) ;
1058 }
1059
1060
1061cleanup:
1062 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1063 xsh_free_frame(&product_frame);
1064 product_frame = NULL;
1065 }
1066 return product_frame;
1067}
1068
1069/*---------------------------------------------------------------------------*/
1081/*----------------------------------------------------------------------------*/
1082
1083cpl_frame*
1085 cpl_frame* frame,
1086 cpl_frameset* frameset,
1087 const cpl_parameterlist* parameters,
1088 const char* recipe_id,
1089 const char *prefix)
1090{
1091 cpl_frame* result = NULL;
1092 char filename[256];
1093 time_t now ;
1094 char* date = NULL;
1095 const char* tag=NULL;
1096 /* check input */
1097 XSH_ASSURE_NOT_NULL( pre);
1098 XSH_ASSURE_NOT_NULL( frame);
1099 XSH_ASSURE_NOT_NULL( parameters);
1100 XSH_ASSURE_NOT_NULL( recipe_id);
1102
1103 /* update header */
1104 check( cpl_frame_set_group (frame, CPL_FRAME_GROUP_PRODUCT));
1105 check (cpl_dfs_setup_product_header (pre->data_header,
1106 frame, frameset, parameters,
1107 recipe_id, pre->instrument->pipeline_id,
1108 pre->instrument->dictionary,NULL));
1109
1110 /* save product */
1111 if ( prefix == NULL ) {
1112 sprintf(filename,"PRODUCT_%s",cpl_frame_get_filename (frame));
1113 }
1114 else if ( xsh_time_stamp_get() ) {
1115 time( &now ) ;
1116 date = xsh_sdate_utc(&now);
1117 sprintf( filename, "%s_%dx%d_%s.fits", prefix, pre->binx, pre->biny,
1118 date);
1119 }
1120 else
1121 sprintf( filename, "%s_%dx%d.fits", prefix, pre->binx, pre->biny );
1123 check(result = xsh_pre_save(pre, filename, tag,0));
1124 /* update frame */
1125 cpl_frame_set_type (result, CPL_FRAME_TYPE_IMAGE),
1126 cpl_frame_set_group (result, CPL_FRAME_GROUP_PRODUCT),
1127 cpl_frame_set_level (result, CPL_FRAME_LEVEL_FINAL);
1128cleanup:
1129 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1130 xsh_free_frame(&result);
1131 result = NULL;
1132 }
1133 XSH_FREE( date ) ;
1134
1135 return result;
1136}
1137
1138/*---------------------------------------------------------------------------*/
1151/*---------------------------------------------------------------------------*/
1153{
1154 xsh_pre *result = NULL;
1155
1156 assure (nx > 0 && ny > 0, CPL_ERROR_ILLEGAL_INPUT,
1157 "Illegal image size: %dx%d", nx, ny);
1158
1159 XSH_CALLOC( result, xsh_pre, 1);
1160
1161 result->nx = nx;
1162 result->ny = ny;
1163
1164 check( result->data = cpl_image_new (nx, ny, XSH_PRE_DATA_TYPE));
1165 check( result->errs = cpl_image_new (nx, ny, XSH_PRE_ERRS_TYPE));
1166 check( result->qual = cpl_image_new (nx, ny, XSH_PRE_QUAL_TYPE));
1167 check( result->data_header = cpl_propertylist_new());
1168 check( result->errs_header = cpl_propertylist_new());
1169 check( result->qual_header = cpl_propertylist_new());
1170
1171 cleanup:
1172 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1173 xsh_pre_free ( &result);
1174 result = NULL;
1175 }
1176 return result;
1177}
1178
1179/*---------------------------------------------------------------------------*/
1185/*---------------------------------------------------------------------------*/
1186int xsh_pre_get_nx (const xsh_pre * pre)
1187{
1188 int result = 0;
1189
1190 XSH_ASSURE_NOT_NULL( pre );
1191 result = pre->nx;
1192
1193 cleanup:
1194 return result;
1195}
1196
1197/*---------------------------------------------------------------------------*/
1203/*---------------------------------------------------------------------------*/
1204int xsh_pre_get_ny (const xsh_pre * pre)
1205{
1206 int result = 0;
1207
1208 XSH_ASSURE_NOT_NULL( pre );
1209 result = pre->ny;
1210
1211 cleanup:
1212 return result;
1213}
1214
1215
1216/*---------------------------------------------------------------------------*/
1222/*---------------------------------------------------------------------------*/
1223int xsh_pre_get_binx (const xsh_pre * pre)
1224{
1225 int result = 0;
1226
1227 XSH_ASSURE_NOT_NULL( pre );
1228 result = pre->binx;
1229
1230 cleanup:
1231 return result;
1232}
1233
1234/*---------------------------------------------------------------------------*/
1240/*---------------------------------------------------------------------------*/
1241int xsh_pre_get_biny (const xsh_pre * pre)
1242{
1243 int result = 0;
1244
1245 XSH_ASSURE_NOT_NULL( pre );
1246 result = pre->biny;
1247
1248 cleanup:
1249 return result;
1250}
1251
1252/*---------------------------------------------------------------------------*/
1258/*---------------------------------------------------------------------------*/
1259float xsh_pre_get_pszx (const xsh_pre * pre)
1260{
1261 float result = 0.0;
1262 assure (pre != NULL, CPL_ERROR_NULL_INPUT, "Null PRE");
1263 result = pre->pszx;
1264
1265 cleanup:
1266 return result;
1267}
1268
1269/*---------------------------------------------------------------------------*/
1278/*---------------------------------------------------------------------------*/
1279
1280void xsh_pre_from_raw_get(xsh_pre* pre, double raw_x, double raw_y,
1281 double* x, double* y)
1282{
1283 /* check parameters input */
1287
1288 /* 0.5 for complience with the data model origin pixel
1289 like ds9
1290 */
1291
1292 /* pixel in detector center at 0.5 ex : 0 => [0 1] */
1295 *x = raw_y ;
1296 *y = pre->ny + pre->cutx - raw_x;
1297 }
1298 else{
1299 *x = raw_x - pre->cutx;
1300 *y = raw_y - pre->cuty;
1301 }
1302
1303 cleanup:
1304 return;
1305}
1306
1307/*---------------------------------------------------------------------------*/
1313/*---------------------------------------------------------------------------*/
1314float xsh_pre_get_pszy (const xsh_pre * pre)
1315{
1316 float result = 0.0;
1317
1319 result = pre->pszy;
1320
1321 cleanup:
1322 return result;
1323}
1324
1325/*---------------------------------------------------------------------------*/
1331/*---------------------------------------------------------------------------*/
1332float xsh_pre_get_gain (const xsh_pre * pre)
1333{
1334 float result = 0.0;
1335
1337 result = pre->gain;
1338
1339 cleanup:
1340 return result;
1341}
1342/*---------------------------------------------------------------------------*/
1348/*---------------------------------------------------------------------------*/
1349cpl_frame_group xsh_pre_get_group (const xsh_pre * pre)
1350{
1351 cpl_frame_group result = CPL_FRAME_GROUP_NONE;
1352
1354 result = pre->group;
1355
1356 cleanup:
1357 return result;
1358}
1359
1365/*---------------------------------------------------------------------------*/
1366cpl_mask* xsh_pre_get_bpmap(const xsh_pre * pre) {
1367 int* qual = NULL;
1368 cpl_mask* mask = NULL;
1369 cpl_binary* binary = NULL;
1370 int nx_ny = 0;
1371 int i = 0;
1372
1374
1375 check(mask = cpl_image_get_bpm(pre->qual));
1376 check(qual = cpl_image_get_data_int(pre->qual));
1377 check(binary = cpl_mask_get_data(mask));
1378 nx_ny = pre->nx * pre->ny;
1379 for (i = 0; i < nx_ny; i++) {
1380 if ( (qual[i] & pre->decode_bp) > 0 ) {
1381 /* bad pix */
1382 binary[i] = CPL_BINARY_1;
1383 }
1384 }
1385
1386 cleanup: return mask;
1387}
1388
1389/*---------------------------------------------------------------------------*/
1409/*---------------------------------------------------------------------------*/
1410
1411void xsh_pre_add (xsh_pre* self, const xsh_pre * right)
1412{
1413 float* errs1 = NULL;
1414 float* errs2 = NULL;
1415 int i = 0;
1416 //cpl_mask* mask = NULL;
1417 //cpl_binary* binary =NULL;
1418 //int* qual = NULL;
1419
1420 XSH_ASSURE_NOT_NULL(self);
1421 XSH_ASSURE_NOT_NULL(right);
1422
1423 /* Check that sizes match */
1424 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
1425 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
1426 CPL_ERROR_INCOMPATIBLE_INPUT,
1427 "Image sizes don't match: %dx%d and %dx%d",
1428 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
1429 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
1430
1431 /* Handle data unit + plist */
1432 check (cpl_image_add(self->data, right->data));
1433
1434 /* Handle errs unit + plist */
1435 check(errs1 = cpl_image_get_data_float(self->errs));
1436 check(errs2 = cpl_image_get_data_float(right->errs));
1437
1438 for(i=0;i< self->nx*self->ny; i++){
1439 errs1[i] = sqrt(pow(errs1[i],2)+pow(errs2[i],2));
1440 }
1441
1442 /* QUAL computation: combine the two QUAL codes */
1443
1444 /* IF there is a CALIB
1445 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
1446 // PUT QFLAG CALIB DEFECT
1447 check( mask = xsh_pre_get_bpmap(right));
1448 check(binary = cpl_mask_get_data(mask));
1449 check(qual = cpl_image_get_data_int(self->qual));
1450 for(i=0;i<self->nx*self->ny;i++){
1451 if (binary[i]){
1452 qual[i] |= QFLAG_CALIB_FILE_DEFECT;
1453 }
1454 }
1455 }
1456 else {
1457 // Need a function to "OR" 2 images
1458 xsh_badpixelmap_or( self, right ) ;
1459 }
1460 */
1461 xsh_badpixelmap_or( self, right ) ;
1462cleanup:
1463 return;
1464}
1465
1466
1483cpl_frameset* xsh_pre_frameset_subtract_frame( cpl_frameset *set,
1484 cpl_frame *sub,
1485 const char* spec,
1486 xsh_instrument *instr )
1487{
1488 int i=0;
1489 int size=0;
1490
1491 cpl_frame* frm=NULL;
1492 cpl_frame* cor=NULL;
1493 cpl_frameset* result=NULL;
1494 char filename[256];
1495 const char * name=NULL;
1496 size=cpl_frameset_get_size(set);
1497 result=cpl_frameset_new();
1498 for(i=0;i<size;i++) {
1499 frm=cpl_frameset_get_frame(set,i);
1500 name=cpl_frame_get_filename(frm);
1501 sprintf(filename,"SUB_%s_%d_%s",spec,i,name);
1502 cor=xsh_pre_frame_subtract(frm,sub,filename,instr,1);
1503 cpl_frameset_insert(result,cor);
1504 }
1505
1506 return result;
1507
1508}
1509
1510
1527cpl_frame*
1529 cpl_frame *two,
1530 const char *filename,
1531 xsh_instrument *instr,
1532 const int clean_tmp)
1533{
1534 xsh_pre * pre1 = NULL ;
1535 xsh_pre * pre2 = NULL ;
1536 xsh_pre * pre_result = NULL ;
1537 cpl_frame * result = NULL ;
1538 const char* tag=NULL;
1539
1540 /* Check input */
1541 XSH_ASSURE_NOT_NULL ( one);
1542 XSH_ASSURE_NOT_NULL ( two);
1543 XSH_ASSURE_NOT_NULL ( filename);
1544 XSH_ASSURE_NOT_NULL ( instr);
1545
1546 /* Load the 2 pre frames */
1547 check ( pre1 = xsh_pre_load( one, instr));
1548 check ( pre2 = xsh_pre_load( two, instr));
1549
1550 /* subtract two from one */
1551 check( pre_result = xsh_pre_duplicate( pre1));
1552 check ( xsh_pre_subtract( pre_result, pre2));
1553
1554 check(tag=cpl_frame_get_tag(one));
1555 /* Save the pre result */
1556 check( result = xsh_pre_save( pre_result, filename,tag,0));
1557
1558 /* Set the result frame tags, etc */
1559 check ((cpl_frame_set_filename (result, filename),
1560 cpl_frame_set_type (result, CPL_FRAME_TYPE_IMAGE),
1561 cpl_frame_set_group (result, xsh_pre_get_group(pre_result)),
1562 cpl_frame_set_level (result, CPL_FRAME_LEVEL_TEMPORARY),
1563 cpl_frame_set_tag( result,tag)));
1564 if(clean_tmp) {
1565 xsh_add_temporary_file( filename ) ;
1566 }
1567 cleanup:
1568 xsh_pre_free( &pre1);
1569 xsh_pre_free( &pre2);
1570 xsh_pre_free( &pre_result);
1571 return result;
1572}
1573
1574/*----------------------------------------------------------------------------*/
1587/*----------------------------------------------------------------------------*/
1588
1589void xsh_pre_subtract (xsh_pre* self, const xsh_pre * right)
1590{
1591
1592 float* errs1 = NULL;
1593 float* errs2 = NULL;
1594 int i = 0;
1595 //cpl_mask* mask = NULL;
1596 //cpl_binary* binary =NULL;
1597 //int* qual = NULL;
1598
1599 /* Check input */
1600 XSH_ASSURE_NOT_NULL( self);
1601 XSH_ASSURE_NOT_NULL( right);
1602
1603 /* Check that sizes match */
1604 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
1605 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
1606 CPL_ERROR_INCOMPATIBLE_INPUT,
1607 "Image sizes don't match: %dx%d and %dx%d",
1608 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
1609 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
1610
1611 /* Handle data unit + plist */
1612 check ( cpl_image_subtract( self->data, right->data));
1613
1614 /* Handle errs unit + plist */
1615 check( errs1 = cpl_image_get_data_float( self->errs));
1616 check( errs2 = cpl_image_get_data_float( right->errs));
1617
1618 for(i=0;i< self->nx*self->ny; i++){
1619 errs1[i] = sqrt(pow(errs1[i],2)+pow(errs2[i],2));
1620 }
1621
1622 /* Handle qual unit + plist
1623 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
1624 check( mask = xsh_pre_get_bpmap(right));
1625 check(binary = cpl_mask_get_data(mask));
1626 check(qual = cpl_image_get_data_int(self->qual));
1627 for(i=0;i<self->nx*self->ny;i++){
1628 if ( (binary[i] & self->decode_bp) > 0 ){
1629 // this is a bad pixel
1630 qual[i] |= QFLAG_CALIB_FILE_DEFECT;
1631 }
1632 }
1633 }
1634 else {
1635 // In case the inputs are RAW frames
1636 xsh_badpixelmap_or( self, right ) ;
1637 }
1638 */
1639 xsh_badpixelmap_or( self, right ) ;
1640 cleanup:
1641 return ;
1642}
1643/*----------------------------------------------------------------------------*/
1663/*----------------------------------------------------------------------------*/
1664xsh_pre* xsh_pre_subsample (xsh_pre* self, const int binx, const int biny, const int rescale,xsh_instrument* inst)
1665{
1666
1667 xsh_pre* result=NULL;
1668 float *errsi = NULL;
1669 float *errso = NULL;
1670 float *datai = NULL;
1671 float *datao = NULL;
1672 int *quali = NULL;
1673 int *qualo = NULL;
1674 int i = 0;
1675 int j = 0;
1676 int nx_ny=0;
1677 int nx=0;
1678 int ny=0;
1679 int m=0;
1680 int k=0;
1681 int sx=0;
1682 int sy=0;
1683
1684 int pixeli=0;
1685 int pixelo=0;
1686
1687 int j_biny=0;
1688 int j_nx=0;
1689 int nx_binx=0;
1690 float inv_binx_biny=1;
1691 float binx_biny=1;
1692
1693 assure (self != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
1694 assure (binx >0, CPL_ERROR_ILLEGAL_INPUT, "binx <=0!");
1695 assure (biny >0, CPL_ERROR_ILLEGAL_INPUT, "biny <=0!");
1696
1697 /* Handle data and errs unit */
1698 check(datai = cpl_image_get_data_float(self->data));
1699 check(errsi = cpl_image_get_data_float(self->errs));
1700 check(quali = cpl_image_get_data_int(self->qual));
1701
1702 sx=self->nx;
1703 sy=self->ny;
1704 nx=sx/binx;
1705 ny=sy/biny;
1706
1707
1708 result=xsh_pre_new(nx,ny);
1709 check(datao = cpl_image_get_data_float(result->data));
1710 check(errso = cpl_image_get_data_float(result->errs));
1711 check(qualo = cpl_image_get_data_int(result->qual));
1712
1713 nx_binx=sx;
1714 int cpix=0;
1715 int cpix_k=0;
1716
1717 //Loop over output image
1718 for (j = 0; j < ny; j++) {
1719 j_biny = j*biny;
1720 j_nx = j*nx;
1721 for (i = 0; i < nx; i++) {
1722 pixeli = j_biny*nx_binx + (i*binx);
1723 pixelo = j_nx + i;
1724
1725 //Loop over pixels to be binned into one pixel in the input image
1726 for (m = 0; m < biny; m++) {
1727 cpix = pixeli +m*nx_binx;
1728 for (k = 0; k < binx;k++) {
1729
1730 cpix_k = cpix + k;
1731 datao[pixelo] += datai[cpix_k];
1732 errso[pixelo] += errsi[cpix_k] * errsi[cpix_k];
1733 qualo[pixelo] |= quali[cpix_k];
1734
1735 }
1736 }
1737 errso[pixelo] = sqrt(errso[pixelo]);
1738 }
1739 }
1740
1741 if (rescale > 0) {
1742 nx_ny=nx*ny;
1743 inv_binx_biny=1./(binx*biny);
1744 /* HERE ONE SHOULD RE-SCALE */
1745 for (i = 0; i < nx_ny; i++) {
1746 datao[i] *= inv_binx_biny;
1747 errso[i] *= inv_binx_biny;
1748 }
1749 }
1750
1751 if (rescale < 0) {
1752 nx_ny=nx*ny;
1753 binx_biny=(binx*biny);
1754 /* HERE ONE SHOULD RE-SCALE */
1755 for (i = 0; i < nx_ny; i++) {
1756 datao[i] *= binx_biny;
1757 errso[i] *= binx_biny;
1758 }
1759 }
1760 cleanup:
1761 return result;
1762}
1763
1764/*----------------------------------------------------------------------------*/
1784/*----------------------------------------------------------------------------*/
1785void xsh_pre_divide (xsh_pre* self, const xsh_pre * right, const int is_flat, double threshold)
1786{
1787
1788 float *errs1 = NULL;
1789 float *errs2 = NULL;
1790 float *data1 = NULL;
1791 float *data2 = NULL;
1792 int *qual1 = NULL;
1793 int *qual2 = NULL;
1794 int i = 0;
1795 int nx_ny=0;
1796 assure (self != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
1797 assure (right != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
1798
1799 /* Check that sizes match */
1800 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
1801 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
1802 CPL_ERROR_INCOMPATIBLE_INPUT,
1803 "Image sizes don't match: %dx%d and %dx%d",
1804 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
1805 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
1806
1807 /* Handle data and errs unit */
1808
1809 check(data1 = cpl_image_get_data_float(self->data));
1810 check(data2 = cpl_image_get_data_float(right->data));
1811 check(errs1 = cpl_image_get_data_float(self->errs));
1812 check(errs2 = cpl_image_get_data_float(right->errs));
1813 check(qual1 = cpl_image_get_data_int(self->qual));
1814 check(qual2 = cpl_image_get_data_int(right->qual));
1815 nx_ny=self->nx*self->ny;
1816
1817 for(i=0;i< nx_ny; i++){
1818
1819 if ( (qual2[i] & self->decode_bp) > 0 ) {
1820 // pixel quality
1821 /*
1822 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
1823 qual1[i] |= QFLAG_CALIB_FILE_DEFECT;
1824 }
1825 else {
1826 qual1[i] |= qual2[i];
1827 }
1828 */
1829 qual1[i] |= qual2[i];
1830 if ( (is_flat) &&
1831 (qual2[i] & (QFLAG_SATURATED_DATA > 0) ) &&
1832 (qual1[i] & (QFLAG_SATURATED_DATA == 0) )
1833 ) {
1834 /* if division was by a saturated flat remove
1835 * saturated pixel quality
1836 */
1837
1838 qual1[i] -= QFLAG_SATURATED_DATA;
1839
1840 }
1841 if (qual1[i] > 0 ) {
1842 /* as QFLAG_SATURATED_DATA was removed the qual1[i] > 0 only
1843 * if the pixel was bad for other reasons. In this case flag this
1844 * with QFLAG_CALIB_FILE_DEFECT
1845 */
1846 qual1[i] |= QFLAG_CALIB_FILE_DEFECT;
1847 }
1848 }
1849 if ( fabs(data2[i]) < threshold){
1850 qual1[i] |= QFLAG_DIVISOR_ZERO;
1851 errs1[i] = 1;
1852 data1[i] = 0;
1853 }
1854 else {
1855 double d1 = 0.0, d2 = 0.0;
1856 double e1 = 0.0, e2 =0.0;
1857 double error = 0.0;
1858
1859 d1 = data1[i];
1860 d2 = data2[i];
1861 e1 = errs1[i];
1862 e2 = errs2[i];
1863 error = fabs( 1.0/d2) * sqrt( (e1*e1) + (e2*e2*d1*d1)/(d2*d2) );
1864 errs1[i] = (float) error;
1865 data1[i] = (float)(d1/d2);
1866 }
1867 }
1868
1869 cleanup:
1870 return;
1871}
1872
1873/*----------------------------------------------------------------------------*/
1892/*----------------------------------------------------------------------------*/
1893void xsh_pre_multiply (xsh_pre* self, const xsh_pre * right, double threshold)
1894{
1895
1896 float *errs1 = NULL;
1897 float *errs2 = NULL;
1898 float *data1 = NULL;
1899 float *data2 = NULL;
1900 int *qual1 = NULL;
1901 int *qual2 = NULL;
1902 int i = 0;
1903 int nx_ny=0;
1904 assure (self != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
1905 assure (right != NULL, CPL_ERROR_NULL_INPUT, "Null image!");
1906
1907 /* Check that sizes match */
1908 assure (xsh_pre_get_nx (self) == xsh_pre_get_nx (right) &&
1909 xsh_pre_get_ny (self) == xsh_pre_get_ny (right),
1910 CPL_ERROR_INCOMPATIBLE_INPUT,
1911 "Image sizes don't match: %dx%d and %dx%d",
1912 xsh_pre_get_nx (self), xsh_pre_get_ny (self),
1913 xsh_pre_get_nx (right), xsh_pre_get_ny (right));
1914
1915 /* Handle data and errs unit */
1916
1917 check(data1 = cpl_image_get_data_float(self->data));
1918 check(data2 = cpl_image_get_data_float(right->data));
1919 check(errs1 = cpl_image_get_data_float(self->errs));
1920 check(errs2 = cpl_image_get_data_float(right->errs));
1921 check(qual1 = cpl_image_get_data_int(self->qual));
1922 check(qual2 = cpl_image_get_data_int(right->qual));
1923 nx_ny=self->nx*self->ny;
1924 for(i=0;i< nx_ny; i++){
1925
1926 if ( (qual2[i] & self->decode_bp) > 0 ) {
1927 // pixel quality
1928 /*
1929 if( (xsh_pre_get_group(right) == CPL_FRAME_GROUP_CALIB) ){
1930 qual1[i] |= QFLAG_CALIB_FILE_DEFECT;
1931 }
1932 else {
1933 qual1[i] |= qual2[i];
1934 }
1935 */
1936 qual1[i] |= qual2[i];
1937 }
1938 else if ( fabs(data2[i]) > threshold){
1939 qual1[i] |= QFLAG_OUTSIDE_DATA_RANGE;
1940 errs1[i] = 1;
1941 data1[i] = 0;
1942 }
1943 else {
1944 double d1 = 0.0, d2 = 0.0;
1945 double e1 = 0.0, e2 =0.0;
1946 double error = 0.0;
1947
1948 d1 = data1[i];
1949 d2 = data2[i];
1950 e1 = errs1[i];
1951 e2 = errs2[i];
1952 error = sqrt( d2*d2*e1*e1 + d1*d1*e2*e2 );
1953 errs1[i] = (float) error;
1954 data1[i] = (float)(d1*d2);
1955 }
1956 }
1957
1958 cleanup:
1959 return;
1960}
1961
1962
1963/*----------------------------------------------------------------------------*/
1968/*----------------------------------------------------------------------------*/
1970{
1971 double val1, ct1;
1972 int i, rej, i1;
1973
1974 XSH_ASSURE_NOT_NULL(self);
1975 i = 0;
1976
1977 /* get the 1st index where the data is not a bad pixel and not 0 */
1978 do {
1979 i++;
1980 check(val1 = cpl_image_get(self->data, i, 1, &rej));
1981 }
1982 while (val1 == 0 || rej == 1);
1983 i1 = i;
1984
1985 /* normalise image data by the mean of the whole frame */
1986 check(cpl_image_normalise( self->data, CPL_NORM_MEAN));
1987
1988 /* obtain the scale factor */
1989 check(ct1 = val1/ cpl_image_get(self->data, i1, 1, &rej));
1990
1991 xsh_msg("normalize estimate pixel (%d,1) : %f",i1,ct1);
1992
1993 /* apply scale factor to errors */
1994 check( cpl_image_divide_scalar( self->errs, ct1));
1995
1996 cleanup:
1997 return;
1998}
1999
2009void xsh_pre_median_mean_stdev( xsh_pre * preFrame, double * mean,
2010 double * median, double * stdev )
2011{
2012 int i, ng, nx, ny ;
2013 float * ppix = NULL;
2014 int * pbad = NULL;
2015 cpl_vector * ptemp = NULL;
2016 int nx_ny=0;
2017 XSH_ASSURE_NOT_NULL( preFrame ) ;
2018 XSH_ASSURE_NOT_NULL( mean ) ;
2019 XSH_ASSURE_NOT_NULL( median ) ;
2020 XSH_ASSURE_NOT_NULL( stdev ) ;
2021
2022 nx = xsh_pre_get_nx (preFrame);
2023 assure (nx != 0, cpl_error_get_code (), "Cant get X size");
2024 ny = xsh_pre_get_ny (preFrame);
2025 assure (ny != 0, cpl_error_get_code (), "Cant get Y size");
2026
2027 check( ptemp = cpl_vector_new( nx*ny )) ;
2028 check( ppix = cpl_image_get_data_float( preFrame->data ) ) ;
2029 check( pbad = cpl_image_get_data_int( preFrame->qual ) ) ;
2030 nx_ny=nx*ny;
2031 for( ng = 0, i = 0 ; i < nx_ny ; i++ ) {
2032 if ( (pbad[i] & preFrame->decode_bp) == 0 ){
2033 /* good pixel */
2034 check( cpl_vector_set( ptemp, ng, (double)(ppix[i]) )) ;
2035 ng++ ;
2036 }
2037 }
2038
2039 /* Now calculate median and Avg */
2040 check( cpl_vector_set_size( ptemp, ng ) );
2041 check( *mean = cpl_vector_get_mean( ptemp ) );
2042 check( *stdev = cpl_vector_get_stdev( ptemp ) );
2043 check( *median = cpl_vector_get_median( ptemp ) );
2044
2045 cleanup:
2046 if (cpl_error_get_code() != CPL_ERROR_NONE){
2047 *mean = 0.0;
2048 *stdev = 0.0;
2049 *median = 0.0;
2050 }
2051 xsh_free_vector( &ptemp ) ;
2052 return;
2053}
2054
2068void xsh_pre_median_mean_stdev_window( xsh_pre * preFrame, double * mean,
2069 double * median, double * stdev,
2070 const int llx, const int lly,
2071 const int urx, const int ury )
2072{
2073 int i=0, ng=0, nx=0, ny=0 ;
2074 int vx=0,vy=0;
2075 float * ppix = NULL;
2076 int * pbad = NULL;
2077 cpl_vector * ptemp = NULL;
2078 int j=0;
2079
2080 XSH_ASSURE_NOT_NULL( preFrame ) ;
2081 XSH_ASSURE_NOT_NULL( mean ) ;
2082 XSH_ASSURE_NOT_NULL( median ) ;
2083 XSH_ASSURE_NOT_NULL( stdev ) ;
2084
2085 nx = xsh_pre_get_nx (preFrame);
2086 assure (nx != 0, cpl_error_get_code (), "Cant get X size");
2087 ny = xsh_pre_get_ny (preFrame);
2088 assure (ny != 0, cpl_error_get_code (), "Cant get Y size");
2089
2090 vx=urx-llx+1;
2091 vy=ury-lly+1;
2092
2093 check( ptemp = cpl_vector_new( vx*vy )) ;
2094 check( ppix = cpl_image_get_data_float( preFrame->data ) ) ;
2095 check( pbad = cpl_image_get_data_int( preFrame->qual ) ) ;
2096
2097 ng=0;
2098 //we need to decrese of 1 as vectors start from 0 while images from 1
2099 for( j = lly-1 ; j < ury ; j++ ) {
2100 for( i = llx-1 ; i < urx ; i++ ) {
2101 //xsh_msg("pbad=%d",pbad[i]);
2102 if ( (pbad[i] & preFrame->decode_bp) == 0) {
2103 /* good pixel */
2104 check( cpl_vector_set( ptemp, ng, (double)(ppix[j*nx+i]) )) ;
2105 ng++ ;
2106 }
2107 }
2108 }
2109
2110 cpl_table* tab=NULL;
2111 cpl_table* ext=NULL;
2112 int kappa=5;
2113 int niter1=2;
2114 int niter2=4;
2115
2116 /* Now calculate median and Avg */
2117 if(ng>0) {
2118 tab=cpl_table_new(ng);
2119 cpl_table_wrap_double(tab,cpl_vector_get_data(ptemp),"VAL");
2120
2121 /* first iteration comparing to median */
2122 *median=cpl_table_get_column_median(tab,"VAL");
2123 *mean=cpl_table_get_column_mean(tab,"VAL");
2124 *stdev=cpl_table_get_column_stdev(tab,"VAL");
2125 for(i=0;i<niter1;i++) {
2126
2127 cpl_table_and_selected_double(tab,"VAL",CPL_LESS_THAN,
2128 *median+kappa*(*stdev));
2129 cpl_table_and_selected_double(tab,"VAL",CPL_GREATER_THAN,
2130 *median-kappa*(*stdev));
2131 xsh_free_table(&ext);
2132 ext=cpl_table_extract_selected(tab);
2133 *median=cpl_table_get_column_median(ext,"VAL");
2134 *mean=cpl_table_get_column_mean(ext,"VAL");
2135 *stdev=cpl_table_get_column_stdev(ext,"VAL");
2136 //xsh_msg("mean=%g median=%g stdev=%g",*mean,*median,*stdev);
2137 }
2138
2139 /* then compare to mean */
2140 for(i=0;i<niter2;i++) {
2141 cpl_table_and_selected_double(tab,"VAL",CPL_LESS_THAN,
2142 *mean+kappa*(*stdev));
2143 cpl_table_and_selected_double(tab,"VAL",CPL_GREATER_THAN,
2144 *mean-kappa*(*stdev));
2145 xsh_free_table(&ext);
2146 ext=cpl_table_extract_selected(tab);
2147 *median=cpl_table_get_column_median(ext,"VAL");
2148 *mean=cpl_table_get_column_mean(ext,"VAL");
2149 *stdev=cpl_table_get_column_stdev(ext,"VAL");
2150 //xsh_msg("mean=%g median=%g stdev=%g",*mean,*median,*stdev);
2151 }
2152 xsh_free_table(&ext);
2153 ext=cpl_table_extract_selected(tab);
2154 //check(cpl_table_save(ext,NULL,NULL,"pippo.fits",CPL_IO_DEFAULT));
2155 cpl_table_unwrap(tab,"VAL");
2156 *median=cpl_table_get_column_median(ext,"VAL");
2157 *mean=cpl_table_get_column_mean(ext,"VAL");
2158 *stdev=cpl_table_get_column_stdev(ext,"VAL");
2159 } else {
2160 *mean = -999.0;
2161 *stdev = -999.0;
2162 *median = -999.0;
2163 }
2164
2165
2166 cleanup:
2167 if (cpl_error_get_code() != CPL_ERROR_NONE){
2168 *mean = 0.0;
2169 *stdev = 0.0;
2170 *median = 0.0;
2171 }
2172 xsh_free_vector( &ptemp ) ;
2173 xsh_free_table(&tab);
2174 xsh_free_table(&ext);
2175 return;
2176}
2177
2178/*---------------------------------------------------------------------------*/
2199/*---------------------------------------------------------------------------*/
2200int xsh_pre_window_best_median_flux_pos( xsh_pre* pre, int xcen, int ycen,
2201 int search_window_hsize, int running_median_hsize, int* xadj, int* yadj)
2202{
2203 int search_win_xmin, search_win_xmax, search_win_ymin, search_win_ymax;
2204 //float* data = NULL;
2205 int* qual = NULL;
2206 double* median_tab = NULL;
2207 int median_box_size = 0;
2208 double max_flux = -99999;
2209 int ibox, jbox;
2210 int xpos = -1, ypos = -1;
2211 int result= 0;
2212 int status=0;
2213 XSH_ASSURE_NOT_NULL( pre);
2214 XSH_ASSURE_NOT_NULL( xadj);
2215 XSH_ASSURE_NOT_NULL( yadj);
2216 XSH_CMP_INT( xcen, >=, 0, "Check central x position",);
2217 XSH_CMP_INT( xcen, <, pre->nx, "Check central x position",);
2218 XSH_CMP_INT( ycen, >=, 0, "Check central x position",);
2219 XSH_CMP_INT( ycen, <, pre->ny, "Check central x position",);
2220
2221 median_box_size = 1+running_median_hsize*2;
2222
2223 search_win_xmin = xcen-search_window_hsize;
2224 search_win_xmax = xcen+search_window_hsize;
2225 search_win_ymin = ycen-search_window_hsize;
2226 search_win_ymax = ycen+search_window_hsize;
2227
2228 if ( search_win_xmin < 0){
2229 search_win_xmin = 0;
2230 }
2231 if ( search_win_xmax >= pre->nx){
2232 search_win_xmax = pre->nx-1;
2233 }
2234 if ( search_win_ymin < 0){
2235 search_win_ymin = 0;
2236 }
2237 if ( search_win_ymax >= pre->ny){
2238 search_win_ymax = pre->ny-1;
2239 }
2240
2241 //check( data = cpl_image_get_data_float( pre->data));
2242 check( qual = cpl_image_get_data_int( pre->qual));
2243 XSH_CALLOC(median_tab, double, median_box_size* median_box_size);
2244
2245 int xc,yc;
2246 double med_flux = 0.0;
2247 /* We search for approx pixel position within a box */
2248 for (jbox=search_win_ymin; jbox <= (search_win_ymax+1-median_box_size);
2249 jbox++){
2250 yc = jbox+running_median_hsize;
2251 int array_shift=yc*pre->nx;
2252 for (ibox=search_win_xmin; ibox <= (search_win_xmax+1-median_box_size);
2253 ibox++){
2254
2255 med_flux = xsh_pre_data_window_median_flux_pa( pre,
2256 ibox, jbox, median_box_size, median_box_size, median_tab,&status);
2257
2258 if ( cpl_error_get_code() == CPL_ERROR_NONE){
2259
2260 if (max_flux < med_flux) {
2261 xc = ibox+running_median_hsize;
2262 if ( (qual[xc+array_shift] & pre->decode_bp ) == 0) {
2263
2264 /* good pixel */
2265 max_flux = med_flux;
2266 xpos = xc;
2267 ypos = yc;
2268
2269 }
2270
2271 }
2272
2273 }
2274 else{
2276 }
2277 }
2278 }
2279 if (xpos < 0 || ypos < 0){
2280 xsh_msg_dbg_high( "No valid pixels in the search box");
2281 result = 1;
2282 }
2283 else{
2284 *xadj = xpos;
2285 *yadj = ypos;
2286 }
2287
2288 cleanup:
2289 XSH_FREE( median_tab);
2290 return result;
2291}
2292
2293
2294
2295/*----------------------------------------------------------------------------*/
2309/*----------------------------------------------------------------------------*/
2311 int size_x, int size_y, double* tab,int* status)
2312{
2313
2314 cpl_vector* v = NULL;
2315 float* data = NULL;
2316 int* qual = NULL;
2317 int ix = 0, iy = 0;
2318 int size = 0;
2319 *status=0;
2320 double flux = 0.0;
2321 /* check parameters */
2323 XSH_ASSURE_NOT_ILLEGAL(x >=1 && x <= pre->nx);
2324 XSH_ASSURE_NOT_ILLEGAL(y >= 1 && y <= pre->ny);
2325 XSH_ASSURE_NOT_ILLEGAL(size_x > 0);
2326 XSH_ASSURE_NOT_ILLEGAL(size_y > 0);
2328
2329 x = x-1;
2330 y = y-1;
2331
2332 if ( (x+size_x) >= pre->nx){
2333 size_x = pre->nx-x;
2334 }
2335 if ( (y+size_y) >= pre->ny){
2336 size_y = pre->ny-y;
2337 }
2338
2339 /* get data */
2340 check(data = cpl_image_get_data_float(pre->data));
2341 /* get qual */
2342 check(qual = cpl_image_get_data_int(pre->qual));
2343
2344 /*Extract good pixels from box*/
2345 int offset=0;
2346 int x_plus_size_x = x + size_x;
2347 int y_plus_size_y = y + size_y;
2348 int ix_plus_offset = 0;
2349 for( iy=y; iy< y_plus_size_y; iy++){
2350 offset=iy*pre->nx;
2351 for( ix=x; ix<x_plus_size_x;ix++){
2352 ix_plus_offset = ix + offset;
2353 if ( (qual[ix_plus_offset] & pre->decode_bp) == 0 ) {
2354 /* good pix */
2355 tab[size] = data[ix_plus_offset];
2356 size++;
2357 }
2358 }
2359 }
2360
2361
2362 if (size==0) {
2363 /* If no good pixels, then return flux = 0.0 */
2364 *status=1;
2365 } else if (size==1) {
2366 /* If one good pixel, then return flux = pix value */
2367 flux = tab[0];
2368 } else if (size>1) {
2369 /* if found more than one good pixel, compute median */
2370 check(v = cpl_vector_wrap(size, tab));
2371 check( flux = cpl_vector_get_median(v));
2372 }
2373 cleanup:
2375 return flux;
2376}
2377
2378
2379
2380/*----------------------------------------------------------------------------*/
2386/*----------------------------------------------------------------------------*/
2387const cpl_propertylist *
2389{
2390 cpl_propertylist *header = NULL;
2391
2393 header = pre->data_header;
2394
2395 cleanup:
2396 return header;
2397}
2398
2399/*----------------------------------------------------------------------------*/
2405/*----------------------------------------------------------------------------*/
2406cpl_propertylist *
2408{
2410
2411 cleanup:
2412 return (cpl_propertylist *) xsh_pre_get_header_const (pre);
2413}
2414
2415
2416/*----------------------------------------------------------------------------*/
2422/*----------------------------------------------------------------------------*/
2423const cpl_image* xsh_pre_get_data_const (const xsh_pre * pre)
2424{
2425 cpl_image *data = NULL;
2426
2428 data = pre->data;
2429
2430 cleanup:
2431 return data;
2432}
2433
2434/*----------------------------------------------------------------------------*/
2440/*----------------------------------------------------------------------------*/
2441cpl_image* xsh_pre_get_data (xsh_pre * pre)
2442{
2443 return (cpl_image *) xsh_pre_get_data_const (pre);
2444}
2445
2446/*----------------------------------------------------------------------------*/
2452/*----------------------------------------------------------------------------*/
2453const cpl_image * xsh_pre_get_errs_const (const xsh_pre * pre)
2454{
2455 cpl_image *errs = NULL;
2456
2458 errs = pre->errs;
2459
2460 cleanup:
2461 return errs;
2462}
2463
2464/*----------------------------------------------------------------------------*/
2470/*----------------------------------------------------------------------------*/
2471cpl_image* xsh_pre_get_errs (xsh_pre * pre)
2472{
2473 return (cpl_image *) xsh_pre_get_errs_const (pre);
2474}
2475
2476/*----------------------------------------------------------------------------*/
2482/*----------------------------------------------------------------------------*/
2483const cpl_image* xsh_pre_get_qual_const (const xsh_pre * pre)
2484{
2485 cpl_image *qual = NULL;
2486
2488 qual = pre->qual;
2489
2490 cleanup:
2491 return qual;
2492}
2493
2494/*----------------------------------------------------------------------------*/
2500cpl_image* xsh_pre_get_qual (xsh_pre * pre)
2501{
2502 return (cpl_image *) xsh_pre_get_qual_const (pre);
2503}
2504
2505/*----------------------------------------------------------------------------*/
2511/*----------------------------------------------------------------------------*/
2512const double* xsh_pre_get_data_buffer_const (const xsh_pre * pre)
2513{
2514 double *buffer = NULL;
2515
2517 /* Before casting from (void *) to (double *), make sure
2518 the CPL image has the expected type
2519 */
2520 passure (cpl_image_get_type (pre->data) == CPL_TYPE_DOUBLE, " ");
2521 buffer = (double *) cpl_image_get_data (pre->data);
2522
2523 cleanup:
2524 return buffer;
2525}
2526
2527/*----------------------------------------------------------------------------*/
2533/*----------------------------------------------------------------------------*/
2535{
2536 return (double *) xsh_pre_get_data_buffer_const (pre);
2537}
2538
2539/*----------------------------------------------------------------------------*/
2545/*----------------------------------------------------------------------------*/
2546const double* xsh_pre_get_errs_buffer_const (const xsh_pre * pre)
2547{
2548 double *buffer = NULL;
2549
2551 passure (cpl_image_get_type (pre->errs) == CPL_TYPE_DOUBLE, " ");
2552 buffer = (double *) cpl_image_get_data (pre->errs);
2553
2554 cleanup:
2555 return buffer;
2556}
2557
2558/*----------------------------------------------------------------------------*/
2564/*----------------------------------------------------------------------------*/
2566{
2567 return (double *) xsh_pre_get_errs_buffer_const(pre);
2568}
2569
2570/*----------------------------------------------------------------------------*/
2578/*----------------------------------------------------------------------------*/
2579void
2580xsh_pre_dump (const xsh_pre * pre, FILE * stream)
2581{
2582 cpl_stats *stats = NULL;
2583
2584 if (pre == NULL) {
2585 fprintf (stream, "Null");
2586 }
2587 else {
2588 /* Dump data image stats */
2589 stats = cpl_stats_new_from_image (pre->data, CPL_STATS_ALL);
2590
2591 cpl_stats_dump (stats, CPL_STATS_ALL, stream);
2592
2593 //fixme: print whatever relevant information
2594
2595 fflush (stream);
2596 }
2597
2598 xsh_free_stats (&stats);
2599 return;
2600}
2601
2602
2603/*----------------------------------------------------------------------------*/
2609void xsh_pre_multiply_scalar (const xsh_pre * pre, double x)
2610{
2611
2612 XSH_ASSURE_NOT_NULL( pre);
2613
2614 /* Handle data unit + plist */
2615 check (cpl_image_multiply_scalar(pre->data, x));
2616
2617 /* Handle errs unit + plist */
2618 check (cpl_image_multiply_scalar(pre->errs, fabs(x)));
2619
2620 cleanup:
2621 return;
2622}
2623
2629void xsh_pre_divide_scalar (const xsh_pre * pre, double x)
2630{
2631
2632 XSH_ASSURE_NOT_NULL( pre);
2634
2635 /* Handle data unit + plist */
2636 check (cpl_image_divide_scalar(pre->data, x));
2637
2638 /* Handle errs unit + plist */
2639 check (cpl_image_divide_scalar(pre->errs, fabs(x)));
2640
2641 cleanup:
2642 return;
2643}
2644
2651void xsh_pre_add_scalar (const xsh_pre * pre, double x)
2652{
2653
2654 XSH_ASSURE_NOT_NULL( pre);
2656
2657 /* Handle data unit + plist */
2658 check (cpl_image_add_scalar(pre->data, x));
2659
2660 cleanup:
2661 return;
2662}
2663
2664
2670void xsh_pre_subtract_scalar (const xsh_pre * pre, double x)
2671{
2672
2673 XSH_ASSURE_NOT_NULL( pre);
2675
2676 /* Handle data unit + plist */
2677 check (cpl_image_subtract_scalar(pre->data, x));
2678
2679 cleanup:
2680 return;
2681}
2682
2689void xsh_pre_multiply_image(const xsh_pre *pre, cpl_image* img)
2690{
2691 cpl_image *abs = NULL;
2692
2693 XSH_ASSURE_NOT_NULL( pre);
2694 XSH_ASSURE_NOT_NULL( img);
2695
2696 /* Handle data unit + plist */
2697 check ( cpl_image_multiply(pre->data, img));
2698 check( abs = cpl_image_abs_create( img));
2699 check ( cpl_image_multiply(pre->errs, abs));
2700
2701 cleanup:
2702 xsh_free_image( &abs);
2703 return;
2704}
2705
2706/*----------------------------------------------------------------------------*/
2713cpl_image* xsh_pre_abs (const xsh_pre * pre)
2714{
2715
2716 cpl_image *result = NULL;
2717 int *result_data = NULL;
2718 float *data = NULL;
2719 int i, size;
2720
2721 XSH_ASSURE_NOT_NULL( pre);
2722
2723 /* Handle data unit + plist */
2724 check ( data = cpl_image_get_data_float( pre->data));
2725 check( result = cpl_image_new( pre->nx, pre->ny, CPL_TYPE_INT));
2726 check( result_data = cpl_image_get_data_int( result));
2727
2728 size = pre->nx*pre->ny;
2729
2730 for(i =0; i < size; i++){
2731 if ( data[i] < 0){
2732 result_data[i] = -1;
2733 data[i] = -data[i];
2734 }
2735 else{
2736 result_data[i] = 1;
2737 }
2738 }
2739
2740 cleanup:
2741 return result;
2742}
2743
2744/*---------------------------------------------------------------------------*/
2753/*---------------------------------------------------------------------------*/
2754
2755cpl_frame*
2756xsh_frameset_average_pre(cpl_frameset *set,
2757 xsh_instrument* instr,
2758 const char* tag)
2759{
2760 cpl_frame* result=NULL;
2761 cpl_frame* frame=NULL;
2762 cpl_image* data=NULL;
2763 cpl_image* errs=NULL;
2764 cpl_image* qual=NULL;
2765
2766 xsh_pre* pre=NULL;
2767 cpl_imagelist* iml_data=NULL;
2768 cpl_imagelist* iml_errs=NULL;
2769 cpl_imagelist* iml_qual=NULL;
2770
2771 cpl_propertylist* plist=NULL;
2772 char name_o[256];
2773 const char* name=NULL;
2774 cpl_frame* frm_tmp=NULL;
2775 int i=0;
2776 int size=0;
2777 //int nx=0;
2778 //int ny=0;
2779
2780 size=cpl_frameset_get_size(set);
2781 iml_data=cpl_imagelist_new();
2782 iml_errs=cpl_imagelist_new();
2783 iml_qual=cpl_imagelist_new();
2784
2785 for(i=0;i<size;i++) {
2786 frame=cpl_frameset_get_frame(set,i);
2787 pre=xsh_pre_load(frame,instr);
2788 //nx=pre->nx;
2789 //ny=pre->ny;
2790 cpl_imagelist_set(iml_data,cpl_image_duplicate(pre->data),i);
2791 cpl_imagelist_set(iml_errs,cpl_image_duplicate(pre->errs),i);
2792 cpl_imagelist_set(iml_qual,cpl_image_duplicate(pre->qual),i);
2793 xsh_pre_free(&pre);
2794 }
2795
2796 //pre=xsh_pre_new(nx,ny);
2797 pre=xsh_pre_load(cpl_frameset_get_frame(set,0),instr);
2798 xsh_free_image(&(pre->data));
2799 xsh_free_image(&(pre->errs));
2800 xsh_free_image(&(pre->qual));
2801 pre->data=cpl_imagelist_collapse_create(iml_data);
2802 pre->errs=cpl_imagelist_collapse_create(iml_errs);
2803 pre->qual=cpl_imagelist_collapse_create(iml_qual);
2804
2805 cpl_image_divide_scalar(pre->data,size);
2806 cpl_image_divide_scalar(pre->errs,size);
2807 cpl_image_divide_scalar(pre->qual,size);
2808
2809 frame=cpl_frameset_get_frame(set,0);
2810 name=cpl_frame_get_filename(frame);
2811 plist=cpl_propertylist_load(name,0);
2812
2813 sprintf(name_o,"%s.fits",tag);
2814 check(frm_tmp=xsh_pre_save(pre,name_o,tag,0));
2815
2816 result=xsh_frame_product(name_o,tag,CPL_FRAME_TYPE_IMAGE,
2817 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
2818
2819 cleanup:
2820 xsh_free_frame(&frm_tmp);
2822 xsh_free_image(&errs);
2823 xsh_free_image(&qual);
2824
2825 xsh_free_imagelist(&iml_data);
2826 xsh_free_imagelist(&iml_errs);
2827 xsh_free_imagelist(&iml_qual);
2828
2829 xsh_pre_free(&pre);
2830 xsh_free_propertylist(&plist);
2831
2832 return result;
2833}
2834
2842void xsh_pre_turn( xsh_pre * pre, int rot )
2843{
2844 cpl_image * img = NULL ;
2845
2846 XSH_ASSURE_NOT_NULL( pre ) ;
2847
2848 check( img = xsh_pre_get_data( pre ) ) ;
2849 check( cpl_image_turn( img, rot ) ) ;
2850
2851 check( img = xsh_pre_get_errs( pre ) ) ;
2852 check( cpl_image_turn( img, rot ) ) ;
2853
2854 check( img = xsh_pre_get_qual( pre ) ) ;
2855 check( cpl_image_turn( img, rot ) ) ;
2856
2857 cleanup:
2858 return ;
2859}
2860
2875void xsh_pre_extract( xsh_pre *pre, int xmin, int ymin, int xmax, int ymax)
2876{
2877 cpl_image * img = NULL ;
2878 cpl_image * ext_img = NULL ;
2879
2880 XSH_ASSURE_NOT_NULL( pre);
2881
2882 check( img = xsh_pre_get_data( pre));
2883
2884 XSH_ASSURE_NOT_NULL( img);
2885
2886 check( ext_img = cpl_image_extract( img, xmin, ymin, xmax, ymax));
2887 xsh_free_image( &img);
2888 pre->data = ext_img;
2889
2890 check( img = xsh_pre_get_errs( pre));
2891 check( ext_img = cpl_image_extract( img, xmin, ymin, xmax, ymax));
2892 xsh_free_image( &img);
2893 pre->errs = ext_img;
2894
2895 check( img = xsh_pre_get_qual( pre));
2896 check( ext_img = cpl_image_extract( img, xmin, ymin, xmax, ymax));
2897 xsh_free_image( &img);
2898 pre->qual = ext_img;
2899
2900 cleanup:
2901 return;
2902}
2911void xsh_pre_flip( xsh_pre * pre, int angle )
2912{
2913 cpl_image * img = NULL ;
2914
2915 XSH_ASSURE_NOT_NULL( pre ) ;
2916
2917 check( img = xsh_pre_get_data( pre ) ) ;
2918 check( cpl_image_flip( img, angle ) ) ;
2919
2920 check( img = xsh_pre_get_errs( pre ) ) ;
2921 check( cpl_image_flip( img, angle ) ) ;
2922
2923 check( img = xsh_pre_get_qual( pre ) ) ;
2924 check( cpl_image_flip( img, angle ) ) ;
2925
2926 cleanup:
2927 return ;
2928}
cpl_vector * xsh_irplib_imagelist_get_clean_mean_levels(const cpl_imagelist *iml, const double kappa, const int nclip, const double tolerance)
find out the character string associated to the DIT keyword in a propertylist
static xsh_instrument * instrument
int binx
int biny
void xsh_badpixelmap_or(xsh_pre *self, const xsh_pre *right)
cpl_error_code xsh_badpixelmap_flag_saturated_pixels(xsh_pre *pre, xsh_instrument *instr, const double cor_val, const int flag, const int is_flat, int *nsat)
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_badpixelmap_count_range_pixels(xsh_pre *pre, const double thresh_min, const double thresh_max, const double cor_val, int *nrange, double *frange)
void xsh_pre_add(xsh_pre *self, const xsh_pre *right)
Add two PRE images.
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
Definition: xsh_data_pre.c:953
void xsh_pre_flip(xsh_pre *pre, int angle)
Flip an image on a given miror line.
void xsh_pre_from_raw_get(xsh_pre *pre, double raw_x, double raw_y, double *x, double *y)
Get x, y coordinates in image from RAW coordinates.
double * xsh_pre_get_data_buffer(xsh_pre *pre)
Get data buffer.
double * xsh_pre_get_errs_buffer(xsh_pre *pre)
Get errs buffer.
cpl_frame * xsh_pre_save_product(xsh_pre *pre, cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, const char *prefix)
Save PRE frame like a product on disk.
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
void xsh_pre_extract(xsh_pre *pre, int xmin, int ymin, int xmax, int ymax)
Create a sub pre image.
cpl_mask * xsh_pre_get_bpmap(const xsh_pre *pre)
Get bpmap of pre structure.
cpl_frame_group xsh_pre_get_group(const xsh_pre *pre)
Get ny of pre structure.
const double * xsh_pre_get_errs_buffer_const(const xsh_pre *pre)
Get errs buffer.
void xsh_pre_divide(xsh_pre *self, const xsh_pre *right, const int is_flat, double threshold)
Divide a PRE image from another.
void xsh_pre_multiply_image(const xsh_pre *pre, cpl_image *img)
multiply a frame in PRE format by an image
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
void xsh_pre_divide_scalar(const xsh_pre *pre, double x)
divides a frame in PRE format by a scalar
int xsh_pre_get_ny(const xsh_pre *pre)
Get ny of pre structure.
int xsh_pre_window_best_median_flux_pos(xsh_pre *pre, int xcen, int ycen, int search_window_hsize, int running_median_hsize, int *xadj, int *yadj)
Search pixel position of best running median flux in the search window.
void xsh_pre_multiply_scalar(const xsh_pre *pre, double x)
multiply a frame in PRE format by a scalar
cpl_image * xsh_pre_abs(const xsh_pre *pre)
computes absolute value of a frame in PRE format
static void xsh_pre_init(xsh_pre *pre, cpl_propertylist *header, xsh_instrument *instrument)
Fill the XSH_PRE structure from FITS header file.
Definition: xsh_data_pre.c:318
float xsh_pre_get_pszx(const xsh_pre *pre)
Get pszx of pre structure.
void xsh_pre_add_scalar(const xsh_pre *pre, double x)
add a scalar to a frame in PRE format
int xsh_pre_get_binx(const xsh_pre *pre)
Get binx of pre structure.
void xsh_pre_turn(xsh_pre *pre, int rot)
cpl_frame * xsh_pre_frame_subtract(cpl_frame *one, cpl_frame *two, const char *filename, xsh_instrument *instr, const int clean_tmp)
Subtract 2 frames (in XSH_PRE format) Just loads the 2 frames, subtract (xsh_pre_subtract) and save r...
xsh_pre * xsh_pre_new(int nx, int ny)
Create new PRE image.
void xsh_pre_normalize(xsh_pre *self)
Normalize data flux.
void xsh_pre_multiply(xsh_pre *self, const xsh_pre *right, double threshold)
Multiply a PRE image from another.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
const cpl_image * xsh_pre_get_data_const(const xsh_pre *pre)
Get data.
int xsh_pre_get_biny(const xsh_pre *pre)
Get biny of pre structure.
static cpl_error_code xsh_preoverscan_corr(cpl_frame *raw, const int corr_mode, double *cor_val)
Definition: xsh_data_pre.c:61
xsh_pre * xsh_pre_create(cpl_frame *raw, cpl_frame *bpmap, cpl_image *bias_data, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
Create a XSH_PRE from a raw frame.
Definition: xsh_data_pre.c:450
int xsh_pre_get_nx(const xsh_pre *pre)
Get nx of pre structure.
float xsh_pre_get_pszy(const xsh_pre *pre)
Get pszy of pre structure.
double xsh_compute_ron_nir(const double dit)
Definition: xsh_data_pre.c:247
cpl_frame * xsh_frameset_average_pre(cpl_frameset *set, xsh_instrument *instr, const char *tag)
Average set of frames in PRE format.
xsh_pre * xsh_pre_subsample(xsh_pre *self, const int binx, const int biny, const int rescale, xsh_instrument *inst)
Subsample a PRE image of a bin factor binx X biny.
cpl_frameset * xsh_pre_frameset_subtract_frame(cpl_frameset *set, cpl_frame *sub, const char *spec, xsh_instrument *instr)
Subtract 2 frames (in XSH_PRE format) Just loads the 2 frames, subtract (xsh_pre_subtract) and save r...
void xsh_pre_subtract_scalar(const xsh_pre *pre, double x)
subtract a scalar from a frame in PRE format
double xsh_pre_data_window_median_flux_pa(xsh_pre *pre, int x, int y, int size_x, int size_y, double *tab, int *status)
compute median sample value on the data window with preallocated memory tab. Only good pixels codes a...
cpl_propertylist * xsh_pre_get_header(xsh_pre *pre)
Get header.
const cpl_propertylist * xsh_pre_get_header_const(const xsh_pre *pre)
Get header.
const double * xsh_pre_get_data_buffer_const(const xsh_pre *pre)
Get data buffer.
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...
float xsh_pre_get_gain(const xsh_pre *pre)
Get gain of pre structure.
const cpl_image * xsh_pre_get_qual_const(const xsh_pre *pre)
Get QUAL.
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.
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)
const cpl_image * xsh_pre_get_errs_const(const xsh_pre *pre)
Get errs.
void xsh_pre_dump(const xsh_pre *pre, FILE *stream)
Dump image to stream.
void xsh_pre_median_mean_stdev(xsh_pre *preFrame, double *mean, double *median, double *stdev)
#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 XSH_CMP_INT(A, OPERATOR, B, SUFFIX,...)
Definition: xsh_error.h:119
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define xsh_error_reset()
Definition: xsh_error.h:87
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
Definition: xsh_error.h:111
#define xsh_error_msg(...)
Definition: xsh_error.h:94
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
#define passure(CONDITION,...)
Definition: xsh_error.h:82
int xsh_instrument_get_binx(xsh_instrument *instrument)
int xsh_instrument_get_biny(xsh_instrument *instrument)
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
int size
int * y
int * x
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
void xsh_pfits_set_arm(cpl_propertylist *plist, xsh_instrument *instr)
Definition: xsh_pfits.c:988
int xsh_pfits_get_ovscx(const cpl_propertylist *plist)
find out the OVSCX value
Definition: xsh_pfits.c:391
double xsh_pfits_get_pszx(const cpl_propertylist *plist)
find out the PSZX (size of 1 pixel in Microns)
Definition: xsh_pfits.c:1765
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
double xsh_pfits_get_conad(const cpl_propertylist *plist)
find out the CONAD value
Definition: xsh_pfits.c:692
void xsh_pfits_set_dpr_tech(cpl_propertylist *plist, const char *value)
Write the DPR TECH value.
Definition: xsh_pfits.c:1059
int xsh_pfits_get_binx(const cpl_propertylist *plist)
find out the BINX value
Definition: xsh_pfits.c:289
const char * xsh_pfits_get_extname(const cpl_propertylist *plist)
find out the EXTNAME
Definition: xsh_pfits.c:1707
double xsh_pfits_get_det_gain(const cpl_propertylist *plist)
find out the ESO DET OUT1 GAIN value
Definition: xsh_pfits.c:730
double xsh_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
Definition: xsh_pfits.c:1405
void xsh_pfits_set_extname(cpl_propertylist *plist, const char *value)
Write the EXTNAME value.
Definition: xsh_pfits.c:979
void xsh_pfits_set_nsat(cpl_propertylist *plist, int value)
Write the number of saturated pixels value.
Definition: xsh_pfits.c:1076
cpl_error_code xsh_pfits_set_wcs(cpl_propertylist *header, const double crpix1, const double crval1, const double cdelt1, const double crpix2, const double crval2, const double cdelt2)
Definition: xsh_pfits.c:4364
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
Definition: xsh_pfits.c:1627
void xsh_pfits_set_frac_sat(cpl_propertylist *plist, double value)
Write the fraction of saturated pixels value.
Definition: xsh_pfits.c:1097
int xsh_pfits_get_ovscy(const cpl_propertylist *plist)
find out the OVSCY value
Definition: xsh_pfits.c:408
void xsh_pfits_set_frac_range_pix(cpl_propertylist *plist, double value)
Write the fraction of range pixels value.
Definition: xsh_pfits.c:1176
void xsh_pfits_set_n_range_pix(cpl_propertylist *plist, int value)
Write the number of range pixels value.
Definition: xsh_pfits.c:1156
double xsh_pfits_get_pszy(const cpl_propertylist *plist)
find out the PSZY (size of 1 pixel in Microns)
Definition: xsh_pfits.c:1784
int xsh_pfits_get_biny(const cpl_propertylist *plist)
find out the BINY value
Definition: xsh_pfits.c:306
void xsh_pfits_set_bunit(cpl_propertylist *plist, const char *value)
Write the BUNIT value.
Definition: xsh_pfits.c:2606
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
Definition: xsh_pfits.c:2254
int xsh_pfits_is_flat(cpl_propertylist *plist)
Definition: xsh_pfits.c:4579
int xsh_pfits_get_prscx(const cpl_propertylist *plist)
find out the PRSCX value
Definition: xsh_pfits.c:425
double xsh_pfits_get_ron(const cpl_propertylist *plist)
find out the RON value
Definition: xsh_pfits.c:459
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: xsh_pfits.c:227
int xsh_pfits_get_prscy(const cpl_propertylist *plist)
find out the PRSCY value
Definition: xsh_pfits.c:442
double xsh_pfits_get_det_pxspace(const cpl_propertylist *plist)
find out the DET CHIP PXSPACE
Definition: xsh_pfits.c:1803
int xsh_pfits_get_naxis2(const cpl_propertylist *plist)
find out the NAXIS2 value
Definition: xsh_pfits.c:244
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
Definition: xsh_utils.c:2345
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
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_stats(cpl_stats **s)
Deallocate a stats object and set the pointer to NULL.
Definition: xsh_utils.c:2314
char * xsh_get_basename(const char *filename)
Return base filename.
Definition: xsh_utils.c:1175
int xsh_time_stamp_get(void)
get timestamp
Definition: xsh_utils.c:3196
char * xsh_sdate_utc(time_t *now)
Definition: xsh_utils.c:1691
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
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
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
const char * pipeline_id
const char * dictionary
int decode_bp
Definition: xsh_data_pre.h:98
int naxis2
Definition: xsh_data_pre.h:78
int cutmy
Definition: xsh_data_pre.h:86
double ron
Definition: xsh_data_pre.h:94
cpl_image * qual
Definition: xsh_data_pre.h:71
int cuty
Definition: xsh_data_pre.h:84
float exptime
Definition: xsh_data_pre.h:92
cpl_frame_group group
Definition: xsh_data_pre.h:74
float pszy
Definition: xsh_data_pre.h:88
double conad
Definition: xsh_data_pre.h:96
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
int naxis1
Definition: xsh_data_pre.h:78
xsh_instrument * instrument
Definition: xsh_data_pre.h:76
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
float pszx
Definition: xsh_data_pre.h:88
int cutx
Definition: xsh_data_pre.h:84
float gain
Definition: xsh_data_pre.h:90
int cutmx
Definition: xsh_data_pre.h:86
#define QFLAG_OUTSIDE_DATA_RANGE
#define QFLAG_CALIB_FILE_DEFECT
#define QFLAG_DIVISOR_ZERO
#define QFLAG_SATURATED_DATA
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
#define XSH_PRE_DATA_TYPE
Definition: xsh_data_pre.h:42
#define XSH_PRE_QUAL_TYPE
Definition: xsh_data_pre.h:46
#define XSH_NIR_DEFAULT_CUT_Y
Definition: xsh_data_pre.h:60
#define XSH_PRE_ERRS_BPP
Definition: xsh_data_pre.h:45
#define XSH_PRE_QUAL_BPP
Definition: xsh_data_pre.h:47
#define XSH_PRE_ERRS_TYPE
Definition: xsh_data_pre.h:44
#define XSH_NIR_DEFAULT_CUT_X
Definition: xsh_data_pre.h:59
#define XSH_NIR_DEFAULT_CUT
Definition: xsh_data_pre.h:58
#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
cpl_boolean rescale
int threshold
Definition: xsh_detmon_lg.c:90
int llx
Definition: xsh_detmon_lg.c:85
int m
Definition: xsh_detmon_lg.c:91
int urx
Definition: xsh_detmon_lg.c:87
int ury
Definition: xsh_detmon_lg.c:88
int ny
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_BIAS_UVB
Definition: xsh_dfs.h:394
#define XSH_BP_MAP_NL_UVB
Definition: xsh_dfs.h:361
#define XSH_BIAS_VIS
Definition: xsh_dfs.h:395
#define XSH_RAW_BP_MAP_NL_UVB
Definition: xsh_dfs.h:366
#define XSH_RAW_IMA_SLIT_UVB
Definition: xsh_dfs.h:243
#define XSH_LINEARITY_UVB
Definition: xsh_dfs.h:383
#define XSH_RAW_IMA_SLIT_VIS
Definition: xsh_dfs.h:242
#define XSH_RAW_BP_MAP_NL_VIS
Definition: xsh_dfs.h:367
#define XSH_LINEARITY_VIS
Definition: xsh_dfs.h:384
#define XSH_BP_MAP_NL_VIS
Definition: xsh_dfs.h:362
#define XSH_BUNIT_NONE_C
Definition: xsh_pfits.h:116
#define XSH_DPR_TECH_SINGLE_PINHOLE
Definition: xsh_pfits.h:219
#define XSH_BUNIT_FLUX_REL_C
Definition: xsh_pfits.h:115
#define XSH_DPR_TYPE
Definition: xsh_pfits.h:147
#define XSH_NEW_PROPERTYLIST(POINTER)
Definition: xsh_utils.h:70
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56