IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39122


Ignore:
Timestamp:
Nov 12, 2015, 5:41:54 PM (11 years ago)
Author:
eugene
Message:

do a better job fitting for the major & minor axis error value; handle edge cases; interpolate to get the galaxy mags

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/addstar/src/loadgalphot_readstars.c

    r39024 r39122  
    237237    // I need to find the minimum position (interpolated) in this 2D space
    238238
     239    // I want to use this flag to set a bit in secfilt.  is that possible?  or maybe galphot.dummy becomes flags
    239240    if (!FitChisqMinimum (fit, &stars[i].galphot, &GAL_CHISQ[Nsample*i], &GAL_FLUX[Nsample*i], &GAL_FLUX_ERR[Nsample*i], Nsample, FR_MAJOR_MIN[i], FR_MAJOR_MAX[i], FR_MAJOR_DEL[i], FR_MINOR_MIN[i], FR_MINOR_MAX[i], FR_MINOR_DEL[i])) {
    240       Nbad ++;
     241      stars[i].flag = TRUE;
    241242    }
    242243
     
    298299  // here are the steps:
    299300  // find the minimum chisq grid point (set X,Y values)
    300   // choose the nearest 9 points
    301   // fit 2D parabola (6 free parameters)
    302   // use this to outlier-clip remaining points
    303   // fit 2D parabola to the rest
     301  // rescale chisq values so minimum is == Npix
     302  // fit 2D parabola
    304303
    305304  memset (mask, 0, Npts*sizeof(char));
     
    324323  }
    325324  if (!Nvalid) {
    326     fprintf (stderr, "no good points\n");
    327325    return FALSE; // we cannot do anything if there is no finite chisq
    328326  }
    329327 
    330   if (Nvalid < 6) {
    331     galphot->majorAxis = Xpt[iMin];
    332     galphot->minorAxis = Ypt[iMin];
    333     galphot->majorAxisErr = MajorDel;
    334     galphot->minorAxisErr = MinorDel;
    335     fprintf (stderr, "too few good points: %d\n", Nvalid);
    336     return FALSE;
    337   }
    338 
    339   // re-fit all except the most extreme set
    340   if (!fit2d (fit, Xpt, Ypt, chisq, chisqFit, mask, Npts)) {
    341     // raise a flag?
    342     galphot->majorAxis = Xpt[iMin];
    343     galphot->minorAxis = Ypt[iMin];
    344     galphot->majorAxisErr = MajorDel;
    345     galphot->minorAxisErr = MinorDel;
    346     fprintf (stderr, "failed fit\n");
    347     return FALSE;
    348   }
     328  for (i = 0; i < Npts; i++) {
     329    chisq[i] *= (galphot->Npix / chisqMin);
     330  }
     331
     332  if (Nvalid < 6) goto bad_fit;
     333
     334# if (0)
     335  {
     336    FILE *f = fopen ("galphot.dump.dat", "w");
     337    for (i = 0; i < Npts; i++) {
     338      fprintf (f, "%d %d %f %f %f %f\n", i, mask[i], Xpt[i], Ypt[i], chisq[i], flux[i]);
     339    }
     340    fclose (f);
     341  }
     342# endif
     343
     344  // fit with 3 iterations, 5 sigma clipping (set above after fit2d_init)
     345  // on failure (for any reason), the stars[i].flag is set to TRUE (XXX bad choice)
     346  if (!fit2d (fit, Xpt, Ypt, chisq, chisqFit, mask, Npts)) goto bad_fit;
    349347
    350348  // get X,Y for the chisq min from the fit:
     
    353351  float Ymin = (fit->c10*fit->c11 - 2.0*fit->c20*fit->c01) * Det;
    354352
     353  // allow the fitting minimum to be just outside the grid, but no further
     354  int inRange = TRUE;
     355  inRange = inRange && (Xmin > MajorMin - MajorDel);
     356  inRange = inRange && (Xmin < MajorMax + MajorDel);
     357  inRange = inRange && (Ymin > MinorMin - MinorDel);
     358  inRange = inRange && (Ymin < MinorMax + MinorDel);
     359
     360  if (!inRange) goto bad_fit;
     361
    355362  // chisqMin @ (Xmin,Ymin)
    356   chisqMin =
    357     fit->c00 +
    358     fit->c10*Xmin + fit->c20*Xmin*Xmin +
    359     fit->c01*Ymin + fit->c02*Ymin*Ymin +
    360     fit->c11*Xmin*Ymin;
    361 
    362   float chisqOff = chisqMin + 1.0;
    363 
    364   float A, B, C, Xoff, Yoff;
    365 
    366   // Xoff @ chisqMin + 1.0
     363  float A, B, C, Q;
     364
     365  // Xoff @ chisqMin + 1.0, delta Y = 0.0
    367366  A = fit->c20;
    368   B = fit->c10 + fit->c11*Ymin;
    369   C = fit->c00 + fit->c01*Ymin + fit->c02*Ymin*Ymin - chisqOff;
    370   Xoff = (-B + sqrt(B*B - 4*A*C)) / (2.0 * A);
    371 
    372   // Xoff @ chisqMin + 1.0
     367  B = fit->c10 + 2*fit->c20*Xmin + fit->c11*Ymin;
     368  C = -1;
     369  Q = B*B - 4*A*C;
     370  if (Q < 0.0) goto bad_err;
     371 
     372  float dXmin = (-B + sqrt(Q)) / (2.0 * A);
     373
     374  // Yoff @ chisqMin + 1.0, delta X = 0.0
    373375  A = fit->c02;
    374   B = fit->c01 + fit->c11*Xmin;
    375   C = fit->c00 + fit->c10*Xmin + fit->c20*Xmin*Xmin - chisqOff;
    376   Yoff = (-B + sqrt(B*B - 4*A*C)) / (2.0 * A);
     376  B = fit->c01 + 2*fit->c02*Ymin + fit->c11*Xmin;
     377  C = -1;
     378  Q = B*B - 4*A*C;
     379  if (Q < 0.0) goto bad_err;
     380
     381  float dYmin = (-B + sqrt(Q)) / (2.0 * A);
    377382
    378383  galphot->majorAxis = Xmin;
    379384  galphot->minorAxis = Ymin;
    380   galphot->majorAxisErr = fabs(Xoff - Xmin);
    381   galphot->minorAxisErr = fabs(Yoff - Ymin);
     385  galphot->majorAxisErr = dXmin;
     386  galphot->minorAxisErr = dYmin;
     387
     388  // convert (Xmin, Ymin) to (iXmin, iYmin)
     389  float iXminF = (Xmin - MajorMin) / MajorDel;
     390  float iYminF = (Ymin - MinorMin) / MinorDel;
     391
     392  // interpolate in X and in Y
     393  int   iXmin = floor(iXminF);
     394  iXmin = MAX(0,MIN(Nx - 2, iXmin)); // force range of iXmin to be 0,Nx-1
     395  float fXmin = iXminF - iXmin;
     396
     397  int   iYmin = floor(iYminF);
     398  iYmin = MAX(0,MIN(Ny - 2, iYmin)); // force range of iYmin to be 0,Ny-1
     399  float fYmin = iYminF - iYmin;
     400
     401  float V00 = flux[iXmin+0 + (iYmin+0)*Nx];
     402  float V01 = flux[iXmin+0 + (iYmin+1)*Nx];
     403  float V10 = flux[iXmin+1 + (iYmin+0)*Nx];
     404  float V11 = flux[iXmin+1 + (iYmin+1)*Nx];
     405
     406  float Vx0 = V10*fXmin + V00*(1.0 - fXmin);
     407  float Vx1 = V11*fXmin + V01*(1.0 - fXmin);
     408
     409  float value = fYmin * Vx1 + (1.0 - fYmin) * Vx0;
     410
     411  float dV00 = fluxErr[iXmin+0 + (iYmin+0)*Nx];
     412  float dV01 = fluxErr[iXmin+0 + (iYmin+1)*Nx];
     413  float dV10 = fluxErr[iXmin+1 + (iYmin+0)*Nx];
     414  float dV11 = fluxErr[iXmin+1 + (iYmin+1)*Nx];
     415
     416  float dVx0 = dV10*fXmin + dV00*(1.0 - fXmin);
     417  float dVx1 = dV11*fXmin + dV01*(1.0 - fXmin);
     418
     419  float dValue = fYmin * dVx1 + (1.0 - fYmin) * dVx0;
    382420
    383421  // use bilinear interpolation to choose Flux @ (Xmin,Ymin)?
     422  // float magRaw    = -2.5*log10(flux[iMin]) + ZeroPt; // correct for exptime?
     423  // float magErrRaw = sqrt(fluxErr[iMin]) / flux[iMin];
     424
     425  galphot->mag    = -2.5*log10(value) + ZeroPt; // correct for exptime?
     426  galphot->magErr = sqrt(dValue) / value;
     427  galphot->chisq  = chisqMin;
     428 
     429  return TRUE;
     430
     431bad_fit:
     432  galphot->majorAxis = Xpt[iMin];
     433  galphot->minorAxis = Ypt[iMin];
     434  galphot->majorAxisErr = MajorDel;
     435  galphot->minorAxisErr = MinorDel;
     436
    384437  galphot->mag    = -2.5*log10(flux[iMin]) + ZeroPt; // correct for exptime?
    385438  galphot->magErr = sqrt(fluxErr[iMin]) / flux[iMin];
    386439  galphot->chisq  = chisqMin;
    387  
    388   return TRUE;
     440  return FALSE;
     441
     442bad_err:
     443  galphot->majorAxisErr = MajorDel;
     444  galphot->minorAxisErr = MinorDel;
     445  return FALSE;
    389446}
    390447
Note: See TracChangeset for help on using the changeset viewer.