IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 19, 2024, 8:12:15 AM (2 years ago)
Author:
eugene
Message:

working on xcfftool

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20230313/ippTools/src/xcfftool.c

    r42594 r42596  
    3333
    3434static bool definebyqueryMode(pxConfig *config);
    35 static bool defineforstacksMode(pxConfig *config);
    3635static bool updaterunMode(pxConfig *config);
    3736static bool todoMode(pxConfig *config);
     
    4847static bool importrunMode(pxConfig *config);
    4948
    50 static bool setxcForceRunState(pxConfig *config, psS64 sky_id, const char *state);
    51 static psS64 getCameraIDfromName(pxConfig *config, const char *camera);
     49static bool setxcForceRunState(pxConfig *config, psS64 xcff_id, const char *state);
     50static psS64 getCameraIDfromName(pxConfig *config, const char *dbname, const char *camera);
    5251
    5352# define MODECASE(caseName, func) \
     
    7069    switch (config->mode) {
    7170        MODECASE(XCFFTOOL_MODE_DEFINEBYQUERY,     definebyqueryMode);
    72         MODECASE(XCFFTOOL_MODE_DEFINEFORSTACKS,   defineforstacksMode);
    7371        MODECASE(XCFFTOOL_MODE_UPDATERUN,         updaterunMode);
    7472        MODECASE(XCFFTOOL_MODE_TODO,              todoMode);
     
    110108{
    111109    PS_ASSERT_PTR_NON_NULL(config, false);
     110
     111    // I need the name of the current working database
     112    psMetadataItem *dbnameMD = pmConfigUserSite(config->modules, "DBNAME", PS_DATA_STRING);
     113    char *dbname = dbnameMD->data.str;
     114
     115    // required:
    112116    PXOPT_LOOKUP_STR(workdir,     config->args, "-set_workdir", true, false);
    113117    PXOPT_LOOKUP_STR(label,       config->args, "-set_label", true, false);
    114118
     119    // we have two relevant databases: the current working database (e.g., gpc1) and the cross-camera
     120    // database used to generate the cross-camera stacks (e.g., xcamera).  the cross-camera database
     121    // name is supplied with -xcamera as an argument.  the current working database is part of the
     122    // database config info
     123
    115124    // The xcamera must be supplied as a string, then the camera_id is selected
    116     // from the table xcstackCamera.
     125    // from the table xcstackCamera. 
    117126    PXOPT_LOOKUP_STR(xcamera, config->args, "-xcamera", true, false);
    118127
    119     // get camera_id for the working database:
    120     // XXX need to extract the camera name (-dbname) from the config
    121     psS64 camera_id = getCameraIDfromName(config, config->dbh->camera);
     128    // get camera_id for the working database from the xcamera database
     129    psS64 camera_id = getCameraIDfromName(config, xcamera, dbname);
    122130    if (!camera_id) {
    123       psError(PXTOOLS_ERR_SYS, false, "failed to find xcamera in database");
    124       return false;
     131        psError(PXTOOLS_ERR_SYS, false, "failed to find camera %s in database", dbname);
     132        return false;
     133    }
     134    // get xcamera_id for the cross-camera database from the working database
     135    psS64 xcamera_id = getCameraIDfromName(config, dbname, xcamera);
     136    if (!xcamera_id) {
     137        psError(PXTOOLS_ERR_SYS, false, "failed to find camera %s in database", xcamera);
     138        return false;
    125139    }
    126140
     
    131145    PXOPT_LOOKUP_STR(reduction,   config->args, "-set_reduction", false, false);
    132146
    133     // XXX is needed or wanted?
    134     // PXOPT_LOOKUP_STR(sources_path_base,   config->args, "-set_sources_path_base", false, false);
    135 
    136147    // first, we need to select the xccalRun(s) which need to have xcForceRuns.  we can limit the selection based on
    137148    // qualities of the xccalRun or the xcstackRun.  There will be one xcForceRun for each
    138     // identified xccalRun.
     149    // identified xccalRun (there may be multiple xcForceRuns with different labels)
    139150
    140151    psMetadata *xccalWhereMD = psMetadataAlloc();
     
    149160    }
    150161
    151     PXOPT_COPY_STR(config->args, xccalWhereMD, "-select_skycell_id",    "xcstackRun.skycell_id",      "LIKE");
    152     PXOPT_COPY_STR(config->args, xccalWhereMD, "-select_tess_id",       "xcstackRun.tess_id",         "==");
    153     pxAddLabelSearchArgs(config, xccalWhereMD, "-select_filter",        "xcstackRun.filter",          "LIKE");
     162    // the select query does not join to xcstackRun, but it does join to stackRun
     163    PXOPT_COPY_STR(config->args, xccalWhereMD, "-select_skycell_id",    "stackRun.skycell_id",      "LIKE");
     164    PXOPT_COPY_STR(config->args, xccalWhereMD, "-select_tess_id",       "stackRun.tess_id",         "==");
     165    pxAddLabelSearchArgs(config, xccalWhereMD, "-select_filter",        "stackRun.filter",          "LIKE");
    154166    if (!pxskycellAddWhere(config, xccalWhereMD)) {
    155167        psError(PXTOOLS_ERR_CONFIG, false, "failed to add skycell search arguments");
     
    167179    pxAddLabelSearchArgs(config, warpWhereMD, "-select_warp_data_group", "warpRun.data_group",      "LIKE");
    168180    PXOPT_COPY_S64(config->args, warpWhereMD, "-select_warp_id",         "warpRun.warp_id",         "==");
     181    PXOPT_COPY_F32(config->args, warpWhereMD, "-select_good_frac_min",   "warpSkyfile.good_frac",  ">=");
    169182    // XXX is is really necessary to limit the warps?
    170183    if (!psListLength(warpWhereMD->list)) {
     
    174187        return false;
    175188    }
    176     PXOPT_COPY_F32(config->args, warpWhereMD, "-select_good_frac_min",   "warpSkyfile.good_frac",  ">=");
    177189
    178190    PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false);
     
    180192    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    181193
    182     psString select = pxDataGet("xcfftool_definebyquery.sql");
    183     if (!select) {
     194    psString select = NULL;
     195
     196    psString query_pt1 = pxDataGet("xcfftool_definebyquery_pt1.sql");
     197    if (!query_pt1) {
    184198        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    185199        psFree(xccalWhereMD);
     
    188202    }
    189203
     204    // query_pt1 has 4 places where the xcamera database name must be supplied
     205    psStringAppend (&select, query_pt1, xcamera, xcamera, xcamera, xcamera);
     206    psFree (query_pt1);
     207
     208    if (!rerun) {
     209        psStringAppend(&select, "LEFT JOIN xcForceRun ON (xcForceRun.xccal_id = %s.xccalRun.xccal_id) AND (xcForceRun.label = '%s')\n", xcamera, label);
     210    }
     211
     212    psString query_pt2 = pxDataGet("xcfftool_definebyquery_pt2.sql");
     213    if (!query_pt2) {
     214        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     215        psFree(xccalWhereMD);
     216        psFree(warpWhereMD);
     217        psFree(select);
     218        return false;
     219    }
     220
     221    // query_pt2 has 3 places where the xcamera database name must be supplied and 1 for this camera
     222    psStringAppend (&select, query_pt2, xcamera, (long long) camera_id, xcamera, xcamera);
     223    psFree (query_pt2);
     224
    190225    psString where = NULL;
    191226    psString whereClause = psDBGenerateWhereConditionSQL(xccalWhereMD, NULL);
    192     psStringAppend(&where, "\nAND %s", whereClause);
    193     psStringAppend(&select, " %s", where);
     227    psStringAppend(&select, "\n AND %s", whereClause);
    194228
    195229    psFree(whereClause);
    196230    psFree(xccalWhereMD);
    197231
    198     psString joinHook = NULL;
    199     if (!rerun) {
    200       psStringAppend(&joinHook, "\nLEFT JOIN xcForceRun ON xcForceRun.xccal_id = %s.xccalRun.xccal_id", xcamera);
    201     }
    202 
    203     psStringAppend(&joinHook, "\n %s, where);
    204 
    205     if (
    206         psStringAppend(&joinHook, "\n %s\nAND xcForceRun.label = '%s'", where, label);
    207         psStringAppend(&select, "\nAND ff_id IS NULL");
    208     }
    209 
    210     // XXX need to add the xcamera name here to match definebyquery.sql
    211     if (!p_psDBRunQueryF(config->dbh, select, joinHook)) {
     232    // accept only the entries without a match in the target label
     233    if (!rerun) psStringAppend(&select, "\n AND xcff_id IS NULL\n");
     234
     235    if (!p_psDBRunQuery(config->dbh, select)) {
    212236        psError(PS_ERR_UNKNOWN, false, "database error");
    213237        psFree(select);
     
    215239    }
    216240    psFree(select);
    217     psFree(joinHook);
    218241
    219242    psArray *output = p_psDBFetchResult(config->dbh);
     
    251274    }
    252275
     276    // this query selects all of the warps where were inputs to the given stackRun,
     277    // with restrictions based on (quality, fault, warpRun.state, and other supplied conditions)
     278    // there is no limit to the version of the warp analyss, to
    253279    psString warpQueryTemplate = pxDataGet("xcfftool_definebyquery_select_warps.sql");
    254280
    255281    whereClause = psDBGenerateWhereConditionSQL(warpWhereMD, NULL);
    256     psStringAppend(&warpQueryTemplate, "\nAND %s", whereClause);
     282    psStringAppend(&warpQueryTemplate, "\n AND %s", whereClause);
    257283
    258284    for (long i = 0; i < output->n; i++) {
     
    268294        // psS64 warp_id = psMetadataLookupS64(&status, row, "warp_id");
    269295        psS64 xccal_id = psMetadataLookupS64(&status, row, "xccal_id");
    270 
    271         psString path_base = NULL;
    272         if (sources_path_base) {
    273             path_base = sources_path_base;
    274         } else {
    275             path_base = psMetadataLookupStr(&status, row, "path_base");
    276             psAssert(status, "failed to find xccal path_base?");
    277         }
    278 
     296        psS64 stack_id = psMetadataLookupS64(&status, row, "stack_id");
     297        psString path_base = psMetadataLookupStr(&status, row, "path_base");
    279298        psString xccal_data_group = psMetadataLookupStr(&status, row, "data_group");
    280299        psString tess_id = psMetadataLookupStr(&status, row, "tess_id");
    281300        psString skycell_id = psMetadataLookupStr(&status, row, "skycell_id");
    282         psString filter = psMetadataLookupStr(&status, row, "filter");
    283301
    284302        psString query = NULL;
    285         psStringAppend(&query, warpQueryTemplate, tess_id, skycell_id, filter);
     303        psStringAppend(&query, warpQueryTemplate, (long long) stack_id);
    286304
    287305        if (!p_psDBRunQuery(config->dbh, query)) {
     
    315333        // create an xcForceRun
    316334        if (!xcForceRunInsert(config->dbh,
    317                                 0x0,         // ff_id
    318                                 xccal_id,
    319                                 path_base,
    320                                 "new",       // state
    321                                 workdir,
    322                                 label,
    323                                 data_group ? data_group : (xccal_data_group ? xccal_data_group : label),
    324                                 dist_group,
    325                                 note,
    326                                 reduction,
    327                                 NULL        // registered
     335                              0x0,           // xcff_id
     336                              xccal_id,
     337                              path_base,
     338                              "new",         // state
     339                              workdir,
     340                              label,
     341                              skycell_id,
     342                              tess_id,
     343                              data_group ? data_group : (xccal_data_group ? xccal_data_group : label),
     344                              dist_group,
     345                              note,
     346                              reduction,
     347                              NULL        // registered
    328348                )
    329349            ) {
     
    342362            psS64 warp_id = psMetadataLookupS64(&status, warpRow, "warp_id");
    343363
    344             if (!xcForceInputInsert(config->dbh, ff_id, warp_id, xcamera_id)
    345                ) {
     364            if (!xcForceInputInsert(config->dbh, xcff_id, warp_id, xcamera_id)) {
    346365                if (!psDBRollback(config->dbh)) {
    347366                    psError(PS_ERR_UNKNOWN, false, "database error failed to rollback transaction");
     
    372391
    373392    psMetadata *where = psMetadataAlloc();
    374     PXOPT_COPY_S64(config->args, where, "-xcff_id",     "xcff_id",   "==");
    375     PXOPT_COPY_STR(config->args, where, "-label",       "xcForceRun.label",    "==");
    376     PXOPT_COPY_STR(config->args, where, "-data_group",  "xcForceRun.data_group",    "==");
    377     PXOPT_COPY_STR(config->args, where, "-state",       "xcForceRun.state",    "==");
    378     PXOPT_COPY_STR(config->args, where, "-skycell_id",  "stackRun.skycell_id",    "==");
     393    PXOPT_COPY_S64(config->args, where, "-xcff_id",     "xcff_id",    "==");
     394    PXOPT_COPY_STR(config->args, where, "-label",       "label",      "==");
     395    PXOPT_COPY_STR(config->args, where, "-state",       "state",      "==");
     396    PXOPT_COPY_STR(config->args, where, "-data_group",  "data_group", "==");
     397    PXOPT_COPY_STR(config->args, where, "-dist_group",  "dist_group", "==");
     398    PXOPT_COPY_STR(config->args, where, "-skycell_id",  "skycell_id", "==");
     399    PXOPT_COPY_STR(config->args, where, "-tess_id",     "tess_id",    "==");
    379400    if (!pxskycellAddWhere(config, where)) {
    380401        psError(PXTOOLS_ERR_CONFIG, false, "failed to add skycell search arguments");
     
    387408    }
    388409
    389     psString query = pxDataGet("xcfftool_updaterun.sql");
    390     if (!select) {
    391         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    392         return false;
    393     }
     410    psString query = psStringCopy ("UPDATE xcForceRun JOIN skycell USING(tess_id, skycell_id)");
    394411
    395412    // pxUpdateRun gets parameters from config->args and updates
    396     bool result = pxUpdateRun(config, where, &query, "xcForceRun", "ff_id", "xcForceResult", true, false);
     413    bool result = pxUpdateRun(config, where, &query, "xcForceRun", "xcff_id", "xcForceResult", true, false);
    397414    psFree(query);
    398415    psFree(where);
     
    407424
    408425    psMetadata *whereMD = psMetadataAlloc();
    409     PXOPT_COPY_S64(config->args, whereMD,  "-ff_id", "ff_id", "==");
    410     pxAddLabelSearchArgs (config, whereMD, "-label", "xcForceRun.label", "==");
     426    PXOPT_COPY_S64(config->args, whereMD,  "-xcff_id", "xcff_id", "==");
     427    pxAddLabelSearchArgs (config, whereMD, "-label",   "xcForceRun.label", "==");
    411428    pxskycellAddWhere(config, whereMD);
    412429
     
    482499    PS_ASSERT_PTR_NON_NULL(config, false);
    483500    // required
    484     PXOPT_LOOKUP_S64(ff_id, config->args, "-ff_id", true, false);
     501    PXOPT_LOOKUP_S64(xcff_id, config->args, "-xcff_id", true, false);
    485502    PXOPT_LOOKUP_S64(warp_id, config->args, "-warp_id", true, false);
    486503
     
    497514    // XXX not sure we need a transaction here...
    498515    if (!xcForceResultInsert(config->dbh,
    499                                ff_id,
     516                               xcff_id,
    500517                               warp_id,
    501518                               path_base,
     
    521538
    522539    psMetadata *where = psMetadataAlloc();
    523     PXOPT_COPY_S64(config->args, where, "-ff_id",      "xcForceRun.ff_id", "==");
     540    PXOPT_COPY_S64(config->args, where, "-xcff_id",    "xcForceRun.xcff_id", "==");
    524541    PXOPT_COPY_S64(config->args, where, "-warp_id",    "xcForceResult.warp_id", "==");
    525542    PXOPT_COPY_S64(config->args, where, "-exp_id",     "rawExp.exp_id", "==");
     
    608625
    609626    psMetadata *where = psMetadataAlloc();
    610     PXOPT_COPY_S64(config->args, where, "-ff_id", "xcForceRun.ff_id", "==");
     627    PXOPT_COPY_S64(config->args, where, "-xcff_id", "xcForceRun.ff_id", "==");
    611628    PXOPT_COPY_S64(config->args, where, "-warp_id", "xcForceResult.warp_id", "==");
    612629    pxAddLabelSearchArgs(config, where, "-label", "xcForceRun.label", "==");
     
    660677
    661678    psMetadata *where = psMetadataAlloc();
    662     PXOPT_COPY_S64(config->args, where, "-ff_id",   "ff_id",   "==");
     679    PXOPT_COPY_S64(config->args, where, "-xcff_id", "xcff_id",   "==");
    663680    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id",   "==");
    664681
     
    672689}
    673690
    674 static bool setxcForceRunState(pxConfig *config, psS64 ff_id, const char *state)
    675 {
    676     psString query = "UPDATE xcForceRun SET state = 'full' WHERE ff_id = %" PRId64;
    677     if (!p_psDBRunQueryF(config->dbh, query, ff_id)) {
    678         psError(PS_ERR_UNKNOWN, false, "database error");
    679         return false;
    680     }
    681 
    682     return true;
    683 }
    684 
    685691static bool toadvanceMode(pxConfig *config) {
    686692    PS_ASSERT_PTR_NON_NULL(config, false);
    687693
    688694    psMetadata *whereMD = psMetadataAlloc();
    689     PXOPT_COPY_S64(config->args, whereMD,  "-ff_id", "ff_id", "==");
     695    PXOPT_COPY_S64(config->args, whereMD,  "-xcff_id", "xcff_id", "==");
    690696    pxAddLabelSearchArgs (config, whereMD, "-label", "xcForceRun.label", "==");
    691697    pxskycellAddWhere(config, whereMD);
     
    765771    PS_ASSERT_PTR_NON_NULL(config, false);
    766772    // required
    767     PXOPT_LOOKUP_S64(ff_id, config->args, "-ff_id", true, false);
     773    PXOPT_LOOKUP_S64(xcff_id, config->args, "-xcff_id", true, false);
    768774
    769775    // optional
     
    783789
    784790    if (!xcForceSummaryInsert(config->dbh,
    785                                ff_id,
     791                               xcff_id,
    786792                               path_base,
    787793                               dtime_script,
     
    799805
    800806    if (!fault) {
    801         if (!setxcForceRunState(config, ff_id, "full")) {
     807        if (!setxcForceRunState(config, xcff_id, "full")) {
    802808            if (!psDBRollback(config->dbh)) {
    803809                psError(PS_ERR_UNKNOWN, false, "database error");
     
    817823}
    818824
    819 static bool revertsummaryMode(pxConfig *config) {
    820     PS_ASSERT_PTR_NON_NULL(config, false);
    821 
    822     psMetadata *where = psMetadataAlloc();
    823     PXOPT_COPY_S64(config->args, where, "-ff_id", "xcForceRun.ff_id", "==");
    824     pxAddLabelSearchArgs(config, where, "-label", "xcForceRun.label", "==");
    825     PXOPT_COPY_S16(config->args, where, "-fault", "xcForceSummary.fault", "==");
    826 
    827     if (!pxskycellAddWhere(config, where)) {
    828         psError(PXTOOLS_ERR_CONFIG, false, "failed to add skycell search arguments");
    829         return false;
    830     }
    831 
    832     if (!psListLength(where->list)) {
    833         psFree(where);
    834         psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    835         return false;
    836     }
    837 
    838     // Delete product
    839     psString delete = pxDataGet("xcfftool_revertsummary.sql");
    840     if (!delete) {
    841         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    842         return false;
    843     }
    844 
    845     if (psListLength(where->list)) {
    846         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    847         psStringAppend(&delete, " AND %s", whereClause);
    848         psFree(whereClause);
    849     }
    850 
    851     if (!p_psDBRunQuery(config->dbh, delete)) {
    852         psError(PS_ERR_UNKNOWN, false, "database error");
    853         psFree(delete);
    854         psFree(where);
    855         return false;
    856     }
    857     psFree(delete);
    858 
    859     int numRows = psDBAffectedRows(config->dbh); // Number of row affected
    860     psLogMsg("xcfftool", PS_LOG_INFO, "Deleted %d rows", numRows);
    861 
    862     psFree(where);
    863     return true;
    864 }
    865 static bool updatesummaryMode(pxConfig *config) {
    866     PS_ASSERT_PTR_NON_NULL(config, false);
    867 
    868     PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    869     PXOPT_LOOKUP_S16(quality, config->args, "-quality", false, false);
    870 
    871     psMetadata *where = psMetadataAlloc();
    872     PXOPT_COPY_S64(config->args, where, "-ff_id",   "ff_id",   "==");
    873 
    874 
    875     if (!pxSetFaultCode(config->dbh, "xcForceSummary", where, fault, quality)) {
    876         psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
    877         psFree (where);
    878         return false;
    879     }
    880     psFree (where);
    881     return true;
    882 }
    883825static bool summaryMode(pxConfig *config) {
    884826    PS_ASSERT_PTR_NON_NULL(config, false);
    885827
    886828    psMetadata *where = psMetadataAlloc();
    887     PXOPT_COPY_S64(config->args, where, "-ff_id",      "xcForceRun.ff_id", "==");
    888     PXOPT_COPY_S64(config->args, where, "-warp_id",    "xcForceInput.warp_id", "==");
    889     PXOPT_COPY_S64(config->args, where, "-exp_id",     "rawExp.exp_id", "==");
    890     PXOPT_COPY_STR(config->args, where, "-exp_name",   "rawExp.exp_name", "==");
    891     PXOPT_COPY_S64(config->args, where, "-xccal_id",  "xcForceRun.xccal_id", "==");
    892     PXOPT_COPY_S64(config->args, where, "-stack_id",   "stackRun.stack_id", "==");
     829    PXOPT_COPY_S64(config->args, where, "-xcff_id",    "xcForceRun.xcff_id", "==");
     830    PXOPT_COPY_S64(config->args, where, "-xccal_id",   "xcForceRun.xccal_id", "==");
    893831    PXOPT_COPY_STR(config->args, where, "-label",      "xcForceRun.label", "==");
    894832    PXOPT_COPY_STR(config->args, where, "-data_group", "xcForceRun.data_group", "LIKE");
    895     PXOPT_COPY_STR(config->args, where, "-tess_id",    "stackRun.tess_id", "LIKE");
    896     PXOPT_COPY_STR(config->args, where, "-skycell_id", "stackRun.skycell_id", "LIKE");
    897     PXOPT_COPY_STR(config->args, where, "-filter",     "stackRun.filter", "LIKE");
     833    PXOPT_COPY_STR(config->args, where, "-tess_id",    "xcForceRun.tess_id", "LIKE");
     834    PXOPT_COPY_STR(config->args, where, "-skycell_id", "xcForceRun.skycell_id", "LIKE");
    898835    PXOPT_COPY_S16(config->args, where, "-fault",      "xcForceSummary.fault", "==");
    899836    pxskycellAddWhere(config, where);
     
    966903}
    967904
     905static bool revertsummaryMode(pxConfig *config) {
     906    PS_ASSERT_PTR_NON_NULL(config, false);
     907
     908    psMetadata *where = psMetadataAlloc();
     909    PXOPT_COPY_S64(config->args, where, "-xcff_id", "xcForceRun.xcff_id", "==");
     910    pxAddLabelSearchArgs(config, where, "-label",   "xcForceRun.label", "==");
     911    PXOPT_COPY_S16(config->args, where, "-fault",   "xcForceSummary.fault", "==");
     912
     913    if (!pxskycellAddWhere(config, where)) {
     914        psError(PXTOOLS_ERR_CONFIG, false, "failed to add skycell search arguments");
     915        return false;
     916    }
     917
     918    if (!psListLength(where->list)) {
     919        psFree(where);
     920        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     921        return false;
     922    }
     923
     924    // Delete product
     925    psString delete = pxDataGet("xcfftool_revertsummary.sql");
     926    if (!delete) {
     927        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     928        return false;
     929    }
     930
     931    if (psListLength(where->list)) {
     932        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     933        psStringAppend(&delete, " AND %s", whereClause);
     934        psFree(whereClause);
     935    }
     936
     937    if (!p_psDBRunQuery(config->dbh, delete)) {
     938        psError(PS_ERR_UNKNOWN, false, "database error");
     939        psFree(delete);
     940        psFree(where);
     941        return false;
     942    }
     943    psFree(delete);
     944
     945    int numRows = psDBAffectedRows(config->dbh); // Number of row affected
     946    psLogMsg("xcfftool", PS_LOG_INFO, "Deleted %d rows", numRows);
     947
     948    psFree(where);
     949    return true;
     950}
     951
     952static bool updatesummaryMode(pxConfig *config) {
     953    PS_ASSERT_PTR_NON_NULL(config, false);
     954
     955    PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
     956    PXOPT_LOOKUP_S16(quality, config->args, "-quality", false, false);
     957
     958    psMetadata *where = psMetadataAlloc();
     959    PXOPT_COPY_S64(config->args, where, "-xcff_id",   "xcff_id",   "==");
     960
     961    if (!pxSetFaultCode(config->dbh, "xcForceSummary", where, fault, quality)) {
     962        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
     963        psFree (where);
     964        return false;
     965    }
     966    psFree (where);
     967    return true;
     968}
     969
    968970bool exportrunMode(pxConfig *config)
    969971{
    970   typedef struct ExportTable {
    971     char tableName[80];
    972     char sqlFilename[80];
    973   } ExportTable;
     972    typedef struct ExportTable {
     973        char tableName[80];
     974        char tableSQL[80];
     975    } ExportTable;
    974976
    975977    PS_ASSERT_PTR_NON_NULL(config, NULL);
    976978
    977     // PXOPT_LOOKUP_S64(det_id,  config->args, "-warp_id", true,  false);
    978979    PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
    979980    PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
    980 //    PXOPT_LOOKUP_BOOL(clean,  config->args, "-clean", false);
    981981
    982982    FILE *f = fopen (outfile, "w");
     
    994994
    995995    ExportTable tables [] = {
    996       {"xcForceRun", "xcfftool_export_run.sql"},
    997       {"xcForceInput", "xcfftool_export_input.sql"},
    998       {"xcForceResult", "xcfftool_export_result.sql"},
    999       {"xcForceSummary", "xcfftool_export_summary.sql"},
     996        {"xcForceRun",     "SELECT xcForceRun.*       FROM xcForceRun"},
     997        {"xcForceInput",   "SELECT xcForceInput.*     FROM xcForceInput"},
     998        {"xcForceResult",  "SELECT xcForceResult.*    FROM xcForceResult"},
     999        {"xcForceSummary", "SELECT xcForceSummary.*   FROM xcForceSummary"},
    10001000    };
    10011001
     
    10031003
    10041004    for (int i=0; i < numTables; i++) {
    1005       psString query = pxDataGet(tables[i].sqlFilename);
    1006       if (!query) {
    1007           psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    1008           return false;
    1009       }
    1010 
    1011       if (where && psListLength(where->list)) {
    1012           psString whereClause = psDBGenerateWhereSQL(where, NULL);
    1013           psStringAppend(&query, " %s", whereClause);
    1014           psFree(whereClause);
    1015       }
    1016 
    1017       // treat limit == 0 as "no limit"
    1018       if (limit) {
    1019         psString limitString = psDBGenerateLimitSQL(limit);
    1020         psStringAppend(&query, " %s", limitString);
    1021         psFree(limitString);
    1022       }
    1023 
    1024       if (!p_psDBRunQuery(config->dbh, query)) {
    1025         psError(PS_ERR_UNKNOWN, false, "database error");
    1026         psFree(query);
    1027         return false;
    1028       }
    1029       psFree(query);
    1030 
    1031       psArray *output = p_psDBFetchResult(config->dbh);
    1032       if (!output) {
    1033           psError(PS_ERR_UNKNOWN, false, "database error");
    1034           return false;
    1035       }
    1036       if (!psArrayLength(output)) {
    1037         psError(PS_ERR_UNKNOWN, true, "no rows found");
    1038         psFree(output);
    1039         return false;
    1040       }
    1041 
    1042       // we must write the export table in non-simple (true) format
    1043       if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
    1044         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    1045         psFree(output);
    1046         return false;
    1047       }
    1048       psFree(output);
     1005        psString query = psStringCopy(tables[i].tableSQL);
     1006
     1007        if (where && psListLength(where->list)) {
     1008            psString whereClause = psDBGenerateWhereSQL(where, NULL);
     1009            psStringAppend(&query, " %s", whereClause);
     1010            psFree(whereClause);
     1011        }
     1012
     1013        // treat limit == 0 as "no limit"
     1014        if (limit) {
     1015            psString limitString = psDBGenerateLimitSQL(limit);
     1016            psStringAppend(&query, " %s", limitString);
     1017            psFree(limitString);
     1018        }
     1019
     1020        if (!p_psDBRunQuery(config->dbh, query)) {
     1021            psError(PS_ERR_UNKNOWN, false, "database error");
     1022            psFree(query);
     1023            return false;
     1024        }
     1025        psFree(query);
     1026
     1027        psArray *output = p_psDBFetchResult(config->dbh);
     1028        if (!output) {
     1029            psError(PS_ERR_UNKNOWN, false, "database error");
     1030            return false;
     1031        }
     1032        if (!psArrayLength(output)) {
     1033            psError(PS_ERR_UNKNOWN, true, "no rows found");
     1034            psFree(output);
     1035            return false;
     1036        }
     1037
     1038        // we must write the export table in non-simple (true) format
     1039        if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
     1040            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1041            psFree(output);
     1042            return false;
     1043        }
     1044        psFree(output);
    10491045    }
    10501046
     
    10541050}
    10551051
     1052// XXX this is coded in a very strange way
    10561053bool importrunMode(pxConfig *config)
    10571054{
     
    10671064
    10681065  psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
    1069 
    1070 #ifdef notdef
    1071   fprintf (stderr, "---- input ----\n");
    1072   psMetadataPrint (stderr, input, 1);
    1073 #endif
    10741066
    10751067  if (!pxCheckImportVersion(config, input)) {
     
    11431135// We expect the camera to be defined, so an invalid name or a missing camera
    11441136// results in a 0 return value which should be treated as an error.
    1145 static psS64 getCameraIDfromName(pxConfig *config, const char *camera)
     1137static psS64 getCameraIDfromName(pxConfig *config, const char *dbname, const char *camera)
    11461138{
    11471139    PS_ASSERT_PTR_NON_NULL(camera, false);
    11481140
    1149     char *query = "SELECT camera_id FROM xcstackCamera WHERE camera = '%s'";
    1150     if (!p_psDBRunQueryF(config->dbh, query, camera)) {
    1151         psError(PS_ERR_UNKNOWN, false, "failed to select camera_id for camera %s", camera);
     1141    char *query = "SELECT camera_id FROM %s.xcstackCamera WHERE camera = '%s'";
     1142    if (!p_psDBRunQueryF(config->dbh, query, dbname, camera)) {
     1143        psError(PS_ERR_UNKNOWN, false, "failed to select camera_id for camera %s from database %s", camera, dbname);
    11521144        return 0;
    11531145    }
     
    11971189}
    11981190
    1199 
     1191static bool setxcForceRunState(pxConfig *config, psS64 xcff_id, const char *state)
     1192{
     1193    psString query = "UPDATE xcForceRun SET state = 'full' WHERE xcff_id = %" PRId64;
     1194    if (!p_psDBRunQueryF(config->dbh, query, xcff_id)) {
     1195        psError(PS_ERR_UNKNOWN, false, "database error");
     1196        return false;
     1197    }
     1198
     1199    return true;
     1200}
     1201
Note: See TracChangeset for help on using the changeset viewer.