irplib_oddeven.c

00001 /* $Id: irplib_oddeven.c,v 1.9 2012/01/12 11:50:41 llundin Exp $
00002  *
00003  * This file is part of the irplib package
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2012/01/12 11:50:41 $
00024  * $Revision: 1.9 $
00025  * $Name: uves-5_0_0 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_oddeven.h"
00040 
00041 /*-----------------------------------------------------------------------------
00042                             Functions prototypes
00043  -----------------------------------------------------------------------------*/
00044 
00045 static cpl_imagelist * irplib_oddeven_cube_conv_xy_rtheta(cpl_imagelist *) ;
00046 static cpl_imagelist * irplib_oddeven_cube_conv_rtheta_xy(cpl_imagelist *) ;
00047  
00048 /*----------------------------------------------------------------------------*/
00052 /*----------------------------------------------------------------------------*/
00053 
00056 /*----------------------------------------------------------------------------*/
00064 /*----------------------------------------------------------------------------*/
00065 int irplib_oddeven_monitor(
00066         const cpl_image     *   in,
00067         int                     iquad,
00068         double              *   r_even) 
00069 {
00070     cpl_image       *   extracted ;        
00071     cpl_image       *   labels ;        
00072     int             *   plabels ;
00073     int                 llx, lly, urx, ury ;
00074     int                 nx, ny ;
00075     double              f_even, f_tot ;
00076     cpl_apertures   *   aperts ;
00077     int                 i, j ;
00078 
00079     /* Test entries */
00080     if (in == NULL || r_even == NULL) return -1 ;
00081     nx = cpl_image_get_size_x(in) ;
00082     ny = cpl_image_get_size_y(in) ;
00083     
00084     switch (iquad){
00085         case 1:
00086             llx = 1 ; lly = 1 ; urx = nx/2 ; ury = ny/2 ; break ;
00087         case 2:
00088             llx = (nx/2)+1 ; lly = 1 ; urx = nx ; ury = ny/2 ; break ;
00089         case 3:
00090             llx = 1 ; lly = (ny/2)+1 ; urx = nx/2 ; ury = ny ; break ;
00091         case 4:
00092             llx = (nx/2)+1 ; lly = (ny/2)+1 ; urx = nx ; ury = ny ; break ;
00093         case 0:
00094             llx = 1 ; lly = 1 ; urx = nx ; ury = ny ; break ;
00095         default:
00096             cpl_msg_error(cpl_func, "Unsupported mode") ;
00097             *r_even = 0.0 ;
00098             return -1 ;
00099     }
00100    
00101     /* Extract quadrant */
00102     if ((extracted = cpl_image_extract(in, llx, lly, urx, ury)) == NULL) {
00103         cpl_msg_error(cpl_func, "Cannot extract quadrant") ;
00104         *r_even = 0.0 ;
00105         return -1 ;
00106     }
00107     nx = cpl_image_get_size_x(extracted) ;
00108     ny = cpl_image_get_size_y(extracted) ;
00109             
00110     /* Get f_tot */
00111     f_tot = cpl_image_get_median(extracted) ;
00112     if (fabs(f_tot) < 1e-6) {
00113         cpl_msg_warning(cpl_func, "Quadrant median is 0.0") ;
00114         cpl_image_delete(extracted) ;
00115         *r_even = 0.0 ;
00116         return -1 ;
00117     }
00118 
00119     /* Create the label image to define the even columns */
00120     labels = cpl_image_new(nx, ny, CPL_TYPE_INT) ;
00121     plabels = cpl_image_get_data_int(labels) ;
00122     for (i=0 ; i<nx ; i++) {
00123         if (i % 2) for (j=0 ; j<ny ; j++) plabels[i+j*nx] = 0 ;
00124         else for (j=0 ; j<ny ; j++) plabels[i+j*nx] = 1 ;
00125     }
00126     
00127     /* Get the median of even columns */
00128     if ((aperts = cpl_apertures_new_from_image(extracted, labels)) == NULL) {
00129         cpl_msg_error(cpl_func, "Cannot compute the even columns median") ;
00130         cpl_image_delete(extracted) ;
00131         cpl_image_delete(labels) ;
00132         *r_even = 0.0 ;
00133         return -1 ;
00134     }
00135     cpl_image_delete(extracted) ;
00136     cpl_image_delete(labels) ;
00137     f_even = cpl_apertures_get_median(aperts, 1) ;
00138     cpl_apertures_delete(aperts) ;
00139     
00140     /* Compute the even rate and return */
00141     *r_even = f_even / f_tot ;
00142     return 0 ;
00143 }
00144 
00145 /*----------------------------------------------------------------------------*/
00151 /*----------------------------------------------------------------------------*/
00152 cpl_image * irplib_oddeven_correct(const cpl_image * in)
00153 {
00154     cpl_image       *   in_real ;
00155     cpl_image       *   in_imag ;
00156     cpl_imagelist   *   freq_i ;
00157     cpl_imagelist   *   freq_i_amp ;
00158     cpl_image       *   cur_im ;
00159     double          *   pcur_im ;
00160     cpl_image       *   cleaned ;
00161     int                 nx ;
00162     cpl_vector      *   hf_med ;
00163 
00164     /* Test entries */
00165     if (in==NULL) return NULL ;
00166 
00167     nx = cpl_image_get_size_x(in) ;
00168 
00169     /* Local copy of the input image in DOUBLE */
00170     in_real = cpl_image_cast(in, CPL_TYPE_DOUBLE) ;
00171     in_imag = cpl_image_duplicate(in_real) ;
00172     cpl_image_multiply_scalar(in_imag, 0.0) ;
00173     
00174     /* Apply FFT to input image */
00175     cpl_image_fft(in_real, in_imag, CPL_FFT_DEFAULT) ;
00176 
00177     /* Put the result in an image list */
00178     freq_i = cpl_imagelist_new() ;
00179     cpl_imagelist_set(freq_i, in_real, 0) ;
00180     cpl_imagelist_set(freq_i, in_imag, 1) ;
00181     
00182     /* Convert to amplitude/phase */
00183     freq_i_amp = irplib_oddeven_cube_conv_xy_rtheta(freq_i);
00184     cpl_imagelist_delete(freq_i) ;
00185 
00186     /* Correct the odd-even frequency */
00187     cur_im = cpl_imagelist_get(freq_i_amp, 0) ;
00188     pcur_im = cpl_image_get_data_double(cur_im) ;
00189     /* Odd-even frequency will be replaced by 
00190        the median of the 5 values around */
00191     hf_med = cpl_vector_new(5); 
00192 
00193     cpl_vector_set(hf_med, 0, pcur_im[nx/2 + 1]); 
00194     cpl_vector_set(hf_med, 1, pcur_im[nx/2 + 2]);
00195     cpl_vector_set(hf_med, 2, pcur_im[nx/2 + 3]);
00196     cpl_vector_set(hf_med, 3, pcur_im[nx/2    ]);
00197     cpl_vector_set(hf_med, 4, pcur_im[nx/2  -1]);
00198 
00199     pcur_im[nx / 2 + 1] = cpl_vector_get_median(hf_med);
00200     cpl_vector_delete(hf_med);
00201 
00202     /* Convert to X/Y */
00203     freq_i = irplib_oddeven_cube_conv_rtheta_xy(freq_i_amp) ;
00204     cpl_imagelist_delete(freq_i_amp) ;
00205     /* FFT back to image space */
00206     cpl_image_fft(cpl_imagelist_get(freq_i, 0), cpl_imagelist_get(freq_i, 1), 
00207             CPL_FFT_INVERSE) ;
00208     cleaned = cpl_image_cast(cpl_imagelist_get(freq_i, 0), CPL_TYPE_FLOAT) ;
00209     cpl_imagelist_delete(freq_i) ;
00210     return cleaned ;
00211 }
00212 
00215 /*----------------------------------------------------------------------------*/
00226 /*----------------------------------------------------------------------------*/
00227 static cpl_imagelist * irplib_oddeven_cube_conv_xy_rtheta(
00228         cpl_imagelist   *   cube_in)
00229 {
00230     cpl_imagelist       *   cube_out ;
00231     double                  re, im ;
00232     double                  mod, phase ;
00233     int                     nx, ny, np ;
00234     cpl_image           *   tmp_im ;
00235     double              *   pim1 ;
00236     double              *   pim2 ;
00237     double              *   pim3 ;
00238     double              *   pim4 ;
00239     int                     i, j ;
00240 
00241     /* Error handling : test entries    */
00242     if (cube_in == NULL) return NULL ;
00243     np = cpl_imagelist_get_size(cube_in) ;
00244     if (np != 2) return NULL ;
00245 
00246     /* Initialise */
00247     tmp_im = cpl_imagelist_get(cube_in, 0) ;
00248     pim1 = cpl_image_get_data_double(tmp_im) ;
00249     nx = cpl_image_get_size_x(tmp_im) ;
00250     ny = cpl_image_get_size_y(tmp_im) ;
00251     tmp_im = cpl_imagelist_get(cube_in, 1) ;
00252     pim2 = cpl_image_get_data_double(tmp_im) ;
00253 
00254     /* Allocate cube_out */
00255     cube_out = cpl_imagelist_duplicate(cube_in) ;
00256 
00257     tmp_im = cpl_imagelist_get(cube_out, 0) ;
00258     pim3 = cpl_image_get_data_double(tmp_im) ;
00259     tmp_im = cpl_imagelist_get(cube_out, 1) ;
00260     pim4 = cpl_image_get_data_double(tmp_im) ;
00261     /* Convert */
00262     for (j=0 ; j<ny ; j++) {
00263         for (i=0 ; i<nx ; i++) {
00264             re = (double)pim1[i+j*nx] ;
00265             im = (double)pim2[i+j*nx] ;
00266             mod = (double)(sqrt(re*re + im*im)) ;
00267             if (re != 0.0)
00268                 phase = (double)atan2(im, re) ;
00269             else 
00270                 phase = 0.0 ;
00271             pim3[i+j*nx] = mod ; 
00272             pim4[i+j*nx] = phase ; 
00273         }
00274     }
00275     return cube_out ;
00276 }
00277 
00278 /*----------------------------------------------------------------------------*/
00291 /*----------------------------------------------------------------------------*/
00292 static cpl_imagelist * irplib_oddeven_cube_conv_rtheta_xy(
00293         cpl_imagelist   *   cube_in)
00294 {
00295     cpl_imagelist       *   cube_out ;
00296     double                  re, im ;
00297     double                  mod, phase ;
00298     int                     nx, ny, np ;
00299     cpl_image           *   tmp_im ;
00300     double              *   pim1 ;
00301     double              *   pim2 ;
00302     double              *   pim3 ;
00303     double              *   pim4 ;
00304     int                     i, j ;
00305 
00306     /* Error handling : test entries    */
00307     if (cube_in == NULL) return NULL ;
00308     np = cpl_imagelist_get_size(cube_in) ;
00309     if (np != 2) return NULL ;
00310 
00311     /* Initialise */
00312     tmp_im = cpl_imagelist_get(cube_in, 0) ;
00313     pim1 = cpl_image_get_data_double(tmp_im) ;
00314     nx = cpl_image_get_size_x(tmp_im) ;
00315     ny = cpl_image_get_size_y(tmp_im) ;
00316     tmp_im = cpl_imagelist_get(cube_in, 1) ;
00317     pim2 = cpl_image_get_data_double(tmp_im) ;
00318 
00319     /* Allocate cube_out */
00320     cube_out = cpl_imagelist_duplicate(cube_in) ;
00321 
00322     tmp_im = cpl_imagelist_get(cube_out, 0) ;
00323     pim3 = cpl_image_get_data_double(tmp_im) ;
00324     tmp_im = cpl_imagelist_get(cube_out, 1) ;
00325     pim4 = cpl_image_get_data_double(tmp_im) ;
00326     /* Convert */
00327     for (j=0 ; j<ny ; j++) {
00328         for (i=0 ; i<nx ; i++) {
00329             mod = (double)pim1[i+j*nx] ;
00330             phase = (double)pim2[i+j*nx] ;
00331             re = (double)(mod * cos(phase));
00332             im = (double)(mod * sin(phase));
00333             pim3[i+j*nx] = re ; 
00334             pim4[i+j*nx] = im ; 
00335         }
00336     }
00337     return cube_out ;
00338 }

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1