IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 13, 2008, 10:17:34 AM (17 years ago)
Author:
bills
Message:

Rework the diff stage to process the skycells from an exposure in a single
diffRun

File:
1 edited

Legend:

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

    r20719 r20973  
    4747
    4848static bool setdiffRunState(pxConfig *config, psS64 diff_id, const char *state);
     49static bool diffRunComplete(pxConfig *config);
    4950
    5051# define MODECASE(caseName, func) \
     
    108109    // required options
    109110    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
    110     PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
    111111    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, false);
    112112    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    113113    PXOPT_LOOKUP_STR(reduction, config->args, "-reduction", false, false);
     114    PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", false, false);
    114115
    115116    // default
     
    120121            0,          // ID
    121122            "reg",      // state
     123            workdir,
    122124            label,
    123125            reduction,
    124             workdir,
    125126            NULL,       // dvodb
    126127            registered,
    127             skycell_id,
    128             tess_id
     128            tess_id,
     129            exp_id
    129130    );
    130131    if (!run) {
     
    178179
    179180    // optional
    180     PXOPT_LOOKUP_S64(stack_id, config->args, "-stack_id", false, false);
    181     PXOPT_LOOKUP_S64(warp_id, config->args, "-warp_id", false, false);
    182     PXOPT_LOOKUP_STR(kind, config->args, "-kind", false, false);
    183 
    184     // defaults to false
    185     PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
    186 
    187     // must provide either stack_id or warp_id but not BOTH
    188     if (!(stack_id || warp_id)) {
    189         psError(PS_ERR_UNKNOWN, true, "either -stack_id or -warp_id must be specified");
    190         return false;
    191     }
    192     if (stack_id && warp_id) {
    193         psError(PS_ERR_UNKNOWN, true, "either -stack_id or -warp_id must be specified");
    194         return false;
    195     }
    196 
    197     // if a warp_id was provided we need to lookup the skycell_id and tess_id
    198     // from the warpRun
    199     psString skycell_id = NULL;
     181    PXOPT_LOOKUP_S64(stack1, config->args, "-stack1", false, false);
     182    PXOPT_LOOKUP_S64(stack2, config->args, "-stack2", false, false);
     183    PXOPT_LOOKUP_S64(warp1, config->args, "-warp1", false, false);
     184    PXOPT_LOOKUP_S64(warp2, config->args, "-warp2", false, false);
     185    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
     186
     187    // must provide either stack1 or warp1 but not BOTH
     188    if (!(stack1 || warp1)) {
     189        psError(PS_ERR_UNKNOWN, true, "either -stack1 or -warp1 must be specified");
     190        return false;
     191    }
     192    if (stack1 && warp1) {
     193        psError(PS_ERR_UNKNOWN, true, "either -stack1 or -warp1 must be specified");
     194        return false;
     195    }
     196    // must provide either stack2 or warp2 but not BOTH
     197    if (!(stack2 || warp2)) {
     198        psError(PS_ERR_UNKNOWN, true, "either -stack2 or -warp2 must be specified");
     199        return false;
     200    }
     201    if (stack2 && warp2) {
     202        psError(PS_ERR_UNKNOWN, true, "either -stack2 or -warp2 must be specified");
     203        return false;
     204    }
     205
     206    // if a warp1 was provided we need to lookup the and tess_id from the diffRun
    200207    psString tess_id = NULL;
    201     if (warp_id) {
     208    if (warp1) {
    202209        if (!p_psDBRunQuery(config->dbh, "SELECT * from diffRun WHERE diff_id = %" PRId64, diff_id)) {
    203210            psError(PS_ERR_UNKNOWN, false, "database error");
     
    217224
    218225        diffRunRow *run = diffRunObjectFromMetadata(output->data[0]);
    219         skycell_id = run->skycell_id;
    220226        tess_id = run->tess_id;
    221227    }
     
    228234    if (!diffInputSkyfileInsert(config->dbh,
    229235            diff_id,
    230             template,
    231             stack_id ? stack_id : PS_MAX_S64, // defined or NULL
    232             warp_id ? warp_id : PS_MAX_S64, // defined or NULL
    233236            skycell_id,
    234             tess_id,
    235             kind
     237            warp1 ? warp1 : PS_MAX_S64, // defined or NULL
     238            stack1 ? stack1 : PS_MAX_S64, // defined or NULL
     239            warp2 ? warp2 : PS_MAX_S64, // defined or NULL
     240            stack2 ? stack2 : PS_MAX_S64, // defined or NULL
     241            tess_id
    236242        )) {
    237243        if (!psDBRollback(config->dbh)) {
     
    298304    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diff_id", "==");
    299305    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
    300     PXOPT_COPY_STR(config->args, where, "-skycell_id", "skycell_id", "==");
     306    PXOPT_COPY_STR(config->args, where, "-skycell_id", "diffInputSkyfile.skycell_id", "==");
    301307    PXOPT_COPY_STR(config->args, where,  "-tess_id", "tess_id", "==");
    302     PXOPT_COPY_STR(config->args, where,  "-kind", "kind", "==");
    303308
    304309    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    349354    }
    350355
    351     if (!p_psDBRunQuery(config->dbh, query, whereClause, whereClause)) {
     356    if (!p_psDBRunQuery(config->dbh, query, whereClause, whereClause, whereClause, whereClause)) {
    352357        psError(PS_ERR_UNKNOWN, false, "database error");
    353358        psFree(whereClause);
     
    471476
    472477    PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false); // required
     478    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
    473479    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
    474480    PXOPT_LOOKUP_STR(uri, config->args, "-uri", (code == 0), false);
     
    503509    if (!diffSkyfileInsert(config->dbh,
    504510                           diff_id,
     511                           skycell_id,
    505512                           uri,
    506513                           path_base,
     
    524531                           hostname,
    525532                           good_frac,
    526                            code
     533                           code,
     534                           0,       // diff_image_id
     535                           0        // magic_ds_id
    527536          )) {
    528537        if (!psDBRollback(config->dbh)) {
     
    533542    }
    534543
    535     if (!setdiffRunState(config, diff_id, "full")) {
     544    if (!diffRunComplete(config)) {
    536545        if (!psDBRollback(config->dbh)) {
    537546            psError(PS_ERR_UNKNOWN, false, "database error");
     
    546555        return false;
    547556    }
     557
    548558
    549559    return true;
     
    762772                         psS64 template_warp_id, // Warp identifier for template image, PS_MAX_S64 for none
    763773                         psS64 template_stack_id, // Stack identifier for template image, PS_MAX_S64 for none
     774                         psS64 exp_id, // exposure id for input_warp_id (if defined)
    764775                         pxConfig *config // Configuration
    765776                         )
     
    786797            0,          // ID
    787798            "reg",      // state
     799            workdir,
    788800            label,
    789801            reduction,
    790             workdir,
    791802            NULL,       // dvodb
    792803            registered,
    793             skycell_id,
    794             tess_id
     804            tess_id,
     805            exp_id
    795806    );
    796807
     
    817828    if (!diffInputSkyfileInsert(config->dbh,
    818829            run->diff_id,
    819             true,
     830            skycell_id,
     831            input_warp_id,
     832            input_stack_id,
     833            template_warp_id,
    820834            template_stack_id,
    821             template_warp_id,
    822             skycell_id,
    823             tess_id,
    824             NULL    // kind
    825         )) {
    826         if (!psDBRollback(config->dbh)) {
    827             psError(PS_ERR_UNKNOWN, false, "database error");
    828         }
    829         psError(PS_ERR_UNKNOWN, false, "database error");
    830         return false;
    831     }
    832 
    833     // Input
    834     if (!diffInputSkyfileInsert(config->dbh,
    835             run->diff_id,
    836             false,
    837             input_stack_id,
    838             input_warp_id,
    839             skycell_id,
    840             tess_id,
    841             NULL    // kind
     835            tess_id
    842836        )) {
    843837        if (!psDBRollback(config->dbh)) {
     
    887881    PXOPT_LOOKUP_S64(input_warp_id, config->args, "-input_warp_id", false, false);
    888882    PXOPT_LOOKUP_S64(input_stack_id, config->args, "-input_stack_id", false, false);
     883    PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", false, false);
    889884    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    890885
     
    906901        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    907902                "No input has been defined (-input_stack_id or -input_warp_id)");
     903        return false;
     904    }
     905    if (input_warp_id && !exp_id) {
     906        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     907                "-exp_id is required with -input_warp_id.");
    908908        return false;
    909909    }
     
    916916                      template_warp_id ? template_warp_id : PS_MAX_S64,
    917917                      template_stack_id ? template_stack_id : PS_MAX_S64,
     918                      exp_id,
    918919                      config)) {
    919920        psError(PS_ERR_UNKNOWN, false, "failed to create populated diffRun");
     
    937938    PS_ASSERT_PTR_NON_NULL(config, false);
    938939
     940    psMetadata *expWhere = psMetadataAlloc();
    939941    psMetadata *warpWhere = psMetadataAlloc();
    940942    psMetadata *stackWhere = psMetadataAlloc();
    941943
     944    PXOPT_COPY_S64(config->args, expWhere, "-exp_id", "exp_id", "==");
    942945    PXOPT_COPY_S64(config->args, warpWhere, "-warp_id", "warpRun.warp_id", "==");
    943946    PXOPT_COPY_STR(config->args, warpWhere, "-skycell_id", "warpRun.skycell_id", "==");
     
    945948    PXOPT_COPY_STR(config->args, warpWhere, "-filter", "rawExp.filter", "==");
    946949    PXOPT_COPY_STR(config->args, warpWhere, "-warp_label", "warpRun.label", "==");
    947     PXOPT_COPY_STR(config->args, warpWhere,  "-kind", "warpsToDiff.kind", "==");
    948950    PXOPT_COPY_F32(config->args, warpWhere,  "-good_frac", "warpSkyfile.good_frac", ">=");
    949951    PXOPT_COPY_STR(config->args, stackWhere, "-stack_label", "stackRun.label", "==");
     
    955957    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    956958    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);
    957961
    958962    // find all things to queue
    959     psString query = pxDataGet("difftool_definebyquery.sql");
     963    psString query = pxDataGet("difftool_definebyquery_part1.sql");
    960964    if (!query) {
    961965        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    963967    }
    964968
    965     if (newTemplates) {
    966         // Warps that haven't been diffed OR warps that can take a newer template in the diff
    967         psStringAppend(&query, " WHERE (diff_id IS NULL OR best_stack_id > current_stack_id)");
    968     } else {
    969         // Only warps that haven't been diffed
    970         psStringAppend(&query, " WHERE diff_id IS NULL");
    971     }
    972 
    973969    psString warpQuery = NULL;
    974970    psString stackQuery = NULL;
    975 
     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);
    976981    if (psListLength(warpWhere->list)) {
    977982        psString whereClause = psDBGenerateWhereConditionSQL(warpWhere, NULL);
     
    979984        psFree(whereClause);
    980985    } else {
    981         warpQuery = psStringCopy("");
     986        warpQuery = psStringCopy("\n");
    982987    }
    983988    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    } else {
     1000        diffQuery = psStringCopy("\n");
     1001    }
    9841002
    9851003    if (psListLength(stackWhere->list)) {
    9861004        psString whereClause = psDBGenerateWhereConditionSQL(stackWhere, NULL);
    987         psStringAppend(&stackQuery, "\n AND %s", whereClause);
     1005        psStringAppend(&stackQuery, "\nAND %s", whereClause);
    9881006        psFree(whereClause);
    9891007    } else {
     
    9921010    psFree(stackWhere);
    9931011
    994     psTrace("difftool", 1, query, warpQuery, stackQuery);
    995 
    996     if (!p_psDBRunQuery(config->dbh, query, warpQuery, stackQuery)) {
     1012    psTrace("difftool", 1, query, warpQuery, diffQuery, expQuery, stackQuery);
     1013
     1014
     1015    if (!p_psDBRunQuery(config->dbh, query, warpQuery, expQuery, diffQuery)) {
    9971016        psError(PS_ERR_UNKNOWN, false, "database error");
    9981017        psFree(query);
     
    10211040    }
    10221041
     1042    // create temporary table
     1043    query = pxDataGet("difftool_definebyquery_temp_create.sql");
     1044    if (!p_psDBRunQuery(config->dbh, query)) {
     1045        psError(PS_ERR_UNKNOWN, false, "database error");
     1046        psFree(query);
     1047        return false;
     1048    }
     1049    psFree(query);
     1050    query = NULL;
     1051
     1052    psString skycell_query = pxDataGet("difftool_definebyquery_part2.sql");
     1053
    10231054    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    10241055    long numGood = 0;                   // Number of good rows added
     1056    psS64 last_exp_id = 0;
    10251057    for (long i = 0; i < output->n; i++) {
    10261058        psMetadata *row = output->data[i]; // Output row from query
    1027 
    1028         // Selected parameters
    10291059        bool mdok;                      // Status of MD lookup
    1030         const char *skycell_id = psMetadataLookupStr(&mdok, row, "skycell_id");
    1031         if (!mdok || !skycell_id) {
    1032             psWarning("skycell_id not found --- ignoring row %ld", i);
     1060
     1061        // Take the first warp for each exposure.
     1062        // The list is sorted by exposure id and warp_id and the warps are in descending
     1063        // order.
     1064        psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id");
     1065        if (!mdok) {
     1066            psError(PXTOOLS_ERR_PROG, false, "exp_id not found");
     1067            return false;
     1068        }
     1069        if (exp_id == last_exp_id) {
    10331070            continue;
    10341071        }
    1035         const char *tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
    1036         if (!mdok || !tess_id) {
    1037             psWarning("tess_id not found --- ignoring row %ld", i);
    1038             continue;
    1039         }
    1040         psS64 stack_id = psMetadataLookupS64(&mdok, row, "best_stack_id");
    1041         if (!mdok) {
    1042             psWarning("stack_id not found --- ignoring row %ld", i);
    1043             continue;
     1072        last_exp_id = exp_id;
     1073
     1074        // clear temporary table
     1075        if (!p_psDBRunQuery(config->dbh, "DELETE FROM skycellsToDiff")) {
     1076            psError(PS_ERR_UNKNOWN, false, "database error");
     1077            psFree(warpQuery);
     1078            psFree(stackQuery);
     1079            psFree(skycell_query);
     1080            return false;
    10441081        }
    10451082        psS64 warp_id = psMetadataLookupS64(&mdok, row, "warp_id");
    10461083        if (!mdok) {
    1047             psWarning("warp_id not found --- ignoring row %ld", i);
     1084            psError(PXTOOLS_ERR_PROG, false, "warp_id not found --- ignoring row %ld", i);
     1085            psFree(warpQuery);
     1086            psFree(stackQuery);
     1087            psFree(skycell_query);
     1088            return false;
     1089        }
     1090        psS64 skycell_count = psMetadataLookupS64(&mdok, row, "skycell_count");
     1091        if (!mdok) {
     1092            psError(PXTOOLS_ERR_PROG, false, "skycell_count not found");
     1093            psFree(warpQuery);
     1094            psFree(stackQuery);
     1095            psFree(skycell_query);
     1096            return false;
     1097        }
     1098        psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
     1099        if (!mdok) {
     1100            psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
     1101            psFree(warpQuery);
     1102            psFree(stackQuery);
     1103            psFree(skycell_query);
     1104            return false;
     1105        }
     1106        psString filter = psMetadataLookupStr(&mdok, row, "filter");
     1107        if (!mdok) {
     1108            psError(PXTOOLS_ERR_PROG, false, "filter not found");
     1109            psFree(warpQuery);
     1110            psFree(stackQuery);
     1111            psFree(skycell_query);
     1112            return false;
     1113        }
     1114        if (!p_psDBRunQuery(config->dbh, skycell_query, warp_id, filter, warpQuery, stackQuery)) {
     1115            psError(PS_ERR_UNKNOWN, false, "database error");
     1116            psFree(warpQuery);
     1117            psFree(stackQuery);
     1118            psFree(skycell_query);
     1119            return false;
     1120        }
     1121        psS64 num = psDBAffectedRows(config->dbh);
     1122
     1123        if (num == 0) {
     1124            psTrace("difftool", PS_LOG_INFO, "no skycells with stack found for %" PRId64, warp_id);
    10481125            continue;
    10491126        }
    10501127
    1051         if (!populatedrun(list, workdir, skycell_id, tess_id, label, reduction, warp_id,
    1052                           PS_MAX_S64, PS_MAX_S64, stack_id, config)) {
    1053             psWarning("Unable to add run for %s,%s,%" PRId64 ",%" PRId64, skycell_id, tess_id,
    1054                       warp_id, stack_id);
    1055             psErrorClear();
     1128        if (!available && (num != skycell_count)) {
     1129            psTrace("difftool", PS_LOG_INFO, "%" PRId64 " skyfiles with stack found for warp_id %"
     1130                PRId64 " need %" PRId64, num, skycell_count, warp_id);
    10561131            continue;
    10571132        }
    10581133
     1134        // ok we've got one create the diffRun
     1135        diffRunRow *run = diffRunRowAlloc(
     1136                0,          // ID
     1137                "reg",      // state
     1138                workdir,
     1139                label,
     1140                reduction,
     1141                NULL,       // dvodb
     1142                registered,
     1143                tess_id,
     1144                exp_id
     1145        );
     1146
     1147        if (!diffRunInsertObject(config->dbh, run)) {
     1148            psError(PS_ERR_UNKNOWN, false, "database error");
     1149            psFree(run);
     1150            return true;
     1151        }
     1152        run->diff_id = psDBLastInsertID(config->dbh);
     1153
     1154        psStringAppend(&query, "UPDATE skycellsToDiff SET diff_id = %" PRId64, run->diff_id);
     1155        if (!p_psDBRunQuery(config->dbh, query)) {
     1156            psError(PS_ERR_UNKNOWN, false, "database error");
     1157            psFree(warpQuery);
     1158            psFree(stackQuery);
     1159            psFree(skycell_query);
     1160            psFree(query);
     1161            return false;
     1162        }
     1163        psFree(query);
     1164        query = NULL;
     1165        psStringAppend(&query, "INSERT INTO diffInputSkyfile SELECT * from skycellsToDiff");
     1166        if (!p_psDBRunQuery(config->dbh, query)) {
     1167            psError(PS_ERR_UNKNOWN, false, "database error");
     1168            psFree(warpQuery);
     1169            psFree(stackQuery);
     1170            psFree(skycell_query);
     1171            psFree(query);
     1172            return false;
     1173        }
     1174        psFree(query);
     1175        query = NULL;
     1176
     1177        if (!setdiffRunState(config, run->diff_id, "new")) {
     1178            psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64,
     1179                run->diff_id);
     1180            psFree(warpQuery);
     1181            psFree(stackQuery);
     1182            psFree(skycell_query);
     1183            psFree(query);
     1184            return false;
     1185        }
     1186
     1187        psArrayAdd(list, list->n, run);
    10591188        numGood++;
    10601189    }
    10611190    psFree(output);
     1191    psFree(warpQuery);
     1192    psFree(stackQuery);
     1193    psFree(skycell_query);
    10621194
    10631195    if (!diffRunPrintObjects(stdout, list, !simple)) {
     
    12741406}
    12751407
     1408static bool diffRunComplete(pxConfig *config)
     1409{
     1410    PS_ASSERT_PTR_NON_NULL(config, false);
     1411
     1412    // look for completed diffRuns
     1413    psString query = pxDataGet("difftool_completed_runs.sql");
     1414    if (!query) {
     1415        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1416        return false;
     1417    }
     1418
     1419    if (!p_psDBRunQuery(config->dbh, query)) {
     1420        psError(PS_ERR_UNKNOWN, false, "database error");
     1421        psFree(query);
     1422        return false;
     1423    }
     1424    psFree(query);
     1425
     1426    psArray *output = p_psDBFetchResult(config->dbh);
     1427    if (!output) {
     1428        psError(PS_ERR_UNKNOWN, false, "database error");
     1429        return false;
     1430    }
     1431    if (!psArrayLength(output)) {
     1432        psTrace("difftool", PS_LOG_INFO, "no rows found");
     1433        psFree(output);
     1434        return true;
     1435    }
     1436    for (long i = 0; i < psArrayLength(output); i++) {
     1437        psMetadata *row = output->data[i];
     1438
     1439        psS64 diff_id = psMetadataLookupS64(NULL, row, "diff_id");
     1440
     1441        // set diffRun.state to 'stop'
     1442        if (!setdiffRunState(config, diff_id, "full")) {
     1443            psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64,
     1444                diff_id);
     1445            psFree(output);
     1446            return false;
     1447        }
     1448    }
     1449
     1450    return true;
     1451}
     1452
Note: See TracChangeset for help on using the changeset viewer.