IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 19, 2011, 12:50:30 PM (14 years ago)
Author:
watersc1
Message:

Fix for pattern correction that I forgot to commit previously.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/detrend/pmPattern.c

    r27676 r32970  
    66
    77#include "pmPattern.h"
     8
     9#define PATTERN_ROW_BKG_FIX 1
    810
    911
     
    8991    psImageInit(corr, NAN);
    9092
     93#ifdef PATTERN_ROW_BKG_FIX
     94    // CZW: 2011-11-30
     95    // Define the vectors to hold the "x" and "y" slope trends.
     96    // Briefly, the slope trend in the y-axis is a due to variations in the 0-th order term
     97    // of the PATTERN.ROW fit between individual rows across the cell.  Similarly, the 1-st
     98    // order term of the PATTERN.ROW fit defines the trend in the x-axis (as that's what we
     99    // are fitting with PATTERN.ROW in the first place).  However, the thing we're trying to
     100    // fix with PATTERN.ROW is the detector level bias wiggles.  These should be overlaid on
     101    // the true sky level.  Therefore, simply applying the PATTERN.ROW correction will
     102    // introduce cell-to-cell sky variations as these two trends are removed.  To avoid this,
     103    // We store the 0th and 1st order values used for each row, and then fit a polynomial to
     104    // these results.  By re-adding these systematic trends back, we can remove the row-to-row
     105    // variations without improperly removing the real sky trend.
     106    psVector *yaxisData = psVectorAlloc(numRows, PS_TYPE_F32); // Data to fit to the constant term
     107    psVector *yaxisMask = psVectorAlloc(numRows, PS_TYPE_VECTOR_MASK); // Mask for rows with no fit
     108    psVector *xaxisData = psVectorAlloc(numRows, PS_TYPE_F32); // Data to fit to the linear term
     109    psVectorInit(yaxisMask, 0);
     110#endif
    91111    for (int y = 0; y < numRows; y++) {
    92112        psVectorInit(clipMask, 0);
     
    105125            // Not enough points to fit
    106126            patternMaskRow(ro, y, maskBad);
     127#ifdef PATTERN_ROW_BKG_FIX
     128            // Ignore this row in our subsequent fits, because the fit failed.
     129            yaxisMask->data.PS_TYPE_VECTOR_MASK_DATA[y] = 0xFF;
     130#endif
    107131            continue;
    108132        }
     
    111135            psErrorClear();
    112136            patternMaskRow(ro, y, maskBad);
    113             continue;
    114         }
    115 
    116         poly->coeff[0] -= background;
     137#ifdef PATTERN_ROW_BKG_FIX
     138            // Ignore this row in our subsequent fits, because the fit failed.
     139            yaxisMask->data.PS_TYPE_VECTOR_MASK_DATA[y] = 0xFF;
     140#endif
     141            continue;
     142        }
     143#ifndef PATTERN_ROW_BKG_FIX
     144        poly->coeff[0] -= background;
     145#else
     146        // Store the results we found for this row.
     147        yaxisData->data.F32[y] = poly->coeff[0];
     148#endif
    117149        memcpy(corr->data.F64[y], poly->coeff, (order + 1) * PSELEMTYPE_SIZEOF(PS_TYPE_F64));
    118150        psVector *solution = psPolynomial1DEvalVector(poly, indices); // Solution vector
     
    121153            psErrorClear();
    122154            patternMaskRow(ro, y, maskBad);
     155#ifdef PATTERN_ROW_BKG_FIX
     156            yaxisMask->data.PS_TYPE_VECTOR_MASK_DATA[y] = 0xFF;
     157#endif
    123158            continue;
    124159        }
     
    130165    }
    131166
     167#ifdef PATTERN_ROW_BKG_FIX
     168    // Put the global trends back that were removed by the PATTERN.ROW correction.
     169    // Set up the indices for the polynomial
     170    psVector *yaxisIndices = psVectorAlloc(numRows, PS_TYPE_F32);
     171    norm = 2.0 / (float)numRows;
     172    for (int y = 0; y < numRows; y++) {
     173      yaxisIndices->data.F32[y] = y * norm - 1.0;
     174      psTrace("psModules.detrend.pattern",10,"%d %f %f\n",y,yaxisIndices->data.F32[y],yaxisData->data.F32[y]);
     175    }
     176
     177    // Fit the trend of the constant term, producing the y-axis global trend
     178    psStatsInit(clip);
     179    psPolynomial1D *yaxisPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2); // Polynomial to fit.
     180    if (!psVectorClipFitPolynomial1D(yaxisPoly,clip,yaxisMask,0xFF,yaxisData, NULL, yaxisIndices)) {
     181      psWarning("Unable to fit polynomial to y-axis trend");
     182      psErrorClear();
     183      // If we've failed, we need to do something, so add back in the background level, and
     184      // expect that the final image will have background mismatches.
     185      for (int y = 0; y < numRows; y++) {
     186        for (int x = 0; x < numCols; x++) {
     187          image->data.F32[y][x] += background;
     188          corr->data.F64[y][0]  -= background;
     189        }
     190      }
     191    }
     192    else {
     193      psVector *solution = psPolynomial1DEvalVector(yaxisPoly,yaxisIndices);
     194      if (!solution) {
     195        psWarning("Unable to evaluate polynomial");
     196        psErrorClear();
     197        // If we've failed, we need to do something, so add back in the background level, and
     198        // expect that the final image will have background mismatches.
     199        for (int y = 0; y < numRows; y++) {
     200          for (int x = 0; x < numCols; x++) {
     201            image->data.F32[y][x] += background;
     202            corr->data.F64[y][0]  -= background;
     203          }
     204        }
     205      }
     206      else {
     207        for (int y = 0; y < numRows; y++) {
     208          for (int x = 0; x < numCols; x++) {
     209            image->data.F32[y][x] += solution->data.F32[y];
     210            corr->data.F64[y][0]  -= solution->data.F32[y];
     211          }
     212        }
     213      }
     214      psFree(solution);
     215    }     
     216
     217    // Fit the trend of the linear term, producing the x-axis global trend
     218    // We can use the same mask vector, as the same rows failed the row-fit earlier.
     219    psStatsInit(clip);
     220    psPolynomial1D *xaxisPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2); // Polynomial to fit.
     221    if (!psVectorClipFitPolynomial1D(xaxisPoly,clip,yaxisMask,0xFF,xaxisData, NULL, yaxisIndices)) {
     222      psWarning("Unable to fit polynomial to x-axis trend");
     223      psErrorClear();
     224    }
     225    else {
     226      psVector *solution = psPolynomial1DEvalVector(xaxisPoly,yaxisIndices);
     227      if (!solution) {
     228        psWarning("Unable to evaluate polynomial");
     229        psErrorClear();
     230      }
     231      else {
     232        for (int y = 0; y < numRows; y++) {
     233          for (int x = 0; x < numCols; x++) {
     234            image->data.F32[y][x] += solution->data.F32[y] * indices->data.F32[x];
     235            corr->data.F64[y][0]  -= solution->data.F32[y] * indices->data.F32[x];
     236          }
     237        }
     238      }
     239      psFree(solution);
     240    }
     241    psFree(yaxisPoly);
     242    psFree(xaxisPoly);
     243    psFree(yaxisIndices);
     244    psFree(yaxisMask);
     245    psFree(yaxisData);
     246    psFree(xaxisData);
     247    // End PATTERN_ROW_BKG_FIX global trend replacement
     248#endif
     249   
    132250    psMetadataAddImage(ro->analysis, PS_LIST_TAIL, PM_PATTERN_ROW_CORRECTION, PS_META_REPLACE,
    133251                       "Pattern row correction", corr);
Note: See TracChangeset for help on using the changeset viewer.