lamp_spec.c

00001 /*----------------------------------------------------------------------------
00002  
00003      File name    :     lamp_spec.c
00004    Author       :   A. Modigliani
00005    Created on   :   Sep 29, 2003
00006    Description  : 
00007 
00008  This does the resampling of an offset-corrected, bad pixel corrected and
00009  eventually interleaved frame of an halogen lamp exposure. 
00010  Additionally, an intensity calibration is carried through by using a 
00011  counts to intensity value measured before and given as input.
00012 
00013  Then one halogen lamp spectrum is extracted by taking a clean mean 
00014  along the spatial direction by omitting the bad pixel positions.
00015 
00016 
00017  ---------------------------------------------------------------------------*/
00018 
00019 /*----------------------------------------------------------------------------
00020                                 Includes
00021  ---------------------------------------------------------------------------*/
00022 #include "lamp_spec.h"
00023 #include "sinfoni_pro_save.h"
00024 #include "lamp_ini_by_cpl.h"
00025 /*----------------------------------------------------------------------------
00026                                 Defines
00027  ---------------------------------------------------------------------------*/
00028 
00029 
00030 /*----------------------------------------------------------------------------
00031                              Function Definitions
00032  ---------------------------------------------------------------------------*/
00033 
00034 
00035 
00036 
00037 void
00038 change_plist_lamp_spec(cpl_propertylist * plist, char* outName, float cenLambda, 
00039                          float dispersion, int cenpix){
00040 
00041     char blank[FILENAMESZ] ;
00042     char firsttext[2*FILENAMESZ] ;
00043     char * date ;
00044 
00045     cpl_propertylist_insert_after_string(plist, "EXPTIME", "CTYPE2", "WAVE" ) ;
00046     cpl_propertylist_set_comment(plist, "WAVE", "wavelength axis in microns" ) ;
00047 
00048     sprintf(blank, "%d", cenpix+1) ;
00049     cpl_propertylist_insert_after_string(plist, "EXPTIME", "CTYPE2", "CRPIX2" ) ;
00050     cpl_propertylist_set_comment(plist, "CRPIX2", "Reference pixel in z" ) ;
00051 
00052     sprintf(blank, "%f", cenLambda) ;
00053     cpl_propertylist_insert_after_string(plist, "EXPTIME", "CRPIX2", "CRVAL2" ) ;
00054     cpl_propertylist_set_comment(plist, "CRVAL2", "central wavelength" ) ;
00055 
00056     sprintf(blank, "%f", dispersion) ;
00057     cpl_propertylist_insert_after_string(plist, "EXPTIME", "CRVAL2", "CDELT2" ) ;
00058     cpl_propertylist_set_comment(plist, "CDELT2", "microns per pixel" ) ;
00059 
00060     cpl_propertylist_insert_after_string(plist, "CDELT2", "CUNIT2", "MICRON" ) ;
00061     cpl_propertylist_set_comment(plist, "MICRON", "spectral unit" ) ;
00062   
00063     date     = get_datetime_iso8601() ;
00064     strcat(firsttext,outName);
00065 
00066     if (strlen(firsttext) > 30) {
00067        /* filename = "lampspec" + " -f " + ini_name[len(ini_name)-10:len(ini_name)] */
00068         cpl_propertylist_set_string(plist,KEY_NAME_PIPEFILE,  outName ) ;
00069        cpl_propertylist_set_string(plist,KEY_NAME_HPRO_TYPE,  "REDUCED" ) ;
00070        cpl_propertylist_set_string(plist,KEY_NAME_HPRO_CATG,  "MASTER_LAMPSPEC" ) ;
00071        cpl_propertylist_set_string(plist,KEY_NAME_HPRO_STATUS, "OK" ) ;
00072        cpl_propertylist_set_string(plist,KEY_NAME_HPRO_DATE, date ) ;
00073        cpl_propertylist_set_string(plist,KEY_NAME_HPRO_RECID, firsttext ) ;
00074        cpl_propertylist_set_string(plist,KEY_NAME_HPRO_DRSID, PACKAGE_VERSION ) ;  
00075 
00076  
00077     }
00078 }
00079 
00080 /*----------------------------------------------------------------------------
00081    Function     :       lamp_flats()
00082    In           :       ini_file: file name of according .ini file
00083    Out          :       integer (0 if it worked, -1 if it doesn't) 
00084    Job          : 
00085 
00086 This does the resampling of an offset-corrected, bad pixel corrected and
00087  eventually interleaved frame of an halogen lamp exposure. 
00088  Additionally, an intensity calibration is carried through by using a 
00089  counts to intensity value measured before and given as input.
00090 
00091  Then one halogen lamp spectrum is extracted by taking a clean mean 
00092  along the spatial direction by omitting the bad pixel positions.
00093 
00094  ---------------------------------------------------------------------------*/
00095 
00096 
00097 int lamp_spec (cpl_parameterlist* config, cpl_frameset* sof)
00098 {
00099     const char* _id = "lamp_spec";
00100     lamp_config * cfg ;
00101     fits_header * list_head ;
00102     OneImage * spectrum ;
00103     OneImage * calspec ;
00104     OneImage * im ;
00105     OneImage * wavemap ;
00106     OneImage * resampledImage ;
00107     int cenpix=0;
00108 
00109     double disp=0;
00110     float min=0;
00111     float max=0;
00112     double centralLam=0.;
00113 
00114     double* centralLambda;
00115     int* centralpix;
00116 
00117 
00118     double* dis;
00119     float* mi;
00120     float* ma;
00121     cpl_image* calspec_img=NULL;
00122     cpl_frameset* raw=NULL;
00123     cpl_propertylist* cplist=NULL;
00124 
00125     /* 
00126        -----------------------------------------------------------------
00127        1) parse the file names and parameters to the tilt_config data 
00128           structure cfg 
00129        -----------------------------------------------------------------
00130      */
00131     cpl_msg_info(_id,"Parsing CPL input");
00132     cfg = parse_cpl_input_lamp(config,sof,&raw) ;
00133     if (cfg == NULL)
00134     {
00135         cpl_msg_error (_id,"could not parse cpl input!") ;
00136         return -1 ;
00137     }
00138 
00139     if(is_fits_file(cfg->inFrame) != 1 ) {
00140       cpl_msg_error(_id,"Input file %s is not FITS",cfg->inFrame);
00141       return -1;
00142     }
00143 
00144     if(is_fits_file(cfg->wavemapim) !=1 ) {
00145       cpl_msg_error(_id,"Input file %s is not FITS",cfg->wavemapim);
00146       return -1;
00147     }
00148 
00149     im = load_image(cfg->inFrame);
00150     if (im == NULL) {
00151       cpl_msg_error(_id,"could not load inFrame" );
00152       return -1;
00153     }
00154     list_head = fits_read_header(cfg->inFrame);
00155     wavemap = load_image(cfg->wavemapim);
00156     if (wavemap == NULL) {
00157       cpl_msg_error(_id,"could not load wavemapim" );
00158       return -1;
00159     }
00160 
00161 
00162     /*
00163 #-----------------------------------------------------------------------
00164 #---------------------------RESAMPLING----------------------------------
00165 #-----------------------------------------------------------------------
00166     */
00167     cpl_msg_info(_id,"Resampling");
00168     dis = new_double_array(1);
00169     mi = new_float_array(1);
00170     ma = new_float_array(1);
00171     centralLambda = new_double_array(1);
00172     centralpix = new_int_array(1);
00173 
00174     doublearray_set_value(dis, 0., 0);
00175     array_set_value(mi, 0., 0);
00176     array_set_value(ma, 0., 0);
00177     doublearray_set_value(centralLambda, 0., 0);
00178     intarray_set_value(centralpix, 0, 0);
00179     resampledImage = definedResampling( im, 
00180                                         wavemap, 
00181                                         cfg->ncoeffs, 
00182                                         &cfg->nrows, 
00183                                         dis, 
00184                                         mi, 
00185                                         ma, 
00186                         centralLambda, 
00187                                         centralpix );
00188 
00189  if (resampledImage == NULL) {
00190    cpl_msg_error(_id, "definedResampling failed" );
00191    return -1;
00192  }
00193  disp = doublearray_get_value(dis, 0);
00194  min = array_get_value(mi, 0);
00195  max = array_get_value(ma, 0);
00196  centralLam = doublearray_get_value(centralLambda, 0);
00197  cenpix = intarray_get_value(centralpix, 0);
00198 
00199  /*
00200  cpl_msg_info("dispersion %f ", disp);
00201  cpl_msg_info("minimal lambda %f ", min);
00202  cpl_msg_info("maximal lambda %f ", max);
00203  cpl_msg_info("central lambda %f ", centralLam);
00204  cpl_msg_info("central pixel %f ", cenpix);
00205  */
00206  destroy_doublearray(dis);
00207  destroy_array(mi);
00208  destroy_array(ma);
00209  destroy_doublearray(centralLambda);
00210  destroy_intarray(centralpix);
00211  destroy_image(im);
00212  destroy_image(wavemap);
00213 
00214 
00215  /*
00216 #------------------------------------------------------------------------
00217 #-----------------EXTRACTSPECTRUM----------------------------------------
00218 #------------------------------------------------------------------------
00219  */
00220  cpl_msg_info(_id,"Extract spectrum");
00221 spectrum = extractSpectrumFromResampledFlat(resampledImage, 
00222                                             cfg->loReject, 
00223                                             cfg->hiReject);
00224 
00225  if (spectrum == NULL) {
00226    cpl_msg_error(_id, "extractSpectrum.. failed" );
00227    return -1;
00228  }
00229 
00230  calspec = cst_op_image(spectrum, cfg->countsToIntensity, '/');
00231  if (calspec == NULL) {
00232    cpl_msg_error(_id, "cst_op_image failed" );
00233    return -1;
00234  }
00235 
00236 
00237 
00238 
00239  
00240  calspec_img=cpl_image_wrap_float(calspec->lx,calspec->ly,calspec->data);
00241 
00242 
00243 
00244     if(-1 == sinfoni_pro_dump_ima(calspec_img,raw,sof,cfg->outName,
00245             PRO_MASTER_LAMP_SPEC,_id,config)) {
00246       cpl_msg_error(_id,"cannot dump ima %s", cfg->outName);
00247       fits_header_destroy(list_head);
00248       destroy_image(spectrum);
00249       destroy_image(calspec);
00250       destroy_image(resampledImage);
00251       lamp_free(cfg);
00252       return -1;
00253     }
00254 
00255 
00256  cplist = cpl_propertylist_new() ;
00257  if ((cpl_error_code)((cplist = cpl_propertylist_load(cfg->outName, 0)) == NULL)) {
00258                cpl_msg_error(_id, "getting header from frame %s",cfg->outName);
00259                cpl_propertylist_delete(cplist) ;
00260                return -1 ;
00261  }
00262 
00263 
00264  change_plist_lamp_spec(cplist, cfg->outName, centralLam, disp, cenpix);
00265 
00266  if (cpl_image_save(calspec_img, cfg->outName, CPL_BPP_DEFAULT, cplist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00267             cpl_msg_error(_id, "Cannot save the product %s",cfg->outName);
00268             cpl_propertylist_delete(cplist) ;
00269             return -1 ;
00270  }
00271 
00272  cpl_propertylist_delete(cplist) ;
00273 
00274 
00275  fits_header_destroy(list_head);
00276  destroy_image(spectrum);
00277  destroy_image(calspec);
00278  destroy_image(resampledImage);
00279  lamp_free(cfg);
00280  return 0;
00281 
00282 
00283 }
00284 
00285 

Generated on Wed Oct 26 13:08:52 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001