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/ppSubMakePSF.c

    r26263 r26899  
    2222#include "ppSub.h"
    2323
     24psArray *ppSubSelectPSFSources(psArray *sources);
     25
    2426bool ppSubMakePSF(ppSubData *data)
    2527{
     
    6163        return false;
    6264    }
     65
    6366    pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer
    64     if (psMetadataLookup(photRO->analysis, "PSPHOT.SOURCES")) {
    65         psMetadataRemoveKey(photRO->analysis, "PSPHOT.SOURCES");
    66     }
    67 
    68     // psphotSaveImage (photFile->fpa->hdu->header, photRO->image, "findpsf.im.fits");
    69     // psphotSaveImage (photFile->fpa->hdu->header, photRO->variance, "findpsf.wt.fits");
    70     // psphotSaveImage (photFile->fpa->hdu->header, photRO->mask, "findpsf.mk.fits");
    71 
    72     // XXX for testing, can i dump a complete copy of the psphot sources here? 
    73     // I actually only need the X,Y coordinates to test this.
     67
     68    // we need to remove any existing PSPHOT.DETECTIONS (why not do this in psphot?)
     69    if (psMetadataLookup(photRO->analysis, "PSPHOT.DETECTIONS")) {
     70        psMetadataRemoveKey(photRO->analysis, "PSPHOT.DETECTIONS");
     71    }
     72    if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) {
     73        psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF");
     74    }
     75
     76# ifdef TESTING
     77    // XXX for testing, dump these images:
     78    psphotSaveImage (NULL, photRO->image, "findpsf.im.fits");
     79    psphotSaveImage (NULL, photRO->variance, "findpsf.wt.fits");
     80    psphotSaveImage (NULL, photRO->mask, "findpsf.mk.fits");
     81# endif
    7482
    7583    // Extract the loaded sources from the associated readout, and generate PSF
    7684    // Here, we assume the image is background-subtracted
    77     psArray *sources = psMetadataLookupPtr(&mdok, minuend->analysis, "PSPHOT.SOURCES");
    78     if (!psphotReadoutFindPSF(config, view, sources)) {
     85    pmDetections *detections = psMetadataLookupPtr(&mdok, minuend->analysis, "PSPHOT.DETECTIONS");
     86    psAssert(detections, "missing detections?");
     87    psArray *sources = detections->allSources;
     88    psAssert(sources, "missing sources?");
     89
     90    // XXX filter sources?  limit the total number and return only brighter objects?
     91    // use flags to toss totally bogus entries?
     92    psArray *goodSources = ppSubSelectPSFSources (sources);
     93    if (!psphotReadoutFindPSF(config, view, goodSources)) {
    7994        // This is likely a data quality issue
    8095        // XXX Split into multiple cases using error codes?
     
    8398        ppSubDataQuality(data, PSPHOT_ERR_PSF, PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
    8499        psFree(view);
     100        psFree(goodSources);
    85101        return true;
     102    }
     103
     104    // save the resulting PSF information on the pmFPAfile PSPHOT.PSF.LOAD
     105    pmFPAfile *psfFile = psMetadataLookupPtr(&mdok, config->files, "PSPHOT.PSF.LOAD"); // PSF file
     106    if (!ppSubCopyPSF (psfFile, photFile, view)) {
     107        psErrorStackPrint(stderr, "PSF was not generated");
     108        psWarning("PSF was not generated --- suspect bad data quality.");
     109        ppSubDataQuality(data, psErrorCodeLast(), PPSUB_FILES_PHOT_SUB | PPSUB_FILES_PHOT_INV);
    86110    }
    87111
     
    89113    psMetadataRemoveKey(photRO->analysis, "PSPHOT.HEADER");
    90114
    91     if (!data->quality) {
    92         data->psf = psMemIncrRefCounter(psMetadataLookupPtr(NULL, photRO->parent->parent->analysis,
    93                                                             "PSPHOT.PSF"));
    94     }
    95 
     115    psFree(goodSources);
    96116    psFree(view);
    97117
    98118    return true;
    99119}
     120
     121bool ppSubCopyPSF (pmFPAfile *output, pmFPAfile *input, pmFPAview *view) {
     122
     123    bool mdok = false;
     124
     125    pmChip *inputChip   = pmFPAviewThisChip(view, input->fpa); // Chip with PSF info
     126    pmChip *outputChip  = pmFPAviewThisChip(view, output->fpa); // Chip to store PSF info
     127
     128    pmReadout *inputRO  = pmFPAviewThisReadout(view, input->fpa); // Readout with PSF info
     129    pmReadout *outputRO = pmFPAviewThisReadout(view, output->fpa); // Readout to store PSF info
     130
     131    if (!outputRO) {
     132        pmCell *outputCell  = pmFPAviewThisCell(view, output->fpa);
     133        outputRO = pmReadoutAlloc(outputCell);
     134        outputRO->image = psMemIncrRefCounter (inputRO->image);
     135    }
     136
     137    // copy the PSF-related data to PSPHOT.PSF.LOAD for safe-keeping
     138    psMetadata *psfRegions = psMetadataAlloc();
     139
     140    int nRegions = psMetadataLookupS32 (&mdok, inputRO->analysis, "PSF.CLUMP.NREGIONS");
     141    if (!nRegions) {
     142        psErrorStackPrint(stderr, "No PSF available");
     143        return false;
     144    }
     145    psMetadataAddS32 (psfRegions, PS_LIST_TAIL, "PSF.CLUMP.NREGIONS",  PS_META_REPLACE, "psf clump regions", nRegions);
     146
     147    for (int i = 0; i < nRegions; i++) {
     148        char fieldName[80];
     149        snprintf (fieldName, 80, "PSF.CLUMP.REGION.%03d", i);
     150        psMetadata *regionMD = psMetadataLookupPtr (&mdok, inputRO->analysis, fieldName);
     151        if (!regionMD) {
     152            psWarning ("missing psf clump region metadata for entry %d", i);
     153            continue;
     154        }
     155        psMetadataAddMetadata (psfRegions, PS_LIST_TAIL, fieldName, PS_META_REPLACE, "psf clump region", regionMD);
     156    }
     157
     158    // XXX why am I replacing the entire analysis MD?
     159    psFree(outputRO->analysis);
     160    outputRO->analysis = psfRegions;
     161
     162    // copy the PSF model data
     163    pmPSF *psf = psMetadataLookupPtr(NULL, inputChip->analysis, "PSPHOT.PSF"); // PSF for photometry
     164    if (!psf) {
     165        psErrorStackPrint(stderr, "No PSF available");
     166        return false;
     167    }
     168
     169    psMetadataAddPtr(outputChip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN | PS_META_REPLACE, "PSF from ppSubMakePSF", psf);
     170
     171    return true;
     172}
     173
     174
     175// XXX hardwired MAX for now
     176# define MAX_NPSF 500
     177
     178psArray *ppSubSelectPSFSources(psArray *sources){
     179
     180    sources = psArraySort (sources, pmSourceSortBySN);
     181
     182    psArray *subset = psArrayAllocEmpty(MAX_NPSF);
     183
     184    int nPSF = 0;
     185    for (int i = 0; (nPSF < MAX_NPSF) && (i < sources->n); i++) {
     186
     187        pmSource *source = sources->data[i];
     188        if (!source) continue;
     189
     190        // skip non-astronomical objects (very likely defects)
     191        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     192        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     193        if (source->mode & PM_SOURCE_MODE_SATSTAR) continue;
     194
     195        psArrayAdd (subset, 100, source);
     196        nPSF++;
     197    }
     198
     199    return subset;
     200}
Note: See TracChangeset for help on using the changeset viewer.