/*
 * regtoolConfig.c
 *
 * Copyright (C) 2006  Joshua Hoblitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <math.h>
#include <stdint.h>

#include <psmodules.h>

#include "pxtools.h"
#include "regtool.h"

pxConfig *regtoolConfig(pxConfig *config, int argc, char **argv)
{
    if (!config) {
        config = pxConfigAlloc();
    }

    pmConfigReadParamsSet(false);

    // setup site config
    config->modules = pmConfigRead(&argc, argv, NULL);
    if (!config->modules) {
        psError(PXTOOLS_ERR_CONFIG, false, "Can't find site configuration");
        psFree(config);
        return NULL;
    }

    // -pendingimfile
    psMetadata *pendingimfileArgs = psMetadataAlloc();
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-exp_id",  0,
        "search by exposure ID", NULL);
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-class",  0,
        "search by class", NULL);
    psMetadataAddStr(pendingimfileArgs, PS_LIST_TAIL, "-class_id",  0,
        "search by class ID", NULL);
    psMetadataAddU64(pendingimfileArgs, PS_LIST_TAIL, "-limit",  0,
        "limit result set to N items", 0);
    psMetadataAddBool(pendingimfileArgs, PS_LIST_TAIL, "-simple",  0,
        "use the simple output format", false);

    // -addprocessedimfile
    psMetadata *addprocessedimfileArgs = psMetadataAlloc();
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-exp_id",  0,
        "define exposure ID (required)", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-exp_name",  0,
        "define the exp_name (required)", NULL); 
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-inst",  0,
        "define the camera name (required)", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-telescope",  0,
        "define the telescope name (required)", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-tmp_class_id",  0,
        "define temp. class ID (required)", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-class_id",  0,
        "define class ID (required)", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-uri",  0,
        "define URI (required)", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-exp_type",  0,
        "define exposure type", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-filelevel",  0,
        "define filelevel", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-filter",  0,
        "define filter ", NULL);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-comment",  0,
        "define comment ", NULL);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-airmass",  0,
        "define airmass", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-ra",  0,
        "define RA", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-decl",  0,
        "define DEC", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-exp_time",  0,
        "define exposure time", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-sat_pixel_frac",  0,
        "define fraction of saturated pixels", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-bg",  0,
        "define exposue background", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-bg_stdev",  0,
        "define exposue background stdev", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-bg_mean_stdev",  0,
        "define exposue background mean stdev", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-alt",  0,
        "define altitute", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-az",  0,
        "define azimuth", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-ccd_temp",  0,
        "define ccd tempature", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-posang",  0,
        "define rotator position angle", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m1_x",  0,
            "define M1 X position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m1_y",  0,
            "define M1 Y position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m1_z",  0,
            "define M1 Z position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m1_tip",  0,
            "define M1 TIP position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m1_tilt",  0,
            "define M1 TILT position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m2_x",  0,
            "define M2 X position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m2_y",  0,
            "define M2 Y position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m2_z",  0,
            "define M2 Z position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m2_tip",  0,
            "define M2 TIP position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-m2_tilt",  0,
            "define M2 TILT position", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-env_temperature",  0,
            "define Environmental Temperature", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-env_humidity",  0,
            "define Environmental Humidity", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-env_wind_speed",  0,
            "define Environmental Wind Speed", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-env_wind_dir",  0,
            "define Environmental Wind Direction", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-teltemp_m1",  0,
            "define Telescope Temperature : M1", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-teltemp_m1cell",  0,
            "define Telescope Temperature : M1 Cell", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-teltemp_m2",  0,
            "define Telescope Temperature : M2", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-teltemp_spider",  0,
            "define Telescope Temperature : Spider", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-teltemp_truss",  0,
            "define Telescope Temperature : Truss", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-teltemp_extra",  0,
            "define Telescope Temperature : Extra", NAN);
    psMetadataAddF32(addprocessedimfileArgs, PS_LIST_TAIL, "-pon_time",  0,
            "define time to last Power On", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-user_1",  0,
            "define user statistic (1)", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-user_2",  0,
            "define user statistic (2)", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-user_3",  0,
            "define user statistic (3)", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-user_4",  0,
            "define user statistic (4)", NAN);
    psMetadataAddF64(addprocessedimfileArgs, PS_LIST_TAIL, "-user_5",  0,
            "define user statistic (5)", NAN);
    psMetadataAddStr(addprocessedimfileArgs, PS_LIST_TAIL, "-object",  0,
        "define exposure object", NULL);
    psMetadataAddTime(addprocessedimfileArgs, PS_LIST_TAIL, "-dateobs",  0,
        "define observation time", NULL);
    psMetadataAddS16(addprocessedimfileArgs, PS_LIST_TAIL, "-code",  0,
        "set fault code", 0);

    // -processedimfile
    psMetadata *processedimfileArgs = psMetadataAlloc();
    psMetadataAddStr(processedimfileArgs, PS_LIST_TAIL, "-exp_id",  0,
        "search by exposure ID", NULL);
    psMetadataAddStr(processedimfileArgs, PS_LIST_TAIL, "-class_id",  0,
        "search by class ID", NULL);
    psMetadataAddU64(processedimfileArgs, PS_LIST_TAIL, "-limit",  0,
        "limit result set to N items", 0);
    psMetadataAddBool(processedimfileArgs, PS_LIST_TAIL, "-faulted",  0,
        "only return imfiles with a fault status set", false);
    psMetadataAddBool(processedimfileArgs, PS_LIST_TAIL, "-simple",  0,
        "use the simple output format", false);

    // -revertprocessedimfile
    psMetadata *revertprocessedimfileArgs = psMetadataAlloc();
    psMetadataAddStr(revertprocessedimfileArgs, PS_LIST_TAIL, "-exp_id",  0,
            "search by exposure ID (required)", NULL);
    psMetadataAddStr(revertprocessedimfileArgs, PS_LIST_TAIL, "-tmp_class_id",  0,
            "searcy by temp. class ID", NULL);
    psMetadataAddStr(revertprocessedimfileArgs, PS_LIST_TAIL, "-class_id",  0,
            "search by class ID", NULL);
    psMetadataAddS16(revertprocessedimfileArgs, PS_LIST_TAIL, "-code",  0,
            "search by fault code", 0);

    // -updateprocessedimfile
    psMetadata *updateprocessedimfileArgs = psMetadataAlloc();
    psMetadataAddStr(updateprocessedimfileArgs, PS_LIST_TAIL, "-exp_id",  0,
        "search by exposure ID", NULL);
    psMetadataAddStr(updateprocessedimfileArgs, PS_LIST_TAIL, "-class_id",  0,
        "search by class ID", NULL);
    psMetadataAddS16(updateprocessedimfileArgs, PS_LIST_TAIL, "-code",  0,
        "set fault code (required)", INT16_MAX);

    // -pendingexp
    psMetadata *pendingexpArgs = psMetadataAlloc();
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-exp_id",  0,
        "search by exposure ID", NULL);
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-inst",  0,
        "search by camera", NULL);
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-telescope",  0,
        "search by telescope", NULL);
    psMetadataAddStr(pendingexpArgs, PS_LIST_TAIL, "-exp_type",  0,
        "search by exposure type", NULL);
    psMetadataAddU64(pendingexpArgs, PS_LIST_TAIL, "-limit",  0,
        "limit result set to N items", 0);
    psMetadataAddBool(pendingexpArgs, PS_LIST_TAIL, "-simple",  0,
        "use the simple output format", false);

    // -addprocessedexp
    psMetadata *addprocessedexpArgs = psMetadataAlloc();
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-exp_id",  0,
        "exp_id to operate on (required)", NULL); 
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-exp_name",  0,
        "define the exp_name (required)", NULL); 
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-inst",  0,
        "define the camera name (required)", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-telescope",  0,
        "define the telescope name (required)", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-exp_tag",  0,
        "define the external exposure tag name (required)", NULL);
    psMetadataAddTime(addprocessedexpArgs, PS_LIST_TAIL, "-dateobs",  0,
        "define observation time", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-exp_type",  0,
        "define exposure type", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-filelevel",  0,
        "define the data partitioning level of this file", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-workdir",  0,
        "define the \"default\" workdir for this exposure", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-dvodb",  0,
        "define the dvodb for the next processing step", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-tess_id",  0,
        "define the tess_id for the next processing step", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-end_stage",  0,
        "define the end goal processing step", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-reduction",  0,
        "define the \"default\" reduction class for this exposure", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-filter",  0,
        "define filter ", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-comment",  0,
        "define comment ", NULL);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-airmass",  0,
        "define airmass", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-ra",  0,
        "define RA", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-decl",  0,
        "define DEC", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-exp_time",  0,
        "define exposure time", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-sat_pixel_frac",  0,
        "define fraction of saturated pixels", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-bg",  0,
        "define exposue background", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-bg_stdev",  0,
        "define exposue background stdev", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-bg_mean_stdev",  0,
        "define exposue background mean stdev", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-alt",  0,
        "define altitute", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-az",  0,
        "define azimuth", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-ccd_temp",  0,
        "define ccd tempature", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-posang",  0,
        "define rotator position angle", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m1_x",  0,
            "define M1 X position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m1_y",  0,
            "define M1 Y position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m1_z",  0,
            "define M1 Z position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m1_tip",  0,
            "define M1 TIP position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m1_tilt",  0,
            "define M1 TILT position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m2_x",  0,
            "define M2 X position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m2_y",  0,
            "define M2 Y position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m2_z",  0,
            "define M2 Z position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m2_tip",  0,
            "define M2 TIP position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-m2_tilt",  0,
            "define M2 TILT position", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-env_temperature",  0,
            "define Environmental Temperature", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-env_humidity",  0,
            "define Environmental Humidity", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-env_wind_speed",  0,
            "define Environmental Wind Speed", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-env_wind_dir",  0,
            "define Environmental Wind Direction", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-teltemp_m1",  0,
            "define Telescope Temperature : M1", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-teltemp_m1cell",  0,
            "define Telescope Temperature : M1 Cell", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-teltemp_m2",  0,
            "define Telescope Temperature : M2", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-teltemp_spider",  0,
            "define Telescope Temperature : Spider", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-teltemp_truss",  0,
            "define Telescope Temperature : Truss", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-teltemp_extra",  0,
            "define Telescope Temperature : Extra", NAN);
    psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-pon_time",  0,
            "define time to last Power On", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-user_1",  0,
            "define user statistic (1)", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-user_2",  0,
            "define user statistic (2)", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-user_3",  0,
            "define user statistic (3)", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-user_4",  0,
            "define user statistic (4)", NAN);
    psMetadataAddF64(addprocessedexpArgs, PS_LIST_TAIL, "-user_5",  0,
            "define user statistic (5)", NAN);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-object",  0,
        "define exposure object", NULL);
    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-label",  0,
        "define label for chip stage (non-detrend data only)", NULL);
    psMetadataAddS16(addprocessedexpArgs, PS_LIST_TAIL, "-code",  0,
        "set fault code", 0);
    /*
    psMetadataAddStr(updateArgs, PS_LIST_TAIL, "-recip",  0,
        "define URL", NULL);
    psMetadataAddStr(updateArgs, PS_LIST_TAIL, "-mosiac",  0,
        "define URL", NULL);
    */

    // -processedexp
    psMetadata *processedexpArgs = psMetadataAlloc();
    psMetadataAddStr(processedexpArgs, PS_LIST_TAIL, "-exp_id",  0,
        "search by exposure ID", NULL);
    psMetadataAddU64(processedexpArgs, PS_LIST_TAIL, "-limit",  0,
        "limit result set to N items", 0);
    psMetadataAddBool(processedexpArgs, PS_LIST_TAIL, "-faulted",  0,
        "only return imfiles with a fault status set", false);
    psMetadataAddBool(processedexpArgs, PS_LIST_TAIL, "-simple",  0,
        "use the simple output format", false);

    // -revertprocessedexp
    psMetadata *revertprocessedexpArgs = psMetadataAlloc();
    psMetadataAddStr(revertprocessedexpArgs, PS_LIST_TAIL, "-exp_id",  0,
            "search by exposure ID (required)", NULL);
    psMetadataAddS16(revertprocessedexpArgs, PS_LIST_TAIL, "-code",  0,
            "search by fault code", 0);

    // -updatedprocessedexp
    psMetadata *updatedprocessedexpArgs = psMetadataAlloc();
    psMetadataAddStr(updatedprocessedexpArgs, PS_LIST_TAIL, "-exp_id",  0,
        "search by exposure ID", NULL);
    psMetadataAddS16(updatedprocessedexpArgs, PS_LIST_TAIL, "-code",  0,
        "set fault code (required)", INT16_MAX);
    

    psMetadata *argSets = psMetadataAlloc();
    psMetadata *modes = psMetadataAlloc();

    PXOPT_ADD_MODE("-pendingimfile",           "", REGTOOL_MODE_PENDINGIMFILE, pendingimfileArgs);
    PXOPT_ADD_MODE("-addprocessedimfile",      "",  REGTOOL_MODE_ADDPROCESSEDIMFILE, addprocessedimfileArgs);
    PXOPT_ADD_MODE("-processedimfile",         "", REGTOOL_MODE_PROCESSEDIMFILE, processedimfileArgs);
    PXOPT_ADD_MODE("-revertprocessedimfile",   "", REGTOOL_MODE_REVERTPROCESSEDIMFILE, revertprocessedimfileArgs);
    PXOPT_ADD_MODE("-updateprocessedimfile",   "",   REGTOOL_MODE_UPDATEPROCESSEDIMFILE, updateprocessedimfileArgs);
    PXOPT_ADD_MODE("-pendingexp",              "",       REGTOOL_MODE_PENDINGEXP,pendingexpArgs);
    PXOPT_ADD_MODE("-addprocessedexp",         "",  REGTOOL_MODE_ADDPROCESSEDEXP, addprocessedexpArgs);
    PXOPT_ADD_MODE("-processedexp",            "",     REGTOOL_MODE_PROCESSEDEXP, processedexpArgs);
    PXOPT_ADD_MODE("-revertprocessedexp",      "", REGTOOL_MODE_REVERTPROCESSEDEXP, revertprocessedexpArgs);
    PXOPT_ADD_MODE("-updateprocessedexp",      "",  REGTOOL_MODE_UPDATEPROCESSEDEXP,      updatedprocessedexpArgs);

    if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
        psError(PS_ERR_UNKNOWN, true, "option parsing failed");
        psFree(argSets);
        psFree(modes);
        psFree(config);
        return NULL;
    }

    psFree(argSets);
    psFree(modes);

    // setup search criterion
#define addWhereStr(name) \
{ \
    psString str = NULL; \
    bool status = false; \
    if ((str = psMetadataLookupStr(&status, config->args, "-" #name))) { \
        if (!psMetadataAddStr(config->where, PS_LIST_TAIL, #name, 0, "==", str)) {\
            psError(PXTOOLS_ERR_PROG, false, "failed to add item " #name); \
            psFree(config); \
            return NULL; \
        } \
    } \
}

    // generate SQL where clause
    config->where = psMetadataAlloc();

    addWhereStr(exp_id);
    // convert '-inst' to 'camera'
    {
        psString str = NULL; 
        bool status = false;
        if ((str = psMetadataLookupStr(&status, config->args, "-inst"))) {
            if (!psMetadataAddStr(config->where, PS_LIST_TAIL, "camera", 0, "==", str)) {
                psError(PXTOOLS_ERR_PROG, false, "failed to add item camera");
                psFree(config);
                return NULL;
            }
        }
    }

    addWhereStr(telescope);
    addWhereStr(exp_type);
    addWhereStr(class);
    addWhereStr(class_id);
    addWhereStr(tmp_class_id);
    addWhereStr(filter);

    // convert '-code' to 'fault'
    {
        psS16 fault = 0;
        bool status = false;
        if ((fault = psMetadataLookupS16(&status, config->args, "-code"))) {
            if (!psMetadataAddS16(config->where, PS_LIST_TAIL, "fault", 0, "==", fault)) {
                psError(PS_ERR_UNKNOWN, false, "failed to add item fault");
                psFree(config);
                return NULL;
            }
        }
    }

    if (config->where->list->n < 1) {
        psFree(config->where);
        config->where = NULL;
    }


    // define Database handle, if used
    config->dbh = pmConfigDB(config->modules);
    if (!config->dbh) {
        psError(PXTOOLS_ERR_SYS, false, "Can't configure database");
        psFree(config);
        return NULL;
    }

    return config;
}
