IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 18, 2006, 1:43:28 PM (20 years ago)
Author:
Paul Price
Message:

Extensive changes to FPA reading/writing functions to support mask and weight map reading/writing. Actually, not so much changes as generalisations to the reading/writing functions. Moved the reading/writing functionality into file-static functions, which the higher level functions for reading/writing particular elements (image, mask, weight) call. Added additional pmFPAfile types for mask and weight, with supporting functionality to call the reading/writing functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/camera/pmFPARead.c

    r9998 r10081  
    1616
    1717#include "pmFPARead.h"
     18
     19//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     20// Definitions
     21//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     22
     23// Specify what to read
     24typedef enum {
     25    FPA_READ_TYPE_IMAGE,                // Read image
     26    FPA_READ_TYPE_MASK,                 // Read mask
     27    FPA_READ_TYPE_WEIGHT                // Read weight map
     28} fpaReadType;
    1829
    1930//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    155166}
    156167
     168
     169// Read into an cell; this is the engine for pmCellRead, pmCellReadMask, pmCellReadWeight
     170// Does most of the work for the reading --- reads the HDU, and portions the HDU into readouts.
     171static bool cellRead(pmCell *cell,      // Cell into which to read
     172                     psFits *fits,      // FITS file from which to read
     173                     psDB *db,          // Database handle, for concepts ingest
     174                     fpaReadType type   // Type to read
     175                    )
     176{
     177    assert(cell);
     178    assert(fits);
     179
     180    pmHDU *hdu = pmHDUFromCell(cell);   // The HDU
     181    if (!hdu) {
     182        return true;                    // We read everything we could
     183    }
     184
     185    bool (*hduReadFunc)(pmHDU*, psFits*); // Function to use to read the HDU
     186    psArray **imageArray;               // Array of images in the HDU
     187    psElemType imageType;               // Expected type for image
     188    switch (type) {
     189    case FPA_READ_TYPE_IMAGE:
     190        hduReadFunc = pmHDURead;
     191        imageArray = &hdu->images;
     192        imageType = PS_TYPE_F32;
     193        break;
     194    case FPA_READ_TYPE_MASK:
     195        hduReadFunc = pmHDUReadMask;
     196        imageArray = &hdu->masks;
     197        imageType = PS_TYPE_MASK;
     198        break;
     199    case FPA_READ_TYPE_WEIGHT:
     200        hduReadFunc = pmHDUReadWeight;
     201        imageArray = &hdu->weights;
     202        imageType = PS_TYPE_F32;
     203        break;
     204    default:
     205        psAbort(__func__, "Unknown read type: %x\n", type);
     206    }
     207
     208    if (!(*imageArray) && !hduReadFunc(hdu, fits)) {
     209        psError(PS_ERR_UNKNOWN, false, "Unable to read HDU for cell.\n");
     210        return false;
     211    }
     212
     213    if (!pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
     214        //psError(PS_ERR_UNKNOWN, false, "Failed to read concepts for cell");
     215        //return false;
     216        psWarning("Difficulty reading concepts for cell; attempting to proceed.");
     217    }
     218
     219    // Having read the cell, we now have to cut it up
     220    psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC");
     221    psList *biassecs = psMetadataLookupPtr(NULL, cell->concepts, "CELL.BIASSEC");
     222    if (psRegionIsNaN(*trimsec)) {
     223        psError(PS_ERR_IO, false, "CELL.TRIMSEC is not set --- can't read cell.\n");
     224        return false;
     225    }
     226
     227    // Iterate over each of the image planes, converting type if necessary, and extracting the bits that
     228    // matter (CELL.TRIMSEC, CELL.BIASSEC) into readouts with readoutCarve.
     229    for (int i = 0; i < (*imageArray)->n; i++) {
     230        psImage *source = (*imageArray)->data[i]; // Source image, from the i-th plane
     231
     232        // Type conversion here to support the modules, which don't have multiple type support yet
     233        if (source->type.type != imageType) {
     234            psImage *temp = psImageCopy(NULL, source, imageType); // Temporary image
     235            psFree((*imageArray)->data[i]);
     236            (*imageArray)->data[i] = temp;
     237            source = temp;
     238        }
     239
     240        pmReadout *readout;             // Readout into which to read
     241        if (cell->readouts->n > i && cell->readouts->data[i]) {
     242            readout = psMemIncrRefCounter(cell->readouts->data[i]);
     243        } else {
     244            readout = pmReadoutAlloc(cell);
     245        }
     246
     247        psImage **target;               // Place in readout to put carved image
     248        switch (type) {
     249        case FPA_READ_TYPE_IMAGE:
     250            target = &readout->image;
     251            break;
     252        case FPA_READ_TYPE_MASK:
     253            target = &readout->mask;
     254            break;
     255        case FPA_READ_TYPE_WEIGHT:
     256            target = &readout->weight;
     257            break;
     258        default:
     259            psAbort(__func__, "Unknown read type: %x\n", type);
     260        }
     261
     262        if (!readoutCarve(readout, target, source, trimsec, biassecs)) {
     263            psError(PS_ERR_UNEXPECTED_NULL, false,
     264                    "Unable to carve readout into image and bias sections for %d the plane.", i);
     265            return NULL;
     266        }
     267        psFree(readout);                // Drop reference
     268    }
     269
     270    pmCellSetDataStatus(cell, true);
     271    return true;
     272}
     273
     274
     275// Read into an chip; this is the engine for pmChipRead, pmChipReadMask, pmChipReadWeight
     276// Iterates over component cells, reading each
     277static bool chipRead(pmChip *chip,      // Chip into which to read
     278                     psFits *fits,      // FITS file from which to read
     279                     psDB *db,          // Database handle, for concepts ingest
     280                     fpaReadType type   // Type to read
     281                    )
     282{
     283    assert(chip);
     284    assert(fits);
     285
     286    bool success = false;               // Were we able to read at least one HDU?
     287    psArray *cells = chip->cells;       // Array of cells
     288    for (int i = 0; i < cells->n; i++) {
     289        pmCell *cell = cells->data[i];  // The cell of interest
     290        success |= cellRead(cell, fits, db, type);
     291    }
     292    if (success) {
     293        if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
     294            psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
     295            return false;
     296        }
     297        // XXX probably could just use chip->data_exists
     298        pmChipSetDataStatus(chip, true);
     299    }
     300
     301    return success;
     302}
     303
     304
     305// Read into an FPA; this is the engine for pmFPARead, pmFPAReadMask, pmFPAReadWeight
     306// Iterates over component chips, reading each
     307static bool fpaRead(pmFPA *fpa,         // FPA into which to read
     308                    psFits *fits,       // FITS file from which to read
     309                    psDB *db,           // Database handle, for concepts ingest
     310                    fpaReadType type    // Type to read
     311                   )
     312{
     313    assert(fpa);
     314    assert(fits);
     315
     316    bool success = false;               // Were we able to read at least one HDU?
     317    psArray *chips = fpa->chips;        // Array of chips
     318    for (int i = 0; i < chips->n; i++) {
     319        pmChip *chip = chips->data[i];  // The cell of interest
     320        success |= chipRead(chip, fits, db, type);
     321    }
     322    if (success) {
     323        if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
     324            psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
     325            return false;
     326        }
     327    } else {
     328        psError(PS_ERR_UNKNOWN, false, "Unable to read any chips in FPA");
     329    }
     330
     331    return success;
     332}
     333
    157334//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    158335// Public functions
     336//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     337
     338
     339//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     340// Reading images
    159341//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    160342
     
    325507    PS_ASSERT_PTR_NON_NULL(fits, false);
    326508
    327     pmHDU *hdu = pmHDUFromCell(cell);   // The HDU
    328     if (!hdu) {
    329         return true;                    // We read everything we could
    330     }
    331     if (!hdu->images && !pmHDURead(hdu, fits)) {
    332         psError(PS_ERR_UNKNOWN, false, "Unable to read HDU for cell.\n");
    333         return false;
    334     }
    335 
    336     if (!pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
    337         //psError(PS_ERR_UNKNOWN, false, "Failed to read concepts for cell");
    338         //return false;
    339         psWarning("Difficulty reading concepts for cell; attempting to proceed.");
    340     }
    341 
    342     // Having read the cell, we now have to cut it up
    343     psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC");
    344     psList *biassecs = psMetadataLookupPtr(NULL, cell->concepts, "CELL.BIASSEC");
    345     if (psRegionIsNaN(*trimsec)) {
    346         psError(PS_ERR_IO, false, "CELL.TRIMSEC is not set --- can't read cell.\n");
    347         return false;
    348     }
    349 
    350     // Iterate over each of the image planes
    351     for (int i = 0; i < hdu->images->n; i++) {
    352         psImage *image = hdu->images->data[i]; // The i-th plane
    353 
    354         // XXX: Type conversion here to support the modules, which don't have multiple type support yet
    355         if (image->type.type != PS_TYPE_F32) {
    356             psImage *temp = psImageCopy(NULL, image, PS_TYPE_F32); // Temporary image
    357             psFree(hdu->images->data[i]);
    358             hdu->images->data[i] = temp;
    359             image = temp;
    360         }
    361 
    362         pmReadout *readout;             // Readout into which to read
    363         if (cell->readouts->n > i && cell->readouts->data[i]) {
    364             readout = psMemIncrRefCounter(cell->readouts->data[i]);
    365         } else {
    366             readout = pmReadoutAlloc(cell);
    367         }
    368         if (!readoutCarve(readout, &readout->image, image, trimsec, biassecs)) {
    369             psError(PS_ERR_UNEXPECTED_NULL, false,
    370                     "Unable to carve readout into image and bias sections for %d the plane.", i);
    371             return NULL;
    372         }
    373         psFree(readout);                // Drop reference
    374     }
    375 
    376     pmCellSetDataStatus(cell, true);
    377     return true;
     509    return cellRead(cell, fits, db, FPA_READ_TYPE_IMAGE);
    378510}
    379511
     
    383515    PS_ASSERT_PTR_NON_NULL(fits, false);
    384516
    385     bool success = false;               // Were we able to read at least one HDU?
    386     psArray *cells = chip->cells;       // Array of cells
    387     for (int i = 0; i < cells->n; i++) {
    388         pmCell *cell = cells->data[i];  // The cell of interest
    389         success |= pmCellRead(cell, fits, db);
    390     }
    391     if (success) {
    392         if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
    393             psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
    394             return false;
    395         }
    396         // XXX probably could just use chip->data_exists
    397         pmChipSetDataStatus(chip, true);
    398     }
    399 
    400     return success;
     517    return chipRead(chip, fits, db, FPA_READ_TYPE_IMAGE);
    401518}
    402519
     
    406523    PS_ASSERT_PTR_NON_NULL(fits, false);
    407524
    408     bool success = false;               // Were we able to read at least one HDU?
    409     psArray *chips = fpa->chips;        // Array of chips
    410     for (int i = 0; i < chips->n; i++) {
    411         pmChip *chip = chips->data[i];  // The cell of interest
    412         success |= pmChipRead(chip, fits, db);
    413     }
    414     if (success) {
    415         if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
    416             psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
    417             return false;
    418         }
    419     } else {
    420         psError(PS_ERR_UNKNOWN, false, "Unable to read any chips in FPA");
    421     }
    422 
    423     return success;
     525    return fpaRead(fpa, fits, db, FPA_READ_TYPE_IMAGE);
    424526}
    425527
     
    434536    PS_ASSERT_PTR_NON_NULL(fits, false);
    435537
    436     pmHDU *hdu = pmHDUFromCell(cell);   // The HDU
    437     if (!hdu) {
    438         return false;                    // Nothing to see here; move along
    439     }
    440     if (!hdu->images && !pmHDURead(hdu, fits)) {
    441         psError(PS_ERR_UNKNOWN, false, "Unable to read HDU for cell.\n");
    442         return false;
    443     }
    444 
    445     if (!pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
    446         psError(PS_ERR_UNKNOWN, false, "Failed to read concepts for cell");
    447         return false;
    448     }
    449 
    450     // Having read the cell, we now have to cut it up
    451     psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC");
    452 
    453     // Iterate over each of the image planes
    454     for (int i = 0; i < hdu->images->n; i++) {
    455         psImage *image = hdu->images->data[i]; // The i-th plane
    456 
    457         if (image->type.type != PS_TYPE_U8) {
    458             psImage *temp = psImageCopy(NULL, image, PS_TYPE_U8); // Temporary image
    459             psFree(hdu->images->data[i]);
    460             hdu->images->data[i] = temp;
    461             image = temp;
    462         }
    463 
    464         pmReadout *readout;             // Readout into which to read
    465         if (cell->readouts->n > i && cell->readouts->data[i]) {
    466             readout = psMemIncrRefCounter(cell->readouts->data[i]);
    467         } else {
    468             readout = pmReadoutAlloc(cell);
    469         }
    470         if (!readoutCarve(readout, &readout->mask, image, trimsec, NULL)) {
    471             psError(PS_ERR_UNEXPECTED_NULL, false,
    472                     "Unable to carve readout into image and bias sections for %d the plane.", i);
    473             return NULL;
    474         }
    475         psFree(readout);                // Drop reference
    476     }
    477 
    478     pmCellSetDataStatus(cell, true);
    479     return true;
     538    return cellRead(cell, fits, db, FPA_READ_TYPE_MASK);
    480539}
    481540
     
    485544    PS_ASSERT_PTR_NON_NULL(fits, false);
    486545
    487     bool success = false;               // Were we able to read at least one HDU?
    488     psArray *cells = chip->cells;       // Array of cells
    489     for (int i = 0; i < cells->n; i++) {
    490         pmCell *cell = cells->data[i];  // The cell of interest
    491         success |= pmCellReadMask(cell, fits, db);
    492     }
    493     if (success) {
    494         if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
    495             psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
    496             return false;
    497         }
    498         // XXX probably could just use chip->data_exists
    499         pmChipSetDataStatus(chip, true);
    500     }
    501 
    502     return success;
     546    return chipRead(chip, fits, db, FPA_READ_TYPE_MASK);
    503547}
    504548
     
    508552    PS_ASSERT_PTR_NON_NULL(fits, false);
    509553
    510     bool success = false;               // Were we able to read at least one HDU?
    511     psArray *chips = fpa->chips;        // Array of chips
    512     for (int i = 0; i < chips->n; i++) {
    513         pmChip *chip = chips->data[i];  // The cell of interest
    514         success |= pmChipReadMask(chip, fits, db);
    515     }
    516     if (success) {
    517         if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
    518             psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
    519             return false;
    520         }
    521     } else {
    522         psError(PS_ERR_UNKNOWN, false, "Unable to read any chips in FPA");
    523     }
    524 
    525     return success;
     554    return fpaRead(fpa, fits, db, FPA_READ_TYPE_MASK);
    526555}
    527556
     
    535564    PS_ASSERT_PTR_NON_NULL(fits, false);
    536565
    537     pmHDU *hdu = pmHDUFromCell(cell);   // The HDU
    538     if (!hdu) {
    539         return false;                    // Nothing to see here; move along
    540     }
    541     if (!hdu->images && !pmHDURead(hdu, fits)) {
    542         psError(PS_ERR_UNKNOWN, false, "Unable to read HDU for cell.\n");
    543         return false;
    544     }
    545 
    546     if (!pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
    547         psError(PS_ERR_UNKNOWN, false, "Failed to read concepts for cell");
    548         return false;
    549     }
    550 
    551     // Having read the cell, we now have to cut it up
    552     psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC");
    553 
    554     // Iterate over each of the image planes
    555     for (int i = 0; i < hdu->images->n; i++) {
    556         psImage *image = hdu->images->data[i]; // The i-th plane
    557 
    558         if (image->type.type != PS_TYPE_F32) {
    559             psImage *temp = psImageCopy(NULL, image, PS_TYPE_F32); // Temporary image
    560             psFree(hdu->images->data[i]);
    561             hdu->images->data[i] = temp;
    562             image = temp;
    563         }
    564 
    565         pmReadout *readout;             // Readout into which to read
    566         if (cell->readouts->n > i && cell->readouts->data[i]) {
    567             readout = psMemIncrRefCounter(cell->readouts->data[i]);
    568         } else {
    569             readout = pmReadoutAlloc(cell);
    570         }
    571         if (!readoutCarve(readout, &readout->weight, image, trimsec, NULL)) {
    572             psError(PS_ERR_UNEXPECTED_NULL, false,
    573                     "Unable to carve readout into image and bias sections for %d the plane.", i);
    574             return NULL;
    575         }
    576         psFree(readout);                // Drop reference
    577     }
    578 
    579     pmCellSetDataStatus(cell, true);
    580     return true;
     566    return cellRead(cell, fits, db, FPA_READ_TYPE_WEIGHT);
    581567}
    582568
     
    586572    PS_ASSERT_PTR_NON_NULL(fits, false);
    587573
    588     bool success = false;               // Were we able to read at least one HDU?
    589     psArray *cells = chip->cells;       // Array of cells
    590     for (int i = 0; i < cells->n; i++) {
    591         pmCell *cell = cells->data[i];  // The cell of interest
    592         success |= pmCellReadWeight(cell, fits, db);
    593     }
    594     if (success) {
    595         if (!pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, true, NULL)) {
    596             psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
    597             return false;
    598         }
    599         // XXX probably could just use chip->data_exists
    600         pmChipSetDataStatus(chip, true);
    601     }
    602 
    603     return success;
     574    return chipRead(chip, fits, db, FPA_READ_TYPE_WEIGHT);
    604575}
    605576
     
    609580    PS_ASSERT_PTR_NON_NULL(fits, false);
    610581
    611     bool success = false;               // Were we able to read at least one HDU?
    612     psArray *chips = fpa->chips;        // Array of chips
    613     for (int i = 0; i < chips->n; i++) {
    614         pmChip *chip = chips->data[i];  // The cell of interest
    615         success |= pmChipReadWeight(chip, fits, db);
    616     }
    617     if (success) {
    618         if (!pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, true, NULL)) {
    619             psError(PS_ERR_IO, false, "Failed to read concepts for FPA.\n");
    620             return false;
    621         }
    622     } else {
    623         psError(PS_ERR_UNKNOWN, false, "Unable to read any chips in FPA");
    624     }
    625 
    626     return success;
    627 }
    628 
     582    return fpaRead(fpa, fits, db, FPA_READ_TYPE_WEIGHT);
     583}
     584
     585//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     586// Reading FITS tables
     587//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    629588
    630589int pmCellReadTable(pmCell *cell, psFits *fits, const char *name)
Note: See TracChangeset for help on using the changeset viewer.