Changeset 4386 for trunk/archive/scripts/src/papFocalPlane.c
- Timestamp:
- Jun 24, 2005, 3:05:59 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/scripts/src/papFocalPlane.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/scripts/src/papFocalPlane.c
r4309 r4386 44 44 psMetadataItem *newItem = psMetadataItemAlloc(valueName, value->type, value->comment, value->data.V); 45 45 46 if (fpa ) {46 if (fpa && strncmp(valueName, "FPA", 3) == 0) { 47 47 psMetadataAddItem(fpa->values, newItem, PS_LIST_TAIL, PS_META_REPLACE); 48 48 } 49 if (chip ) {49 if (chip && strncmp(valueName, "CHIP", 4) == 0) { 50 50 psMetadataAddItem(chip->values, newItem, PS_LIST_TAIL, PS_META_REPLACE); 51 51 } 52 if (cell ) {52 if (cell && strncmp(valueName, "CELL", 4) == 0) { 53 53 psMetadataAddItem(cell->values, newItem, PS_LIST_TAIL, PS_META_REPLACE); 54 54 } 55 55 56 psFree(newItem);57 56 } 58 57 … … 65 64 { 66 65 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 } 67 71 68 72 // 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) { 71 75 // We have a FITS header to look up --- search each level 72 76 if (cell->header) { … … 110 114 ) 111 115 { 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); 113 124 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 } 114 157 setValueInCache(fpa, chip, cell, valueName, defItem); 115 158 } 159 160 // XXX: Need to clean up before returning 116 161 return defItem; // defItem is either NULL or points to what was desired 117 162 } … … 125 170 ) 126 171 { 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); 145 239 } 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); 191 243 } 192 // XXX: Need to clean up before returning 193 return result; 244 result = (psMetadataItem*)dbResult->data[0]; 194 245 } 195 }196 } // Doing the "given"s.197 }198 199 // XXX: Need to clean up before returning200 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 202 253 return NULL; 203 254 } … … 209 260 ) 210 261 { 262 psTrace(__func__, 3, "Trying to retrieve %s for %s:%s...\n", valueName, chipName, cellName); 263 211 264 bool mdStatus = true; // Status of MD lookup 212 265 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 } 213 270 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 } 214 275 215 276 // Try cache, headers, database, defaults in order 277 psTrace(__func__, 5, "Trying caches....\n"); 216 278 psMetadataItem *item = getValueFromCache(fpa, chip, cell, valueName); 217 279 if (! item) { 280 psTrace(__func__, 5, "Trying headers....\n"); 218 281 item = getValueFromHeader(fpa, chip, cell, valueName); 219 282 } 220 283 if (! item) { 284 psTrace(__func__, 5, "Trying database....\n"); 221 285 item = getValueFromDB(fpa, chip, cell, valueName); 222 286 } 223 287 if (! item) { 288 psTrace(__func__, 5, "Trying defaults....\n"); 224 289 item = getValueFromDefault(fpa, chip, cell, valueName); 225 290 } … … 245 310 } 246 311 247 psFree(item);248 312 return value; 249 313 } … … 266 330 } 267 331 268 psFree(item);269 332 return value; 270 333 } … … 288 351 } 289 352 290 psFree(item);291 353 return value; 292 354 } … … 310 372 } 311 373 312 psFree(item);313 374 return value; 314 375 } … … 318 379 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 319 380 320 papFPA *papFPAAlloc(psMetadata *fits, // FITS translation info 321 psMetadata *database, // Database lookup info 322 psMetadata *defaults, // Defaults info 323 psDB *db // Database handle 381 papFPA *papFPAAlloc(const psMetadata *camera, // Camera configuration 382 psDB *db // Database 324 383 ) 325 384 { … … 327 386 psMemSetDeallocator(fpa, (psFreeFcn)p_papFPAFree); 328 387 329 // Fill in the variouscomponents330 fpa->f its = 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; 333 392 334 393 fpa->values = psMetadataAlloc(); 394 fpa->camera = psMemIncrRefCounter((psPtr)camera); 395 fpa->db = db; 396 fpa->chips = psMetadataAlloc(); 397 398 fpa->pixels = NULL; 335 399 fpa->header = NULL; 336 fpa->db = psMemIncrRefCounter(db);337 338 fpa->chips = psMetadataAlloc();339 fpa->images = NULL;340 400 341 401 return fpa; … … 344 404 void p_papFPAFree(papFPA *fpa) 345 405 { 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); 349 409 350 410 psFree(fpa->values); 411 psFree((psPtr)fpa->camera); 412 psFree(fpa->db); 413 psFree(fpa->chips); 414 415 psFree(fpa->pixels); 351 416 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 419 papChip *papChipAlloc(papFPA *fpa, // FPA to which the chip belongs 420 const char *name // Name of the chip 362 421 ) 363 422 { 364 423 papChip *chip = psAlloc(sizeof(papChip)); // The chip 365 424 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; 367 435 368 436 chip->values = psMetadataAlloc(); 437 chip->cells = psMetadataAlloc(); 438 439 chip->pixels = NULL; 369 440 chip->header = NULL; 370 441 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)); 373 444 374 445 return chip; … … 377 448 void p_papChipFree(papChip *chip) 378 449 { 450 psFree(chip->toFPA); 451 psFree(chip->fromFPA); 452 379 453 psFree(chip->values); 454 psFree(chip->cells); 455 456 psFree(chip->pixels); 380 457 psFree(chip->header); 381 psFree(chip->cells);382 psFree(chip->images);383 458 } 384 459 385 460 papCell *papCellAlloc(papChip *chip, // Chip to which the cell belongs 386 const char *name, // Name of the cell387 int nReadouts // Number of readouts contained461 const char *name, // Name of the cell 462 int nReadouts // Number of readouts contained 388 463 ) 389 464 { 390 465 papCell *cell = psAlloc(sizeof(papCell)); // The cell 391 466 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; 393 480 394 481 cell->values = psMetadataAlloc(); 482 cell->readouts = psArrayAlloc(nReadouts); 483 484 cell->pixels = NULL; 395 485 cell->header = NULL; 396 486 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)); 399 489 400 490 return cell; … … 403 493 void p_papCellFree(papCell *cell) 404 494 { 495 psFree(cell->toChip); 496 psFree(cell->fromChip); 497 psFree(cell->toFPA); 498 psFree(cell->toTP); 499 psFree(cell->toSky); 500 405 501 psFree(cell->values); 502 psFree(cell->readouts); 503 504 psFree(cell->pixels); 406 505 psFree(cell->header); 407 psFree(cell->readouts);408 psFree(cell->images);409 506 } 410 507
Note:
See TracChangeset
for help on using the changeset viewer.
