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/difftool.c

    r25851 r27838  
    4343static bool definewarpstackMode(pxConfig *config);
    4444static bool definewarpwarpMode(pxConfig *config);
     45static bool definestackstackMode(pxConfig *config);
    4546static bool pendingcleanuprunMode(pxConfig *config);
    4647static bool pendingcleanupskyfileMode(pxConfig *config);
     
    5556static bool topurgedskyfileMode(pxConfig *config);
    5657static bool toscrubbedskyfileMode(pxConfig *config);
     58static bool tofullskyfileMode(pxConfig *config);
     59static bool listrunMode(pxConfig *config);
     60static bool setskyfiletoupdateMode(pxConfig *config);
    5761
    5862
     
    8892        MODECASE(DIFFTOOL_MODE_DEFINEWARPSTACK,       definewarpstackMode);
    8993        MODECASE(DIFFTOOL_MODE_DEFINEWARPWARP,        definewarpwarpMode);
     94        MODECASE(DIFFTOOL_MODE_DEFINESTACKSTACK,      definestackstackMode);
    9095        MODECASE(DIFFTOOL_MODE_PENDINGCLEANUPRUN,     pendingcleanuprunMode);
    9196        MODECASE(DIFFTOOL_MODE_PENDINGCLEANUPSKYFILE, pendingcleanupskyfileMode);
     
    9499        MODECASE(DIFFTOOL_MODE_EXPORTRUN,             exportrunMode);
    95100        MODECASE(DIFFTOOL_MODE_IMPORTRUN,             importrunMode);
    96         MODECASE(DIFFTOOL_MODE_TOCLEANEDSKYFILE,   tocleanedskyfileMode);
    97         MODECASE(DIFFTOOL_MODE_TOPURGEDSKYFILE,    topurgedskyfileMode);
    98         MODECASE(DIFFTOOL_MODE_TOSCRUBBEDSKYFILE,  toscrubbedskyfileMode);
     101        MODECASE(DIFFTOOL_MODE_TOCLEANEDSKYFILE,      tocleanedskyfileMode);
     102        MODECASE(DIFFTOOL_MODE_TOPURGEDSKYFILE,       topurgedskyfileMode);
     103        MODECASE(DIFFTOOL_MODE_TOSCRUBBEDSKYFILE,     toscrubbedskyfileMode);
     104        MODECASE(DIFFTOOL_MODE_TOFULLSKYFILE,         tofullskyfileMode);
     105        MODECASE(DIFFTOOL_MODE_LISTRUN,               listrunMode);
     106        MODECASE(DIFFTOOL_MODE_SETSKYFILETOUPDATE,    setskyfiletoupdateMode);
    99107
    100108        default:
     
    132140    PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
    133141    PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false);
    134     PXOPT_LOOKUP_STR(reduction, config->args, "-rset_eduction", false, false);
     142    PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
     143    PXOPT_LOOKUP_S16(diff_mode, config->args, "-set_diff_mode", false, false);
    135144    PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
    136145
     
    153162            exposure,
    154163            false,
     164            diff_mode,
    155165            note
    156166    );
     
    186196    psMetadata *where = psMetadataAlloc();
    187197
    188     PXOPT_COPY_S64(config->args, where, "-diff_id",  "stack_id",   "==");
     198    PXOPT_COPY_S64(config->args, where, "-diff_id",  "diff_id",   "==");
    189199    PXOPT_COPY_STR(config->args, where, "-label",     "label",     "==");
     200    PXOPT_COPY_STR(config->args, where, "-data_group","data_group",     "==");
    190201    PXOPT_COPY_STR(config->args, where, "-state",     "state",     "==");
    191202    if (!psListLength(where->list)) {
    192203        psFree(where);
    193         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     204        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    194205        return false;
    195206    }
     
    198209
    199210    // pxUpdateRun gets parameters from config->args and updates
    200     bool result = pxUpdateRun(config, where, &query, "diffRun", true);
     211    bool result = pxUpdateRun(config, where, &query, "diffRun", "diff_id", "diffSkyfile", true);
    201212
    202213    psFree(query);
     
    204215
    205216    return result;
    206 
    207 #ifdef notdef
    208     // required options
    209     PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", false, false);
    210     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    211     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    212    
    213     // Copy of my hacky work around from stacktool.c
    214     if ((state)&&(diff_id)) {
    215         // set detRun.state to state
    216         return setdiffRunState(config, diff_id, state, false);
    217     }
    218 
    219     if ((state)&&(label)) {
    220       return setdiffRunStateByLabel(config, label, state);
    221     }
    222 
    223     psError(PS_ERR_UNKNOWN, false, "Required options not found.");
    224 
    225     return false;
    226 #endif
    227217}
    228218
     
    379369    psString query = pxDataGet("difftool_inputskyfile.sql");
    380370    if (!query) {
    381         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    382         return false;
    383     }
    384 
    385     psString whereClause = NULL;
     371        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     372        return false;
     373    }
     374
     375    psString whereClause = psStringCopy("");
    386376    if (psListLength(where->list)) {
    387377        whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    388         psStringPrepend(&whereClause, "\n AND ");
     378        psStringPrepend(&whereClause, "\n WHERE ");
    389379    }
    390380    psFree(where);
    391381
    392382    // Add condition to get only templates or only inputs
     383    psString templateClause = psStringCopy("");
    393384    {
    394         psString templateClause = NULL;
    395385        if (template) {
    396             psStringAppend(&templateClause, " %s", " template != 0");
     386            psStringAppend(&templateClause, "\n WHERE %s", " template != 0");
    397387        } else if (input) {
    398             psStringAppend(&templateClause, " %s", " template = 0");
    399         }
    400         if (templateClause) {
    401             psStringAppend(&whereClause, "\n AND %s", templateClause);
    402         }
    403         psFree(templateClause);
     388            psStringAppend(&templateClause, "\n WHERE %s", " template = 0");
     389        }
    404390    }
    405391
     
    412398    }
    413399
    414     if (!p_psDBRunQueryF(config->dbh, query, whereClause, whereClause, whereClause, whereClause)) {
    415         psError(PS_ERR_UNKNOWN, false, "database error");
     400    if (!p_psDBRunQueryF(config->dbh, query, whereClause, whereClause, whereClause, whereClause, templateClause)) {
     401        psError(PS_ERR_UNKNOWN, false, "database error");
     402        psFree(templateClause);
    416403        psFree(whereClause);
    417404        psFree(query);
    418405        return false;
    419406    }
     407    psFree(templateClause);
    420408    psFree(whereClause);
    421409    psFree(query);
     
    469457    psString query = pxDataGet("difftool_todiffskyfile.sql");
    470458    if (!query) {
    471         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     459        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    472460        return false;
    473461    }
     
    626614    psString query = pxDataGet("difftool_completed_runs.sql");
    627615    if (!query) {
    628         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     616        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    629617        return false;
    630618    }
     
    717705    PXOPT_COPY_S64(config->args, where,  "-magicked", "diffSkyfile.magicked", "==");
    718706    pxAddLabelSearchArgs (config, where, "-label", "diffRun.label", "LIKE");
    719 
     707    pxAddLabelSearchArgs (config, where, "-data_group", "diffRun.data_group", "LIKE");
     708
     709    PXOPT_LOOKUP_BOOL(pstamp_order, config->args, "-pstamp_order", false);
    720710    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
    721711    if (!template) {
     
    725715        PXOPT_COPY_TIME(config->args, where, "-dateobs_begin", "rawInput.dateobs",  ">=");
    726716        PXOPT_COPY_TIME(config->args, where, "-dateobs_end",   "rawInput.dateobs",  "<=");
    727         PXOPT_COPY_STR(config->args, where, "-filter",     "rawInput.filter", "==");
     717        PXOPT_COPY_STR(config->args, where, "-filter",     "rawInput.filter", "LIKE");
    728718    } else {
    729719        PXOPT_COPY_S64(config->args, where, "-exp_id", "rawTemplate.exp_id", "==");
     
    732722        PXOPT_COPY_TIME(config->args, where, "-dateobs_begin", "rawTemplate.dateobs",  ">=");
    733723        PXOPT_COPY_TIME(config->args, where, "-dateobs_end",   "rawTemplate.dateobs",  "<=");
    734         PXOPT_COPY_STR(config->args, where, "-filter",     "rawTemplate.filter", "==");
     724        PXOPT_COPY_STR(config->args, where, "-filter",     "rawTemplate.filter", "LIKE");
    735725    }
    736726
     
    745735    psString query = pxDataGet("difftool_skyfile.sql");
    746736    if (!query) {
    747         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     737        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    748738        return false;
    749739    }
     
    756746        psStringAppend(&query, " WHERE diffRun.diff_id is not null %s", where2);
    757747    } else if (!all) {
    758         psError(PXTOOLS_ERR_DATA, true, "search parameters or -all are required");
     748        psError(PXTOOLS_ERR_CONFIG, true, "search parameters or -all are required");
    759749        return false;
    760750    }
    761751    psFree(where);
    762752
     753    if (pstamp_order) {
     754        if (template) {
     755            psStringAppend(&query, " ORDER BY rawTemplate.exp_id, diff_id DESC");
     756        } else {
     757            psStringAppend(&query, " ORDER BY rawInput.exp_id, diff_id DESC");
     758        }
     759    }
     760           
    763761    // treat limit == 0 as "no limit"
    764762    if (limit) {
     
    816814    psMetadata *where = psMetadataAlloc();
    817815    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diffSkyfile.diff_id", "==");
    818     PXOPT_COPY_STR(config->args, where,  "-label", "diffRun.label", "==");
     816    PXOPT_COPY_STR(config->args, where,  "-skycell_id", "diffSkyfile.skycell_id", "==");
     817    pxAddLabelSearchArgs(config, where, "-label", "diffRun.label", "==");
    819818    PXOPT_COPY_S16(config->args, where, "-fault",     "fault", "==");
    820819
    821820    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    822821        psFree(where);
    823         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
    824         return false;
    825     }
    826 
    827     if (!psDBTransaction(config->dbh)) {
    828         psError(PS_ERR_UNKNOWN, false, "database error");
    829         psFree(where);
    830         return false;
    831     }
    832 
    833     // Update state to 'new'
     822        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     823        return false;
     824    }
     825
    834826    {
    835         psString query = pxDataGet("difftool_revertdiffskyfile_update.sql");
     827        psString query = pxDataGet("difftool_revertdiffskyfile_delete.sql");
    836828        if (!query) {
    837             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    838             if (!psDBRollback(config->dbh)) {
    839                 psError(PS_ERR_UNKNOWN, false, "database error");
    840             }
     829            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     830            return false;
     831        }
     832        psString query_updated = pxDataGet("difftool_revertdiffskyfile_updated.sql");
     833        if (!query_updated) {
     834            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    841835            return false;
    842836        }
     
    845839            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    846840            psStringAppend(&query, " AND %s", whereClause);
     841            psStringAppend(&query_updated, " AND %s", whereClause);
    847842            psFree(whereClause);
    848843        }
     
    851846            psError(PS_ERR_UNKNOWN, false, "database error");
    852847            psFree(query);
    853             if (!psDBRollback(config->dbh)) {
    854                 psError(PS_ERR_UNKNOWN, false, "database error");
    855             }
    856848            return false;
    857849        }
    858850        psFree(query);
     851        psLogMsg("difftool", PS_LOG_INFO, "Deleted %" PRIu64 " rows", psDBAffectedRows(config->dbh));
     852
     853        if (!p_psDBRunQuery(config->dbh, query_updated)) {
     854            psError(PS_ERR_UNKNOWN, false, "database error");
     855            psFree(query_updated);
     856            return false;
     857        }
     858        psFree(query_updated);
    859859
    860860        psLogMsg("difftool", PS_LOG_INFO, "Updated %" PRIu64 " rows", psDBAffectedRows(config->dbh));
    861861    }
    862862
    863     // Delete product
    864     {
    865         psString query = pxDataGet("difftool_revertdiffskyfile_delete.sql");
    866         if (!query) {
    867             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    868             if (!psDBRollback(config->dbh)) {
    869                 psError(PS_ERR_UNKNOWN, false, "database error");
    870             }
    871             return false;
    872         }
    873 
    874         if (psListLength(where->list)) {
    875             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    876             psStringAppend(&query, " AND %s", whereClause);
    877             psFree(whereClause);
    878         }
    879 
    880         if (!p_psDBRunQuery(config->dbh, query)) {
    881             psError(PS_ERR_UNKNOWN, false, "database error");
    882             psFree(query);
    883             if (!psDBRollback(config->dbh)) {
    884                 psError(PS_ERR_UNKNOWN, false, "database error");
    885             }
    886             return false;
    887         }
    888         psFree(query);
    889 
    890         psLogMsg("difftool", PS_LOG_INFO, "Deleted %" PRIu64 " rows", psDBAffectedRows(config->dbh));
    891     }
    892 
    893863    psFree(where);
    894864
    895     if (!psDBCommit(config->dbh)) {
    896         psError(PS_ERR_UNKNOWN, false, "database error");
    897         return false;
    898     }
    899865
    900866    return true;
     
    914880    if (magicked) {
    915881      char *query = "UPDATE diffRun SET state = '%s', magicked = %" PRId64 " WHERE diff_id = %"PRId64;
    916      
     882
    917883      if (!p_psDBRunQueryF(config->dbh, query, state, magicked, diff_id)) {
    918884        psError(PS_ERR_UNKNOWN, false,
     
    923889    else {
    924890      char *query = "UPDATE diffRun SET state = '%s' WHERE diff_id = %"PRId64;
    925      
     891
    926892      if (!p_psDBRunQueryF(config->dbh, query, state, diff_id)) {
    927893        psError(PS_ERR_UNKNOWN, false,
     
    930896      }
    931897    }
    932    
     898
    933899    return true;
    934900}
     
    948914  if (!p_psDBRunQueryF(config->dbh,query,state,label)) {
    949915    psError(PS_ERR_UNKNOWN, false,
    950             "failed to change state for label %s", label);
     916            "failed to change state for label %s", label);
    951917    return(false);
    952918  }
     
    986952        return false;
    987953    }
     954    psS16 diff_mode = IPP_DIFF_MODE_UNDEFINED;
     955    if ((input_warp_id != PS_MAX_S64) && (template_warp_id != PS_MAX_S64)) {
     956      diff_mode = IPP_DIFF_MODE_WARP_WARP;
     957    }
     958    else if ((input_warp_id != PS_MAX_S64) && (template_stack_id != PS_MAX_S64)) {
     959      diff_mode = IPP_DIFF_MODE_WARP_STACK;
     960    }
     961    else if ((input_stack_id != PS_MAX_S64) && (template_warp_id != PS_MAX_S64)) {
     962      diff_mode = IPP_DIFF_MODE_STACK_WARP;
     963    }
     964    else if ((input_stack_id != PS_MAX_S64) && (template_stack_id != PS_MAX_S64)) {
     965      diff_mode = IPP_DIFF_MODE_STACK_STACK;
     966    }
     967
    988968
    989969    // default
     
    1005985            false,
    1006986            0,       // magicked
     987            diff_mode, // diff_mode
    1007988            note
    1008989    );
     
    11101091
    11111092    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    1112 
     1093    // Populated Run will generate the diff_mode value
    11131094    if (!populatedrun(list, workdir, skycell_id, tess_id, label, data_group ? data_group : label, dist_group, reduction, note,
    11141095                      input_warp_id ? input_warp_id : PS_MAX_S64,
     
    11381119
    11391120    psMetadata *expWhere = psMetadataAlloc();
    1140     psMetadata *warpWhere = psMetadataAlloc();
     1121    psMetadata *warp1Where = psMetadataAlloc(); // First set of restrictions on warp
     1122    psMetadata *warp2Where = psMetadataAlloc(); // Second set of restriction on warp
    11411123    psMetadata *stackWhere = psMetadataAlloc();
    11421124
    11431125    PXOPT_COPY_S64(config->args, expWhere, "-exp_id", "exp_id", "==");
    11441126    PXOPT_COPY_STR(config->args, expWhere, "-filter", "filter", "==");
    1145     PXOPT_COPY_S64(config->args, warpWhere, "-warp_id", "warpRun.warp_id", "==");
    1146     PXOPT_COPY_STR(config->args, warpWhere, "-skycell_id", "warpSkyfile.skycell_id", "==");
    1147     PXOPT_COPY_STR(config->args, warpWhere, "-tess_id", "warpRun.tess_id", "==");
    1148     PXOPT_COPY_STR(config->args, warpWhere, "-warp_label", "warpRun.label", "==");
    1149     PXOPT_COPY_F32(config->args, warpWhere,  "-good_frac", "warpSkyfile.good_frac", ">=");
     1127    PXOPT_COPY_STR(config->args, expWhere, "-comment", "comment", "LIKE");
     1128    PXOPT_COPY_S64(config->args, warp1Where, "-warp_id", "warpRun.warp_id", "==");
     1129    PXOPT_COPY_STR(config->args, warp1Where, "-warp_label", "warpRun.label", "==");
     1130    PXOPT_COPY_STR(config->args, warp1Where, "-tess_id", "warpRun.tess_id", "==");
     1131    PXOPT_COPY_STR(config->args, warp1Where, "-data_group", "warpRun.data_group", "==");
     1132    PXOPT_COPY_S64(config->args, warp2Where, "-warp_id", "warpRun.warp_id", "==");
     1133    PXOPT_COPY_STR(config->args, warp2Where, "-tess_id", "warpRun.tess_id", "==");
     1134    PXOPT_COPY_STR(config->args, warp2Where, "-data_group", "warpRun.data_group", "==");
     1135    PXOPT_COPY_STR(config->args, warp2Where, "-skycell_id", "warpSkyfile.skycell_id", "==");
     1136    PXOPT_COPY_STR(config->args, warp2Where, "-warp_label", "warpRun.label", "==");
     1137    PXOPT_COPY_F32(config->args, warp2Where,  "-good_frac", "warpSkyfile.good_frac", ">=");
    11501138    PXOPT_COPY_STR(config->args, stackWhere, "-stack_label", "stackRun.label", "==");
    11511139
     
    11671155    psString query = pxDataGet("difftool_definewarpstack_part1.sql");
    11681156    if (!query) {
    1169         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    1170         return false;
    1171     }
    1172 
    1173     psString warpQuery = NULL;
     1157        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1158        return false;
     1159    }
     1160
     1161    psString warp1Query = NULL;
     1162    psString warp2Query = NULL;
    11741163    psString stackQuery = NULL;
    11751164    psString expQuery = NULL;
     
    11831172    }
    11841173    psFree(expWhere);
    1185     if (psListLength(warpWhere->list)) {
    1186         psString whereClause = psDBGenerateWhereConditionSQL(warpWhere, NULL);
    1187         psStringAppend(&warpQuery, "\n AND %s", whereClause);
     1174    if (psListLength(warp1Where->list)) {
     1175        psString whereClause = psDBGenerateWhereConditionSQL(warp1Where, NULL);
     1176        psStringAppend(&warp1Query, "\n AND %s", whereClause);
    11881177        psFree(whereClause);
    11891178    } else {
    1190         warpQuery = psStringCopy("\n");
    1191     }
    1192     psFree(warpWhere);
    1193 
    1194     if (!available) {
    1195         // diff what's available, even if warp run has some faults and is incomplete
    1196         psStringAppend(&warpQuery, " AND warpRun.state = 'full'");
    1197     }
     1179        warp1Query = psStringCopy("\n");
     1180    }
     1181    psFree(warp1Where);
     1182    if (psListLength(warp2Where->list)) {
     1183        psString whereClause = psDBGenerateWhereConditionSQL(warp2Where, NULL);
     1184        psStringAppend(&warp2Query, "\n AND %s", whereClause);
     1185        psFree(whereClause);
     1186    } else {
     1187        warp2Query = psStringCopy("\n");
     1188    }
     1189    psFree(warp2Where);
    11981190
    11991191    // don't queue for exposures that have already been diff'd unless requested
     
    12141206    psFree(stackWhere);
    12151207
    1216     psTrace("difftool", 1, query, warpQuery, diffQuery, expQuery, stackQuery);
     1208    psTrace("difftool", 1, query, warp1Query, warp2Query, diffQuery, expQuery, stackQuery);
    12171209
    12181210    if (!psDBTransaction(config->dbh)) {
     
    12211213    }
    12221214
    1223     if (!p_psDBRunQueryF(config->dbh, query, warpQuery, expQuery, diffQuery)) {
     1215    if (!p_psDBRunQueryF(config->dbh, query, warp1Query, warp2Query, expQuery, diffQuery)) {
    12241216        psError(PS_ERR_UNKNOWN, false, "database error");
    12251217        psFree(query);
     
    12811273        return false;
    12821274    }
     1275    psFree(warp1Query);
    12831276    psFree(query);
    12841277    query = NULL;
     
    13121305        if (!p_psDBRunQuery(config->dbh, "DELETE FROM skycellsToDiff")) {
    13131306            psError(PS_ERR_UNKNOWN, false, "database error");
    1314             psFree(warpQuery);
     1307            psFree(warp2Query);
    13151308            psFree(stackQuery);
    13161309            psFree(skycell_query);
     
    13231316        if (!mdok) {
    13241317            psError(PXTOOLS_ERR_PROG, false, "warp_id not found --- ignoring row %ld", i);
    1325             psFree(warpQuery);
     1318            psFree(warp2Query);
    13261319            psFree(stackQuery);
    13271320            psFree(skycell_query);
     
    13341327        if (!mdok) {
    13351328            psError(PXTOOLS_ERR_PROG, false, "skycell_count not found");
    1336             psFree(warpQuery);
     1329            psFree(warp2Query);
    13371330            psFree(stackQuery);
    13381331            psFree(skycell_query);
     
    13451338        if (!mdok) {
    13461339            psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
    1347             psFree(warpQuery);
     1340            psFree(warp2Query);
    13481341            psFree(stackQuery);
    13491342            psFree(skycell_query);
     
    13531346            return false;
    13541347        }
     1348
    13551349        psString filter = psMetadataLookupStr(&mdok, row, "filter");
    13561350        if (!mdok) {
    13571351            psError(PXTOOLS_ERR_PROG, false, "filter not found");
    1358             psFree(warpQuery);
     1352            psFree(warp2Query);
    13591353            psFree(stackQuery);
    13601354            psFree(skycell_query);
     
    13641358            return false;
    13651359        }
    1366         if (!p_psDBRunQueryF(config->dbh, skycell_query, stackQuery, warp_id, filter, warpQuery)) {
    1367             psError(PS_ERR_UNKNOWN, false, "database error");
    1368             psFree(warpQuery);
     1360
     1361        psString warp_data_group = psMetadataLookupStr(&mdok, row, "data_group");
     1362        if (!mdok) {
     1363          psError(PXTOOLS_ERR_PROG, false, "warp data_group not found");
     1364          psFree(warp2Query);
     1365          psFree(stackQuery);
     1366          psFree(skycell_query);
     1367          if (!psDBRollback(config->dbh)) {
     1368            psError(PS_ERR_UNKNOWN, false, "database error");
     1369          }
     1370          return false;
     1371        }
     1372        if (warp_data_group) {
     1373          data_group = warp_data_group;
     1374        }
     1375       
     1376        if (!p_psDBRunQueryF(config->dbh, skycell_query, stackQuery, warp_id, filter, warp2Query)) {
     1377            psError(PS_ERR_UNKNOWN, false, "database error");
     1378            psFree(warp2Query);
    13691379            psFree(stackQuery);
    13701380            psFree(skycell_query);
     
    13771387
    13781388        if (num == 0) {
    1379             psTrace("difftool", PS_LOG_INFO, "no skycells with stack found for %" PRId64, warp_id);
     1389            psTrace("difftool", PS_LOG_INFO, "no skycells with stack found for warp_id %" PRId64, warp_id);
    13801390            continue;
    13811391        }
     
    13831393        if (!available && (num != skycell_count)) {
    13841394            psTrace("difftool", PS_LOG_INFO, "%" PRId64 " skyfiles with stack found for warp_id %" PRId64
    1385                     " need %" PRId64, num, warp_id, skycell_count);
     1395                    " but need %" PRId64, num, warp_id, skycell_count);
    13861396            continue;
    13871397        }
     
    14021412                true,                   // exposure
    14031413                0,       // magicked
     1414                IPP_DIFF_MODE_WARP_STACK,
    14041415                note
    14051416        );
     
    14181429        if (!p_psDBRunQuery(config->dbh, query)) {
    14191430            psError(PS_ERR_UNKNOWN, false, "database error");
    1420             psFree(warpQuery);
     1431            psFree(warp2Query);
    14211432            psFree(stackQuery);
    14221433            psFree(skycell_query);
     
    14321443        if (!p_psDBRunQuery(config->dbh, query)) {
    14331444            psError(PS_ERR_UNKNOWN, false, "database error");
    1434             psFree(warpQuery);
     1445            psFree(warp2Query);
    14351446            psFree(stackQuery);
    14361447            psFree(skycell_query);
     
    14471458            psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64,
    14481459                run->diff_id);
    1449             psFree(warpQuery);
     1460            psFree(warp2Query);
    14501461            psFree(stackQuery);
    14511462            psFree(skycell_query);
     
    14611472    }
    14621473    psFree(output);
    1463     psFree(warpQuery);
     1474    psFree(warp2Query);
    14641475    psFree(stackQuery);
    14651476    psFree(skycell_query);
     
    15491560    // Additional controls
    15501561    PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false);
    1551     PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
    15521562
    15531563    // Settings to apply to defined run
     
    15741584        psString tempCreate = pxDataGet("difftool_definewarpwarp_temp_create.sql"); // Create temp table SQL
    15751585        if (!tempCreate) {
    1576             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1586            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    15771587            psFree(selectWhere);
    15781588            psFree(insertWhere);
     
    15971607        psString tempInsert = pxDataGet("difftool_definewarpwarp_temp_insert.sql"); // Insert to temp table
    15981608        if (!tempInsert) {
    1599             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1609            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    16001610            psFree(selectWhere);
    16011611            psFree(insertWhere);
     
    16291639    psString select = pxDataGet("difftool_definewarpwarp_select.sql");
    16301640    if (!select) {
    1631         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1641        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    16321642        psFree(selectWhere);
    16331643        psFree(insertWhere);
     
    16421652    if (psListLength(selectWhere->list)) {
    16431653        psString new = psDBGenerateWhereConditionSQL(selectWhere, NULL);
    1644         psStringAppend(&whereClause, "\n%s %s", whereClause ? "AND" : "WHERE", new);
     1654        psStringAppend(&whereClause, "\nAND %s", new);
    16451655        psFree(new);
    16461656    }
    16471657    psFree(selectWhere);
    16481658
    1649     if (!available) {
    1650         psStringAppend(&whereClause,
    1651                        "\n%s inputWarpRun.state = 'full'"
    1652                        "AND templateWarpRun.state = 'full'",
    1653                        whereClause ? "AND" : "WHERE");
    1654     }
    1655 
    16561659    if (!rerun) {
    1657         psStringAppend(&whereClause, "\n%s diffs.diff_id IS NULL", whereClause ? "AND" : "WHERE");
     1660        psStringAppend(&whereClause, "\nAND diffs.diff_id IS NULL");
    16581661    }
    16591662
     
    17321735        const char *template = psMetadataLookupStr(NULL, row, "template_warp_id");
    17331736        const char *tess_id = psMetadataLookupStr(NULL, row, "tess_id");
     1737        psString input_data_group = psMetadataLookupStr(NULL, row, "input_data_group");
     1738        if (input_data_group) {
     1739          data_group = input_data_group;
     1740        }
     1741
    17341742        if (!input_id || !template || !tess_id) {
    17351743            psError(PXTOOLS_ERR_PROG, false, "Identifiers not found");
     
    17421750            return false;
    17431751        }
    1744 
    1745         diffRunRow *run = diffRunRowAlloc(0, "reg", workdir, label, data_group ? data_group : label, dist_group, reduction, NULL, registered,
    1746                                           tess_id, true, true, false, note); // Run to insert
     1752       
     1753        diffRunRow *run = diffRunRowAlloc(0,
     1754                                          "reg",
     1755                                          workdir,
     1756                                          label,
     1757                                          data_group ? data_group : label,
     1758                                          dist_group,
     1759                                          reduction,
     1760                                          NULL,  // dvodb
     1761                                          registered,
     1762                                          tess_id,
     1763                                          true,  // bothways
     1764                                          true,  // exposure
     1765                                          false, // magicked
     1766                                          IPP_DIFF_MODE_WARP_WARP,
     1767                                          note); // Run to insert
    17471768        if (!diffRunInsertObject(config->dbh, run)) {
    17481769            psError(PS_ERR_UNKNOWN, false, "database error");
     
    18161837}
    18171838
     1839static bool definestackstackMode(pxConfig *config) {
     1840  PS_ASSERT_PTR_NON_NULL(config, false);
     1841
     1842  psMetadata *stack1Where = psMetadataAlloc();
     1843  psMetadata *stack2Where = psMetadataAlloc();
     1844
     1845  PXOPT_COPY_STR(config->args, stack1Where, "-tess_id", "stackRun.tess_id", "==");
     1846  PXOPT_COPY_STR(config->args, stack2Where, "-tess_id", "stackRun.tess_id", "==");
     1847  PXOPT_COPY_STR(config->args, stack1Where, "-filter", "stackRun.filter", "==");
     1848  PXOPT_COPY_STR(config->args, stack2Where, "-filter", "stackRun.filter", "==");
     1849  PXOPT_COPY_STR(config->args, stack1Where, "-skycell_id", "stackRun.skycell_id", "==");
     1850  PXOPT_COPY_STR(config->args, stack2Where, "-skycell_id", "stackRun.skycell_id", "==");
     1851  PXOPT_COPY_STR(config->args, stack1Where, "-input_label", "stackRun.label","==");
     1852  PXOPT_COPY_STR(config->args, stack2Where, "-template_label", "stackRun.label","==");
     1853  PXOPT_COPY_F32(config->args, stack1Where, "-good_frac", "stackSumSkyfile.good_frac", ">=");
     1854  PXOPT_COPY_F32(config->args, stack2Where, "-good_frac", "stackSumSkyfile.good_frac", ">=");
     1855
     1856  PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", true, false); // required option
     1857  PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false); // option
     1858  PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false); // option
     1859  PXOPT_LOOKUP_TIME(registered, config->args, "-set_registered", false, false);
     1860  PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     1861  PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false);
     1862  PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
     1863
     1864  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1865  PXOPT_LOOKUP_BOOL(reRun, config->args, "-rerun", false);
     1866  PXOPT_LOOKUP_BOOL(newTemplates,config->args,"-new-templates", false);
     1867
     1868  PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
     1869  PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     1870
     1871  if (!(label)) {
     1872    PXOPT_LOOKUP_STR(label,config->args, "-input_label",true,false);
     1873  }
     1874
     1875  // Organize the config information into queries.
     1876  psString stack1Query = NULL;
     1877  psString stack2Query = NULL;
     1878
     1879  if (psListLength(stack1Where->list)) {
     1880    psString whereClause = psDBGenerateWhereConditionSQL(stack1Where, NULL);
     1881    psStringAppend(&stack1Query, "\n AND %s", whereClause);
     1882    psFree(whereClause);
     1883  } else {
     1884    stack1Query = psStringCopy("\n");
     1885  }
     1886  psFree(stack1Where);
     1887
     1888  if (psListLength(stack2Where->list)) {
     1889    psString whereClause = psDBGenerateWhereConditionSQL(stack2Where, NULL);
     1890    psStringAppend(&stack2Query, "\n AND %s", whereClause);
     1891    psFree(whereClause);
     1892  } else {
     1893    stack2Query = psStringCopy("\n");
     1894  }
     1895  psFree(stack2Where);
     1896
     1897  // don't queue for stacks that have already been diffed unless requested
     1898  psString diffQuery = NULL;
     1899  if (! (reRun || newTemplates) ) {
     1900    psStringAppend(&diffQuery, "\n AND diffExp.diff_id IS NULL");
     1901  } else {
     1902    diffQuery = psStringCopy("\n");
     1903  }
     1904
     1905  // find the distinct set of data_groups and filters
     1906  psString query = pxDataGet("difftool_definestackstack_part0.sql");
     1907  if (!query) {
     1908    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1909    return(false);
     1910  }
     1911  psTrace("difftool",1,query,stack2Query,stack1Query,diffQuery,stack1Query);
     1912/*   exit(10); */
     1913
     1914  if (!psDBTransaction(config->dbh)) {
     1915    psError(PS_ERR_UNKNOWN, false, "database error");
     1916    return(false);
     1917  }
     1918
     1919  if (!p_psDBRunQueryF(config->dbh, query, stack2Query, stack1Query, diffQuery, stack1Query)) {
     1920    psError(PS_ERR_UNKNOWN, false, "database error");
     1921    psFree(query);
     1922    if (!psDBRollback(config->dbh)) {
     1923      psError(PS_ERR_UNKNOWN, false, "database error");
     1924    }
     1925    return false;
     1926  }
     1927
     1928  psFree(query);
     1929
     1930  psArray *output = p_psDBFetchResult(config->dbh);
     1931  if (!output) {
     1932    psErrorCode err = psErrorCodeLast();
     1933    switch (err) {
     1934    case PS_ERR_DB_CLIENT:
     1935      psError(PXTOOLS_ERR_SYS, false, "database error");
     1936      break;
     1937    case PS_ERR_DB_SERVER:
     1938      psError(PXTOOLS_ERR_PROG, false, "database error");
     1939      break;
     1940    default:
     1941      psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1942      break;
     1943    }
     1944    if (!psDBRollback(config->dbh)) {
     1945      psError(PS_ERR_UNKNOWN, false, "database error");
     1946    }
     1947    return false;
     1948  }
     1949  if (!psArrayLength(output)) {
     1950    psTrace("difftool", PS_LOG_INFO, "no rows found");
     1951    psFree(output);
     1952    if (!psDBCommit(config->dbh)) {
     1953      psError(PS_ERR_UNKNOWN, false, "database error");
     1954      return false;
     1955    }
     1956    return true;
     1957  }
     1958
     1959  query = pxDataGet("difftool_definestackstack_part1.sql");
     1960  if (pretend) {
     1961    // negative simple so the default is true
     1962    if (!ippdbPrintMetadatas(stdout, output, "diffRunTemp", !simple)) {
     1963      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1964      psFree(output);
     1965      return false;
     1966    }
     1967    psFree(output);
     1968    return true;
     1969  }
     1970
     1971  psArray *list = psArrayAllocEmpty(16); // List of runs defined, to print
     1972  for (long i = 0; i < output->n; i++) {
     1973    psMetadata *row = output->data[i]; // Output row from query
     1974    bool mdok;                      // Status of MD lookup
     1975
     1976    psString tess_id = psMetadataLookupStr(&mdok,row,"INPUT_tess_id");
     1977    psString this_data_group = psMetadataLookupStr(&mdok,row,"INPUT_data_group");
     1978    psString this_dist_group = psMetadataLookupStr(&mdok,row,"INPUT_dist_group");
     1979    psString this_label = psMetadataLookupStr(&mdok,row,"INPUT_label");
     1980
     1981    psString this_stack1Query = psStringCopy(stack1Query);
     1982
     1983    psString thisWhere = psDBGenerateWhereConditionSQL(row,NULL);
     1984    psStringSubstitute(&thisWhere,"stackRun.","INPUT_");
     1985    psStringAppend(&this_stack1Query,"\n AND %s", thisWhere);
     1986    psFree(thisWhere);
     1987
     1988    psTrace("difftool",1, query,stack2Query,this_stack1Query,diffQuery,this_stack1Query);
     1989    if (!psDBTransaction(config->dbh)) {
     1990      psError(PS_ERR_UNKNOWN, false, "database error");
     1991      return(false);
     1992    }
     1993
     1994    if (!p_psDBRunQueryF(config->dbh, query, stack2Query, this_stack1Query, diffQuery, this_stack1Query)) {
     1995      psError(PS_ERR_UNKNOWN, false, "database error");
     1996      psFree(query);
     1997      if (!psDBRollback(config->dbh)) {
     1998        psError(PS_ERR_UNKNOWN, false, "database error");
     1999      }
     2000      return false;
     2001    }
     2002    psFree(this_stack1Query);
     2003
     2004    psArray *diff_id_output = p_psDBFetchResult(config->dbh);
     2005    if (!diff_id_output) {
     2006      psErrorCode err = psErrorCodeLast();
     2007      switch (err) {
     2008      case PS_ERR_DB_CLIENT:
     2009        psError(PXTOOLS_ERR_SYS, false, "database error");
     2010        break;
     2011      case PS_ERR_DB_SERVER:
     2012        psError(PXTOOLS_ERR_PROG, false, "database error");
     2013        break;
     2014      default:
     2015        psError(PXTOOLS_ERR_PROG, false, "unknown error");
     2016        break;
     2017      }
     2018      if (!psDBRollback(config->dbh)) {
     2019        psError(PS_ERR_UNKNOWN, false, "database error");
     2020      }
     2021      return false;
     2022    }
     2023    if (!psArrayLength(diff_id_output)) {
     2024      psTrace("difftool", PS_LOG_INFO, "no rows found");
     2025      psFree(diff_id_output);
     2026      if (!psDBCommit(config->dbh)) {
     2027        psError(PS_ERR_UNKNOWN, false, "database error");
     2028        return false;
     2029      }
     2030      return true;
     2031    }
     2032
     2033    // ok we've got one create the diffRun
     2034    diffRunRow *run = diffRunRowAlloc(
     2035                                      0,          // ID
     2036                                      "reg",      // state
     2037                                      workdir,
     2038                                      label ? label : this_label,
     2039                                      data_group ? data_group : this_data_group,
     2040                                      dist_group ? dist_group : this_dist_group,
     2041                                      reduction,
     2042                                      NULL,       // dvodb
     2043                                      registered,
     2044                                      tess_id,
     2045                                      false,                 // bothways
     2046                                      false,                 // exposure
     2047                                      0,       // magicked
     2048                                      IPP_DIFF_MODE_STACK_STACK, // diff_mode
     2049                                      note
     2050                                      );
     2051    // Commit to database
     2052    if (!diffRunInsertObject(config->dbh, run)) {
     2053      psError(PS_ERR_UNKNOWN, false, "database error");
     2054      psFree(run);
     2055      if (!psDBRollback(config->dbh)) {
     2056        psError(PS_ERR_UNKNOWN, false, "database error");
     2057      }
     2058      return false;
     2059    }
     2060/*     diffRunPrintObject(stdout,run,1); */
     2061    run->diff_id = psDBLastInsertID(config->dbh);
     2062    for (long j = 0; j < diff_id_output->n; j++) {
     2063      psMetadata *input_row = diff_id_output->data[j]; // Output row from query
     2064      bool mdok;
     2065      psString in_skycell_id  = psMetadataLookupStr(&mdok,input_row,"skycell_id");
     2066      psS64 in_input_stack_id = psMetadataLookupS64(&mdok,input_row,"stack_id");
     2067      psS64 in_template_stack_id = psMetadataLookupS64(&mdok,input_row,"max_stack_id");
     2068      psString in_tess_id = psMetadataLookupStr(&mdok,input_row,"tess_id");
     2069      psTrace("difftool",1,"%s %" PRId64 " %" PRId64 " %s\n",
     2070              in_skycell_id, in_input_stack_id, in_template_stack_id, in_tess_id);
     2071      diffInputSkyfileRow *input = diffInputSkyfileRowAlloc(
     2072                                                            run->diff_id,   // ID
     2073                                                            in_skycell_id,
     2074                                                            PS_MAX_S64, // warp1_id -> NULL
     2075                                                            in_input_stack_id, // stack1
     2076                                                            PS_MAX_S64, // warp2_id -> NULL
     2077                                                            in_template_stack_id, // stack2
     2078                                                            in_tess_id,
     2079                                                            0 // diff_skyfile_id
     2080                                                            );
     2081      // Commit to database the input
     2082      if (!diffInputSkyfileInsertObject(config->dbh, input)) {
     2083        psError(PS_ERR_UNKNOWN, false, "database error");
     2084        psFree(input);
     2085        if (!psDBRollback(config->dbh)) {
     2086          psError(PS_ERR_UNKNOWN, false, "database error");
     2087        }
     2088        return false;
     2089      }
     2090
     2091/*       diffInputSkyfilePrintObject(stdout,input,1); */
     2092      psFree(input);
     2093    }
     2094
     2095    if (!setdiffRunState(config, run->diff_id, "new", false)) {
     2096        psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, run->diff_id);
     2097        psFree(stack1Query);
     2098        psFree(stack2Query);
     2099        psFree(diffQuery);
     2100        psFree(run);
     2101        psFree(list);
     2102        if (!psDBRollback(config->dbh)) {
     2103            psError(PS_ERR_UNKNOWN, false, "database error");
     2104        }
     2105        return false;
     2106    }
     2107    psArrayAdd(list, list->n, run);
     2108    psFree(run);
     2109  }
     2110
     2111  if (!psDBCommit(config->dbh)) {
     2112    psError(PS_ERR_UNKNOWN, false, "database error");
     2113    psFree(list);
     2114    return false;
     2115  }
     2116
     2117  if (!diffRunPrintObjects(stdout, list, !simple)) {
     2118    psError(PS_ERR_UNKNOWN, false, "failed to print object");
     2119    psFree(list);
     2120    return false;
     2121  }
     2122
     2123  psFree(query);
     2124  psFree(stack1Query);
     2125  psFree(stack2Query);
     2126  psFree(diffQuery);
     2127  psFree(output);
     2128  psFree(list);
     2129
     2130    if (!psDBCommit(config->dbh)) {
     2131        psError(PS_ERR_UNKNOWN, false, "database error");
     2132        psFree(list);
     2133        return false;
     2134    }
     2135
     2136  return(true);
     2137}
    18182138
    18192139static bool pendingcleanuprunMode(pxConfig *config)
     
    18252145
    18262146    psMetadata *where = psMetadataAlloc();
    1827     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     2147    pxAddLabelSearchArgs (config, where, "-label", "diffRun.label", "==");
    18282148
    18292149    psString query = pxDataGet("difftool_pendingcleanuprun.sql");
    18302150    if (!query) {
    1831         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    1832         return false;
    1833     }
     2151        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     2152        return false;
     2153    }
     2154    // pxDataGet/psStringSubstitute workaround
     2155    psString queryCopy = psStringCopy(query);
     2156    psFree(query);
     2157    query = queryCopy;
    18342158
    18352159    if (where && psListLength(where->list)) {
    18362160        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    1837         psStringAppend(&query, " AND %s", whereClause);
     2161        psStringAppend(&whereClause, " AND ");
     2162        psStringSubstitute(&query,whereClause,"@INNERCONSTRAINT@");
    18382163        psFree(whereClause);
     2164    }
     2165    else {
     2166      psStringSubstitute(&query,NULL,"@INNERCONSTRAINT@");
    18392167    }
    18402168    psFree(where);
     
    18462174        psFree(limitString);
    18472175    }
    1848 
    18492176    //    fprintf(stderr,"%s",query);
     2177
     2178    //fprintf(stderr,"%s",query);
    18502179    if (!p_psDBRunQuery(config->dbh, query)) {
    18512180        psError(PS_ERR_UNKNOWN, false, "database error");
     
    18952224    psString query = pxDataGet("difftool_pendingcleanupskyfile.sql");
    18962225    if (!query) {
    1897         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     2226        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    18982227        return false;
    18992228    }
     
    19552284    psString query = pxDataGet("difftool_donecleanup.sql");
    19562285    if (!query) {
    1957         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     2286        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    19582287        return false;
    19592288    }
     
    20102339    psMetadata *where = psMetadataAlloc();
    20112340    PXOPT_COPY_S64(config->args, where, "-diff_id",   "diff_id",   "==");
     2341    PXOPT_COPY_STR(config->args, where, "-skycell_id", "skycell_id",   "==");
    20122342
    20132343    if (!pxSetFaultCode(config->dbh, "diffSkyfile", where, fault)) {
     
    20272357  PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false);
    20282358  PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
     2359
     2360
     2361  psS64 magicked = 0;
     2362  if (!strcmp(data_state, "full")) {
     2363      PXOPT_LOOKUP_S64(set_magicked, config->args, "-magicked", 0, false);
     2364      magicked = set_magicked;
     2365  }
    20292366
    20302367  psString query = pxDataGet("difftool_change_skyfile_data_state.sql");
     
    20352372  }
    20362373
     2374  psString set_magicked_skyfile = psStringCopy("");
     2375  psString set_magicked_run = psStringCopy("");
     2376  if (magicked != 0 && !strcmp(data_state, "full")) {
     2377    psStringAppend(&set_magicked_skyfile,  "\n , diffSkyfile.magicked = %" PRId64, magicked);
     2378    psStringAppend(&set_magicked_run,      "\n , diffRun.magicked = %" PRId64, magicked);
     2379
     2380  } else if (!strcmp(data_state, "cleaned") || !strcmp(data_state, "purged")) {
     2381    psStringAppend(&set_magicked_skyfile, "\n, diffSkyfile.magicked = IF(diffSkyfile.magicked = 0, 0, -1)");
     2382    psStringAppend(&set_magicked_run, "\n, diffRun.magicked = IF(diffRun.magicked = 0, 0, -1)");
     2383  }
     2384
    20372385  // Uses the unconstrained (diffRun.state [NEED NOT EQUAL] run_state) version from warptool.c
    20382386
    2039   if (!p_psDBRunQueryF(config->dbh, query, data_state, diff_id, skycell_id)) {
     2387  if (!p_psDBRunQueryF(config->dbh, query, data_state, set_magicked_skyfile, diff_id, skycell_id)) {
    20402388    psError(PS_ERR_UNKNOWN, false, "database error");
    20412389    // rollback
     
    20442392    }
    20452393    psError(PS_ERR_UNKNOWN, false, "database error");
     2394    psFree(set_magicked_skyfile);
    20462395    return(false);
    20472396  }
     2397  psFree(set_magicked_skyfile);
    20482398  psFree(query);
    20492399
    20502400  query = pxDataGet("difftool_change_run_state.sql");
    2051   if (!p_psDBRunQueryF(config->dbh, query, data_state, diff_id, data_state)) {
     2401  if (!p_psDBRunQueryF(config->dbh, query, data_state, set_magicked_run, diff_id, data_state)) {
    20522402    // rollback
    20532403    if (!psDBRollback(config->dbh)) {
     
    20552405    }
    20562406    psError(PS_ERR_UNKNOWN, false, "database error");
     2407    psFree(set_magicked_run);
    20572408    return(false);
    20582409  }
     
    20602411  if (!psDBCommit(config->dbh)) {
    20612412    psError(PS_ERR_UNKNOWN, false, "database error");
     2413    psFree(set_magicked_run);
    20622414    return(false);
    20632415  }
    2064  
     2416  psFree(set_magicked_run);
     2417
    20652418  return(true);
    20662419}
     
    20742427static bool toscrubbedskyfileMode(pxConfig *config) {
    20752428  return change_skyfile_data_state(config, "scrubbed", "goto_scrubbed");
     2429}
     2430static bool tofullskyfileMode(pxConfig *config) {
     2431  return change_skyfile_data_state(config, "full", "update");
    20762432}
    20772433
     
    21152471    psString query = pxDataGet(tables[i].sqlFilename);
    21162472    if (!query) {
    2117       psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     2473      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    21182474      return false;
    21192475    }
     
    22522608  return true;
    22532609}
     2610
     2611static bool listrunMode(pxConfig *config)
     2612{
     2613    PS_ASSERT_PTR_NON_NULL(config, false);
     2614
     2615    psMetadata *where = psMetadataAlloc();
     2616    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diffRun.diff_id", "==");
     2617    PXOPT_COPY_STR(config->args, where, "-tess_id", "diffRun.tess_id", "==");
     2618    PXOPT_COPY_S64(config->args, where,  "-magicked", "diffRun.magicked", "==");
     2619    pxAddLabelSearchArgs (config, where, "-label", "diffRun.label", "LIKE");
     2620    pxAddLabelSearchArgs (config, where, "-data_group", "diffRun.data_group", "LIKE");
     2621    pxAddLabelSearchArgs (config, where, "-dist_group", "diffRun.dist_group", "LIKE");
     2622
     2623    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
     2624    if (!template) {
     2625        PXOPT_COPY_S64(config->args, where, "-exp_id", "rawInput.exp_id", "==");
     2626        PXOPT_COPY_STR(config->args, where, "-exp_name", "rawInput.exp_name", "==");
     2627        PXOPT_COPY_STR(config->args, where, "-warp_id", "warpInput.warp_id", "==");
     2628        PXOPT_COPY_TIME(config->args, where, "-dateobs_begin", "rawInput.dateobs",  ">=");
     2629        PXOPT_COPY_TIME(config->args, where, "-dateobs_end",   "rawInput.dateobs",  "<=");
     2630        PXOPT_COPY_STR(config->args, where, "-filter",     "rawInput.filter", "LIKE");
     2631    } else {
     2632        PXOPT_COPY_S64(config->args, where, "-exp_id", "rawTemplate.exp_id", "==");
     2633        PXOPT_COPY_STR(config->args, where, "-exp_name", "rawTemplate.exp_name", "==");
     2634        PXOPT_COPY_STR(config->args, where, "-warp_id", "warpTemplate.warp_id", "==");
     2635        PXOPT_COPY_TIME(config->args, where, "-dateobs_begin", "rawTemplate.dateobs",  ">=");
     2636        PXOPT_COPY_TIME(config->args, where, "-dateobs_end",   "rawTemplate.dateobs",  "<=");
     2637        PXOPT_COPY_STR(config->args, where, "-filter",     "rawTemplate.filter", "LIKE");
     2638    }
     2639
     2640    PXOPT_LOOKUP_BOOL(all, config->args, "-all", false);
     2641    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     2642    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     2643    PXOPT_LOOKUP_BOOL(pstamp_order, config->args, "-pstamp_order", false);
     2644
     2645
     2646    psString where2 = NULL;
     2647    pxmagicAddWhere(config, &where2, "diffRun");
     2648    pxspaceAddWhere(config, &where2, template ? "rawTemplate" : "rawInput");
     2649    psString query = pxDataGet("difftool_listrun.sql");
     2650    if (!query) {
     2651        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     2652        return false;
     2653    }
     2654
     2655    if (psListLength(where->list)) {
     2656        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     2657        psStringAppend(&query, " WHERE %s", whereClause);
     2658        psFree(whereClause);
     2659    } else if (where2) {
     2660        psStringAppend(&query, " WHERE diffRun.diff_id is not null %s", where2);
     2661    } else if (!all) {
     2662        psError(PXTOOLS_ERR_CONFIG, true, "search parameters or -all are required");
     2663        return false;
     2664    }
     2665    psFree(where);
     2666
     2667    if (pstamp_order) {
     2668        if (template) {
     2669            psStringAppend(&query, " ORDER BY rawTemplate.exp_id, diff_id DESC");
     2670        } else {
     2671            psStringAppend(&query, " ORDER BY rawInput.exp_id, diff_id DESC");
     2672        }
     2673    }
     2674           
     2675    // treat limit == 0 as "no limit"
     2676    if (limit) {
     2677        psString limitString = psDBGenerateLimitSQL(limit);
     2678        psStringAppend(&query, " %s", limitString);
     2679        psFree(limitString);
     2680    }
     2681
     2682    if (!p_psDBRunQuery(config->dbh, query)) {
     2683        psError(PS_ERR_UNKNOWN, false, "database error");
     2684        psFree(query);
     2685        return false;
     2686    }
     2687    psFree(query);
     2688
     2689    psArray *output = p_psDBFetchResult(config->dbh);
     2690    if (!output) {
     2691        psErrorCode err = psErrorCodeLast();
     2692        switch (err) {
     2693            case PS_ERR_DB_CLIENT:
     2694                psError(PXTOOLS_ERR_SYS, false, "database error");
     2695            case PS_ERR_DB_SERVER:
     2696                psError(PXTOOLS_ERR_PROG, false, "database error");
     2697            default:
     2698                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     2699        }
     2700
     2701        return false;
     2702    }
     2703    if (!psArrayLength(output)) {
     2704        psTrace("difftool", PS_LOG_INFO, "no rows found");
     2705        psFree(output);
     2706        return true;
     2707    }
     2708
     2709    if (psArrayLength(output)) {
     2710        // negative simple so the default is true
     2711        if (!ippdbPrintMetadatas(stdout, output, "diffRun", !simple)) {
     2712            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     2713            psFree(output);
     2714            return false;
     2715        }
     2716    }
     2717
     2718    psFree(output);
     2719
     2720    return true;
     2721}
     2722// a very specfic function to queue a cleaned warpSkyfile to be updated
     2723static bool setskyfiletoupdateMode(pxConfig *config)
     2724{
     2725    PS_ASSERT_PTR_NON_NULL(config, NULL);
     2726
     2727    PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false);
     2728    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", false, false);
     2729    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     2730
     2731    psString query = pxDataGet("difftool_setskyfiletoupdate.sql");
     2732    if (!query) {
     2733        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     2734        return false;
     2735    }
     2736
     2737    psString setHook = psStringCopy("");
     2738    if (label) {
     2739        psStringAppend(&setHook, "\n , diffRun.label = '%s'", label);
     2740    }
     2741
     2742    if (skycell_id) {
     2743        psStringAppend(&query, " AND (diffSkyfile.skycell_id = '%s')", skycell_id);
     2744    }
     2745
     2746    if (!p_psDBRunQueryF(config->dbh, query, setHook, diff_id)) {
     2747        psError(PS_ERR_UNKNOWN, false, "database error");
     2748        return false;
     2749    }
     2750
     2751    psFree(setHook);
     2752    psFree(query);
     2753
     2754    return true;
     2755}
Note: See TracChangeset for help on using the changeset viewer.