uves_merge.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: 2007/06/06 08:17:33 $
00023  * $Revision: 1.41 $
00024  * $Name: uves-3_3_1 $
00025  * $Log: uves_merge.c,v $
00026  * Revision 1.41  2007/06/06 08:17:33  amodigli
00027  * replace tab with 4 spaces
00028  *
00029  * Revision 1.40  2007/05/03 15:21:13  jmlarsen
00030  * Decreased output message verbosity
00031  *
00032  * Revision 1.39  2007/04/24 12:50:29  jmlarsen
00033  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00034  *
00035  * Revision 1.38  2007/02/09 08:57:54  jmlarsen
00036  * Include <float.h>
00037  *
00038  * Revision 1.37  2007/02/09 08:14:16  jmlarsen
00039  * Do not use CPL_PIXEL_MAXVAL which works only for integer images
00040  *
00041  * Revision 1.36  2007/02/08 07:34:28  jmlarsen
00042  * Minor doc change
00043  *
00044  * Revision 1.35  2006/11/15 15:02:14  jmlarsen
00045  * Implemented const safe workarounds for CPL functions
00046  *
00047  * Revision 1.33  2006/11/15 14:04:08  jmlarsen
00048  * Removed non-const version of parameterlist_get_first/last/next which is already
00049  * in CPL, added const-safe wrapper, unwrapper and deallocator functions
00050  *
00051  * Revision 1.32  2006/11/13 14:23:55  jmlarsen
00052  * Removed workarounds for CPL const bugs
00053  *
00054  * Revision 1.31  2006/11/06 15:19:41  jmlarsen
00055  * Removed unused include directives
00056  *
00057  * Revision 1.30  2006/09/11 14:00:11  jmlarsen
00058  * Minor documentation change
00059  *
00060  * Revision 1.29  2006/08/17 13:56:53  jmlarsen
00061  * Reduced max line length
00062  *
00063  * Revision 1.28  2006/08/17 09:16:47  jmlarsen
00064  * Removed CPL2 code
00065  *
00066  * Revision 1.27  2006/08/11 14:37:59  jmlarsen
00067  * Added input validation
00068  *
00069  * Revision 1.26  2006/08/10 10:50:12  jmlarsen
00070  * Removed workaround for cpl_image_get_bpm
00071  *
00072  * Revision 1.25  2006/07/03 13:16:42  jmlarsen
00073  * Reduced number of significant digits in message
00074  *
00075  * Revision 1.24  2006/04/06 08:38:56  jmlarsen
00076  * Changed char* -> const char* for static string
00077  *
00078  * Revision 1.23  2006/03/03 13:54:11  jmlarsen
00079  * Changed syntax of check macro
00080  *
00081  * Revision 1.22  2006/02/03 07:46:30  jmlarsen
00082  * Moved recipe implementations to ./uves directory
00083  *
00084  * Revision 1.21  2006/01/31 08:24:29  jmlarsen
00085  * Wrapper for cpl_image_get_bpm
00086  *
00087  * Revision 1.20  2006/01/25 16:13:20  jmlarsen
00088  * Changed interface of gauss.fitting routine
00089  *
00090  * Revision 1.19  2005/12/19 16:17:56  jmlarsen
00091  * Replaced bool -> int
00092  *
00093  * Revision 1.18  2005/12/16 14:22:23  jmlarsen
00094  * Removed midas test data; Added sof files
00095  *
00096  * Revision 1.17  2005/11/24 15:09:06  jmlarsen
00097  * Implemented 2d extraction/rebinning/merging
00098  *
00099  * Revision 1.16  2005/11/24 11:54:46  jmlarsen
00100  * Added support for CPL 3 interface
00101  *
00102  * Revision 1.15  2005/11/18 10:52:06  jmlarsen
00103  * Split into optimal/sum merge methods
00104  *
00105  * Revision 1.14  2005/11/11 13:18:54  jmlarsen
00106  * Reorganized code, renamed source files
00107  *
00108  */
00109 
00110 #ifdef HAVE_CONFIG_H
00111 #  include <config.h>
00112 #endif
00113 
00114 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00124 /*-----------------------------------------------------------------------------
00125                                 Includes
00126  -----------------------------------------------------------------------------*/
00127 
00128 #include <uves_merge.h>
00129 
00130 #include <uves_pfits.h>
00131 #include <uves_utils.h>
00132 #include <uves_utils_wrappers.h>
00133 #include <uves_dump.h>
00134 #include <uves_error.h>
00135 
00136 #include <irplib_access.h>
00137 #include <cpl.h>
00138 #include <float.h>
00139 
00140 /*-----------------------------------------------------------------------------
00141                             Functions prototypes
00142  -----------------------------------------------------------------------------*/
00143 
00144 /*-----------------------------------------------------------------------------
00145                             Implementation
00146  -----------------------------------------------------------------------------*/
00147 
00148 /*----------------------------------------------------------------------------*/
00175 /*----------------------------------------------------------------------------*/
00176 
00177 cpl_image *
00178 uves_merge_orders(const cpl_image *spectrum, const cpl_image *spectrum_noise,
00179           const uves_propertylist *spectrum_header,
00180           merge_method m_method,
00181           int n_traces,
00182           uves_propertylist **merged_header,
00183           cpl_image **merged_noise)
00184 {
00185     cpl_image *merged = NULL;    /* Result */
00186 
00187     const double *spectrum_data     = NULL;
00188     const cpl_mask *spectrum_badmap = NULL;
00189     const cpl_binary *spectrum_bad  = NULL;
00190 
00191     const double *noise_data     = NULL;
00192     const cpl_mask *noise_badmap = NULL;
00193     const cpl_binary *noise_bad  = NULL;
00194 
00195     int nbins, ny, norders;          /* Input image size. ny = norders*n_traces */
00196     double wavestep;
00197     int bin_min = 0, bin_max = 0;    /* wavelength of min/max bin in units of 'wavestep' */
00198     int total_bins;
00199     int order, trace;
00200 
00201     passure( spectrum != NULL, " ");
00202     passure( spectrum_noise != NULL, " ");
00203     passure( spectrum_header != NULL, " ");
00204     passure( merged_header != NULL, " ");
00205     passure( merged_noise != NULL, " ");
00206 
00207     assure( m_method == MERGE_OPTIMAL || m_method == MERGE_SUM, CPL_ERROR_ILLEGAL_INPUT,
00208         "Unknown merge method: %d", m_method);
00209 
00210     assure( cpl_image_get_type(spectrum) == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
00211         "Spectrum must have type double. It is '%s'",
00212         uves_tostring_cpl_type(cpl_image_get_type(spectrum)));
00213 
00214     assure( cpl_image_get_type(spectrum_noise) == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
00215         "Spectrum noise must have type double. It is '%s'",
00216         uves_tostring_cpl_type(cpl_image_get_type(spectrum_noise)));
00217 
00218     /* Read input spectrum geometry */
00219     nbins           = cpl_image_get_size_x(spectrum);
00220     ny              = cpl_image_get_size_y(spectrum);
00221 
00222     assure( cpl_image_get_size_x(spectrum_noise) == nbins &&
00223         cpl_image_get_size_y(spectrum_noise) == ny,
00224         CPL_ERROR_INCOMPATIBLE_INPUT,
00225         "Incompatible spectrum/noise image sizes: %dx%d vs. %dx%d",
00226         nbins, ny,
00227         cpl_image_get_size_x(spectrum_noise),
00228         cpl_image_get_size_y(spectrum_noise));
00229     
00230     assure( ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
00231         "Spectrum image height (%d) is not a multiple of "
00232         "the number of traces (%d). Confused, bailing out",
00233         ny, n_traces);
00234     
00235     norders         = ny / n_traces;
00236     
00237     check( wavestep = uves_pfits_get_cdelt1(spectrum_header),
00238        "Error reading bin width");
00239 
00240     /* Get data pointers (for efficiency) */
00241     spectrum_data   = irplib_image_get_data_double_const(spectrum);
00242     spectrum_badmap = irplib_image_get_bpm_const(spectrum);
00243     spectrum_bad    = irplib_mask_get_data_const(spectrum_badmap);
00244 
00245     noise_data   = irplib_image_get_data_double_const(spectrum_noise);
00246     noise_badmap = irplib_image_get_bpm_const(spectrum_noise);
00247     noise_bad    = irplib_mask_get_data_const(noise_badmap);
00248 
00249     /* Read max/min lambda */
00250     for (order = 1; order <= norders; order++)
00251     {
00252         double wstart, wend;
00253         check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00254            "Error reading start wavelength for order #%d", order);
00255 
00256         check( wend = uves_pfits_get_wend(spectrum_header, order),
00257            "Error reading end wavelength for order #%d", order);
00258         
00259         uves_msg_debug("Order #%d: wstart - wend = %f - %f wlu", order, wstart, wend);
00260 
00261         if (order == 1)
00262         {
00263             bin_min = uves_round_double(wstart/wavestep);
00264             bin_max = uves_round_double(wend  /wavestep);
00265         }
00266         
00267         bin_min = uves_min_int(bin_min, uves_round_double(wstart/wavestep));
00268         bin_max = uves_max_int(bin_max, uves_round_double(wend  /wavestep));
00269     }
00270 
00271     total_bins = (bin_max - bin_min) + 1;
00272 
00273     uves_msg_debug("Merging orders into %d bins covering wavelengths %.3f - %.3f wlu", 
00274             total_bins, bin_min * wavestep, bin_max * wavestep);
00275 
00276     /* Initialize spectrum to zero and noise to negative */
00277     merged        = cpl_image_new(total_bins, n_traces, CPL_TYPE_DOUBLE);
00278     *merged_noise = cpl_image_new(total_bins, n_traces, CPL_TYPE_DOUBLE);
00279     cpl_image_add_scalar(*merged_noise, -1.0);
00280     
00281     /* Distribute input in output bins */
00282     for (order = 1; order <= norders; order++)
00283     {
00284         double wstart, wend;
00285         int wstart_bin, wend_bin;      /* In 1d space */
00286 
00287         check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00288            "Error reading start wavelength for order #%d", order);
00289         
00290         check( wend = uves_pfits_get_wend(spectrum_header, order),
00291            "Error reading end wavelength for order #%d", order);
00292         
00293         wstart_bin = uves_round_double(wstart/wavestep);
00294         wend_bin   = uves_round_double(wend/wavestep);
00295 
00296         /* Loop over spatial traces (only 1 trace, unless extraction was 2d) */
00297         for (trace = 1; trace <= n_traces; trace++)
00298         {
00299             int spectrum_row = (order - 1)*n_traces + trace;
00300             int rel_bin;                   /* Counting columns in input spectrum */
00301             
00302             for (rel_bin = 1; rel_bin <= wend_bin - wstart_bin + 1; rel_bin++)
00303             {
00304                 double flux, noise;
00305                 double current_flux, new_flux;
00306                 double current_noise, new_noise;
00307                 double weight;
00308                 int pis_rejected, noise_rejected;
00309                 
00310                 /* merged_bin = (offset of order)  +  (offset inside order) */
00311                 int merged_bin = (wstart_bin - bin_min) + rel_bin;
00312                 
00313                 passure(1 <= merged_bin && merged_bin <= total_bins,
00314                     "%d %d %d", rel_bin, merged_bin, total_bins);
00315                 
00316                 /* This is slow:
00317                    check( flux  = cpl_image_get(spectrum      , 
00318                           rel_bin, spectrum_row, &pis_rejected);
00319                    noise = cpl_image_get(spectrum_noise, rel_bin, 
00320                     spectrum_row, &noise_rejected),
00321                    "Error reading input spectrum");
00322                 */
00323                 
00324                 flux         = spectrum_data[(rel_bin-1) + (spectrum_row-1) * nbins];
00325                 pis_rejected = spectrum_bad [(rel_bin-1) + (spectrum_row-1) * nbins];
00326 
00327                 noise          = noise_data[(rel_bin-1) + (spectrum_row-1) * nbins];
00328                 noise_rejected = noise_bad [(rel_bin-1) + (spectrum_row-1) * nbins];
00329                 
00330                 if (!pis_rejected && !noise_rejected)
00331                 {
00332                     check(( current_flux  = cpl_image_get(
00333                         merged      , merged_bin, trace, &pis_rejected),
00334                         current_noise = cpl_image_get(
00335                         *merged_noise, merged_bin, trace, &pis_rejected)),
00336                        "Error reading merged spetrum");
00337 
00338                     weight = 1/(noise*noise);
00339                     
00340                     /*
00341                      * Optimal formulas for Variance and Flux are
00342                      *
00343                      *   Vn = ( 1/sigma1^2 + ... +  1/sigmaN^2)^-1
00344                      *   Fn = (f1/sigma1^2 + ... + fN/sigmaN^2) * Vn
00345                      *
00346                      * Update by using these recurrence relations
00347                      *
00348                      *   Fn+1 = (Fn/Vn + fn+1/(sigma_{n+1})^2) * Vn+1  for n > 1
00349                      *   Vn+1 = (Vn^-1 + 1/(sigma_{n+1})^2)^-1  for n > 1
00350                      *
00351                      *
00352                      *  In the case of method = sum,
00353                      *
00354                      *  Vn = sigma1^2 + ... + sigmaN^2
00355                      *  Fn = f1 + ... + fN
00356                      *
00357                      */
00358                     
00359                     if (current_noise > 0)
00360                     {
00361                         if (m_method == MERGE_OPTIMAL)
00362                         {
00363                             new_noise  = 1/(current_noise*current_noise);
00364                             new_noise += weight;
00365                             new_noise  = 1/sqrt(new_noise);
00366                         }
00367                         else if (m_method == MERGE_SUM)
00368                         {
00369                             new_noise = sqrt(current_noise*current_noise
00370                                      + noise*noise);
00371                         }
00372                         else
00373                         {
00374                             /* Impossible */
00375                             passure( false, "%d", m_method);
00376                         }
00377                     }
00378                     else
00379                     {
00380                         /* First time in this bin */
00381                         new_noise = noise;
00382                     }
00383                     
00384                     if (current_noise > 0)
00385                     {
00386                         if (m_method == MERGE_OPTIMAL)
00387                         {
00388                             new_flux = (current_flux / 
00389                                 (current_noise*current_noise)
00390                                 + flux * weight) * 
00391                             (new_noise*new_noise);
00392                         }
00393                         else if (m_method == MERGE_SUM)
00394                         {
00395                             new_flux = current_flux + flux;
00396                         }
00397                         else
00398                         {
00399                             /* Impossible */
00400                             passure( false, "%d", m_method);
00401                         }
00402                     }
00403                     else
00404                     {
00405                         new_flux = flux;
00406                     }
00407                     
00408                     check( cpl_image_set(
00409                            merged      , merged_bin, trace, new_flux),
00410                        "Error updating merged spectrum");
00411                     check( cpl_image_set(
00412                            *merged_noise, merged_bin, trace, new_noise),
00413                        "Error updating weights");
00414                     
00415 /*            uves_msg("Input flux = %e +- %e ;  
00416             Binned flux changed from %e +- %e to %e +- %e",
00417             flux, noise,
00418             current_flux, current_noise, new_flux, new_noise);*/
00419                     
00420                 } /* If pixel is good ... */
00421 
00422             } /* For each input bin */
00423 
00424         }/* For trace ... */
00425 
00426     }/* For order ... */
00427     
00428     /* Undefined bins have  (flux, noise) = (0, -1)  (the initial values).
00429      * Set to (flux, noise) = (0, 1)
00430      */
00431     check( cpl_image_threshold(*merged_noise, 
00432                    0, DBL_MAX,        /* Outside this interval */
00433                    1, 1),             /* Set to these values   */
00434        "Error setting undefined noise");
00435     
00436     if (merged_header != NULL)
00437     {
00438         check( *merged_header = uves_initialize_image_header(
00439                "WAVELENGTH", (n_traces > 1) ? "PIXEL" : " ", "FLUX",
00440                bin_min * wavestep, 1.0,
00441                1.0, 1.0,
00442                wavestep, 1.0),
00443            "Error initializing merged spectrum header");
00444     }
00445     
00446   cleanup:
00447     return merged;
00448 }
00449 
00450 /*----------------------------------------------------------------------------*/
00460 /*----------------------------------------------------------------------------*/
00461 merge_method
00462 uves_get_merge_method(const cpl_parameterlist *parameters, const char *context, 
00463               const char *subcontext)
00464 {
00465     const char *mm = "";
00466     merge_method result = 0;
00467     
00468     check( uves_get_parameter(parameters, context, subcontext, "merge", CPL_TYPE_STRING, &mm),
00469        "Could not read parameter");
00470     
00471     if      (strcmp(mm, "optimal") == 0) result = MERGE_OPTIMAL;
00472     else if (strcmp(mm, "sum"    ) == 0) result = MERGE_SUM;
00473     else
00474     {
00475         assure(false, CPL_ERROR_ILLEGAL_INPUT, "No such merging method: '%s'", mm);
00476     }
00477     
00478   cleanup:
00479     return result;
00480 }
00481     
00482 

Generated on Tue Jun 19 14:39:16 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6