uves_response_utils.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019 
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2011/12/08 14:02:17 $
00023  * $Revision: 1.18 $
00024  * $Name: uves-4_9_15 $
00025  * $Log: uves_response_utils.c,v $
00026  * Revision 1.18  2011/12/08 14:02:17  amodigli
00027  * Fix warnings with CPL6
00028  *
00029  * Revision 1.17  2010/09/24 09:32:07  amodigli
00030  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00031  *
00032  * Revision 1.15  2007/08/21 13:08:26  jmlarsen
00033  * Removed irplib_access module, largely deprecated by CPL-4
00034  *
00035  * Revision 1.14  2007/06/06 08:17:33  amodigli
00036  * replace tab with 4 spaces
00037  *
00038  * Revision 1.13  2007/04/24 12:50:29  jmlarsen
00039  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00040  *
00041  * Revision 1.12  2007/04/10 07:09:48  jmlarsen
00042  * Changed interface of uves_spline_hermite()
00043  *
00044  * Revision 1.11  2006/11/15 15:02:15  jmlarsen
00045  * Implemented const safe workarounds for CPL functions
00046  *
00047  * Revision 1.9  2006/11/15 14:04:08  jmlarsen
00048  * Removed non-const version of parameterlist_get_first/last/next which is
00049  * already in CPL, added const-safe wrapper, unwrapper and deallocator functions
00050  *
00051  * Revision 1.8  2006/11/07 14:04:45  jmlarsen
00052  * Fixed memory error by setting pointer to NULL after cpl_free
00053  *
00054  * Revision 1.7  2006/11/06 15:19:42  jmlarsen
00055  * Removed unused include directives
00056  *
00057  * Revision 1.6  2006/11/03 15:01:21  jmlarsen
00058  * Killed UVES 3d table module and use CPL 3d tables
00059  *
00060  * Revision 1.5  2006/08/17 13:56:53  jmlarsen
00061  * Reduced max line length
00062  *
00063  * Revision 1.4  2006/08/17 09:20:43  jmlarsen
00064  * Get reference object ID from flux table, not raw header
00065  *
00066  * Revision 1.3  2006/06/01 14:43:17  jmlarsen
00067  * Added missing documentation
00068  *
00069  * Revision 1.2  2006/03/24 13:56:13  jmlarsen
00070  * Changed ambigous text message
00071  *
00072  * Revision 1.1  2006/02/03 07:51:04  jmlarsen
00073  * Moved recipe implementations to ./uves directory
00074  *
00075  * Revision 1.2  2005/12/19 16:17:55  jmlarsen
00076  * Replaced bool -> int
00077  *
00078  * Revision 1.1  2005/11/11 13:18:54  jmlarsen
00079  * Reorganized code, renamed source files
00080  *
00081  */
00082 
00083 #ifdef HAVE_CONFIG_H
00084 #  include <config.h>
00085 #endif
00086 
00087 /*----------------------------------------------------------------------------*/
00091 /*----------------------------------------------------------------------------*/
00094 /*-----------------------------------------------------------------------------
00095                                 Includes
00096  -----------------------------------------------------------------------------*/
00097 
00098 #include <uves_response_utils.h>
00099 
00100 #include <uves_utils.h>
00101 #include <uves_utils_wrappers.h>
00102 #include <uves_pfits.h>
00103 #include <uves_error.h>
00104 #include <uves_msg.h>
00105 
00106 #include <cpl.h>
00107 
00108 #include <stdbool.h>
00109 
00110 /*-----------------------------------------------------------------------------
00111                             Defines
00112  -----------------------------------------------------------------------------*/
00113 /*-----------------------------------------------------------------------------
00114                             Functions prototypes
00115  -----------------------------------------------------------------------------*/
00116 
00117 /*-----------------------------------------------------------------------------
00118                             Implementation
00119  -----------------------------------------------------------------------------*/
00120 
00121 /*----------------------------------------------------------------------------*/
00138 /*----------------------------------------------------------------------------*/
00139 cpl_image *
00140 uves_calculate_response(const cpl_image *spectrum, const uves_propertylist *spectrum_header,
00141             const cpl_table *flux_table,
00142             const uves_propertylist *raw_header,
00143             double PACCURACY,
00144             bool inverse,
00145             char **ref_obj_id)
00146 {
00147     cpl_image *response = NULL;             /* Result */
00148     cpl_table *catalogue_flux = NULL;
00149     int nx, norders;
00150     
00151     nx      = cpl_image_get_size_x(spectrum);
00152     norders = cpl_image_get_size_y(spectrum);
00153     
00154     response = cpl_image_new(nx, norders, CPL_TYPE_DOUBLE);
00155 
00156     check( catalogue_flux = uves_align(raw_header, flux_table, PACCURACY, ref_obj_id),
00157        "Cannot read catalogue flux");
00158 
00159     /* Correct for atmospheric extinction, and calculate response */
00160     {
00161     double dlambda;
00162     int order;
00163     
00164     check( dlambda = uves_pfits_get_cdelt1(spectrum_header),
00165            "Error reading bin width from header");
00166     
00167     for (order = 1; order <= norders; order++)
00168         {
00169         double lambda_start;
00170         int x;
00171         
00172         /* If spectrum was already merged, then read cdelt1,
00173          * otherwise read wstart for each order
00174          */
00175         if (norders == 1)
00176             {
00177             check( lambda_start = uves_pfits_get_crval1(spectrum_header),
00178                    "Error reading start wavelength from header");    
00179             }
00180         else
00181             {
00182             check( lambda_start = uves_pfits_get_wstart(spectrum_header, order),
00183                    "Error reading start wavelength from header");    
00184             }
00185         
00186         for (x = 1; x <= nx; x++)
00187             {
00188             int pis_rejected;
00189             double lambda;            
00190             double flux, std_flux, resp;
00191                         int istart = 0;
00192 
00193             lambda = lambda_start + (x-1) * dlambda;
00194             
00195             check( flux = cpl_image_get(spectrum, x, order, &pis_rejected),
00196                    "Error reading flux");
00197             
00198             if (!pis_rejected)
00199                 {
00200                 /* Get interpolated catalogue flux */
00201                 check( std_flux = uves_spline_hermite_table(
00202                        lambda, catalogue_flux,
00203                        "LAMBDA", "F_LAMBDA", &istart),
00204                        "Error interpolating catalogue flux");
00205                 
00206                 if (inverse)
00207                     {
00208                     resp = (flux == 0) ? 0 : std_flux / flux;
00209                     }
00210                 else
00211                     {
00212                     resp = (std_flux == 0) ? 0 : flux / std_flux;
00213                     }
00214                 
00215                 check( cpl_image_set(response, x, order, resp),
00216                        "Error writing response image");
00217                 }
00218             else
00219                 {
00220                 cpl_image_reject(response, x, order);
00221                 }
00222             }
00223         }
00224     }
00225     
00226   cleanup:
00227     uves_free_table(&catalogue_flux);
00228     if (cpl_error_get_code() != CPL_ERROR_NONE)
00229     {
00230         uves_free_image(&response);
00231     }
00232     return response;
00233 }
00234 
00235 
00236 /*----------------------------------------------------------------------------*/
00255 /*----------------------------------------------------------------------------*/
00256 cpl_table *
00257 uves_align(const uves_propertylist *object_header, const cpl_table *flux_table,
00258        double accuracy,
00259        char **ref_name_dynamic)
00260 {
00261     double obj_ra, obj_dec;  /* Object position in degrees    */
00262     int nident = 0;          /* Number of identifications     */
00263     int match_row = 0;       /* Catalogue row number of match */
00264     double min_dist = 0;     /* Accuracy of match             */
00265     double std_ra = 0, std_dec = 0;    /* Catalogue position  */
00266     const char *ref_type = NULL;
00267 
00268     cpl_table *result = NULL;
00269 
00270     int i;
00271 
00272     assure_nomsg( ref_name_dynamic != NULL, CPL_ERROR_NULL_INPUT );
00273     *ref_name_dynamic = NULL;
00274 
00275     check( obj_ra  = uves_pfits_get_ra (object_header), "Could not read right ascension");
00276     check( obj_dec = uves_pfits_get_dec(object_header), "Could not read declination");
00277     
00278     uves_msg("Object RA, DEC = (%e, %e)", obj_ra, obj_dec);
00279 
00280     nident = 0;
00281     for (i = 0; i < cpl_table_get_nrow(flux_table); i++)
00282     {
00283         double ref_ra, ref_dec;
00284         double dist;
00285 
00286         check( ref_ra  = cpl_table_get_double(flux_table, "RA_DEG", i, NULL),
00287            "Could not read catalogue star right ascension");
00288         check( ref_dec = cpl_table_get_double(flux_table, "DEC_DEG", i, NULL),
00289            "Could not read catalogue star declination");
00290 
00291         /* Calculate angular separation in arcsecs
00292          * cos(sep) = sin(d1)sin(d2) + cos(d1)cos(d2)cos(ra1-ra2) 
00293          *
00294          * All input angles are in degrees.
00295          */
00296         
00297         dist = 
00298         SIN_DEG(obj_dec)*SIN_DEG(ref_dec) + 
00299         COS_DEG(obj_dec)*COS_DEG(ref_dec)*COS_DEG(obj_ra - ref_ra);
00300         
00301         dist = uves_max_double(dist, -1);
00302         dist = uves_min_double(dist,  1);
00303         
00304         dist = ACOS_DEG(dist) * 3600;
00305 
00306         uves_msg_debug("Angular separation = %f arcseconds", dist);
00307 
00308 
00309         /* Keep track of best match also if it is not within the pointing accuracy */
00310         if (i == 0 || dist < min_dist)
00311         {
00312             min_dist = dist;
00313             std_ra = ref_ra;
00314             std_dec = ref_dec;
00315         }
00316 
00317 
00318         /* Does it match? */
00319         if (dist < accuracy)
00320         {
00321             nident += 1;
00322             match_row = i;        
00323             min_dist = dist;
00324             std_ra = ref_ra;
00325             std_dec = ref_dec;            
00326         }
00327     }
00328 
00329     assure( nident >= 1, CPL_ERROR_INCOMPATIBLE_INPUT, 
00330         "No catalogue object within %f arcsecs. "
00331         "Nearest object is %f arcsecs away at (RA, DEC) = (%f, %f)", 
00332         accuracy, min_dist, std_ra, std_dec);
00333 
00334     assure( nident <= 1, CPL_ERROR_INCOMPATIBLE_INPUT,
00335         "%" CPL_SIZE_FORMAT " matching catalogue objects found. Confused. "
00336         "Decrease pointing accuracy (currently %f arcsecs) to get fewer matches", 
00337         nident, accuracy);
00338 
00339     check( *ref_name_dynamic = cpl_strdup(
00340            cpl_table_get_string(flux_table, "OBJECT", match_row)),
00341        "Could not read reference object name");
00342     
00343     check( ref_type = cpl_table_get_string(flux_table, "TYPE", match_row),
00344        "Could not read reference object type");
00345     
00346     uves_msg("Object ID is '%s', type = '%s'. Residual between header/catalogue = %f arcsecs",
00347          *ref_name_dynamic, ref_type, min_dist);
00348     
00349     
00350     /* Create (2d) flux table from catalogue table row number 'match_row' */
00351     {
00352     const char *columns[3] = {"LAMBDA", "BIN_WIDTH", "F_LAMBDA"};
00353     int ndata;                      /* Number of elements in column */
00354 
00355     check( ndata = cpl_table_get_int(flux_table, "NDATA", match_row, NULL),
00356            "Error reading length of flux array");
00357     
00358     result = cpl_table_new(ndata);
00359 
00360     for(i = 0; i < 3; i++)
00361         {
00362         const cpl_array *data;
00363         int indx;
00364         
00365         cpl_table_new_column(result, columns[i], CPL_TYPE_DOUBLE);
00366         
00367         data = cpl_table_get_array(flux_table, columns[i], match_row);
00368 
00369         /* Only the 'ndata' first elements of the array are used,
00370            and the array may be longer than this */
00371         uves_msg_debug("3d table array size = %" CPL_SIZE_FORMAT ", ndata = %" CPL_SIZE_FORMAT "",
00372                    cpl_array_get_size(data), ndata);
00373 
00374         assure( cpl_array_get_size(data) >= ndata,
00375             CPL_ERROR_ILLEGAL_INPUT,
00376             "Flux table row %" CPL_SIZE_FORMAT ": column '%s' depth (%" CPL_SIZE_FORMAT ") "
00377             "is less than NDATA (%" CPL_SIZE_FORMAT ")",
00378             match_row, columns[i], cpl_array_get_size(data), ndata);
00379 
00380         for (indx = 0; indx < ndata; indx++)
00381             {
00382             /* 3d columns are float */
00383             cpl_table_set_double(result, columns[i], indx, 
00384                          cpl_array_get_float(data, indx, NULL));
00385             }
00386         }
00387     }
00388   
00389   cleanup:
00390     if (cpl_error_get_code() != CPL_ERROR_NONE)
00391     {
00392         uves_free_table(&result);
00393         if (ref_name_dynamic != NULL) 
00394         {
00395             cpl_free(*ref_name_dynamic);
00396             *ref_name_dynamic = NULL;
00397         }
00398     }
00399 
00400     return result;
00401 }
00402 

Generated on 28 Feb 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1