35 #include "sinfo_new_cubes_build.h"
36 #include "sinfo_pfits.h"
37 #include "sinfo_msg.h"
38 #include "sinfo_pro_save.h"
39 #include "sinfo_objnod_ini_by_cpl.h"
40 #include "sinfo_functions.h"
41 #include "sinfo_hidden.h"
42 #include "sinfo_utilities_scired.h"
43 #include "sinfo_wave_calibration.h"
44 #include "sinfo_cube_construct.h"
45 #include "sinfo_skycor.h"
46 #include "sinfo_product_config.h"
47 #include "sinfo_error.h"
48 #include "sinfo_utils_wrappers.h"
49 #include "sinfo_atmo_disp.h"
53 #define PI_NUMB (3.1415926535897932384626433832795)
56 sinfo_atm_correction(cpl_imagelist** ppCube, cpl_frameset* sof,
57 const char* polyshiftname,
double dis_cube,
58 double centralLambda_cube,
int centralpix_cube);
86 sinfo_new_cubes_build(
const char* plugin_id, cpl_parameterlist* config,
87 cpl_frameset* sof,
const char* procatg,
88 const int frame_index)
91 object_config * cfg = NULL;
92 cpl_image * im = NULL;
93 cpl_image * wavemapim = NULL;
94 cpl_image * wim = NULL;
95 cpl_image * res_obj = NULL;
96 cpl_image * calim = NULL;
97 cpl_image * halospec = NULL;
98 cpl_image * sky_im = NULL;
99 cpl_image* res_ima = NULL;
100 cpl_image* res_wim = NULL;
101 cpl_image* res_flat = NULL;
102 cpl_image* res_sky = NULL;
103 cpl_image* flat_im = NULL;
104 cpl_image* eima_avg = NULL;
105 cpl_image* eima_med = NULL;
106 cpl_imagelist * cube = NULL;
107 cpl_imagelist * outcube = NULL;
108 cpl_imagelist * outcube2 = NULL;
109 cpl_imagelist* cflat = NULL;
110 cpl_imagelist* cflat2 = NULL;
111 cpl_imagelist* csky = NULL;
112 cpl_imagelist* csky2 = NULL;
113 cpl_propertylist* plist = NULL;
114 cpl_frame* obj_frm = NULL;
115 cpl_frame* sky_frm = NULL;
116 cpl_imagelist* obj_cor = NULL;
127 float * correct_dist = NULL;
128 float * distances = NULL;
129 float ** slit_edges = NULL;
132 cpl_image* dif = NULL;
141 char pro_mjit[MAX_NAME_SIZE];
142 char pro_obs[MAX_NAME_SIZE];
143 char pro_med[MAX_NAME_SIZE];
146 char file_name[FILE_NAME_SZ];
148 cpl_table* qclog_tbl = NULL;
149 cpl_frameset* stk = NULL;
150 cpl_parameter* p = NULL;
152 sinfo_skycor_qc* sqc = NULL;
153 cpl_table* int_obj = NULL;
155 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.product.density"));
156 check_nomsg(pdensity = cpl_parameter_get_int(p));
158 if (strcmp(procatg, PRO_COADD_STD) == 0) {
159 strcpy(pro_mjit, PRO_MASK_COADD_STD);
160 strcpy(pro_obs, PRO_OBS_STD);
161 strcpy(pro_med, PRO_MED_COADD_STD);
164 else if (strcmp(procatg, PRO_COADD_PSF) == 0) {
165 strcpy(pro_mjit, PRO_MASK_COADD_PSF);
166 strcpy(pro_obs, PRO_OBS_PSF);
167 strcpy(pro_med, PRO_MED_COADD_PSF);
170 strcpy(pro_mjit, PRO_MASK_COADD_OBJ);
171 strcpy(pro_obs, PRO_OBS_OBJ);
172 strcpy(pro_med, PRO_MED_COADD_OBJ);
176 check_nomsg(stk = cpl_frameset_new());
178 cknull(cfg = sinfo_parse_cpl_input_objnod(config, sof, &stk),
179 "Error setting parameter configuration");
181 ck0(sinfo_check_input_data(cfg),
"error checking input");
183 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.objnod.fcol"));
184 check_nomsg(fcol = (
float )cpl_parameter_get_double(p));
186 for (n = 0; n < cfg->nframes; n++) {
189 name = cfg->framelist[n];
190 if (sinfo_is_fits_file(name) != 1) {
196 cknull(im = cpl_image_load(name,CPL_TYPE_FLOAT,0,0),
197 " could not load frame %s!",name);
204 sinfo_msg(
"Resampling object");
205 cknull(wavemapim = cpl_image_load(cfg->wavemap, CPL_TYPE_FLOAT, 0, 0),
206 "could not load wavemap");
207 check_nomsg(wim = cpl_image_duplicate(wavemapim));
210 p = cpl_parameterlist_find(config,
211 "sinfoni.objnod.flux_cor"));
212 check_nomsg(flux_cor = cpl_parameter_get_bool(p));
214 nx = cpl_image_get_size_x(wim);
215 ny = cpl_image_get_size_y(wim);
228 cknull(res_ima = sinfo_new_defined_resampling(im,
237 " sinfo_definedResampling() failed" );
243 check_nomsg(dif = cpl_image_new(nx, ny, CPL_TYPE_FLOAT));
244 pw = cpl_image_get_data(wim);
245 pd = cpl_image_get_data(dif);
247 for (i = 1; i < nx - 1; i++) {
248 for (j = 1; j < ny - 1; j++) {
249 if (!isnan(pd[nx*j+i])) {
250 pd[nx * j + i] = 2.0 * dis
251 / (pw[nx * (j + 1) + i]
252 - pw[nx * (j - 1) + i]);
256 pd[i] = dis / (pw[nx + i] - pw[i]);
258 if (!isnan(pd[nx*(ny-1)+i])) {
259 pd[nx * (ny - 1) + i] =
261 / (pw[nx * (ny - 1) + i]
272 cknull(res_wim = sinfo_new_defined_resampling(dif,
281 " sinfo_definedResampling() failed" );
283 cpl_image_delete(dif);
291 res_obj = cpl_image_duplicate(res_ima);
292 sinfo_free_image(&res_ima);
294 sinfo_msg(
"Apply flux correction");
295 cpl_image_divide(res_obj, res_wim);
300 sinfo_free_image(&wim);
302 if ((pdensity == 3) || (pdensity == 2 && frame_index == 0)) {
303 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%d%s",
304 RESAMPLED_OUT_OBJ_FILENAME, frame_index,
".fits");
306 sinfo_pro_save_ima(res_obj,sof,sof,file_name, PRO_RESAMPLED_OBJ, qclog_tbl,plugin_id,config),
307 "cannot save image %s", file_name);
310 if (strcmp(cfg->sky_dist,
"no_sky") != 0) {
311 sinfo_msg(
"Resampling sky");
313 sky_im = cpl_image_load(cfg->sky_dist,
314 CPL_TYPE_FLOAT, 0, 0));
315 check_nomsg(wim = cpl_image_duplicate(wavemapim));
317 res_ima = sinfo_new_defined_resampling(sky_im, wim,
318 cfg->ncoeffs, &cfg->nrows, &dis,
319 &mi, &ma, &cwav, &cpix),
320 " sinfo_definedResampling() failed");
321 res_sky = cpl_image_duplicate(res_ima);
322 sinfo_free_image(&res_ima);
324 sinfo_msg(
"Apply flux correction");
325 cpl_image_divide(res_sky, res_wim);
328 sinfo_free_image(&wim);
329 sinfo_free_image(&sky_im);
330 if ((pdensity == 3) || (pdensity == 2 && frame_index == 0)) {
332 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%d%s",
333 RESAMPLED_OUT_SKY_FILENAME, frame_index,
336 sinfo_pro_save_ima(res_sky,sof,sof,file_name, PRO_RESAMPLED_SKY, qclog_tbl,plugin_id,config),
337 "cannot save image %s", file_name);
343 if (strcmp(cfg->mflat_dist,
"not_found") != 0) {
344 sinfo_msg(
"Resampling master flat");
346 flat_im = cpl_image_load(cfg->mflat_dist,
347 CPL_TYPE_FLOAT, 0, 0),
348 "Distorted master flat field not found\n"
349 "You may have set --stack-flat_ind=FALSE\n"
350 "Flat field resampling skipped");
351 check_nomsg(wim = cpl_image_duplicate(wavemapim));
352 cknull(res_ima = sinfo_new_defined_resampling(flat_im,
361 " sinfo_definedResampling() failed" );
363 res_flat = cpl_image_duplicate(res_ima);
364 sinfo_free_image(&res_ima);
366 sinfo_msg(
"Apply flux correction");
367 cpl_image_divide(res_flat, res_wim);
369 sinfo_free_image(&wim);
370 sinfo_free_image(&flat_im);
371 if ((pdensity == 3) || (pdensity == 2 && frame_index == 0)) {
372 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%d%s",
373 RESAMPLED_OUT_FLAT_FILENAME, frame_index,
376 sinfo_pro_save_ima(res_flat,sof,sof,file_name, PRO_RESAMPLED_FLAT_LAMP, qclog_tbl,plugin_id,config),
377 "cannot save image %s", file_name);
384 sinfo_msg(
"wmin %f wmax %f wcent %f wstep %f cpix %d", mi, ma, cwav,
386 sinfo_free_image(&res_wim);
387 sinfo_free_image(&im);
388 sinfo_free_image(&wavemapim);
396 if (cfg->halocorrectInd == 1) {
397 sinfo_msg(
"Calibration");
398 check_nomsg(halospec = cpl_image_load(cfg->halospectrum,
399 CPL_TYPE_FLOAT,0,0));
401 cknull(calim = sinfo_new_multiply_image_with_spectrum(res_obj,
403 " sinfo_new_multiply_image_with_spectrum() failed" );
405 sinfo_free_image(&halospec);
406 sinfo_free_image(&res_obj);
407 res_obj = cpl_image_duplicate(calim);
408 sinfo_free_image(&calim);
416 sinfo_msg(
"Cube creation");
418 if (cfg->northsouthInd == 0) {
419 sinfo_msg(
"cfg->northsouthInd == 0");
421 slit_edges = sinfo_read_slitlets_edges(cfg->nslits,
423 "error reading slitlets edges");
426 sinfo_msg(
"cfg->northsouthInd != 0");
427 cknull(distances = sinfo_read_distances(cfg->nslits, cfg->distlist),
428 "error reading distances");
431 cknull(correct_dist = (
float*) cpl_calloc(cfg->nslits, sizeof (
float)),
432 " could not allocate memory!");
434 sinfo_msg(
"Create cube object");
435 if (cfg->northsouthInd == 0) {
437 cknull(cube = sinfo_new_make_cube_spi(res_obj,slit_edges,
439 "could not construct data cube!");
443 cknull(cube = sinfo_new_make_cube_dist(res_obj,fcol,distances,
445 "could not construct a data cube!");
447 sinfo_free_image(&res_obj);
449 if (strcmp(cfg->sky_dist,
"no_sky") != 0) {
451 sinfo_msg(
"Create cube sky");
452 if (cfg->northsouthInd == 0) {
453 cknull(csky = sinfo_new_make_cube_spi(res_sky,slit_edges,
455 "could not construct data cube!");
458 cknull(csky = sinfo_new_make_cube_dist(res_sky,fcol,distances,
460 "could not construct a data cube!");
462 sinfo_free_image(&res_sky);
466 if (strcmp(cfg->mflat_dist,
"not_found") != 0) {
467 sinfo_msg(
"Create cube master flat");
468 if (cfg->northsouthInd == 0) {
469 cknull(cflat=sinfo_new_make_cube_spi(res_flat,slit_edges,
471 "could not construct data cube!");
474 cknull(cflat = sinfo_new_make_cube_dist(res_flat,fcol,distances,
476 "could not construct a data cube!");
478 sinfo_free_image(&res_flat);
482 if (cfg->northsouthInd == 0) {
483 sinfo_new_destroy_2Dfloatarray(&slit_edges, cfg->nslits);
486 sinfo_new_destroy_array(&distances);
499 if (strcmp(cfg->sky_dist,
"no_sky") != 0) {
501 cknull(csky2=sinfo_new_fine_tune(csky,
506 " could not fine tune the data cube");
508 sinfo_free_imagelist(&csky);
509 sinfo_msg(
"Stretch output cube along Y direction");
511 cknull(csky = sinfo_new_bin_cube(csky2, 1, 2, 0, 63, 0, 63),
512 "error rebinning sky cube");
513 sinfo_free_imagelist(&csky2);
515 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%2.2d%s",
"out_sky_cube",
516 frame_index,
".fits");
518 sinfo_pro_save_ims(csky,sof,sof,file_name, PRO_OBS_SKY,NULL,plugin_id,config),
519 "cannot dump cube %s", file_name);
520 cknull(eima_med = sinfo_new_median_cube(csky),
521 "Creating an average image");
522 check_nomsg(center_x = cpl_image_get_size_x(eima_med) / 2. + 0.5);
523 check_nomsg(center_y = cpl_image_get_size_y(eima_med) / 2. + 0.5);
525 sinfo_new_set_wcs_cube(csky, file_name, cwav, dis, cpix, center_x,
528 sinfo_free_imagelist(&csky);
530 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%2.2d%s",
"out_sky_med",
531 frame_index,
".fits");
533 sinfo_pro_save_ima(eima_med,sof,sof,file_name, PRO_SKY_MED,NULL,plugin_id,config),
534 "cannot save ima %s", file_name);
537 sinfo_new_set_wcs_image(eima_med, file_name,
538 center_x, center_y));
539 sinfo_free_image(&eima_med);
544 if (strcmp(cfg->mflat_dist,
"not_found") != 0) {
546 cknull(cflat2=sinfo_new_fine_tune(cflat,
551 " could not fine tune the data cube");
553 sinfo_free_imagelist(&cflat);
554 sinfo_msg(
"Stretch output cube along Y direction");
556 cknull(cflat = sinfo_new_bin_cube(cflat2, 1, 2, 0, 63, 0, 63),
557 "Error binning flat cube");
558 sinfo_free_imagelist(&cflat2);
561 sinfo_pro_save_ims(cflat,sof,sof,OBJNOD_OUT_MFLAT_CUBE_FILENAME, PRO_MFLAT_CUBE,NULL,plugin_id,config),
562 "cannot save cube %s",
563 OBJNOD_OUT_MFLAT_CUBE_FILENAME);
565 sinfo_new_set_wcs_cube(cflat, OBJNOD_OUT_MFLAT_CUBE_FILENAME,
566 cwav, dis, cpix, center_x, center_y);
568 cknull(eima_avg = sinfo_new_average_cube_to_image(cflat),
569 "Creating an average image");
572 sinfo_pro_save_ima(eima_avg,sof,sof,
"out_mflat_avg.fits",
"MFLAT_AVG",NULL,plugin_id,config),
573 "cannot save ima %s",
"out_mflat_avg.fits");
575 sinfo_free_image(&eima_avg);
577 cknull(eima_med = sinfo_new_median_cube(cflat),
578 "Error computing median on cube flat");
581 sinfo_pro_save_ima(eima_med,sof,sof,
"out_mflat_med.fits",
"MFLAT_MED",NULL,plugin_id,config),
582 "cannot save ima %s",
"out_mflat_med.fits");
584 sinfo_free_imagelist(&cflat);
585 sinfo_free_image(&eima_med);
590 cknull(outcube2=sinfo_new_fine_tune(cube,
595 " could not fine tune the data cube");
597 sinfo_msg(
"Stretch output cube along Y direction");
598 cknull(outcube = sinfo_new_bin_cube(outcube2, 1, 2, 0, 63, 0, 63),
599 "Error binning cube");
600 sinfo_free_imagelist(&cube);
601 cknull_nomsg(qclog_tbl = sinfo_qclog_init());
602 sinfo_get_pupil_shift(outcube, n, &qclog_tbl);
603 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%2.2d%s",
"out_cube_obj",
604 frame_index,
".fits");
606 check_nomsg(center_x = cpl_image_get_size_x(
607 cpl_imagelist_get(outcube,0))/2.+0.5);
609 center_y = cpl_image_get_size_y(cpl_imagelist_get(outcube, 0))
613 if (cfg->polyshiftname && strlen(cfg->polyshiftname)) {
614 sinfo_atm_correction(&outcube, sof, cfg->polyshiftname, dis, cwav, cpix);
619 sinfo_pro_save_ims(outcube, sof, sof, file_name, pro_obs,
620 qclog_tbl, plugin_id, config),
621 "cannot save cube %s", file_name);
623 sinfo_new_set_wcs_cube(outcube, file_name, cwav, dis, cpix, center_x, center_y);
627 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.objnod.sky_cor"));
629 check_nomsg(sky_cor = cpl_parameter_get_bool(p));
630 if (sky_cor == 1 && (pdensity == 1 || pdensity == 3)
631 && strcmp(cfg->sky_dist,
"no_sky") != 0) {
632 obj_frm = cpl_frameset_find(sof, pro_obs);
633 sky_frm = cpl_frameset_find(sof, PRO_OBS_SKY);
634 sqc = sinfo_skycor_qc_new();
635 ck0(sinfo_skycor(config, obj_frm, sky_frm, sqc, &obj_cor, &int_obj),
636 "determining sky residuals corrected object");
637 cpl_frameset_erase(sof, pro_obs);
638 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%2.2d%s",
"out_cube_obj_cor",
639 frame_index,
".fits");
641 sinfo_qclog_add_int(qclog_tbl,
"QC SKYCOR THBKGFIT",
643 "Thermal background fit success",
"%d"));
646 sinfo_pro_save_ims(obj_cor, sof, sof, file_name, pro_obs,
647 qclog_tbl, plugin_id, config),
648 "cannot save cube %s", file_name);
650 sinfo_skycor_qc_delete(&sqc);
652 sinfo_new_set_wcs_cube(obj_cor, file_name, cwav, dis, cpix, center_x,
655 sinfo_free_imagelist(&obj_cor);
656 snprintf(file_name, MAX_NAME_SIZE - 1,
"%s%2.2d%s",
"out_int_obj",
657 frame_index,
".fits");
660 sinfo_pro_save_tbl(int_obj,sof,sof,file_name, PRO_SPECTRA_QC,qclog_tbl,plugin_id,config),
661 "cannot save cube %s", file_name);
662 sinfo_free_table(&int_obj);
664 sinfo_free_table(&qclog_tbl);
665 sinfo_free_imagelist(&outcube2);
666 sinfo_free_imagelist(&outcube);
667 sinfo_free_float(&correct_dist);
672 sinfo_objnod_free(&cfg);
673 sinfo_free_frameset(&stk);
677 cleanup: sinfo_skycor_qc_delete(&sqc);
678 sinfo_free_imagelist(&obj_cor);
679 sinfo_free_propertylist(&plist);
680 sinfo_free_imagelist(&outcube2);
681 sinfo_free_imagelist(&outcube);
682 sinfo_free_table(&qclog_tbl);
683 sinfo_free_image(&eima_avg);
684 sinfo_free_image(&eima_med);
685 sinfo_free_imagelist(&cflat);
686 sinfo_free_imagelist(&cflat2);
687 sinfo_free_imagelist(&cube);
688 sinfo_free_imagelist(&csky);
689 sinfo_free_imagelist(&csky2);
692 if (cfg->northsouthInd == 0) {
693 if (slit_edges != NULL ) {
694 sinfo_new_destroy_2Dfloatarray(&slit_edges, cfg->nslits);
698 if (distances != NULL ) {
699 sinfo_new_destroy_array(&distances);
704 sinfo_free_float(&correct_dist);
705 sinfo_free_image(&res_flat);
706 sinfo_free_image(&res_sky);
707 sinfo_free_image(&res_wim);
708 sinfo_free_image(&calim);
709 sinfo_free_image(&halospec);
710 sinfo_free_image(&sky_im);
711 sinfo_free_image(&res_obj);
712 sinfo_free_image(&flat_im);
713 sinfo_free_image(&wavemapim);
714 sinfo_free_image(&wim);
715 sinfo_free_image(&im);
716 sinfo_objnod_free(&cfg);
717 sinfo_free_frameset(&stk);
724 sinfo_atm_correction(cpl_imagelist** ppCube, cpl_frameset* sof,
725 const char* polyshiftname,
double dis_cube,
726 double centralLambda_cube,
int centralpix_cube)
728 cpl_polynomial* poly = NULL;
729 const char* ref_file = NULL;
730 cpl_propertylist *plist_cube = NULL;
731 cpl_propertylist* ppolylist = NULL;
732 cpl_imagelist* retcube = NULL;
736 cpl_imagelist* outcube = *ppCube;
739 cpl_frameset_iterator* it = cpl_frameset_iterator_new(sof);
740 cpl_frame *first_frame = cpl_frameset_iterator_get(it);
742 ref_file = cpl_frame_get_filename(first_frame);
743 if (ref_file && strlen(ref_file)) {
746 plist_cube = cpl_propertylist_load(ref_file, 0);
749 ppolylist = cpl_propertylist_load(polyshiftname, 0);
751 double pixelscale = sinfo_pfits_get_pixscale(plist_cube);
752 double poly_pixelscale = sinfo_pfits_get_pixscale(ppolylist);
753 if (fabs(poly_pixelscale - pixelscale) < 1E-8) {
754 poly = sinfo_atmo_load_polynom(polyshiftname);
765 "is different: provided[%f] expected[%f]",
766 poly_pixelscale, pixelscale);
768 sinfo_free_propertylist(&ppolylist);
772 " would not be applied");
779 double airmass = ((cpl_propertylist_get_double(plist_cube,
780 "ESO TEL AIRM START")
781 + cpl_propertylist_get_double(plist_cube,
782 "ESO TEL AIRM END")) / 2);
783 double angle = ((cpl_propertylist_get_double(plist_cube,
784 "ESO ADA ABSROT START")
785 + cpl_propertylist_get_double(plist_cube,
786 "ESO ADA ABSROT END")) / 2) * PI_NUMB / 180;
787 sinfo_msg(
"dis_cube[%f] centralLambda_cube[%f] centralpix_cube[%d]",
788 dis_cube, centralLambda_cube, centralpix_cube);
789 retcube = sinfo_atmo_apply_cube_polynomial_shift(poly, outcube,
790 centralLambda_cube, airmass, angle, dis_cube,
792 sinfo_free_polynomial(&poly);
794 sinfo_free_imagelist(&outcube);
798 sinfo_free_propertylist(&plist_cube);
800 cpl_frameset_iterator_delete(it);
#define sinfo_msg_debug(...)
Print a debug message.
#define sinfo_msg_error(...)
Print an error message.
#define sinfo_msg_warning(...)
Print an warning message.