00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 #ifdef HAVE_CONFIG_H
00123 # include <config.h>
00124 #endif
00125
00126 #include <uves_utils_cpl.h>
00127 #include <uves_utils.h>
00128 #include <uves_utils_wrappers.h>
00129 #include <uves_error.h>
00130
00131 #include <cpl_test.h>
00132
00133 #include <cpl.h>
00134
00135 #include <float.h>
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00149
00154 static cpl_error_code
00155 test_gaussian_fitting(void)
00156 {
00157 cpl_image *image = NULL;
00158 cpl_image *noise = NULL;
00159
00160 int sizex = 200;
00161 int sizey = 100;
00162 int center_x = 85;
00163 int center_y = 55;
00164 int norm[2] = {1, 1000};
00165 int background[3] = {-3, 2, 900};
00166 int sigma_x[2] = {2, 15};
00167 int sigma_y[2] = {6, 10};
00168
00169 int n_norm = sizeof(norm) / sizeof(int);
00170 int n_back = sizeof(background) / sizeof(int);
00171 int n_sx = sizeof(sigma_x) / sizeof(int);
00172 int n_sy = sizeof(sigma_y) / sizeof(int);
00173 int i_norm, i_back, i_sx, i_sy;
00174
00175
00176 double tolerance_xy = 1;
00177 double tolerance_z = 1;
00178
00179
00180
00181 for (i_norm = 0; i_norm < n_norm; i_norm++)
00182 for (i_back = 0; i_back < n_back; i_back++)
00183 for (i_sx = 0; i_sx < n_sx ; i_sx++)
00184 for (i_sy = 0; i_sy < n_sy ; i_sy++)
00185 {
00186 cpl_image *noisep[2] = {NULL, NULL};
00187 int n_noise = sizeof(noisep) / sizeof(cpl_image *);
00188 int i_noise;
00189
00190
00191 uves_free_image(&image);
00192 uves_free_image(&noise);
00193 image = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
00194 noise = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
00195 assure_mem( image );
00196 assure_mem( noise );
00197
00198 check(( cpl_image_fill_gaussian(image,
00199 center_x, center_y,
00200 norm[i_norm],
00201 sigma_x[i_sx], sigma_y[i_sy]),
00202 cpl_image_add_scalar(image, background[i_back])),
00203 "Error creating test image");
00204
00205
00206
00207
00208
00209
00210 check(( cpl_image_fill_gaussian(noise,
00211 center_x, center_y,
00212 norm[i_norm],
00213 sigma_x[i_sx], sigma_y[i_sy]),
00214 cpl_image_power(noise, 0.5),
00215 cpl_image_add_scalar(noise, .0001)),
00216 "Error creating noise image");
00217
00218
00219 noisep[0] = noise;
00220 noisep[1] = NULL;
00221 for (i_noise = 0; i_noise < n_noise; i_noise++)
00222 {
00223 double x0, y_0, sx, sy;
00224 double height;
00225 double norm_fit;
00226
00227 uves_msg_debug(" In: Center = (%.2f, %.2f) "
00228 "Sigma = (%.2f, %.2f) Norm = %.2f Bkg = %.2f",
00229 (double) center_x, (double) center_y,
00230 (double) sigma_x[i_sx], (double) sigma_y[i_sy],
00231 (double) norm[i_norm], (double) background[i_back]);
00232
00233 check( uves_fit_gaussian_2d_image(image, noisep[i_noise],
00234 1, 1,
00235 sizex, sizey,
00236 &x0, &y_0, &sx, &sy,
00237 &height,
00238 NULL, NULL),
00239 "2d fitting routine failed");
00240
00241
00242 norm_fit = height * 2 * M_PI * sx * sy;
00243
00244 uves_msg_debug("Fit: Center = (%.2f, %.2f) "
00245 "Sigma = (%.2f, %.2f) Norm = %.2f Height = %.2e",
00246 x0, y_0,
00247 sx, sy,
00248 norm_fit, height);
00249
00250 assure( fabs(center_x - x0) < tolerance_xy,
00251 CPL_ERROR_ILLEGAL_OUTPUT,
00252 "x-center deviates more than %f pixel(s)",
00253 tolerance_xy);
00254 assure( fabs(center_y - y_0) < tolerance_xy,
00255 CPL_ERROR_ILLEGAL_OUTPUT,
00256 "y-center deviates more than %f pixel(s)",
00257 tolerance_xy);
00258 assure( fabs(sigma_x[i_sx] - sx) < tolerance_xy,
00259 CPL_ERROR_ILLEGAL_OUTPUT,
00260 "sigma_x deviates more than %f pixel(s)",
00261 tolerance_xy);
00262 assure( fabs(sigma_y[i_sy] - sy) < tolerance_xy,
00263 CPL_ERROR_ILLEGAL_OUTPUT,
00264 "sigma_y deviates more than %f pixel(s)",
00265 tolerance_xy);
00266
00267
00268
00269
00270 assure( fabs(norm[i_norm] - norm_fit) < tolerance_z,
00271 CPL_ERROR_ILLEGAL_OUTPUT,
00272 "Norm deviates more than %f", tolerance_z);
00273
00274 }
00275 }
00276 cleanup:
00277 uves_free_image(&image);
00278 uves_free_image(&noise);
00279
00280 return cpl_error_get_code();
00281 }
00282
00283
00284 #if 0
00285
00286 #define QFITS_MEMORY_MAXPTRS 200003
00287 #define PTR_HASH(ptr) (((unsigned long int) ptr) % QFITS_MEMORY_MAXPTRS)
00288
00289
00290 #define LOOP 1000
00291
00292 static void
00293 realloc_cpl(void *p)
00294 {
00295 int i;
00296 for (i = LOOP; i >=0; i--) p = cpl_realloc(p, 16);
00297 return;
00298 }
00299
00300 static void
00301 realloc_system(void *p)
00302 {
00303 long i;
00304 int j;
00305 for (j = 0; j < 5000; j++)
00306 for (i = LOOP; i >=0; i--) p = realloc(p, 16);
00307 return;
00308 }
00309
00310 static void
00311 test_xmemory(void)
00312 {
00313 int i;
00314 int j;
00315
00316
00317
00318
00319
00320 const int N = 15;
00321 const int size[] = {
00322 99440, 99820, 99820, 99820, 99820,
00323 99820, 99820, 99820, 99820, 99800,
00324 99820, 99820, 99820, 99820, 99820,
00325 99820, 99820, 99820, 99820, 99800,
00326 99820, 99820, 99820, 99820, 99820,
00327 99820, 99820, 99820, 99820, 99800,
00328 99820, 99820, 99820, 99820, 99800,
00329 99820, 99820, 99820, 99820, 99800,
00330 99820, 99820, 99820, 99820, 99800,
00331 99820, 99820, 99820, 99820, 99800,
00332 99820, 99820};
00333
00334 for (j = 0; j < sizeof(size)/sizeof(int); j++)
00335 {
00336 for (i = 0; i < N; i++)
00337 {
00338 cpl_malloc(16);
00339 }
00340
00341 cpl_malloc(size[j]);
00342 cpl_malloc(size[j]);
00343 }
00344
00345 void *p1 = cpl_malloc(16);
00346 void *p2 = malloc(16);
00347
00348 realloc_cpl (p1);
00349 realloc_system(p2);
00350
00351 const char *p = NULL;
00352 printf("%c", *p);
00353
00354 return;
00355
00356 int M = sizeof(size)/sizeof(int);
00357
00358 #if 0
00359 for (j = 0; j < M; j++)
00360 {
00361 unsigned long alloc = 0;
00362 void *p;
00363 for (i = 0; i < N; i++)
00364 {
00365 p = cpl_malloc(16);
00366 alloc += 16;
00367
00368 fprintf(stderr, "%x, %d, %d alloc=%d\n", p, p, PTR_HASH(p), alloc);
00369 }
00370
00371 fprintf(stderr, "-----------------------%d\n", j);
00372
00373 for (i = 0; i < 2; i++)
00374 {
00375 p = cpl_malloc(size[j]);
00376 alloc += size[j];
00377 fprintf(stderr, "%d %x, %d, %d alloc=%d\n", size, p, p, PTR_HASH(p), alloc);
00378 }
00379 fprintf(stderr, "-----------------------\n");
00380 }
00381 #endif
00382 }
00383 #endif
00384
00385
00394
00395
00396 int main(void)
00397 {
00398
00399 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00400
00401 check( uves_check_version(),
00402 "Dependency libraries version check failed");
00403
00404
00405
00406 check( test_gaussian_fitting(),
00407 "Test of gaussian fitting failed");
00408
00409 cleanup:
00410 return cpl_test_end(0);
00411 }
00412
00413