X-shooter Pipeline Reference Manual 3.8.15
xsh_combine_nod.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 $
23 * $Revision: 1.25 $
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*----------------------------------------------------------------------------*/
37/*----------------------------------------------------------------------------*/
40/*-----------------------------------------------------------------------------
41 Includes
42 -----------------------------------------------------------------------------*/
43
44#include <math.h>
45#include <xsh_drl.h>
46
47#include <xsh_badpixelmap.h>
48#include <xsh_utils_image.h>
49#include <xsh_data_pre.h>
50#include <xsh_data_order.h>
51#include <xsh_data_wavemap.h>
53#include <xsh_data_rec.h>
54#include <xsh_dfs.h>
55#include <xsh_pfits.h>
56#include <xsh_error.h>
57#include <xsh_msg.h>
58#include <xsh_fit.h>
59#include <xsh_badpixelmap.h>
60
61#include <cpl.h>
62#define combine_nod_old 0
63/*-----------------------------------------------------------------------------
64 Functions prototypes
65 -----------------------------------------------------------------------------*/
66
67/*-----------------------------------------------------------------------------
68 Implementation
69 -----------------------------------------------------------------------------*/
70
71
72void xsh_compute_slit_index( float slit_min, float slit_step,
73 xsh_rec_list **from, int *slit_index_tab, int size)
74{
75 int i;
76
78 XSH_ASSURE_NOT_NULL( slit_index_tab);
79
80 for( i=0; i< size; i++){
81 xsh_rec_list *list = NULL;
82 float* nod_slit = NULL;
83
84 list = from[i];
85 check( nod_slit = xsh_rec_list_get_slit( list, 0));
86 slit_index_tab[i] = (int)xsh_round_double((nod_slit[0]-slit_min)/slit_step);
87 }
88 cleanup:
89 return;
90}
91
92/* TODO: NOT USED
93static void
94xsh_rec_list_add_scaled(xsh_rec_list *dest, xsh_rec_list **from,
95 int *slit_index, int nb_frames, int no,
96 int method, const int decode_bp)
97{
98 int nslit, from_slit, nlambda;
99 float *dest_data1 = NULL, *dest_errs1 = NULL;
100 int *dest_qual1 = NULL;
101 float *from_data1 = NULL, *from_errs1 = NULL;
102 int *from_qual1 = NULL;
103 int ns, nl, nf;
104 double *flux_tab = NULL;
105 cpl_vector *flux_vect = NULL;
106 double *err_tab = NULL;
107 cpl_vector *err_vect = NULL;
108 cpl_imagelist* all_iml;
109 cpl_imagelist* flag_iml;
110 cpl_image* all_ima;
111 cpl_image* flag_ima;
112 cpl_image* tmp_ima;
113 cpl_image* scale_ima;
114 float* pall_ima;
115 float* pflag_ima;
116 //float* pscale_ima;
117 double scale;
118 int sx=1;
119 int sy=5;
120 cpl_mask* bpm;
121 cpl_binary* pbpm;
122 int pix=0;
123 int i,j;
124 int nx=2*sx+1;
125 int ny=2*sy+1;
126 XSH_ASSURE_NOT_NULL( dest);
127 XSH_ASSURE_NOT_NULL( from);
128 XSH_ASSURE_NOT_NULL( slit_index);
129
130 xsh_msg_dbg_medium(
131 "xsh_rec_list_add: nb frames: %d, order: %d", nb_frames, no);
132
133 check( nslit = xsh_rec_list_get_nslit( dest, no));
134 check( from_slit = xsh_rec_list_get_nslit( from[0], no));
135 check( nlambda = xsh_rec_list_get_nlambda( dest, no));
136
137 check( dest_data1 = xsh_rec_list_get_data1( dest, no));
138 check( dest_errs1 = xsh_rec_list_get_errs1( dest, no));
139 check( dest_qual1 = xsh_rec_list_get_qual1( dest, no));
140
141 XSH_MALLOC( flux_tab, double, nb_frames);
142 XSH_MALLOC ( err_tab, double , nb_frames);
143
144 for (nf = 0; nf < nb_frames; nf++) {
145 xsh_msg_dbg_high(
146 "slit index: max %d min=%d", slit_index[nf], slit_index[nf]+from_slit);
147 }
148 //xsh_msg("nslit=%d nlambda=%d",nslit,nlambda);
149
150 all_iml=cpl_imagelist_new();
151 flag_iml=cpl_imagelist_new();
152
153 for (nf = 0; nf < nb_frames; nf++) {
154 all_ima=cpl_image_new(nx,ny,CPL_TYPE_FLOAT);
155 cpl_imagelist_set(all_iml,cpl_image_duplicate(all_ima),nf);
156 cpl_imagelist_set(flag_iml,cpl_image_duplicate(all_ima),nf);
157 cpl_image_delete(all_ima);
158 }
159
160 for (ns = 0; ns < nslit; ns++) {
161 for (nl = 0; nl < nlambda; nl++) {
162 int dest_idx;
163 int good = 0, bad = 0;
164 //int isum =0;
165 int x_min=-sx, x_max=sx, y_min=-sy, y_max=sy;
166 //double dest_err = 0, dest_data = 0. ;
167 //double bad_err = 0, bad_data = 0;
168 unsigned int qflag = QFLAG_GOOD_PIXEL;
169 unsigned int badflag = QFLAG_GOOD_PIXEL;
170
171 dest_idx = nl + ns * nlambda;
172 //xsh_msg("ok4");
173
174 if (y_min < 0) {
175 y_min = 0;
176 y_max = ny;
177 } else if (ns+y_max > nslit) {
178 y_max=nslit;
179 y_min=y_max-ny;
180 }
181
182 if (x_min < 0) {
183 x_min = 0;
184 x_max = nx;
185 } else if (nl+x_max > nlambda) {
186 x_max=nlambda;
187 x_min=x_max-nx;
188 }
189
190 for (nf = 0; nf < nb_frames; nf++) {
191 int from_idx;
192
193 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
194 continue;
195 }
196 from_idx = nl + (ns - slit_index[nf]) * nlambda;
197 check( from_data1 = xsh_rec_list_get_data1( from[nf], no));
198 check( from_errs1 = xsh_rec_list_get_errs1( from[nf], no));
199 check( from_qual1 = xsh_rec_list_get_qual1( from[nf], no));
200 //xsh_msg("ok6");
201
202 tmp_ima=cpl_imagelist_get(all_iml,nf);
203 pall_ima=cpl_image_get_data_float(tmp_ima);
204 tmp_ima=cpl_imagelist_get(flag_iml,nf);
205 pflag_ima=cpl_image_get_data_float(tmp_ima);
206 bpm=cpl_image_get_bpm(tmp_ima);
207
208 //xsh_msg("ok7 x_min=%d x_max=%d y_min=%d y_max=%d",x_min,x_max,y_min,y_max);
209
210 for(j=y_min; j < y_max; j++) {
211 for(i=x_min; i < x_max; i++) {
212 pix=j*sx+i;
213
214 //xsh_msg("i=%d j=%d pix=%d size_ima=%d nf=%d slit_index[nf]=%d ns=%d nl=%d from_idx=%d from_idx+pix=%d",
215 // i,j,pix,nx*ny,nf,slit_index[nf],ns,nl,from_idx,from_idx+pix);
216
217 pall_ima[pix]=from_data1[from_idx+pix]; // Invalid Write of size 4
218 pflag_ima[pix]=from_data1[from_idx+pix];// Invalid Write of size 4
219 //xsh_msg("all[%d,%d]=%g flag=%g",i,j,pall_ima[pix],pflag_ima[pix]);
220 }
221 }
222
223 pbpm=cpl_mask_get_data(bpm);
224 // previous implementation separates good and bad pixels
225 if ((from_qual1[from_idx] & decode_bp) == 0) {
226 qflag |= from_qual1[from_idx];
227 flux_tab[good] = from_data1[from_idx];
228 err_tab[good] = from_errs1[from_idx];
229 good++;
230
231 } else if ((from_qual1[from_idx] & decode_bp) > 0) {
232 flux_tab[good + bad] = from_data1[from_idx];
233 err_tab[good + bad] = from_errs1[from_idx];
234 badflag |= from_qual1[from_idx];
235 for(j=y_min; j < y_max; j++) {
236 for(i=x_max; i < x_min; i++) {
237 //flag all plane
238 pbpm[j*sx+i]=CPL_BINARY_1;
239 }
240 }
241 bad++;
242 }
243
244 }
245
246 // compute scale factors
247
248 all_ima = cpl_imagelist_collapse_create(all_iml);
249 flag_ima = cpl_imagelist_collapse_create(flag_iml);
250
251 //cpl_image_save(all_ima, "all_ima.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
252 //cpl_image_save(flag_ima, "flag_ima.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
253 scale_ima=cpl_image_duplicate(all_ima);
254 cpl_image_divide(scale_ima,flag_ima);
255
256 scale=cpl_image_get_mean(scale_ima);
257 //cpl_image_save(scale_ima, "scale_ima.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
258 xsh_msg("scale=%g",scale);
259 cpl_image_delete(all_ima);
260 cpl_image_delete(flag_ima);
261 cpl_image_delete(scale_ima);
262
263 // Dont calculate mean value, just sum
264 // previous implementation separates good and bad pixels
265 if (good == 0) {
266 check( flux_vect = cpl_vector_wrap( bad, flux_tab));
267 check( err_vect = cpl_vector_wrap( bad, err_tab));
268 // No good pixel
269 dest_qual1[dest_idx] |= badflag;
270 scale=1;
271 } else {
272 dest_qual1[dest_idx] |= qflag;
273 if(bad > 0) {
274 dest_qual1[dest_idx] |= QFLAG_INCOMPLETE_DATA;
275 } else {
276 scale=1;
277 }
278 check( flux_vect = cpl_vector_wrap( good, flux_tab));
279 check( err_vect = cpl_vector_wrap( good, err_tab));
280 }
281
282 if (method == COMBINE_MEAN_METHOD) {
283 check( dest_data1[dest_idx] = cpl_vector_get_mean( flux_vect) *scale );
284 check( dest_errs1[dest_idx] = xsh_vector_get_err_mean( err_vect));
285 } else {
286 check( dest_data1[dest_idx] = cpl_vector_get_median( flux_vect) *scale);
287 check( dest_errs1[dest_idx] = xsh_vector_get_err_median( err_vect));
288 }
289
290 xsh_unwrap_vector(&flux_vect);
291 xsh_unwrap_vector(&err_vect);
292 }
293 }
294
295 cleanup:
296 if (cpl_error_get_code() != CPL_ERROR_NONE) {
297 xsh_unwrap_vector(&flux_vect);
298 xsh_unwrap_vector(&err_vect);
299 }
300
301 XSH_FREE( flux_tab);
302 XSH_FREE( err_tab);
303
304 xsh_print_rec_status(15);
305 return;
306}
307 */
308/* TODO: NOT USED YET
309static cpl_image*
310xsh_rec_list_compute_scale(xsh_rec_list *dest, xsh_rec_list **from,
311 int *slit_index, int nb_frames, int no,
312 int method, const int decode_bp)
313{
314
315 cpl_image* scale;
316 float *from_data1 = NULL;
317 int *from_qual1 = NULL;
318 float *dest_data1 = NULL;
319 int *dest_qual1 = NULL;
320 int nslit, from_slit, nlambda;
321 int ns, nl, nf;
322 XSH_ASSURE_NOT_NULL( dest);
323 XSH_ASSURE_NOT_NULL( from);
324 XSH_ASSURE_NOT_NULL( slit_index);
325
326 nslit = xsh_rec_list_get_nslit( dest, no);
327 from_slit = xsh_rec_list_get_nslit( from[0], no);
328 nlambda = xsh_rec_list_get_nlambda( dest, no);
329
330 dest_data1 = xsh_rec_list_get_data1( dest, no);
331 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
332
333 //cpl_image* from_ima_tmp;
334 //cpl_imagelist* from_iml;
335 scale=cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
336 float sum_good_pix;
337 float sum_tot_pix;
338 int num_good_pix;
339 int num_tot_pix;
340 int win_hsx=3;
341 int win_hsy=3;
342 int win_sx=2*win_hsx+1;
343 int win_sy=2*win_hsy+1;
344 float sum_all;
345 float sum_good;
346 cpl_imagelist* iml_good;
347 cpl_imagelist* iml_all;
348 cpl_imagelist* iml_copy;
349 cpl_image* ima_tmp;
350 int frame_bad;
351 float* pdata;
352 float* pscale=cpl_image_get_data_float(scale);
353
354
355
356 for (ns = 0; ns < nslit; ns++) {
357 for (nl = 0; nl < nlambda; nl++) {
358 int dest_idx;
359 int good = 0, bad = 0;
360 int isum =0;
361
362 dest_idx = nl + ns * nlambda;
363
364 for (nf = 0; nf < nb_frames; nf++) {
365 int from_idx;
366
367 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
368 continue;
369 }
370 from_idx = nl + (ns - slit_index[nf]) * nlambda;
371 from_data1 = xsh_rec_list_get_data1( from[nf], no);
372 from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
373
374
375 }
376 }
377 }
378 cleanup:
379 return scale;
380
381}
382 */
383
384/*
385static cpl_error_code
386xsh_compute_scale_tab4(cpl_imagelist* iml_data, cpl_table* tab_bpm,
387 cpl_imagelist* scale_frames,cpl_image** ima_corr)
388{
389 xsh_msg("start of function");
390 cpl_image* scale_ima;
391 int sx_j;
392 int pix;
393
394 int sz=cpl_imagelist_get_size(iml_data);
395 double norm_good_pix;
396 double* pdata;
397 cpl_image* ima_tmp;
398 cpl_binary* pbpm;
399
400 cpl_mask* bpm_tmp;
401
402 ima_tmp=cpl_imagelist_get(iml_data,0);
403 int sx=cpl_image_get_size_x(ima_tmp);
404 //int sy=cpl_image_get_size_y(ima_tmp);
405
406
407 int* px=cpl_table_get_data_int(tab_bpm,"x");
408 int* py=cpl_table_get_data_int(tab_bpm,"y");
409 int size=cpl_table_get_nrow(tab_bpm);
410 int num_good_pix;
411
412 int i,j,m;
413 char name[80];
414 double* pscale;
415 double* pcor=cpl_image_get_data_double(*ima_corr);
416 for(m=0;m<size;m++) {
417
418 i=px[m];
419 j=py[m];
420 xsh_msg("j=%d",j);
421 xsh_msg("i=%d",i);
422 sx_j=sx*j;
423 pix=sx_j+i;
424
425 //xsh_msg("pix=%d",pix);
426
427 norm_good_pix=0;
428 num_good_pix=0;
429
430 // to sum only over good pixels we flag the bad ones
431 for(int k=0;k<sz;k++) {
432 ima_tmp=cpl_imagelist_get(iml_data,k);
433 bpm_tmp=cpl_image_get_bpm(ima_tmp);
434 pbpm=cpl_mask_get_data(bpm_tmp);
435 sprintf(name,"bad_%d.fits",k);
436 cpl_mask_save(bpm_tmp,name,NULL,CPL_IO_DEFAULT);
437
438 if( pbpm[pix] == CPL_BINARY_0 ) {
439 pdata=cpl_image_get_data_double(ima_tmp);
440 //xsh_msg("k=%d j=%d pdata=%g",k,j,pdata[pix]);
441 scale_ima=cpl_imagelist_get(scale_frames,k);
442 pscale=cpl_image_get_data_double(scale_ima);
443 //xsh_msg("k=%d j=%d pscale=%g",k,j,pscale[j]);
444 norm_good_pix += pdata[pix]/pscale[j];
445 num_good_pix++;
446 //xsh_msg("k=%d j=%d ratio=%g",k,j,pdata[pix]/pscale[j]);
447
448 }
449
450
451 }
452 norm_good_pix /= num_good_pix;
453 xsh_msg("m=%d norm=%g num=%d",m,norm_good_pix,num_good_pix);
454 pcor[pix] = norm_good_pix;
455
456 }
457 xsh_msg("end of function");
458 return cpl_error_get_code();
459}
460 */
461
462static cpl_imagelist*
463xsh_compute_scale_factors_by_object(cpl_imagelist* scaled, cpl_image* scale,
464 const int nlambda,const int nslit,const int no,const double smin,
465 const double smax)
466{
467
468 cpl_imagelist* scale_factors = cpl_imagelist_new();
469
470 double med;
471 //double avg;
472 //double rms;
473 double* pscale;
474 int nb_frames=cpl_imagelist_get_size(scaled);
475 cpl_image* ima_tmp;
476 int slit_min = (int) smin;
477 int slit_max = (int) smax;
478
479 int nbad;
480 const cpl_mask* mask;
481 for (int nf = 0; nf < nb_frames; nf++) {
482 scale=cpl_image_new(1,nslit,CPL_TYPE_DOUBLE);
483 cpl_image_add_scalar(scale,1.);
484 pscale=cpl_image_get_data_double(scale);
485
486
487 ima_tmp=cpl_imagelist_get(scaled,nf);
488 mask=cpl_image_get_bpm_const(ima_tmp);
489 nbad=cpl_mask_count_window(mask,1,slit_min,nlambda,slit_max);
490
491 if( nbad < nlambda * (slit_max-slit_min) ) {
492 //avg=cpl_image_get_mean_window(ima_tmp,1,slit_min,nlambda,slit_max);
493 med=cpl_image_get_median_window(ima_tmp,1,slit_min,nlambda,slit_max);
494 //rms=cpl_image_get_stdev_window(ima_tmp,1,slit_min,nlambda,slit_max);
495 /*
496 xsh_msg("frame %d slice %d mean=%g median=%g rm==%g",
497 nf, ns, avg,med,rms);
498 */
499 }
500 //xsh_msg("order %d frame %d slice %d scale med=%g",no, nf, ns, med);
501 for (int ns = slit_min; ns <= slit_max; ns++) {
502 pscale[ns]=med;
503 }
504
505 cpl_imagelist_set(scale_factors,scale,nf);
506 }
507
508 char fname[80];
509 sprintf(fname,"scale_factors_obj_%d.fits",no);
510 cpl_imagelist_save(scale_factors,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_DEFAULT);
511 //cpl_imagelist_delete(scaled);
512 return scale_factors;
513
514}
515
516static cpl_imagelist*
517xsh_compute_scale_factors_by_slice(cpl_imagelist* scaled, cpl_image* scale,
518 const int nlambda,const int nslit,const int no)
519{
520
521 cpl_imagelist* scale_factors = cpl_imagelist_new();
522
523 double med;
524 //double rms;
525 //double avg;
526 double* pscale;
527 int nb_frames=cpl_imagelist_get_size(scaled);
528 cpl_image* ima_tmp;
529 int nbad;
530 const cpl_mask* mask;
531 for (int nf = 0; nf < nb_frames; nf++) {
532 scale=cpl_image_new(1,nslit,CPL_TYPE_DOUBLE);
533 cpl_image_add_scalar(scale,1.);
534 pscale=cpl_image_get_data_double(scale);
535 for (int ns = 0; ns < nslit; ns++) {
536
537 ima_tmp=cpl_imagelist_get(scaled,nf);
538 mask=cpl_image_get_bpm_const(ima_tmp);
539 nbad=cpl_mask_count_window(mask,1,ns+1,nlambda,ns+1);
540
541 if(nbad<nlambda) {
542 //avg=cpl_image_get_mean_window(ima_tmp,1,ns+1,nlambda,ns+1);
543 med=cpl_image_get_median_window(ima_tmp,1,ns+1,nlambda,ns+1);
544 //rms=cpl_image_get_stdev_window(ima_tmp,1,ns+1,nlambda,ns+1);
545 /*
546 xsh_msg("frame %d slice %d mean=%g median=%g rm==%g",
547 nf, ns, avg,med,rms);
548 */
549 }
550 //xsh_msg("order %d frame %d slice %d scale med=%g",no, nf, ns, med);
551 pscale[ns]=med;
552 }
553 cpl_imagelist_set(scale_factors,scale,nf);
554 }
555
556 char fname[80];
557 sprintf(fname,"scale_factors_slices_%d.fits",no);
558 cpl_imagelist_save(scale_factors,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_DEFAULT);
559 //cpl_imagelist_delete(scaled);
560
561 return scale_factors;
562
563}
564
565
566static cpl_imagelist*
567xsh_compute_scale_factors_pix_pix(cpl_imagelist* scaled, cpl_image* scale,
568 const int nlambda,const int nslit,const int no)
569{
570
571 cpl_imagelist* scale_factors = cpl_imagelist_new();
572
573 double med=0;
574 //double rms=0;
575 //double avg=0;
576 double* pscale;
577 cpl_binary* pbpm=NULL;
578 cpl_mask* bpm;
579 int nb_frames=cpl_imagelist_get_size(scaled);
580 cpl_image* ima_tmp;
581 int nbad;
582 const cpl_mask* mask;
583 //xsh_msg("nlambda=%d",nlambda);
584
585
586 int hbox_sz=6;// was 6
587 int lmin=0, lmax=0;
588 //int sx=0;
589 //int sy=0;
590 for (int nf = 0; nf < nb_frames; nf++) {
591 ima_tmp=cpl_imagelist_get(scaled,nf);
592 mask=cpl_image_get_bpm_const(ima_tmp);
593 //sx=cpl_mask_get_size_x(mask);
594 //sy=cpl_mask_get_size_y(mask);
595 //xsh_msg("nlambda=%d",nlambda);
596 scale=cpl_image_new(nlambda,nslit,CPL_TYPE_DOUBLE);
597 cpl_image_add_scalar(scale,1.);
598 pscale=cpl_image_get_data_double(scale);
599 bpm=cpl_image_get_bpm(scale);
600 pbpm=cpl_mask_get_data(bpm);
601 // AMO TBD: for (int ns = 0; ns < nslit-1; ns++) {
602 for (int ns = 0; ns < nslit; ns++) {
603
604 for (int nl = hbox_sz; nl < nlambda-hbox_sz; nl++) {
605 lmin=nl-hbox_sz+1;
606 lmax=nl+hbox_sz+1;
607 /*
608
609
610 if(lmin <=1 || ns+1 <=1 || lmax>=sx || ns+1 >= sy) {
611 xsh_msg("lmin=%d lmax=%d ns+1=%d sx=%d sy=%d",lmin,lmax,ns+1,sx,sy);
612 }
613 */
614 check(nbad=cpl_mask_count_window(mask,lmin,ns+1,lmax,ns+1));
615
616 if(nbad<lmax-lmin) {
617 //avg=cpl_image_get_mean_window(ima_tmp,lmin,ns+1,lmax,ns+1);
618 med=cpl_image_get_median_window(ima_tmp,lmin,ns+1,lmax,ns+1);
619 //check(rms=cpl_image_get_stdev_window(ima_tmp,lmin,ns+1,lmax,ns+1));
620 /*
621 xsh_msg("frame %d slice %d mean=%g median=%g rm==%g",
622 nf, ns, avg,med,rms);
623 */
624 }
625
626 /*
627 if (no == 4 && nl > 1855 && nl < 1865 && ns > 39 && ns < 41) {
628 xsh_msg("pix pix: order %d frame %d scale[%d,%d] med=%g",
629 no, nf, nl, ns, med);
630 }
631 */
632
633
634 if(med != 0 ) {
635 pscale[ns*nlambda+nl]=med;
636 } else {
637 pbpm[ns*nlambda+nl]=CPL_BINARY_1;
638 /*
639 xsh_msg("lmin=%d lmax=%d ns=%d nslit=%d nlambda=%d",
640 lmin,lmax,ns,nslit,nlambda);
641 cpl_image_save(ima_tmp, "ima_tmp.fits", XSH_PRE_DATA_BPP,
642 NULL, CPL_IO_DEFAULT);
643 mask=cpl_image_get_bpm_const(ima_tmp);
644 cpl_mask_save(mask,"mask_tmp.fits",NULL,CPL_IO_DEFAULT);
645 exit(0);
646 */
647 }
648 }
649
650 /* wave edge regions */
651
652 for (int nl = 0; nl < hbox_sz; nl++) {
653 lmin=nl+1;
654 lmax=nl+2*hbox_sz+1;
655 lmax=(lmax < nlambda) ? lmax : nlambda-1;
656 //lmin=(lmin < lmax) ? lmin : lmax-hbox_sz;
657 lmin=(lmin < lmax) ? lmin : lmax-1;
658 /*
659 if( ns == (nslit-1) ) {
660 xsh_msg("lmin=%d lmax=%d sx=%d sy=%d ns=%d nslit=%d nlambda=%d",
661 lmin,lmax,sx,sy,ns,nslit,nlambda);
662 }
663 */
664 nbad=cpl_mask_count_window(mask,lmin,ns+1,lmax,ns+1);
665
666 if(nbad<lmax-lmin) {
667 //avg=cpl_image_get_mean_window(ima_tmp,lmin,ns+1,lmax,ns+1);
668 med=cpl_image_get_median_window(ima_tmp,lmin,ns+1,lmax,ns+1);
669
670 //check(rms=cpl_image_get_stdev_window(ima_tmp,lmin,ns+1,lmax,ns+1));
671 /*
672 xsh_msg("frame %d slice %d mean=%g median=%g rm==%g",
673 nf, ns, avg,med,rms);
674 */
675 }
676
677 //xsh_msg("order %d frame %d slice %d scale med=%g",no, nf, ns, med);
678 if(med != 0 ) {
679 pscale[ns*nlambda+nl]=med;
680 } else {
681 pbpm[ns*nlambda+nl]=CPL_BINARY_1;
682 }
683
684 }
685
686 for (int nl = nlambda-1; nl > nlambda-hbox_sz-1; nl--) {
687 lmax=nl+1;
688 lmin=nl-2*hbox_sz+1;
689 lmin=(lmin > 0) ? lmin : 1;
690 lmax=(lmax > lmin) ? lmax : lmin+1;
691 //xsh_msg("lmin=%d lmax=%d ns=%d nslit=%d nlambda=%d",lmin,lmax,ns,nslit,nlambda);
692 check(nbad=cpl_mask_count_window(mask,lmin,ns+1,lmax,ns+1));
693
694 if(nbad<lmax-lmin) {
695 //avg=cpl_image_get_mean_window(ima_tmp,lmin,ns+1,lmax,ns+1);
696 med=cpl_image_get_median_window(ima_tmp,lmin,ns+1,lmax,ns+1);
697
698 //check(rms=cpl_image_get_stdev_window(ima_tmp,lmin,ns+1,lmax,ns+1));
699 /*
700 xsh_msg("frame %d slice %d mean=%g median=%g rm==%g",
701 nf, ns, avg,med,rms);
702 */
703 }
704 //xsh_msg("order %d frame %d slice %d scale med=%g",no, nf, ns, med);
705 if(med != 0 ) {
706 pscale[ns*nlambda+nl]=med;
707 } else {
708 pbpm[ns*nlambda+nl]=CPL_BINARY_1;
709 }
710 }
711
712 } /* end loop over s */
713
714 cpl_imagelist_set(scale_factors,scale,nf);
715
716 }
717
718 char fname[80];
719 sprintf(fname,"scale_factors_pix_pix_%d.fits",no);
720 //cpl_imagelist_save(scale_factors,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_DEFAULT);
721 cleanup:
722 //cpl_imagelist_delete(scaled);
724 return scale_factors;
725
726}
727
728/* This routine creates a table containing for each pixels how many frames
729 * contributed. Pixels for wich at least one frame has a bad pixel are also
730 * flagged.
731*/
732static cpl_table*
733xsh_qual2tab_frames(int nframes,const int code, int *slit_index,
734 xsh_rec_list *dest, xsh_rec_list **from,const int no)
735{
736
737
738
739 int m=0;
740
741 int nf=0;
742 int ns=0;
743 int nl=0;
744
745
746 int *from_qual1 = NULL;
747 int from_idx=0;
748 int* dest_qual1 = xsh_rec_list_get_qual1( dest, no);
749 int nlambda = xsh_rec_list_get_nlambda( dest, no);
750 int nslit = xsh_rec_list_get_nslit( dest, no);
751 int from_slit = xsh_rec_list_get_nslit( from[0], no);
752 const int size=nlambda*nslit;
753 cpl_table* xy_pos=cpl_table_new(size);
754 cpl_table_new_column(xy_pos,"x",CPL_TYPE_INT);
755 cpl_table_new_column(xy_pos,"y",CPL_TYPE_INT);
756 cpl_table_new_column(xy_pos,"ngood",CPL_TYPE_INT);
757 char cname[8];
758
759 for(int i=0; i < nframes;i++) {
760 sprintf(cname,"f%d",i);
761 cpl_table_new_column(xy_pos,cname,CPL_TYPE_INT);
762 }
763 int* px=cpl_table_get_data_int(xy_pos,"x");
764 int* py=cpl_table_get_data_int(xy_pos,"y");
765 int* pg=cpl_table_get_data_int(xy_pos,"ngood");
766 int nbad=0;
767
768 //xsh_msg("copy bad pixels in table");
769 for (ns = 0; ns < nslit; ns++) {
770 for (nl = 0; nl < nlambda; nl++) {
771 int dest_idx = nl + ns * nlambda;
772 for(nf=0;nf<nframes;nf++) {
773 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
774 continue;
775 }
776 if ( (dest_qual1[dest_idx] & code) > 0) {
777 xsh_msg("found bad pixel at [%d,%d]",nl,ns);
778 px[m]=nl;
779 py[m]=ns;
780 from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
781 sprintf(cname,"f%d",nf);
782 int* pq=cpl_table_get_data_int(xy_pos,cname);
783 from_idx = nl + (ns - slit_index[nf]) * nlambda;
784 nbad=0;
785 if ( (from_qual1[from_idx] & code) > 0) {
786 pq[m]=1;
787 nbad++;
788 }
789 pg[m]=nframes-nbad;
790 m++;
791 }
792
793 }
794 }
795 }
796 cpl_table_set_size(xy_pos,m);
797 char fname[80];
798 sprintf(fname,"bad_pix_ord_%d.fits",no);
799 cpl_table_save(xy_pos,NULL,NULL,fname,CPL_IO_DEFAULT);
800 return xy_pos;
801}
802
803static cpl_imagelist*
805 int *slit_index, int nb_frames, int no,
806 int method, const int decode_bp, int scale_method)
807{
808
809 cpl_image* scale=NULL;
810
811 int nslit, from_slit, nlambda;
812 nslit = xsh_rec_list_get_nslit( dest, no);
813 from_slit = xsh_rec_list_get_nslit( from[0], no);
814 nlambda = xsh_rec_list_get_nlambda( dest, no);
815
816
817 float *dest_data1 = NULL;
818 float *dest_errs1 = NULL;
819
820 int *dest_qual1 = NULL;
821 dest_data1 = xsh_rec_list_get_data1( dest, no);
822 dest_errs1 = xsh_rec_list_get_errs1( dest, no);
823 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
824
825
826 /* transform rec list into imagelist to easily handle operations */
827 int ns, nl, nf;
828 cpl_image* ima_tmp;
829 cpl_image* ima_tmp2;
830 cpl_image* ima_tmp3;
831 cpl_imagelist* scaled = cpl_imagelist_new();
832 cpl_imagelist* data = cpl_imagelist_new();
833 cpl_imagelist* qual = cpl_imagelist_new();
834 /* create imagelist */
835
836 for (nf = 0; nf < nb_frames; nf++) {
837 ima_tmp=cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
838 cpl_imagelist_set(scaled,ima_tmp,nf);
839 ima_tmp2=cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
840 cpl_imagelist_set(data,ima_tmp2,nf);
841 ima_tmp3=cpl_image_new(nlambda,nslit,CPL_TYPE_INT);
842 cpl_imagelist_set(qual,ima_tmp3,nf);
843 }
844
845 /* fill image list with values and flag bad pixels */
846 float* pscaled=NULL;
847 float* pdata=NULL;
848 int* pqual=NULL;
849 cpl_binary* pmsk;
850 float *from_data1 = NULL;
851 float *from_errs1 = NULL;
852 int *from_qual1 = NULL;
853 char fname[80];
854
855 for (ns = 0; ns < nslit; ns++) {
856 for (nl = 0; nl < nlambda; nl++) {
857 int dest_idx;
858
859 dest_idx = nl + ns * nlambda;
860
861 for (nf = 0; nf < nb_frames; nf++) {
862 int from_idx;
863 ima_tmp=cpl_imagelist_get(scaled,nf);
864 pscaled=cpl_image_get_data_float(ima_tmp);
865 ima_tmp2=cpl_imagelist_get(data,nf);
866 pdata=cpl_image_get_data_float(ima_tmp2);
867 ima_tmp3=cpl_imagelist_get(qual,nf);
868 pqual=cpl_image_get_data_int(ima_tmp3);
869
870
871 pmsk=cpl_mask_get_data(cpl_image_get_bpm(ima_tmp));
872 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
873 continue;
874 }
875
876 from_idx = nl + (ns - slit_index[nf]) * nlambda;
877 from_data1 = xsh_rec_list_get_data1( from[nf], no);
878 from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
879 from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
880 //xsh_msg("ns=%d nl=%d dest_idx=%d from_idx=%d",ns,nl,dest_idx,from_idx);
881 pscaled[dest_idx] = from_data1[from_idx] / dest_data1[dest_idx];
882 /*
883 if (no == 4 && nl > 1855 && nl < 1865 && ns > 39 && ns < 41) {
884 xsh_msg("scale factors: ord %d frm %d num=%g den=%g num_e=%g den_e=%g num_e/num=%g den_e/den=%g scale=%g",
885 no, nf, from_data1[from_idx], dest_data1[dest_idx],
886 from_errs1[from_idx], dest_errs1[dest_idx],
887 fabs(from_errs1[from_idx]/from_data1[from_idx]),
888 fabs(dest_errs1[from_idx]/dest_data1[from_idx]),
889 pscaled[dest_idx]);
890 }
891 */
892 pdata[dest_idx] = from_data1[from_idx] ;
893 pqual[dest_idx] = from_qual1[from_idx] ;
894 if( ( ( dest_qual1[dest_idx] & decode_bp ) > 0 ) ||
895 ( ( from_qual1[from_idx] & decode_bp ) > 0 ) ||
896 isnan(pscaled[dest_idx]) ||
897 isinf(pscaled[dest_idx]) ||
898 fabs(from_errs1[from_idx]/from_data1[from_idx]) > 5 ||
899 fabs(dest_errs1[from_idx]/dest_data1[from_idx]) > 5
900 ) {
901 pmsk[dest_idx]=CPL_BINARY_1;
902 /* force to be 1 (otherwhise it would be NAN) */
903 pscaled[dest_idx]=1;
904 /*
905 if (no > 0) {
906 int sx=xsh_rec_list_get_nlambda( from[nf], no);
907 int sy=xsh_rec_list_get_nslit( from[nf], no);
908 cpl_image* ima=NULL;
909 xsh_msg("sx=%d sy=%d",sx,sy);
910 ima=cpl_image_wrap(sx,sy,CPL_TYPE_FLOAT,from_data1);
911 cpl_image_save(ima, "from_data.fits", XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
912
913 cpl_image_unwrap(ima);
914 ima=cpl_image_wrap(sx,sy,CPL_TYPE_INT,from_qual1);
915 cpl_image_save(ima, "from_qual.fits", XSH_PRE_QUAL_BPP, NULL, CPL_IO_DEFAULT);
916
917
918
919 xsh_msg("nlambda: %d nslit: %d",nlambda,nslit);
920 xsh_msg("from size: %d %d",
921 xsh_rec_list_get_nlambda( from[nf], no),
922 xsh_rec_list_get_nslit( from[nf], no));
923
924 xsh_msg("dest size: %d %d",
925 xsh_rec_list_get_nlambda( dest, no),
926 xsh_rec_list_get_nslit( dest, no));
927
928 xsh_msg("from_data=%g dest_data=%g nl=%d ns=%d",
929 from_data1[from_idx],dest_data1[dest_idx],nl,ns);
930 xsh_msg("nf=%d slit index=%d from_idx=%d",
931 nf, slit_index[nf],from_idx);
932 sx=xsh_rec_list_get_nlambda( dest, no);
933 sy=xsh_rec_list_get_nslit( dest, no);
934 cpl_image_unwrap(ima);
935 ima=cpl_image_wrap(sx,sy,CPL_TYPE_FLOAT,dest_data1);
936 cpl_image_save(ima, "dest_data.fits", XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
937
938
939 cpl_image_unwrap(ima);
940 ima=cpl_image_wrap(sx,sy,CPL_TYPE_INT,dest_data1);
941 cpl_image_save(ima, "dest_qual.fits", XSH_PRE_QUAL_BPP, NULL, CPL_IO_DEFAULT);
942
943 cpl_image_unwrap(ima);
944 ima=cpl_image_wrap(sx,sy,CPL_TYPE_FLOAT,pscaled);
945 cpl_image_save(ima, "scaled.fits", XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
946
947
948 cpl_image_unwrap(ima);
949 exit(0);
950 }
951 */
952 }
953 }
954 }
955 }
956
957
958 sprintf(fname,"data_rectified_order_%d.fits",no);
959 //cpl_imagelist_save(data,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_DEFAULT);
960 sprintf(fname,"qual_rectified_order_%d.fits",no);
961 //cpl_imagelist_save(qual,fname,CPL_TYPE_INT,NULL,CPL_IO_DEFAULT);
962
963 sprintf(fname,"scale_rectified_order_%d.fits",no);
964 //cpl_imagelist_save(scaled,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_DEFAULT);
965 /* determine scale factors: one at each Y (spatial) position
966 * we assume the scale is approximatively uniform along X (wavelength)
967 */
968
969 /* we should correct scaling only where the object is */
970 cpl_imagelist* scale_factors;
971 double slit_ext_min, slit_ext_max, slit_step;
972 if(scale_method > 0) {
973
976 &slit_ext_max);
977 slit_ext_min += 1;
978 slit_ext_max -= 1;
979 }
980
981 switch(scale_method) {
982
983 case 0:
984 check(scale_factors=xsh_compute_scale_factors_by_slice(scaled,
985 scale, nlambda, nslit, no));
986 break;
987
988 case 1:
989 check(scale_factors=xsh_compute_scale_factors_by_object(scaled,
990 scale, nlambda, nslit, no, slit_ext_min, slit_ext_max));
991 break;
992
993 case 2:
994 check(scale_factors=xsh_compute_scale_factors_pix_pix(scaled,
995 scale, nlambda, nslit, no));
996 break;
997 }
998 cleanup:
999 cpl_imagelist_delete(scaled);
1000 cpl_imagelist_delete(data);
1001 cpl_imagelist_delete(qual);
1003 return scale_factors;
1004}
1005
1006/*
1007static cpl_image*
1008xsh_rec_list_compute_scale2(xsh_rec_list *dest, xsh_rec_list **from,
1009 int *slit_index, int nb_frames, int no,
1010 int method, const int decode_bp)
1011{
1012
1013
1014 float *from_data1 = NULL;
1015 float *dest_data1 = NULL;
1016
1017 //int *from_qual1 = NULL;
1018 int *dest_qual1 = NULL;
1019 int nslit, from_slit, nlambda;
1020 int ns, nl, nf;
1021 XSH_ASSURE_NOT_NULL( dest);
1022 XSH_ASSURE_NOT_NULL( from);
1023 XSH_ASSURE_NOT_NULL( slit_index);
1024
1025 nslit = xsh_rec_list_get_nslit( dest, no);
1026 from_slit = xsh_rec_list_get_nslit( from[0], no);
1027 nlambda = xsh_rec_list_get_nlambda( dest, no);
1028
1029 dest_data1 = xsh_rec_list_get_data1( dest, no);
1030 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
1031 cpl_image* ima_tmp;
1032 cpl_imagelist* scaled = cpl_imagelist_new();
1033 float* pscaled=NULL;
1034 cpl_binary* pmsk;
1035 // to easy coding copy rec list on an imagelist and transfer
1036 // the qualifier info in the associated BP map
1037
1038 for (nf = 0; nf < nb_frames; nf++) {
1039 ima_tmp=cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
1040 cpl_imagelist_set(scaled,ima_tmp,nf);
1041 }
1042
1043 for (ns = 0; ns < nslit; ns++) {
1044 for (nl = 0; nl < nlambda; nl++) {
1045 int dest_idx;
1046 //int good = 0, bad = 0;
1047 //int isum =0;
1048
1049 dest_idx = nl + ns * nlambda;
1050
1051 for (nf = 0; nf < nb_frames; nf++) {
1052 int from_idx;
1053 ima_tmp=cpl_imagelist_get(scaled,nf);
1054 pscaled=cpl_image_get_data_float(ima_tmp);
1055 pmsk=cpl_mask_get_data(cpl_image_get_bpm(ima_tmp));
1056 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1057 continue;
1058 }
1059
1060 from_idx = nl + (ns - slit_index[nf]) * nlambda;
1061 from_data1 = xsh_rec_list_get_data1( from[nf], no);
1062 //xsh_msg("ns=%d nl=%d dest_idx=%d from_idx=%d",ns,nl,dest_idx,from_idx);
1063 pscaled[dest_idx] = from_data1[from_idx] / dest_data1[dest_idx];
1064 if( ( ( dest_qual1[dest_idx] > decode_bp ) > 0 ) ||
1065 isnan(pscaled[dest_idx]) || isinf(pscaled[dest_idx]) ) {
1066 pmsk[dest_idx]=CPL_BINARY_1;
1067 }
1068 }
1069 }
1070 }
1071 float avg;
1072 float med;
1073 float rms;
1074 int nbad;
1075 const cpl_mask* mask;
1076 for (nf = 0; nf < nb_frames; nf++) {
1077 for (ns = 0; ns < nslit; ns++) {
1078
1079 ima_tmp=cpl_imagelist_get(scaled,nf);
1080 mask=cpl_image_get_bpm_const(ima_tmp);
1081 check(nbad=cpl_mask_count_window(mask,1,ns+1,nlambda,ns+1));
1082 if(nbad<nlambda) {
1083 check(avg=cpl_image_get_mean_window(ima_tmp,1,ns+1,nlambda,ns+1));
1084 check(med=cpl_image_get_median_window(ima_tmp,1,ns+1,nlambda,ns+1));
1085 check(rms=cpl_image_get_stdev_window(ima_tmp,1,ns+1,nlambda,ns+1));
1086 xsh_msg("frame %d slice %d mean=%g median=%g rm==%g",
1087 nf, ns, avg,med,rms);
1088 }
1089 }
1090
1091 }
1092 char fname[80];
1093 cleanup:
1094
1095 sprintf(fname,"scaled_%d.fits",no);
1096 cpl_imagelist_save(scaled,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_DEFAULT);
1097 cpl_imagelist_delete(scaled);
1098 xsh_print_rec_status(0);
1099 return NULL;
1100
1101}
1102 */
1103/*
1104static void
1105xsh_fill_gauss(float** pgauss, const int sx, const int sy, const double A,
1106 const double sigma, const double bkg, const int i,
1107 const double gauss_c,const int min, const int max)
1108{
1109
1110 // add Gauss profile positioned where the trace is
1111 // I=A*exp( -0.5*( (X-Xo)/sigma )^2 ) +bkg
1112
1113
1114 double arg = 0;
1115 int j;
1116 double y=0;
1117
1118 // uniform along X (horizontal direction) Gauss along Y (vertical)
1119 for (j = min; j <= max; j++) {
1120 y=j;
1121
1122 arg = (y - gauss_c) / sigma;
1123 arg *= arg;
1124 xsh_msg("j=%d i=%d arg=%g,sx=%d val=%g",j,i,arg,sx,A * exp(-0.5 * arg) + bkg);
1125 // *pgauss[j * sx + i] += (float) (A * exp(-0.5 * arg) + bkg);
1126 *pgauss[j * sx + i] = 1;
1127 //xsh_msg("val=%g",pgauss[j * sx + i]);
1128
1129 }
1130
1131 return;
1132}
1133 */
1134
1135#if 0
1136static void
1137xsh_rec_list_fill_gauss(xsh_rec_list *dest, xsh_rec_list **from,
1138 int *slit_index, int nb_frames, int no,
1139 int method, const int decode_bp)
1140{
1141
1142 int nslit, from_slit, nlambda;
1143 //float *dest_data1 = NULL, *dest_errs1 = NULL;
1144 //int *dest_qual1 = NULL;
1145 nslit = xsh_rec_list_get_nslit( dest, no);
1146 from_slit = xsh_rec_list_get_nslit( from[0], no);
1147 nlambda = xsh_rec_list_get_nlambda( dest, no);
1148
1149 //dest_data1 = xsh_rec_list_get_data1( dest, no);
1150 //dest_errs1 = xsh_rec_list_get_errs1( dest, no);
1151 //dest_qual1 = xsh_rec_list_get_qual1( dest, no);
1152
1153 int ns, nl, nf;
1154 //cpl_vector* trace;
1155 int sy=nslit;
1156 //int sx=nlambda;
1157
1158 double gauss_c=sy/2;
1159 //double sigma=3;
1160 //double bkg=1;
1161 //int min=2*sy/5;
1162 //int max=3*sy/5;
1163 //double y;
1164 //double arg;
1165 xsh_msg("gauss_c=%g from_slit=%d nb_frames=%d",gauss_c,from_slit,nb_frames);
1166 char fname[80];
1167 cpl_image* ima;
1168
1169 /* reset data to 0 */
1170 float *from_data1 = NULL;
1171 //float *from_errs1 = NULL;
1172 for (nf = 0; nf < nb_frames; nf++) {
1173 xsh_msg("slit_index[%d]=%d",nf,slit_index[nf]);
1174 sprintf(fname,"data_%d.fits",nf);
1175 from_data1 = xsh_rec_list_get_data1( from[nf], no);
1176 //from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
1177 int from_idx;
1178 for (ns = 0; ns < nslit; ns++) {
1179 for (nl = 0; nl < nlambda; nl++) {
1180
1181 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1182 continue;
1183 }
1184
1185 from_idx = nl + (ns - slit_index[nf]) * nlambda;
1186 from_data1[from_idx] = 0;
1187 //from_data1[ns*nlambda+nl] = 0;
1188 }
1189 }
1190
1191 }
1192 /*
1193 for (ns = 0; ns < nslit; ns++) {
1194 int from_idx;
1195 for (nl = 0; nl < nlambda; nl++) {
1196 for (nf = 0; nf < nb_frames; nf++) {
1197 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1198 continue;
1199 }
1200 from_data1 = xsh_rec_list_get_data1( from[nf], no);
1201 from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
1202 from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
1203
1204 from_idx = nl + (ns - slit_index[nf]) * nlambda;
1205
1206 double A=(nf+1)*10;
1207
1208 y=ns-slit_index[nf];
1209 arg = (y - gauss_c) / sigma;
1210 //xsh_msg("ns=%d gauss_c=%g arg=%g",ns,gauss_c,arg);
1211 arg *= arg;
1212 //xsh_msg("j=%d i=%d arg=%g,sx=%d val=%g",j,i,arg,sx,A * exp(-0.5 * arg) + bkg);
1213 from_data1[from_idx] += (float) (A * exp(-0.5 * arg) + bkg);
1214 //from_data1[from_idx] = 1;
1215
1216 //xsh_fill_gauss(&from_data1,sx,sy,A,sigma, bkg,nl,gauss_c,min,max);
1217 }
1218 }
1219 }
1220 */
1221 for (nf = 0; nf < nb_frames; nf++) {
1222 sprintf(fname,"data_%d.fits",nf);
1223 from_data1 = xsh_rec_list_get_data1( from[nf], no);
1224 //from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
1225 //int* from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
1226 ima= cpl_image_wrap_float(nlambda,nslit,from_data1);
1227 if(no==0) {
1228 cpl_image_save(ima, fname,CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1229 } else {
1230 cpl_image_save(ima, fname,CPL_TYPE_FLOAT, NULL, CPL_IO_EXTEND);
1231 }
1232 }
1233
1234 return;
1235
1236}
1237#endif
1238/* TODO: NOT USED
1239static double
1240xsh_compute_scale_nod(xsh_rec_list **from,int *slit_index,
1241 int nb_frames, int no,const int decode_bp, int pix)
1242{
1243
1245 double scale=0;
1246 float *from_data1 = NULL, *from_errs1 = NULL;
1247 int *from_qual1 = NULL;
1248 int win_hsx;
1249 int win_hsy;
1250 int mode=0;
1251 int win_hsz=3;
1252
1253 int num_good_pix;
1254 int num_tot_pix;
1255 double sum_all;
1256 double sum_good;
1257 double sum_good_pix;
1258 double sum_tot_pix;
1259 cpl_image* ima_tmp;
1260 int i=0;
1261 int j=0;
1262 int sx=cpl_image_get_size_x(ima_tmp);
1263 int sy=cpl_image_get_size_y(ima_tmp);
1264
1265 if(mode==0) {
1266 win_hsx=0;
1267 win_hsy=win_hsz;
1268 } else if (mode == 1) {
1269 win_hsx=win_hsz;
1270 win_hsy=0;
1271 } else {
1272 win_hsx=win_hsz;
1273 win_hsy=win_hsz;
1274 }
1275 int win_sx=2*win_hsx+1;
1276 int win_sy=2*win_hsy+1;
1277
1278 for (int nf = 0; nf < nb_frames; nf++) {
1279
1280 sum_good_pix=0;
1281 num_good_pix=0;
1282 sum_tot_pix=0;
1283 num_tot_pix=0;
1284 xsh_msg("found bad pix at pos[%d,%d]",i,j);
1286 int y_min=j-win_hsy, y_max=j+win_hsy;
1287 //xsh_msg("init y_min=%d y_max=%d",y_min,y_max);
1289 if (y_min < 0) {
1290 xsh_msg("y case1");
1291 y_min = 0;
1292 y_max = win_sy;
1293 } else if ( y_max > sy) {
1294 //xsh_msg("y case2 j=%d j+y_max=%d,sy=%d",j,j+y_max,sy);
1295 y_max=sy;
1296 y_min=y_max-win_sy;
1297 }
1298 //xsh_msg("corrected y_min=%d y_max=%d",y_min,y_max);
1299 int x_min=i-win_hsx, x_max=i+win_hsx;
1300 //xsh_msg("init x_min=%d x_max=%d",x_min,x_max);
1301 if (x_min < 0) {
1302 //xsh_msg("x case1")/;
1303 x_min = 0;
1304 x_max = win_sx;
1305 } else if ( x_max > sx) {
1306 //xsh_msg("x case2 i=%d i+x_max=%d,sx=%d",i,i+x_max,sx);
1307 x_max=sx;
1308 x_min=x_max-win_sx;
1309 }
1310 //xsh_msg("corrected x_min=%d x_max=%d",x_min,x_max);
1311 sum_all=0;
1312 sum_good=0;
1313
1314
1315 from_data1 = xsh_rec_list_get_data1( from[nf], no);
1316 from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
1317 from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
1318
1319
1320
1321
1322 }
1323 return scale;
1324}
1325 */
1326
1327
1328 static cpl_error_code
1330 int *slit_index, int nb_frames, int no,
1331 int method, const int decode_bp, const int nf)
1332 {
1333 int nslit, from_slit, nlambda;
1334 float *dest_data1 = NULL, *dest_errs1 = NULL;
1335 int *dest_qual1 = NULL;
1336 float *from_data1 = NULL, *from_errs1 = NULL;
1337 int *from_qual1 = NULL;
1338 int ns, nl=0;
1339
1340 XSH_ASSURE_NOT_NULL( dest);
1341 XSH_ASSURE_NOT_NULL( from);
1342 XSH_ASSURE_NOT_NULL( slit_index);
1343
1345 "xsh_rec_list_add: nb frames: %d, order: %d", nb_frames, no);
1346
1347 check( nslit = xsh_rec_list_get_nslit( dest, no));
1348 check( from_slit = xsh_rec_list_get_nslit( from[0], no));
1349 check( nlambda = xsh_rec_list_get_nlambda( dest, no));
1350 check( dest_data1 = xsh_rec_list_get_data1( dest, no));
1351 check( dest_errs1 = xsh_rec_list_get_errs1( dest, no));
1352 check( dest_qual1 = xsh_rec_list_get_qual1( dest, no));
1353
1354 char name[80];
1355 cpl_frame* frm=NULL;
1356 //double scale=0;
1357
1358
1359
1360 for (ns = 0; ns < nslit; ns++) {
1361 for (nl = 0; nl < nlambda; nl++) {
1362 int dest_idx;
1363 dest_idx = nl + ns * nlambda;
1364 dest_data1[dest_idx] = 0;
1365 dest_errs1[dest_idx] = 0;
1366 dest_qual1[dest_idx] = 0;
1367 }
1368 }
1369
1370 //sprintf(name,"dummy_resampled_shifted_%d.fits",nf);
1371 //frm=xsh_rec_list_save2(dest,name,"test");
1372 //xsh_add_temporary_file(name);
1373 //xsh_free_frame(&frm);
1374
1375 for (ns = 0; ns < nslit; ns++) {
1376 for (nl = 0; nl < nlambda; nl++) {
1377
1378 int dest_idx;
1379 int from_idx;
1380 dest_idx = nl + ns * nlambda;
1381
1382 //xsh_msg("ns=%d slit_index=%d from_slit=%d",ns,slit_index[nf],from_slit);
1383 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1384 continue;
1385 }
1386 from_idx = nl + (ns - slit_index[nf]) * nlambda;
1387 check( from_data1 = xsh_rec_list_get_data1( from[nf], no));
1388 check( from_errs1 = xsh_rec_list_get_errs1( from[nf], no));
1389 check( from_qual1 = xsh_rec_list_get_qual1( from[nf], no));
1390
1391 dest_data1[dest_idx] = from_data1[from_idx];
1392 dest_errs1[dest_idx] = from_errs1[from_idx];
1393 dest_qual1[dest_idx] = from_qual1[from_idx];
1394
1395 }
1396
1397 }
1398 //xsh_msg("no=%d rec_input_list=%p",no,rec_input_list);
1399 sprintf(name,"input_resampled_shifted_%d.fits",nf);
1400 frm=xsh_rec_list_save2(dest,name,"test");
1402 xsh_free_frame(&frm);
1403
1404
1405 cleanup:
1406 return cpl_error_get_code();
1407
1408 }
1409
1410
1411
1412void
1414 int *slit_index, int nb_frames, int no,
1415 int method, const int decode_bp)
1416{
1417 int nslit, from_slit, nlambda;
1418 float *dest_data1 = NULL, *dest_errs1 = NULL;
1419 int *dest_qual1 = NULL;
1420 float *from_data1 = NULL, *from_errs1 = NULL;
1421 int *from_qual1 = NULL;
1422 int ns, nl=0, nf;
1423 double *flux_tab = NULL;
1424 cpl_vector *flux_vect = NULL;
1425 double *err_tab = NULL;
1426 cpl_vector *err_vect = NULL;
1427
1428 XSH_ASSURE_NOT_NULL( dest);
1429 XSH_ASSURE_NOT_NULL( from);
1430 XSH_ASSURE_NOT_NULL( slit_index);
1431
1432
1433 char fname[80];
1434 cpl_frame* frm=NULL;
1435 for (nf = 0; nf < nb_frames; nf++) {
1436 sprintf(fname,"list_%d.fits",nf);
1437 //frm=xsh_rec_list_save( from[nf], fname, "TEST",0);
1438 xsh_free_frame(&frm);
1439 }
1440
1442 "xsh_rec_list_add: nb frames: %d, order: %d", nb_frames, no);
1443
1444 check( nslit = xsh_rec_list_get_nslit( dest, no));
1445 check( from_slit = xsh_rec_list_get_nslit( from[0], no));
1446 check( nlambda = xsh_rec_list_get_nlambda( dest, no));
1447
1448 check( dest_data1 = xsh_rec_list_get_data1( dest, no));
1449 check( dest_errs1 = xsh_rec_list_get_errs1( dest, no));
1450 check( dest_qual1 = xsh_rec_list_get_qual1( dest, no));
1451
1452 XSH_MALLOC( flux_tab, double, nb_frames);
1453 XSH_MALLOC ( err_tab, double , nb_frames);
1454
1455 for (nf = 0; nf < nb_frames; nf++) {
1457 "slit index: max %d min=%d", slit_index[nf], slit_index[nf]+from_slit);
1458 }
1459
1460
1461 //double scale=0;
1462 for (ns = 0; ns < nslit; ns++) {
1463 for (nl = 0; nl < nlambda; nl++) {
1464
1465 int dest_idx;
1466 //int good = 0, bad = 0;
1467 int isum =0;
1468 //double dest_err = 0, dest_data = 0. ;
1469 //double bad_err = 0, bad_data = 0;
1470 unsigned int qflag = QFLAG_GOOD_PIXEL;
1471 //unsigned int badflag = QFLAG_GOOD_PIXEL;
1472 int from_idx;
1473 dest_idx = nl + ns * nlambda;
1474
1475 for (nf = 0; nf < nb_frames; nf++) {
1476
1477 //xsh_msg("ns=%d slit_index=%d from_slit=%d",ns,slit_index[nf],from_slit);
1478 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1479 continue;
1480 }
1481 from_idx = nl + (ns - slit_index[nf]) * nlambda;
1482 check( from_data1 = xsh_rec_list_get_data1( from[nf], no));
1483 check( from_errs1 = xsh_rec_list_get_errs1( from[nf], no));
1484 check( from_qual1 = xsh_rec_list_get_qual1( from[nf], no));
1485
1486 //xsh_msg("2 isum=%d flux_tab=%p",isum,flux_tab);
1487
1488#if combine_nod_old
1489
1490 /* previous implementation separates good and bad pixels */
1491 if ((from_qual1[from_idx] & decode_bp) == 0) {
1492 qflag |= from_qual1[from_idx];
1493 flux_tab[good] = from_data1[from_idx];
1494 err_tab[good] = from_errs1[from_idx];
1495 good++;
1496
1497 } else if ((from_qual1[from_idx] & decode_bp) > 0) {
1498 flux_tab[good + bad] = from_data1[from_idx];
1499 err_tab[good + bad] = from_errs1[from_idx];
1500 badflag |= from_qual1[from_idx];
1501 bad++;
1502
1503 }
1504
1505#else
1506
1507 /* new implementation: we combine good and bad pixels but also quality */
1508 //xsh_msg("new combine_nod");
1509
1510 qflag |= from_qual1[from_idx];
1511
1512 flux_tab[isum] = from_data1[from_idx];
1513
1514 err_tab[isum] = from_errs1[from_idx];
1515
1516 isum++;
1517 //xsh_msg("3 isum=%d flux_tab=%p",isum,flux_tab);
1518#endif
1519 }
1520
1521 //xsh_msg("4 isum=%d flux_tab=%p",isum,flux_tab);
1522
1523 /* Dont calculate mean value, just sum */
1524#if combine_nod_old
1525 /* previous implementation separates good and bad pixels */
1526 if (good == 0) {
1527 check( flux_vect = cpl_vector_wrap( bad, flux_tab));
1528 check( err_vect = cpl_vector_wrap( bad, err_tab));
1529 // No good pixel
1530 dest_qual1[dest_idx] |= badflag;
1531 } else {
1532 dest_qual1[dest_idx] |= qflag;
1533 if(bad > 0) {
1534 dest_qual1[dest_idx] |= QFLAG_INCOMPLETE_NOD;
1535 //scale=xsh_compute_scale_nod(from,slit_index,nb_frames,no,decode_bp,from_idx);
1536 }
1537 check( flux_vect = cpl_vector_wrap( good, flux_tab));
1538 check( err_vect = cpl_vector_wrap( good, err_tab));
1539 }
1540
1541#else
1542 /* new implementation: we combine good and bad pixels but also quality */
1543 //xsh_msg("5 isum=%d flux_tab=%p",isum,flux_tab);
1544
1545 dest_qual1[dest_idx] |= qflag;
1546
1547 //xsh_msg("6 isum=%d flux_tab=%p",isum,flux_tab);
1548
1549 check( flux_vect = cpl_vector_wrap( isum, flux_tab));
1550 check( err_vect = cpl_vector_wrap( isum, err_tab));
1551#endif
1552
1553 if (method == COMBINE_MEAN_METHOD) {
1554 check( dest_data1[dest_idx] = cpl_vector_get_mean( flux_vect));
1555 check( dest_errs1[dest_idx] = xsh_vector_get_err_mean( err_vect));
1556 } else {
1557 check( dest_data1[dest_idx] = cpl_vector_get_median( flux_vect));
1558 check( dest_errs1[dest_idx] = xsh_vector_get_err_median( err_vect));
1559 }
1560 xsh_unwrap_vector(&flux_vect);
1561 xsh_unwrap_vector(&err_vect);
1562
1563 }
1564 }
1565 cpl_image* ima_bp=cpl_image_wrap_int(nl,ns,dest_qual1);
1566 sprintf(fname,"ima_bp_%d.fits",no);
1567 //cpl_image_save(ima_bp, fname, XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1568
1569 cpl_table* tab_bp=xsh_qual2tab(ima_bp,QFLAG_INCOMPLETE_NOD);
1570 sprintf(fname,"tab_bp_%d.fits",no);
1571 //cpl_table_save(tab_bp,NULL,NULL,fname,CPL_IO_DEFAULT);
1572 cpl_image_unwrap(ima_bp);
1573
1574
1575 cleanup:
1576
1577 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1578 xsh_unwrap_vector(&flux_vect);
1579 xsh_unwrap_vector(&err_vect);
1580 }
1581 xsh_free_table(&tab_bp);
1582 XSH_FREE( flux_tab);
1583 XSH_FREE( err_tab);
1584 return;
1585}
1586cpl_error_code
1588 int *slit_index, int nb_frames, int no, int method, const int decode_bp,
1589 cpl_imagelist* scale_factors, const int scale_method, const cpl_table* bp)
1590{
1591
1592 /* Here for each point of the bp table we need to compute two sums:
1593 * 1) Sum of the good pixels in a small range (along wavelength) from all
1594 * frames (including the ones that are coming from a frame that had a bad
1595 * pixel)
1596 * 2) Sum of the good pixels coming only from the frames that have good
1597 * pixels at the flagged pixel
1598 * Then the scale is scale=sum(1)/sum(2)
1599 * To get the sum we need to use the original structures (from).
1600 *
1601 */
1602 //xsh_msg("start of function");
1603 //cpl_image* scale_ima;
1604 int nlambda_ns;
1605 int pix;
1606
1607 double sum_good_pix=0.;
1608 double sum_bad_pix=0.;
1609 //double norm_good_pix=0.;
1610 XSH_ASSURE_NOT_NULL( dest);
1611 XSH_ASSURE_NOT_NULL( from);
1612 XSH_ASSURE_NOT_NULL( slit_index);
1613
1614 int nslit = xsh_rec_list_get_nslit( dest, no );
1615 int nlambda = xsh_rec_list_get_nlambda( dest, no );
1616 int win_hsz=3; // this sets the size of the box to search for good pixels
1617 int win_hsx=win_hsz;
1618 int win_sx=2*win_hsx+1;
1619 int num_good_pix;
1620 int num_bad_pix;
1621 double sum_all_pix;
1622
1623 int ns,nl,m;
1624 char fname[80];
1625 //double* pscale;
1626 float* dest_data1;
1627 int from_slit = xsh_rec_list_get_nslit( from[0], no);
1628
1629
1630 int *dest_qual1 = NULL;
1631 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
1632
1633 cpl_image* ima_bp=cpl_image_wrap_int(nlambda,nslit,dest_qual1);
1634 sprintf(fname,"ima_bp_%d.fits",no);
1635 //cpl_image_save(ima_bp, fname, XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1636
1637 /* selects only points for which not all frames contributed
1638 * (some pixel is bad) */
1639 cpl_table* tab_bpm=xsh_qual2tab(ima_bp,QFLAG_INCOMPLETE_NOD);
1640 sprintf(fname,"tab_bp_%d.fits",no);
1641 cpl_table_save(tab_bpm,NULL,NULL,fname,CPL_IO_DEFAULT);
1642 //exit(0);
1643 cpl_image_unwrap(ima_bp);
1644
1645 int size=cpl_table_get_nrow(tab_bpm);
1646 int* px=cpl_table_get_data_int(tab_bpm,"x");
1647 int* py=cpl_table_get_data_int(tab_bpm,"y");
1648 int ns_min=-999;
1649 int ns_max=999;
1650 double scale=1;
1651 double slit_ext_min=0;
1652 double slit_ext_max=0;
1655 &slit_ext_max);
1656
1657 for (int nf = 0; nf < nb_frames; nf++) {
1658 //xsh_msg("slit index: max %d min=%d", slit_index[nf], slit_index[nf]+from_slit);
1659 if(slit_index[nf]>ns_min) {
1660 ns_min=slit_index[nf];
1661 }
1662 if( (slit_index[nf]+from_slit) < ns_max) {
1663 ns_max=(slit_index[nf]+from_slit);
1664 }
1665 }
1666 //xsh_msg("ck1");
1667 //xsh_msg("ns min=%d max=%d",ns_min,ns_max);
1668
1669 //xsh_msg("slit_ext_min=%g slit_ext_max=%g slit_step=%g",
1670 // slit_ext_min,slit_ext_max,slit_step);
1671 //ns_min=slit_ext_min;
1672 //ns_max=slit_ext_max;
1673 //exit(0);
1674 dest_data1 = xsh_rec_list_get_data1( dest, no);
1675 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
1676 cpl_image* ima;
1677 ima=cpl_image_wrap_float(nlambda,nslit,dest_data1);
1678 //cpl_image_save(ima, "ima_org.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1679 cpl_image_unwrap(ima);
1680 //int pix_scale;
1681 //cpl_mask* mask;
1682 char cname[80];
1683 //cpl_binary* pbpm;
1684 for(m=0;m<size;m++) {
1685
1686 nl=px[m];
1687 ns=py[m];
1688
1689 int x_min=nl-win_hsx, x_max=nl+win_hsx;
1690 /* treats properly evaluation box edges */
1691 //xsh_msg("init x_min=%d x_max=%d",x_min,x_max);
1692 if (x_min < 0) {
1693 //xsh_msg("x case1")/;
1694 x_min = 0;
1695 x_max = win_sx;
1696 } else if ( x_max > nlambda) {
1697 //xsh_msg("x case2 i=%d i+x_max=%d,sx=%d",i,i+x_max,sx);
1698 x_max=nlambda;
1699 x_min=x_max-win_sx;
1700 }
1701
1702 //xsh_msg("order %d nl=%d ns=%d",no,nl,ns);
1703 if( (ns > ns_min) && (ns < ns_max) ) {
1704 nlambda_ns=nlambda*ns;
1705 pix=nlambda_ns+nl;
1706
1707 //xsh_msg("pix=%d",pix);
1708
1709 sum_good_pix=0.;
1710 sum_bad_pix=0.;
1711 num_good_pix=0;
1712 num_bad_pix=0;
1713 //norm_good_pix=0.;
1714 //int sign_from=0;
1715 //int sign_scal=0;
1716 // to sum only over good pixels we flag the bad ones
1717 for(int i=x_min;i<=x_max;i++) {
1718 for(int nf=0; nf < nb_frames; nf++) {
1719
1720 int from_idx;
1721 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1722
1723 xsh_msg("continued i=%d",i);
1724 continue;
1725
1726 }
1727 //i is like nl
1728 from_idx = i + (ns - slit_index[nf]) * nlambda;
1729
1730 sprintf(cname,"f%d",nf);
1731 const int* pq=cpl_table_get_data_int_const(bp,cname);
1732
1733 float * from_data1 = xsh_rec_list_get_data1( from[nf], no);
1734 //float * from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
1735 int* from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
1736 //xsh_msg("order %d nf=%d from_idx=%d",no,nf,from_idx);
1737 if( (from_qual1[from_idx] & decode_bp) == 0 ) {
1738 /* sum over good pixels */
1739 //xsh_msg("correct pix ns=%d nl=%d",ns,nl);
1740 //xsh_msg("k=%d j=%d pdata=%g",k,j,pdata[pix]);
1741 // pq==0 it to pick-up those pixels that are good
1742 // in at least one frame
1743 if((ns >= slit_ext_min && ns <= slit_ext_max) &&
1744 pq[m]==0 && i != 0) {
1745 sum_good_pix += (from_data1[from_idx]);
1746 num_good_pix++;
1747 } else {
1748 sum_bad_pix += (from_data1[from_idx]);
1749 num_bad_pix++;
1750 }
1751 }
1752
1753 } /* end sum over frames */
1754 } /* end loop over small box along X (wavelength) */
1755 //double sum=norm_good_pix;
1756 sum_all_pix= sum_good_pix+sum_bad_pix;
1757 //norm_good_pix=
1758 scale = sum_all_pix/sum_good_pix*num_good_pix/nb_frames;
1759
1760 if(isinf(scale) || isnan(scale)) {
1761 xsh_msg("found infinite sum_good: %g sum_bad: %g num_good: %d",
1762 sum_good_pix, sum_bad_pix, num_good_pix);
1763 xsh_msg("x_min=%d x_max=%d",x_min,x_max);
1764
1765
1766
1767 scale=1;
1768 //xsh_msg("sum=%g num=%d",sum,sum_good_pix);
1769 //exit(0);
1770 }
1771 //xsh_msg("dest before=%g norm=%g",dest_data1[pix],norm_good_pix);
1772
1773 dest_data1[pix] = scale;
1774 dest_qual1[pix] -= QFLAG_INCOMPLETE_NOD;
1775 dest_qual1[pix] |= QFLAG_SCALED_NOD;
1776
1777 } //end check if on overlapping NOD images
1778
1779 } //end loop over bad pix table
1780
1781 ima=cpl_image_wrap_float(nlambda,nslit,dest_data1);
1782 sprintf(fname,"ima_cor_%d.fits",no);
1783 cpl_image_save(ima, fname, CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1784 cpl_table_save(bp,NULL,NULL,"bp_tab.fits",CPL_IO_DEFAULT);
1785 cpl_image_unwrap(ima);
1786 cpl_table_delete(tab_bpm);
1787 cleanup:
1788 //xsh_msg("end of function");
1789 //exit(0);
1790 return cpl_error_get_code();
1791}
1792
1793cpl_error_code
1795 int *slit_index, int nb_frames, int no,
1796 int method, const int decode_bp,
1797 cpl_imagelist* scale_factors,const int scale_method)
1798{
1799 //xsh_msg("start of function");
1800 cpl_image* scale_ima;
1801 int nlambda_ns;
1802 int pix;
1803
1804 double norm_good_pix=0.;
1805
1806 XSH_ASSURE_NOT_NULL( dest);
1807 XSH_ASSURE_NOT_NULL( from);
1808 XSH_ASSURE_NOT_NULL( slit_index);
1809
1810 int nslit = xsh_rec_list_get_nslit( dest, no );
1811 int nlambda = xsh_rec_list_get_nlambda( dest, no );
1812
1813 int num_good_pix;
1814
1815 int ns,nl,m;
1816 char fname[80];
1817 double* pscale;
1818 float* dest_data1;
1819 int from_slit = xsh_rec_list_get_nslit( from[0], no);
1820
1821
1822 int *dest_qual1 = NULL;
1823 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
1824
1825 cpl_image* ima_bp=cpl_image_wrap_int(nlambda,nslit,dest_qual1);
1826 sprintf(fname,"ima_bp_%d.fits",no);
1827 //cpl_image_save(ima_bp, fname, XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1828
1829 cpl_table* tab_bpm=xsh_qual2tab(ima_bp,QFLAG_INCOMPLETE_NOD);
1830 sprintf(fname,"tab_bp_%d.fits",no);
1831 //cpl_table_save(tab_bpm,NULL,NULL,fname,CPL_IO_DEFAULT);
1832 cpl_image_unwrap(ima_bp);
1833
1834 int size=cpl_table_get_nrow(tab_bpm);
1835 int* px=cpl_table_get_data_int(tab_bpm,"x");
1836 int* py=cpl_table_get_data_int(tab_bpm,"y");
1837 int ns_min=-999;
1838 int ns_max=999;
1839
1840 double slit_ext_min=0;
1841 double slit_ext_max=0;
1844 &slit_ext_max);
1845
1846 for (int nf = 0; nf < nb_frames; nf++) {
1847 //xsh_msg("slit index: max %d min=%d", slit_index[nf], slit_index[nf]+from_slit);
1848 if(slit_index[nf]>ns_min) {
1849 ns_min=slit_index[nf];
1850 }
1851 if( (slit_index[nf]+from_slit) < ns_max) {
1852 ns_max=(slit_index[nf]+from_slit);
1853 }
1854 }
1855 //xsh_msg("ck1");
1856 //xsh_msg("ns min=%d max=%d",ns_min,ns_max);
1857
1858 //xsh_msg("slit_ext_min=%g slit_ext_max=%g slit_step=%g",
1859 // slit_ext_min,slit_ext_max,slit_step);
1860 //ns_min=slit_ext_min;
1861 //ns_max=slit_ext_max;
1862 //exit(0);
1863 dest_data1 = xsh_rec_list_get_data1( dest, no);
1864 dest_qual1 = xsh_rec_list_get_qual1( dest, no);
1865 cpl_image* ima;
1866 ima=cpl_image_wrap_float(nlambda,nslit,dest_data1);
1867 //cpl_image_save(ima, "ima_org.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1868 cpl_image_unwrap(ima);
1869 int pix_scale;
1870 cpl_mask* mask;
1871 cpl_binary* pbpm;
1872 for(m=0;m<size;m++) {
1873
1874 nl=px[m];
1875 ns=py[m];
1876 //xsh_msg("order %d nl=%d ns=%d",no,nl,ns);
1877 if( (ns > ns_min) && (ns < ns_max) ) {
1878 nlambda_ns=nlambda*ns;
1879 pix=nlambda_ns+nl;
1880
1881 //xsh_msg("pix=%d",pix);
1882
1883 norm_good_pix=0.;
1884 num_good_pix=0;
1885 //int sign_from=0;
1886 //int sign_scal=0;
1887 // to sum only over good pixels we flag the bad ones
1888 for(int nf=0; nf < nb_frames; nf++) {
1889
1890 int from_idx;
1891 if ((ns < slit_index[nf]) || (ns >= (slit_index[nf] + from_slit))) {
1892 continue;
1893 }
1894 from_idx = nl + (ns - slit_index[nf]) * nlambda;
1895 if(scale_method < 2) {
1896 pix_scale=ns;
1897 } else {
1898 pix_scale=pix;
1899 }
1900
1901 float * from_data1 = xsh_rec_list_get_data1( from[nf], no);
1902 //float * from_errs1 = xsh_rec_list_get_errs1( from[nf], no);
1903 int* from_qual1 = xsh_rec_list_get_qual1( from[nf], no);
1904 //xsh_msg("order %d nf=%d from_idx=%d",no,nf,from_idx);
1905 if( (from_qual1[from_idx] & decode_bp) == 0 ) {
1906 //xsh_msg("correct pix ns=%d nl=%d",ns,nl);
1907 //xsh_msg("k=%d j=%d pdata=%g",k,j,pdata[pix]);
1908 scale_ima=cpl_imagelist_get(scale_factors,nf);
1909 mask=cpl_image_get_bpm(scale_ima);
1910 pbpm=cpl_mask_get_data(mask);
1911 //xsh_msg("sk1 %d %d",cpl_image_get_size_x(scale_ima),cpl_image_get_size_y(scale_ima));
1912 pscale=cpl_image_get_data_double(scale_ima);
1913 /*
1914 if ( (nl > 1765 && nl < 1775) && (ns > 27 && ns < 35) ) {
1915 xsh_msg("no=%d nf=%d ns=%d nl=%d data=%g pscale=%g r=%g",
1916 no,nf,ns,nl,from_data1[from_idx],pscale[pix_scale],
1917 from_data1[from_idx]/pscale[pix_scale]);
1918 }
1919 */
1920
1921
1922 /*
1923 if(no==4 && (nl > 1860 && nl < 1862) && (ns > 39 && ns < 41)) {
1924 xsh_msg("no=%d nf=%d ns=%d nl=%d data=%g pscale=%g r=%g",
1925 no,nf,ns,nl,from_data1[from_idx],pscale[pix_scale],
1926 from_data1[from_idx]/pscale[pix_scale]);
1927 sprintf(fname,"scale_ima_ord_%d_frame_%d.fits",no,nf);
1928 cpl_image_save(scale_ima,fname,CPL_TYPE_FLOAT,NULL,CPL_IO_CREATE);
1929 }
1930 */
1931
1932 if((ns >= slit_ext_min && ns <= slit_ext_max) &&
1933 pbpm[pix_scale]==CPL_BINARY_0) {
1934 norm_good_pix += (from_data1[from_idx]) /
1935 (pscale[pix_scale]);
1936 /*
1937 if(no==4 && (nl > 1860 && nl < 1862) && (ns > 39 && ns < 41)) {
1938 xsh_msg("norm=%g num=%d slit_ext_min=%g slit_ext_max=%g",
1939 norm_good_pix,num_good_pix,slit_ext_min, slit_ext_max);
1940 }
1941 */
1942
1943 } else {
1944 /*
1945 sign_from = (from_data1[from_idx] > 0) ? 1 : -1;
1946 sign_scal = (pscale[pix_scale] > 0) ? -1 : 1;
1947 norm_good_pix += (sign_from*from_data1[from_idx]) /
1948 (sign_scal*pscale[pix_scale]);
1949 */
1950
1951 }
1952
1953 num_good_pix++;
1954 /*
1955 if(isinf(norm_good_pix)) {
1956 xsh_msg("add=%g scale=%g",from_data1[from_idx],pscale[pix_scale]);
1957 }
1958 */
1959
1960 }
1961
1962 }
1963 //double sum=norm_good_pix;
1964 norm_good_pix /= num_good_pix;
1965 /*
1966 if(no==4 && (nl > 1860 && nl < 1862) && (ns > 39 && ns < 41)) {
1967 xsh_msg("m=%d norm=%g num=%d slit_ext_min=%g slit_ext_max=%g",
1968 m, norm_good_pix,num_good_pix,slit_ext_min,slit_ext_max);
1969 }
1970 */
1971
1972 if(isinf(norm_good_pix)) {
1973 xsh_msg("found infinite");
1974 //xsh_msg("sum=%g num=%d",sum,num_good_pix);
1975 //exit(0);
1976 }
1977 //xsh_msg("dest before=%g norm=%g",dest_data1[pix],norm_good_pix);
1978
1979 dest_data1[pix] = norm_good_pix;
1980 dest_qual1[pix] -= QFLAG_INCOMPLETE_NOD;
1981 dest_qual1[pix] |= QFLAG_SCALED_NOD;
1982
1983 } //end check if on overlapping NOD images
1984
1985 } //end loop over bad pix table
1986
1987 ima=cpl_image_wrap_float(nlambda,nslit,dest_data1);
1988 //cpl_image_save(ima, "ima_cor.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_DEFAULT);
1989 cpl_image_unwrap(ima);
1990 cpl_table_delete(tab_bpm);
1991 cleanup:
1992 //xsh_msg("end of function");
1993 return cpl_error_get_code();
1994}
1995
1996
1997/*****************************************************************************/
2009/*****************************************************************************/
2010cpl_frame* xsh_combine_nod( cpl_frameset *nod_frames,
2011 xsh_combine_nod_param * nod_par,
2012 const char * tag,
2014 cpl_frame** res_frame_ext,
2015 const int scale_nod)
2016{
2017 xsh_rec_list *result_list = NULL ;
2018 cpl_frame *result = NULL ;
2019 int nb_frames = 0 ;
2020 int i, no, nb_orders;
2021 float slit_min=999, slit_max=-999;
2022 double slit_step =0.0;
2023 double lambda_min, lambda_max, lambda_step;
2024 xsh_rec_list **rec_input_list = NULL ;
2025 xsh_rec_list *rec_first = NULL;
2026 //float *rec_first_slit = NULL;
2027 float *result_slit = NULL;
2028 int nslit;
2029 char *fname = NULL ;
2030 char *tag_drl = NULL ;
2031 char *fname_drl = NULL ;
2032 int *slit_index = NULL;
2033 int slit_ext_min=-9999;
2034 int slit_ext_max=9999;
2035 cpl_propertylist *header = NULL;
2036 int ncrh_tot=0;
2037
2038 XSH_ASSURE_NOT_NULL( nod_frames);
2039 XSH_ASSURE_NOT_NULL( nod_par);
2041 XSH_ASSURE_NOT_NULL( tag);
2042 XSH_ASSURE_NOT_NULL( res_frame_ext);
2043
2044 /* Create list of_reclist */
2045 check( nb_frames = cpl_frameset_get_size( nod_frames));
2046 xsh_msg( "---xsh_combine_nod (method %s) - Nb frames = %d",
2047 COMBINE_METHOD_PRINT( nod_par->method), nb_frames);
2048
2049 /* create the list of pointers to input rec lists */
2050 XSH_CALLOC( rec_input_list, xsh_rec_list *, nb_frames);
2051
2052 /* Populate the input lists */
2053 for ( i = 0 ; i < nb_frames ; i++ ) {
2054 cpl_frame * nod_frame = NULL ;
2055 xsh_rec_list * list = NULL;
2056
2057 check( nod_frame = cpl_frameset_get_frame( nod_frames, i));
2058 check( list = xsh_rec_list_load( nod_frame, instrument));
2059 rec_input_list[i] = list;
2060 if (cpl_propertylist_has((rec_input_list[i])->header,XSH_QC_CRH_NUMBER) > 0 ){
2061 ncrh_tot += xsh_pfits_get_qc_ncrh((rec_input_list[i])->header);
2062 }
2063
2064
2065 char name[80];
2066
2067 //xsh_msg("no=%d rec_input_list=%p",no,rec_input_list);
2068 sprintf(name,"input_resampled_%d.fits",i);
2069 cpl_frame* frm=xsh_rec_list_save2(rec_input_list[i],name,"test");
2071 xsh_free_frame(&frm);
2072
2073 }
2074 /* Search for limits in slit */
2075 for ( i = 0 ; i < nb_frames ; i++ ) {
2076 float nod_slit_min, nod_slit_max;
2077 xsh_rec_list * list = NULL;
2078 nslit=0;
2079 float* nod_slit = NULL;
2080
2081 list = rec_input_list[i];
2082 check( nslit = xsh_rec_list_get_nslit( list, 0));
2083 check( nod_slit = xsh_rec_list_get_slit( list, 0));
2084 nod_slit_min = nod_slit[0];
2085 nod_slit_max = nod_slit[nslit-1];
2086
2087 if ( nod_slit_min < slit_min){
2088 slit_min = nod_slit_min;
2089 }
2090 if (nod_slit_max > slit_max){
2091 slit_max = nod_slit_max;
2092 }
2093 }
2094 rec_first = rec_input_list[0];
2095 //check( rec_first_slit = xsh_rec_list_get_slit( rec_first, 0));
2096 check( header = xsh_rec_list_get_header( rec_first));
2098 nslit = (slit_max-slit_min)/slit_step+1;
2099
2100 /* Create result slit */
2101 XSH_CALLOC( result_slit, float, nslit);
2102 for ( i = 0 ; i < nslit; i++ ) {
2103 result_slit[i] = slit_min+i*slit_step;
2104 }
2105
2106 xsh_msg("Combine nod slit : (%f,%f) step %f nslit %d", slit_min, slit_max,
2107 slit_step, nslit);
2108
2109 nb_orders = rec_first->size;
2110 check( result_list = xsh_rec_list_create_with_size( nb_orders,
2111 instrument));
2112
2113 for ( no = 0 ; no < nb_orders ; no++ ) {
2114 int absorder, nlambda;
2115 double *dnew = NULL;
2116 double *dold = NULL;
2117 float *dslit = NULL;
2118
2119 absorder = xsh_rec_list_get_order( rec_first, no);
2120 nlambda = xsh_rec_list_get_nlambda( rec_first, no);
2121 check( xsh_rec_list_set_data_size( result_list, no, absorder, nlambda,
2122 nslit));
2123 /* copy lambda */
2124 dold = xsh_rec_list_get_lambda( rec_first, no);
2125 dnew = xsh_rec_list_get_lambda( result_list, no);
2126 memcpy( dnew, dold, nlambda*sizeof( double));
2127 /* copy slit */
2128 dslit = xsh_rec_list_get_slit( result_list, no);
2129 memcpy( dslit, result_slit, nslit*sizeof( float));
2130 }
2131
2132 /* compute slit index */
2133 XSH_CALLOC( slit_index, int, nb_frames);
2134 check( xsh_compute_slit_index( slit_min, slit_step, rec_input_list,
2135 slit_index, nb_frames));
2136
2137
2138
2139 /* Set header from science header */
2140 check( cpl_propertylist_append( result_list->header,
2141 rec_first->header));
2142
2143
2144 /* Now combine */
2145 xsh_msg("Nod combination save extra files to evaluate pixel scaling");
2146 for ( int nf = 0; nf < nb_frames; nf++) {
2147 for ( no = 0; no < nb_orders; no++) {
2148 //xsh_msg("order %d",no);
2149
2150 xsh_rec_list_qc( result_list, rec_input_list, slit_index,
2151 nb_frames, no, nod_par->method,instrument->decode_bp,nf);
2152
2153 }
2154 }
2155
2156 for ( no = 0; no < nb_orders; no++) {
2157
2158 /*
2159 check( xsh_rec_list_fill_gauss( result_list, rec_input_list, slit_index,
2160 nb_frames, no, nod_par->method,instrument->decode_bp) ) ;
2161 */
2162
2163
2164 check( xsh_rec_list_add( result_list, rec_input_list, slit_index,
2165 nb_frames, no, nod_par->method,instrument->decode_bp) ) ;
2166
2167
2168 if(scale_nod>0) {
2169 xsh_msg("combine-nod: scale flagged pixel");
2170 int scale_method=2;
2171 cpl_imagelist* sf = NULL;
2172 cpl_table* tab_bp=NULL;
2173 if(scale_nod>1) {
2174
2175 xsh_msg("filling table");
2176 tab_bp =
2177 xsh_qual2tab_frames(nb_frames,instrument->decode_bp, slit_index, result_list,
2178 rec_input_list, no);
2179 xsh_msg("filling table ok");
2180 } else {
2181
2182
2183 check(sf =xsh_find_image_scale_factors(result_list, rec_input_list,
2184 slit_index, nb_frames, no, nod_par->method,
2185 instrument->decode_bp,scale_method));
2186 }
2187 if (scale_nod>1) {
2188 check(xsh_correct_scale_w(result_list, rec_input_list, slit_index,
2189 nb_frames, no, nod_par->method, instrument->decode_bp, sf,
2190 scale_method, tab_bp));
2191 } else {
2192 check(xsh_correct_scale(result_list, rec_input_list, slit_index,
2193 nb_frames, no, nod_par->method, instrument->decode_bp, sf,
2194 scale_method));
2195 }
2196 xsh_free_imagelist(&sf);
2197
2198 /*
2199 cpl_image* scale_ima;
2200 check( scale_ima=xsh_rec_list_compute_scale2( result_list,
2201 rec_input_list, slit_index, nb_frames, no, nod_par->method,
2202 instrument->decode_bp) ) ;
2203 */
2204
2205 /*
2206 float* data;
2207 float* errs;
2208 int* qual;
2209 data=xsh_rec_list_get_data1(result_list,no);
2210 errs=xsh_rec_list_get_errs1(result_list,no);
2211 qual=xsh_rec_list_get_qual1(result_list,no);
2212 */
2213
2214
2215 //sprintf(name,"result_resampled_%d.fits",no);
2216 //xsh_rec_list_save2(result_list,name,"test");
2217
2218
2219 }
2220
2221
2222
2223
2224 }
2225
2226 /* Now save result list s frame */
2227 fname = xsh_stringcat_any( tag, ".fits", (void*)NULL);
2228 tag_drl = xsh_stringcat_any( tag, "_DRL", (void*)NULL);
2229 fname_drl = xsh_stringcat_any( "DRL_", fname, (void*)NULL);
2230
2231
2232 /* Add bin_lambda and bin_space to property list */
2234 result_list->header));
2236 result_list->header));
2238 result_list->header));
2240 lambda_step));
2242 slit_step));
2243
2244 /* Add lambda min and max (global) */
2245 /* Lambda min is lambda_min of last order */
2246 /* Lambda Max is lambda_max of first order */
2248 lambda_min)) ;
2250 lambda_max));
2251
2252
2253 /* Now compute slit min/max for 1D extraction: AMO: should we control
2254 this bit of code via some parameters??
2255 */
2256
2257 if(1) {
2258 int nf=0;
2259 int from_slit=0;
2260 for ( no = 0; no < nb_orders; no++) {
2261 check( from_slit = xsh_rec_list_get_nslit( rec_input_list[0], no));
2262
2263 for( nf = 0 ; nf < nb_frames ; nf++ ) {
2264 slit_ext_min=(slit_index[nf]>slit_ext_min) ? slit_index[nf]: slit_ext_min;
2265 slit_ext_max=(slit_index[nf]+from_slit<slit_ext_max) ? slit_index[nf]+from_slit: slit_ext_max;
2266
2267 }
2268 }
2269 }
2270
2271 xsh_msg("slit index: min %f max=%f",
2272 slit_min,slit_max);
2273
2274 //result_list->slit_min = slit_min-slit_step/2.0;
2275 //result_list->slit_max = slit_max+slit_step/2.0;
2276 result_list->slit_min = slit_min;
2277 result_list->slit_max = slit_max;
2278
2279 //xsh_rec_list_set_slit_min(result_slit,slit_min);
2280 //xsh_rec_list_set_slit_max(result_slit,slit_max);
2281 /*
2282 xsh_msg("slit index: min %f max=%f",
2283 result_list->slit_min,result_list->slit_max);
2284 */
2285
2286
2288 result_list->slit_min));
2290 result_list->slit_max));
2291 check( xsh_pfits_set_pcatg( result_list->header, tag));
2292
2293 xsh_pfits_set_extract_slit_min(result_list->header,slit_ext_min);
2294 xsh_pfits_set_extract_slit_max(result_list->header,slit_ext_max);
2295 xsh_pfits_set_qc_ncrh_tot(result_list->header,ncrh_tot);
2296 xsh_pfits_set_qc_ncrh_mean(result_list->header,ncrh_tot/nb_frames);
2297
2298 check( *res_frame_ext=xsh_rec_list_save2( result_list, fname, tag));
2299 check( result = xsh_rec_list_save( result_list, fname_drl, tag_drl, 1));
2300 xsh_add_temporary_file(fname_drl);
2301 cleanup:
2303 if (cpl_error_get_code() != CPL_ERROR_NONE){
2304 xsh_free_frame( &result);
2305 xsh_free_frame( res_frame_ext);
2306 }
2307 xsh_rec_list_free( &result_list);
2308 if ( rec_input_list != NULL){
2309 for ( i = 0 ; i < nb_frames ; i++ ) {
2310 xsh_rec_list_free( &rec_input_list[i]);
2311 }
2312 }
2313 XSH_FREE( result_slit);
2314 XSH_FREE( slit_index);
2315 XSH_FREE( tag_drl);
2316 XSH_FREE( fname_drl);
2317 XSH_FREE( fname);
2318 XSH_FREE( rec_input_list);
2319 return result ;
2320}
2321/*****************************************************************************/
2322
static xsh_instrument * instrument
static float slit_step
static float lambda_step
static cpl_imagelist * xsh_compute_scale_factors_by_object(cpl_imagelist *scaled, cpl_image *scale, const int nlambda, const int nslit, const int no, const double smin, const double smax)
cpl_frame * xsh_combine_nod(cpl_frameset *nod_frames, xsh_combine_nod_param *nod_par, const char *tag, xsh_instrument *instrument, cpl_frame **res_frame_ext, const int scale_nod)
static cpl_table * xsh_qual2tab_frames(int nframes, const int code, int *slit_index, xsh_rec_list *dest, xsh_rec_list **from, const int no)
static cpl_imagelist * xsh_compute_scale_factors_pix_pix(cpl_imagelist *scaled, cpl_image *scale, const int nlambda, const int nslit, const int no)
static cpl_imagelist * xsh_compute_scale_factors_by_slice(cpl_imagelist *scaled, cpl_image *scale, const int nlambda, const int nslit, const int no)
void xsh_rec_list_add(xsh_rec_list *dest, xsh_rec_list **from, int *slit_index, int nb_frames, int no, int method, const int decode_bp)
cpl_error_code xsh_correct_scale_w(xsh_rec_list *dest, xsh_rec_list **from, int *slit_index, int nb_frames, int no, int method, const int decode_bp, cpl_imagelist *scale_factors, const int scale_method, const cpl_table *bp)
cpl_error_code xsh_correct_scale(xsh_rec_list *dest, xsh_rec_list **from, int *slit_index, int nb_frames, int no, int method, const int decode_bp, cpl_imagelist *scale_factors, const int scale_method)
static cpl_imagelist * xsh_find_image_scale_factors(xsh_rec_list *dest, xsh_rec_list **from, int *slit_index, int nb_frames, int no, int method, const int decode_bp, int scale_method)
void xsh_compute_slit_index(float slit_min, float slit_step, xsh_rec_list **from, int *slit_index_tab, int size)
static cpl_error_code xsh_rec_list_qc(xsh_rec_list *dest, xsh_rec_list **from, int *slit_index, int nb_frames, int no, int method, const int decode_bp, const int nf)
int xsh_rec_list_get_nslit(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:827
double * xsh_rec_list_get_lambda(xsh_rec_list *list, int idx)
xsh_rec_list * xsh_rec_list_load(cpl_frame *frame, xsh_instrument *instrument)
load an rec list from a frame
Definition: xsh_data_rec.c:289
float * xsh_rec_list_get_data1(xsh_rec_list *list, int idx)
cpl_frame * xsh_rec_list_save(xsh_rec_list *list, const char *filename, const char *tag, int is_temp)
Save a rec list in a frame.
cpl_frame * xsh_rec_list_save2(xsh_rec_list *list, const char *filename, const char *tag)
save an rec list to a frame
cpl_error_code xsh_rec_get_nod_extract_slit_min_max(xsh_rec_list *rlist, const double slit_step, double *smin, double *smax)
xsh_rec_list * xsh_rec_list_create_with_size(int size, xsh_instrument *instr)
Create an empty order list.
Definition: xsh_data_rec.c:147
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
cpl_propertylist * xsh_rec_list_get_header(xsh_rec_list *list)
get header of the table
Definition: xsh_data_rec.c:809
int xsh_rec_list_get_order(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:846
void xsh_rec_list_set_data_size(xsh_rec_list *list, int idx, int absorder, int nlambda, int ns)
Allocate memory for the order idx of the rectify list.
Definition: xsh_data_rec.c:191
int * xsh_rec_list_get_qual1(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_slit(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:884
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
Definition: xsh_data_rec.c:865
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
Definition: xsh_data_rec.c:760
#define check(COMMAND)
Definition: xsh_error.h:71
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#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_rectify_bin_lambda(cpl_propertylist *plist, double value)
WRITE the lambda binning.
Definition: xsh_pfits.c:3188
void xsh_pfits_set_rectify_lambda_max(cpl_propertylist *plist, double value)
WRITE the lambda max value.
Definition: xsh_pfits.c:3236
double xsh_pfits_get_rectify_lambda_min(cpl_propertylist *plist)
find out the rectify lambda min
Definition: xsh_pfits.c:3319
double xsh_pfits_get_rectify_bin_lambda(cpl_propertylist *plist)
find out the rectify lambda binning
Definition: xsh_pfits.c:3284
void xsh_pfits_set_rectify_space_max(cpl_propertylist *plist, double value)
WRITE the space (slit) max value.
Definition: xsh_pfits.c:3268
double xsh_pfits_get_rectify_bin_space(cpl_propertylist *plist)
find out the rectify space (slit) binning
Definition: xsh_pfits.c:3302
void xsh_pfits_set_rectify_space_min(cpl_propertylist *plist, double value)
WRITE the space (slit) min value.
Definition: xsh_pfits.c:3252
void xsh_pfits_set_rectify_bin_space(cpl_propertylist *plist, double value)
WRITE the space (slit) binning.
Definition: xsh_pfits.c:3204
void xsh_pfits_set_rectify_lambda_min(cpl_propertylist *plist, double value)
WRITE the lambda min value.
Definition: xsh_pfits.c:3220
void xsh_pfits_set_extract_slit_min(cpl_propertylist *plist, double value)
WRITE the min slit for extraction.
Definition: xsh_pfits.c:2815
void xsh_pfits_set_extract_slit_max(cpl_propertylist *plist, double value)
WRITE the min slit for extraction.
Definition: xsh_pfits.c:2834
double xsh_pfits_get_rectify_lambda_max(cpl_propertylist *plist)
find out the rectify lambda max
Definition: xsh_pfits.c:3336
int xsh_pfits_get_qc_ncrh(const cpl_propertylist *plist)
Find out the QC NCRH value.
Definition: xsh_pfits_qc.c:74
void xsh_pfits_set_qc_ncrh_mean(cpl_propertylist *plist, const double value)
Write the QC.NCRH.AVG value.
Definition: xsh_pfits_qc.c:771
void xsh_pfits_set_qc_ncrh_tot(cpl_propertylist *plist, const int value)
Write the QC.NCRH.TOT value.
Definition: xsh_pfits_qc.c:788
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
Definition: xsh_utils.c:2345
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
double xsh_vector_get_err_median(cpl_vector *vect)
Computes median error on a data set.
Definition: xsh_utils.c:4319
double xsh_vector_get_err_mean(cpl_vector *vect)
Computes mean error on a data set.
Definition: xsh_utils.c:4354
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
Definition: xsh_utils.c:1925
long xsh_round_double(double x)
Computes round(x)
Definition: xsh_utils.c:4383
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
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
enum combine_method method
double slit_max
Definition: xsh_data_rec.h:83
cpl_propertylist * header
Definition: xsh_data_rec.h:89
double slit_min
Definition: xsh_data_rec.h:82
#define QFLAG_INCOMPLETE_NOD
#define QFLAG_GOOD_PIXEL
#define QFLAG_SCALED_NOD
int m
Definition: xsh_detmon_lg.c:91
const char * method
Definition: xsh_detmon_lg.c:78
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
Definition: xsh_dfs.c:877
@ COMBINE_MEAN_METHOD
#define COMBINE_METHOD_PRINT(method)
#define XSH_QC_CRH_NUMBER
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_MALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:49
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56
cpl_table * xsh_qual2tab(cpl_image *qual, const int code)