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