X-shooter Pipeline Reference Manual 3.8.15
xsh_extract.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-04-12 14:01:44 $
23 * $Revision: 1.57 $
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_utils_table.h>
48#include <xsh_badpixelmap.h>
49#include <xsh_data_pre.h>
50#include <xsh_dfs.h>
51#include <xsh_pfits.h>
52#include <xsh_error.h>
53#include <xsh_msg.h>
54#include <xsh_fit.h>
55#include <xsh_data_instrument.h>
57#include <xsh_data_rec.h>
58#include <xsh_ifu_defs.h>
59
60#include <cpl.h>
61
62/*-----------------------------------------------------------------------------
63 Functions prototypes
64 -----------------------------------------------------------------------------*/
65#define debug_extraction 0
66/*-----------------------------------------------------------------------------
67 Implementation
68 -----------------------------------------------------------------------------*/
88static cpl_frame *
89xsh_extract_with_tag(cpl_frame * rec_frame, cpl_frame* loc_frame,
91 const char * tag, cpl_frame** res_frame_ext) {
92 cpl_frame *res_frame = NULL;
93 xsh_localization *loc_list = NULL;
94 xsh_rec_list *rec_list = NULL, *result_list = NULL;
95 int i = 0;
96 double slit_step = 0.0;
97 char * fname = NULL;
98 char * fname_drl = NULL;
99 char * tag_drl = NULL;
100 double slit_ext_min = 0;
101 double slit_ext_max = 0;
102 cpl_propertylist* plist = NULL;
103 const char* name = NULL;
104
105 XSH_ASSURE_NOT_NULL( rec_frame);
106
107 xsh_msg_dbg_medium( "Entering xsh_extract_with_tag(%s)", tag);
108 xsh_msg_dbg_medium( "Parameters");
109 xsh_msg_dbg_medium( " method %s", EXTRACT_METHOD_PRINT(extract_par->method));
110
111 /* Load rectified table */
112 check( rec_list = xsh_rec_list_load( rec_frame, instrument));
114
115 if (extract_par->method == NOD_METHOD) {
116 double nod_throw=0;
117
118 name = cpl_frame_get_filename(rec_frame);
119 plist = cpl_propertylist_load(name, 0);
120 slit_ext_min = xsh_pfits_get_extract_slit_min(plist);
121 slit_ext_max = xsh_pfits_get_extract_slit_max(plist) - 2;
122 nod_throw = xsh_pfits_get_nodthrow(plist);
123
124 //xsh_msg("before slit min: %g max: %g",slit_ext_min,slit_ext_max);
125 int nslit = xsh_rec_list_get_nslit(rec_list, 0);
126 int hslit = round(0.5 * nod_throw / slit_step);
127 slit_ext_max = nslit/2 + hslit;
128 slit_ext_min = nslit/2 - hslit;
129
130 //xsh_msg("after slit min: %g max: %g",slit_ext_min,slit_ext_max);
131 xsh_free_propertylist(&plist);
132 }
133
134
135 /* Load localization table */
136 if ((extract_par->method != FULL_METHOD)
137 && (extract_par->method != NOD_METHOD)) {
138 XSH_ASSURE_NOT_NULL( loc_frame);
139 check( loc_list = xsh_localization_load( loc_frame));
140 }check(
141 result_list = xsh_rec_list_create_with_size( rec_list->size, instrument));
142 /* loop over order */
143 for (i = 0; i < rec_list->size; i++) {
144 int order = xsh_rec_list_get_order(rec_list, i);
145 int nslit = xsh_rec_list_get_nslit(rec_list, i);
146 int nlambda = xsh_rec_list_get_nlambda(rec_list, i);
147 float* slit = xsh_rec_list_get_slit(rec_list, i);
148 double* lambda = xsh_rec_list_get_lambda(rec_list, i);
149 float slitmin = slit[0];
150 float slitmax = slit[nslit - 1];
151 double lambdamin = lambda[0];
152 double lambdamax = lambda[nlambda - 1];
153 float *flux = xsh_rec_list_get_data1(rec_list, i);
154 float *errs = xsh_rec_list_get_errs1(rec_list, i);
155 int *qual = xsh_rec_list_get_qual1(rec_list, i);
156 int ilambda, islit;
157 /* result REC Frame */
158 double *res_lambda = NULL;
159 float *res_flux = NULL, *res_errs = NULL;
160 int *res_qual = NULL;
161
163 "order %d nslit %d (%f,%f) nlambda %d (%f %f)", order, nslit, slitmin, slitmax, nlambda, lambdamin, lambdamax);
164
165 check( xsh_rec_list_set_data_size( result_list, i, order, nlambda, 1));
166
167 check( res_lambda = xsh_rec_list_get_lambda( result_list, i));
168 check( res_flux = xsh_rec_list_get_data1( result_list, i));
169 check( res_errs = xsh_rec_list_get_errs1( result_list, i));
170 check( res_qual = xsh_rec_list_get_qual1( result_list, i));
171
172 for (ilambda = 0; ilambda < nlambda; ilambda++) {
173 double slit_lo = 0.0, slit_up = 0.0;
174 double ypix_low, ypix_up;
175
176 int ylow = 0, yup = 0;
177 double y_frac_low, y_frac_up;
178 double fluxval = 0.0;
179 double errval = 0.0;
180 int qualval = 0;
181 int idx;
182
183 /* Localisation method */
184 if (extract_par->method == LOCALIZATION_METHOD) {
185 check(
186 slit_lo = cpl_polynomial_eval_1d( loc_list->edglopoly, lambda[ilambda], NULL));
187 check(
188 slit_up = cpl_polynomial_eval_1d( loc_list->edguppoly, lambda[ilambda], NULL));
189
191 "LOCALIZATION_METHOD get poly ok lambda %f slit_lo %f slit_up %f", lambda[ilambda], slit_lo, slit_up);
192 /* Find limit in slit */
193 ypix_low = (slit_lo - slit[0]) / slit_step;
194 ypix_up = (slit_up - slit[0]) / slit_step;
195
196 if (ypix_low < 0.0) {
197 xsh_msg(
198 "WARNING : localization give low pixel \
199 outside of image : fix it to zero");
200 ypix_low = 0.0;
201 }
202 if (ypix_up >= nslit) {
203 xsh_msg(
204 "WARNING : localization give up pixel \
205 outside of image : fix it to %d", nslit-1);
206 ypix_up = nslit - 1;
207 }
208 xsh_msg_dbg_medium("ypix_low %f ypix_up %f", ypix_low, ypix_up);
209
210 ylow = (int) (ypix_low);
211 yup = (int) (ypix_up);
212
213 y_frac_up = ypix_up - yup;
214 y_frac_low = 1 - (ypix_low - ylow);
215
216 xsh_msg_dbg_medium("ylow %d yup %d", ylow, yup);
217
218 /* consider the fraction of pixels for ylow */
219 idx = ylow * nlambda + ilambda;
220 if (idx >= (nlambda * nslit)) {
222 "Index Out of Image: %d >= %d (ylow: %d, ilambda: %d)", idx, nlambda*nslit, ylow, ilambda);
223 continue;
224 }
225 fluxval += y_frac_low * flux[idx];
226 errval += y_frac_low * (errs[idx] * errs[idx]);
227 qualval |= qual[idx];
228
229 /* Integrate on complete pixels */
230 for (islit = ylow + 1; islit < yup; islit++) {
231 idx = islit * nlambda + ilambda;
232 fluxval += flux[idx];
233 errval += errs[idx]*errs[idx];
234 qualval |= qual[idx];
235 }
236
237 /* consider the fraction of pixels for yup */
238 if (yup < nslit) {
239 idx = yup * nlambda + ilambda;
240 fluxval += y_frac_up * flux[idx];
241 errval += y_frac_up * errs[idx]*errs[idx];
242 qualval |= qual[idx];
243 }
244
245 /* Not used any more */
246 } else if (extract_par->method == FULL_METHOD) {
247 //xsh_msg_warning("ymin=%d ymax=%d",0,nslit);
248 for (islit = 0; islit < nslit; islit++) {
249 idx = islit * nlambda + ilambda;
250 fluxval += flux[idx];
251 errval += errs[idx]*errs[idx];
252 qualval |= qual[idx];
253 }
254
255 /* Nod method */
256 } else if (extract_par->method == NOD_METHOD) {
257 /*
258 xsh_msg_warning("slit_inf=%f slit_upp=%f slit0 %f",
259 slit_ext_min,slit_ext_max,slit[0]);
260 */
261
262 /* original definition of extraction range
263 ylow = (slit_ext_min - slit[0]) / slit_step;
264 yup = (slit_ext_max - slit[0]) / slit_step;
265 */
266
267 ylow = slit_ext_min;
268 yup = slit_ext_max;
269
270 for (islit = ylow; islit <= yup; islit++) {
271 idx = islit * nlambda + ilambda;
272 fluxval += flux[idx];
273 errval += errs[idx]*errs[idx];
274 qualval |= qual[idx];
275 }
276 }
277 res_lambda[ilambda] = lambda[ilambda];
278 res_flux[ilambda] = fluxval;
279 res_errs[ilambda] = sqrt(errval);
280 res_qual[ilambda] = qualval;
281 }
282 }
283
284 check( cpl_propertylist_append ( result_list->header, rec_list->header));
285 check( xsh_pfits_set_pcatg( result_list->header, tag));
286 fname = xsh_stringcat_any(tag, ".fits", (void*)NULL);
287 tag_drl = xsh_stringcat_any(tag, "_DRL", (void*)NULL);
288 fname_drl = xsh_stringcat_any(tag_drl, ".fits", (void*)NULL);
289
290 check( *res_frame_ext= xsh_rec_list_save2( result_list,fname, tag));
291 check( res_frame = xsh_rec_list_save( result_list, fname_drl, tag_drl,0));
292
293 xsh_msg_dbg_medium("%s created", fname);
294
295 cleanup: xsh_rec_list_free(&result_list);
296 xsh_rec_list_free(&rec_list);
297 xsh_localization_free(&loc_list);
298
299 XSH_FREE( tag_drl);
300 XSH_FREE( fname_drl);
301 XSH_FREE( fname);
302
303 return res_frame;
304}
305
306static cpl_error_code
308 const double lambda, const double slit_0, const double slit_step,
309 const int nslit, int* ylow, int* yup,double* y_frac_up,double* y_frac_low) {
310 double slit_lo = 0;
311 double slit_up = 0;
312 double ypix_low = 0;
313 double ypix_up = 0;
314
315 slit_lo = cpl_polynomial_eval_1d(loc_list->edglopoly, lambda, NULL);
316 slit_up = cpl_polynomial_eval_1d(loc_list->edguppoly, lambda, NULL);
317
319 "LOCALIZATION_METHOD get poly ok lambda %f slit_lo %f slit_up %f", lambda, slit_lo, slit_up);
320 /* Find limit in slit */
321 ypix_low = (slit_lo - slit_0) / slit_step;
322 ypix_up = (slit_up - slit_0) / slit_step;
323
324 if (ypix_low < 0.0) {
325 xsh_msg(
326 "WARNING : localization give low pixel \
327 outside of image : fix it to zero");
328 ypix_low = 0.0;
329 }
330 if (ypix_up >= nslit) {
331 xsh_msg(
332 "WARNING : localization give up pixel \
333 outside of image : fix it to %d", nslit-1);
334 ypix_up = nslit - 1;
335 }
336 xsh_msg_dbg_medium("ypix_low %f ypix_up %f", ypix_low, ypix_up);
337
338 *ylow = (int) (ypix_low);
339 *yup = (int) (ypix_up);
340
341 *y_frac_up = ypix_up - *yup;
342 *y_frac_low = 1 - (ypix_low - *ylow);
343 return cpl_error_get_code();
344}
345
346cpl_error_code xsh_extract_clean_slice(const float* flux, const float* errs,
347 int* qual, const double* lambda,
348 const int ilambda, xsh_instrument* instrument, const int slit_min,
349 const int slit_max, const int nlambda, const int nslit,
350 const int mask_hsize, double* fluxval, double* errval, int* qualval,
351 float* pima,float* fima,float* rima) {
352
353 int mask_size = 0;
354
355 int nbad = 0;
356 int islit = 0;
357 int idx = 0;
358 const int slit_size = slit_max - slit_min + 1;
359 double den_fct = 0;
360 double num_fct = 0;
361 int mdx = 0;
362 int m = 0;
363
364 float* prof = NULL;
365
366 double ipol_fct = 0;
367 cpl_vector* vec_wave_flag=NULL;
368 double* pflag=NULL;
369 int nflag=0;
370 double inv_err2=0;
371 double err2=0;
372 int ilambda_min=0;
373 int ilambda_max=0;
374 int mstart=0;
375 int mend=0;
376 //int code_extrap=1048576;
377 //int code_special=2143289343;
378 //int code_special=2147483647;
379 int decode_bp = instrument->decode_bp;
380 //decode_bp = code_special;
381 /* check if there are bad pixels to be interpolated, else we can integrate */
382 for (islit = slit_min; islit <= slit_max; islit++) {
383 idx = islit * nlambda + ilambda;
384 if (( qual[idx] & decode_bp ) > 0) {
385 //if ((qual[idx] & code_special) > 0) {
386 nbad++;
387 }
388 }
389
390 //xsh_msg_debug("lambda=%g nbad=%d",lambda[ilambda],nbad);
391 if (nbad == slit_size || nbad == 0) {
392 /* only good pixel, trivial case, we can sum up */
393 for (islit = slit_min; islit <= slit_max; islit++) {
394 idx = islit * nlambda + ilambda;
395 //xsh_msg("flux=%g",flux[idx]);
396 *fluxval += flux[idx];
397 *errval += (errs[idx] * errs[idx]);
398 *qualval |= qual[idx];
399#if debug_extraction
400 fima[idx]=flux[idx];
401 rima[idx]=(flux[idx]-flux[idx])/errs[idx];
402#endif
403 }
404 } else { // 0<nbad<nslit
405 xsh_msg_debug("Found %d bad pix at lambda=%g", nbad, lambda[ilambda]);
406
407 /* determine the wavelength index limits for integration */
408 ilambda_min=ilambda-mask_hsize;
409 ilambda_max=ilambda+mask_hsize;
410 ilambda_min = (ilambda_min>=0) ? ilambda_min : 0;
411 ilambda_max = (ilambda_max<nlambda) ? ilambda_max : nlambda-1;
412
413 mask_size=ilambda_max-ilambda_min+1;
414 mstart=ilambda_min-ilambda;
415 mend=ilambda_max-ilambda;
416
417 /* This part of the code is flagging wavelength with at least one bad pixel */
418 xsh_free_vector(&vec_wave_flag);
419 vec_wave_flag=cpl_vector_new(mask_size);
420 pflag=cpl_vector_get_data(vec_wave_flag);
421 for (m = 0; m < mask_size; m++) {
422 pflag[m]=0;
423 }
424 nflag=0;
425 for (m = mstart; m <= mend; m++) {
426 for (islit = slit_min; islit <= slit_max; islit++) {
427 mdx = islit * nlambda + ilambda + m;
428 //xsh_msg("qual[%d,%d]=%d",m,islit,qual[mdx]);
429 if (( qual[mdx] & decode_bp ) > 0) {
430
431 //if ((qual[mdx] & code_special) > 0) {
432 pflag[m-mstart]=1;
433 nflag++;
434 break;
435 }
436 }
437 }
438
439 //xsh_msg("m_start=%d m_end=%d",mstart,mend);
440
441
442 /* if all wavelength within the extraction mask have at least one bad pixel then throw exception */
443 if (nflag == mask_size) {
444 /* 2a: exception: Too few good pixels for interpolation, flag this pixel */
445 //xsh_msg_warning("Too few good pixels for interpolation at lambda %g. Try to increase interpolation box parameter", lambda[ilambda]);
446 *qualval = QFLAG_MISSING_DATA;
447
448 } else {
449 //xsh_msg_warning("Determine interpolation profile at lambda %g.", lambda[ilambda]);
450 /* 3: create profile */
451 /* at least one good wavelength==>that means we can define an interpolation profile */
452 prof = cpl_malloc(nslit * sizeof(float));
453 for (islit = 0; islit < nslit; islit++) {
454 prof[islit] = 0;
455 }
456
457 /* build profile using good wavelength */
458 // For every wavelength coord
459 for (m = mstart; m <= mend; m++) {
460 if (pflag[m-mstart] == 0) {
461 // For every spatial position *across* that wavelength
462 for (islit = slit_min; islit <= slit_max; islit++) {
463 mdx = islit * nlambda + ilambda+m;
464 // Profile at that spatial position includes this flux
465 prof[islit] += flux[mdx];
466 }
467 }
468 }
469
470 /* use the profile to calculate the interpolation factor */
471 num_fct = 0;
472 den_fct = 0;
473 for (islit = slit_min; islit <= slit_max; islit++) {
474 idx = islit * nlambda + ilambda;
475 if (( qual[idx] & decode_bp ) == 0) {
476 //if ((qual[idx] & code_special) == 0) {
477 err2 = errs[idx] * errs[idx];
478 inv_err2=1./ err2;
479 num_fct += prof[islit] * flux[idx] * inv_err2;
480 den_fct += prof[islit] * prof[islit] * inv_err2 ;
481 }
482#if debug_extraction
483 pima[idx]=prof[islit];
484#endif
485 }/* end loop over slit */
486 ipol_fct = num_fct / den_fct;
487
488 /* finally apply interpolation */
489 //xsh_msg("slit_min=%d slit_max=%d",slit_min,slit_max);
490 for (islit = slit_min; islit <= slit_max; islit++) {
491 idx = islit * nlambda + ilambda;
492 if ((qual[idx] & decode_bp ) == 0) {
493 //if ((qual[idx] & code_special) == 0) {
494 *fluxval += flux[idx];
495 *errval += (errs[idx] * errs[idx]);
496 *qualval |= qual[idx];
497#if debug_extraction
498 fima[idx]=flux[idx];
499 rima[idx]=(flux[idx]-flux[idx])/errs[idx];
500#endif
501 } else {
502 *fluxval += ipol_fct * prof[islit];
503 *errval += (prof[islit] * prof[islit])/ den_fct;
504 *qualval |= QFLAG_INTERPOL_FLUX;
505#if debug_extraction
506 fima[idx]=ipol_fct * prof[islit];
507 rima[idx]=(flux[idx]-ipol_fct * prof[islit])/((prof[islit] * prof[islit])/ den_fct);
508#endif
509 }
510 }
511
512 XSH_FREE(prof);
513 } /* end case at least some bad pixels is found
514 but not too many to interpolate bad ones */
515 xsh_free_vector(&vec_wave_flag);
516
517 }/* end case where we can try to interpolate a bad pixel */
518 //xsh_msg("ck3 flux=%g err=%g qual=%d",fluxval,errval,qual);
519
520 return cpl_error_get_code();
521
522}
523
524
544static cpl_frame *
545xsh_extract_clean_with_tag(cpl_frame * rec_frame, cpl_frame* loc_frame,
547 xsh_interpolate_bp_param * ipol_bp_par, const char * tag,
548 cpl_frame** res_frame_ext) {
549 cpl_frame *res_frame = NULL;
550 xsh_localization *loc_list = NULL;
551 xsh_rec_list *rec_list = NULL, *result_list = NULL;
552 int i = 0;
553 double slit_step = 0.0;
554 char * fname = NULL;
555 char * fname_drl = NULL;
556 char * tag_drl = NULL;
557 double slit_ext_min = 0;
558 double slit_ext_max = 0;
559 cpl_propertylist* plist = NULL;
560 const char* name = NULL;
561
562 XSH_ASSURE_NOT_NULL( rec_frame);
563 xsh_msg("Extract method clean with tag");
564
565 xsh_msg_dbg_medium( "Entering xsh_extract_with_tag(%s)", tag);
566 xsh_msg_dbg_medium( "Parameters");
567 xsh_msg_dbg_medium( " method %s", EXTRACT_METHOD_PRINT(extract_par->method));
568
569 /* Load rectified table */
570 check( rec_list = xsh_rec_list_load( rec_frame, instrument));
572 if (extract_par->method == NOD_METHOD) {
573 name = cpl_frame_get_filename(rec_frame);
574 plist = cpl_propertylist_load(name, 0);
575 slit_ext_min = xsh_pfits_get_extract_slit_min(plist);
576 slit_ext_max = xsh_pfits_get_extract_slit_max(plist) - 2;
577 double nod_throw = xsh_pfits_get_nodthrow(plist);
578 //xsh_msg("before slit min: %g max: %g",slit_ext_min,slit_ext_max);
579 int nslit = xsh_rec_list_get_nslit(rec_list, 0);
580 int hslit =0;
581 if (nod_throw <= 5.5 ) {
582 hslit = round(0.5 * nod_throw / slit_step);
583 } else {
584 hslit = round(0.5 * (11.-nod_throw) / slit_step);
585 }
586 xsh_msg("nod_throw=%g slit_step=%g nslit=%d hslit=%d",nod_throw,slit_step,nslit,hslit);
587 slit_ext_max = nslit/2 + hslit;
588 slit_ext_min = nslit/2 - hslit;
589 xsh_msg("after slit min: %g max: %g",slit_ext_min,slit_ext_max);
590 xsh_free_propertylist(&plist);
591 }
592
593
594
595 /* Load localization table */
596 if ((extract_par->method != FULL_METHOD)
597 && (extract_par->method != NOD_METHOD)) {
598 XSH_ASSURE_NOT_NULL( loc_frame);
599 xsh_msg("loc_frame name=%s", cpl_frame_get_filename(loc_frame));
600 check( loc_list = xsh_localization_load( loc_frame));
601 }check(
602 result_list = xsh_rec_list_create_with_size( rec_list->size, instrument));
603 int mask_hsize = ipol_bp_par->mask_hsize;
604 /* loop over order */
605 xsh_msg("Number of orders=%d", rec_list->size);
606 for (i = 0; i < rec_list->size; i++) {
607 int order = xsh_rec_list_get_order(rec_list, i);
608 int nslit = xsh_rec_list_get_nslit(rec_list, i);
609 int nlambda = xsh_rec_list_get_nlambda(rec_list, i);
610 float* slit = xsh_rec_list_get_slit(rec_list, i);
611 double* lambda = xsh_rec_list_get_lambda(rec_list, i);
612 float slitmin = slit[0];
613 float slitmax = slit[nslit - 1];
614 double lambdamin = lambda[0];
615 double lambdamax = lambda[nlambda - 1];
616 float *flux = xsh_rec_list_get_data1(rec_list, i);
617 float *errs = xsh_rec_list_get_errs1(rec_list, i);
618 int *qual = xsh_rec_list_get_qual1(rec_list, i);
619 int ilambda, islit;
620 /* result REC Frame */
621 double *res_lambda = NULL;
622 float *res_flux = NULL, *res_errs = NULL;
623 int *res_qual = NULL;
624 float* pima=NULL;
625 float* fima=NULL;
626 float* rima=NULL;
627#if debug_extraction
628 cpl_image* prof_ima =cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
629 cpl_image* flux_ima =cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
630 cpl_image* res_ima =cpl_image_new(nlambda,nslit,CPL_TYPE_FLOAT);
631 pima=cpl_image_get_data_float(prof_ima);
632 fima=cpl_image_get_data_float(flux_ima);
633 rima=cpl_image_get_data_float(res_ima);
634#endif
636 "order %d nslit %d (%f,%f) nlambda %d (%f %f)", order, nslit, slitmin, slitmax, nlambda, lambdamin, lambdamax);
637
638 check( xsh_rec_list_set_data_size( result_list, i, order, nlambda, 1));
639
640 check( res_lambda = xsh_rec_list_get_lambda( result_list, i));
641 check( res_flux = xsh_rec_list_get_data1( result_list, i));
642 check( res_errs = xsh_rec_list_get_errs1( result_list, i));
643 check( res_qual = xsh_rec_list_get_qual1( result_list, i));
644 /* loop over wavelength */
645
646
647 for (ilambda = 0; ilambda < nlambda; ilambda++) {
648
649 int ylow = 0, yup = 0;
650 double y_frac_low, y_frac_up;
651 double fluxval = 0.0;
652 double errval = 0.0;
653 int qualval = 0;
654 int idx;
655 /* Localisation method */
656 if (extract_par->method == LOCALIZATION_METHOD) {
657
658 xsh_extraction_get_slit_minmax(loc_list, lambda[ilambda], slit[0],
659 slit_step, nslit, &ylow, &yup, &y_frac_up, &y_frac_low);
660
661 xsh_msg_dbg_medium("ylow %d yup %d", ylow, yup);
662
663 if (mask_hsize <= 0) {
664 /* consider the fraction of pixels for ylow */
665 idx = ylow * nlambda + ilambda;
666 if (idx >= (nlambda * nslit)) {
668 "Index Out of Image: %d >= %d (ylow: %d, ilambda: %d)", idx, nlambda*nslit, ylow, ilambda);
669 continue;
670 }
671 fluxval += y_frac_low * flux[idx];
672 errval += y_frac_low * (errs[idx] * errs[idx]);
673 qualval |= qual[idx];
674
675 /* Integrate on complete pixels */
676 if(i==0 && ilambda==0) {
677 xsh_msg("Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
678 ylow+1,yup-1,slit_step*(ylow+1)+slit[0],slit_step*(yup-1)+slit[0]);
679 }
680 for (islit = ylow + 1; islit < yup; islit++) {
681 idx = islit * nlambda + ilambda;
682 fluxval += flux[idx];
683 errval += errs[idx] * errs[idx];
684 qualval |= qual[idx];
685 }
686
687 /* consider the fraction of pixels for yup */
688 if (yup < nslit) {
689 idx = yup * nlambda + ilambda;
690 fluxval += y_frac_up * flux[idx];
691 errval += y_frac_up * errs[idx] * errs[idx];
692 qualval |= qual[idx];
693 }
694
695 } else {
696 int slit_min = ylow + 1;
697 int slit_max = yup;
698 if(i==0 && ilambda==0) {
699 xsh_msg("Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
700 ylow+1,yup,slit_step*(ylow+1)+slit[0],slit_step*(yup)+slit[0]);
701 }
702 xsh_extract_clean_slice(flux, errs, qual, lambda,
703 ilambda, instrument, slit_min, slit_max, nlambda, nslit,
704 mask_hsize, &fluxval, &errval, &qualval,pima,fima,rima);
705
706 }
707
708 /* Nod method */
709 } else if (extract_par->method == NOD_METHOD) {
710
711 /*
712 xsh_msg_warning("slit_inf=%f slit_upp=%f slit0 %f",
713 xsh_utils_scired_slit.c:slit_ext_min,slit_ext_max,slit[0]);
714 */
715
716 /* original definition of extraction range
717 ylow = (slit_ext_min - slit[0]) / slit_step;
718 yup = (slit_ext_max - slit[0]) / slit_step;
719 */
720
721 ylow = slit_ext_min;
722 yup = slit_ext_max;
723 //xsh_msg("mask_hsize=%d",mask_hsize);
724 if (mask_hsize <= 0) {
725 if(i == 0 && ilambda==0) {
726 xsh_msg("Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
727 ylow,yup,slit_step*(ylow)+slit[0],slit_step*(yup)+slit[0]);
728 }
729 for (islit = ylow; islit <= yup; islit++) {
730 idx = islit * nlambda + ilambda;
731 fluxval += flux[idx];
732 errval += errs[idx] * errs[idx];
733 qualval |= qual[idx];
734 }
735
736 } else {
737
738 int slit_min = ylow + 1;
739 int slit_max = yup;
740 if(i==0 && ilambda==0) {
741 xsh_msg("Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
742 ylow+1,yup,slit_step*(ylow+1)+slit[0],slit_step*(yup)+slit[0]);
743 }
744 xsh_extract_clean_slice(flux, errs, qual, lambda,
745 ilambda, instrument, slit_min, slit_max, nlambda, nslit,
746 mask_hsize, &fluxval, &errval, &qualval,pima,fima,rima);
747 }
748 }
749 res_lambda[ilambda] = lambda[ilambda];
750 res_flux[ilambda] = fluxval;
751 res_errs[ilambda] = sqrt(errval);
752 res_qual[ilambda] = qualval;
753 }
754#if debug_extraction
755 //xsh_msg("saving image profile");
756 char prof_name[80];
757 sprintf(prof_name,"prof_ord_%d.fits",i);
758 cpl_image_save(prof_ima, prof_name, CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
759 sprintf(prof_name,"flux_ord_%d.fits",i);
760 cpl_image_save(flux_ima, prof_name, CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
761 sprintf(prof_name,"res_ord_%d.fits",i);
762 cpl_image_save(res_ima, prof_name, CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
763 cpl_image_delete(prof_ima);
764 cpl_image_delete(flux_ima);
765 cpl_image_delete(res_ima);
766#endif
767
768 }
769
770 check( cpl_propertylist_append ( result_list->header, rec_list->header));
771 check( xsh_pfits_set_pcatg( result_list->header, tag));
772 fname = xsh_stringcat_any(tag, ".fits", (void*)NULL);
773 tag_drl = xsh_stringcat_any(tag, "_DRL", (void*)NULL);
774 fname_drl = xsh_stringcat_any(tag_drl, ".fits", (void*)NULL);
775
776 check( *res_frame_ext= xsh_rec_list_save2( result_list,fname, tag));
777 check( res_frame = xsh_rec_list_save( result_list, fname_drl, tag_drl,0));
778
779 xsh_msg_dbg_medium("%s created", fname);
780
781 cleanup: xsh_rec_list_free(&result_list);
782 xsh_rec_list_free(&rec_list);
783 xsh_localization_free(&loc_list);
784
785 XSH_FREE( tag_drl);
786 XSH_FREE( fname_drl);
787 XSH_FREE( fname);
788
789 return res_frame;
790}
791
792
793
794
795
807cpl_frame*
808xsh_extract(cpl_frame * rec_frame, cpl_frame* loc_frame,
810 cpl_frame** res_frame_ext, const char* rec_prefix) {
811 cpl_frame *res_frame = NULL;
812 char tag[256];
813
814 sprintf(tag, "%s_%s", rec_prefix,
816
817 check( res_frame = xsh_extract_with_tag( rec_frame, loc_frame, instrument,
818 extract_par, tag,res_frame_ext ) );
819 xsh_add_temporary_file(cpl_frame_get_filename(res_frame));
820 cleanup: return res_frame;
821}
822
835cpl_frame*
836xsh_extract_clean(cpl_frame * rec_frame, cpl_frame* loc_frame,
838 xsh_interpolate_bp_param * ipol_bp_par, cpl_frame** res_frame_ext,
839 const char* rec_prefix) {
840 cpl_frame *res_frame = NULL;
841 char tag[256];
842
843 sprintf(tag, "%s_%s", rec_prefix,
845
846 check(
847 res_frame=xsh_extract_clean_with_tag( rec_frame, loc_frame, instrument, extract_par, ipol_bp_par, tag,res_frame_ext ));
848
849 /*
850 check( res_frame = xsh_extract_with_tag( rec_frame, loc_frame, instrument,
851 extract_par, tag,res_frame_ext ) );
852
853 */
854 xsh_add_temporary_file(cpl_frame_get_filename(res_frame));
855 cleanup: return res_frame;
856}
857
868cpl_frameset * xsh_extract_ifu(cpl_frameset * rec_frameset,
869 cpl_frameset * loc_frameset, xsh_instrument* instrument,
870 xsh_extract_param * extract_par, const char* rec_prefix) {
871 cpl_frameset * result_set = NULL;
872 int slitlet;
873 int i;
874
875 xsh_msg( " Entering xsh_extract_ifu");
876 XSH_ASSURE_NOT_NULL( rec_frameset);
877 //XSH_ASSURE_NOT_NULL( loc_frameset ) ;
879 XSH_ASSURE_NOT_NULL( extract_par);
880
881 check( result_set = cpl_frameset_new());
882
883 /* Loop over the 3 IFU slitlets */
884 for (i = 0, slitlet = LOWER_IFU_SLITLET; slitlet <= UPPER_IFU_SLITLET;
885 slitlet++, i++) {
886 cpl_frame * loc_frame = NULL;
887 cpl_frame * rec_frame = NULL;
888 cpl_frame * ext_frame = NULL;
889 cpl_frame * ext_frame_eso = NULL;
890 char tag[256];
891 const char * tag_suf = NULL;
892
893 switch (slitlet) {
895 tag_suf = XSH_GET_TAG_FROM_ARM( XSH_ORDER1D_DOWN_IFU,
896 instrument);
897 break;
899 tag_suf = XSH_GET_TAG_FROM_ARM( XSH_ORDER1D_CEN_IFU,
900 instrument);
901 break;
903 tag_suf = XSH_GET_TAG_FROM_ARM( XSH_ORDER1D_UP_IFU,
904 instrument);
905 break;
906 }
907 sprintf(tag, "%s_%s", rec_prefix, tag_suf);
908 xsh_msg( " Slitlet %s", SlitletName[slitlet]);
909 check( rec_frame = cpl_frameset_get_frame( rec_frameset, i ));
910 xsh_msg( " REC Frame '%s' Got", cpl_frame_get_filename( rec_frame ));
911
912 if (loc_frameset != NULL) {
913 check( loc_frame = cpl_frameset_get_frame( loc_frameset, i ));
914 xsh_msg( " LOC Frame '%s' Got", cpl_frame_get_filename( loc_frame ));
915 } else {
916 xsh_msg_dbg_medium( "No localization table");
917 }
918
919 /* Now extract this slitlet */xsh_msg( "Calling xsh_extract");
920 check(
921 ext_frame = xsh_extract_with_tag( rec_frame, loc_frame, instrument, extract_par, tag,&ext_frame_eso));
922 xsh_msg( "Extracted frame '%s'", cpl_frame_get_filename( ext_frame ));
923 check( cpl_frameset_insert( result_set, ext_frame_eso ));
924 check( cpl_frameset_insert( result_set, ext_frame ));
925 }
926
927 cleanup:
928 xsh_msg( "Exit xsh_extract_ifu");
929 return result_set;
930}
931
932/*--------------------------------------------------------------------------*/
933
static xsh_instrument * instrument
static float slit_step
xsh_localization * xsh_localization_load(cpl_frame *frame)
Load a localization list from a frame.
void xsh_localization_free(xsh_localization **list)
free memory associated to a localization_list
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
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)
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
cpl_frame * xsh_extract(cpl_frame *rec_frame, cpl_frame *loc_frame, xsh_instrument *instrument, xsh_extract_param *extract_par, cpl_frame **res_frame_ext, const char *rec_prefix)
simple 1D extraction of point source like object
Definition: xsh_extract.c:808
static cpl_error_code xsh_extraction_get_slit_minmax(xsh_localization *loc_list, const double lambda, const double slit_0, const double slit_step, const int nslit, int *ylow, int *yup, double *y_frac_up, double *y_frac_low)
Definition: xsh_extract.c:307
cpl_error_code xsh_extract_clean_slice(const float *flux, const float *errs, int *qual, const double *lambda, const int ilambda, xsh_instrument *instrument, const int slit_min, const int slit_max, const int nlambda, const int nslit, const int mask_hsize, double *fluxval, double *errval, int *qualval, float *pima, float *fima, float *rima)
Definition: xsh_extract.c:346
cpl_frame * xsh_extract_clean(cpl_frame *rec_frame, cpl_frame *loc_frame, xsh_instrument *instrument, xsh_extract_param *extract_par, xsh_interpolate_bp_param *ipol_bp_par, cpl_frame **res_frame_ext, const char *rec_prefix)
simple 1D extraction of point source like object
Definition: xsh_extract.c:836
cpl_frameset * xsh_extract_ifu(cpl_frameset *rec_frameset, cpl_frameset *loc_frameset, xsh_instrument *instrument, xsh_extract_param *extract_par, const char *rec_prefix)
Loop on IFU Frames to extract simple 1D (1 per slitlet)
Definition: xsh_extract.c:868
static cpl_frame * xsh_extract_clean_with_tag(cpl_frame *rec_frame, cpl_frame *loc_frame, xsh_instrument *instrument, xsh_extract_param *extract_par, xsh_interpolate_bp_param *ipol_bp_par, const char *tag, cpl_frame **res_frame_ext)
Simple 1D extraction of point source like object.
Definition: xsh_extract.c:545
static cpl_frame * xsh_extract_with_tag(cpl_frame *rec_frame, cpl_frame *loc_frame, xsh_instrument *instrument, xsh_extract_param *extract_par, const char *tag, cpl_frame **res_frame_ext)
Simple 1D extraction of point source like object.
Definition: xsh_extract.c:89
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#define xsh_msg_debug(...)
Print a debug message.
Definition: xsh_msg.h:99
#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
double xsh_pfits_get_nodthrow(const cpl_propertylist *plist)
Get the Nod Throw value.
Definition: xsh_pfits.c:4199
double xsh_pfits_get_rectify_bin_space(cpl_propertylist *plist)
find out the rectify space (slit) binning
Definition: xsh_pfits.c:3302
double xsh_pfits_get_extract_slit_max(cpl_propertylist *plist)
find out the extraction min slit
Definition: xsh_pfits.c:3052
double xsh_pfits_get_extract_slit_min(cpl_propertylist *plist)
find out the extraction min slit
Definition: xsh_pfits.c:3033
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
Definition: xsh_utils.c:1925
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
enum extract_method method
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_propertylist * header
Definition: xsh_data_rec.h:89
#define QFLAG_MISSING_DATA
#define QFLAG_INTERPOL_FLUX
int m
Definition: xsh_detmon_lg.c:91
int order
Definition: xsh_detmon_lg.c:80
#define XSH_ORDER1D
Definition: xsh_dfs.h:587
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
@ CENTER_IFU_SLITLET
Definition: xsh_ifu_defs.h:42
@ LOWER_IFU_SLITLET
Definition: xsh_ifu_defs.h:42
@ UPPER_IFU_SLITLET
Definition: xsh_ifu_defs.h:42
static const char * SlitletName[]
Definition: xsh_ifu_defs.h:48
#define EXTRACT_METHOD_PRINT(method)
@ FULL_METHOD
@ NOD_METHOD
@ LOCALIZATION_METHOD
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92