sinfo_utl_cube2spectrum.c

00001 /* $Id: sinfo_utl_cube2spectrum.c,v 1.11 2012/03/03 10:17:31 amodigli Exp $
00002  *
00003  * This file is part of the IIINSTRUMENT Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2012/03/03 10:17:31 $
00024  * $Revision: 1.11 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 /*----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 
00036 
00037 #include "sinfo_utl_cube2spectrum.h"
00038 #include "sinfo_functions.h"
00039 #include "sinfo_spectrum_ops.h"
00040 #include "sinfo_key_names.h"
00041 #include "sinfo_pro_save.h"
00042 #include "sinfo_utilities_scired.h"
00043 #include "sinfo_globals.h"
00044 #include "sinfo_error.h"
00045 #include "sinfo_utils_wrappers.h"
00046 /*----------------------------------------------------------------------------
00047                             Functions prototypes
00048  ----------------------------------------------------------------------------*/
00049 
00050 /*----------------------------------------------------------------------------
00051                             Static variables
00052  ----------------------------------------------------------------------------*/
00053 
00054 static int
00055 sinfo_change_header(const char * name);
00063 /*-----------------------------------------------------------------------------
00064                             Function implementation
00065  ----------------------------------------------------------------------------*/
00066 
00067 static int
00068 sinfo_change_header(const char * name) {
00069   int cenpix=0;
00070   double cenLambda=0;
00071   double dispersion=0;
00072   cpl_propertylist* plist=NULL;
00073 
00074   plist=cpl_propertylist_load(name,0);
00075   cenpix = sinfo_pfits_get_crpix3(plist);
00076   cenLambda = sinfo_pfits_get_crval3(plist);
00077   dispersion = sinfo_pfits_get_cdelt3(plist);
00078 
00079   
00080   check(cpl_propertylist_set_string(plist,"CTYPE2","WAVE"),"Setting CTYPE2");
00081   check_nomsg(cpl_propertylist_set_comment(plist,"CTYPE2",
00082                        "wavelength axis in microns"));
00083 
00084   check(cpl_propertylist_set_double(plist,"CRPIX2",(double)cenpix),
00085     "Setting CRPIX2");
00086   check_nomsg(cpl_propertylist_set_comment(plist,"CRPIX2","Reference pixel"));
00087  
00088   check(cpl_propertylist_set_double(plist,"CRVAL2",cenLambda),
00089     "Setting CRVAL2");
00090   check_nomsg(cpl_propertylist_set_comment(plist,"CRVAL2",
00091                        "central wavelength"));
00092 
00093   check(cpl_propertylist_set_double(plist,"CDELT2",dispersion),
00094     "Setting CDELT2");
00095   check_nomsg(cpl_propertylist_set_comment(plist,"CDELT2",
00096                        "microns per pixel"));
00097 
00098   check(cpl_propertylist_set_string(plist,"CUNIT2","um"),"Setting CUNIT2"); 
00099   check_nomsg(cpl_propertylist_set_comment(plist,"CUNIT2","spectral unit")); 
00100 
00101   check(cpl_propertylist_set_string(plist,"CTYPE1","ONESPEC"),
00102     "Setting CTYPE1");
00103   check_nomsg(cpl_propertylist_set_comment(plist,"CTYPE1",
00104                        "one spectrum in y-direction"));
00105 
00106   if(cpl_propertylist_has(plist,"CRPIX1")==1) {
00107     check(cpl_propertylist_erase_regexp(plist, "CRPIX1",0),"Erasing CRPIX1");
00108   }
00109 
00110   if(cpl_propertylist_has(plist,"CRVAL1")==1) {
00111     check(cpl_propertylist_erase_regexp(plist, "CRVAL1",0),"Erasing CRVAL1");
00112   }
00113 
00114   if(cpl_propertylist_has(plist,"CDELT1")==1) {
00115     check(cpl_propertylist_erase_regexp(plist, "CDELT1",0),"Erasing CDELT1");
00116   }
00117 
00118 
00119   if(cpl_propertylist_has(plist,"CUNIT1")==1) {
00120     check(cpl_propertylist_erase_regexp(plist, "CUNIT1",0),"Erasing CUNIT1");
00121   }
00122 
00123 
00124   if(cpl_propertylist_has(plist,"CTYPE3")==1) {
00125     check(cpl_propertylist_erase_regexp(plist, "CTYPE3",0),"Erasing CTYPE3");
00126   }
00127 
00128 
00129   if(cpl_propertylist_has(plist,"CRPIX3")==1) {
00130     check(cpl_propertylist_erase_regexp(plist, "CRPIX3",0),"Erasing CRPIX3");
00131   }
00132 
00133   if(cpl_propertylist_has(plist,"CRVAL3")==1) {
00134     check(cpl_propertylist_erase_regexp(plist, "CRVAL3",0),"Erasing CRVAL3");
00135   }
00136 
00137   if(cpl_propertylist_has(plist,"CDELT3")==1) {
00138     check(cpl_propertylist_erase_regexp(plist, "CDELT3",0),"Erasing CDELT3");
00139   }
00140 
00141   if(cpl_propertylist_has(plist,"CUNIT3")==1) {
00142     check(cpl_propertylist_erase_regexp(plist, "CUNIT3",0),"Erasing CUNIT3");
00143   }
00144 
00145 
00146   if(cpl_propertylist_has(plist,"CD1_1")==1) {
00147     check(cpl_propertylist_erase_regexp(plist, "CD1_1",0),"Erasing CD1_1");
00148   }
00149 
00150 
00151   if(cpl_propertylist_has(plist,"CD1_2")==1) {
00152     check(cpl_propertylist_erase_regexp(plist, "CD1_2",0),"Erasing CD1_2");
00153   }
00154 
00155   if(cpl_propertylist_has(plist,"CD2_1")==1) {
00156     check(cpl_propertylist_erase_regexp(plist, "CD2_1",0),"Erasing CD2_1");
00157   }
00158 
00159   if(cpl_propertylist_has(plist,"CD2_2")==1) {
00160     check(cpl_propertylist_erase_regexp(plist, "CD2_2",0),"Erasing CD2_2");
00161   }
00162 
00163  cleanup:
00164   sinfo_free_propertylist(&plist);
00165 
00166    /* Return */
00167     if (cpl_error_get_code()) 
00168         return -1 ;
00169     else 
00170         return 0 ;
00171 
00172 }
00173 
00174 /*---------------------------------------------------------------------------*/
00182 /*---------------------------------------------------------------------------*/
00183 int sinfo_utl_cube2spectrum(
00184         cpl_parameterlist   *   parlist, 
00185         cpl_frameset        *   framelist,
00186          const char* tag)
00187 {
00188     cpl_parameter       *   param =NULL;
00189     const char          *   operation =NULL;
00190     const char          *   aperture =NULL;
00191     char  name_i [MAX_NAME_SIZE];
00192     int                    llx=0;
00193     int                    urx=0;
00194     int                    lly=0;
00195     int                    ury=0;
00196     int                    lo_rej=0;
00197     int                    hi_rej=0;
00198     int                    centerx=0;
00199     int                    centery=0;
00200     int                    radius=0;
00201     int                    clx=0;
00202     int                    cly=0;
00203     
00204     double                 disp=0;
00205     double                 cpix=0;
00206     double                 clam=0;
00207 
00208     cpl_frame           *   frm_cub=NULL;
00209 
00210     char ima_o[MAX_NAME_SIZE];
00211     char tbl_o[MAX_NAME_SIZE];
00212     char tag_i[MAX_NAME_SIZE];
00213     char tag_o[MAX_NAME_SIZE];
00214     cpl_propertylist    *   plist =NULL;
00215     cpl_image           *   image =NULL;
00216     cpl_frame           *   product_frame=NULL;
00217 
00218     cpl_image * im_spec=NULL;
00219     cpl_table * tbl_spec=NULL;
00220     cpl_image * img=NULL;
00221     cpl_imagelist  * cube=NULL;
00222     Vector * spec=NULL;
00223     /*fits_header* head=NULL; */
00224 
00225      /* HOW TO RETRIEVE INPUT PARAMETERS */
00226     /* --stropt */
00227     check_nomsg(param=cpl_parameterlist_find(parlist, 
00228                                          "sinfoni.sinfo_utl_cube2spectrum.op"));
00229     check_nomsg(operation=cpl_parameter_get_string(param));
00230  
00231     /* --stropt */
00232     check_nomsg(param=cpl_parameterlist_find(parlist, 
00233                                          "sinfoni.sinfo_utl_cube2spectrum.ap"));
00234     check_nomsg(aperture=cpl_parameter_get_string(param));
00235      /* --stropt */
00236 
00237  
00238     if (tag == NULL) {
00239       strcpy(ima_o,"out_spec_ima.fits");
00240       strcpy(tbl_o,"out_spec_tbl.fits");
00241       strcpy(tag_i,SI_UTL_CUBE2SPECTRUM_CUBE);
00242       strcpy(tag_o,SI_UTL_CUBE2SPECTRUM_PROIMA);
00243     } else {
00244       snprintf(ima_o,MAX_NAME_SIZE-1,"%s%s",tag,"_spec_ima.fits");
00245       snprintf(tbl_o,MAX_NAME_SIZE-1,"%s%s",tag,"_spec_tbl.fits");
00246       snprintf(tag_o,MAX_NAME_SIZE-1,"%s%s",tag,"_SPCT");
00247       strcpy(tag_i,  tag);
00248     }
00249  
00250 
00251     /* --intopt */
00252     check_nomsg(param=cpl_parameterlist_find(parlist,
00253                                     "sinfoni.sinfo_utl_cube2spectrum.llx"));
00254     check_nomsg(llx=cpl_parameter_get_int(param));
00255 
00256     /* --intopt */
00257     check_nomsg(param=cpl_parameterlist_find(parlist,
00258                                     "sinfoni.sinfo_utl_cube2spectrum.lly"));
00259     check_nomsg(lly=cpl_parameter_get_int(param));
00260 
00261     /* --intopt */
00262     check_nomsg(param=cpl_parameterlist_find(parlist,
00263                                     "sinfoni.sinfo_utl_cube2spectrum.urx"));
00264     check_nomsg(urx=cpl_parameter_get_int(param));
00265 
00266     /* --intopt */
00267     check_nomsg(param=cpl_parameterlist_find(parlist,
00268                                     "sinfoni.sinfo_utl_cube2spectrum.ury"));
00269     check_nomsg(ury=cpl_parameter_get_int(param));
00270 
00271     /* --intopt */
00272     check_nomsg(param=cpl_parameterlist_find(parlist,
00273                                   "sinfoni.sinfo_utl_cube2spectrum.lo_rej"));
00274     check_nomsg(lo_rej=cpl_parameter_get_int(param));
00275 
00276     /* --intopt */
00277     check_nomsg(param=cpl_parameterlist_find(parlist,
00278                                   "sinfoni.sinfo_utl_cube2spectrum.hi_rej"));
00279     check_nomsg(hi_rej=cpl_parameter_get_int(param));
00280 
00281     /* --intopt */
00282     check_nomsg(param=cpl_parameterlist_find(parlist,
00283                                   "sinfoni.sinfo_utl_cube2spectrum.centerx"));
00284     check_nomsg(centerx=cpl_parameter_get_int(param));
00285 
00286     /* --intopt */
00287     check_nomsg(param=cpl_parameterlist_find(parlist,
00288                                   "sinfoni.sinfo_utl_cube2spectrum.centery"));
00289     check_nomsg(centery=cpl_parameter_get_int(param));
00290 
00291     /* --intopt */
00292     check_nomsg(param=cpl_parameterlist_find(parlist,
00293                                   "sinfoni.sinfo_utl_cube2spectrum.radius"));
00294     check_nomsg(radius=cpl_parameter_get_int(param));
00295   
00296     /* Identify the RAW and CALIB frames in the input frameset */
00297     if (sinfo_dfs_set_groups(framelist)) {
00298         sinfo_msg_error( "Cannot identify RAW and CALIB frames") ;
00299         goto cleanup;
00300     }
00301 
00302     /* HOW TO ACCESS INPUT DATA */
00303     cknull(frm_cub=cpl_frameset_find(framelist,tag_i),
00304       "SOF does not have a file tagged as %s",tag_i);
00305    
00306     /* Now performing the data reduction */
00307     /* Let's generate one image for the example */
00308     check_nomsg(strcpy(name_i,cpl_frame_get_filename(frm_cub)));
00309     check_nomsg(cube = cpl_imagelist_load((char*)name_i,CPL_TYPE_FLOAT,0));
00310 
00311     check_nomsg(img=cpl_imagelist_get(cube,0));
00312     check_nomsg(clx=cpl_image_get_size_x(img));
00313     check_nomsg(cly=cpl_image_get_size_y(img));
00314     check(plist=cpl_propertylist_load(name_i,0),
00315       "Cannot read the FITS header") ;
00316 
00317     cpix = (double) sinfo_pfits_get_crpix3(plist);
00318     clam = (double) sinfo_pfits_get_crval3(plist);
00319     disp = (double) sinfo_pfits_get_cdelt3(plist);
00320     sinfo_free_propertylist(&plist);
00321     if(strcmp(aperture,"rectangle") ==0) {
00322       if (llx<0) {
00323         sinfo_msg_warning("llx=%d too low set it to 0",llx);
00324         llx=0;
00325       }   
00326       if (lly<0) {
00327         sinfo_msg_warning("lly=%d too low set it to 0",lly);
00328         lly=0;
00329       }   
00330       if (urx>clx-1) {
00331         sinfo_msg_warning("urx=%d too large set it to %d",urx,clx-1);
00332         urx=clx-1;
00333       }   
00334       if (ury>cly-1) {
00335         sinfo_msg_warning("ury=%d too large set it to %d",ury,cly-1);
00336         ury=cly-1;
00337       }   
00338       if(llx>=urx) {
00339         sinfo_msg_error("llx>=urx!");
00340         goto cleanup;
00341       }
00342       if(lly>=ury) {
00343         sinfo_msg_error("lly>=ury!");
00344         goto cleanup;
00345       }
00346 
00347     } 
00348 
00349     if(strcmp(aperture,"circle") ==0) {
00350       if((centerx-radius) < 0) {
00351     sinfo_msg_error("It is not possible to set centerx-radius<0.");
00352         goto cleanup;
00353       }
00354 
00355       if((centery-radius) < 0) {
00356     sinfo_msg_error("It is not possible to set centery-radius<0.");
00357         goto cleanup;
00358       }
00359 
00360       if((centerx+radius) >= clx) {
00361     sinfo_msg_error("It is not possible to set centerx+radius >= cube x sixe");
00362         goto cleanup;
00363       }
00364 
00365       if((centery+radius) >= cly) {
00366     sinfo_msg_error("It is not possible to set centery+radius >= cube y size.");
00367         goto cleanup;
00368       }
00369 
00370       if((radius) < 0) {
00371     sinfo_msg_error("It is not possible to set radius<0.");
00372         goto cleanup;
00373       }
00374 
00375     }
00376 
00377     if(strcmp(operation,"average") ==0) {
00378        if (strcmp(aperture,"rectangle") ==0) {
00379           spec = sinfo_new_mean_rectangle_of_cube_spectra(cube,llx,lly,urx,ury);
00380        } else if (strcmp(aperture,"circle")==0) {
00381           spec = sinfo_new_mean_circle_of_cube_spectra(cube,centerx,
00382                                                        centery,radius);
00383        } else {
00384           sinfo_msg_error("Aperture not supported");
00385           sinfo_msg("Supported apertures are only:");
00386           sinfo_msg("rectangle, circle:");
00387         goto cleanup;
00388        }
00389     } else if (strcmp(operation,"clean_mean") ==0) {
00390       if (strcmp(aperture,"rectangle") == 0) {
00391           spec = sinfo_new_clean_mean_rectangle_of_cube_spectra(cube,llx,lly,
00392                                                                 urx,ury,
00393                                                                 lo_rej,hi_rej);
00394       } else if (strcmp(aperture,"circle")==0) {
00395           spec = sinfo_new_clean_mean_circle_of_cube_spectra(cube,centerx,
00396                                                              centery,radius,
00397                                                              lo_rej,hi_rej); 
00398       } else {
00399           sinfo_msg_error("Aperture not supported");
00400           sinfo_msg("Supported apertures are only:");
00401           sinfo_msg("rectangle, circle:");
00402           goto cleanup;
00403       }
00404     } else if (strcmp(operation,"median") ==0) {
00405        if (strcmp(aperture,"rectangle")==0) {
00406           spec = sinfo_new_median_rectangle_of_cube_spectra(cube,llx,lly,
00407                                                             urx,ury);
00408        } else if (strcmp(aperture,"circle")==0) {
00409           spec = sinfo_new_median_circle_of_cube_spectra(cube,centerx,
00410                                                          centery,radius);
00411        } else {
00412           sinfo_msg_error("Aperture not supported");
00413           sinfo_msg("Supported apertures are only:");
00414           sinfo_msg("rectangle, circle:");
00415           goto cleanup;
00416        }
00417     } else if (strcmp(operation,"sum") ==0) {
00418        if (strcmp(aperture,"rectangle")==0) {
00419           spec = sinfo_new_sum_rectangle_of_cube_spectra(cube,llx,lly,urx,ury);
00420        } else if (strcmp(aperture,"circle")==0) {
00421           spec = sinfo_new_sum_circle_of_cube_spectra(cube,centerx,
00422                                                       centery,radius);
00423        } else {
00424           sinfo_msg_error("Aperture not supported");
00425           sinfo_msg("Supported apertures are only:");
00426           sinfo_msg("rectangle, circle:");
00427           goto cleanup;
00428        }
00429     } else if (strcmp(operation,"extract") == 0) {
00430        if (strcmp(aperture,"rectangle")==0) {
00431       spec = sinfo_new_median_rectangle_of_cube_spectra(cube,llx,lly,
00432                                                             urx,ury); 
00433        } else if (strcmp(aperture,"circle")==0) {
00434      spec = sinfo_new_median_circle_of_cube_spectra(cube,centerx,
00435                                                         centery,radius);
00436        } else {
00437           sinfo_msg_error("Aperture not supported");
00438           sinfo_msg("Supported apertures are only:");
00439           sinfo_msg("rectangle, circle:");
00440           goto cleanup;
00441        }
00442     } else {
00443       sinfo_msg_error("Operation not supported");
00444       sinfo_msg("Supported operations are only:");
00445       sinfo_msg("average, clean_mean, median, sum, extract :");
00446       goto cleanup;
00447     }
00448     im_spec = sinfo_new_vector_to_image(spec);
00449     /* head = sinfo_fits_read_header((char*)name_i); */
00450     sinfo_msg("name_i=%s",name_i);
00451     ck0_nomsg(sinfo_change_header(name_i));
00452 
00453     /* HOW TO SAVE A PRODUCT ON DISK  */
00454     /* Set the file name */
00455  
00456     /* Create product frame */
00457     ck0(sinfo_pro_save_ima(im_spec,framelist,framelist,ima_o,
00458                tag_o,NULL,cpl_func,parlist),"failed to save ima");
00459 
00460 
00461     sinfo_new_set_wcs_spectrum (im_spec,ima_o,clam, disp, cpix);
00462 
00463     sinfo_stectrum_ima2table(im_spec,ima_o,&tbl_spec);
00464      ck0(sinfo_pro_save_tbl(tbl_spec,framelist,framelist,tbl_o,
00465                tag_o,NULL,cpl_func,parlist),
00466                            "failed to save spectrum");
00467 
00468  cleanup:
00469     sinfo_free_propertylist(&plist) ;
00470     sinfo_free_frame(&product_frame) ;
00471     sinfo_free_image(&image) ;
00472     sinfo_free_imagelist(&cube);
00473     sinfo_free_image(&im_spec);
00474     /* This generate seg fault
00475     if(spec != NULL) sinfo_free_svector(&spec);
00476     */
00477     sinfo_free_table(&tbl_spec);
00478     /*if(head != NULL) sinfo_fits_header_destroy(head); */
00479  
00480     /* Return */
00481     if (cpl_error_get_code()) 
00482         return -1 ;
00483     else 
00484         return 0 ;
00485 
00486 
00487 }

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1