IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 16, 2006, 3:47:44 PM (20 years ago)
Author:
Paul Price
Message:

PHU=NONE (i.e., megacam split) now working, with concept ingest; changed definition of pmFPAAddSource to return a pmFPAview

File:
1 edited

Legend:

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

    r6589 r6618  
    11#include <stdio.h>
    2 #include <strings.h>
     2#include <assert.h>
     3#include <string.h>
    34#include "pslib.h"
     5#include "psMetadataItemParse.h"
    46
    57#include "pmFPA.h"
    68#include "pmConcepts.h"
    79#include "pmFPAConstruct.h"
     10#include "pmFPAview.h"
    811
    912//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    3134}
    3235
    33 // Find a chip by name
    34 static pmChip *findChip(pmFPA *fpa, // FPA in which to find the chip
    35                         const char *name // Name of the chip
    36                        )
     36// Find a chip by name; return the index
     37static int findChip(pmFPA *fpa, // FPA in which to find the chip
     38                    const char *name // Name of the chip
     39                   )
    3740{
    3841    psArray *chips = fpa->chips;    // Array of chips
     
    4144        psString testName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME"); // Name of this chip
    4245        if (strcmp(name, testName) == 0) {
    43             return chip;
     46            return i;
    4447        }
    4548    }
    4649
    4750    psError(PS_ERR_IO, true, "Unable to find chip %s\n", name);
    48     return NULL;
    49 }
    50 
    51 // Find a cell by name
    52 static pmCell *findCell(pmChip *chip, // Chip in which to find the cell
    53                         const char *name // Name of the cell
    54                        )
     51    return -1;
     52}
     53
     54// Find a cell by name; return the index
     55static int findCell(pmChip *chip, // Chip in which to find the cell
     56                    const char *name // Name of the cell
     57                   )
    5558{
    5659    psArray *cells = chip->cells;    // Array of cells
     
    5962        psString testName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // Name of this cell
    6063        if (strcmp(name, testName) == 0) {
    61             return cell;
     64            return i;
    6265        }
    6366    }
    6467
    6568    psError(PS_ERR_IO, true, "Unable to find cell %s\n", name);
    66     return NULL;
     69    return -1;
    6770}
    6871
     
    100103}
    101104
    102 // Get the chip name from a primary header lookup
    103 static psString chipNameFromHeader(const psMetadata *format, // FORMAT within the camera format description
    104                                    const psMetadata *header // Primary header
    105                                   )
     105// Get the name of a PHU chip or cell from the header
     106static psString phuNameFromHeader(const char *name, // The name to lookup: "CELL.NAME" or "CHIP.NAME"
     107                                  const psMetadata *format, // FORMAT within the camera format description
     108                                  const psMetadata *header // Primary header
     109                                 )
    106110{
    107111    bool mdok = true;                   // Result of MD lookup
    108     psString chipNameHeader = psMetadataLookupStr(&mdok, format, "CHIP.NAME");
    109     if (!mdok || strlen(chipNameHeader) == 0) {
    110         psError(PS_ERR_IO, false, "Unable to find CHIP.NAME in the FORMAT.\n");
     112    psString keyword = psMetadataLookupStr(&mdok, format, name);
     113    if (!mdok || strlen(keyword) == 0) {
     114        psError(PS_ERR_IO, false, "Unable to find %s in the FORMAT.\n", name);
    111115        return false;
    112116    }
    113     psString chipName = psMetadataLookupStr(&mdok, header, chipNameHeader);
    114     if (!chipName || strlen(chipName) == 0) {
    115         psError(PS_ERR_IO, false, "Unable to find %s in primary header to identify chip name.\n", chipName);
     117    psString result = psMetadataLookupStr(&mdok, header, keyword);
     118    if (!mdok || strlen(result) == 0) {
     119        psError(PS_ERR_IO, false, "Unable to find %s in primary header to identify %s.\n", keyword, name);
    116120        return NULL;
    117121    }
    118122
    119     return chipName;
     123    return result;
    120124}
    121125
     
    159163
    160164// Add an input file to the FPA
    161 bool pmFPAAddSource(pmFPA *fpa,         // FPA to which to add
    162                     pmChip *chip,       // Chip to which to add, or NULL
    163                     pmCell *cell,       // Cell to which to add, or NULL
    164                     psMetadata *phu,    // Primary header of file
    165                     psMetadata *format // Format of file
    166                    )
    167 {
    168     // Just in case someone's playing silly billy...
    169     if (cell && !chip) {
    170         chip = cell->parent;
    171     }
    172     if (chip && !fpa) {
    173         fpa = chip->parent;
    174     }
     165pmFPAview *pmFPAAddSource(pmFPA *fpa, // The FPA
     166                          psMetadata *phu, // Primary header of file
     167                          psMetadata *format // Format of file
     168                         )
     169{
     170    assert(fpa);
     171    assert(phu);
     172    assert(format);
     173
     174    pmChip *chip = NULL;                // The chip, if any one in particular
     175    pmCell *cell = NULL;                // The cell, if any one in particular
     176    psString phuChipName = NULL;        // Name of the chip from the PHU
    175177
    176178    bool mdok = true;                   // Status from metadata lookups
     
    178180    if (!mdok || !formatSpec) {
    179181        psError(PS_ERR_IO, false, "Unable to find FORMAT in the camera format configuration.\n");
    180         return false;
    181     }
     182        return NULL;
     183    }
     184
     185    // Check the name of the FPA
     186    const char *nameKeyword = psMetadataLookupStr(&mdok, formatSpec, "FPA.NAME"); // Keyword that gives name
     187    if (!mdok || strlen(nameKeyword) == 0) {
     188        psError(PS_ERR_IO, false, "Unable to find FPA.NAME in the format specification.\n");
     189        return NULL;
     190    }
     191    psMetadataItem *fpaNameItem = psMetadataLookup(phu, nameKeyword); // Name of the FPA
     192    const char *newFPAname = psMetadataItemParseString(fpaNameItem); // FPA.NAME for new source
     193    fpaNameItem = psMetadataLookup(fpa->concepts, "FPA.NAME"); // Current name of FPA
     194    if (fpaNameItem && fpaNameItem->type == PS_DATA_STRING) {
     195        const char *currentFPAname = fpaNameItem->data.V; // Name of the current FPA
     196        if (currentFPAname && strlen(currentFPAname) > 0 && strcmp(currentFPAname, newFPAname) != 0) {
     197            psLogMsg(__func__, PS_LOG_WARN, "FPA.NAME for new source (%s) doesn't match FPA.NAME for current "
     198                     "fpa (%s).\n", newFPAname, currentFPAname);
     199        }
     200    }
     201    psMetadataAddStr(fpa->concepts, PS_LIST_HEAD, "FPA.NAME", PS_META_REPLACE, "Name of FPA", newFPAname);
    182202
    183203    // Where does the PHU go?
     
    185205    if (!mdok || strlen(phuType) == 0) {
    186206        psError(PS_ERR_IO, false, "Unable to find PHU in the format specification.\n");
    187         return false;
     207        return NULL;
    188208    }
    189209    pmHDU *phdu = pmHDUAlloc("PHU");    // The primary header data unit
    190210    phdu->header = psMemIncrRefCounter(phu);
    191211    phdu->format = psMemIncrRefCounter(format);
    192     if (fpa && !fpa->hdu && strcasecmp(phuType, "FPA") == 0) {
     212
     213    // Generate the view
     214    pmFPAview *view = pmFPAviewAlloc(0); // The FPA view corresponding to the PHU, to be returned
     215    view->chip = -1;
     216    view->cell = -1;
     217    view->readout = -1;
     218    view->fpa = psMemIncrRefCounter(fpa);
     219    if (strcasecmp(phuType, "FPA") == 0) {
     220        if (fpa->hdu) {
     221            // Something's already here
     222            psError(PS_ERR_IO, true, "Unable to add source since FPA already has a PHU.\n");
     223            psFree(view);
     224            return NULL;
     225        }
    193226        fpa->hdu = phdu;
    194227        pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, NULL);
    195     } else if (chip && !chip->hdu && strcasecmp(phuType, "CHIP") == 0) {
    196         chip->hdu = phdu;
    197         pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, NULL);
    198     } else if (cell && !cell->hdu && strcasecmp(phuType, "CELL") == 0) {
    199         // cell->hdu = phdu;
    200         psError(PS_ERR_IO, true, "The case of PHU == CELL has not been written yet!\n");
    201         return false;
     228    } else {
     229        // Get the chip
     230        phuChipName = phuNameFromHeader("CHIP.NAME", formatSpec, phu); // Name of the chip
     231        int chipNum = findChip(fpa, phuChipName); // Chip number
     232        if (chipNum == -1) {
     233            psError(PS_ERR_IO, true, "Unable to find chip %s in FPA.\n", phuChipName);
     234            psFree(view);
     235            return NULL;
     236        }
     237        chip = fpa->chips->data[chipNum];
     238        view->chip = chipNum;
     239
     240        if (strcasecmp(phuType, "CHIP") == 0) {
     241            if (chip->hdu) {
     242                // Something's already here
     243                psError(PS_ERR_IO, true, "Unable to add source since chip already has a PHU.\n");
     244                psFree (view);
     245                return NULL;
     246            }
     247            chip->hdu = phdu;
     248            pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, NULL);
     249        } else if (strcasecmp(phuType, "CELL") == 0) {
     250            psError(PS_ERR_IO, true, "Not yet sure how to handle PHU=CELL.\n");
     251            psFree(view);
     252            return NULL;
     253            #if 0
     254            // Added this for possible future plans which might allow PHU=CELL
     255            phuCellName = phuNameFromHeader("CELL.NAME", formatSpec, phu); // Name of the cell
     256            int cellNum = findCell(chip, phuCellName); // Cell number
     257            if (cellNum == -1) {
     258                psError(PS_ERR_IO, true, "Unable to find cell %s in chip %s.\n", phuCellName, phuChipName);
     259                psFree(view);
     260                return NULL;
     261            }
     262            cell = chip->cells->data[cellNum];
     263            view->cell = cellNum;
     264
     265            if (cell->hdu) {
     266                // Something's already here
     267                psError(PS_ERR_IO, true, "Unable to add source since cell already has a PHU.\n");
     268                psFree(view);
     269                return NULL;
     270            }
     271            cell->hdu = phdu;
     272            pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL);
     273            #endif
     274
     275        } else {
     276            psError(PS_ERR_IO, true, "The format of the PHU (%s) is not FPA, CHIP or CELL.\n", phuType);
     277            psFree(view);
     278            return NULL;
     279        }
    202280    }
    203281
     
    206284    if (!mdok || strlen(extType) == 0) {
    207285        psError(PS_ERR_IO, false, "Unable to find EXTENSIONS in the format specification.\n");
    208         return false;
     286        psFree(view);
     287        return NULL;
    209288    }
    210289    psMetadata *contents = psMetadataLookupMD(&mdok, format, "CONTENTS"); // The contents of the FITS file
    211290    if (!mdok || !contents) {
    212291        psError(PS_ERR_IO, false, "Unable to find CONTENTS in the camera format configuration.\n");
    213         return false;
    214     }
    215 
     292        psFree(view);
     293        return NULL;
     294    }
     295
     296
     297    // Now, different actions, according to what's in the file:
     298
     299    // No extensions --- it's all in the PHU
    216300    if (strcasecmp(extType, "NONE") == 0) {
    217         // No extensions --- it's all in the PHU
    218         psString chipType = chipNameFromHeader(formatSpec, phu); // Type of chip
    219         printf("chipType: %s<---\n", chipType);
    220         #if 1                                   // This is here for the courtesy of MegaCam, which has "CCD13   "
    221 
    222         char *space = NULL;             // Position of a space
    223         if ((space = strchr(chipType, ' '))) {
    224             psString temp = psStringNCopy(chipType, strlen(chipType) - strlen(space));
    225             // Free memory???
    226             chipType = temp;
    227         }
    228         #endif
    229 
    230         psString content = psMetadataLookupStr(&mdok, contents, chipType); // The content line
     301        if (strcasecmp(phuType, "FPA") == 0) {
     302            psError(PS_ERR_IO, true, "Not sure how to handle the case PHU=FPA, EXTENSIONS=NONE.\n");
     303            psFree(view);
     304            return NULL;
     305        }
     306        if (strcasecmp(phuType, "CELL") == 0) {
     307            psError(PS_ERR_IO, true, "Not sure how to handle the case PHU=CELL, EXTENSIONS=NONE.\n");
     308            psFree(view);
     309            return NULL;
     310        }
     311
     312        psString content = psMetadataLookupStr(&mdok, contents, phuChipName); // The content line
    231313        if (!mdok || strlen(content) == 0) {
    232             psError(PS_ERR_IO, true, "Cannot find chip %s in the list of CONTENTS.\n");
    233             psFree(chipType);
    234             psFree(content);
    235             return false;
     314            psError(PS_ERR_IO, true, "Cannot find chip %s in the list of CONTENTS.\n", phuChipName);
     315            psFree(view);
     316            return NULL;
    236317        }
    237318
     
    241322        parsePairs(&names, &values, content);
    242323        for (int i = 0; i < names->n; i++) {
    243             psString chipName = names->data[i]; // The name of the chip
    244             psString cellName = values->data[i]; // The name of the cell
    245             pmChip *chip = findChip(fpa, chipName); // The chip we're looking for
    246             if (!chip) {
    247                 psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in fpa --- ignored.\n", chipName);
     324            psString chipName = names->data[i]; // The name of the chip specified
     325            if (phuChipName && strcmp(phuChipName, chipName) != 0) {
     326                psLogMsg(__func__, PS_LOG_WARN, "Chip name in CONTENT (%s) does not match chip name from PHU "
     327                         "(%s) --- there may be problems when reading!\n", chipName, phuChipName);
     328                // Find the other chip
     329                int chipNum = findChip(fpa, chipName); // The chip we're looking for
     330                if (chipNum == -1) {
     331                    psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in fpa --- ignored.\n", chipName);
     332                    continue;
     333                }
     334                chip = fpa->chips->data[chipNum];
     335            }
     336            psString cellName = values->data[i]; // The name of the cell specified
     337            int cellNum = findCell(chip, cellName); // The cell we're looking for
     338            if (cellNum == -1) {
     339                psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored.\n",
     340                         cellName, chipName);
    248341                continue;
    249342            }
    250             pmCell *cell = findCell(chip, cellName); // The cell we're looking for
    251             if (!cell) {
    252                 psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored.\n", cellName,
    253                          chipName);
    254                 continue;
    255             }
     343            cell = chip->cells->data[cellNum];
     344
    256345            psMetadata *cellData = getCellData(format, cellName); // Data for this cell
    257346
    258             // Have already plugged in the PHU
     347            // Have already plugged in the PHU, which is all we have
    259348
    260349            // Put in the cell data
     
    265354            }
    266355            cell->config = psMemIncrRefCounter(cellData);
    267             pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, NULL);
    268         }
    269         return true;
    270     }
    271 
    272 
    273     // Go through the contents
     356            pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
     357        }
     358
     359        return view;
     360    }
     361
     362
     363    // Otherwise, go through the contents
    274364    psMetadataIterator *contentsIter = psMetadataIteratorAlloc(contents, PS_LIST_HEAD, NULL);
    275365    psMetadataItem *contentsItem = NULL; // Item from contents
     
    295385                psString chipName = names->data[i]; // The name of the chip
    296386                psString cellName = values->data[i]; // The name of the cell
    297                 pmChip *chip = findChip(fpa, chipName); // The chip we're looking for
    298                 if (!chip) {
     387                int chipNum = findChip(fpa, chipName); // The chip we're looking for
     388                if (chipNum == -1) {
    299389                    psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in FPA --- ignored.\n", chipName);
    300390                    continue;
    301391                }
    302                 pmCell *cell = findCell(chip, cellName); // The cell we're looking for
    303                 if (!cell) {
     392                chip = fpa->chips->data[chipNum];
     393                int cellNum = findCell(chip, cellName); // The cell we're looking for
     394                if (cellNum == -1) {
    304395                    psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored\n",
    305396                             cellName, chipName);
    306397                    continue;
    307398                }
     399                pmCell *cell = chip->cells->data[cellNum];
    308400                psMetadata *cellData = getCellData(format, cellName); // Data for this cell
    309401
     
    322414                }
    323415                cell->config = psMemIncrRefCounter(cellData);
    324                 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, NULL);
     416                pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
    325417            }
    326418
     
    331423            if (! fpa->hdu->phu) {
    332424                // Need to look up the name of the chip
    333                 chipName = chipNameFromHeader(formatSpec, phu);
    334                 chip = findChip(fpa, chipName);
    335                 if (!chip) {
     425                chipName = phuNameFromHeader("CHIP.NAME", formatSpec, phu);
     426                int chipNum = findChip(fpa, chipName);
     427                if (chipNum == -1) {
    336428                    psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in FPA --- ignored.\n", chipName);
    337429                    continue;
    338430                }
     431                chip = fpa->chips->data[chipNum];
    339432            }
    340433            for (int i = 0; i < names->n; i++) {
     
    345438                    chipName = names->data[i];
    346439                    cellName = cellType;
    347                     chip = findChip(fpa, chipName);
    348                     if (! chip) {
     440                    int chipNum = findChip(fpa, chipName); // The chip we're looking for
     441                    if (chipNum == -1) {
    349442                        psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in FPA --- ignored.\n",
    350443                                 chipName);
    351444                        continue;
    352445                    }
     446                    chip = fpa->chips->data[chipNum];
    353447                } else {
    354448                    // We've got cellName:cellType and the chipName comes from before
    355449                    cellName = names->data[i];
    356450                }
    357                 pmCell *cell = findCell(chip, cellName); // The cell we're looking for
    358                 if (!cell) {
     451                int cellNum = findCell(chip, cellName); // The cell we're looking for
     452                if (cellNum == -1) {
    359453                    psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored.\n",
    360454                             cellName, chipName);
    361455                    continue;
    362456                }
     457                pmCell *cell = chip->cells->data[cellNum];
    363458                psMetadata *cellData = getCellData(format, cellType); // Data for this cell
    364459
     
    378473                }
    379474                cell->config = psMemIncrRefCounter(cellData);
    380                 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, NULL);
     475                pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
    381476            }
    382477
     
    390485    pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_DEFAULTS, NULL);
    391486
    392     return true;
     487    return view;
    393488}
    394489
     
    663758                pmReadout *readout = readouts->data[k]; // The readout
    664759                psImage *image = readout->image; // The image
    665                 psTrace(__func__, 8, "Image: [%d:%d,%d:%d] (%dx%d)\n", image->col0, image->col0 +
    666                         image->numCols, image->row0, image->row0 + image->numRows, image->numCols,
    667                         image->numRows);
     760                if (image) {
     761                    psTrace(__func__, 8, "Image: [%d:%d,%d:%d] (%dx%d)\n", image->col0, image->col0 +
     762                            image->numCols, image->row0, image->row0 + image->numRows, image->numCols,
     763                            image->numRows);
     764                }
    668765            } // Iterating over cell
    669766        } // Iterating over chip
Note: See TracChangeset for help on using the changeset viewer.