IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 10, 2010, 7:42:02 PM (16 years ago)
Author:
eugene
Message:

updates from eam_branches/20091201

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppSub/src/ppSubMatchPSFs.c

    r26036 r26899  
    1818#include <pslib.h>
    1919#include <psmodules.h>
     20#include <psphot.h>
    2021
    2122#include "ppSub.h"
     23
     24#define COVAR_FRAC 0.01                 // Truncation fraction for covariance matrix
    2225
    2326// Normalise a region on an image
     
    3740}
    3841
     42// Measure the PSF for an image
     43static float subImagePSF(ppSubData *data, // Processing data
     44                         const pmReadout *ro, // Readout for which to measure PSF
     45                         psArray *sources     // Sources with positions at which to measure PSF
     46    )
     47{
     48    psAssert(data, "Require processing data");
     49    pmConfig *config = data->config;    // Configuration
     50    psAssert(config, "Require configuration");
     51
     52    psAssert(ro, "Need readout");
     53    psAssert(sources, "Need sources.");
     54
     55    pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT"); // Photometry file
     56    psAssert(photFile, "Need photometry file.");
     57    if (!pmFPACopy(photFile->fpa, ro->parent->parent->parent)) {
     58        psError(PS_ERR_UNKNOWN, false, "Unable to copy FPA for photometry");
     59        return false;
     60    }
     61
     62    pmFPAview *view = ppSubViewReadout(); // View to readout
     63    pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
     64
     65    if (psMetadataLookup(photRO->analysis, "PSPHOT.DETECTIONS")) {
     66        psMetadataRemoveKey(photRO->analysis, "PSPHOT.DETECTIONS");
     67    }
     68    if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) {
     69        psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF");
     70    }
     71
     72    // Extract the loaded sources from the associated readout, and generate PSF
     73    // Here, we assume the image is background-subtracted
     74    if (!psphotReadoutFindPSF(config, view, sources)) {
     75        psErrorStackPrint(stderr, "Unable to determine PSF");
     76        psWarning("Unable to determine PSF.");
     77        psFree(view);
     78        return true;
     79    }
     80
     81    psFree(view);
     82
     83    psMetadata *header = psMetadataLookupMetadata(NULL, photRO->analysis, "PSPHOT.HEADER");
     84    psAssert(header, "Require header.");
     85    float fwhm = psMetadataLookupF32(NULL, header, "FWHM_MAJ");
     86
     87    return fwhm;
     88}
     89
     90// Scale the kernel parameters according to the PSFs
     91static bool subScaleKernel(ppSubData *data, // Processing data
     92                           psVector *kernelWidths, // Widths for kernel
     93                           int *kernelSize,        // Size of kernel
     94                           int *stampSize          // Size of stamps (footprint)
     95    )
     96{
     97    psAssert(data, "Require processing data");
     98    pmConfig *config = data->config;    // Configuration
     99    psAssert(config, "Require configuration");
     100
     101    // Nothing to do if pre-calculated kernel exists
     102    pmFPAview *view = ppSubViewReadout(); // View to readout
     103    pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel
     104    if (kernelRO) {
     105        psFree(view);
     106        return true;
     107    }
     108
     109    // Look up recipe values
     110    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
     111    psAssert(recipe, "We checked this earlier, so it should be here.");
     112    if (!psMetadataLookupBool(NULL, recipe, "SCALE")) {
     113        // No scaling requested
     114        psFree(view);
     115        return true;
     116    }
     117
     118    // Input images
     119    pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout
     120    pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout
     121
     122    // Input sources
     123    pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES");
     124    pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES");
     125    // XXX assert on inSourcesRO and refSourcesRO?
     126
     127    pmDetections *inDetections = psMetadataLookupPtr(NULL, inSourceRO->analysis, "PSPHOT.DETECTIONS"); // Input sources
     128    pmDetections *refDetections = psMetadataLookupPtr(NULL, refSourceRO->analysis, "PSPHOT.DETECTIONS"); // Ref sources
     129
     130    psFree(view);
     131
     132    if (!inDetections || !refDetections) {
     133        psWarning("Unable to scale kernel, since no sources were provided.");
     134        return true;
     135    }
     136
     137    psArray *inSources = inDetections->allSources;
     138    psAssert (inSources, "missing inSources?");
     139
     140    psArray *refSources = refDetections->allSources;
     141    psAssert (refSources, "missing refSources?");
     142
     143    float inFWHM = subImagePSF(data, inRO, inSources); // FWHM for input
     144    float refFWHM = subImagePSF(data, refRO, refSources); // FWHM for reference
     145
     146    psLogMsg("ppSub", PS_LOG_INFO, "Input FWHM: %f\nReference FWHM: %f\n", inFWHM, refFWHM);
     147    if (!isfinite(inFWHM) || !isfinite(refFWHM)) {
     148        psWarning("Unable to scale kernel, since unable to measure PSFs.");
     149        return true;
     150    }
     151
     152    float scaleRef = psMetadataLookupF32(NULL, recipe, "SCALE.REF"); // Reference for scaling
     153    float scaleMin = psMetadataLookupF32(NULL, recipe, "SCALE.MIN"); // Minimum for scaling
     154    float scaleMax = psMetadataLookupF32(NULL, recipe, "SCALE.MAX"); // Maximum for scaling
     155    if (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax)) {
     156        psError(PPSUB_ERR_ARGUMENTS, false,
     157                "Scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
     158                scaleRef, scaleMin, scaleMax);
     159        return false;
     160    }
     161
     162    if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths, inFWHM, refFWHM,
     163                                  scaleRef, scaleMin, scaleMax)) {
     164        psError(PS_ERR_UNKNOWN, false, "Unable to scale parameters.");
     165        return false;
     166    }
     167
     168    return true;
     169}
     170
    39171
    40172bool ppSubMatchPSFs(ppSubData *data)
     
    71203    pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel
    72204
    73     psFree(view);
    74 
    75205    // Sources in image, used for stamps: these must be loaded from previous analysis stages
    76206    pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES");
    77207    pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES");
    78     psArray *inSources = inSourceRO ? psMetadataLookupPtr(&mdok, inSourceRO->analysis, "PSPHOT.SOURCES") :
    79         NULL; // Source list from input image
    80     psArray *refSources = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.SOURCES") :
    81         NULL ; // Source list from reference image
    82 
    83     psArray *sources = NULL;            // Merged list of sources
    84     if (inSources && refSources) {
     208
     209    pmDetections *inDetections  = inSourceRO  ? psMetadataLookupPtr(&mdok, inSourceRO->analysis,  "PSPHOT.DETECTIONS") : NULL; // Input sources
     210    pmDetections *refDetections = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.DETECTIONS") : NULL; // Ref sources
     211
     212    psFree(view);
     213
     214    pmDetections *detections = NULL;    // Merged detection set
     215    if (inDetections && refDetections) {
     216        psArray *inSources  = inDetections->allSources;
     217        psArray *refSources = refDetections->allSources;
     218
     219        psAssert (inSources, "missing in sources?");
     220        psAssert (refSources, "missing ref sources?");
     221
     222        detections = pmDetectionsAlloc();
    85223        float radius = psMetadataLookupF32(NULL, recipe, "SOURCE.RADIUS"); // Matching radius
    86224        psArray *lists = psArrayAlloc(2); // Source lists
    87225        lists->data[0] = psMemIncrRefCounter(inSources);
    88226        lists->data[1] = psMemIncrRefCounter(refSources);
    89         sources = pmSourceMatchMerge(lists, radius);
     227        detections->allSources = pmSourceMatchMerge(lists, radius);
    90228        psFree(lists);
    91         if (!sources) {
     229        if (!detections->allSources) {
    92230            psError(PS_ERR_UNKNOWN, false, "Unable to merge source lists");
     231            psFree(detections);
    93232            return false;
    94233        }
    95     } else if (inSources) {
    96         sources = psMemIncrRefCounter(inSources);
    97     } else if (refSources) {
    98         sources = psMemIncrRefCounter(refSources);
    99     }
    100 
    101     psMetadataAddArray(inConv->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_META_REPLACE,
    102                        "Merged source list", sources);
    103     psMetadataAddArray(refConv->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_META_REPLACE,
    104                        "Merged source list", sources);
    105     psFree(sources);                    // Drop reference
     234    }
     235    if (!detections && inDetections) {
     236        detections = psMemIncrRefCounter(inDetections);
     237    }
     238    if (!detections && refDetections) {
     239        detections = psMemIncrRefCounter(refDetections);
     240    }
     241
     242    psMetadataAddPtr(inConv->analysis,  PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "Merged source list", detections);
     243    psMetadataAddPtr(refConv->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "Merged source list", detections);
     244    psFree(detections);                    // Drop reference
    106245
    107246    int footprint = psMetadataLookupS32(NULL, recipe, "STAMP.FOOTPRINT"); // Stamp half-size
     
    131270    float rej = psMetadataLookupF32(NULL, recipe, "REJ"); // Rejection threshold
    132271    float kernelErr = psMetadataLookupF32(NULL, recipe, "KERNEL.ERR"); // Relative systematic error in kernel
     272    float normFrac = psMetadataLookupF32(NULL, recipe, "NORM.FRAC"); // Fraction of window for normalisn windw
    133273    float sysErr = psMetadataLookupF32(NULL, recipe, "SYS.ERR"); // Relative systematic error in images
     274    float skyErr = psMetadataLookupF32(NULL, recipe, "SKY.ERR"); // Additional error in sky
     275    float covarFrac = psMetadataLookupF32(NULL, recipe, "COVAR.FRAC"); // Fraction for covariance calculation
    134276
    135277    float badFrac = psMetadataLookupF32(NULL, recipe, "BADFRAC"); // Maximum bad fraction
     
    169311            break;
    170312          default:
    171             psErrorStackPrint(stderr, "Invalid value for -convolve");
     313            psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Invalid value for -convolve");
    172314            return false;
    173315        }
     316    }
     317
     318    if (!subScaleKernel(data, widths, &size, &footprint)) {
     319        psError(PS_ERR_UNKNOWN, false, "Unable to scale kernel parameters");
     320        return false;
    174321    }
    175322
     
    177324    if (threads > 0) {
    178325        pmSubtractionThreadsInit(inRO, refRO);
     326    }
     327
     328    if (inRO->covariance) {
     329        psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC);
     330    }
     331    if (refRO->covariance) {
     332        psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC);
    179333    }
    180334
     
    183337    if (kernelRO) {
    184338        success = pmSubtractionMatchPrecalc(inConv, refConv, inRO, refRO, kernelRO->analysis,
    185                                             stride, kernelErr, maskVal, maskBad, maskPoor, poorFrac, badFrac);
     339                                            stride, kernelErr, covarFrac, maskVal, maskBad, maskPoor,
     340                                            poorFrac, badFrac);
    186341    } else {
    187342        success = pmSubtractionMatch(inConv, refConv, inRO, refRO, footprint, stride, regionSize,
    188                                      spacing, threshold, sources, data->stamps, type, size, order,
     343                                     spacing, threshold, detections->allSources, data->stamps, type, size, order,
    189344                                     widths, orders, inner, ringsOrder, binning, penalty, optimum,
    190                                      optWidths, optOrder, optThresh, iter, rej, sysErr, kernelErr, maskVal,
    191                                      maskBad, maskPoor, poorFrac, badFrac, subMode);
    192     }
     345                                     optWidths, optOrder, optThresh, iter, rej, normFrac,
     346                                     sysErr, skyErr, kernelErr, covarFrac, maskVal, maskBad, maskPoor,
     347                                     poorFrac, badFrac, subMode);
     348    }
     349
     350# ifdef TESTING
     351    // XXX for testing
     352    psphotSaveImage (NULL, refRO->image,    "refRO.im.fits");
     353    psphotSaveImage (NULL, refRO->variance, "refRO.wt.fits");
     354    psphotSaveImage (NULL, refRO->mask,     "refRO.mk.fits");
     355
     356    psphotSaveImage (NULL, inRO->image,    "inRO.im.fits");
     357    psphotSaveImage (NULL, inRO->variance, "inRO.wt.fits");
     358    psphotSaveImage (NULL, inRO->mask,     "inRO.mk.fits");
     359
     360    psphotSaveImage (NULL, inConv->image,    "inConv.im.fits");
     361    psphotSaveImage (NULL, inConv->variance, "inConv.wt.fits");
     362    psphotSaveImage (NULL, inConv->mask,     "inConv.mk.fits");
     363
     364    psphotSaveImage (NULL, refConv->image,    "refConv.im.fits");
     365    psphotSaveImage (NULL, refConv->variance, "refConv.wt.fits");
     366    psphotSaveImage (NULL, refConv->mask,     "refConv.mk.fits");
     367# endif
    193368
    194369    psFree(optWidths);
     
    261436    pmConceptsCopyFPA(refConv->parent->parent->parent, refRO->parent->parent->parent, true, true);
    262437
     438    if (inConv->covariance) {
     439        psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC);
     440    }
     441    if (refConv->covariance) {
     442        psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC);
     443    }
     444
    263445    if (inConv->variance) {
    264446        psImageCovarianceTransfer(inConv->variance, inConv->covariance);
Note: See TracChangeset for help on using the changeset viewer.