si_rec_psf.c

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