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 /*
00022  * $Author: amodigli $
00023  * $Date: 2012/05/02 06:08:32 $
00024  * $Revision: 1.63 $
00025  * $Name: HEAD $
00026  * $Log: uves_merge.c,v $
00027  * Revision 1.63  2012/05/02 06:08:32  amodigli
00028  * replace Ang by Angstrom
00029  *
00030  * Revision 1.62  2012/03/02 16:53:31  amodigli
00031  * fixed warning related to upgrade to CPL6
00032  *
00033  * Revision 1.61  2011/12/08 14:03:09  amodigli
00034  * Fix warnings with CPL6
00035  *
00036  * Revision 1.60  2010/12/16 16:57:40  amodigli
00037  * fixed compiler warnings
00038  *
00039  * Revision 1.59  2010/12/08 11:07:59  amodigli
00040  * added chip parameter to uves_merge_orders() to have proper filenames. Fixed content err data for noappend case
00041  *
00042  * Revision 1.58  2010/09/27 06:32:22  amodigli
00043  * fixed mem leaks
00044  *
00045  * Revision 1.57  2010/09/24 09:32:04  amodigli
00046  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00047  *
00048  * Revision 1.55  2010/06/07 09:48:00  amodigli
00049  * changed units: A-->Ang, FLUX-->ADU
00050  *
00051  * Revision 1.54  2010/06/01 16:06:55  amodigli
00052  * added unit [A] to reduced product
00053  *
00054  * Revision 1.53  2008/09/29 06:57:41  amodigli
00055  * add #include <string.h>
00056  *
00057  * Revision 1.52  2008/09/27 16:05:12  amodigli
00058  * fixed bug using delta parmeters
00059  *
00060  * Revision 1.51  2008/09/23 11:32:39  amodigli
00061  * added check on array upper bound access to fix DFS05803
00062  *
00063  * Revision 1.50  2008/08/29 09:52:39  amodigli
00064  * fixed compiler warning
00065  *
00066  * Revision 1.49  2008/06/26 08:30:38  amodigli
00067  * fixed bug in setting delta
00068  *
00069  * Revision 1.48  2008/06/11 14:42:50  amodigli
00070  * fixed seg fault
00071  *
00072  * Revision 1.45  2008/03/04 07:35:31  amodigli
00073  * generate spectra of each order only if NOAPPEND
00074  *
00075  * Revision 1.44  2008/02/21 07:50:38  amodigli
00076  * added method NOAPPEND
00077  *
00078  * Revision 1.43  2007/08/21 13:08:26  jmlarsen
00079  * Removed irplib_access module, largely deprecated by CPL-4
00080  *
00081  * Revision 1.42  2007/06/21 11:28:57  jmlarsen
00082  * Added support for type float (for FLAMES)
00083  *
00084  * Revision 1.41  2007/06/06 08:17:33  amodigli
00085  * replace tab with 4 spaces
00086  *
00087  * Revision 1.40  2007/05/03 15:21:13  jmlarsen
00088  * Decreased output message verbosity
00089  *
00090  * Revision 1.39  2007/04/24 12:50:29  jmlarsen
00091  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00092  *
00093  * Revision 1.38  2007/02/09 08:57:54  jmlarsen
00094  * Include <float.h>
00095  *
00096  * Revision 1.37  2007/02/09 08:14:16  jmlarsen
00097  * Do not use CPL_PIXEL_MAXVAL which works only for integer images
00098  *
00099  * Revision 1.36  2007/02/08 07:34:28  jmlarsen
00100  * Minor doc change
00101  *
00102  * Revision 1.35  2006/11/15 15:02:14  jmlarsen
00103  * Implemented const safe workarounds for CPL functions
00104  *
00105  * Revision 1.33  2006/11/15 14:04:08  jmlarsen
00106  * Removed non-const version of parameterlist_get_first/last/next which is already
00107  * in CPL, added const-safe wrapper, unwrapper and deallocator functions
00108  *
00109  * Revision 1.32  2006/11/13 14:23:55  jmlarsen
00110  * Removed workarounds for CPL const bugs
00111  *
00112  * Revision 1.31  2006/11/06 15:19:41  jmlarsen
00113  * Removed unused include directives
00114  *
00115  * Revision 1.30  2006/09/11 14:00:11  jmlarsen
00116  * Minor documentation change
00117  *
00118  * Revision 1.29  2006/08/17 13:56:53  jmlarsen
00119  * Reduced max line length
00120  *
00121  * Revision 1.28  2006/08/17 09:16:47  jmlarsen
00122  * Removed CPL2 code
00123  *
00124  * Revision 1.27  2006/08/11 14:37:59  jmlarsen
00125  * Added input validation
00126  *
00127  * Revision 1.26  2006/08/10 10:50:12  jmlarsen
00128  * Removed workaround for cpl_image_get_bpm
00129  *
00130  * Revision 1.25  2006/07/03 13:16:42  jmlarsen
00131  * Reduced number of significant digits in message
00132  *
00133  * Revision 1.24  2006/04/06 08:38:56  jmlarsen
00134  * Changed char* -> const char* for static string
00135  *
00136  * Revision 1.23  2006/03/03 13:54:11  jmlarsen
00137  * Changed syntax of check macro
00138  *
00139  * Revision 1.22  2006/02/03 07:46:30  jmlarsen
00140  * Moved recipe implementations to ./uves directory
00141  *
00142  * Revision 1.21  2006/01/31 08:24:29  jmlarsen
00143  * Wrapper for cpl_image_get_bpm
00144  *
00145  * Revision 1.20  2006/01/25 16:13:20  jmlarsen
00146  * Changed interface of gauss.fitting routine
00147  *
00148  * Revision 1.19  2005/12/19 16:17:56  jmlarsen
00149  * Replaced bool -> int
00150  *
00151  * Revision 1.18  2005/12/16 14:22:23  jmlarsen
00152  * Removed midas test data; Added sof files
00153  *
00154  * Revision 1.17  2005/11/24 15:09:06  jmlarsen
00155  * Implemented 2d extraction/rebinning/merging
00156  *
00157  * Revision 1.16  2005/11/24 11:54:46  jmlarsen
00158  * Added support for CPL 3 interface
00159  *
00160  * Revision 1.15  2005/11/18 10:52:06  jmlarsen
00161  * Split into optimal/sum merge methods
00162  *
00163  * Revision 1.14  2005/11/11 13:18:54  jmlarsen
00164  * Reorganized code, renamed source files
00165  *
00166  */
00167 
00168 #ifdef HAVE_CONFIG_H
00169 #  include <config.h>
00170 #endif
00171 
00172 /*----------------------------------------------------------------------------*/
00178 /*----------------------------------------------------------------------------*/
00182 /*-----------------------------------------------------------------------------
00183                                 Includes
00184  -----------------------------------------------------------------------------*/
00185 
00186 #include <uves_merge.h>
00187 
00188 #include <uves_pfits.h>
00189 #include <uves_utils.h>
00190 #include <uves_utils_wrappers.h>
00191 #include <uves_dump.h>
00192 #include <uves_dfs.h>
00193 #include <uves_error.h>
00194 
00195 #include <cpl.h>
00196 #include <float.h>
00197 #include <string.h>
00198 /*-----------------------------------------------------------------------------
00199                             Functions prototypes
00200  ----------------------------------------------------------------------------*/
00201 
00202 /*-----------------------------------------------------------------------------
00203                             Implementation
00204  ----------------------------------------------------------------------------*/
00205 
00206 /*---------------------------------------------------------------------------*/
00233 /*---------------------------------------------------------------------------*/
00234 
00235 cpl_image *
00236 uves_merge_orders(const cpl_image *spectrum, 
00237                   const cpl_image *spectrum_noise,
00238                   const uves_propertylist *spectrum_header,
00239                   merge_method m_method,
00240                   int n_traces,
00241                   uves_propertylist **merged_header,
00242                   const double delt1, 
00243                   const double delt2,
00244                   enum uves_chip chip,
00245                   cpl_image **merged_noise)
00246 {
00247     cpl_image *merged = NULL;    /* Result */
00248 
00249     const double *spectrum_data_double = NULL;
00250     const float  *spectrum_data_float  = NULL;
00251     const cpl_mask *spectrum_badmap    = NULL;
00252     const cpl_binary *spectrum_bad     = NULL;
00253 
00254     const double *noise_data_double = NULL;
00255     const float *noise_data_float   = NULL;
00256     const cpl_mask *noise_badmap    = NULL;
00257     const cpl_binary *noise_bad     = NULL;
00258     cpl_type type;               /* input/output images type */
00259 
00260     int nbins, ny, norders;          /* Input image size. ny = norders*n_traces */
00261     double wavestep;
00262     int bin_min = 0, bin_max = 0;    /* wavelength of min/max bin in units of 'wavestep' */
00263     int total_bins;
00264     int order, trace;
00265     int spectrum_sx=0;
00266     int spectrum_sy=0;
00267     double delt1_bin=0;
00268     double delt2_bin=0;
00269     char* filename=NULL;
00270 
00271     cpl_vector* image_1d=NULL;
00272     uves_propertylist* hext=NULL;
00273     //double med_noise=0;
00274     passure( spectrum != NULL, " ");
00275     passure( spectrum_noise != NULL, " ");
00276     passure( spectrum_header != NULL, " ");
00277     passure( merged_header != NULL, " ");
00278     passure( merged_noise != NULL, " ");
00279 
00280     assure( m_method == MERGE_OPTIMAL || 
00281             m_method == MERGE_SUM || 
00282             m_method == MERGE_NOAPPEND, 
00283             CPL_ERROR_ILLEGAL_INPUT,
00284             "Unknown merge method: %d", m_method);
00285     
00286     assure( cpl_image_get_type(spectrum) == CPL_TYPE_DOUBLE ||
00287             cpl_image_get_type(spectrum) == CPL_TYPE_FLOAT,
00288             CPL_ERROR_TYPE_MISMATCH,
00289             "Spectrum must have type double or float. It is '%s'",
00290             uves_tostring_cpl_type(cpl_image_get_type(spectrum)));
00291     
00292     assure( cpl_image_get_type(spectrum_noise) == CPL_TYPE_DOUBLE ||
00293             cpl_image_get_type(spectrum_noise) == CPL_TYPE_FLOAT,
00294             CPL_ERROR_TYPE_MISMATCH,
00295             "Spectrum noise must have type double. It is '%s'",
00296             uves_tostring_cpl_type(cpl_image_get_type(spectrum_noise)));
00297 
00298     assure( cpl_image_get_type(spectrum) ==
00299             cpl_image_get_type(spectrum_noise),
00300             CPL_ERROR_TYPE_MISMATCH,
00301             "Spectrum and spectrum noise must have same type. They are "
00302             "%s and %s, respectively",
00303             uves_tostring_cpl_type(cpl_image_get_type(spectrum)),
00304             uves_tostring_cpl_type(cpl_image_get_type(spectrum_noise)) );
00305 
00306     type = cpl_image_get_type(spectrum);
00307 
00308     /* Read input spectrum geometry */
00309     nbins           = cpl_image_get_size_x(spectrum);
00310     ny              = cpl_image_get_size_y(spectrum);
00311 
00312     assure( cpl_image_get_size_x(spectrum_noise) == nbins &&
00313             cpl_image_get_size_y(spectrum_noise) == ny,
00314             CPL_ERROR_INCOMPATIBLE_INPUT,
00315             "Incompatible spectrum/noise image sizes: %dx%d vs. %" CPL_SIZE_FORMAT "x%" CPL_SIZE_FORMAT "",
00316             nbins, ny,
00317             cpl_image_get_size_x(spectrum_noise),
00318             cpl_image_get_size_y(spectrum_noise) );
00319     
00320     assure( ny % n_traces == 0, CPL_ERROR_INCOMPATIBLE_INPUT,
00321             "Spectrum image height (%d) is not a multiple of "
00322             "the number of traces (%d). Confused, bailing out",
00323             ny, n_traces);
00324 
00325     norders         = ny / n_traces;
00326     
00327     check( wavestep = uves_pfits_get_cdelt1(spectrum_header),
00328        "Error reading bin width");
00329 
00330     /* Get data pointers (for efficiency) */
00331    
00332     if (type == CPL_TYPE_DOUBLE) {
00333         spectrum_data_double   = cpl_image_get_data_double_const(spectrum);
00334     }
00335     else {
00336         spectrum_data_float   = cpl_image_get_data_float_const(spectrum);
00337     }
00338 
00339     spectrum_sx=cpl_image_get_size_x(spectrum);
00340     spectrum_sy=cpl_image_get_size_y(spectrum);
00341 
00342     spectrum_badmap = cpl_image_get_bpm_const(spectrum);
00343     spectrum_bad    = cpl_mask_get_data_const(spectrum_badmap);
00344     
00345     if (type == CPL_TYPE_DOUBLE) {
00346         noise_data_double = cpl_image_get_data_double_const(spectrum_noise);        
00347     }
00348     else {
00349         noise_data_float  = cpl_image_get_data_float_const(spectrum_noise);
00350     }
00351     noise_badmap = cpl_image_get_bpm_const(spectrum_noise);
00352     noise_bad    = cpl_mask_get_data_const(noise_badmap);
00353     uves_msg("delt1=%f delt2=%f",delt1,delt2);
00354     /* Read max/min lambda */
00355     for (order = 1; order <= norders; order++)
00356     {
00357         double wstart, wend;
00358         check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00359            "Error reading start wavelength for order #%d", order);
00360 
00361         check( wend = uves_pfits_get_wend(spectrum_header, order),
00362            "Error reading end wavelength for order #%d", order);
00363         
00364         uves_msg_debug("Order #%d: wstart - wend = %f - %f wlu", order, wstart, wend);
00365 
00366     //wstart+=delt1;
00367     //wend-=delt2;
00368 
00369         if (order == 1)
00370         {
00371             bin_min = uves_round_double(wstart/wavestep);
00372             bin_max = uves_round_double(wend  /wavestep);
00373         }
00374         
00375         bin_min = uves_min_int(bin_min, uves_round_double(wstart/wavestep));
00376         bin_max = uves_max_int(bin_max, uves_round_double(wend  /wavestep));
00377     }
00378     total_bins = (bin_max - bin_min) + 1;
00379 
00380     uves_msg_debug("Merging orders into %d bins covering wavelengths %.3f - %.3f wlu", 
00381             total_bins, bin_min * wavestep, bin_max * wavestep);
00382 
00383     /* Initialize spectrum to zero and noise to negative */
00384     if(m_method == MERGE_NOAPPEND) {
00385       merged        = cpl_image_new(total_bins, n_traces*norders, type);
00386       *merged_noise = cpl_image_new(total_bins, n_traces*norders, type);
00387     } else {
00388       merged        = cpl_image_new(total_bins, n_traces, type);
00389       *merged_noise = cpl_image_new(total_bins, n_traces, type);
00390     }
00391     cpl_image_add_scalar(*merged_noise, -1.0);
00392     //cpl_image_power(spectrum_noise,0.5);
00393     //cpl_image_multiply_scalar(spectrum_noise,0.25);
00394     /* Distribute input in output bins */
00395     for (order = 1; order <= norders; order++)
00396     {
00397         double wstart, wend;
00398         int wstart_bin, wend_bin;      /* In 1d space */
00399 
00400 
00401 
00402         check( wstart = uves_pfits_get_wstart(spectrum_header, order),
00403            "Error reading start wavelength for order #%d", order);
00404         
00405         check( wend = uves_pfits_get_wend(spectrum_header, order),
00406            "Error reading end wavelength for order #%d", order);
00407 
00408     //wstart+=delt1;
00409     //wend-=delt2;
00410         
00411         wstart_bin = uves_round_double(wstart/wavestep);
00412         wend_bin   = uves_round_double(wend/wavestep);
00413         delt1_bin  = uves_round_double(delt1/wavestep);
00414         delt2_bin  = uves_round_double(delt2/wavestep);
00415 
00416         
00417     int bin_min_ord = uves_round_double(wstart/wavestep);
00418     int bin_max_ord = uves_round_double(wend  /wavestep);
00419         int nbins_ord = (bin_max_ord - bin_min_ord) + 1;
00420       cpl_image* merged_ord=NULL;
00421       cpl_image * noise_ord=NULL;
00422 
00423     if(m_method == MERGE_NOAPPEND) {
00424       merged_ord = cpl_image_new(nbins_ord, n_traces, type);
00425       noise_ord = cpl_image_new(nbins_ord, n_traces, type);
00426     }
00427 
00428         //if(order>1 && order<norders) {
00429     //  med_noise=cpl_image_get_median_window(spectrum_noise,
00430     //                  1,order,ny,order);
00431     //} else {
00432     //  med_noise=cpl_image_get_median(spectrum_noise);
00433     //}
00434         /* Loop over spatial traces (only 1 trace, unless extraction was 2d) */
00435         for (trace = 1; trace <= n_traces; trace++)
00436         {
00437       int merged_row = 0;
00438       int spectrum_row = (order - 1)*n_traces + trace;
00439       if(m_method == MERGE_NOAPPEND) {
00440         merged_row = (order - 1)*n_traces + trace;
00441       } else {
00442         merged_row = trace;
00443 
00444       }
00445             int rel_bin;                   /* Counting columns in input spectrum */
00446             
00447             for (rel_bin = 1+delt1_bin; 
00448                  (rel_bin <= wend_bin - wstart_bin + 1-delt2_bin) &&
00449                  (rel_bin <(spectrum_sx*spectrum_sy+1-(spectrum_row-1)*nbins));
00450                  rel_bin++)
00451             {
00452                 double flux, noise;
00453                 double current_flux, new_flux;
00454                 double current_noise, new_noise;
00455                 double weight;
00456                 int pis_rejected, noise_rejected;
00457                 
00458                 /* merged_bin = (offset of order)  +  (offset inside order) */
00459                 int merged_bin = (wstart_bin - bin_min) + rel_bin;
00460                 int merged_bin_ord = rel_bin;
00461                 
00462                 passure(1 <= merged_bin && merged_bin <= total_bins,
00463                     "%d %d %d", rel_bin, merged_bin, total_bins);
00464                 
00465                 /* This is slow:
00466                    check( flux  = cpl_image_get(spectrum      , 
00467                           rel_bin, spectrum_row, &pis_rejected);
00468                    noise = cpl_image_get(spectrum_noise, rel_bin, 
00469                     spectrum_row, &noise_rejected),
00470                    "Error reading input spectrum");
00471                 */
00472                 
00473                 if (type == CPL_TYPE_DOUBLE) {
00474                     flux  = spectrum_data_double[(rel_bin-1) + (spectrum_row-1) * nbins];
00475                     noise = noise_data_double   [(rel_bin-1) + (spectrum_row-1) * nbins];
00476 
00477                 }
00478                 else {
00479                     flux  = spectrum_data_float[(rel_bin-1) + (spectrum_row-1) * nbins];
00480                     noise = noise_data_float   [(rel_bin-1) + (spectrum_row-1) * nbins];
00481                 }
00482 
00483                 pis_rejected   = spectrum_bad[(rel_bin-1) + (spectrum_row-1) * nbins];
00484                 noise_rejected = noise_bad   [(rel_bin-1) + (spectrum_row-1) * nbins];
00485                 
00486                 if (!pis_rejected && !noise_rejected)
00487                 {
00488 
00489           if(m_method == MERGE_NOAPPEND) {
00490                     check(( current_flux  = cpl_image_get(merged, 
00491                                                           merged_bin, 
00492                                                           merged_row, 
00493                               &pis_rejected),
00494                 current_noise = cpl_image_get(*merged_noise, 
00495                               merged_bin, 
00496                                                           merged_row, 
00497                               &pis_rejected)),
00498               "Error reading merged spetrum");
00499           } else {
00500                     check(( current_flux  = cpl_image_get(
00501                         merged      , merged_bin, trace, &pis_rejected),
00502                         current_noise = cpl_image_get(
00503                         *merged_noise, merged_bin, trace, &pis_rejected)),
00504                        "Error reading merged spetrum");
00505           }
00506           weight = 1/(noise*noise);
00507                     
00508                     /*
00509                      * Optimal formulas for Variance and Flux are
00510                      *
00511                      *   Vn = ( 1/sigma1^2 + ... +  1/sigmaN^2)^-1
00512                      *   Fn = (f1/sigma1^2 + ... + fN/sigmaN^2) * Vn
00513                      *
00514                      * Update by using these recurrence relations
00515                      *
00516                      *   Fn+1 = (Fn/Vn + fn+1/(sigma_{n+1})^2) * Vn+1  for n > 1
00517                      *   Vn+1 = (Vn^-1 + 1/(sigma_{n+1})^2)^-1  for n > 1
00518                      *
00519                      *
00520                      *  In the case of method = sum,
00521                      *
00522                      *  Vn = sigma1^2 + ... + sigmaN^2
00523                      *  Fn = f1 + ... + fN
00524                      *
00525                      */
00526                     
00527                     if (current_noise > 0)
00528                     {
00529                         if (m_method == MERGE_OPTIMAL)
00530                         {
00531                             new_noise  = 1/(current_noise*current_noise);
00532                             new_noise += weight;
00533                             new_noise  = 1/sqrt(new_noise);
00534                         }
00535                         else if (m_method == MERGE_SUM)
00536                         {
00537                             new_noise = sqrt(current_noise*current_noise
00538                                      + noise*noise);
00539                         }
00540                         else if (m_method == MERGE_NOAPPEND)
00541                         {
00542                             new_noise = current_noise;
00543                         }
00544                         else
00545                         {
00546                             /* Impossible */
00547                             passure( false, "%d", m_method);
00548                         }
00549                     }
00550                     else
00551                     {
00552                         /* First time in this bin */
00553                         new_noise = noise;
00554                     }
00555                     
00556                     if (current_noise > 0)
00557                     {
00558                         if (m_method == MERGE_OPTIMAL)
00559                         {
00560                             new_flux = (current_flux / 
00561                                 (current_noise*current_noise)
00562                                 + flux * weight) * 
00563                             (new_noise*new_noise);
00564                         }
00565                         else if (m_method == MERGE_SUM)
00566                         {
00567                             new_flux = current_flux + flux;
00568                         }
00569                         else if (m_method == MERGE_NOAPPEND)
00570               {
00571                             new_flux = flux;
00572               }
00573                         else
00574                         {
00575                             /* Impossible */
00576                             passure( false, "%d", m_method);
00577                         }
00578                     }
00579                     else
00580                     {
00581                         new_flux = flux;
00582                     }
00583                     
00584           if(m_method == MERGE_NOAPPEND) {
00585 
00586             /*
00587               uves_msg_warning("flux[%d,%d]=%g noise=%g",
00588                        merged_bin,merged_row,
00589                        new_flux,current_noise);
00590             */
00591                     check( cpl_image_set(
00592                      merged, 
00593                                          merged_bin, 
00594                                          merged_row, 
00595                                          new_flux),
00596                        "Error updating merged spectrum");
00597 
00598                     check( cpl_image_set(
00599                      *merged_noise, 
00600                                           merged_bin, 
00601                      merged_row, 
00602                                          new_noise),
00603                        "Error updating weights");
00604 
00605 
00606                     check( cpl_image_set(
00607                      merged_ord, 
00608                                          merged_bin_ord, 
00609                                          trace, 
00610                                          new_flux),
00611                        "Error updating merged spectrum");
00612 
00613                     check( cpl_image_set(
00614                      noise_ord, 
00615                                          merged_bin_ord, 
00616                                          trace, 
00617                                          new_noise),
00618                        "Error updating merged spectrum");
00619 
00620 
00621           } else {
00622                     check( cpl_image_set(
00623                            merged      , merged_bin, trace, new_flux),
00624                        "Error updating merged spectrum");
00625                     check( cpl_image_set(
00626                            *merged_noise, merged_bin, trace, new_noise),
00627                        "Error updating weights");
00628           }
00629 /*            uves_msg("Input flux = %e +- %e ;  
00630             Binned flux changed from %e +- %e to %e +- %e",
00631             flux, noise,
00632             current_flux, current_noise, new_flux, new_noise);*/
00633                     
00634                 } /* If pixel is good ... */
00635 
00636             } /* For each input bin */
00637 
00638         }/* For trace ... */
00639 
00640 
00641     if (merged_header == NULL)
00642         {
00643            uves_free_propertylist(merged_header);
00644            check( *merged_header = uves_initialize_image_header(
00645                      "WAVELENGTH", (n_traces > 1) ? "PIXEL" : " ", "FLUX",
00646                      bin_min_ord * wavestep, 1.0,
00647                      1.0, 1.0,
00648                      wavestep, 1.0),
00649                   "Error initializing merged spectrum header");
00650         }
00651 
00652         if(m_method == MERGE_NOAPPEND) {
00653            filename=uves_sprintf("merged_data_noappend_%s.fits",
00654                            uves_chip_tostring_upper(chip));
00655            image_1d = cpl_vector_wrap(cpl_image_get_size_x(merged_ord),
00656                                       cpl_image_get_data_double(merged_ord));
00657            uves_free_propertylist(&hext);
00658            hext=uves_propertylist_new();
00659            uves_propertylist_append_double(hext,"CRVAL1",wstart-delt1);
00660            uves_propertylist_append_double(hext,"CDELT1",wavestep);
00661            uves_propertylist_append_double(hext,"CRPIX1",0);
00662 
00663            if(order==1) {
00664             uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext, 
00665                                     CPL_IO_DEFAULT);
00666            } else {
00667             uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext, 
00668                                     CPL_IO_EXTEND);
00669            }
00670            cpl_vector_unwrap(image_1d);
00671 
00672            image_1d = cpl_vector_wrap(cpl_image_get_size_x(noise_ord),
00673                                       cpl_image_get_data_double(noise_ord));
00674 
00675            uves_free(filename);
00676       
00677            filename=uves_sprintf("merged_sigma_noappend_%s.fits",
00678                                  uves_chip_tostring_upper(chip));
00679            if(order==1) {
00680             uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext, 
00681                                     CPL_IO_DEFAULT);
00682            } else {
00683             uves_vector_save(image_1d,filename,CPL_BPP_IEEE_FLOAT,hext, 
00684                                     CPL_IO_EXTEND);
00685            }
00686            cpl_vector_unwrap(image_1d);
00687 
00688            uves_free(filename);
00689            uves_free_image(&merged_ord);
00690            uves_free_image(&noise_ord);
00691            uves_free_propertylist(&hext);
00692            uves_free_propertylist(merged_header);
00693         }
00694 
00695     }/* For order ... */
00696     
00697     /* Undefined bins have  (flux, noise) = (0, -1)  (the initial values).
00698      * Set to (flux, noise) = (0, 1)
00699      */
00700 
00701 /* Here commented out piece of code to be used eventually to have
00702    noappend files files as products
00703     if(m_method == MERGE_NOAPPEND) {
00704        cpl_frame* mdata_noappend=NULL;
00705        cpl_frame* msigma_noappend=NULL;
00706        mdata_noappend=cpl_frame_new();
00707        msigma_noappend=cpl_frame_new();
00708        cpl_frame_set_group(mdata_noappend,CPL_FRAME_GROUP_PRODUCT);
00709        cpl_frame_set_type(mdata_noappend,CPL_FRAME_TYPE_IMAGE);
00710        cpl_frame_set_level(mdata_noappend,CPL_FRAME_LEVEL_FINAL);
00711 
00712        tag=uves_sprintf("MERGED_SCI_NOAPPEND_%s",
00713                              uves_chip_tostring_upper(chip));
00714        filename=uves_sprintf("%s_%s.fits",tag,uves_chip_tostring_upper(chip));
00715        cpl_frame_set_tag(mdata_noappend,tag);
00716        cpl_frame_set_filename(mdata_noappend,filename);
00717        uves_free(filename);
00718        uves_free(tag);
00719 
00720        cpl_frame_set_group(msigma_noappend,CPL_FRAME_GROUP_PRODUCT);
00721        cpl_frame_set_type(msigma_noappend,CPL_FRAME_TYPE_IMAGE);
00722        cpl_frame_set_level(msigma_noappend,CPL_FRAME_LEVEL_FINAL);
00723 
00724        tag=uves_sprintf("MERGED_ERR_NOAPPEND_%s",
00725                              uves_chip_tostring_upper(chip));
00726        filename=uves_sprintf("%s_%s.fits",tag,uves_chip_tostring_upper(chip));
00727        cpl_frame_set_tag(msigma_noappend,tag);
00728        cpl_frame_set_filename(msigma_noappend,filename);
00729 
00730        uves_free_frame(&mdata_noappend);
00731        uves_free_frame(&msigma_noappend);
00732        uves_free(filename);
00733        uves_free(tag);
00734 
00735     }
00736 */
00737     check( cpl_image_threshold(*merged_noise, 
00738                    0, DBL_MAX,        /* Outside this interval */
00739                    1, 1),             /* Set to these values   */
00740        "Error setting undefined noise");
00741  
00742     
00743     if (merged_header != NULL)
00744     {
00745         check( *merged_header = uves_initialize_image_header(
00746                "WAVELENGTH [Angstrom]", (n_traces > 1) ? "PIXEL" : " ", "ADU",
00747                bin_min * wavestep, 1.0,
00748                1.0, 1.0,
00749                wavestep, 1.0),
00750            "Error initializing merged spectrum header");
00751     }
00752  
00753     
00754   cleanup:
00755     return merged;
00756 }
00757 
00758 /*---------------------------------------------------------------------------*/
00768 /*---------------------------------------------------------------------------*/
00769 merge_method
00770 uves_get_merge_method(const cpl_parameterlist *parameters, 
00771                       const char *context, 
00772                       const char *subcontext)
00773 {
00774     const char *mm = "";
00775     merge_method result = 0;
00776     
00777     check( uves_get_parameter(parameters, context, subcontext, "merge", CPL_TYPE_STRING, &mm),
00778        "Could not read parameter");
00779     
00780     if      (strcmp(mm, "optimal") == 0) result = MERGE_OPTIMAL;
00781     else if (strcmp(mm, "sum"    ) == 0) result = MERGE_SUM;
00782     else if (strcmp(mm, "noappend") == 0) result = MERGE_NOAPPEND;
00783     else
00784     {
00785         assure(false, CPL_ERROR_ILLEGAL_INPUT, "No such merging method: '%s'", mm);
00786     }
00787     
00788   cleanup:
00789     return result;
00790 }
00791     
00792 

Generated on 3 Mar 2013 for UVES Pipeline Reference Manual by  doxygen 1.6.1