IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 13970


Ignore:
Timestamp:
Jun 25, 2007, 2:27:15 PM (19 years ago)
Author:
eugene
Message:

adding fringe residual measurement in addition to fringe amplitude; clean up ppImageLoop calls to stats and fringe

Location:
trunk/ppImage/src
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppImage/src/Makefile.am

    r13528 r13970  
    2626        ppImageAstrom.c \
    2727        ppImageAddstar.c \
     28        ppImageStats.c \
    2829        ppImageDefineFile.c \
    2930        ppImageVersion.c
     
    4849        ppImageAstrom.c \
    4950        ppImageAddstar.c \
     51        ppImageStats.c \
    5052        ppImageDefineFile.c \
    5153        ppImageVersion.c
  • trunk/ppImage/src/ppImage.h

    r13901 r13970  
    6565bool ppImageDefineFile (pmConfig *config, pmFPA *input, char *filerule, char *argname, pmFPAfileType fileType, pmDetrendType detrendType);
    6666
     67// calculate stats, including MD5
     68bool ppImageStats (pmConfig *config, pmChip *chip, const pmFPAview *inputView, const ppImageOptions *options);
     69
     70// write stats to output file
     71bool ppImageStatsOutput (pmConfig *config, pmFPA *fpa, const ppImageOptions *options);
     72
    6773#endif
  • trunk/ppImage/src/ppImageDetrendFringe.c

    r13779 r13970  
    1010#include "ppImageDetrendFringe.h"
    1111
    12 bool ppImageDetrendFringeMeasure(pmReadout *readout, pmCell *fringe, const ppImageOptions *options)
     12bool ppImageDetrendFringeMeasure(pmReadout *readout, pmCell *fringe, const bool isResidual, const ppImageOptions *options)
    1313{
    1414    PS_ASSERT_PTR_NON_NULL(readout, false);
     
    4949    psBinaryOp(measurements->df, measurements->df, "*", psScalarAlloc(expTime, PS_TYPE_F32));
    5050
     51    char *scienceFringes = NULL;
     52    if (isResidual) {
     53        scienceFringes = psStringCopy ("FRINGE.RESIDUALS");
     54    } else {
     55        scienceFringes = psStringCopy ("FRINGE.MEASUREMENTS");
     56    }
     57
    5158    // Science fringe measurements
    52     pmFringeStats *previous = psMetadataLookupPtr(NULL, readout->parent->analysis, "FRINGE.MEASUREMENTS");
     59    pmFringeStats *previous = psMetadataLookupPtr(NULL, readout->parent->analysis, scienceFringes);
    5360    if (previous) {
    5461        // Multiple readouts: concatenate
     
    7380    }
    7481
    75     psMetadataAdd(readout->parent->analysis, PS_LIST_TAIL, "FRINGE.MEASUREMENTS",
     82    psMetadataAdd(readout->parent->analysis, PS_LIST_TAIL, scienceFringes,
    7683                  PS_DATA_UNKNOWN | PS_META_REPLACE, "Fringe measurements", measurements);
    7784    psFree(measurements);
    78 
     85    psFree(scienceFringes);
    7986    psFree(references);
    8087
     
    8491
    8592// Pull the fringes out of the cell analysis FRINGE.MEASUREMENTS for a chip
    86 static psArray *getFringes(const pmChip *chip // Chip of interest
    87     )
     93// XXX need some error checks
     94static psArray *getFringes(const pmChip *chip, const char *source)
    8895{
    8996    psArray *cells = chip->cells;       // Component cells
     
    9198    for (int i = 0; i < cells->n; i++) {
    9299        pmCell *cell = cells->data[i];  // Cell of interest
    93         fringes->data[i] = psMemIncrRefCounter(psMetadataLookupPtr(NULL, cell->analysis,
    94                                                                    "FRINGE.MEASUREMENTS"));
     100        fringes->data[i] = psMemIncrRefCounter(psMetadataLookupPtr(NULL, cell->analysis, source));
    95101    }
    96102
     
    101107// Solve the fringe system: we have science fringe measurements for each cell, and an array of reference
    102108// fringe measurements for each cell.  Need to concatenate these together first, and then solve.
    103 bool ppImageDetrendFringeSolve(pmChip *scienceChip, const pmChip *refChip, const ppImageOptions *options)
     109bool ppImageDetrendFringeSolve(pmChip *scienceChip, const pmChip *refChip, const bool isResidual, const ppImageOptions *options)
    104110{
    105111    PS_ASSERT_PTR_NON_NULL(scienceChip, NULL);
     
    107113    PS_ASSERT_PTR_NON_NULL(options, NULL);
    108114
    109     psArray *science = getFringes(scienceChip); // Fringe measurements on science chip
     115    psArray *science = NULL;
     116    if (isResidual) {
     117        science = getFringes(scienceChip, "FRINGE.RESIDUALS"); // Fringe residuals on science chip
     118    } else {
     119        science = getFringes(scienceChip, "FRINGE.MEASUREMENTS"); // Fringe measurements on science chip
     120    }
     121
    110122    pmFringeStats *scienceCat = pmFringeStatsConcatenate(science, NULL, NULL); // Science fringes
    111123    psFree(science);
     
    113125    // Need to transform the array of cells each with an array of fringes --> array of fringes for the chip as
    114126    // a whole
    115     psArray *references = getFringes(refChip); // Fringe measurements on reference chip
     127    psArray *references = getFringes(refChip, "FRINGE.MEASUREMENTS"); // Fringe measurements on reference chip
    116128    int numRefs = ((psArray*)references->data[0])->n; // Number of reference fringes
    117129    psArray *referencesCat = psArrayAlloc(numRefs); // Reference fringes
     
    132144                                                   options->fringeIter, options->fringeKeep);
    133145
    134     psMetadataAdd(scienceChip->analysis, PS_LIST_TAIL, "FRINGE.SOLUTION", PS_DATA_UNKNOWN,
    135                   "Fringe solution", solution);
     146    if (isResidual) {
     147        psMetadataAdd(scienceChip->analysis, PS_LIST_TAIL, "FRINGE.RESIDUAL.SOLUTION", PS_DATA_UNKNOWN, "Fringe solution", solution);
     148    } else {
     149        psMetadataAdd(scienceChip->analysis, PS_LIST_TAIL, "FRINGE.SOLUTION", PS_DATA_UNKNOWN, "Fringe solution", solution);
     150    }
     151
     152# if (0)
     153    // write the fringe amplitude or residual amplitude to the header
     154    // XXX this is measured per cell, but we only have headers per chip
     155    pmHDU *hdu = pmHDUFromCell(science);// HDU  of interest
     156    for (int i = 0; i < solution->nFringeFrames; i++) {
     157        // write metadata header value
     158        psString keyword = NULL;
     159        if (isResidual) {
     160            psStringAppend (&keyword, "FRES_%02dV", i);
     161        } else {
     162            psStringAppend (&keyword, "FRNG_%02dV", i);
     163        }
     164        psMetadataAddF32(hdu->header, PS_LIST_TAIL, keyword, PS_META_REPLACE, "Fringe Amplitude", solution->coeff->data.F32[i + 1]);
     165        psFree (keyword);
     166
     167        keyword = NULL;
     168        if (isResidual) {
     169            psStringAppend (&keyword, "FRES_%02dE", i);
     170        } else {
     171            psStringAppend (&keyword, "FRNG_%02dE", i);
     172        }
     173        psMetadataAddF32(hdu->header, PS_LIST_TAIL, keyword, PS_META_REPLACE, "Fringe Amplitude error", solution->coeffErr->data.F32[i + 1]);
     174        psFree (keyword);
     175    }
     176# endif
     177
    136178    psFree(solution);
    137179    psFree(scienceCat);
     
    142184
    143185
    144 bool ppImageDetrendFringeGenerate(pmCell *science, pmCell *fringes)
     186bool ppImageDetrendFringeGenerate(pmCell *science, pmCell *fringes, const ppImageOptions *options)
    145187{
    146188    PS_ASSERT_PTR_NON_NULL(science, false);
     
    191233        psFree(md5string);
    192234
     235# if (0)
    193236        // write metadata header value
     237        // XXX this is measured per cell, but we only have headers per chip
    194238        psString keyword = NULL;
    195         psStringAppend (&keyword, "FRNG_%02d", i);
     239        psStringAppend (&keyword, "FRNG_%02dV", i);
    196240        psMetadataAddF32(hdu->header, PS_LIST_TAIL, keyword, PS_META_REPLACE, "Fringe Amplitude", solution->coeff->data.F32[i + 1]);
    197241        psFree (keyword);
    198242
    199243        keyword = NULL;
    200         psStringAppend (&keyword, "FRNG_%02dD", i);
     244        psStringAppend (&keyword, "FRNG_%02dE", i);
    201245        psMetadataAddF32(hdu->header, PS_LIST_TAIL, keyword, PS_META_REPLACE, "Fringe Amplitude error", solution->coeffErr->data.F32[i + 1]);
    202246        psFree (keyword);
     247# endif
     248
    203249    }
    204250    if (expTime != 1.0) {
     
    219265            return false;
    220266        }
     267
     268        // measure residual fringe amplitude. results go to FRINGE.RESIDUALS
     269        ppImageDetrendFringeMeasure (readout, fringes, true, options);
    221270    }
    222271    psFree(sumFringe);
     
    233282}
    234283
     284bool ppImageDetrendFringeApply (pmConfig *config, pmChip *chip, const pmFPAview *inputView, const ppImageOptions *options) {
     285
     286    pmCell *cell = NULL;
     287
     288    assert (options->doFringe); // do not call if not needed
     289    assert (inputView->chip != -1);
     290    assert (inputView->cell == -1);
     291    assert (inputView->readout == -1);
     292
     293    pmFPAview *view = pmFPAviewAlloc(0); // View for local processing
     294    *view = *inputView;
     295
     296    // select the reference chip
     297    pmChip *fringe = pmFPAfileThisChip(config->files, view, "PPIMAGE.FRINGE");
     298    if (!fringe) {
     299        psError(PS_ERR_UNKNOWN, false, "missing fringe reference data.\n");
     300        psFree (view);
     301        return false;
     302    }
     303
     304    // Solve the fringe system
     305    if (!ppImageDetrendFringeSolve(chip, fringe, false, options)) {
     306        psError(PS_ERR_UNKNOWN, false, "failed to solve the fringe system.\n");
     307        psFree (view);
     308        return false;
     309    }
     310
     311    // Go back over the cells to apply the fringe correction
     312    view->cell = view->readout = -1;
     313    while ((cell = pmFPAviewNextCell(view, chip->parent, 1)) != NULL) {
     314        if (!cell->process || !cell->file_exists) {
     315            continue;
     316        }
     317
     318        // Apply the fringe correction
     319        psTrace("ppImage", 3, "Applying fringe correction...\n");
     320        pmCell *fringeCell = pmFPAfileThisCell(config->files, view, "PPIMAGE.FRINGE");
     321        if (!fringeCell) {
     322            psError(PS_ERR_UNKNOWN, false, "missing fringe reference data.\n");
     323            psFree (view);
     324            return false;
     325        }
     326
     327        if (!ppImageDetrendFringeGenerate(cell, fringeCell, options)) {
     328            psError(PS_ERR_UNKNOWN, false, "failed to apply fringe image.\n");
     329            psFree (view);
     330            return false;
     331        }
     332    }
     333
     334    // Solve the residual fringe system
     335    if (!ppImageDetrendFringeSolve(chip, fringe, true, options)) {
     336        psError(PS_ERR_UNKNOWN, false, "failed to solve the residual fringe system.\n");
     337        psFree (view);
     338        return false;
     339    }
     340
     341    psFree (view);
     342    return true;
     343}
  • trunk/ppImage/src/ppImageDetrendFringe.h

    r10335 r13970  
    99bool ppImageDetrendFringeMeasure(pmReadout *readout, // Readout to measure
    1010                                 pmCell *fringe, // Fringe cell (each readout is a different component)
     11                                 const bool isResidual,
    1112                                 const ppImageOptions *options // Options
    1213    );
     
    1516bool ppImageDetrendFringeSolve(pmChip *scienceChip, // Chip with science
    1617                               const pmChip *refChip, // Chip with reference fringes
     18                               const bool isResidual,
    1719                               const ppImageOptions *options // Options
    1820    );
     
    2022// Generate fringe frame
    2123bool ppImageDetrendFringeGenerate(pmCell *science, // Science cell
    22                                   pmCell *fringes // Fringe cell, one readout per fringe component
     24                                  pmCell *fringes, // Fringe cell, one readout per fringe component
     25                                  const ppImageOptions *options // Options
    2326    );
    2427
    2528
     29bool ppImageDetrendFringeApply (pmConfig *config, // config
     30                                pmChip *chip, // science chip
     31                                const pmFPAview *inputView, // current view
     32                                const ppImageOptions *options // options
     33);
     34
    2635#endif
  • trunk/ppImage/src/ppImageDetrendReadout.c

    r13812 r13970  
    8888    if (options->doFringe) {
    8989        pmCell *fringe = pmFPAfileThisCell(config->files, detview, "PPIMAGE.FRINGE");
    90         if (!ppImageDetrendFringeMeasure(input, fringe, options)) {
     90        if (!ppImageDetrendFringeMeasure(input, fringe, false, options)) {
    9191            return false;
    9292        }
  • trunk/ppImage/src/ppImageLoop.c

    r13924 r13970  
    33#endif
    44
    5 #include <ppStats.h>
    65#include "ppImage.h"
    76#include "ppImageDetrendFringe.h"
     
    109bool ppImageLoop (pmConfig *config, ppImageOptions *options) {
    1110
    12     bool mdok;                      // Status of MD lookup
    1311    bool status;
    1412    pmChip *chip;
    1513    pmCell *cell;
    1614    pmReadout *readout;
    17     psMetadata *stats = NULL;
    18 
    19     const char *statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); // Filename for statistics
    20     FILE *statsFile = NULL;             // File stream for statistics
    21     if (mdok && statsName && strlen(statsName) > 0) {
    22         psString resolved = pmConfigConvertFilename(statsName, config, true); // Resolved filename
    23         statsFile = fopen(resolved, "w");
    24         if (!statsFile) {
    25             psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
    26             psFree(resolved);
    27             return false;
    28         }
    29         stats = psMetadataAlloc();
    30         psFree(resolved);
    31     }
    3215
    3316    pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT");
     
    9679        }
    9780
    98         // Solve the fringe system
     81        // Apply the fringe correction
    9982        if (options->doFringe) {
    100             pmChip *fringe = pmFPAfileThisChip(config->files, view, "PPIMAGE.FRINGE");
    101             if (!ppImageDetrendFringeSolve(chip, fringe, options)) {
     83            if (!ppImageDetrendFringeApply (config, chip, view, options)) {
    10284                psFree (view);
    10385                return false;
    10486            }
    105         }
    106 
    107         // Go back over the cells to apply the fringe and do statistics
    108         view->cell = view->readout = -1;
    109         while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
    110             if (!cell->process || !cell->file_exists) {
    111                 continue;
    112             }
    113 
    114             // Apply the fringe correction
    115             if (options->doFringe) {
    116                 psTrace("ppImage", 3, "Applying fringe correction...\n");
    117                 pmCell *fringeCell = pmFPAfileThisCell(config->files, view, "PPIMAGE.FRINGE");
    118                 if (!ppImageDetrendFringeGenerate(cell, fringeCell)) {
    119                     psFree (view);
    120                     return false;
    121                 }
    122             }
    123 
    124             // Perform statistics on the detrended cell
    125             if (stats) {
    126                 bool mdok;              // Status of MD lookup
    127                 pmFPAfile *output = psMetadataLookupPtr(&mdok, config->files, "PPIMAGE.OUTPUT");
    128                 if (!mdok || !output) {
    129                     psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find file PPIMAGE.OUTPUT.");
    130                     psFree (view);
    131                     return false;
    132                 }
    133 
    134                 if (!ppStats(stats, output->fpa, view,
    135                              options->satMask | options->badMask | options->maskValue,
    136                              config)) {
    137                     psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to generate stats for image.");
    138                     psFree(stats);
    139                     psFree(view);
    140                     return false;
    141                 }
    142 
    143                 if (options->doFringe && !ppStatsFringe(stats, chip, "FRINGE.SOLUTION")) {
    144                     psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to extract fringe solution for image.");
    145                     psFree(stats);
    146                     psFree(view);
    147                     return false;
    148                 }
    149             }
    150 
    151             // Add MD5 information for cell
    152             pmHDU *hdu = pmHDUFromCell(cell); // HDU that owns the cell
    153             while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
    154                 const char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
    155                 const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME");
    156 
    157                 psString headerName = NULL; // Header name for MD5
    158                 psStringAppend(&headerName, "MD5_%s_%s_%d", chipName, cellName, view->readout);
    159 
    160                 psVector *md5 = psImageMD5(readout->image); // md5 hash
    161                 psString md5string = psMD5toString(md5); // String
    162                 psFree(md5);
    163                 psMetadataAddStr(hdu->header, PS_LIST_TAIL, headerName, PS_META_REPLACE,
    164                                  "Image MD5", md5string);
    165                 psFree(md5string);
    166                 psFree(headerName);
    167             }
    168         }
     87        }
     88       
     89        // measure various statistics for this image
     90        if (!ppImageStats (config, chip, view, options)) {
     91            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to measures stats for image");
     92            psFree (view);
     93            return false;
     94        }
    16995
    17096        if (!ppImageMosaicChip(config, options, view, "PPIMAGE.CHIP", "PPIMAGE.OUTPUT")) {
     
    214140
    215141    // Write out summary statistics
    216     if (stats) {
    217         char *statsMDC = psMetadataConfigFormat(stats);
    218         if (!statsMDC || strlen(statsMDC) == 0) {
    219             psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
    220         } else {
    221             fprintf(statsFile, "%s", statsMDC);
    222         }
    223         psFree(statsMDC);
    224         fclose(statsFile);
    225         psFree(stats);
     142    if (!ppImageStatsOutput (config, input->fpa, options)) {
     143        psError(PS_ERR_UNKNOWN, false, "Unable to write statistics file.\n");
     144        psFree (view);
     145        return false;
    226146    }
    227147
  • trunk/ppImage/src/ppImageOptions.c

    r13921 r13970  
    3939    options->doAstromChip = false;      // Astrometry
    4040    options->doAstromMosaic = false;    // Astrometry
     41
     42    options->doStats    = false;        // Measure and save image statistics
    4143
    4244    options->BaseFITS   = false;        // create binned image (scale 1)
     
    187189    options->doShutter = psMetadataLookupBool(NULL, recipe, "SHUTTER");
    188190
     191    options->doStats = false;
     192    char *statsName = psMetadataLookupStr(&status, config->arguments, "STATS"); // Filename for statistics
     193    if (statsName) {
     194        options->doStats = true;
     195    }
     196
    189197    // binned image options
    190198    options->xBin1 = psMetadataLookupS32(&status, recipe, "BIN1.XBIN");
  • trunk/ppImage/src/ppImageOptions.h

    r13901 r13970  
    2929    void *nonLinearSource;
    3030
     31    bool doStats;
     32
    3133    bool BaseFITS;
    3234    bool ChipFITS;
Note: See TracChangeset for help on using the changeset viewer.