IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 25, 2005, 5:00:41 PM (21 years ago)
Author:
eugene
Message:

updating to work with current psModules

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/pmPSFtry.c

    r5593 r5607  
    1616static void pmPSFtryFree (pmPSFtry *test) {
    1717
    18   if (test == NULL) return;
    19 
    20   psFree (test->psf);
    21   psFree (test->sources);
    22   psFree (test->modelFLT);
    23   psFree (test->modelPSF);
    24   psFree (test->metric);
    25   psFree (test->fitMag);
    26   psFree (test->mask);
    27   return;
     18    if (test == NULL) return;
     19
     20    psFree (test->psf);
     21    psFree (test->sources);
     22    psFree (test->modelFLT);
     23    psFree (test->modelPSF);
     24    psFree (test->metric);
     25    psFree (test->fitMag);
     26    psFree (test->mask);
     27    return;
    2828}
    2929
     
    9595        // exclude the poor fits
    9696        if (!status) {
    97           try->mask->data.U8[i] = PSFTRY_MASK_FLT_FAIL;
    98           psFree (model);
    99           continue;
     97            try->mask->data.U8[i] = PSFTRY_MASK_FLT_FAIL;
     98            psFree (model);
     99            continue;
    100100        }
    101101        try->modelFLT->data[i] = model;
     
    136136
    137137        // otherwise, save the resulting model
    138         // pmModelSkyOffset (modelPSF, x, y, RADIUS);
    139138        try->modelPSF->data[i] = modelPSF;
    140139
     
    171170bool pmPSFtryMetric (pmPSFtry *try, float RADIUS) {
    172171
    173   float dBin;
    174   int   nKeep, nSkip;
    175 
    176   // the measured (aperture - fit) magnitudes (dA == try->metric)
    177   //   depend on both the true ap-fit (dAo) and the bias in the sky measurement:
    178   //     dA = dAo + dsky/flux
    179   //   where flux is the flux of the star
    180   // we fit this trend to find the infinite flux aperture correction (dAo),
    181   //   the nominal sky bias (dsky), and the error on dAo
    182   // the values of dA are contaminated by stars with close neighbors in the aperture
    183   //   we use an outlier rejection to avoid this bias
    184 
    185   FILE *f;
    186   f = fopen ("apresid.dat", "w");
    187   if (f == NULL) psAbort ("pmPSFtry", "can't open output file");
    188 
    189   // rflux = ten(0.4*fitMag);
    190   psVector *rflux = psVectorAlloc (try->sources->n, PS_TYPE_F64);
    191   for (int i = 0; i < try->sources->n; i++) {
    192     if (try->mask->data.U8[i] & PSFTRY_MASK_ALL) continue;
    193     rflux->data.F64[i] = pow(10.0, 0.4*try->fitMag->data.F64[i]);
    194     fprintf (f, "%3d %8.4f %12.5e %8.4f\n", i, try->fitMag->data.F64[i], rflux->data.F64[i], try->metric->data.F64[i]);
    195   }
    196   fclose (f);
    197 
    198   // XXX EAM : try 3hi/1lo sigma clipping on the rflux v dap fit
    199 
    200   // find min and max of (1/flux):
    201   psStats *stats = psStatsAlloc (PS_STAT_MIN | PS_STAT_MAX);
    202   psVectorStats (stats, rflux, NULL, try->mask, PSFTRY_MASK_ALL);
     172    float dBin;
     173    int   nKeep, nSkip;
     174
     175    // the measured (aperture - fit) magnitudes (dA == try->metric)
     176    //   depend on both the true ap-fit (dAo) and the bias in the sky measurement:
     177    //     dA = dAo + dsky/flux
     178    //   where flux is the flux of the star
     179    // we fit this trend to find the infinite flux aperture correction (dAo),
     180    //   the nominal sky bias (dsky), and the error on dAo
     181    // the values of dA are contaminated by stars with close neighbors in the aperture
     182    //   we use an outlier rejection to avoid this bias
     183
     184    // rflux = ten(0.4*fitMag);
     185    psVector *rflux = psVectorAlloc (try->sources->n, PS_TYPE_F64);
     186    for (int i = 0; i < try->sources->n; i++) {
     187        if (try->mask->data.U8[i] & PSFTRY_MASK_ALL) continue;
     188        rflux->data.F64[i] = pow(10.0, 0.4*try->fitMag->data.F64[i]);
     189    }
     190
     191    // find min and max of (1/flux):
     192    psStats *stats = psStatsAlloc (PS_STAT_MIN | PS_STAT_MAX);
     193    psVectorStats (stats, rflux, NULL, try->mask, PSFTRY_MASK_ALL);
    203194 
    204   // build binned versions of rflux, metric
    205   dBin = (stats->max - stats->min) / 10.0;
    206   psVector *rfBin = psVectorCreate (NULL, stats->min, stats->max, dBin, PS_TYPE_F64);
    207   psVector *daBin = psVectorAlloc (rfBin->n, PS_TYPE_F64);
    208   psVector *maskB = psVectorAlloc (rfBin->n, PS_TYPE_U8);
    209   psFree (stats);
    210 
    211   psTrace ("psphot.metricmodel", 3, "rflux max: %g, min: %g, delta: %g\n", stats->max, stats->min, dBin);
    212 
    213   // group data in daBin bins, measure lower 50% mean
    214   for (int i = 0; i < daBin->n; i++) {
    215 
    216     psVector *tmp = psVectorAlloc (try->sources->n, PS_TYPE_F64);
    217     tmp->n = 0;
    218 
    219     // accumulate data within bin range
    220     for (int j = 0; j < try->sources->n; j++) {
    221       // masked for: bad model fit, outlier in parameters
    222       if (try->mask->data.U8[j] & PSFTRY_MASK_ALL) continue;
     195    // build binned versions of rflux, metric
     196    dBin = (stats->max - stats->min) / 10.0;
     197    psVector *rfBin = psVectorCreate (NULL, stats->min, stats->max, dBin, PS_TYPE_F64);
     198    psVector *daBin = psVectorAlloc (rfBin->n, PS_TYPE_F64);
     199    psVector *maskB = psVectorAlloc (rfBin->n, PS_TYPE_U8);
     200    psFree (stats);
     201
     202    psTrace ("psphot.metricmodel", 3, "rflux max: %g, min: %g, delta: %g\n", stats->max, stats->min, dBin);
     203
     204    // group data in daBin bins, measure lower 50% mean
     205    for (int i = 0; i < daBin->n; i++) {
     206
     207        psVector *tmp = psVectorAlloc (try->sources->n, PS_TYPE_F64);
     208        tmp->n = 0;
     209
     210        // accumulate data within bin range
     211        for (int j = 0; j < try->sources->n; j++) {
     212            // masked for: bad model fit, outlier in parameters
     213            if (try->mask->data.U8[j] & PSFTRY_MASK_ALL) continue;
    223214   
    224       // skip points with extreme dA values
    225       if (fabs(try->metric->data.F64[j]) > 0.5) continue;
    226 
    227       // skip points outside of this bin
    228       if (rflux->data.F64[j] < rfBin->data.F64[i] - 0.5*dBin) continue;
    229       if (rflux->data.F64[j] > rfBin->data.F64[i] + 0.5*dBin) continue;
    230 
    231       tmp->data.F64[tmp->n] = try->metric->data.F64[j];
    232       tmp->n ++;
    233     }
    234 
    235     // is this a valid point?
    236     maskB->data.U8[i] = 0;
    237     if (tmp->n < 2) {
    238       maskB->data.U8[i] = 1;
    239       psFree (tmp);
    240       continue;
    241     }
    242 
    243     // dA values are contaminated with low outliers
    244     // measure statistics only on upper 50% of points
    245     // this would be easier if we could sort in reverse:
    246 
    247     psVectorSort (tmp, tmp);
    248     nKeep = 0.5*tmp->n;
    249     nSkip = tmp->n - nKeep;
    250 
    251     psVector *tmp2 = psVectorAlloc (nKeep, PS_TYPE_F64);
    252     for (int j = 0; j < tmp2->n; j++) {
    253       tmp2->data.F64[j] = tmp->data.F64[j + nSkip];
    254     }
    255 
    256     stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN);
    257     psVectorStats (stats, tmp2, NULL, NULL, 0);
    258     psTrace ("psphot.metricmodel", 4, "rfBin %d (%g): %d pts, %g\n", i, rfBin->data.F64[i], tmp->n, stats->sampleMedian);
    259 
    260     daBin->data.F64[i] = stats->sampleMedian;
    261 
     215            // skip points with extreme dA values
     216            if (fabs(try->metric->data.F64[j]) > 0.5) continue;
     217
     218            // skip points outside of this bin
     219            if (rflux->data.F64[j] < rfBin->data.F64[i] - 0.5*dBin) continue;
     220            if (rflux->data.F64[j] > rfBin->data.F64[i] + 0.5*dBin) continue;
     221
     222            tmp->data.F64[tmp->n] = try->metric->data.F64[j];
     223            tmp->n ++;
     224        }
     225
     226        // is this a valid point?
     227        maskB->data.U8[i] = 0;
     228        if (tmp->n < 2) {
     229            maskB->data.U8[i] = 1;
     230            psFree (tmp);
     231            continue;
     232        }
     233
     234        // dA values are contaminated with low outliers
     235        // measure statistics only on upper 50% of points
     236        // this would be easier if we could sort in reverse:
     237
     238        psVectorSort (tmp, tmp);
     239        nKeep = 0.5*tmp->n;
     240        nSkip = tmp->n - nKeep;
     241
     242        psVector *tmp2 = psVectorAlloc (nKeep, PS_TYPE_F64);
     243        for (int j = 0; j < tmp2->n; j++) {
     244            tmp2->data.F64[j] = tmp->data.F64[j + nSkip];
     245        }
     246
     247        stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN);
     248        psVectorStats (stats, tmp2, NULL, NULL, 0);
     249        psTrace ("psphot.metricmodel", 4, "rfBin %d (%g): %d pts, %g\n", i, rfBin->data.F64[i], tmp->n, stats->sampleMedian);
     250
     251        daBin->data.F64[i] = stats->sampleMedian;
     252
     253        psFree (stats);
     254        psFree (tmp);
     255        psFree (tmp2);
     256    }
     257
     258    // linear clipped fit to rfBin, daBin
     259    psPolynomial1D *poly = psPolynomial1DAlloc (1, PS_POLYNOMIAL_ORD);
     260    psStats *fitstat = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     261    poly = psVectorClipFitPolynomial1D (poly, fitstat, maskB, 1, daBin, NULL, rfBin);
     262
     263    psVector *daBinFit = psPolynomial1DEvalVector (poly, rfBin);
     264    psVector *daResid  = (psVector *) psBinaryOp (NULL, (void *) daBin, "-", (void *) daBinFit);
     265
     266    stats = psStatsAlloc (PS_STAT_CLIPPED_STDEV);
     267    stats = psVectorStats (stats, daResid, NULL, maskB, 1);
     268
     269    try->psf->ApResid = poly->coeff[0];
     270    try->psf->dApResid = stats->clippedStdev;
     271    try->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
     272
     273    psFree (rflux);
     274    psFree (rfBin);
     275    psFree (daBin);
     276    psFree (maskB);
     277    psFree (daBinFit);
     278    psFree (daResid);
     279    psFree (poly);
    262280    psFree (stats);
    263     psFree (tmp);
    264     psFree (tmp2);
    265   }
    266 
    267   // linear clipped fit to rfBin, daBin
    268   psPolynomial1D *poly = psPolynomial1DAlloc (1, PS_POLYNOMIAL_ORD);
    269   psStats *fitstat = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
    270   poly = psVectorClipFitPolynomial1D (poly, fitstat, maskB, 1, daBin, NULL, rfBin);
    271 
    272   psVector *daBinFit = psPolynomial1DEvalVector (poly, rfBin);
    273   psVector *daResid  = (psVector *) psBinaryOp (NULL, (void *) daBin, "-", (void *) daBinFit);
    274 
    275   stats = psStatsAlloc (PS_STAT_CLIPPED_STDEV);
    276   stats = psVectorStats (stats, daResid, NULL, maskB, 1);
    277 
    278   try->psf->ApResid = poly->coeff[0];
    279   try->psf->dApResid = stats->clippedStdev;
    280   try->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
    281 
    282   psFree (rflux);
    283   psFree (rfBin);
    284   psFree (daBin);
    285   psFree (maskB);
    286   psFree (daBinFit);
    287   psFree (daResid);
    288   psFree (poly);
    289   psFree (stats);
    290   psFree (fitstat);
    291 
    292   return true;
     281    psFree (fitstat);
     282
     283    return true;
    293284}
    294285
    295286bool pmPSFtryMetric_Alt (pmPSFtry *try, float RADIUS) {
    296287
    297   // the measured (aperture - fit) magnitudes (dA == try->metric)
    298   //   depend on both the true ap-fit (dAo) and the bias in the sky measurement:
    299   //     dA = dAo + dsky/flux
    300   //   where flux is the flux of the star
    301   // we fit this trend to find the infinite flux aperture correction (dAo),
    302   //   the nominal sky bias (dsky), and the error on dAo
    303   // the values of dA are contaminated by stars with close neighbors in the aperture
    304   //   we use an outlier rejection to avoid this bias
    305 
    306   // rflux = ten(0.4*fitMag);
    307   psVector *rflux = psVectorAlloc (try->sources->n, PS_TYPE_F64);
    308   for (int i = 0; i < try->sources->n; i++) {
    309     if (try->mask->data.U8[i] & PSFTRY_MASK_ALL) continue;
    310     rflux->data.F64[i] = pow(10.0, 0.4*try->fitMag->data.F64[i]);
    311   }
    312 
    313   // XXX EAM : try 3hi/1lo sigma clipping on the rflux vs metric fit
    314   psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
    315 
    316   // XXX EAM
    317   stats->min = 1.0;
    318   stats->max = 3.0;
    319   stats->clipIter = 3;
    320 
    321   // linear clipped fit to rfBin, daBin
    322   psPolynomial1D *poly = psPolynomial1DAlloc (1, PS_POLYNOMIAL_ORD);
    323   poly = psVectorClipFitPolynomial1D (poly, stats, try->mask, PSFTRY_MASK_ALL, try->metric, NULL, rflux);
    324   fprintf (stderr, "fit stats: %f +/- %f\n", stats->sampleMedian, stats->sampleStdev);
    325 
    326   try->psf->ApResid = poly->coeff[0];
    327   try->psf->dApResid = stats->sampleStdev;
    328   try->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
    329 
    330   FILE *f;
    331   f = fopen ("apresid.dat", "w");
    332   if (f == NULL) psAbort ("pmPSFtry", "can't open output file");
    333 
    334   for (int i = 0; i < try->sources->n; i++) {
    335     fprintf (f, "%3d %8.4f %12.5e %8.4f %3d\n", i, try->fitMag->data.F64[i], rflux->data.F64[i], try->metric->data.F64[i], try->mask->data.U8[i]);
    336   }
    337   fclose (f);
    338 
    339   psFree (rflux);
    340   psFree (poly);
    341   psFree (stats);
    342 
    343   // psFree (daFit);
    344   // psFree (daResid);
    345 
    346   return true;
    347 }
     288    // the measured (aperture - fit) magnitudes (dA == try->metric)
     289    //   depend on both the true ap-fit (dAo) and the bias in the sky measurement:
     290    //     dA = dAo + dsky/flux
     291    //   where flux is the flux of the star
     292    // we fit this trend to find the infinite flux aperture correction (dAo),
     293    //   the nominal sky bias (dsky), and the error on dAo
     294    // the values of dA are contaminated by stars with close neighbors in the aperture
     295    //   we use an outlier rejection to avoid this bias
     296
     297    // rflux = ten(0.4*fitMag);
     298    psVector *rflux = psVectorAlloc (try->sources->n, PS_TYPE_F64);
     299    for (int i = 0; i < try->sources->n; i++) {
     300        if (try->mask->data.U8[i] & PSFTRY_MASK_ALL) continue;
     301        rflux->data.F64[i] = pow(10.0, 0.4*try->fitMag->data.F64[i]);
     302    }
     303
     304    // XXX EAM : try 3hi/1lo sigma clipping on the rflux vs metric fit
     305    psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     306
     307    // XXX EAM
     308    stats->min = 1.0;
     309    stats->max = 3.0;
     310    stats->clipIter = 3;
     311
     312    // linear clipped fit to rfBin, daBin
     313    psPolynomial1D *poly = psPolynomial1DAlloc (1, PS_POLYNOMIAL_ORD);
     314    poly = psVectorClipFitPolynomial1D (poly, stats, try->mask, PSFTRY_MASK_ALL, try->metric, NULL, rflux);
     315    fprintf (stderr, "fit stats: %f +/- %f\n", stats->sampleMedian, stats->sampleStdev);
     316
     317    try->psf->ApResid = poly->coeff[0];
     318    try->psf->dApResid = stats->sampleStdev;
     319    try->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
     320
     321    FILE *f;
     322    f = fopen ("apresid.dat", "w");
     323    if (f == NULL) psAbort ("pmPSFtry", "can't open output file");
     324
     325    for (int i = 0; i < try->sources->n; i++) {
     326        fprintf (f, "%3d %8.4f %12.5e %8.4f %3d\n", i, try->fitMag->data.F64[i], rflux->data.F64[i], try->metric->data.F64[i], try->mask->data.U8[i]);
     327    }
     328    fclose (f);
     329
     330    psFree (rflux);
     331    psFree (poly);
     332    psFree (stats);
     333
     334    // psFree (daFit);
     335    // psFree (daResid);
     336
     337    return true;
     338}
Note: See TracChangeset for help on using the changeset viewer.