IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 14, 2011, 1:05:28 PM (15 years ago)
Author:
watersc1
Message:

Merge of trunk back into branch.

Location:
branches/czw_branch/20101203
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20101203

  • branches/czw_branch/20101203/ppStack/src/ppStackLoop.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
     2
     3// static functions are defined below
     4static int stackSummary(const ppStackOptions *options, const char *place);
     5
     6bool ppStackLoop(pmConfig *config, ppStackOptions *options)
     7{
     8    assert(config);
     9
     10    psTimerStart("PPSTACK_TOTAL");
     11    psTimerStart("PPSTACK_STEPS");
     12
     13    // Setup
     14    psTrace("ppStack", 1, "Setup....\n");
     15    if (!ppStackSetup(options, config)) {
     16        psError(psErrorCodeLast(), false, "Unable to setup.");
     17        return false;
     18    }
     19    psLogMsg("ppStack", PS_LOG_INFO, "Stage 0: Setup: %f sec", psTimerClear("PPSTACK_STEPS"));
     20    ppStackMemDump("setup");
     21
     22    // Preparation for stacking
     23    psTrace("ppStack", 1, "Preparation for stacking: merging sources, determining target PSF....\n");
     24
     25    if (!ppStackPrepare(options, config)) {
     26        psError(psErrorCodeLast(), false, "Unable to prepare for stacking.");
     27        return false;
     28    }
     29    psLogMsg("ppStack", PS_LOG_INFO, "Stage 1: Load Sources and Generate Target PSF: %f sec", psTimerClear("PPSTACK_STEPS"));
     30    ppStackMemDump("prepare");
     31    if (options->quality) return true; // Can't do anything else
     32
     33    // Convolve inputs
     34    psTrace("ppStack", 1, "Convolving inputs to target PSF....\n");
     35    if (!ppStackConvolve(options, config)) {
     36        psError(psErrorCodeLast(), false, "Unable to convolve images.");
     37        return false;
     38    }
     39    psLogMsg("ppStack", PS_LOG_INFO, "Stage 2: Generate Convolutions and Save: %f sec", psTimerClear("PPSTACK_STEPS"));
     40    ppStackMemDump("convolve");
     41    if (options->quality) return true; // Can't do anything else
     42
     43    // Ensure sufficient inputs
     44    {
     45        int numGood = stackSummary(options, "initial combination");
     46        psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
     47        bool safe = psMetadataLookupBool(NULL, recipe, "SAFE"); // Be safe when combining
     48        if (safe && numGood <= 1) {
     49            options->quality = PPSTACK_ERR_REJECTED;
     50            psErrorStackPrint(stderr, "Insufficient inputs for combination with safety on");
     51            psErrorClear();
     52            psWarning("Insufficient inputs for combination with safety on");
     53            return true;
     54        }
     55    }
     56
     57    // Start threading
     58    ppStackThreadInit();
     59    ppStackThreadData *stack = ppStackThreadDataSetup(options, config, true);
     60    if (!stack) {
     61        psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
     62        return false;
     63    }
     64
     65    // Prepare for combination
     66    if (!ppStackCombinePrepare("PPSTACK.OUTPUT", "PPSTACK.OUTPUT.EXP", PPSTACK_FILES_STACK, stack, options, config)) {
     67        psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
     68        psFree(stack);
     69        return false;
     70    }
     71
     72    // Initial combination
     73    psTrace("ppStack", 1, "Initial stack of convolved images....\n");
     74    if (!ppStackCombineInitial(stack, options, config)) {
     75        psError(psErrorCodeLast(), false, "Unable to perform initial combination.");
     76        psFree(stack);
     77        return false;
     78    }
     79    ppStackMemDump("initial");
     80    psLogMsg("ppStack", PS_LOG_INFO, "Stage 3: Make Initial Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
     81
     82    // Done with stack inputs for now
     83    // XXX is this where we are leaking??
     84    for (int i = 0; i < options->num; i++) {
     85        pmCellFreeData(options->cells->data[i]);
     86    }
     87    psFree(stack);
     88
     89    // Pixel rejection
     90    psTrace("ppStack", 1, "Reject pixels....\n");
     91    if (!ppStackReject(options, config)) {
     92        psError(psErrorCodeLast(), false, "Unable to reject pixels.");
     93        psFree(stack);
     94        return false;
     95    }
     96    psLogMsg("ppStack", PS_LOG_INFO, "Stage 4: Pixel Rejection: %f sec", psTimerClear("PPSTACK_STEPS"));
     97
     98    // Check inputs
     99    {
     100        int numGood = stackSummary(options, "final combination");
     101        if (numGood <= 0) {
     102            options->quality = PPSTACK_ERR_REJECTED;
     103            psErrorStackPrint(stderr, "Insufficient inputs survived rejection stage");
     104            psErrorClear();
     105            psWarning("Insufficient inputs survived rejection stage");
     106            return true;
     107        }
     108    }
     109
     110    stack = ppStackThreadDataSetup(options, config, true);
     111    if (!stack) {
     112        psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
     113        return false;
     114    }
     115
     116    // Final combination
     117    psTrace("ppStack", 2, "Final stack of convolved images....\n");
     118    if (!ppStackCombineFinal(stack, options->convCovars, options, config, false, false, true)) {
     119        psError(psErrorCodeLast(), false, "Unable to perform final combination.");
     120        psFree(stack);
     121        return false;
     122    }
     123    psLogMsg("ppStack", PS_LOG_INFO, "Stage 5: Final Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
     124    ppStackMemDump("final");
     125
     126    // Photometry
     127    psTrace("ppStack", 1, "Photometering stacked image....\n");
     128    if (!ppStackPhotometry(options, config)) {
     129        psError(psErrorCodeLast(), false, "Unable to perform photometry.");
     130        return false;
     131    }
     132    psLogMsg("ppStack", PS_LOG_INFO, "Stage 6: Photometry Analysis: %f sec", psTimerClear("PPSTACK_STEPS"));
     133    ppStackMemDump("photometry");
     134
     135    // Update Header
     136    if (!ppStackUpdateHeader(stack, options, config)) {
     137        psError(psErrorCodeLast(), false, "Unable to update header.");
     138        psFree(stack);
     139        return false;
     140    }
     141    // Create JPEGS
     142    if (!ppStackJPEGs(stack, options, config)) {
     143        psError(psErrorCodeLast(), false, "Unable to make jpegs.");
     144        psFree(stack);
     145        return false;
     146    }
     147    // Assemble Stats
     148    if (!ppStackStats(stack, options, config)) {
     149        psError(psErrorCodeLast(), false, "Unable to assemble statistics.");
     150        psFree(stack);
     151        return false;
     152    }
     153
     154   // Clean up
     155    psTrace("ppStack", 2, "Cleaning up after combination....\n");
     156    if (!ppStackCleanupFiles(stack, options, config, PPSTACK_FILES_STACK, PPSTACK_FILES_PHOT, true)) {
     157        psError(psErrorCodeLast(), false, "Unable to clean up.");
     158        psFree(stack);
     159        return false;
     160    }
     161    psFree(stack);
     162    psLogMsg("ppStack", PS_LOG_INFO, "Stage 7: Cleanup, WCS & JPEGS: %f sec", psTimerClear("PPSTACK_STEPS"));
     163    ppStackMemDump("cleanup");
     164
     165    // Unconvolved stack --- it's cheap to calculate, compared to everything else!
     166    // XXX unconvolved stack is currently using the convolved mask!  oops!
     167    if (options->convolve) {
     168        // Start threading
     169        ppStackThreadData *stack = ppStackThreadDataSetup(options, config, false);
     170        if (!stack) {
     171            psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
     172            return false;
     173        }
     174
     175        // Prepare for combination
     176        if (!ppStackCombinePrepare("PPSTACK.UNCONV", "PPSTACK.UNCONV.EXP", PPSTACK_FILES_UNCONV,
     177                                   stack, options, config)) {
     178            psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
     179            psFree(stack);
     180            return false;
     181        }
     182
     183        // generate the unconvolved stack
     184        psTrace("ppStack", 2, "Stack of unconvolved images....\n");
     185        if (!ppStackCombineFinal(stack, options->origCovars, options, config,
     186                                 false, true, false)) {
     187            psError(psErrorCodeLast(), false, "Unable to perform unconvolved combination.");
     188            psFree(stack);
     189            return false;
     190        }
     191        psLogMsg("ppStack", PS_LOG_INFO, "Stage 8: Unconvolved Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
     192        ppStackMemDump("unconv");
     193
     194        // Clean up unconvolved stack
     195        psTrace("ppStack", 2, "Cleaning up after unconvolved stack....\n");
     196        if (!ppStackCleanupFiles(stack, options, config, PPSTACK_FILES_UNCONV, PPSTACK_FILES_NONE, false)) {
     197            psError(psErrorCodeLast(), false, "Unable to clean up.");
     198            psFree(stack);
     199            return false;
     200        }
     201        psFree(stack);
     202    }
     203    psFree(options->cells); options->cells = NULL;
     204
     205    // Finish up
     206    psTrace("ppStack", 1, "Finishing up....\n");
     207    if (!ppStackFinish(options, config)) {
     208        psError(psErrorCodeLast(), false, "Unable to finish up.");
     209        return false;
     210    }
     211    ppStackMemDump("finish");
     212
     213    return true;
     214}
    11215
    12216/// Print a summary of the inputs, and return the number of good inputs
    13 static int stackSummary(const ppStackOptions *options, // Stack options, with input mask
    14                         const char *place              // Place in code
    15     )
     217static int stackSummary(const ppStackOptions *options, const char *place)
    16218{
    17219    int numGood = 0;                // Number of good inputs
     
    47249    return numGood;
    48250}
    49 
    50 
    51 
    52 bool ppStackLoop(pmConfig *config, ppStackOptions *options)
    53 {
    54     assert(config);
    55 
    56     psTimerStart("PPSTACK_TOTAL");
    57     psTimerStart("PPSTACK_STEPS");
    58 
    59     // Setup
    60     psTrace("ppStack", 1, "Setup....\n");
    61     if (!ppStackSetup(options, config)) {
    62         psError(psErrorCodeLast(), false, "Unable to setup.");
    63         return false;
    64     }
    65     psLogMsg("ppStack", PS_LOG_INFO, "Stage 0: Setup: %f sec", psTimerClear("PPSTACK_STEPS"));
    66     ppStackMemDump("setup");
    67 
    68 
    69     // Preparation for stacking
    70     psTrace("ppStack", 1, "Preparation for stacking: merging sources, determining target PSF....\n");
    71     if (!ppStackPrepare(options, config)) {
    72         psError(psErrorCodeLast(), false, "Unable to prepare for stacking.");
    73         return false;
    74     }
    75     psLogMsg("ppStack", PS_LOG_INFO, "Stage 1: Load Sources and Generate Target PSF: %f sec",
    76              psTimerClear("PPSTACK_STEPS"));
    77     ppStackMemDump("prepare");
    78     if (options->quality) {
    79         // Can't do anything else
    80         return true;
    81     }
    82 
    83     // Convolve inputs
    84     psTrace("ppStack", 1, "Convolving inputs to target PSF....\n");
    85     if (!ppStackConvolve(options, config)) {
    86         psError(psErrorCodeLast(), false, "Unable to convolve images.");
    87         return false;
    88     }
    89     psLogMsg("ppStack", PS_LOG_INFO, "Stage 2: Generate Convolutions and Save: %f sec",
    90              psTimerClear("PPSTACK_STEPS"));
    91     ppStackMemDump("convolve");
    92 
    93     if (options->quality) {
    94         // Can't do anything else
    95         return true;
    96     }
    97 
    98     // Ensure sufficient inputs
    99     {
    100         int numGood = stackSummary(options, "initial combination");
    101         psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
    102         bool safe = psMetadataLookupBool(NULL, recipe, "SAFE"); // Be safe when combining
    103         if (safe && numGood <= 1) {
    104             options->quality = PPSTACK_ERR_REJECTED;
    105             psErrorStackPrint(stderr, "Insufficient inputs for combination with safety on");
    106             psErrorClear();
    107             psWarning("Insufficient inputs for combination with safety on");
    108             return true;
    109         }
    110     }
    111 
    112     // Start threading
    113     ppStackThreadInit();
    114     ppStackThreadData *stack = ppStackThreadDataSetup(options, config, true);
    115     if (!stack) {
    116         psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
    117         return false;
    118     }
    119 
    120     // Prepare for combination
    121     if (!ppStackCombinePrepare("PPSTACK.OUTPUT", "PPSTACK.OUTPUT.EXP", PPSTACK_FILES_STACK,
    122                                stack, options, config)) {
    123         psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
    124         psFree(stack);
    125         return false;
    126     }
    127 
    128     // Initial combination
    129     psTrace("ppStack", 1, "Initial stack of convolved images....\n");
    130     if (!ppStackCombineInitial(stack, options, config)) {
    131         psError(psErrorCodeLast(), false, "Unable to perform initial combination.");
    132         psFree(stack);
    133         return false;
    134     }
    135     ppStackMemDump("initial");
    136     psLogMsg("ppStack", PS_LOG_INFO, "Stage 3: Make Initial Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
    137 
    138     // Done with stack inputs for now
    139     for (int i = 0; i < options->num; i++) {
    140         pmCellFreeData(options->cells->data[i]);
    141     }
    142     psFree(stack);
    143 
    144     // Pixel rejection
    145     psTrace("ppStack", 1, "Reject pixels....\n");
    146     if (!ppStackReject(options, config)) {
    147         psError(psErrorCodeLast(), false, "Unable to reject pixels.");
    148         psFree(stack);
    149         return false;
    150     }
    151     psLogMsg("ppStack", PS_LOG_INFO, "Stage 4: Pixel Rejection: %f sec", psTimerClear("PPSTACK_STEPS"));
    152 
    153     // Check inputs
    154     {
    155         int numGood = stackSummary(options, "final combination");
    156         if (numGood <= 0) {
    157             options->quality = PPSTACK_ERR_REJECTED;
    158             psErrorStackPrint(stderr, "Insufficient inputs survived rejection stage");
    159             psErrorClear();
    160             psWarning("Insufficient inputs survived rejection stage");
    161             return true;
    162         }
    163     }
    164 
    165     stack = ppStackThreadDataSetup(options, config, true);
    166     if (!stack) {
    167         psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
    168         return false;
    169     }
    170 
    171     // Final combination
    172     psTrace("ppStack", 2, "Final stack of convolved images....\n");
    173     if (!ppStackCombineFinal(stack, options->convCovars, options, config, false, false, true)) {
    174         psError(psErrorCodeLast(), false, "Unable to perform final combination.");
    175         psFree(stack);
    176         return false;
    177     }
    178     psLogMsg("ppStack", PS_LOG_INFO, "Stage 5: Final Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
    179     ppStackMemDump("final");
    180 
    181     // Photometry
    182     psTrace("ppStack", 1, "Photometering stacked image....\n");
    183     if (!ppStackPhotometry(options, config)) {
    184         psError(psErrorCodeLast(), false, "Unable to perform photometry.");
    185         return false;
    186     }
    187     psLogMsg("ppStack", PS_LOG_INFO, "Stage 6: Photometry Analysis: %f sec", psTimerClear("PPSTACK_STEPS"));
    188     ppStackMemDump("photometry");
    189 
    190     // Clean up
    191     psTrace("ppStack", 2, "Cleaning up after combination....\n");
    192     if (!ppStackCleanup(stack, options, config)) {
    193         psError(psErrorCodeLast(), false, "Unable to clean up.");
    194         psFree(stack);
    195         return false;
    196     }
    197     for (int i = 0; i < options->num; i++) {
    198         pmCellFreeData(options->cells->data[i]);
    199     }
    200     psFree(stack);
    201     psLogMsg("ppStack", PS_LOG_INFO, "Stage 7: Cleanup, WCS & JPEGS: %f sec", psTimerClear("PPSTACK_STEPS"));
    202     ppStackMemDump("cleanup");
    203 
    204 #if 1
    205     // Unconvolved stack --- it's cheap to calculate, compared to everything else!
    206     // XXX unconvolved stack is currently using the convolved mask!  oops!
    207     if (options->convolve) {
    208         // Start threading
    209         ppStackThreadData *stack = ppStackThreadDataSetup(options, config, false);
    210         if (!stack) {
    211             psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
    212             return false;
    213         }
    214 
    215         // Prepare for combination
    216         if (!ppStackCombinePrepare("PPSTACK.UNCONV", "PPSTACK.UNCONV.EXP", PPSTACK_FILES_UNCONV,
    217                                    stack, options, config)) {
    218             psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
    219             psFree(stack);
    220             return false;
    221         }
    222 
    223         psTrace("ppStack", 2, "Stack of unconvolved images....\n");
    224         if (!ppStackCombineFinal(stack, options->origCovars, options, config,
    225                                  false, true, false)) {
    226             psError(psErrorCodeLast(), false, "Unable to perform unconvolved combination.");
    227             psFree(stack);
    228             return false;
    229         }
    230         psLogMsg("ppStack", PS_LOG_INFO, "Stage 8: Unconvolved Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
    231         ppStackMemDump("unconv");
    232 
    233         if (!ppStackFilesIterateUp(config)) {
    234             psError(psErrorCodeLast(), false, "Unable to close files.");
    235             psFree(stack);
    236             return false;
    237         }
    238         ppStackFileActivation(config, PPSTACK_FILES_UNCONV, false);
    239         options->outRO->data_exists = false;
    240         options->outRO->parent->data_exists = false;
    241         options->outRO->parent->parent->data_exists = false;
    242         psFree(options->outRO);
    243         options->outRO = NULL;
    244         options->expRO->data_exists = false;
    245         options->expRO->parent->data_exists = false;
    246         options->expRO->parent->parent->data_exists = false;
    247         psFree(options->expRO);
    248         options->expRO = NULL;
    249 
    250         for (int i = 0; i < options->num; i++) {
    251             pmCellFreeData(options->cells->data[i]);
    252         }
    253         psFree(stack);
    254     }
    255     psFree(options->cells); options->cells = NULL;
    256 #endif
    257 
    258     // Finish up
    259     psTrace("ppStack", 1, "Finishing up....\n");
    260     if (!ppStackFinish(options, config)) {
    261         psError(psErrorCodeLast(), false, "Unable to finish up.");
    262         return false;
    263     }
    264     ppStackMemDump("finish");
    265 
    266     return true;
    267 }
Note: See TracChangeset for help on using the changeset viewer.