IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2006, 5:57:35 PM (20 years ago)
Author:
Paul Price
Message:

Code coming together, doesn't yet compile

Location:
trunk/ppMerge/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppMerge/src

    • Property svn:ignore set to
      .deps
      Makefile
      Makefile.in
  • trunk/ppMerge/src/ppMergeBackground.c

    r7001 r7061  
    55
    66#include "ppMergeBackground.h"
     7
     8#define MAX_ITER 10                     // Maximum number of iterations for pmFlatNormalize
     9#define TOLERANCE 1e-3                  // Tolerance to reach for pmFlatNormalize
     10
    711
    812// Extract a particular statistic from the stats
     
    2933
    3034
    31 // Get the background for each chip of each input
    32 psImage *ppMergeBackground(const ppMergeOptions *options, // The options
    33                            const pmConfig *config // The configuration
     35// Get the scale and zero for each chip of each input
     36bool ppMergeScaleZero(ppImage **scales, // The scales for each integration/cell
     37                      ppImage **zeros, // The zeroes for each integration/cell
     38                      ppMergeData *data,// The data
     39                      const ppMergeOptions *options, // The options
     40                      const pmConfig *config // The configuration
    3441    )
    3542{
    3643    assert(config->camera);             // Need the camera configuration
    37     int numCells = 0;                   // Number of cells in the model FPA
    38     {
    39         // Count the cells
    40         pmFPA *fpa = pmFPAConstruct(config->camera); // The FPA
    41         psArray *chips = fpa->chips;        // Array of chips
    42         for (int i = 0; i < chips->n; i++) {
    43             pmChip *chip = chips->data[i];  // Chip of interest
    44             if (!chip) {
    45                 continue;
    46             }
    47             psArray *cells = chip->cells;   // Array of cells
    48             for (int j = 0; j < cells->n; j++) {
    49                 pmCell *cell = cells->data[j];
    50                 if (cell) {
    51                 numCells++;
    52                 }
    53             }
    54         }
    55         psFree(fpa);
    56     }
     44
    5745    psArray *filenames = psMetadataLookupPtr(NULL, config->arguments, "INPUT"); // The input file names
    5846    assert(filenames);                  // It should be here --- it's put here in ppMergeConfig
    5947
    60     psImage *background = psImageAlloc(filenames->n, numCells, PS_TYPE_F64); // Background measurements
     48    // Sanity checks
     49    assert(!options->scale || scales);
     50    assert(!scales || !*scales || ((*scales)->type.type == PS_TYPE_F64 &&
     51                                   (*scales)->numCols == data->numCells &&
     52                                   (*scales)->numRows == filenames->n));
     53    assert(!options->zero || zeros);
     54    assert(!zeros || !*zeros || ((*zeros)->type.type == PS_TYPE_F64 &&
     55                                 (*zeros)->numCols == data->numCells &&
     56                                 (*zeros)->numRows == filenames->n));
     57
     58    psImage *background = psImageAlloc(filenames->n, data->numCells, PS_TYPE_F64); // Background measurements
    6159    psImageInit(background, NAN);
    6260    psStats *bgStats = psStatsAlloc(options->background); // Statistic to measure the background
    63 
     61    psVector *gains = NULL;             // The gains for each cell
     62    psImage *exptime = NULL;            // The exposure time for each integration of each cell
     63    if (options->scale) {
     64        gains = psVectorAlloc(data->numCells, PS_TYPE_F64);
     65    }
     66    if (options->exptime) {
     67        exptime = psImageAlloc(filenames->n, data->numCells, PS_TYPE_F64);
     68    }
     69
     70    bool status = true;                 // Status of getting the scale and zero --- did everything go right?
    6471    for (int i = 0; i < filenames->n; i++) {
    6572        psString name = filenames->data[i]; // The name of the file
     
    7077        if (!inFile) {
    7178            psErrorPrint(PS_ERR_IO, false, "Unable to open input file %s --- ignored.\n", name);
     79            status = false;
    7280            continue;
    7381        }
    74         psMetadata *header = psFitsReadHeader(NULL, inFile); // The FITS (primary) header
    75         psMetadata *format = pmConfigCameraFormatFromHeader(config, header); // The camera format
    76         psFree(header);
    77         if (!format) {
    78             psErrorPrint(PS_ERR_IO, false, "Unable to identify camera format for input file %s --- "
    79                          "ignored.\n", name);
    80             continue;
    81         }
    82 
    83         pmFPA *fpa = pmFPAConstruct(config->camera); // The FPA
    84         pmFPAview *view = pmFPAAddSourceFromHeader(fpa, phu, format); // View of the PHU
    85         int cellNum = 0;                // Number of the cell
     82
     83        pmFPA *fpa = in->data[i];       // The FPA for this input
     84        int cellNum = -1;               // Number of the cell
    8685        psArray *chips = fpa->chips;    // Array of chips
    87         for (int i = 0; i < chips->n; i++) {
    88             pmChip *chip = chips->data[i]; // The chip of interest
     86        for (int j = 0; j < chips->n; j++) {
     87            pmChip *chip = chips->data[j]; // The chip of interest
    8988            if (!chip) {
    9089                continue;
    9190            }
    9291            psArray *cells = chip->cells; // Array of cells
    93             for (int j = 0; j < cells->n; j++) {
    94                 pmCell *cell = cells->data[j]; // The cell of interest
     92            for (int k = 0; k < cells->n; k++) {
     93                pmCell *cell = cells->data[k]; // The cell of interest
    9594                if (!cell) {
    9695                    continue;
    9796                }
    98 
    99                 if (!pmCellRead(cell, inFile, config->database)) {
    100                     // Nothing here
    101                     psFree(cell);
    102                     cellNum++;
    103                     continue;
    104                 }
    105 
    106                 if (cell->readouts->n > 1) {
    107                     psLogMsg(__func__, PS_LOG_WARN, "File %s chip %d cell %d contains more than one readout "
    108                              "--- ignoring all but the first.\n", name, i, j);
    109                 }
    110 
    111                 pmReadout *readout = cell->readouts->data[0]; // The readout of interest
    112                 psImage *image = readout->image; // The pixels of interest
    113                 if (!image) {
    114                     psFree(cell);
    115                     cellNum++;
    116                     continue;
    117                 }
    118 
    119                 psImageStats(bgStats, image, readout->mask, options->maskVal);
    120                 background[i][cellNum++] = getStat(bgStats, options->background);
    121                 psFree(cell);
     97                cellNum++;
     98
     99                if (!pmCellReadHeader(cell, inFile)) {
     100                    psLogMsg(__func__, PS_LOG_WARN, "Unable to read header for file %s chip %d cell %d --- "
     101                             "ignored.\n", name, j, k);
     102                    status = false;
     103                }
     104
     105                // Normalising by the exposure time
     106                if (options->exptime) {
     107                    bool mdok = true;   // Status of MD lookup
     108                    exptime->data.F64[i][cellNum] = psMetadataLookup(&mdok, cell->concepts, "CELL.EXPOSURE");
     109                    if (!mdok || isnan(exptime->data.F64[i][cellNum])) {
     110                        psLogMsg(__func__, PS_LOG_WARN, "CELL.EXPOSURE for file %s chip %d cell %d is not "
     111                                 "set.\n", name, j, k);
     112                        exptime->data.F64[i][cellNum] = NAN;
     113                        status = false;
     114                    }
     115                }
     116
     117                // Scaling by the background
     118                if (options->scale || options->zero) {
     119                    if (!pmCellRead(cell, inFile, config->database)) {
     120                        // Nothing here
     121                        psFree(cell);
     122                        continue;
     123                    }
     124
     125                    if (cell->readouts->n > 1) {
     126                        psLogMsg(__func__, PS_LOG_WARN, "File %s chip %d cell %d contains more than one "
     127                                 "readout --- ignoring all but the first.\n", name, j, k);
     128                        status = false;
     129                    }
     130
     131                    pmReadout *readout = cell->readouts->data[0]; // The readout of interest
     132                    psImage *image = readout->image; // The pixels of interest
     133                    if (!image) {
     134                        psFree(cell);
     135                        continue;
     136                    }
     137
     138                    // Get the gain
     139                    if (options->scale) {
     140                        bool mdok = true;   // Status of MD lookup
     141                        gain->data.F32[cellNum] = psMetadataLookupF32(&mdok, cell->concepts, "CELL.GAIN");
     142                        if (!mdok || isnan(gain->data.F32[cellNum])) {
     143                            psLogMsg(__func__, PS_LOG_WARN, "CELL.GAIN for file %s chip %d cell %d is not "
     144                                     "set.\n", name, j, k);
     145                            gain->data.F32[cellNum] = NAN;
     146                            status = false;
     147                        }
     148                    }
     149
     150                    // Get the background
     151                    psImageStats(bgStats, image, readout->mask, options->maskVal);
     152                    background[i][cellNum] = getStat(bgStats, options->background);
     153                }
     154
     155                psCellFreeData(cell);
    122156            }
    123             psFree(chip);
    124         }
    125         psFree(fpa);
    126         psFree(view);
     157            psChipFreeData(chip);
     158        }
     159        psFPAFreeData(fpa);
    127160        psFitsClose(inFile);
    128161    }
    129162    psFree(bgStats);
    130163
    131     return background;
     164    if (options->scale) {
     165        // Need to normalize over the focal plane
     166        bool converge = true;           // Did we converge?
     167        psVector *fluxes = pmFlatNormalize(&converge, gains, background, MAX_ITER, TOLERANCE);
     168        if (!converge || !fluxes) {
     169            psLogMsg(__func__, PS_LOG_WARN, "Normalisation failed to converge --- continuing anyway.\n");
     170            status = false;
     171        }
     172        psFree(gains);
     173
     174        *scales = psImageCopy(*scales, background, PS_TYPE_F64);
     175        psImage *scalesDeref = *scales; // Dereference the pointer
     176
     177        for (int i = 0; i < scalesDeref->numRows; i++) {
     178            psF64 bg = fluxes->data.F64[i];
     179            for (int j = 0; j < scalesDeref->numCols; j++) {
     180                scalesDeref->data.F64[i][j] = bg;
     181            }
     182        }
     183    }
     184
     185    if (options->exptime) {
     186        if (!options->scale) {
     187            // Copy over the exposure times
     188            psImageCopy(*scales, exptime, PS_TYPE_F64);
     189        } else {
     190            psBinaryOp(*scales, *scales, "*", exptime);
     191        }
     192    }
     193
     194    if (options->zero) {
     195        if (!*zeros) {
     196            // This is much faster than copying!
     197            *zeros = psMemIncrRefCounter(background);
     198        } else {
     199            *zeros = psImageCopy(*zeros, background, PS_TYPE_F64);
     200        }
     201    }
     202
     203    psFree(background);
     204
     205    return status;
    132206}
     207
Note: See TracChangeset for help on using the changeset viewer.