si_rec_stdstar.c

00001 /* $Id: si_rec_stdstar.c,v 1.33 2005/10/08 10:34:50 amodigli Exp $
00002  *
00003  * This file is part of the CPL (Common Pipeline Library)
00004  * Copyright (C) 2002 European Southern Observatory
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library 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 GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2005/10/08 10:34:50 $
00023  * $Revision: 1.33 $
00024  * $Name:  $
00025  */
00026 
00027  /****************************************************************
00028   *          Standard_Star Frames Data Reduction                          *
00029   ****************************************************************/
00030 
00031 /*-----------------------------------------------------------------------------
00032                                 Includes
00033  -----------------------------------------------------------------------------*/
00034 
00035 #ifdef HAVE_CONFIG_H
00036 #include <config.h>          /* allows the program compilation */
00037 #endif
00038 
00039 /* std */
00040 #include <strings.h>
00041 #include <string.h>
00042 #include <stdio.h>
00043 
00044 /* cx */
00045 #include <cxmacros.h>
00046 #include <cxtypes.h>
00047 #include <cxmessages.h>    
00048 
00049 /* qfits */
00050 #include <xmemory.h>
00051 
00052 /* cpl */
00053 #include <cpl.h>       /* defines memory allocation functions */
00054 
00055 /* sinfoni */
00056 #include <sinfoni_pro_types.h>
00057 #include <sinfoni_prepare_stacked_frames_config.h>
00058 #include <sinfoni_objnod_config.h>
00059 #include <sinfoni_standard_star_config.h>
00060 #include <sinfoni_functions.h>
00061 #include <prepare_stacked_frames.h>
00062 #include <objnod.h>
00063 #include <objnod_rel.h>
00064 #include <stdstar.h>
00065 #include <sinfoni_tpl_utils.h>
00066 #include <sinfoni_tpl_dfs.h>
00067 #include <sinfoni_hidden.h>
00068 #include <sinfoni_globals.h>
00069 #include <sinfoni_memory.h>
00070 
00071 
00072 
00073 
00074 /*-----------------------------------------------------------------------------
00075                             Functions prototypes
00076  -----------------------------------------------------------------------------*/
00077 
00078 static cxint si_rec_stdstar(cpl_parameterlist *, cpl_frameset *);
00079 
00080 /*-----------------------------------------------------------------------------
00081                             Static variables
00082  -----------------------------------------------------------------------------*/
00083 
00084 static char si_rec_stdstar_description1[] =
00085 "This recipe performs science data reduction.\n"
00086 "The input files are science object and sky frames with tags STD_NODDING and SKY_STD_NODDING\n";
00087 
00088 
00089 static char si_rec_stdstar_description2[] =
00090 "Master calibration frames:\n"
00091 "A corresponding (band,preoptics) wavelength map image with tag WAVE_MAP\n"
00092 "A corresponding (band,preoptics) master flat field with tag MASTER_FLAT_LAMP\n"
00093 "A corresponding (band,preoptics) master bad pixel map with tag MASTER_BP_MAP\n"
00094 "A corresponding (band,preoptics) slitlets position frame with tag SLIT_POS\n"
00095 "A corresponding (band) distortion table with tag DISTORTION\n"
00096 "A corresponding (band) slitlet distance table with tag SLITLETS_DISTANCE\n";
00097 
00098 
00099 static char si_rec_stdstar_description3[] =
00100 "The output is an image resulting from the IMA1 op IMA2 where op indicates\n"
00101 "A reference table with the position of the first  column with tag FIRST_COLUMN\n"
00102 "Relevant outputs are:\n"
00103 "combined cubes (PRO.CATG=x_OBS x=STD,OBJ,PSF)\n"
00104 "reconstructed cube (PRO.CATG=COADD_x_OBS x=STD,OBJ,PSF)\n";
00105 
00106 
00107 static char si_rec_stdstar_description4[] =
00108 "An average along Z of the reconstructed cube (PRO.CATG=MED_x_OBS x=STD,OBJ,PSF)\n"
00109 "The bad pixel map associated to the cube (PRO.CATG=BP_MAP_COADD_x_OBS x=STD,OBJ,PSF)\n"
00110 "The std star spectrum image (PRO.CATG=STD_SPECTRUM)\n"
00111 "The std start spectrum and efficiency spectrum table (PRO.CATG=STD_SPECTRA)\n"
00112 "Information on relevant parameters can be found with\n"
00113 "esorex --params si_rec_stdstar\n"
00114 "esorex --help si_rec_stdstar\n"
00115 "\n";
00116 
00117 
00118 static char si_rec_stdstar_description[1300];
00119 
00120 /*-----------------------------------------------------------------------------
00121                                 Functions code
00122  -----------------------------------------------------------------------------*/
00123 
00124  /*
00125   *
00126   * Create the recipe instance, i.e. setup the parameter list for this
00127   * recipe and make it available to the application using the interface.
00128   *
00129   */
00130 
00131 
00132 cxint
00133 si_rec_stdstar_create(cpl_plugin *plugin)
00134 {
00135 
00136   /*
00137    * We have to provide the option we accept to the application.
00138    * We need to setup our parameter list and hook it into the recipe
00139    * interface.
00140    */
00141 
00142   cpl_recipe *recipe = (cpl_recipe *)plugin;
00143   recipe->parameters = cpl_parameterlist_new();
00144   if(recipe->parameters == NULL) {
00145     return 1;
00146   }
00147 
00148   /*
00149    * Fill the parameter list.
00150    */
00151   sinfoni_prepare_stacked_frames_config_add(recipe->parameters);
00152   sinfoni_objnod_config_add(recipe->parameters); 
00153   sinfoni_standard_star_config_add(recipe->parameters);
00154   
00155   return 0;
00156 
00157 }
00158 
00159 cxint
00160 si_rec_stdstar_exec(cpl_plugin *plugin)
00161 {
00162 
00163   cpl_recipe *recipe = (cpl_recipe *) plugin;
00164   if(recipe->parameters == NULL) {
00165     return 1;
00166   }
00167   if(recipe->frames == NULL) {
00168     return 1;
00169   }
00170   /* cx_print("exec\n"); */
00171   cpl_error_reset();
00172   return si_rec_stdstar(recipe->parameters, recipe->frames);
00173 
00174 }
00175 
00176 cxint
00177 si_rec_stdstar_destroy(cpl_plugin *plugin)
00178 {
00179 
00180   cpl_recipe *recipe = (cpl_recipe *) plugin;
00181   /*
00182    * We just destroy what was created during the plugin initializzation phase
00183    * i.e. the parameter list. The frame set is managed by the application which
00184    * called us, so that we must not touch it.
00185    */
00186   cpl_parameterlist_delete(recipe->parameters);
00187   return 0;
00188 
00189 }
00190 
00191 int
00192 cpl_plugin_get_info(cpl_pluginlist *list)
00193 {
00194 
00195   cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00196   cpl_plugin *plugin = &recipe->interface;
00197 
00198   strcpy(si_rec_stdstar_description,si_rec_stdstar_description1);
00199   strcat(si_rec_stdstar_description,si_rec_stdstar_description2);
00200   strcat(si_rec_stdstar_description,si_rec_stdstar_description3);
00201   strcat(si_rec_stdstar_description,si_rec_stdstar_description4);
00202 
00203 
00204   cpl_plugin_init(plugin,
00205           CPL_PLUGIN_API,
00206           SINFONI_BINARY_VERSION,
00207           CPL_PLUGIN_TYPE_RECIPE,
00208           "si_rec_stdstar",
00209           "Standard star data reduction",
00210           si_rec_stdstar_description,
00211           "Andrea Modigliani",
00212           "Andrea.Modigliani@eso.org",
00213           sinfoni_get_license(),
00214           si_rec_stdstar_create,
00215           si_rec_stdstar_exec,
00216           si_rec_stdstar_destroy);
00217 
00218   cpl_pluginlist_append(list, plugin);
00219 
00220   return 0;
00221 
00222 }
00223 
00224 /*
00225  * The actual recipe actually start here.
00226  */
00227 
00228 static cxint
00229 si_rec_stdstar(cpl_parameterlist *config, cpl_frameset *set)
00230 {
00231  
00232 
00233   const char *_id = "si_rec_stdstar";
00234   char filename[FILE_NAME_SZ];
00235   char out_name[FILE_NAME_SZ];
00236 
00237   int i=0;
00238   int j=0;
00239   int k=0;
00240   int ind=0;
00241   int nsky=0;
00242   int nobj=0;
00243   int ncdb=0;
00244   int nwrk=0;
00245   int nstk=0;
00246   int ntot=0;
00247 
00248 
00249 
00250   double mjd_obj=0;
00251   double mjd_sky=0;
00252   double mjd_sky_frm=0;
00253 
00254   cpl_frameset  * obj_set=NULL;
00255   cpl_frameset  * sky_set=NULL;
00256   cpl_frameset  * cdb_set=NULL;
00257   cpl_frameset  * wrk_set=NULL;
00258   cpl_frameset  * stk_set=NULL;
00259   cpl_frameset  * tot_set=NULL;
00260 
00261   cpl_frame     * dup_frm=NULL; 
00262   cpl_frame     * obj_frm=NULL; 
00263   cpl_frame     * sky_frm=NULL;
00264   cpl_frame     * cdb_frm=NULL;
00265   cpl_frame     * wrk_frm=NULL;
00266   cpl_parameter * p=NULL;
00267  
00268   cpl_frame     * tmp_sky_frm=NULL;
00269   fake* fk=fake_new();
00270   /* hidden parameters */
00271 
00272 
00273    if(sinfoni_dfs_set_groups(set)) {
00274        cpl_msg_error(_id, "Cannot indentify RAW and CALIB frames") ;
00275        fake_delete(fk);
00276        return -1;
00277     }
00278 
00279      /* =============================================================== 
00280         Defines several framesets each with a pair obj-sky 
00281         =============================================================== */
00282   obj_set=cpl_frameset_new();
00283   sky_set=cpl_frameset_new();
00284   cdb_set=cpl_frameset_new();
00285 
00286   sinfoni_extract_obj_frames(set,obj_set);
00287   sinfoni_extract_sky_frames(set,sky_set);
00288   sinfoni_extract_mst_frames(set,cdb_set);
00289 
00290   nobj=cpl_frameset_get_size(obj_set);
00291   nsky=cpl_frameset_get_size(sky_set);
00292   ncdb=cpl_frameset_get_size(cdb_set);
00293 
00294 
00295   if ((nobj==0) && (nsky==0)) {
00296     cpl_msg_error(_id,"Empty input set");
00297     cpl_frameset_delete(obj_set);
00298     cpl_frameset_delete(sky_set);
00299     cpl_frameset_delete(cdb_set);
00300     fake_delete(fk);
00301     return -1;
00302   }
00303 
00304 
00305   if ( (nobj != 0) && (nsky != 0) ) {
00306     /* We have either OBJ and SKY: we need to create OBJ-SKY pairs */
00307     cpl_msg_info (_id,"------------------------------") ; 
00308     cpl_msg_info(_id,"generates OBJ-SKY pairs");
00309     cpl_msg_info (_id,"------------------------------") ; 
00310     tot_set=cpl_frameset_new();
00311     p = cpl_parameterlist_find(config,"sinfoni.objnod.autojitter_method");
00312     cpl_parameter_set_int(p,0);
00313 
00314     for (i=0;i<nobj;i++) {
00315 
00316        wrk_set=cpl_frameset_new();
00317        obj_frm=cpl_frameset_get_frame(obj_set,i);
00318        mjd_obj=sinfoni_get_mjd_obs(obj_frm);
00319        sky_frm=cpl_frameset_get_frame(sky_set,0);
00320        mjd_sky=sinfoni_get_mjd_obs(sky_frm);
00321        strcpy(filename,cpl_frame_get_filename(obj_frm));
00322 
00323        for (j=1;j<nsky;j++) {
00324           tmp_sky_frm = cpl_frameset_get_frame(sky_set,j);
00325           mjd_sky_frm = sinfoni_get_mjd_obs(tmp_sky_frm);
00326 
00327           if(1000.*(mjd_sky_frm-mjd_obj)*(mjd_sky_frm-mjd_obj) <
00328              1000.*(mjd_sky-    mjd_obj)*(mjd_sky-    mjd_obj) ) {
00329          mjd_sky=mjd_sky_frm;
00330              sky_frm=cpl_frame_duplicate(tmp_sky_frm);
00331       }
00332        }
00333        /* monitors whih obj-sky frames are inserted in each wrk_set */
00334        dup_frm=cpl_frame_duplicate(obj_frm);
00335        cpl_frameset_insert(wrk_set,dup_frm);
00336        dup_frm=cpl_frame_duplicate(sky_frm);
00337        cpl_frameset_insert(wrk_set,dup_frm);
00338 
00339        /* merged CDB frames to work set */
00340           for(k=0;k<ncdb;k++) {
00341             cdb_frm=cpl_frameset_get_frame(cdb_set,k);
00342             dup_frm=cpl_frame_duplicate(cdb_frm);
00343         cpl_frameset_insert(wrk_set,dup_frm);
00344       }
00345        
00346        nwrk=cpl_frameset_get_size(wrk_set);
00347        for(k=0;k<nwrk;k++) {
00348            wrk_frm=cpl_frameset_get_frame(wrk_set,k);
00349        }
00350 
00351        sprintf(out_name,"%s%d%s","out_stack",i,".fits");
00352        {
00353      
00354       cpl_msg_info (_id,"------------------------------") ; 
00355       cpl_msg_info (_id,"PREPARE STACKED SET %d",i) ; 
00356       cpl_msg_info (_id,"------------------------------") ; 
00357           if ( -1 == (ind = prepare_stacked_frames(_id,config, wrk_set, PRO_STD_NODDING_STACKED,i,fk)))
00358           {
00359         cpl_msg_error(_id,"PREPARING STACKED SET %d No:%d",i, ind) ;
00360             cpl_frameset_delete(wrk_set);
00361             cpl_frameset_delete(obj_set);
00362             cpl_frameset_delete(sky_set);
00363             cpl_frameset_delete(cdb_set);
00364             cpl_frameset_delete(tot_set);
00365             fake_delete(fk);
00366             sinfoni_memory_status();
00367         return -1 ;
00368       }
00369       cpl_msg_info (_id,"PREPARE STACKED SET %d SUCCESS",i) ; 
00370      
00371        }
00372        stk_set=cpl_frameset_new();
00373        sinfoni_contains_frames_kind(wrk_set,stk_set,PRO_STACKED);
00374        nstk=cpl_frameset_get_size(stk_set);
00375        /* merged result frames to tot set */
00376        for(k=0;k<nstk;k++) {
00377          wrk_frm=cpl_frameset_get_frame(stk_set,k);
00378          dup_frm=cpl_frame_duplicate(wrk_frm);
00379      cpl_frameset_insert(tot_set,dup_frm);
00380        }
00381        cpl_frameset_delete(stk_set);
00382        cpl_frameset_delete(wrk_set);
00383     }
00384 
00385     ntot=cpl_frameset_get_size(tot_set);
00386     for(k=0;k<ntot;k++) {
00387       wrk_frm=cpl_frameset_get_frame(tot_set,k);
00388       dup_frm=cpl_frame_duplicate(wrk_frm);
00389       cpl_frameset_insert(set,dup_frm);
00390     }
00391     cpl_frameset_delete(tot_set);
00392 
00393   }
00394   cpl_frameset_delete(obj_set);
00395   cpl_frameset_delete(sky_set);
00396   cpl_frameset_delete(cdb_set);
00397 
00398 
00399 
00400  
00401   if ( (nobj == 0) && (nsky != 0) ) {
00402 
00403      /* =============================================================== 
00404         SKY FRAMES STACKING 
00405         =============================================================== */
00406 
00407     {
00408     cpl_msg_info (_id,"------------------------------") ; 
00409     cpl_msg_info (_id,"SKY FRAMES STACKING") ; 
00410     cpl_msg_info (_id,"------------------------------") ; 
00411         if ( -1 == (ind = prepare_stacked_frames(_id,config, set, PRO_SKY_NODDING_STACKED,0,fk)))
00412         {
00413         cpl_msg_error(_id,"STACKING SKY FRAMES %d\n", ind) ;
00414             fake_delete(fk);
00415         sinfoni_memory_status();
00416         return -1 ;
00417     }
00418     cpl_msg_info (_id,"SKY FRAMES STACKING SUCCESS") ; 
00419     }    
00420 
00421 
00422   }
00423 
00424   if ( (nobj != 0) && (nsky == 0) ) {
00425 
00426      /* =============================================================== 
00427         OBJ FRAMES STACKING 
00428         =============================================================== */
00429 
00430     {
00431     cpl_msg_info (_id,"------------------------------") ; 
00432     cpl_msg_info (_id,"OBJ FRAMES STACKING") ; 
00433     cpl_msg_info (_id,"------------------------------") ; 
00434         if ( -1 == (ind = prepare_stacked_frames(_id,config, set, PRO_STD_NODDING_STACKED,0,fk)))
00435         {
00436         cpl_msg_error(_id,"STACKING OBJ FRAMES %d\n", ind) ;
00437             fake_delete(fk);
00438         sinfoni_memory_status();
00439         return -1 ;
00440     }
00441     cpl_msg_info (_id,"OBJ FRAMES STACKING SUCCESS") ; 
00442     }    
00443 
00444   }
00445 
00446 
00447     /* =============================================================== 
00448         SCI SCIENCE 
00449         =============================================================== */
00450     {
00451         cpl_msg_info (_id,"------------------------------") ; 
00452             cpl_msg_info(_id,"NODDING SCIENCE FRAMES");
00453         cpl_msg_info (_id,"------------------------------") ; 
00454         if ( -1 == (ind = objnod(_id,config, set, PRO_COADD_STD ) ) )
00455         {
00456                 cpl_msg_info(_id,"NODDING SCIENCE FRAMES No: %d",ind);
00457             fake_delete(fk);
00458         sinfoni_memory_status();
00459         return -1 ;
00460         }
00461             cpl_msg_info(_id,"NODDING SCIENCE FRAMES SUCCESS");
00462     }
00463     
00464 
00465      /* =============================================================== 
00466         STDSTAR 
00467         =============================================================== */
00468   cpl_msg_info (_id,"------------------------------") ;
00469   cpl_msg_info(_id,"STD STAR DATA REDUCTION");
00470   cpl_msg_info (_id,"------------------------------") ;
00471 
00472    {
00473         if ( -1 == (ind = stdstar(_id,config, set ) ) )
00474         {
00475         cpl_msg_info(_id,"REDUCING STD STAR DATA No: %d",ind) ;
00476         fake_delete(fk);
00477         sinfoni_memory_status();
00478         return -1 ;
00479     }
00480     cpl_msg_info (_id,"STD STAR DATA REDUCTION SUCCESS") ; 
00481    }    
00482    fake_delete(fk);
00483    sinfoni_memory_status();
00484     return 0 ;
00485 
00486 }
00487 

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