IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

Removing old code that was #ifdef-ed out

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.