Changeset 6553
- Timestamp:
- Mar 8, 2006, 5:09:28 PM (20 years ago)
- Location:
- branches/rel10_ifa/psModules/src/config
- Files:
-
- 2 edited
-
pmConfig.c (modified) (15 diffs)
-
pmConfig.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/rel10_ifa/psModules/src/config/pmConfig.c
r6479 r6553 3 3 * @author PAP, IfA 4 4 * 5 * @version $Revision: 1.7.4. 2$ $Name: not supported by cvs2svn $6 * @date $Date: 2006-0 2-24 04:02:19$5 * @version $Revision: 1.7.4.3 $ $Name: not supported by cvs2svn $ 6 * @date $Date: 2006-03-09 03:09:28 $ 7 7 * 8 8 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 12 12 #include <string.h> 13 13 #include <unistd.h> 14 #include <assert.h> 14 15 #include "pslib.h" 15 16 #include "pmConfig.h" … … 17 18 #define PS_SITE "PS_SITE" // Name of the environment variable containing the site config file 18 19 #define PS_DEFAULT_SITE "ipprc.config" // Default site config file 20 21 static void configFree(pmConfig *config) 22 { 23 psFree(config->site); 24 psFree(config->camera); 25 psFree(config->recipe); 26 psFree(config->arguments); 27 psFree(config->database); 28 } 29 30 pmConfig *pmConfigAlloc(void) 31 { 32 pmConfig *config = psAlloc(sizeof(pmConfig)); 33 (void)psMemSetDeallocator(config, (psFreeFunc)configFree); 34 35 // Initialise 36 config->site = NULL; 37 config->camera = NULL; 38 config->recipe = NULL; 39 config->arguments = NULL; 40 config->database = NULL; 41 42 return config; 43 } 44 19 45 20 46 /** readConfig … … 57 83 XXX: Must load camera and recipe configuration if specified in the command line. 58 84 *****************************************************************************/ 59 bool pmConfigRead( 60 psMetadata **site, 61 psMetadata **camera, 62 psMetadata **recipe, 85 pmConfig *pmConfigRead( 63 86 int *argc, 64 87 char **argv, 65 88 const char *recipeName) 66 89 { 67 PS_ASSERT_PTR_NON_NULL(site, false);68 // PS_ASSERT_PTR_NON_NULL(*site, false);69 PS_ASSERT_PTR_NON_NULL(camera, false);70 // PS_ASSERT_PTR_NON_NULL(*camera, false);71 PS_ASSERT_PTR_NON_NULL(recipe, false);72 // PS_ASSERT_PTR_NON_NULL(*recipe, false);73 90 PS_ASSERT_INT_POSITIVE(*argc, false); 74 91 PS_ASSERT_PTR_NON_NULL(argv, false); 92 93 pmConfig *config = pmConfigAlloc(); // The configuration, containing site, camera and recipe 75 94 76 95 // … … 120 139 // 121 140 122 if (!readConfig(site, siteName, "site")) { 123 return false; 141 if (!readConfig(&config->site, siteName, "site")) { 142 psFree(config); 143 return NULL; 124 144 } 125 145 if (cleanupSiteName) { … … 127 147 } 128 148 149 150 // 151 // Next, we do a similar thing for the camera configuration file. The 152 // file is read and parsed into psMetadata struct "camera". 153 // 154 argNum = psArgumentGet(*argc, argv, "-camera"); 155 if (argNum > 0) { 156 psArgumentRemove(argNum, argc, argv); 157 if (argNum >= *argc) { 158 psLogMsg(__func__, PS_LOG_WARN, 159 "-camera command-line switch provided without the required filename --- ignored.\n"); 160 } else { 161 psArgumentRemove(argNum, argc, argv); 162 readConfig(&config->camera, argv[argNum], "camera"); 163 } 164 } 129 165 130 166 // … … 141 177 } else { 142 178 psArgumentRemove(argNum, argc, argv); 143 readConfig( recipe, argv[argNum], "recipe");179 readConfig(&config->recipe, argv[argNum], "recipe"); 144 180 } 145 181 } 146 182 // Or, load the recipe from the camera file, if appropriate 147 if (! *recipe && *camera && recipeName) { 148 *recipe = pmConfigRecipeFromCamera(*camera, recipeName); 149 } 150 151 152 // 153 // Next, we do a similar thing for the camera configuration file. The 154 // file is read and parsed into psMetadata struct "camera". 155 // 156 argNum = psArgumentGet(*argc, argv, "-camera"); 157 if (argNum > 0) { 158 psArgumentRemove(argNum, argc, argv); 159 if (argNum >= *argc) { 160 psLogMsg(__func__, PS_LOG_WARN, 161 "-camera command-line switch provided without the required filename --- ignored.\n"); 162 } else { 163 psArgumentRemove(argNum, argc, argv); 164 readConfig(camera, argv[argNum], "camera"); 165 } 166 } else { 167 // XXX: Not sure is this is correct. 168 *camera = NULL; 183 if (! config->recipe && config->camera && recipeName) { 184 pmConfigRecipeFromCamera(config, recipeName); 169 185 } 170 186 … … 181 197 // with a call to psTimeInitialize. 182 198 // 183 psString timeName = psMetadataLookupStr(&mdok, *site, "TIME");199 psString timeName = psMetadataLookupStr(&mdok, config->site, "TIME"); 184 200 if (mdok && timeName) { 185 201 psTrace(__func__, 7, "Initialising psTime with file %s\n", timeName); … … 193 209 // with a call to psLogSetLevel(). 194 210 // 195 int logLevel = psMetadataLookupS32(&mdok, *site, "LOGLEVEL");211 int logLevel = psMetadataLookupS32(&mdok, config->site, "LOGLEVEL"); 196 212 if (mdok && logLevel >= 0) { 197 213 psTrace(__func__, 7, "Setting log level to %d\n", logLevel); … … 204 220 // with a call to psLogSetFormat(). 205 221 // 206 psString logFormat = psMetadataLookupStr(&mdok, *site, "LOGFORMAT");222 psString logFormat = psMetadataLookupStr(&mdok, config->site, "LOGFORMAT"); 207 223 if (mdok && logFormat) { 208 224 psTrace(__func__, 7, "Setting log format to %s\n", logFormat); … … 216 232 // XXX: This is not spec'ed in the SDRS. 217 233 // 218 psString logDest = psMetadataLookupStr(&mdok, *site, "LOGDEST");234 psString logDest = psMetadataLookupStr(&mdok, config->site, "LOGDEST"); 219 235 if (mdok && logDest) { 220 236 // XXX: Only stdout and stderr are provided for now; this section should be … … 240 256 // XXX: This is not spec'ed in the SDRS. 241 257 // 242 psMetadata *trace = psMetadataLookupMD(&mdok, *site, "TRACE");258 psMetadata *trace = psMetadataLookupMD(&mdok, config->site, "TRACE"); 243 259 if (mdok && trace) { 244 260 psMetadataIterator *traceIter = psMetadataIteratorAlloc(trace, PS_LIST_HEAD, NULL); // Iterator … … 268 284 psLogSetLevel(saveLogLevel); 269 285 } 270 return(true); 271 } 272 273 274 // XXX EAM : was trying headerItem when it was NULL 275 // XXX EAM : should just free & return on first failure 276 bool pmConfigValidateCamera( 277 const psMetadata *camera, 286 287 return config; 288 } 289 290 291 bool pmConfigValidateCameraFormat( 292 const psMetadata *cameraFormat, 278 293 const psMetadata *header) 279 294 { 280 // Read the rule for that camera 295 // Read the rule for that camera format 281 296 bool mdStatus = true; 282 psMetadata *rule = psMetadataLookupMD(&mdStatus, camera , "RULE");297 psMetadata *rule = psMetadataLookupMD(&mdStatus, cameraFormat, "RULE"); 283 298 if (! mdStatus || ! rule) { 284 299 psLogMsg(__func__, PS_LOG_WARN, "Unable to read rule for camera.\n"); … … 289 304 psMetadataIterator *ruleIter = psMetadataIteratorAlloc(rule, PS_LIST_HEAD, NULL); // Rule iterator 290 305 psMetadataItem *ruleItem = NULL; // Item from the metadata 291 bool match = true; // Does it match? 292 while ((ruleItem = psMetadataGetAndIncrement(ruleIter)) && match) { 306 while ((ruleItem = psMetadataGetAndIncrement(ruleIter))) { 293 307 // Check for the existence of the rule 294 308 psMetadataItem *headerItem = psMetadataLookup((psMetadata*)header, ruleItem->name); … … 341 355 342 356 psFree(ruleIter); 343 return match; 344 } 345 346 347 348 // Work out what camera we have, based on the FITS header and a set of 349 // rules specified in the IPP configuration; return the camera configuration 350 psMetadata *pmConfigCameraFromHeader( 351 const psMetadata *ipprc, // The IPP configuration 352 const psMetadata *header) // The FITS header 353 { 354 bool mdStatus = false; // Metadata lookup status 355 psMetadata *cameras = psMetadataLookupMD(&mdStatus, ipprc, "CAMERAS"); 356 if (! mdStatus) { 357 psError(PS_ERR_IO, false, "Unable to find CAMERAS in the configuration.\n"); 358 return NULL; 359 } 360 psMetadata *winner = NULL; // The camera configuration whose rule first matches 361 // the supplied header 362 // Iterate over the cameras 363 psMetadataIterator *iterator = psMetadataIteratorAlloc(cameras, PS_LIST_HEAD, NULL); 364 psMetadataItem *cameraItem = NULL; // Item from the metadata 365 366 while ((cameraItem = psMetadataGetAndIncrement(iterator))) { 367 // Open the camera information 368 psTrace(__func__, 3, "Inspecting camera %s (%s)\n", cameraItem->name, 369 cameraItem->comment); 370 psMetadata *camera = NULL; // The camera metadata 371 if (cameraItem->type == PS_DATA_METADATA) { 372 camera = psMemIncrRefCounter(cameraItem->data.md); 373 } else if (cameraItem->type == PS_DATA_STRING) { 374 psTrace(__func__, 5, "Reading camera configuration for %s...\n", cameraItem->name); 357 return true; 358 } 359 360 361 static bool formatFromHeader(psMetadata **format, // Format to return 362 psMetadata *camera, // Camera configuration 363 const psMetadata *header, // FITS header 364 const char *cameraName // Name of camera 365 ) 366 { 367 assert(format); 368 assert(camera); 369 assert(header); 370 371 bool result = false; // Did we find the first match? 372 373 // Read the list of formats 374 bool mdok = true; // Status of MD lookup 375 psMetadata *formats = psMetadataLookupMD(&mdok, camera, "FORMATS"); // List of formats 376 if (!mdok || !formats) { 377 psLogMsg(__func__, PS_LOG_WARN, "Unable to find list of FORMATS in camera %s --- ignored\n", 378 cameraName); 379 return false; 380 } 381 // Iterate over the formats 382 psMetadataIterator *formatsIter = psMetadataIteratorAlloc(formats, PS_LIST_HEAD, NULL); 383 psMetadataItem *formatsItem = NULL; // Item from formats 384 while ((formatsItem = psMetadataGetAndIncrement(formatsIter))) { 385 if (formatsItem->type != PS_DATA_STRING) { 386 psLogMsg(__func__, PS_LOG_WARN, "In camera %s, camera format %s is not of type STR --- " 387 "ignored.\n", cameraName, formatsItem->name); 388 continue; 389 } 390 psTrace(__func__, 5, "Reading camera format for %s...\n", formatsItem->name); 391 unsigned int badLines = 0; // Number of bad lines in reading camera configuration 392 // Format to test against what we've got 393 psMetadata *testFormat = psMetadataConfigParse(NULL, &badLines, formatsItem->data.V, true); 394 if (badLines > 0) { 395 psLogMsg(__func__, PS_LOG_WARN, "%d bad lines encountered while reading camera" 396 "format %s\n", badLines, formatsItem->name); 397 } 398 if (pmConfigValidateCameraFormat(testFormat, header)) { 399 if (!*format) { 400 psLogMsg(__func__, PS_LOG_INFO, "Camera %s, format %s matches header.\n", cameraName, 401 formatsItem->name); 402 *format = psMemIncrRefCounter(testFormat); 403 result = true; 404 } else { 405 psLogMsg(__func__, PS_LOG_WARN, "Camera %s, format %s also matches header --- ignored.\n", 406 cameraName, formatsItem->name); 407 } 408 } 409 psFree(testFormat); 410 } 411 psFree(formatsIter); 412 413 return result; 414 } 415 416 // Work out what camera we have, based on the FITS header and a set of rules specified in the IPP 417 // configuration; return the camera configuration and format 418 psMetadata *pmConfigCameraFormatFromHeader( 419 pmConfig *config, // The configuration 420 const psMetadata *header // The FITS header 421 ) 422 { 423 psMetadata *format = NULL; // The winning format 424 bool mdok = false; // Metadata lookup status 425 426 // If we don't know what sort of camera we have, we try all that we know 427 if (! config->camera) { 428 psMetadata *cameras = psMetadataLookupMD(&mdok, config->site, "CAMERAS"); 429 if (! mdok) { 430 psError(PS_ERR_IO, false, "Unable to find CAMERAS in the configuration.\n"); 431 return false; 432 } 433 434 // Iterate over the cameras 435 psMetadataIterator *camerasIter = psMetadataIteratorAlloc(cameras, PS_LIST_HEAD, NULL); 436 psMetadataItem *camerasItem = NULL; // Item from the metadata 437 while ((camerasItem = psMetadataGetAndIncrement(camerasIter))) { 438 // Open the camera information 439 psTrace(__func__, 3, "Inspecting camera %s (%s)\n", camerasItem->name, camerasItem->comment); 440 if (camerasItem->type != PS_DATA_STRING) { 441 psLogMsg(__func__, PS_LOG_WARN, "Camera configuration for %s in CAMERAS is not of type STR " 442 "--- ignored.\n", camerasItem->name); 443 continue; 444 } 445 446 psTrace(__func__, 5, "Reading camera configuration for %s...\n", camerasItem->name); 375 447 unsigned int badLines = 0; // Number of bad lines in reading camera configuration 376 camera = psMetadataConfigParse(NULL, &badLines, cameraItem->data.V, true); 448 // Camera to test against what we've got: 449 psMetadata *testCamera = psMetadataConfigParse(NULL, &badLines, camerasItem->data.V, true); 377 450 if (badLines > 0) { 378 451 psLogMsg(__func__, PS_LOG_WARN, "%d bad lines encountered while reading camera" 379 "configuration %s\n", badLines, cameraItem->name); 380 } 381 } 382 383 if (! camera) { 384 psLogMsg(__func__, PS_LOG_WARN, "Unable to interpret camera configuration for %s (%s)\n", 385 cameraItem->name, cameraItem->comment); 386 continue; 387 } 388 389 if (pmConfigValidateCamera(camera, header)) { 390 if (! winner) { 391 // This is the first match 392 winner = psMemIncrRefCounter(camera); 393 psLogMsg(__func__, PS_LOG_INFO, "FITS header matches camera %s\n", 394 cameraItem->name); 395 } else { 396 // We have a duplicate match 397 psLogMsg(__func__, PS_LOG_WARN, "Additional camera found that matches the rules: %s\n", 398 cameraItem->name); 399 } 400 } // Done inspecting the camera 401 402 psFree(camera); 403 404 } // Done looking at all cameras 405 if (! winner) { 406 psError(PS_ERR_IO, true, "Unable to find an camera that matches input FITS header!\n"); 407 } 408 409 psFree(iterator); 410 return winner; 411 } 412 413 psMetadata *pmConfigRecipeFromCamera( 414 const psMetadata *camera, 452 "configuration %s\n", badLines, camerasItem->name); 453 } 454 if (! testCamera) { 455 psLogMsg(__func__, PS_LOG_WARN, "Unable to interpret camera configuration for %s (%s) --- " 456 "ignored\n", camerasItem->name, camerasItem->comment); 457 continue; 458 } 459 460 if (formatFromHeader(&format, testCamera, header, camerasItem->name)) { 461 config->camera = psMemIncrRefCounter(testCamera); 462 } 463 psFree(testCamera); 464 } // Done looking at all cameras 465 psFree(camerasIter); 466 467 if (! config->camera) { 468 psError(PS_ERR_IO, true, "Unable to find a camera that matches input FITS header!\n"); 469 return NULL; 470 } 471 472 return format; 473 } 474 475 // Otherwise, try the specific camera 476 if (! formatFromHeader(&format, config->camera, header, "specified camera")) { 477 psError(PS_ERR_IO, true, "Unable to find a format with the specified camera that matches the " 478 "given header.\n"); 479 return NULL; 480 } 481 return format; 482 } 483 484 bool pmConfigRecipeFromCamera( 485 pmConfig *config, 415 486 const char *recipeName) 416 487 { 417 PS_ASSERT_PTR_NON_NULL(camera, false); 488 PS_ASSERT_PTR_NON_NULL(config, false); 489 PS_ASSERT_PTR_NON_NULL(config->camera, false); 418 490 PS_ASSERT_PTR_NON_NULL(recipeName, false); 419 491 420 psMetadata *recipe = NULL; // Recipe to read421 492 bool mdok = true; // Status of MD lookup 422 psMetadata *recipes = psMetadataLookupMD(&mdok, c amera, "RECIPES"); // The list of recipes493 psMetadata *recipes = psMetadataLookupMD(&mdok, config->camera, "RECIPES"); // The list of recipes 423 494 if (! mdok || ! recipes) { 424 495 psLogMsg(__func__, PS_LOG_WARN, "RECIPES in the camera configuration file is not of type METADATA\n"); 425 } else {426 psString recipeFileName = psMetadataLookupStr(&mdok, recipes, recipeName);427 (void)readConfig(&recipe, recipeFileName, "recipe");428 }429 430 return recipe;496 return false; 497 } 498 psString recipeFileName = psMetadataLookupStr(&mdok, recipes, recipeName); 499 (void)readConfig(&config->recipe, recipeFileName, "recipe"); 500 501 return true; 431 502 } 432 503 -
branches/rel10_ifa/psModules/src/config/pmConfig.h
r6297 r6553 3 3 * @author PAP, IfA 4 4 * 5 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $6 * @date $Date: 2006-0 2-02 04:51:14$5 * @version $Revision: 1.3.4.1 $ $Name: not supported by cvs2svn $ 6 * @date $Date: 2006-03-09 03:09:28 $ 7 7 * 8 8 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 15 15 16 16 17 // Configuration information 18 typedef struct 19 { 20 psMetadata *site; // Site configuration 21 psMetadata *camera; // Camera specification 22 psMetadata *recipe; // Recipe for processing 23 psMetadata *arguments; // Command-line arguments 24 psDB *database; // Database handle 25 } 26 pmConfig; 27 28 pmConfig *pmConfigAlloc(void); 17 29 18 30 /** pmConfigRead 19 * 31 * 20 32 * pmConfigRead shall load the site configuration (according to the above rule 21 33 * for determining the source). The camera configuration shall also be loaded if … … 32 44 * 33 45 */ 34 bool pmConfigRead( 35 psMetadata **site, 36 psMetadata **camera, 37 psMetadata **recipe, 46 pmConfig *pmConfigRead( 38 47 int *argc, 39 48 char **argv, 40 const char *recipeName 41 ); 42 49 const char *recipeName); 43 50 44 51 45 52 /** pmConfigValidateCamera 46 * 53 * 47 54 * This function, used by pmConfigCameraFromHeader, shall return true if the 48 55 * FITS header matches the rule contained in the camera configuration (see 49 56 * x2.2.2.3); otherwise it shall return false. 50 * 57 * 51 58 */ 52 bool pmConfigValidateCamera( 53 const psMetadata *camera, 54 const psMetadata *header 55 ); 56 59 bool pmConfigValidateCameraFormat( 60 const psMetadata *cameraFormat, 61 const psMetadata *header); 57 62 58 63 59 64 /** pmConfigCameraFromHeader 60 * 65 * 61 66 * pmConfigCameraFromHeader shall load the camera configuration based on the 62 67 * contents of the FITS header, using the list of known cameras contained in the 63 68 * site configuration. If more than one camera matches the FITS header, a warning 64 69 * shall be generated and the first matching camera returned. 65 * 70 * 66 71 */ 67 psMetadata *pmConfigCameraF romHeader(68 const psMetadata *site,69 const psMetadata *header 72 psMetadata *pmConfigCameraFormatFromHeader( 73 pmConfig *config, // The configuration 74 const psMetadata *header // The FITS header 70 75 ); 71 76 72 77 73 74 78 /** pmConfigRecipeFromCamera 75 * 79 * 76 80 * pmConfigRecipeFromCamera shall load the recipe configuration based on the 77 * recipeName and the list of known recipes contained in the camera 81 * recipeName and the list of known recipes contained in the camera 78 82 * configuration. 79 * 83 * 80 84 */ 81 psMetadata *pmConfigRecipeFromCamera(82 const psMetadata *camera,85 bool pmConfigRecipeFromCamera( 86 pmConfig *config, 83 87 const char *recipeName 84 88 ); 85 89 86 90 /** pmConfigDB 87 * 91 * 88 92 * pmConfigDB shall use the site configuration data to open a database handle. 89 93 * This is fairly straightforward at the moment, but will change when we beef up 90 94 * security. (TBD) 91 * 95 * 92 96 */ 93 97 #ifdef DOMIT_PSDB
Note:
See TracChangeset
for help on using the changeset viewer.
