IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 1, 2006, 10:16:15 AM (20 years ago)
Author:
Paul Price
Message:

Working version for flat-fielding. Need to add extenal scale/zero input, mask saturated pixels, test on dark, bias, fringe.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppMerge/src/ppMergeBackground.c

    r7067 r7260  
    4949    // Sanity checks
    5050    assert(!options->scale || scales);
    51     assert(!scales || !*scales || ((*scales)->type.type == PS_TYPE_F64 &&
     51    assert(!scales || !*scales || ((*scales)->type.type == PS_TYPE_F32 &&
    5252                                   (*scales)->numCols == data->numCells &&
    5353                                   (*scales)->numRows == filenames->n));
    5454    assert(!options->zero || zeros);
    55     assert(!zeros || !*zeros || ((*zeros)->type.type == PS_TYPE_F64 &&
     55    assert(!zeros || !*zeros || ((*zeros)->type.type == PS_TYPE_F32 &&
    5656                                 (*zeros)->numCols == data->numCells &&
    5757                                 (*zeros)->numRows == filenames->n));
    5858
    59     psImage *background = psImageAlloc(filenames->n, data->numCells, PS_TYPE_F64); // Background measurements
     59    psImage *background = psImageAlloc(data->numCells, filenames->n, PS_TYPE_F32); // Background measurements
    6060    psImageInit(background, NAN);
    6161    psStats *bgStats = psStatsAlloc(options->background); // Statistic to measure the background
     
    6363    psImage *exptime = NULL;            // The exposure time for each integration of each cell
    6464    if (options->scale) {
    65         gains = psVectorAlloc(data->numCells, PS_TYPE_F64);
     65        gains = psVectorAlloc(data->numCells, PS_TYPE_F32);
     66        gains->n = data->numCells;
    6667    }
    6768    if (options->exptime) {
    68         exptime = psImageAlloc(filenames->n, data->numCells, PS_TYPE_F64);
     69        exptime = psImageAlloc(filenames->n, data->numCells, PS_TYPE_F32);
    6970    }
    7071
     
    7576            continue;
    7677        }
     78        psTrace(__func__, 9, "Opening %s to get background...\n", name);
    7779        psFits *inFile = psFitsOpen(filenames->data[i], "r"); // The FITS file to read
    7880        if (!inFile) {
     
    107109                if (options->exptime) {
    108110                    bool mdok = true;   // Status of MD lookup
    109                     exptime->data.F64[i][cellNum] = psMetadataLookupF32(&mdok, cell->concepts,
     111                    exptime->data.F32[i][cellNum] = psMetadataLookupF32(&mdok, cell->concepts,
    110112                                                                        "CELL.EXPOSURE");
    111                     if (!mdok || isnan(exptime->data.F64[i][cellNum])) {
     113                    if (!mdok || isnan(exptime->data.F32[i][cellNum])) {
    112114                        psLogMsg(__func__, PS_LOG_WARN, "CELL.EXPOSURE for file %s chip %d cell %d is not "
    113115                                 "set.\n", name, j, k);
    114                         exptime->data.F64[i][cellNum] = NAN;
     116                        exptime->data.F32[i][cellNum] = NAN;
    115117                        status = false;
    116118                    }
     
    151153
    152154                    // Get the background
    153                     psImageStats(bgStats, image, readout->mask, options->combine->maskVal);
    154                     background->data.F64[i][cellNum] = getStat(bgStats, options->background);
     155                    int sampleSize = (image->numCols * image->numRows) / options->sample; // Size of sample
     156                    psVector *sample = psVectorAlloc(sampleSize, PS_TYPE_F32); // Sample of the image
     157                    sample->n = sampleSize;
     158                    psVector *sampleMask = NULL; // Mask for sample
     159                    if (readout->mask) {
     160                        sampleMask = psVectorAlloc(sampleSize, PS_TYPE_U8);
     161                        sampleMask->n = sampleSize;
     162                    }
     163                    psImage *mask = readout->mask; // The mask image
     164                    for (long i = 0; i < sampleSize; i++) {
     165                        int j = i * options->sample; // Index into image
     166                        int x = j % image->numCols; // x index
     167                        int y = j / image->numCols; // y index
     168                        sample->data.F32[i] = image->data.F32[y][x];
     169                        if (readout->mask) {
     170                            sampleMask->data.U8[i] = mask->data.U8[y][x];
     171                        }
     172                    }
     173                    psVectorStats(bgStats, sample, sampleMask, NULL, options->combine->maskVal);
     174                    psFree(sample);
     175                    psFree(sampleMask);
     176                    background->data.F32[i][cellNum] = getStat(bgStats, options->background);
     177                    psTrace(__func__, 3, "Background for %s, cell %d is %f\n", name, cellNum,
     178                            background->data.F32[i][cellNum]);
    155179                }
    156180
     
    167191        // Need to normalize over the focal plane
    168192        bool converge = true;           // Did we converge?
     193        if (psTraceGetLevel(__func__) > 9) {
     194            for (int i = 0; i < gains->n; i++) {
     195                psTrace(__func__, 10, "Gain for cell %d is %f\n", i, gains->data.F32[i]);
     196            }
     197        }
    169198        psVector *fluxes = pmFlatNormalize(&converge, gains, background, MAX_ITER, TOLERANCE);
    170199        if (!converge || !fluxes) {
     
    174203        psFree(gains);
    175204
    176         *scales = psImageCopy(*scales, background, PS_TYPE_F64);
     205        *scales = psImageCopy(*scales, background, PS_TYPE_F32);
    177206        psImage *scalesDeref = *scales; // Dereference the pointer
    178207
    179208        for (int i = 0; i < scalesDeref->numRows; i++) {
    180             psF64 bg = fluxes->data.F64[i];
     209            psF32 bg = fluxes->data.F32[i];
    181210            for (int j = 0; j < scalesDeref->numCols; j++) {
    182                 scalesDeref->data.F64[i][j] = bg;
     211                scalesDeref->data.F32[i][j] = bg;
    183212            }
    184213        }
     
    188217        if (!options->scale) {
    189218            // Copy over the exposure times
    190             psImageCopy(*scales, exptime, PS_TYPE_F64);
     219            psImageCopy(*scales, exptime, PS_TYPE_F32);
    191220        } else {
    192221            psBinaryOp(*scales, *scales, "*", exptime);
     
    199228            *zeros = psMemIncrRefCounter(background);
    200229        } else {
    201             *zeros = psImageCopy(*zeros, background, PS_TYPE_F64);
     230            *zeros = psImageCopy(*zeros, background, PS_TYPE_F32);
     231        }
     232    }
     233
     234    // Diagnostic stuff
     235    if (scales && *scales && psTraceGetLevel(__func__) > 9) {
     236        psImage *scalesDeref = *scales; // Dereference the pointer
     237        for (int i = 0; i < scalesDeref->numRows; i++) {
     238            for (int j = 0; j < scalesDeref->numCols; j++) {
     239                psTrace(__func__, 9, "Scale for exposure %d, cell %d is: %f\n", i, j,
     240                        scalesDeref->data.F32[i][j]);
     241            }
     242        }
     243    }
     244    if (zeros && *zeros && psTraceGetLevel(__func__) > 9) {
     245        psImage *zerosDeref = *zeros; // Dereference the pointer
     246        for (int i = 0; i < zerosDeref->numRows; i++) {
     247            for (int j = 0; j < zerosDeref->numCols; j++) {
     248                psTrace(__func__, 9, "Zero for exposure %d, cell %d is: %f\n", i, j,
     249                        zerosDeref->data.F32[i][j]);
     250            }
    202251        }
    203252    }
Note: See TracChangeset for help on using the changeset viewer.