35 #include "sinfo_new_objnod.h"
36 #include "sinfo_hidden.h"
37 #include "sinfo_pro_save.h"
38 #include "sinfo_objnod_ini_by_cpl.h"
39 #include "sinfo_functions.h"
40 #include "sinfo_pfits.h"
41 #include "sinfo_utilities_scired.h"
42 #include "sinfo_wave_calibration.h"
43 #include "sinfo_cube_construct.h"
44 #include "sinfo_error.h"
45 #include "sinfo_utils_wrappers.h"
50 #define PI_NUMB (3.1415926535897932384626433832795)
76 int sinfo_new_objnod (
const char* plugin_id,cpl_parameterlist* config,
77 cpl_frameset* sof,
const char* procatg)
80 object_config * cfg=NULL ;
82 cpl_image * wavemapim=NULL ;
83 cpl_image * resampledImage=NULL ;
84 cpl_image * calim=NULL ;
85 cpl_image * halospec=NULL ;
86 cpl_image * sky_im=NULL;
87 cpl_image* res_flat=NULL;
88 cpl_image* res_sky=NULL;
89 cpl_image* flat_im=NULL;
90 cpl_image* jitter_image=NULL;
91 cpl_image* eima_avg=NULL;
92 cpl_image* eima_med=NULL;
93 cpl_imagelist * cube=NULL ;
94 cpl_imagelist * outcube=NULL ;
95 cpl_imagelist * outcube2=NULL ;
96 cpl_imagelist ** cubeobject=NULL ;
97 cpl_imagelist ** cube_tmp=NULL ;
98 cpl_imagelist * jittercube=NULL ;
99 cpl_imagelist * maskcube=NULL ;
100 cpl_imagelist* cflat=NULL;
101 cpl_imagelist* cflat2=NULL;
102 cpl_imagelist* csky=NULL;
103 cpl_imagelist* csky2=NULL;
139 float * correct_dist=NULL ;
140 float * distances=NULL ;
141 double * times=NULL ;
142 float * offsetx=NULL;
143 float * offsety=NULL;
144 float ** slit_edges=NULL ;
145 float offx_min=1.e10;
146 float offy_min=1.e10;
147 float offx_max=-1.e10;
148 float offy_max=-1.e10;
151 double centralLambda=0;
153 char name_jitter[MAX_NAME_SIZE] ;
154 char pro_mjit[MAX_NAME_SIZE];
155 char pro_obs[MAX_NAME_SIZE];
156 char pro_med[MAX_NAME_SIZE];
160 char * partname=NULL;
162 char file_name[MAX_NAME_SIZE];
170 cpl_image* j_img=NULL;
171 cpl_image* m_img=NULL;
172 cpl_table* qclog_tbl=NULL;
173 cpl_image* ill_cor=NULL;
174 cpl_frame* frame=NULL;
175 cpl_frameset* stk=NULL;
176 cpl_parameter* p=NULL;
177 cpl_propertylist* plist=NULL;
178 int mosaic_max_size=0;
180 check_nomsg(p=cpl_parameterlist_find(config,
181 "sinfoni.objnod.mosaic_max_size"));
182 check_nomsg(mosaic_max_size=cpl_parameter_get_int(p));
184 if (strcmp(procatg,PRO_COADD_STD) == 0) {
185 strcpy(pro_mjit,PRO_MASK_COADD_STD);
186 strcpy(pro_obs,PRO_OBS_STD);
187 strcpy(pro_med,PRO_MED_COADD_STD);
189 }
else if (strcmp(procatg,PRO_COADD_PSF) == 0) {
190 strcpy(pro_mjit,PRO_MASK_COADD_PSF);
191 strcpy(pro_obs,PRO_OBS_PSF);
192 strcpy(pro_med,PRO_MED_COADD_PSF);
194 strcpy(pro_mjit,PRO_MASK_COADD_OBJ);
195 strcpy(pro_obs,PRO_OBS_OBJ);
196 strcpy(pro_med,PRO_MED_COADD_OBJ);
201 check_nomsg(stk = cpl_frameset_new());
203 cknull(cfg = sinfo_parse_cpl_input_objnod(config,sof,&stk),
204 "Error setting parameter configuration");
206 ck0(sinfo_check_input_data(cfg),
"error checking input");
208 if ( cfg->jitterind == 1 )
210 cknull(times = (
double*) cpl_calloc (cfg->nframes, sizeof (
double)),
211 " could not allocate memory!") ;
213 cknull(offsetx = (
float*) cpl_calloc (cfg->nframes,
sizeof(
float)),
214 " could not allocate memory!") ;
216 cknull(offsety = (
float*) cpl_calloc (cfg->nframes,
sizeof(
float)),
217 " could not allocate memory!") ;
220 if (cfg->jitterind == 0)
222 if ( NULL != (partname = strtok(cfg->outName,
".")))
229 ck0(sinfo_auto_size_cube5(cfg,&ref_offx,&ref_offy,
231 &offx_max,&offy_max),
232 "Error resizing cube");
236 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.objnod.fcol"));
237 check_nomsg(fcol=cpl_parameter_get_double(p));
239 if(NULL != cpl_frameset_find(sof,PRO_ILL_COR)) {
240 sinfo_msg(
"Illumination correction cube is provided");
241 frame = cpl_frameset_find(sof,PRO_ILL_COR);
242 ill_cor=cpl_image_load(cpl_frame_get_filename(frame),CPL_TYPE_FLOAT,0,0);
244 sinfo_msg(
"Illumination correction cube not provided");
248 for ( n = 0 ; n < cfg->nframes ; n++ )
251 sinfo_msg(
"Read FITS information");
252 name = cfg->framelist[n] ;
255 strcpy (name_jitter, name) ;
257 if( sinfo_is_fits_file(name) != 1) {
263 plist=cpl_propertylist_load(name,0);
267 if(cpl_error_get_code() != CPL_ERROR_NONE) {
270 sinfo_free_propertylist(&plist);
278 sinfo_msg(
"frame no.: %d, name: %s\n", n, name) ;
279 cknull(im = cpl_image_load(name,CPL_TYPE_FLOAT,0,0),
280 " could not load frame %s!",name) ;
282 if (cfg->jitterind == 1)
284 exptime = sinfo_pfits_get_ditndit(name) ;
292 ck0(sinfo_new_assign_offset(n,name,offsetx,offsety,
294 "Error assigning offsets");
303 sinfo_msg(
"Resampling object");
304 cknull(wavemapim = cpl_image_load(cfg->wavemap,CPL_TYPE_FLOAT,0,0),
305 "could not load wavemap");
307 cknull(resampledImage = sinfo_new_defined_resampling( im,
316 " sinfo_definedResampling() failed" ) ;
320 if(strcmp(cfg->mflat_dist,
"not_found") != 0) {
321 sinfo_msg(
"Resampling master flat");
322 cknull(flat_im=cpl_image_load(cfg->mflat_dist,CPL_TYPE_FLOAT,0,0),
323 "Distorted master flat field not found\n"
324 "You may have set --stack-flat_ind=FALSE\n"
325 "Flat field resampling skipped");
326 cknull(res_flat = sinfo_new_defined_resampling(flat_im,
335 " sinfo_definedResampling() failed" ) ;
337 sinfo_free_image(&flat_im) ;
340 if(strcmp(cfg->sky_dist,
"no_sky")!=0) {
341 sinfo_msg(
"Resampling sky");
342 check_nomsg(sky_im=cpl_image_load(cfg->sky_dist,
343 CPL_TYPE_FLOAT,0,0));
344 cknull(res_sky = sinfo_new_defined_resampling(sky_im,
353 " sinfo_definedResampling() failed" );
355 sinfo_free_image(&sky_im) ;
361 sinfo_msg (
"dispersion %f\n", dis) ;
362 sinfo_msg (
"lambda min %f max %f cent %f\n", mi,ma,centralLambda ) ;
363 sinfo_msg (
"central pixel %d\n", centralpix) ;
365 sinfo_free_image(&im) ;
366 sinfo_free_image(&wavemapim) ;
374 if (cfg->halocorrectInd == 1)
376 sinfo_msg(
"Calibration");
377 check_nomsg(halospec = cpl_image_load(cfg->halospectrum,
378 CPL_TYPE_FLOAT,0,0)) ;
380 cknull(calim = sinfo_new_multiply_image_with_spectrum(resampledImage,
382 " sinfo_new_multiply_image_with_spectrum() failed" ) ;
384 sinfo_free_image(&halospec) ;
385 sinfo_free_image(&resampledImage) ;
386 resampledImage = cpl_image_duplicate(calim) ;
387 sinfo_free_image(&calim);
395 sinfo_msg(
"Cube creation");
397 if (cfg->northsouthInd == 0) {
398 sinfo_msg(
"cfg->northsouthInd == 0");
399 cknull(slit_edges = sinfo_read_slitlets_edges(cfg->nslits,cfg->poslist),
400 "error reading slitlets edges");
402 sinfo_msg(
"cfg->northsouthInd != 0");
403 cknull(distances = sinfo_read_distances(cfg->nslits,cfg->distlist),
404 "error reading distances");
407 cknull(correct_dist = (
float*) cpl_calloc(cfg->nslits, sizeof (
float)),
408 " could not allocate memory!") ;
410 sinfo_msg(
"Create cube object");
411 if (cfg->northsouthInd ==0 ) {
413 cknull(cube = sinfo_new_make_cube_spi(resampledImage,
416 " could not construct data cube!") ;
419 cknull(cube = sinfo_new_make_cube_dist(resampledImage,
423 " could not construct a data cube!") ;
425 sinfo_free_image(&resampledImage);
428 if(strcmp(cfg->mflat_dist,
"not_found")!=0) {
429 sinfo_msg(
"Create cube master flat");
430 if (cfg->northsouthInd ==0 ) {
431 cknull(cflat=sinfo_new_make_cube_spi(res_flat,
434 " could not construct data cube!") ;
436 cknull(cflat = sinfo_new_make_cube_dist(res_flat,
440 " could not construct a data cube!") ;
442 sinfo_free_image(&res_flat);
444 if(strcmp(cfg->sky_dist,
"no_sky")!=0) {
446 sinfo_msg(
"Create cube sky");
447 if (cfg->northsouthInd ==0 ) {
448 cknull(csky = sinfo_new_make_cube_spi(res_sky,
451 " could not construct data cube!") ;
453 cknull(csky = sinfo_new_make_cube_dist(res_sky,
457 " could not construct a data cube!") ;
459 sinfo_free_image(&res_sky);
464 if (cfg->northsouthInd ==0 )
466 sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
470 sinfo_new_destroy_array(&distances);
484 if(strcmp(cfg->sky_dist,
"no_sky")!=0) {
485 cknull(csky2=sinfo_new_fine_tune(csky,
490 " could not fine tune the data cube") ;
492 sinfo_free_imagelist(&csky);
493 sinfo_msg(
"Stretch output cube along Y direction");
495 cknull(csky = sinfo_new_bin_cube(csky2,1,2,0,63,0,63),
496 "error rebinning sky cube");
497 sinfo_free_imagelist(&csky2);
501 ck0(sinfo_pro_save_ims(csky,sof,sof,
"out_sky_cube.fits",
502 PRO_OBS_SKY,NULL,plugin_id,config),
503 "cannot dump cube %s",
"out_sky_cube.fits");
505 cknull(eima_med=sinfo_new_median_cube(csky),
506 "Creating an average image");
507 check_nomsg(center_x = cpl_image_get_size_x(eima_med)/ 2. + 0.5);
508 check_nomsg(center_y = cpl_image_get_size_y(eima_med)/ 2. + 0.5);
510 sinfo_new_set_wcs_cube(csky,
"out_sky_cube.fits", centralLambda,
511 dis, centralpix, center_x, center_y);
513 sinfo_free_imagelist(&csky) ;
515 ck0(sinfo_pro_save_ima(eima_med,sof,sof,
"out_sky_med.fits",
516 PRO_SKY_MED,NULL,plugin_id,config),
517 "cannot save ima %s",
"out_sky_med.fits");
519 sinfo_new_set_wcs_image(eima_med,
"out_sky_med.fits",
521 sinfo_free_image(&eima_med);
525 if(strcmp(cfg->mflat_dist,
"not_found")!=0) {
527 cknull(cflat2=sinfo_new_fine_tune(cflat,correct_dist,
528 cfg->method,cfg->order,
530 " could not fine tune the data cube") ;
532 sinfo_free_imagelist(&cflat);
533 sinfo_msg(
"Stretch output cube along Y direction");
535 cknull(cflat = sinfo_new_bin_cube(cflat2,1,2,0,63,0,63),
536 "Error binning flat cube");
537 sinfo_free_imagelist(&cflat2);
539 ck0(sinfo_pro_save_ims(cflat,sof,sof,OBJNOD_OUT_MFLAT_CUBE_FILENAME,
540 PRO_MFLAT_CUBE,NULL,plugin_id,config),
541 "cannot save cube %s", OBJNOD_OUT_MFLAT_CUBE_FILENAME);
543 cknull(eima_avg=sinfo_new_average_cube_to_image(cflat),
544 "Creating an average image");
546 ck0(sinfo_pro_save_ima(eima_avg,sof,sof,
"out_mflat_avg.fits",
547 "MFLAT_AVG",NULL,plugin_id,config),
548 "cannot save ima %s",
"out_mflat_avg.fits");
550 sinfo_free_image(&eima_avg);
552 cknull(eima_med=sinfo_new_median_cube(cflat),
553 "Error computing median on cube flat");
555 ck0(sinfo_pro_save_ima(eima_med,sof,sof,
"out_mflat_med.fits",
556 "MFLAT_MED",NULL,plugin_id,config),
557 "cannot save ima %s",
"out_mflat_med.fits");
559 sinfo_free_imagelist(&cflat);
560 sinfo_free_image(&eima_med);
565 cknull(outcube2=sinfo_new_fine_tune(cube,
570 " could not fine tune the data cube") ;
572 sinfo_msg(
"Stretch output cube along Y direction");
573 cknull(outcube = sinfo_new_bin_cube(outcube2,1,2,0,63,0,63),
574 "Error binning cube");
575 sinfo_free_imagelist(&cube);
576 cknull_nomsg(qclog_tbl=sinfo_qclog_init());
577 sinfo_get_pupil_shift(outcube,n,&qclog_tbl);
578 snprintf(file_name,MAX_NAME_SIZE-1,
"%s%2.2d%s",
579 "out_cube_obj",n,
".fits");
580 ck0(sinfo_pro_save_ims(outcube,sof,sof,file_name,
581 pro_obs,qclog_tbl,plugin_id,config),
582 "cannot save cube %s", file_name);
585 sinfo_free_table(&qclog_tbl);
586 check_nomsg(center_x = cpl_image_get_size_x(
587 cpl_imagelist_get(outcube,0))/2.+0.5) ;
588 check_nomsg(center_y = cpl_image_get_size_y(
589 cpl_imagelist_get(outcube,0))/2.+0.5 );
592 sinfo_new_set_wcs_cube(outcube, file_name, centralLambda, dis,
593 centralpix, center_x, center_y);
597 sinfo_free_imagelist(&outcube2);
598 sinfo_free_imagelist(&outcube) ;
599 sinfo_free_float(&correct_dist) ;
605 if(cfg->jitterind == 0) {
610 if( (cfg->size_x*cfg->size_y*cfg->nframes) > (100*mosaic_max_size) ) {
612 cfg->size_x,cfg->size_y,cfg->nframes);
614 "sixeX*sixeY*Nframes < 100*%d",mosaic_max_size);
618 if ( cfg->jitterind == 1 )
620 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.objnod.vllx"));
621 check_nomsg(vllx = cpl_parameter_get_int(p));
622 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.objnod.vlly"));
623 check_nomsg(vlly = cpl_parameter_get_int(p));
624 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.objnod.vurx"));
625 check_nomsg(vurx = cpl_parameter_get_int(p));
626 check_nomsg(p = cpl_parameterlist_find(config,
"sinfoni.objnod.vury"));
627 check_nomsg(vury = cpl_parameter_get_int(p));
629 cknull(cube_tmp =(cpl_imagelist**) cpl_calloc(cfg->nframes,
630 sizeof (cpl_imagelist*)),
631 "Could not allocate memory for cube_tmp");
632 cknull(cubeobject =(cpl_imagelist**) cpl_calloc(cfg->nframes,
633 sizeof(cpl_imagelist*)),
634 "Could not allocate memory for cubeobject");
636 for ( n = 0 ; n < cfg->nframes ; n++ ) {
637 snprintf(file_name,MAX_NAME_SIZE-1,
"%s%2.2d%s",
"out_cube_obj",
639 check_nomsg(cube_tmp[n] = cpl_imagelist_load(file_name,
641 check_nomsg(cubeobject[n] = sinfo_new_cube_getvig(cube_tmp[n],
644 check_nomsg(sinfo_free_imagelist(&cube_tmp[n]));
646 sinfo_free_array_imagelist(&cube_tmp);
656 if (cfg->jitterind == 1)
658 sinfo_msg(
"Jittering...");
660 sinfo_msg(
"Coadded cube size. x: %d y: %d",
661 cfg->size_x,cfg->size_y);
662 check_nomsg(jittercube = cpl_imagelist_new()) ;
671 check_nomsg(p=cpl_parameterlist_find(config,
672 "sinfoni.objnod.scales_sky"));
673 check_nomsg(scales_sky=cpl_parameter_get_bool(p));
674 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.objnod.ks_clip"));
675 check_nomsg(ks_clip = cpl_parameter_get_bool(p));
676 check_nomsg(p=cpl_parameterlist_find(config,
"sinfoni.objnod.kappa"));
677 check_nomsg(kappa = cpl_parameter_get_double(p));
680 if(scales_sky == 1) {
681 sinfo_msg(
"Subtract spatial sinfo_median to each cube plane");
682 for(n=0;n<cfg->nframes;n++) {
683 sinfo_msg(
"process cube %d\n",n);
684 sinfo_new_sinfoni_correct_median_it(&(cubeobject[n]));
691 cknull(maskcube=cpl_imagelist_new(),
"could not allocate cube!");
694 if(ill_cor != NULL) {
695 for(n=0;n<cfg->nframes;n++) {
696 sinfo_msg(
"Illumination correction is applied");
697 cpl_imagelist_divide_image(cubeobject[n],ill_cor);
700 sinfo_free_image(&ill_cor);
702 sinfo_msg(
"Combine jittered cubes");
706 sinfo_msg(
"Cube coaddition with kappa-sigma");
708 check_nomsg(onp=cpl_imagelist_get_size(cubeobject[0]));
709 for(z=0;z<onp;z+=z_stp) {
710 z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
713 sinfo_msg(
"Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
716 for(j=z_min;j<z_max;j++) {
717 check_nomsg(j_img=cpl_image_new(cfg->size_x,
718 cfg->size_y,CPL_TYPE_FLOAT));
719 check_nomsg(cpl_imagelist_set(jittercube,j_img,j));
720 check_nomsg(m_img = cpl_image_new(cfg->size_x,
721 cfg->size_y,CPL_TYPE_FLOAT));
722 check_nomsg(cpl_imagelist_set(maskcube,m_img,j));
725 sinfo_new_combine_jittered_cubes_thomas_range(cubeobject,
737 sinfo_new_combine_jittered_cubes_range(cubeobject,
749 sinfo_new_convert_0_to_ZERO_for_cubes(jittercube) ;
751 if (jittercube == NULL)
757 if (maskcube == NULL)
763 for ( i = 0 ; i <cfg->nframes ; i++ ) {
764 sinfo_free_imagelist(&cubeobject[i]);
766 sinfo_free_array_imagelist(&cubeobject);
772 ck0(sinfo_pro_save_ims(jittercube,sof,sof,cfg->outName,
773 procatg,NULL,plugin_id,config),
774 "cannot save cube %s", cfg->outName);
776 sinfo_new_set_wcs_cube(jittercube, cfg->outName, centralLambda,
777 dis, centralpix, center_x, center_y);
779 cknull(jitter_image = sinfo_new_median_cube(jittercube),
780 " could not do sinfo_medianCube()");
783 ck0(sinfo_pro_save_ima(jitter_image,sof,sof,cfg->med_cube_name,
784 pro_med,NULL,plugin_id,config),
785 "cannot save ima %s", cfg->outName);
787 sinfo_new_set_wcs_image(jitter_image, cfg->med_cube_name,
790 sinfo_free_image(&jitter_image);
792 ck0(sinfo_pro_save_ims(maskcube,sof,sof,cfg->maskname,
793 pro_mjit,NULL,plugin_id,config),
794 "cannot save cube %s", cfg->maskname);
796 sinfo_new_set_wcs_cube(maskcube, cfg->maskname, centralLambda,
797 dis, centralpix, center_x, center_y);
799 sinfo_free_double(×) ;
800 sinfo_free_float(&offsetx) ;
801 sinfo_free_float(&offsety) ;
802 sinfo_free_imagelist(&maskcube) ;
803 sinfo_free_imagelist(&jittercube) ;
810 sinfo_objnod_free(&cfg);
811 sinfo_free_frameset(&stk);
815 sinfo_free_propertylist(&plist);
816 sinfo_free_image(&jitter_image);
817 sinfo_free_imagelist(&jittercube) ;
818 sinfo_free_imagelist(&maskcube) ;
821 if(cube_tmp != NULL) {
822 for ( n = 0 ; n < cfg->nframes ; n++ ) {
823 sinfo_free_imagelist(&(cube_tmp[n]));
825 sinfo_free_array_imagelist(&cube_tmp);
827 if(cubeobject != NULL) {
828 for ( n = 0 ; n < cfg->nframes ; n++ ) {
829 sinfo_free_imagelist(&(cubeobject[n]));
831 sinfo_free_array_imagelist(&cubeobject);
836 sinfo_free_imagelist(&outcube2) ;
837 sinfo_free_imagelist(&outcube) ;
838 sinfo_free_table(&qclog_tbl);
839 sinfo_free_image(&eima_avg);
840 sinfo_free_image(&eima_med);
841 sinfo_free_imagelist(&cflat) ;
842 sinfo_free_imagelist(&cflat2) ;
843 sinfo_free_imagelist(&cube) ;
844 sinfo_free_imagelist(&csky) ;
845 sinfo_free_imagelist(&csky2) ;
848 if (cfg->northsouthInd ==0 ) {
849 if(slit_edges != NULL) {
850 sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
853 if (distances != NULL ) {
854 sinfo_new_destroy_array(&distances);
859 sinfo_free_float(&correct_dist);
860 sinfo_free_image(&res_flat);
861 sinfo_free_image(&res_sky);
862 sinfo_free_image(&calim);
863 sinfo_free_image(&halospec) ;
864 sinfo_free_image(&sky_im) ;
865 sinfo_free_image(&resampledImage);
866 sinfo_free_image(&flat_im) ;
867 sinfo_free_image(&wavemapim);
868 sinfo_free_image(&im);
869 sinfo_free_image(&ill_cor);
870 sinfo_free_float(&offsety);
871 sinfo_free_float(&offsetx);
872 sinfo_free_double(×);
873 sinfo_objnod_free(&cfg);
874 sinfo_free_frameset(&stk);