uves_wavecal_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: 2010/09/24 09:32:10 $
00023  * $Revision: 1.13 $
00024  * $Name: uves-4_9_1 $
00025  * $Log: uves_wavecal_utils.c,v $
00026  * Revision 1.13  2010/09/24 09:32:10  amodigli
00027  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00028  *
00029  * Revision 1.11  2007/06/06 08:17:34  amodigli
00030  * replace tab with 4 spaces
00031  *
00032  * Revision 1.10  2007/05/07 14:25:43  jmlarsen
00033  * Changed formatting
00034  *
00035  * Revision 1.9  2007/03/05 10:43:16  jmlarsen
00036  * Reject outliers based on line FWHM and fit residual
00037  *
00038  * Revision 1.8  2006/11/06 15:19:42  jmlarsen
00039  * Removed unused include directives
00040  *
00041  * Revision 1.7  2006/10/10 11:20:11  jmlarsen
00042  * Renamed line table columns to match MIDAS
00043  *
00044  * Revision 1.6  2006/07/14 12:52:57  jmlarsen
00045  * Exported/renamed function find_nearest
00046  *
00047  * Revision 1.5  2006/06/06 08:40:25  jmlarsen
00048  * Shortened max line length
00049  *
00050  * Revision 1.4  2006/06/01 14:43:17  jmlarsen
00051  * Added missing documentation
00052  *
00053  * Revision 1.3  2006/04/21 12:29:30  jmlarsen
00054  * Write QC parameters to line table
00055  *
00056  * Revision 1.2  2006/02/15 13:19:15  jmlarsen
00057  * Reduced source code max. line length
00058  *
00059  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00060  * Moved recipe implementations to ./uves directory
00061  *
00062  * Revision 1.3  2005/12/19 16:17:55  jmlarsen
00063  * Replaced bool -> int
00064  *
00065  * Revision 1.2  2005/11/14 13:18:44  jmlarsen
00066  * Minor update
00067  *
00068  * Revision 1.1  2005/11/11 13:18:54  jmlarsen
00069  * Reorganized code, renamed source files
00070  *
00071  */
00072 
00073 #ifdef HAVE_CONFIG_H
00074 #  include <config.h>
00075 #endif
00076 
00077 /*----------------------------------------------------------------------------*/
00081 /*----------------------------------------------------------------------------*/
00084 /*-----------------------------------------------------------------------------
00085                                 Includes
00086  -----------------------------------------------------------------------------*/
00087 
00088 #include <uves_wavecal_utils.h>
00089 #include <uves_utils.h>
00090 #include <uves_utils_wrappers.h>
00091 #include <uves_error.h>
00092 #include <uves_msg.h>
00093 
00094 #include <cpl.h>
00095 
00096 /*-----------------------------------------------------------------------------
00097                             Functions prototypes
00098  -----------------------------------------------------------------------------*/
00099 
00100 /*-----------------------------------------------------------------------------
00101                             Implementation
00102  -----------------------------------------------------------------------------*/
00103 /*----------------------------------------------------------------------------*/
00111 /*----------------------------------------------------------------------------*/
00112 
00113 lt_type *uves_lt_new(int windows, int traces)
00114 {
00115     /* Allocate all line tables for this chip */
00116     lt_type *lt = cpl_malloc(sizeof(lt_type));
00117 
00118     assure_mem( lt );
00119     
00120     lt->windows = windows;
00121     lt->traces = traces;
00122 
00123     /* Initialize pointer to NULL */
00124     lt->table                = cpl_calloc(windows*traces, sizeof(cpl_table *));
00125     lt->dispersion_relation  = cpl_calloc(windows*traces, sizeof(polynomial *));
00126     lt->absolute_order       = cpl_calloc(windows*traces, sizeof(polynomial *));
00127     lt->first_absolute_order = cpl_calloc(windows*traces, sizeof(int));
00128     lt->last_absolute_order  = cpl_calloc(windows*traces, sizeof(int));
00129 
00130   cleanup:
00131     return lt;
00132 }
00133 
00134 /*----------------------------------------------------------------------------*/
00139 /*----------------------------------------------------------------------------*/
00140 
00141 void uves_lt_delete(lt_type **lt)
00142 {
00143     if (lt != NULL && *lt != NULL)
00144     {
00145         int i;
00146         for (i = 0; i < (*lt)->windows * (*lt)->traces; i++)
00147         {
00148             uves_free_table       (&((*lt)->table[i]));
00149             uves_polynomial_delete(&((*lt)->dispersion_relation[i]));
00150             uves_polynomial_delete(&((*lt)->absolute_order[i]));
00151         }
00152 
00153         cpl_free((*lt)->table);
00154         cpl_free((*lt)->dispersion_relation);
00155         cpl_free((*lt)->absolute_order);
00156         cpl_free((*lt)->first_absolute_order);
00157         cpl_free((*lt)->last_absolute_order);
00158         
00159         cpl_free(*lt);
00160     }
00161 
00162     return;
00163 }
00164 
00165 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 cpl_table **
00175 uves_lt_get_table(const lt_type *lt, int window, int trace)
00176 {
00177     return &(lt->table[trace + (window-1)*lt->traces]);
00178 }
00179 
00180 /*----------------------------------------------------------------------------*/
00188 /*----------------------------------------------------------------------------*/
00189 polynomial **
00190 uves_lt_get_disprel(const lt_type *lt, int window, int trace)
00191 {
00192     return &(lt->dispersion_relation[trace + (window-1)*lt->traces]);
00193 }
00194 /*----------------------------------------------------------------------------*/
00202 /*----------------------------------------------------------------------------*/
00203 polynomial **
00204 uves_lt_get_absord(const lt_type *lt, int window, int trace)
00205 {
00206     return &(lt->absolute_order[trace + (window-1)*lt->traces]);
00207 }
00208 /*----------------------------------------------------------------------------*/
00216 /*----------------------------------------------------------------------------*/
00217 int *
00218 uves_lt_get_firstabs(const lt_type *lt, int window, int trace)
00219 {
00220     return &(lt->first_absolute_order[trace + (window-1)*lt->traces]);
00221 }
00222 /*----------------------------------------------------------------------------*/
00230 /*----------------------------------------------------------------------------*/
00231 int *
00232 uves_lt_get_lastabs(const lt_type *lt, int window, int trace)
00233 {
00234     return &(lt->last_absolute_order[trace + (window-1)*lt->traces]);
00235 }
00236 
00237 /*----------------------------------------------------------------------------*/
00253 /*----------------------------------------------------------------------------*/
00254 int
00255 uves_wavecal_find_nearest(const cpl_table *line_refer, double lambda, int lo, int hi)
00256 {
00257     if (lo == hi) /* One-row interval */
00258     {
00259         return lo;
00260     }
00261     else if (lo + 1 == hi)
00262     {  /* Two-row interval */
00263         double llo, lhi;
00264         lhi = cpl_table_get_double(line_refer, "Wave", hi, NULL);
00265         llo = cpl_table_get_double(line_refer, "Wave", lo, NULL);
00266         
00267         /* Return the one of 'llo' and 'lhi' that is closest to 'lambda' */
00268         return ((llo-lambda)*(llo-lambda) < (lhi-lambda)*(lhi-lambda)) ? lo : hi;
00269     }
00270     else
00271     { /* Three or more rows to consider */ 
00272         double lmid;
00273         int mid = (lo + hi)/2;
00274         /* mid is different from both 'lo' and 'hi', so this will terminate */
00275         
00276         lmid = cpl_table_get_double(line_refer, "Wave", mid, NULL);
00277         
00278         if (lmid < lambda)
00279         {
00280             return uves_wavecal_find_nearest(line_refer, lambda, mid, hi);
00281         }
00282         else
00283         {
00284             return uves_wavecal_find_nearest(line_refer, lambda, lo, mid);
00285         }
00286     }
00287 }
00288 
00289 
00290 /*----------------------------------------------------------------------------*/
00305 /*----------------------------------------------------------------------------*/
00306 int
00307 uves_delete_bad_lines(cpl_table *table, double TOLERANCE, double kappa)
00308 {    
00309     int result = 0;
00310     int numb_lines = cpl_table_get_nrow(table);
00311 
00312     /* Delete rows with invalid 'Ident' (and therefore invalid 'Residual_xxx') */
00313     check( uves_erase_invalid_table_rows(table, "Ident"),
00314        "Error erasing un-identified lines");
00315 
00316     assure( cpl_table_has_column(table, "Residual_pix"),
00317             CPL_ERROR_DATA_NOT_FOUND,
00318             "Missing column 'Residual_pix'");
00319 
00320     assure( cpl_table_has_column(table, LINETAB_RESIDUAL),
00321             CPL_ERROR_DATA_NOT_FOUND,
00322             "Missing column '" LINETAB_RESIDUAL "'");
00323 
00324     if (TOLERANCE > 0)
00325     {   
00326             /* Pixel units */
00327         check(( uves_erase_table_rows(table,        /*  >  tol  */
00328                       "Residual_pix", CPL_GREATER_THAN, TOLERANCE),
00329             uves_erase_table_rows(table,        /*  < -tol */
00330                       "Residual_pix", CPL_LESS_THAN   , -TOLERANCE)),
00331             "Error removing rows");
00332     }
00333     else
00334     {  
00335             /* Wavelength units */
00336         check(( uves_erase_table_rows(table,       /*  >  |tol| */
00337                       LINETAB_RESIDUAL, CPL_GREATER_THAN, -TOLERANCE),
00338             uves_erase_table_rows(table,       /*  < -|tol| */
00339                       LINETAB_RESIDUAL, CPL_LESS_THAN   , TOLERANCE)),
00340           "Error removing rows");
00341     }
00342 
00343 
00344     /* Also reject outliers (if more than 2 points)
00345        by kappa sigma clipping                       */
00346 
00347     if (cpl_table_get_nrow(table) - cpl_table_count_invalid(table, "Residual_pix") >= 2)
00348         {
00349             check_nomsg( uves_average_reject(table,
00350                                              "Residual_pix", "temp",
00351                                              kappa));
00352 
00353             check_nomsg( uves_average_reject(table,
00354                                              "Xwidth", "temp",
00355                                              kappa));
00356 
00357 
00358             /* Don't do this:
00359                the same is achieved by detecting fewer
00360                lines from the start 
00361             check_nomsg( uves_erase_table_rows(table,
00362                                                "Peak",
00363                                                CPL_LESS_THAN,
00364                                                0.5 *
00365                                                cpl_table_get_column_median(table, "Peak")));
00366             */
00367         }
00368     
00369     result = numb_lines - cpl_table_get_nrow(table);
00370 
00371   cleanup:
00372     return result;
00373 }
00374 
00375 /*----------------------------------------------------------------------------*/
00405 /*----------------------------------------------------------------------------*/
00406 cpl_error_code
00407 uves_draw_lines(cpl_image *image, polynomial *dispersion, 
00408         const polynomial *order_locations, const cpl_table *t, 
00409         const char *lambda_column, const char *abs_order, 
00410         const int *relative_order, int minorder, int maxorder,
00411         bool vertical, int offset)
00412 {
00413     int nx, ny;
00414     int row;
00415     
00416     /* Check input */
00417     passure( image != NULL, " ");
00418     passure( dispersion != NULL, " ");
00419     passure( uves_polynomial_get_dimension(dispersion) == 2, "%d", 
00420          uves_polynomial_get_dimension(dispersion));
00421     passure( order_locations != NULL, " ");
00422     passure( uves_polynomial_get_dimension(order_locations) == 2, "%d",
00423          uves_polynomial_get_dimension(order_locations));
00424     passure( t != NULL, " ");
00425     passure( cpl_table_has_column(t, lambda_column), "%s", lambda_column);
00426     if (abs_order != NULL)
00427     {
00428         passure( cpl_table_has_column(t, abs_order), "%s", abs_order);
00429     }
00430 
00431     nx = cpl_image_get_size_x(image);
00432     ny = cpl_image_get_size_y(image);
00433     
00434     for (row = 0; row < cpl_table_get_nrow(t); row++) {
00435     double x, xguess;
00436     double lambda, lambda_left, lambda_right;
00437     int order;
00438     
00439     check( lambda    = cpl_table_get_double(t, lambda_column, row, NULL),
00440            "Error reading table");
00441     
00442     if (abs_order != NULL)
00443         check (minorder = maxorder = cpl_table_get_int(t, abs_order, row, NULL),
00444            "Error reading table");
00445     
00446     for (order = minorder; order <= maxorder; order++) {
00447         lambda_left  = uves_polynomial_evaluate_2d(dispersion, 1 , order)/order;
00448         lambda_right = uves_polynomial_evaluate_2d(dispersion, nx, order)/order;
00449         
00450         /* Solve  dispersion(x, m) = ident*m */
00451         xguess = 1 + (nx - 1) * (lambda - lambda_left)/(lambda_right - lambda_left);
00452             /* Simple linear interpolation */
00453         
00454         /* Skip if lambda is not in this order */
00455         if (1 <= xguess && xguess <= nx) {
00456         x = uves_polynomial_solve_2d(dispersion, lambda*order, xguess,
00457                          1,                 /* multiplicity */
00458                          2,                 /* fix this variable number */
00459                          order);            /* ... to this value */
00460         
00461         /* Ignore if solve failed */
00462         if (cpl_error_get_code() != CPL_ERROR_NONE)
00463             {
00464             uves_error_reset();
00465             }
00466         else {
00467             /* Otherwise plot the solution */
00468             uves_msg_debug("lambda(x=%f)\t = %f", x     ,
00469                    uves_polynomial_evaluate_2d(dispersion, x     , order)/order);
00470             uves_msg_debug("lambda(x0=%f)\t = %f", xguess,
00471                    uves_polynomial_evaluate_2d(dispersion, xguess, order)/order);
00472             
00473             if (1 <= x && x <= nx) {
00474             double y = uves_polynomial_evaluate_2d(order_locations, x,
00475                                    relative_order[order]);
00476             int i;
00477             
00478             for (i = -3; i <= 3; i++) {
00479                 if (vertical) {
00480                 check( cpl_image_set(image,
00481                              uves_min_int(nx, uves_max_int(1, x    )),
00482                              uves_min_int(ny, uves_max_int(1, (int) y + i +
00483                                            offset)), 0),
00484                     "Error writing image");
00485                 }
00486                 else {
00487                 check( cpl_image_set(image, 
00488                              uves_min_int(nx, uves_max_int(1, x - i)),
00489                              uves_min_int(ny, uves_max_int(1, (int) y +
00490                                            offset)), 0),
00491                        "Error writing image");
00492                 }
00493             }
00494             }
00495         } /* Solve succeeded */
00496         } /* if lambda was inside this order */
00497         
00498     } /* for each order */
00499     
00500     } /* for each lambda */
00501     
00502   cleanup:
00503     return cpl_error_get_code();
00504 }
00505 

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1