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: not supported by cvs2svn $
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 #include <fors_saturation.h>
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, NULL, 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);
00088     
00089     saturated = fors_saturation_img_satper(image);
00090     
00091     
00092     test_rel( saturated, 100 * (1 - 1.0 / (nx*ny)), 0.01 ); 
00093     test_eq( nx, fors_image_get_size_x(image) );
00094     test_eq( ny, fors_image_get_size_y(image) );
00095 
00096     cpl_frame_delete(frame);
00097     cpl_propertylist_delete(header);
00098     fors_image_delete(&image);
00099 
00100 
00101     /* Test croppping */
00102     data     = cpl_image_new(3, 2, FORS_IMAGE_TYPE);
00103     variance = cpl_image_new(3, 2, FORS_IMAGE_TYPE);
00104 
00105     {
00106         int x, y;
00107         for (y = 1; y <= 2; y++) {
00108             for (x = 1; x <= 3; x++) {
00109                 cpl_image_set(data, x, y, x*y);
00110             }
00111         }
00112         /*
00113           Input data now:
00114             2 4 6
00115             1 2 3
00116         */
00117     }
00118     cpl_image_add_scalar(variance, 1.0);
00119     image = fors_image_new(data, variance);
00120     
00121     int xlo = 2;
00122     int xhi = 2;
00123     int ylo = 1;
00124     int yhi = 2;
00125     fors_image_crop(image, 
00126                     xlo, ylo, xhi, yhi);
00127 
00128     /*
00129          Should have now:
00130               4
00131               2
00132      */
00133     test_eq( fors_image_get_size_x(image), 1 );
00134     test_eq( fors_image_get_size_y(image), 2 );
00135 
00136     test_rel( fors_image_get_min(image), 2, 0.0001 );
00137     test_rel( fors_image_get_max(image), 4, 0.0001 );
00138     test_rel( fors_image_get_mean(image, NULL), 3, 0.0001 );
00139     test_rel( fors_image_get_stdev(image, NULL), sqrt(2), 0.0001 );
00140 
00141     fors_image_delete(&image);
00142 
00143     cleanup;
00144     return;
00145 }
00146 
00150 static void
00151 test_median_filter(void)
00152 {
00153     //const int nx = 4000;
00154     //const int ny = 2000;
00155     const int nx = 400;
00156     const int ny = 200;
00157 
00158     const int xradius = 1;
00159     const int yradius = 1;
00160     const int xstart = 1;
00161     const int ystart = 1;
00162     const int xend = nx;
00163     const int yend = ny;
00164     const int xstep = 1;
00165     const int ystep = 1;
00166 
00167     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00168     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00169     
00170     fors_image *image;
00171     cpl_image *smooth;
00172 
00173     cpl_image_add_scalar(variance, 1.0);
00174 
00175     image = fors_image_new(data, variance);
00176     bool use_data = true;
00177     smooth = fors_image_filter_median_create(image,
00178                                              xradius, yradius,
00179                                              xstart, ystart,
00180                                              xend, yend,
00181                                              xstep, ystep,
00182                                              use_data);
00183     
00184     cpl_image_delete(smooth);
00185     fors_image_delete(&image);
00186 
00187     return;
00188 }
00189 
00190 #undef cleanup
00191 #define cleanup \
00192 do { \
00193     fors_image_delete(&left); \
00194     fors_image_delete(&right); \
00195 } while(0)
00196 
00200 static void
00201 test_subtract(void)
00202 {
00203     /* Simulate data */
00204     const int nx = 20;
00205     const int ny = 30;
00206     const double values  = 17;
00207     const double variance = 5;
00208     cpl_image *left_data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00209     cpl_image *left_variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00210     cpl_image *right_data;
00211     cpl_image *right_variance;
00212     fors_image *left, *right;
00213     double error_before, error_after;
00214 
00215     cpl_image_add_scalar(left_data, values);
00216     cpl_image_add_scalar(left_variance, variance);
00217     
00218     right_data    = cpl_image_multiply_scalar_create(left_data, 0.6);
00219     right_variance = cpl_image_duplicate(left_variance);
00220 
00221     left  = fors_image_new(left_data , left_variance);
00222     right = fors_image_new(right_data, right_variance);
00223 
00224     error_before = fors_image_get_error_mean(left, NULL);
00225 
00226     /* Call function */
00227     fors_image_subtract(left, right);
00228 
00229     /* Check results */
00230     error_after = fors_image_get_error_mean(left, NULL);
00231     test_rel( fors_image_get_mean(left, NULL), values*(1-0.6), 0.001);
00232     test_rel( error_after, error_before*sqrt(2.0), 0.001);
00233     
00234     cleanup;
00235     return;
00236 }
00237 
00238 #undef cleanup
00239 #define cleanup \
00240 do { \
00241     fors_image_delete(&image); \
00242 } while(0)
00243 
00247 static void
00248 test_exponential(void)
00249 {
00250     /* Simulate data */
00251     const int nx = 20;
00252     const int ny = 30;
00253     const double values  = 17;
00254     const double var_val = 5;
00255     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00256     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00257     double base = 10;
00258     double dbase = -1;
00259     fors_image *image;
00260     
00261     cpl_image_add_scalar(data, values);
00262     cpl_image_add_scalar(variance, var_val);
00263     
00264     image = fors_image_new(data, variance);
00265 
00266     /* Call function */
00267     fors_image_exponential(image, base, dbase);
00268 
00269     /* Check results */
00270     test_rel( fors_image_get_mean(image, NULL), pow(base, values), 0.001);
00271     
00272     cleanup;
00273     return;
00274 }
00275 
00276 
00277 #undef cleanup
00278 #define cleanup \
00279 do { \
00280     fors_image_delete(&image); \
00281 } while(0)
00282 
00285 static void
00286 test_divide(void)
00287 {
00288     /* Simulate data */
00289     const int nx = 20;
00290     const int ny = 30;
00291     const double values  = 17;
00292     const double variance_value = 5;
00293     cpl_image *data     = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00294     cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
00295     fors_image *image = NULL;
00296     double error_before, error_after;
00297 
00298     cpl_image_add_scalar(data    , values);
00299     cpl_image_add_scalar(variance, variance_value);
00300     
00301     image  = fors_image_new(data, variance);
00302 
00303     error_before = 
00304         fors_image_get_error_mean(image, NULL) /
00305         fors_image_get_mean      (image, NULL);
00306 
00307     /* Call function */
00308     fors_image_divide(image, image);
00309 
00310     /* Check results,
00311      * relative errors add in quadrature
00312      */
00313     error_after =
00314         fors_image_get_error_mean(image, NULL) /
00315         fors_image_get_mean      (image, NULL);
00316     test_rel( fors_image_get_mean(image, NULL), 1.0, 0.001 );
00317     test_rel( error_after, error_before*sqrt(2.0), 0.001 );
00318 
00319     cleanup;
00320     return;
00321 }
00322 
00323 
00324 
00328 int main(void)
00329 {
00330     TEST_INIT;
00331 
00332     test_image();
00333     
00334     test_median_filter();
00335 
00336     test_subtract();
00337 
00338     test_divide();
00339 
00340     test_exponential();
00341 
00342     TEST_END;
00343 }
00344 

Generated on 12 Feb 2016 for FORS Pipeline Reference Manual by  doxygen 1.6.1