IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
         1*.la
         2*.lo
        13.deps
        24.gdb_history
         
        46Makefile
        57Makefile.in
         8addtool
         9caltool
         10camtool
         11chiptool
        612config.h
        713config.h.in
        8 stamp-h1
        9 *.la
        10 *.lo
         14detselect
         15dettool
         16difftool
         17disttool
         18dqstatstool
         19faketool
         20flatcorr
         21guidetool
         22magicdstool
         23magictool
         24pstamptool
         25pubtool
         26pxadmin
         27pxdata.c
         28pxinject
        1129pxtoolsErrorCodes.c
        1230pxtoolsErrorCodes.h
        13 pxadmin
        14 pxinject
        15 pztool
        1631pzgetexp
        1732pzgetimfiles
         33pztool
         34receivetool
        1835regtool
        19 guidetool
        20 chiptool
        21 camtool
         36stacktool
         37stamp-h1
        2238warptool
        23 difftool
        24 stacktool
        25 faketool
        26 dettool
        27 detselect
        28 pxdata.c
        29 magictool
        30 magicdstool
        31 caltool
        32 flatcorr
        33 pstamptool
        34 disttool
        35 receivetool
        36 
        37 pubtool
  • branches/simtest_nebulous_branches/ippTools/src/chiptool.c

    r24562 r27840  
    3636
    3737static bool definebyqueryMode(pxConfig *config);
     38static bool definecopyMode(pxConfig *config);
    3839static bool updaterunMode(pxConfig *config);
    3940static bool pendingimfileMode(pxConfig *config);
     
    5455static bool tofullimfileMode(pxConfig *config);
    5556static bool topurgedimfileMode(pxConfig *config);
     57static bool toscrubbedimfileMode(pxConfig *config);
    5658static bool exportrunMode(pxConfig *config);
    5759static bool importrunMode(pxConfig *config);
     60static bool runstateMode(pxConfig *config);
     61static bool setimfiletoupdateMode(pxConfig *config);
     62static bool listrunMode(pxConfig *config);
    5863
    5964# define MODECASE(caseName, func) \
     
    7580    switch (config->mode) {
    7681        MODECASE(CHIPTOOL_MODE_DEFINEBYQUERY,           definebyqueryMode);
     82        MODECASE(CHIPTOOL_MODE_DEFINECOPY,              definecopyMode);
    7783        MODECASE(CHIPTOOL_MODE_UPDATERUN,               updaterunMode);
    7884        MODECASE(CHIPTOOL_MODE_PENDINGIMFILE,           pendingimfileMode);
     
    9399        MODECASE(CHIPTOOL_MODE_TOFULLIMFILE,            tofullimfileMode);
    94100        MODECASE(CHIPTOOL_MODE_TOPURGEDIMFILE,          topurgedimfileMode);
     101        MODECASE(CHIPTOOL_MODE_TOSCRUBBEDIMFILE,        toscrubbedimfileMode);
    95102        MODECASE(CHIPTOOL_MODE_EXPORTRUN,               exportrunMode);
    96103        MODECASE(CHIPTOOL_MODE_IMPORTRUN,               importrunMode);
     104        MODECASE(CHIPTOOL_MODE_RUNSTATE,                runstateMode);
     105        MODECASE(CHIPTOOL_MODE_SETIMFILETOUPDATE,       setimfiletoupdateMode);
     106        MODECASE(CHIPTOOL_MODE_LISTRUN,                 listrunMode);
    97107        default:
    98108            psAbort("invalid option (this should not happen)");
     
    116126}
    117127
     128// Queue exposures for chip processing
     129static bool queue_exposures(pxConfig *config,  // Configuration
     130                            const psArray *exps, // Exposures information
     131                            const char *workdir, // Working directory, or NULL to inherit
     132                            const char *label,   // Label, or NULL to inherit
     133                            const char *data_group, // Data group, or NULL to inherit
     134                            const char *dist_group, // Distribution group, or NULL to inherit
     135                            const char *reduction, // Reduction class, or NULL to inherit
     136                            const char *expgroup,  // Exposure group
     137                            const char *dvodb,     // DVO database, or NULL to inherit
     138                            const char *tess_id,   // Tessellation identifier, or NULL to inherit
     139                            const char *end_stage,  // End stage, or NULL to inherit
     140                            const char *note        // Note
     141    )
     142{
     143    // start a transaction so we don't end up with an exp without any associted
     144    // imfiles
     145    if (!psDBTransaction(config->dbh)) {
     146        psError(PS_ERR_UNKNOWN, false, "database error");
     147        return false;
     148    }
     149
     150    // if end_stage is warp (or NULL), check for valid tess_id
     151    for (long i = 0; i < psArrayLength(exps); i++) {
     152        psMetadata *md = exps->data[i];
     153
     154        bool status;
     155        char *end_stage = psMetadataLookupStr(&status, md, "end_stage");
     156        if (end_stage && strcasecmp(end_stage, "warp")) continue;
     157
     158        char *raw_tess_id   = psMetadataLookupStr(&status, md, "tess_id");
     159        if (raw_tess_id || tess_id) continue;
     160
     161        char *label  = psMetadataLookupStr(&status, md, "label");
     162        psS64 exp_id = psMetadataLookupS64(&status, md, "exp_id");
     163
     164        if (!status) {
     165            psError(PS_ERR_UNKNOWN, false,
     166                    "cannot queue analysis to WARP without a defined tess id: label: %s, exp_id %" PRId64,
     167                    label, exp_id);
     168            return false;
     169        }
     170    }
     171
     172
     173# define GET_VALUE(PTYPE,CTYPE,VALUE,NAME)                              \
     174    PTYPE VALUE;                                                        \
     175    {                                                                   \
     176        bool status;                                                    \
     177        VALUE = psMetadataLookup##CTYPE(&status, md, NAME);             \
     178        if (!status) {                                                  \
     179            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for %s", NAME); \
     180            return false;                                               \
     181        }                                                               \
     182    }
     183
     184    // loop over our list of exp_ids
     185    for (long i = 0; i < psArrayLength(exps); i++) {
     186        psMetadata *md = exps->data[i];
     187
     188        rawExpRow *row = rawExpObjectFromMetadata(md);
     189        if (!row) {
     190            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
     191            return false;
     192        }
     193
     194        GET_VALUE (psS64,    S64, exp_id,        "exp_id");
     195        GET_VALUE (psString, Str, raw_workdir,   "workdir");
     196        GET_VALUE (psString, Str, raw_label,     "label");
     197        GET_VALUE (psString, Str, raw_reduction, "reduction");
     198        // GET_VALUE (psString, Str, raw_expgroup,  "expgroup");
     199        GET_VALUE (psString, Str, raw_dvodb,     "dvodb");
     200        GET_VALUE (psString, Str, raw_tess_id,   "tess_id");
     201        GET_VALUE (psString, Str, raw_end_stage, "end_stage");
     202
     203        if (!row->exp_id) {
     204            psError(PS_ERR_UNKNOWN, false, "failed to find value for exp_id");
     205            return false;
     206        }
     207
     208        // queue the exp
     209        if (!pxchipQueueByExpTag(config,
     210                                 exp_id,
     211                                 workdir     ? workdir   : raw_workdir,
     212                                 label       ? label     : raw_label,
     213                                 data_group  ? data_group : (label ? label : raw_label),
     214                                 dist_group,
     215                                 reduction   ? reduction : raw_reduction,
     216                                 // expgroup    ? expgroup  : raw_expgroup,
     217                                 // XXX how does expgroup get defined?
     218                                 expgroup,
     219                                 dvodb       ? dvodb     : raw_dvodb,
     220                                 tess_id     ? tess_id   : raw_tess_id,
     221                                 end_stage   ? end_stage : raw_end_stage,
     222                                 note
     223                                 )) {
     224            if (!psDBRollback(config->dbh)) {
     225                psError(PS_ERR_UNKNOWN, false, "database error");
     226            }
     227            psError(PS_ERR_UNKNOWN, false,
     228                    "failed to trying to queue exp_id: %" PRId64, exp_id);
     229            return false;
     230        }
     231    }
     232
     233    if (!psDBCommit(config->dbh)) {
     234        psError(PS_ERR_UNKNOWN, false, "database error");
     235        return false;
     236    }
     237
     238    return true;
     239}
    118240
    119241static bool definebyqueryMode(pxConfig *config)
     
    125247    psMetadata *where = psMetadataAlloc();
    126248    pxchipGetSearchArgs (config, where); // rawExp only
    127     pxAddLabelSearchArgs (config, where, "-label", "newExp.label", "LIKE");
     249    pxAddLabelSearchArgs (config, where, "-label", "newExp.label", "LIKE"); // define using newExp label
    128250
    129251    // psListLength(where->list) is at least 1 because exp_type defaults to "object"
     
    131253    if ((psListLength(where->list) <= 1) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    132254        psFree(where);
    133         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     255        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    134256        return false;
    135257    }
     
    142264    PXOPT_LOOKUP_STR(tess_id, config->args, "-set_tess_id", false, false);
    143265    PXOPT_LOOKUP_STR(end_stage, config->args, "-set_end_stage", false, false);
     266    PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false);
     267    PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     268    PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
    144269
    145270    // default
     
    150275    psString query = pxDataGet("chiptool_find_rawexp.sql");
    151276    if (!query) {
    152         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     277        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    153278        psFree(where);
    154279        return false;
     
    190315    }
    191316
    192     // start a transaction so we don't end up with an exp without any associted
    193     // imfiles
    194     if (!psDBTransaction(config->dbh)) {
    195         psError(PS_ERR_UNKNOWN, false, "database error");
    196         psFree(output);
    197         return false;
    198     }
    199 
    200     // if end_stage is warp (or NULL), check for valid tess_id
    201     for (long i = 0; i < psArrayLength(output); i++) {
    202         psMetadata *md = output->data[i];
    203 
    204         bool status;
    205         char *end_stage = psMetadataLookupStr(&status, md, "end_stage");
    206         if (end_stage && strcasecmp(end_stage, "warp")) continue;
    207 
    208         char *raw_tess_id   = psMetadataLookupStr(&status, md, "tess_id");
    209         if (raw_tess_id || tess_id) continue;
    210 
    211         char *label  = psMetadataLookupStr(&status, md, "label");
    212         psS64 exp_id = psMetadataLookupS64(&status, md, "exp_id");
    213 
    214         if (!status) {
    215             psError(PS_ERR_UNKNOWN, false, "cannot queue analysis to WARP without a defined tess id: label: %s, exp_id %" PRId64, label, exp_id);
     317    if (!queue_exposures(config, output, workdir, label, data_group, dist_group, reduction, expgroup,
     318                         dvodb, tess_id, end_stage, note)) {
     319        psError(psErrorCodeLast(), false, "Unable to queue exposures for chip.");
     320        psFree(output);
     321        return false;
     322    }
     323    psFree(output);
     324
     325    return true;
     326}
     327
     328static bool definecopyMode(pxConfig *config)
     329{
     330    PS_ASSERT_PTR_NON_NULL(config, NULL);
     331
     332    PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", false, false);
     333
     334    psMetadata *where = psMetadataAlloc();
     335    pxchipGetSearchArgs (config, where); // rawExp only
     336    pxAddLabelSearchArgs (config, where, "-label", "chipOld.label", "LIKE");
     337
     338    // psListLength(where->list) is at least 1 because exp_type defaults to "object"
     339    // so we require a list longer than 1 entry
     340    if ((psListLength(where->list) <= 1) && !psMetadataLookupBool(NULL, config->args, "-all")) {
     341        psFree(where);
     342        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     343        return false;
     344    }
     345
     346    PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", true, false);
     347    PXOPT_LOOKUP_STR(label, config->args, "-set_label", true, false);
     348    PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
     349    PXOPT_LOOKUP_STR(expgroup, config->args, "-set_expgroup", false, false);
     350    PXOPT_LOOKUP_STR(dvodb, config->args, "-set_dvodb", false, false);
     351    PXOPT_LOOKUP_STR(tess_id, config->args, "-set_tess_id", false, false);
     352    PXOPT_LOOKUP_STR(end_stage, config->args, "-set_end_stage", false, false);
     353    PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false);
     354    PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     355    PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
     356
     357    // default
     358    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     359    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     360
     361    // find the exp_id of all the exposures that we want to queue up.
     362    psString query = pxDataGet("chiptool_definecopy.sql");
     363    if (!query) {
     364        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     365        psFree(where);
     366        return false;
     367    }
     368
     369    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     370    psStringAppend(&query, " AND %s", whereClause);
     371
     372    psFree(whereClause);
     373    psFree(where);
     374
     375    if (!p_psDBRunQueryF(config->dbh, query, label)) {
     376        psError(PS_ERR_UNKNOWN, false, "database error");
     377        psFree(query);
     378        return false;
     379    }
     380    psFree(query);
     381
     382    psArray *output = p_psDBFetchResult(config->dbh);
     383    if (!output) {
     384        psError(PS_ERR_UNKNOWN, false, "database error");
     385        return false;
     386    }
     387    if (!psArrayLength(output)) {
     388        psTrace("chiptool", PS_LOG_INFO, "no rows found");
     389        psFree(output);
     390        return true;
     391    }
     392
     393    if (pretend) {
     394        // negative simple so the default is true
     395        if (!ippdbPrintMetadatas(stdout, output, "rawExp", !simple)) {
     396            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    216397            psFree(output);
    217398            return false;
    218399        }
    219     }
    220 
    221 
    222 # define GET_VALUE(PTYPE,CTYPE,VALUE,NAME)                              \
    223     PTYPE VALUE;                                                        \
    224     { bool status;                                                      \
    225         VALUE = psMetadataLookup##CTYPE(&status, md, NAME);             \
    226         if (!status) {                                                  \
    227             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for %s", NAME); \
    228             psFree(output);                                             \
    229             return false;                                               \
    230         } }
    231 
    232     // loop over our list of exp_ids
    233     for (long i = 0; i < psArrayLength(output); i++) {
    234         psMetadata *md = output->data[i];
    235 
    236         rawExpRow *row = rawExpObjectFromMetadata(md);
    237         if (!row) {
    238             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
    239             psFree(output);
    240             return false;
    241         }
    242 
    243         GET_VALUE (psS64,    S64, exp_id,        "exp_id");
    244         GET_VALUE (psString, Str, raw_workdir,   "workdir");
    245         GET_VALUE (psString, Str, raw_label,     "label");
    246         GET_VALUE (psString, Str, raw_reduction, "reduction");
    247         // GET_VALUE (psString, Str, raw_expgroup,  "expgroup");
    248         GET_VALUE (psString, Str, raw_dvodb,     "dvodb");
    249         GET_VALUE (psString, Str, raw_tess_id,   "tess_id");
    250         GET_VALUE (psString, Str, raw_end_stage, "end_stage");
    251 
    252         if (!row->exp_id) {
    253             psError(PS_ERR_UNKNOWN, false, "failed to find value for exp_id");
    254             psFree(output);
    255             return false;
    256         }
    257 
    258         // queue the exp
    259         if (!pxchipQueueByExpTag(config,
    260                                  exp_id,
    261                                  workdir     ? workdir   : raw_workdir,
    262                                  label       ? label     : raw_label,
    263                                  reduction   ? reduction : raw_reduction,
    264                                  // expgroup    ? expgroup  : raw_expgroup,
    265                                  // XXX how does expgroup get defined?
    266                                  expgroup,
    267                                  dvodb       ? dvodb     : raw_dvodb,
    268                                  tess_id     ? tess_id   : raw_tess_id,
    269                                  end_stage   ? end_stage : raw_end_stage
    270                                  )) {
    271             if (!psDBRollback(config->dbh)) {
    272                 psError(PS_ERR_UNKNOWN, false, "database error");
    273             }
    274             psError(PS_ERR_UNKNOWN, false,
    275                     "failed to trying to queue exp_id: %" PRId64, exp_id);
    276             psFree(output);
    277             return false;
    278         }
     400        psFree(output);
     401        return true;
     402    }
     403
     404    if (!queue_exposures(config, output, workdir, label, data_group, dist_group, reduction, expgroup,
     405                         dvodb, tess_id, end_stage, note)) {
     406        psError(psErrorCodeLast(), false, "Unable to queue exposures for chip.");
     407        psFree(output);
     408        return false;
    279409    }
    280410    psFree(output);
    281 
    282     if (!psDBCommit(config->dbh)) {
    283         psError(PS_ERR_UNKNOWN, false, "database error");
    284         return false;
    285     }
    286411
    287412    return true;
     
    299424    PXOPT_COPY_STR(config->args,  where, "-label",   "chipRun.label",   "==");
    300425    PXOPT_COPY_STR(config->args,  where, "-state",   "chipRun.state",   "==");
    301 
    302     if (!psListLength(where->list) &&
    303         !psMetadataLookupBool(NULL, config->args, "-all")) {
     426    PXOPT_COPY_STR(config->args,  where, "-data_group", "chipRun.data_group",   "==");
     427    PXOPT_COPY_STR(config->args,  where, "-dist_group", "chipRun.dist_group",   "==");
     428
     429    if (!psListLength(where->list)) {
    304430        psFree(where);
    305         where = NULL;
    306         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
    307         return false;
    308     }
    309 
    310     PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    311     PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
    312 
    313     if ((!state) && (!label)) {
    314         psError(PXTOOLS_ERR_DATA, false, "parameters are required");
    315         psFree(where);
    316         return false;
    317     }
    318 
    319     if (state) {
    320         // set chipRun.state to state
    321         if (!pxchipRunSetStateByQuery(config, where, state)) {
    322             psFree(where);
    323             return false;
    324         }
    325     }
    326 
    327     if (label) {
    328         // set chipRun.label to label
    329         if (!pxchipRunSetLabelByQuery(config, where, label)) {
    330             psFree(where);
    331             return false;
    332         }
    333     }
    334 
     431        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     432        return false;
     433    }
     434    psString query = psStringCopy("UPDATE chipRun JOIN rawExp USING(exp_id) -- join hook %s\n");
     435
     436    // pxUpdateRun gets parameters from config->args and updates
     437    bool result = pxUpdateRun(config, where, &query, "chipRun", "chip_id", "chipProcessedImfile", true);
     438    if (!result) {
     439        psError(psErrorCodeLast(), false, "pxUpdateRun failed");
     440    }
     441
     442    psFree(query);
    335443    psFree(where);
    336444
    337     return true;
     445    return result;
    338446}
    339447
     
    353461    psString query = pxDataGet("chiptool_pendingimfile.sql");
    354462    if (!query) {
    355         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     463        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    356464        return false;
    357465    }
     
    593701    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    594702    PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
     703    PXOPT_LOOKUP_BOOL(allfiles, config->args, "-allfiles", false);
     704    if (allfiles) {
     705        faulted = false;
     706    }
    595707
    596708    psMetadata *where = psMetadataAlloc();
     
    601713    PXOPT_COPY_STR(config->args, where, "-reduction", "chipRun.reduction", "==");
    602714    pxAddLabelSearchArgs (config, where, "-label", "chipRun.label", "LIKE");
    603     PXOPT_COPY_S64(config->args, where, "-magicked", "chipRun.magicked", "==");
    604 
    605     if (!psListLength(where->list) &&
    606         !psMetadataLookupBool(NULL, config->args, "-all")) {
    607         psFree(where);
    608         psError(PXTOOLS_ERR_DATA, false, "search parameters (or -all) are required");
     715    pxAddLabelSearchArgs (config, where, "-data_group", "chipRun.data_group", "LIKE");
     716    PXOPT_COPY_S64(config->args, where, "-magicked", "chipProcessedImfile.magicked", "==");
     717
     718    psString where2 = NULL;
     719    pxmagicAddWhere(config, &where2, "chipProcessedImfile");
     720    // add cuts on ra and decl if supplied
     721    if (!pxspaceAddWhere(config, &where2, "rawExp")) {
     722        psError(psErrorCodeLast(), false, "pxSpaceAddWhere failed");
    609723        return false;
    610724    }
     
    612726    psString query = pxDataGet("chiptool_processedimfile.sql");
    613727    if (!query) {
    614         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    615         return false;
    616     }
    617 
    618     if (where && psListLength(where->list)) {
     728        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     729        return false;
     730    }
     731
     732    if (psListLength(where->list)) {
    619733        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    620         psStringAppend(&query, " WHERE %s", whereClause);
     734        psStringAppend(&query, " WHERE %s %s", whereClause, where2 ? where2 : "");
    621735        psFree(whereClause);
     736    } else if (psMetadataLookupBool(NULL, config->args, "-all") || (faulted || where2)) {
     737        psStringAppend(&query, " WHERE chipRun.chip_id IS NOT NULL %s", where2 ? where2 : "");
     738    } else {
     739        psFree(where);
     740        psError(PXTOOLS_ERR_CONFIG, false, "search parameters (or -all) are required");
     741        return false;
    622742    }
    623743    psFree(where);
     
    626746        // list only faulted rows
    627747        psStringAppend(&query, " %s", "AND chipProcessedImfile.fault != 0");
    628     } else {
     748    } else if (!allfiles) {
    629749        // don't list faulted rows
    630750        psStringAppend(&query, " %s", "AND chipProcessedImfile.fault = 0");
     
    683803        && !psMetadataLookupBool(NULL, config->args, "-all")) {
    684804        psFree(where);
    685         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     805        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    686806        return false;
    687807    }
     
    689809    psString query = pxDataGet("chiptool_revertprocessedimfile.sql");
    690810    if (!query) {
    691         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     811        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     812        psFree(where);
     813        return false;
     814    }
     815    psString query_update = pxDataGet("chiptool_revertupdatedimfile.sql");
     816    if (!query) {
     817        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    692818        psFree(where);
    693819        return false;
     
    697823        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    698824        psStringAppend(&query, " AND %s", whereClause);
     825        psStringAppend(&query_update, " AND %s", whereClause);
    699826        psFree(whereClause);
    700827    }
     
    708835    }
    709836    psFree(query);
     837    if (!p_psDBRunQuery(config->dbh, query_update)) {
     838        psError(PS_ERR_UNKNOWN, false, "database error");
     839        psFree(query_update);
     840        return false;
     841    }
     842    psFree(query_update);
    710843
    711844    return true;
     
    719852    PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
    720853    PXOPT_COPY_STR(config->args, where, "-class_id", "class_id", "==");
    721     PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    722 
    723     if (!pxSetFaultCode(config->dbh, "chipProcessedImfile", where, fault)) {
     854    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, NULL);
     855    if (psListLength(where->list) == 0) {
     856        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
     857        return false;
     858    }
     859
     860    if (!state) {
     861      PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
     862
     863      if (!pxSetFaultCode(config->dbh, "chipProcessedImfile", where, fault)) {
    724864        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
    725865        return false;
    726     }
    727     psFree(where);
     866      }
     867      psFree(where);
     868    }
     869    else {
     870      if (!pxchipProcessedImfileSetStateByQuery(config,where,state)) {
     871        psError(PS_ERR_UNKNOWN, false, "failed to set chipProcessedImfile state");
     872        return(false);
     873      }
     874    }
     875
    728876
    729877    return true;
     
    828976    psString query = pxDataGet("chiptool_unmasked.sql");
    829977    if (!query) {
    830         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     978        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    831979        return false;
    832980    }
     
    9061054    psString query = pxDataGet("chiptool_pendingcleanuprun.sql");
    9071055    if (!query) {
    908         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1056        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    9091057        return false;
    9101058    }
     
    9701118    psString query = pxDataGet("chiptool_pendingcleanupimfile.sql");
    9711119    if (!query) {
    972         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1120        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    9731121        return false;
    9741122    }
     
    10311179    psString query = pxDataGet("chiptool_donecleanup.sql");
    10321180    if (!query) {
    1033         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1181        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    10341182        return false;
    10351183    }
     
    10891237    // make sure that the state string is valid
    10901238    if (!pxIsValidState(state)) {
    1091         psError(PXTOOLS_ERR_DATA, false, "%s is not a valid state", state);
     1239        psError(PXTOOLS_ERR_CONFIG, false, "%s is not a valid state", state);
    10921240        return false;
    10931241    }
    10941242
    10951243    psMetadata *where = psMetadataAlloc();
     1244    pxchipGetSearchArgs (config, where); // chipRun, chipProcessedImfile, rawExp
     1245    PXOPT_COPY_S64(config->args, where, "-chip_id", "chipRun.chip_id", "==");
     1246    PXOPT_COPY_STR(config->args, where, "-reduction", "chipRun.reduction", "==");
    10961247    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
    10971248    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     
    10991250    psString query = pxDataGet("chiptool_run.sql");
    11001251    if (!query) {
    1101         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1252        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11021253        return false;
    11031254    }
     
    11611312    psString query = pxDataGet("chiptool_completely_processed_exp.sql");
    11621313    if (!query) {
    1163         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1314        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11641315        return false;
    11651316    }
     
    12331384                    chipRun->workdir,
    12341385                    chipRun->label,
     1386                    chipRun->data_group,
     1387                    chipRun->dist_group,
    12351388                    chipRun->reduction,
    12361389                    chipRun->expgroup,
    12371390                    chipRun->dvodb,
    12381391                    chipRun->tess_id,
    1239                     chipRun->end_stage
     1392                    chipRun->end_stage,
     1393                    chipRun->magicked,
     1394                    NULL    // note does not propagate
    12401395        )) {
    12411396           if (!psDBRollback(config->dbh)) {
     
    12781433    }
    12791434
    1280     psString set_magic = "";
     1435    psString set_magicked_imfile = psStringCopy("");
     1436    psString set_magicked_run = psStringCopy("");
    12811437    if (!strcmp(data_state, "full")) {
    1282         // copy the magicked state from the input to the output when transitioning to full state
    1283         set_magic = "\n , chipProcessedImfile.magicked = rawImfile.magicked";
    1284     }
    1285 
    1286     // note only updates if chipRun.state = run_state
    1287     // XXX note that we have removed this constraint for now
    1288     if (!p_psDBRunQueryF(config->dbh, query, data_state, set_magic, chip_id, class_id)) {
     1438        // if (chipProcessedImfile.magicked < 0 and rawImfile.magicked = 0) leave magicked unchanged. This will
     1439        // block warp processing until destreaking has been done
     1440        // otherwise copy magicked from the rawImfile
     1441        // Same thing for chipRun/rawExp
     1442        psStringAppend(&set_magicked_imfile, "\n , chipProcessedImfile.magicked = IF((chipProcessedImfile.magicked < 0"
     1443                                      " AND rawImfile.magicked = 0), chipProcessedImfile.magicked, rawImfile.magicked)");
     1444        psStringAppend(&set_magicked_run, "\n , chipRun.magicked = IF((chipRun.magicked < 0 AND rawExp.magicked = 0), "
     1445                                      " chipRun.magicked, rawExp.magicked)");
     1446
     1447    } else if (!strcmp(data_state, "cleaned") || !strcmp(data_state, "purged")) {
     1448        // if magicked is non-zero set it to -1
     1449        // Once one imfile has been cleaned, the chipRun is no longer 'magicked'
     1450        psStringAppend(&set_magicked_imfile, "\n, chipProcessedImfile.magicked = IF(chipProcessedImfile.magicked = 0, 0, -1),"
     1451                                             " chipRun.magicked = IF(chipRun.magicked = 0, 0, -1)");
     1452    }
     1453
     1454    if (!p_psDBRunQueryF(config->dbh, query, data_state, set_magicked_imfile, chip_id, class_id)) {
    12891455        psFree(query);
    12901456        psError(PS_ERR_UNKNOWN, false, "database error");
     
    12971463    }
    12981464    psFree(query);
     1465    psFree(set_magicked_imfile);
     1466    psFree(set_magicked_run);
    12991467    if (psDBAffectedRows(config->dbh) < 1) {
    13001468        psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
     
    13031471
    13041472    query = pxDataGet("chiptool_change_exp_state.sql");
    1305     if (!p_psDBRunQueryF(config->dbh, query, data_state, chip_id, data_state)) {
     1473    if (!p_psDBRunQueryF(config->dbh, query, data_state, set_magicked_run, chip_id, data_state)) {
    13061474        psFree(query);
    13071475        // rollback
     
    13331501    return change_imfile_data_state(config, "purged", "goto_purged");
    13341502}
    1335 
     1503static bool toscrubbedimfileMode(pxConfig *config)
     1504{
     1505  return change_imfile_data_state(config, "scrubbed", "goto_scrubbed");
     1506}
    13361507bool exportrunMode(pxConfig *config)
    13371508{
     
    13571528  }
    13581529
     1530  if (!pxExportVersion(config, f)) {
     1531    psError(PS_ERR_UNKNOWN, false, "failed to write dbversion output file");
     1532    return false;
     1533  }
     1534
    13591535  psMetadata *where = psMetadataAlloc();
    13601536  PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
     
    13701546    psString query = pxDataGet(tables[i].sqlFilename);
    13711547    if (!query) {
    1372       psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1548      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    13731549      return false;
    13741550    }
     
    14061582
    14071583    if (clean) {
    1408         bool success = true; 
     1584        bool success = true;
    14091585        if (!strcmp(tables[i].tableName, "chipRun")) {
    14101586            success = pxSetStateCleaned("chipRun", "state", output);
     
    14481624    psVector *identifiers = psVectorAllocEmpty(16, PS_TYPE_U64); // Identifiers inserted
    14491625
     1626    psMetadataIterator *iter = psMetadataIteratorAlloc(input, PS_LIST_HEAD, NULL);       // Iterator
     1627
     1628    if (!pxCheckImportVersion(config, input)) {
     1629        psError(PS_ERR_UNKNOWN, false, "pxCheckImportVersion failed");
     1630        return false;
     1631    }
     1632    // first item is the dbversion, skip it
     1633    psMetadataItem *dbversion =  psMetadataGetAndIncrement(iter);
     1634    (void) dbversion;
     1635
    14501636    if (!psDBTransaction(config->dbh)) {
    14511637        psError(PS_ERR_UNKNOWN, false, "database error");
     
    14531639    }
    14541640
    1455     psMetadataIterator *iter = psMetadataIteratorAlloc(input, PS_LIST_HEAD, NULL);       // Iterator
    14561641    psMetadataItem *item;               // Item from iteration
    14571642    while ((item = psMetadataGetAndIncrement(iter))) {
     
    14821667    return true;
    14831668}
     1669
     1670static bool runstateMode(pxConfig *config)
     1671{
     1672    PS_ASSERT_PTR_NON_NULL(config, false);
     1673
     1674    psMetadata *where = psMetadataAlloc();
     1675    PXOPT_COPY_S64(config->args, where, "-chip_id",    "chipRun.chip_id", "==");
     1676    PXOPT_COPY_S64(config->args, where, "-exp_id",     "rawExp.exp_id", "==");
     1677    PXOPT_COPY_STR(config->args, where, "-exp_name",   "rawExp.exp_name", "==");
     1678    pxAddLabelSearchArgs (config, where, "-label",     "chipRun.label", "LIKE");
     1679
     1680//    PXOPT_LOOKUP_BOOL(all, config->args, "-all", false);
     1681    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
     1682
     1683    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1684    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1685
     1686    psString query = pxDataGet("chiptool_runstate.sql");
     1687    if (!query) {
     1688        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1689        return false;
     1690    }
     1691
     1692    if (psListLength(where->list)) {
     1693        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1694        psStringAppend(&query, " WHERE %s", whereClause);
     1695        psFree(whereClause);
     1696    } else {
     1697        psError(PXTOOLS_ERR_CONFIG, true, "search parameters or -all are required");
     1698        return false;
     1699    }
     1700    psFree(where);
     1701
     1702    // treat limit == 0 as "no limit"
     1703    if (limit) {
     1704        psString limitString = psDBGenerateLimitSQL(limit);
     1705        psStringAppend(&query, " %s", limitString);
     1706        psFree(limitString);
     1707    }
     1708
     1709    if (!p_psDBRunQuery(config->dbh, query)) {
     1710        psError(PS_ERR_UNKNOWN, false, "database error");
     1711        psFree(query);
     1712        return false;
     1713    }
     1714    psFree(query);
     1715
     1716    psArray *output = p_psDBFetchResult(config->dbh);
     1717    if (!output) {
     1718        psErrorCode err = psErrorCodeLast();
     1719        switch (err) {
     1720            case PS_ERR_DB_CLIENT:
     1721                psError(PXTOOLS_ERR_SYS, false, "database error");
     1722            case PS_ERR_DB_SERVER:
     1723                psError(PXTOOLS_ERR_PROG, false, "database error");
     1724            default:
     1725                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1726        }
     1727
     1728        return false;
     1729    }
     1730    if (!psArrayLength(output)) {
     1731        psTrace("chiptool", PS_LOG_INFO, "no rows found");
     1732        psFree(output);
     1733        return true;
     1734    }
     1735
     1736    if (psArrayLength(output)) {
     1737        // negative simple so the default is true
     1738        if (!ippdbPrintMetadatas(stdout, output, "chipRunState", !simple)) {
     1739            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1740            psFree(output);
     1741            return false;
     1742        }
     1743    }
     1744
     1745    psFree(output);
     1746
     1747    return true;
     1748}
     1749
     1750// a very specfic function to queue a cleaned chipProcessedImfile to be updated
     1751static bool setimfiletoupdateMode(pxConfig *config)
     1752{
     1753    PS_ASSERT_PTR_NON_NULL(config, NULL);
     1754
     1755    PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", true, false);
     1756    PXOPT_LOOKUP_STR(class_id, config->args, "-class_id", false, false);
     1757    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     1758
     1759    psString query = pxDataGet("chiptool_setimfiletoupdate.sql");
     1760    if (!query) {
     1761        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1762        return false;
     1763    }
     1764
     1765    psString setHook = psStringCopy("");
     1766    if (label) {
     1767        psStringAppend(&setHook, "\n , chipRun.label = '%s'", label);
     1768    }
     1769
     1770    if (class_id) {
     1771        psStringAppend(&query, " AND (chipProcessedImfile.class_id = '%s')", class_id);
     1772    }
     1773
     1774    if (!p_psDBRunQueryF(config->dbh, query, setHook, chip_id)) {
     1775        psError(PS_ERR_UNKNOWN, false, "database error");
     1776        return false;
     1777    }
     1778
     1779    psFree(setHook);
     1780    psFree(query);
     1781
     1782    return true;
     1783}
     1784
     1785static bool listrunMode(pxConfig *config)
     1786{
     1787    PS_ASSERT_PTR_NON_NULL(config, NULL);
     1788
     1789    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1790    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1791    PXOPT_LOOKUP_BOOL(pstamp_order, config->args, "-pstamp_order", false);
     1792
     1793    psMetadata *where = psMetadataAlloc();
     1794    pxchipGetSearchArgs (config, where); // chipRun, chipProcessedImfile, rawExp
     1795    PXOPT_COPY_S64(config->args, where, "-chip_id", "chipRun.chip_id", "==");
     1796    PXOPT_COPY_STR(config->args, where, "-reduction", "chipRun.reduction", "==");
     1797    PXOPT_COPY_STR(config->args, where, "-state", "chipRun.state", "==");
     1798    pxAddLabelSearchArgs (config, where, "-label", "chipRun.label", "LIKE");
     1799    pxAddLabelSearchArgs (config, where, "-data_group", "chipRun.data_group", "LIKE");
     1800    pxAddLabelSearchArgs (config, where, "-dist_group", "chipRun.dist_group", "LIKE");
     1801    PXOPT_COPY_S64(config->args, where, "-magicked", "chipRun.magicked", "==");
     1802
     1803    psString where2 = NULL;
     1804    pxmagicAddWhere(config, &where2, "chipRun");
     1805    // add cuts on ra and decl if supplied
     1806    if (!pxspaceAddWhere(config, &where2, "rawExp")) {
     1807        psError(psErrorCodeLast(), false, "pxSpaceAddWhere failed");
     1808        return false;
     1809    }
     1810
     1811    psString query = pxDataGet("chiptool_listrun.sql");
     1812    if (!query) {
     1813        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1814        return false;
     1815    }
     1816
     1817    if (psListLength(where->list)) {
     1818        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1819        psStringAppend(&query, " WHERE %s %s", whereClause, where2 ? where2 : "");
     1820        psFree(whereClause);
     1821    } else if (psMetadataLookupBool(NULL, config->args, "-all") || where2) {
     1822        psStringAppend(&query, " WHERE chipRun.chip_id IS NOT NULL %s", where2 ? where2 : "");
     1823    } else {
     1824        psFree(where);
     1825        psError(PXTOOLS_ERR_CONFIG, false, "search parameters (or -all) are required");
     1826        return false;
     1827    }
     1828    psFree(where);
     1829
     1830    if (pstamp_order) {
     1831        // put runs in order of exposure id with newest chip Runs first
     1832        // The postage stamp parser depends on this behavior
     1833        psStringAppend(&query, "\nORDER by exp_id, chip_id DESC");
     1834    }
     1835
     1836    // treat limit == 0 as "no limit"
     1837    if (limit) {
     1838        psString limitString = psDBGenerateLimitSQL(limit);
     1839        psStringAppend(&query, " %s", limitString);
     1840        psFree(limitString);
     1841    }
     1842
     1843    if (!p_psDBRunQuery(config->dbh, query)) {
     1844        psError(PS_ERR_UNKNOWN, false, "database error");
     1845        psFree(query);
     1846        return false;
     1847    }
     1848    psFree(query);
     1849
     1850    psArray *output = p_psDBFetchResult(config->dbh);
     1851    if (!output) {
     1852        psError(PS_ERR_UNKNOWN, false, "database error");
     1853        return false;
     1854    }
     1855    if (!psArrayLength(output)) {
     1856        psTrace("chiptool", PS_LOG_INFO, "no rows found");
     1857        psFree(output);
     1858        return true;
     1859    }
     1860
     1861    // negative simple so the default is true
     1862    if (!ippdbPrintMetadatas(stdout, output, "chipRun", !simple)) {
     1863        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1864        psFree(output);
     1865        return false;
     1866    }
     1867
     1868    psFree(output);
     1869
     1870    return true;
     1871}
Note: See TracChangeset for help on using the changeset viewer.