00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 # include <stdio.h>
00019 # include <string.h>
00020 # include <stdlib.h>
00021 # include <math.h>
00022 # include <complex.h>
00023 # include <fftw3.h>
00024
00025 typedef int i4;
00026 typedef float f4;
00027
00028
00029
00030 i4 cross_cor (i4 init, i4 hires, i4 expand, double *arr, double *barr,
00031 double **absccor, i4 nx, i4 ny, double *shiftx, double *shifty,
00032 i4 filterflag, double kr);
00033 i4 interpcc2d (double *fdata, double xmiss, i4 nx, i4 ny,
00034 double *xwant, i4 nxinterp, double *ywant, i4 nyinterp, double **finterp);
00035 i4 gaussfilt(double *filter, double *kx, double *ky, i4 nx, i4 ny, double kr);
00036 i4 shift2d (double *arr, i4 nx, i4 ny, i4 ishift, i4 jshift);
00037 i4 maxloc (double *arr, i4 xsize);
00038 i4 minloc (double *arr, i4 xsize);
00039 i4 make_freq(double *k, i4 ndim);
00040 i4 is_big_endian ();
00041
00042
00043
00044
00045
00046 int flct4jsoc(int *dims, double *bz0, double *bz1,
00047 double deltas, double deltat, double sigma,
00048 int useThresh, double threshb, int doFilter, double kr,
00049 int quiet, int absthresh,
00050 double *vx, double *vy, double *vm)
00051 {
00052 int status = 0;
00053 char *version ="1.01 ";
00054
00055
00056
00057
00058
00059 double deltinv = 1. / deltat;
00060 i4 sigmaeq0 = (sigma > 0.) ? 0 : 1;
00061 double sigminv = (sigmaeq0) ? -999. : (1. / sigma);
00062 i4 hires = -1, verbose = ((quiet) ? 0 : 1), expand = 1;
00063 double thresh = (useThresh) ? fabs(threshb) : 0.;
00064 i4 filter = doFilter;
00065 i4 poffset = 0, qoffset = 0, skip = 0, skipon = 0;
00066 i4 skipxy, noskipx, noskipy, noskipxy;
00067 i4 xoffset = 0, yoffset = 0, interpolate = 0;
00068 skipon = skip + abs(qoffset) + abs(poffset);
00069 i4 ibe = is_big_endian();
00070 char *aloc = NULL;
00071 if (absthresh) aloc = (char *) malloc(sizeof(char));
00072 char *ploc = NULL, *qloc = NULL, *intloc = NULL;
00073 double xmiss = -999999.;
00074
00075 if (verbose) {
00076 printf("flct: Version %s Copyright: 2007,2008 University of California\n",
00077 version);
00078 if (ibe)
00079 printf ("flct: large endian machine; i/o not byteswapped\n");
00080 else
00081 printf ("flct: small endian machine; i/o will be byteswapped\n");
00082 printf ("flct: deltat = %g\n", deltat);
00083 printf ("flct: deltas = %g\n", deltas);
00084 printf ("flct: sigma = %g\n", sigma);
00085 printf ("flct: threshold image value for LCT is %g\n", thresh);
00086 if (aloc)
00087 printf ("flct: threshold forced to be in abs. units\n");
00088 if (skipon)
00089 printf ("flct: skip = %d pixels with p=%d, q=%d\n",skip, poffset, qoffset);
00090 if (skipon && ((poffset < 0) || (qoffset < 0)))
00091 printf ("flct: p=%d, q=%d: negative p,q will be reset to skip-|p,q|\n",
00092 poffset, qoffset);
00093 if (interpolate)
00094 printf ("flct: skipped pixels interpolated with cubic convolution\n");
00095 if (filter)
00096 printf ("flct: filter rolloff value for input images is %g\n", kr);
00097 if (hires == 0) printf ("flct: hires option turned on\n");
00098 }
00099 if (poffset < 0) poffset = skip - abs(poffset);
00100 if (qoffset < 0) qoffset = skip - abs(qoffset);
00101
00102
00103
00104 i4 ier = 0, transp = 1;
00105 int nx = dims[1], ny = dims[0], nxny = nx * ny;
00106 i4 nxorig = nx, nyorig = ny;
00107 double *f1 = bz0, *f2 = bz1;
00108
00109 if (verbose) {
00110 printf ("flct: from input file, nx = %d, ny = %d\n", nx, ny);
00111 if ((skip >= nx) || (skip >= ny))
00112 {
00113 printf("flct: skip = %d is too big compared to nx or ny, fatal\n", skip);
00114 return 1;
00115 }
00116 printf("bz0[0]=%f\n", f1[0]);
00117 printf("bz0[33]=%f\n", f1[33]);
00118 }
00119
00120
00121
00122 i4 nt, ndmin, nsize = 0;
00123 double tol = 1e-2;
00124 if (!sigmaeq0) {
00125 nt = (i4)sigma * sqrt(log (1. / tol));
00126 ndmin = (nx < ny) ? (((nx / 3) / 2)) * 2 : (((ny / 3) / 2)) * 2;
00127 nsize = ((nt / 2) * 2 < ndmin) ? (nt / 2) * 2 : ndmin;
00128 if (verbose) printf ("flct: nominal sliding box size = %d\n",
00129 2 * nsize);
00130 if(nsize <= 0) {
00131 printf("flct: error - illegal box size, exiting\n");
00132 return 1;
00133 }
00134 } else {
00135 nx = 1; ny = 1;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145 if (!sigmaeq0) {
00146 if ((thresh > 0.) && (thresh < 1.) && (aloc == NULL)) {
00147 double *f1temp = (double *) malloc (sizeof (double) * nx * ny);
00148 double *f2temp = (double *) malloc (sizeof (double) * nx * ny);
00149 for (int i = 0; i < nx * ny; i++) {
00150
00151
00152 *(f1temp + i) = (double) fabs (*(f1 + i));
00153 *(f2temp + i) = (double) fabs (*(f2 + i));
00154 }
00155
00156 i4 iloc1 = maxloc (f1temp, nx * ny);
00157 i4 iloc2 = maxloc (f2temp, nx * ny);
00158 double f1max = *(f1temp + iloc1);
00159 double f2max = *(f2temp + iloc2);
00160 double fmax = (f1max > f2max) ? f1max : f2max;
00161
00162 thresh *= fmax;
00163 if (verbose)
00164 printf ("flct: relative threshold in abs. units = %g\n", thresh);
00165 free (f1temp); free (f2temp);
00166 }
00167 }
00168 if (aloc) free(aloc);
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 double *gaussdata = (double *) malloc (sizeof (double) * (2 * nxorig) * (2 * nyorig));
00182 if (!sigmaeq0) {
00183 for (int i = 0; i < 2 * nxorig; i++) {
00184 double argx = sigminv * (double) (i - nxorig);
00185 for (int j = 0; j < 2 * nyorig; j++) {
00186 double argy = sigminv * (double) (j - nyorig);
00187 *(gaussdata + i * (2 * ny) + j) = exp (-argx * argx - argy * argy);
00188 }
00189 }
00190 } else {
00191 for (int i = 0; i < 2 * nxorig; i++) {
00192 for (int j = 0; j < 2 * nyorig; j++) {
00193 *(gaussdata + i * (2 * nyorig) + j) = (double) 1.;
00194 }
00195 }
00196 }
00197
00198
00199
00200 i4 init, icc;
00201 i4 hardworkneeded, belowthresh;
00202 i4 imin0, jmax0, jmin0, imax0, imin, jmin, imax, jmax, isize, jsize;
00203 double fabsbar, f1bar=0., f2bar=0., vxx=0., vyy=0., vmask;
00204 double shiftx, shifty;
00205 double *g1, *g2, *absccor;
00206
00207 for (int i = 0; i < nx; i++) {
00208 for (int j = 0; j < ny; j++) {
00209
00210 if ((nx == 1) && (ny == 1)) {
00211 init = 2;
00212 } else if ((i == 0) && (j == 0) && ((nx + ny) > 2)) {
00213
00214
00215 init = 1;
00216 }
00217 else if ((i == (nx - 1)) && (j == (ny - 1)) && ((nx+ny) > 2)) {
00218
00219
00220
00221 init = -1;
00222 } else {
00223
00224 init = 0;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233 fabsbar = 0.5 * (fabs (*(f1 + i * ny + j) + *(f2 + i * ny + j)));
00234 belowthresh = (fabsbar < thresh);
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 if (skipon) {
00247 if (transp) {
00248 xoffset=qoffset;
00249 yoffset=poffset;
00250 } else {
00251 xoffset=poffset;
00252 yoffset=qoffset;
00253 }
00254 noskipx = !((i - xoffset) % skip);
00255 noskipy = !((j - yoffset) % skip);
00256 noskipxy = noskipx * noskipy;
00257 } else {
00258 noskipxy=1;
00259 }
00260
00261 hardworkneeded = (((!belowthresh) && (noskipxy)) || (init != 0));
00262
00263 if (hardworkneeded) {
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 if (!sigmaeq0) {
00278
00279 imin0 = (0 > (i - (nsize - 1))) ? 0 : i - (nsize - 1);
00280 imax0 = ((nx - 1) < (i + nsize)) ? nx - 1 : i + nsize;
00281 imin = (imax0 == nx - 1) ? nx - 1 - (2 * nsize - 1) : imin0;
00282 imax = (imin0 == 0) ? 0 + (2 * nsize - 1) : imax0;
00283
00284 jmin0 = (0 > (j - (nsize - 1))) ? 0 : j - (nsize - 1);
00285 jmax0 = ((ny - 1) < (j + nsize)) ? ny - 1 : j + nsize;
00286 jmin = (jmax0 == ny - 1) ? ny - 1 - (2 * nsize - 1) : jmin0;
00287 jmax = (jmin0 == 0) ? 0 + (2 * nsize - 1) : jmax0;
00288
00289 isize = imax - imin + 1;
00290 jsize = jmax - jmin + 1;
00291
00292
00293
00294
00295 if (isize != 2 * nsize) {
00296 printf ("flct: exiting, bad isize = %d\n", isize);
00297 return 1;
00298 }
00299 if (jsize != 2 * nsize) {
00300 printf ("flct: exiting, bad jsize = %d\n", jsize);
00301 return 1;
00302 }
00303
00304 } else {
00305
00306 isize = nxorig;
00307 jsize = nyorig;
00308 imin = 0;
00309 jmin = 0;
00310
00311 }
00312
00313
00314
00315 f1bar = 0.;
00316 f2bar = 0.;
00317 for (int ii = 0; ii < isize; ii++) {
00318 for (int jj = 0; jj < jsize; jj++) {
00319 f1bar=f1bar + *(f1 + (ii+imin)*nyorig + (jj+jmin));
00320 f2bar=f2bar + *(f2 + (ii+imin)*nyorig + (jj+jmin));
00321 }
00322 }
00323
00324 f1bar = f1bar / ((double)isize*jsize);
00325 f2bar = f2bar / ((double)isize*jsize);
00326
00327 g1 = (double *) malloc (sizeof (double) * isize * jsize);
00328 g2 = (double *) malloc (sizeof (double) * isize * jsize);
00329
00330
00331
00332
00333
00334 for (int ii = 0; ii < isize; ii++) {
00335 for (int jj = 0; jj < jsize; jj++) {
00336 *(g1 + ii * jsize + jj) =
00337 *(gaussdata + (nxorig-i+(ii+imin))*2*nyorig
00338 + nyorig-j+(jj+jmin)) *
00339 (*(f1 + (ii + imin) * nyorig + (jj + jmin))-f1bar) ;
00340 *(g2 + ii * jsize + jj) =
00341 *(gaussdata + (nxorig-i+(ii+imin))*2*nyorig
00342 + nyorig-j+(jj+jmin)) *
00343 (*(f2 + (ii + imin) * nyorig + (jj + jmin))-f2bar) ;
00344 }
00345 }
00346
00347
00348
00349
00350
00351 icc = cross_cor (init, hires, expand, g1, g2, &absccor,
00352 isize, jsize, &shiftx, &shifty, filter, kr);
00353
00354
00355
00356
00357 free (g1);
00358 free (g2);
00359 free (absccor);
00360
00361
00362
00363
00364
00365
00366
00367
00368 if (transp) {
00369 vxx = shifty * deltinv * deltas;
00370 vyy = shiftx * deltinv * deltas;
00371 } else {
00372 vxx = shiftx * deltinv * deltas;
00373 vyy = shifty * deltinv * deltas;
00374 }
00375
00376
00377
00378 }
00379
00380
00381
00382 vmask = 1.;
00383
00384 if ((belowthresh || !noskipxy) && !sigmaeq0) {
00385
00386
00387
00388
00389
00390 vxx = xmiss;
00391 vyy = xmiss;
00392 vmask = 0.;
00393 }
00394
00395 if ((j == 0) && (verbose)) {
00396 printf ("flct: progress i = %d out of %d\r", i, nx - 1);
00397 fflush (stdout);
00398 }
00399
00400 *(vx + i * ny + j) = vxx;
00401 *(vy + i * ny + j) = vyy;
00402 *(vm + i * ny + j) = vmask;
00403 if (verbose && sigmaeq0) {
00404 printf("\nflct: vx = %g vy = %g \n",vxx,vyy);
00405 fflush(stdout);
00406 }
00407
00408 }
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 for (int i=0; i < nx; i++) {
00418 for(int j=0; j < ny; j++) {
00419 if (*(vx+i*ny+j) == xmiss) *(vm+i*ny+j) = 0.;
00420 if (*(vx+i*ny+j) == xmiss) *(vx+i*ny+j) = 0.;
00421 if (*(vy+i*ny+j) == xmiss) *(vy+i*ny+j) = 0.;
00422 }
00423 }
00424
00425
00426 free (gaussdata);
00427
00428 if (verbose)
00429 printf ("\nflct: finished\n");
00430
00431 return 0;
00432
00433 }
00434
00435
00436
00437
00438
00439 i4 cross_cor (i4 init, i4 hires, i4 expand, double *arr, double *barr,
00440 double **absccor, i4 nx, i4 ny, double *shiftx, double *shifty,
00441 i4 filterflag, double kr)
00442 {
00443
00444
00445
00446
00447 i4 i, j, ixx, iyy, maxind, ixmax, iymax, ishft, maxfine, absccmax;
00448 i4 nxinterp, nyinterp, nfgppergp;
00449 double normfac, rangex, rangey, shiftx0, shifty0, shiftxx, shiftyy;
00450 double *xwant, *ywant, *peakarea;
00451 double shiftsubx, shiftsuby, fx, fy, fxx, fyy, fxy;
00452 double xmiss=0.;
00453
00454
00455
00456 static double *ina, *inb, *ccor;
00457 static double *filter, *kx, *ky;
00458 static fftw_complex *outa, *outb, *ccorconj;
00459 static fftw_plan pa, pb, pback;
00460
00461
00462
00463
00464
00465
00466
00467 *absccor = malloc (sizeof (double) * nx * ny);
00468
00469
00470
00471 if (hires == 1)
00472 {
00473 nxinterp = 101;
00474 nyinterp = 101;
00475 nfgppergp = 50;
00476 }
00477 else
00478 {
00479 nxinterp = 21;
00480 nyinterp = 21;
00481 nfgppergp = 10;
00482 }
00483
00484 if ((init == 1) || (init == 2))
00485 {
00486
00487
00488
00489
00490
00491
00492
00493
00494 outa = (fftw_complex *) fftw_malloc (sizeof (fftw_complex) *
00495 nx * ((ny / 2) + 2));
00496 outb = (fftw_complex *) fftw_malloc (sizeof (fftw_complex) *
00497 nx * ((ny / 2) + 2));
00498 ccorconj = (fftw_complex *) fftw_malloc (sizeof (fftw_complex) *
00499 nx * ((ny / 2) + 2));
00500
00501 ina = (double *) fftw_malloc (sizeof (double) * nx * ny);
00502 inb = (double *) fftw_malloc (sizeof (double) * nx * ny);
00503 ccor = (double *) fftw_malloc (sizeof (double) * nx * ny);
00504 filter = (double *) fftw_malloc (sizeof (double) * nx * ny);
00505 kx=(double *) fftw_malloc (sizeof(double)*nx);
00506 ky=(double *) fftw_malloc (sizeof(double)*ny);
00507 if(filterflag)
00508 {
00509 make_freq(kx,nx);
00510 make_freq(ky,ny);
00511 gaussfilt(filter,kx,ky,nx,ny,kr);
00512 }
00513
00514 for (i = 0; i < nx * ny; i++)
00515 {
00516 *(ina + i) = (double) 0.;
00517 *(inb + i) = (double) 0.;
00518 }
00519 for (i = 0; i < nx * ((ny / 2 + 1)); i++)
00520 {
00521 *(ccorconj+i)=0.;
00522
00523
00524
00525
00526
00527
00528
00529
00530 }
00531 pa = fftw_plan_dft_r2c_2d (nx, ny, ina, outa, FFTW_MEASURE);
00532 pb = fftw_plan_dft_r2c_2d (nx, ny, inb, outb, FFTW_MEASURE);
00533 pback = fftw_plan_dft_c2r_2d (nx, ny, ccorconj, ccor, FFTW_MEASURE);
00534 }
00535
00536 for (i = 0; i < nx * ny; i++)
00537
00538 {
00539
00540
00541
00542
00543
00544 *(ina + i) = (double) (*(arr + i));
00545 *(inb + i) = (double) (*(barr + i));
00546 }
00547
00548
00549
00550 fftw_execute (pa);
00551 fftw_execute (pb);
00552
00553
00554
00555 normfac = (1. / ((double) nx * ny));
00556 normfac *= normfac;
00557
00558
00559
00560 if(filterflag)
00561 {
00562 for (i=0;i<nx;i++)
00563 {
00564 for (j=0;j<(ny/2)+1;j++)
00565 {
00566
00567
00568
00569
00570
00571
00572 outa[i*((ny/2)+1)+j]=outa[i*((ny/2)+1)+j]*filter[i*ny+j];
00573 outb[i*((ny/2)+1)+j]=outb[i*((ny/2)+1)+j]*filter[i*ny+j];
00574
00575 }
00576 }
00577 }
00578
00579
00580
00581 for (i = 0; i < nx * ((ny/2) + 1); i++)
00582 {
00583
00584 *(ccorconj+i)=(conj(*(outa+i))*(*(outb+i)))*normfac;
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598 }
00599
00600
00601
00602 fftw_execute (pback);
00603
00604
00605
00606 for (i = 0; i < nx * ny; i++)
00607 {
00608 *(*absccor + i) = (double) fabs(*(ccor+i));
00609 }
00610
00611 if ((init == -1) || (init == 2))
00612 {
00613
00614
00615 fftw_free (outa);
00616 fftw_free (outb);
00617 fftw_free (ccorconj);
00618 fftw_free (ccor);
00619 fftw_free (filter);
00620 fftw_free (kx);
00621 fftw_free (ky);
00622 fftw_free (ina);
00623 fftw_free (inb);
00624 fftw_destroy_plan (pa);
00625 fftw_destroy_plan (pback);
00626 fftw_destroy_plan (pb);
00627 }
00628
00629
00630
00631 ishft = shift2d (*absccor, nx, ny, nx / 2, ny / 2);
00632
00633
00634
00635
00636 absccmax=1;
00637 maxind = maxloc (*absccor, nx * ny);
00638 if( *(*absccor+maxind) == (double)0.)
00639 {
00640 absccmax=0;
00641 }
00642 if(absccmax == 1)
00643 {
00644 ixmax = maxind / ny;
00645 iymax = maxind % ny;
00646 }
00647 else
00648 {
00649 ixmax = nx/2;
00650 iymax = ny/2;
00651 }
00652 shiftx0 = ixmax;
00653 shifty0 = iymax;
00654 shiftsubx=0.;
00655 shiftsuby=0.;
00656
00657 if(( expand == 1) && (hires == -1) && (ixmax > 0) && (ixmax < (nx-1))
00658 && (iymax > 0) && (iymax < (ny-1)) && (absccmax == 1))
00659 {
00660 fx=0.5* ( *(*absccor+(ixmax+1)*ny+iymax) -
00661 *(*absccor+(ixmax-1)*ny+iymax) );
00662 fy=0.5* ( *(*absccor+ixmax*ny+iymax+1) - *(*absccor+ixmax*ny+iymax-1) );
00663 fxx = ( *(*absccor+(ixmax+1)*ny+iymax)+ *(*absccor+(ixmax-1)*ny+iymax)
00664 -2.*( *(*absccor+ixmax*ny+iymax)) );
00665 fyy = ( *(*absccor+ixmax*ny+iymax+1) + *(*absccor+ixmax*ny+iymax-1)
00666 -2.*( *(*absccor+ixmax*ny+iymax)) );
00667 fxy = 0.25*( *(*absccor+(ixmax+1)*ny+iymax+1) +
00668 *(*absccor+(ixmax-1)*ny+iymax-1) -
00669 *(*absccor+(ixmax+1)*ny+iymax-1) -
00670 *(*absccor+(ixmax-1)*ny+iymax+1) );
00671
00672 shiftsubx=(fyy*fx-fy*fxy)/(fxy*fxy-fxx*fyy);
00673 shiftsuby=(fxx*fy-fx*fxy)/(fxy*fxy-fxx*fyy);
00674 }
00675
00676 shiftxx=shiftx0 + shiftsubx;
00677 shiftyy=shifty0 + shiftsuby;
00678
00679
00680
00681
00682
00683
00684
00685 if(hires != -1)
00686 {
00687
00688 rangex = (double) (nxinterp - 1) / nfgppergp;
00689 rangey = (double) (nyinterp - 1) / nfgppergp;
00690
00691 xwant = (double *) malloc (sizeof (double) * nxinterp);
00692 ywant = (double *) malloc (sizeof (double) * nyinterp);
00693
00694 for (i = 0; i < nxinterp; i++)
00695 {
00696 *(xwant + i) = ((((double) i) * rangex) / ((double) (nxinterp - 1)))
00697 - 0.5 * rangex + shiftx0;
00698
00699 }
00700 for (j = 0; j < nyinterp; j++)
00701 {
00702 *(ywant + j) = ((((double) j) * rangey) / ((double) (nyinterp - 1)))
00703 - 0.5 * rangey + shifty0;
00704
00705 }
00706
00707
00708
00709 interpcc2d (*absccor, xmiss, nx, ny, xwant, nxinterp, ywant,
00710 nyinterp, &peakarea);
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722 maxfine = maxloc (peakarea, nxinterp * nyinterp);
00723 ixx = maxfine / nyinterp;
00724 iyy = maxfine % nyinterp;
00725
00726 shiftsubx=0.;
00727 shiftsuby=0.;
00728 if((expand == 1) && (ixx > 0) && (ixx < (nxinterp-1)) && (iyy > 0)
00729 && (iyy < (nyinterp-1)))
00730 {
00731 fx=0.5* ( *(peakarea+(ixx+1)*nyinterp+iyy) -
00732 *(peakarea+(ixx-1)*nyinterp+iyy) );
00733 fy=0.5* ( *(peakarea+ixx*nyinterp+iyy+1) -
00734 *(peakarea+ixx*nyinterp+iyy-1) );
00735 fxx = ( *(peakarea+(ixx+1)*nyinterp+iyy)+
00736 *(peakarea+(ixx-1)*nyinterp+iyy)
00737 -2.*( *(peakarea+ixx*nyinterp+iyy)) );
00738 fyy = ( *(peakarea+ixx*nyinterp+iyy+1) + *(peakarea+ixx*nyinterp+iyy-1)
00739 -2.*( *(peakarea+ixx*nyinterp+iyy)) );
00740 fxy = 0.25*( *(peakarea+(ixx+1)*nyinterp+iyy+1) +
00741 *(peakarea+(ixx-1)*nyinterp+iyy-1) -
00742 *(peakarea+(ixx+1)*nyinterp+iyy-1) -
00743 *(peakarea+(ixx-1)*nyinterp+iyy+1) );
00744
00745 shiftsubx=((fyy*fx-fy*fxy)/(fxy*fxy-fxx*fyy))*
00746 rangex/((double) (nxinterp -1));
00747 shiftsuby=((fxx*fy-fx*fxy)/(fxy*fxy-fxx*fyy))*
00748 rangey/((double) (nyinterp -1));
00749 }
00750 shiftxx = *(xwant + ixx) + shiftsubx;
00751 shiftyy = *(ywant + iyy) + shiftsuby;
00752
00753 free (xwant);
00754 free (ywant);
00755 free (peakarea);
00756 }
00757
00758
00759
00760 *shiftx = shiftxx - (double) (nx / 2);
00761 *shifty = shiftyy - (double) (ny / 2);
00762
00763
00764
00765
00766
00767
00768
00769 return 0;
00770 }
00771
00772
00773
00774
00775 i4 gaussfilt(double *filter, double *kx, double *ky, i4 nx, i4 ny, double kr)
00776 {
00777 double kxmax,kymax,kxroll,kyroll,smxinv,smyinv,argx,argy;
00778 i4 i,j;
00779 kxmax=(double)kx[nx/2];
00780 kymax=(double)ky[ny/2];
00781 kxroll=kr*kxmax;
00782 kyroll=kr*kymax;
00783 smxinv=(double)1./kxroll;
00784 smyinv=(double)1./kyroll;
00785 for (i=0;i<nx;i++)
00786 {
00787 argx=kx[i]*smxinv;
00788 for(j=0;j<ny;j++)
00789 {
00790 argy=ky[j]*smyinv;
00791 filter[i*ny+j]=exp( -(argx*argx + argy*argy) );
00792 }
00793 }
00794 return 0;
00795 }
00796
00797
00798
00799
00800
00801
00802 i4 shift2d (double *arr, i4 nx, i4 ny, i4 ishift, i4 jshift)
00803 {
00804 double *temp;
00805 i4 i, j, ii, jj;
00806 temp = (double *) malloc (sizeof (double) * nx * ny);
00807 for (i = 0; i < nx; i++)
00808 {
00809 ii = (i + ishift) % nx;
00810
00811 for (j = 0; j < ny; j++)
00812 {
00813 jj = (j + jshift) % ny;
00814
00815
00816
00817 *(temp + ii * ny + jj) = *(arr + i * ny + j);
00818 }
00819 }
00820
00821
00822
00823 memcpy ((void *) arr, (void *) temp, nx * ny * sizeof (double));
00824 free (temp);
00825 return 0;
00826 }
00827
00828
00829
00830
00831 i4 maxloc (double *arr, i4 xsize)
00832 {
00833 i4 i, location;
00834 double amax;
00835
00836 amax = *(arr + 0);
00837 location = 0;
00838 for (i = 1; i < xsize; i++)
00839 {
00840 if (*(arr + i) > amax)
00841 {
00842 amax = *(arr + i);
00843 location = i;
00844 }
00845 }
00846 return location;
00847 }
00848
00849
00850
00851 i4 minloc (double *arr, i4 xsize)
00852 {
00853 i4 i, location;
00854 double amin;
00855
00856 amin = *(arr + 0);
00857 location = 0;
00858 for (i = 1; i < xsize; i++)
00859 {
00860 if (*(arr + i) < amin)
00861 {
00862 amin = *(arr + i);
00863 location = i;
00864 }
00865 }
00866 return location;
00867 }
00868
00869
00870
00871
00872 i4 make_freq(double *k, i4 ndim)
00873 {
00874
00875 i4 n21,i,inext;
00876 n21=(ndim/2)-1;
00877 for (i=0;i<n21+1;i++)
00878 {
00879 k[i]=(double)i;
00880 }
00881
00882 inext=n21+1;
00883 if((ndim/2)*2 != ndim)
00884 {
00885 k[inext]=(double)(ndim/2);
00886 inext++;
00887 k[inext]=-(double)(ndim/2);
00888 inext++;
00889 }
00890
00891 else
00892 {
00893 k[inext]=(double)(ndim/2);
00894 inext++;
00895 }
00896
00897 for (i=inext;i<ndim;i++)
00898 {
00899 k[i]=-(double)(n21-(i-inext));
00900 }
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912 return 0;
00913 }
00914
00915
00916
00917 i4 interpcc2d (double *fdata, double xmiss, i4 nx, i4 ny,
00918 double *xwant, i4 nxinterp, double *ywant, i4 nyinterp, double **finterp)
00919 {
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943 double *cdata;
00944
00945 double txt, tyt, xint, yint, ftmp;
00946
00947
00948
00949
00950 double tx, ty, rx, ry;
00951 i4 i, ii, j, jj, itemp, jtemp, izero, jzero, databad;
00952
00953
00954
00955
00956
00957 cdata = (double *) malloc (sizeof (double) * (nx + 2) * (ny + 2));
00958
00959
00960
00961 for (i = 0; i < nx; i++)
00962 {
00963 for (j = 0; j < ny; j++)
00964 {
00965 *(cdata + (i + 1)*(ny + 2) + (j + 1)) = *(fdata + i*ny + j);
00966 }
00967 }
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985 for (j = 0; j < ny; j++)
00986 {
00987
00988
00989
00990 *(cdata + 0*(ny + 2) + (j+1)) = *(fdata + 2*ny + j)
00991 - 3. * (*(fdata + 1*ny + j)) + 3. * (*(fdata + 0*ny + j));
00992
00993 *(cdata + (nx + 1)*(ny + 2) + (j + 1)) = *(fdata + (nx - 3)*ny + j)
00994 - 3. * (*(fdata + (nx - 2)*ny + j)) + 3. * (*(fdata + (nx - 1)*ny + j));
00995 }
00996 for (i = 0; i < nx; i++)
00997 {
00998
00999
01000
01001 *(cdata + (i + 1)*(ny + 2) + 0) = *(fdata + i*ny + 2)
01002 - 3. * (*(fdata + i*ny + 1)) + 3. * (*(fdata + i*ny + 0));
01003
01004 *(cdata + (i + 1)*(ny + 2) + ny + 1) = *(fdata + i*ny + ny - 3)
01005 - 3. * (*(fdata + i*ny + ny - 2)) + 3. * (*(fdata + i*ny + ny - 1));
01006 }
01007
01008
01009
01010 *(cdata + 0*(nx + 2) + 0) =
01011 3. * (*(cdata + 1*(ny + 2) + 0)) -
01012 3. * (*(cdata + 2*(ny + 2) + 0)) + *(cdata + 3*(ny + 2) + 0);
01013
01014 *(cdata + (nx + 1)*(ny + 2) + 0) =
01015 3. * (*(cdata + nx*(ny + 2) + 0)) -
01016 3. * (*(cdata + (nx - 1)*(ny + 2) + 0)) + *(cdata +
01017 (nx - 2)*(ny + 2) + 0);
01018
01019 *(cdata + 0*(ny + 2) + ny + 1) =
01020 3. * (*(cdata + 0*(ny + 2) + ny)) -
01021 3. * (*(cdata + 0*(ny + 2) + ny - 1)) + *(cdata + 0*(ny + 2) + ny - 2);
01022
01023 *(cdata + (nx + 1)*(ny + 2) + ny + 1) =
01024 3. * (*(cdata + nx*(ny + 2) + ny + 1)) -
01025 3. * (*(cdata + (nx - 1)*(ny + 2) + ny + 1)) + *(cdata +
01026 (nx - 2)*(ny + 2) +
01027 ny + 1);
01028
01029
01030
01031 *finterp = (double *) malloc (sizeof (double) * nxinterp * nyinterp);
01032
01033
01034
01035 for (i = 0; i < nxinterp; i++)
01036 {
01037
01038
01039 xint = *(xwant + i);
01040
01041
01042
01043 itemp = ((i4) xint > 0) ? (i4) xint : 0;
01044 izero = (itemp < (nx - 2)) ? itemp : nx - 2;
01045 for (j = 0; j < nyinterp; j++)
01046 {
01047
01048
01049 yint = *(ywant + j);
01050 if ((yint < 0.) || (yint > (double) (ny - 1))
01051 || ((xint < 0) || (xint > (double) (nx - 1))))
01052 {
01053
01054
01055
01056
01057
01058
01059 *(*finterp + i * nyinterp + j) = xmiss;
01060 }
01061 else
01062 {
01063
01064
01065 jtemp = ((i4) yint > 0) ? (i4) yint : 0;
01066 jzero = (jtemp < (ny - 2)) ? jtemp : ny - 2;
01067
01068
01069
01070 ftmp = (double) 0.;
01071
01072
01073
01074
01075 databad=0;
01076 for (ii = -1; ii < 3; ii++)
01077 {
01078 txt = xint - (double) (izero + ii);
01079 tx = (double) fabs (txt);
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089 rx = (tx >= (double) 1.0) ?
01090 (((((double) (-0.5)) * tx +
01091 ((double) 2.5)) * tx) -
01092 (double) 4.) * tx + (double) 2. :
01093 (((double) (1.5)) * tx -
01094 ((double) (2.5))) * tx * tx + (double) 1.;
01095
01096 for (jj = -1; jj < 3; jj++)
01097 {
01098
01099 tyt = yint - (double) (jzero + jj);
01100 ty = (double) fabs (tyt);
01101
01102
01103
01104
01105 ry = (ty >= (double) 1.0) ?
01106 (((((double) (-0.5)) * ty +
01107 ((double) 2.5)) * ty) -
01108 (double) 4.) * ty + (double) 2. :
01109 (((double) (1.5)) * ty -
01110 ((double) (2.5))) * ty * ty + (double) 1.;
01111
01112
01113
01114
01115
01116
01117
01118 ftmp = ftmp +
01119 *(cdata + (izero + 1 + ii)*(ny + 2)
01120 + jzero + 1 + jj) * rx*ry;
01121 if( *(cdata+(izero+1+ii)*(ny+2)+jzero+1+jj) == xmiss)
01122 databad=1;
01123 }
01124 }
01125
01126
01127 if(databad)
01128 {
01129
01130
01131
01132 *(*finterp + i*nyinterp + j) = xmiss;
01133 }
01134 else
01135 {
01136 *(*finterp + i*nyinterp + j) = ftmp;
01137 }
01138 }
01139 }
01140 }
01141
01142
01143
01144
01145
01146
01147
01148
01149 free (cdata);
01150
01151
01152
01153 return 0;
01154 }
01155
01156
01157
01158
01159
01160 i4 is_big_endian ()
01161 {
01162 const unsigned char fakeword[4] = { 0xFF, 0x00, 0xFF, 0x00 };
01163 i4 realword = *((i4 *) fakeword);
01164 if (realword == 0xFF00FF00)
01165 {
01166 return 1;
01167 }
01168 else
01169 {
01170 return 0;
01171 }
01172 }