IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6124


Ignore:
Timestamp:
Jan 21, 2006, 12:23:58 AM (21 years ago)
Author:
Paul Price
Message:

Working, but leaks memory something chronic

Location:
branches/eam_rel9_p0/psModules/src/astrom
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c

    r6113 r6124  
    131131}
    132132
     133
     134// Set the concepts in the new cell, based on the values in the old one
     135static bool cellConcepts(pmCell *target,// Target cell
     136                         psArray *sources, // Source cells
     137                         int xBin, int yBin // Binning
     138                        )
     139{
     140    bool success = true;                // Result of setting everything
     141    float gain       = 0.0;             // Gain
     142    float readnoise  = 0.0;             // Read noise
     143    float saturation = 0.0;             // Saturation level
     144    float bad        = 0.0;             // Bad level
     145    float exposure   = 0.0;             // Exposure time
     146    float darktime   = 0.0;             // Dark time
     147    double time      = 0.0;             // Time of observation
     148    psTimeType timeSys = 0;             // Time system
     149
     150    int nCells = 0;                     // Number of cells;
     151    for (int i = 0; i < sources->n; i++) {
     152        pmCell *cell = sources->data[i];// The cell of interest
     153        if (!cell) {
     154            continue;
     155        }
     156        nCells++;
     157        gain       += psMetadataLookupF32(NULL, cell->concepts, "CELL.GAIN");
     158        readnoise  += psMetadataLookupF32(NULL, cell->concepts, "CELL.READNOISE");
     159        saturation += psMetadataLookupF32(NULL, cell->concepts, "CELL.SATURATION");
     160        bad        += psMetadataLookupF32(NULL, cell->concepts, "CELL.BAD");
     161        exposure   += psMetadataLookupF32(NULL, cell->concepts, "CELL.EXPOSURE");
     162        darktime   += psMetadataLookupF32(NULL, cell->concepts, "CELL.DARKTIME");
     163        time       += psTimeToMJD(psMetadataLookupPtr(NULL, cell->concepts, "CELL.TIME"));
     164        if (i == 0) {
     165            timeSys = psMetadataLookupS32(NULL, cell->concepts, "CELL.TIMESYS");
     166        } else if (timeSys != psMetadataLookupS32(NULL, cell->concepts, "CELL.TIMESYS")) {
     167            psLogMsg(__func__, PS_LOG_ERROR, "Differing time systems in use: %d vs %d\n", timeSys,
     168                     psMetadataLookupS32(NULL, cell->concepts, "CELL.TIMESYS"));
     169            success = false;
     170        }
     171    }
     172    gain       /= (float)nCells;
     173    readnoise  /= (float)nCells;
     174    saturation /= (float)nCells;
     175    bad        /= (float)nCells;
     176    exposure   /= (float)nCells;
     177    darktime   /= (float)nCells;
     178    psTime *timePtr = psTimeFromMJD(time/(double)nCells);
     179    timePtr = psTimeConvert(timePtr, timeSys);
     180
     181    // XXX *REALLY* need a generic "concept update" function that handles the type and comments transparently.
     182    psMetadataAddF32(target->concepts, PS_LIST_TAIL, "CELL.GAIN", PS_META_REPLACE, "Gain (e/ADU)", gain);
     183    psMetadataAddF32(target->concepts, PS_LIST_TAIL, "CELL.READNOISE", PS_META_REPLACE, "Read noise (e)", readnoise);
     184    psMetadataAddF32(target->concepts, PS_LIST_TAIL, "CELL.SATURATION", PS_META_REPLACE, "Saturation level (ADU)", saturation);
     185    psMetadataAddF32(target->concepts, PS_LIST_TAIL, "CELL.BAD", PS_META_REPLACE, "Bad level (ADU)", bad);
     186    psMetadataAddF32(target->concepts, PS_LIST_TAIL, "CELL.EXPOSURE", PS_META_REPLACE, "Exposure time (sec)", exposure);
     187    psMetadataAddF32(target->concepts, PS_LIST_TAIL, "CELL.DARKTIME", PS_META_REPLACE, "Time since last CCD flush (sec)", darktime);
     188    psMetadataAddPtr(target->concepts, PS_LIST_TAIL, "CELL.TIME", PS_DATA_TIME | PS_META_REPLACE, "Time of observation", timePtr);
     189    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.TIMESYS", PS_META_REPLACE, "Time system", timeSys);
     190    psFree(timePtr);
     191
     192    // Now fill in the ones I know by other means
     193    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.X0", PS_META_REPLACE, "Position of (0,0) on the chip", 0);
     194    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.Y0", PS_META_REPLACE, "Position of (0,0) on the chip", 0);
     195    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.XPARITY", PS_META_REPLACE, "Orientation in x compared to the rest of the FPA", 1);
     196    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.YPARITY", PS_META_REPLACE, "Orientation in x compared to the rest of the FPA", 1);
     197    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.XBIN", PS_META_REPLACE, "Binning in x", xBin);
     198    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.YBIN", PS_META_REPLACE, "Binning in x", yBin);
     199    psMetadataAddS32(target->concepts, PS_LIST_TAIL, "CELL.READDIR", PS_META_REPLACE, "Read direction (faked)", 1);
     200    psRegion *trimsec = psMetadataLookupPtr(NULL, target->concepts, "CELL.TRIMSEC");
     201    trimsec->x0 = trimsec->x1 = trimsec->y0 = trimsec->y1 = 0.0;
     202
     203    return success;
     204}
     205
     206
     207
    133208// Mosaic a chip together into a single image
    134209int pmChipMosaic(pmChip *chip,// Chip to mosaic
     
    152227    for (int i = 0; i < cells->n; i++) {
    153228        pmCell *cell = cells->data[i];  // The cell of interest
     229        if (!cell) {
     230            continue;
     231        }
    154232        x0->data.S32[i] = psMetadataLookupS32(NULL, cell->concepts, "CELL.X0");
    155233        y0->data.S32[i] = psMetadataLookupS32(NULL, cell->concepts, "CELL.Y0");
     
    206284    psFree(masks);
    207285    int nCells = cells->n;
     286
     287    // Modify the "original" pixels
     288    if (chip->parent->hdu) {
     289        psLogMsg(__func__, PS_LOG_WARN, "The original format has the entire FPA in a single extension.  "
     290                 "The FPA hierarchy may be invalid following the pmChipMosaic.\n");
     291    } else {
     292        if (! chip->hdu) {
     293            psString chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
     294            chip->hdu = p_pmHDUAlloc(chipName);
     295        }
     296        p_pmHDU *hdu = chip->hdu;
     297        psArrayElementsFree(hdu->images);
     298        psArrayElementsFree(hdu->weights);
     299        psArrayElementsFree(hdu->masks);
     300        hdu->images  = psArrayAlloc(1);
     301        hdu->weights = psArrayAlloc(1);
     302        hdu->masks   = psArrayAlloc(1);
     303        hdu->images->data[0]  = image;
     304        hdu->weights->data[0] = weight;
     305        hdu->masks->data[0]   = mask;
     306        psMetadataAddS32(hdu->header, PS_LIST_TAIL, "NAXIS1", PS_META_REPLACE, "Number of columns", image->numCols);
     307        psMetadataAddS32(hdu->header, PS_LIST_TAIL, "NAXIS2", PS_META_REPLACE, "Number of rows", image->numRows);
     308    }
     309
     310    // Chop off all the component cells, and put in a new one
     311    chip->cells = psArrayAlloc(0);
     312    pmCell *cell = pmCellAlloc(chip, NULL, __func__); // New cell
     313    cellConcepts(cell, cells, xBinChip, yBinChip);
    208314    psFree(cells);
    209 
    210     // Chop off all the component cells, and put in a new one
    211315
    212316    #if 1
     
    214318    // the double free is coming from.  I'm going to drop the pointers on the array and create a memory
    215319    // leak.  We can clean this up later, when we're not under as much pressure.
    216     chip->cells = psArrayAlloc(0);
    217320    #else
    218321
    219322    psArrayElementsFree(chip->cells);
    220     chip->cells = psArrayRealloc(chip->cells, 0);
    221323    chip->cells->n = 0;
    222324    #endif
    223325
    224     pmCell *cell = pmCellAlloc(chip, NULL, __func__); // New cell
    225326    cell->exists = true;
    226327    cell->process = true;
    227328    pmReadout *readout = pmReadoutAlloc(cell); // New readout
    228     readout->image = image;
    229     readout->weight = weight;
    230     readout->mask = mask;
    231     //psFree(readout);
     329    psRegion entire = {0.0, 0.0, 0.0, 0.0};
     330    // Want the readouts to contain a subimage, but that subimage is the whole image.
     331    // This preserves the relationship there was before, where freeing the parent frees the child.
     332    readout->image = psImageSubset(image, entire);
     333    readout->weight = psImageSubset(weight, entire);
     334    readout->mask = psImageSubset(mask, entire);
     335    psFree(readout);
     336
     337    // Well, we've stuffed around with the camera configuration, so it's no longer valid...
     338    #if 0
     339
     340    psFree(chip->parent->camera);
     341    chip->parent->camera = NULL;
     342    #endif
    232343
    233344    return nCells;
  • branches/eam_rel9_p0/psModules/src/astrom/pmConcepts.c

    r6080 r6124  
     1// XXX *REALLY* need generic "concept update" and "concept read" functions that handles the type transparently
     2
    13#include <stdio.h>
    24#include <assert.h>
     
    156158{
    157159    pmConceptsInit();
     160    if (! fpa->camera) {
     161        return false;
     162    }
    158163    psMetadataIterator *iter = psMetadataIteratorAlloc(source, PS_LIST_HEAD, NULL); // Iterator on concepts
    159164    psMetadataItem *item = NULL;    // Item from the concepts
  • branches/eam_rel9_p0/psModules/src/astrom/pmConceptsRead.c

    r6062 r6124  
    275275                    "undefined.\n", name, item->comment, item->type);
    276276        }
     277        psTrace(__func__, 7, "Adding %s (%s): %f\n", name, item->comment, value);
    277278    } else {
    278279        psError(PS_ERR_IO, true, "Concept %s is not defined.\n", name);
    279280    }
    280     psTrace(__func__, 7, "Adding %s (%s): %f\n", name, item->comment, value);
    281281
    282282    return value;
     
    309309                    "--- treating as undefined.\n", name, item->comment, item->type);
    310310        }
     311        psTrace(__func__, 7, "Adding %s (%s): %f\n", name, item->comment, value);
    311312    } else {
    312313        psError(PS_ERR_IO, true, "Concept %s is not defined.\n", name);
    313314    }
    314     psTrace(__func__, 7, "Adding %s (%s): %f\n", name, item->comment, value);
    315315
    316316    return value;
     
    345345                    "undefined.\n", name, item->comment, item->type);
    346346        }
     347        psTrace(__func__, 7, "Read concept %s (%s): %d\n", name, item->comment, value);
    347348    } else {
    348349        psError(PS_ERR_IO, true, "Concept %s is not defined.\n", name);
    349350    }
    350     psTrace(__func__, 7, "Read concept %s (%s): %d\n", name, item->comment, value);
    351351
    352352    return value;
     
    377377                    "undefined.\n", name, item->comment, item->type);
    378378        }
     379        psTrace(__func__, 7, "Read concept %s (%s): %s\n", name, item->comment, value);
    379380    } else {
    380381        psError(PS_ERR_IO, true, "Concept %s is not defined.\n", name);
    381382        value = psStringCopy("");
    382383    }
    383     psTrace(__func__, 7, "Read concept %s (%s): %s\n", name, item->comment, value);
    384384
    385385    return value;
  • branches/eam_rel9_p0/psModules/src/astrom/pmConceptsStandard.c

    r6080 r6124  
    321321{
    322322    psList *biassecs = psListAlloc(NULL); // List of bias sections
     323    psMetadataItem *item = psMetadataItemAlloc("CELL.BIASSEC", PS_DATA_LIST, "Bias sections", biassecs);
     324    psFree(biassecs);               // Drop reference
    323325
    324326    psMetadataItem *secItem = pmConceptRead(fpa, chip, cell, db, "CELL.BIASSEC");
    325327    if (! secItem) {
    326328        psError(PS_ERR_IO, false, "Couldn't find CELL.BIASSEC.\n");
    327     } else if (secItem->type != PS_DATA_STRING) {
     329        return item;
     330    }
     331    if (secItem->type != PS_DATA_STRING) {
    328332        psError(PS_ERR_IO, true, "CELL.BIASSEC is not of type STR (%x)\n", secItem->type);
    329     } else {
    330         psString sections = secItem->data.V; // The section string
    331 
    332         psMetadataItem *sourceItem = pmConceptRead(fpa, chip, cell, db, "CELL.BIASSEC.SOURCE");
    333         if (! sourceItem) {
    334             psError(PS_ERR_IO, false, "Couldn't find CELL.BIASSEC.SOURCE.\n");
    335         } else if (sourceItem->type != PS_DATA_STRING) {
    336             psError(PS_ERR_IO, true, "CELL.BIASSEC.SOURCE is not of type STR (%x)\n", sourceItem->type);
     333        return item;
     334    }
     335
     336    psString sections = secItem->data.V; // The section string
     337
     338    psMetadataItem *sourceItem = pmConceptRead(fpa, chip, cell, db, "CELL.BIASSEC.SOURCE");
     339    if (! sourceItem) {
     340        psError(PS_ERR_IO, false, "Couldn't find CELL.BIASSEC.SOURCE.\n");
     341        return item;
     342    } else if (sourceItem->type != PS_DATA_STRING) {
     343        psError(PS_ERR_IO, true, "CELL.BIASSEC.SOURCE is not of type STR (%x)\n", sourceItem->type);
     344        return item;
     345    }
     346
     347    psString source = sourceItem->data.V; // The source string
     348
     349    if (! strcasecmp(source, "NONE")) {
     350        return item;                // There is no biassec
     351    }
     352
     353    psList *secList = psStringSplit(sections, " ;"); // List of sections
     354    psListIterator *secIter = psListIteratorAlloc(secList, PS_LIST_HEAD, false); // Iterator over
     355    // sections
     356    psString aSection = NULL; // A section from the list
     357    while ((aSection = psListGetAndIncrement(secIter))) {
     358        psRegion *region = psAlloc(sizeof(psRegion)); // Make space for a psRegion (usually passed by
     359        // value)
     360
     361        if (strcasecmp(source, "VALUE") == 0) {
     362            *region = psRegionFromString(aSection);
     363        } else if (strcasecmp(source, "HEADER") == 0) {
     364            psMetadata *header = NULL; // The FITS header
     365            if (cell->hdu) {
     366                header = cell->hdu->header;
     367            } else if (chip->hdu) {
     368                header = chip->hdu->header;
     369            } else if (fpa->hdu) {
     370                header = fpa->hdu->header;
     371            }
     372            if (! header) {
     373                psError(PS_ERR_IO, true, "Unable to find FITS header!\n");
     374                *region = psRegionSet(0.0,0.0,0.0,0.0);
     375            } else {
     376                bool mdok = true;           // Status of MD lookup
     377                psString secValue = psMetadataLookupStr(&mdok, header, aSection);
     378                if (! mdok || ! secValue) {
     379                    psError(PS_ERR_IO, false, "Unable to locate header %s\n", aSection);
     380                    *region = psRegionSet(0.0,0.0,0.0,0.0);
     381                } else {
     382                    *region = psRegionFromString(secValue);
     383                }
     384            }
    337385        } else {
    338             psString source = sourceItem->data.V; // The source string
    339 
    340             psList *secList = psStringSplit(sections, " ;"); // List of sections
    341             psListIterator *secIter = psListIteratorAlloc(secList, PS_LIST_HEAD, false); // Iterator over
    342             // sections
    343             psString aSection = NULL; // A section from the list
    344             while ((aSection = psListGetAndIncrement(secIter))) {
    345                 psRegion *region = psAlloc(sizeof(psRegion)); // Make space for a psRegion (usually passed
    346                 // by value)
    347 
    348                 if (strcasecmp(source, "VALUE") == 0) {
    349                     *region = psRegionFromString(aSection);
    350                 } else if (strcasecmp(source, "HEADER") == 0) {
    351                     psMetadata *header = NULL; // The FITS header
    352                     if (cell->hdu) {
    353                         header = cell->hdu->header;
    354                     } else if (chip->hdu) {
    355                         header = chip->hdu->header;
    356                     } else if (fpa->hdu) {
    357                         header = fpa->hdu->header;
    358                     }
    359                     if (! header) {
    360                         psError(PS_ERR_IO, true, "Unable to find FITS header!\n");
    361                         *region = psRegionSet(0.0,0.0,0.0,0.0);
    362                     } else {
    363                         bool mdok = true;           // Status of MD lookup
    364                         psString secValue = psMetadataLookupStr(&mdok, header, aSection);
    365                         if (! mdok || ! secValue) {
    366                             psError(PS_ERR_IO, false, "Unable to locate header %s\n", aSection);
    367                             *region = psRegionSet(0.0,0.0,0.0,0.0);
    368                         } else {
    369                             *region = psRegionFromString(secValue);
    370                         }
    371                     }
    372                 } else {
    373                     psError(PS_ERR_IO, true, "CELL.BIASSEC.SOURCE (%s) is not HEADER or VALUE --- trying "
    374                             "VALUE.\n", source);
    375                     *region = psRegionFromString(aSection);
    376                 } // Value of CELL.BIASSEC.SOURCE
    377 
    378                 psListAdd(biassecs, PS_LIST_TAIL, region);
    379                 psFree(region);
    380             } // Iterating over multiple sections
    381             psFree(secIter);
    382             psFree(secList);
    383         } // Looking up CELL.BIASSEC.SOURCE
    384     } // Looking up CELL.BIASSEC
    385 
    386     psMetadataItem *item = psMetadataItemAlloc("CELL.BIASSEC", PS_DATA_LIST, "Bias sections", biassecs);
    387     psFree(biassecs);
     386            psError(PS_ERR_IO, true, "CELL.BIASSEC.SOURCE (%s) is not HEADER or VALUE --- trying "
     387                    "VALUE.\n", source);
     388            *region = psRegionFromString(aSection);
     389        } // Value of CELL.BIASSEC.SOURCE
     390
     391        psListAdd(biassecs, PS_LIST_TAIL, region);
     392        psFree(region);
     393    } // Iterating over multiple sections
     394    psFree(secIter);
     395    psFree(secList);
     396
    388397    return item;
    389398}
     
    689698    psMetadataItem *biassecItem = psMetadataLookup(cell->concepts, "CELL.BIASSEC");
    690699    psList *biassecs = biassecItem->data.V; // The biassecs region list
     700
     701    if (biassecs->n == 0) {
     702        psMetadataItem *noneItem = psMetadataItemAllocStr("CELL.BIASSEC", "Bias section", "NONE");
     703        pmConceptWriteItem(fpa, chip, cell, db, noneItem);
     704        return true;
     705    }
     706
    691707    psString source = pmConceptReadString(fpa, chip, cell, db, "CELL.TRIMSEC.SOURCE"); // The source string
    692708
  • branches/eam_rel9_p0/psModules/src/astrom/pmConceptsWrite.c

    r6062 r6124  
    7373                           )
    7474{
     75    if (! cell->camera) {
     76        return false;
     77    }
    7578    if (cell) {
    7679        psMetadataItem *item = psMetadataLookup(cell->camera, concept->name); // Info we want
     
    9093    bool mdok = true;                   // Status of MD lookup
    9194    bool status = false;                // Status of setting header
     95    if (! fpa->camera) {
     96        return false;
     97    }
    9298    psMetadata *translation = psMetadataLookupMD(&mdok, fpa->camera, "TRANSLATION"); // FITS translation
    9399    if (! mdok) {
     
    158164{
    159165    bool mdOK = true;                   // Status of MD lookup
     166    if (! fpa->camera) {
     167        return false;
     168    }
    160169    psMetadata *defaults = psMetadataLookupMD(&mdOK, fpa->camera, "DEFAULTS");
    161170    if (! mdOK || ! defaults) {
     
    224233
    225234    bool mdStatus = true;               // Status of MD lookup
     235    if (! fpa->camera) {
     236        return false;
     237    }
    226238    psMetadata *database = psMetadataLookupMD(&mdStatus, fpa->camera, "DATABASE");
    227239    if (! mdStatus) {
     
    319331                       )
    320332{
     333    if (! fpa->camera) {
     334        return false;
     335    }
     336
    321337    // Try headers, database, defaults in order
    322338    psTrace(__func__, 3, "Trying to set concept %s...\n", concept->name);
  • branches/eam_rel9_p0/psModules/src/astrom/pmFPARead.c

    r6080 r6124  
    161161
    162162    // Read in....
    163     psTrace(__func__, 1, "Working on FPA...\n");
     163    psTrace(__func__, 1, "Reading FPA...\n");
    164164    if (fpa->hdu) {
    165165        hdu = fpa->hdu;
     
    217217                }
    218218            }
     219            psTrace(__func__, 5, "Reading concepts for cell %d...\n", j);
    219220            pmConceptsReadCell(cell, db);
    220221
    221             psTrace(__func__, 5, "Allocating readouts for chip %d cell %d...\n",
    222                     i, j);
     222            psTrace(__func__, 5, "Allocating readouts for chip %d cell %d...\n", i, j);
    223223            generateReadouts(cell, hdu);
    224224
     
    227227        chip->exists = true;
    228228    }
     229
     230    psTrace(__func__, 1, "Done reading FPA...\n");
    229231
    230232    return true;
  • branches/eam_rel9_p0/psModules/src/astrom/pmFPAWrite.c

    r6062 r6124  
    2727{
    2828    bool status = true;                 // Status of writing, to return
     29
     30    psTrace(__func__, 1, "Writing FPA...\n");
    2931
    3032    psTrace(__func__, 7, "Outgesting FPA concepts...\n");
     
    7072    }
    7173
    72 
     74    psTrace(__func__, 1, "Done writing FPA...\n");
    7375
    7476    return status;
Note: See TracChangeset for help on using the changeset viewer.