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 #ifdef HAVE_CONFIG_H
00120 # include <config.h>
00121 #endif
00122
00123 #include <uves_utils_cpl.h>
00124 #include <uves_utils.h>
00125 #include <uves_utils_wrappers.h>
00126 #include <uves_error.h>
00127
00128 #include <irplib_test.h>
00129
00130 #include <cpl.h>
00131
00132 #include <float.h>
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00146
00151 static cpl_error_code
00152 test_gaussian_fitting(void)
00153 {
00154 cpl_image *image = NULL;
00155 cpl_image *noise = NULL;
00156
00157 int sizex = 200;
00158 int sizey = 100;
00159 int center_x = 85;
00160 int center_y = 55;
00161 int norm[2] = {1, 1000};
00162 int background[3] = {-3, 2, 900};
00163 int sigma_x[2] = {2, 15};
00164 int sigma_y[2] = {6, 10};
00165
00166 int n_norm = sizeof(norm) / sizeof(int);
00167 int n_back = sizeof(background) / sizeof(int);
00168 int n_sx = sizeof(sigma_x) / sizeof(int);
00169 int n_sy = sizeof(sigma_y) / sizeof(int);
00170 int i_norm, i_back, i_sx, i_sy;
00171
00172
00173 double tolerance_xy = 1;
00174 double tolerance_z = 1;
00175
00176
00177
00178 for (i_norm = 0; i_norm < n_norm; i_norm++)
00179 for (i_back = 0; i_back < n_back; i_back++)
00180 for (i_sx = 0; i_sx < n_sx ; i_sx++)
00181 for (i_sy = 0; i_sy < n_sy ; i_sy++)
00182 {
00183 cpl_image *noisep[2] = {NULL, NULL};
00184 int n_noise = sizeof(noisep) / sizeof(cpl_image *);
00185 int i_noise;
00186
00187
00188 uves_free_image(&image);
00189 uves_free_image(&noise);
00190 image = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
00191 noise = cpl_image_new(sizex, sizey, CPL_TYPE_DOUBLE);
00192 assure_mem( image );
00193 assure_mem( noise );
00194
00195 check(( cpl_image_fill_gaussian(image,
00196 center_x, center_y,
00197 norm[i_norm],
00198 sigma_x[i_sx], sigma_y[i_sy]),
00199 cpl_image_add_scalar(image, background[i_back])),
00200 "Error creating test image");
00201
00202
00203
00204
00205
00206
00207 check(( cpl_image_fill_gaussian(noise,
00208 center_x, center_y,
00209 norm[i_norm],
00210 sigma_x[i_sx], sigma_y[i_sy]),
00211 cpl_image_power(noise, 0.5),
00212 cpl_image_add_scalar(noise, .0001)),
00213 "Error creating noise image");
00214
00215
00216 noisep[0] = noise;
00217 noisep[1] = NULL;
00218 for (i_noise = 0; i_noise < n_noise; i_noise++)
00219 {
00220 double x0, y_0, sx, sy;
00221 double height;
00222 double norm_fit;
00223
00224 uves_msg_debug(" In: Center = (%.2f, %.2f) "
00225 "Sigma = (%.2f, %.2f) Norm = %.2f Bkg = %.2f",
00226 (double) center_x, (double) center_y,
00227 (double) sigma_x[i_sx], (double) sigma_y[i_sy],
00228 (double) norm[i_norm], (double) background[i_back]);
00229
00230 check( uves_fit_gaussian_2d_image(image, noisep[i_noise],
00231 1, 1,
00232 sizex, sizey,
00233 &x0, &y_0, &sx, &sy,
00234 &height,
00235 NULL, NULL),
00236 "2d fitting routine failed");
00237
00238
00239 norm_fit = height * 2 * M_PI * sx * sy;
00240
00241 uves_msg_debug("Fit: Center = (%.2f, %.2f) "
00242 "Sigma = (%.2f, %.2f) Norm = %.2f Height = %.2e",
00243 x0, y_0,
00244 sx, sy,
00245 norm_fit, height);
00246
00247 assure( fabs(center_x - x0) < tolerance_xy,
00248 CPL_ERROR_ILLEGAL_OUTPUT,
00249 "x-center deviates more than %f pixel(s)",
00250 tolerance_xy);
00251 assure( fabs(center_y - y_0) < tolerance_xy,
00252 CPL_ERROR_ILLEGAL_OUTPUT,
00253 "y-center deviates more than %f pixel(s)",
00254 tolerance_xy);
00255 assure( fabs(sigma_x[i_sx] - sx) < tolerance_xy,
00256 CPL_ERROR_ILLEGAL_OUTPUT,
00257 "sigma_x deviates more than %f pixel(s)",
00258 tolerance_xy);
00259 assure( fabs(sigma_y[i_sy] - sy) < tolerance_xy,
00260 CPL_ERROR_ILLEGAL_OUTPUT,
00261 "sigma_y deviates more than %f pixel(s)",
00262 tolerance_xy);
00263
00264
00265
00266
00267 assure( fabs(norm[i_norm] - norm_fit) < tolerance_z,
00268 CPL_ERROR_ILLEGAL_OUTPUT,
00269 "Norm deviates more than %f", tolerance_z);
00270
00271 }
00272 }
00273 cleanup:
00274 uves_free_image(&image);
00275 uves_free_image(&noise);
00276
00277 return cpl_error_get_code();
00278 }
00279
00280
00281 #if 0
00282
00283 #define QFITS_MEMORY_MAXPTRS 200003
00284 #define PTR_HASH(ptr) (((unsigned long int) ptr) % QFITS_MEMORY_MAXPTRS)
00285
00286
00287 #define LOOP 1000
00288
00289 static void
00290 realloc_cpl(void *p)
00291 {
00292 int i;
00293 for (i = LOOP; i >=0; i--) p = cpl_realloc(p, 16);
00294 return;
00295 }
00296
00297 static void
00298 realloc_system(void *p)
00299 {
00300 long i;
00301 int j;
00302 for (j = 0; j < 5000; j++)
00303 for (i = LOOP; i >=0; i--) p = realloc(p, 16);
00304 return;
00305 }
00306
00307 static void
00308 test_xmemory(void)
00309 {
00310 int i;
00311 int j;
00312
00313
00314
00315
00316
00317 const int N = 15;
00318 const int size[] = {
00319 99440, 99820, 99820, 99820, 99820,
00320 99820, 99820, 99820, 99820, 99800,
00321 99820, 99820, 99820, 99820, 99820,
00322 99820, 99820, 99820, 99820, 99800,
00323 99820, 99820, 99820, 99820, 99820,
00324 99820, 99820, 99820, 99820, 99800,
00325 99820, 99820, 99820, 99820, 99800,
00326 99820, 99820, 99820, 99820, 99800,
00327 99820, 99820, 99820, 99820, 99800,
00328 99820, 99820, 99820, 99820, 99800,
00329 99820, 99820};
00330
00331 for (j = 0; j < sizeof(size)/sizeof(int); j++)
00332 {
00333 for (i = 0; i < N; i++)
00334 {
00335 cpl_malloc(16);
00336 }
00337
00338 cpl_malloc(size[j]);
00339 cpl_malloc(size[j]);
00340 }
00341
00342 void *p1 = cpl_malloc(16);
00343 void *p2 = malloc(16);
00344
00345 realloc_cpl (p1);
00346 realloc_system(p2);
00347
00348 const char *p = NULL;
00349 printf("%c", *p);
00350
00351 return;
00352
00353 int M = sizeof(size)/sizeof(int);
00354
00355 #if 0
00356 for (j = 0; j < M; j++)
00357 {
00358 unsigned long alloc = 0;
00359 void *p;
00360 for (i = 0; i < N; i++)
00361 {
00362 p = cpl_malloc(16);
00363 alloc += 16;
00364
00365 fprintf(stderr, "%x, %d, %d alloc=%d\n", p, p, PTR_HASH(p), alloc);
00366 }
00367
00368 fprintf(stderr, "-----------------------%d\n", j);
00369
00370 for (i = 0; i < 2; i++)
00371 {
00372 p = cpl_malloc(size[j]);
00373 alloc += size[j];
00374 fprintf(stderr, "%d %x, %d, %d alloc=%d\n", size, p, p, PTR_HASH(p), alloc);
00375 }
00376 fprintf(stderr, "-----------------------\n");
00377 }
00378 #endif
00379 }
00380 #endif
00381
00382
00391
00392
00393 int main(void)
00394 {
00395
00396 IRPLIB_TEST_INIT;
00397
00398 check( uves_check_version(),
00399 "Dependency libraries version check failed");
00400
00401
00402
00403 check( test_gaussian_fitting(),
00404 "Test of gaussian fitting failed");
00405
00406 cleanup:
00407 IRPLIB_TEST_END;
00408 }
00409
00410