33#include "kmclipm_priv_splines.h"
34#include "kmclipm_functions.h"
35#include "kmclipm_constants.h"
38#include "kmos_pfits.h"
45static cpl_vector * kmos_gen_telluric_resample(
51 double * new_cdelt1) ;
52static int kmos_gen_telluric_get_avg_zpoint(
53 const cpl_frame * zp_frame,
57static int kmos_gen_telluric_create_file_multiply(
58 const cpl_frame * data1_frame,
59 const cpl_frame * data2_frame,
60 const cpl_frame * zp_frame,
61 const char * filename,
62 cpl_frameset * allframes,
64 const cpl_parameterlist * parlist) ;
65static int kmos_gen_telluric_create_file_simple(
66 const cpl_frame * data_frame,
67 const cpl_frame * zp_frame,
68 const cpl_frame * resp_frame,
69 const cpl_frame * mh_frame,
70 const char * filename,
71 cpl_frameset * allframes,
74 const cpl_parameterlist * parlist) ;
76static int kmos_gen_telluric_check_inputs(cpl_frameset *,
int);
78static int kmos_gen_telluric_create(cpl_plugin *);
79static int kmos_gen_telluric_exec(cpl_plugin *);
80static int kmos_gen_telluric_destroy(cpl_plugin *);
81static int kmos_gen_telluric(cpl_parameterlist *, cpl_frameset *);
87static char kmos_gen_telluric_description[] =
88"This recipe creates the TELLURIC frame needed by kmos_sci_red by merging\n"
89"the TELLURIC (produced by kmos_std_star), the static RESPONŠ
E frame and \n"
90"the TELLURIC_CORR (produced with molecfit).\n"
91"The way the frames are combined is controlled by the --method parameter:\n"
92" - 0 (default) : use TELLURIC and get zpoint from TELLURIC\n"
93" If TELLURIC missing, use RESPONSE and get zpoint from RESPONSE\n"
94" - 1 : use TELLURIC_CORR and get zpoint from TELLURIC_CORR\n"
95" - 2 : use RESPONSE and get zpoint from TELLURIC\n"
96" If TELLURIC missing, get zpoint from RESPONSE\n"
97" For missing ZPOINTs, use the average of other IFUs for the \n"
99" - 3 : use RESPONSE x TELLURIC_CORR and get zpoint from TELLURIC_CORR\n"
101"----------------------------------------------------------------------------\n"
104" DO category Explanation Required \n"
105" ----------- ----------- -------- \n"
106" TELLURIC Produced by kmos_std_star N\n"
107" TELLURIC_CORR Produced by molecfit N\n"
108" RESPONSE static calibration N\n"
112" DO category Explanation\n"
113" ----------- -----------\n"
114" TELLURIC_GEN Used by kmos_sci_red\n"
115"----------------------------------------------------------------------------\n"
140 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe);
141 cpl_plugin *plugin = &recipe->interface;
143 cpl_plugin_init(plugin,
146 CPL_PLUGIN_TYPE_RECIPE,
148 "Generate a TELLURIC frame",
149 kmos_gen_telluric_description,
151 "https://support.eso.org/",
153 kmos_gen_telluric_create,
154 kmos_gen_telluric_exec,
155 kmos_gen_telluric_destroy);
156 cpl_pluginlist_append(list, plugin);
170static int kmos_gen_telluric_create(cpl_plugin *plugin)
176 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
177 recipe = (cpl_recipe *)plugin;
182 recipe->parameters = cpl_parameterlist_new();
186 p = cpl_parameter_new_value(
"kmos.kmos_gen_telluric.method",
187 CPL_TYPE_INT,
"How the TELLURIC is generated",
188 "kmos.kmos_gen_telluric", 0);
189 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"method");
190 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
191 cpl_parameterlist_append(recipe->parameters, p);
194 p = cpl_parameter_new_value(
"kmos.kmos_gen_telluric.b_samples",
195 CPL_TYPE_INT,
"The number of samples in wavelength",
196 "kmos.kmos_gen_telluric", -1);
197 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"b_samples");
198 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
199 cpl_parameterlist_append(recipe->parameters, p);
202 p = cpl_parameter_new_value(
"kmos.kmos_gen_telluric.fill_empty_ifus",
203 CPL_TYPE_BOOL,
"Flag to fill empty TELLURIC IFUs (only method 0)",
204 "kmos.kmos_gen_telluric", FALSE);
205 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"fill_empty_ifus");
206 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
207 cpl_parameterlist_append(recipe->parameters, p);
219static int kmos_gen_telluric_exec(cpl_plugin *plugin)
224 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
225 recipe = (cpl_recipe *)plugin;
228 return kmos_gen_telluric(recipe->parameters, recipe->frames);
238static int kmos_gen_telluric_destroy(cpl_plugin *plugin)
243 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
244 recipe = (cpl_recipe *)plugin;
247 cpl_parameterlist_delete(recipe->parameters);
259static int kmos_gen_telluric(cpl_parameterlist *parlist, cpl_frameset *frameset)
261 const cpl_parameter * par ;
262 int method, fill_empty_ifus, b_samples ;
263 cpl_frame * tell_frame ;
264 cpl_frame * tell_corr_frame ;
265 cpl_frame * resp_frame ;
269 if (kmos_check_and_set_groups(frameset) != CPL_ERROR_NONE) {
270 return cpl_error_get_code();
274 fn =
"kmos_gen_telluric.fits" ;
277 par = cpl_parameterlist_find_const(parlist,
"kmos.kmos_gen_telluric.method");
278 method = cpl_parameter_get_int(par);
279 par = cpl_parameterlist_find_const(parlist,
280 "kmos.kmos_gen_telluric.b_samples");
281 b_samples = cpl_parameter_get_int(par);
282 par = cpl_parameterlist_find_const(parlist,
283 "kmos.kmos_gen_telluric.fill_empty_ifus");
284 fill_empty_ifus = cpl_parameter_get_bool(par);
287 if (method != 0 && fill_empty_ifus != 0) {
288 fill_empty_ifus = 0 ;
289 cpl_msg_warning(__func__,
290 "Filling empty IFUs is only possible in method 0 - disable it");
294 if (kmos_gen_telluric_check_inputs(frameset, method) != 1) {
295 cpl_msg_error(__func__,
"Input frameset / parameter is unconsistent") ;
296 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
301 tell_frame = cpl_frameset_find(frameset, TELLURIC) ;
302 resp_frame = cpl_frameset_find(frameset, RESPONSE) ;
303 tell_corr_frame = cpl_frameset_find(frameset, TELLURIC_CORR) ;
307 if (tell_frame != NULL) {
308 cpl_msg_info(__func__,
"Use %s for Data and Zpoint",
309 cpl_frame_get_filename(tell_frame)) ;
310 kmos_gen_telluric_create_file_simple(tell_frame, tell_frame, resp_frame,
311 tell_frame, fn, frameset, b_samples,
312 fill_empty_ifus, parlist) ;
314 cpl_msg_info(__func__,
"Use %s for Data and Zpoint",
315 cpl_frame_get_filename(resp_frame)) ;
316 kmos_gen_telluric_create_file_simple(resp_frame, resp_frame, NULL,
317 resp_frame, fn, frameset, b_samples, 0, parlist) ;
319 }
else if (method == 1) {
320 cpl_msg_info(__func__,
"Use %s for Data and Zpoint",
321 cpl_frame_get_filename(tell_corr_frame)) ;
322 kmos_gen_telluric_create_file_simple(tell_corr_frame, NULL, NULL,
323 tell_corr_frame, fn, frameset, b_samples, 0, parlist) ;
324 }
else if (method == 2) {
325 if (tell_frame != NULL) {
326 cpl_msg_info(__func__,
"Use %s for Data and %s for Zpoint",
327 cpl_frame_get_filename(resp_frame),
328 cpl_frame_get_filename(tell_frame)) ;
331 kmos_gen_telluric_create_file_simple(resp_frame, tell_frame, NULL,
332 tell_frame, fn, frameset, b_samples, 0, parlist) ;
334 cpl_msg_info(__func__,
"Use %s for Data and Zpoint",
335 cpl_frame_get_filename(resp_frame)) ;
336 kmos_gen_telluric_create_file_simple(resp_frame, NULL, NULL,
337 resp_frame, fn, frameset, b_samples, 0, parlist) ;
339 }
else if (method == 3) {
340 cpl_msg_info(__func__,
"Use %s X %s for Data and %s for Zpoint",
341 cpl_frame_get_filename(resp_frame),
342 cpl_frame_get_filename(tell_corr_frame),
343 cpl_frame_get_filename(tell_corr_frame)) ;
344 kmos_gen_telluric_create_file_multiply(resp_frame, tell_corr_frame,
345 tell_corr_frame, fn, frameset, b_samples, parlist) ;
347 cpl_msg_error(__func__,
"Unsupported - Should never come here") ;
348 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
356static int kmos_gen_telluric_create_file_multiply(
357 const cpl_frame * data1_frame,
358 const cpl_frame * data2_frame,
359 const cpl_frame * zp_frame,
360 const char * filename,
361 cpl_frameset * allframes,
363 const cpl_parameterlist * parlist)
365 cpl_propertylist * mh ;
366 cpl_propertylist * applist ;
367 cpl_propertylist * eh ;
368 cpl_propertylist * zph ;
371 cpl_vector * vec_resampled ;
372 double zpoint, crval1, cdelt1, new_crval1, new_cdelt1 ;
373 enum kmo_frame_type ft ;
375 const char * extname ;
377 int i, ext_nb, id, ifu_nr ;
378 cpl_vector * zpt_list;
380 cpl_propertylist ** eh_list ;
387 ext_nb = cpl_fits_count_extensions(cpl_frame_get_filename(data1_frame)) ;
389 if (cpl_fits_count_extensions(cpl_frame_get_filename(data2_frame))!=ext_nb){
390 cpl_msg_error(__func__,
"Extensions numbers do not match - abort") ;
394 zpt_list = cpl_vector_new(ext_nb);
395 vec3 = (cpl_vector**)cpl_calloc(ext_nb,
sizeof(cpl_vector*));
396 eh_list = (cpl_propertylist**)cpl_calloc(ext_nb,
sizeof(cpl_propertylist*));
399 applist = cpl_propertylist_new() ;
400 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, TELLURIC_GEN) ;
403 for (i = 0; i < ext_nb ; i++) {
405 eh = cpl_propertylist_load(cpl_frame_get_filename(data2_frame),i+1);
407 extname = cpl_propertylist_get_string(eh,
"EXTNAME") ;
408 kmo_extname_extractor(extname, &ft, &ifu_nr, content) ;
409 cpl_propertylist_delete(eh);
411 vec1 = cpl_vector_load(cpl_frame_get_filename(data2_frame), i+1) ;
412 vec2 = cpl_vector_load(cpl_frame_get_filename(data1_frame), i+1) ;
414 cpl_vector_delete(vec1) ;
417 keyword = cpl_sprintf(
"%s%d", PRO_STD, ifu_nr);
418 cpl_propertylist_update_int(applist, keyword, 1);
427 mh = cpl_propertylist_load(cpl_frame_get_filename(data2_frame), 0);
430 for (i = 0; i < ext_nb ; i++) {
433 eh = cpl_propertylist_load(cpl_frame_get_filename(data1_frame),i+1);
436 extname = cpl_propertylist_get_string(eh,
"EXTNAME") ;
437 kmo_extname_extractor(extname, &ft, &
id, content) ;
440 vec1 = cpl_vector_load(cpl_frame_get_filename(data1_frame), i+1) ;
441 vec2 = cpl_vector_load(cpl_frame_get_filename(data2_frame), i+1) ;
442 if (vec1 == NULL || vec2 == NULL) cpl_error_reset() ;
446 cpl_vector_get_size(vec1) != b_samples && b_samples > 0) {
449 cdelt1 = kmos_pfits_get_cdelt1(eh) ;
450 crval1 = kmos_pfits_get_crval1(eh) ;
453 vec_resampled = kmos_gen_telluric_resample(vec1, b_samples, crval1,
454 cdelt1, &new_crval1, &new_cdelt1) ;
455 if (vec_resampled != NULL) {
456 cpl_vector_delete(vec1) ;
457 vec1 = vec_resampled ;
460 cpl_propertylist_update_double(eh,
"CRVAL1", new_crval1) ;
461 cpl_propertylist_update_double(eh,
"CDELT1", new_cdelt1) ;
466 if (vec1 != NULL && vec2 != NULL &&
467 cpl_vector_get_size(vec1) != cpl_vector_get_size(vec2)) {
470 cdelt1 = kmos_pfits_get_cdelt1(eh) ;
471 crval1 = kmos_pfits_get_crval1(eh) ;
474 vec_resampled = kmos_gen_telluric_resample(vec2, b_samples, crval1,
475 cdelt1, &new_crval1, &new_cdelt1) ;
476 if (vec_resampled != NULL) {
477 cpl_vector_delete(vec2) ;
478 vec2 = vec_resampled ;
483 if (vec1 != NULL && vec2 != NULL && zp_frame != NULL) {
484 zph=cpl_propertylist_load(cpl_frame_get_filename(zp_frame),i+1);
485 zpoint = cpl_propertylist_get_double(zph,
"ESO QC ZPOINT");
486 cpl_propertylist_delete(zph);
487 if (cpl_error_get_code() != CPL_ERROR_NONE) {
488 cpl_msg_warning(__func__,
"No QC ZPOINT found in %s",
489 cpl_frame_get_filename(zp_frame)) ;
491 cpl_vector_set(zpt_list, i, 0.0);
493 cpl_propertylist_update_double(eh,
"ESO QC ZPOINT", zpoint) ;
494 cpl_vector_set(zpt_list, i, zpoint);
499 if (!strcmp(content,
"DATA")) cpl_vector_multiply(vec1, vec2) ;
500 cpl_vector_delete(vec2) ;
514 cpl_vector_delete(vec1) ;
515 cpl_propertylist_delete(eh);
518 kmclipm_update_property_double(applist, QC_ZPOINT_AVG, cpl_vector_get_mean(zpt_list),
"Avg of Zeropoint values ") ;
519 kmclipm_update_property_double(applist, QC_ZPOINT_RMS, cpl_vector_get_stdev(zpt_list),
"RMS of Zeropoint values") ;
522 if(cpl_propertylist_has(mh,
"ESO OBS PROG ID")){
523 const char *prog_id= cpl_propertylist_get_string(mh,
"ESO OBS PROG ID");
525 if (strcmp(prog_id,
"60.A-9452(A)") && strcmp(prog_id,
"60.A-9254(A)")) {
526 kmclipm_update_property_int(applist, QC_FLAG_OB, 1,
"Obs Flag") ;
529 kmclipm_update_property_int(applist, QC_FLAG_OB, 0,
"Obs Flag" ) ;
532 kmclipm_update_property_int(applist, QC_FLAG_OB, 1,
"Obs Flag" ) ;
534 if(cpl_propertylist_has(mh,QC_NR_STD_STARS)){
535 kmclipm_update_property_int(applist, QC_NR_STD_STARS,
536 cpl_propertylist_get_int(mh, QC_NR_STD_STARS),
"Nr. of std stars") ;
538 kmclipm_update_property_int(applist, QC_NR_STD_STARS,0,
"Nr. of std stars") ;
542 cpl_dfs_save_propertylist(allframes, mh, parlist, allframes, NULL,
543 "kmos_gen_telluric", applist, NULL, PACKAGE
"/" PACKAGE_VERSION,
546 cpl_propertylist_delete(mh);
547 cpl_propertylist_delete(applist);
550 for (i = 0; i < ext_nb ; i++) {
553 if (cpl_error_get_code() == CPL_ERROR_NONE) {
555 cpl_vector_save(vec3[i], filename, CPL_TYPE_DOUBLE, eh_list[ext_nb],CPL_IO_EXTEND) ;
559 cpl_propertylist_save(eh_list[i], filename, CPL_IO_EXTEND) ;
563 for (i = 0; i < ext_nb ; i++) {
564 cpl_vector_delete(vec3[i]);
565 cpl_propertylist_delete(eh_list[i]);
571static int kmos_gen_telluric_create_file_simple(
572 const cpl_frame * data_frame,
573 const cpl_frame * zp_frame,
574 const cpl_frame * resp_frame,
575 const cpl_frame * mh_frame,
576 const char * filename,
577 cpl_frameset * allframes,
580 const cpl_parameterlist * parlist)
582 cpl_propertylist * mh ;
583 cpl_propertylist * applist ;
584 cpl_propertylist * eh ;
585 cpl_propertylist * tmp_eh ;
586 cpl_propertylist * zph ;
588 cpl_vector * vec_ref ;
589 cpl_vector * resp_ref ;
590 cpl_vector * resp_cur ;
591 cpl_vector * vec_resampled ;
592 double zpoint, zpoint_vec_ref, zpoint_resp_ref,
593 zpoint_resp_cur, zp1_avg, zp2_avg, zp3_avg,
594 cdelt1, crval1, new_crval1, new_cdelt1;
595 int i, ext_nb, id, det_nr, ifu_nr, first_data_det1,
596 first_data_det2, first_data_det3,
597 first_noise_det1, first_noise_det2,
598 first_noise_det3, ref_ifu, nvals ;
599 enum kmo_frame_type ft ;
602 const char * extname ;
603 cpl_vector * zpt_list;
605 cpl_propertylist ** eh_list;
608 zp1_avg = zp2_avg = zp3_avg = -1.0 ;
611 first_data_det1 = first_data_det2 = first_data_det3 = -1 ;
612 first_noise_det1 = first_noise_det2 = first_noise_det3 = -1 ;
615 ext_nb = cpl_fits_count_extensions(cpl_frame_get_filename(data_frame)) ;
617 zpt_list = cpl_vector_new(ext_nb);
618 vec1 = (cpl_vector**)cpl_calloc(ext_nb,
sizeof(cpl_vector*));
619 eh_list = (cpl_propertylist**)cpl_calloc(ext_nb,
sizeof(cpl_propertylist*));
622 applist = cpl_propertylist_new() ;
623 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, TELLURIC_GEN) ;
626 for (i = 0; i < ext_nb ; i++) {
628 eh = cpl_propertylist_load(cpl_frame_get_filename(data_frame), i+1);
630 extname = cpl_propertylist_get_string(eh,
"EXTNAME") ;
632 kmo_extname_extractor(extname, &ft, &ifu_nr, content) ;
633 cpl_propertylist_delete(eh);
636 if (ifu_nr >= 1 && ifu_nr <= 8) det_nr = 1 ;
637 else if (ifu_nr >= 9 && ifu_nr <= 16) det_nr = 2 ;
638 else if (ifu_nr >= 17 && ifu_nr <= 24) det_nr = 3 ;
640 cpl_msg_error(__func__,
"Cannot Identify the detector") ;
641 cpl_propertylist_delete(applist);
646 vec = cpl_vector_load(cpl_frame_get_filename(data_frame), i+1) ;
647 if (vec == NULL) cpl_error_reset() ;
651 if (!strcmp(content,
"DATA")) {
652 if (det_nr == 1 && first_data_det1 < 0) first_data_det1 = i+1 ;
653 if (det_nr == 2 && first_data_det2 < 0) first_data_det2 = i+1 ;
654 if (det_nr == 3 && first_data_det3 < 0) first_data_det3 = i+1 ;
655 }
else if (!strcmp(content,
"NOISE")) {
656 if (det_nr == 1 && first_noise_det1 < 0) first_noise_det1 = i+1;
657 if (det_nr == 2 && first_noise_det2 < 0) first_noise_det2 = i+1;
658 if (det_nr == 3 && first_noise_det3 < 0) first_noise_det3 = i+1;
662 if (vec != NULL || fill_empty_ifus == 1) {
663 if (vec != NULL) cpl_vector_delete(vec) ;
666 keyword = cpl_sprintf(
"%s%d", PRO_STD, ifu_nr);
667 cpl_propertylist_update_int(applist, keyword, 1);
675 mh = cpl_propertylist_load(cpl_frame_get_filename(mh_frame), 0);
677 cpl_msg_info(__func__,
"BEFORE SAVE BEFORE SAVING %s ", cpl_frame_get_filename(mh_frame)) ;
680 if (zp_frame != NULL) {
681 kmos_gen_telluric_get_avg_zpoint(zp_frame,&zp1_avg,&zp2_avg,&zp3_avg) ;
685 for (i = 0; i < ext_nb ; i++) {
688 eh = cpl_propertylist_load(cpl_frame_get_filename(data_frame), i+1);
691 extname = cpl_propertylist_get_string(eh,
"EXTNAME") ;
692 kmo_extname_extractor(extname, &ft, &
id, content) ;
694 if (
id >= 1 &&
id <= 8) det_nr = 1 ;
695 else if (
id >= 9 &&
id <= 16) det_nr = 2 ;
696 else if (
id >= 17 &&
id <= 24) det_nr = 3 ;
698 cpl_msg_error(__func__,
"Cannot Identify the detector") ;
699 cpl_propertylist_delete(eh);
704 vec = cpl_vector_load(cpl_frame_get_filename(data_frame), i+1) ;
705 if (vec == NULL) cpl_error_reset() ;
708 if (vec == NULL && fill_empty_ifus == 1) {
713 if (!strcmp(content,
"NOISE")) {
715 if (det_nr==1) ref_ifu = first_noise_det1 ;
716 if (det_nr==2) ref_ifu = first_noise_det2 ;
717 if (det_nr==3) ref_ifu = first_noise_det3 ;
720 vec_ref=cpl_vector_load(cpl_frame_get_filename(data_frame),
723 }
else if (!strcmp(content,
"DATA")) {
726 if (det_nr==1) ref_ifu = first_data_det1 ;
727 if (det_nr==2) ref_ifu = first_data_det2 ;
728 if (det_nr==3) ref_ifu = first_data_det3 ;
731 vec_ref=cpl_vector_load(cpl_frame_get_filename(data_frame),
734 resp_ref = cpl_vector_load(cpl_frame_get_filename(resp_frame),
736 resp_cur = cpl_vector_load(cpl_frame_get_filename(resp_frame),
739 if (resp_ref != NULL && resp_cur != NULL) {
741 if (cpl_vector_get_size(vec_ref) !=
742 cpl_vector_get_size(resp_ref)) {
745 tmp_eh = cpl_propertylist_load(
746 cpl_frame_get_filename(resp_frame), ref_ifu);
747 cdelt1 = kmos_pfits_get_cdelt1(tmp_eh) ;
748 crval1 = kmos_pfits_get_crval1(tmp_eh) ;
749 cpl_propertylist_delete(tmp_eh) ;
752 vec_resampled = kmos_gen_telluric_resample(resp_ref,
753 cpl_vector_get_size(vec_ref), crval1, cdelt1,
754 &new_crval1, &new_cdelt1) ;
755 if (vec_resampled != NULL) {
756 cpl_vector_delete(resp_ref) ;
757 resp_ref = vec_resampled ;
762 if (cpl_vector_get_size(vec_ref) !=
763 cpl_vector_get_size(resp_cur)) {
766 tmp_eh = cpl_propertylist_load(
767 cpl_frame_get_filename(resp_frame), i+1);
768 cdelt1 = kmos_pfits_get_cdelt1(tmp_eh) ;
769 crval1 = kmos_pfits_get_crval1(tmp_eh) ;
770 cpl_propertylist_delete(tmp_eh) ;
773 vec_resampled = kmos_gen_telluric_resample(resp_cur,
774 cpl_vector_get_size(vec_ref), crval1, cdelt1,
775 &new_crval1, &new_cdelt1) ;
776 if (vec_resampled != NULL) {
777 cpl_vector_delete(resp_ref) ;
778 resp_ref = vec_resampled ;
781 cpl_vector_divide(vec_ref, resp_ref);
782 cpl_vector_multiply(vec_ref, resp_cur);
784 if (resp_ref != NULL) cpl_vector_delete(resp_ref) ;
785 if (resp_cur != NULL) cpl_vector_delete(resp_cur) ;
790 tmp_eh = cpl_propertylist_load(
791 cpl_frame_get_filename(data_frame), ref_ifu);
793 cpl_propertylist_copy_property(eh, tmp_eh,
"CTYPE1") ;
794 cpl_propertylist_copy_property(eh, tmp_eh,
"CRPIX1") ;
795 cpl_propertylist_copy_property(eh, tmp_eh,
"CRVAL1") ;
796 cpl_propertylist_copy_property(eh, tmp_eh,
"CDELT1") ;
797 cpl_propertylist_delete(tmp_eh) ;
798 cpl_propertylist_erase(eh,
"CRPIX2") ;
799 cpl_propertylist_erase(eh,
"CTYPE2") ;
800 cpl_propertylist_erase(eh,
"CD1_1") ;
801 cpl_propertylist_erase(eh,
"CD1_2") ;
802 cpl_propertylist_erase(eh,
"CD2_1") ;
803 cpl_propertylist_erase(eh,
"CD2_2") ;
806 tmp_eh = cpl_propertylist_load(
807 cpl_frame_get_filename(data_frame), ref_ifu);
808 zpoint_vec_ref = cpl_propertylist_get_double(tmp_eh,
810 cpl_propertylist_delete(tmp_eh) ;
811 tmp_eh = cpl_propertylist_load(
812 cpl_frame_get_filename(resp_frame), ref_ifu);
813 zpoint_resp_ref = cpl_propertylist_get_double(tmp_eh,
815 cpl_propertylist_delete(tmp_eh) ;
816 tmp_eh = cpl_propertylist_load(
817 cpl_frame_get_filename(resp_frame), i+1);
818 zpoint_resp_cur = cpl_propertylist_get_double(tmp_eh,
820 cpl_propertylist_delete(tmp_eh) ;
822 zpoint = zpoint_vec_ref - zpoint_resp_ref + zpoint_resp_cur ;
823 cpl_propertylist_update_double(eh,
"ESO QC ZPOINT", zpoint) ;
827 if (vec != NULL && zp_frame != NULL) {
828 zph = cpl_propertylist_load(cpl_frame_get_filename(zp_frame),i+1);
829 zpoint = cpl_propertylist_get_double(zph,
"ESO QC ZPOINT");
830 cpl_propertylist_delete(zph);
831 if (cpl_error_get_code() != CPL_ERROR_NONE) {
833 if (det_nr == 1) zpoint = zp1_avg ;
834 else if (det_nr == 2) zpoint = zp2_avg ;
835 else zpoint = zp3_avg ;
852 if (nvals == 0) zpoint = -1.0 ;
853 else zpoint /= nvals ;
854 cpl_msg_warning(__func__,
855 "No QC ZPOINT in %s[%d] - Use average of other Dets: %g",
856 cpl_frame_get_filename(zp_frame), i+1, zpoint) ;
858 cpl_msg_warning(__func__,
859 "No QC ZPOINT in %s[%d] - Use average for Det %d: %g",
860 cpl_frame_get_filename(zp_frame), i+1, det_nr, zpoint) ;
863 cpl_propertylist_update_double(eh,
"ESO QC ZPOINT", zpoint) ;
864 cpl_vector_set(zpt_list, i, zpoint);
869 cpl_vector_get_size(vec) != b_samples && b_samples > 0) {
872 cdelt1 = kmos_pfits_get_cdelt1(eh) ;
873 crval1 = kmos_pfits_get_crval1(eh) ;
876 vec_resampled = kmos_gen_telluric_resample(vec, b_samples, crval1,
877 cdelt1, &new_crval1, &new_cdelt1) ;
878 if (vec_resampled != NULL) {
879 cpl_vector_delete(vec) ;
880 vec = vec_resampled ;
883 cpl_propertylist_update_double(eh,
"CRVAL1", new_crval1) ;
884 cpl_propertylist_update_double(eh,
"CDELT1", new_cdelt1) ;
889 cpl_propertylist_erase(eh, CDELT1);
890 cpl_propertylist_erase(eh, CDELT2);
891 cpl_propertylist_erase(eh, CDELT3);
892 cpl_propertylist_erase(eh, CRVAL3);
893 cpl_propertylist_erase(eh, CRPIX3);
894 cpl_propertylist_erase(eh, CTYPE3);
908 kmclipm_update_property_double(applist, QC_ZPOINT_AVG, cpl_vector_get_mean(zpt_list),
"Avg of Zeropoint values ") ;
909 kmclipm_update_property_double(applist, QC_ZPOINT_RMS, cpl_vector_get_stdev(zpt_list),
"RMS of Zeropoint values") ;
912 if(cpl_propertylist_has(mh,
"ESO OBS PROG ID")){
913 const char *prog_id= cpl_propertylist_get_string(mh,
"ESO OBS PROG ID");
915 if (strcmp(prog_id,
"60.A-9452(A)") && strcmp(prog_id,
"60.A-9254(A)")) {
916 kmclipm_update_property_int(applist, QC_FLAG_OB, 1,
"Obs Flag") ;
919 kmclipm_update_property_int(applist, QC_FLAG_OB, 0,
"Obs Flag" ) ;
922 kmclipm_update_property_int(applist, QC_FLAG_OB, 1,
"Obs Flag" ) ;
926 if(cpl_propertylist_has(mh,QC_NR_STD_STARS)){
927 kmclipm_update_property_int(applist, QC_NR_STD_STARS,
928 cpl_propertylist_get_int(mh, QC_NR_STD_STARS),
"Nr. of std stars") ;
930 kmclipm_update_property_int(applist, QC_NR_STD_STARS,0,
"Nr. of std stars") ;
934 cpl_msg_info(__func__,
"zpoint values saved in main header") ;
938 cpl_dfs_save_propertylist(allframes, mh, parlist, allframes, zp_frame,
939 "kmos_gen_telluric", applist, NULL, PACKAGE
"/" PACKAGE_VERSION,
941 cpl_msg_info(__func__,
"main header saved ") ;
943 cpl_propertylist_delete(mh);
944 cpl_propertylist_delete(applist);
946 cpl_msg_info(__func__,
"starting to save sub headers ") ;
948 for (i = 0; i < ext_nb ; i++) {
950 if (vec1[i] != NULL) {
952 cpl_vector_save(vec1[i], filename, CPL_TYPE_DOUBLE, eh_list[i], CPL_IO_EXTEND) ;
957 cpl_propertylist_erase(eh_list[i], CTYPE1);
958 cpl_propertylist_erase(eh_list[i], CTYPE2);
959 cpl_propertylist_erase(eh_list[i], CRPIX1);
960 cpl_propertylist_erase(eh_list[i], CRPIX2);
961 cpl_propertylist_erase(eh_list[i], CRVAL1);
962 cpl_propertylist_erase(eh_list[i], CRVAL2);
963 cpl_propertylist_erase(eh_list[i], CD1_1);
964 cpl_propertylist_erase(eh_list[i], CD1_2);
965 cpl_propertylist_erase(eh_list[i], CD2_1);
966 cpl_propertylist_erase(eh_list[i], CD2_2);
967 cpl_propertylist_save(eh_list[i], filename, CPL_IO_EXTEND) ;
971 cpl_msg_info(__func__,
"subheaders saved ") ;
976 for (i = 0; i < ext_nb ; i++) {
977 cpl_vector_delete(vec1[i]);
978 cpl_propertylist_delete(eh_list[i]);
995static cpl_vector * kmos_gen_telluric_resample(
1000 double * new_crval1,
1001 double * new_cdelt1)
1003 cpl_bivector * fold ;
1004 cpl_bivector * fnew ;
1005 cpl_vector * old_x ;
1006 cpl_vector * new_x ;
1007 cpl_vector * new_y ;
1010 double current_lambda ;
1014 if (vec == NULL || new_crval1 == NULL || new_cdelt1 == NULL)
return NULL;
1017 vec_size = cpl_vector_get_size(vec) ;
1020 if (b_samples == vec_size) {
1021 *new_crval1 = crval1 ;
1022 *new_cdelt1 = cdelt1 ;
1023 return cpl_vector_duplicate(vec) ;
1027 *new_crval1 = crval1 ;
1028 *new_cdelt1 = cdelt1 * vec_size / b_samples ;
1031 old_x = cpl_vector_new(vec_size) ;
1032 for (i=0 ; i<vec_size ; i++) {
1033 cpl_vector_set(old_x, i, crval1 + i*cdelt1) ;
1037 new_x = cpl_vector_new(b_samples) ;
1038 new_y = cpl_vector_new(b_samples) ;
1039 for (i=0 ; i<b_samples ; i++) {
1040 cpl_vector_set(new_x, i, *new_crval1 + i*(*new_cdelt1)) ;
1043 if (b_samples < vec_size) {
1045 fold = cpl_bivector_wrap_vectors(old_x, (cpl_vector *)vec) ;
1046 fnew = cpl_bivector_wrap_vectors(new_x, new_y) ;
1049 err = cpl_bivector_interpolate_linear(fnew, fold) ;
1052 cpl_bivector_unwrap_vectors(fold) ;
1053 cpl_bivector_unwrap_vectors(fnew) ;
1055 if (err != CPL_ERROR_NONE) {
1056 cpl_msg_error(__func__,
"Cannot Resample") ;
1057 cpl_vector_delete(old_x) ;
1058 cpl_vector_delete(new_x) ;
1059 cpl_vector_delete(new_y) ;
1064 for (i=0 ; i<b_samples ; i++) {
1066 current_lambda = cpl_vector_get(new_x, i) ;
1068 for (j=0 ; j<vec_size-1 ; j++) {
1069 if (cpl_vector_get(old_x, j) > current_lambda) break ;
1071 cpl_vector_set(new_y, i, cpl_vector_get(vec, j)) ;
1074 cpl_vector_delete(old_x) ;
1075 cpl_vector_delete(new_x) ;
1089static int kmos_gen_telluric_get_avg_zpoint(
1090 const cpl_frame * zp_frame,
1095 double zp1_avg_loc, zp2_avg_loc, zp3_avg_loc, zpoint ; ;
1096 int zp1_nb, zp2_nb, zp3_nb, ext_nb, id, det_nr, i;
1097 cpl_propertylist * eh ;
1098 const char * extname ;
1099 enum kmo_frame_type ft ;
1103 if (zp_frame==NULL || zp1_avg==NULL || zp2_avg==NULL || zp3_avg==NULL)
1107 *zp1_avg = *zp2_avg = *zp3_avg = -1.0 ;
1108 zp1_nb = zp2_nb = zp3_nb = 0 ;
1109 zp1_avg_loc = zp2_avg_loc = zp3_avg_loc = 0.0 ;
1111 ft = illegal_frame ;
1114 ext_nb = cpl_fits_count_extensions(cpl_frame_get_filename(zp_frame)) ;
1117 for (i = 0; i < ext_nb ; i++) {
1119 eh = cpl_propertylist_load(cpl_frame_get_filename(zp_frame), i+1);
1122 extname = cpl_propertylist_get_string(eh,
"EXTNAME") ;
1123 kmo_extname_extractor(extname, &ft, &
id, content) ;
1126 if (!strcmp(content,
"DATA")) {
1128 zpoint = cpl_propertylist_get_double(eh,
"ESO QC ZPOINT");
1131 if (cpl_error_get_code() == CPL_ERROR_NONE) {
1133 if (
id >= 1 &&
id <= 8) det_nr = 1 ;
1134 else if (
id >= 9 &&
id <= 16) det_nr = 2 ;
1135 else if (
id >= 17 &&
id <= 24) det_nr = 3 ;
1137 cpl_msg_error(__func__,
"Cannot Identify the detector") ;
1138 cpl_propertylist_delete(eh);
1143 zp1_avg_loc += zpoint ;
1145 }
else if (det_nr == 2) {
1146 zp2_avg_loc += zpoint ;
1148 }
else if (det_nr == 3) {
1149 zp3_avg_loc += zpoint ;
1152 cpl_msg_error(__func__,
"Cannot Identify the detector") ;
1153 cpl_propertylist_delete(eh);
1160 cpl_propertylist_delete(eh);
1164 if (zp1_nb > 0) *zp1_avg = zp1_avg_loc / zp1_nb ;
1165 if (zp2_nb > 0) *zp2_avg = zp2_avg_loc / zp2_nb ;
1166 if (zp3_nb > 0) *zp3_avg = zp3_avg_loc / zp3_nb ;
1178static int kmos_gen_telluric_check_inputs(
1179 cpl_frameset * frameset,
1182 cpl_frame * tell_frame ;
1183 cpl_frame * tell_corr_frame ;
1184 cpl_frame * resp_frame ;
1187 if (frameset == NULL)
return -1;
1188 if (method < 0 || method > 3)
return -1 ;
1190 tell_frame = cpl_frameset_find(frameset, TELLURIC) ;
1191 resp_frame = cpl_frameset_find(frameset, RESPONSE) ;
1192 tell_corr_frame = cpl_frameset_find(frameset, TELLURIC_CORR) ;
1194 if (method == 0 && tell_frame == NULL && resp_frame == NULL)
1196 if (method == 1 && tell_corr_frame == NULL)
1198 if (method == 2 && resp_frame == NULL)
1200 if (method == 3 && (resp_frame == NULL || tell_corr_frame == NULL))
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.