irplib_oddeven.c

00001 /* $Id: irplib_oddeven.c,v 1.6 2007/09/26 10:49:19 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: 2007/09/26 10:49:19 $
00024  * $Revision: 1.6 $
00025  * $Name: uves-3_4_5 $
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, ny ;
00162 
00163     /* Test entries */
00164     if (in==NULL) return NULL ;
00165 
00166     nx = cpl_image_get_size_x(in) ;
00167     ny = cpl_image_get_size_y(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     /* Nullify the odd-even frequencies */
00186     cur_im = cpl_imagelist_get(freq_i_amp, 0) ;
00187     pcur_im = cpl_image_get_data_double(cur_im) ;
00188     pcur_im[nx/2] = 0.0 ;
00189     pcur_im[nx/2 + (ny-1)*nx] = 0.0 ;
00190     /* Convert to X/Y */
00191     freq_i = irplib_oddeven_cube_conv_rtheta_xy(freq_i_amp) ;
00192     cpl_imagelist_delete(freq_i_amp) ;
00193     /* FFT back to image space */
00194     cpl_image_fft(cpl_imagelist_get(freq_i, 0), cpl_imagelist_get(freq_i, 1), 
00195             CPL_FFT_INVERSE) ;
00196     cleaned = cpl_image_cast(cpl_imagelist_get(freq_i, 0), CPL_TYPE_FLOAT) ;
00197     cpl_imagelist_delete(freq_i) ;
00198     return cleaned ;
00199 }
00200 
00203 /*----------------------------------------------------------------------------*/
00214 /*----------------------------------------------------------------------------*/
00215 static cpl_imagelist * irplib_oddeven_cube_conv_xy_rtheta(
00216         cpl_imagelist   *   cube_in)
00217 {
00218     cpl_imagelist       *   cube_out ;
00219     double                  re, im ;
00220     double                  mod, phase ;
00221     int                     nx, ny, np ;
00222     cpl_image           *   tmp_im ;
00223     double              *   pim1 ;
00224     double              *   pim2 ;
00225     double              *   pim3 ;
00226     double              *   pim4 ;
00227     int                     i, j ;
00228 
00229     /* Error handling : test entries    */
00230     if (cube_in == NULL) return NULL ;
00231     np = cpl_imagelist_get_size(cube_in) ;
00232     if (np != 2) return NULL ;
00233 
00234     /* Initialise */
00235     tmp_im = cpl_imagelist_get(cube_in, 0) ;
00236     pim1 = cpl_image_get_data_double(tmp_im) ;
00237     nx = cpl_image_get_size_x(tmp_im) ;
00238     ny = cpl_image_get_size_y(tmp_im) ;
00239     tmp_im = cpl_imagelist_get(cube_in, 1) ;
00240     pim2 = cpl_image_get_data_double(tmp_im) ;
00241 
00242     /* Allocate cube_out */
00243     cube_out = cpl_imagelist_duplicate(cube_in) ;
00244 
00245     tmp_im = cpl_imagelist_get(cube_out, 0) ;
00246     pim3 = cpl_image_get_data_double(tmp_im) ;
00247     tmp_im = cpl_imagelist_get(cube_out, 1) ;
00248     pim4 = cpl_image_get_data_double(tmp_im) ;
00249     /* Convert */
00250     for (j=0 ; j<ny ; j++) {
00251         for (i=0 ; i<nx ; i++) {
00252             re = (double)pim1[i+j*nx] ;
00253             im = (double)pim2[i+j*nx] ;
00254             mod = (double)(sqrt(re*re + im*im)) ;
00255             if (re != 0.0)
00256                 phase = (double)atan2(im, re) ;
00257             else 
00258                 phase = 0.0 ;
00259             pim3[i+j*nx] = mod ; 
00260             pim4[i+j*nx] = phase ; 
00261         }
00262     }
00263     return cube_out ;
00264 }
00265 
00266 /*----------------------------------------------------------------------------*/
00279 /*----------------------------------------------------------------------------*/
00280 static cpl_imagelist * irplib_oddeven_cube_conv_rtheta_xy(
00281         cpl_imagelist   *   cube_in)
00282 {
00283     cpl_imagelist       *   cube_out ;
00284     double                  re, im ;
00285     double                  mod, phase ;
00286     int                     nx, ny, np ;
00287     cpl_image           *   tmp_im ;
00288     double              *   pim1 ;
00289     double              *   pim2 ;
00290     double              *   pim3 ;
00291     double              *   pim4 ;
00292     int                     i, j ;
00293 
00294     /* Error handling : test entries    */
00295     if (cube_in == NULL) return NULL ;
00296     np = cpl_imagelist_get_size(cube_in) ;
00297     if (np != 2) return NULL ;
00298 
00299     /* Initialise */
00300     tmp_im = cpl_imagelist_get(cube_in, 0) ;
00301     pim1 = cpl_image_get_data_double(tmp_im) ;
00302     nx = cpl_image_get_size_x(tmp_im) ;
00303     ny = cpl_image_get_size_y(tmp_im) ;
00304     tmp_im = cpl_imagelist_get(cube_in, 1) ;
00305     pim2 = cpl_image_get_data_double(tmp_im) ;
00306 
00307     /* Allocate cube_out */
00308     cube_out = cpl_imagelist_duplicate(cube_in) ;
00309 
00310     tmp_im = cpl_imagelist_get(cube_out, 0) ;
00311     pim3 = cpl_image_get_data_double(tmp_im) ;
00312     tmp_im = cpl_imagelist_get(cube_out, 1) ;
00313     pim4 = cpl_image_get_data_double(tmp_im) ;
00314     /* Convert */
00315     for (j=0 ; j<ny ; j++) {
00316         for (i=0 ; i<nx ; i++) {
00317             mod = (double)pim1[i+j*nx] ;
00318             phase = (double)pim2[i+j*nx] ;
00319             re = (double)(mod * cos(phase));
00320             im = (double)(mod * sin(phase));
00321             pim3[i+j*nx] = re ; 
00322             pim4[i+j*nx] = im ; 
00323         }
00324     }
00325     return cube_out ;
00326 }

Generated on Thu Nov 15 14:32:26 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1