IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 20772


Ignore:
Timestamp:
Nov 16, 2008, 4:42:02 PM (17 years ago)
Author:
eugene
Message:

use the already-defined background model from psphot, if available

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppImage/src/ppImageReplaceBackground.c

    r20444 r20772  
    2020    if (!status) {
    2121        psError(PS_ERR_UNEXPECTED_NULL, true, "PPIMAGE.CHIP file is not defined");
    22         return false;
    23     }
    24 
    25     // The background model file may be defined, though the model hasn't been built
    26     // If so, we will build it below.
    27     pmFPAfile *modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL"); // Background model
    28     if (!status) {
    29         psError(PS_ERR_UNEXPECTED_NULL, true, "PSPHOT.BACKMDL file is not defined");
    3022        return false;
    3123    }
     
    7567    psImage *image = ro->image, *mask = ro->mask; // Image and mask of interest
    7668
     69    // View corresponding to this readout
     70    pmFPAview roView = *view;
     71    roView.cell = roView.readout = 0;
    7772
    78     pmFPAview roView = *view;           // View to readout
    79     roView.cell = roView.readout = 0;
    80     pmReadout *modelRO = pmFPAviewThisReadout(&roView, modelFile->fpa); // Background model
    81     if (!modelRO) {
    82         // Create the background model
     73    // If the background model file has not been defined, psphotModelBackground will generate it
     74    pmReadout *modelRO = NULL;
     75    pmFPAfile *modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL"); // Background model
     76    if (modelFile) {
     77        modelRO = pmFPAviewThisReadout(&roView, modelFile->fpa); // Background model
     78    }
     79
     80    // the background model has not been defined, or at least not generated
     81    if (!modelFile || !modelRO) {
    8382        if (!psphotModelBackground(config, &roView, "PPIMAGE.CHIP")) {
    8483            psError(PS_ERR_UNKNOWN, false, "Unable to model background");
    8584            return false;
    8685        }
    87         modelRO = (modelFile->mode == PM_FPA_MODE_INTERNAL) ? modelFile->readout :
    88                    pmFPAviewThisReadout(&roView, modelFile->fpa);
    89         if (!modelRO) {
     86        // the model file should now at least be defined
     87        modelFile = psMetadataLookupPtr(&status, config->files, "PSPHOT.BACKMDL"); // Background model
     88        if (!modelFile) {
     89            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to define background model I/O file");
     90            return false;
     91        }
     92        // now grab the readout from the correct location:
     93        if (modelFile->mode == PM_FPA_MODE_INTERNAL) {
     94            modelRO = modelFile->readout;
     95        } else {
     96            modelRO = pmFPAviewThisReadout(&roView, modelFile->fpa);
     97        }       
     98        if (!modelRO) {
    9099            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find background model");
    91100            return false;
    92101        }
    93102    }
    94     psImageBinning *binning = psMetadataLookupPtr(&status, psphotRecipe,
    95                                                   "PSPHOT.BACKGROUND.BINNING"); // Binning for model
    96     psImage *modelImage = modelRO->image; // Background model
     103    psImageBinning *binning = psMetadataLookupPtr(&status, psphotRecipe, "PSPHOT.BACKGROUND.BINNING"); // Binning for model
     104    if (!binning) {
     105        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find background binning");
     106        return false;
     107    }
     108
     109# define USE_UNBIN 1
     110# if (USE_UNBIN)
     111    // select background pixels, from output background file, or create
     112    pmReadout *background = NULL;
     113    pmFPAfile *backfile = psMetadataLookupPtr (&status, config->files, "PSPHOT.BACKGND");
     114    if (backfile) {
     115        // we are using PSPHOT.BACKGND as an I/O file: select readout or create
     116        if (backfile->mode == PM_FPA_MODE_INTERNAL) {
     117            background = backfile->readout;
     118        } else {
     119            background = pmFPAviewThisReadout (&roView, backfile->fpa);
     120        }
     121        if (background == NULL) {
     122            // readout does not yet exist: create from input
     123            pmFPAfileCopyStructureView (backfile->fpa, input->fpa, 1, 1, &roView);
     124            background = pmFPAviewThisReadout (&roView, backfile->fpa);
     125            if ((image->numCols != background->image->numCols) || (image->numRows != background->image->numRows)) {
     126                psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for background dimensions");
     127                return false;
     128            }
     129        }
     130    } else {
     131        background = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", image->numCols, image->numRows, PS_TYPE_F32);
     132    }
     133    psF32 **backData = background->image->data.F32;
     134
     135    // linear interpolation to full-scale
     136    if (!psImageUnbin (background->image, modelRO->image, binning)) {
     137        psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
     138        return false;
     139    }
    97140
    98141    // Do the background subtraction
     
    103146                image->data.F32[y][x] = 0.0;
    104147            } else {
    105                 float value = psImageUnbinPixel_V2(x, y, modelImage, binning); // Background value
     148                float value = backData[y][x];
    106149                if (!isfinite(value)) {
    107150                    image->data.F32[y][x] = NAN;
     
    113156        }
    114157    }
    115 
    116     pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL");
    117     pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL.STDEV");
     158# else
     159    // Do the background subtraction
     160    int numCols = image->numCols, numRows = image->numRows; // Size of image
     161    for (int y = 0; y < numRows; y++) {
     162        for (int x = 0; x < numCols; x++) {
     163            if (mask && mask->data.PS_TYPE_MASK_DATA[y][x] & maskVal) {
     164                image->data.F32[y][x] = 0.0;
     165            } else {
     166                float value = psImageUnbinPixel(x + 0.5, y + 0.5, modelRO->image, binning); // Background value
     167                if (!isfinite(value)) {
     168                    image->data.F32[y][x] = NAN;
     169                    mask->data.PS_TYPE_MASK_DATA[y][x] |= options->badMask;
     170                } else {
     171                    image->data.F32[y][x] -= value;
     172                }
     173            }
     174        }
     175    }
     176# endif
     177   
     178    // XXX should these really be here?? (probably not...)
     179    // pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL");
     180    // pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL.STDEV");
    118181
    119182    return true;
Note: See TracChangeset for help on using the changeset viewer.