IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 24, 2005, 3:05:59 PM (21 years ago)
Author:
Paul Price
Message:

Retrieval of concepts seems to be working.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/scripts/src/papFocalPlane.c

    r4309 r4386  
    4444    psMetadataItem *newItem = psMetadataItemAlloc(valueName, value->type, value->comment, value->data.V);
    4545
    46     if (fpa) {
     46    if (fpa && strncmp(valueName, "FPA", 3) == 0) {
    4747        psMetadataAddItem(fpa->values, newItem, PS_LIST_TAIL, PS_META_REPLACE);
    4848    }
    49     if (chip) {
     49    if (chip && strncmp(valueName, "CHIP", 4) == 0) {
    5050        psMetadataAddItem(chip->values, newItem, PS_LIST_TAIL, PS_META_REPLACE);
    5151    }
    52     if (cell) {
     52    if (cell && strncmp(valueName, "CELL", 4) == 0) {
    5353        psMetadataAddItem(cell->values, newItem, PS_LIST_TAIL, PS_META_REPLACE);
    5454    }
    5555
    56     psFree(newItem);
    5756}
    5857
     
    6564{
    6665    bool mdStatus = true;               // Status of MD lookup
     66    psMetadata *translation = psMetadataLookupMD(&mdStatus, fpa->camera, "TRANSLATION"); // FITS translation
     67    if (! mdStatus) {
     68        psError(PS_ERR_IO, false, "Unable to find TRANSLATION in camera configuration.\n");
     69        return NULL;
     70    }
    6771
    6872    // Look for how to translate the concept into a FITS header name
    69     const char *header = psMetadataLookupString(&mdStatus, fpa->fits, valueName);
    70     if (strlen(header) > 0) {
     73    const char *header = psMetadataLookupString(&mdStatus, translation, valueName);
     74    if (mdStatus && strlen(header) > 0) {
    7175        // We have a FITS header to look up --- search each level
    7276        if (cell->header) {
     
    110114    )
    111115{
    112     psMetadataItem *defItem = psMetadataLookup((psMetadata*)fpa->defaults, valueName);
     116    bool mdOK = true;                   // Status of MD lookup
     117    psMetadata *defaults = psMetadataLookupMD(&mdOK, fpa->camera, "DEFAULTS");
     118    if (! mdOK) {
     119        psError(PS_ERR_IO, false, "Unable to find DEFAULTS in camera configuration.\n");
     120        return NULL;
     121    }
     122
     123    psMetadataItem *defItem = psMetadataLookup(defaults, valueName);
    113124    if (defItem) {
     125        if (defItem->type == PS_META_META) {
     126            // A dependent default
     127            psTrace(__func__, 7, "Evaluating dependent default....\n");
     128            psMetadata *dependents = defItem->data.V; // The list of dependents
     129            // Find out what it depends on
     130            psString dependName = psStringCopy(valueName);
     131            psStringAppend(&dependName, "_DEPEND");
     132            psString dependsOn = psMetadataLookupString(&mdOK, defaults, dependName);
     133            if (! mdOK) {
     134                psError(PS_ERR_IO, false, "Unable to find %s in camera configuration for dependent default"
     135                        " --- ignored\n", dependName);
     136                // XXX: Need to clean up before returning
     137                return NULL;
     138            }
     139            psFree(dependName);
     140            // Find the value of the dependent concept
     141            psMetadataItem *depItem = getValueFromCache(fpa, chip, cell, dependsOn);
     142            if (! depItem) {
     143                depItem = getValueFromHeader(fpa, chip, cell, dependsOn);
     144            }
     145            if (! depItem) {
     146                psError(PS_ERR_IO, true, "Unable to find value for %s (required for %s)\n", dependsOn,
     147                        valueName);
     148                return NULL;
     149            }
     150            if (depItem->type != PS_META_STR) {
     151                psError(PS_ERR_IO, true, "Value of %s is not of type string, as required for dependency"
     152                        " --- ignored.\n", dependsOn);
     153            }
     154
     155            defItem = psMetadataLookup(dependents, depItem->data.V);    // This is now what we were after
     156        }
    114157        setValueInCache(fpa, chip, cell, valueName, defItem);
    115158    }
     159
     160    // XXX: Need to clean up before returning
    116161    return defItem;                     // defItem is either NULL or points to what was desired
    117162}
     
    125170    )
    126171{
    127     if (fpa->db) {
    128         // The database has been initialised
    129         bool mdStatus = true;           // Status of MD lookup
    130         psMetadata *dbLookup = psMetadataLookupMD(&mdStatus, fpa->database, valueName);
    131         if (dbLookup) {
    132             const char *tableName = psMetadataLookupString(&mdStatus, dbLookup, "TABLE"); // Name of the table
    133             const char *colName = psMetadataLookupString(&mdStatus, dbLookup, "COLUMN"); // Name of the column
    134             const char *givenCols = psMetadataLookupString(&mdStatus, dbLookup, "GIVENDBCOL");  // Name of "where" columns
    135             const char *givenPS = psMetadataLookupString(&mdStatus, dbLookup, "GIVENPS"); // Values for "where" columns
    136            
    137             // Now, need to get the "given"s
    138             if (strlen(givenCols) || strlen(givenPS)) {
    139                 psList *cols = papSplit(givenCols, ",;"); // List of column names
    140                 psList *values = papSplit(givenPS, ",;"); // List of value names for the columns
    141                 psMetadata *selection = psMetadataAlloc(); // The stuff to select in the DB
    142                 if (cols->size != values->size) {
    143                     psLogMsg(__func__, PS_LOG_WARN, "The GIVENDBCOL and GIVENPS entries for %s do not have "
    144                              "the same number of entries --- ignored.\n", valueName);
     172    if (! fpa->db) {
     173        // No database initialised
     174        return NULL;
     175    }
     176
     177    bool mdStatus = true;               // Status of MD lookup
     178    psMetadata *database = psMetadataLookupMD(&mdStatus, fpa->camera, "DATABASE");
     179    if (! mdStatus) {
     180        // No error, because not everyone needs to use the DB
     181        return NULL;
     182    }
     183
     184    psMetadata *dbLookup = psMetadataLookupMD(&mdStatus, database, valueName);
     185    if (dbLookup) {
     186        const char *tableName = psMetadataLookupString(&mdStatus, dbLookup, "TABLE"); // Name of the table
     187        const char *colName = psMetadataLookupString(&mdStatus, dbLookup, "COLUMN"); // Name of the column
     188        const char *givenCols = psMetadataLookupString(&mdStatus, dbLookup, "GIVENDBCOL");      // Name of "where" columns
     189        const char *givenPS = psMetadataLookupString(&mdStatus, dbLookup, "GIVENPS"); // Values for "where" columns
     190       
     191        // Now, need to get the "given"s
     192        if (strlen(givenCols) || strlen(givenPS)) {
     193            psList *cols = papSplit(givenCols, ",;"); // List of column names
     194            psList *values = papSplit(givenPS, ",;"); // List of value names for the columns
     195            psMetadata *selection = psMetadataAlloc(); // The stuff to select in the DB
     196            if (cols->size != values->size) {
     197                psLogMsg(__func__, PS_LOG_WARN, "The GIVENDBCOL and GIVENPS entries for %s do not have "
     198                         "the same number of entries --- ignored.\n", valueName);
     199            } else {
     200                // Iterators for the lists
     201                psListIterator *colsIter = psListIteratorAlloc(cols, PS_LIST_HEAD, false);
     202                psListIterator *valuesIter = psListIteratorAlloc(values, PS_LIST_HEAD, false);
     203                char *column = NULL;    // Name of the column
     204                while (column = psListGetAndIncrement(colsIter)) {
     205                    const char *name = psListGetAndIncrement(valuesIter); // Name for the value
     206                    if (!strlen(column) || !strlen(name)) {
     207                        psLogMsg(__func__, PS_LOG_WARN, "One of the columns or value names for %s is "
     208                                 " empty --- ignored.\n", valueName);
     209                    } else {
     210                        // Search for the value name
     211                        psMetadataItem *item = getValueFromCache(fpa, chip, cell, name);
     212                        if (! item) {
     213                            item = getValueFromHeader(fpa, chip, cell, name);
     214                        }
     215                        if (! item) {
     216                            item = getValueFromDefault(fpa, chip, cell, name);
     217                        }
     218                        if (! item) {
     219                            psLogMsg(__func__, PS_LOG_ERROR, "Unable to find the value name %s for DB "
     220                                     " lookup on %s --- ignored.\n", name, valueName);
     221                        } else {
     222                            // We need to create a new psMetadataItem.  I don't think we can't simply hack
     223                            // the existing one, since that could conceivably cause memory leaks
     224                            psMetadataItem *newItem = psMetadataItemAlloc(valueName, item->type,
     225                                                                          item->comment, item->data.V);
     226                            psMetadataAddItem(selection, newItem, PS_LIST_TAIL, PS_META_REPLACE);
     227                            psFree(newItem);
     228                        }
     229                    }
     230                } // Iterating through the columns
     231               
     232                psArray *dbResult = psDBSelectRows(fpa->db, tableName, selection, 2); // Lookup result
     233                // Note that we use limit=2 in order to test if there are multiple rows returned
     234               
     235                psMetadataItem *result = NULL; // The final result of the DB lookup
     236                if (dbResult->n == 0) {
     237                    psLogMsg(__func__, PS_LOG_WARN, "Unable to find any rows in DB for %s --- ignored\n",
     238                             valueName);
    145239                } else {
    146                     // Iterators for the lists
    147                     psListIterator *colsIter = psListIteratorAlloc(cols, PS_LIST_HEAD, false);
    148                     psListIterator *valuesIter = psListIteratorAlloc(values, PS_LIST_HEAD, false);
    149                     char *column = NULL;        // Name of the column
    150                     while (column = psListGetAndIncrement(colsIter)) {
    151                         const char *name = psListGetAndIncrement(valuesIter); // Name for the value
    152                         if (!strlen(column) || !strlen(name)) {
    153                             psLogMsg(__func__, PS_LOG_WARN, "One of the columns or value names for %s is "
    154                                      " empty --- ignored.\n", valueName);
    155                         } else {
    156                             // Search for the value name
    157                             psMetadataItem *item = getValueFromCache(fpa, chip, cell, name);
    158                             if (! item) {
    159                                 item = getValueFromHeader(fpa, chip, cell, name);
    160                             }
    161                             if (! item) {
    162                                 item = getValueFromDefault(fpa, chip, cell, name);
    163                             }
    164                             if (! item) {
    165                                 psLogMsg(__func__, PS_LOG_ERROR, "Unable to find the value name %s for DB "
    166                                          " lookup on %s --- ignored.\n", name, valueName);
    167                             } else {
    168                                 // We need to create a new psMetadataItem.  I don't think we can't simply hack
    169                                 // the existing one, since that could conceivably cause memory leaks
    170                                 psMetadataItem *newItem = psMetadataItemAlloc(valueName, item->type,
    171                                                                               item->comment, item->data.V);
    172                                 psMetadataAddItem(selection, newItem, PS_LIST_TAIL, PS_META_REPLACE);
    173                                 psFree(newItem);
    174                             }
    175                         }
    176                     } // Iterating through the columns
    177                    
    178                     psArray *dbResult = psDBSelectRows(fpa->db, tableName, selection, 2); // Result of lookup
    179                     // Note that we use limit=2 in order to test if there are multiple rows returned
    180 
    181                     psMetadataItem *result = NULL; // The final result of the DB lookup
    182                     if (dbResult->n == 0) {
    183                         psLogMsg(__func__, PS_LOG_WARN, "Unable to find any rows in DB for %s --- ignored\n",
    184                                  valueName);
    185                     } else {
    186                         if (dbResult-> n > 1) {
    187                             psLogMsg(__func__, PS_LOG_WARN, "Multiple rows returned in DB lookup for %s --- "
    188                                      " using the first one only.\n", valueName);
    189                         }
    190                         result = (psMetadataItem*)dbResult->data[0];
     240                    if (dbResult-> n > 1) {
     241                        psLogMsg(__func__, PS_LOG_WARN, "Multiple rows returned in DB lookup for %s --- "
     242                                 " using the first one only.\n", valueName);
    191243                    }
    192                     // XXX: Need to clean up before returning
    193                     return result;
     244                    result = (psMetadataItem*)dbResult->data[0];
    194245                }
    195             }
    196         } // Doing the "given"s.
    197     }
    198 
    199     // XXX: Need to clean up before returning
    200 
    201     // If we've fallen through somehow, we didn't get anything.
     246                // XXX: Need to clean up before returning
     247                return result;
     248            }
     249        }
     250    } // Doing the "given"s.
     251
     252    // Shouldn't get here
    202253    return NULL;
    203254}
     
    209260                               )
    210261{
     262    psTrace(__func__, 3, "Trying to retrieve %s for %s:%s...\n", valueName, chipName, cellName);
     263
    211264    bool mdStatus = true;               // Status of MD lookup
    212265    papChip *chip = psMetadataLookupChip(&mdStatus, fpa->chips, chipName); // The chip
     266    if (!mdStatus) {
     267        psError(PS_ERR_IO, false, "Unable to find chip named %s\n", chipName);
     268        return NULL;
     269    }
    213270    papCell *cell = psMetadataLookupCell(&mdStatus, chip->cells, cellName); // The cell
     271    if (!mdStatus) {
     272        psError(PS_ERR_IO, false, "Unable to find cell named %s in chip %s\n", cellName, chipName);
     273        return NULL;
     274    }
    214275
    215276    // Try cache, headers, database, defaults in order
     277    psTrace(__func__, 5, "Trying caches....\n");
    216278    psMetadataItem *item = getValueFromCache(fpa, chip, cell, valueName);
    217279    if (! item) {
     280        psTrace(__func__, 5, "Trying headers....\n");
    218281        item = getValueFromHeader(fpa, chip, cell, valueName);
    219282    }
    220283    if (! item) {
     284        psTrace(__func__, 5, "Trying database....\n");
    221285        item = getValueFromDB(fpa, chip, cell, valueName);
    222286    }
    223287    if (! item) {
     288        psTrace(__func__, 5, "Trying defaults....\n");
    224289        item = getValueFromDefault(fpa, chip, cell, valueName);
    225290    }
     
    245310    }
    246311
    247     psFree(item);
    248312    return value;
    249313}
     
    266330    }
    267331
    268     psFree(item);
    269332    return value;
    270333}
     
    288351    }
    289352
    290     psFree(item);
    291353    return value;
    292354}
     
    310372    }
    311373
    312     psFree(item);
    313374    return value;
    314375}
     
    318379//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    319380
    320 papFPA *papFPAAlloc(psMetadata *fits, // FITS translation info
    321                     psMetadata *database, // Database lookup info
    322                     psMetadata *defaults, // Defaults info
    323                     psDB *db            // Database handle
     381papFPA *papFPAAlloc(const psMetadata *camera, // Camera configuration
     382                    psDB *db            // Database
    324383    )
    325384{
     
    327386    psMemSetDeallocator(fpa, (psFreeFcn)p_papFPAFree);
    328387
    329     // Fill in the various components
    330     fpa->fits = psMemIncrRefCounter(fits);
    331     fpa->database = psMemIncrRefCounter(database);
    332     fpa->defaults = psMemIncrRefCounter(defaults);
     388    // Fill in the components
     389    fpa->fromTangentPlane = NULL;
     390    fpa->toTangentPlane = NULL;
     391    fpa->projection = NULL;
    333392
    334393    fpa->values = psMetadataAlloc();
     394    fpa->camera = psMemIncrRefCounter((psPtr)camera);
     395    fpa->db = db;
     396    fpa->chips = psMetadataAlloc();
     397
     398    fpa->pixels = NULL;
    335399    fpa->header = NULL;
    336     fpa->db = psMemIncrRefCounter(db);
    337 
    338     fpa->chips = psMetadataAlloc();
    339     fpa->images = NULL;
    340400
    341401    return fpa;
     
    344404void p_papFPAFree(papFPA *fpa)
    345405{
    346     psFree((psPtr)fpa->fits);
    347     psFree((psPtr)fpa->database);
    348     psFree((psPtr)fpa->defaults);
     406    psFree(fpa->fromTangentPlane);
     407    psFree(fpa->toTangentPlane);
     408    psFree(fpa->projection);
    349409
    350410    psFree(fpa->values);
     411    psFree((psPtr)fpa->camera);
     412    psFree(fpa->db);
     413    psFree(fpa->chips);
     414
     415    psFree(fpa->pixels);
    351416    psFree(fpa->header);
    352     if (fpa->db) {
    353         psDBCleanup((psDB*)fpa->db);
    354     }
    355 
    356     psFree(fpa->chips);
    357     psFree(fpa->images);
    358 }
    359 
    360 papChip *papChipAlloc(papFPA *fpa,              // FPA to which the chip belongs
    361                     const char *name    // Name of the chip
     417}
     418
     419papChip *papChipAlloc(papFPA *fpa,      // FPA to which the chip belongs
     420                      const char *name  // Name of the chip
    362421    )
    363422{
    364423    papChip *chip = psAlloc(sizeof(papChip)); // The chip
    365424    psMemSetDeallocator(chip, (psFreeFcn)p_papChipFree);
    366     psMetadataAdd(fpa->chips, PS_LIST_TAIL, name, PS_META_CHIP, "Added on allocation of chip", chip);
     425
     426    // Push onto the FPA
     427    psMetadataAdd(fpa->chips, PS_LIST_TAIL, name, PS_META_CHIP, "Chip added on allocation", chip);
     428
     429    // Fill in the components
     430    *(int*)&chip->col0 = 0;             // Good enough for now
     431    *(int*)&chip->row0 = 0;             // Good enough for now
     432
     433    chip->toFPA = NULL;
     434    chip->fromFPA = NULL;
    367435
    368436    chip->values = psMetadataAlloc();
     437    chip->cells = psMetadataAlloc();
     438
     439    chip->pixels = NULL;
    369440    chip->header = NULL;
    370441
    371     chip->cells = psMetadataAlloc();
    372     chip->images = NULL;
     442    psMetadataAdd(chip->values, PS_LIST_TAIL, "CHIP.NAME", PS_META_STR, "Name of the chip",
     443                  psStringCopy(name));
    373444
    374445    return chip;
     
    377448void p_papChipFree(papChip *chip)
    378449{
     450    psFree(chip->toFPA);
     451    psFree(chip->fromFPA);
     452
    379453    psFree(chip->values);
     454    psFree(chip->cells);
     455
     456    psFree(chip->pixels);
    380457    psFree(chip->header);
    381     psFree(chip->cells);
    382     psFree(chip->images);
    383458}
    384459
    385460papCell *papCellAlloc(papChip *chip,    // Chip to which the cell belongs
    386                     const char *name,   // Name of the cell
    387                     int nReadouts       // Number of readouts contained
     461                      const char *name, // Name of the cell
     462                      int nReadouts     // Number of readouts contained
    388463    )
    389464{
    390465    papCell *cell = psAlloc(sizeof(papCell)); // The cell
    391466    psMemSetDeallocator(cell, (psFreeFcn)p_papCellFree);
    392     psMetadataAdd(chip->cells, PS_LIST_TAIL, name, PS_META_CELL, "Added on allocation of cell", cell);
     467
     468    // Push onto the chip
     469    psMetadataAdd(chip->cells, PS_LIST_TAIL, name, PS_META_CELL, "Cell added on allocation", cell);
     470
     471    // Fill in components
     472    *(int*)&cell->col0 = 0;             // Good enough for now
     473    *(int*)&cell->row0 = 0;             // Good enough for now
     474
     475    cell->toChip = NULL;
     476    cell->fromChip = NULL;
     477    cell->toFPA = NULL;
     478    cell->toTP = NULL;
     479    cell->toSky = NULL;
    393480
    394481    cell->values = psMetadataAlloc();
     482    cell->readouts = psArrayAlloc(nReadouts);
     483
     484    cell->pixels = NULL;
    395485    cell->header = NULL;
    396486
    397     cell->readouts = psArrayAlloc(nReadouts);
    398     cell->images = NULL;
     487    psMetadataAdd(cell->values, PS_LIST_TAIL, "CELL.NAME", PS_META_STR, "Name of the cell",
     488                  psStringCopy(name));
    399489
    400490    return cell;
     
    403493void p_papCellFree(papCell *cell)
    404494{
     495    psFree(cell->toChip);
     496    psFree(cell->fromChip);
     497    psFree(cell->toFPA);
     498    psFree(cell->toTP);
     499    psFree(cell->toSky);
     500
    405501    psFree(cell->values);
     502    psFree(cell->readouts);
     503
     504    psFree(cell->pixels);
    406505    psFree(cell->header);
    407     psFree(cell->readouts);
    408     psFree(cell->images);
    409506}
    410507
Note: See TracChangeset for help on using the changeset viewer.