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
00123
00124
00125
00126
00127
00128
00129
00130
00131 #ifdef HAVE_CONFIG_H
00132 # include <config.h>
00133 #endif
00134 # include <assert.h>
00135
00142
00143
00146 #include <uves_utils_cpl.h>
00147
00148 #include <uves_utils.h>
00149 #include <uves_utils_wrappers.h>
00150 #include <uves_dump.h>
00151 #include <uves_error.h>
00152
00153 #include <irplib_access.h>
00154 #include <cpl.h>
00155 #include <stdbool.h>
00156
00157 static cpl_image *filter_median(const cpl_image *image, int radx, int rady,
00158 bool extrapolate_border);
00159
00160
00161
00173
00174 const cpl_property *
00175 uves_find_property_const(const uves_propertylist *plist, const char *name,
00176 int number)
00177 {
00178 int i = 0;
00179 int size = uves_propertylist_get_size(plist);
00180
00181 assure( number >= 0, CPL_ERROR_ILLEGAL_INPUT, "Number (%d) must be non-negative",
00182 number);
00183
00184 for (i = 0; i < size; i++)
00185 {
00186 const cpl_property *p = uves_propertylist_get_const(plist, i);
00187
00188 if (strcmp(cpl_property_get_name(p), name) == 0)
00189 {
00190 if (number == 0)
00191 {
00192 return p;
00193 }
00194 else
00195
00196 {
00197 number--;
00198 }
00199 }
00200 }
00201
00202 cleanup:
00203 return NULL;
00204 }
00205 cpl_property *
00206 uves_find_property(uves_propertylist *plist, const char *name,
00207 int number)
00208 {
00209 return (cpl_property *) uves_find_property_const(plist, name, number);
00210 }
00211
00212
00224
00225 cpl_error_code
00226 uves_filter_image_average(cpl_image *image, int radius_x, int radius_y)
00227 {
00228 cpl_image *aux = NULL;
00229 double *image_data = NULL;
00230 double *aux_data = NULL;
00231 int nx, ny;
00232 int i;
00233
00234
00235
00236 assure( image != NULL, CPL_ERROR_NULL_INPUT, "Null image");
00237 assure( radius_x >= 0, CPL_ERROR_ILLEGAL_INPUT, "Negative x-radius (%d)", radius_x);
00238 assure( radius_y >= 0, CPL_ERROR_ILLEGAL_INPUT, "Negative y-radius (%d)", radius_y);
00239 assure( cpl_image_get_type(image) == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
00240 "Type is %s. double expected", uves_tostring_cpl_type(cpl_image_get_type(image)));
00241
00242 nx = cpl_image_get_size_x(image);
00243 ny = cpl_image_get_size_y(image);
00244 image_data = irplib_image_get_data_double(image);
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 aux = cpl_image_new(nx+1, ny+1, CPL_TYPE_DOUBLE);
00258 aux_data = irplib_image_get_data(aux);
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 for (i = 0; i < (nx+1)*(ny+1); i++)
00269 {
00270 int x = i % (nx+1);
00271 int y = i / (nx+1);
00272
00273 if ( x >= 1 && y >= 1)
00274 {
00275 aux_data[x + y*(nx+1)] = image_data[x-1 + (y-1) * nx]
00276 + aux_data [x-1 + y * (nx+1)]
00277 + aux_data [x + (y-1)* (nx+1)]
00278 - aux_data [x-1 + (y-1)* (nx+1)];
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 }
00300
00301 uves_msg_debug("Finished setting up auxillary image. Get average");
00302
00303
00304 for (i = 0; i < nx*ny; i++)
00305 {
00306 int x = (i % nx);
00307 int y = (i / nx);
00308
00309 int lower, upper;
00310 int left, right;
00311
00312 lower = y - radius_y; if (lower < 0) lower = 0;
00313 upper = y + radius_y; if (upper >= ny) upper = ny - 1;
00314
00315 left = x - radius_x; if (left < 0) left = 0;
00316 right = x + radius_x; if (right >= nx) right = nx - 1;
00317
00318 image_data[x + y*nx] =
00319 (
00320 aux_data[(right+1) + (upper+1)*(nx+1)] +
00321 aux_data[ left + lower *(nx+1)] -
00322 aux_data[ left + (upper+1)*(nx+1)] -
00323 aux_data[(right+1) + lower *(nx+1)]
00324 )
00325 /
00326 ( (double) (upper-lower+1) * (right-left+1) );
00327 }
00328
00329 cleanup:
00330 uves_free_image(&aux);
00331 return cpl_error_get_code();
00332 }
00333
00334
00335
00349
00350 cpl_error_code
00351 uves_filter_image_median(cpl_image **image, int xwindow, int ywindow, bool extrapolate_border)
00352 {
00353 cpl_matrix *id = NULL;
00354 cpl_image *temp = NULL;
00355
00356 assure( xwindow >= 0 && ywindow >= 0, CPL_ERROR_ILLEGAL_INPUT,
00357 "Illegal window radius: %d x %d",
00358 (2*xwindow + 1),
00359 (2*ywindow + 1));
00360
00361 UVES_TIME_START("median filter");
00362
00363 if (xwindow <= 1 && ywindow <= 1)
00364
00365 {
00366 check(( id = cpl_matrix_new(2*xwindow+1, 2*ywindow+1),
00367 cpl_matrix_fill(id, 1)), "Could not create kernel matrix");
00368
00369
00370 if (cpl_image_get_type(*image) == CPL_TYPE_INT)
00371 {
00372 temp = cpl_image_cast(*image, CPL_TYPE_DOUBLE);
00373 uves_free_image(image);
00374 }
00375 else
00376 {
00377 temp = *image;
00378 }
00379 check( *image = cpl_image_filter_median(temp, id), "Error applying median filter");
00380
00381
00382
00383 }
00384 else
00385 {
00386 temp = *image;
00387 check( *image = filter_median(temp, xwindow, ywindow, extrapolate_border),
00388 "Error applying median filter");
00389 uves_free_image(&temp);
00390 }
00391
00392 UVES_TIME_END;
00393
00394 cleanup:
00395 uves_free_matrix(&id);
00396 uves_free_image(&temp);
00397 return cpl_error_get_code();
00398 }
00399
00401 #define DOUBLE_SWAP(a,b) { register double t=(a);(a)=(b);(b)=t; }
00402
00413 inline
00414 double uves_utils_get_kth_double(
00415 double * a,
00416 int n,
00417 int k)
00418 {
00419 register double x ;
00420 register int i, j, l, m ;
00421
00422 l=0 ; m=n-1 ;
00423 while (l<m) {
00424 x=a[k] ;
00425 i=l ;
00426 j=m ;
00427 do {
00428 while (a[i]<x) i++ ;
00429 while (x<a[j]) j-- ;
00430 if (i<=j) {
00431 DOUBLE_SWAP(a[i],a[j]) ;
00432 i++ ; j-- ;
00433 }
00434 } while (i<=j) ;
00435 if (j<k) l=i ;
00436 if (k<i) m=j ;
00437 }
00438 return a[k] ;
00439 }
00440
00449 inline double
00450 uves_tools_get_median(double *a, int n)
00451 {
00452 if (n % 2 == 0)
00453 {
00454 return
00455 (uves_utils_get_kth_double(a, n, n/2) +
00456 uves_utils_get_kth_double(a, n, n/2-1))/2.0;
00457
00458 }
00459 else
00460 {
00461 return uves_utils_get_kth_double(a, n, (n-1)/2);
00462 }
00463 }
00464
00465
00466
00467
00489
00490 static cpl_image *
00491 filter_median(const cpl_image *image, int radx, int rady, bool extrapolate_border)
00492 {
00493 int x, y;
00494 int nx = cpl_image_get_size_x(image);
00495 int ny = cpl_image_get_size_y(image);
00496 cpl_image *result = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
00497 double *result_data;
00498 const double *image_data;
00499 double *window = NULL;
00500
00501 window = cpl_malloc(sizeof(double) * (2*radx+1)*(2*rady+1));
00502 assure_mem( result );
00503 assure( cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
00504 CPL_ERROR_UNSUPPORTED_MODE, "Type is %s",
00505 uves_tostring_cpl_type(cpl_image_get_type(image)));
00506
00507 result_data = irplib_image_get_data_double(result);
00508 image_data = irplib_image_get_data_double_const(image);
00509
00510 for (y = 1; y <= ny; y++)
00511 {
00512 for (x = 1; x <= nx; x++)
00513 {
00514 int x1, y_1, x2, y2;
00515
00516 x1 = x - radx; y_1 = y - rady;
00517 x2 = x + radx; y2 = y + rady;
00518
00519 if (extrapolate_border)
00520 {
00521
00522
00523 if (x1 < 1)
00524 {
00525 x2 += (1 - x1);
00526 x1 += (1 - x1);
00527 }
00528 if (nx < x2)
00529 {
00530 x1 -= (x2 - nx);
00531 x2 -= (x2 - nx);
00532 }
00533
00534 if (y_1 < 1)
00535 {
00536 y2 += (1 - y_1);
00537 y_1 += (1 - y_1);
00538 }
00539 if (ny < y2)
00540 {
00541 y_1 -= (y2 - ny);
00542 y2 -= (y2 - ny);
00543 }
00544 }
00545 else { }
00546
00547 #if 0
00548 result_data[(x-1) + (y-1)*nx] =
00549 cpl_image_get_median_window(image,
00550 uves_max_int(1, x1),
00551 uves_max_int(1, y_1),
00552 uves_min_int(nx, x2),
00553 uves_min_int(ny, y2));
00554
00555 #else
00556
00557 {
00558 int i, j, k;
00559
00560 k = 0;
00561 for (j = uves_max_int(1 , y_1)-1;
00562 j <= uves_min_int(ny, y2 )-1;
00563 j++)
00564 for (i = uves_max_int(1, x1)-1;
00565 i <= uves_min_int(nx, x2)-1;
00566 i++)
00567 {
00568 window[k++] = image_data[i + j*nx];
00569 }
00570
00571 result_data[(x-1) + (y-1)*nx] =
00572 uves_utils_get_kth_double(window,k,(((k)&1)?((k)/2):(((k)/2)-1))) ;
00573 }
00574 #endif
00575 }
00576 }
00577
00578
00579 assure( cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
00580 "Error calculating %dx%d median filter", radx, rady);
00581
00582 cleanup:
00583 cpl_free(window);
00584 return result;
00585 }
00586
00587
00588
00616
00617
00618 cpl_error_code
00619 uves_fit_gaussian_2d_image(const cpl_image *image, const cpl_image *noise,
00620 int x1, int y_1,
00621 int x2, int y2,
00622 double *x0, double *y_0, double *sigmax, double *sigmay,
00623 double *amplitude,
00624 double *dx0, double *dy0
00625 )
00626 {
00627 cpl_image *marginal_x = NULL;
00628 cpl_image *marginal_y = NULL;
00629 cpl_image *marginal_x_noise = NULL;
00630 cpl_image *marginal_y_noise = NULL;
00631 cpl_image *variance = NULL;
00632 cpl_matrix *covariance = NULL;
00633
00634 int nx, ny;
00635 double norm_x, norm_y;
00636 double background_x, background_y;
00637
00638
00639 assure( image != NULL, CPL_ERROR_NULL_INPUT, "Null image");
00640 nx = cpl_image_get_size_x(image);
00641 ny = cpl_image_get_size_y(image);
00642 assure( noise != NULL || (dx0 == NULL && dy0 == NULL), CPL_ERROR_INCOMPATIBLE_INPUT,
00643 "Cannot compute uncertainty of fit with no noise image specified");
00644 assure( noise == NULL ||
00645 (cpl_image_get_size_x(noise) == nx &&
00646 cpl_image_get_size_y(noise) == ny),
00647 CPL_ERROR_INCOMPATIBLE_INPUT,
00648 "Size of input image (%dx%d) and noise image (%dx%d) differ",
00649 nx, ny,
00650 cpl_image_get_size_x(noise),
00651 cpl_image_get_size_y(noise));
00652 assure( 1 <= x1 && x1 <= x2 && x2 <= nx &&
00653 1 <= y_1 && y_1 <= y2 && y2 <= ny, CPL_ERROR_ILLEGAL_INPUT,
00654 "Illegal window: (%d, %d)-(%d, %d)", x1, y_1, x2, y2);
00655 assure( x0 != NULL, CPL_ERROR_NULL_INPUT, "Null x-center");
00656 assure( y_0 != NULL, CPL_ERROR_NULL_INPUT, "Null y-center");
00657 assure( sigmax != NULL, CPL_ERROR_NULL_INPUT, "Null sigma_x");
00658 assure( sigmay != NULL, CPL_ERROR_NULL_INPUT, "Null sigma_y");
00659
00660
00661 if (noise != NULL)
00662 {
00663
00664 check(( variance = cpl_image_extract(noise, x1, y_1, x2, y2),
00665 cpl_image_power(variance, 2.0)),
00666 "Error creating variance image");
00667 }
00668
00669
00670 check( marginal_x = cpl_image_collapse_window_create(image,
00671 x1, y_1, x2, y2,
00672 0),
00673 "Error collapsing window (%d, %d) - (%d, %d)", x1, y_1, x2, y2);
00674
00675 if (noise != NULL)
00676 {
00677
00678
00679 check( marginal_x_noise = cpl_image_collapse_window_create(variance,
00680 1, 1,
00681 x2-x1+1, y2-y_1+1,
00682 0),
00683 "Error collapsing window (1, 1) - (%d, %d)", x2-x1+1, y2-y_1+1);
00684
00685
00686 cpl_image_power(marginal_x_noise, 0.5);
00687 }
00688
00689
00690 check( marginal_y = cpl_image_collapse_window_create(image,
00691 x1, y_1, x2, y2,
00692 1),
00693 "Error collapsing window (%d, %d) - (%d, %d)", x1, y_1, x2, y2);
00694
00695 if (noise != NULL)
00696 {
00697 check( marginal_y_noise = cpl_image_collapse_window_create(variance,
00698 1, 1,
00699 x2-x1+1, y2-y_1+1,
00700 1),
00701 "Error collapsing window (1, 1) - (%d, %d)", x2-x1+1, y2-y_1+1);
00702
00703
00704 cpl_image_power(marginal_y_noise, 0.5);
00705 }
00706
00707
00708 uves_fit_1d_image(marginal_x, marginal_x_noise, NULL,
00709 true,
00710 false, false,
00711 1, x2 - x1 + 1, 1,
00712 x0, sigmax, &norm_x, &background_x, NULL,
00713 NULL, NULL,
00714 (dx0 != NULL) ? &covariance : NULL,
00715 uves_gauss, uves_gauss_derivative, 4);
00716
00717
00718 assure( cpl_error_get_code() != CPL_ERROR_CONTINUE ||
00719 cpl_error_get_code() != CPL_ERROR_SINGULAR_MATRIX,
00720 CPL_ERROR_CONTINUE, "Fitting along x failed");
00721 assure( cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
00722 "Fitting along x failed");
00723
00724
00725 *x0 += (x1 - 1);
00726
00727 if (dx0 != NULL)
00728 {
00729 *dx0 = cpl_matrix_get(covariance, 0, 0);
00730 }
00731
00732
00733
00734 uves_free_matrix(&covariance);
00735 uves_fit_1d_image(marginal_y, marginal_y_noise, NULL,
00736 false,
00737 false, false,
00738 1, y2 - y_1 + 1, 1,
00739 y_0, sigmay, &norm_y, &background_y, NULL,
00740 NULL, NULL,
00741 (dy0 != NULL) ? &covariance : NULL,
00742 uves_gauss, uves_gauss_derivative, 4);
00743
00744
00745 assure( cpl_error_get_code() != CPL_ERROR_CONTINUE ||
00746 cpl_error_get_code() != CPL_ERROR_SINGULAR_MATRIX,
00747 CPL_ERROR_CONTINUE, "Fitting along y failed");
00748 assure( cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
00749 "Fitting along y failed");
00750
00751
00752 *y_0 += (y_1 - 1);
00753
00754 if (dy0 != NULL)
00755 {
00756 *dy0 = cpl_matrix_get(covariance, 0, 0);
00757 }
00758
00759
00760
00761
00762
00763
00764 if (amplitude != NULL)
00765 {
00766 *amplitude = sqrt(norm_x * norm_y) / (2*M_PI * (*sigmax) * (*sigmay));
00767 }
00768
00769 cleanup:
00770 uves_free_matrix(&covariance);
00771 uves_free_image(&variance);
00772 uves_free_image(&marginal_x);
00773 uves_free_image(&marginal_x_noise);
00774 uves_free_image(&marginal_y);
00775 uves_free_image(&marginal_y_noise);
00776
00777 return cpl_error_get_code();
00778 }
00779
00780