IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16382


Ignore:
Timestamp:
Feb 8, 2008, 11:43:52 AM (18 years ago)
Author:
Paul Price
Message:

Code compiles and is ready for preliminary testing. Some features (e.g., rejection based on initial combination) are disabled for now, marked with XXX in the code.

Location:
branches/pap_branch_080207/ppStack/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/pap_branch_080207/ppStack/src/Makefile.am

    r14626 r16382  
    99        ppStackCamera.c         \
    1010        ppStackLoop.c           \
     11        ppStackPSF.c            \
    1112        ppStackReadout.c        \
     13        ppStackPhotometry.c     \
    1214        ppStackVersion.c        \
    1315        ppStackMatch.c
  • branches/pap_branch_080207/ppStack/src/ppStack.h

    r15844 r16382  
    1919    );
    2020
     21// Determine target PSF for input images
     22pmPSF *ppStackPSF(const pmConfig *config, // Configuration
     23                  int numCols, int numRows, // Size of image
     24                  const psList *list    // List of input PSFs
     25    );
     26
    2127// Perform stacking on a readout
    22 bool ppStackReadout(pmConfig *config,   // Configuration
    23                     const pmFPAview *view // View for readout
     28bool ppStackReadout(const pmConfig *config,   // Configuration
     29                    pmReadout *outRO,   // Output readout
     30                    const psArray *readouts // Input readouts
     31    );
     32
     33// Perform photometry on stack
     34bool ppStackPhotometry(pmConfig *config, // Configuration
     35                       const pmReadout *readout, // Readout to be photometered
     36                       const pmFPAview *view // View to readout
    2437    );
    2538
     
    3548
    3649/// Convolve image to match specified seeing
    37 bool ppStackMatch(pmReadout *output,    ///< Convolved readout
    38                   const pmReadout *input, ///< Readout to be convolved
     50bool ppStackMatch(pmReadout *readout, ///< Readout to be convolved; replaced with output
    3951                  const pmReadout *sourcesRO, ///< Readout with sources
    4052                  const pmPSF *psf,     ///< Target PSF
  • branches/pap_branch_080207/ppStack/src/ppStackLoop.c

    r16367 r16382  
    1313// Here follows lists of files for activation/deactivation at various stages.  Each must be NULL-terminated.
    1414
     15#if 0
    1516// All files in the system
    1617static char *allFiles[] = { "PPSTACK.INPUT", "PPSTACK.INPUT.MASK", "PPSTACK.INPUT.WEIGHT",
     
    2324                            "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID", "PSPHOT.INPUT.CMF",
    2425                            0 };
     26#endif
    2527
    2628// Files required in preparation for convolution
    2729static char *prepareFiles[] = { "PSPHOT.PSF.LOAD", "PPSTACK.SOURCES", 0 };
    2830
    29 // Files for convolution; these should be turned on one by one
    30 static char *convolutionFiles[] = { "PPSTACK.INPUT", "PPSTACK.INPUT.MASK", "PPSTACK.INPUT.WEIGHT",
    31                                     "PPSTACK.OUTCONV", "PPSTACK.OUTCONV.MASK", "PPSTACK.OUTCONV.WEIGHT",
    32                                     0 };
    33 
    3431// Input files for the combination
    35 static char *inCombineFiles = { "PPSTACK.INCONV", "PPSTACK.INCONV.MASK", "PPSTACK.INCONV.WEIGHT", 0 };
     32static char *inCombineFiles[] = { "PPSTACK.INCONV", "PPSTACK.INCONV.MASK", "PPSTACK.INCONV.WEIGHT", 0 };
    3633
    3734// Output files for the combination and photometry
    38 static char *outCombineFiles = { "PPSTACK.OUTPUT", "PPSTACK.OUTPUT.MASK", "PPSTACK.OUTPUT.WEIGHT",
    39                                  "PSPHOT.OUTPUT", "PSPHOT.RESID", "PSPHOT.BACKMDL",
    40                                  "PSPHOT.BACKMDL.STDEV", "PSPHOT.BACKGND", "PSPHOT.BACKSUB",
    41                                  "SOURCE.PLOT.MOMENTS", "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID",
    42                                  "PSPHOT.INPUT.CMF", 0 };
     35static char *outCombineFiles[] = { "PPSTACK.OUTPUT", "PPSTACK.OUTPUT.MASK", "PPSTACK.OUTPUT.WEIGHT",
     36                                   "PSPHOT.OUTPUT", "PSPHOT.RESID", "PSPHOT.BACKMDL",
     37                                   "PSPHOT.BACKMDL.STDEV", "PSPHOT.BACKGND", "PSPHOT.BACKSUB",
     38                                   "SOURCE.PLOT.MOMENTS", "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID",
     39                                   "PSPHOT.INPUT.CMF", 0 };
    4340
    4441
     
    103100
    104101
    105 
    106102// Iterate down the hierarchy, loading files; we can get away with this because we're working on skycells
    107103static pmFPAview *filesIterateDown(pmConfig *config // Configuration
     
    158154bool ppStackLoop(pmConfig *config)
    159155{
     156    assert(config);
     157
    160158    psMaskType maskBlank = psMetadataLookupU8(NULL, config->arguments, "MASK.BLANK"); // Mask for blank reg.
    161159
     
    182180        return false;
    183181    }
     182    int num = psMetadataLookupS32(NULL, config->arguments, "INPUTS.NUM"); // Number of inputs
     183    int numScans = psMetadataLookupS32(NULL, config->arguments, "ROWS"); // Number of scans to read at once
     184
     185
     186
     187    // XXX Will need to adjust this when we're doing the convolution
     188    int overlap = 0;                    // Overlap between consecutive scans
     189
     190
     191
    184192
    185193    // Preparation iteration: Load the sources, and get a target PSF model
    186     psArray *sources = NULL;            // Sources to use for PSF matching
     194    pmReadout *sources = NULL;          // Readout with sources to use for PSF matching
    187195    pmPSF *targetPSF = NULL;            // Target PSF
    188196    {
     
    194202        }
    195203
    196         pmReadout *sourcesRO = pmFPAfileThisReadout(config->files, view, "PPSTACK.SOURCES"); // Readout
    197204        // We want to hang on to the 'sources' even when its host FPA is blown away
    198         sources = psMemIncrRefCounter(psMetadataLookupPtr(&mdok, sourcesRO->analysis, "PSPHOT.SOURCES"));
     205        sources = psMemIncrRefCounter(pmFPAfileThisReadout(config->files, view, "PPSTACK.SOURCES"));
    199206        if (!sources) {
    200207            psError(PS_ERR_UNKNOWN, true, "Unable to find sources.");
     
    207214                                                               "^PPSTACK.INPUT$"); // Iterator
    208215        psMetadataItem *fileItem; // Item from iteration
    209         int fileNum = 0;        // Number of file
    210216        psList *psfList = psListAlloc(NULL); // List of PSFs for PSF envelope
     217        int numCols = 0, numRows = 0;   // Size of image
    211218        while ((fileItem = psMetadataGetAndIncrement(fileIter))) {
    212219            assert(fileItem->type == PS_DATA_UNKNOWN);
    213220            pmFPAfile *inputFile = fileItem->data.V; // An input file
    214221            pmChip *chip = pmFPAviewThisChip(view, inputFile->fpa); // The chip: holds the PSF
    215             pmPSF *psf = psMetadataLookupPtr(NULL, ro->parent->parent->analysis, "PSPHOT.PSF"); // PSF
     222            pmPSF *psf = psMetadataLookupPtr(NULL, chip->analysis, "PSPHOT.PSF"); // PSF
    216223            if (!psf) {
    217224                psError(PS_ERR_UNKNOWN, false, "Unable to find PSF.");
    218225                psFree(view);
    219226                psFree(sources);
     227                psFree(fileIter);
     228                psFree(psfList);
    220229                return false;
    221230            }
    222231            psListAdd(psfList, PS_LIST_TAIL, psf);
     232
     233            pmCell *cell = pmFPAviewThisCell(view, inputFile->fpa); // Cell of interest
     234            pmHDU *hdu = pmHDUFromCell(cell);
     235            assert(hdu && hdu->header);
     236            int naxis1 = psMetadataLookupS32(NULL, hdu->header, "NAXIS1"); // Number of columns
     237            int naxis2 = psMetadataLookupS32(NULL, hdu->header, "NAXIS2"); // Number of rows
     238            if (naxis1 <= 0 || naxis2 <= 0) {
     239                psError(PS_ERR_UNKNOWN, false, "Unable to determine size of image from PSF.");
     240                psFree(view);
     241                psFree(sources);
     242                psFree(fileIter);
     243                psFree(psfList);
     244                return false;
     245            }
     246            if (numCols == 0 && numRows == 0) {
     247                numCols = naxis1;
     248                numRows = naxis2;
     249            }
    223250        }
    224251        psFree(fileIter);
    225252        psFree(view);
    226253
    227         psArray *psfArray = psListToArray(psfList); // Array of PSFs
     254        targetPSF = ppStackPSF(config, numCols, numRows, psfList);
    228255        psFree(psfList);
    229         targetPSF = pmPSFEnvelope(numCols, numRows, psfArray, psfInstances, psfRadius, psfModel,
    230                                   psfOrder, psfOrder);
    231         psFree(psfArray);
    232256        if (!targetPSF) {
    233257            psError(PS_ERR_UNKNOWN, false, "Unable to determine output PSF.");
     
    241265
    242266    // Generate convolutions and write them to disk
    243     psArray *cells = psArrayAlloc(numFiles);    // Cells for convolved images --- a handle for reading again
    244     for (int i = 0; i < numFiles; i++) {
     267    psArray *cells = psArrayAlloc(num); // Cells for convolved images --- a handle for reading again
     268    for (int i = 0; i < num; i++) {
    245269        pmFPAfileActivate(config->files, false, NULL);
    246270        psArray *files = fileActivationSingle(config, prepareFiles, true, i);
    247271        pmFPAview *view = filesIterateDown(config);
    248272        if (!view) {
     273            psFree(sources);
     274            psFree(targetPSF);
    249275            return false;
    250276        }
     
    254280
    255281        // Background subtraction, scaling and normalisation is performed automatically by the image matching
    256         if (!ppStackMatch(ro, sources, outPSF, config)) {
     282        if (!ppStackMatch(readout, sources, targetPSF, config)) {
    257283            psError(PS_ERR_UNKNOWN, false, "Unable to match image %d --- ignoring.", i);
    258             psFree(convolved);
     284            psFree(sources);
     285            psFree(targetPSF);
    259286            return false;
    260287        }
     
    290317        filesIterateUp(config);
    291318    }
     319    psFree(sources);
     320    psFree(targetPSF);
    292321
    293322    // Set files to read at the readout level --- then when we iterate down, the pmFPAfileIO stuff should take
     
    295324    fileSetDataLevel(config, inCombineFiles, PM_FPA_LEVEL_CHUNK);
    296325    pmFPAfileActivate(config->files, false, NULL);
    297     fileActivation(inCombineFiles, true);
    298     fileActivation(outCombineFiles, true);
     326    fileActivation(config, inCombineFiles, true);
     327    fileActivation(config, outCombineFiles, true);
    299328
    300329    {
     
    304333            return false;
    305334        }
    306         pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSTACK.OUTPUT"); // Output readout
    307         psFree(view);
    308 
    309         psArray *readouts = psArrayAlloc(numFiles); // Readouts for convolved images
    310         for (int i = 0; i < numFiles; i++) {
     335        pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT"); // Output cell
     336        pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout
     337
     338        psArray *readouts = psArrayAlloc(num); // Readouts for convolved images
     339        for (int i = 0; i < num; i++) {
    311340            readouts->data[i] = pmReadoutAlloc(cells->data[i]); // Readout into which to read
    312341        }
     342        psFree(cells);
    313343
    314344        // Read convolutions by chunks
    315         int numRead = 0;                    // Number of inputs read
    316         int numChunk = 0;                   // Chunk number
    317345        bool more = true;               // More to read?
    318346        for (int numChunk = 0; more; numChunk++) {
    319             for (int i = 0; i < numFiles; i++) {
     347            for (int i = 0; i < num; i++) {
    320348                pmReadout *readout = readouts->data[i];
    321349                assert(readout);
     
    336364                    psError(PS_ERR_IO, false, "Unable to read chunk %d for file %d", numChunk, i);
    337365                    psFree(readouts);
    338                     psFree(cells);
     366                    psFree(outRO);
     367                    psFree(view);
     368                    return false;
    339369                }
    340370            }
    341371
     372#if 0
    342373            if (!ppStackReadout(config, outRO, readouts)) {
    343374                psError(PS_ERR_UNKNOWN, false, "Unable to stack images.\n");
     375                psFree(readouts);
     376                psFree(outRO);
     377                psFree(view);
    344378                return false;
    345379            }
    346 
    347             for (int i = 0; i < numFiles && more; i++) {
     380#else
     381            printf("Stack...\n");
     382#endif
     383
     384            for (int i = 0; i < num && more; i++) {
    348385                pmReadout *readout = readouts->data[i];
    349386                assert(readout);
     
    364401            }
    365402        }
     403        psFree(readouts);
    366404
    367405        // Get rid of the input files
    368         fileActivation(outCombineFiles, false);
     406        fileActivation(config, outCombineFiles, false);
    369407        filesIterateUp(config);
    370408
    371         ppStackPhotometry(config, outRO);
     409        if (psMetadataLookupBool(&mdok, config->arguments, "PHOTOMETRY") &&
     410            !ppStackPhotometry(config, outRO, view)) {
     411            psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry on output.");
     412            psFree(outRO);
     413            psFree(view);
     414            return false;
     415        }
     416
     417        // Statistics on output
     418        if (stats) {
     419            ppStatsFPA(stats, outCell->parent->parent, view, maskBlank, config);
     420        }
    372421
    373422        // Put version information into the header
    374423        pmHDU *hdu = pmHDUFromCell(outRO->parent);
    375424        if (!hdu) {
    376             psError(PS_ERR_UNKNOWN, true, "Unable to find HDU for output.");
     425            psError(PS_ERR_UNKNOWN, false, "Unable to find HDU for output.");
    377426            return false;
    378427        }
     
    382431        ppStackVersionMetadata(hdu->header);
    383432
    384         // Statistics on output
    385         if (stats) {
    386             ppStatsFPA(stats, output->fpa, view, maskBlank, config);
    387         }
    388 
    389433        // Write out the output files
    390         filesActivation(outCombineFiles, true);
     434        fileActivation(config, outCombineFiles, true);
    391435        filesIterateUp(config);
    392     }
    393 
    394     psFree(view);
     436
     437        psFree(outRO);
     438        psFree(view);
     439    }
     440
    395441
    396442    // Write out summary statistics
  • branches/pap_branch_080207/ppStack/src/ppStackPhotometry.c

    r16378 r16382  
    1010#include "ppStack.h"
    1111
    12 bool ppStackPhotometry(const pmConfig *config, const pmReadout *readout, const pmFPAview *view)
     12bool ppStackPhotometry(pmConfig *config, const pmReadout *readout, const pmFPAview *view)
    1313{
    1414    pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT");
    15     pmFPACopy(photFile->fpa, outRO->parent->parent->parent);
     15    pmFPACopy(photFile->fpa, readout->parent->parent->parent);
    1616
    1717    if (!psphotReadout(config, view)) {
  • branches/pap_branch_080207/ppStack/src/ppStackReadout.c

    r15849 r16382  
    1010#include "ppStack.h"
    1111
    12 #define ARRAY_BUFFER 16                 // Number to add to array at a time
    1312#define WCS_TOLERANCE 0.001             // Tolerance for WCS
    1413
    15 //#define NO_CONVOLUTION                  // Don't perform convolutions?
    16 //#define CONVOLUTION_FILES               // Write convolutions?
    1714//#define REJECTION_FILES                 // Write rejection mask?
    1815//#define INSPECTION_FILES                // Write inspection mask?
    1916
    20 bool ppStackReadout(pmConfig *config, const pmFPAview *view)
     17bool ppStackReadout(const pmConfig *config, pmReadout *outRO, const psArray *readouts)
    2118{
     19    assert(config);
     20    assert(outRO);
     21    assert(readouts);
     22
    2223    // Get the recipe values
    23     bool mdok;                          // Status of MD lookup
    2424    int iter = psMetadataLookupS32(NULL, config->arguments, "ITER"); // Rejection iterations
    2525    float combineRej = psMetadataLookupF32(NULL, config->arguments, "COMBINE.REJ"); // Combination threshold
    2626    psMaskType maskBad = psMetadataLookupU8(NULL, config->arguments, "MASK.BAD"); // Value to mask
    2727    psMaskType maskBlank = psMetadataLookupU8(NULL, config->arguments, "MASK.BLANK"); // Mask for blank reg.
    28     float threshold = psMetadataLookupF32(NULL, config->arguments, "THRESHOLD.MASK"); // Threshold for mask deconvolution
    29     bool photometry = psMetadataLookupBool(&mdok, config->arguments, "PHOTOMETRY"); // Perform photometry?
    30     int psfInstances = psMetadataLookupS32(&mdok, config->arguments, "PSF.INSTANCES"); // Number of instances for PSF
    31     float psfRadius = psMetadataLookupF32(NULL, config->arguments, "PSF.RADIUS"); // Radius for PSF
    32     const char *psfModel = psMetadataLookupStr(NULL, config->arguments, "PSF.MODEL"); // Model for PSF
    33     int psfOrder = psMetadataLookupS32(&mdok, config->arguments, "PSF.ORDER"); // Spatial order for PSF
    34 
    35     // Get the output target
    36     pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT"); // Output cell
    37     pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout
    38     pmFPA *outFPA = outCell->parent->parent; // Output FPA
    39 
    40     int num = psMetadataLookupS32(NULL, config->arguments, "INPUTS.NUM"); // Number of inputs
    41     assert(num > 0);
    42 
    43     // Get the input sources
    44     psArray *stack = psArrayAllocEmpty(ARRAY_BUFFER); // The stack of inputs
    45     pmReadout *sources = pmFPAfileThisReadout(config->files, view, "PPSTACK.SOURCES"); // Sources for stamps
    46     psMetadataIterator *fileIter = psMetadataIteratorAlloc(config->files, PS_LIST_HEAD,
    47                                                            "^PPSTACK.INPUT$"); // Iterator over input files
    48     psMetadataItem *fileItem;           // Item from iteration
    49     int fileNum = 0;                    // Number of file
    50     psList *psfList = psListAlloc(NULL); // List of PSFs for PSF envelope
    51     pmPSF *outPSF = NULL;               // Ouptut PSF
    52     int numCols = 0, numRows = 0;       // Size of image
    53     while ((fileItem = psMetadataGetAndIncrement(fileIter))) {
    54         assert(fileItem->type == PS_DATA_UNKNOWN);
    55         pmFPAfile *inputFile = fileItem->data.V; // An input file
    56         pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Corresponding readout
    57         pmCell *cell = ro->parent;      // The cell
    58         pmChip *chip = cell->parent;    // The chip: holds the PSF
    59 
    60         bool mdok;                      // Status of MD lookup
    61         pmPSF *psf = psMetadataLookupPtr(&mdok, chip->analysis, "PSPHOT.PSF");
    62         if (mdok && psf) {
    63             psListAdd(psfList, PS_LIST_TAIL, psf);
    64             if (numCols == 0 && numRows == 0) {
    65                 numCols = ro->image->numCols;
    66                 numRows = ro->image->numRows;
    67             }
    68         }
    69     }
    70 
    71     bool convolve = false;              // Convolve the input images?
    72     if (psfList->n > 0) {
    73         psArray *psfArray = psListToArray(psfList); // Array of PSFs
    74         outPSF = pmPSFEnvelope(numCols, numRows, psfArray, psfInstances, psfRadius, psfModel,
    75                                psfOrder, psfOrder);
    76         psFree(psfArray);
    77         if (!outPSF) {
    78             psError(PS_ERR_UNKNOWN, false, "Unable to determine output PSF.");
    79             // XXX Free stuff
    80             return false;
    81         }
    82         convolve = true;
    83         PS_ASSERT_PTR_NON_NULL(sources, false);
    84     }
    85 
    86     // Iterate through again to get the convolved images (or not)
    87 
    88     psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics
    89     psRandom *rng = psRandomAlloc(0, PS_RANDOM_TAUS); // Random number generator
     28//    float threshold = psMetadataLookupF32(NULL, config->arguments, "THRESHOLD.MASK"); // Threshold for mask deconvolution
     29
     30    int num = readouts->n;              // Number of inputs
     31    psArray *stack = psArrayAlloc(num); // Array for stacking
     32
     33    pmCell *outCell = outRO->parent;    // Output cell
     34    pmChip *outChip = outCell->parent;  // Output chip
     35    pmFPA *outFPA = outChip->parent;    // Output FPA
     36
    9037    float totExposure = 0.0;            // Total exposure time
    9138    psList *fpaList = psListAlloc(NULL); // List of input FPAs, for concept averaging
    9239    psList *cellList = psListAlloc(NULL); // List of input cells, for concept averaging
    93 
    94     psMetadataIteratorSet(fileIter, PS_LIST_HEAD);
    95     fileNum = 0;
    96     while ((fileItem = psMetadataGetAndIncrement(fileIter))) {
    97         assert(fileItem->type == PS_DATA_UNKNOWN);
    98         pmFPAfile *inputFile = fileItem->data.V; // An input file
    99         pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Corresponding readout
     40    for (int i = 0; i < num; i++) {
     41        pmReadout *ro = readouts->data[i];
     42        assert(ro);
     43        pmFPA *fpa = ro->parent->parent->parent; // Parent FPA
    10044
    10145        bool mdok;                      // Status of MD lookup
    102         float weighting = psMetadataLookupF32(&mdok, inputFile->fpa->analysis,
    103                                               "PPSTACK.WEIGHTING"); // Relative weighting
     46        float weighting = psMetadataLookupF32(&mdok, fpa->analysis, "PPSTACK.WEIGHTING"); // Relative weight
    10447        if (!mdok || !isfinite(weighting)) {
    105             psWarning("No WEIGHTING supplied for image %d --- set to unity.", fileNum);
     48            psWarning("No WEIGHTING supplied for image %d --- set to unity.", i);
    10649            weighting = 1.0;
    10750        }
    108         float scale = psMetadataLookupF32(&mdok, inputFile->fpa->analysis, "PPSTACK.SCALE"); // Rel. scale
     51
     52#if 0
     53        // I don't think 'scale' is needed, since the convolution matches the scales
     54        float scale = psMetadataLookupF32(&mdok, fpa->analysis, "PPSTACK.SCALE"); // Rel. scale
    10955        if (!mdok || !isfinite(scale)) {
    110             psWarning("No SCALE supplied for image %d --- set to unity.", fileNum);
     56            psWarning("No SCALE supplied for image %d --- set to unity.", i);
    11157            scale = 1.0;
    11258        }
     59#endif
    11360
    11461        float exposure = psMetadataLookupF32(NULL, ro->parent->concepts, "CELL.EXPOSURE"); // Exposure time
    115 #if 0
    11662        if (!isfinite(exposure)) {
    11763            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    11864                    "CELL.EXPOSURE is not set for input file %ld", stack->n);
    119             psFree(stats);
    120             psFree(rng);
    121             psFree(fileIter);
    12265            psFree(fpaList);
    12366            psFree(cellList);
    124             psFree(outRO);
    12567            psFree(stack);
    12668            return false;
    12769        }
    128 #endif
    12970        totExposure += exposure;        // Total exposure time
    13071
    131         // Generate convolved version of input
    132         pmReadout *convolved;
    133         if (convolve) {
    134             convolved = pmReadoutAlloc(NULL); // Convolved version of input readout
    135             // Background subtraction, scaling and normalisation is performed automatically by the image
    136             // matching
    137             if (!ppStackMatch(convolved, ro, sources, outPSF, config)) {
    138                 psWarning("Unable to match image %d --- ignoring.", fileNum);
    139                 psErrorClear();
    140                 psFree(convolved);
    141                 // XXX Free the bad image so it's not taking up good memory!
    142                 continue;
    143             }
    144 
    145 #ifdef CONVOLUTION_FILES
    146             if (convolved->image) {
    147                 psString name = NULL;           // Name of image
    148                 psStringAppend(&name, "convolved%03d_image.fits", fileNum);
    149                 psFits *fits = psFitsOpen(name, "w");
    150                 psFree(name);
    151                 psFitsWriteImage(fits, NULL, convolved->image, 0, NULL);
    152                 psFitsClose(fits);
    153             }
    154             if (convolved->mask) {
    155                 psString name = NULL;           // Name of image
    156                 psStringAppend(&name, "convolved%03d_mask.fits", fileNum);
    157                 psFits *fits = psFitsOpen(name, "w");
    158                 psFree(name);
    159                 psFitsWriteImage(fits, NULL, convolved->mask, 0, NULL);
    160                 psFitsClose(fits);
    161             }
    162             if (convolved->weight) {
    163                 psString name = NULL;           // Name of image
    164                 psStringAppend(&name, "convolved%03d_weight.fits", fileNum);
    165                 psFits *fits = psFitsOpen(name, "w");
    166                 psFree(name);
    167                 psFitsWriteImage(fits, NULL, convolved->weight, 0, NULL);
    168                 psFitsClose(fits);
    169             }
    170 #endif // CONVOLUTION_FILES
    171 
    172         } else {
    173             // No convolution --- just use the unconvolved images as the "convolved" images, with some tweaks
    174             convolved = psMemIncrRefCounter(ro);
    175             sources = NULL;
    176 
    177             // Brain-dead background subtraction
    178             if (!psImageBackground(stats, NULL, ro->image, ro->mask, maskBad, rng)) {
    179                 psError(PS_ERR_UNKNOWN, false, "Unable to get image background on image %ld", stack->n);
    180                 psFree(stats);
    181                 psFree(rng);
    182                 psFree(fileIter);
    183                 psFree(fpaList);
    184                 psFree(cellList);
    185                 psFree(stack);
    186                 psFree(outRO);
    187                 psFree(convolved);
    188                 return false;
    189             }
    190             psTrace("ppStack", 3, "Background for image %d is %f\n", fileNum, stats->robustMedian);
    191             (void)psBinaryOp(ro->image, ro->image, "-", psScalarAlloc(stats->robustMedian, PS_TYPE_F32));
    192 
    193             // Apply scaling
    194             (void)psBinaryOp(ro->image, ro->image, "*", psScalarAlloc(1.0 / scale, PS_TYPE_F32));
    195 
    196             // Normalise each input by the exposure time
    197             float exposure = psMetadataLookupF32(NULL, ro->parent->concepts, "CELL.EXPOSURE");// Exposure time
    198             if (!isfinite(exposure)) {
    199                 psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    200                         "CELL.EXPOSURE is not set for input file %ld", stack->n);
    201                 psFree(stats);
    202                 psFree(rng);
    203                 psFree(fileIter);
    204                 psFree(fpaList);
    205                 psFree(cellList);
    206                 psFree(outRO);
    207                 psFree(convolved);
    208                 psFree(stack);
    209                 return false;
    210             }
    211 
    212             (void)psBinaryOp(ro->image, ro->image, "/", psScalarAlloc(exposure, PS_TYPE_F32));
    213             if (ro->weight) {
    214                 (void)psBinaryOp(ro->weight, ro->weight, "/", psScalarAlloc(exposure, PS_TYPE_F32));
    215             }
    216         }
    217 
    218         if (fileNum == 0) {
     72        if (i == 0) {
    21973            // Copy astrometry over
    220             pmFPA *fpa = ro->parent->parent->parent; // Template FPA
    22174            pmHDU *hdu = fpa->hdu; // Template HDU
    22275            pmHDU *outHDU = outFPA->hdu; // Output HDU
     
    22477                psWarning("Unable to find HDU at FPA level to copy astrometry.");
    22578            } else {
    226                 if (!pmAstromReadWCS(outFPA, outCell->parent, hdu->header, 1.0)) {
     79                if (!pmAstromReadWCS(outFPA, outChip, hdu->header, 1.0)) {
    22780                    psErrorClear();
    22881                    psWarning("Unable to read WCS astrometry from input FPA.");
     
    23184                        outHDU->header = psMetadataAlloc();
    23285                    }
    233                     if (!pmAstromWriteWCS(outHDU->header, outFPA, outCell->parent, WCS_TOLERANCE)) {
     86                    if (!pmAstromWriteWCS(outHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
    23487                        psErrorClear();
    23588                        psWarning("Unable to write WCS astrometry to output FPA.");
     
    23992        }
    24093
    241         // Don't need the original any more!
    242         psFree(ro);
    243 
    24494        // Ensure there is a mask, or pmStackCombine will complain
    245         if (!convolved->mask) {
    246             convolved->mask = psImageAlloc(convolved->image->numCols, convolved->image->numRows,
    247                                            PS_TYPE_MASK);
    248             psImageInit(convolved->mask, 0);
    249         }
    250 
    251         psListAdd(fpaList, PS_LIST_TAIL, inputFile->fpa);
     95        if (!ro->mask) {
     96            ro->mask = psImageAlloc(ro->image->numCols, ro->image->numRows, PS_TYPE_MASK);
     97            psImageInit(ro->mask, 0);
     98        }
     99
     100        psListAdd(fpaList, PS_LIST_TAIL, fpa);
    252101        psListAdd(cellList, PS_LIST_TAIL, ro->parent);
    253102
    254         pmStackData *data = pmStackDataAlloc(convolved, weighting); // Data to stack
    255         psFree(convolved);
    256         psArrayAdd(stack, ARRAY_BUFFER, data);
    257         psFree(data);                   // Drop reference
    258 
    259         fileNum++;
    260     }
    261     psFree(fileIter);
    262     psFree(stats);
    263     psFree(rng);
    264 
    265     if (fileNum == 0) {
    266         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Not enough good files to combine.");
    267         psFree(fpaList);
    268         psFree(cellList);
    269         psFree(stack);
    270         psFree(outRO);
    271         return false;
     103        stack->data[i] = pmStackDataAlloc(ro, weighting);
    272104    }
    273105
     
    277109        psFree(cellList);
    278110        psFree(stack);
    279         psFree(outRO);
    280111        return false;
    281112    }
     
    304135#endif
    305136
    306     for (int i = 0; i < stack->n; i++) {
     137
     138
     139    // XXX THIS NEEDS WORK
     140#if 0
     141    for (int i = 0; i < num; i++) {
    307142        pmStackData *data = stack->data[i]; // Data for this image
    308143        pmReadout *readout = data->readout; // Readout for this image
     144
     145
     146        // XXX Need some other mechanism to get the subtraction kernel
     147
    309148
    310149        // Extract the regions and solutions used in the image matching
     
    344183        psFree(regions);
    345184    }
     185#endif
     186
    346187
    347188#ifdef REJECTION_FILES
     
    370211        psFree(cellList);
    371212        psFree(stack);
    372         psFree(outRO);
    373213        return false;
    374     }
    375 
    376     if (!convolve) {
    377         // Restore image to counts using the total exposure time
    378         (void)psBinaryOp(outRO->image, outRO->image, "*", psScalarAlloc(totExposure, PS_TYPE_F32));
    379         if (outRO->weight) {
    380             (void)psBinaryOp(outRO->weight, outRO->weight, "*", psScalarAlloc(totExposure, PS_TYPE_F32));
    381         }
    382214    }
    383215
     
    397229    psFree(cellList);
    398230
    399     if (photometry) {
    400         pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT");
    401         pmFPACopy(photFile->fpa, outRO->parent->parent->parent);
    402 
    403         if (!psphotReadout(config, view)) {
    404             psWarning("Unable to perform photometry on stacked image.");
    405             psErrorStackPrint(stderr, "Error stack from photometry:");
    406             psErrorClear();
    407         }
    408 
    409         pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");
    410     }
    411 
    412231    psFree(stack);
    413     psFree(outRO);                      // Drop reference
    414232
    415233    return true;
Note: See TracChangeset for help on using the changeset viewer.