SINFONI Pipeline Reference Manual  2.5.2
sinfoni/sinfo_utl_cube2spectrum.c
1 /* $Id: sinfo_utl_cube2spectrum.c,v 1.11 2012-03-03 10:17:31 amodigli Exp $
2  *
3  * This file is part of the IIINSTRUMENT Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2012-03-03 10:17:31 $
24  * $Revision: 1.11 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 /*----------------------------------------------------------------------------
33  Includes
34  ----------------------------------------------------------------------------*/
35 
36 
37 #include "sinfo_utl_cube2spectrum.h"
38 #include "sinfo_functions.h"
39 #include "sinfo_spectrum_ops.h"
40 #include "sinfo_key_names.h"
41 #include "sinfo_pro_save.h"
42 #include "sinfo_utilities_scired.h"
43 #include "sinfo_globals.h"
44 #include "sinfo_error.h"
45 #include "sinfo_utils_wrappers.h"
46 /*----------------------------------------------------------------------------
47  Functions prototypes
48  ----------------------------------------------------------------------------*/
49 
50 /*----------------------------------------------------------------------------
51  Static variables
52  ----------------------------------------------------------------------------*/
53 
54 static int
55 sinfo_change_header(const char * name);
63 /*-----------------------------------------------------------------------------
64  Function implementation
65  ----------------------------------------------------------------------------*/
66 
67 static int
68 sinfo_change_header(const char * name) {
69  int cenpix=0;
70  double cenLambda=0;
71  double dispersion=0;
72  cpl_propertylist* plist=NULL;
73 
74  plist=cpl_propertylist_load(name,0);
75  cenpix = sinfo_pfits_get_crpix3(plist);
76  cenLambda = sinfo_pfits_get_crval3(plist);
77  dispersion = sinfo_pfits_get_cdelt3(plist);
78 
79 
80  check(cpl_propertylist_set_string(plist,"CTYPE2","WAVE"),"Setting CTYPE2");
81  check_nomsg(cpl_propertylist_set_comment(plist,"CTYPE2",
82  "wavelength axis in microns"));
83 
84  check(cpl_propertylist_set_double(plist,"CRPIX2",(double)cenpix),
85  "Setting CRPIX2");
86  check_nomsg(cpl_propertylist_set_comment(plist,"CRPIX2","Reference pixel"));
87 
88  check(cpl_propertylist_set_double(plist,"CRVAL2",cenLambda),
89  "Setting CRVAL2");
90  check_nomsg(cpl_propertylist_set_comment(plist,"CRVAL2",
91  "central wavelength"));
92 
93  check(cpl_propertylist_set_double(plist,"CDELT2",dispersion),
94  "Setting CDELT2");
95  check_nomsg(cpl_propertylist_set_comment(plist,"CDELT2",
96  "microns per pixel"));
97 
98  check(cpl_propertylist_set_string(plist,"CUNIT2","um"),"Setting CUNIT2");
99  check_nomsg(cpl_propertylist_set_comment(plist,"CUNIT2","spectral unit"));
100 
101  check(cpl_propertylist_set_string(plist,"CTYPE1","ONESPEC"),
102  "Setting CTYPE1");
103  check_nomsg(cpl_propertylist_set_comment(plist,"CTYPE1",
104  "one spectrum in y-direction"));
105 
106  if(cpl_propertylist_has(plist,"CRPIX1")==1) {
107  check(cpl_propertylist_erase_regexp(plist, "CRPIX1",0),"Erasing CRPIX1");
108  }
109 
110  if(cpl_propertylist_has(plist,"CRVAL1")==1) {
111  check(cpl_propertylist_erase_regexp(plist, "CRVAL1",0),"Erasing CRVAL1");
112  }
113 
114  if(cpl_propertylist_has(plist,"CDELT1")==1) {
115  check(cpl_propertylist_erase_regexp(plist, "CDELT1",0),"Erasing CDELT1");
116  }
117 
118 
119  if(cpl_propertylist_has(plist,"CUNIT1")==1) {
120  check(cpl_propertylist_erase_regexp(plist, "CUNIT1",0),"Erasing CUNIT1");
121  }
122 
123 
124  if(cpl_propertylist_has(plist,"CTYPE3")==1) {
125  check(cpl_propertylist_erase_regexp(plist, "CTYPE3",0),"Erasing CTYPE3");
126  }
127 
128 
129  if(cpl_propertylist_has(plist,"CRPIX3")==1) {
130  check(cpl_propertylist_erase_regexp(plist, "CRPIX3",0),"Erasing CRPIX3");
131  }
132 
133  if(cpl_propertylist_has(plist,"CRVAL3")==1) {
134  check(cpl_propertylist_erase_regexp(plist, "CRVAL3",0),"Erasing CRVAL3");
135  }
136 
137  if(cpl_propertylist_has(plist,"CDELT3")==1) {
138  check(cpl_propertylist_erase_regexp(plist, "CDELT3",0),"Erasing CDELT3");
139  }
140 
141  if(cpl_propertylist_has(plist,"CUNIT3")==1) {
142  check(cpl_propertylist_erase_regexp(plist, "CUNIT3",0),"Erasing CUNIT3");
143  }
144 
145 
146  if(cpl_propertylist_has(plist,"CD1_1")==1) {
147  check(cpl_propertylist_erase_regexp(plist, "CD1_1",0),"Erasing CD1_1");
148  }
149 
150 
151  if(cpl_propertylist_has(plist,"CD1_2")==1) {
152  check(cpl_propertylist_erase_regexp(plist, "CD1_2",0),"Erasing CD1_2");
153  }
154 
155  if(cpl_propertylist_has(plist,"CD2_1")==1) {
156  check(cpl_propertylist_erase_regexp(plist, "CD2_1",0),"Erasing CD2_1");
157  }
158 
159  if(cpl_propertylist_has(plist,"CD2_2")==1) {
160  check(cpl_propertylist_erase_regexp(plist, "CD2_2",0),"Erasing CD2_2");
161  }
162 
163  cleanup:
164  sinfo_free_propertylist(&plist);
165 
166  /* Return */
167  if (cpl_error_get_code())
168  return -1 ;
169  else
170  return 0 ;
171 
172 }
173 
174 /*---------------------------------------------------------------------------*/
182 /*---------------------------------------------------------------------------*/
183 int sinfo_utl_cube2spectrum(
184  cpl_parameterlist * parlist,
185  cpl_frameset * framelist,
186  const char* tag)
187 {
188  cpl_parameter * param =NULL;
189  const char * operation =NULL;
190  const char * aperture =NULL;
191  char name_i [MAX_NAME_SIZE];
192  int llx=0;
193  int urx=0;
194  int lly=0;
195  int ury=0;
196  int lo_rej=0;
197  int hi_rej=0;
198  int centerx=0;
199  int centery=0;
200  int radius=0;
201  int clx=0;
202  int cly=0;
203 
204  double disp=0;
205  double cpix=0;
206  double clam=0;
207 
208  cpl_frame * frm_cub=NULL;
209 
210  char ima_o[MAX_NAME_SIZE];
211  char tbl_o[MAX_NAME_SIZE];
212  char tag_i[MAX_NAME_SIZE];
213  char tag_o[MAX_NAME_SIZE];
214  cpl_propertylist * plist =NULL;
215  cpl_image * image =NULL;
216  cpl_frame * product_frame=NULL;
217 
218  cpl_image * im_spec=NULL;
219  cpl_table * tbl_spec=NULL;
220  cpl_image * img=NULL;
221  cpl_imagelist * cube=NULL;
222  Vector * spec=NULL;
223  /*fits_header* head=NULL; */
224 
225  /* HOW TO RETRIEVE INPUT PARAMETERS */
226  /* --stropt */
227  check_nomsg(param=cpl_parameterlist_find(parlist,
228  "sinfoni.sinfo_utl_cube2spectrum.op"));
229  check_nomsg(operation=cpl_parameter_get_string(param));
230 
231  /* --stropt */
232  check_nomsg(param=cpl_parameterlist_find(parlist,
233  "sinfoni.sinfo_utl_cube2spectrum.ap"));
234  check_nomsg(aperture=cpl_parameter_get_string(param));
235  /* --stropt */
236 
237 
238  if (tag == NULL) {
239  strcpy(ima_o,"out_spec_ima.fits");
240  strcpy(tbl_o,"out_spec_tbl.fits");
241  strcpy(tag_i,SI_UTL_CUBE2SPECTRUM_CUBE);
242  strcpy(tag_o,SI_UTL_CUBE2SPECTRUM_PROIMA);
243  } else {
244  snprintf(ima_o,MAX_NAME_SIZE-1,"%s%s",tag,"_spec_ima.fits");
245  snprintf(tbl_o,MAX_NAME_SIZE-1,"%s%s",tag,"_spec_tbl.fits");
246  snprintf(tag_o,MAX_NAME_SIZE-1,"%s%s",tag,"_SPCT");
247  strcpy(tag_i, tag);
248  }
249 
250 
251  /* --intopt */
252  check_nomsg(param=cpl_parameterlist_find(parlist,
253  "sinfoni.sinfo_utl_cube2spectrum.llx"));
254  check_nomsg(llx=cpl_parameter_get_int(param));
255 
256  /* --intopt */
257  check_nomsg(param=cpl_parameterlist_find(parlist,
258  "sinfoni.sinfo_utl_cube2spectrum.lly"));
259  check_nomsg(lly=cpl_parameter_get_int(param));
260 
261  /* --intopt */
262  check_nomsg(param=cpl_parameterlist_find(parlist,
263  "sinfoni.sinfo_utl_cube2spectrum.urx"));
264  check_nomsg(urx=cpl_parameter_get_int(param));
265 
266  /* --intopt */
267  check_nomsg(param=cpl_parameterlist_find(parlist,
268  "sinfoni.sinfo_utl_cube2spectrum.ury"));
269  check_nomsg(ury=cpl_parameter_get_int(param));
270 
271  /* --intopt */
272  check_nomsg(param=cpl_parameterlist_find(parlist,
273  "sinfoni.sinfo_utl_cube2spectrum.lo_rej"));
274  check_nomsg(lo_rej=cpl_parameter_get_int(param));
275 
276  /* --intopt */
277  check_nomsg(param=cpl_parameterlist_find(parlist,
278  "sinfoni.sinfo_utl_cube2spectrum.hi_rej"));
279  check_nomsg(hi_rej=cpl_parameter_get_int(param));
280 
281  /* --intopt */
282  check_nomsg(param=cpl_parameterlist_find(parlist,
283  "sinfoni.sinfo_utl_cube2spectrum.centerx"));
284  check_nomsg(centerx=cpl_parameter_get_int(param));
285 
286  /* --intopt */
287  check_nomsg(param=cpl_parameterlist_find(parlist,
288  "sinfoni.sinfo_utl_cube2spectrum.centery"));
289  check_nomsg(centery=cpl_parameter_get_int(param));
290 
291  /* --intopt */
292  check_nomsg(param=cpl_parameterlist_find(parlist,
293  "sinfoni.sinfo_utl_cube2spectrum.radius"));
294  check_nomsg(radius=cpl_parameter_get_int(param));
295 
296  /* Identify the RAW and CALIB frames in the input frameset */
297  if (sinfo_dfs_set_groups(framelist)) {
298  sinfo_msg_error( "Cannot identify RAW and CALIB frames") ;
299  goto cleanup;
300  }
301 
302  /* HOW TO ACCESS INPUT DATA */
303  cknull(frm_cub=cpl_frameset_find(framelist,tag_i),
304  "SOF does not have a file tagged as %s",tag_i);
305 
306  /* Now performing the data reduction */
307  /* Let's generate one image for the example */
308  check_nomsg(strcpy(name_i,cpl_frame_get_filename(frm_cub)));
309  check_nomsg(cube = cpl_imagelist_load((char*)name_i,CPL_TYPE_FLOAT,0));
310 
311  check_nomsg(img=cpl_imagelist_get(cube,0));
312  check_nomsg(clx=cpl_image_get_size_x(img));
313  check_nomsg(cly=cpl_image_get_size_y(img));
314  check(plist=cpl_propertylist_load(name_i,0),
315  "Cannot read the FITS header") ;
316 
317  cpix = (double) sinfo_pfits_get_crpix3(plist);
318  clam = (double) sinfo_pfits_get_crval3(plist);
319  disp = (double) sinfo_pfits_get_cdelt3(plist);
320  sinfo_free_propertylist(&plist);
321  if(strcmp(aperture,"rectangle") ==0) {
322  if (llx<0) {
323  sinfo_msg_warning("llx=%d too low set it to 0",llx);
324  llx=0;
325  }
326  if (lly<0) {
327  sinfo_msg_warning("lly=%d too low set it to 0",lly);
328  lly=0;
329  }
330  if (urx>clx-1) {
331  sinfo_msg_warning("urx=%d too large set it to %d",urx,clx-1);
332  urx=clx-1;
333  }
334  if (ury>cly-1) {
335  sinfo_msg_warning("ury=%d too large set it to %d",ury,cly-1);
336  ury=cly-1;
337  }
338  if(llx>=urx) {
339  sinfo_msg_error("llx>=urx!");
340  goto cleanup;
341  }
342  if(lly>=ury) {
343  sinfo_msg_error("lly>=ury!");
344  goto cleanup;
345  }
346 
347  }
348 
349  if(strcmp(aperture,"circle") ==0) {
350  if((centerx-radius) < 0) {
351  sinfo_msg_error("It is not possible to set centerx-radius<0.");
352  goto cleanup;
353  }
354 
355  if((centery-radius) < 0) {
356  sinfo_msg_error("It is not possible to set centery-radius<0.");
357  goto cleanup;
358  }
359 
360  if((centerx+radius) >= clx) {
361  sinfo_msg_error("It is not possible to set centerx+radius >= cube x sixe");
362  goto cleanup;
363  }
364 
365  if((centery+radius) >= cly) {
366  sinfo_msg_error("It is not possible to set centery+radius >= cube y size.");
367  goto cleanup;
368  }
369 
370  if((radius) < 0) {
371  sinfo_msg_error("It is not possible to set radius<0.");
372  goto cleanup;
373  }
374 
375  }
376 
377  if(strcmp(operation,"average") ==0) {
378  if (strcmp(aperture,"rectangle") ==0) {
379  spec = sinfo_new_mean_rectangle_of_cube_spectra(cube,llx,lly,urx,ury);
380  } else if (strcmp(aperture,"circle")==0) {
381  spec = sinfo_new_mean_circle_of_cube_spectra(cube,centerx,
382  centery,radius);
383  } else {
384  sinfo_msg_error("Aperture not supported");
385  sinfo_msg("Supported apertures are only:");
386  sinfo_msg("rectangle, circle:");
387  goto cleanup;
388  }
389  } else if (strcmp(operation,"clean_mean") ==0) {
390  if (strcmp(aperture,"rectangle") == 0) {
391  spec = sinfo_new_clean_mean_rectangle_of_cube_spectra(cube,llx,lly,
392  urx,ury,
393  lo_rej,hi_rej);
394  } else if (strcmp(aperture,"circle")==0) {
395  spec = sinfo_new_clean_mean_circle_of_cube_spectra(cube,centerx,
396  centery,radius,
397  lo_rej,hi_rej);
398  } else {
399  sinfo_msg_error("Aperture not supported");
400  sinfo_msg("Supported apertures are only:");
401  sinfo_msg("rectangle, circle:");
402  goto cleanup;
403  }
404  } else if (strcmp(operation,"median") ==0) {
405  if (strcmp(aperture,"rectangle")==0) {
406  spec = sinfo_new_median_rectangle_of_cube_spectra(cube,llx,lly,
407  urx,ury);
408  } else if (strcmp(aperture,"circle")==0) {
409  spec = sinfo_new_median_circle_of_cube_spectra(cube,centerx,
410  centery,radius);
411  } else {
412  sinfo_msg_error("Aperture not supported");
413  sinfo_msg("Supported apertures are only:");
414  sinfo_msg("rectangle, circle:");
415  goto cleanup;
416  }
417  } else if (strcmp(operation,"sum") ==0) {
418  if (strcmp(aperture,"rectangle")==0) {
419  spec = sinfo_new_sum_rectangle_of_cube_spectra(cube,llx,lly,urx,ury);
420  } else if (strcmp(aperture,"circle")==0) {
421  spec = sinfo_new_sum_circle_of_cube_spectra(cube,centerx,
422  centery,radius);
423  } else {
424  sinfo_msg_error("Aperture not supported");
425  sinfo_msg("Supported apertures are only:");
426  sinfo_msg("rectangle, circle:");
427  goto cleanup;
428  }
429  } else if (strcmp(operation,"extract") == 0) {
430  if (strcmp(aperture,"rectangle")==0) {
431  spec = sinfo_new_median_rectangle_of_cube_spectra(cube,llx,lly,
432  urx,ury);
433  } else if (strcmp(aperture,"circle")==0) {
434  spec = sinfo_new_median_circle_of_cube_spectra(cube,centerx,
435  centery,radius);
436  } else {
437  sinfo_msg_error("Aperture not supported");
438  sinfo_msg("Supported apertures are only:");
439  sinfo_msg("rectangle, circle:");
440  goto cleanup;
441  }
442  } else {
443  sinfo_msg_error("Operation not supported");
444  sinfo_msg("Supported operations are only:");
445  sinfo_msg("average, clean_mean, median, sum, extract :");
446  goto cleanup;
447  }
448  im_spec = sinfo_new_vector_to_image(spec);
449  /* head = sinfo_fits_read_header((char*)name_i); */
450  sinfo_msg("name_i=%s",name_i);
451  ck0_nomsg(sinfo_change_header(name_i));
452 
453  /* HOW TO SAVE A PRODUCT ON DISK */
454  /* Set the file name */
455 
456  /* Create product frame */
457  ck0(sinfo_pro_save_ima(im_spec,framelist,framelist,ima_o,
458  tag_o,NULL,cpl_func,parlist),"failed to save ima");
459 
460 
461  sinfo_new_set_wcs_spectrum (im_spec,ima_o,clam, disp, cpix);
462 
463  sinfo_stectrum_ima2table(im_spec,ima_o,&tbl_spec);
464  ck0(sinfo_pro_save_tbl(tbl_spec,framelist,framelist,tbl_o,
465  tag_o,NULL,cpl_func,parlist),
466  "failed to save spectrum");
467 
468  cleanup:
469  sinfo_free_propertylist(&plist) ;
470  sinfo_free_frame(&product_frame) ;
471  sinfo_free_image(&image) ;
472  sinfo_free_imagelist(&cube);
473  sinfo_free_image(&im_spec);
474  /* This generate seg fault
475  if(spec != NULL) sinfo_free_svector(&spec);
476  */
477  sinfo_free_table(&tbl_spec);
478  /*if(head != NULL) sinfo_fits_header_destroy(head); */
479 
480  /* Return */
481  if (cpl_error_get_code())
482  return -1 ;
483  else
484  return 0 ;
485 
486 
487 }