uves_corrbadpix.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.21 $
00024  * $Name: uves-3_3_1 $
00025  * $Log: uves_corrbadpix.c,v $
00026  * Revision 1.21  2007/06/06 08:17:33  amodigli
00027  * replace tab with 4 spaces
00028  *
00029  * Revision 1.20  2007/04/24 12:50:29  jmlarsen
00030  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00031  *
00032  * Revision 1.19  2007/01/16 10:26:52  jmlarsen
00033  * Fixed calloc -> cpl_calloc
00034  *
00035  * Revision 1.18  2007/01/15 15:10:01  jmlarsen
00036  * Fixed memory bug in deallocator
00037  *
00038  * Revision 1.17  2007/01/15 13:57:20  jmlarsen
00039  * Fixed crashing BLUE chips
00040  *
00041  * Revision 1.16  2007/01/10 12:35:59  jmlarsen
00042  * Added uves_get_badpix
00043  *
00044  * Revision 1.15  2006/11/15 15:02:14  jmlarsen
00045  * Implemented const safe workarounds for CPL functions
00046  *
00047  * Revision 1.13  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.12  2006/11/06 15:19:41  jmlarsen
00052  * Removed unused include directives
00053  *
00054  * Revision 1.11  2006/08/17 13:56:52  jmlarsen
00055  * Reduced max line length
00056  *
00057  * Revision 1.10  2006/08/17 09:16:30  jmlarsen
00058  * Removed CPL2 code
00059  *
00060  * Revision 1.9  2006/08/14 12:15:26  jmlarsen
00061  * Update to CPL3
00062  *
00063  * Revision 1.8  2006/08/10 10:49:06  jmlarsen
00064  * Removed workaround for cpl_image_get_bpm
00065  *
00066  * Revision 1.7  2006/08/08 11:27:18  amodigli
00067  * upgrade to CPL3
00068  *
00069  * Revision 1.6  2006/07/07 06:45:23  amodigli
00070  * added doc
00071  *
00072  * Revision 1.5  2006/07/03 14:20:39  jmlarsen
00073  * Exclude bad pixels from order tracing
00074  *
00075  * Revision 1.4  2006/07/03 12:58:34  jmlarsen
00076  * Support flagging instead of interpolating bad pixels
00077  *
00078  * Revision 1.3  2006/04/06 08:29:56  jmlarsen
00079  * Include self
00080  *
00081  * Revision 1.2  2005/12/19 16:17:56  jmlarsen
00082  * Replaced bool -> int
00083  *
00084  * Revision 1.1  2005/11/11 13:18:54  jmlarsen
00085  * Reorganized code, renamed source files
00086  *
00087  */
00088 
00089 #ifdef HAVE_CONFIG_H
00090 #  include <config.h>
00091 #endif
00092 
00093 /*----------------------------------------------------------------------------*/
00099 /*----------------------------------------------------------------------------*/
00102 /*-----------------------------------------------------------------------------
00103                                 Includes
00104  -----------------------------------------------------------------------------*/
00105 
00106 #include "uves_corrbadpix.h"
00107 
00108 #include <uves_pfits.h>
00109 #include <uves_dump.h>
00110 #include <uves_error.h>
00111 #include <uves_msg.h>
00112 
00113 #include <irplib_access.h>
00114 #include <cpl.h>
00115 
00116 #include <stdbool.h>
00117 /*-----------------------------------------------------------------------------
00118                             Functions prototypes
00119  -----------------------------------------------------------------------------*/
00120 static int
00121 uves_correct_badpix(cpl_image *master_bias, uves_propertylist *header, int **badmap,
00122             bool mark_bad);
00123 
00124 /*-----------------------------------------------------------------------------
00125                             Implementation
00126  -----------------------------------------------------------------------------*/
00127 
00128 
00129 /*----------------------------------------------------------------------------*/
00146 /*----------------------------------------------------------------------------*/
00147 int
00148 uves_correct_badpix_all(cpl_image *master_bias, uves_propertylist *mbias_header,
00149             enum uves_chip chip,
00150             int binx, int biny, int mark_bad)
00151 {
00152     int badpixels_cleaned = -1; /* result */
00153 
00154     int **badmap = NULL;
00155 
00156     check( badmap = uves_get_badpix(chip, binx, biny, mark_bad),
00157        "Could not get bad pixel map");
00158 
00159     check( badpixels_cleaned = 
00160        uves_correct_badpix(master_bias, mbias_header, badmap, mark_bad),
00161        "Error cleaning bad pixels");
00162 
00163   cleanup:
00164     uves_badmap_free(&badmap);
00165     return badpixels_cleaned;
00166 }
00167 
00168 
00169 /*----------------------------------------------------------------------------*/
00174 /*----------------------------------------------------------------------------*/
00175 void
00176 uves_badmap_free(int ***badmap)
00177 {
00178     if (badmap != NULL)
00179     {
00180         if (*badmap != NULL)
00181         {
00182             int row;
00183             for (row = 0;
00184              (*badmap)[row][0] != -1;
00185              row++)
00186             {
00187                 cpl_free((*badmap)[row]);
00188             }
00189             cpl_free((*badmap)[row]);
00190 
00191             cpl_free(*badmap);
00192             *badmap = NULL;
00193         }
00194     }
00195 }
00196 
00197 /*----------------------------------------------------------------------------*/
00203 /*----------------------------------------------------------------------------*/
00204 static int **
00205 dup_map(int badmap[][4])
00206 {
00207     int **map = NULL;
00208     int row;
00209     bool finished = false;
00210 
00211     /* The execution time is O(n*n) which is okay, due to the low number
00212        of entries */
00213     
00214     for (row = 0; !finished; row++)
00215     {
00216         map = cpl_realloc(map, (row+1)*sizeof(int *));
00217         /* First time equivalent to: malloc(sizeof(int *)) */
00218 
00219         map[row] = cpl_calloc(4, sizeof(int));
00220         map[row][0] = badmap[row][0];
00221         map[row][1] = badmap[row][1];
00222         map[row][2] = badmap[row][2];
00223         map[row][3] = badmap[row][3];
00224 
00225         finished = (badmap[row][0] == -1);
00226     }
00227     
00228     return map;
00229 }
00230 
00231 
00232 /*----------------------------------------------------------------------------*/
00246 /*----------------------------------------------------------------------------*/
00247 int **
00248 uves_get_badpix(enum uves_chip chip, 
00249         int binx, int biny, int mark_bad)
00250 {
00251     int **map = NULL;
00252 
00253     if (chip == UVES_CHIP_REDL)
00254     {
00255         if (binx == 1 && biny == 1)
00256         {
00257             int badmap[][4] = {{1,4,2088,4},
00258                        {1,63,2282,63},
00259                        {1,108,1778,108},
00260                        {1,176,2443,176},
00261                        {1,196,2021,196},
00262                        {1,285,1974,285},
00263                        {1,352,1942,352},
00264                        {-1,-1,-1,-1}};
00265 
00266             map = dup_map(badmap);
00267         }
00268         else if (binx == 1 && biny == 2)
00269         {
00270             int badmap[][4] = {{1,4,1044,4},
00271                     {1,63,1141,63},
00272                     {1,108,894,108},
00273                     {1,176,1222,176},
00274                     {1,196,1011,196},
00275                     {1,285,988,285},
00276                     {1,352,971,352},
00277                     {-1,-1,-1,-1}};
00278 
00279             map = dup_map(badmap);
00280         }
00281         else if (binx == 2 && biny == 2)
00282         {
00283             int badmap[][4] = {{1,3,1044,3},
00284                        {1,33,1141,33},
00285                        {1,55,894,56},
00286                        {1,89,1222,90},
00287                        {1,99,1011,100},
00288                        {1,144,988,145},
00289                        {1,177,971,178},
00290                        {-1,-1,-1,-1}};
00291             map = dup_map(badmap);
00292         }
00293         else if (binx == 2 && biny == 3)
00294         {
00295             int badmap[][4] = {{1,3,696,3},
00296                        {1,33,761,33},
00297                        {1,55,592,56},
00298                        {1,89,814,90},
00299                        {1,99,674,100},
00300                        {1,144,658,144},
00301                        {1,177,647,178},
00302                        {-1,-1,-1,-1}};
00303             map = dup_map(badmap);
00304         }
00305         else
00306         {
00307             assure( false, CPL_ERROR_ILLEGAL_INPUT,
00308                 "Don't know bad pixel map for %dx%d binning, red, lower chip", 
00309                 binx, biny);
00310         }
00311     }
00312     else if  (chip == UVES_CHIP_REDU)
00313     {
00314         /* Use different bad pixels maps for bias and
00315            orderpos recipes !
00316         */
00317         if (binx == 1 && biny ==1)
00318         {
00319             int badmap[][4] = {{1,2030,1268,2033},
00320                        {1269,2033,4096,2033},
00321                        {1201, 491, 3271, 492},
00322                        {-1,-1,-1,-1}};
00323 
00324             if (!mark_bad)
00325             {
00326                 badmap[2][0] = -1;
00327                 badmap[2][1] = -1;
00328                 badmap[2][2] = -1;
00329                 badmap[2][3] = -1;
00330             }
00331             map = dup_map(badmap);
00332         }
00333         else if (binx == 1 && biny == 2)
00334         {
00335             int badmap[][4] = {{1,2030,634,2033},
00336                        {635,2033,2048,2033},
00337                        {600, 491,1635, 492},
00338                        {-1,-1,-1,-1}};
00339             if (!mark_bad)
00340             {
00341                 badmap[2][0] = -1;
00342                 badmap[2][1] = -1;
00343                 badmap[2][2] = -1;
00344                 badmap[2][3] = -1;
00345             }
00346             map = dup_map(badmap);
00347         }
00348         else if (binx == 2 && biny == 2)
00349         {
00350             int badmap[][4] = {{1,1013,634,1016},
00351                        {635,1015,2048,1016},
00352                        {600, 244,1635, 245},
00353                        {-1,-1,-1,-1}};
00354             if (!mark_bad)
00355             {
00356                 badmap[2][0] = -1;
00357                 badmap[2][1] = -1;
00358                 badmap[2][2] = -1;
00359                 badmap[2][3] = -1;
00360             }
00361             map = dup_map(badmap);
00362         }
00363         else if (binx == 2 && biny == 3)
00364         {
00365             int badmap[][4] = {{1,1013,423,1016},
00366                        {424,1015,1365,1016},
00367                        {400, 244,1090, 245},
00368                        {-1,-1,-1,-1}};
00369             if (!mark_bad)
00370             {
00371                 badmap[2][0] = -1;
00372                 badmap[2][1] = -1;
00373                 badmap[2][2] = -1;
00374                 badmap[2][3] = -1;
00375             }
00376             map = dup_map(badmap);
00377         }
00378         else
00379         {
00380             assure( false, CPL_ERROR_ILLEGAL_INPUT,
00381                 "Don't know bad pixel map for %dx%d binning, red, upper chip", 
00382                 binx, biny);
00383         }
00384     }
00385     else
00386     {
00387         /* No blue chip bad pixels */
00388         int badmap[][4] = {{-1,-1,-1,-1}};
00389         
00390         map = dup_map(badmap);
00391     }
00392 
00393   cleanup:
00394     return map;
00395 }
00396 
00397 
00398 
00399 /*----------------------------------------------------------------------------*/
00414 /*----------------------------------------------------------------------------*/
00415 static int
00416 uves_correct_badpix(cpl_image *master_bias, uves_propertylist *header, int **badmap,
00417             bool mark_bad)
00418 {
00419     int ncorrect = 0;   /* Result */
00420     int xstart, ystart, xend, yend;
00421     int row;
00422     bool finished = false;
00423     int nx, ny;
00424     cpl_mask  *image_bad = NULL;
00425     cpl_binary*image_bpm = NULL;
00426 
00427     assure( cpl_image_get_type(master_bias) == CPL_TYPE_DOUBLE,
00428         CPL_ERROR_UNSUPPORTED_MODE,
00429         "Image type must be double. It is %s",
00430         uves_tostring_cpl_type(cpl_image_get_type(master_bias)));
00431 
00432     image_bad = irplib_image_get_bpm(master_bias);
00433     image_bpm = irplib_mask_get_data(image_bad);
00434 
00435     nx = cpl_image_get_size_x(master_bias);
00436     ny = cpl_image_get_size_y(master_bias);
00437 
00438     row = 0;
00439     while (!finished)
00440     {
00441         xstart = badmap[row][0];
00442         ystart = badmap[row][1];
00443         xend   = badmap[row][2];
00444         yend   = badmap[row][3];
00445 
00446         if (xstart > 0)
00447         {
00448             int ylow, yhigh;
00449             int x, y;
00450             
00451             assure( 1 <= xstart && xstart <= nx &&
00452                 1 <= xend   && xend   <= nx &&
00453                 1 <= ystart && ystart <= ny &&
00454                 1 <= yend   && yend   <= ny, CPL_ERROR_ILLEGAL_INPUT, 
00455                 "Illegal window (%d, %d) - (%d, %d). Image size = %dx%d",
00456                 xstart, ystart, xend, yend, nx, ny);
00457             
00458             if ( ystart < 3 )
00459             {
00460                 assure( yend + 2 <= ny, CPL_ERROR_ILLEGAL_INPUT,
00461                     "Too large range in y: %d - %d", ystart, yend);
00462 
00463                 ylow  = yend + 1;
00464                 yhigh = yend + 2;
00465             }
00466             else if (yend > ny - 3 )
00467             {
00468                 assure( ystart - 2 >= 1, CPL_ERROR_ILLEGAL_INPUT,
00469                     "Too large range in y: %d - %d", ystart, yend);
00470                 
00471                 ylow  = ystart - 2;
00472                 yhigh = ystart - 1;
00473             }
00474             else
00475             {
00476                 ylow  = ystart - 2;
00477                 yhigh = yend + 2;
00478             }
00479             
00480             uves_msg("Correcting window (%d, %d)-(%d, %d)", xstart, ystart, xend, yend);
00481 
00482             for (x = xstart; x <= xend; x++) {
00483             for (y = ystart; y <= yend; y++) {
00484                 int pis_rejected;
00485                 double i1, i2;
00486                 
00487                 if (mark_bad)
00488                 {
00489                     /* This is extremely slow with CPL2:
00490                        cpl_image_reject(master_bias, x, y);
00491                     */
00492                     image_bpm[(x-1) + (y-1)*nx] = CPL_BINARY_1;
00493                 }
00494                 else
00495                 /* interpolate */
00496                 {
00497                     double *master_bias_data;
00498 
00499                     i1 = cpl_image_get(master_bias, x, ylow , &pis_rejected);
00500                     i2 = cpl_image_get(master_bias, x, yhigh, &pis_rejected);
00501                     
00502                     /* Write average */
00503                     
00504                     /* This will make the bpm invalid:
00505                        cpl_image_set(master_bias, x, y, (i1+i2)/2);
00506                     */
00507                     master_bias_data = irplib_image_get_data_double(master_bias);
00508                     master_bias_data[(x-1) + (y-1)*nx] = (i1+i2)/2;
00509                 }
00510                 ncorrect += 1;
00511             }
00512             }
00513         }
00514         else
00515         {
00516             finished = true;
00517         }
00518         row++;
00519     }
00520 
00521     /* Update product header */
00522     if (ncorrect > 0)
00523     {
00524         check( uves_pfits_set_badpixcorr(header, "true"),
00525            "Error updating product header");
00526     }
00527 
00528   cleanup:
00529     return ncorrect;
00530 }

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