IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 5, 2008, 2:07:07 PM (18 years ago)
Author:
eugene
Message:

updates from HEAD

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20080719/ippTools/src/magictool.c

    r18573 r18923  
    110110    PS_ASSERT_PTR_NON_NULL(config, false);
    111111
     112    // Required
    112113    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false);
     114
     115    // Optional
    113116    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    114117    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
    115118    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
    116 
    117     // create warped skycells temp table
     119    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     120
     121    // Create temporary table of the best diffs
    118122    {
    119         psString query = pxDataGet("magictool_create_tmp_warpcomplete.sql");
     123        psString query = pxDataGet("magictool_definebyquery_temp_create.sql");
    120124        if (!query) {
    121125            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    127131            return false;
    128132        }
    129     }
    130 
    131     // find warped skycells
     133        psFree(query);
     134    }
     135
     136    // Insert list of best diffs into temporary table
    132137    {
    133         psString query = pxDataGet("magictool_find_complete_warpruns.sql");
     138        psString query = pxDataGet("magictool_definebyquery_temp_insert.sql");
    134139        if (!query) {
    135140            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    136141            return false;
    137142        }
     143
     144        psMetadata *where = psMetadataAlloc();
     145        PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
     146        PXOPT_COPY_F32(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">=");
     147
     148        if (psListLength(where->list)) {
     149            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     150            psStringAppend(&query, " AND %s", whereClause);
     151            psFree(whereClause);
     152        }
     153        psFree(where);
     154
     155        psString groupby = pxDataGet("magictool_definebyquery_temp_insert_groupby.sql");
     156        if (!groupby) {
     157            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     158            psFree(query);
     159            return false;
     160        }
     161
     162        psStringAppend(&query, " %s", groupby);
     163        psFree(groupby);
    138164
    139165        if (!p_psDBRunQuery(config->dbh, query)) {
     
    145171    }
    146172
    147     // find the diff_id's of the warped skycells
     173    // Get list of exposures ready to magic
    148174    {
    149         psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
     175        psString query = pxDataGet("magictool_definebyquery_select_part1.sql");
    150176        if (!query) {
    151177            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    152178            return false;
    153179        }
     180
     181        {
     182            psMetadata *where = psMetadataAlloc();
     183            PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
     184            PXOPT_COPY_F32(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">=");
     185
     186            if (psListLength(where->list)) {
     187                psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     188                psStringAppend(&query, " AND %s", whereClause);
     189                psFree(whereClause);
     190            }
     191            psFree(where);
     192        }
     193
     194        psString part2 = pxDataGet("magictool_definebyquery_select_part2.sql");
     195        if (!part2) {
     196            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     197            return false;
     198        }
     199
     200        psStringAppend(&query, " %s", part2);
     201        psFree(part2);
    154202
    155203        if (!p_psDBRunQuery(config->dbh, query)) {
     
    180228    }
    181229
    182     // pre-allocate enough hash buckets for 1 unique warp_id per row
    183     psHash *groups = psHashAlloc(psArrayLength(output));
    184     // iterate over result set, and group by warp_id
     230    if (!psDBTransaction(config->dbh)) {
     231        psError(PS_ERR_UNKNOWN, false, "database error");
     232        return false;
     233    }
     234
     235    psString insert = pxDataGet("magictool_definebyquery_insert.sql"); // Insert query
     236
     237    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
    185238    for (long i = 0; i < psArrayLength(output); i++) {
    186         // lookup the key we're grouping by
    187         bool status = false;
    188         psS64 warp_id = psMetadataLookupS64(&status, output->data[i], "warp_id");
    189         if (!status) {
    190             psAbort("failed to lookup value for warp_id");
    191         }
    192 
    193         psString key = psDBIntToString(warp_id);
    194         // look to see if there is a bin for this key
    195         psArray *groupBin = psHashLookup(groups, key);
    196         if (!groupBin) {
    197             // if not, create one
    198             groupBin = psArrayAlloc(0);
    199             psHashAdd(groups, key, groupBin);
    200         }
    201         psFree(key);
    202 
    203         // add this row to the bin
    204         psArrayAdd(groupBin, 0, output->data[i]);
    205         psFree(groupBin);
    206     }
    207     psFree(output);
    208 
    209     // create a magic run
    210     psArray *grouped =  psHashToArray(groups);
    211     psFree(groups);
    212 
    213     // iterate over array of grouped warp_ids
    214     for (long i = 0; i < psArrayLength(grouped); i++) {
    215         psArray *group = grouped->data[i];
     239        psMetadata *row = output->data[i]; // Row of interest
     240        psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id"); // Exposure identifier
    216241
    217242        // create a new magicRun for this group
    218         magicRunRow *run = magicRunRowAlloc(
    219             0,                          // ID
    220             "reg",                      // state
    221             workdir,                    // workdir
    222             "dirty",                    // workdir_state
    223             label,                      // label
    224             dvodb,                      // dvodb
    225             registered,                 // registered
    226             0                           // fault
    227             );
     243        magicRunRow *run = magicRunRowAlloc(0, exp_id, "run", workdir, "dirty", label, dvodb, registered, 0);
    228244        if (!run) {
    229245            psAbort("failed to alloc magicRun object");
    230246        }
     247
    231248        if (!magicRunInsertObject(config->dbh, run)) {
    232249            psError(PS_ERR_UNKNOWN, false, "database error");
    233250            psFree(run);
    234             psFree(grouped);
    235             return false;
    236         }
    237 
    238         // get the assigned warp_id
    239         psS64 magic_id = psDBLastInsertID(config->dbh);
    240 
    241         // insert all rows in this bin into the new magicRun
    242         for (long j = 0; j < psArrayLength(group); j++) {
    243             psMetadata *row = group->data[j];
    244 
    245             bool status = false;
    246             psS64 diff_id = psMetadataLookupS64(&status, row, "diff_id");
    247             if (!status) {
    248                 psAbort("failed to lookup value for diff_id");
     251            psFree(insert);
     252            psFree(output);
     253            psFree(list);
     254            if (!psDBRollback(config->dbh)) {
     255                psError(PS_ERR_UNKNOWN, false, "database error");
    249256            }
    250             psString node = psMetadataLookupStr(&status, row, "skycell_id");
    251             if (!status) {
    252                 psAbort("failed to lookup value for diff_id");
     257            return false;
     258        }
     259
     260        psS64 magic_id = psDBLastInsertID(config->dbh); // Assigned identifier
     261        run->magic_id = magic_id;
     262
     263        psArrayAdd(list, list->n, run);
     264        psFree(run);
     265
     266        // Create a suitable insertion query for this run
     267        psString thisInsert = psStringCopy(insert);
     268        {
     269            psString idString = NULL;
     270            psStringAppend(&idString, "%" PRId64, magic_id);
     271            psStringSubstitute(&thisInsert, idString, "@MAGIC_ID@");
     272            psFree(idString);
     273        }
     274        {
     275            psString idString = NULL;
     276            psStringAppend(&idString, "%" PRId64, exp_id);
     277            psStringSubstitute(&thisInsert, idString, "@EXP_ID@");
     278            psFree(idString);
     279        }
     280
     281        if (!p_psDBRunQuery(config->dbh, thisInsert, magic_id, exp_id)) {
     282            psError(PS_ERR_UNKNOWN, false, "database error");
     283            psFree(thisInsert);
     284            psFree(insert);
     285            psFree(output);
     286            psFree(list);
     287            if (!psDBRollback(config->dbh)) {
     288                psError(PS_ERR_UNKNOWN, false, "database error");
    253289            }
    254 
    255             if (!magicInputSkyfileInsert(config->dbh,
    256                                          magic_id,
    257                                          diff_id,
    258                                          node)) {
    259                 psError(PS_ERR_UNKNOWN, false, "database error");
    260                 psFree(grouped);
    261                 return false;
    262             }
    263         }
    264 
    265         // set magicRun.state to 'run'
    266         return setmagicRunState(config, magic_id, "run");
    267     }
    268     psFree(grouped);
     290            return false;
     291        }
     292        psFree(thisInsert);
     293    }
     294
     295    if (!psDBCommit(config->dbh)) {
     296        psError(PS_ERR_UNKNOWN, false, "database error");
     297        return false;
     298    }
     299    psFree(output);
     300
     301    if (!magicRunPrintObjects(stdout, list, !simple)) {
     302        psError(PS_ERR_UNKNOWN, false, "failed to print object");
     303        psFree(list);
     304        return false;
     305    }
     306
     307    psFree(list);
    269308
    270309    return true;
     
    277316    // required
    278317    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
     318    PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", true, false);
    279319
    280320    // optional
     
    286326    magicRunRow *run = magicRunRowAlloc(
    287327            0,          // ID
     328            exp_id,
    288329            "reg",      // state
    289330            workdir,
     
    658699    }
    659700
     701    if (!node->data) {
     702        // It's a leaf node, not done
     703        return false;
     704    }
     705
    660706    if (psMetadataLookupBool(NULL, node->data, "done")) {
    661707        // It's already done
     
    671717        while ((child = psListGetAndIncrement(iter))) {
    672718            psMetadata *data = child->data;
     719            if (!data) {
     720                // Child is a leaf node, not done
     721                psFree(iter);
     722                psFree(work);
     723                return false;
     724            }
    673725
    674726            bool status = false;
     
    812864
    813865    // find the root of the tree
    814     pxNode *root = psHashLookup(forest, "root");
     866    pxNode *root = psMemIncrRefCounter(psHashLookup(forest, "root"));
    815867    psFree(forest);
    816     // pxTreePrint(stdout, root);
     868    //    pxTreePrint(stdout, root);
    817869
    818870    // crawl through the tree and looking for nodes with children that are all
Note: See TracChangeset for help on using the changeset viewer.