IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6840


Ignore:
Timestamp:
Apr 11, 2006, 6:11:54 PM (20 years ago)
Author:
Paul Price
Message:

Removing old code that was #ifdef-ed out

Location:
branches/rel10_ifa/psModules/src/astrom
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/rel10_ifa/psModules/src/astrom/pmFPARead.c

    r6720 r6840  
    156156}
    157157
    158 
    159 #if 0
    160 // Don't know that we need these
    161 
    162 // Read the PHU into the nominated HDU
    163 static bool readPHU(pmHDU *hdu,         // HDU to read into
    164                     psFits *fits        // FITS file from which to read
    165                    )
    166 {
    167     if (! hdu || ! hdu->phu) {
    168         return false;                   // Nothing to see here, move along
    169     }
    170     if (hdu->header) {
    171         return true;                    // Already something to see here, no need to gawk at it...  (;
    172     }
    173     // Read the PHU
    174     psFitsMoveExtNum(fits, 0, false);
    175     hdu->header = psFitsReadHeader(hdu->header, fits);
    176     return true;
    177 }
    178 
    179 // Read the PHU into a cell
    180 bool pmCellReadPHU(pmCell *cell,        // Cell to read into
    181                    psFits *fits         // FITS file from which to read
    182                   )
    183 {
    184     return readPHU(cell->hdu, fits) || readPHU(cell->parent->hdu, fits) ||
    185            readPHU(cell->parent->parent->hdu, fits);
    186 
    187 }
    188 
    189 // Read the PHU into a chip
    190 bool pmChipReadPHU(pmChip *chip,        // Chip to read into
    191                    psFits *fits         // FITS file from which to read
    192                   )
    193 {
    194     return readPHU(chip->hdu, fits) || readPHU(chip->parent->hdu, fits);
    195 }
    196 
    197 // Read the PHU into an FPA
    198 bool pmFPAReadPHU(pmFPA *fpa,           // FPA to read into
    199                   psFits *fits          // FITS file from which to read
    200                  )
    201 {
    202     return readPHU(fpa->hdu, fits);
    203 }
    204 
    205 // Translate a name from the configuration file, containing something like "%a_%d" to a real value
    206 psString p_pmFPATranslateName(const psString name, // The name to translate
    207                               const pmCell *cell // The cell for which to translate
    208                              )
    209 {
    210     // %a is the FPA.NAME
    211     // %b is the CHIP.NAME
    212     // %c is the CELL.NAME
    213     // %d is the chip number
    214     // %e if the cell number
    215     // %f is the extension name
    216 
    217     psString translation = psMemIncrRefCounter(name); // The translated string
    218     char *temp = NULL;                  // Temporary string
    219 
    220     pmChip *chip = cell->parent;        // Chip of interest
    221     pmFPA *fpa = chip->parent;          // FPA of interest
    222 
    223     // FPA.NAME
    224     if ((temp = strstr(translation, "%a"))) {
    225         // This is not particularly friendly to the memory allocation, but the cache should make it OK,
    226         // and there's not much of it anyway...
    227         psFree(translation);
    228         translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
    229         psString fpaName = psMetadataLookupStr(NULL, fpa->concepts, "FPA.NAME"); // The value of FPA.NAME
    230         psStringAppend(&translation, "%s%s", fpaName, temp + 2);
    231         // So "translation" now contains the first part, the replaced string, and the last part.
    232     }
    233 
    234     // CHIP.NAME
    235     if ((temp = strstr(translation, "%b"))) {
    236         // This is not particularly friendly to the memory allocation, but the cache should make it OK,
    237         // and there's not much of it anyway...
    238         psFree(translation);
    239         translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
    240         psString chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME"); // CHIP.NAME's value
    241         psStringAppend(&translation, "%s%s", chipName, temp + 2);
    242         // So "translation" now contains the first part, the replaced string, and the last part.
    243     }
    244 
    245     // CELL.NAME
    246     if ((temp = strstr(translation, "%c"))) {
    247         // This is not particularly friendly to the memory allocation, but the cache should make it OK,
    248         // and there's not much of it anyway...
    249         psFree(translation);
    250         translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
    251         psString cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // CELL.NAME's value
    252         psStringAppend(&translation, "%s%s", cellName, temp + 2);
    253         // So "translation" now contains the first part, the replaced string, and the last part.
    254     }
    255 
    256     // Chip number
    257     if ((temp = strstr(translation, "%d"))) {
    258         // This is not particularly friendly to the memory allocation, but the cache should make it OK,
    259         // and there's not much of it anyway...
    260         psFree(translation);
    261         translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
    262         // Search for the pointer to get the chip number
    263         int chipNum = -1;
    264         psArray *chips = fpa->chips;    // The array of chips
    265         for (int i = 0; i < chips->n && chipNum < 0; i++) {
    266             if (chips->data[i] == chip) {
    267                 chipNum = i;
    268             }
    269         }
    270         if (chipNum < 0) {
    271             psError(PS_ERR_IO, true, "Unable to find chip to get name: %s\n", name);
    272             // Try to muddle on by leaving the number out
    273             psStringAppend(&translation, "%s", temp + 2);
    274         } else {
    275             psStringAppend(&translation, "%d%s", chipNum, temp + 2);
    276         }
    277         // So "translation" now contains the first part, the replaced string, and the last part.
    278     }
    279 
    280     // Cell number
    281     if ((temp = strstr(translation, "%e"))) {
    282         // This is not particularly friendly to the memory allocation, but the cache should make it OK,
    283         // and there's not much of it anyway...
    284         psFree(translation);
    285         translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
    286         // Search for the pointer to get the cell number
    287         int cellNum = -1;
    288         psArray *cells = chip->cells;   // The array of cells
    289         for (int i = 0; i < cells->n && cellNum < 0; i++) {
    290             if (cells->data[i] == cell) {
    291                 cellNum = i;
    292             }
    293         }
    294         if (cellNum < 0) {
    295             psError(PS_ERR_IO, true, "Unable to find cell to get name: %s\n", name);
    296             // Try to muddle on by leaving the number out
    297             psStringAppend(&translation, "%s", temp + 2);
    298         } else {
    299             psStringAppend(&translation, "%d%s", cellNum, temp + 2);
    300         }
    301         // So "translation" now contains the first part, the replaced string, and the last part.
    302     }
    303 
    304     // Extension name
    305     if ((temp = strstr(translation, "%f"))) {
    306         // This is not particularly friendly to the memory allocation, but the cache should make it OK,
    307         // and there's not much of it anyway...
    308         psFree(translation);
    309         translation = psStringNCopy(name, strlen(name) - strlen(temp)); // Copy first part of string
    310         const char *extname = NULL;
    311         if (cell->hdu) {
    312             extname = cell->hdu->extname;
    313         } else if (chip->hdu) {
    314             extname = chip->hdu->extname;
    315         } else if (fpa->hdu) {
    316             extname = fpa->hdu->extname;
    317         }
    318         psStringAppend(&translation, "%s%s", extname, temp + 2);
    319         // So "translation" now contains the first part, the replaced string, and the last part.
    320     }
    321 
    322     return translation;
    323 }
    324 
    325 // Get filename and extension out of "somefile.fits:ext"
    326 psString p_pmFPATranslateFileExt(psString *extName, // Extension name, to be returned
    327                                  const psString name, // The string to be translated and then parsed
    328                                  const pmCell *cell // The cell
    329                                 )
    330 {
    331     psString filenameExt = p_pmFPATranslateName(name, cell);
    332     const char *colon = strchr(filenameExt, ':'); // Pointer to a colon in the filename-extn
    333     psString filename = NULL;           // The filename
    334     if (extName && *extName) {
    335         psFree(*extName);
    336     }
    337     if (extName) {
    338         *extName = NULL;
    339     }
    340 
    341     if (colon) {
    342         filename = psStringNCopy(filenameExt, strlen(filenameExt) - strlen(colon));
    343         if (strlen(colon) > 1) {
    344             if (extName) {
    345                 *extName = psStringCopy(colon + 1);
    346             }
    347         }
    348     } else {
    349         filename = psMemIncrRefCounter(filenameExt);
    350     }
    351 
    352     return filename;
    353 }
    354 
    355 // Read a mask into the FPA
    356 bool pmFPAReadMask(pmFPA *fpa,          // FPA to read into
    357                    psFits *source       // Source FITS file (for the original data)
    358                   )
    359 {
    360     const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    361     bool mdok = false;                  // Status of MD lookup
    362 
    363     // Get the required information from the camera configuration
    364     psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    365     if (! mdok || ! supps) {
    366         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    367         return false;
    368     }
    369     psString sourceType = psMetadataLookupStr(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE
    370     if (! mdok || strlen(sourceType) <= 0) {
    371         psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
    372                 "configuration!\n");
    373         return false;
    374     }
    375     psString name = psMetadataLookupStr(&mdok, supps, "MASK.NAME"); // Name of mask
    376     if (! mdok || strlen(sourceType) <= 0) {
    377         psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
    378                 "configuration!\n");
    379         return false;
    380     }
    381 
    382     // Go through the FPA to each cell/readout to get the mask
    383     p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the mask
    384     psArray *chips = fpa->chips;        // Array of chips
    385     for (int chipNum = 0; chipNum < chips->n; chipNum++) {
    386         pmChip *chip = chips->data[chipNum]; // The current chip of interest
    387         if (chip->process && !chip->data_exists) {
    388             if (chip->hdu) {
    389                 hdu = chip->hdu;
    390             }
    391             psArray *cells = chip->cells;       // Array of cells
    392             for (int cellNum = 0; cellNum < cells->n; cellNum++) {
    393                 pmCell *cell = cells->data[cellNum]; // The current cell of interest
    394                 if (cell->process && !cell->data_exists) {
    395                     if (cell->hdu) {
    396                         hdu = cell->hdu;
    397                     }
    398 
    399                     // Now, need to find out where to get the pixels
    400                     psFits *maskSource = psMemIncrRefCounter(source); // Source of mask image
    401                     if (strcasecmp(sourceType, "FILE") == 0) {
    402                         // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
    403                         psString extname = NULL; // Extension name
    404                         psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename
    405 
    406                         psFree(maskSource);
    407                         maskSource = psFitsOpen(filename, "r");
    408                         if (extname) {
    409                             if (! psFitsMoveExtName(maskSource, extname)) {
    410                                 psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read mask.\n",
    411                                          extname);
    412                                 return false;
    413                             }
    414                         }
    415                         psFree(filename);
    416                         psFree(extname);
    417                     } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
    418                         // Source is an extension in the original file
    419                         psString extname = p_pmFPATranslateName(name, cell);
    420                         psFitsMoveExtName(maskSource, extname);
    421                     }
    422 
    423                     // We've arrived where the pixels are.  Now we need to read them in.
    424                     psMetadata *header = psFitsReadHeader(NULL, maskSource); // The header
    425                     bool mdStatus = false;
    426                     int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS");
    427                     if (!mdStatus) {
    428                         psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for "
    429                                  "mask (%s)!\n", name);
    430                     }
    431                     if (nAxis != 2 && nAxis != 3) {
    432                         psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into "
    433                                  "a single image anyway.\n");
    434                     }
    435 
    436                     int numPlanes = 1;  // Number of planes
    437                     if (nAxis == 3) {
    438                         numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3");
    439                         if (!mdStatus) {
    440                             psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n");
    441                             // Try to proceed by taking only the first plane
    442                             numPlanes = 1;
    443                         }
    444                         if (numPlanes != 1 && numPlanes != cell->readouts->n) {
    445                             psError(PS_ERR_IO, false, "Number of masks (%d) does not match number of "
    446                                     "readouts (%d)\n", numPlanes, cell->readouts->n);
    447                             // Try to proceed by taking only the first plane
    448                             numPlanes = 1;
    449                         }
    450                     }
    451 
    452                     hdu->masks = psArrayAlloc(hdu->images->n);
    453 
    454                     // Read each plane into the array
    455                     psArray *readouts = cell->readouts; // The array of readouts
    456                     for (int i = 0; i < hdu->masks->n; i++) {
    457                         psImage *mask = NULL; // The mask to be added
    458                         if (i < numPlanes) {
    459                             // Read the mask from the file
    460                             psTrace(__func__, 9, "Reading plane %d\n", i);
    461                             psRegion region = {0, 0, 0, 0};
    462                             mask = psFitsReadImage(NULL, maskSource, region, i);
    463                         } else {
    464                             // One mask in the file is provided for all planes in the original image
    465                             psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i);
    466                             psImage *original = hdu->masks->data[0];
    467                             mask = psImageCopy(NULL, original, original->type.type);
    468                         }
    469                         hdu->masks->data[0] = mask;
    470                         pmReadout *readout = readouts->data[i];
    471                         readout->mask = mask;
    472                         // Check the dimensions
    473                         // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size?
    474                         if (mask->numCols < readout->image->numCols ||
    475                                 mask->numRows < readout->image->numRows) {
    476                             psError(PS_ERR_IO, false, "Mask size (%dx%d) not compatible with image (%dx%d)\n",
    477                                     mask->numCols, mask->numRows, readout->image->numCols,
    478                                     readout->image->numRows);
    479                             return false;
    480                         }
    481                     } // Iterating over readouts
    482                     psFree(maskSource);
    483                 } // Valid cells
    484             } // Iterating over cells
    485         } // Valid chips
    486     } // Iterating over chips
    487 
    488     return true;
    489 }
    490 
    491 
    492 // Read a mask into the FPA
    493 // This is just a copy of the above pmFPAReadMask, replacing "mask" with "weight" throughout.
    494 bool pmFPAReadWeight(pmFPA *fpa,        // FPA to read into
    495                      psFits *source     // Source FITS file (for the original data)
    496                     )
    497 {
    498     const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    499     bool mdok = false;                  // Status of MD lookup
    500 
    501     // Get the required information from the camera configuration
    502     psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    503     if (! mdok || ! supps) {
    504         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    505         return false;
    506     }
    507     psString sourceType = psMetadataLookupStr(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE
    508     if (! mdok || strlen(sourceType) <= 0) {
    509         psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
    510                 "configuration!\n");
    511         return false;
    512     }
    513     psString name = psMetadataLookupStr(&mdok, supps, "WEIGHT.NAME"); // Name of weight
    514     if (! mdok || strlen(sourceType) <= 0) {
    515         psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
    516                 "configuration!\n");
    517         return false;
    518     }
    519 
    520     // Go through the FPA to each cell/readout to get the weight
    521     p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the weight
    522     psArray *chips = fpa->chips;        // Array of chips
    523     for (int chipNum = 0; chipNum < chips->n; chipNum++) {
    524         pmChip *chip = chips->data[chipNum]; // The current chip of interest
    525         if (chip->process && !chip->data_exists) {
    526             if (chip->hdu) {
    527                 hdu = chip->hdu;
    528             }
    529             psArray *cells = chip->cells;       // Array of cells
    530             for (int cellNum = 0; cellNum < cells->n; cellNum++) {
    531                 pmCell *cell = cells->data[cellNum]; // The current cell of interest
    532                 if (cell->process && !cell->data_exists) {
    533                     if (cell->hdu) {
    534                         hdu = cell->hdu;
    535                     }
    536 
    537                     // Now, need to find out where to get the pixels
    538                     psFits *weightSource = psMemIncrRefCounter(source); // Source of weight image
    539                     if (strcasecmp(sourceType, "FILE") == 0) {
    540                         // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
    541                         psString extname = NULL; // Extension name
    542                         psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename
    543 
    544                         psFree(weightSource);
    545                         weightSource = psFitsOpen(filename, "r");
    546                         if (extname) {
    547                             if (! psFitsMoveExtName(weightSource, extname)) {
    548                                 psLogMsg(__func__, PS_LOG_WARN, "Unable to find extension %s to read "
    549                                          "weight.\n", extname);
    550                                 return false;
    551                             }
    552                         }
    553                         psFree(filename);
    554                         psFree(extname);
    555                     } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
    556                         // Source is an extension in the original file
    557                         psString extname = p_pmFPATranslateName(name, cell);
    558                         psFitsMoveExtName(weightSource, extname);
    559                     }
    560 
    561                     // We've arrived where the pixels are.  Now we need to read them in.
    562                     psMetadata *header = psFitsReadHeader(NULL, weightSource); // The header
    563                     bool mdStatus = false;
    564                     int nAxis = psMetadataLookupS32(&mdStatus, header, "NAXIS");
    565                     if (!mdStatus) {
    566                         psLogMsg(__func__, PS_LOG_WARN, "There is no NAXIS keyword in the FITS header for "
    567                                  "weight (%s)!\n", name);
    568                     }
    569                     if (nAxis != 2 && nAxis != 3) {
    570                         psLogMsg(__func__, PS_LOG_WARN, "Image is not 2- or 3-dimensional --- reading into "
    571                                  "a single image anyway.\n");
    572                     }
    573 
    574                     int numPlanes = 1;  // Number of planes
    575                     if (nAxis == 3) {
    576                         numPlanes = psMetadataLookupS32(&mdStatus, header, "NAXIS3");
    577                         if (!mdStatus) {
    578                             psError(PS_ERR_IO, false, "Unable to read NAXIS3 for 3-dimensional image!\n");
    579                             // Try to proceed by taking only the first plane
    580                             numPlanes = 1;
    581                         }
    582                         if (numPlanes != 1 && numPlanes != cell->readouts->n) {
    583                             psError(PS_ERR_IO, false, "Number of weights (%d) does not match number of "
    584                                     "readouts (%d)\n", numPlanes, cell->readouts->n);
    585                             // Try to proceed by taking only the first plane
    586                             numPlanes = 1;
    587                         }
    588                     }
    589 
    590                     hdu->weights = psArrayAlloc(hdu->images->n);
    591 
    592                     // Read each plane into the array
    593                     psArray *readouts = cell->readouts; // The array of readouts
    594                     for (int i = 0; i < hdu->weights->n; i++) {
    595                         psImage *weight = NULL; // The weight to be added
    596                         if (i < numPlanes) {
    597                             // Read the weight from the file
    598                             psTrace(__func__, 9, "Reading plane %d\n", i);
    599                             psRegion region = {0, 0, 0, 0};
    600                             weight = psFitsReadImage(NULL, weightSource, region, i);
    601                         } else {
    602                             // One weight in the file is provided for all planes in the original image
    603                             psTrace(__func__, 9, "Copying plane 0 into plane %d\n", i);
    604                             psImage *original = hdu->weights->data[0];
    605                             weight = psImageCopy(NULL, original, original->type.type);
    606                         }
    607                         hdu->weights->data[0] = weight;
    608                         pmReadout *readout = readouts->data[i];
    609                         readout->weight = weight;
    610                         // Check the dimensions
    611                         // XXX: Perhaps this check should be against the CELL.TRIMSEC, not the image size?
    612                         if (weight->numCols < readout->image->numCols ||
    613                                 weight->numRows < readout->image->numRows) {
    614                             psError(PS_ERR_IO, false, "Weight size (%dx%d) not compatible with image "
    615                                     "(%dx%d)\n", weight->numCols, weight->numRows, readout->image->numCols,
    616                                     readout->image->numRows);
    617                             return false;
    618                         }
    619                     } // Iterating over readouts
    620                     psFree(weightSource);
    621                 } // Valid cells
    622             } // Iterating over cells
    623         } // Valid chips
    624     } // Iterating over chips
    625 
    626     return true;
    627 }
    628 
    629 
    630 #endif
  • branches/rel10_ifa/psModules/src/astrom/pmFPAWrite.c

    r6734 r6840  
    7777}
    7878
    79 
    80 
    81 
    82 #if 0
    83 static bool writeHDU(psFits *fits,      // FITS file to which to write
    84                      pmHDU *hdu         // Pixel data to write
    85                     )
    86 {
    87     bool status = true;                 // Status of write, to return
    88     for (int i = 0; i < hdu->images->n; i++) {
    89         status &= psFitsWriteImage(fits, hdu->header, hdu->images->data[i], i);
    90         // XXX: Insert here the writing on mask and weight images
    91     }
    92 
    93     return status;
    94 }
    95 
    96 
    97 bool pmFPAWrite(psFits *fits,           // FITS file to which to write
    98                 pmFPA *fpa,             // FPA to write
    99                 psDB *db                // Database to update
    100                )
    101 {
    102     bool status = true;                 // Status of writing, to return
    103 
    104     psTrace(__func__, 1, "Writing FPA...\n");
    105 
    106     psTrace(__func__, 7, "Outgesting FPA concepts...\n");
    107     pmConceptsWriteFPA(fpa, db);
    108 
    109     // Write the primary header
    110     if (fpa->phu) {
    111         status &= psFitsMoveExtNum(fits, 0, false);
    112         status &= psFitsWriteHeader(fpa->phu, fits);
    113     }
    114 
    115     psArray *chips = fpa->chips;        // Array of component chips
    116     for (int i = 0; i < chips->n; i++) {
    117         pmChip *chip = chips->data[i];  // The component chip
    118         if (chip && chip->exists && chip->process) {
    119             psTrace(__func__, 1, "Writing out chip %d...\n", i);
    120 
    121             pmConceptsWriteChip(chip, db);
    122 
    123             psArray *cells = chip->cells;       // Array of component cells
    124             for (int j = 0; j < cells->n; j++) {
    125                 pmCell *cell = cells->data[j]; // The component cell
    126                 if (cell && cell->exists && cell->process) {
    127                     psTrace(__func__, 2, "Writing out cell, %d...\n", j);
    128 
    129                     pmConceptsWriteCell(cell, db);
    130 
    131                     if (cell->hdu && strlen(cell->hdu->extname) > 0) {
    132                         status &= writeHDU(fits, cell->hdu);
    133                     }
    134                 }
    135             }
    136 
    137             if (chip->hdu && strlen(chip->hdu->extname) > 0) {
    138                 status &= writeHDU(fits, chip->hdu);
    139             }
    140         }
    141 
    142     }
    143 
    144     if (fpa->hdu && strlen(fpa->hdu->extname) > 0) {
    145         status &= writeHDU(fits, fpa->hdu);
    146     }
    147 
    148     psTrace(__func__, 1, "Done writing FPA...\n");
    149 
    150     return status;
    151 }
    152 
    153 
    154 bool pmFPAWriteMask(pmFPA *fpa,         // FPA containing mask to write
    155                     psFits *fits        // FITS file for image
    156                    )
    157 {
    158     const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    159     bool mdok = false;                  // Status of MD lookup
    160 
    161     // Get the required information from the camera configuration
    162     psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    163     if (! mdok || ! supps) {
    164         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    165         return false;
    166     }
    167     psString sourceType = psMetadataLookupStr(&mdok, supps, "MASK.SOURCE"); // Type of source: EXT | FILE
    168     if (! mdok || strlen(sourceType) <= 0) {
    169         psError(PS_ERR_IO, false, "Unable to find MASK.SOURCE in SUPPLEMENTARY section of camera "
    170                 "configuration!\n");
    171         return false;
    172     }
    173     psString name = psMetadataLookupStr(&mdok, supps, "MASK.NAME"); // Name of mask
    174     if (! mdok || strlen(sourceType) <= 0) {
    175         psError(PS_ERR_IO, false, "Unable to find MASK.NAME in SUPPLEMENTARY section of camera "
    176                 "configuration!\n");
    177         return false;
    178     }
    179 
    180     // Go through the FPA to each cell/readout to get the mask
    181     p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the mask
    182     psArray *chips = fpa->chips;        // Array of chips
    183     for (int chipNum = 0; chipNum < chips->n; chipNum++) {
    184         pmChip *chip = chips->data[chipNum]; // The current chip of interest
    185         if (chip->exists && chip->process) {
    186             if (chip->hdu) {
    187                 hdu = chip->hdu;
    188             }
    189             psArray *cells = chip->cells;       // Array of cells
    190             for (int cellNum = 0; cellNum < cells->n; cellNum++) {
    191                 pmCell *cell = cells->data[cellNum]; // The current cell of interest
    192                 if (cell->exists && cell->process) {
    193                     if (cell->hdu) {
    194                         hdu = cell->hdu;
    195                     }
    196 
    197                     // Now, need to find out where to write the pixels
    198                     psFits *maskDest = psMemIncrRefCounter(fits); // Destination of mask image
    199                     psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
    200                     if (strcasecmp(sourceType, "FILE") == 0) {
    201                         // Source is a file (with optional extension, e.g., "myMaskFile.fits:thisExt"
    202                         psString extname = NULL; // Extension name
    203                         psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename
    204                         psFree(maskDest);
    205                         maskDest = psFitsOpen(filename, "rw");
    206                         if (extname) {
    207                             psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    208                         }
    209                         psFree(filename);
    210                         psFree(extname);
    211                     } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
    212                         // Source is an extension in the original file
    213                         psString extname = p_pmFPATranslateName(name, cell);
    214                         psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    215                         psFree(extname);
    216                     }
    217 
    218                     // We've arrived where the pixels are.  Now we need to write them out.
    219                     psArray *readouts = cell->readouts; // The array of readouts
    220                     for (int readNum = 0; readNum < readouts->n; readNum++) {
    221                         pmReadout *readout = readouts->data[readNum]; // The readout of interest
    222                         if (! readout->mask) {
    223                             psLogMsg(__func__, PS_LOG_WARN, "No mask to write out in %d,%d,%d\n",
    224                                      chipNum, cellNum, readNum);
    225                         } else {
    226                             // XXX: Need to add the extname to the existing header
    227                             if (! psFitsWriteImage(maskDest, header, readout->mask, readNum)) {
    228                                 psError(PS_ERR_IO, false, "Unable to write mask plane %d in extension %s\n",
    229                                         readNum, hdu->extname);
    230                                 return false;
    231                             }
    232                         }
    233                     } // Iterating over readouts
    234                     psFree(header);
    235                     psFree(maskDest);
    236                 } // Valid cells
    237             } // Iterating over cells
    238         } // Valid chips
    239     } // Iterating over chips
    240 
    241     return true;
    242 }
    243 
    244 
    245 bool pmFPAWriteWeight(pmFPA *fpa,       // FPA containing mask to write
    246                       psFits *fits      // FITS file for image
    247                      )
    248 {
    249     const psMetadata *camera = fpa->camera; // Camera configuration for FPA
    250     bool mdok = false;                  // Status of MD lookup
    251 
    252     // Get the required information from the camera configuration
    253     psMetadata *supps = psMetadataLookupMD(&mdok, camera, "SUPPLEMENTARY"); // Rules for supplementary data
    254     if (! mdok || ! supps) {
    255         psError(PS_ERR_IO, false, "Unable to find SUPPLEMENTARY in camera configuration!\n");
    256         return false;
    257     }
    258     psString sourceType = psMetadataLookupStr(&mdok, supps, "WEIGHT.SOURCE"); // Type of source: EXT | FILE
    259     if (! mdok || strlen(sourceType) <= 0) {
    260         psError(PS_ERR_IO, false, "Unable to find WEIGHT.SOURCE in SUPPLEMENTARY section of camera "
    261                 "configuration!\n");
    262         return false;
    263     }
    264     psString name = psMetadataLookupStr(&mdok, supps, "WEIGHT.NAME"); // Name of weight
    265     if (! mdok || strlen(sourceType) <= 0) {
    266         psError(PS_ERR_IO, false, "Unable to find WEIGHT.NAME in SUPPLEMENTARY section of camera "
    267                 "configuration!\n");
    268         return false;
    269     }
    270 
    271     // Go through the FPA to each cell/readout to get the weight
    272     p_pmHDU *hdu = fpa->hdu;            // The HDU into which we will read the weight
    273     psArray *chips = fpa->chips;        // Array of chips
    274     for (int chipNum = 0; chipNum < chips->n; chipNum++) {
    275         pmChip *chip = chips->data[chipNum]; // The current chip of interest
    276         if (chip->exists && chip->process) {
    277             if (chip->hdu) {
    278                 hdu = chip->hdu;
    279             }
    280             psArray *cells = chip->cells;       // Array of cells
    281             for (int cellNum = 0; cellNum < cells->n; cellNum++) {
    282                 pmCell *cell = cells->data[cellNum]; // The current cell of interest
    283                 if (cell->exists && cell->process) {
    284                     if (cell->hdu) {
    285                         hdu = cell->hdu;
    286                     }
    287 
    288                     // Now, need to find out where to write the pixels
    289                     psFits *weightDest = psMemIncrRefCounter(fits); // Destination of weight image
    290                     psMetadata *header = psMetadataAlloc(); // A dummy header, containing the extension name
    291                     if (strcasecmp(sourceType, "FILE") == 0) {
    292                         // Source is a file (with optional extension, e.g., "myWeightFile.fits:thisExt"
    293                         psString extname = NULL; // Extension name
    294                         psString filename = p_pmFPATranslateFileExt(&extname, name, cell); // The filename
    295 
    296                         psFree(weightDest);
    297                         weightDest = psFitsOpen(filename, "rw");
    298                         if (extname) {
    299                             psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    300                         }
    301                         psFree(filename);
    302                         psFree(extname);
    303                     } else if (strncasecmp(sourceType, "EXT", 3) == 0) {
    304                         // Source is an extension in the original file
    305                         psString extname = p_pmFPATranslateName(name, cell);
    306                         psMetadataAddStr(header, PS_LIST_TAIL, "EXTNAME", 0, "Extension name", extname);
    307                         psFree(extname);
    308                     }
    309 
    310                     // We've arrived where the pixels are.  Now we need to write them out.
    311                     psArray *readouts = cell->readouts; // The array of readouts
    312                     for (int readNum = 0; readNum < readouts->n; readNum++) {
    313                         pmReadout *readout = readouts->data[readNum]; // The readout of interest
    314                         if (! readout->weight) {
    315                             psLogMsg(__func__, PS_LOG_WARN, "No weight image to write out in %d,%d,%d\n",
    316                                      chipNum, cellNum, readNum);
    317                         } else {
    318                             if (! psFitsWriteImage(weightDest, header, readout->weight, readNum)) {
    319                                 psError(PS_ERR_IO, false, "Unable to write weight plane %d in extension %s\n",
    320                                         readNum, hdu->extname);
    321                                 return false;
    322                             }
    323                         }
    324                     } // Iterating over readouts
    325                     psFree(header);
    326                     psFree(weightDest);
    327                 } // Valid cells
    328             } // Iterating over cells
    329         } // Valid chips
    330     } // Iterating over chips
    331 
    332     return true;
    333 }
    334 #endif
Note: See TracChangeset for help on using the changeset viewer.