IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 10, 2008, 6:11:39 PM (18 years ago)
Author:
bills
Message:

diff re-work nearly complete

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/bills_081204/ippTools/src/difftool.c

    r20903 r20954  
    121121            0,          // ID
    122122            "reg",      // state
     123            workdir,
    123124            label,
    124125            reduction,
    125             workdir,
    126126            NULL,       // dvodb
    127127            registered,
    128             exp_id,
    129             tess_id
     128            tess_id,
     129            exp_id
    130130    );
    131131    if (!run) {
     
    234234    if (!diffInputSkyfileInsert(config->dbh,
    235235            diff_id,
     236            skycell_id,
    236237            warp1 ? warp1 : PS_MAX_S64, // defined or NULL
     238            stack1 ? stack1 : PS_MAX_S64, // defined or NULL
    237239            warp2 ? warp2 : PS_MAX_S64, // defined or NULL
    238             stack1 ? stack1 : PS_MAX_S64, // defined or NULL
    239240            stack2 ? stack2 : PS_MAX_S64, // defined or NULL
    240             skycell_id,
    241241            tess_id
    242242        )) {
     
    304304    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
    305305    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
    306     PXOPT_COPY_STR(config->args, where, "-skycell_id", "skycell_id", "==");
     306    PXOPT_COPY_STR(config->args, where, "-skycell_id", "diffInputSkyfile.skycell_id", "==");
    307307    PXOPT_COPY_STR(config->args, where,  "-tess_id", "tess_id", "==");
    308308
     
    531531                           hostname,
    532532                           good_frac,
    533                            code
     533                           code,
     534                           0,       // diff_image_id
     535                           0        // magic_ds_id
    534536          )) {
    535537        if (!psDBRollback(config->dbh)) {
     
    795797            0,          // ID
    796798            "reg",      // state
     799            workdir,
    797800            label,
    798801            reduction,
    799             workdir,
    800802            NULL,       // dvodb
    801803            registered,
    802             exp_id,
    803             tess_id
     804            tess_id,
     805            exp_id
    804806    );
    805807
     
    826828    if (!diffInputSkyfileInsert(config->dbh,
    827829            run->diff_id,
     830            skycell_id,
    828831            input_warp_id,
     832            input_stack_id,
    829833            template_warp_id,
    830             input_stack_id,
    831834            template_stack_id,
    832             skycell_id,
    833835            tess_id
    834836        )) {
     
    936938    PS_ASSERT_PTR_NON_NULL(config, false);
    937939
     940    psMetadata *expWhere = psMetadataAlloc();
    938941    psMetadata *warpWhere = psMetadataAlloc();
    939942    psMetadata *stackWhere = psMetadataAlloc();
    940943
     944    PXOPT_COPY_S64(config->args, expWhere, "-exp_id", "exp_id", "==");
    941945    PXOPT_COPY_S64(config->args, warpWhere, "-warp_id", "warpRun.warp_id", "==");
    942946    PXOPT_COPY_STR(config->args, warpWhere, "-skycell_id", "warpRun.skycell_id", "==");
     
    953957    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    954958    PXOPT_LOOKUP_BOOL(newTemplates, config->args, "-new-templates", false);
     959    PXOPT_LOOKUP_BOOL(reRun, config->args, "-rerun", false);
     960    PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
    955961
    956962    // find all things to queue
    957     psString query = pxDataGet("difftool_definebyquery.sql");
     963    psString query = pxDataGet("difftool_definebyquery_part1.sql");
    958964    if (!query) {
    959965        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    961967    }
    962968
    963     if (newTemplates) {
    964         // Warps that haven't been diffed OR warps that can take a newer template in the diff
    965         psStringAppend(&query, " WHERE (diff_id IS NULL OR best_stack_id > current_stack_id)");
    966     } else {
    967         // Only warps that haven't been diffed
    968         psStringAppend(&query, " WHERE diff_id IS NULL");
    969     }
    970 
    971969    psString warpQuery = NULL;
    972970    psString stackQuery = NULL;
    973 
     971    psString expQuery = NULL;
     972
     973    if (psListLength(expWhere->list)) {
     974        psString whereClause = psDBGenerateWhereConditionSQL(expWhere, NULL);
     975        psStringAppend(&expQuery, "\n AND %s", whereClause);
     976        psFree(whereClause);
     977    } else {
     978        expQuery = psStringCopy("\n");
     979    }
     980    psFree(expWhere);
    974981    if (psListLength(warpWhere->list)) {
    975982        psString whereClause = psDBGenerateWhereConditionSQL(warpWhere, NULL);
     
    977984        psFree(whereClause);
    978985    } else {
    979         warpQuery = psStringCopy("");
     986        warpQuery = psStringCopy("\n");
    980987    }
    981988    psFree(warpWhere);
     989
     990    if (!available) {
     991        // diff what's available, even if warp run has some faults and is incomplete
     992        psStringAppend(&warpQuery, " AND warpRun.state = 'full'");
     993    }
     994
     995    // don't queue for exposures that have already been diff'd unless requested
     996    psString diffQuery = NULL;
     997    if (! (reRun || newTemplates) ) {
     998        psStringAppend(&diffQuery, "\nAND diff_id IS NULL");
     999    }
    9821000
    9831001    if (psListLength(stackWhere->list)) {
    9841002        psString whereClause = psDBGenerateWhereConditionSQL(stackWhere, NULL);
    985         psStringAppend(&stackQuery, "\n AND %s", whereClause);
     1003        psStringAppend(&stackQuery, "\nAND %s", whereClause);
    9861004        psFree(whereClause);
    9871005    } else {
     
    9901008    psFree(stackWhere);
    9911009
    992     psTrace("difftool", 1, query, warpQuery, stackQuery);
    993 
    994     if (!p_psDBRunQuery(config->dbh, query, warpQuery, stackQuery)) {
     1010    psTrace("difftool", 1, query, warpQuery, diffQuery, expQuery, stackQuery);
     1011
     1012
     1013    if (!p_psDBRunQuery(config->dbh, query, warpQuery, expQuery, diffQuery)) {
    9951014        psError(PS_ERR_UNKNOWN, false, "database error");
    9961015        psFree(query);
     
    10191038    }
    10201039
     1040    // create temporary table
     1041    query = pxDataGet("difftool_definebyquery_temp_create.sql");
     1042    if (!p_psDBRunQuery(config->dbh, query)) {
     1043        psError(PS_ERR_UNKNOWN, false, "database error");
     1044        psFree(query);
     1045        return false;
     1046    }
     1047    psFree(query);
     1048    query = NULL;
     1049
     1050    psString skycell_query = pxDataGet("difftool_definebyquery_part2.sql");
     1051
    10211052    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    10221053    long numGood = 0;                   // Number of good rows added
     1054    psS64 last_exp_id = 0;
    10231055    for (long i = 0; i < output->n; i++) {
    10241056        psMetadata *row = output->data[i]; // Output row from query
    1025 
    1026         // Selected parameters
    10271057        bool mdok;                      // Status of MD lookup
    1028         const char *skycell_id = psMetadataLookupStr(&mdok, row, "skycell_id");
    1029         if (!mdok || !skycell_id) {
    1030             psWarning("skycell_id not found --- ignoring row %ld", i);
     1058
     1059        // Take the first warp for each exposure.
     1060        // The list is sorted by exposure id and warp_id and the warps are in descending
     1061        // order.
     1062        psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id");
     1063        if (!mdok) {
     1064            psError(PXTOOLS_ERR_PROG, false, "exp_id not found");
     1065            return false;
     1066        }
     1067        if (exp_id == last_exp_id) {
    10311068            continue;
    10321069        }
    1033         const char *tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
    1034         if (!mdok || !tess_id) {
    1035             psWarning("tess_id not found --- ignoring row %ld", i);
    1036             continue;
    1037         }
    1038         psS64 stack_id = psMetadataLookupS64(&mdok, row, "best_stack_id");
    1039         if (!mdok) {
    1040             psWarning("stack_id not found --- ignoring row %ld", i);
    1041             continue;
     1070        last_exp_id = exp_id;
     1071
     1072        // clear temporary table
     1073        if (!p_psDBRunQuery(config->dbh, "DELETE FROM skycellsToDiff")) {
     1074            psError(PS_ERR_UNKNOWN, false, "database error");
     1075            psFree(warpQuery);
     1076            psFree(stackQuery);
     1077            psFree(skycell_query);
     1078            return false;
    10421079        }
    10431080        psS64 warp_id = psMetadataLookupS64(&mdok, row, "warp_id");
    10441081        if (!mdok) {
    1045             psWarning("warp_id not found --- ignoring row %ld", i);
     1082            psError(PXTOOLS_ERR_PROG, false, "warp_id not found --- ignoring row %ld", i);
     1083            psFree(warpQuery);
     1084            psFree(stackQuery);
     1085            psFree(skycell_query);
     1086            return false;
     1087        }
     1088        psS64 skycell_count = psMetadataLookupS64(&mdok, row, "skycell_count");
     1089        if (!mdok) {
     1090            psError(PXTOOLS_ERR_PROG, false, "skycell_count not found");
     1091            psFree(warpQuery);
     1092            psFree(stackQuery);
     1093            psFree(skycell_query);
     1094            return false;
     1095        }
     1096        psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
     1097        if (!mdok) {
     1098            psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
     1099            psFree(warpQuery);
     1100            psFree(stackQuery);
     1101            psFree(skycell_query);
     1102            return false;
     1103        }
     1104        if (!p_psDBRunQuery(config->dbh, skycell_query, warp_id, warpQuery, stackQuery)) {
     1105            psError(PS_ERR_UNKNOWN, false, "database error");
     1106            psFree(warpQuery);
     1107            psFree(stackQuery);
     1108            psFree(skycell_query);
     1109            return false;
     1110        }
     1111        psS64 num = psDBAffectedRows(config->dbh);
     1112
     1113        if (num == 0) {
     1114            psTrace("difftool", PS_LOG_INFO, "no skycells with stack found for %" PRId64, warp_id);
    10461115            continue;
    10471116        }
    1048         psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id");
    1049         if (!mdok) {
    1050             psWarning("exp_id not found --- ignoring row %ld", i);
     1117
     1118        if (!available && (num != skycell_count)) {
     1119            psTrace("difftool", PS_LOG_INFO, "%" PRId64 " skyfiles with stack found for warp_id %"
     1120                PRId64 " need %" PRId64, num, skycell_count, warp_id);
    10511121            continue;
    10521122        }
    10531123
    1054         if (!populatedrun(list, workdir, skycell_id, tess_id, label, reduction, warp_id,
    1055                           PS_MAX_S64, PS_MAX_S64, stack_id, exp_id, config)) {
    1056             psWarning("Unable to add run for %s,%s,%" PRId64 ",%" PRId64, skycell_id, tess_id,
    1057                       warp_id, stack_id);
    1058             psErrorClear();
    1059             continue;
    1060         }
    1061 
     1124        // ok we've got one create the diffRun
     1125        diffRunRow *run = diffRunRowAlloc(
     1126                0,          // ID
     1127                "reg",      // state
     1128                workdir,
     1129                label,
     1130                reduction,
     1131                NULL,       // dvodb
     1132                registered,
     1133                tess_id,
     1134                exp_id
     1135        );
     1136
     1137        if (!diffRunInsertObject(config->dbh, run)) {
     1138            psError(PS_ERR_UNKNOWN, false, "database error");
     1139            psFree(run);
     1140            return true;
     1141        }
     1142        run->diff_id = psDBLastInsertID(config->dbh);
     1143
     1144        psStringAppend(&query, "UPDATE skycellsToDiff SET diff_id = %" PRId64, run->diff_id);
     1145        if (!p_psDBRunQuery(config->dbh, query)) {
     1146            psError(PS_ERR_UNKNOWN, false, "database error");
     1147            psFree(warpQuery);
     1148            psFree(stackQuery);
     1149            psFree(skycell_query);
     1150            psFree(query);
     1151            return false;
     1152        }
     1153        psFree(query);
     1154        query = NULL;
     1155        psStringAppend(&query, "INSERT INTO diffInputSkyfile SELECT * from skycellsToDiff");
     1156        if (!p_psDBRunQuery(config->dbh, query)) {
     1157            psError(PS_ERR_UNKNOWN, false, "database error");
     1158            psFree(warpQuery);
     1159            psFree(stackQuery);
     1160            psFree(skycell_query);
     1161            psFree(query);
     1162            return false;
     1163        }
     1164
     1165        if (!setdiffRunState(config, run->diff_id, "new")) {
     1166            psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64,
     1167                run->diff_id);
     1168            psFree(warpQuery);
     1169            psFree(stackQuery);
     1170            psFree(skycell_query);
     1171            psFree(query);
     1172            return false;
     1173        }
     1174
     1175        psArrayAdd(list, list->n, run);
    10621176        numGood++;
    10631177    }
Note: See TracChangeset for help on using the changeset viewer.