IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9983


Ignore:
Timestamp:
Nov 14, 2006, 2:40:02 PM (19 years ago)
Author:
Paul Price
Message:

Adding functions to write masks. For this to work, had to add mask handling to pmHDUGenerate, and figured I may as well add weights as well.

Location:
trunk/psModules/src/camera
Files:
6 edited

Legend:

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

    r9949 r9983  
    263263            readout->row0 += readout->image->numRows;
    264264            offset = readout->row0;
     265            if (numScans == 0) {
     266                numScans = naxis2 - offset;
     267            }
    265268        } else {
    266269            // Reading columns
    267270            readout->col0 += readout->image->numCols;
    268271            offset = readout->col0;
     272            if (numScans == 0) {
     273                numScans = naxis1 - offset;
     274            }
    269275        }
    270276    }
  • trunk/psModules/src/camera/pmFPAWrite.c

    r9949 r9983  
    242242
    243243
     244bool pmCellWriteMask(pmCell *cell,          // Cell to write
     245                     psFits *fits,          // FITS file to which to write
     246                     psDB *db           // Database handle for "concepts" update
     247                    )
     248{
     249    PS_ASSERT_PTR_NON_NULL(cell, false);
     250    PS_ASSERT_PTR_NON_NULL(fits, false);
     251
     252    pmHDU *hdu = cell->hdu;             // The HDU
     253    if (!hdu) {
     254        return true;                    // We wrote every HDU that exists
     255    }
     256
     257    psTrace ("pmFPAWrite", 5, "writing mask to Cell\n");
     258
     259    // Generate the HDU if needed --- this is required after a pmFPACopy, or similar, which does not
     260    // generate the HDU, but only copies the structure.
     261    if (!hdu->blankPHU && !hdu->masks) {
     262        if (!pmHDUGenerateForCell(cell) || !hdu->masks) {
     263            psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for cell --- likely programming error.\n");
     264            return false;
     265        }
     266    }
     267
     268    pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | PM_CONCEPT_SOURCE_DEFAULTS;
     269    if (!pmConceptsWriteCell(cell, source, false, NULL)) {
     270        psError(PS_ERR_IO, false, "Unable to write concepts for cell.\n");
     271        return false;
     272    }
     273    if (!pmHDUWriteMask(hdu, fits)) {
     274        psError(PS_ERR_IO, false, "Unable to write HDU for cell.\n");
     275        return false;
     276    }
     277
     278    return true;
     279}
     280
     281
     282bool pmChipWriteMask(pmChip *chip, psFits *fits, psDB *db)
     283{
     284    PS_ASSERT_PTR_NON_NULL(chip, false);
     285    PS_ASSERT_PTR_NON_NULL(fits, false);
     286
     287    pmHDU *hdu = chip->hdu;             // The HDU
     288    if (!hdu) {
     289        return true;                    // We wrote every HDU that exists
     290    }
     291
     292    psTrace ("pmFPAWrite", 5, "writing mask to Chip\n");
     293
     294    // Generate the HDU if needed --- this is required after a pmFPACopy, or similar, which does not
     295    // generate the HDU, but only copies the structure.
     296    if (!hdu->blankPHU && !hdu->masks) {
     297        if (!pmHDUGenerateForChip(chip) || !hdu->masks) {
     298            psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for chip --- likely programming error.\n");
     299            return false;
     300        }
     301    }
     302
     303    pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | PM_CONCEPT_SOURCE_DEFAULTS;
     304    if (!pmConceptsWriteChip(chip, source, false, true, NULL)) {
     305        psError(PS_ERR_IO, false, "Unable to write concepts for chip.\n");
     306        return false;
     307    }
     308    if (!pmHDUWriteMask(hdu, fits)) {
     309        psError(PS_ERR_IO, false, "Unable to write HDU for chip.\n");
     310        return false;
     311    }
     312
     313    return true;
     314}
     315
     316
     317
     318bool pmFPAWriteMask(pmFPA *fpa, psFits *fits, psDB *db)
     319{
     320    PS_ASSERT_PTR_NON_NULL(fpa, false);
     321    PS_ASSERT_PTR_NON_NULL(fits, false);
     322
     323    pmHDU *hdu = fpa->hdu;              // The HDU
     324    if (!hdu) {
     325        return true;                    // We wrote every HDU that exists
     326    }
     327
     328    psTrace ("pmFPAWrite", 5, "writing mask to FPA\n");
     329
     330    // Generate the HDU if needed --- this is required after a pmFPACopy, or similar, which does not
     331    // generate the HDU, but only copies the structure.
     332    if (!hdu->blankPHU && !hdu->masks) {
     333        if (!pmHDUGenerateForFPA(fpa) || !hdu->masks) {
     334            psError(PS_ERR_UNKNOWN, false, "Unable to generate HDU for chip --- likely programming error.\n");
     335            return false;
     336        }
     337    }
     338
     339    pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CELLS | PM_CONCEPT_SOURCE_DEFAULTS;
     340    if (!pmConceptsWriteFPA(fpa, source, true, NULL)) {
     341        psError(PS_ERR_IO, false, "Unable to write concepts for FPA.\n");
     342        return false;
     343    }
     344    if (!pmHDUWriteMask(hdu, fits))  {
     345        psError(PS_ERR_IO, false, "Unable to write HDU for FPA.\n");
     346        return false;
     347    }
     348
     349    return true;
     350}
     351
     352
     353
    244354int pmCellWriteTable(psFits *fits, const pmCell *cell, const char *name)
    245355{
  • trunk/psModules/src/camera/pmFPAWrite.h

    r9949 r9983  
    77/// @author Paul Price, IfA
    88///
    9 /// @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    10 /// @date $Date: 2006-11-13 22:15:05 $
     9/// @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     10/// @date $Date: 2006-11-15 00:40:02 $
    1111///
    1212/// Copyright 2005-2006 Institute for Astronomy, University of Hawaii
     
    6767               );
    6868
     69/// Write a cell mask to a FITS file
     70///
     71/// Generates CELL.TRIMSEC, CELL.BIASSEC and the HDU pixels if required.  Writes the concepts to the various
     72/// locations, and then the HDU mask to the FITS file.
     73bool pmCellWriteMask(pmCell *cell,      ///<  Cell to write
     74                     psFits *fits,      ///<  FITS file to which to write
     75                     psDB *db           ///<  Database handle for "concepts" update
     76                    );
     77
     78/// Write a chip mask to a FITS file
     79///
     80/// Generates CELL.TRIMSEC, CELL.BIASSEC and the HDU pixels if required.  Writes the concepts to the various
     81/// locations, and then the HDU mask to the FITS file.
     82bool pmChipWriteMask(pmChip *chip,      ///<  Chip to write
     83                     psFits *fits,      ///<  FITS file to which to write
     84                     psDB *db           ///<  Database handle for "concepts" update
     85                    );
     86
     87/// Write an FPA mask to a FITS file
     88///
     89/// Generates CELL.TRIMSEC, CELL.BIASSEC and the HDU pixels if required.  Writes the concepts to the various
     90/// locations, and then the HDU mask to the FITS file.
     91bool pmFPAWriteMask(pmFPA *fpa,         ///<  FPA to write
     92                    psFits *fits,       ///<  FITS file to which to write
     93                    psDB *db            ///<  Database handle for "concepts" update
     94                   );
     95
    6996/// Write a FITS table from the cell's analysis metadata.
    7097///
  • trunk/psModules/src/camera/pmHDU.c

    r9609 r9983  
    124124}
    125125
    126 // XXX: Add a region specifier?
    127 bool pmHDUWrite(pmHDU *hdu, psFits *fits)
     126// Write an HDU to a FITS file
     127bool hduWrite(pmHDU *hdu,               // HDU to write
     128              psArray *images,          // Images to write
     129              psFits *fits              // FITS file to which to write
     130             )
    128131{
    129     PS_ASSERT_PTR_NON_NULL(hdu, false);
    130     PS_ASSERT_PTR_NON_NULL(fits, false);
     132    assert(hdu);
     133    assert(fits);
    131134
    132135    psTrace("psModules.camera", 7, "Writing HDU %s\n", hdu->extname);
    133136
    134     if (hdu->images && !hdu->header) {
    135         psError(PS_ERR_IO, true, "Both image and table data provided in HDU, but no header --- "
    136                 "don't know which to write!\n");
    137         return false;
    138     }
    139 
    140     if (!hdu->images && !hdu->header) {
     137    if (!images && !hdu->header) {
    141138        psLogMsg(__func__, PS_LOG_WARN, "Nothing to write for HDU %s\n", hdu->extname);
    142139        return false;
     
    154151
    155152    // Only a header
    156     if (!hdu->images && !psFitsWriteBlank(fits, hdu->header, extname)) {
     153    if (!images && !psFitsWriteBlank(fits, hdu->header, extname)) {
    157154        psError(PS_ERR_IO, false, "Unable to write header for extension %s\n", extname);
    158155    }
    159156
    160     if (hdu->images) {
     157    if (images) {
    161158        psTrace("psModules.camera", 9, "Writing pixels for %s\n", hdu->extname);
    162         if (!psFitsWriteImageCube(fits, hdu->header, hdu->images, extname)) {
     159        if (!psFitsWriteImageCube(fits, hdu->header, images, extname)) {
    163160            psError(PS_ERR_IO, false, "Unable to write image to extension %s\n", hdu->extname);
    164161            return false;
     
    168165}
    169166
     167// XXX: Add a region specifier?
     168bool pmHDUWrite(pmHDU *hdu, psFits *fits)
     169{
     170    PS_ASSERT_PTR_NON_NULL(hdu, false);
     171    PS_ASSERT_PTR_NON_NULL(fits, false);
     172
     173    return hduWrite(hdu, hdu->images, fits);
     174}
     175
     176bool pmHDUWriteMask(pmHDU *hdu, psFits *fits)
     177{
     178    PS_ASSERT_PTR_NON_NULL(hdu, false);
     179    PS_ASSERT_PTR_NON_NULL(fits, false);
     180
     181    return hduWrite(hdu, hdu->masks, fits);
     182}
     183
  • trunk/psModules/src/camera/pmHDU.h

    r9698 r9983  
    77/// @author Paul Price, IfA
    88///
    9 /// @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    10 /// @date $Date: 2006-10-21 03:00:43 $
     9/// @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     10/// @date $Date: 2006-11-15 00:40:02 $
    1111///
    1212/// Copyright 2005-2006 Institute for Astronomy, University of Hawaii
     
    5656               );
    5757
     58/// Write the HDU header and mask
     59bool pmHDUWriteMask(pmHDU *hdu,         ///< HDU to write mask
     60                    psFits *fits        ///< FITS file to write to
     61                   );
     62
    5863#endif
  • trunk/psModules/src/camera/pmHDUGenerate.c

    r9730 r9983  
    218218
    219219        pmReadout *readout = cell->readouts->data[0]; // The first readout, as representative
    220         psImage *image = readout->image;// The proper image
     220        // The proper image, used to get the size
     221        psImage *image = readout->image ? readout->image : (readout->mask ? readout->mask : readout->weight);
    221222        if (!image) {
    222223            continue;
     224        }
     225        if (readout->mask &&
     226                (readout->mask->numCols != image->numCols || readout->mask->numRows != image->numRows)) {
     227            psLogMsg(__func__, PS_LOG_WARN, "Image and mask have different sizes (%dx%d vs %dx%d)!\n",
     228                     image->numCols, image->numRows, readout->mask->numCols, readout->mask->numRows);
     229        }
     230        if (readout->weight &&
     231                (readout->weight->numCols != image->numCols || readout->weight->numRows != image->numRows)) {
     232            psLogMsg(__func__, PS_LOG_WARN, "Image and weight have different sizes (%dx%d vs %dx%d)!\n",
     233                     image->numCols, image->numRows, readout->weight->numCols, readout->weight->numRows);
    223234        }
    224235        // New reference
     
    239250    return (position > 0);
    240251}
     252
     253// Check the type for a current image against a previous type
     254static psElemType checkTypes(psElemType previous, // Previously defined type, or 0
     255                             psElemType current // Current type
     256                            )
     257{
     258    if (previous == 0) {
     259        return current;
     260    }
     261
     262    if (previous != current) {
     263        psLogMsg(__func__, PS_LOG_WARN, "Images within the HDU are of different types "
     264                 "(%x vs %x) --- promoting\n", previous, current);
     265        return PS_MAX(previous, current);
     266    }
     267
     268    return previous;
     269}
     270
     271
     272// Paste the source image into the target, according to the provided region.  The source is then updated to
     273// reference the region within the target.
     274static void pasteImage(psImage *target, // Target image, into which the paste is made
     275                       psImage **sourcePtr,// Source image, from which the paste is made, and then changed
     276                       psRegion *region // Image section into which to paste
     277                      )
     278{
     279    psImage *source = *sourcePtr;       // Dereference pointer, for convenience
     280    if (source->numCols != region->x1 - region->x0 || source->numRows != region->y1 - region->y0) {
     281        psString regionString = psRegionToString(*region);
     282        psLogMsg(__func__, PS_LOG_WARN, "Image size (%dx%d) does not match region (%s).\n",
     283                 source->numCols, source->numRows, regionString);
     284        psFree(regionString);
     285    }
     286    psImageOverlaySection(target, source, region->x0, region->y0, "=");
     287
     288    // Reference the HDU version, so that subsequent changes will touch the HDU
     289    psFree(source);
     290    *sourcePtr = psImageSubset(target, *region);
     291
     292    return;
     293}
     294
    241295
    242296// Generate the HDU, given a list of cells below that HDU.  This is the main engine function, that does all
     
    248302    // Check the number of readouts is consistent within the HDU
    249303    int numReadouts = -1;               // Number of readouts
    250     psElemType type = 0;                // Type of readout images
     304    psElemType imageType = 0;           // Type of readout images
     305    psElemType maskType = 0;   // Type of readout masks
     306    psElemType weightType = 0;          // Type of readout weights
    251307    {
    252308        psListIterator *iter = psListIteratorAlloc(cells, PS_LIST_HEAD, false); // Iterator for cells
     
    264320            for (int i = 0; i < numReadouts; i++) {
    265321                pmReadout *readout = readouts->data[i]; // The readout
    266                 if (!readout || !readout->image) {
     322                if (!readout) {
    267323                    continue;
    268324                }
    269                 psElemType imageType = readout->image->type.type; // Type for this image
    270                 if (type == 0) {
    271                     type = imageType;
    272                 } else if (type != imageType) {
    273                     psLogMsg(__func__, PS_LOG_WARN, "Images within the HDU are of different types "
    274                              "(%x vs %x) --- promoting\n", type, imageType);
    275                     type = PS_MAX(type, imageType);
     325
     326                if (readout->image) {
     327                    imageType = checkTypes(imageType, readout->image->type.type);
     328                }
     329                if (readout->mask) {
     330                    maskType = checkTypes(maskType, readout->mask->type.type);
     331                }
     332                if (readout->weight) {
     333                    weightType = checkTypes(weightType, readout->weight->type.type);
    276334                }
    277335            }
     
    279337        psFree(iter);
    280338    }
    281     if (numReadouts == 0 || type == 0) {
     339    if (numReadouts == 0 || (imageType == 0 && maskType == 0 && weightType == 0)) {
    282340        // Nothing from which to create an HDU
    283341        psFree(cells);
     
    293351
    294352    // Generate the HDU
    295     hdu->images = psArrayAlloc(numReadouts);
    296     for (int i = 0; i < numReadouts; i++) {
    297         psImage *image = psImageAlloc(xSize, ySize, type);
    298         psImageInit(image, 0.0);
    299         hdu->images->data[i] = image;
     353    if (imageType) {
     354        hdu->images = psArrayAlloc(numReadouts);
     355        for (int i = 0; i < numReadouts; i++) {
     356            psImage *image = psImageAlloc(xSize, ySize, imageType);
     357            psImageInit(image, 0.0);
     358            hdu->images->data[i] = image;
     359        }
     360    }
     361    if (maskType) {
     362        hdu->masks = psArrayAlloc(numReadouts);
     363        for (int i = 0; i < numReadouts; i++) {
     364            psImage *mask = psImageAlloc(xSize, ySize, maskType);
     365            psImageInit(mask, 0);
     366            hdu->masks->data[i] = mask;
     367        }
     368    }
     369    if (weightType) {
     370        hdu->weights = psArrayAlloc(numReadouts);
     371        for (int i = 0; i < numReadouts; i++) {
     372            psImage *weight = psImageAlloc(xSize, ySize, weightType);
     373            psImageInit(weight, 0.0);
     374            hdu->weights->data[i] = weight;
     375        }
    300376    }
    301377
     
    319395            psArray *readouts = cell->readouts; // Array of readouts
    320396            psArray *hduImages = hdu->images; // Array of images in the HDU
     397            psArray *hduMasks = hdu->masks; // Array of masks in the HDU
     398            psArray *hduWeights = hdu->weights; // Array of weights in the HDU
    321399            for (int i = 0; i < readouts->n; i++) {
    322400                pmReadout *readout = readouts->data[i]; // The readout of interest
     
    324402                    continue;
    325403                }
    326                 psImage *image = readout->image; // The image pixels
    327                 psImage *hduImage = hduImages->data[i]; // The HDU image of interest
    328 
    329                 if (image->numCols != trimsec->x1 - trimsec->x0 ||
    330                         image->numRows != trimsec->y1 - trimsec->y0) {
    331                     psString trimsecString = psRegionToString(*trimsec);
    332                     psLogMsg(__func__, PS_LOG_WARN, "Image size (%dx%d) does not match CELL.TRIMSEC (%s).\n",
    333                              image->numCols, image->numRows, trimsecString);
    334                     psFree(trimsecString);
    335                 }
    336                 psImageOverlaySection(hduImage, image, trimsec->x0, trimsec->y0, "=");
    337 
    338                 // Reference the HDU version, so that subsequent changes will touch the HDU
    339                 psFree(image);
    340                 readout->image = psImageSubset(hduImage, *trimsec);
     404
     405                if (readout->image) {
     406                    pasteImage(hduImages->data[i], &readout->image, trimsec);
     407                }
     408                if (readout->mask) {
     409                    pasteImage(hduMasks->data[i], &readout->mask, trimsec);
     410                }
     411                if (readout->weight) {
     412                    pasteImage(hduWeights->data[i], &readout->weight, trimsec);
     413                }
    341414
    342415                if (biassecs->n != readout->bias->n) {
     
    351424                while ((bias = psListGetAndIncrement(biasIter)) &&
    352425                        (biassec = psListGetAndIncrement(biassecsIter))) {
    353                     if (bias->numCols != biassec->x1 - biassec->x0 ||
    354                             bias->numRows != biassec->y1 - biassec->y0) {
    355                         psString biassecString = psRegionToString(*biassec);
    356                         psLogMsg(__func__, PS_LOG_WARN, "Bias size (%dx%d) does not match CELL.BIASSEC (%s)."
    357                                  "\n", bias->numCols, bias->numRows, biassecString);
    358                         psFree(biassecString);
    359                     }
    360 
    361                     psImageOverlaySection(hduImage, bias, biassec->x0, biassec->y0, "=");
    362 
    363                     // Reference the HDU version, so that subsequent changes will touch the HDU
    364                     bias = psImageSubset(hduImage, *biassec);
     426                    pasteImage(hduImages->data[i], &bias, biassec);
    365427                    psListAdd(newBias, PS_LIST_TAIL, bias);
    366428                }
Note: See TracChangeset for help on using the changeset viewer.