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 #define POSIX_SOURCE 1
00058 #include "vltPort.h"
00059
00060
00061 static int filecounter ;
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #include "shift_images.h"
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 double determineShiftByCorrelation ( OneImage * refImage,
00089 OneImage * shiftedImage )
00090 {
00091 int col, row ;
00092 int i, j, k, width;
00093 unsigned long convsize ;
00094 float * lineref ;
00095 float * line ;
00096 float * offset2 ;
00097 double * result ;
00098 double mean_offset2 ;
00099
00100 int maxlag ;
00101 float * refres ;
00102 float * myres ;
00103 int halfsearch ;
00104 int delta ;
00105 double xcorr_max ;
00106
00107 float par[MAXPAR] ;
00108 float derv_par[MAXPAR] ;
00109 int iters, xdim, ndat ;
00110 int numpar, its ;
00111 int * mpar ;
00112 float tol, lab ;
00113 float * xdat, * wdat ;
00114 Vector * peak;
00115 char filename[FILENAMESZ] ;
00116 FILE * fp ;
00117
00118 if ( NullImage == refImage || NullImage == shiftedImage )
00119 {
00120 cpl_msg_error("determineShiftByCorrelation:","image not given!\n") ;
00121 return ZERO ;
00122 }
00123 if ( refImage->lx != shiftedImage -> lx || refImage -> ly != shiftedImage -> ly )
00124 {
00125 cpl_msg_error("determineShiftByCorrelation:","image size not compatible!\n") ;
00126 return ZERO ;
00127 }
00128 sprintf(filename, "offset%d.list", filecounter) ;
00129
00130 fp = fopen(filename, "w") ;
00131
00132 convsize = shiftedImage->ly;
00133
00134 lineref = (float*) cpl_calloc(convsize, sizeof(float) ) ;
00135 line = (float*) cpl_calloc(convsize, sizeof(float) ) ;
00136
00137 offset2 = (float*) cpl_calloc(shiftedImage->lx, sizeof(float) ) ;
00138
00139 for ( col = 0 ; col < shiftedImage->lx ; col++ )
00140 {
00141
00142 for ( row = 0 ; row < (int) convsize ; row++ )
00143 {
00144 lineref[row] = 0. ;
00145 line[row] = 0. ;
00146 }
00147
00148
00149 for ( row = 0 ; row < (shiftedImage->ly) ; row++ )
00150 {
00151 lineref[row] = refImage->data[col+row*shiftedImage->lx] ;
00152 line[row] = shiftedImage->data[col+row*shiftedImage->lx] ;
00153 }
00154
00155 myres = function1d_filter_lowpass(line, convsize, LOW_PASS_GAUSSIAN, 3) ;
00156 refres = function1d_filter_lowpass(lineref, convsize, LOW_PASS_GAUSSIAN, 4) ;
00157
00158
00159 halfsearch = convsize / 2 ;
00160 result = xcorrel( myres, convsize, refres, convsize, halfsearch, &delta, &maxlag, &xcorr_max ) ;
00161
00162 if ( xcorr_max < 0. )
00163 {
00164 function1d_del ( refres ) ;
00165 function1d_del ( myres ) ;
00166 cpl_free (result) ;
00167 continue ;
00168 }
00169
00170
00171
00172
00173 i = maxlag; j = i+1;
00174 while (result[j] < result[i])
00175 {
00176 i++; j++;
00177 }
00178 i = maxlag; k = i-1;
00179 while (result[k] < result[i])
00180 {
00181 i--; k--;
00182 }
00183 width = j-k+1;
00184
00185 if ( NULL == (peak = newVector (width)) )
00186 {
00187 cpl_msg_error ("determineShiftByCorrelation:","cannot allocate new Vector \n") ;
00188 return ZERO ;
00189 }
00190
00191
00192
00193 xdat = (float *) cpl_calloc( peak -> n_elements, sizeof (float) ) ;
00194 wdat = (float *) cpl_calloc( peak -> n_elements, sizeof (float) ) ;
00195 mpar = (int *) cpl_calloc( MAXPAR, sizeof (int) ) ;
00196
00197
00198
00199
00200 for ( i = 0 ; i < width ; i++ )
00201 {
00202 peak -> data[i] = result[k+i] ;
00203 xdat[i] = i;
00204 wdat[i] = 1.0;
00205 }
00206
00207
00208 xdim = XDIM;
00209 ndat = peak -> n_elements ;
00210 numpar = MAXPAR ;
00211 tol = TOL ;
00212 lab = LAB ;
00213 its = ITS ;
00214 par[1] = width/2.0 ;
00215 par[2] = (float) (maxlag - k) ;
00216 par[3] = (peak -> data[0] + peak -> data[peak->n_elements - 1]) / 2.0 ;
00217 par[0] = result[maxlag] - (par[3]) ;
00218
00219 for ( i = 0 ; i < MAXPAR ; i++ )
00220 {
00221 derv_par[i] = 0.0 ;
00222 mpar[i] = 1 ;
00223 }
00224
00225
00226 if ( 0 > ( iters = lsqfit_c( xdat, &xdim, peak -> data, wdat, &ndat, par, derv_par, mpar,
00227 &numpar, &tol, &its, &lab )) )
00228 {
00229 cpl_msg_warning ("determineShiftByCorrelation:","lsqfit_c: least squares fit failed in col: %d, error no.: %d\n", col, iters) ;
00230 destroyVector ( peak ) ;
00231 cpl_free ( xdat ) ;
00232 cpl_free ( wdat ) ;
00233 cpl_free ( mpar ) ;
00234 function1d_del ( refres ) ;
00235 function1d_del ( myres ) ;
00236 cpl_free (result) ;
00237 continue ;
00238 }
00239
00240 destroyVector ( peak ) ;
00241 cpl_free (xdat) ;
00242 cpl_free (wdat) ;
00243 cpl_free (mpar) ;
00244 function1d_del ( refres ) ;
00245 function1d_del ( myres ) ;
00246 cpl_free (result) ;
00247
00248 offset2[col] = (float)( k+par[2] - shiftedImage->ly/2) ;
00249 fprintf(fp, "offset: %f in col: %d\n", offset2[col], col) ;
00250 }
00251
00252 mean_offset2 = (double)clean_mean (offset2, shiftedImage->lx, 15., 15. ) ;
00253 fprintf(fp, "mean offset: %f\n", mean_offset2) ;
00254 fclose(fp) ;
00255
00256 cpl_free ( lineref ) ;
00257 cpl_free ( line ) ;
00258 cpl_free ( offset2 ) ;
00259 filecounter++ ;
00260 if (filecounter > 100 ) filecounter = 0 ;
00261
00262 return mean_offset2 ;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 OneImage * shiftImageInSpec ( OneImage * shiftedImage, double shift, double * sub_shift )
00277 {
00278 OneImage * retIm ;
00279 int col, row ;
00280 int intshift ;
00281
00282 if ( shiftedImage == NullImage )
00283 {
00284 cpl_msg_error( "shiftImageInSpec:","no image given!\n") ;
00285 return NullImage ;
00286 }
00287
00288 intshift = nint (shift) ;
00289 *sub_shift = shift - (double) intshift ;
00290 if ( intshift == 0 )
00291 {
00292 retIm = copy_image ( shiftedImage ) ;
00293 return retIm ;
00294 }
00295 else
00296 {
00297 if ( NullImage == (retIm = new_image(shiftedImage->lx, shiftedImage->ly)) )
00298 {
00299 cpl_msg_error ("shiftImageInSpec:","could not allocate memory!\n") ;
00300 return NullImage ;
00301 }
00302 }
00303
00304 for ( col = 0 ; col < shiftedImage -> lx ; col++ )
00305 {
00306 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00307 {
00308 if ( (row-intshift >= 0 ) && (row - intshift < retIm -> ly) )
00309 {
00310 retIm->data[col+(row-intshift)*retIm->lx] = shiftedImage->data[col+row*retIm->lx] ;
00311 }
00312 }
00313 }
00314 return retIm ;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 OneImage * fineShiftImageInSpecPoly ( OneImage * shiftedImage, double sub_shift, int order )
00327 {
00328 OneImage * retIm ;
00329 float spec[shiftedImage -> ly] ;
00330 float corrected_spec[shiftedImage -> ly] ;
00331 float xnum[order+1] ;
00332 float sum, new_sum ;
00333 float eval ;
00334 float * imageptr ;
00335 int row, col ;
00336 int firstpos ;
00337 int n_points ;
00338 int i ;
00339 int flag;
00340
00341 if ( shiftedImage == NullImage )
00342 {
00343 cpl_msg_error( "fineShiftImageInSpecPoly:","no image given!\n") ;
00344 return NullImage ;
00345 }
00346 if ( order <= 0 )
00347 {
00348 cpl_msg_error("fineShiftImageInSpecPoly:","wrong order of interpolation polynom given!/n") ;
00349 return NullImage ;
00350 }
00351
00352
00353 if ( NullImage == (retIm = new_image(shiftedImage->lx, shiftedImage->ly)) )
00354 {
00355 cpl_msg_error ("fineShiftImageInSpecPoly:","could not allocate memory!\n") ;
00356 return NullImage ;
00357 }
00358
00359 n_points = order + 1 ;
00360 if ( n_points % 2 == 0 )
00361 {
00362 firstpos = (int)(n_points/2) - 1 ;
00363 }
00364 else
00365 {
00366 firstpos = (int)(n_points/2) ;
00367 }
00368
00369
00370 for ( i = 0 ; i < n_points ; i++ )
00371 {
00372 xnum[i] = i ;
00373 }
00374
00375 for ( col = 0 ; col < shiftedImage -> lx ; col++ )
00376 {
00377 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00378 {
00379 corrected_spec[row] = 0. ;
00380 }
00381 sum = 0. ;
00382 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00383 {
00384 spec[row] = shiftedImage->data[col + row*shiftedImage->lx] ;
00385 if (qfits_isnan(spec[row]) )
00386 {
00387 spec[row] = 0. ;
00388
00389 for ( i = row - firstpos ; i < row-firstpos+n_points ; i++ )
00390 {
00391 if ( i < 0 ) continue ;
00392 if ( i >= shiftedImage->ly) continue ;
00393 corrected_spec[i] = ZERO ;
00394 }
00395 }
00396 if ( row != 0 && row != shiftedImage->ly - 1 )
00397 {
00398 sum += spec[row] ;
00399 }
00400 }
00401
00402 new_sum = 0. ;
00403 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00404 {
00405
00406
00407
00408
00409
00410
00411 if (qfits_isnan(corrected_spec[row])) continue ;
00412 if ( row - firstpos < 0 )
00413 {
00414 imageptr = &spec[0] ;
00415 eval = sub_shift + row ;
00416 }
00417 else if ( row - firstpos + n_points >= shiftedImage->ly )
00418 {
00419 imageptr = &spec[shiftedImage->ly - n_points] ;
00420 eval = sub_shift + row + n_points - shiftedImage->ly ;
00421 }
00422 else
00423 {
00424 imageptr = &spec[row-firstpos] ;
00425 eval = sub_shift + firstpos ;
00426 }
00427
00428 flag=0;
00429 corrected_spec[row]=nev_ille( xnum, imageptr, order, eval, &flag) ;
00430 if ( row != 0 && row != shiftedImage->ly - 1 )
00431 {
00432 new_sum += corrected_spec[row] ;
00433 }
00434 }
00435
00436 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00437 {
00438 if ( new_sum == 0. )
00439 {
00440 new_sum = 1. ;
00441 }
00442 if ( row == 0 )
00443 {
00444 retIm -> data[col+row*retIm->lx] = ZERO ;
00445 }
00446 else if ( row == shiftedImage -> ly - 1 )
00447 {
00448 retIm -> data[col+row*retIm->lx] = ZERO ;
00449 }
00450 else if ( qfits_isnan(corrected_spec[row]) )
00451 {
00452 retIm -> data[col+row*retIm->lx] = ZERO ;
00453 }
00454 else
00455 {
00456 corrected_spec[row] *= sum / new_sum ;
00457 retIm->data[col+row*retIm->lx] = corrected_spec[row] ;
00458 }
00459 }
00460 }
00461 return retIm ;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471 OneImage * fineShiftImageInSpecCubicspline ( OneImage * shiftedImage, double sub_shift )
00472 {
00473 OneImage * retIm ;
00474 float spec[shiftedImage -> ly] ;
00475 float corrected_spec[shiftedImage -> ly] ;
00476
00477 float xnum[shiftedImage->ly] ;
00478 float sum, new_sum ;
00479 float eval[shiftedImage->ly] ;
00480 int row, col ;
00481 int i ;
00482
00483 if ( shiftedImage == NullImage )
00484 {
00485 cpl_msg_error( "fineShiftImageInSpecPoly:","no image given!\n") ;
00486 return NullImage ;
00487 }
00488
00489
00490 if ( NullImage == (retIm = new_image(shiftedImage->lx, shiftedImage->ly)) )
00491 {
00492 cpl_msg_error ("fineShiftImageInSpecPoly:","could not allocate memory!\n") ;
00493 return NullImage ;
00494 }
00495
00496
00497 for ( i = 0 ; i < shiftedImage->ly ; i++ )
00498 {
00499 xnum[i] = i ;
00500 }
00501
00502 for ( col = 0 ; col < shiftedImage -> lx ; col++ )
00503 {
00504 sum = 0. ;
00505 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00506 {
00507 spec[row] = shiftedImage->data[col + row*shiftedImage->lx] ;
00508 if (qfits_isnan(spec[row]) )
00509 {
00510 for ( i = row-1 ; i <= row+1 ; i++ )
00511 {
00512 if ( i < 0 ) continue ;
00513 if ( i >= shiftedImage->ly) continue ;
00514 corrected_spec[i] = ZERO ;
00515 }
00516 spec[row] = 0. ;
00517 }
00518 sum += spec[row] ;
00519 eval[row] = (float)sub_shift+(float)row ;
00520 }
00521
00522 if ( -1 == function1d_natural_spline( xnum, spec, shiftedImage->ly, eval, corrected_spec,
00523 shiftedImage->ly ) )
00524 {
00525 cpl_msg_error( "fineShiftImageInSpecPoly:","error in spline interpolation!\n") ;
00526 return NullImage ;
00527 }
00528
00529 new_sum = 0. ;
00530 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00531 {
00532 if ( qfits_isnan(corrected_spec[row]) )
00533 {
00534 continue ;
00535 }
00536 new_sum += corrected_spec[row] ;
00537 }
00538
00539 for ( row = 0 ; row < shiftedImage -> ly ; row++ )
00540 {
00541 if ( new_sum == 0. ) new_sum =1. ;
00542 {
00543 if ( qfits_isnan(corrected_spec[row]) )
00544 {
00545 retIm->data[col+row*retIm->lx] = ZERO ;
00546 }
00547 else
00548 {
00549 corrected_spec[row] *= sum / new_sum ;
00550 retIm->data[col+row*retIm->lx] = corrected_spec[row] ;
00551 }
00552 }
00553 }
00554 }
00555 return retIm ;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569 OneCube * alignCubeToReference ( OneCube * cube,
00570 OneImage * refIm,
00571 int order,
00572 int shift_indicator )
00573 {
00574 OneCube * retCube ;
00575 OneImage * shiftedIm ;
00576 OneImage * fineShiftedIm ;
00577 double shift ;
00578 double sub_shift ;
00579 int z;
00580 double * ker ;
00581
00582 if (cube == NullCube)
00583 {
00584 cpl_msg_error("alignCubeToReference:","no input cube given!\n") ;
00585 return NullCube ;
00586 }
00587 if (refIm == NullImage)
00588 {
00589 cpl_msg_error("alignCubeToReference:","no input ref. image given!\n") ;
00590 return NullCube ;
00591 }
00592
00593
00594 retCube = (OneCube*)cpl_malloc(sizeof(OneCube)) ;
00595 retCube->plane = (OneImage**)cpl_calloc(cube->np , sizeof(OneImage*)) ;
00596
00597 retCube->lx = cube->lx ;
00598 retCube->ly = cube->ly ;
00599 retCube->np = cube->np ;
00600 retCube->nbpix = (ulong32)cube->lx * (ulong32)cube->ly * (ulong32)cube->np ;
00601 retCube->history = (char*)NULL ;
00602 retCube->n_comments = 0 ;
00603 retCube->orig_ptype = CPL_BPP_DEFAULT ;
00604 retCube->filename = NULL ;
00605
00606 if ( shift_indicator != 0 && shift_indicator != 1 )
00607 {
00608 ker = generate_interpolation_kernel("tanh") ;
00609 if (ker == NULL)
00610 {
00611 cpl_msg_error("shiftArray:","kernel generation failure: aborting resampling") ;
00612 return NULL ;
00613 }
00614 }
00615
00616 for ( z = 0 ; z < cube->np ; z++ )
00617 {
00618
00619 if (qfits_isnan( shift = determineShiftByCorrelation ( refIm, cube->plane[z] ) ) )
00620 {
00621 cpl_msg_error("alignCubeToReference:","error in determineShiftByCorrelation()!\n") ;
00622 return NullCube ;
00623 }
00624
00625 if ( NullImage == (shiftedIm = shiftImageInSpec ( cube->plane[z], shift, &sub_shift )) )
00626 {
00627 cpl_msg_error("alignCubeToReference:","error in shiftImageInSpec()!\n") ;
00628 return NullCube ;
00629 }
00630 if ( shift_indicator == 0 )
00631 {
00632 if ( NullImage == (fineShiftedIm = fineShiftImageInSpecPoly (shiftedIm, sub_shift, order ) ) )
00633 {
00634 cpl_msg_error("alignCubeToReference:","error in fineShiftImageInSpecPoly()!\n") ;
00635 return NullCube ;
00636 }
00637 }
00638 else if ( shift_indicator == 1)
00639 {
00640 if ( NullImage == (fineShiftedIm = fineShiftImageInSpecCubicspline (shiftedIm, sub_shift) ) )
00641 {
00642 cpl_msg_error("alignCubeToReference:","error in fineShiftImageInSpecCubicspline()!\n") ;
00643 return NullCube ;
00644 }
00645 }
00646
00647 else
00648 {
00649 if ( NullImage == (fineShiftedIm = shiftImage (shiftedIm,0. ,sub_shift, ker ) ) )
00650 {
00651 cpl_msg_error("alignCubeToReference:","error in fineShiftImageInSpecCubicspline()!\n") ;
00652 return NullCube ;
00653 }
00654 }
00655 retCube -> plane[z] = copy_image (fineShiftedIm) ;
00656 destroy_image (shiftedIm) ;
00657 destroy_image (fineShiftedIm) ;
00658 }
00659 if ( shift_indicator != 0 && shift_indicator != 1 ) cpl_free(ker) ;
00660 return retCube ;
00661 }
00662
00663