65#define debug_extraction 0
91 const char * tag, cpl_frame** res_frame_ext) {
92 cpl_frame *res_frame = 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;
118 name = cpl_frame_get_filename(rec_frame);
119 plist = cpl_propertylist_load(name, 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;
143 for (i = 0; i < rec_list->
size; i++) {
149 float slitmin = slit[0];
150 float slitmax = slit[nslit - 1];
151 double lambdamin = lambda[0];
152 double lambdamax = lambda[nlambda - 1];
158 double *res_lambda = NULL;
159 float *res_flux = NULL, *res_errs = NULL;
160 int *res_qual = NULL;
163 "order %d nslit %d (%f,%f) nlambda %d (%f %f)",
order, nslit, slitmin, slitmax, nlambda, lambdamin, lambdamax);
172 for (ilambda = 0; ilambda < nlambda; ilambda++) {
173 double slit_lo = 0.0, slit_up = 0.0;
174 double ypix_low, ypix_up;
176 int ylow = 0, yup = 0;
177 double y_frac_low, y_frac_up;
178 double fluxval = 0.0;
186 slit_lo = cpl_polynomial_eval_1d( loc_list->
edglopoly, lambda[ilambda], NULL));
188 slit_up = cpl_polynomial_eval_1d( loc_list->
edguppoly, lambda[ilambda], NULL));
191 "LOCALIZATION_METHOD get poly ok lambda %f slit_lo %f slit_up %f", lambda[ilambda], slit_lo, slit_up);
193 ypix_low = (slit_lo - slit[0]) /
slit_step;
194 ypix_up = (slit_up - slit[0]) /
slit_step;
196 if (ypix_low < 0.0) {
198 "WARNING : localization give low pixel \
199 outside of image : fix it to zero");
202 if (ypix_up >= nslit) {
204 "WARNING : localization give up pixel \
205 outside of image : fix it to %d", nslit-1);
210 ylow = (int) (ypix_low);
211 yup = (int) (ypix_up);
213 y_frac_up = ypix_up - yup;
214 y_frac_low = 1 - (ypix_low - 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);
225 fluxval += y_frac_low * flux[idx];
226 errval += y_frac_low * (errs[idx] * errs[idx]);
227 qualval |= qual[idx];
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];
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];
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];
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];
277 res_lambda[ilambda] = lambda[ilambda];
278 res_flux[ilambda] = fluxval;
279 res_errs[ilambda] = sqrt(errval);
280 res_qual[ilambda] = qualval;
284 check( cpl_propertylist_append ( result_list->header, rec_list->
header));
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) {
315 slit_lo = cpl_polynomial_eval_1d(loc_list->
edglopoly, lambda, NULL);
316 slit_up = cpl_polynomial_eval_1d(loc_list->
edguppoly, lambda, NULL);
319 "LOCALIZATION_METHOD get poly ok lambda %f slit_lo %f slit_up %f", lambda, slit_lo, slit_up);
321 ypix_low = (slit_lo - slit_0) /
slit_step;
322 ypix_up = (slit_up - slit_0) /
slit_step;
324 if (ypix_low < 0.0) {
326 "WARNING : localization give low pixel \
327 outside of image : fix it to zero");
330 if (ypix_up >= nslit) {
332 "WARNING : localization give up pixel \
333 outside of image : fix it to %d", nslit-1);
338 *ylow = (int) (ypix_low);
339 *yup = (int) (ypix_up);
341 *y_frac_up = ypix_up - *yup;
342 *y_frac_low = 1 - (ypix_low - *ylow);
343 return cpl_error_get_code();
347 int* qual,
const double* lambda,
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) {
358 const int slit_size = slit_max - slit_min + 1;
367 cpl_vector* vec_wave_flag=NULL;
382 for (islit = slit_min; islit <= slit_max; islit++) {
383 idx = islit * nlambda + ilambda;
384 if (( qual[idx] & decode_bp ) > 0) {
391 if (nbad == slit_size || nbad == 0) {
393 for (islit = slit_min; islit <= slit_max; islit++) {
394 idx = islit * nlambda + ilambda;
396 *fluxval += flux[idx];
397 *errval += (errs[idx] * errs[idx]);
398 *qualval |= qual[idx];
401 rima[idx]=(flux[idx]-flux[idx])/errs[idx];
405 xsh_msg_debug(
"Found %d bad pix at lambda=%g", nbad, lambda[ilambda]);
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;
413 mask_size=ilambda_max-ilambda_min+1;
414 mstart=ilambda_min-ilambda;
415 mend=ilambda_max-ilambda;
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++) {
425 for (
m = mstart;
m <= mend;
m++) {
426 for (islit = slit_min; islit <= slit_max; islit++) {
427 mdx = islit * nlambda + ilambda +
m;
429 if (( qual[mdx] & decode_bp ) > 0) {
443 if (nflag == mask_size) {
452 prof = cpl_malloc(nslit *
sizeof(
float));
453 for (islit = 0; islit < nslit; islit++) {
459 for (
m = mstart;
m <= mend;
m++) {
460 if (pflag[
m-mstart] == 0) {
462 for (islit = slit_min; islit <= slit_max; islit++) {
463 mdx = islit * nlambda + ilambda+
m;
465 prof[islit] += flux[mdx];
473 for (islit = slit_min; islit <= slit_max; islit++) {
474 idx = islit * nlambda + ilambda;
475 if (( qual[idx] & decode_bp ) == 0) {
477 err2 = errs[idx] * errs[idx];
479 num_fct += prof[islit] * flux[idx] * inv_err2;
480 den_fct += prof[islit] * prof[islit] * inv_err2 ;
483 pima[idx]=prof[islit];
486 ipol_fct = num_fct / den_fct;
490 for (islit = slit_min; islit <= slit_max; islit++) {
491 idx = islit * nlambda + ilambda;
492 if ((qual[idx] & decode_bp ) == 0) {
494 *fluxval += flux[idx];
495 *errval += (errs[idx] * errs[idx]);
496 *qualval |= qual[idx];
499 rima[idx]=(flux[idx]-flux[idx])/errs[idx];
502 *fluxval += ipol_fct * prof[islit];
503 *errval += (prof[islit] * prof[islit])/ den_fct;
506 fima[idx]=ipol_fct * prof[islit];
507 rima[idx]=(flux[idx]-ipol_fct * prof[islit])/((prof[islit] * prof[islit])/ den_fct);
520 return cpl_error_get_code();
548 cpl_frame** res_frame_ext) {
549 cpl_frame *res_frame = 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;
563 xsh_msg(
"Extract method clean with tag");
573 name = cpl_frame_get_filename(rec_frame);
574 plist = cpl_propertylist_load(name, 0);
581 if (nod_throw <= 5.5 ) {
582 hslit = round(0.5 * nod_throw /
slit_step);
584 hslit = round(0.5 * (11.-nod_throw) /
slit_step);
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);
599 xsh_msg(
"loc_frame name=%s", cpl_frame_get_filename(loc_frame));
606 for (i = 0; i < rec_list->
size; i++) {
612 float slitmin = slit[0];
613 float slitmax = slit[nslit - 1];
614 double lambdamin = lambda[0];
615 double lambdamax = lambda[nlambda - 1];
621 double *res_lambda = NULL;
622 float *res_flux = NULL, *res_errs = NULL;
623 int *res_qual = NULL;
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);
636 "order %d nslit %d (%f,%f) nlambda %d (%f %f)",
order, nslit, slitmin, slitmax, nlambda, lambdamin, lambdamax);
647 for (ilambda = 0; ilambda < nlambda; ilambda++) {
649 int ylow = 0, yup = 0;
650 double y_frac_low, y_frac_up;
651 double fluxval = 0.0;
659 slit_step, nslit, &ylow, &yup, &y_frac_up, &y_frac_low);
663 if (mask_hsize <= 0) {
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);
671 fluxval += y_frac_low * flux[idx];
672 errval += y_frac_low * (errs[idx] * errs[idx]);
673 qualval |= qual[idx];
676 if(i==0 && ilambda==0) {
677 xsh_msg(
"Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
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];
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];
696 int slit_min = ylow + 1;
698 if(i==0 && ilambda==0) {
699 xsh_msg(
"Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
703 ilambda,
instrument, slit_min, slit_max, nlambda, nslit,
704 mask_hsize, &fluxval, &errval, &qualval,pima,fima,rima);
724 if (mask_hsize <= 0) {
725 if(i == 0 && ilambda==0) {
726 xsh_msg(
"Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
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];
738 int slit_min = ylow + 1;
740 if(i==0 && ilambda==0) {
741 xsh_msg(
"Object extraction limits: [%d,%d] [bin]/[%g,%g] [arcsec]",
745 ilambda,
instrument, slit_min, slit_max, nlambda, nslit,
746 mask_hsize, &fluxval, &errval, &qualval,pima,fima,rima);
749 res_lambda[ilambda] = lambda[ilambda];
750 res_flux[ilambda] = fluxval;
751 res_errs[ilambda] = sqrt(errval);
752 res_qual[ilambda] = qualval;
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);
770 check( cpl_propertylist_append ( result_list->header, rec_list->
header));
810 cpl_frame** res_frame_ext,
const char* rec_prefix) {
811 cpl_frame *res_frame = NULL;
814 sprintf(tag,
"%s_%s", rec_prefix,
818 extract_par, tag,res_frame_ext ) );
820 cleanup:
return res_frame;
839 const char* rec_prefix) {
840 cpl_frame *res_frame = NULL;
843 sprintf(tag,
"%s_%s", rec_prefix,
855 cleanup:
return res_frame;
871 cpl_frameset * result_set = NULL;
875 xsh_msg(
" Entering xsh_extract_ifu");
881 check( result_set = cpl_frameset_new());
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;
891 const char * tag_suf = NULL;
907 sprintf(tag,
"%s_%s", rec_prefix, tag_suf);
909 check( rec_frame = cpl_frameset_get_frame( rec_frameset, i ));
910 xsh_msg(
" REC Frame '%s' Got", cpl_frame_get_filename( rec_frame ));
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 ));
919 xsh_msg(
"Calling xsh_extract");
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 ));
928 xsh_msg(
"Exit xsh_extract_ifu");
static xsh_instrument * instrument
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)
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
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.
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
int xsh_rec_list_get_order(xsh_rec_list *list, int idx)
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.
int * xsh_rec_list_get_qual1(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_slit(xsh_rec_list *list, int idx)
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
#define XSH_ASSURE_NOT_NULL(pointer)
#define xsh_msg_dbg_medium(...)
#define xsh_msg_debug(...)
Print a debug message.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_high(...)
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
double xsh_pfits_get_nodthrow(const cpl_propertylist *plist)
Get the Nod Throw value.
double xsh_pfits_get_rectify_bin_space(cpl_propertylist *plist)
find out the rectify space (slit) binning
double xsh_pfits_get_extract_slit_max(cpl_propertylist *plist)
find out the extraction min slit
double xsh_pfits_get_extract_slit_min(cpl_propertylist *plist)
find out the extraction min slit
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_propertylist * header
#define QFLAG_MISSING_DATA
#define QFLAG_INTERPOL_FLUX
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
static const char * SlitletName[]
#define EXTRACT_METHOD_PRINT(method)
#define XSH_FREE(POINTER)