Changeset 35149
- Timestamp:
- Feb 11, 2013, 4:50:12 PM (13 years ago)
- Location:
- branches/czw_branch/20130211
- Files:
-
- 14 edited
-
dbconfig (modified) (1 prop)
-
dbconfig/changes.txt (modified) (1 prop)
-
ippTools (modified) (1 prop)
-
ippTools/share/pxadmin_create_tables.sql (modified) (3 diffs)
-
ippTools/src (modified) (1 prop)
-
ippTools/src/difftool.c (modified) (10 diffs)
-
ippconfig/gpc1/ppImage.config (modified) (4 diffs)
-
ippconfig/recipes/ppImage.config (modified) (1 diff)
-
ppImage/src/ppImage.h (modified) (1 diff)
-
ppImage/src/ppImageDetrendPattern.c (modified) (2 diffs)
-
ppImage/src/ppImageLoop.c (modified) (1 diff)
-
ppImage/src/ppImageOptions.c (modified) (2 diffs)
-
psModules/src/detrend/pmPattern.c (modified) (3 diffs)
-
psModules/src/detrend/pmPattern.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/czw_branch/20130211/dbconfig
- Property svn:mergeinfo set to
-
branches/czw_branch/20130211/dbconfig/changes.txt
- Property svn:mergeinfo changed (with no actual effect on merging)
-
branches/czw_branch/20130211/ippTools
- Property svn:mergeinfo set to
-
branches/czw_branch/20130211/ippTools/share/pxadmin_create_tables.sql
r35144 r35149 1844 1844 data_group VARCHAR(64), -- group for data 1845 1845 dist_group VARCHAR(64), -- group for distribution 1846 reduction VARCHAR(64), -- reduction class (for altering recipe) 1846 reduction VARCHAR(64), -- reduction class (for altering recipe) 1847 alt_cam_label VARCHAR(64), -- label for alternate version of astrometry (optional) 1847 1848 note VARCHAR(255), -- note 1848 1849 registered TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- time run was registered … … 2162 2163 surveyID INT(8), 2163 2164 releaseName VARCHAR(64), 2164 state VARCHAR(16), -- active, pending archivedrop2165 relase_state VARCHAR(16), -- active, pending, archive, drop 2165 2166 dataRelease INT(8), -- PSPS dataRelease 2167 priority INT(8), 2166 2168 PRIMARY KEY(rel_id), 2167 2169 KEY(releaseName), 2168 2170 KEY(state), 2171 KEY(priority), 2169 2172 FOREIGN KEY(surveyID) REFERENCES survey(surveyID) 2170 2173 ) ENGINE=Innodb DEFAULT CHARSET=latin1; … … 2192 2195 ) ENGINE=innodb DEFAULT CHARSET=latin1; 2193 2196 2194 2195 -- This comment line is here to avoid empty query error. 2197 CREATE TABLE relStack ( 2198 relstack_id BIGINT AUTO_INCREMENT, 2199 rel_id INT(8), 2200 skycal_id BIGINT, -- id of the sky calibration run that supplied the calibration 2201 -- for this release. We get back to stack from here 2202 skycell_id varchar(64), 2203 tess_id varchar(64), 2204 state VARCHAR(16), -- released, pending, archive, drop 2205 flags INT UNSIGNED, 2206 stack_type VARCHAR(16), -- nightly, deep, reference 2207 zpt_obs FLOAT, -- calibrated zero point for this release of this skycell 2208 zpt_stdev FLOAT, 2209 fwhm_major FLOAT, -- seeing as measured by staticsky 2210 path_base VARCHAR(255), -- path_base of any supporting files, 2211 registered DATETIME, 2212 time_stamp DATETIME, 2213 PRIMARY KEY (relstack_id), 2214 KEY (tess_id, skycell_id), 2215 KEY (rel_id, tess_id, skycell_id), 2216 FOREIGN KEY(rel_id) REFERENCES ippRelease(rel_id), 2217 FOREIGN KEY(skycal_id) REFERENCES skycalRun(skycal_id) 2218 ) ENGINE=innodb DEFAULT CHARSET=latin1; 2219 2220 2221 2222 -- These comment lines are here to avoid an empty query error. 2196 2223 -- Another way to avoid that problem is to omit the semicolon above but I think that is untidy. -
branches/czw_branch/20130211/ippTools/src
- Property svn:mergeinfo changed (with no actual effect on merging)
-
branches/czw_branch/20130211/ippTools/src/difftool.c
r34770 r35149 41 41 static bool revertdiffskyfileMode(pxConfig *config); 42 42 static bool definepoprunMode(pxConfig *config); 43 static bool definewarpstackMode(pxConfig *config); 43 //static bool definewarpstackMode(pxConfig *config); 44 static bool definewarpstackModeNew(pxConfig *config); 44 45 static bool definewarpwarpMode(pxConfig *config); 45 46 static bool definestackstackMode(pxConfig *config); … … 94 95 MODECASE(DIFFTOOL_MODE_REVERTDIFFSKYFILE, revertdiffskyfileMode); 95 96 MODECASE(DIFFTOOL_MODE_DEFINEPOPRUN, definepoprunMode); 96 MODECASE(DIFFTOOL_MODE_DEFINEWARPSTACK, definewarpstackMode );97 MODECASE(DIFFTOOL_MODE_DEFINEWARPSTACK, definewarpstackModeNew); 97 98 MODECASE(DIFFTOOL_MODE_DEFINEWARPWARP, definewarpwarpMode); 98 99 MODECASE(DIFFTOOL_MODE_DEFINESTACKSTACK, definestackstackMode); … … 1226 1227 } 1227 1228 1228 1229 static bool definewarpstackMode(pxConfig *config) 1229 static bool definewarpstackModeNew(pxConfig *config) 1230 1230 { 1231 PS_ASSERT_PTR_NON_NULL(config, false); 1232 1231 PS_ASSERT_PTR_NON_NULL(config,false); 1233 1232 psMetadata *expWhere = psMetadataAlloc(); 1234 1233 psMetadata *warp1Where = psMetadataAlloc(); // First set of restrictions on warp 1235 psMetadata *warp2Where = psMetadataAlloc(); // Second set of restriction on warp1234 // psMetadata *warp2Where = psMetadataAlloc(); // Second set of restriction on warp 1236 1235 psMetadata *stackWhere = psMetadataAlloc(); 1237 1236 … … 1243 1242 PXOPT_COPY_STR(config->args, warp1Where, "-tess_id", "warpRun.tess_id", "=="); 1244 1243 PXOPT_COPY_STR(config->args, warp1Where, "-data_group", "warpRun.data_group", "=="); 1245 PXOPT_COPY_S64(config->args, warp2Where, "-warp_id", "warpRun.warp_id", "==");1246 PXOPT_COPY_STR(config->args, warp2Where, "-tess_id", "warpRun.tess_id", "==");1247 PXOPT_COPY_STR(config->args, warp2Where, "-data_group", "warpRun.data_group", "==");1248 PXOPT_COPY_STR(config->args, warp2Where, "-skycell_id", "warpSkyfile.skycell_id", "==");1249 PXOPT_COPY_STR(config->args, warp2Where, "-warp_label", "warpRun.label", "==");1250 PXOPT_COPY_F32(config->args, warp 2Where, "-good_frac", "warpSkyfile.good_frac", ">=");1244 // PXOPT_COPY_S64(config->args, warp2Where, "-warp_id", "warpRun.warp_id", "=="); 1245 // PXOPT_COPY_STR(config->args, warp2Where, "-tess_id", "warpRun.tess_id", "=="); 1246 // PXOPT_COPY_STR(config->args, warp2Where, "-data_group", "warpRun.data_group", "=="); 1247 // PXOPT_COPY_STR(config->args, warp2Where, "-skycell_id", "warpSkyfile.skycell_id", "=="); 1248 // PXOPT_COPY_STR(config->args, warp2Where, "-warp_label", "warpRun.label", "=="); 1249 PXOPT_COPY_F32(config->args, warp1Where, "-good_frac", "warpSkyfile.good_frac", ">="); 1251 1250 PXOPT_COPY_STR(config->args, stackWhere, "-stack_label", "stackRun.label", "=="); 1252 1251 PXOPT_COPY_STR(config->args, stackWhere, "-stack_data_group", "stackRun.data_group", "=="); … … 1268 1267 PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false); 1269 1268 1270 // find all things to queue 1271 psString query = pxDataGet("difftool_definewarpstack_part1.sql"); 1272 if (!query) { 1273 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 1274 return false; 1275 } 1276 1277 psString warp1Query = NULL; 1278 psString warp2Query = NULL; 1279 psString stackQuery = NULL; 1280 psString expQuery = NULL; 1281 1269 psString newQuery = NULL; 1270 psStringAppend(&newQuery, "select exp_id,warp_id,rawExp.filter,warpRun.label,warpRun.data_group,warpRun.tess_id,warpRun.data_group,"); 1271 psStringAppend(&newQuery, "warpSkyfile.skycell_id,stack_id,stackRun.skycell_id,stackRun.tess_id,stackRun.label,stackRun.data_group,diff_id "); 1272 psStringAppend(&newQuery, "from warpRun JOIN fakeRun USING(fake_id) JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id) "); 1273 psStringAppend(&newQuery, "JOIN warpSkyfile USING(warp_id) JOIN stackRun USING(skycell_id,filter) "); 1274 psStringAppend(&newQuery, "LEFT JOIN diffInputSkyfile ON (warp_id = diffInputSkyfile.warp1 AND stack_id = diffInputSkyfile.stack2 AND diffInputSkyfile.skycell_id = stackRun.skycell_id) WHERE 1"); 1275 1282 1276 if (psListLength(expWhere->list)) { 1283 1277 psString whereClause = psDBGenerateWhereConditionSQL(expWhere, NULL); 1284 psStringAppend(& expQuery, "\n AND %s", whereClause);1278 psStringAppend(&newQuery, "\n AND %s", whereClause); 1285 1279 psFree(whereClause); 1286 1280 } else { 1287 expQuery = psStringCopy("\n");1281 // newQuery = psStringCopy("\n"); 1288 1282 } 1289 1283 psFree(expWhere); 1290 1284 if (psListLength(warp1Where->list)) { 1291 1285 psString whereClause = psDBGenerateWhereConditionSQL(warp1Where, NULL); 1292 psStringAppend(& warp1Query, "\n AND %s", whereClause);1286 psStringAppend(&newQuery, "\n AND %s", whereClause); 1293 1287 psFree(whereClause); 1294 1288 } else { 1295 warp1Query = psStringCopy("\n");1289 // waQuery = psStringCopy("\n"); 1296 1290 } 1297 1291 psFree(warp1Where); 1298 if (psListLength(warp2Where->list)) { 1299 psString whereClause = psDBGenerateWhereConditionSQL(warp2Where, NULL); 1300 psStringAppend(&warp2Query, "\n AND %s", whereClause); 1292 1293 // don't queue for exposures that have already been diff'd unless requested 1294 // psString diffQuery = NULL; 1295 if (! (reRun || newTemplates) ) { 1296 psStringAppend(&newQuery, "\nAND diff_id IS NULL"); 1297 } else { 1298 // diffQuery = psStringCopy("\n"); 1299 } 1300 1301 if (psListLength(stackWhere->list)) { 1302 psString whereClause = psDBGenerateWhereConditionSQL(stackWhere, NULL); 1303 psStringAppend(&newQuery, "\nAND %s", whereClause); 1301 1304 psFree(whereClause); 1302 1305 } else { 1303 warp2Query = psStringCopy("\n"); 1304 } 1305 psFree(warp2Where); 1306 1307 // don't queue for exposures that have already been diff'd unless requested 1308 psString diffQuery = NULL; 1309 if (! (reRun || newTemplates) ) { 1310 psStringAppend(&diffQuery, "\nAND diff_id IS NULL"); 1311 } else { 1312 diffQuery = psStringCopy("\n"); 1313 } 1314 1315 if (psListLength(stackWhere->list)) { 1316 psString whereClause = psDBGenerateWhereConditionSQL(stackWhere, NULL); 1317 psStringAppend(&stackQuery, "\nAND %s", whereClause); 1318 psFree(whereClause); 1319 } else { 1320 stackQuery = psStringCopy(""); 1306 // stackQuery = psStringCopy(""); 1321 1307 } 1322 1308 psFree(stackWhere); 1323 1309 1324 psTrace("difftool", 1, query, warp1Query, warp2Query, diffQuery, expQuery, stackQuery); 1325 1310 // psTrace("difftool", 1, query, warp1Query, warp2Query, diffQuery, expQuery, stackQuery); 1311 fprintf(stderr,"%s",newQuery); 1312 return(true); 1313 #if (0) 1326 1314 if (!psDBTransaction(config->dbh)) { 1327 1315 psError(PS_ERR_UNKNOWN, false, "database error"); … … 1329 1317 } 1330 1318 1319 1331 1320 if (!p_psDBRunQueryF(config->dbh, query, warp1Query, warp2Query, expQuery, diffQuery)) { 1332 1321 psError(PS_ERR_UNKNOWN, false, "database error"); … … 1367 1356 return true; 1368 1357 } 1369 1370 if (pretend) {1371 // negative simple so the default is true1372 if (!ippdbPrintMetadatas(stdout, output, "diffRun", !simple)) {1373 psError(PS_ERR_UNKNOWN, false, "failed to print array");1374 psFree(output);1375 return false;1376 }1377 psFree(output);1378 return true;1379 }1380 1381 // create temporary table1382 query = pxDataGet("difftool_definewarpstack_temp_create.sql");1383 if (!p_psDBRunQuery(config->dbh, query)) {1384 psError(PS_ERR_UNKNOWN, false, "database error");1385 psFree(query);1386 if (!psDBRollback(config->dbh)) {1387 psError(PS_ERR_UNKNOWN, false, "database error");1388 }1389 return false;1390 }1391 psFree(warp1Query);1392 psFree(query);1393 query = NULL;1394 1395 psString skycell_query = pxDataGet("difftool_definewarpstack_part2.sql");1396 1397 1358 psArray *list = psArrayAllocEmpty(16); // List of runs, to print 1398 1359 long numGood = 0; // Number of good rows added … … 1401 1362 psMetadata *row = output->data[i]; // Output row from query 1402 1363 bool mdok; // Status of MD lookup 1403 1404 // Take the first warp for each exposure.1405 // The list is sorted by exposure id and warp_id and the warps are in descending1406 // order.1407 psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id");1408 if (!mdok) {1409 psError(PXTOOLS_ERR_PROG, false, "exp_id not found");1410 if (!psDBRollback(config->dbh)) {1411 psError(PS_ERR_UNKNOWN, false, "database error");1412 }1413 return false;1414 }1415 if (exp_id == last_exp_id) {1416 continue;1417 }1418 last_exp_id = exp_id;1419 1420 // clear temporary table1421 if (!p_psDBRunQuery(config->dbh, "DELETE FROM skycellsToDiff")) {1422 psError(PS_ERR_UNKNOWN, false, "database error");1423 psFree(warp2Query);1424 psFree(stackQuery);1425 psFree(skycell_query);1426 if (!psDBRollback(config->dbh)) {1427 psError(PS_ERR_UNKNOWN, false, "database error");1428 }1429 return false;1430 }1431 psS64 warp_id = psMetadataLookupS64(&mdok, row, "warp_id");1432 if (!mdok) {1433 psError(PXTOOLS_ERR_PROG, false, "warp_id not found --- ignoring row %ld", i);1434 psFree(warp2Query);1435 psFree(stackQuery);1436 psFree(skycell_query);1437 if (!psDBRollback(config->dbh)) {1438 psError(PS_ERR_UNKNOWN, false, "database error");1439 }1440 return false;1441 }1442 psS64 skycell_count = psMetadataLookupS64(&mdok, row, "skycell_count");1443 if (!mdok) {1444 psError(PXTOOLS_ERR_PROG, false, "skycell_count not found");1445 psFree(warp2Query);1446 psFree(stackQuery);1447 psFree(skycell_query);1448 if (!psDBRollback(config->dbh)) {1449 psError(PS_ERR_UNKNOWN, false, "database error");1450 }1451 return false;1452 }1453 psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");1454 if (!mdok) {1455 psError(PXTOOLS_ERR_PROG, false, "tess_id not found");1456 psFree(warp2Query);1457 psFree(stackQuery);1458 psFree(skycell_query);1459 if (!psDBRollback(config->dbh)) {1460 psError(PS_ERR_UNKNOWN, false, "database error");1461 }1462 return false;1463 }1464 1465 psString filter = psMetadataLookupStr(&mdok, row, "filter");1466 if (!mdok) {1467 psError(PXTOOLS_ERR_PROG, false, "filter not found");1468 psFree(warp2Query);1469 psFree(stackQuery);1470 psFree(skycell_query);1471 if (!psDBRollback(config->dbh)) {1472 psError(PS_ERR_UNKNOWN, false, "database error");1473 }1474 return false;1475 }1476 1477 psString warp_data_group = psMetadataLookupStr(&mdok, row, "data_group");1478 if (!mdok) {1479 psError(PXTOOLS_ERR_PROG, false, "warp data_group not found");1480 psFree(warp2Query);1481 psFree(stackQuery);1482 psFree(skycell_query);1483 if (!psDBRollback(config->dbh)) {1484 psError(PS_ERR_UNKNOWN, false, "database error");1485 }1486 return false;1487 }1488 if (!data_group && warp_data_group) {1489 data_group = warp_data_group;1490 }1491 1492 if (!p_psDBRunQueryF(config->dbh, skycell_query, stackQuery, warp_id, filter, warp2Query)) {1493 psError(PS_ERR_UNKNOWN, false, "database error");1494 psFree(warp2Query);1495 psFree(stackQuery);1496 psFree(skycell_query);1497 if (!psDBRollback(config->dbh)) {1498 psError(PS_ERR_UNKNOWN, false, "database error");1499 }1500 return false;1501 }1502 psS64 num = psDBAffectedRows(config->dbh);1503 1504 if (num == 0) {1505 psTrace("difftool", PS_LOG_INFO, "no skycells with stack found for warp_id %" PRId64, warp_id);1506 continue;1507 }1508 1509 if (!available && (num != skycell_count)) {1510 psTrace("difftool", PS_LOG_INFO, "%" PRId64 " skyfiles with stack found for warp_id %" PRId641511 " but need %" PRId64, num, warp_id, skycell_count);1512 continue;1513 }1514 1515 1364 // ok we've got one create the diffRun 1516 1365 diffRunRow *run = diffRunRowAlloc( … … 1537 1386 note 1538 1387 ); 1539 1540 if (!diffRunInsertObject(config->dbh, run)) {1541 psError(PS_ERR_UNKNOWN, false, "database error");1542 psFree(run);1543 if (!psDBRollback(config->dbh)) {1544 psError(PS_ERR_UNKNOWN, false, "database error");1545 }1546 return false;1547 }1548 run->diff_id = psDBLastInsertID(config->dbh);1549 1550 psStringAppend(&query, "UPDATE skycellsToDiff SET diff_id = %" PRId64, run->diff_id);1551 if (!p_psDBRunQuery(config->dbh, query)) {1552 psError(PS_ERR_UNKNOWN, false, "database error");1553 psFree(warp2Query);1554 psFree(stackQuery);1555 psFree(skycell_query);1556 psFree(query);1557 if (!psDBRollback(config->dbh)) {1558 psError(PS_ERR_UNKNOWN, false, "database error");1559 }1560 return false;1561 }1562 psFree(query);1563 query = NULL;1564 1388 psStringAppend(&query, 1565 1389 "INSERT INTO diffInputSkyfile(diff_id, skycell_id, warp1, stack1, warp2, stack2, tess_id) SELECT diff_id, skycell_id, warp1, stack1, warp2, stack2, tess_id from skycellsToDiff"); … … 1613 1437 1614 1438 return true; 1439 #endif 1615 1440 } 1616 1441 #if (0) 1442 static bool definewarpstackMode(pxConfig *config) 1443 { 1444 PS_ASSERT_PTR_NON_NULL(config, false); 1445 1446 psMetadata *expWhere = psMetadataAlloc(); 1447 psMetadata *warp1Where = psMetadataAlloc(); // First set of restrictions on warp 1448 psMetadata *warp2Where = psMetadataAlloc(); // Second set of restriction on warp 1449 psMetadata *stackWhere = psMetadataAlloc(); 1450 1451 PXOPT_COPY_S64(config->args, expWhere, "-exp_id", "exp_id", "=="); 1452 PXOPT_COPY_STR(config->args, expWhere, "-filter", "filter", "=="); 1453 PXOPT_COPY_STR(config->args, expWhere, "-comment", "comment", "LIKE"); 1454 PXOPT_COPY_S64(config->args, warp1Where, "-warp_id", "warpRun.warp_id", "=="); 1455 PXOPT_COPY_STR(config->args, warp1Where, "-warp_label", "warpRun.label", "=="); 1456 PXOPT_COPY_STR(config->args, warp1Where, "-tess_id", "warpRun.tess_id", "=="); 1457 PXOPT_COPY_STR(config->args, warp1Where, "-data_group", "warpRun.data_group", "=="); 1458 PXOPT_COPY_S64(config->args, warp2Where, "-warp_id", "warpRun.warp_id", "=="); 1459 PXOPT_COPY_STR(config->args, warp2Where, "-tess_id", "warpRun.tess_id", "=="); 1460 PXOPT_COPY_STR(config->args, warp2Where, "-data_group", "warpRun.data_group", "=="); 1461 PXOPT_COPY_STR(config->args, warp2Where, "-skycell_id", "warpSkyfile.skycell_id", "=="); 1462 PXOPT_COPY_STR(config->args, warp2Where, "-warp_label", "warpRun.label", "=="); 1463 PXOPT_COPY_F32(config->args, warp2Where, "-good_frac", "warpSkyfile.good_frac", ">="); 1464 PXOPT_COPY_STR(config->args, stackWhere, "-stack_label", "stackRun.label", "=="); 1465 PXOPT_COPY_STR(config->args, stackWhere, "-stack_data_group", "stackRun.data_group", "=="); 1466 1467 PXOPT_LOOKUP_BOOL(bothways, config->args, "-bothways", false); 1468 1469 PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", true, false); // required option 1470 PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false); // option 1471 PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false); // option 1472 PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false); 1473 PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false); 1474 PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false); 1475 PXOPT_LOOKUP_TIME(registered, config->args, "-set_registered", false, false); 1476 1477 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 1478 PXOPT_LOOKUP_BOOL(newTemplates, config->args, "-new-templates", false); 1479 PXOPT_LOOKUP_BOOL(reRun, config->args, "-rerun", false); 1480 PXOPT_LOOKUP_BOOL(available, config->args, "-available", false); 1481 PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false); 1482 1483 // find all things to queue 1484 psString query = pxDataGet("difftool_definewarpstack_part1.sql"); 1485 if (!query) { 1486 psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement"); 1487 return false; 1488 } 1489 1490 psString warp1Query = NULL; 1491 psString warp2Query = NULL; 1492 psString stackQuery = NULL; 1493 psString expQuery = NULL; 1494 1495 if (psListLength(expWhere->list)) { 1496 psString whereClause = psDBGenerateWhereConditionSQL(expWhere, NULL); 1497 psStringAppend(&expQuery, "\n AND %s", whereClause); 1498 psFree(whereClause); 1499 } else { 1500 expQuery = psStringCopy("\n"); 1501 } 1502 psFree(expWhere); 1503 if (psListLength(warp1Where->list)) { 1504 psString whereClause = psDBGenerateWhereConditionSQL(warp1Where, NULL); 1505 psStringAppend(&warp1Query, "\n AND %s", whereClause); 1506 psFree(whereClause); 1507 } else { 1508 warp1Query = psStringCopy("\n"); 1509 } 1510 psFree(warp1Where); 1511 if (psListLength(warp2Where->list)) { 1512 psString whereClause = psDBGenerateWhereConditionSQL(warp2Where, NULL); 1513 psStringAppend(&warp2Query, "\n AND %s", whereClause); 1514 psFree(whereClause); 1515 } else { 1516 warp2Query = psStringCopy("\n"); 1517 } 1518 psFree(warp2Where); 1519 1520 // don't queue for exposures that have already been diff'd unless requested 1521 psString diffQuery = NULL; 1522 if (! (reRun || newTemplates) ) { 1523 psStringAppend(&diffQuery, "\nAND diff_id IS NULL"); 1524 } else { 1525 diffQuery = psStringCopy("\n"); 1526 } 1527 1528 if (psListLength(stackWhere->list)) { 1529 psString whereClause = psDBGenerateWhereConditionSQL(stackWhere, NULL); 1530 psStringAppend(&stackQuery, "\nAND %s", whereClause); 1531 psFree(whereClause); 1532 } else { 1533 stackQuery = psStringCopy(""); 1534 } 1535 psFree(stackWhere); 1536 1537 psTrace("difftool", 1, query, warp1Query, warp2Query, diffQuery, expQuery, stackQuery); 1538 1539 if (!psDBTransaction(config->dbh)) { 1540 psError(PS_ERR_UNKNOWN, false, "database error"); 1541 return false; 1542 } 1543 1544 if (!p_psDBRunQueryF(config->dbh, query, warp1Query, warp2Query, expQuery, diffQuery)) { 1545 psError(PS_ERR_UNKNOWN, false, "database error"); 1546 psFree(query); 1547 if (!psDBRollback(config->dbh)) { 1548 psError(PS_ERR_UNKNOWN, false, "database error"); 1549 } 1550 return false; 1551 } 1552 psFree(query); 1553 1554 psArray *output = p_psDBFetchResult(config->dbh); 1555 if (!output) { 1556 psErrorCode err = psErrorCodeLast(); 1557 switch (err) { 1558 case PS_ERR_DB_CLIENT: 1559 psError(PXTOOLS_ERR_SYS, false, "database error"); 1560 break; 1561 case PS_ERR_DB_SERVER: 1562 psError(PXTOOLS_ERR_PROG, false, "database error"); 1563 break; 1564 default: 1565 psError(PXTOOLS_ERR_PROG, false, "unknown error"); 1566 break; 1567 } 1568 if (!psDBRollback(config->dbh)) { 1569 psError(PS_ERR_UNKNOWN, false, "database error"); 1570 } 1571 return false; 1572 } 1573 if (!psArrayLength(output)) { 1574 psTrace("difftool", PS_LOG_INFO, "no rows found"); 1575 psFree(output); 1576 if (!psDBCommit(config->dbh)) { 1577 psError(PS_ERR_UNKNOWN, false, "database error"); 1578 return false; 1579 } 1580 return true; 1581 } 1582 1583 if (pretend) { 1584 // negative simple so the default is true 1585 if (!ippdbPrintMetadatas(stdout, output, "diffRun", !simple)) { 1586 psError(PS_ERR_UNKNOWN, false, "failed to print array"); 1587 psFree(output); 1588 return false; 1589 } 1590 psFree(output); 1591 return true; 1592 } 1593 1594 // create temporary table 1595 query = pxDataGet("difftool_definewarpstack_temp_create.sql"); 1596 if (!p_psDBRunQuery(config->dbh, query)) { 1597 psError(PS_ERR_UNKNOWN, false, "database error"); 1598 psFree(query); 1599 if (!psDBRollback(config->dbh)) { 1600 psError(PS_ERR_UNKNOWN, false, "database error"); 1601 } 1602 return false; 1603 } 1604 psFree(warp1Query); 1605 psFree(query); 1606 query = NULL; 1607 1608 psString skycell_query = pxDataGet("difftool_definewarpstack_part2.sql"); 1609 1610 psArray *list = psArrayAllocEmpty(16); // List of runs, to print 1611 long numGood = 0; // Number of good rows added 1612 psS64 last_exp_id = 0; 1613 for (long i = 0; i < output->n; i++) { 1614 psMetadata *row = output->data[i]; // Output row from query 1615 bool mdok; // Status of MD lookup 1616 1617 // Take the first warp for each exposure. 1618 // The list is sorted by exposure id and warp_id and the warps are in descending 1619 // order. 1620 psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id"); 1621 if (!mdok) { 1622 psError(PXTOOLS_ERR_PROG, false, "exp_id not found"); 1623 if (!psDBRollback(config->dbh)) { 1624 psError(PS_ERR_UNKNOWN, false, "database error"); 1625 } 1626 return false; 1627 } 1628 if (exp_id == last_exp_id) { 1629 continue; 1630 } 1631 last_exp_id = exp_id; 1632 1633 // clear temporary table 1634 if (!p_psDBRunQuery(config->dbh, "DELETE FROM skycellsToDiff")) { 1635 psError(PS_ERR_UNKNOWN, false, "database error"); 1636 psFree(warp2Query); 1637 psFree(stackQuery); 1638 psFree(skycell_query); 1639 if (!psDBRollback(config->dbh)) { 1640 psError(PS_ERR_UNKNOWN, false, "database error"); 1641 } 1642 return false; 1643 } 1644 psS64 warp_id = psMetadataLookupS64(&mdok, row, "warp_id"); 1645 if (!mdok) { 1646 psError(PXTOOLS_ERR_PROG, false, "warp_id not found --- ignoring row %ld", i); 1647 psFree(warp2Query); 1648 psFree(stackQuery); 1649 psFree(skycell_query); 1650 if (!psDBRollback(config->dbh)) { 1651 psError(PS_ERR_UNKNOWN, false, "database error"); 1652 } 1653 return false; 1654 } 1655 psS64 skycell_count = psMetadataLookupS64(&mdok, row, "skycell_count"); 1656 if (!mdok) { 1657 psError(PXTOOLS_ERR_PROG, false, "skycell_count not found"); 1658 psFree(warp2Query); 1659 psFree(stackQuery); 1660 psFree(skycell_query); 1661 if (!psDBRollback(config->dbh)) { 1662 psError(PS_ERR_UNKNOWN, false, "database error"); 1663 } 1664 return false; 1665 } 1666 psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id"); 1667 if (!mdok) { 1668 psError(PXTOOLS_ERR_PROG, false, "tess_id not found"); 1669 psFree(warp2Query); 1670 psFree(stackQuery); 1671 psFree(skycell_query); 1672 if (!psDBRollback(config->dbh)) { 1673 psError(PS_ERR_UNKNOWN, false, "database error"); 1674 } 1675 return false; 1676 } 1677 1678 psString filter = psMetadataLookupStr(&mdok, row, "filter"); 1679 if (!mdok) { 1680 psError(PXTOOLS_ERR_PROG, false, "filter not found"); 1681 psFree(warp2Query); 1682 psFree(stackQuery); 1683 psFree(skycell_query); 1684 if (!psDBRollback(config->dbh)) { 1685 psError(PS_ERR_UNKNOWN, false, "database error"); 1686 } 1687 return false; 1688 } 1689 1690 psString warp_data_group = psMetadataLookupStr(&mdok, row, "data_group"); 1691 if (!mdok) { 1692 psError(PXTOOLS_ERR_PROG, false, "warp data_group not found"); 1693 psFree(warp2Query); 1694 psFree(stackQuery); 1695 psFree(skycell_query); 1696 if (!psDBRollback(config->dbh)) { 1697 psError(PS_ERR_UNKNOWN, false, "database error"); 1698 } 1699 return false; 1700 } 1701 if (!data_group && warp_data_group) { 1702 data_group = warp_data_group; 1703 } 1704 1705 if (!p_psDBRunQueryF(config->dbh, skycell_query, stackQuery, warp_id, filter, warp2Query)) { 1706 psError(PS_ERR_UNKNOWN, false, "database error"); 1707 psFree(warp2Query); 1708 psFree(stackQuery); 1709 psFree(skycell_query); 1710 if (!psDBRollback(config->dbh)) { 1711 psError(PS_ERR_UNKNOWN, false, "database error"); 1712 } 1713 return false; 1714 } 1715 psS64 num = psDBAffectedRows(config->dbh); 1716 1717 if (num == 0) { 1718 psTrace("difftool", PS_LOG_INFO, "no skycells with stack found for warp_id %" PRId64, warp_id); 1719 continue; 1720 } 1721 1722 if (!available && (num != skycell_count)) { 1723 psTrace("difftool", PS_LOG_INFO, "%" PRId64 " skyfiles with stack found for warp_id %" PRId64 1724 " but need %" PRId64, num, warp_id, skycell_count); 1725 continue; 1726 } 1727 1728 // ok we've got one create the diffRun 1729 diffRunRow *run = diffRunRowAlloc( 1730 0, // ID 1731 "reg", // state 1732 workdir, 1733 label, 1734 data_group ? data_group : label, 1735 dist_group, 1736 reduction, 1737 NULL, // dvodb 1738 registered, 1739 tess_id, 1740 bothways, // bothways (default is false) 1741 true, // exposure 1742 0, // magicked 1743 NULL, // software version 1744 0, // mask stat npix 1745 NAN, // static 1746 NAN, // dynamic 1747 NAN, // magic 1748 NAN, // advisory 1749 IPP_DIFF_MODE_WARP_STACK, 1750 note 1751 ); 1752 1753 if (!diffRunInsertObject(config->dbh, run)) { 1754 psError(PS_ERR_UNKNOWN, false, "database error"); 1755 psFree(run); 1756 if (!psDBRollback(config->dbh)) { 1757 psError(PS_ERR_UNKNOWN, false, "database error"); 1758 } 1759 return false; 1760 } 1761 run->diff_id = psDBLastInsertID(config->dbh); 1762 1763 psStringAppend(&query, "UPDATE skycellsToDiff SET diff_id = %" PRId64, run->diff_id); 1764 if (!p_psDBRunQuery(config->dbh, query)) { 1765 psError(PS_ERR_UNKNOWN, false, "database error"); 1766 psFree(warp2Query); 1767 psFree(stackQuery); 1768 psFree(skycell_query); 1769 psFree(query); 1770 if (!psDBRollback(config->dbh)) { 1771 psError(PS_ERR_UNKNOWN, false, "database error"); 1772 } 1773 return false; 1774 } 1775 psFree(query); 1776 query = NULL; 1777 psStringAppend(&query, 1778 "INSERT INTO diffInputSkyfile(diff_id, skycell_id, warp1, stack1, warp2, stack2, tess_id) SELECT diff_id, skycell_id, warp1, stack1, warp2, stack2, tess_id from skycellsToDiff"); 1779 if (!p_psDBRunQuery(config->dbh, query)) { 1780 psError(PS_ERR_UNKNOWN, false, "database error"); 1781 psFree(warp2Query); 1782 psFree(stackQuery); 1783 psFree(skycell_query); 1784 psFree(query); 1785 if (!psDBRollback(config->dbh)) { 1786 psError(PS_ERR_UNKNOWN, false, "database error"); 1787 } 1788 return false; 1789 } 1790 psFree(query); 1791 query = NULL; 1792 1793 if (!setdiffRunState(config, run->diff_id, "new", false)) { 1794 psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, 1795 run->diff_id); 1796 psFree(warp2Query); 1797 psFree(stackQuery); 1798 psFree(skycell_query); 1799 psFree(query); 1800 if (!psDBRollback(config->dbh)) { 1801 psError(PS_ERR_UNKNOWN, false, "database error"); 1802 } 1803 return false; 1804 } 1805 1806 psArrayAdd(list, list->n, run); 1807 numGood++; 1808 } 1809 psFree(output); 1810 psFree(warp2Query); 1811 psFree(stackQuery); 1812 psFree(skycell_query); 1813 1814 if (!psDBCommit(config->dbh)) { 1815 psError(PS_ERR_UNKNOWN, false, "database error"); 1816 psFree(list); 1817 return false; 1818 } 1819 1820 if (numGood && !diffRunPrintObjects(stdout, list, !simple)) { 1821 psError(PS_ERR_UNKNOWN, false, "failed to print object"); 1822 psFree(list); 1823 return false; 1824 } 1825 psFree(list); 1826 1827 return true; 1828 } 1829 #endif 1617 1830 static bool definewarpwarpMode(pxConfig *config) 1618 1831 { -
branches/czw_branch/20130211/ippconfig/gpc1/ppImage.config
r35115 r35149 159 159 CHIP.VARIANCE.FITS BOOL TRUE # Save chip-mosaic-ed image? 160 160 OVERSCAN BOOL TRUE # Overscan subtraction 161 NONLIN BOOL TRUE # apply non-linearity correction161 NONLIN BOOL FALSE # apply non-linearity correction 162 162 BIAS BOOL FALSE # Bias subtraction 163 163 DARK BOOL TRUE # Dark subtraction … … 169 169 MASK.BUILD BOOL TRUE # Build internal mask? 170 170 MASK.BURNTOOL BOOL TRUE # Mask potential burntool trails 171 PATTERN.ROW BOOL TRUE# Row pattern correction171 PATTERN.ROW BOOL FALSE # Row pattern correction 172 172 PATTERN.CELL BOOL FALSE # Cell pattern correction 173 173 PATTERN.CONTINUITY BOOL TRUE # Cell pattern correction using edge continuity 174 PATTERN.MEDIAN BOOL TRUE 174 175 FRINGE BOOL TRUE # Fringe subtraction 175 176 BIN1.FITS BOOL TRUE # Save 1st binned chip image? … … 1229 1230 CHIP.VARIANCE.FITS BOOL TRUE # Save chip-mosaic-ed image? 1230 1231 OVERSCAN BOOL TRUE # Overscan subtraction 1231 NONLIN BOOL TRUE # apply non-linearity correction1232 NONLIN BOOL TRUE # apply non-linearity correction 1232 1233 BIAS BOOL FALSE # Bias subtraction 1233 1234 DARK BOOL TRUE # Dark subtraction … … 1238 1239 MASK.BUILD BOOL TRUE # Build internal mask? 1239 1240 MASK.BURNTOOL BOOL TRUE # Mask potential burntool trails 1240 PATTERN.ROW BOOL FALSE # Row pattern correction1241 PATTERN.CELL BOOL FALSE # Cell pattern correction1241 PATTERN.ROW BOOL TRUE # Row pattern correction 1242 PATTERN.CELL BOOL TRUE # Cell pattern correction 1242 1243 FRINGE BOOL TRUE # Fringe subtraction 1243 1244 BIN1.FITS BOOL TRUE # Save 1st binned chip image? -
branches/czw_branch/20130211/ippconfig/recipes/ppImage.config
r35081 r35149 30 30 PATTERN.CONTINUITY.WIDTH S32 50 31 31 PATTERN.CONTINUITY.SUBSET BOOL TRUE 32 PATTERN.MEDIAN BOOL FALSE 32 33 FRINGE BOOL FALSE # Fringe subtraction 33 34 PHOTOM BOOL FALSE # Source identification and photometry -
branches/czw_branch/20130211/ppImage/src/ppImage.h
r35081 r35149 42 42 bool doPatternContinuity; // Cell continuity correction 43 43 bool doBackgroundContinuity; // Do mosaic continuity correction 44 bool doPatternMedian; // Do median based pattern correction 44 45 bool doFringe; // Fringe subtraction 45 46 bool doPhotom; // Source identification and photometry -
branches/czw_branch/20130211/ppImage/src/ppImageDetrendPattern.c
r33243 r35149 18 18 pmCell *cell = NULL; 19 19 20 assert(options->doPatternRow || options->doPatternCell || options->doPatternContinuity); // do not call if not needed 20 assert(options->doPatternMedian || options->doPatternRow || 21 options->doPatternCell || options->doPatternContinuity); // do not call if not needed 21 22 assert(inputView->chip != -1); 22 23 assert(inputView->cell == -1); … … 35 36 // chip. 36 37 38 if (options->doPatternMedian) { 39 // bool status; 40 int numCells = chip->cells->n; // Number of cells 41 psVector *tweak = psVectorAlloc(numCells, PS_TYPE_U8); // Tweak cell? 42 // pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT"); 43 pmFPAview *view = pmFPAviewAlloc(0); // View for local processing 44 45 46 pmHDU *hdu = pmHDUFromChip(chip); 47 if (psMetadataLookupBool(NULL,hdu->header, "PTRN_MED")) { 48 psLogMsg("ppImage", PS_LOG_INFO, "Not performing median pattern correction as it has already been done."); 49 goto pattern_row; 50 } 51 for (int i = 0; i < chip->cells->n; i++) { 52 view->cell = i; 53 54 pmCell *cell = chip->cells->data[i]; // Cell of interest 55 56 if (cell->readouts->n > 1) { 57 psLogMsg("ppImage", PS_LOG_INFO, "Not performing cell continuity correction on video cell."); 58 continue; 59 } 60 61 bool doPattern = false; 62 if (!doPatternForView(&doPattern, config, chip, view, RECIPE_NAME, "PATTERN.CONTINUITY.SUBSET")) { 63 ESCAPE(false, "Unable to determine whether row pattern matching should be applied."); 64 } 65 if (doPattern) { 66 tweak->data.U8[i] = 0xFF; 67 } 68 } 69 70 // Tweak the cells 71 if (!pmPatternMedian(chip, tweak, options->patternCellBG, options->patternCellMean, 72 options->maskValue, options->darkMask,true,true)) { 73 psFree(tweak); 74 psFree(view); 75 return false; 76 } 77 psFree(tweak); 78 psFree(view); 79 80 psMetadataAddBool(hdu->header, PS_LIST_TAIL, "PTRN_MED",PS_META_REPLACE,"PATTERN.MEDIAN correction applied",true); 81 82 } 83 pattern_row: 37 84 if (options->doPatternRow) { 38 85 bool status; -
branches/czw_branch/20130211/ppImage/src/ppImageLoop.c
r35081 r35149 174 174 175 175 // Apply the pattern correction 176 if (options->doPatternRow || options->doPatternCell || options->doPatternContinuity) { 176 if (options->doPatternMedian || options->doPatternRow || 177 options->doPatternCell || options->doPatternContinuity) { 177 178 if (!ppImageDetrendPatternApply(config,chip,view,options)) { 178 179 ESCAPE("Unable to apply pattern corrections"); -
branches/czw_branch/20130211/ppImage/src/ppImageOptions.c
r35081 r35149 36 36 options->doPatternContinuity = false; // Cell continuity correction 37 37 options->doBackgroundContinuity = false; // Chip level background continuity correction 38 options->doPatternMedian = false; // Median based pattern correction 38 39 options->doFringe = false; // Fringe subtraction 39 40 options->doPhotom = false; // Source identification and photometry … … 259 260 options->doPatternCell = psMetadataLookupBool(NULL, recipe, "PATTERN.CELL"); 260 261 options->doPatternContinuity = psMetadataLookupBool(NULL, recipe, "PATTERN.CONTINUITY"); 262 options->doPatternMedian = psMetadataLookupBool(NULL, recipe, "PATTERN.MEDIAN"); 261 263 262 264 options->doMaskStats = psMetadataLookupBool(NULL, recipe, "MASK.STATS"); -
branches/czw_branch/20130211/psModules/src/detrend/pmPattern.c
r35081 r35149 4 4 5 5 #include "pmFPA.h" 6 6 #include "pmStack.h" 7 7 #include "pmPattern.h" 8 8 … … 1085 1085 1086 1086 } 1087 psTrace("psModules.detrend.cont",5, " OTA: %d (%d %d) (%d %d) A: %f B: %f C: %f D: %f",1087 psTrace("psModules.detrend.cont",5, "Continuity OTA: %d (%d %d) (%d %d) A: %f B: %f C: %f D: %f", 1088 1088 i,x,y,xParity,yParity, 1089 1089 A->data.F64[y][x],B->data.F64[y][x],C->data.F64[y][x],D->data.F64[y][x]); … … 1302 1302 return true; 1303 1303 } 1304 1305 1306 bool pmPatternMedian(pmChip *chip, const psVector *tweak, psStatsOptions bgStat, psStatsOptions cellStat, 1307 psImageMaskType maskVal, psImageMaskType maskBad, bool doRows, bool doCols) 1308 { 1309 // fprintf(stderr,"Inside pmPatternMedian!\n"); 1310 PS_ASSERT_PTR_NON_NULL(chip, false); 1311 PS_ASSERT_VECTOR_NON_NULL(tweak, false); 1312 PS_ASSERT_VECTOR_SIZE(tweak, chip->cells->n, false); 1313 PS_ASSERT_VECTOR_TYPE(tweak, PS_TYPE_U8, false); 1314 1315 int numCells = tweak->n; // Number of cells 1316 numCells = chip->cells->n; 1317 psStats *bgStats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV); // Statistics on background 1318 psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS); // Random number generator 1319 psVector *cellMedians = psVectorAlloc(numCells, PS_TYPE_F32); // Mask for means 1320 psVector *cellSigmas = psVectorAlloc(numCells, PS_TYPE_F32); 1321 psVector *meanMask = psVectorAlloc(numCells, PS_TYPE_VECTOR_MASK); 1322 // Mask bits 1323 enum { 1324 PM_PATTERN_IGNORE = 0x01, // Ignore this cell 1325 PM_PATTERN_TWEAK = 0x02, // Tweak this cell 1326 PM_PATTERN_ERROR = 0x04, // Error in calculating background 1327 PM_PATTERN_ALL = 0xFF, // All causes 1328 }; 1329 1330 // Count number of cells to tweak 1331 int numTweak = 0; // Number of cells to tweak 1332 int numIgnore = 0; // Number of cells to ignore 1333 psStatsInit(bgStats); 1334 1335 for (int i = 0; i < numCells; i++) { // Calculate cell level statistics, and set ignore mask 1336 pmCell *cell = chip->cells->data[i]; // Cell of interest 1337 1338 if (!cell || !cell->data_exists || !cell->process || 1339 cell->readouts->n == 0 || cell->readouts->n > 1 || !cell->readouts->data[0]) { 1340 numIgnore++; 1341 meanMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PM_PATTERN_IGNORE; 1342 cellMedians->data.F32[i] = NAN; 1343 cellSigmas->data.F32[i] = NAN; 1344 continue; 1345 } 1346 pmReadout *ro = cell->readouts->data[0]; 1347 if (tweak->data.U8[i]) { 1348 meanMask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PM_PATTERN_TWEAK; 1349 numTweak++; 1350 } 1351 if (!psImageBackground(bgStats, NULL, ro->image, ro->mask, maskVal, rng)) { 1352 psWarning("Unable to measure background for cell %d\n", i); 1353 psErrorClear(); 1354 meanMask->data.PS_TYPE_VECTOR_MASK_DATA[i] |= PM_PATTERN_ERROR; 1355 continue; 1356 } 1357 cellMedians->data.F32[i] = bgStats->robustMedian; 1358 cellSigmas->data.F32[i] = bgStats->robustStdev; 1359 } 1360 if (numTweak == 0) { 1361 // fprintf(stderr,"Inside pmPatternMedian but I'm claiming nothing to do!\n"); 1362 // Nothing to do 1363 psFree(meanMask); 1364 return true; 1365 } 1366 1367 int num = 8; 1368 1369 psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_QUARTILE); 1370 // psArray *inputs = psArrayAlloc(num); 1371 psVector *inputs = psVectorAlloc(num,PS_TYPE_S32); 1372 psVector *pixelData = psVectorAlloc(inputs->n,PS_TYPE_F32); 1373 psVector *pixelMask = psVectorAlloc(inputs->n,PS_TYPE_VECTOR_MASK); 1374 1375 for (int mode = 0; mode <= 1; mode++) { 1376 if ((mode == 0)&&(!doRows)) { 1377 continue; 1378 } 1379 if ((mode == 1)&&(!doCols)) { 1380 continue; 1381 } 1382 for (int i = 0; i < num; i++) { // For each row of cells 1383 int k = 0; 1384 int numvalid = 0; 1385 for (int j = 0; j < numCells; j++) { // Scan the cell list for matches for this row. 1386 if (meanMask->data.PS_TYPE_VECTOR_MASK_DATA[j] & PM_PATTERN_IGNORE) { 1387 continue; 1388 } 1389 if (!(meanMask->data.PS_TYPE_VECTOR_MASK_DATA[j] & PM_PATTERN_TWEAK)) { 1390 continue; 1391 } 1392 1393 pmCell *cell = chip->cells->data[j]; 1394 const char *cellName = psMetadataLookupStr(NULL,cell->concepts, "CELL.NAME"); 1395 int x = cellName[2] - '0'; 1396 int y = cellName[3] - '0'; 1397 if (((mode == 0)&&(doRows)&&(y == i))|| 1398 ((mode == 1)&&(doCols)&&(x == i))) { 1399 inputs->data.S32[k] = j; // Add the index to the list of inputs. 1400 k++; 1401 numvalid++; 1402 } 1403 } 1404 // Allocate the median image and mask 1405 pmCell *exCell = chip->cells->data[inputs->data.S32[0]]; 1406 pmReadout *example = exCell->readouts->data[0]; 1407 pmReadout *median = pmReadoutAlloc(NULL); 1408 median->image = psImageAlloc(example->image->numCols, 1409 example->image->numRows, 1410 PS_TYPE_F32); 1411 median->mask = psImageAlloc(example->image->numCols, 1412 example->image->numRows, 1413 PS_TYPE_IMAGE_MASK); 1414 // Block to do median. 1415 { 1416 psImageMaskType badMask = 0x7F; // Configuable would be better. 1417 for (int y = 0 ; y < example->image->numRows; y++) { 1418 for (int x = 0; x < example->image->numCols; x++) { 1419 int badVal = 0; 1420 for (k = 0; k < numvalid; k++) { 1421 pmCell *cell = chip->cells->data[inputs->data.S32[k]]; 1422 pmReadout *ro = cell->readouts->data[0]; 1423 psImage *image = ro->image; 1424 psImage *mask = ro->mask; 1425 1426 pixelData->data.F32[k] = image->data.F32[y][x]; 1427 pixelMask->data.PS_TYPE_VECTOR_MASK_DATA[k] = 0; 1428 //#if (0) 1429 double cell_median = cellMedians->data.F32[inputs->data.S32[k]]; 1430 double cell_sigma = cellSigmas->data.F32[inputs->data.S32[k]]; 1431 if (fabs(pixelData->data.F32[k] - cell_median) > (3.0 * cell_sigma)) { 1432 pixelMask->data.PS_TYPE_VECTOR_MASK_DATA[k] = 1; 1433 badVal++; 1434 } 1435 //#endif 1436 if (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & badMask) { 1437 pixelMask->data.PS_TYPE_VECTOR_MASK_DATA[k] = 1; 1438 badVal++; 1439 } 1440 } 1441 for (k = numvalid; k < num; k++) { 1442 pixelMask->data.PS_TYPE_VECTOR_MASK_DATA[k] = 1; 1443 badVal++; 1444 } 1445 if (badVal >= num) { 1446 median->image->data.F32[y][x] = NAN; 1447 } 1448 else { 1449 if (!psVectorStats(stats,pixelData,NULL,pixelMask,1)) { 1450 // Die gracefully. 1451 return(false); 1452 } 1453 int worst_k = -1; 1454 double worst_v = 0; 1455 for (k = 0; k < num; k++) { 1456 if (pixelMask->data.PS_TYPE_VECTOR_MASK_DATA[k] == 0) { 1457 double v = fabs(stats->sampleMedian - pixelData->data.F32[k]); 1458 if (v > worst_v) { 1459 worst_v = v; 1460 worst_k = k; 1461 } 1462 } 1463 } 1464 if (worst_k != -1) { 1465 pixelMask->data.PS_TYPE_VECTOR_MASK_DATA[worst_k] = 1; 1466 if (!psVectorStats(stats,pixelData,NULL,pixelMask,1)) { 1467 // Die gracefully. 1468 return(false); 1469 } 1470 } 1471 median->image->data.F32[y][x] = stats->sampleMedian; 1472 } 1473 } 1474 } 1475 } // End median block 1476 1477 double correction_median = 0; 1478 if (!psImageBackground(bgStats, NULL, median->image, NULL, maskVal, rng)) { 1479 psWarning("Unable to measure background for cell section %d\n", i); 1480 psErrorClear(); 1481 } 1482 correction_median = bgStats->robustMedian; 1483 for (int v = 0; v < median->image->numRows; v++) { 1484 for (int u = 0; u < median->image->numCols; u++) { 1485 median->image->data.F32[v][u] -= correction_median; 1486 } 1487 } 1488 1489 // Block to subtract median 1490 for (int j = 0; j < numCells; j++) { 1491 if (meanMask->data.PS_TYPE_VECTOR_MASK_DATA[j] & PM_PATTERN_IGNORE) { 1492 continue; 1493 } 1494 if (!(meanMask->data.PS_TYPE_VECTOR_MASK_DATA[j] & PM_PATTERN_TWEAK)) { 1495 continue; 1496 } 1497 1498 pmCell *cell = chip->cells->data[j]; 1499 1500 const char *cellName = psMetadataLookupStr(NULL,cell->concepts, "CELL.NAME"); 1501 int x = cellName[2] - '0'; 1502 int y = cellName[3] - '0'; 1503 if (((mode == 0)&&(doRows)&&(y == i))|| 1504 ((mode == 1)&&(doCols)&&(x == i))) { 1505 psLogMsg("psModules.detrend",PS_LOG_DETAIL,"PATTERN.MEDIAN mode: %d Cell Statistics Pre: %d %d %d %s %g %g\n", 1506 mode, j,x,y,cellName,cellMedians->data.F32[j],cellSigmas->data.F32[j]); 1507 pmReadout *ro = cell->readouts->data[0]; 1508 psImage *im = ro->image; 1509 for (int v = 0; v < im->numRows; v++) { 1510 for (int u = 0; u < im->numCols; u++) { 1511 if (isfinite(median->image->data.F32[v][u])) { 1512 1513 im->data.F32[v][u] -= (median->image->data.F32[v][u]);// - correction_median); 1514 } 1515 } 1516 } 1517 if (!psImageBackground(bgStats, NULL, ro->image, ro->mask, maskVal, rng)) { 1518 psWarning("Unable to measure background for cell %d\n", j); 1519 psErrorClear(); 1520 meanMask->data.PS_TYPE_VECTOR_MASK_DATA[i] |= PM_PATTERN_ERROR; 1521 continue; 1522 } 1523 psLogMsg("psModules.detrend",PS_LOG_DETAIL,"PATTERN.MEDIAN mode: %d Cell Statistics Post: %d %d %d %s %g %g\n", 1524 mode, j,x,y,cellName,bgStats->robustMedian,bgStats->robustStdev); 1525 cellMedians->data.F32[j] = bgStats->robustMedian; 1526 cellSigmas->data.F32[j] = bgStats->robustStdev; 1527 } 1528 } // End subtraction block 1529 // Write out the median? 1530 #if (1) 1531 const char *expname = psMetadataLookupStr(NULL,chip->hdu->header,"FILENAME"); 1532 char filename[256]; 1533 pmCell *exCell2 = chip->cells->data[inputs->data.S32[0]]; 1534 snprintf(filename,sizeof(filename), 1535 "/home/panstarrs/watersc1/pattern_median.20130201/tests/%s.%d.%d.fits", 1536 expname,mode,i); 1537 psFits *fits = psFitsOpen(filename,"w"); 1538 if (!fits) { 1539 fprintf(stderr,"Couldn't write\n"); 1540 } 1541 if (!psFitsWriteImage(fits,exCell2->hdu->header,median->image,0,NULL)) { 1542 fprintf(stderr,"Couldn't write2\n"); 1543 } 1544 if (!psFitsClose(fits)) { 1545 fprintf(stderr,"Couldn't write3\n"); 1546 } 1547 #endif 1548 1549 psFree(median->image); 1550 psFree(median->mask); 1551 psFree(median); 1552 } 1553 } 1554 psFree(bgStats); 1555 psFree(rng); 1556 psFree(cellMedians); 1557 psFree(cellSigmas); 1558 psFree(meanMask); 1559 psFree(stats); 1560 psFree(inputs); 1561 psFree(pixelData); 1562 psFree(pixelMask); 1563 1564 1565 return true; 1566 } -
branches/czw_branch/20130211/psModules/src/detrend/pmPattern.h
r35081 r35149 84 84 ); 85 85 86 /// Fix the pattern noise using medians 87 bool pmPatternMedian( 88 pmChip *chip, ///< Chip to correct 89 const psVector *tweak, ///< U8 vector indicating whether to tweak the corresponding cell 90 psStatsOptions bgStat, ///< Statistic to use for background measurement 91 psStatsOptions cellStat, ///< Statistic to use for combination of cell background measurementsn 92 psImageMaskType maskVal, ///< Mask value to use 93 psImageMaskType maskBad, ///< Mask value to give bad pixels 94 bool doRows, ///< Do correction in cell rows 95 bool doCols ///< Do correction in cell columns 96 ); 97 98 86 99 87 100
Note:
See TracChangeset
for help on using the changeset viewer.
