IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6553


Ignore:
Timestamp:
Mar 8, 2006, 5:09:28 PM (20 years ago)
Author:
Paul Price
Message:

Modified the camera configuration to use camera config (components of the camera) and format descriptions (FITS file layout).

Location:
branches/rel10_ifa/psModules/src/config
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/rel10_ifa/psModules/src/config/pmConfig.c

    r6479 r6553  
    33 *  @author PAP, IfA
    44 *
    5  *  @version $Revision: 1.7.4.2 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2006-02-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 $
    77 *
    88 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1212#include <string.h>
    1313#include <unistd.h>
     14#include <assert.h>
    1415#include "pslib.h"
    1516#include "pmConfig.h"
     
    1718#define PS_SITE "PS_SITE"               // Name of the environment variable containing the site config file
    1819#define PS_DEFAULT_SITE "ipprc.config"  // Default site config file
     20
     21static 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
     30pmConfig *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
    1945
    2046/** readConfig
     
    5783XXX: Must load camera and recipe configuration if specified in the command line.
    5884 *****************************************************************************/
    59 bool pmConfigRead(
    60     psMetadata **site,
    61     psMetadata **camera,
    62     psMetadata **recipe,
     85pmConfig *pmConfigRead(
    6386    int *argc,
    6487    char **argv,
    6588    const char *recipeName)
    6689{
    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);
    7390    PS_ASSERT_INT_POSITIVE(*argc, false);
    7491    PS_ASSERT_PTR_NON_NULL(argv, false);
     92
     93    pmConfig *config = pmConfigAlloc(); // The configuration, containing site, camera and recipe
    7594
    7695    //
     
    120139    //
    121140
    122     if (!readConfig(site, siteName, "site")) {
    123         return false;
     141    if (!readConfig(&config->site, siteName, "site")) {
     142        psFree(config);
     143        return NULL;
    124144    }
    125145    if (cleanupSiteName) {
     
    127147    }
    128148
     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    }
    129165
    130166    //
     
    141177        } else {
    142178            psArgumentRemove(argNum, argc, argv);
    143             readConfig(recipe, argv[argNum], "recipe");
     179            readConfig(&config->recipe, argv[argNum], "recipe");
    144180        }
    145181    }
    146182    // 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);
    169185    }
    170186
     
    181197    // with a call to psTimeInitialize.
    182198    //
    183     psString timeName = psMetadataLookupStr(&mdok, *site, "TIME");
     199    psString timeName = psMetadataLookupStr(&mdok, config->site, "TIME");
    184200    if (mdok && timeName) {
    185201        psTrace(__func__, 7, "Initialising psTime with file %s\n", timeName);
     
    193209    // with a call to psLogSetLevel().
    194210    //
    195     int logLevel = psMetadataLookupS32(&mdok, *site, "LOGLEVEL");
     211    int logLevel = psMetadataLookupS32(&mdok, config->site, "LOGLEVEL");
    196212    if (mdok && logLevel >= 0) {
    197213        psTrace(__func__, 7, "Setting log level to %d\n", logLevel);
     
    204220    // with a call to psLogSetFormat().
    205221    //
    206     psString logFormat = psMetadataLookupStr(&mdok, *site, "LOGFORMAT");
     222    psString logFormat = psMetadataLookupStr(&mdok, config->site, "LOGFORMAT");
    207223    if (mdok && logFormat) {
    208224        psTrace(__func__, 7, "Setting log format to %s\n", logFormat);
     
    216232    // XXX: This is not spec'ed in the SDRS.
    217233    //
    218     psString logDest = psMetadataLookupStr(&mdok, *site, "LOGDEST");
     234    psString logDest = psMetadataLookupStr(&mdok, config->site, "LOGDEST");
    219235    if (mdok && logDest) {
    220236        // XXX: Only stdout and stderr are provided for now; this section should be
     
    240256    // XXX: This is not spec'ed in the SDRS.
    241257    //
    242     psMetadata *trace = psMetadataLookupMD(&mdok, *site, "TRACE");
     258    psMetadata *trace = psMetadataLookupMD(&mdok, config->site, "TRACE");
    243259    if (mdok && trace) {
    244260        psMetadataIterator *traceIter = psMetadataIteratorAlloc(trace, PS_LIST_HEAD, NULL); // Iterator
     
    268284        psLogSetLevel(saveLogLevel);
    269285    }
    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
     291bool pmConfigValidateCameraFormat(
     292    const psMetadata *cameraFormat,
    278293    const psMetadata *header)
    279294{
    280     // Read the rule for that camera
     295    // Read the rule for that camera format
    281296    bool mdStatus = true;
    282     psMetadata *rule = psMetadataLookupMD(&mdStatus, camera, "RULE");
     297    psMetadata *rule = psMetadataLookupMD(&mdStatus, cameraFormat, "RULE");
    283298    if (! mdStatus || ! rule) {
    284299        psLogMsg(__func__, PS_LOG_WARN, "Unable to read rule for camera.\n");
     
    289304    psMetadataIterator *ruleIter = psMetadataIteratorAlloc(rule, PS_LIST_HEAD, NULL); // Rule iterator
    290305    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))) {
    293307        // Check for the existence of the rule
    294308        psMetadataItem *headerItem = psMetadataLookup((psMetadata*)header, ruleItem->name);
     
    341355
    342356    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
     361static 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
     418psMetadata *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);
    375447            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);
    377450            if (badLines > 0) {
    378451                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
     484bool pmConfigRecipeFromCamera(
     485    pmConfig *config,
    415486    const char *recipeName)
    416487{
    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);
    418490    PS_ASSERT_PTR_NON_NULL(recipeName, false);
    419491
    420     psMetadata *recipe = NULL;          // Recipe to read
    421492    bool mdok = true;                   // Status of MD lookup
    422     psMetadata *recipes = psMetadataLookupMD(&mdok, camera, "RECIPES"); // The list of recipes
     493    psMetadata *recipes = psMetadataLookupMD(&mdok, config->camera, "RECIPES"); // The list of recipes
    423494    if (! mdok || ! recipes) {
    424495        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;
    431502}
    432503
  • branches/rel10_ifa/psModules/src/config/pmConfig.h

    r6297 r6553  
    33 *  @author PAP, IfA
    44 *
    5  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2006-02-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 $
    77 *
    88 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1515
    1616
     17// Configuration information
     18typedef 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}
     26pmConfig;
     27
     28pmConfig *pmConfigAlloc(void);
    1729
    1830/** pmConfigRead
    19  * 
     31 *
    2032 * pmConfigRead shall load the site configuration (according to the above rule
    2133 * for determining the source). The camera configuration shall also be loaded if
     
    3244 *
    3345 */
    34 bool pmConfigRead(
    35     psMetadata **site,
    36     psMetadata **camera,
    37     psMetadata **recipe,
     46pmConfig *pmConfigRead(
    3847    int *argc,
    3948    char **argv,
    40     const char *recipeName
    41 );
    42 
     49    const char *recipeName);
    4350
    4451
    4552/** pmConfigValidateCamera
    46  * 
     53 *
    4754 * This function, used by pmConfigCameraFromHeader, shall return true if the
    4855 * FITS header matches the rule contained in the camera configuration (see
    4956 * x2.2.2.3); otherwise it shall return false.
    50  * 
     57 *
    5158 */
    52 bool pmConfigValidateCamera(
    53     const psMetadata *camera,
    54     const psMetadata *header
    55 );
    56 
     59bool pmConfigValidateCameraFormat(
     60    const psMetadata *cameraFormat,
     61    const psMetadata *header);
    5762
    5863
    5964/** pmConfigCameraFromHeader
    60  * 
     65 *
    6166 * pmConfigCameraFromHeader shall load the camera configuration based on the
    6267 * contents of the FITS header, using the list of known cameras contained in the
    6368 * site configuration. If more than one camera matches the FITS header, a warning
    6469 * shall be generated and the first matching camera returned.
    65  * 
     70 *
    6671 */
    67 psMetadata *pmConfigCameraFromHeader(
    68     const psMetadata *site,
    69     const psMetadata *header
     72psMetadata *pmConfigCameraFormatFromHeader(
     73    pmConfig *config,                   // The configuration
     74    const psMetadata *header           // The FITS header
    7075);
    7176
    7277
    73 
    7478/** pmConfigRecipeFromCamera
    75  * 
     79 *
    7680 * 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
    7882 * configuration.
    79  * 
     83 *
    8084 */
    81 psMetadata *pmConfigRecipeFromCamera(
    82     const psMetadata *camera,
     85bool pmConfigRecipeFromCamera(
     86    pmConfig *config,
    8387    const char *recipeName
    8488);
    8589
    8690/** pmConfigDB
    87  * 
     91 *
    8892 * pmConfigDB shall use the site configuration data to open a database handle.
    8993 * This is fairly straightforward at the moment, but will change when we beef up
    9094 * security. (TBD)
    91  * 
     95 *
    9296 */
    9397#ifdef DOMIT_PSDB
Note: See TracChangeset for help on using the changeset viewer.