IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 13, 2008, 4:56:38 PM (18 years ago)
Author:
eugene
Message:

substantial work on argument handling to support externally supplied images

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20080511/ppSim/src/ppSimArguments.c

    r17557 r17672  
    1 #include "ppSim.h"
     1# include "ppSim.h"
    22
    33// Print usage information and die
     
    1818}
    1919
    20 // Get a value from the command-line arguments and add it to recipe options
    21 bool valueArgRecipe(psMetadata *options,    // Target to which to add value
    22                     const char *recipeName, // Name for value in the recipe
    23                     psMetadata *arguments,  // Command-line arguments
    24                     const char *argName     // Argument name in the command-line arguments
    25     )
     20// this function supplements the RECIPE:OPTIONS folder with command-line options
     21bool ppSimArguments(int argc, char **argv, pmConfig *config)
    2622{
    27     bool status;                                                    // Status of MD lookup
    28     float value = psMetadataLookupF32(&status, arguments, argName); // Value of interest
    29     if (isnan(value)) return true;
    30     status = psMetadataAddF32(options, PS_LIST_TAIL, recipeName, 0, NULL, value);
    31     return status;
    32 }
    33 
    34 // this function supplements the RECIPE:OPTIONS folder with command-line options
    35 void ppSimArguments(int argc, char *argv[], pmConfig *config)
    36 {
     23    bool status;
    3724    bool mdok;                          // Status of MD lookup
    3825
    3926    assert(config);
    4027
    41     // save the following command-line options in the arguments structure
     28    // save the following command-line options in the arguments structure.  these will later be
     29    // parsed and moved to the config->recipes:PPSIM_RECIPE folder
     30
    4231    psMetadata *arguments = psMetadataAlloc(); // Command-line arguments
    4332    psMetadataAddStr(arguments, PS_LIST_TAIL, "-format", 0, "Camera format name", NULL);
     
    7160
    7261    pmConfigFileSetsMD (config->arguments, &argc, argv, "PSPHOT.PSF", "-psf", "-psflist");
    73 
    74     // only one of -camera and -file is needed
    75     bool status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT", "-file", "-list");
    76     if (!config->camera && !status) {
    77         psErrorStackPrint(stderr, "A camera name (-camera NAME) or an image (-file NAME) must be specified");
    78         exit(PS_EXIT_CONFIG_ERROR);
     62    if (psErrorCodeLast () != PS_ERR_NONE) { psAbort ("problem with PSPHOT.PSF option"); }
     63
     64    // Only one of -camera and -file is needed or allowed.  The -camera option would have been
     65    // already parsed by pmConfigRead in ppSim.c and resulted in a value for config->camera
     66    bool loadImage = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT", "-file", "-list");
     67    if (!config->camera && !loadImage) {
     68        psError(PS_ERR_IO, true, "A camera name (-camera NAME) or an image (-file NAME) must be specified");
     69        psFree(arguments);
     70        return false;
     71    }
     72    if (config->camera && loadImage) {
     73        psError(PS_ERR_IO, true, "Only one of (-camera NAME) and (-file NAME) may be specified");
     74        psFree(arguments);
     75        return false;
    7976    }
    8077
    8178    if (!psArgumentParse(arguments, &argc, argv)) {
    82         exit(PS_EXIT_CONFIG_ERROR);
     79        psError(PS_ERR_IO, false, "error in command-line arguments");
     80        psFree(arguments);
     81        return false;
    8382    }
    8483
    8584    if (argc != 2) {
    86         psErrorStackPrint(stderr, "Missing output filename");
    87         exit(PS_EXIT_CONFIG_ERROR);
     85        psError(PS_ERR_IO, true, "Missing output filename");
     86        psFree(arguments);
     87        return false;
     88    }
     89
     90    // output filename
     91    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "Name of the output image", argv[1]);
     92
     93    // save the additional recipe values based on command-line options. These options override
     94    // the PPSIM recipe values loaded from recipe files
     95    psMetadata *options = pmConfigRecipeOptions (config, PPSIM_RECIPE);
     96    if (!options) {
     97        psError(PS_ERR_IO, false, "Unable to find recipe options for %s", PPSIM_RECIPE);
     98        psFree(arguments);
     99        return false;
     100    }
     101
     102    // these arguments may be used whether the input image is created or loaded
     103    ppSimArgToRecipeF32(&status, options, "STARS.LUM",     arguments, "-starslum");
     104    ppSimArgToRecipeF32(&status, options, "STARS.MAG",     arguments, "-starsmag");
     105    ppSimArgToRecipeF32(&status, options, "STARS.DENSITY", arguments, "-starsdensity");
     106
     107    // if we are loading the input image (not creating it), then we can skip the remaining arguments
     108    if (loadImage) {
     109        // if we are supplying an input image, it is so we may supply fake stars; force it to
     110        // be treated as an OBJECT image.
     111        psMetadataAddStr(options, PS_LIST_TAIL, "IMAGE.TYPE", 0, "Exposure type", "OBJECT");
     112
     113        // check for these options as well
     114        ppSimArgToRecipeStr(&status, options, "PSF.MODEL",    arguments, "-psfclass"); // PSF model class
     115        ppSimArgToRecipeStr(&status, options, "GALAXY.MODEL", arguments, "-galmodel"); // Galaxy model name
     116
     117        // 'seeing' is not required: we can load a psf-model instead; but if not, it is allowed
     118        ppSimArgToRecipeF32(&status, options, "SEEING", arguments, "-seeing"); // seeing (FWHM in arcsec)
     119
     120        // 'scale' is not required: we can use the WCS instead; but if not, it is allowed
     121        ppSimArgToRecipeF32(&status, options, "PIXEL.SCALE", arguments, "-scale"); // Plate scale
     122
     123        psFree (arguments);
     124        return true;
    88125    }
    89126
     
    96133        psMetadata *formats = psMetadataLookupMetadata(NULL, config->camera, "FORMATS"); // The camera formats
    97134        if (!formats) {
    98             psErrorStackPrint(stderr, "Unable to find FORMATS in camera configuration.");
     135            psError(PS_ERR_IO, false, "Unable to find FORMATS in camera configuration.");
    99136            psFree(arguments);
    100             psFree(config);
    101             exit(PS_EXIT_CONFIG_ERROR);
     137            return false;
    102138        }
    103139        psMetadata *format = psMetadataLookupMetadata(NULL, formats, formatName); // Format of interest
    104140        if (!format) {
    105             psErrorStackPrint(stderr, "Unable to find format %s in camera FORMATS.", formatName);
     141            psError(PS_ERR_IO, false, "Unable to find format %s in camera FORMATS.", formatName);
    106142            psFree(arguments);
    107             psFree(config);
    108             exit(PS_EXIT_CONFIG_ERROR);
     143            return false;
    109144        }
    110145        config->format = psMemIncrRefCounter(format);
    111146    }
    112147
    113     // specify the type of simulated image to produce
    114     // XXX this should not be required if we supplied INPUT
    115     const char *typeStr = psMetadataLookupStr(NULL, arguments, "-type"); // Exposure type
    116     if (!typeStr) {
    117         psErrorStackPrint(stderr, "An exposure type must be specified using -type");
    118         exit(PS_EXIT_CONFIG_ERROR);
    119     }
    120     ppSimType type = PPSIM_TYPE_NONE;   // Type to simulate
    121     if (strcasecmp(typeStr, "BIAS") == 0) {
    122         type = PPSIM_TYPE_BIAS;
    123     } else if (strcasecmp(typeStr, "DARK") == 0) {
    124         type = PPSIM_TYPE_DARK;
    125     } else if (strcasecmp(typeStr, "FLAT") == 0) {
    126         type = PPSIM_TYPE_FLAT;
    127     } else if (strcasecmp(typeStr, "OBJECT") == 0) {
    128         type = PPSIM_TYPE_OBJECT;
     148    char *typeStr = ppSimArgToRecipeStr (&status, options, "IMAGE.TYPE", arguments, "-type"); // Requested exposure type
     149    if (!status) {
     150        psError(PS_ERR_IO, false, "An exposure type must be specified using -type");
     151        psFree(arguments);
     152        return false;
     153    }
     154    ppSimType type = ppSimTypeFromString (typeStr);
     155
     156    char *filter = ppSimArgToRecipeStr(&status, options, "FILTER", arguments, "-filter"); // Filter name
     157
     158    // set the exposure time
     159    if (type == PPSIM_TYPE_BIAS) {
     160        psMetadataAddF32(options, PS_LIST_TAIL, "EXPTIME", 0, "Exposure time (s)", 0.0);
    129161    } else {
    130         psErrorStackPrint(stderr, "Unrecognised exposure type: %s", typeStr);
    131         exit(PS_EXIT_CONFIG_ERROR);
    132     }
    133     assert(type != PPSIM_TYPE_NONE);
    134     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "TYPE", 0, "Exposure type", type);
    135 
    136     const char *filter = psMetadataLookupStr(NULL, arguments, "-filter"); // Filter name
    137     psMetadataAddStr(config->arguments, PS_LIST_TAIL, "FILTER", 0, "Filter name", filter);
    138 
    139     // save the following additional recipe values based on command-line options
    140     // these options override the PPSIM recipe values loaded from recipe files
    141     psMetadata *options = pmConfigRecipeOptions (config, PPSIM_RECIPE);
    142     if (!options) {
    143         psErrorStackPrint(stderr, "Unable to find recipe options for %s", PPSIM_RECIPE);
    144         psFree(arguments);
    145         psFree(config);
    146         exit(PS_EXIT_CONFIG_ERROR);
    147     }
    148 
    149     float expTime;
    150     if (type == PPSIM_TYPE_BIAS) {
    151         expTime = 0.0;
    152     } else {
    153         expTime = psMetadataLookupF32(NULL, arguments, "-exptime"); // Exposure time
    154         if (isnan(expTime)) {
    155             psErrorStackPrint(stderr, "The exposure time must be specified using -exptime");
    156             exit(PS_EXIT_CONFIG_ERROR);
    157         }
    158     }
    159     psMetadataAddF32(options, PS_LIST_TAIL, "EXPTIME", 0, "Exposure time (s)", expTime);
     162        ppSimArgToRecipeF32(&status, options, "EXPTIME", arguments, "-exptime");
     163        if (!status) {
     164            psError(PS_ERR_IO, false, "The exposure time must be specified using -exptime");
     165            psFree(arguments);
     166            return false;
     167        }
     168    }
    160169
    161170    // these values all get moved from arguments to RECIPE:OPTIONS
    162     valueArgRecipe(options, "BIAS.LEVEL",    arguments, "-biaslevel");
    163     valueArgRecipe(options, "BIAS.RANGE",    arguments, "-biasrange");
    164     valueArgRecipe(options, "DARK.RATE",     arguments, "-darkrate");
    165     valueArgRecipe(options, "FLAT.SIGMA",    arguments, "-flatsigma");
    166     valueArgRecipe(options, "FLAT.RATE",     arguments, "-flatrate");
    167     valueArgRecipe(options, "SHUTTER.TIME",  arguments, "-shuttertime");
    168     valueArgRecipe(options, "SKY.RATE",      arguments, "-skyrate");
    169     valueArgRecipe(options, "STARS.LUM",     arguments, "-starslum");
    170     valueArgRecipe(options, "STARS.MAG",     arguments, "-starsmag");
    171     valueArgRecipe(options, "STARS.DENSITY", arguments, "-starsdensity");
     171    ppSimArgToRecipeF32(&status, options, "BIAS.LEVEL",    arguments, "-biaslevel");
     172    ppSimArgToRecipeF32(&status, options, "BIAS.RANGE",    arguments, "-biasrange");
     173    ppSimArgToRecipeF32(&status, options, "DARK.RATE",     arguments, "-darkrate");
     174    ppSimArgToRecipeF32(&status, options, "FLAT.SIGMA",    arguments, "-flatsigma");
     175    ppSimArgToRecipeF32(&status, options, "FLAT.RATE",     arguments, "-flatrate");
     176    ppSimArgToRecipeF32(&status, options, "SHUTTER.TIME",  arguments, "-shuttertime");
     177    ppSimArgToRecipeF32(&status, options, "SKY.RATE",      arguments, "-skyrate");
     178    ppSimArgToRecipeS32(&status, options, "BINNING",       arguments, "-bin");
    172179
    173180    psMetadata *recipe = psMetadataLookupMetadata(&mdok, config->recipes, PPSIM_RECIPE); // Recipe
    174 
    175     int biasOrder = psMetadataLookupS32(&mdok, recipe, "BIAS.ORDER"); // Overscan polynomial order
    176     if (!mdok) {
    177         psWarning("BIAS.ORDER(S32) is not set in the recipe %s --- assuming %d", PPSIM_RECIPE, biasOrder);
    178     }
    179     psMetadataAddS32(options, PS_LIST_TAIL, "BIAS.ORDER", 0, "Overscan polynomial order", biasOrder);
    180 
    181     int binning = psMetadataLookupS32(NULL, arguments, "-bin"); // Binning in x and y
    182     if (binning <= 0) {
    183         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Binning (%d) is non-positive.", binning);
    184         exit(PS_EXIT_CONFIG_ERROR);
    185     }
    186     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "BINNING", 0, "Binning in x and y", binning);
    187 
    188     psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "Name of the output image", argv[1]);
    189181
    190182    if (type == PPSIM_TYPE_OBJECT) {
     
    197189        // XXX scale and zp should be supplied by the config file (allow override, but this is camera-dependent)
    198190        if (isnan(ra0) || isnan(dec0) || isnan(pa) || isnan(seeing)) {
    199             psErrorStackPrint(stderr, "-ra, -dec, -pa, -scale, -zp, -seeing must be specified for OBJECT type");
    200             exit(PS_EXIT_CONFIG_ERROR);
    201         }
    202 
    203         float zp = psMetadataLookupF32(NULL, arguments, "-zp"); // Zero point
    204         if (isnan(zp)) {
    205             // use the filter to get the zeropoint from the recipe
    206             psMetadataItem *zpItem = psMetadataLookup (recipe, "ZEROPTS");
    207             // check that item is multi...
    208            
    209             psArray *entries = psListToArray (zpItem->data.list);
    210          
    211             // search for matching filter
    212             for (int i = 0; i < entries->n; i++) {
    213                 psMetadataItem *item = entries->data[i];
    214                 psMetadata *entry = item->data.V;
    215 
    216                 char *filterName = psMetadataLookupStr (&status, entry, "FILTER");
    217                 assert (filterName);
    218 
    219                 if (strcmp(filterName, filter)) continue;
    220 
    221                 zp = psMetadataLookupF32 (&status, entry, "ZERO_PT");
    222                 assert (status);
    223                 break;
    224             }
    225             psFree (entries);
    226         }
    227 
    228         float scale   = psMetadataLookupF32(NULL, arguments, "-scale"); // Plate scale
    229         if (isnan(scale)) {
    230             scale = psMetadataLookupF32 (&status, recipe, "PIXEL.SCALE");
    231         }
    232 
    233         float skymags = psMetadataLookupF32(NULL, arguments, "-skymags"); // Position angle
    234         if (isnan(skymags)) {
    235             skymags = psMetadataLookupF32 (&status, recipe, "SKY.MAGS");
    236         }
    237 
     191            psError(PS_ERR_IO, false, "-ra, -dec, -pa, -scale, -zp, -seeing must be specified for OBJECT type");
     192            psFree(arguments);
     193            return false;
     194        }
     195
     196        float scale = ppSimArgToRecipeF32(&status, options, "PIXEL.SCALE", arguments, "-scale"); // Plate scale
     197        ppSimArgToRecipeF32(&status, options, "SKY.MAGS", arguments, "-skymags"); // Plate scale
     198        ppSimArgToRecipeStr(&status, options, "PSF.MODEL",    arguments, "-psfclass"); // PSF model class
     199        ppSimArgToRecipeStr(&status, options, "GALAXY.MODEL", arguments, "-galmodel"); // Galaxy model name
     200
     201        // the user supplies FWHM in arcsec; here we convert to Sigma in pixels
     202        psMetadataAddF32(options, PS_LIST_TAIL, "SEEING", 0, "Seeing SIGMA (pixels)", seeing / 2.0 / sqrt(2.0 * log(2.0)) / scale);
    238203        psMetadataAddF32(options, PS_LIST_TAIL, "RA", 0, "Boresight RA (radians)", ra0 * M_PI / 180.0);
    239204        psMetadataAddF32(options, PS_LIST_TAIL, "DEC", 0, "Boresight Declination (radians)", dec0 * M_PI / 180.0);
    240205        psMetadataAddF32(options, PS_LIST_TAIL, "PA", 0, "Boresight position angle (radians)",pa * M_PI / 180.0);
    241206
    242         // the user supplies FWHM in arcsec; here we convert to Sigma in pixels
    243         psMetadataAddF32(options, PS_LIST_TAIL, "SEEING", 0, "Seeing SIGMA (pixels)", seeing / 2.0 / sqrt(2.0 * log(2.0)) / scale);
    244 
    245         psMetadataAddF32(options, PS_LIST_TAIL, "SCALE", 0, "Plate scale (arcsec/pix)", scale);
    246         psMetadataAddF32(options, PS_LIST_TAIL, "ZEROPOINT", 0, "Photometric zeropoint", zp);
    247         psMetadataAddF32(options, PS_LIST_TAIL, "SKY.MAGS", 0, "sky surface brightness", skymags);
    248 
    249         const char *psfClass = psMetadataLookupStr(NULL, arguments, "-psfclass"); // PSF model class
    250         psMetadataAddStr(config->arguments, PS_LIST_TAIL, "PSF.MODEL", 0, "PSF model class", psfClass);
    251 
    252         const char *galModel = psMetadataLookupStr(NULL, arguments, "-galmodel"); // Galaxy model name
    253         psMetadataAddStr(config->arguments, PS_LIST_TAIL, "GALAXY.MODEL", 0, "Galaxy model", galModel);
     207        assert (filter != NULL);
     208
     209        ppSimArgToRecipeF32(&status, options, "ZEROPOINT", arguments, "-zp"); // Zero point
     210        if (!status) {
     211            float zp = ppSimGetZeroPoint (recipe, filter);
     212            psMetadataAddF32(options, PS_LIST_TAIL, "ZEROPOINT", 0, "Photometric zeropoint", zp);
     213        }
    254214    }
    255215
    256216    psFree(arguments);
    257     return;
     217    return true;
    258218}
    259219
     
    262222   PSPHOT.PSF
    263223   INPUT
    264    TYPE
    265    FILTER
    266    BIAS.ORDER
    267    BINNING
    268224   OUTPUT
    269    PSF.MODEL
    270    GALAXY.MODEL
    271 
    272    all othr values should come from the recipe
     225
     226   all other values should come from the recipe
    273227*/
Note: See TracChangeset for help on using the changeset viewer.