SINFONI Pipeline Reference Manual  2.6.0
sinfo_new_objnod.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program 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  File name : sinfo_new_objnod.c
22  Author : J. Schreiber
23  Created on : December 3, 2003
24  Description : Creates data cubes or merges data cubes
25  out of jittered object-sky
26  nodding observations
27  ---------------------------------------------------------------------------*/
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 /*----------------------------------------------------------------------------
33  Includes
34  ---------------------------------------------------------------------------*/
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"
46 
47 /*----------------------------------------------------------------------------
48  Defines
49  ---------------------------------------------------------------------------*/
50 #define PI_NUMB (3.1415926535897932384626433832795) /* pi */
51 
52 
53 /*----------------------------------------------------------------------------
54  Function Definitions
55  ---------------------------------------------------------------------------*/
56 
65 /*----------------------------------------------------------------------------
66  Function : sinfo_new_objnod()
67  In : ini_file: file name of according .ini file
68  Out : integer (0 if it worked, -1 if it doesn't)
69  Job : this routine carries through the data cube creation of an
70  object science observation using object-sky nodding
71  and jittering. This script expects jittered frames that
72  were already sky-subtracted
73  averaged, flatfielded, spectral tilt corrected and
74  interleaved if necessary
75  ---------------------------------------------------------------------------*/
76 int sinfo_new_objnod (const char* plugin_id,cpl_parameterlist* config,
77  cpl_frameset* sof, const char* procatg)
78 {
79 
80  object_config * cfg=NULL ;
81  cpl_image * im=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;
104 
105 
106  int i=0;
107  int n=0;
108  /* int partind = 0 ; */
109  int centralpix=0 ;
110  int z_siz=0;
111  int z_min=0;
112  int z_max=0;
113  int z=0;
114  int z_stp=100;
115  int scales_sky=0;
116  int ks_clip=0;
117  double kappa=2.0;
118 
119  float ref_offx=0;
120  float ref_offy=0;
121  float mi=0 ;
122  float ma=0 ;
123  float fcol=0 ;
124  float center_x=0;
125  /* float newcenter_x=0 ; */
126  float center_y=0;
127  /* float newcenter_y=0; */
128  /*
129  float cd1_1=0;
130  float cd1_2=0;
131  float cd2_1=0;
132  float cd2_2=0;
133  float pixelscale=0;
134  */
135  //float angle=0;
136  //float radangle=0;
137  double exptime=0;
138 
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;
149 
150  double dis=0;
151  double centralLambda=0;
152 
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];
157 
158 
159  char * name=NULL ;
160  char * partname=NULL;
161  /* char * partname2=NULL; */
162  char file_name[MAX_NAME_SIZE];
163  int vllx=0;
164  int vlly=0;
165  int vurx=0;
166  int vury=0;
167 
168  int onp=0;
169  int j=0;
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;
179 
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));
183 
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);
188 
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);
193  } else {
194  strcpy(pro_mjit,PRO_MASK_COADD_OBJ);
195  strcpy(pro_obs,PRO_OBS_OBJ);
196  strcpy(pro_med,PRO_MED_COADD_OBJ);
197  }
198 
199 
200  /*----parse input data and parameters to set cube_config cfg---*/
201  check_nomsg(stk = cpl_frameset_new());
202 
203  cknull(cfg = sinfo_parse_cpl_input_objnod(config,sof,&stk),
204  "Error setting parameter configuration");
205 
206  ck0(sinfo_check_input_data(cfg),"error checking input");
207 
208  if ( cfg->jitterind == 1 )
209  {
210  cknull(times = (double*) cpl_calloc (cfg->nframes, sizeof (double)),
211  " could not allocate memory!") ;
212 
213  cknull(offsetx = (float*) cpl_calloc (cfg->nframes, sizeof(float)),
214  " could not allocate memory!") ;
215 
216  cknull(offsety = (float*) cpl_calloc (cfg->nframes, sizeof(float)),
217  " could not allocate memory!") ;
218  }
219 
220  if (cfg->jitterind == 0)
221  {
222  if ( NULL != (partname = strtok(cfg->outName, ".")))
223  {
224  /* partname2 = strtok (NULL, ".") ;*/
225  /* partind = 1 ; */
226  }
227  }
228 
229  ck0(sinfo_auto_size_cube5(cfg,&ref_offx,&ref_offy,
230  &offx_min,&offy_min,
231  &offx_max,&offy_max),
232  "Error resizing cube");
233 
234 
235 
236  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.fcol"));
237  check_nomsg(fcol=cpl_parameter_get_double(p));
238 
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);
243  } else {
244  sinfo_msg("Illumination correction cube not provided");
245  cpl_error_reset();
246  }
247 
248  for ( n = 0 ; n < cfg->nframes ; n++ )
249  {
250 
251  sinfo_msg("Read FITS information");
252  name = cfg->framelist[n] ;
253  if (n == 0)
254  {
255  strcpy (name_jitter, name) ;
256  }
257  if( sinfo_is_fits_file(name) != 1) {
258  sinfo_msg_error("Input file %s is not FITS",name);
259  goto cleanup;
260  }
261 
262  /* get some header values and compute the CD-sinfo_matrix */
263  plist=cpl_propertylist_load(name,0);
264  /* pixelscale = sinfo_pfits_get_pixscale(plist) /2; */
265  //angle = sinfo_pfits_get_posangle(plist) ;
266  /* in PUPIL data there is not posangle info: we reset the error */
267  if(cpl_error_get_code() != CPL_ERROR_NONE) {
268  cpl_error_reset();
269  }
270  sinfo_free_propertylist(&plist);
271  /*
272  radangle = angle * PI_NUMB / 180. ;
273  cd1_1 = cos(radangle) ;
274  cd1_2 = sin(radangle) ;
275  cd2_1 = -sin(radangle) ;
276  cd2_2 = cos(radangle) ;
277  */
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) ;
281 
282  if (cfg->jitterind == 1)
283  {
284  exptime = sinfo_pfits_get_ditndit(name) ;
285 
286  if (exptime == FLAG)
287  {
288  sinfo_msg_error("could not read fits header keyword exptime!");
289  goto cleanup;
290  }
291  times[n] = exptime ;
292  ck0(sinfo_new_assign_offset(n,name,offsetx,offsety,
293  ref_offx,ref_offy),
294  "Error assigning offsets");
295 
296  }
297 
298  /*
299  *--------------------------------------------------------------
300  *---------------------RESAMPLING-------------------------------
301  *--------------------------------------------------------------
302  */
303  sinfo_msg("Resampling object");
304  cknull(wavemapim = cpl_image_load(cfg->wavemap,CPL_TYPE_FLOAT,0,0),
305  "could not load wavemap");
306 
307  cknull(resampledImage = sinfo_new_defined_resampling( im,
308  wavemapim,
309  cfg->ncoeffs,
310  &cfg->nrows,
311  &dis,
312  &mi,
313  &ma,
314  &centralLambda,
315  &centralpix),
316  " sinfo_definedResampling() failed" ) ;
317 
318 
319  if(n ==0) {
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,
327  wavemapim,
328  cfg->ncoeffs,
329  &cfg->nrows,
330  &dis,
331  &mi,
332  &ma,
333  &centralLambda,
334  &centralpix),
335  " sinfo_definedResampling() failed" ) ;
336 
337  sinfo_free_image(&flat_im) ;
338  }
339 
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,
345  wavemapim,
346  cfg->ncoeffs,
347  &cfg->nrows,
348  &dis,
349  &mi,
350  &ma,
351  &centralLambda,
352  &centralpix),
353  " sinfo_definedResampling() failed" );
354 
355  sinfo_free_image(&sky_im) ;
356 
357 
358  }
359  }
360 
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) ;
364 
365  sinfo_free_image(&im) ;
366  sinfo_free_image(&wavemapim) ;
367 
368  /*
369  *-------------------------------------------------------------------
370  *----------------Calibration----------------------------------------
371  *-------------------------------------------------------------------
372  */
373  /*----Multiply with calibrated halogen lamp spectrum----*/
374  if (cfg->halocorrectInd == 1)
375  {
376  sinfo_msg("Calibration");
377  check_nomsg(halospec = cpl_image_load(cfg->halospectrum,
378  CPL_TYPE_FLOAT,0,0)) ;
379 
380  cknull(calim = sinfo_new_multiply_image_with_spectrum(resampledImage,
381  halospec),
382  " sinfo_new_multiply_image_with_spectrum() failed" ) ;
383 
384  sinfo_free_image(&halospec) ;
385  sinfo_free_image(&resampledImage) ;
386  resampledImage = cpl_image_duplicate(calim) ;
387  sinfo_free_image(&calim);
388  }
389 
390  /*
391  *-------------------------------------------------------------------
392  *------------------CUBECREATION-------------------------------------
393  *-------------------------------------------------------------------
394  */
395  sinfo_msg("Cube creation");
396  /*---select north-south-test or fitting of slitlet edges--*/
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");
401  } else {
402  sinfo_msg("cfg->northsouthInd != 0");
403  cknull(distances = sinfo_read_distances(cfg->nslits,cfg->distlist),
404  "error reading distances");
405  }
406 
407  cknull(correct_dist = (float*) cpl_calloc(cfg->nslits, sizeof (float)),
408  " could not allocate memory!") ;
409 
410  sinfo_msg("Create cube object");
411  if (cfg->northsouthInd ==0 ) {
412 
413  cknull(cube = sinfo_new_make_cube_spi(resampledImage,
414  slit_edges,
415  correct_dist),
416  " could not construct data cube!") ;
417 
418  } else {
419  cknull(cube = sinfo_new_make_cube_dist(resampledImage,
420  fcol,
421  distances,
422  correct_dist),
423  " could not construct a data cube!") ;
424  }
425  sinfo_free_image(&resampledImage);
426 
427  if(n==0) {
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,
432  slit_edges,
433  correct_dist),
434  " could not construct data cube!") ;
435  } else {
436  cknull(cflat = sinfo_new_make_cube_dist(res_flat,
437  fcol,
438  distances,
439  correct_dist),
440  " could not construct a data cube!") ;
441  }
442  sinfo_free_image(&res_flat);
443  }
444  if(strcmp(cfg->sky_dist,"no_sky")!=0) {
445 
446  sinfo_msg("Create cube sky");
447  if (cfg->northsouthInd ==0 ) {
448  cknull(csky = sinfo_new_make_cube_spi(res_sky,
449  slit_edges,
450  correct_dist),
451  " could not construct data cube!") ;
452  } else {
453  cknull(csky = sinfo_new_make_cube_dist(res_sky,
454  fcol,
455  distances,
456  correct_dist),
457  " could not construct a data cube!") ;
458  }
459  sinfo_free_image(&res_sky);
460  }
461  }
462 
463 
464  if (cfg->northsouthInd ==0 )
465  {
466  sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
467  }
468  else
469  {
470  sinfo_new_destroy_array(&distances);
471  }
472 
473  /*
474  *--------------------------------------------------------------------
475  *------------------------FINETUNING----------------------------------
476  *--------------------------------------------------------------------
477  * shift the rows of the reconstructed images of the data cube to the
478  * correct sub pixel position select the shift method: polynomial
479  * interpolation, FFT or cubic spline interpolation
480  *--------------------------------------------------------------------
481  */
482 
483  if(n==0) {
484  if(strcmp(cfg->sky_dist,"no_sky")!=0) {
485  cknull(csky2=sinfo_new_fine_tune(csky,
486  correct_dist,
487  cfg->method,
488  cfg->order,
489  cfg->nslits),
490  " could not fine tune the data cube") ;
491 
492  sinfo_free_imagelist(&csky);
493  sinfo_msg("Stretch output cube along Y direction");
494 
495  cknull(csky = sinfo_new_bin_cube(csky2,1,2,0,63,0,63),
496  "error rebinning sky cube");
497  sinfo_free_imagelist(&csky2);
498 
499 
500 
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");
504 
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);
509 
510  sinfo_new_set_wcs_cube(csky, "out_sky_cube.fits", centralLambda,
511  dis, centralpix, center_x, center_y);
512 
513  sinfo_free_imagelist(&csky) ;
514 
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");
518 
519  sinfo_new_set_wcs_image(eima_med,"out_sky_med.fits",
520  center_x, center_y);
521  sinfo_free_image(&eima_med);
522  }
523 
524 
525  if(strcmp(cfg->mflat_dist,"not_found")!=0) {
526 
527  cknull(cflat2=sinfo_new_fine_tune(cflat,correct_dist,
528  cfg->method,cfg->order,
529  cfg->nslits),
530  " could not fine tune the data cube") ;
531 
532  sinfo_free_imagelist(&cflat);
533  sinfo_msg("Stretch output cube along Y direction");
534 
535  cknull(cflat = sinfo_new_bin_cube(cflat2,1,2,0,63,0,63),
536  "Error binning flat cube");
537  sinfo_free_imagelist(&cflat2);
538 
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);
542 
543  cknull(eima_avg=sinfo_new_average_cube_to_image(cflat),
544  "Creating an average image");
545 
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");
549 
550  sinfo_free_image(&eima_avg);
551 
552  cknull(eima_med=sinfo_new_median_cube(cflat),
553  "Error computing median on cube flat");
554 
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");
558 
559  sinfo_free_imagelist(&cflat);
560  sinfo_free_image(&eima_med);
561  }
562  }
563 
564 
565  cknull(outcube2=sinfo_new_fine_tune(cube,
566  correct_dist,
567  cfg->method,
568  cfg->order,
569  cfg->nslits),
570  " could not fine tune the data cube") ;
571 
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);
583 
584 
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 );
590 
591 
592  sinfo_new_set_wcs_cube(outcube, file_name, centralLambda, dis,
593  centralpix, center_x, center_y);
594 
595  /* free memory */
596  /* to prevent error message comment next line */
597  sinfo_free_imagelist(&outcube2);
598  sinfo_free_imagelist(&outcube) ;
599  sinfo_free_float(&correct_dist) ;
600 
601 
602  } /* end loop over n (nframes) */
603 
604  /* leak free */
605  if(cfg->jitterind == 0) {
606  goto exit;
607  }
608 
609  /* Here in case of autojitter we estimate the sky */
610  if( (cfg->size_x*cfg->size_y*cfg->nframes) > (100*mosaic_max_size) ) {
611  sinfo_msg_warning("Coadd cube size:%d,%d. N frames: %d",
612  cfg->size_x,cfg->size_y,cfg->nframes);
613  sinfo_msg_warning("Max allowed should be such that "
614  "sixeX*sixeY*Nframes < 100*%d",mosaic_max_size);
615  goto exit;
616  }
617 
618  if ( cfg->jitterind == 1 )
619  {
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));
628 
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");
635 
636  for ( n = 0 ; n < cfg->nframes ; n++ ) {
637  snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_cube_obj",
638  n,".fits");
639  check_nomsg(cube_tmp[n] = cpl_imagelist_load(file_name,
640  CPL_TYPE_FLOAT,0));
641  check_nomsg(cubeobject[n] = sinfo_new_cube_getvig(cube_tmp[n],
642  1+vllx,1+vlly,
643  64-vurx,64-vury));
644  check_nomsg(sinfo_free_imagelist(&cube_tmp[n]));
645  }
646  sinfo_free_array_imagelist(&cube_tmp);
647 
648  }
649 
650  /*
651  ---------------------------------------------------------------------
652  ------------------------JITTERING------------------------------------
653  ---------------------------------------------------------------------
654  */
655 
656  if (cfg->jitterind == 1)
657  {
658  sinfo_msg("Jittering...");
659 
660  sinfo_msg("Coadded cube size. x: %d y: %d",
661  cfg->size_x,cfg->size_y);
662  check_nomsg(jittercube = cpl_imagelist_new()) ;
663 
664 
665  /*
666  ---------------------------------------------------------------------
667  -------------------THOMAS ALGORITHM----------------------------------
668  ---------------------------------------------------------------------
669  */
670 
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));
678 
679 
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]));
685  }
686  }
687 
688 
689  /* AMO CHECK */
690 
691  cknull(maskcube=cpl_imagelist_new(),"could not allocate cube!");
692 
693  /* Illumination correction */
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);
698  }
699  }
700  sinfo_free_image(&ill_cor);
701 
702  sinfo_msg("Combine jittered cubes");
703 
704 
705  if(ks_clip == 1) {
706  sinfo_msg("Cube coaddition with kappa-sigma");
707  }
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);
711  z_min=z;
712  z_max=z_min+z_siz;
713  sinfo_msg("Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
714  z_min,z_max,onp);
715 
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));
723  }
724  if(ks_clip == 1) {
725  sinfo_new_combine_jittered_cubes_thomas_range(cubeobject,
726  jittercube,
727  maskcube,
728  cfg->nframes,
729  offsetx,offsety,
730  times,
731  cfg->kernel_type,
732  z_min,
733  z_max,
734  kappa);
735 
736  } else {
737  sinfo_new_combine_jittered_cubes_range(cubeobject,
738  jittercube,
739  maskcube,
740  cfg->nframes,
741  offsetx,
742  offsety,
743  times,
744  cfg->kernel_type,
745  z_min,
746  z_max) ;
747  }
748  }
749  sinfo_new_convert_0_to_ZERO_for_cubes(jittercube) ;
750 
751  if (jittercube == NULL)
752  {
753  sinfo_msg_error(" could not allocate new data cube!") ;
754  goto cleanup;
755  }
756 
757  if (maskcube == NULL)
758  {
759  sinfo_msg_error(" could not merge the jittered data cubes\n") ;
760  goto cleanup;
761  }
762 
763  for ( i = 0 ; i <cfg->nframes ; i++ ) {
764  sinfo_free_imagelist(&cubeobject[i]);
765  }
766  sinfo_free_array_imagelist(&cubeobject);
767 
768  /*
769  newcenter_x = cfg->size_x / 2. + 0.5 ;
770  newcenter_y = cfg->size_y / 2. + 0.5 ;
771  */
772  ck0(sinfo_pro_save_ims(jittercube,sof,sof,cfg->outName,
773  procatg,NULL,plugin_id,config),
774  "cannot save cube %s", cfg->outName);
775 
776  sinfo_new_set_wcs_cube(jittercube, cfg->outName, centralLambda,
777  dis, centralpix, center_x, center_y);
778 
779  cknull(jitter_image = sinfo_new_median_cube(jittercube),
780  " could not do sinfo_medianCube()");
781 
782 
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);
786 
787  sinfo_new_set_wcs_image(jitter_image, cfg->med_cube_name,
788  center_x,center_y);
789 
790  sinfo_free_image(&jitter_image);
791 
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);
795 
796  sinfo_new_set_wcs_cube(maskcube, cfg->maskname, centralLambda,
797  dis, centralpix, center_x, center_y);
798 
799  sinfo_free_double(&times) ;
800  sinfo_free_float(&offsetx) ;
801  sinfo_free_float(&offsety) ;
802  sinfo_free_imagelist(&maskcube) ;
803  sinfo_free_imagelist(&jittercube) ;
804 
805  } /* end of jittering */
806 
807  exit:
808 
809  /* free memory */
810  sinfo_objnod_free(&cfg);
811  sinfo_free_frameset(&stk);
812  return 0;
813 
814  cleanup:
815  sinfo_free_propertylist(&plist);
816  sinfo_free_image(&jitter_image);
817  sinfo_free_imagelist(&jittercube) ;
818  sinfo_free_imagelist(&maskcube) ;
819 
820  if(cfg != NULL) {
821  if(cube_tmp != NULL) {
822  for ( n = 0 ; n < cfg->nframes ; n++ ) {
823  sinfo_free_imagelist(&(cube_tmp[n]));
824  }
825  sinfo_free_array_imagelist(&cube_tmp);
826  }
827  if(cubeobject != NULL) {
828  for ( n = 0 ; n < cfg->nframes ; n++ ) {
829  sinfo_free_imagelist(&(cubeobject[n]));
830  }
831  sinfo_free_array_imagelist(&cubeobject);
832  }
833 
834  }
835 
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) ;
846 
847  if(cfg!=NULL) {
848  if (cfg->northsouthInd ==0 ) {
849  if(slit_edges != NULL) {
850  sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
851  }
852  } else {
853  if (distances != NULL ) {
854  sinfo_new_destroy_array(&distances);
855  }
856  }
857  }
858 
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(&times);
873  sinfo_objnod_free(&cfg);
874  sinfo_free_frameset(&stk);
875 
876  return -1;
877 
878 
879 
880 }
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69
#define sinfo_msg_warning(...)
Print an warning message.
Definition: sinfo_msg.h:93