IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 25, 2010, 1:38:50 PM (16 years ago)
Author:
eugene
Message:

updates to staticskytool (now mostly working)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/staticskytool.c

    r27963 r28091  
    11/*
    2  * stacktool.c
     2 * staticskytool.c
    33 *
    44 * Copyright (C) 2007-2010  Joshua Hoblitt, Paul Price
     
    2929
    3030#include "pxtools.h"
    31 #include "stacktool.h"
     31#include "staticskytool.h"
    3232
    3333static bool definebyqueryMode(pxConfig *config);
    34 static bool definerunMode(pxConfig *config);
    3534static bool updaterunMode(pxConfig *config);
    36 static bool addinputskyfileMode(pxConfig *config);
    37 static bool inputskyfileMode(pxConfig *config);
    38 static bool tosumMode(pxConfig *config);
    39 static bool addsumskyfileMode(pxConfig *config);
    40 static bool sumskyfileMode(pxConfig *config);
    41 static bool revertsumskyfileMode(pxConfig *config);
    42 static bool pendingcleanuprunMode(pxConfig *config);
    43 static bool pendingcleanupskyfileMode(pxConfig *config);
    44 static bool donecleanupMode(pxConfig *config);
    45 static bool updatesumskyfileMode(pxConfig *config);
     35static bool inputsMode(pxConfig *config);
     36static bool todoMode(pxConfig *config);
     37static bool addresultMode(pxConfig *config);
     38static bool resultMode(pxConfig *config);
     39static bool revertMode(pxConfig *config);
     40static bool updateresult(pxConfig *config);
    4641static bool exportrunMode(pxConfig *config);
    4742static bool importrunMode(pxConfig *config);
    48 
    49 static bool setstackRunState(pxConfig *config, psS64 stack_id, const char *state);
    5043
    5144# define MODECASE(caseName, func) \
     
    6053    psLibInit(NULL);
    6154
    62     pxConfig *config = stacktoolConfig(NULL, argc, argv);
     55    pxConfig *config = staticskytoolConfig(NULL, argc, argv);
    6356    if (!config) {
    6457        psError(PXTOOLS_ERR_CONFIG, false, "failed to configure");
     
    6760
    6861    switch (config->mode) {
    69         MODECASE(STACKTOOL_MODE_DEFINEBYQUERY, definebyqueryMode);
    70         MODECASE(STACKTOOL_MODE_UPDATERUN,     updaterunMode);
    71         MODECASE(STACKTOOL_MODE_INPUTS,        inputsMode);
    72         MODECASE(STACKTOOL_MODE_TODO,          todoMode);
    73         MODECASE(STACKTOOL_MODE_ADDRESULT,     addresultMode);
    74         MODECASE(STACKTOOL_MODE_RESULT,        resultMode);
    75         MODECASE(STACKTOOL_MODE_REVERT,        revertMode);
    76         MODECASE(STACKTOOL_MODE_UPDATERESULT,  updateresultMode);
    77         MODECASE(STACKTOOL_MODE_EXPORTRUN,     exportrunMode);
    78         MODECASE(STACKTOOL_MODE_IMPORTRUN,     importrunMode);
     62        MODECASE(STATICSKYTOOL_MODE_DEFINEBYQUERY,    definebyqueryMode);
     63        MODECASE(STATICSKYTOOL_MODE_UPDATERUN,         updaterunMode);
     64        MODECASE(STATICSKYTOOL_MODE_INPUTS,            inputsMode);
     65        MODECASE(STATICSKYTOOL_MODE_TODO,              todoMode);
     66        MODECASE(STATICSKYTOOL_MODE_ADDRESULT,         addresultMode);
     67        MODECASE(STATICSKYTOOL_MODE_RESULT,            resultMode);
     68        MODECASE(STATICSKYTOOL_MODE_REVERT,            revertMode);
     69        MODECASE(STATICSKYTOOL_MODE_UPDATERESULT,      updateresult);
     70        MODECASE(STATICSKYTOOL_MODE_EXPORTRUN,         exportrunMode);
     71        MODECASE(STATICSKYTOOL_MODE_IMPORTRUN,         importrunMode);
    7972        default:
    8073            psAbort("invalid option (this should not happen)");
     
    114107    PXOPT_LOOKUP_TIME(registered, config->args, "-set_registered", false, false);
    115108
    116     psMetadata *where = psMetadataAlloc();
    117     psMetadata *having = psMetadataAlloc(); // HAVING clause
    118 
    119     PXOPT_COPY_F32(config->args, where, "-select_good_frac_min", "stackSumSkyfile.good_frac", ">=");
    120     PXOPT_COPY_STR(config->args, where, "-select_skycell_id",    "stackRun.skycell_id",       "==");
    121     pxAddLabelSearchArgs(config, where, "-select_label",         "stackRun.label",            "LIKE");
    122     pxAddLabelSearchArgs(config, where, "-select_filter",        "stackRun.filter",           "LIKE");
    123 
    124     // these are used to build the HAVING restriction
    125     PXOPT_COPY_S32(config->args, having, "-min_num",    "num",        ">=");
    126     PXOPT_COPY_S32(config->args, having, "-min_filter", "num_filter", ">=");
     109    psMetadata *whereMD = psMetadataAlloc();
     110
     111    PXOPT_COPY_STR(config->args, whereMD, "-select_skycell_id",    "stackRun.skycell_id",       "==");
     112    PXOPT_COPY_STR(config->args, whereMD, "-select_tess_id",       "stackRun.tess_id",          "==");
     113    PXOPT_COPY_F32(config->args, whereMD, "-select_good_frac_min", "stackSumSkyfile.good_frac", ">=");
     114    pxAddLabelSearchArgs(config, whereMD, "-select_label",         "stackRun.label",            "LIKE");
     115    pxAddLabelSearchArgs(config, whereMD, "-select_filter",        "stackRun.filter",           "LIKE");
     116
     117    // find the number of requested filters:
     118    psMetadataItem *filters = psMetadataLookup(config->args, "-select_filter");
     119    psAssert (filters, "-select_filter must exist");
     120    psAssert (filters->type == PS_DATA_METADATA_MULTI, "-select_filter should be a multi container");
     121    psAssert (filters->data.list->n, "-select_filter should at least have a place-holder");
     122    int num_filter = filters->data.list->n;
     123    if (num_filter < 2) {
     124        psError(PXTOOLS_ERR_CONFIG, false, "invalid request: only 1 filter selected");
     125        psFree(whereMD);
     126        return false;
     127    }
    127128
    128129    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    129130    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    130 
    131     if (!psListLength(where->list)) {
    132         psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    133         psFree(where);
    134         psFree(having);
    135         return false;
    136     }
    137131
    138132    psString select = pxDataGet("staticskytool_definebyquery_select.sql");
    139133    if (!select) {
    140134        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    141         psFree(where);
    142         psFree(having);
    143         return false;
    144     }
    145 
    146     psString where1 = psStringCopy("");
    147     if (psListLength(where->list)) {
    148         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    149         psStringAppend(&where1, "\nAND %s", whereClause);
    150         psFree(whereClause);
    151     }
    152 
    153     psString where2 = psStringCopy("");
    154     if (label) {
    155         psStringAppend(&where2, "\nWHERE stackRun.label = '%s'", label);
    156     }
    157 
    158     // Restriction on aggregated quantities using HAVING
    159     {
    160         psString havingClause = NULL;   // HAVING string
    161         if (psListLength(having->list)) {
    162             havingClause = psDBGenerateWhereConditionSQL(having, NULL);
    163         }
    164 
    165         if (min_new > 0) {
    166             if (havingClause) {
    167                 psStringAppend(&havingClause, " AND");
    168             }
    169             psStringAppend(&havingClause,
    170                            " (num_warp - num_stack >= %d OR (num_warp >= %d AND num_stack IS NULL))",
    171                            min_new, min_new);
    172         }
    173         if (isfinite(min_frac)) {
    174             if (havingClause) {
    175                 psStringAppend(&havingClause, " AND");
    176             }
    177             // Avoiding division by zero
    178             psStringAppend(&havingClause, " (num_warp >= %f * num_stack OR num_stack IS NULL)",
    179                            (double)min_frac);
    180         }
    181         if (havingClause) {
    182             psStringAppend(&select, " HAVING %s", havingClause);
    183             psFree(havingClause);
    184         }
    185     }
    186     psFree(having);
    187 
    188     if (!p_psDBRunQueryF(config->dbh, select, where1, where2)) {
     135        psFree(whereMD);
     136        return false;
     137    }
     138
     139    if (!psListLength(whereMD->list)) {
     140        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     141        psFree(whereMD);
     142        return false;
     143    }
     144
     145    // this 'where' is used for both staticskytool_definebyquery_select.sql and staticskytool_definebyquery_inputs.sql
     146    psString where = NULL;
     147    psString whereClause = psDBGenerateWhereConditionSQL(whereMD, NULL);
     148    psStringAppend(&where, "\nAND %s", whereClause);
     149    psFree(whereClause);
     150    psFree(whereMD);
     151
     152    if (!p_psDBRunQueryF(config->dbh, select, where, num_filter)) {
    189153        psError(PS_ERR_UNKNOWN, false, "database error");
    190154        psFree(select);
    191         psFree(where1);
    192         psFree(where2);
    193155        return false;
    194156    }
    195157    psFree(select);
    196     psFree(where1);
    197     psFree(where2);
     158
     159    // we now have a list of (tess_id, skycell_id) that (potentially) meet out needs
     160    // we now need to loop over all of these and for each pair, select the best set of
     161    // inputs
    198162
    199163    psArray *output = p_psDBFetchResult(config->dbh);
     
    212176    }
    213177    if (!psArrayLength(output)) {
    214         psWarning("stacktool: no rows found");
     178        psWarning("staticskytool: no rows found");
    215179        psFree(output);
    216180        psFree(where);
     
    219183    if (pretend) {
    220184        // negative simple so the default is true
    221         if (!ippdbPrintMetadatas(stdout, output, "stackSkycells", !simple)) {
     185        if (!ippdbPrintMetadatas(stdout, output, "staticskyInput", !simple)) {
    222186            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    223187            psFree(output);
     
    230194    }
    231195
    232     psString insert = NULL;             // Insertion query
    233     if (randomLimit > 0) {
    234         insert = pxDataGet("stacktool_definebyquery_insert_random_part1.sql");
    235     } else {
    236         insert = pxDataGet("stacktool_definebyquery_insert.sql");
    237     }
    238     if (!insert) {
     196    psString inputsSQL = pxDataGet("staticskytool_definebyquery_inputs.sql");
     197    if (!inputsSQL) {
    239198        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    240199        psFree(where);
     
    242201    }
    243202
    244     if (psListLength(where->list)) {
    245         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    246         psStringAppend(&insert, " AND %s", whereClause);
    247         psFree(whereClause);
    248     }
    249     psFree(where);
    250 
    251     if (randomLimit > 0) {
    252         psString part2 = pxDataGet("stacktool_definebyquery_insert_random_part2.sql");
    253         if (!part2) {
    254             psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    255             psFree(insert);
    256             return false;
    257         }
    258         psStringAppend(&insert, "%s", part2);
    259         psFree(part2);
    260     }
    261 
    262     if (!psDBTransaction(config->dbh)) {
    263         psError(PS_ERR_UNKNOWN, false, "database error");
    264         psFree(where);
    265         return false;
    266     }
    267 
    268     psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    269203    for (long i = 0; i < output->n; i++) {
    270204        psMetadata *row = output->data[i]; // Row from select
     
    276210            psError(PS_ERR_UNKNOWN, false, "failed to lookup skycell_id");
    277211            psFree(output);
    278             psFree(insert);
    279             psFree(list);
     212            psFree(inputsSQL);
    280213            if (!psDBRollback(config->dbh)) {
    281214                psError(PS_ERR_UNKNOWN, false, "database error");
     
    288221            psError(PS_ERR_UNKNOWN, false, "failed to lookup tess_id");
    289222            psFree(output);
    290             psFree(insert);
    291             psFree(list);
     223            psFree(inputsSQL);
    292224            if (!psDBRollback(config->dbh)) {
    293225                psError(PS_ERR_UNKNOWN, false, "database error");
     
    296228        }
    297229
    298         psString filter = psMetadataLookupStr(&status, row, "filter");
    299         if (!status) {
    300             psError(PS_ERR_UNKNOWN, false, "failed to lookup filter");
     230        // query for the inputs
     231        if (!p_psDBRunQueryF(config->dbh, inputsSQL, tess_id, skycell_id, where)) {
     232            psError(PS_ERR_UNKNOWN, false, "database error");
    301233            psFree(output);
    302             psFree(insert);
    303             psFree(list);
    304             if (!psDBRollback(config->dbh)) {
    305                 psError(PS_ERR_UNKNOWN, false, "database error");
    306             }
    307             return false;
    308         }
    309 
    310         // create a new stackRun for this stack
    311         stackRunRow *run = stackRunRowAlloc(
    312             0,                          // ID
    313             "new",                      // state
    314             workdir,
    315             label,
    316             data_group ? data_group : label,
    317             dist_group,
    318             reduction,
    319             dvodb,
    320             registered,
    321             skycell_id,
    322             tess_id,
    323             filter,
    324             note);
    325 
    326         if (!stackRunInsertObject(config->dbh, run)) {
    327             if (!psDBRollback(config->dbh)) {
    328                 psError(PS_ERR_UNKNOWN, false, "database error");
    329             }
    330             psError(PS_ERR_UNKNOWN, false, "database error");
     234            psFree(inputsSQL);
     235            return false;
     236        }
     237       
     238        psArray *inputs = p_psDBFetchResult(config->dbh);
     239        if (!inputs) {
     240            psErrorCode err = psErrorCodeLast();
     241            switch (err) {
     242              case PS_ERR_DB_CLIENT:
     243                psError(PXTOOLS_ERR_SYS, false, "database error");
     244              case PS_ERR_DB_SERVER:
     245                psError(PXTOOLS_ERR_PROG, false, "database error");
     246              default:
     247                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     248            }
     249            psFree(inputs);
    331250            psFree(output);
    332             psFree(run);
    333             psFree(insert);
    334             psFree(list);
    335             if (!psDBRollback(config->dbh)) {
    336                 psError(PS_ERR_UNKNOWN, false, "database error");
    337             }
    338             return false;
    339         }
    340 
    341         // figure out the new stack_id
    342         psS64 stack_id = psDBLastInsertID(config->dbh);
    343         run->stack_id = stack_id;
    344 
    345         psArrayAdd(list, list->n, run);
    346         psFree(run);
    347 
    348         // Create a suitable insertion query for this run
    349         psString thisInsert = psStringCopy(insert);
    350         psString idString = NULL;
    351         psStringAppend(&idString, "%" PRId64, stack_id);
    352         psStringSubstitute(&thisInsert, idString, "@STACK_ID@");
    353         psFree(idString);
    354 
    355         // replace @FILTER@, @SKYCELL_ID@, @RANDOM_LIMIT@
    356         psStringSubstitute(&thisInsert, filter, "@FILTER@");
    357         psStringSubstitute(&thisInsert, skycell_id, "@SKYCELL_ID@");
    358 
    359         if (randomLimit > 0) {
    360           psString limString = NULL;
    361           psStringAppend(&limString, "%d", randomLimit);
    362           psStringSubstitute(&thisInsert, limString, "@RANDOM_LIMIT@");
    363           psFree(limString);
    364         }
    365 
    366         // XXX this insert uses a select to generate the list of warp_ids for the stack,
    367         // we have applied a set of criteria above (WHERE) to select the relevant warps
    368         // this insert needs to use exactly the same restrictions (race condition is probably not critical)
    369         // the insert below seems to only restrict matches to the skycell, tess, and filter
    370         if (!p_psDBRunQuery(config->dbh, thisInsert)) {
    371             psError(PS_ERR_UNKNOWN, false, "database error");
    372             psFree(thisInsert);
    373             psFree(insert);
     251            psFree(inputsSQL);
     252            return false;
     253        }
     254        if (!psArrayLength(inputs)) {
     255            psWarning("staticskytool ERROR: no rows found for known tess_id, skycell_id?");
     256            continue;
     257        }
     258
     259        // XXX if we are unable to guarantee that all selected inputs have all and only the
     260        // requested filters, see the code at the bottom of this file (ifdef'ed out for now)
     261
     262        // insert a new staticsky entry and find its new id
     263        if (!psDBTransaction(config->dbh)) {
     264            psError(PS_ERR_UNKNOWN, false, "database error");
     265            psFree(inputs);
    374266            psFree(output);
    375             psFree(list);
    376             if (!psDBRollback(config->dbh)) {
    377                 psError(PS_ERR_UNKNOWN, false, "database error");
    378             }
    379             return false;
    380         }
    381         psFree(thisInsert);
    382 
    383 # if (0)
    384         {
    385           psArray *output = p_psDBFetchResult(config->dbh);
    386           if (!output) {
    387             psErrorCode err = psErrorCodeLast();
    388             switch (err) {
    389             case PS_ERR_DB_CLIENT:
    390               psError(PXTOOLS_ERR_SYS, false, "database error");
    391             case PS_ERR_DB_SERVER:
    392               psError(PXTOOLS_ERR_PROG, false, "database error");
    393             default:
    394               psError(PXTOOLS_ERR_PROG, false, "unknown error");
    395             }
    396             psFree(where);
    397             return false;
    398           }
    399           if (!psArrayLength(output)) {
    400             psWarning("stacktool (definebyquery, insert): no rows found");
     267            psFree(inputsSQL);
     268            return false;
     269        }
     270
     271        // create a chipRun
     272        if (!staticskyRunInsert(config->dbh,
     273                                0x0,         // sky_id
     274                                "new",       // state
     275                                workdir,
     276                                label,
     277                                data_group,
     278                                dist_group,
     279                                reduction,
     280                                registered,
     281                                note
     282                )
     283            ) {
     284            if (!psDBRollback(config->dbh)) {
     285                psError(PS_ERR_UNKNOWN, false, "database error failed to rollback transaction");
     286            }
     287            psError(PS_ERR_UNKNOWN, false, "database error");
     288            psFree(inputs);
    401289            psFree(output);
    402             psFree(where);
    403             return true;
    404           }
    405           // negative simple so the default is true
    406           if (!ippdbPrintMetadatas(stdout, output, "stackSkycells", !simple)) {
    407             psError(PS_ERR_UNKNOWN, false, "failed to print array");
    408             psFree(output);
    409             psFree(where);
    410             return false;
    411           }
    412           psFree(output);
    413           psFree(where);
    414           return true;
    415         }
    416 # endif
    417 
    418     }
     290            psFree(inputsSQL);
     291            return false;
     292        }
     293
     294        psS64 sky_id =  psDBLastInsertID(config->dbh);
     295
     296        // loop over the possible inputs and record only the valid ones
     297        for (int j = 0; j < inputs->n; j++) {
     298            psMetadata *inputRow = inputs->data[j]; // Row from select
     299           
     300            // pull out the skycell_id, tess_id, filter
     301            psS64 stack_id = psMetadataLookupS64(&status, inputRow, "stack_id");
     302            psAssert(status, "failed to find stack_id?");
     303           
     304            // add a staticskyInput entry
     305            if (!staticskyInputInsert(config->dbh, sky_id, stack_id)) {
     306                if (!psDBRollback(config->dbh)) {
     307                    psError(PS_ERR_UNKNOWN, false, "database error failed to rollback transaction");
     308                }
     309                psError(PS_ERR_UNKNOWN, false, "database error");
     310                psFree(inputs);
     311                psFree(output);
     312                psFree(inputsSQL);
     313                return false;
     314            }
     315        }
     316        psFree (inputs);
     317       
     318        if (!psDBCommit(config->dbh)) {
     319            psError(PS_ERR_UNKNOWN, false, "database error");
     320            psFree(output);
     321            psFree(inputsSQL);
     322            return false;
     323        }
     324    }
     325    psFree(inputsSQL);
    419326    psFree(output);
    420 
    421     if (!psDBCommit(config->dbh)) {
    422         psError(PS_ERR_UNKNOWN, false, "database error");
    423         psFree(list);
    424         return false;
    425     }
    426 
    427     if (!stackRunPrintObjects(stdout, list, !simple)) {
    428         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    429         psFree(list);
    430         return false;
    431     }
    432     psFree(list);
    433327    return true;
    434328}
     
    438332    PS_ASSERT_PTR_NON_NULL(config, false);
    439333
    440 #ifdef notdef
    441     PXOPT_LOOKUP_S64(stack_id, config->args, "-stack_id", false, false);
    442     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    443     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    444 #endif
    445334    psMetadata *where = psMetadataAlloc();
    446     PXOPT_COPY_S64(config->args, where, "-stack_id",  "stack_id",   "==");
    447     PXOPT_COPY_STR(config->args, where, "-label",     "label",     "==");
    448     PXOPT_COPY_STR(config->args, where, "-state",     "state",     "==");
     335    PXOPT_COPY_S64(config->args, where, "-sky_id",  "sky_id",   "==");
     336    PXOPT_COPY_STR(config->args, where, "-label",   "label",    "==");
     337    PXOPT_COPY_STR(config->args, where, "-state",   "state",    "==");
    449338    if (!psListLength(where->list)) {
    450339        psFree(where);
     
    453342    }
    454343
    455     psString query = psStringCopy("UPDATE stackRun");
     344    psString query = psStringCopy("UPDATE staticskyRun");
    456345
    457346    // pxUpdateRun gets parameters from config->args and updates
    458     bool result = pxUpdateRun(config, where, &query, "stackRun", "stack_id", "stackSumSkyfile", true);
    459 
     347    bool result = pxUpdateRun(config, where, &query, "staticskyRun", "sky_id", "staticskyResult", true);
    460348    psFree(query);
    461349    psFree(where);
    462350
    463351    return result;
    464 
    465 #ifdef notdef
    466     // Hack-y work around to make stacktool more like the other tools, without breaking other stuff (hopefully).
    467 
    468     if ((state)&&(stack_id)) {
    469         // set detRun.state to state
    470         return setstackRunState(config, stack_id, state);
    471     }
    472 
    473     if ((state)&&(label)) {
    474       return setstackRunStateByLabel(config, label, state);
    475     }
    476 
    477     psError(PS_ERR_UNKNOWN, false, "Required options not found.");
    478     return false;
    479 #endif
    480352}
    481353
     
    485357    PS_ASSERT_PTR_NON_NULL(config, false);
    486358
    487     // XXX require at least a stack id (add better search options)
    488     PXOPT_LOOKUP_S64(stack_id, config->args, "-stack_id", true, false);
     359    // XXX require at least a sky id (add better search options)
     360    PXOPT_LOOKUP_S64(sky_id, config->args, "-sky_id", true, false);
    489361
    490362    psMetadata *where = psMetadataAlloc();
    491     PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
    492     PXOPT_COPY_S64(config->args, where, "-stack_id", "stack_id", "==");
     363    PXOPT_COPY_S64(config->args, where, "-sky_id", "sky_id", "==");
    493364
    494365    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    495366    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    496367
    497     psString query = pxDataGet("stacktool_inputskyfile.sql");
     368    psString query = pxDataGet("staticskytool_inputs.sql");
    498369    if (!query) {
    499370        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     
    502373
    503374    if (psListLength(where->list)) {
    504         psString whereClause = psDBGenerateWhereConditionSQL(where, "stackInputSkyfile");
    505         psStringAppend(&query, " AND %s", whereClause);
     375        psString whereClause = psDBGenerateWhereConditionSQL(where, "staticskyInput");
     376        psStringAppend(&query, " WHERE %s", whereClause);
    506377        psFree(whereClause);
    507378    }
     
    537408    }
    538409    if (!psArrayLength(output)) {
    539         psTrace("stacktool", PS_LOG_INFO, "no rows found");
     410        psTrace("staticskytool", PS_LOG_INFO, "no rows found");
    540411        psFree(output);
    541412        return true;
     
    544415    if (psArrayLength(output)) {
    545416        // negative simple so the default is true
    546         if (!ippdbPrintMetadatas(stdout, output, "stackInputSkyfile", !simple)) {
     417        if (!ippdbPrintMetadatas(stdout, output, "staticskyInput", !simple)) {
    547418            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    548419            psFree(output);
     
    561432    PS_ASSERT_PTR_NON_NULL(config, false);
    562433
    563     psMetadata *where = psMetadataAlloc();
    564     PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
    565     PXOPT_COPY_S64(config->args, where, "-stack_id", "stack_id", "==");
    566     pxAddLabelSearchArgs (config, where, "-label", "stackRun.label", "==");
     434    psMetadata *whereMD = psMetadataAlloc();
     435    PXOPT_COPY_S64(config->args, whereMD, "-sky_id", "sky_id", "==");
     436    pxAddLabelSearchArgs (config, whereMD, "-label", "staticskyRun.label", "==");
    567437
    568438    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    569439    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    570440
    571     psString query = pxDataGet("stacktool_tosum.sql");
     441    psString query = pxDataGet("staticskytool_todo.sql");
    572442    if (!query) {
    573443        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     
    575445    }
    576446
    577     if (psListLength(where->list)) {
    578         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    579         psStringAppend(&query, " AND %s", whereClause);
    580         psFree(whereClause);
    581     }
    582     psFree(where);
     447    if (!psListLength(whereMD->list)) {
     448        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     449        psFree(whereMD);
     450        return false;
     451    }
     452
     453    psString where = NULL;
     454    psString whereClause = psDBGenerateWhereConditionSQL(whereMD, NULL);
     455    psStringAppend(&where, "\n AND %s", whereClause);
     456    psFree(whereClause);
     457    psFree(whereMD);
    583458
    584459    // treat limit == 0 as "no limit"
     
    589464    }
    590465
    591     if (!p_psDBRunQuery(config->dbh, query)) {
     466    // the where clause is required and matches the WHERE hook format string
     467    if (!p_psDBRunQueryF(config->dbh, query, where)) {
    592468        psError(PS_ERR_UNKNOWN, false, "database error");
    593469        psFree(query);
     
    611487    }
    612488    if (!psArrayLength(output)) {
    613         psTrace("stacktool", PS_LOG_INFO, "no rows found");
     489        psTrace("staticskytool", PS_LOG_INFO, "no rows found");
    614490        psFree(output);
    615491        return true;
     
    618494    if (psArrayLength(output)) {
    619495        // negative simple so the default is true
    620         if (!ippdbPrintMetadatas(stdout, output, "stackSumSkyfile", !simple)) {
     496        if (!ippdbPrintMetadatas(stdout, output, "staticskyResult", !simple)) {
    621497            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    622498            psFree(output);
     
    626502
    627503    psFree(output);
    628 
    629504    return true;
    630505}
    631506
    632 
    633507static bool addresultMode(pxConfig *config)
    634508{
     
    636510
    637511    // required
     512    PXOPT_LOOKUP_S64(sky_id, config->args, "-sky_id", true, false);
    638513    PXOPT_LOOKUP_S64(stack_id, config->args, "-stack_id", true, false);
    639514
    640515    // optional
    641     PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
    642516    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
    643     PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
    644     PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
    645     PXOPT_LOOKUP_F32(dtime_stack, config->args, "-dtime_stack", false, false);
    646     PXOPT_LOOKUP_F32(dtime_match_mean, config->args, "-dtime_match_mean", false, false);
    647     PXOPT_LOOKUP_F32(dtime_match_stdev, config->args, "-dtime_match_stdev", false, false);
    648     PXOPT_LOOKUP_F32(dtime_initial, config->args, "-dtime_initial", false, false);
    649     PXOPT_LOOKUP_F32(dtime_reject, config->args, "-dtime_reject", false, false);
    650     PXOPT_LOOKUP_F32(dtime_final, config->args, "-dtime_final", false, false);
    651517    PXOPT_LOOKUP_F32(dtime_phot, config->args, "-dtime_phot", false, false);
    652518    PXOPT_LOOKUP_F32(dtime_script, config->args, "-dtime_script", false, false);
    653     PXOPT_LOOKUP_F32(match_mean, config->args, "-match_mean", false, false);
    654     PXOPT_LOOKUP_F32(match_stdev, config->args, "-match_stdev", false, false);
    655     PXOPT_LOOKUP_F32(match_rms, config->args, "-match_rms", false, false);
    656     PXOPT_LOOKUP_F32(stamps_mean, config->args, "-stamps_mean", false, false);
    657     PXOPT_LOOKUP_F32(stamps_stdev, config->args, "-stamps_stdev", false, false);
    658     PXOPT_LOOKUP_S32(stamps_min, config->args, "-stamps_min", false, false);
    659     PXOPT_LOOKUP_S32(reject_images, config->args, "-reject_images", false, false);
    660     PXOPT_LOOKUP_F32(reject_pix_mean, config->args, "-reject_pix_mean", false, false);
    661     PXOPT_LOOKUP_F32(reject_pix_stdev, config->args, "-reject_pix_stdev", false, false);
    662519    PXOPT_LOOKUP_S32(sources, config->args, "-sources", false, false);
    663520    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
     
    668525    PXOPT_LOOKUP_S16(quality, config->args, "-quality", false, false);
    669526
     527    // XXX not sure we need a transaction here...
    670528    if (!psDBTransaction(config->dbh)) {
    671529        psError(PS_ERR_UNKNOWN, false, "database error");
     
    673531    }
    674532
    675     // XXX need to validate the stack_id here
    676     // XXX instead of validiting it here we should just use forgein key
    677     // constrants
    678     if (!stackSumSkyfileInsert(config->dbh,
     533    if (!staticskyResultInsert(config->dbh,
     534                               sky_id,
    679535                               stack_id,
    680                                uri,
    681536                               path_base,
    682                                bg,
    683                                bg_stdev,
    684                                dtime_stack,
    685                                dtime_match_mean,
    686                                dtime_match_stdev,
    687                                dtime_initial,
    688                                dtime_reject,
    689                                dtime_final,
    690537                               dtime_phot,
    691538                               dtime_script,
    692                                match_mean,
    693                                match_stdev,
    694                                match_rms,
    695                                stamps_mean,
    696                                stamps_stdev,
    697                                stamps_min,
    698                                reject_images,
    699                                reject_pix_mean,
    700                                reject_pix_stdev,
    701539                               sources,
    702540                               hostname,
     
    712550    }
    713551
    714     if (fault == 0) {
    715         if (!setstackRunState(config, stack_id, "full")) {
    716             if (!psDBRollback(config->dbh)) {
    717                 psError(PS_ERR_UNKNOWN, false, "database error");
    718             }
    719             psError(PS_ERR_UNKNOWN, false, "failed to change stackRun's state");
    720             return false;
    721         }
    722     }
    723 
    724552    // point of no return
    725553    if (!psDBCommit(config->dbh)) {
     
    731559}
    732560
    733 
     561// XXX what is this used by?  what filters are needed?
    734562static bool resultMode(pxConfig *config)
    735563{
     
    737565
    738566    psMetadata *where = psMetadataAlloc();
    739     PXOPT_COPY_S64(config->args, where, "-stack_id", "stackSumSkyfile.stack_id", "==");
    740     PXOPT_COPY_STR(config->args, where, "-tess_id", "stackRun.tess_id", "==");
    741     PXOPT_COPY_STR(config->args, where, "-skycell_id", "stackRun.skycell_id", "==");
    742     PXOPT_COPY_STR(config->args, where, "-filter", "stackRun.filter", "LIKE");
    743     PXOPT_COPY_STR(config->args, where, "-label", "stackRun.label", "==");
    744     PXOPT_COPY_STR(config->args, where, "-data_group", "stackRun.data_group", "LIKE");
    745     PXOPT_COPY_S16(config->args, where, "-fault", "stackSumSkyfile.fault", "==");
    746 
    747 //  The following three selectors are incompatible with the sql so omit them
    748 //    PXOPT_COPY_S64(config->args, where, "-warp_id", "warpRun.warp_id", "==");
    749 //     PXOPT_COPY_S64(config->args, where, "-exp_id", "rawExp.exp_id", "==");
    750 //    PXOPT_COPY_STR(config->args, where, "-exp_name", "rawExp.exp_name", "==");
     567    PXOPT_COPY_S64(config->args, where, "-sky_id",     "staticskyRun.sky_id", "==");
     568    PXOPT_COPY_STR(config->args, where, "-label",      "staticskyRun.label", "==");
     569    PXOPT_COPY_STR(config->args, where, "-data_group", "staticskyRun.data_group", "LIKE");
     570    PXOPT_COPY_S16(config->args, where, "-fault",      "staticskyResult.fault", "==");
    751571
    752572    PXOPT_LOOKUP_BOOL(all, config->args, "-all", false);
     
    755575    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    756576
    757     psString query = pxDataGet("stacktool_sumskyfile.sql");
     577    psString query = pxDataGet("staticskytool_result.sql");
    758578    if (!query) {
    759579        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     
    801621    }
    802622    if (!psArrayLength(output)) {
    803         psTrace("stacktool", PS_LOG_INFO, "no rows found");
     623        psTrace("staticskytool", PS_LOG_INFO, "no rows found");
    804624        psFree(output);
    805625        return true;
     
    807627
    808628    if (psArrayLength(output)) {
    809         if (!ippdbPrintMetadatas(stdout, output, "stackSumSkyfile", !simple)) {
     629        if (!ippdbPrintMetadatas(stdout, output, "staticskyResult", !simple)) {
    810630            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    811631            psFree(output);
     
    819639}
    820640
    821 
    822641static bool revertMode(pxConfig *config)
    823642{
    824643    PS_ASSERT_PTR_NON_NULL(config, false);
    825644
     645# if (0)
    826646    psMetadata *where = psMetadataAlloc();
    827647    PXOPT_COPY_S64(config->args, where, "-stack_id", "stackSumSkyfile.stack_id", "==");
     
    836656
    837657    // Delete product
    838     psString delete = pxDataGet("stacktool_revertsumskyfile_delete.sql");
     658    psString delete = pxDataGet("staticskytool_revertsumskyfile_delete.sql");
    839659    if (!delete) {
    840660        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     
    857677
    858678    int numRows = psDBAffectedRows(config->dbh); // Number of row affected
    859     psLogMsg("stacktool", PS_LOG_INFO, "Deleted %d rows", numRows);
     679    psLogMsg("staticskytool", PS_LOG_INFO, "Deleted %d rows", numRows);
    860680
    861681    psFree(where);
     682# endif
    862683
    863684    return true;
    864685}
    865686
    866 
    867 static bool updateresultState(pxConfig *config, psS64 stack_id, const char *state)
    868 {
    869     PS_ASSERT_PTR_NON_NULL(state, false);
    870 
     687# if (0)
     688static bool updateresultState(pxConfig *config)
     689{
    871690    // check that state is a valid string value
    872691    if (!pxIsValidState(state)) {
     
    881700        return false;
    882701    }
    883 
    884702    return true;
    885703}
    886 
    887 static bool updateresultMode(pxConfig *config)
     704# endif
     705
     706static bool updateresult(pxConfig *config)
    888707{
    889708    PS_ASSERT_PTR_NON_NULL(config, false);
    890709
     710# if (0)
    891711    PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    892712
     
    901721    psFree (where);
    902722
     723# endif
    903724    return true;
    904725}
     
    906727bool exportrunMode(pxConfig *config)
    907728{
     729# if (0)
    908730  typedef struct ExportTable {
    909731    char tableName[80];
     
    934756
    935757  ExportTable tables [] = {
    936     {"stackRun", "stacktool_export_run.sql"},
    937     {"stackInputSkyfile", "stacktool_export_input_skyfile.sql"},
    938     {"stackSumSkyfile", "stacktool_export_sum_skyfile.sql"},
     758    {"stackRun", "staticskytool_export_run.sql"},
     759    {"stackInputSkyfile", "staticskytool_export_input_skyfile.sql"},
     760    {"stackSumSkyfile", "staticskytool_export_sum_skyfile.sql"},
    939761  };
    940762
     
    999821    fclose (f);
    1000822
     823# endif
    1001824    return true;
    1002825}
     
    1004827bool importrunMode(pxConfig *config)
    1005828{
     829# if (0)
    1006830  unsigned int nFail;
    1007831
     
    1072896  }
    1073897
     898# endif
    1074899  return true;
    1075900}
     901
     902# if (0)
     903        // now we need to loop over all requested filters and check that each is found
     904        // XXX is this needed?  haven't we required that we only match the requested filters
     905        // and that N unique filters are found?
     906
     907        psVector *found = psVectorAlloc(inputs->n, PS_TYPE_U8);
     908        psVectorInit (found, 0);
     909
     910        psMetadataItem *filter = NULL;
     911        psListIterator *iter = psListIteratorAlloc (filters->data.list, PS_LIST_HEAD, false);
     912        while ((filter = psListGetAndIncrement(iter))) {
     913
     914            bool foundOne = false;
     915
     916            psAssert (filter->type == PS_DATA_STR, "filter is not a string?");
     917            for (int j = 0; !foundOne && (j < inputs->n); j++) {
     918                if (found->data.U8[j]) continue;
     919                psMetadata *inputRow = inputs->data[j]; // Row from select
     920
     921                // pull out the skycell_id, tess_id, filter
     922                psString inFilter = psMetadataLookupStr(&status, row, "filter");
     923                psAssert (status);
     924
     925                found->data.U8[j] = true;
     926                foundOne = true;
     927            }
     928            if (!foundOne) {
     929                // this required filter was not found in the inputs, skip the entry
     930                skip();
     931            }
     932        }           
     933# endif
     934
Note: See TracChangeset for help on using the changeset viewer.