IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/ppSub/src

    • Property svn:ignore
      •  

        old new  
        1313ppSubErrorCodes.c
        1414ppSubVersionDefinitions.h
         15ppSubConvolve
  • branches/simtest_nebulous_branches/ppSub/src/ppSubMatchPSFs.c

    r24862 r27840  
    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(PPSUB_ERR_CONFIG, 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    if (psMetadataLookup(photRO->analysis, "PSPHOT.DETECTIONS")) {
     88        psMetadataRemoveKey(photRO->analysis, "PSPHOT.DETECTIONS");
     89    }
     90    if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) {
     91        psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF");
     92    }
     93
     94    return fwhm;
     95}
     96
     97// Scale the kernel parameters according to the PSFs
     98static bool subScaleKernel(ppSubData *data, // Processing data
     99                           psVector *kernelWidths, // Widths for kernel
     100                           int *kernelSize,        // Size of kernel
     101                           int *stampSize          // Size of stamps (footprint)
     102    )
     103{
     104    psAssert(data, "Require processing data");
     105    pmConfig *config = data->config;    // Configuration
     106    psAssert(config, "Require configuration");
     107
     108    // Nothing to do if pre-calculated kernel exists
     109    pmFPAview *view = ppSubViewReadout(); // View to readout
     110    pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel
     111    if (kernelRO) {
     112        psFree(view);
     113        return true;
     114    }
     115
     116    // Look up recipe values
     117    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim
     118    psAssert(recipe, "We checked this earlier, so it should be here.");
     119    if (!psMetadataLookupBool(NULL, recipe, "SCALE")) {
     120        // No scaling requested
     121        psFree(view);
     122        return true;
     123    }
     124
     125    // Input images
     126    pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout
     127    pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout
     128
     129    // Input sources
     130    pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES");
     131    pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES");
     132    if (!inSourceRO || !refSourceRO) {
     133        psWarning("Unable to scale kernel, since no sources were provided.");
     134        return true;
     135    }
     136
     137    pmDetections *inDetections = psMetadataLookupPtr(NULL, inSourceRO->analysis, "PSPHOT.DETECTIONS"); // Input sources
     138    pmDetections *refDetections = psMetadataLookupPtr(NULL, refSourceRO->analysis, "PSPHOT.DETECTIONS"); // Ref sources
     139
     140    psFree(view);
     141
     142    if (!inDetections || !refDetections) {
     143        psWarning("Unable to scale kernel, since no sources were provided.");
     144        return true;
     145    }
     146
     147    psArray *inSources = inDetections->allSources;
     148    psAssert (inSources, "missing inSources?");
     149
     150    psArray *refSources = refDetections->allSources;
     151    psAssert (refSources, "missing refSources?");
     152
     153    float inFWHM = psMetadataLookupF32(NULL, inRO->parent->parent->concepts, "CHIP.SEEING"); // FWHM for input
     154    if (!isfinite(inFWHM) || inFWHM == 0.0) {
     155        inFWHM = subImagePSF(data, inRO, inSources);
     156    }
     157    float refFWHM = psMetadataLookupF32(NULL, refRO->parent->parent->concepts, "CHIP.SEEING"); // FWHM for ref
     158    if (!isfinite(refFWHM) || refFWHM == 0.0) {
     159        refFWHM = subImagePSF(data, refRO, refSources);
     160    }
     161    psLogMsg("ppSub", PS_LOG_INFO, "Input FWHM: %f\nReference FWHM: %f\n", inFWHM, refFWHM);
     162    if (!isfinite(inFWHM) || !isfinite(refFWHM)) {
     163        psWarning("Unable to scale kernel, since unable to measure PSFs.");
     164        return true;
     165    }
     166
     167    float scaleRef = psMetadataLookupF32(NULL, recipe, "SCALE.REF"); // Reference for scaling
     168    float scaleMin = psMetadataLookupF32(NULL, recipe, "SCALE.MIN"); // Minimum for scaling
     169    float scaleMax = psMetadataLookupF32(NULL, recipe, "SCALE.MAX"); // Maximum for scaling
     170    if (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax)) {
     171        psError(PPSUB_ERR_ARGUMENTS, false,
     172                "Scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
     173                scaleRef, scaleMin, scaleMax);
     174        return false;
     175    }
     176
     177    if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths, inFWHM, refFWHM,
     178                                  scaleRef, scaleMin, scaleMax)) {
     179        psError(PPSUB_ERR_DATA, false, "Unable to scale parameters.");
     180        return false;
     181    }
     182
     183    return true;
     184}
     185
    39186
    40187bool ppSubMatchPSFs(ppSubData *data)
     
    71218    pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel
    72219
    73     psFree(view);
    74 
    75220    // Sources in image, used for stamps: these must be loaded from previous analysis stages
    76221    pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES");
    77222    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) {
     223
     224    pmDetections *inDetections  = inSourceRO  ? psMetadataLookupPtr(&mdok, inSourceRO->analysis,  "PSPHOT.DETECTIONS") : NULL; // Input sources
     225    pmDetections *refDetections = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.DETECTIONS") : NULL; // Ref sources
     226
     227    psFree(view);
     228
     229    pmDetections *detections = NULL;    // Merged detection set
     230    if (inDetections && refDetections) {
     231        psArray *inSources  = inDetections->allSources;
     232        psArray *refSources = refDetections->allSources;
     233
     234        psAssert (inSources, "missing in sources?");
     235        psAssert (refSources, "missing ref sources?");
     236
     237        detections = pmDetectionsAlloc();
    85238        float radius = psMetadataLookupF32(NULL, recipe, "SOURCE.RADIUS"); // Matching radius
    86239        psArray *lists = psArrayAlloc(2); // Source lists
    87240        lists->data[0] = psMemIncrRefCounter(inSources);
    88241        lists->data[1] = psMemIncrRefCounter(refSources);
    89         sources = pmSourceMatchMerge(lists, radius);
     242        detections->allSources = pmSourceMatchMerge(lists, radius);
    90243        psFree(lists);
    91         if (!sources) {
    92             psError(PS_ERR_UNKNOWN, false, "Unable to merge source lists");
     244        if (!detections->allSources) {
     245            psError(PPSUB_ERR_DATA, false, "Unable to merge source lists");
     246            psFree(detections);
    93247            return false;
    94248        }
    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
     249    }
     250    if (!detections && inDetections) {
     251        detections = psMemIncrRefCounter(inDetections);
     252    }
     253    if (!detections && refDetections) {
     254        detections = psMemIncrRefCounter(refDetections);
     255    }
     256
     257    psMetadataAddPtr(inConv->analysis,  PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "Merged source list", detections);
     258    psMetadataAddPtr(refConv->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "Merged source list", detections);
     259    psFree(detections);                    // Drop reference
    106260
    107261    int footprint = psMetadataLookupS32(NULL, recipe, "STAMP.FOOTPRINT"); // Stamp half-size
     
    115269    pmSubtractionKernelsType type = pmSubtractionKernelsTypeFromString(typeStr); // Type of kernel
    116270    if (type == PM_SUBTRACTION_KERNEL_NONE) {
    117         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised kernel type: %s", typeStr);
     271        psError(PPSUB_ERR_ARGUMENTS, true, "Unrecognised kernel type: %s", typeStr);
    118272        return false;
    119273    }
     
    130284    int iter = psMetadataLookupS32(NULL, recipe, "ITER"); // Rejection iterations
    131285    float rej = psMetadataLookupF32(NULL, recipe, "REJ"); // Rejection threshold
    132     float sys = psMetadataLookupF32(NULL, recipe, "SYS"); // Relative systematic error in kernel
     286    float kernelErr = psMetadataLookupF32(NULL, recipe, "KERNEL.ERR"); // Relative systematic error in kernel
     287    float normFrac = psMetadataLookupF32(NULL, recipe, "NORM.FRAC"); // Fraction of window for normalisn windw
     288    float sysErr = psMetadataLookupF32(NULL, recipe, "SYS.ERR"); // Relative systematic error in images
     289    float skyErr = psMetadataLookupF32(NULL, recipe, "SKY.ERR"); // Additional error in sky
     290    float covarFrac = psMetadataLookupF32(NULL, recipe, "COVAR.FRAC"); // Fraction for covariance calculation
    133291
    134292    float badFrac = psMetadataLookupF32(NULL, recipe, "BADFRAC"); // Maximum bad fraction
     
    168326            break;
    169327          default:
    170             psErrorStackPrint(stderr, "Invalid value for -convolve");
     328            psError(PPSUB_ERR_ARGUMENTS, false, "Invalid value for -convolve");
    171329            return false;
    172330        }
    173331    }
    174332
     333    if (!subScaleKernel(data, widths, &size, &footprint)) {
     334        psError(PPSUB_ERR_DATA, false, "Unable to scale kernel parameters");
     335        return false;
     336    }
     337
    175338    int threads = psMetadataLookupS32(NULL, config->arguments, "-threads"); // Number of threads
    176339    if (threads > 0) {
    177         pmSubtractionThreadsInit(inRO, refRO);
    178     }
     340        pmSubtractionThreadsInit();
     341    }
     342
     343    if (inRO->covariance) {
     344        psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC);
     345    }
     346    if (refRO->covariance) {
     347        psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC);
     348    }
     349
     350    // Data doesn't exist until it's created in pmSubtraction...
     351    inConv->data_exists = inConv->parent->data_exists = inConv->parent->parent->data_exists = false;
     352    refConv->data_exists = refConv->parent->data_exists = refConv->parent->parent->data_exists = false;
    179353
    180354    // Match the PSFs
     
    182356    if (kernelRO) {
    183357        success = pmSubtractionMatchPrecalc(inConv, refConv, inRO, refRO, kernelRO->analysis,
    184                                             stride, sys, maskVal, maskBad, maskPoor, poorFrac, badFrac);
     358                                            stride, kernelErr, covarFrac, maskVal, maskBad, maskPoor,
     359                                            poorFrac, badFrac);
    185360    } else {
    186361        success = pmSubtractionMatch(inConv, refConv, inRO, refRO, footprint, stride, regionSize,
    187                                      spacing, threshold, sources, data->stamps, type, size, order,
    188                                      widths, orders, inner, ringsOrder, binning, penalty, optimum,
    189                                      optWidths, optOrder, optThresh, iter, rej, sys, maskVal,
    190                                      maskBad, maskPoor, poorFrac, badFrac, subMode);
    191     }
     362                                     spacing, threshold, detections ? detections->allSources : NULL,
     363                                     data->stamps, type, size, order, widths, orders, inner, ringsOrder,
     364                                     binning, penalty, optimum, optWidths, optOrder, optThresh, iter, rej,
     365                                     normFrac, sysErr, skyErr, kernelErr, covarFrac,
     366                                     maskVal, maskBad, maskPoor, poorFrac, badFrac, subMode);
     367    }
     368
     369# ifdef TESTING
     370    // XXX for testing
     371    psphotSaveImage (NULL, refRO->image,    "refRO.im.fits");
     372    psphotSaveImage (NULL, refRO->variance, "refRO.wt.fits");
     373    psphotSaveImage (NULL, refRO->mask,     "refRO.mk.fits");
     374
     375    psphotSaveImage (NULL, inRO->image,    "inRO.im.fits");
     376    psphotSaveImage (NULL, inRO->variance, "inRO.wt.fits");
     377    psphotSaveImage (NULL, inRO->mask,     "inRO.mk.fits");
     378
     379    psphotSaveImage (NULL, inConv->image,    "inConv.im.fits");
     380    psphotSaveImage (NULL, inConv->variance, "inConv.wt.fits");
     381    psphotSaveImage (NULL, inConv->mask,     "inConv.mk.fits");
     382
     383    psphotSaveImage (NULL, refConv->image,    "refConv.im.fits");
     384    psphotSaveImage (NULL, refConv->variance, "refConv.wt.fits");
     385    psphotSaveImage (NULL, refConv->mask,     "refConv.mk.fits");
     386# endif
    192387
    193388    psFree(optWidths);
    194     pmSubtractionThreadsFinalize(inRO, refRO);
     389    pmSubtractionThreadsFinalize();
    195390
    196391    if (!success) {
     
    207402            return true;
    208403        } else {
    209             psError(PS_ERR_UNKNOWN, false, "Unable to match images.");
     404            psError(PPSUB_ERR_DATA, false, "Unable to match images.");
    210405            return false;
    211406        }
     
    260455    pmConceptsCopyFPA(refConv->parent->parent->parent, refRO->parent->parent->parent, true, true);
    261456
    262     psImageCovarianceTransfer(inConv->variance, inConv->covariance);
    263     psImageCovarianceTransfer(refConv->variance, refConv->covariance);
     457    if (inConv->covariance) {
     458        psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC);
     459    }
     460    if (refConv->covariance) {
     461        psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC);
     462    }
     463
     464    if (inConv->variance) {
     465        psImageCovarianceTransfer(inConv->variance, inConv->covariance);
     466    }
     467    if (refConv->variance) {
     468        psImageCovarianceTransfer(refConv->variance, refConv->covariance);
     469    }
    264470
    265471    return true;
Note: See TracChangeset for help on using the changeset viewer.