00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <math.h>
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 int autoweed_vel (int *n, double *l, double *ux, double *uy, int *mask,
00052 int npts) {
00053
00054
00055
00056
00057
00058
00059 const int maxn = 8;
00060 const double tol = 5.0;
00061
00062 int i, j, offset;
00063 int n_num[maxn];
00064 double num;
00065 double sumx, sumxx, sumy, sumyy, meanx, meany, stdx, stdy;
00066 double range, uxmin, uxmax, uymin, uymax;
00067 double lmin,lmax;
00068
00069 for (i=0; i<maxn; i++) n_num[i] = 0;
00070 for (i=0; i<npts; i++)
00071 if (n[i] < maxn) n_num[n[i]]++;
00072
00073 offset = 0;
00074 for (i=0; i<maxn; i++) {
00075 num = 0.0;
00076 sumx = 0.0;
00077 sumy = 0.0;
00078 sumxx = 0.0;
00079 sumyy = 0.0;
00080
00081 lmin = lmax = l[offset];
00082 for (j=offset; j < n_num[i] + offset; j++) {
00083 if (l[j] < lmin) lmin = l[j];
00084 if (l[j] > lmax) lmax = l[j];
00085 }
00086 range = 0.2 * (lmax - lmin);
00087 lmin += 2.0 * range;
00088 lmax -= 2.0 * range;
00089 for (j=offset; j < n_num[i] + offset; j++) {
00090 if ((l[j] <= lmax) && (l[j] >= lmin)) {
00091 sumx += ux[j];
00092 sumxx += ux[j]*ux[j];
00093 sumy += uy[j];
00094 sumyy += uy[j]*uy[j];
00095 num++;
00096 }
00097 }
00098 meanx = sumx / num;
00099 meany = sumy / num;
00100 stdx = num*sumxx - sumx*sumx;
00101 stdy = num*sumyy - sumy*sumy;
00102 stdx /= (num * (num - 1));
00103 stdy /= (num * (num - 1));
00104 stdx = sqrt (stdx);
00105 stdy = sqrt (stdy);
00106 uxmin = meanx - 5.0 * stdx;
00107 uxmax = meanx + 5.0 * stdx;
00108 uymin = meany - 5.0 * stdy;
00109 uymax = meany + 5.0 * stdy;
00110
00111 for (j=offset; j<n_num[i]+offset; j++) {
00112 if (ux[j] <= uxmax && ux[j] >= uxmin &&
00113 uy[j] <= uymax && uy[j] >= uymin) mask[j]=1;
00114 else mask[j]=0;
00115 }
00116 offset +=n_num[i];
00117 }
00118 return 0;
00119 }
00120
00121 int read_fit (FILE *fpt, int *npts, int **n, double **l, double **f,
00122 double **ef) {
00123 int i, nlines;
00124 char buffer[8192];
00125 if (ferror (fpt)) return -1;
00126 if (feof (fpt)) rewind (fpt);
00127
00128 nlines = 0;
00129 while (!feof(fpt)) {
00130 fgets (buffer, 8192, fpt);
00131 if (buffer[0] != '#' && !feof (fpt)) nlines++;
00132 }
00133 if (nlines == 0) return 1;
00134
00135 *n = (int *)malloc (nlines * sizeof (int));
00136 *l = (double *)malloc (nlines * sizeof (double));
00137 *f = (double *)malloc (nlines * sizeof (double));
00138 *ef = (double *)malloc (nlines * sizeof (double));
00139
00140 rewind (fpt);
00141
00142 for (i=0; i<nlines; i++) {
00143 fgets (buffer, 8192, fpt);
00144 while (buffer[0] == '#') fgets( buffer, 8192, fpt);
00145 sscanf (buffer, "%i %lf %*f %lf %lf", &(*n)[i], &(*l)[i], &(*f)[i],
00146 &(*ef)[i]);
00147 }
00148 *npts = nlines;
00149
00150 return 0;
00151 }
00152
00153 int read_fit_v (FILE *fpt, int *npts, int **n, double **l, double **f,
00154 double **ef, double **ux, double **eux, double **uy, double **euy) {
00155 int i, nlines;
00156 char buffer[8192];
00157 if (ferror (fpt)) return -1;
00158 if (feof (fpt)) rewind (fpt);
00159
00160 nlines = 0;
00161 while (!feof (fpt)) {
00162 fgets (buffer, 8192, fpt);
00163 if (buffer[0] != '#' && !feof (fpt)) nlines++;
00164 }
00165 if (nlines == 0) return 1;
00166
00167 *n = (int *)realloc (*n, nlines * sizeof (int));
00168 *l = (double *)realloc (*l, nlines * sizeof (double));
00169 *f = (double *)realloc (*f, nlines * sizeof (double));
00170 *ef = (double *)realloc (*ef, nlines * sizeof (double));
00171 *ux = (double *)realloc (*ux, nlines * sizeof (double));
00172 *eux = (double *)realloc (*eux, nlines * sizeof (double));
00173 *uy = (double *)realloc (*uy, nlines * sizeof (double));
00174 *euy = (double *)realloc (*euy, nlines * sizeof (double));
00175
00176 rewind (fpt);
00177
00178 for (i=0; i<nlines; i++) {
00179 fgets (buffer, 8192, fpt);
00180 while (buffer[0] == '#') fgets (buffer, 8192, fpt);
00181 sscanf (buffer, "%i %lf %*f %lf %lf %lf %lf %lf %lf", &(*n)[i], &(*l)[i],
00182 &(*f)[i], &(*ef)[i], &(*ux)[i], &(*eux)[i], &(*uy)[i], &(*euy)[i]);
00183 }
00184 *npts = nlines;
00185
00186 return 0;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 void line (int m, double *x, double *y) {
00416 y[0] = x[0];
00417 y[1] = 1.0;
00418 return;
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503 int nearst (double xb, double x[], int ntab) {
00504 int low, igh, mid;
00505
00506 low=0; igh=ntab-1;
00507 if((xb < x[low]) != (xb < x[igh]) ) {
00508
00509
00510
00511 while(igh-low > 1) {
00512 mid=(low+igh)/2;
00513 if((xb < x[mid]) == (xb < x[low])) low=mid;
00514 else igh=mid;
00515 }
00516 }
00517
00518 if(fabs(xb-x[low]) < fabs(xb-x[igh])) return low;
00519 else return igh;
00520 }
00521
00522 int divdif(double xb, double x[], double f[], int *nuse, int ntab,
00523 double fb[], double aeps, double *dfb, double *ddfb) {
00524 int i,j,k,next,in,ip,nit,ier, nmax=10;
00525 double err,px,dpx,ddpx,xn[11],xd[11];
00526
00527
00528
00529 next=nearst(xb,x,ntab);
00530 fb[1]=f[next];
00531 xd[1]=f[next];
00532 xn[1]=x[next];
00533 ier=0;
00534 px=1.0;
00535
00536
00537
00538 *dfb=0.0; *ddfb=0.0;
00539 dpx=0.0; ddpx=0.0;
00540
00541
00542
00543 ip=next; in=next;
00544
00545
00546 nit=*nuse; if(nmax<nit) nit=nmax; if(ntab<nit) nit=ntab;
00547 if(*nuse>nmax || *nuse>ntab) ier=22;
00548 if(*nuse<1) {
00549 ier=21;
00550 nit=6; if(nmax<nit) nit=nmax; if(ntab<nit) nit=ntab;
00551 }
00552 *nuse=1;
00553
00554
00555 for(j=2; j<=nit; ++j) {
00556
00557 if(in<=0 ) {
00558 ip=ip+1; next=ip;
00559 }
00560 else if(ip >= ntab-1) {
00561 in=in-1; next=in;
00562 }
00563 else if(fabs(xb-x[ip+1]) < fabs(xb-x[in-1]) ) {
00564 ip=ip+1; next=ip;
00565 }
00566 else {
00567 in=in-1; next=in;
00568 }
00569
00570
00571 xd[j]=f[next];
00572 xn[j]=x[next];
00573 for(k=j-1; k>=1; --k) xd[k]=(xd[k+1]-xd[k])/(xn[j]-xn[k]);
00574
00575
00576 ddpx=ddpx*(xb-xn[j-1])+2.*dpx;
00577 dpx=dpx*(xb-xn[j-1])+px;
00578 *dfb = *dfb+dpx*xd[1];
00579 *ddfb = *ddfb+ddpx*xd[1];
00580
00581 px=px*(xb-xn[j-1]);
00582 err=xd[1]*px;
00583 fb[j]=fb[j-1]+err;
00584 *nuse=j;
00585
00586 if(fabs(err) < aeps) return ier;
00587 }
00588 return 23;
00589 }
00590
00591 int svd (int n, int m, double *a, double *v, double sigma[], int la, int lv) {
00592 int i, j, k, l, itr, ier, itmax=30;
00593 double f, g, h, rmax, s, r1, r2, c, x, y, z, aeps, eps=1.e-16;
00594
00595 double e[2];
00596
00597 if(n>m || n<=0 || m<=0 || n>la || n>lv) return 105;
00598 ier=0;
00599
00600
00601 g=0.0; rmax=0.0;
00602
00603
00604 for(i=0; i<n; ++i) {
00605
00606 e[i]=g;
00607 s=0.0;
00608 for(j=i; j<m; ++j) s=s+a[i+j*la]*a[i+j*la];
00609 if(s <= 0.0) {
00610
00611 g=0.0;
00612 }
00613 else {
00614 f= a[i+i*la];
00615 g=sqrt(s);
00616 if(f>=0.0) g=-g;
00617 h=f*g-s;
00618 a[i+i*la] = f-g;
00619
00620 for(j=i+1; j<n; ++j) {
00621 s=0.0;
00622 for(k=i; k<m; ++k) s=s+a[i+k*la]*a[j+k*la];
00623 f=s/h;
00624 for(k=i; k<m; ++k) a[j+k*la]= a[j+k*la]+f*a[i+k*la];
00625 }
00626 }
00627
00628
00629 sigma[i]=g;
00630 s=0.0;
00631 for(j=i+1; j<n; ++j) s=s+a[j+i*la]*a[j+i*la];
00632
00633 if(s<= 0.0) g=0.0;
00634 else {
00635 f= a[i*la+(i+1)];
00636 g=sqrt(s);
00637 if(f>= 0.0) g=-g;
00638 h=f*g-s;
00639 a[i*la+(i+1)]=f-g;
00640 for(j=i+1; j<n; ++j) e[j]=a[j+i*la]/h;
00641
00642 for(j=i+1; j<m; ++j) {
00643 s=0.0;
00644 for(k=i+1; k<n; ++k) s=s+a[k+j*la]*a[k+i*la];
00645 for(k=i+1; k<n; ++k) a[k+j*la] = a[k+j*la]+s*e[k];
00646 }
00647 }
00648 r1=fabs(sigma[i])+fabs(e[i]);
00649 if(r1 > rmax) rmax=r1;
00650 }
00651
00652
00653 for(i=n-1; i>=0; --i) {
00654 if(g != 0.0) {
00655 h=g*a[i*la+(i+1)];
00656 for(j=i+1; j<n; ++j) v[i+j*lv]=a[j+i*la]/h;
00657
00658 for(j=i+1; j<n; ++j) {
00659 s=0.0;
00660 for(k=i+1; k<n; ++k) s=s+a[k+i*la]*v[j+k*lv];
00661 for(k=i+1; k<n; ++k) v[j+k*lv]=v[j+k*lv]+s*v[i+k*lv];
00662 }
00663 }
00664
00665 for(j=i+1; j<n; ++j) {
00666 v[j+i*lv]=0.0; v[i+j*lv]=0.0;
00667 }
00668 v[i+i*lv]=1;
00669 g= e[i];
00670 }
00671
00672
00673 for(i=n-1; i>=0; --i) {
00674 g=sigma[i];
00675 for(j=i+1; j<n; ++j) a[j+i*la]=0.0;
00676 if(g != 0.0) {
00677 h=g*a[i+i*la];
00678
00679 for(j=i+1; j<n; ++j) {
00680 s=0.0;
00681 for(k=i+1; k<m; ++k) s=s+a[i+k*la]*a[j+k*la];
00682 f=s/h;
00683 for(k=i; k<m; ++k) a[j+k*la]=a[j+k*la]+f*a[i+k*la];
00684 }
00685
00686 for(j=i; j<m; ++j) a[i+j*la]=a[i+j*la]/g;
00687 }
00688 else {
00689 for(j=i; j<m; ++j) a[i+j*la]=0.0;
00690 }
00691 a[i+i*la] = a[i+i*la]+1;
00692 }
00693
00694
00695 aeps=eps*rmax;
00696
00697 for(k=n-1; k>=0; --k) {
00698
00699 for(itr=1; itr<=itmax; ++itr) {
00700
00701
00702 for(l=k; l>=0; --l) {
00703 if(fabs(e[l]) < aeps) goto split;
00704 if(fabs(sigma[l-1]) < aeps) break;
00705 }
00706
00707
00708 c=0.0; s=1.0;
00709 for(i=l; i<=k; ++i) {
00710 f=s*e[i];
00711 e[i] = c*e[i];
00712 if(fabs(f) < aeps) goto split;
00713 g=sigma[i];
00714 sigma[i]=sqrt(f*f+g*g);
00715 c=g/sigma[i];
00716 s=-f/sigma[i];
00717
00718 for(j=0; j<m; ++j) {
00719 r1= a[j*la+(l-1)];
00720 r2= a[i+j*la];
00721 a[j*la+(l-1)]=r1*c+r2*s;
00722 a[i+j*la]=c*r2-s*r1;
00723 }
00724 }
00725
00726 split: z=sigma[k];
00727 if(l == k) {
00728
00729 if(z < 0.0) {
00730 sigma[k] = -z;
00731 for(j=0; j<n; ++j) v[k+j*lv]=-v[k+j*lv];
00732 }
00733 break;
00734 }
00735
00736 if(itr==itmax) {ier=12; break;}
00737
00738
00739 x=sigma[l];
00740 y=sigma[k-1];
00741 g=e[k-1];
00742 h=e[k];
00743 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.*h*y);
00744 g=sqrt(1.+f*f);
00745 if(f < 0.0) g=-g;
00746 f=((x-z)*(x+z)+h*(y/(f+g)-h))/x;
00747
00748
00749 c=1.0; s=1.0;
00750
00751 for(i=l+1; i<=k; ++i) {
00752 g=e[i];
00753 y=sigma[i];
00754 h=s*g;
00755 g=c*g;
00756 e[i-1]=sqrt(f*f+h*h);
00757 c=f/e[i-1];
00758 s=h/e[i-1];
00759 f=c*x+s*g;
00760 g=c*g-s*x;
00761 h=s*y;
00762 y=c*y;
00763
00764 for(j=0; j<n; ++j) {
00765 x=v[j*lv+(i-1)];
00766 z=v[i+j*lv];
00767 v[j*lv+(i-1)]=c*x+s*z;
00768 v[i+j*lv]=c*z-s*x;
00769 }
00770
00771 sigma[i-1]=sqrt(f*f+h*h);
00772 if(sigma[i-1] != 0.0) {
00773 c=f/sigma[i-1];
00774 s=h/sigma[i-1];
00775 }
00776 f=c*g+s*y;
00777 x=c*y-s*g;
00778 for(j=0; j<m; ++j) {
00779 y= a[j*la+(i-1)];
00780 z= a[i+j*la];
00781 a[j*la+(i-1)] = c*y+s*z;
00782 a[i+j*la] = c*z-s*y;
00783 }
00784 }
00785
00786 e[l]=0.0;
00787 e[k]=f;
00788 sigma[k]=x;
00789 }
00790 }
00791
00792 return ier;
00793 }
00794
00795 int svdevl(int n, int m, double *u, double *v, double sigma[], int lu,
00796 int lv, double b[], double reps)
00797
00798 {
00799 int i,j;
00800 double smax, aeps, s;
00801 double *wk;
00802
00803
00804 smax=0.0;
00805 for(i=0; i<n; ++i)
00806 if(sigma[i] > smax) smax=sigma[i];
00807
00808 aeps=smax*reps;
00809 wk=(double *)calloc(n, sizeof(double));
00810 for(i=0; i<n; ++i) {
00811 s=0.0;
00812
00813 if(sigma[i] > aeps) {
00814 for(j=0; j<m; ++j) s=s+b[j]*u[i+j*lu];
00815 s=s/sigma[i];
00816 }
00817 wk[i]=s;
00818 }
00819
00820 for(i=0; i<n; ++i) {
00821 s=0.0;
00822 for(j=0; j<n; ++j) s=s+v[j+i*lv]*wk[j];
00823 b[i]=s;
00824 }
00825 free(wk);
00826 return 0;
00827 }
00828
00829 int llsq(int n, int m, int k, double *x, int ix, double f[], double ef[],
00830 double a[], double *u, double *v, int iu, int iv, double sigma[],
00831 double y[], void (* phi) (int , double * , double * ), double reps,
00832 double *chisq)
00833
00834 {
00835 int i,j,ier;
00836 double s1;
00837
00838 double wk[2];
00839
00840 if(m>n || m<=0 || n<=0 || k>ix) return 606;
00841
00842
00843
00844 for(i=0; i<n; ++i) {
00845 if(ef[i]<=0.0) {return 607;}
00846 a[i]=f[i]/ef[i];
00847 phi(m,&x[i*ix],wk);
00848 for(j=0; j<m; ++j) u[j+i*iu]=wk[j]/ef[i];
00849 }
00850
00851 ier=svd(m,n,u,v,sigma,iu,iv);
00852 if(ier>100) { return ier;}
00853
00854
00855 ier=svdevl(m,n,u,v,sigma,iu,iv,a,reps);
00856
00857
00858 *chisq=0.0;
00859 for(i=0; i<n; ++i) {
00860 phi(m,&x[i*ix],wk);
00861 s1=0.0;
00862 for(j=0; j<m; ++j) s1=s1+a[j]*wk[j];
00863 y[i]=s1;
00864 s1=(f[i]-y[i])/ef[i];
00865 *chisq=(*chisq)+s1*s1;
00866 }
00867
00868
00869 return 0;
00870 }
00871
00872 int interp_vel (int *n, double *l, double *f, double *ef, double *ux,
00873 double *eux, double *uy, double *euy, int npts) {
00874
00875
00876
00877
00878 int i, j, nuse, ierr, offset=0;
00879 int n_num[13];
00880 double fb[10], ll;
00881 double dfb, ddfb, aeps=1e-7;
00882 double *inp_l, *inp_f, *inp_ef, *inp_ux, *inp_uy, *inp_eux, *inp_euy;
00883
00884
00885
00886
00887 for (i=0; i < 13; i++) n_num[i] = 0;
00888 for (i=0; i < npts; i++) if (n[i] < 13) n_num[n[i]]++;
00889
00890 inp_l = (double *) malloc (npts*sizeof(double));
00891 inp_f = (double *) malloc (npts*sizeof(double));
00892 inp_ef = (double *) malloc (npts*sizeof(double));
00893 inp_ux = (double *) malloc (npts*sizeof(double));
00894 inp_uy = (double *) malloc (npts*sizeof(double));
00895 inp_eux = (double *) malloc (npts*sizeof(double));
00896 inp_euy = (double *) malloc (npts*sizeof(double));
00897
00898 for (i=0; i < 10; i++) {
00899 for (j=0; j<npts; j++) {
00900 inp_l[j] = 0.0;
00901 inp_f[j] = 0.0;
00902 inp_ef[j] = 0.0;
00903 inp_ux[j] = 0.0;
00904 inp_uy[j] = 0.0;
00905 inp_eux[j] = 0.0;
00906 inp_euy[j] = 0.0;
00907 }
00908 for(j=0; j<n_num[i]; j++) {
00909 inp_l[j] = l[j+offset];
00910 inp_f[j] = f[j+offset];
00911 inp_ef[j] = ef[j+offset];
00912 inp_ux[j] = ux[j+offset];
00913 inp_uy[j] = uy[j+offset];
00914 inp_eux[j] = eux[j+offset];
00915 inp_euy[j] = euy[j+offset];
00916 }
00917 for (j = 0; j<n_num[i]; j++) {
00918 ll = (int)(inp_l[j]+0.5);
00919 nuse = 3;
00920 ierr = divdif (ll, inp_l, inp_f, &nuse, n_num[i], fb, aeps, &dfb, &ddfb);
00921 f[j+offset] = fb[nuse];
00922 nuse = 3;
00923 ierr = divdif (ll, inp_l, inp_ef, &nuse, n_num[i], fb, aeps, &dfb, &ddfb);
00924 ef[j+offset] = fb[nuse];
00925 nuse = 3;
00926 ierr = divdif (ll, inp_l, inp_ux, &nuse, n_num[i], fb, aeps, &dfb, &ddfb);
00927 ux[j+offset] = fb[nuse];
00928 nuse = 3;
00929 ierr = divdif (ll, inp_l, inp_uy, &nuse, n_num[i], fb, aeps, &dfb, &ddfb);
00930 uy[j+offset] = fb[nuse];
00931 nuse = 3;
00932 ierr = divdif (ll, inp_l, inp_eux, &nuse, n_num[i], fb, aeps, &dfb, &ddfb);
00933 eux[j+offset] = fb[nuse];
00934 nuse = 3;
00935 ierr = divdif (ll, inp_l, inp_euy, &nuse, n_num[i], fb, aeps, &dfb, &ddfb);
00936 euy[j+offset] = fb[nuse];
00937 l[j+offset] = ll;
00938 }
00939 offset += n_num[i];
00940 }
00941 free (inp_l);
00942 free (inp_f);
00943 free (inp_ef);
00944 free (inp_ux);
00945 free (inp_uy);
00946 free (inp_eux);
00947 free (inp_euy);
00948
00949 return 0;
00950 }