IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 23, 2006, 5:25:14 PM (20 years ago)
Author:
Paul Price
Message:

Fixing up pmConfigReadRecipes to get recipes from the command-line. Also pushed argc and argv into pmConfig.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/config/pmConfig.c

    r7601 r7676  
    33 *  @author PAP, IfA
    44 *
    5  *  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2006-06-21 03:05:53 $
     5 *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
     6 *  @date $Date: 2006-06-24 03:25:14 $
    77 *
    88 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3535}
    3636
    37 pmConfig *pmConfigAlloc(void)
     37pmConfig *pmConfigAlloc(int argc, char **argv)
    3838{
    3939    pmConfig *config = psAlloc(sizeof(pmConfig));
     
    4646    config->arguments = NULL;
    4747    config->database = NULL;
     48    config->argc = argc;
     49    config->argv = argv;
    4850
    4951    // the file structure is used to carry pmFPAfiles
     
    166168 *****************************************************************************/
    167169pmConfig *pmConfigRead(
    168     int *argc,
     170    int argc,
    169171    char **argv)
    170172{
    171     PS_ASSERT_INT_POSITIVE(*argc, false);
     173    PS_ASSERT_INT_POSITIVE(argc, false);
    172174    PS_ASSERT_PTR_NON_NULL(argv, false);
    173175
    174     pmConfig *config = pmConfigAlloc(); // The configuration, containing site, camera and recipes
     176    pmConfig *config = pmConfigAlloc(argc, argv); // The configuration, containing site, camera and recipes
    175177
    176178    //
     
    183185    // First, try command line
    184186    //
    185     psS32 argNum = psArgumentGet(*argc, argv, "-site");
     187    psS32 argNum = psArgumentGet(config->argc, config->argv, "-site");
    186188    if (argNum != 0) {
    187189        //
     
    190192        // remove it as well.
    191193        //
    192         psArgumentRemove(argNum, argc, argv);
    193         if (argNum >= *argc) {
     194        psArgumentRemove(argNum, &config->argc, config->argv);
     195        if (argNum >= config->argc) {
    194196            psLogMsg(__func__, PS_LOG_WARN,
    195197                     "-site command-line switch provided without the required filename --- ignored.\n");
    196198        } else {
    197             siteName = psStringCopy(argv[argNum]);
    198             psArgumentRemove(argNum, argc, argv);
     199            siteName = psStringCopy(config->argv[argNum]);
     200            psArgumentRemove(argNum, &config->argc, config->argv);
    199201        }
    200202    }
     
    250252    // file is read and parsed into psMetadata struct "camera".
    251253    //
    252     argNum = psArgumentGet(*argc, argv, "-camera");
     254    argNum = psArgumentGet(config->argc, config->argv, "-camera");
    253255    if (argNum > 0) {
    254         psArgumentRemove(argNum, argc, argv);
    255         if (argNum >= *argc) {
     256        psArgumentRemove(argNum, &config->argc, config->argv);
     257        if (argNum >= config->argc) {
    256258            psLogMsg(__func__, PS_LOG_WARN,
    257259                     "-camera command-line switch provided without the required camera or filename --- "
    258260                     "ignored.\n");
    259261        } else {
    260             char *cameraFile = argv[argNum]; // The camera configuration file to read
     262            char *cameraFile = config->argv[argNum]; // The camera configuration file to read
    261263
    262264            // look for a symbolic camera name in the CAMERAS metadata
     
    277279
    278280            readConfig(&config->camera, cameraFile, "camera");
    279             psArgumentRemove(argNum, argc, argv);
     281            psArgumentRemove(argNum, &config->argc, config->argv);
    280282        }
    281283    }
     
    328330
    329331
    330     argNum = psArgumentGet(*argc, argv, "-log");
     332    argNum = psArgumentGet(config->argc, config->argv, "-log");
    331333    if (argNum > 0) {
    332         psArgumentRemove(argNum, argc, argv);
    333         if (argNum >= *argc) {
     334        psArgumentRemove(argNum, &config->argc, config->argv);
     335        if (argNum >= config->argc) {
    334336            psLogMsg(__func__, PS_LOG_WARN,
    335337                     "-log command-line switch provided without the required log destination --- ignored.\n");
    336338        } else {
    337             if (!psLogSetDestination(psMessageDestination(argv[argNum]))) {
    338                 psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n", argv[argNum]);
    339             }
    340             psArgumentRemove(argNum, argc, argv);
     339            if (!psLogSetDestination(psMessageDestination(config->argv[argNum]))) {
     340                psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n",
     341                         config->argv[argNum]);
     342            }
     343            psArgumentRemove(argNum, &config->argc, config->argv);
    341344        }
    342345    } else {
     
    351354            // expanded in the future to do files, and perhaps even sockets.
    352355            if (!psLogSetDestination(psMessageDestination(logDest))) {
    353                 psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n", argv[argNum]);
     356                psLogMsg(__func__, PS_LOG_WARN, "Unable to set log destination to %s\n",
     357                         config->argv[argNum]);
    354358            }
    355359        }
     
    384388    //
    385389    psS32 saveLogLevel = psLogGetLevel();
    386     psArgumentVerbosity(argc, argv);
     390    psArgumentVerbosity(&config->argc, config->argv);
    387391    // XXX: substitute the string for the default log level for "2".
    388392    if (2 == psLogGetLevel()) {
     
    498502psMetadata *pmConfigCameraFormatFromHeader(
    499503    pmConfig *config,                   // The configuration
    500     const psMetadata *header           // The FITS header
     504    const psMetadata *header            // The FITS header
    501505)
    502506{
     
    599603}
    600604
    601 bool pmConfigReadRecipes(
    602     pmConfig *config
    603 )
     605
     606// Read the recipe filenames
     607static bool recipeFileNames(psMetadata *target, // The target metadata into which to read the recipes
     608                            psMetadata *source, // The source configuration, from which to read the filenames
     609                            const char *sourceName // The name of the source, for error messages
     610                           )
     611{
     612    assert(target);
     613
     614    if (!source) {
     615        psLogMsg(__func__, PS_LOG_WARN, "The %s has not been read --- cannot read recipes from this "
     616                 "location.\n", sourceName);
     617        return false;
     618    }
     619
     620    bool mdok = true;                   // Status of MD lookup
     621    psMetadata *recipes = psMetadataLookupMD(&mdok, source, "RECIPES"); // The list of recipes
     622    if (!mdok || !recipes) {
     623        psLogMsg(__func__, PS_LOG_WARN, "RECIPES in the %s is not of type METADATA --- ignored\n",
     624                 sourceName);
     625        return false;
     626    }
     627
     628    // Copy the filenames to the target from the "RECIPES" in the source.
     629    // We could use psMetadataCopy for this, but it's better to check that everything's of the correct type.
     630    // If it's not of the correct type, we can tell the user which file it's in, so they can find it easier.
     631    psMetadataIterator *recipesIter = psMetadataIteratorAlloc(recipes, PS_LIST_HEAD, NULL); // Iterator
     632    psMetadataItem *fileItem = NULL;    // MD item containing the filename, from recipe iteration
     633    while ((fileItem = psMetadataGetAndIncrement(recipesIter))) {
     634        if (fileItem->type != PS_DATA_STRING) {
     635            psLogMsg(__func__, PS_LOG_WARN, "Recipe %s from %s is not of type STR --- ignored.\n",
     636                     fileItem->name, sourceName);
     637            continue;
     638        }
     639        psMetadataAddItem(target, fileItem, PS_LIST_TAIL, PS_META_REPLACE);
     640    }
     641    psFree(recipesIter);
     642
     643    return true;
     644}
     645
     646
     647bool pmConfigReadRecipes(pmConfig *config
     648                        )
    604649{
    605650    PS_ASSERT_PTR_NON_NULL(config, false);
    606     PS_ASSERT_PTR_NON_NULL(config->camera, false);
    607651
    608652    if (!config->recipes) {
     
    611655
    612656    bool mdok = true;                   // Status of MD lookup
    613     psMetadata *recipes = psMetadataLookupMD(&mdok, config->camera, "RECIPES"); // The list of recipes
    614     if (! mdok || ! recipes) {
    615         psLogMsg(__func__, PS_LOG_WARN, "RECIPES in the camera configuration file is not of type METADATA\n");
    616         return false;
    617     }
    618     // Go through the recipes and load each one
    619     psMetadataIterator *recipesIter = psMetadataIteratorAlloc(recipes, PS_LIST_HEAD, NULL); // Iterator
     657
     658    // Read the recipe file names from the site configuration and camera configuration
     659    recipeFileNames(config->recipes, config->site, "site configuration");
     660    recipeFileNames(config->recipes, config->camera, "camera configuration");
     661
     662    // Go through the command-line arguments
     663    int argNum; // Argument number for "-recipe"
     664    while ((argNum = psArgumentGet(config->argc, config->argv, "-recipe")) > 0) {
     665        psArgumentRemove(argNum, &config->argc, config->argv);
     666        if (argNum + 1 >= config->argc) {
     667            psLogMsg(__func__, PS_LOG_WARN, "-recipe command-line switch provided without the "
     668                     "required recipe and source --- ignored.\n");
     669            if (argNum == config->argc) {
     670                // Remove the single last argument (we required two, they gave us one)
     671                psArgumentRemove(argNum, &config->argc, config->argv);
     672            }
     673            continue;
     674        }
     675
     676        const char *recipeName = psStringCopy(config->argv[argNum]); // Name of the recipe
     677        psArgumentRemove(argNum, &config->argc, config->argv);
     678        const char *recipeSource = psStringCopy(config->argv[argNum]); // Source of the recipe
     679        psArgumentRemove(argNum, &config->argc, config->argv);
     680
     681        // Is the source a symbolic reference?
     682        const char *recipeFile = psMetadataLookupStr(&mdok, config->recipes, recipeSource);
     683        if (mdok && recipeFile && strlen(recipeFile) > 0) {
     684            psString comment = psStringCopy("Recipe added from command line as symbolic link to ");
     685            psStringAppend(&comment, "%s", recipeSource);
     686            psMetadataAddStr(config->recipes, PS_LIST_TAIL, recipeName, PS_META_REPLACE, comment, recipeFile);
     687        } else {
     688            // Otherwise, treat the source as a filename
     689            psMetadataAddStr(config->recipes, PS_LIST_TAIL, recipeName, PS_META_REPLACE,
     690                             "Recipe added from command line", recipeSource);
     691        }
     692    }
     693
     694    // Read the recipes
     695    psMetadataIterator *recipesIter = psMetadataIteratorAlloc(config->recipes, PS_LIST_HEAD, NULL);// Iterator
    620696    psMetadataItem *recipesItem = NULL; // Item from iteration
    621697    while ((recipesItem = psMetadataGetAndIncrement(recipesIter))) {
     
    625701            continue;
    626702        }
    627 
    628703        psMetadata *recipe = NULL;      // Recipe from file
    629704        if (readConfig(&recipe, recipesItem->data.V, "recipe")) {
     
    705780// given the 'file' and 'list' words, find the arguments associated with these words
    706781// and interpret them as lists of files.  return an array of the resulting filenames.
    707 psArray *pmConfigFileSets (int *argc, char **argv, char *file, char *list)
    708 {
    709     PS_ASSERT_PTR_NON_NULL(argc, NULL);
    710     PS_ASSERT_INT_NONNEGATIVE(*argc, NULL);
    711     PS_ASSERT_PTR_NON_NULL(argv, NULL);
     782psArray *pmConfigFileSets(pmConfig *config, char *file, char *list)
     783{
     784    PS_ASSERT_INT_NONNEGATIVE(config->argc, NULL);
     785    PS_ASSERT_PTR_NON_NULL(config, NULL);
     786    PS_ASSERT_PTR_NON_NULL(config->argv, NULL);
    712787    PS_ASSERT_PTR_NON_NULL(file, NULL);
    713788    PS_ASSERT_INT_POSITIVE(strlen(file), NULL);
     
    722797
    723798    // load the list of filenames the supplied file (may be a glob: "file*.fits")
    724     if ((Narg = psArgumentGet (*argc, argv, file))) {
     799    if ((Narg = psArgumentGet (config->argc, config->argv, file))) {
    725800        glob_t globList;
    726         psArgumentRemove (Narg, argc, argv);
     801        psArgumentRemove (Narg, &config->argc, config->argv);
    727802        globList.gl_offs = 0;
    728         glob (argv[Narg], 0, NULL, &globList);
     803        glob (config->argv[Narg], 0, NULL, &globList);
    729804
    730805        if (globList.gl_pathc == 0) {
    731             psError(PS_ERR_IO, true, "No match for %s", argv[Narg]);
     806            psError(PS_ERR_IO, true, "No match for %s", config->argv[Narg]);
    732807            return input;
    733808        }
     
    738813            psFree (filename);
    739814        }
    740         psArgumentRemove (Narg, argc, argv);
     815        psArgumentRemove (Narg, &config->argc, config->argv);
    741816    }
    742817
    743818    // load the list from the supplied text file
    744     if ((Narg = psArgumentGet (*argc, argv, list))) {
     819    if ((Narg = psArgumentGet (config->argc, config->argv, list))) {
    745820        int nItems;
    746821        char line[1024]; // XXX limits the list lines to 1024 chars
     
    748823        char *filename;
    749824
    750         psArgumentRemove (Narg, argc, argv);
    751         FILE *f = fopen (argv[Narg], "r");
     825        psArgumentRemove (Narg, &config->argc, config->argv);
     826        FILE *f = fopen (config->argv[Narg], "r");
    752827        if (f == NULL) {
    753828            psAbort ("psphot", "unable to open specified list file");
     
    769844            }
    770845        }
    771         psArgumentRemove (Narg, argc, argv);
     846        psArgumentRemove (Narg, &config->argc, config->argv);
    772847    }
    773848
     
    775850}
    776851
    777 bool pmConfigFileSetsMD (psMetadata *metadata, int *argc, char **argv, char *name, char *file, char *list)
     852bool pmConfigFileSetsMD (psMetadata *metadata, pmConfig *config, char *name, char *file, char *list)
    778853{
    779854    PS_ASSERT_PTR_NON_NULL(metadata, false);
    780     PS_ASSERT_PTR_NON_NULL(argc, false);
    781     PS_ASSERT_INT_NONNEGATIVE(*argc, NULL);
    782     PS_ASSERT_PTR_NON_NULL(argv, false);
     855    PS_ASSERT_PTR_NON_NULL(config, NULL);
     856    PS_ASSERT_INT_NONNEGATIVE(config->argc, NULL);
     857    PS_ASSERT_PTR_NON_NULL(config->argv, false);
    783858    PS_ASSERT_PTR_NON_NULL(name, false);
    784859    PS_ASSERT_INT_POSITIVE(strlen(name), NULL);
     
    790865    psErrorClear();   // pmConfigFileSets may or may not call psError, so
    791866    // if files->n == 0 we'll want to call psError(..., false, ...)
    792     psArray *files = pmConfigFileSets (argc, argv, file, list);
     867    psArray *files = pmConfigFileSets(config, file, list);
    793868    if (files->n == 0) {
    794869        // psError(PS_ERR_IO, false, "pmConfigFileSets failed to find %s in the argument list", name);
Note: See TracChangeset for help on using the changeset viewer.