fors_image-test.c

00001 /* $Id: fors_image-test.c,v 1.20 2007/11/23 14:24:24 jmlarsen Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2006 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  02110-1301 USA
00019  */
00020 
00021 /*
00022  * $Author: jmlarsen $
00023  * $Date: 2007/11/23 14:24:24 $
00024  * $Revision: 1.20 $
00025  * $Name: fors-4_8_6 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <fors_image.h>
00033 #include <fors_pfits.h>
00034 
00035 #include <test_simulate.h>
00036 #include <test.h>
00037 
00038 #include <math.h>
00039 
00046 #undef cleanup
00047 #define cleanup \
00048 do { \
00049     fors_setting_delete(&setting); \
00050 } while(0)
00051 
00055 static void
00056 test_image(void)
00057 {
00058     const int nx = 200; /* Duplication here. Must be updated
00059                            in synchronization with nx and ny in ./test_simulate.c */
00060     const int ny = 200;
00061     const char *const filename = "fors_image.fits";
00062 
00063     cpl_frame *frame = cpl_frame_new();
00064     cpl_propertylist *header = cpl_propertylist_new();
00065     fors_setting *setting = NULL;
00066     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00067     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00068     fors_image *image;
00069     double saturated;
00070 
00071     cpl_image_add_scalar(variance, 1.0);
00072     cpl_image_set(data, 1, 1, 1); /* One non-saturated pixel */
00073     image   = fors_image_new(data, variance);
00074     
00075     /* save, load */
00076     {
00077         double exptime = 423;
00078         create_standard_keys(header, exptime);
00079     }
00080 
00081     fors_image_save(image, header, filename);
00082     fors_image_delete(&image);
00083     cpl_frame_set_filename(frame, filename);
00084 
00085     setting = fors_setting_new(frame);
00086 
00087     image = fors_image_load(frame, NULL, setting, &saturated);
00088     
00089     test_rel( saturated, 100 * (1 - 1.0 / (nx*ny)), 0.01 ); 
00090     test_eq( nx, fors_image_get_size_x(image) );
00091     test_eq( ny, fors_image_get_size_y(image) );
00092 
00093     cpl_frame_delete(frame);
00094     cpl_propertylist_delete(header);
00095     fors_image_delete(&image);
00096 
00097 
00098     /* Test croppping */
00099     data     = cpl_image_new(3, 2, FORS_IMAGE_TYPE);
00100     variance = cpl_image_new(3, 2, FORS_IMAGE_TYPE);
00101 
00102     {
00103         int x, y;
00104         for (y = 1; y <= 2; y++) {
00105             for (x = 1; x <= 3; x++) {
00106                 cpl_image_set(data, x, y, x*y);
00107             }
00108         }
00109         /*
00110           Input data now:
00111             2 4 6
00112             1 2 3
00113         */
00114     }
00115     cpl_image_add_scalar(variance, 1.0);
00116     image = fors_image_new(data, variance);
00117     
00118     int xlo = 2;
00119     int xhi = 2;
00120     int ylo = 1;
00121     int yhi = 2;
00122     fors_image_crop(image, 
00123                     xlo, ylo, xhi, yhi);
00124 
00125     /*
00126          Should have now:
00127               4
00128               2
00129      */
00130     test_eq( fors_image_get_size_x(image), 1 );
00131     test_eq( fors_image_get_size_y(image), 2 );
00132 
00133     test_rel( fors_image_get_min(image), 2, 0.0001 );
00134     test_rel( fors_image_get_max(image), 4, 0.0001 );
00135     test_rel( fors_image_get_mean(image, NULL), 3, 0.0001 );
00136     test_rel( fors_image_get_stdev(image, NULL), sqrt(2), 0.0001 );
00137 
00138     fors_image_delete(&image);
00139 
00140     cleanup;
00141     return;
00142 }
00143 
00147 static void
00148 test_median_filter(void)
00149 {
00150     //const int nx = 4000;
00151     //const int ny = 2000;
00152     const int nx = 400;
00153     const int ny = 200;
00154 
00155     const int xradius = 1;
00156     const int yradius = 1;
00157     const int xstart = 1;
00158     const int ystart = 1;
00159     const int xend = nx;
00160     const int yend = ny;
00161     const int xstep = 1;
00162     const int ystep = 1;
00163 
00164     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00165     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00166     
00167     fors_image *image;
00168     cpl_image *smooth;
00169 
00170     cpl_image_add_scalar(variance, 1.0);
00171 
00172     image = fors_image_new(data, variance);
00173     bool use_data = true;
00174     smooth = fors_image_filter_median_create(image,
00175                                              xradius, yradius,
00176                                              xstart, ystart,
00177                                              xend, yend,
00178                                              xstep, ystep,
00179                                              use_data);
00180     
00181     cpl_image_delete(smooth);
00182     fors_image_delete(&image);
00183 
00184     return;
00185 }
00186 
00187 #undef cleanup
00188 #define cleanup \
00189 do { \
00190     fors_image_delete(&left); \
00191     fors_image_delete(&right); \
00192 } while(0)
00193 
00197 static void
00198 test_subtract(void)
00199 {
00200     /* Simulate data */
00201     const int nx = 20;
00202     const int ny = 30;
00203     const double values  = 17;
00204     const double variance = 5;
00205     cpl_image *left_data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00206     cpl_image *left_variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00207     cpl_image *right_data;
00208     cpl_image *right_variance;
00209     fors_image *left, *right;
00210     double error_before, error_after;
00211 
00212     cpl_image_add_scalar(left_data, values);
00213     cpl_image_add_scalar(left_variance, variance);
00214     
00215     right_data    = cpl_image_multiply_scalar_create(left_data, 0.6);
00216     right_variance = cpl_image_duplicate(left_variance);
00217 
00218     left  = fors_image_new(left_data , left_variance);
00219     right = fors_image_new(right_data, right_variance);
00220 
00221     error_before = fors_image_get_error_mean(left, NULL);
00222 
00223     /* Call function */
00224     fors_image_subtract(left, right);
00225 
00226     /* Check results */
00227     error_after = fors_image_get_error_mean(left, NULL);
00228     test_rel( fors_image_get_mean(left, NULL), values*(1-0.6), 0.001);
00229     test_rel( error_after, error_before*sqrt(2.0), 0.001);
00230     
00231     cleanup;
00232     return;
00233 }
00234 
00235 #undef cleanup
00236 #define cleanup \
00237 do { \
00238     fors_image_delete(&image); \
00239 } while(0)
00240 
00244 static void
00245 test_exponential(void)
00246 {
00247     /* Simulate data */
00248     const int nx = 20;
00249     const int ny = 30;
00250     const double values  = 17;
00251     const double var_val = 5;
00252     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00253     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00254     double base = 10;
00255     double dbase = -1;
00256     fors_image *image;
00257     
00258     cpl_image_add_scalar(data, values);
00259     cpl_image_add_scalar(variance, var_val);
00260     
00261     image = fors_image_new(data, variance);
00262 
00263     /* Call function */
00264     fors_image_exponential(image, base, dbase);
00265 
00266     /* Check results */
00267     test_rel( fors_image_get_mean(image, NULL), pow(base, values), 0.001);
00268     
00269     cleanup;
00270     return;
00271 }
00272 
00273 
00274 #undef cleanup
00275 #define cleanup \
00276 do { \
00277     fors_image_delete(&image); \
00278 } while(0)
00279 
00282 static void
00283 test_divide(void)
00284 {
00285     /* Simulate data */
00286     const int nx = 20;
00287     const int ny = 30;
00288     const double values  = 17;
00289     const double variance_value = 5;
00290     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00291     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00292     fors_image *image = NULL;
00293     double error_before, error_after;
00294 
00295     cpl_image_add_scalar(data    , values);
00296     cpl_image_add_scalar(variance, variance_value);
00297     
00298     image  = fors_image_new(data, variance);
00299 
00300     error_before = 
00301         fors_image_get_error_mean(image, NULL) /
00302         fors_image_get_mean      (image, NULL);
00303 
00304     /* Call function */
00305     fors_image_divide(image, image);
00306 
00307     /* Check results,
00308      * relative errors add in quadrature
00309      */
00310     error_after =
00311         fors_image_get_error_mean(image, NULL) /
00312         fors_image_get_mean      (image, NULL);
00313     test_rel( fors_image_get_mean(image, NULL), 1.0, 0.001 );
00314     test_rel( error_after, error_before*sqrt(2.0), 0.001 );
00315 
00316     cleanup;
00317     return;
00318 }
00319 
00320 
00321 
00325 int main(void)
00326 {
00327     TEST_INIT;
00328 
00329     test_image();
00330     
00331     test_median_filter();
00332 
00333     test_subtract();
00334 
00335     test_divide();
00336 
00337     test_exponential();
00338 
00339     TEST_END;
00340 }
00341 

Generated on Fri Mar 4 09:46:00 2011 for FORS Pipeline Reference Manual by  doxygen 1.4.7