IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 13657


Ignore:
Timestamp:
Jun 5, 2007, 3:06:50 PM (19 years ago)
Author:
Paul Price
Message:

The FITS handle may be NULL (e.g., when ppStats is being called from
another program, which takes care of the I/O itself). Only free data
when we've read it in ourselves. Cleaning up memory leaks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppStats/src/ppStatsLoop.c

    r13640 r13657  
    6969
    7070static psExit cellStats(psMetadata *chipResults, // Metadata holding the chip results
    71                         pmCell *cell,     // Cell for which to get statistics
    72                         psFits *fits,     // FITS file handle
    73                         ppStatsData *data,// The data
    74                         const pmConfig *config // Configuration
     71                        pmCell *cell,     // Cell for which to get statistics
     72                        psFits *fits,     // FITS file handle
     73                        ppStatsData *data,// The data
     74                        const pmConfig *config // Configuration
    7575    )
    7676{
    7777    assert(chipResults);
    7878    assert(cell);
    79     assert(fits);
    8079    assert(data);
    8180    assert(config);
     
    9695
    9796    if (psListLength(data->headers) > 0 && cell->hdu) {
    98         if (!pmCellReadHeader(cell, fits)) {
     97        if (fits && !pmCellReadHeader(cell, fits)) {
    9998            psError (PS_ERR_IO, false, "trouble reading cell header\n");
    100             return PS_EXIT_DATA_ERROR;
     99            psFree(cellResults);
     100            return PS_EXIT_DATA_ERROR;
    101101        }
    102102        pmHDU *hdu = cell->hdu;     // HDU for headers
     
    104104    }
    105105    if (psListLength(data->concepts) > 0) {
    106         if (!pmCellReadHeader(cell, fits)) {
     106        if (fits && !pmCellReadHeader(cell, fits)) {
    107107            psError (PS_ERR_IO, false, "trouble reading cell header\n");
    108             return PS_EXIT_DATA_ERROR;
     108            psFree(cellResults);
     109            return PS_EXIT_DATA_ERROR;
    109110        }
    110111        pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_ALL, false, config->database);
     
    123124    pmHDU *hdu = pmHDUFromCell(cell); // HDU for cell
    124125    if (!hdu || hdu->blankPHU) {
    125         psError (PS_ERR_UNKNOWN, false, "trouble finding HDU for cell\n");
    126         return PS_EXIT_CONFIG_ERROR;
    127     }
    128 
    129     if (!pmCellRead(cell, fits, config->database)) {
    130         psError (PS_ERR_IO, false, "trouble reading cell data\n");
    131         return PS_EXIT_DATA_ERROR;
     126        psError (PS_ERR_UNKNOWN, false, "trouble finding HDU for cell\n");
     127        psFree(cellResults);
     128        return PS_EXIT_CONFIG_ERROR;
     129    }
     130
     131    if (fits && !pmCellRead(cell, fits, config->database)) {
     132        psError (PS_ERR_IO, false, "trouble reading cell data\n");
     133        psFree(cellResults);
     134        return PS_EXIT_DATA_ERROR;
    132135    }
    133136
     
    147150    }
    148151
    149     // Measure basic image statistics (means, stdevs, etc). 
     152    // Measure basic image statistics (means, stdevs, etc).
    150153    if (data->sample <= 0.0) {
    151154        if (!psImageStats(data->stats, readout->image, readout->mask, data->maskVal)) {
     
    205208
    206209    // measure other types of statistics tests
    207    
     210
    208211statsDone:
    209212    // count saturated pixels
    210     if (psListLength(data->summary) > 0)     { 
    211         bool get_nSatPixels = false;
    212         bool get_fSatPixels = false;
     213    if (psListLength(data->summary) > 0)     {
     214        bool get_nSatPixels = false;
     215        bool get_fSatPixels = false;
    213216
    214217        psListIterator *iterator = psListIteratorAlloc(data->summary, PS_LIST_HEAD, false);
    215         psString choice; 
     218        psString choice;
    216219        while ((choice = psListGetAndIncrement(iterator))) {
    217             if (!strcasecmp (choice, "SAT_PIXEL_NUM"))  get_nSatPixels = true;
    218             if (!strcasecmp (choice, "SAT_PIXEL_FRAC")) get_fSatPixels = true;
    219         }
    220 
    221         if (!get_nSatPixels && !get_fSatPixels) {
    222             goto cellDone;
    223         }
    224 
    225         // Get the "concepts" of interest
    226         float saturation = psMetadataLookupF32(&mdok, cell->concepts, "CELL.SATURATION"); // Saturation level
    227         if (!mdok || isnan(saturation)) {
    228             psLogMsg(__func__, PS_LOG_WARN, "CELL.SATURATION is not set --- unable to measure N_SAT_PIXELS.\n");
    229             if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, 0);
    230             if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, NAN);
    231             goto cellDone;
    232         }
    233 
    234         int nSatPixels = 0;
    235         for (int j = 0; j < readout->image->numRows; j++) {
    236             for (int i = 0; i < readout->image->numCols; i++) {
    237                 if (readout->image->data.F32[j][i] >= saturation) {
    238                     nSatPixels ++;
    239                 }
    240             }
    241         }
    242         if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, nSatPixels);
    243         if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, nSatPixels / (double)(readout->image->numRows * readout->image->numCols));
     220            if (!strcasecmp (choice, "SAT_PIXEL_NUM"))  get_nSatPixels = true;
     221            if (!strcasecmp (choice, "SAT_PIXEL_FRAC")) get_fSatPixels = true;
     222        }
     223
     224        if (!get_nSatPixels && !get_fSatPixels) {
     225            goto cellDone;
     226        }
     227
     228        // Get the "concepts" of interest
     229        float saturation = psMetadataLookupF32(&mdok, cell->concepts, "CELL.SATURATION"); // Saturation level
     230        if (!mdok || isnan(saturation)) {
     231            psLogMsg(__func__, PS_LOG_WARN, "CELL.SATURATION is not set --- unable to measure N_SAT_PIXELS.\n");
     232            if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, 0);
     233            if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, NAN);
     234            goto cellDone;
     235        }
     236
     237        int nSatPixels = 0;
     238        for (int j = 0; j < readout->image->numRows; j++) {
     239            for (int i = 0; i < readout->image->numCols; i++) {
     240                if (readout->image->data.F32[j][i] >= saturation) {
     241                    nSatPixels ++;
     242                }
     243            }
     244        }
     245        if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, nSatPixels);
     246        if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, nSatPixels / (double)(readout->image->numRows * readout->image->numCols));
    244247    }
    245248
     
    247250    // Add the cell results to the chip
    248251    addToHierarchy(cellResults, chipResults, cellName, "Results for cell");
    249     pmCellFreeData(cell);
     252    if (fits) {
     253        pmCellFreeData(cell);
     254    }
    250255    return PS_EXIT_SUCCESS;
    251256}
    252257
    253258static psExit chipStats(psMetadata *fpaResults, // Metadata holding the fpa results
    254                         pmChip *chip,     // Chip for which to get statistics
    255                         psFits *fits,     // FITS file handle
    256                         pmFPAview *view,  // View for analysis
    257                         ppStatsData *data,// The data
    258                         const pmConfig *config // Configuration
     259                        pmChip *chip,     // Chip for which to get statistics
     260                        psFits *fits,     // FITS file handle
     261                        pmFPAview *view,  // View for analysis
     262                        ppStatsData *data,// The data
     263                        const pmConfig *config // Configuration
    259264    )
    260265{
    261266    assert(fpaResults);
    262267    assert(chip);
    263     assert(fits);
    264268    assert(view);
    265269    assert(data);
     
    279283        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Desired cell view (%d) doesn't match "
    280284                "number of cells (%ld)\n", view->cell, cells->n);
    281         return PS_EXIT_CONFIG_ERROR;
     285        return PS_EXIT_CONFIG_ERROR;
    282286    }
    283287
     
    290294
    291295    if (psListLength(data->headers) > 0 && chip->hdu) {
    292         if (!pmChipReadHeader(chip, fits)) {
     296        if (fits && !pmChipReadHeader(chip, fits)) {
    293297            psError (PS_ERR_IO, false, "trouble reading chip header\n");
    294             return PS_EXIT_DATA_ERROR;
     298            psFree(chipResults);
     299            return PS_EXIT_DATA_ERROR;
    295300        }
    296301        pmHDU *hdu = chip->hdu;     // HDU for headers
     
    298303    }
    299304    if (psListLength(data->concepts) > 0) {
    300         if (!pmChipReadHeader(chip, fits)) {
     305        if (fits && !pmChipReadHeader(chip, fits)) {
    301306            psError (PS_ERR_IO, false, "trouble reading chip header\n");
    302             return PS_EXIT_DATA_ERROR;
     307            psFree(chipResults);
     308            return PS_EXIT_DATA_ERROR;
    303309        }
    304310        pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_ALL, false, false, config->database);
     
    309315        pmCell *cell = cells->data[view->cell]; // Cell of interest
    310316        result = cellStats(chipResults, cell, fits, data, config);
    311         if (result != PS_EXIT_SUCCESS) {
    312             psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", view->cell);
    313             pmChipFreeData(chip);
    314             return result;
    315         }
    316         addToHierarchy(chipResults, fpaResults, chipName, "Results for chip");
    317         pmChipFreeData(chip);
    318         return PS_EXIT_SUCCESS;
     317        if (result != PS_EXIT_SUCCESS) {
     318            psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", view->cell);
     319            if (fits) {
     320                pmChipFreeData(chip);
     321            }
     322            psFree(chipResults);
     323            return result;
     324        }
     325        addToHierarchy(chipResults, fpaResults, chipName, "Results for chip");
     326        if (fits) {
     327            pmChipFreeData(chip);
     328        }
     329        psFree(chipResults);
     330        return PS_EXIT_SUCCESS;
    319331    }
    320332
     
    323335        pmCell *cell = cells->data[i];  // Cell of interest
    324336        result = cellStats(chipResults, cell, fits, data, config);
    325         if (result != PS_EXIT_SUCCESS) {
    326             psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", i);
    327             pmChipFreeData(chip);
    328             return result;
    329         }
     337        if (result != PS_EXIT_SUCCESS) {
     338            psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", i);
     339            if (fits) {
     340                pmChipFreeData(chip);
     341            }
     342            psFree(chipResults);
     343            return result;
     344        }
    330345    }
    331346
    332347    addToHierarchy(chipResults, fpaResults, chipName, "Results for chip");
    333     pmChipFreeData(chip);
     348    if (fits) {
     349        pmChipFreeData(chip);
     350    }
    334351    return PS_EXIT_SUCCESS;
    335352}
    336353
    337354psMetadata *ppStatsLoop(psExit *result,
    338                         psMetadata *fpaResults, // Metadata to hold the FPA results
     355                        psMetadata *fpaResults, // Metadata to hold the FPA results
    339356                        ppStatsData *data, // The data
    340357                        const pmConfig *config // Configuration
     
    345362    psFits *fits = data->fits;          // FITS file handle
    346363    PS_ASSERT_PTR_NON_NULL(fpa, NULL);
    347     PS_ASSERT_PTR_NON_NULL(fits, NULL);
    348364
    349365    *result = PS_EXIT_SUCCESS;
     
    353369
    354370    // allocate a new one if needed
    355     psMetadata *newResults = psMemIncrRefCounter(fpaResults);
     371    psMetadata *newResults = fpaResults;
    356372    if (!newResults) {
    357         newResults = psMetadataAlloc();
     373        newResults = psMetadataAlloc();
    358374    }
    359375
    360376    // Iterate through the FPA
    361377    if (psListLength(data->headers) > 0 && fpa->hdu) {
    362         pmFPAReadHeader(fpa, fits);
     378        if (fits && !pmFPAReadHeader(fpa, fits)) {
     379            psError(PS_ERR_IO, false, "Unable to read header for FPA.");
     380            psFree(view);
     381            psFree(newResults);
     382            return NULL;
     383        }
    363384        pmHDU *hdu = fpa->hdu;          // HDU for headers
    364385        getMetadata(newResults, hdu->header, data->headers);
    365386    }
    366387    if (psListLength(data->concepts) > 0) {
    367         pmFPAReadHeader(fpa, fits);
     388        if (fits && !pmFPAReadHeader(fpa, fits)) {
     389            psError(PS_ERR_IO, false, "Unable to read header for FPA.");
     390            psFree(view);
     391            psFree(newResults);
     392            return NULL;
     393        }
    368394        pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_ALL, false, config->database);
    369395        getMetadata(newResults, fpa->concepts, data->concepts);
     
    373399    if (view->chip >= 0) {
    374400        pmChip *chip = chips->data[view->chip]; // Chip of interest
    375         *result = chipStats(newResults, chip, fits, view, data, config);
     401        *result = chipStats(newResults, chip, fits, view, data, config);
    376402        if (*result != PS_EXIT_SUCCESS) {
    377             psError(PS_ERR_UNKNOWN, false, "trouble with stats for cell %d\n", view->cell);
    378             psFree (view);
    379             psFree (newResults);
    380             return NULL;
    381         }
    382         pmFPAFreeData(fpa);
    383         psFree(view);
    384         return newResults;
     403            psError(PS_ERR_UNKNOWN, false, "trouble with stats for cell %d\n", view->cell);
     404            psFree (view);
     405            psFree (newResults);
     406            return NULL;
     407        }
     408        if (fits) {
     409            pmFPAFreeData(fpa);
     410        }
     411        psFree(view);
     412        return newResults;
    385413    }
    386414
     
    389417        pmChip *chip = chips->data[i];  // Chip of interest
    390418        *result = chipStats(newResults, chip, fits, view, data, config);
    391         if (*result != PS_EXIT_SUCCESS) {
    392             psError(PS_ERR_UNKNOWN, false, "trouble with stats for chip %d\n", i);
    393             psFree (view);
    394             psFree (newResults);
    395             return NULL;
    396         }
    397     }
    398 
    399     pmFPAFreeData(fpa);
     419        if (*result != PS_EXIT_SUCCESS) {
     420            psError(PS_ERR_UNKNOWN, false, "trouble with stats for chip %d\n", i);
     421            psFree (view);
     422            psFree (newResults);
     423            return NULL;
     424        }
     425    }
     426
     427    if (fits) {
     428        pmFPAFreeData(fpa);
     429    }
    400430    psFree(view);
     431
    401432    return newResults;
    402433}
Note: See TracChangeset for help on using the changeset viewer.