Changeset 18923 for branches/eam_branch_20080719/ippTools/src/magictool.c
- Timestamp:
- Aug 5, 2008, 2:07:07 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branch_20080719/ippTools/src/magictool.c
r18573 r18923 110 110 PS_ASSERT_PTR_NON_NULL(config, false); 111 111 112 // Required 112 113 PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false); 114 115 // Optional 113 116 PXOPT_LOOKUP_STR(label, config->args, "-label", false, false); 114 117 PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false); 115 118 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 118 122 { 119 psString query = pxDataGet("magictool_ create_tmp_warpcomplete.sql");123 psString query = pxDataGet("magictool_definebyquery_temp_create.sql"); 120 124 if (!query) { 121 125 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); … … 127 131 return false; 128 132 } 129 } 130 131 // find warped skycells 133 psFree(query); 134 } 135 136 // Insert list of best diffs into temporary table 132 137 { 133 psString query = pxDataGet("magictool_ find_complete_warpruns.sql");138 psString query = pxDataGet("magictool_definebyquery_temp_insert.sql"); 134 139 if (!query) { 135 140 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 136 141 return false; 137 142 } 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); 138 164 139 165 if (!p_psDBRunQuery(config->dbh, query)) { … … 145 171 } 146 172 147 // find the diff_id's of the warped skycells173 // Get list of exposures ready to magic 148 174 { 149 psString query = pxDataGet("magictool_ find_complete_diffed_exposures.sql");175 psString query = pxDataGet("magictool_definebyquery_select_part1.sql"); 150 176 if (!query) { 151 177 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 152 178 return false; 153 179 } 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); 154 202 155 203 if (!p_psDBRunQuery(config->dbh, query)) { … … 180 228 } 181 229 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 185 238 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 216 241 217 242 // 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); 228 244 if (!run) { 229 245 psAbort("failed to alloc magicRun object"); 230 246 } 247 231 248 if (!magicRunInsertObject(config->dbh, run)) { 232 249 psError(PS_ERR_UNKNOWN, false, "database error"); 233 250 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"); 249 256 } 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"); 253 289 } 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); 269 308 270 309 return true; … … 277 316 // required 278 317 PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false); 318 PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", true, false); 279 319 280 320 // optional … … 286 326 magicRunRow *run = magicRunRowAlloc( 287 327 0, // ID 328 exp_id, 288 329 "reg", // state 289 330 workdir, … … 658 699 } 659 700 701 if (!node->data) { 702 // It's a leaf node, not done 703 return false; 704 } 705 660 706 if (psMetadataLookupBool(NULL, node->data, "done")) { 661 707 // It's already done … … 671 717 while ((child = psListGetAndIncrement(iter))) { 672 718 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 } 673 725 674 726 bool status = false; … … 812 864 813 865 // find the root of the tree 814 pxNode *root = ps HashLookup(forest, "root");866 pxNode *root = psMemIncrRefCounter(psHashLookup(forest, "root")); 815 867 psFree(forest); 816 // pxTreePrint(stdout, root);868 // pxTreePrint(stdout, root); 817 869 818 870 // crawl through the tree and looking for nodes with children that are all
Note:
See TracChangeset
for help on using the changeset viewer.
