HiVector DriftCorrect::Solve(const HiVector &d) { ostringstream hist; _data = d; if ( _skipFit || (!gotGoodLines(d))) { _b2 = _data; _coefs = HiVector(4, 0.0); _uncert = _coefs; _cc = HiVector(2, 0.0); _chisq = 0.0; if ( !gotGoodLines(d) ) { hist << "NotEnoughLines(GoodLines[" << goodLines(d) << "],MinimumLines[" << _minLines << "]);"; } hist << "SkipFit(TRUE: Not using LMFit)"; _history.add(hist.str()); } else { hist << "Fit("; _b2 = HiVector(goodLines(_data)); if ( success(curvefit()) ) { _coefs = coefs(); _uncert = uncert(); hist << "Solved,#Iters[" << nIterations() << "],ChiSq[" << Chisq() << "],DoF[" << DoF() << "])"; _history.add(hist.str()); _history.add("a0("+ToString(_coefs[0])+"+-"+ToString(_uncert[0])+")"); _history.add("a1("+ToString(_coefs[1])+"+-"+ToString(_uncert[1])+")"); _history.add("a2("+ToString(_coefs[2])+"+-"+ToString(_uncert[2])+")"); _history.add("a3("+ToString(_coefs[3])+"+-"+ToString(_uncert[3])+")"); } else { // Punt, fit a straight line to the data _cc = poly_fit(d); HiVector a(4); a[0] = _cc[0]; a[1] = _cc[1]; a[2] = 0.0; a[3] = 0.0; _coefs = a; hist << "Failed::Reason("<< statusstr() << "),#Iters[" << nIterations() << "])"; _history.add(hist.str()); _history.add("a0("+ToString(_coefs[0])+")"); _history.add("a1("+ToString(_coefs[1])+")"); _history.add("a2("+ToString(_coefs[2])+")"); _history.add("a3("+ToString(_coefs[3])+")"); if ( _useLinFit ) { _history.add("OnFailureUse(LinearFit(Zf))"); } else { _skipFit = true; _history.add("OnFailureUse(ZfBuffer)"); } } } return (Yfit()); }
main(int ac,char **av) { FILE *fpr, *fpw, *fopfile(); float *per, *resid, vs30, cdst, xcos, ycos, tmin, tmax; float *bias, *sigma, *sigma0, *cl90m, *cl90p; int nstat, nper, nval, i; float min_cdst = -1e+15; float max_cdst = 1e+15; float min_vs30 = -1e+15; float max_vs30 = 1e+15; float min_xcos = -1e+15; float max_xcos = 1e+15; float min_ycos = -1e+15; float max_ycos = 1e+15; char residfile[256], fileroot[256], comp[16], rdcomp[16]; char *sptr, string[2048]; setpar(ac,av); mstpar("residfile","s",residfile); mstpar("fileroot","s",fileroot); mstpar("comp","s",comp); mstpar("nstat","d",&nstat); mstpar("nper","d",&nper); getpar("min_cdst","f",&min_cdst); getpar("max_cdst","f",&max_cdst); getpar("min_vs30","f",&min_vs30); getpar("max_vs30","f",&max_vs30); getpar("min_xcos","f",&min_xcos); getpar("max_xcos","f",&max_xcos); getpar("min_ycos","f",&min_ycos); getpar("max_ycos","f",&max_ycos); endpar(); per = (float *) check_malloc (nper*sizeof(float)); bias = (float *) check_malloc (nper*sizeof(float)); sigma = (float *) check_malloc (nper*sizeof(float)); sigma0 = (float *) check_malloc (nper*sizeof(float)); cl90m = (float *) check_malloc (nper*sizeof(float)); cl90p = (float *) check_malloc (nper*sizeof(float)); resid = (float *) check_malloc (nstat*nper*sizeof(float)); fpr = fopfile(residfile,"r"); fgets(string,2048,fpr); sptr = skipval(13,string); for(i=0;i<nper;i++) sptr = getflt(&per[i],sptr); nval = 0; while(fgets(string,2048,fpr) != NULL) { sscanf(string,"%*s %*f %*s %*f %*f %*d %f %f %f %f %f %f %s",&vs30,&cdst,&xcos,&ycos,&tmin,&tmax,rdcomp); if((vs30 >= min_vs30 && vs30 <= max_vs30) && (cdst >= min_cdst && cdst <= max_cdst) && (xcos >= min_xcos && xcos <= max_xcos) && (ycos >= min_ycos && ycos <= max_ycos) && (strcmp(rdcomp,comp)==0) ) { sptr = skipval(13,string); for(i=0;i<nper;i++) sptr = getflt(&resid[i+nval*nper],sptr); nval++; } if(nval>nstat) { fprintf(stderr,"(nval= %d) > (nstat= %d), exiting...\n",nval,nstat); exit(-1); } } fclose(fpr); fprintf(stderr,"nval= %d\n",nval); uncert(nval,nper,resid,bias,sigma,sigma0,cl90m,cl90p); sprintf(string,"%s.bias",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],bias[i]); fclose(fpw); sprintf(string,"%s.sigma",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],sigma[i]); fclose(fpw); sprintf(string,"%s.sigma0",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],sigma0[i]); fclose(fpw); sprintf(string,"%s.m90",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],cl90m[i]); fclose(fpw); sprintf(string,"%s.p90",fileroot); fpw = fopfile(string,"w"); for(i=0;i<nper;i++) fprintf(fpw,"%13.5e %13.5e\n",per[i],cl90p[i]); fclose(fpw); }