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()); }
static int track_calc_offset(const double qdlat_min, const double qdlat_max, const double *qdlat, double *B, const size_t n) { int s = 0; const gsl_multifit_robust_type *robust_t = gsl_multifit_robust_bisquare; const curvefit_type *curve_t = curvefit_poly; const size_t p = 1; curvefit_workspace *curvefit_p; double *x, *y; size_t npts = 0; size_t i; x = malloc(n * sizeof(double)); y = malloc(n * sizeof(double)); /* store data in arrays, excluding equatorial region */ for (i = 0; i < n; ++i) { if (fabs(qdlat[i]) < qdlat_min || fabs(qdlat[i]) > qdlat_max) continue; x[npts] = qdlat[i]; y[npts] = B[i]; ++npts; } curvefit_p = curvefit_alloc(robust_t, curve_t, npts, p); curvefit(1, x, y, curvefit_p); for (i = 0; i < n; ++i) { double cval = curvefit_eval(qdlat[i], curvefit_p); B[i] -= cval; } curvefit_free(curvefit_p); free(x); free(y); return s; } /* track_calc_offset() */
int correlateJ(const char *data_file, const char *corr_file, current_data *data1, current_data *data2) { int s = 0; size_t i, j; bin2d_workspace *bin2d_p; bin_workspace *bin_p; const double phi_min = -5.0; const double phi_max = 25.0; const size_t nphi = 7; const double t_min = -60.0; const double t_max = 60.0; const size_t nt = 2; FILE *fp_data, *fp_corr; double *dp, *x, *y, *r; size_t n = 0; double sigma; fp_data = fopen(data_file, "w"); fp_corr = fopen(corr_file, "w"); bin2d_p = bin2d_alloc(phi_min, phi_max, nphi, t_min, t_max, nt); bin_p = bin_alloc(phi_min, phi_max, nphi); dp = malloc(data1->n * sizeof(double)); x = malloc(data1->n * sizeof(double)); y = malloc(data1->n * sizeof(double)); r = malloc(data1->n * sizeof(double)); i = 1; fprintf(fp_data, "# Field %zu: timestamp of satellite 1 crossing (UT)\n", i++); fprintf(fp_data, "# Field %zu: delta t (minutes)\n", i++); fprintf(fp_data, "# Field %zu: delta phi (degrees)\n", i++); fprintf(fp_data, "# Field %zu: satellite 1 peak current (mA/m)\n", i++); fprintf(fp_data, "# Field %zu: satellite 2 peak current (mA/m)\n", i++); for (i = 0; i < data1->n; ++i) { curr_profile *prof = &(data1->profiles[i]); curr_profile *prof2; time_t dt; double dphi, dt_min; size_t idx; double lt = get_localtime(prof->t, prof->phi * M_PI / 180.0); s = find_profile_t(prof->t, data2, &idx); if (s) continue; prof2 = &(data2->profiles[idx]); if (prof->npeak != 1 || prof2->npeak != 1) continue; #if 0 if (prof->peak_J > 20.0) continue; /* a couple outliers */ #endif #if 0 if (lt > 20.0) continue; #endif dt = prof2->t - prof->t; dphi = wrap180(prof2->phi - prof->phi); dt_min = (double)dt / 60.0; #if 0 bin2d_add_element_corr(dphi, dt_min, prof->peak_J, prof2->peak_J, bin2d_p); #endif if (fabs(dt_min) <= 30.0 && dphi >= phi_min && dphi <= phi_max) { /* store this point */ x[n] = prof->peak_J; y[n] = prof2->peak_J; dp[n] = dphi; ++n; fprintf(fp_data, "%ld %f %f %f %f\n", prof->t, dt_min, dphi, prof->peak_J, prof2->peak_J); } } /* robust fit straight line to (x,y) */ { curvefit_workspace *curvefit_p = curvefit_alloc(gsl_multifit_robust_bisquare, curvefit_poly, n, 2); curvefit(0, x, y, curvefit_p); curvefit_residuals(x, y, r, curvefit_p); curvefit_free(curvefit_p); const double alpha = 2.5; sigma = gsl_stats_sd(r, 1, n); fprintf(stderr, "sigma = %f\n", sigma); /* loop through again and add (J1,J2) points which aren't outliers */ for (i = 0; i < n; ++i) { if (fabs(r[i]) > alpha*sigma) continue; bin_add_element_corr(dp[i], x[i], y[i], bin_p); } } #if 0 i = 1; fprintf(fp_corr, "# Field %zu: delta longitude (degrees)\n", i++); fprintf(fp_corr, "# Field %zu: delta t (minutes)\n", i++); fprintf(fp_corr, "# Field %zu: correlation\n", i++); fprintf(fp_corr, "# Field %zu: number of points in bin\n", i++); for (i = 0; i < nphi; ++i) { for (j = 0; j < nt; ++j) { double dt, dlon; size_t n; double r; bin2d_xyval(i, j, &dlon, &dt, bin2d_p); n = bin2d_n(dlon, dt, bin2d_p); r = bin2d_correlation(dlon, dt, bin2d_p); fprintf(fp_corr, "%f %f %f %zu\n", dlon, dt, r, n); } fprintf(fp_corr, "\n"); } #else i = 1; fprintf(fp_corr, "# Field %zu: delta longitude (degrees)\n", i++); fprintf(fp_corr, "# Field %zu: correlation\n", i++); fprintf(fp_corr, "# Field %zu: number of points in bin\n", i++); for (i = 0; i < nphi; ++i) { double dlon; size_t n; double r; bin_xval(i, &dlon, bin_p); n = bin_n(dlon, bin_p); r = bin_correlation(dlon, bin_p); fprintf(fp_corr, "%f %f %zu\n", dlon, r, n); } #endif fclose(fp_data); fclose(fp_corr); free(x); free(y); free(r); return s; }