IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 26649


Ignore:
Timestamp:
Jan 20, 2010, 5:20:00 PM (16 years ago)
Author:
Paul Price
Message:

Adding scaling of kernel based on input PSFs.

Location:
branches/eam_branches/20091201/ppSub/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20091201/ppSub/src/ppSubCamera.c

    r26597 r26649  
    317317
    318318    // psPhot input
    319     if (data->photometry) {
     319    if (data->photometry || 1) {
    320320        psphotModelClassInit();        // load implementation-specific models
    321321
     
    329329            return false;
    330330        }
    331         // specify the number of psphot input images
    332         psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "number of inputs", 1);
     331        // specify the number of psphot input images
     332        psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "number of inputs", 1);
    333333        pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");
    334334
  • branches/eam_branches/20091201/ppSub/src/ppSubMakePSF.c

    r26612 r26649  
    7070        psMetadataRemoveKey(photRO->analysis, "PSPHOT.SOURCES");
    7171    }
     72    if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) {
     73        psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF");
     74    }
    7275
    7376# ifdef TESTING
     
    101104        psErrorStackPrint(stderr, "PSF was not generated");
    102105        psWarning("PSF was not generated --- suspect bad data quality.");
    103         ppSubDataQuality(data, psErrorCodeLast(), PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
     106        ppSubDataQuality(data, psErrorCodeLast(), PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
    104107    }
    105108
     
    124127
    125128    if (!outputRO) {
    126         pmCell *outputCell  = pmFPAviewThisCell(view, output->fpa);
    127         outputRO = pmReadoutAlloc(outputCell);
    128         outputRO->image = psMemIncrRefCounter (inputRO->image);
     129        pmCell *outputCell  = pmFPAviewThisCell(view, output->fpa);
     130        outputRO = pmReadoutAlloc(outputCell);
     131        outputRO->image = psMemIncrRefCounter (inputRO->image);
    129132    }
    130133
     
    135138    if (!nRegions) {
    136139        psErrorStackPrint(stderr, "No PSF available");
    137         return false;
     140        return false;
    138141    }
    139142    psMetadataAddS32 (psfRegions, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", nRegions);
    140143
    141144    for (int i = 0; i < nRegions; i++) {
    142         char fieldName[80];
    143         snprintf (fieldName, 80, "PSF.CLUMP.REGION.%03d", i);
     145        char fieldName[80];
     146        snprintf (fieldName, 80, "PSF.CLUMP.REGION.%03d", i);
    144147        psMetadata *regionMD = psMetadataLookupPtr (&mdok, inputRO->analysis, fieldName);
    145148        if (!regionMD) {
    146             psWarning ("missing psf clump region metadata for entry %d", i);
    147             continue;
    148         }
    149         psMetadataAddMetadata (psfRegions, PS_LIST_TAIL, fieldName, PS_META_REPLACE, "psf clump region", regionMD);
     149            psWarning ("missing psf clump region metadata for entry %d", i);
     150            continue;
     151        }
     152        psMetadataAddMetadata (psfRegions, PS_LIST_TAIL, fieldName, PS_META_REPLACE, "psf clump region", regionMD);
    150153    }
    151154
  • branches/eam_branches/20091201/ppSub/src/ppSubMatchPSFs.c

    r26581 r26649  
    3636    psFree(subImage);
    3737    return;
     38}
     39
     40// Measure the PSF for an image
     41static float subImagePSF(ppSubData *data, // Processing data
     42                         const pmReadout *ro, // Readout for which to measure PSF
     43                         psArray *sources     // Sources with positions at which to measure PSF
     44    )
     45{
     46    psAssert(data, "Require processing data");
     47    pmConfig *config = data->config;    // Configuration
     48    psAssert(config, "Require configuration");
     49
     50    psAssert(ro, "Need readout");
     51    psAssert(sources, "Need sources.");
     52
     53    pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT"); // Photometry file
     54    psAssert(photFile, "Need photometry file.");
     55    if (!pmFPACopy(photFile->fpa, ro->parent->parent->parent)) {
     56        psError(PS_ERR_UNKNOWN, false, "Unable to copy FPA for photometry");
     57        return false;
     58    }
     59
     60    pmFPAview *view = ppSubViewReadout(); // View to readout
     61    pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
     62
     63    if (psMetadataLookup(photRO->analysis, "PSPHOT.SOURCES")) {
     64        psMetadataRemoveKey(photRO->analysis, "PSPHOT.SOURCES");
     65    }
     66    if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) {
     67        psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF");
     68    }
     69
     70    // Extract the loaded sources from the associated readout, and generate PSF
     71    // Here, we assume the image is background-subtracted
     72    if (!psphotReadoutFindPSF(config, view, sources)) {
     73        psErrorStackPrint(stderr, "Unable to determine PSF");
     74        psWarning("Unable to determine PSF.");
     75        psFree(view);
     76        return true;
     77    }
     78
     79    psFree(view);
     80
     81    psMetadata *header = psMetadataLookupMetadata(NULL, photRO->analysis, "PSPHOT.HEADER");
     82    psAssert(header, "Require header.");
     83    float fwhm = psMetadataLookupF32(NULL, header, "FWHM_MAJ");
     84
     85    return fwhm;
     86}
     87
     88// Scale the kernel parameters according to the PSFs
     89static bool subScaleKernel(ppSubData *data, // Processing data
     90                           psVector *kernelWidths, // Widths for kernel
     91                           int *kernelSize,        // Size of kernel
     92                           int *stampSize          // Size of stamps (footprint)
     93    )
     94{
     95    psAssert(data, "Require processing data");
     96    pmConfig *config = data->config;    // Configuration
     97    psAssert(config, "Require configuration");
     98
     99    // Nothing to do if pre-calculated kernel exists
     100    pmFPAview *view = ppSubViewReadout(); // View to readout
     101    pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel
     102    if (kernelRO) {
     103        psFree(view);
     104        return true;
     105    }
     106
     107    // Look up recipe values
     108    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
     109    psAssert(recipe, "We checked this earlier, so it should be here.");
     110    if (!psMetadataLookupBool(NULL, recipe, "SCALE")) {
     111        // No scaling requested
     112        psFree(view);
     113        return true;
     114    }
     115
     116    // Input images
     117    pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout
     118    pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout
     119
     120    // Input sources
     121    pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES");
     122    pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES");
     123    psArray *inSources = psMetadataLookupPtr(NULL, inSourceRO->analysis, "PSPHOT.SOURCES"); // Input sources
     124    psArray *refSources = psMetadataLookupPtr(NULL, refSourceRO->analysis, "PSPHOT.SOURCES"); // Ref sources
     125
     126    psFree(view);
     127
     128    if (!inSources || !refSources) {
     129        psWarning("Unable to scale kernel, since no sources were provided.");
     130        return true;
     131    }
     132
     133
     134    float inFWHM = subImagePSF(data, inRO, inSources); // FWHM for input
     135    float refFWHM = subImagePSF(data, refRO, refSources); // FWHM for reference
     136    psLogMsg("ppSub", PS_LOG_INFO, "Input FWHM: %f\nReference FWHM: %f\n", inFWHM, refFWHM);
     137    if (!isfinite(inFWHM) || !isfinite(refFWHM)) {
     138        psWarning("Unable to scale kernel, since unable to measure PSFs.");
     139        return true;
     140    }
     141
     142//    float diff = sqrtf(PS_SQR(PS_MAX(inFWHM, refFWHM)) - PS_SQR(PS_MIN(inFWHM, refFWHM))); // Difference
     143    float diff = PS_MAX(inFWHM, refFWHM);
     144
     145    float scaleRef = psMetadataLookupF32(NULL, recipe, "SCALE.REF"); // Reference for scaling
     146    float scaleMin = psMetadataLookupF32(NULL, recipe, "SCALE.MIN"); // Minimum for scaling
     147    float scaleMax = psMetadataLookupF32(NULL, recipe, "SCALE.MAX"); // Maximum for scaling
     148    if (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax)) {
     149        psError(PPSUB_ERR_ARGUMENTS, false,
     150                "Scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
     151                scaleRef, scaleMin, scaleMax);
     152        return false;
     153    }
     154
     155    float scale = diff / scaleRef;      // Scaling factor
     156    if (scale < scaleMin) {
     157        scale = scaleMin;
     158    }
     159    if (scale > scaleMax) {
     160        scale = scaleMax;
     161    }
     162
     163    for (int i = 0; i < kernelWidths->n; i++) {
     164        kernelWidths->data.F32[i] *= scale;
     165    }
     166    *kernelSize = *kernelSize * scale + 0.5;
     167    *stampSize = *stampSize * scale + 0.5;
     168
     169    psLogMsg("ppSub", PS_LOG_INFO, "Scaled kernel parameters by %f: %d %d", scale, *kernelSize, *stampSize);
     170
     171    return true;
    38172}
    39173
     
    133267    float kernelErr = psMetadataLookupF32(NULL, recipe, "KERNEL.ERR"); // Relative systematic error in kernel
    134268    float sysErr = psMetadataLookupF32(NULL, recipe, "SYS.ERR"); // Relative systematic error in images
    135     float skyErr = psMetadataLookupF32(NULL, recipe, "SKY.ERR"); // Relative systematic error in images
     269    float skyErr = psMetadataLookupF32(NULL, recipe, "SKY.ERR"); // Additional error in sky
    136270
    137271    float badFrac = psMetadataLookupF32(NULL, recipe, "BADFRAC"); // Maximum bad fraction
     
    171305            break;
    172306          default:
    173             psErrorStackPrint(stderr, "Invalid value for -convolve");
     307            psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Invalid value for -convolve");
    174308            return false;
    175309        }
     310    }
     311
     312    if (!subScaleKernel(data, widths, &size, &footprint)) {
     313        psError(PS_ERR_UNKNOWN, false, "Unable to scale kernel parameters");
     314        return false;
    176315    }
    177316
  • branches/eam_branches/20091201/ppSub/src/ppSubReadoutPhotometry.c

    r26432 r26649  
    7878    // erase the overlays from a previous psphot-related step
    7979    if (pmVisualIsVisual()) {
    80         psphotVisualEraseOverlays (1, "all");
     80        //      psphotVisualEraseOverlays (1, "all");
    8181    }
    8282
Note: See TracChangeset for help on using the changeset viewer.