IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 27, 2016, 5:17:08 PM (10 years ago)
Author:
watersc1
Message:

Added binned initial step method for irls pm/plx fitting.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.astro/fitpm_irls.c

    r39228 r39585  
    2525  }
    2626
     27  double binning_step = 0.0;
     28  if ((N = get_argument (argc, argv, "-binning"))) {
     29    remove_argument (N, &argc, argv);
     30    binning_step = atof(argv[N]);
     31    remove_argument (N, &argc, argv);
     32  }
     33 
     34 
    2735  if (argc != 6) {
    28     gprint (GP_ERR, "USAGE: fitplx_irls (ra) (dR) (dec) (dD) (mjd) [-mask mask]\n");
     36    gprint (GP_ERR, "USAGE: fitplx_irls (ra) (dR) (dec) (dD) (mjd) [-mask mask] [-binning step_size]\n");
    2937    // what about the errors?
    3038    return (FALSE);
     
    105113
    106114  PlxFit fit;
    107   if (!FitPMonly_IRLS (&fit, X, dX, Y, dY, t, n, VERBOSE)) {
     115  if (!FitPMonly_IRLS (&fit, X, dX, Y, dY, t, n, binning_step, VERBOSE)) {
    108116    return FALSE;
    109117  }
     
    140148
    141149/* do we want an init function which does the alloc and a clear function to free? */
    142 int FitPMonly_IRLS (PlxFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts, int VERBOSE) {
     150int FitPMonly_IRLS (PlxFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts, double binning_step, int VERBOSE) {
    143151
    144152  int i,j;
     
    196204  }
    197205 
    198   // Solve OLS equation 
    199   if (!weighted_LS_PM(T,X,Wx,Y,Wy,Npts,
    200                    A,B,VERBOSE)) {
    201     // Handle fail case
    202     return(FALSE);
     206  // Solve OLS equation
     207  if (binning_step == 0.0) {
     208    if (!weighted_LS_PM(T,X,Wx,Y,Wy,Npts,
     209                        A,B,VERBOSE)) {
     210      // Handle fail case
     211      return(FALSE);
     212    }
     213  }
     214  else {
     215    int Nbins;
     216    double *Tbin = NULL;
     217    double *Xbin = NULL;
     218    double *Ybin = NULL;
     219    double *WXbin = NULL;
     220    double *WYbin = NULL;
     221   
     222    if (!bin_points(T, X, Wx, Y, Wy, Npts,
     223                    &Tbin, &Xbin, &WXbin, &Ybin, &WYbin, &Nbins,  binning_step) ) {
     224      return(FALSE);
     225    }
     226    if (!weighted_LS_PM(Tbin,Xbin,WXbin,Ybin,WYbin, Nbins,
     227                        A,B,VERBOSE)) {
     228      return(FALSE);
     229    }
     230    FREE(Tbin);
     231    FREE(Xbin);
     232    FREE(Ybin);
     233    FREE(WXbin);
     234    FREE(WYbin);
    203235  }
    204236
     
    424456}
    425457
     458int bin_points (double *T, double *X, double *WX, double *Y, double *WY, int Npts,
     459                double **Tbin, double **Xbin, double **WXbin, double **Ybin, double **WYbin, int *Nbins, double binning_step) {
     460  double *T_tmp = NULL;
     461  int i,j,k,l;
     462
     463  int Nbins_test;
     464  int *bin_test = NULL;
     465
     466  double *T_int = NULL, *X_int = NULL, *Y_int = NULL, *WX_int = NULL, *WY_int = NULL;
     467  double T_min, T_max;
     468
     469
     470  //  printf("In binning code\n");
     471
     472  // Allocate more bins than we need
     473  ALLOCATE(T_tmp,double,Npts);
     474  for (i = 0; i < Npts; i++) {
     475    T_tmp[i] = T[i];
     476  }
     477  dsort(T_tmp,Npts);
     478
     479  Nbins_test = floor((T_tmp[Npts - 1] - T_tmp[0]) / binning_step) + 1;
     480  ALLOCATE(bin_test,int,Nbins_test);
     481
     482  T_min = T_tmp[0];
     483  T_max = T_tmp[Npts - 1];
     484
     485  // printf("Found %d points, putting into %d bins between %f and %f\n",Npts,Nbins_test,T_min,T_max);
     486  // Check how many bins are filled.
     487  for (j = 0; j < Nbins_test; j++) {
     488    bin_test[j] = 0;
     489  }
     490  for (i = 0; i < Npts; i++) {
     491    j = floor((T_tmp[i] - T_tmp[0]) / binning_step);
     492    bin_test[j] ++;
     493  }
     494
     495  *Nbins = 0;
     496  for (j = 0; j < Nbins_test; j++) {
     497    if (bin_test[j] != 0) {
     498      *Nbins = *Nbins + 1;
     499    }
     500  }
     501
     502  // printf("Using %d actual bins\n",*Nbins);
     503
     504  double *Tbin_in = NULL;
     505  double *Xbin_in = NULL;
     506  double *Ybin_in = NULL;
     507  double *WXbin_in = NULL;
     508  double *WYbin_in = NULL;
     509 
     510  ALLOCATE(Tbin_in,double,*Nbins);
     511  ALLOCATE(Xbin_in,double,*Nbins);
     512  ALLOCATE(WXbin_in,double,*Nbins);
     513  ALLOCATE(Ybin_in,double,*Nbins);
     514  ALLOCATE(WYbin_in,double,*Nbins);
     515
     516  k = 0;
     517  for (j = 0; j < Nbins_test; j++) {
     518    if (bin_test[j] == 0) { // No data for this bin
     519      continue;
     520    }
     521    else {
     522      // printf("%d bin %d, N = %d\n",k,j,bin_test[j]);
     523      // Allocate internal arrays to hold the data that goes into this bin.
     524      ALLOCATE(T_int,double, bin_test[j]);
     525      ALLOCATE(X_int,double, bin_test[j]);
     526      ALLOCATE(Y_int,double, bin_test[j]);
     527      ALLOCATE(WX_int,double, bin_test[j]);
     528      ALLOCATE(WY_int,double, bin_test[j]);
     529
     530      // Fill those arrays.
     531      l = 0;
     532      for (i = 0; i < Npts; i++) {
     533        if ((T[i] >= T_min + j * binning_step)&&
     534            (T[i] <  T_min + (j + 1) * binning_step)) {
     535          T_int[l] = T[i];
     536          X_int[l] = X[i];
     537          Y_int[l] = Y[i];
     538          WX_int[l] = WX[i];
     539          WY_int[l] = WY[i];
     540          // printf("%d %d %f %f %f %f %f\n",i,l,T[i],X[i],Y[i],WX[i],WY[i]);
     541          l++;
     542        }
     543      } // End loop over input points
     544      // printf("%d bin %d, N = %d, l = %d\n",k,j,bin_test[j],l);
     545
     546      // Make a decision what the binned values should be.
     547      if (l == 1) {
     548        Tbin_in[k] = T_int[0];
     549        Xbin_in[k] = X_int[0];
     550        Ybin_in[k] = Y_int[0];
     551        WXbin_in[k] = WX_int[0];
     552        WYbin_in[k] = WY_int[0];
     553      }
     554      else {
     555        // I think I'm going with medians.
     556        dsort(T_int,l);
     557        dsort(X_int,l);
     558        dsort(Y_int,l);
     559       
     560        // The median gives the midpoint of all the measurements
     561        if ((l % 2) == 0) {
     562          Tbin_in[k] = 0.5*(T_int[(int)(0.5*l)] + T_int[(int)(0.5*l) - 1]);
     563          Xbin_in[k] = 0.5*(X_int[(int)(0.5*l)] + X_int[(int)(0.5*l) - 1]);
     564          Ybin_in[k] = 0.5*(Y_int[(int)(0.5*l)] + Y_int[(int)(0.5*l) - 1]);
     565        } else {
     566          Tbin_in[k] = T_int[(int)(0.5*l)];
     567          Xbin_in[k] = X_int[(int)(0.5*l)];
     568          Ybin_in[k] = Y_int[(int)(0.5*l)];
     569        }
     570       
     571        // The scatter between points is probably the most useful measurement for the error.
     572        for (i = 0; i < l; i++) {
     573          X_int[i] = fabs(X_int[i] - Xbin_in[k]);
     574          Y_int[i] = fabs(Y_int[i] - Ybin_in[k]);
     575        }
     576        dsort(X_int,l);
     577        dsort(Y_int,l);
     578       
     579        if ((l % 2) == 0) {
     580          WXbin_in[k] = 1.0 / (0.5*(X_int[(int)(0.5*l)] + X_int[(int)(0.5*l) - 1]));
     581          WYbin_in[k] = 1.0 / (0.5*(Y_int[(int)(0.5*l)] + Y_int[(int)(0.5*l) - 1]));
     582        } else {
     583          WXbin_in[k] = 1.0 / (X_int[(int)(0.5*l)]);
     584          WYbin_in[k] = 1.0 / (Y_int[(int)(0.5*l)]);
     585        }
     586      }
     587
     588      if (WXbin_in[k] == 0.0) {
     589        WXbin_in[k] = WX_int[0];
     590      }
     591      if (WYbin_in[k] == 0.0) {
     592        WYbin_in[k] = WY_int[0];
     593      }
     594      if (!isfinite(WXbin_in[k])) {
     595        WXbin_in[k] = 1.0;
     596      }
     597      if (!isfinite(WYbin_in[k])) {
     598        WYbin_in[k] = 1.0;
     599      }
     600     
     601      //      printf("%f %f %f %f %f\n",Tbin_in[k],Xbin_in[k],Ybin_in[k],WXbin_in[k],WYbin_in[k]);
     602      // Increment bin index.
     603      k++;
     604     
     605      // Clean up
     606      FREE(T_int);
     607      FREE(X_int);
     608      FREE(Y_int);
     609      FREE(WX_int);
     610      FREE(WY_int);
     611    }
     612  } // End loop over initial bin set.
     613
     614  *Tbin = Tbin_in;
     615  *Xbin = Xbin_in;
     616  *Ybin = Ybin_in;
     617  *WXbin = WXbin_in;
     618  *WYbin = WYbin_in;
     619
     620  FREE(T_tmp);
     621  FREE(bin_test);
     622 
     623  return TRUE;
     624}
     625
     626
    426627double weight_cauchy (double x) {
    427628  double r = x / 2.385;
     
    450651  dsort(x,N);
    451652
    452   if (N % 2) {
     653  if ((N % 2) == 0) {
    453654    median = 0.5*(x[(int)(0.5*N)] + x[(int)(0.5*N) - 1]);
    454655  } else {
     
    462663  dsort(x,N);
    463664
    464   if (N % 2) {
     665  if ((N % 2) == 0) {
    465666    median = 0.5*(x[(int)(0.5*N)] + x[(int)(0.5*N) - 1]);
    466667  } else {
     
    468669  }
    469670
     671  FREE(x);
    470672  return(median);
    471673}
Note: See TracChangeset for help on using the changeset viewer.