IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 21, 2009, 12:57:48 PM (17 years ago)
Author:
bills
Message:

Added magicdstool -definebyquery and fixed a bug in magicdstool_torevert_diff.sql

File:
1 edited

Legend:

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

    r24709 r24882  
    22 * magicdstool.c
    33 *
    4  * Copyright (C) 2006-2007  IfA
     4 * Copyright (C) 2006-2009  IfA
    55 *
    66 * This program is free software; you can redistribute it and/or modify it
     
    2727#include <math.h>
    2828#include <ippdb.h>
     29#include <ippStages.h>
    2930
    3031#include "pxtools.h"
     
    9798static bool definebyqueryMode(pxConfig *config)
    9899{
    99     PS_ASSERT_PTR_NON_NULL(config, false);
    100     psError(PS_ERR_UNKNOWN, true, "definebyquery not implelmented yet");
    101 
    102     return false;
    103 #ifdef notyet
    104100
    105101    // Required
     102    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     103
     104    // Optional
    106105    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false);
    107 
    108     // Optional
    109     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    110     PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
    111     PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
     106    PXOPT_LOOKUP_STR(recoveryroot, config->args, "-recoveryroot", false, false);
     107    PXOPT_LOOKUP_BOOL(re_place, config->args, "-replace", false);
     108    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     109    PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false);
     110    PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);
    112111    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    113 
    114     // Create temporary table of the best diffs
    115     {
    116         psString query = pxDataGet("magictool_definebyquery_temp_create.sql");
    117         if (!query) {
    118             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    119             return false;
    120         }
    121 
    122         if (!p_psDBRunQuery(config->dbh, query)) {
    123             psError(PS_ERR_UNKNOWN, false, "database error");
    124             return false;
    125         }
     112    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     113
     114    // search args
     115    psMetadata *where = psMetadataAlloc();
     116    PXOPT_COPY_S64(config->args, where, "-exp_id",  "exp_id", "==");
     117    PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
     118    PXOPT_COPY_S64(config->args, where, "-cam_id",  "cam_id", "==");
     119    PXOPT_COPY_S64(config->args, where, "-warp_id", "warp_id", "==");
     120    PXOPT_COPY_S64(config->args, where, "-diff_id", "diff_id", "==");
     121    PXOPT_COPY_S64(config->args, where, "-magic_id","magic_id", "==");
     122
     123    pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
     124
     125    ippStage stageNum = ippStringToStage(stage);
     126   
     127    psString query = NULL;
     128    switch (stageNum) {
     129    case IPP_STAGE_RAW:
     130        query = pxDataGet("magicdstool_definebyquery_raw.sql");
     131        break;
     132    case IPP_STAGE_CHIP:
     133        query = pxDataGet("magicdstool_definebyquery_chip.sql");
     134        break;
     135    case IPP_STAGE_CAMERA:
     136        query = pxDataGet("magicdstool_definebyquery_camera.sql");
     137        break;
     138    case IPP_STAGE_WARP:
     139        query = pxDataGet("magicdstool_definebyquery_warp.sql");
     140        break;
     141    case IPP_STAGE_DIFF:
     142        query = pxDataGet("magicdstool_definebyquery_diff.sql");
     143        break;
     144    case IPP_STAGE_FAKE:
     145    case IPP_STAGE_STACK:
     146        psError(PXTOOLS_ERR_DATA, true, "%sRuns do not need to be destreaked", stage);
     147        return false;
     148    case IPP_STAGE_NONE:
     149        psError(PXTOOLS_ERR_DATA, true, "%s is not a valid stage", stage);
     150        return false;
     151    default:
     152        psError(PXTOOLS_ERR_PROG, true, "ippStageToString returned %d for invalid stage %s",
     153            stageNum, stage);
     154        return false;
     155    }
     156
     157    if (!query) {
     158        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     159        return false;
     160    }
     161
     162    if (psListLength(where->list)) {
     163        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     164        psStringAppend(&query, "\n AND %s", whereClause);
     165        psFree(whereClause);
     166    }
     167    psFree(where);
     168
     169    dry_run = true;
     170
     171    // treat limit == 0 as "no limit"
     172    if (limit) {
     173        psString limitString = psDBGenerateLimitSQL(limit);
     174        psStringAppend(&query, " %s", limitString);
     175        psFree(limitString);
     176    }
     177
     178    if (!p_psDBRunQueryF(config->dbh, query, rerun ? "\n1 " : "\n0 ")) {
     179        psError(PS_ERR_UNKNOWN, false, "database error");
    126180        psFree(query);
    127     }
    128 
    129     // Insert list of best diffs into temporary table
    130     {
    131         psString query = pxDataGet("magictool_definebyquery_temp_insert.sql");
    132         if (!query) {
    133             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    134             return false;
    135         }
    136 
    137         psMetadata *where = psMetadataAlloc();
    138         PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
    139         PXOPT_COPY_STR(config->args, where, "-diff_label", "diffRun.label", "==");
    140         PXOPT_COPY_F32(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">=");
    141 
    142         psString whereClause = NULL;    // WHERE conditions
    143         if (psListLength(where->list)) {
    144             whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    145             psStringPrepend(&whereClause, "\n AND ");
    146         }
    147         psFree(where);
    148 
    149         if (!p_psDBRunQueryF(config->dbh, query, whereClause)) {
    150             psError(PS_ERR_UNKNOWN, false, "database error");
    151             psFree(whereClause);
    152             psFree(query);
    153             return false;
    154         }
    155         psFree(whereClause);
    156         psFree(query);
    157     }
    158 
    159     // Get list of exposures ready to magic
    160     {
    161         psString query = pxDataGet("magictool_definebyquery_select.sql");
    162         if (!query) {
    163             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    164             return false;
    165         }
    166 
    167         psString magicSkyCellNumsWhere = NULL;    // WHERE conditions for magicSkyCellNums
    168         {
    169             psMetadata *where = psMetadataAlloc();
    170             PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
    171             PXOPT_COPY_STR(config->args, where, "-diff_label", "diffRun.label", "==");
    172             PXOPT_COPY_F32(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">=");
    173 
    174             if (psListLength(where->list)) {
    175                 magicSkyCellNumsWhere = psDBGenerateWhereConditionSQL(where, NULL);
    176                 psStringPrepend(&magicSkyCellNumsWhere, "\n AND ");
    177             }
    178             psFree(where);
    179         }
    180 
    181         // "available" means only concern ourselves with exposures that have all diffs completed, unless we're
    182         // told to only take what's available.
    183         // "new" means we want a new run even if there's already a magic run defined
    184         PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
    185         PXOPT_LOOKUP_BOOL(new, config->args, "-new", false);
    186 
    187         psString queryWhere = NULL;     // WHERE conditions for entire query
    188         if (available) {
    189             psStringAppend(&queryWhere, " WHERE num_done = num_todo");
    190         }
    191         if (new) {
    192             const char *newWhere = " magic_id IS NULL"; // String to add
    193             if (queryWhere) {
    194                 psStringAppend(&queryWhere, " AND %s", newWhere);
    195             } else {
    196                 psStringAppend(&queryWhere, " WHERE %s", newWhere);
    197             }
    198         }
    199         if (queryWhere) {
    200             psStringAppend(&query, " %s", queryWhere);
    201             psFree(queryWhere);
    202         }
    203 
    204 
    205         if (!p_psDBRunQueryF(config->dbh, query, magicSkyCellNumsWhere ? magicSkyCellNumsWhere : "")) {
    206             psError(PS_ERR_UNKNOWN, false, "database error");
    207             psFree(magicSkyCellNumsWhere);
    208             psFree(query);
    209             return false;
    210         }
    211         psFree(magicSkyCellNumsWhere);
    212         psFree(query);
    213     }
     181        return false;
     182    }
     183    psFree(query);
    214184
    215185    psArray *output = p_psDBFetchResult(config->dbh);
     
    233203    }
    234204
    235     // Parse the list of exposures ready to magic
    236 
    237     if (!psDBTransaction(config->dbh)) {
    238         psError(PS_ERR_UNKNOWN, false, "database error");
    239         return false;
    240     }
    241 
    242     psString insert = pxDataGet("magictool_definebyquery_insert.sql"); // Insert query
     205    // Parse the list of runs ready to be destreaked
     206
     207    if (!dry_run && !psDBTransaction(config->dbh)) {
     208        psError(PS_ERR_UNKNOWN, false, "database error");
     209        return false;
     210    }
    243211
    244212    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    245213    for (long i = 0; i < psArrayLength(output); i++) {
    246214        psMetadata *row = output->data[i]; // Row of interest
    247         psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id"); // Exposure identifier
     215        psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     216        psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id");
     217        psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
     218        psS64 cam_id = psMetadataLookupS64(NULL, row, "cam_id");
     219        psString magicRunLabel = psMetadataLookupStr(NULL, row, "label");
     220        psString magicRunWorkdir = psMetadataLookupStr(NULL, row, "workdir");
     221       
     222        // if workdir is not supplied use the magicRun's
     223        if (!workdir) {
     224            workdir = magicRunWorkdir;
     225        }
     226
     227        psString outroot = NULL;
     228        // set outroot to workdir/exp_id/stage for example /somewhere/424242/chip
     229        psStringAppend(&outroot, "%s/%" PRId64 "/%s", workdir, exp_id, stage);
    248230
    249231        // create a new magicRun for this group
    250         magicRunRow *run = magicRunRowAlloc(0, exp_id, "new", workdir, "dirty", label, dvodb, registered, 0);
     232        magicDSRunRow *run = magicDSRunRowAlloc(
     233                0, // magic_ds_id
     234                magic_id,
     235                "new",
     236                stage,
     237                stage_id,
     238                cam_id,
     239                set_label ? set_label : magicRunLabel,
     240                outroot,
     241                recoveryroot,
     242                re_place,
     243                0); // remove
     244
     245        psFree(outroot);
    251246        if (!run) {
    252             psAbort("failed to alloc magicRun object");
    253         }
    254 
    255         if (!magicRunInsertObject(config->dbh, run)) {
    256             psError(PS_ERR_UNKNOWN, false, "database error");
    257             psFree(run);
    258             psFree(insert);
    259             psFree(output);
    260             psFree(list);
    261             if (!psDBRollback(config->dbh)) {
     247            psAbort("failed to alloc magicDSRun object");
     248        }
     249
     250        if (!dry_run) {
     251            if (!magicDSRunInsertObject(config->dbh, run)) {
    262252                psError(PS_ERR_UNKNOWN, false, "database error");
     253                psFree(run);
     254                psFree(output);
     255                psFree(list);
     256                if (!psDBRollback(config->dbh)) {
     257                    psError(PS_ERR_UNKNOWN, false, "database error");
     258                }
     259                return false;
     260                psS64 magic_ds_id = psDBLastInsertID(config->dbh); // Assigned identifier
     261                run->magic_ds_id = magic_ds_id;
    263262            }
    264             return false;
    265         }
    266 
    267         psS64 magic_id = psDBLastInsertID(config->dbh); // Assigned identifier
    268         run->magic_id = magic_id;
     263        }
    269264
    270265        psArrayAdd(list, list->n, run);
    271266        psFree(run);
    272 
    273         // Create a suitable insertion query for this run
    274         psString thisInsert = psStringCopy(insert);
    275         {
    276             psString idString = NULL;
    277             psStringAppend(&idString, "%" PRId64, magic_id);
    278             psStringSubstitute(&thisInsert, idString, "@MAGIC_ID@");
    279             psFree(idString);
    280         }
    281         {
    282             psString idString = NULL;
    283             psStringAppend(&idString, "%" PRId64, exp_id);
    284             psStringSubstitute(&thisInsert, idString, "@EXP_ID@");
    285             psFree(idString);
    286         }
    287 
    288         if (!p_psDBRunQueryF(config->dbh, thisInsert, magic_id, exp_id)) {
    289             psError(PS_ERR_UNKNOWN, false, "database error");
    290             psFree(thisInsert);
    291             psFree(insert);
    292             psFree(output);
    293             psFree(list);
    294             if (!psDBRollback(config->dbh)) {
    295                 psError(PS_ERR_UNKNOWN, false, "database error");
    296             }
    297             return false;
    298         }
    299         psFree(thisInsert);
    300     }
    301 
    302     if (!psDBCommit(config->dbh)) {
     267    }
     268
     269    if (!dry_run && !psDBCommit(config->dbh)) {
    303270        psError(PS_ERR_UNKNOWN, false, "database error");
    304271        return false;
     
    306273    psFree(output);
    307274
    308     if (!magicRunPrintObjects(stdout, list, !simple)) {
     275    if (!magicDSRunPrintObjects(stdout, list, !simple)) {
    309276        psError(PS_ERR_UNKNOWN, false, "failed to print object");
    310277        psFree(list);
     
    315282
    316283    return true;
    317 #endif // notyet
    318284}
    319285
     
    405371    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
    406372    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     373    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    407374
    408375    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    501468    psMetadata *row = output->data[0];
    502469
    503     psString stage = psMetadataLookupStr(NULL, row, "stage");
     470    psString stage= psMetadataLookupStr(NULL, row, "stage");
    504471    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
    505472
    506     if (!strcmp(stage, "camera")) {
    507         // no there is no magicked column in camProcessedExp
     473    ippStage stageNum = ippStringToStage(stage);
     474
     475    // chose the appropriate query based on the stage
     476    switch (stageNum) {
     477    case IPP_STAGE_RAW:
     478        query = "UPDATE rawImfile SET magicked = %" PRId64 " where exp_id = %" PRId64 " AND class_id = '%s'";
     479        break;
     480    case IPP_STAGE_CHIP:
     481        query = "UPDATE chipProcessedImfile SET magicked = %" PRId64 " where chip_id = %" PRId64 " AND class_id = '%s'";
     482        break;
     483    case IPP_STAGE_CAMERA:
     484        // no there is no magicked column in camProcessedExp so we have nothing to do
    508485        psFree(output);
    509486        return true;
    510     }
    511 
    512     // chose the appropriate query based on the stage
    513     if (!strcmp(stage, "raw")) {
    514         query = "UPDATE rawImfile SET magicked = %" PRId64 " where exp_id = %" PRId64 " AND class_id = '%s'";
    515     } else if (!strcmp(stage, "chip")) {
    516         query = "UPDATE chipProcessedImfile SET magicked = %" PRId64 " where chip_id = %" PRId64 " AND class_id = '%s'";
    517     } else if (!strcmp(stage, "warp")) {
     487    case IPP_STAGE_WARP:
    518488        query = "UPDATE warpSkyfile SET magicked = %" PRId64 " where warp_id = %" PRId64 " AND skycell_id = '%s'";
    519     } else if (!strcmp(stage, "diff")) {
     489        break;
     490    case IPP_STAGE_DIFF:
    520491        query = "UPDATE diffSkyfile SET magicked = %" PRId64 " where diff_id = %" PRId64 " AND skycell_id = '%s'";
    521     } else {
     492        break;
     493    default:
    522494        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
    523495        psFree(output);
    524496        return false;
    525497    }
     498
    526499    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id, stage_id, component)) {
    527500        psError(PS_ERR_UNKNOWN, false, "database error");
     
    571544    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
    572545
     546    ippStage stageNum = ippStringToStage(stage);
    573547
    574548    // chose the appropriate query based on the stage
    575     if (!strcmp(stage, "raw")) {
     549    switch (stageNum) {
     550    case IPP_STAGE_RAW:
    576551        query = "UPDATE rawExp SET magicked = %" PRId64 " where exp_id = %" PRId64;
    577     } else if (!strcmp(stage, "chip")) {
     552        break;
     553    case IPP_STAGE_CHIP:
    578554        query = "UPDATE chipRun SET magicked = %" PRId64 " where chip_id = %" PRId64;
    579     } else if (!strcmp(stage, "camera")) {
     555        break;
     556    case IPP_STAGE_CAMERA:
    580557        query = "UPDATE camRun SET magicked = %" PRId64 " where cam_id = %" PRId64;
    581     } else if (!strcmp(stage, "warp")) {
     558        break;
     559    case IPP_STAGE_WARP:
    582560        query = "UPDATE warpRun SET magicked = %" PRId64 " where warp_id = %" PRId64;
    583     } else if (!strcmp(stage, "diff")) {
     561        break;
     562    case IPP_STAGE_DIFF:
    584563        query = "UPDATE diffRun SET magicked = %" PRId64 " where diff_id = %" PRId64;
    585     } else {
     564        break;
     565    default:
    586566        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
    587567        psFree(output);
     
    671651    PS_ASSERT_PTR_NON_NULL(cam_id, false);
    672652
    673     if (!strcmp(stage, "diff")) {
    674         // don't need these ids for diff stage because diff_id is in the magicRun
    675         *stage_id = 0;
    676         *cam_id = 0;
    677         return true;
    678     }
    679 
    680     int stageNum;
    681     if (!strcmp(stage, "raw")) {
    682         stageNum = 0;
    683     } else if (!strcmp(stage, "chip")) {
    684         stageNum = 1;
    685     } else if (!strcmp(stage, "camera")) {
    686         stageNum = 2;
    687     } else if (!strcmp(stage, "warp")) {
    688         stageNum = 3;
    689     } else {
    690         psError(PXTOOLS_ERR_DATA, true, "%s is not a valid value for stage", stage);
    691         return false;
    692     }
    693 
     653    int stageNum = ippStringToStage(stage);;
     654    if (stageNum == IPP_STAGE_NONE) {
     655        psError(PXTOOLS_ERR_DATA, false, "%s is not a valid value for stage", stage);
     656        return false;
     657    }
    694658
    695659    psString query = pxDataGet("magicdstool_getrunids.sql");
     
    724688
    725689    *cam_id = psMetadataLookupS64(NULL, row, "cam_id");
    726     if (stageNum == 0) {
     690    switch (stageNum) {
     691    case IPP_STAGE_RAW:
    727692        *stage_id = psMetadataLookupS64(NULL, row, "exp_id");
    728     } else if (stageNum == 1) {
     693        break;
     694    case IPP_STAGE_CHIP:
    729695        *stage_id = psMetadataLookupS64(NULL, row, "chip_id");
    730     } else if (stageNum == 2) {
     696        break;
     697    case IPP_STAGE_CAMERA:
    731698        *stage_id = *cam_id;
    732     } else if (stageNum == 3) {
     699        return true;
     700    case IPP_STAGE_WARP:
    733701        *stage_id = psMetadataLookupS64(NULL, row, "warp_id");
     702        break;
     703    case IPP_STAGE_DIFF:
     704        *stage_id = psMetadataLookupS64(NULL, row, "diff_id");
     705        break;
    734706    }
    735707
Note: See TracChangeset for help on using the changeset viewer.