Changeset 18663 for trunk/ippTools/src/magictool.c
- Timestamp:
- Jul 22, 2008, 4:36:48 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/ippTools/src/magictool.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippTools/src/magictool.c
r18573 r18663 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_S64(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">="); 147 PXOPT_COPY_S64(config->args, where, "-good_frac", "diffSkyfile.good_frac", ">="); 148 149 if (psListLength(where->list)) { 150 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 151 psStringAppend(&query, " AND %s", whereClause); 152 psFree(whereClause); 153 } 154 psFree(where); 155 156 psString groupby = pxDataGet("magictool_definebyquery_temp_insert_groupby.sql"); 157 if (!groupby) { 158 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 159 psFree(query); 160 return false; 161 } 162 163 psStringAppend(&query, " %s", groupby); 164 psFree(groupby); 138 165 139 166 if (!p_psDBRunQuery(config->dbh, query)) { … … 145 172 } 146 173 147 // find the diff_id's of the warped skycells174 // Get list of exposures ready to magic 148 175 { 149 psString query = pxDataGet("magictool_ find_complete_diffed_exposures.sql");176 psString query = pxDataGet("magictool_definebyquery_select_part1.sql"); 150 177 if (!query) { 151 178 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 152 179 return false; 153 180 } 181 182 { 183 psMetadata *where = psMetadataAlloc(); 184 PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "=="); 185 PXOPT_COPY_S64(config->args, where, "-good_frac", "warpSkyfile.good_frac", ">="); 186 187 if (psListLength(where->list)) { 188 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 189 psStringAppend(&query, " AND %s", whereClause); 190 psFree(whereClause); 191 } 192 psFree(where); 193 } 194 195 psString part2 = pxDataGet("magictool_definebyquery_select_part2.sql"); 196 if (!part2) { 197 psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); 198 return false; 199 } 200 201 psStringAppend(&query, " %s", part2); 202 psFree(part2); 154 203 155 204 if (!p_psDBRunQuery(config->dbh, query)) { … … 180 229 } 181 230 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 231 if (!psDBTransaction(config->dbh)) { 232 psError(PS_ERR_UNKNOWN, false, "database error"); 233 return false; 234 } 235 236 psString insert = pxDataGet("magictool_definebyquery_insert.sql"); // Insert query 237 238 // iterate over array of grouped warp_ids 185 239 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]; 240 psMetadata *row = output->data[i]; // Row of interest 241 psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id"); // Exposure identifier 216 242 217 243 // 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 ); 244 magicRunRow *run = magicRunRowAlloc(0, exp_id, "reg", workdir, "dirty", label, dvodb, registered, 0); 228 245 if (!run) { 229 246 psAbort("failed to alloc magicRun object"); 230 247 } 248 231 249 if (!magicRunInsertObject(config->dbh, run)) { 232 250 psError(PS_ERR_UNKNOWN, false, "database error"); 233 251 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"); 252 psFree(insert); 253 psFree(output); 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 if (!magicRunPrintObject(stdout, run, !simple)) { 260 psError(PS_ERR_UNKNOWN, false, "failed to print object"); 261 psFree(run); 262 psFree(insert); 263 psFree(output); 264 if (!psDBRollback(config->dbh)) { 265 psError(PS_ERR_UNKNOWN, false, "database error"); 253 266 } 254 255 if (!magicInputSkyfileInsert(config->dbh, 256 magic_id, 257 diff_id, 258 node)) { 267 return false; 268 } 269 psFree(run); 270 271 psS64 magic_id = psDBLastInsertID(config->dbh); // Assigned identifier 272 273 // Create a suitable insertion query for this run 274 psString thisInsert = psStringCopy(insert); 275 psString idString = NULL; 276 psStringAppend(&idString, "%" PRId64, magic_id); 277 psStringSubstitute(&thisInsert, idString, "@MAGIC_ID@"); 278 psFree(idString); 279 280 if (!p_psDBRunQuery(config->dbh, thisInsert, magic_id)) { 281 psError(PS_ERR_UNKNOWN, false, "database error"); 282 psFree(thisInsert); 283 psFree(insert); 284 psFree(output); 285 if (!psDBRollback(config->dbh)) { 259 286 psError(PS_ERR_UNKNOWN, false, "database error"); 260 psFree(grouped);261 return false;262 287 } 263 } 264 265 // set magicRun.state to 'run' 266 return setmagicRunState(config, magic_id, "run"); 267 } 268 psFree(grouped); 288 return false; 289 } 290 psFree(thisInsert); 291 } 292 293 if (!psDBCommit(config->dbh)) { 294 psError(PS_ERR_UNKNOWN, false, "database error"); 295 return false; 296 } 297 psFree(output); 269 298 270 299 return true; … … 277 306 // required 278 307 PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false); 308 PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", true, false); 279 309 280 310 // optional … … 286 316 magicRunRow *run = magicRunRowAlloc( 287 317 0, // ID 318 exp_id, 288 319 "reg", // state 289 320 workdir,
Note:
See TracChangeset
for help on using the changeset viewer.
