IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

updates from trunk

Location:
branches/tap_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/tap_branches

  • branches/tap_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
        1616difftool
        1717disttool
         18dqstatstool
        1819faketool
        1920flatcorr
  • branches/tap_branches/ippTools/src/chiptool.c

    r25851 r27838  
    3636
    3737static bool definebyqueryMode(pxConfig *config);
     38static bool definecopyMode(pxConfig *config);
    3839static bool updaterunMode(pxConfig *config);
    3940static bool pendingimfileMode(pxConfig *config);
     
    5859static bool importrunMode(pxConfig *config);
    5960static bool runstateMode(pxConfig *config);
     61static bool setimfiletoupdateMode(pxConfig *config);
     62static bool listrunMode(pxConfig *config);
    6063
    6164# define MODECASE(caseName, func) \
     
    7780    switch (config->mode) {
    7881        MODECASE(CHIPTOOL_MODE_DEFINEBYQUERY,           definebyqueryMode);
     82        MODECASE(CHIPTOOL_MODE_DEFINECOPY,              definecopyMode);
    7983        MODECASE(CHIPTOOL_MODE_UPDATERUN,               updaterunMode);
    8084        MODECASE(CHIPTOOL_MODE_PENDINGIMFILE,           pendingimfileMode);
     
    9599        MODECASE(CHIPTOOL_MODE_TOFULLIMFILE,            tofullimfileMode);
    96100        MODECASE(CHIPTOOL_MODE_TOPURGEDIMFILE,          topurgedimfileMode);
    97         MODECASE(CHIPTOOL_MODE_TOSCRUBBEDIMFILE,        toscrubbedimfileMode);
     101        MODECASE(CHIPTOOL_MODE_TOSCRUBBEDIMFILE,        toscrubbedimfileMode);
    98102        MODECASE(CHIPTOOL_MODE_EXPORTRUN,               exportrunMode);
    99103        MODECASE(CHIPTOOL_MODE_IMPORTRUN,               importrunMode);
    100104        MODECASE(CHIPTOOL_MODE_RUNSTATE,                runstateMode);
     105        MODECASE(CHIPTOOL_MODE_SETIMFILETOUPDATE,       setimfiletoupdateMode);
     106        MODECASE(CHIPTOOL_MODE_LISTRUN,                 listrunMode);
    101107        default:
    102108            psAbort("invalid option (this should not happen)");
     
    120126}
    121127
     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}
    122240
    123241static bool definebyqueryMode(pxConfig *config)
     
    129247    psMetadata *where = psMetadataAlloc();
    130248    pxchipGetSearchArgs (config, where); // rawExp only
    131     pxAddLabelSearchArgs (config, where, "-label", "newExp.label", "LIKE");
     249    pxAddLabelSearchArgs (config, where, "-label", "newExp.label", "LIKE"); // define using newExp label
    132250
    133251    // psListLength(where->list) is at least 1 because exp_type defaults to "object"
     
    135253    if ((psListLength(where->list) <= 1) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    136254        psFree(where);
    137         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     255        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    138256        return false;
    139257    }
     
    157275    psString query = pxDataGet("chiptool_find_rawexp.sql");
    158276    if (!query) {
    159         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     277        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    160278        psFree(where);
    161279        return false;
     
    197315    }
    198316
    199     // start a transaction so we don't end up with an exp without any associted
    200     // imfiles
    201     if (!psDBTransaction(config->dbh)) {
    202         psError(PS_ERR_UNKNOWN, false, "database error");
    203         psFree(output);
    204         return false;
    205     }
    206 
    207     // if end_stage is warp (or NULL), check for valid tess_id
    208     for (long i = 0; i < psArrayLength(output); i++) {
    209         psMetadata *md = output->data[i];
    210 
    211         bool status;
    212         char *end_stage = psMetadataLookupStr(&status, md, "end_stage");
    213         if (end_stage && strcasecmp(end_stage, "warp")) continue;
    214 
    215         char *raw_tess_id   = psMetadataLookupStr(&status, md, "tess_id");
    216         if (raw_tess_id || tess_id) continue;
    217 
    218         char *label  = psMetadataLookupStr(&status, md, "label");
    219         psS64 exp_id = psMetadataLookupS64(&status, md, "exp_id");
    220 
    221         if (!status) {
    222             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");
    223397            psFree(output);
    224398            return false;
    225399        }
    226     }
    227 
    228 
    229 # define GET_VALUE(PTYPE,CTYPE,VALUE,NAME)                              \
    230     PTYPE VALUE;                                                        \
    231     { bool status;                                                      \
    232         VALUE = psMetadataLookup##CTYPE(&status, md, NAME);             \
    233         if (!status) {                                                  \
    234             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for %s", NAME); \
    235             psFree(output);                                             \
    236             return false;                                               \
    237         } }
    238 
    239     // loop over our list of exp_ids
    240     for (long i = 0; i < psArrayLength(output); i++) {
    241         psMetadata *md = output->data[i];
    242 
    243         rawExpRow *row = rawExpObjectFromMetadata(md);
    244         if (!row) {
    245             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
    246             psFree(output);
    247             return false;
    248         }
    249 
    250         GET_VALUE (psS64,    S64, exp_id,        "exp_id");
    251         GET_VALUE (psString, Str, raw_workdir,   "workdir");
    252         GET_VALUE (psString, Str, raw_label,     "label");
    253         GET_VALUE (psString, Str, raw_reduction, "reduction");
    254         // GET_VALUE (psString, Str, raw_expgroup,  "expgroup");
    255         GET_VALUE (psString, Str, raw_dvodb,     "dvodb");
    256         GET_VALUE (psString, Str, raw_tess_id,   "tess_id");
    257         GET_VALUE (psString, Str, raw_end_stage, "end_stage");
    258 
    259         if (!row->exp_id) {
    260             psError(PS_ERR_UNKNOWN, false, "failed to find value for exp_id");
    261             psFree(output);
    262             return false;
    263         }
    264 
    265         // queue the exp
    266         if (!pxchipQueueByExpTag(config,
    267                                  exp_id,
    268                                  workdir     ? workdir   : raw_workdir,
    269                                  label       ? label     : raw_label,
    270                                  data_group  ? data_group : (label ? label : raw_label),
    271                                  dist_group,
    272                                  reduction   ? reduction : raw_reduction,
    273                                  // expgroup    ? expgroup  : raw_expgroup,
    274                                  // XXX how does expgroup get defined?
    275                                  expgroup,
    276                                  dvodb       ? dvodb     : raw_dvodb,
    277                                  tess_id     ? tess_id   : raw_tess_id,
    278                                  end_stage   ? end_stage : raw_end_stage,
    279                                  note
    280                                  )) {
    281             if (!psDBRollback(config->dbh)) {
    282                 psError(PS_ERR_UNKNOWN, false, "database error");
    283             }
    284             psError(PS_ERR_UNKNOWN, false,
    285                     "failed to trying to queue exp_id: %" PRId64, exp_id);
    286             psFree(output);
    287             return false;
    288         }
     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;
    289409    }
    290410    psFree(output);
    291 
    292     if (!psDBCommit(config->dbh)) {
    293         psError(PS_ERR_UNKNOWN, false, "database error");
    294         return false;
    295     }
    296411
    297412    return true;
     
    314429    if (!psListLength(where->list)) {
    315430        psFree(where);
    316         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
    317         return false;
    318     }
    319     psString query = psStringCopy("UPDATE chipRun JOIN rawExp USING(exp_id)");
     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");
    320435
    321436    // pxUpdateRun gets parameters from config->args and updates
    322     bool result = pxUpdateRun(config, where, &query, "chipRun", true);
     437    bool result = pxUpdateRun(config, where, &query, "chipRun", "chip_id", "chipProcessedImfile", true);
    323438    if (!result) {
    324         psError(PXTOOLS_ERR_DATA, false, "pxUpdateRun failed");
     439        psError(psErrorCodeLast(), false, "pxUpdateRun failed");
    325440    }
    326441
     
    346461    psString query = pxDataGet("chiptool_pendingimfile.sql");
    347462    if (!query) {
    348         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     463        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    349464        return false;
    350465    }
     
    586701    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    587702    PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
     703    PXOPT_LOOKUP_BOOL(allfiles, config->args, "-allfiles", false);
     704    if (allfiles) {
     705        faulted = false;
     706    }
    588707
    589708    psMetadata *where = psMetadataAlloc();
     
    594713    PXOPT_COPY_STR(config->args, where, "-reduction", "chipRun.reduction", "==");
    595714    pxAddLabelSearchArgs (config, where, "-label", "chipRun.label", "LIKE");
     715    pxAddLabelSearchArgs (config, where, "-data_group", "chipRun.data_group", "LIKE");
    596716    PXOPT_COPY_S64(config->args, where, "-magicked", "chipProcessedImfile.magicked", "==");
    597717
     
    600720    // add cuts on ra and decl if supplied
    601721    if (!pxspaceAddWhere(config, &where2, "rawExp")) {
    602         psError(PXTOOLS_ERR_DATA, false, "pxSpaceAddWhere failed");
     722        psError(psErrorCodeLast(), false, "pxSpaceAddWhere failed");
    603723        return false;
    604724    }
     
    606726    psString query = pxDataGet("chiptool_processedimfile.sql");
    607727    if (!query) {
    608         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     728        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    609729        return false;
    610730    }
     
    612732    if (psListLength(where->list)) {
    613733        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    614         psStringAppend(&query, " WHERE %s", whereClause);
     734        psStringAppend(&query, " WHERE %s %s", whereClause, where2 ? where2 : "");
    615735        psFree(whereClause);
    616736    } else if (psMetadataLookupBool(NULL, config->args, "-all") || (faulted || where2)) {
     
    618738    } else {
    619739        psFree(where);
    620         psError(PXTOOLS_ERR_DATA, false, "search parameters (or -all) are required");
     740        psError(PXTOOLS_ERR_CONFIG, false, "search parameters (or -all) are required");
    621741        return false;
    622742    }
     
    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;
     
    720853    PXOPT_COPY_STR(config->args, where, "-class_id", "class_id", "==");
    721854    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, NULL);
    722        
     855    if (psListLength(where->list) == 0) {
     856        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
     857        return false;
     858    }
     859
    723860    if (!state) {
    724861      PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    725      
     862
    726863      if (!pxSetFaultCode(config->dbh, "chipProcessedImfile", where, fault)) {
    727864        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
     
    732869    else {
    733870      if (!pxchipProcessedImfileSetStateByQuery(config,where,state)) {
    734         psError(PS_ERR_UNKNOWN, false, "failed to set chipProcessedImfile state");
    735         return(false);
     871        psError(PS_ERR_UNKNOWN, false, "failed to set chipProcessedImfile state");
     872        return(false);
    736873      }
    737874    }
    738875
    739      
     876
    740877    return true;
    741878}
     
    839976    psString query = pxDataGet("chiptool_unmasked.sql");
    840977    if (!query) {
    841         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     978        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    842979        return false;
    843980    }
     
    9171054    psString query = pxDataGet("chiptool_pendingcleanuprun.sql");
    9181055    if (!query) {
    919         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1056        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    9201057        return false;
    9211058    }
     
    9811118    psString query = pxDataGet("chiptool_pendingcleanupimfile.sql");
    9821119    if (!query) {
    983         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1120        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    9841121        return false;
    9851122    }
     
    10421179    psString query = pxDataGet("chiptool_donecleanup.sql");
    10431180    if (!query) {
    1044         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1181        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    10451182        return false;
    10461183    }
     
    11001237    // make sure that the state string is valid
    11011238    if (!pxIsValidState(state)) {
    1102         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);
    11031240        return false;
    11041241    }
     
    11131250    psString query = pxDataGet("chiptool_run.sql");
    11141251    if (!query) {
    1115         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1252        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11161253        return false;
    11171254    }
     
    11751312    psString query = pxDataGet("chiptool_completely_processed_exp.sql");
    11761313    if (!query) {
    1177         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1314        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11781315        return false;
    11791316    }
     
    12541391                    chipRun->tess_id,
    12551392                    chipRun->end_stage,
     1393                    chipRun->magicked,
    12561394                    NULL    // note does not propagate
    12571395        )) {
     
    12951433    }
    12961434
    1297     psString set_magic = "";
     1435    psString set_magicked_imfile = psStringCopy("");
     1436    psString set_magicked_run = psStringCopy("");
    12981437    if (!strcmp(data_state, "full")) {
    1299         // copy the magicked state from the input to the output when transitioning to full state
    1300         set_magic = "\n , chipProcessedImfile.magicked = rawImfile.magicked";
    1301     }
    1302 
    1303     // note only updates if chipRun.state = run_state
    1304     // XXX note that we have removed this constraint for now
    1305     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)) {
    13061455        psFree(query);
    13071456        psError(PS_ERR_UNKNOWN, false, "database error");
     
    13141463    }
    13151464    psFree(query);
     1465    psFree(set_magicked_imfile);
     1466    psFree(set_magicked_run);
    13161467    if (psDBAffectedRows(config->dbh) < 1) {
    13171468        psError(PS_ERR_UNKNOWN, false, "should have affected atleast 1 row");
     
    13201471
    13211472    query = pxDataGet("chiptool_change_exp_state.sql");
    1322     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)) {
    13231474        psFree(query);
    13241475        // rollback
     
    13951546    psString query = pxDataGet(tables[i].sqlFilename);
    13961547    if (!query) {
    1397       psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1548      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    13981549      return false;
    13991550    }
     
    14311582
    14321583    if (clean) {
    1433         bool success = true; 
     1584        bool success = true;
    14341585        if (!strcmp(tables[i].tableName, "chipRun")) {
    14351586            success = pxSetStateCleaned("chipRun", "state", output);
     
    15351686    psString query = pxDataGet("chiptool_runstate.sql");
    15361687    if (!query) {
    1537         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1688        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    15381689        return false;
    15391690    }
     
    15441695        psFree(whereClause);
    15451696    } else {
    1546         psError(PXTOOLS_ERR_DATA, true, "search parameters or -all are required");
     1697        psError(PXTOOLS_ERR_CONFIG, true, "search parameters or -all are required");
    15471698        return false;
    15481699    }
     
    15961747    return true;
    15971748}
     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.