IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 26, 2009, 1:59:32 PM (17 years ago)
Author:
beaumont
Message:

merged with trunk

Location:
branches/cnb_branches/cnb_branch_20090301
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/cnb_branches/cnb_branch_20090301

  • branches/cnb_branches/cnb_branch_20090301/ippTools/src

    • Property svn:ignore
      •  

        old new  
        3333pstamptool
        3434disttool
         35receivetool
  • branches/cnb_branches/cnb_branch_20090301/ippTools/src/difftool.c

    r23594 r24244  
    4040static bool revertdiffskyfileMode(pxConfig *config);
    4141static bool definepoprunMode(pxConfig *config);
    42 static bool definebyqueryMode(pxConfig *config);
     42static bool definewarpstackMode(pxConfig *config);
    4343static bool definewarpwarpMode(pxConfig *config);
    4444static bool pendingcleanuprunMode(pxConfig *config);
     
    7979        MODECASE(DIFFTOOL_MODE_REVERTDIFFSKYFILE,     revertdiffskyfileMode);
    8080        MODECASE(DIFFTOOL_MODE_DEFINEPOPRUN,          definepoprunMode);
    81         MODECASE(DIFFTOOL_MODE_DEFINEBYQUERY,         definebyqueryMode);
     81        MODECASE(DIFFTOOL_MODE_DEFINEWARPSTACK,         definewarpstackMode);
    8282        MODECASE(DIFFTOOL_MODE_DEFINEWARPWARP,        definewarpwarpMode);
    8383        MODECASE(DIFFTOOL_MODE_PENDINGCLEANUPRUN,     pendingcleanuprunMode);
     
    116116    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
    117117    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, false);
     118    PXOPT_LOOKUP_BOOL(bothways, config->args, "-bothways", false);
     119    PXOPT_LOOKUP_BOOL(exposure, config->args, "-exposure", false);
    118120    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    119121    PXOPT_LOOKUP_STR(reduction, config->args, "-reduction", false, false);
    120     PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", false, false);
    121122
    122123    // default
     
    133134            registered,
    134135            tess_id,
    135             exp_id,
     136            bothways,
     137            exposure,
    136138            false
    137139    );
    138140    if (!run) {
    139141        psError(PS_ERR_UNKNOWN, false, "failed to alloc diffRun object");
    140         return true;
     142        return false;
    141143    }
    142144    if (!diffRunInsertObject(config->dbh, run)) {
    143145        psError(PS_ERR_UNKNOWN, false, "database error");
    144146        psFree(run);
    145         return true;
     147        return false;
    146148    }
    147149
     
    150152
    151153    if (!diffRunPrintObject(stdout, run, !simple)) {
    152             psError(PS_ERR_UNKNOWN, false, "failed to print object");
    153             psFree(run);
    154             return false;
     154        psError(PS_ERR_UNKNOWN, false, "failed to print object");
     155        psFree(run);
     156        return false;
    155157    }
    156158
     
    412414    psMetadata *where = psMetadataAlloc();
    413415    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diffRun.diff_id", "==");
    414     PXOPT_COPY_STR(config->args, where, "-label", "diffRun.label", "==");
     416    pxAddLabelSearchArgs (config, where, "-label", "diffRun.label", "==");
    415417
    416418    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    485487    PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false); // required
    486488    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
    487     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
    488     PXOPT_LOOKUP_STR(uri, config->args, "-uri", (code == 0), false);
    489     PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", (code == 0), false);
     489    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
     490    PXOPT_LOOKUP_S16(quality, config->args, "-quality", false, false);
     491    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", (fault == 0), false);
    490492
    491493    // optional
     
    506508    PXOPT_LOOKUP_F32(kernel_xy, config->args, "-kernel_xy", false, false);
    507509    PXOPT_LOOKUP_F32(kernel_yy, config->args, "-kernel_yy", false, false);
     510    PXOPT_LOOKUP_F32(deconv_max, config->args, "-deconv_max", false, false);
    508511    PXOPT_LOOKUP_S32(sources, config->args, "-sources", false, false);
    509512    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
     
    519522                           diff_id,
    520523                           skycell_id,
    521                            uri,
    522524                           path_base,
     525                           "full",
    523526                           bg,
    524527                           bg_stdev,
     
    533536                           kernel_xy,
    534537                           kernel_yy,
     538                           deconv_max,
    535539                           sources,
    536540                           dtime_diff,
     
    540544                           hostname,
    541545                           good_frac,
    542                            code,
     546                           fault,
     547                           quality,
    543548                           magicked
    544549          )) {
     
    578583    PXOPT_COPY_S64(config->args, where,  "-diff_skyfile_id", "diffInputSkyfile.diff_skyfile_id", "==");
    579584    PXOPT_COPY_STR(config->args, where, "-tess_id", "diffSkyfile.tess_id", "==");
    580     PXOPT_COPY_S16(config->args, where, "-code", "diffSkyfile.fault", "==");
     585    PXOPT_COPY_S16(config->args, where, "-fault", "diffSkyfile.fault", "==");
    581586    PXOPT_COPY_S64(config->args, where, "-exp_id", "rawExp.exp_id", "==");
    582587    PXOPT_COPY_STR(config->args, where, "-exp_name", "rawExp.exp_name", "==");
     
    655660    PXOPT_COPY_S64(config->args, where,  "-diff_id", "diffSkyfile.diff_id", "==");
    656661    PXOPT_COPY_STR(config->args, where,  "-label", "diffRun.label", "==");
    657     PXOPT_COPY_S16(config->args, where, "-code",     "fault", "==");
     662    PXOPT_COPY_S16(config->args, where, "-fault",     "fault", "==");
    658663
    659664    if (!psDBTransaction(config->dbh)) {
     
    690695        psFree(query);
    691696
    692         psLogMsg("diftool", PS_LOG_INFO, "Updated %" PRIu64 " rows", psDBAffectedRows(config->dbh));
     697        psLogMsg("difftool", PS_LOG_INFO, "Updated %" PRIu64 " rows", psDBAffectedRows(config->dbh));
    693698    }
    694699
     
    766771                         psS64 template_warp_id, // Warp identifier for template image, PS_MAX_S64 for none
    767772                         psS64 template_stack_id, // Stack identifier for template image, PS_MAX_S64 for none
    768                          psS64 exp_id, // exposure id for input_warp_id (if defined)
    769773                         pxConfig *config // Configuration
    770774                         )
     
    797801            registered,
    798802            tess_id,
    799             exp_id,
     803            false,
     804            false,
    800805            false       // magicked
    801806    );
     
    877882    PXOPT_LOOKUP_S64(input_warp_id, config->args, "-input_warp_id", false, false);
    878883    PXOPT_LOOKUP_S64(input_stack_id, config->args, "-input_stack_id", false, false);
    879     PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", false, false);
    880884    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    881885
     
    897901        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    898902                "No input has been defined (-input_stack_id or -input_warp_id)");
    899         return false;
    900     }
    901     if (input_warp_id && !exp_id) {
    902         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    903                 "-exp_id is required with -input_warp_id.");
    904903        return false;
    905904    }
     
    912911                      template_warp_id ? template_warp_id : PS_MAX_S64,
    913912                      template_stack_id ? template_stack_id : PS_MAX_S64,
    914                       exp_id ? exp_id : PS_MAX_S64,
    915913                      config)) {
    916914        psError(PS_ERR_UNKNOWN, false, "failed to create populated diffRun");
     
    930928
    931929
    932 static bool definebyqueryMode(pxConfig *config)
     930static bool definewarpstackMode(pxConfig *config)
    933931{
    934932    PS_ASSERT_PTR_NON_NULL(config, false);
     
    955953    PXOPT_LOOKUP_BOOL(reRun, config->args, "-rerun", false);
    956954    PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
     955    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    957956
    958957    // find all things to queue
    959     psString query = pxDataGet("difftool_definebyquery_part1.sql");
     958    psString query = pxDataGet("difftool_definewarpstack_part1.sql");
    960959    if (!query) {
    961960        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    10521051    }
    10531052
     1053    if (pretend) {
     1054        // negative simple so the default is true
     1055        if (!ippdbPrintMetadatas(stdout, output, "diffRun", !simple)) {
     1056            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1057            psFree(output);
     1058            return false;
     1059        }
     1060        psFree(output);
     1061        return true;
     1062    }
     1063
    10541064    // create temporary table
    1055     query = pxDataGet("difftool_definebyquery_temp_create.sql");
     1065    query = pxDataGet("difftool_definewarpstack_temp_create.sql");
    10561066    if (!p_psDBRunQuery(config->dbh, query)) {
    10571067        psError(PS_ERR_UNKNOWN, false, "database error");
     
    10651075    query = NULL;
    10661076
    1067     psString skycell_query = pxDataGet("difftool_definebyquery_part2.sql");
     1077    psString skycell_query = pxDataGet("difftool_definewarpstack_part2.sql");
    10681078
    10691079    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
     
    11781188                registered,
    11791189                tess_id,
    1180                 exp_id,
     1190                false,                  // bothways
     1191                true,                   // exposure
    11811192                false       // magicked
    11821193        );
     
    12691280    PXOPT_COPY_S64(config->args, selectWhere, "-exp_id", "inputRawExp.exp_id", "==");
    12701281    PXOPT_COPY_STR(config->args, selectWhere, "-filter", "inputRawExp.filter", "==");
     1282    PXOPT_COPY_STR(config->args, selectWhere, "-obs_mode", "inputRawExp.obs_mode", "==");
     1283    PXOPT_COPY_STR(config->args, selectWhere, "-obs_mode", "templateRawExp.obs_mode", "==");
    12711284    PXOPT_COPY_STR(config->args, selectWhere, "-input_label", "inputWarpRun.label", "==");
    12721285    PXOPT_COPY_STR(config->args, selectWhere, "-template_label", "templateWarpRun.label", "==");
     1286    PXOPT_COPY_F32(config->args, selectWhere, "-rotdiff", "ABS(inputRawExp.posang - templateRawExp.posang)", "<=");
    12731287    PXOPT_COPY_F32(config->args, selectWhere, "-timediff",
    12741288                   "ABS(TIME_TO_SEC(TIMEDIFF(inputRawExp.dateobs, templateRawExp.dateobs)))", "<=");
    1275     PXOPT_COPY_F32(config->args, selectWhere, "-rotdiff", "ABS(inputRawExp.posang - templateRawExp.posang)", "<=");
     1289
     1290    // Haversine formula for great circle distance
     1291    PXOPT_COPY_F32(config->args, selectWhere, "-distance",
     1292                   "DEGREES(2*ASIN(SQRT(POW(SIN(inputRawExp.decl - templateRawExp.decl),2) + "
     1293                   "COS(inputRawExp.decl)*COS(templateRawExp.decl)*"
     1294                   "POW(SIN(inputRawExp.ra - templateRawExp.ra),2))))", "<=");
     1295
     1296    PXOPT_LOOKUP_BOOL(backwards, config->args, "-backwards", false);
     1297
     1298    psMetadataAddF32(selectWhere, PS_LIST_TAIL,
     1299                     "TIME_TO_SEC(TIMEDIFF(inputRawExp.dateobs, templateRawExp.dateobs))",
     1300                     PS_META_DUPLICATE_OK, backwards ? "<" : ">", 0.0);
    12761301
    12771302    // Restrictions for inserting skycells
     
    12891314    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
    12901315    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    1291 
    1292 
     1316    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     1317
     1318    if (!psDBTransaction(config->dbh)) {
     1319        psError(PS_ERR_UNKNOWN, false, "database error");
     1320        psFree(selectWhere);
     1321        psFree(insertWhere);
     1322        return false;
     1323    }
     1324
     1325    if (!rerun) {
     1326        // Need to build table of exposures with diffs
     1327        psString tempCreate = pxDataGet("difftool_definewarpwarp_temp_create.sql"); // Create temp table SQL
     1328        if (!tempCreate) {
     1329            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1330            psFree(selectWhere);
     1331            psFree(insertWhere);
     1332            if (!psDBRollback(config->dbh)) {
     1333                psError(PS_ERR_UNKNOWN, false, "database error");
     1334            }
     1335            return false;
     1336        }
     1337
     1338        if (!p_psDBRunQuery(config->dbh, tempCreate)) {
     1339            psError(PS_ERR_UNKNOWN, false, "Unable to create temp table: %s", tempCreate);
     1340            psFree(tempCreate);
     1341            psFree(selectWhere);
     1342            psFree(insertWhere);
     1343            if (!psDBRollback(config->dbh)) {
     1344                psError(PS_ERR_UNKNOWN, false, "database error");
     1345            }
     1346            return false;
     1347        }
     1348        psFree(tempCreate);
     1349
     1350        psString tempInsert = pxDataGet("difftool_definewarpwarp_temp_insert.sql"); // Insert to temp table
     1351        if (!tempInsert) {
     1352            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1353            psFree(selectWhere);
     1354            psFree(insertWhere);
     1355            if (!psDBRollback(config->dbh)) {
     1356                psError(PS_ERR_UNKNOWN, false, "database error");
     1357            }
     1358            return false;
     1359        }
     1360
     1361        psString where = psStringCopy(""); // WHERE for insertion
     1362        if (label) {
     1363            psStringAppend(&where, "\nAND diffRun.label = '%s'", label);
     1364        }
     1365
     1366        if (!p_psDBRunQueryF(config->dbh, tempInsert, where, where)) {
     1367            psError(PS_ERR_UNKNOWN, false, "Unable to insert into temp table: %s", tempInsert);
     1368            psFree(tempInsert);
     1369            psFree(where);
     1370            psFree(selectWhere);
     1371            psFree(insertWhere);
     1372            if (!psDBRollback(config->dbh)) {
     1373                psError(PS_ERR_UNKNOWN, false, "database error");
     1374            }
     1375            return false;
     1376        }
     1377        psFree(where);
     1378        psFree(tempInsert);
     1379    }
     1380
     1381    // Get list of warps to diff
    12931382    psString select = pxDataGet("difftool_definewarpwarp_select.sql");
    12941383    if (!select) {
    12951384        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1385        psFree(selectWhere);
     1386        psFree(insertWhere);
     1387        if (!psDBRollback(config->dbh)) {
     1388            psError(PS_ERR_UNKNOWN, false, "database error");
     1389        }
    12961390        return false;
    12971391    }
     
    13061400    psFree(selectWhere);
    13071401
     1402    if (!available) {
     1403        psStringAppend(&whereClause,
     1404                       "\n%s inputWarpRun.state = 'full'"
     1405                       "AND templateWarpRun.state = 'full'",
     1406                       whereClause ? "AND" : "WHERE");
     1407    }
     1408
    13081409    if (!rerun) {
    1309         psStringAppend(&whereClause, "\n%s diffRun.diff_id IS NULL", whereClause ? "AND" : "WHERE");
    1310     }
    1311 
    1312     if (!available) {
    1313         psStringAppend(&whereClause, "\n%s inputWarpRun.state = 'full'", whereClause ? "AND" : "WHERE");
    1314     }
    1315 
    1316     if (!psDBTransaction(config->dbh)) {
    1317         psError(PS_ERR_UNKNOWN, false, "database error");
    1318         psFree(select);
    1319         psFree(whereClause);
    1320         psFree(insertWhere);
    1321         return false;
     1410        psStringAppend(&whereClause, "\n%s diffs.diff_id IS NULL", whereClause ? "AND" : "WHERE");
    13221411    }
    13231412
    13241413    if (!p_psDBRunQueryF(config->dbh, select,
    1325                          !rerun ? "\n" : "", // Activate LEFT JOIN against diffRun?
     1414                         !rerun ? "\n" : "", // Activate LEFT JOIN against diffs?
    13261415                         whereClause)) {
    13271416        psError(PS_ERR_UNKNOWN, false, "Unable to run query: %s [WITH] %s", select, whereClause);
     
    13681457    }
    13691458
     1459    if (pretend) {
     1460        // negative simple so the default is true
     1461        if (!ippdbPrintMetadatas(stdout, results, "diffRun", !simple)) {
     1462            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1463            psFree(results);
     1464            return false;
     1465        }
     1466        psFree(results);
     1467        return true;
     1468    }
     1469
    13701470    psString insert = pxDataGet("difftool_definewarpwarp_insert.sql"); // Insertion for each new run
    13711471
    13721472    if (psListLength(insertWhere->list)) {
    13731473        psString whereClause = psDBGenerateWhereConditionSQL(insertWhere, NULL);
    1374         psStringAppend(&insert, "\n%s", whereClause);
     1474        psStringAppend(&insert, "\nAND %s", whereClause);
    13751475        psFree(whereClause);
    13761476    }
     
    13821482        psMetadata *row = results->data[i]; // Result row from query
    13831483
    1384         psS64 exp_id = psMetadataLookupS64(NULL, row, "input_exp_id");
    13851484        psS64 input_id = psMetadataLookupS64(NULL, row, "input_warp_id");
    1386         psS64 template_id = psMetadataLookupS64(NULL, row, "template_warp_id");
     1485        const char *template = psMetadataLookupStr(NULL, row, "template_warp_id");
    13871486        const char *tess_id = psMetadataLookupStr(NULL, row, "tess_id");
    1388         if (!exp_id || !input_id || !template_id || !tess_id) {
     1487        if (!input_id || !template || !tess_id) {
    13891488            psError(PXTOOLS_ERR_PROG, false, "Identifiers not found");
    13901489            psFree(list);
     
    13981497
    13991498        diffRunRow *run = diffRunRowAlloc(0, "reg", workdir, label, reduction, NULL, registered,
    1400                                           tess_id, exp_id, false); // Run to insert
     1499                                          tess_id, true, true, false); // Run to insert
    14011500        if (!diffRunInsertObject(config->dbh, run)) {
    14021501            psError(PS_ERR_UNKNOWN, false, "database error");
     
    14131512
    14141513        // Convert identifiers to string, for insertion into query
    1415         psString diff = NULL, input = NULL, template = NULL; // String versions of identifiers
     1514        psString diff = NULL, input = NULL; // String versions of identifiers
    14161515        psStringAppend(&diff, "%" PRId64, run->diff_id);
    14171516        psStringAppend(&input, "%" PRId64, input_id);
    1418         psStringAppend(&template, "%" PRId64, template_id);
    14191517
    14201518        if (!p_psDBRunQueryF(config->dbh, insert, diff, input, template, input, template)) {
    14211519            psError(PS_ERR_UNKNOWN, false, "database error");
    14221520            psFree(input);
    1423             psFree(template);
    14241521            psFree(diff);
    14251522            psFree(run);
     
    14331530        }
    14341531        psFree(input);
    1435         psFree(template);
    14361532        psFree(diff);
    14371533
     
    14821578
    14831579    psMetadata *where = psMetadataAlloc();
    1484     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     1580    pxAddLabelSearchArgs (config, where, "-label", "diffRun.label", "==");
    14851581
    14861582    psString query = pxDataGet("difftool_pendingcleanuprun.sql");
     
    15471643        PXOPT_COPY_S64(config->args, where, "-diff_id", "diff_id", "==");
    15481644    }
    1549     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     1645    pxAddLabelSearchArgs (config, where, "-label", "diffRun.label", "==");
    15501646
    15511647    psString query = pxDataGet("difftool_pendingcleanupskyfile.sql");
     
    16621758    PS_ASSERT_PTR_NON_NULL(config, false);
    16631759
    1664     PXOPT_LOOKUP_S16(code, config->args, "-code", true, false);
     1760    PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    16651761
    16661762    psMetadata *where = psMetadataAlloc();
    16671763    PXOPT_COPY_S64(config->args, where, "-diff_id",   "diff_id",   "==");
    16681764
    1669     if (!pxSetFaultCode(config->dbh, "diffSkyfile", where, code)) {
     1765    if (!pxSetFaultCode(config->dbh, "diffSkyfile", where, fault)) {
    16701766        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
    16711767        psFree (where);
     
    17371833  PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
    17381834  PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
     1835  PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    17391836
    17401837  FILE *f = fopen (outfile, "w");
     
    17821879    psArray *output = p_psDBFetchResult(config->dbh);
    17831880    if (!output) {
    1784       psError(PS_ERR_UNKNOWN, false, "database error");
    17851881      return false;
    17861882    }
    17871883    if (!psArrayLength(output)) {
    1788       psTrace("regtool", PS_LOG_INFO, "no rows found");
     1884      psError(PS_ERR_UNKNOWN, true, "no rows found");
    17891885      psFree(output);
    1790       return true;
     1886      return false;
     1887    }
     1888
     1889    if (clean) {
     1890        bool success = true;
     1891        if (!strcmp(tables[i].tableName, "diffRun")) {
     1892            success = pxSetStateCleaned("diffRun", "state", output);
     1893#ifdef notyet
     1894        // diffSkyfile doesn't have dataState yet
     1895        } else if (!strcmp(tables[i].tableName, "diffSkyfile")) {
     1896            success = pxSetStateCleaned("diffSkyfile", "data_state", output);
     1897#endif
     1898        }
     1899        if (!success) {
     1900            psFree(output);
     1901            psError(PS_ERR_UNKNOWN, false, "pxSetStateClean failed for table %s",  tables[i].tableName);
     1902            return false;
     1903        }
    17911904    }
    17921905
Note: See TracChangeset for help on using the changeset viewer.