hawki_util_extinction.c

00001 /* $Id: hawki_util_extinction.c,v 1.2 2010/11/29 05:26:13 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI 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: cgarcia $
00023  * $Date: 2010/11/29 05:26:13 $
00024  * $Revision: 1.2 $
00025  * $Name: hawki-1_8_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_utils.h"
00041 #include "irplib_strehl.h"
00042 #include "irplib_stdstar.h"
00043 #include "irplib_cat.h"
00044 
00045 #include "hawki_utils.h"
00046 #include "hawki_calib.h"
00047 #include "hawki_load.h"
00048 #include "hawki_save.h"
00049 #include "hawki_pfits.h"
00050 #include "hawki_dfs.h"
00051 #include "hawki_alloc.h"
00052 
00053 /*-----------------------------------------------------------------------------
00054                             Functions prototypes
00055  -----------------------------------------------------------------------------*/
00056 
00057 static int hawki_util_extinction_create(cpl_plugin *) ;
00058 static int hawki_util_extinction_exec(cpl_plugin *) ;
00059 static int hawki_util_extinction_destroy(cpl_plugin *) ;
00060 static int hawki_util_extinction(cpl_parameterlist *, cpl_frameset *) ;
00061 
00062 static cpl_table ** hawki_util_extinction_reduce
00063 (cpl_frameset    *   set,
00064  const char      *   stdstars,
00065  const char      *   bpm,
00066  const char      *   flat,
00067  cpl_table       **  raw_zpoint_stats,
00068  int             *   star_labels,
00069  int             *   det_labels,
00070  cpl_imagelist   **  images);
00071 static int hawki_util_extinction_save_photsol
00072 (cpl_table           **  photsol_table,
00073  cpl_frameset        *   zpoint_frames,
00074  cpl_parameterlist   *   parlist,
00075  cpl_frameset        *   set);
00076 static cpl_table ** hawki_util_extinction_photom
00077 (cpl_frameset *   std_stars_photom);
00078 static cpl_table * hawki_util_extinction_get_photomsol
00079 (cpl_table * std_stars_photom);
00080 static int hawki_util_extinction_compute_keywords
00081 (cpl_frameset * set, 
00082  int          * det_labels);
00083 
00084 /*-----------------------------------------------------------------------------
00085                             Static variables
00086  -----------------------------------------------------------------------------*/
00087 
00088 
00089 static char hawki_util_extinction_description[] =
00090 "hawki_util_extinction -- Zero point recipe with extinction\n"
00091 "The input of the recipe files listed in the Set Of Frames (sof-file)\n"
00092 "must be tagged as:\n"
00093 "hawki_util_extinction.fits ("HAWKI_CALPRO_ZPOINT_TAB"): Zero point solution table\n"
00094 "The recipe creates as an output:\n"
00095 "hawki_cal_photom.fits ("HAWKI_CALPRO_PHOT_TAB"): Photometric solution\n"
00096 "Return code:\n"
00097 "esorex exits with an error code of 0 if the recipe completes successfully\n"
00098 "or 1 otherwise";
00099 
00100 /*-----------------------------------------------------------------------------
00101                                 Functions code
00102  -----------------------------------------------------------------------------*/
00103 
00104 /*----------------------------------------------------------------------------*/
00112 /*----------------------------------------------------------------------------*/
00113 int cpl_plugin_get_info(cpl_pluginlist * list)
00114 {
00115     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe)) ;
00116     cpl_plugin  *   plugin = &recipe->interface ;
00117 
00118     cpl_plugin_init(plugin,
00119                     CPL_PLUGIN_API,
00120                     HAWKI_BINARY_VERSION,
00121                     CPL_PLUGIN_TYPE_RECIPE,
00122                     "hawki_util_extinction",
00123                     "Zero point with extinction computation recipe",
00124                     hawki_util_extinction_description,
00125                     "Cesar Enrique Garcia Dabo",
00126                     "cgarcia@eso.org",
00127                     hawki_get_license(),
00128                     hawki_util_extinction_create,
00129                     hawki_util_extinction_exec,
00130                     hawki_util_extinction_destroy) ;
00131 
00132     cpl_pluginlist_append(list, plugin) ;
00133 
00134     return 0;
00135 }
00136 
00137 /*----------------------------------------------------------------------------*/
00146 /*----------------------------------------------------------------------------*/
00147 static int hawki_util_extinction_create(cpl_plugin * plugin)
00148 {
00149     cpl_recipe      * recipe ;
00150     cpl_parameter   * p ;
00151 
00152     /* Get the recipe out of the plugin */
00153     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00154         recipe = (cpl_recipe *)plugin ;
00155     else return -1 ;
00156 
00157     /* Create the parameters list in the cpl_recipe object */
00158     recipe->parameters = cpl_parameterlist_new() ;
00159 
00160     /* Fill the parameters list */
00161 
00162     /* Return */
00163     return 0;
00164 }
00165 
00166 /*----------------------------------------------------------------------------*/
00172 /*----------------------------------------------------------------------------*/
00173 static int hawki_util_extinction_exec(cpl_plugin * plugin)
00174 {
00175     cpl_recipe  *   recipe ;
00176 
00177     /* Get the recipe out of the plugin */
00178     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00179         recipe = (cpl_recipe *)plugin ;
00180     else return -1 ;
00181 
00182     /* Issue a banner */
00183     hawki_print_banner();
00184 
00185     return hawki_util_extinction(recipe->parameters, recipe->frames) ;
00186 }
00187 
00188 /*----------------------------------------------------------------------------*/
00194 /*----------------------------------------------------------------------------*/
00195 static int hawki_util_extinction_destroy(cpl_plugin * plugin)
00196 {
00197     cpl_recipe  *   recipe ;
00198 
00199     /* Get the recipe out of the plugin */
00200     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00201         recipe = (cpl_recipe *)plugin ;
00202     else return -1 ;
00203 
00204     cpl_parameterlist_delete(recipe->parameters) ;
00205     return 0 ;
00206 }
00207 
00208 /*----------------------------------------------------------------------------*/
00215 /*----------------------------------------------------------------------------*/
00216 static int hawki_util_extinction(
00217         cpl_parameterlist   *   parlist,
00218         cpl_frameset        *   framelist)
00219 {
00220     cpl_frameset    *   std_stars_photom ;
00221     cpl_table       **  zpoint_tables;
00222 
00223     /* Initialise Output */
00224     std_stars_photom = NULL ;
00225 
00226     /* Identify the RAW and CALIB frames in the input frameset */
00227     if (hawki_dfs_set_groups(framelist)) {
00228         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00229         return -1 ;
00230     }
00231 
00232     /* Retrieve zpoint tables */
00233     if ((std_stars_photom = hawki_extract_frameset
00234             (framelist, HAWKI_CALPRO_ZPOINT_TAB)) == NULL)
00235     {
00236         cpl_msg_error(__func__, "Cannot find std stars info (%s)",
00237                       HAWKI_CALPRO_ZPOINT_TAB);
00238         return -1 ;
00239     }
00240 
00241     /* Compute the zpoint values */
00242     cpl_msg_info(__func__, "Reduce the data") ;
00243     cpl_msg_indent_more() ;
00244     if ((zpoint_tables = hawki_util_extinction_photom(std_stars_photom))==NULL)
00245     {
00246         cpl_msg_error(__func__, "Cannot compute photometric solution") ;
00247         cpl_frameset_delete(std_stars_photom) ;
00248         cpl_msg_indent_less() ;
00249         return -1 ;
00250     }
00251     cpl_msg_indent_less() ;
00252 
00253     /* Save the products */
00254     cpl_msg_info(__func__, "Save the products");
00255     cpl_msg_indent_more() ;
00256     if (hawki_util_extinction_save_photsol
00257             (zpoint_tables, std_stars_photom,
00258              parlist, framelist) == -1)
00259     {
00260         cpl_msg_warning(__func__, "Data could not be saved. "
00261                         "Check permisions or disk space") ;
00262         hawki_table_delete(zpoint_tables) ;
00263         cpl_frameset_delete(std_stars_photom);
00264         cpl_msg_indent_less() ;
00265         return -1 ;
00266     }
00267     cpl_msg_indent_less() ;
00268 
00269     /* Free and return */
00270     cpl_frameset_delete(std_stars_photom);
00271     hawki_table_delete(zpoint_tables);
00272 
00273     /* Return */
00274     if (cpl_error_get_code()) return -1 ;
00275     else return 0 ;
00276 }
00277 
00278 /*----------------------------------------------------------------------------*/
00288 /*----------------------------------------------------------------------------*/
00289 static cpl_table ** hawki_util_extinction_photom
00290 (cpl_frameset *   std_stars_photom)
00291 
00292 {
00293     cpl_table **        photsol;
00294     int                 nframes;
00295     double              extinction;
00296     cpl_bivector    *   iqe_res ;
00297     int                 iframe;
00298     int                 idet;
00299     int                 jdet;
00300     int                 iext;
00301     int                 ext_nb;
00302     cpl_frame       *   ref_frame;
00303 
00304     /* Test entries */
00305     if (std_stars_photom == NULL) return NULL ;
00306 
00307     /* Initialise */
00308     photsol = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table*));
00309     nframes = cpl_frameset_get_size(std_stars_photom) ;
00310 
00311     /* Get reference */
00312     ref_frame = cpl_frameset_get_first(std_stars_photom);
00313     nframes = cpl_frameset_get_size(std_stars_photom);
00314 
00315     /* Loop on detectors */
00316     for (idet=0 ; idet<HAWKI_NB_DETECTORS; idet++)
00317     {
00318         cpl_table * merged_std_stars_photom;
00319 
00320         cpl_msg_info(cpl_func, "Working on detector %d", idet +1);
00321         cpl_msg_indent_more();
00322         /* Get the extension */
00323         ext_nb=hawki_get_ext_from_detector(cpl_frame_get_filename(ref_frame), idet+1);
00324 
00325         /* Load all the tables and merge them */
00326         for( iframe = 0 ; iframe < nframes ; ++iframe)
00327         {
00328             cpl_frame * this_frame;
00329             cpl_table * this_table;
00330             /* Get the current image */
00331             this_frame = cpl_frameset_get_frame(std_stars_photom, iframe);
00332             if(iframe == 0)
00333                 merged_std_stars_photom = cpl_table_load
00334                     (cpl_frame_get_filename(this_frame),ext_nb, 1);
00335             else
00336             {
00337                 cpl_table * this_table =
00338                         cpl_table_load(cpl_frame_get_filename(this_frame),ext_nb, 1);
00339                 cpl_table_insert(merged_std_stars_photom, this_table,
00340                                  cpl_table_get_nrow(merged_std_stars_photom));
00341                 cpl_table_delete(this_table);
00342             }
00343         }
00344 
00345         if(cpl_table_get_nrow(merged_std_stars_photom) < 2)
00346         {
00347             cpl_msg_error(__func__, "%d stars found. At least 2 stars must be present",
00348                     cpl_table_get_nrow(merged_std_stars_photom));
00349             cpl_table_delete(merged_std_stars_photom);
00350             return NULL;
00351         }
00352         cpl_msg_info(__func__,"Number of star measurements %d",
00353                      cpl_table_get_nrow(merged_std_stars_photom));
00354 
00355         if((photsol[idet] = hawki_util_extinction_get_photomsol
00356                 (merged_std_stars_photom)) == NULL)
00357         {
00358             cpl_table_delete(merged_std_stars_photom);
00359             for(jdet=0; jdet < idet; jdet++)
00360                 cpl_table_delete(photsol[jdet]);
00361             cpl_free(photsol);
00362             return NULL;
00363         }
00364 
00365         cpl_table_delete(merged_std_stars_photom);
00366         cpl_msg_indent_less();
00367     }
00368     return photsol;
00369 }
00370 
00371 static cpl_table * hawki_util_extinction_get_photomsol
00372 (cpl_table * std_stars_photom)
00373 {
00374     const double    * instrmag;
00375     const double    * airmass;
00376     const double    * catmag;
00377     cpl_table       * photsol;
00378     double            zeropoint;
00379     double            extcoeff;
00380     cpl_matrix      * xpos;
00381     cpl_vector      * ypos;
00382     cpl_polynomial  * phot_coeff;
00383     int               nstars;
00384     int               istar;
00385     const cpl_boolean sampsym = CPL_TRUE;
00386     const int         mindeg1d = 0;
00387     const int         maxdeg1d = 1;
00388     int               pows;
00389 
00390 
00391     nstars = cpl_table_get_nrow(std_stars_photom);
00392 
00393     photsol = cpl_table_new(1);
00394     instrmag = cpl_table_get_data_double_const
00395             (std_stars_photom, HAWKI_COL_ZPOINT_INSTRMAG);
00396     airmass = cpl_table_get_data_double_const
00397             (std_stars_photom, HAWKI_COL_ZPOINT_AIRMASS);
00398     catmag = cpl_table_get_data_double_const
00399             (std_stars_photom, HAWKI_COL_ZPOINT_STARMAG);
00400 
00401     if(instrmag == NULL || airmass == NULL || catmag == NULL)
00402     {
00403         cpl_msg_error(cpl_func, "Some of the following columns not found "
00404                       "in table: %s, %s, %s", HAWKI_COL_ZPOINT_INSTRMAG,
00405                       HAWKI_COL_ZPOINT_AIRMASS, HAWKI_COL_ZPOINT_STARMAG);
00406         cpl_table_delete(photsol);
00407         return NULL;
00408     }
00409 
00410     cpl_table_new_column(photsol, HAWKI_COL_PHOT_FILTER, CPL_TYPE_STRING);
00411     cpl_table_new_column(photsol, HAWKI_COL_PHOT_ZEROPOINT, CPL_TYPE_DOUBLE);
00412     cpl_table_set_column_unit(photsol,HAWKI_COL_PHOT_ZEROPOINT,"mag");
00413     cpl_table_new_column(photsol, HAWKI_COL_PHOT_EXTCOEFF, CPL_TYPE_DOUBLE);
00414     cpl_table_set_column_unit(photsol,HAWKI_COL_PHOT_EXTCOEFF,"mag/airmass");
00415         
00416     /* Make the fit to get the coefficients */
00417     xpos = cpl_matrix_new(1, nstars);
00418     ypos = cpl_vector_new(nstars);
00419     for(istar = 0; istar < nstars; ++istar)
00420     {
00421         double y;
00422         cpl_matrix_set(xpos, 0, istar, airmass[istar]);
00423         y = catmag[istar] + instrmag[istar];
00424         cpl_vector_set(ypos, istar, y);
00425     }
00426     /* phot_coeff[0] --> ZP
00427      * phot_coeff[1] --> -extcoeff
00428      */
00429     phot_coeff = cpl_polynomial_new(1);
00430     if(cpl_polynomial_fit(phot_coeff, xpos, NULL, ypos, NULL,
00431                           CPL_FALSE, &mindeg1d, &maxdeg1d) != CPL_ERROR_NONE)
00432     {
00433         cpl_msg_error(cpl_func,"Cannot get the photometric solution");
00434         cpl_table_delete(photsol);
00435         return NULL;
00436     }
00437     cpl_matrix_delete(xpos);
00438     cpl_vector_delete(ypos);
00439     pows = 0;
00440     zeropoint = cpl_polynomial_get_coeff(phot_coeff, &pows);
00441     pows = 1;
00442     extcoeff  = -cpl_polynomial_get_coeff(phot_coeff, &pows);
00443     cpl_polynomial_delete(phot_coeff);
00444 
00445     cpl_table_set_double(photsol, HAWKI_COL_PHOT_ZEROPOINT, 0,
00446                          zeropoint);
00447     cpl_table_set_double(photsol, HAWKI_COL_PHOT_EXTCOEFF, 0,
00448                          extcoeff);
00449 
00450     /* Output results */
00451     cpl_msg_indent_more();
00452     cpl_msg_info(__func__, "Zero point: %f", zeropoint);
00453     cpl_msg_info(__func__, "Extinction coefficient: %f", extcoeff);
00454     cpl_msg_indent_less();
00455 
00456     return photsol;
00457 }
00458 
00459 /*----------------------------------------------------------------------------*/
00468 /*----------------------------------------------------------------------------*/
00469 static int hawki_util_extinction_save_photsol
00470 (cpl_table           **  photsol_table,
00471  cpl_frameset        *   zpoint_frames,
00472  cpl_parameterlist   *   parlist,
00473  cpl_frameset        *   set)
00474 {
00475     cpl_propertylist    **  qclists ;
00476     const char          *   ref_filename;
00477     cpl_propertylist    *   inputlist ;
00478     int                     ext_nb, nframes ;
00479     const char          *   recipe_name = "hawki_util_extinction" ;
00480     int                     idet;
00481     int                     iframe;
00482     cpl_errorstate          error_prevstate = cpl_errorstate_get();
00483 
00484 
00485     /* Initialise */
00486     nframes = cpl_frameset_get_size(set) ;
00487 
00488     /* Get the reference frame */
00489     ref_filename = hawki_get_extref_file(set);
00490 
00491     /* Create the QC lists for the extensions */
00492     qclists = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00493     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
00494     {
00495         int            this_iframe = -1;
00496 
00497         qclists[idet] = cpl_propertylist_new() ;
00498         /* These QC are common to all extensions */
00499 
00500 
00501         /* Propagate some keywords from input raw frame extensions */
00502         ext_nb=hawki_get_ext_from_detector(ref_filename, idet+1);
00503         inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
00504                 HAWKI_HEADER_EXT_FORWARD, 0) ;
00505         cpl_propertylist_append(qclists[idet], inputlist) ;
00506         cpl_propertylist_delete(inputlist) ;
00507 
00508     }
00509     
00510     /* Write the photometric table  */
00511     hawki_tables_save(set,
00512                       parlist,
00513                       zpoint_frames,
00514                       (const cpl_table **)photsol_table,
00515                       recipe_name,
00516                       HAWKI_CALPRO_PHOT_TAB,
00517                       HAWKI_PROTYPE_PHOT_TAB,
00518                       NULL,
00519                       (const cpl_propertylist **)qclists,
00520                       "hawki_util_extinction.fits");
00521 
00522     /* Free */
00523     for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
00524         cpl_propertylist_delete(qclists[idet]) ;
00525     }
00526     cpl_free(qclists) ;
00527 
00528     /* Free */
00529     if(!cpl_errorstate_is_equal(error_prevstate))
00530     {
00531         cpl_errorstate_set(CPL_ERROR_NONE);
00532         return -1;
00533     }
00534     return  0;
00535 }

Generated on Thu Feb 17 17:13:08 2011 for HAWKI Pipeline Reference Manual by  doxygen 1.4.7