IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 7, 2009, 12:45:24 PM (17 years ago)
Author:
eugene
Message:

update from mainline

Location:
branches/eam_branches/20090715
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090715

  • branches/eam_branches/20090715/ippTools/src/magicdstool.c

    r24709 r25022  
    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)) {
     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    PXOPT_COPY_S32(config->args, where, "-streaks_max","streaks", "<=");
     123
     124    pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
     125
     126    ippStage stageNum = ippStringToStage(stage);
     127   
     128    psString query = NULL;
     129    switch (stageNum) {
     130    case IPP_STAGE_RAW:
     131        query = pxDataGet("magicdstool_definebyquery_raw.sql");
     132        break;
     133    case IPP_STAGE_CHIP:
     134        query = pxDataGet("magicdstool_definebyquery_chip.sql");
     135        break;
     136    case IPP_STAGE_CAMERA:
     137        query = pxDataGet("magicdstool_definebyquery_camera.sql");
     138        break;
     139    case IPP_STAGE_WARP:
     140        query = pxDataGet("magicdstool_definebyquery_warp.sql");
     141        break;
     142    case IPP_STAGE_DIFF:
     143        query = pxDataGet("magicdstool_definebyquery_diff.sql");
     144        break;
     145    case IPP_STAGE_FAKE:
     146    case IPP_STAGE_STACK:
     147        psError(PXTOOLS_ERR_DATA, true, "%sRuns do not need to be destreaked", stage);
     148        return false;
     149    case IPP_STAGE_NONE:
     150        psError(PXTOOLS_ERR_DATA, true, "%s is not a valid stage", stage);
     151        return false;
     152    default:
     153        psError(PXTOOLS_ERR_PROG, true, "ippStageToString returned %d for invalid stage %s",
     154            stageNum, stage);
     155        return false;
     156    }
     157
     158    if (!query) {
     159        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     160        return false;
     161    }
     162    char *rerun_flag =  rerun ? "\n1 " : "\n0" ;
     163
     164    if (stageNum != IPP_STAGE_DIFF) {
     165        if (psListLength(where->list)) {
     166            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     167            psStringAppend(&query, "\nAND %s", whereClause);
     168            psFree(whereClause);
     169        }
     170        psFree(where);
     171
     172        // treat limit == 0 as "no limit"
     173        if (limit) {
     174            psString limitString = psDBGenerateLimitSQL(limit);
     175            psStringAppend(&query, " %s", limitString);
     176            psFree(limitString);
     177        }
     178        if (!p_psDBRunQueryF(config->dbh, query, rerun_flag)) {
    123179            psError(PS_ERR_UNKNOWN, false, "database error");
    124             return false;
    125         }
    126         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);
    152180            psFree(query);
    153181            return false;
    154182        }
    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 : "")) {
     183    } else {
     184        // diff stage query has two types bothways and !bothways
     185        // so we need to send the rerun flag and the where data twice
     186        psString whereString = psStringCopy("");
     187        if (psListLength(where->list)) {
     188            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     189            psStringAppend(&whereString, "\nAND %s", whereClause);
     190            psFree(whereClause);
     191        }
     192        psFree(where);
     193
     194        // treat limit == 0 as "no limit"
     195        if (limit) {
     196            psString limitString = psDBGenerateLimitSQL(limit);
     197            psStringAppend(&query, " %s", limitString);
     198            psFree(limitString);
     199        }
     200
     201        if (!p_psDBRunQueryF(config->dbh, query, rerun_flag, whereString, rerun_flag, whereString)) {
    206202            psError(PS_ERR_UNKNOWN, false, "database error");
    207             psFree(magicSkyCellNumsWhere);
     203            psFree(whereString);
    208204            psFree(query);
    209205            return false;
    210206        }
    211         psFree(magicSkyCellNumsWhere);
    212         psFree(query);
    213     }
     207        psFree(whereString);
     208    }
     209    psFree(query);
    214210
    215211    psArray *output = p_psDBFetchResult(config->dbh);
     
    233229    }
    234230
    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
     231    // Parse the list of runs ready to be destreaked
     232
     233    if (!dry_run && !psDBTransaction(config->dbh)) {
     234        psError(PS_ERR_UNKNOWN, false, "database error");
     235        return false;
     236    }
    243237
    244238    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    245239    for (long i = 0; i < psArrayLength(output); i++) {
    246240        psMetadata *row = output->data[i]; // Row of interest
    247         psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id"); // Exposure identifier
     241        psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     242        psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id");
     243        psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
     244        psS64 inv_magic_id = psMetadataLookupS64(NULL, row, "inv_magic_id");
     245        psS64 cam_id = psMetadataLookupS64(NULL, row, "cam_id");
     246        psString magicRunLabel = psMetadataLookupStr(NULL, row, "label");
     247        psString magicRunWorkdir = psMetadataLookupStr(NULL, row, "workdir");
     248       
     249        // if workdir is not supplied use the magicRun's
     250        if (!workdir) {
     251            workdir = magicRunWorkdir;
     252        }
     253
     254        psString outroot = NULL;
     255        // set outroot to workdir/exp_id/stage for example /somewhere/424242/chip
     256        psStringAppend(&outroot, "%s/%" PRId64 "/%s", workdir, exp_id, stage);
    248257
    249258        // create a new magicRun for this group
    250         magicRunRow *run = magicRunRowAlloc(0, exp_id, "new", workdir, "dirty", label, dvodb, registered, 0);
     259        magicDSRunRow *run = magicDSRunRowAlloc(
     260                0, // magic_ds_id
     261                magic_id,
     262                inv_magic_id,
     263                "new",
     264                stage,
     265                stage_id,
     266                cam_id,
     267                set_label ? set_label : magicRunLabel,
     268                outroot,
     269                recoveryroot,
     270                re_place,
     271                0); // remove
     272
     273        psFree(outroot);
    251274        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)) {
     275            psAbort("failed to alloc magicDSRun object");
     276        }
     277
     278        if (!dry_run) {
     279            if (!magicDSRunInsertObject(config->dbh, run)) {
    262280                psError(PS_ERR_UNKNOWN, false, "database error");
     281                psFree(run);
     282                psFree(output);
     283                psFree(list);
     284                if (!psDBRollback(config->dbh)) {
     285                    psError(PS_ERR_UNKNOWN, false, "database error");
     286                }
     287                return false;
    263288            }
    264             return false;
    265         }
    266 
    267         psS64 magic_id = psDBLastInsertID(config->dbh); // Assigned identifier
    268         run->magic_id = magic_id;
     289            psS64 magic_ds_id = psDBLastInsertID(config->dbh); // Assigned identifier
     290            run->magic_ds_id = magic_ds_id;
     291        }
    269292
    270293        psArrayAdd(list, list->n, run);
    271294        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)) {
     295    }
     296
     297    if (!dry_run && !psDBCommit(config->dbh)) {
    303298        psError(PS_ERR_UNKNOWN, false, "database error");
    304299        return false;
     
    306301    psFree(output);
    307302
    308     if (!magicRunPrintObjects(stdout, list, !simple)) {
     303    if (!magicDSRunPrintObjects(stdout, list, !simple)) {
    309304        psError(PS_ERR_UNKNOWN, false, "failed to print object");
    310305        psFree(list);
     
    315310
    316311    return true;
    317 #endif // notyet
    318312}
    319313
     
    344338            0,          // ID
    345339            magic_id,
     340            0,          // inv_magic_id
    346341            "new",      // state
    347342            stage,
     
    405400    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
    406401    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     402    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    407403
    408404    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    501497    psMetadata *row = output->data[0];
    502498
    503     psString stage = psMetadataLookupStr(NULL, row, "stage");
     499    psString stage= psMetadataLookupStr(NULL, row, "stage");
    504500    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
    505501
    506     if (!strcmp(stage, "camera")) {
    507         // no there is no magicked column in camProcessedExp
     502    ippStage stageNum = ippStringToStage(stage);
     503
     504    // chose the appropriate query based on the stage
     505    switch (stageNum) {
     506    case IPP_STAGE_RAW:
     507        query = "UPDATE rawImfile SET magicked = %" PRId64 " where exp_id = %" PRId64 " AND class_id = '%s'";
     508        break;
     509    case IPP_STAGE_CHIP:
     510        query = "UPDATE chipProcessedImfile SET magicked = %" PRId64 " where chip_id = %" PRId64 " AND class_id = '%s'";
     511        break;
     512    case IPP_STAGE_CAMERA:
     513        // no there is no magicked column in camProcessedExp so we have nothing to do
    508514        psFree(output);
    509515        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")) {
     516    case IPP_STAGE_WARP:
    518517        query = "UPDATE warpSkyfile SET magicked = %" PRId64 " where warp_id = %" PRId64 " AND skycell_id = '%s'";
    519     } else if (!strcmp(stage, "diff")) {
     518        break;
     519    case IPP_STAGE_DIFF:
    520520        query = "UPDATE diffSkyfile SET magicked = %" PRId64 " where diff_id = %" PRId64 " AND skycell_id = '%s'";
    521     } else {
     521        break;
     522    default:
    522523        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
    523524        psFree(output);
    524525        return false;
    525526    }
     527
    526528    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id, stage_id, component)) {
    527529        psError(PS_ERR_UNKNOWN, false, "database error");
     
    571573    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
    572574
     575    ippStage stageNum = ippStringToStage(stage);
    573576
    574577    // chose the appropriate query based on the stage
    575     if (!strcmp(stage, "raw")) {
     578    switch (stageNum) {
     579    case IPP_STAGE_RAW:
    576580        query = "UPDATE rawExp SET magicked = %" PRId64 " where exp_id = %" PRId64;
    577     } else if (!strcmp(stage, "chip")) {
     581        break;
     582    case IPP_STAGE_CHIP:
    578583        query = "UPDATE chipRun SET magicked = %" PRId64 " where chip_id = %" PRId64;
    579     } else if (!strcmp(stage, "camera")) {
     584        break;
     585    case IPP_STAGE_CAMERA:
    580586        query = "UPDATE camRun SET magicked = %" PRId64 " where cam_id = %" PRId64;
    581     } else if (!strcmp(stage, "warp")) {
     587        break;
     588    case IPP_STAGE_WARP:
    582589        query = "UPDATE warpRun SET magicked = %" PRId64 " where warp_id = %" PRId64;
    583     } else if (!strcmp(stage, "diff")) {
     590        break;
     591    case IPP_STAGE_DIFF:
    584592        query = "UPDATE diffRun SET magicked = %" PRId64 " where diff_id = %" PRId64;
    585     } else {
     593        break;
     594    default:
    586595        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
    587596        psFree(output);
     
    671680    PS_ASSERT_PTR_NON_NULL(cam_id, false);
    672681
    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 
     682    int stageNum = ippStringToStage(stage);;
     683    if (stageNum == IPP_STAGE_NONE) {
     684        psError(PXTOOLS_ERR_DATA, false, "%s is not a valid value for stage", stage);
     685        return false;
     686    }
    694687
    695688    psString query = pxDataGet("magicdstool_getrunids.sql");
     
    724717
    725718    *cam_id = psMetadataLookupS64(NULL, row, "cam_id");
    726     if (stageNum == 0) {
     719    switch (stageNum) {
     720    case IPP_STAGE_RAW:
    727721        *stage_id = psMetadataLookupS64(NULL, row, "exp_id");
    728     } else if (stageNum == 1) {
     722        break;
     723    case IPP_STAGE_CHIP:
    729724        *stage_id = psMetadataLookupS64(NULL, row, "chip_id");
    730     } else if (stageNum == 2) {
     725        break;
     726    case IPP_STAGE_CAMERA:
    731727        *stage_id = *cam_id;
    732     } else if (stageNum == 3) {
     728        return true;
     729    case IPP_STAGE_WARP:
    733730        *stage_id = psMetadataLookupS64(NULL, row, "warp_id");
     731        break;
     732    case IPP_STAGE_DIFF:
     733        *stage_id = psMetadataLookupS64(NULL, row, "diff_id");
     734        break;
    734735    }
    735736
Note: See TracChangeset for help on using the changeset viewer.