Changeset 27839 for branches/simmosaic_branches/pswarp/src/pswarpLoop.c
- Timestamp:
- May 3, 2010, 8:45:22 AM (16 years ago)
- Location:
- branches/simmosaic_branches
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
pswarp/src/pswarpLoop.c (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/simmosaic_branches
- Property svn:mergeinfo changed
-
branches/simmosaic_branches/pswarp/src/pswarpLoop.c
r24491 r27839 16 16 17 17 #define WCS_NONLIN_TOL 0.001 // Non-linear tolerance for header WCS 18 #define PSPHOT_FIND_PSF 1 // Use psphot's findPSF function?19 18 #define TESTING 0 // Testing output? 20 19 21 20 22 // XXX these are generic functions which should be moved to psModules 23 // Activate a list of files 24 static void fileActivation(pmConfig *config, // Configuration 25 char **files, // Files to turn on/off 26 bool state // Activation state 27 ) 28 { 29 for (int i = 0; files[i] != NULL; i++) { 30 pmFPAfileActivate(config->files, state, files[i]); 31 } 32 return; 33 } 34 35 /** 36 * Run down the FPA hierarchy, checking files 37 */ 38 static bool ioChecksBefore(pmConfig *config ///< Configuration 39 ) 40 { 41 pmFPAview *view = pmFPAviewAlloc(0); ///< View for checking 42 bool status = true; ///< Status of checks 43 status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE); 44 view->chip = 0; 45 status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE); 46 view->cell = 0; 47 status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE); 48 psFree(view); 49 return status; 50 } 51 52 /** 53 * Run up the FPA hierarchy, checking files 54 */ 55 static bool ioChecksAfter(pmConfig *config ///< Configuration 56 ) 57 { 58 pmFPAview *view = pmFPAviewAlloc(0); ///< View for checking 59 view->chip = view->cell = 0; 60 bool status = true; ///< Status of checks 61 status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 62 view->cell = -1; 63 status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 64 view->chip = -1; 65 status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 66 psFree(view); 67 return status; 68 } 69 70 71 /** 72 * Loop over the inputs, warp them to the output skycell and then write out the output. 73 */ 74 bool pswarpLoop(pmConfig *config) 21 22 // Loop over the inputs, warp them to the output skycell and then write out the output. 23 bool pswarpLoop(pmConfig *config, psMetadata *stats) 75 24 { 76 25 bool status; 26 bool mdok; // Status of MD lookup 77 27 78 28 const char *skyCamera = psMetadataLookupStr(NULL, config->arguments, 79 "SKYCELL.CAMERA"); // /<Name of camera for skycell29 "SKYCELL.CAMERA"); // Name of camera for skycell 80 30 pmConfigCamerasCull(config, skyCamera); 81 31 pmConfigRecipesCull(config, "PSWARP,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG"); 82 83 32 84 33 // load the recipe 85 34 psMetadata *recipe = psMetadataLookupPtr (&status, config->recipes, PSWARP_RECIPE); 86 35 if (!recipe) { 87 psError(PS PHOT_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);36 psError(PSWARP_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE); 88 37 return false; 89 38 } 90 39 91 40 if (!pswarpSetMaskBits(config)) { 92 psError( PS_ERR_IO, false, "failed to set mask bits");41 psError(psErrorCodeLast(), false, "failed to set mask bits"); 93 42 return NULL; 94 43 } … … 112 61 113 62 if (astrom->camera != input->camera) { 114 psError(PS _ERR_UNKNOWN, true, "Input camera and astrometry camera do not match.");63 psError(PSWARP_ERR_DATA, true, "Input camera and astrometry camera do not match."); 115 64 return false; 116 65 } … … 128 77 psFree (view); 129 78 130 bool mdok; ///< Status of MD lookup131 const char *statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); ///< Filename for statistics132 psMetadata *stats = NULL; ///< Container for statistics133 FILE *statsFile = NULL; ///< File stream for statistics134 if (mdok && statsName && strlen(statsName) > 0) {135 psString resolved = pmConfigConvertFilename(statsName, config, true, true);136 statsFile = fopen(resolved, "w");137 if (!statsFile) {138 psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);139 psFree(resolved);140 return false;141 }142 psFree(resolved);143 stats = psMetadataAlloc();144 psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", 0, "No problems", 0);145 }146 79 147 80 // Turn all skycell files on to generate them, and then turn them off for the loop over the input images 148 81 // the input, which is in a different format. 149 82 { 150 fileActivation(config, detectorFiles, false); 151 fileActivation(config, photFiles, false); 152 fileActivation(config, independentFiles, false); 153 fileActivation(config, skycellFiles, true); 154 ioChecksBefore(config); 155 fileActivation(config, skycellFiles, false); 83 pswarpFileActivation(config, detectorFiles, false); 84 pswarpFileActivation(config, photFiles, false); 85 pswarpFileActivation(config, independentFiles, false); 86 pswarpFileActivation(config, skycellFiles, true); 87 if (!pswarpIOChecksBefore(config)) { 88 psError(psErrorCodeLast(), false, "Unable to read files."); 89 goto DONE; 90 } 91 pswarpFileActivation(config, skycellFiles, false); 156 92 } 157 93 … … 163 99 pmChip *chip; 164 100 pmFPAview *view = pmFPAviewAlloc(0); 165 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE); 101 if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) { 102 psError(psErrorCodeLast(), false, "Unable to read files."); 103 goto DONE; 104 } 166 105 while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) { 167 106 psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process); 168 107 if (!chip->process || !chip->file_exists) { continue; } 169 pmFPAfileIOChecks(config, view, PM_FPA_BEFORE); 108 if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) { 109 psError(psErrorCodeLast(), false, "Unable to read files."); 110 goto DONE; 111 } 170 112 pmCell *cell; 171 113 while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) { 172 114 psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process); 173 115 if (!cell->process || !cell->file_exists) { continue; } 174 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE); 175 pmFPAfileIOChecks (config, view, PM_FPA_AFTER); 176 } 177 pmFPAfileIOChecks (config, view, PM_FPA_AFTER); 178 } 179 pmFPAfileIOChecks (config, view, PM_FPA_AFTER); 180 psFree(view); 181 182 fileActivation(config, detectorFiles, true); 116 if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE) || 117 !pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { 118 psError(psErrorCodeLast(), false, "Unable to read files."); 119 goto DONE; 120 } 121 } 122 if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { 123 psError(psErrorCodeLast(), false, "Unable to write files."); 124 goto DONE; 125 } 126 } 127 if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { 128 psError(psErrorCodeLast(), false, "Unable to write files."); 129 goto DONE; 130 } 131 psFree(view); 132 133 pswarpFileActivation(config, detectorFiles, true); 183 134 pmFPAfileActivate(config->files, false, "PSWARP.ASTROM"); 184 135 } … … 202 153 if (bilevelAstrometry) { 203 154 if (!pmAstromReadBilevelMosaic(input->fpa, phu->header)) { 204 psError( PS_ERR_UNKNOWN, false, "Unable to read bilevel mosaic astrometry for input FPA.");155 psError(psErrorCodeLast(), false, "Unable to read bilevel mosaic astrometry for input FPA."); 205 156 psFree(view); 206 157 psFree(stats); 207 return false;158 goto DONE; 208 159 } 209 160 } … … 212 163 213 164 // files associated with the science image 214 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE); 165 if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) { 166 psError(psErrorCodeLast(), false, "Unable to read files."); 167 goto DONE; 168 } 215 169 216 170 pmChip *chip; … … 218 172 psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process); 219 173 if (!chip->process || !chip->file_exists) { continue; } 220 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE); 174 if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) { 175 psError(psErrorCodeLast(), false, "Unable to read files."); 176 goto DONE; 177 } 221 178 222 179 // read WCS data from the corresponding header … … 224 181 if (bilevelAstrometry) { 225 182 if (!pmAstromReadBilevelChip (chip, hdu->header)) { 226 psError( PS_ERR_UNKNOWN, false, "Unable to read bilevel chip astrometry for input FPA.");183 psError(psErrorCodeLast(), false, "Unable to read bilevel chip astrometry for input FPA."); 227 184 psFree(view); 228 185 psFree(stats); 229 return false;186 goto DONE; 230 187 } 231 188 } else { 232 189 // we use a default FPA pixel scale of 1.0 233 190 if (!pmAstromReadWCS (input->fpa, chip, hdu->header, 1.0)) { 234 psError( PS_ERR_UNKNOWN, false, "Unable to read WCS astrometry for input FPA.");191 psError(psErrorCodeLast(), false, "Unable to read WCS astrometry for input FPA."); 235 192 psFree(view); 236 193 psFree(stats); 237 return false;194 goto DONE; 238 195 } 239 196 } … … 243 200 psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process); 244 201 if (!cell->process || !cell->file_exists) { continue; } 245 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE); 202 if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) { 203 psError(psErrorCodeLast(), false, "Unable to read files."); 204 goto DONE; 205 } 246 206 247 207 psListAdd(cells, PS_LIST_TAIL, cell); … … 250 210 pmReadout *readout; 251 211 while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) { 252 pmFPAfileIOChecks(config, view, PM_FPA_BEFORE); 212 if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) { 213 psError(psErrorCodeLast(), false, "Unable to read files."); 214 goto DONE; 215 } 253 216 if (!readout->data_exists) { 254 217 continue; 255 218 } 256 219 257 // Copy the sources from the astrometry carrier to the input, so they can be accessed by220 // Copy the detections from the astrometry carrier to the input, so they can be accessed by 258 221 // pswarpTransformReadout 259 222 pmReadout *astromRO = pmFPAviewThisReadout(view, astrom->fpa); // Readout for astrometry 260 psArray *sources = psMetadataLookupPtr(&mdok, astromRO->analysis, 261 "PSPHOT.SOURCES"); // Sources from astrometry 262 if (sources) { 263 psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY, 264 "Sources from input astrometry", sources); 223 pmDetections *detections = psMetadataLookupPtr(&mdok, astromRO->analysis, "PSPHOT.DETECTIONS"); // Sources from astrometry 224 if (detections) { 225 psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_ARRAY, "Sources from input astrometry", detections); 265 226 } 266 227 267 228 pswarpTransformReadout(output, readout, config); 268 229 269 pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 270 } 271 pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 272 } 273 pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 230 if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) { 231 psError(psErrorCodeLast(), false, "Unable to write files."); 232 goto DONE; 233 } 234 } 235 if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) { 236 psError(psErrorCodeLast(), false, "Unable to write files."); 237 goto DONE; 238 } 239 } 240 if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) { 241 psError(psErrorCodeLast(), false, "Unable to write files."); 242 goto DONE; 243 } 274 244 } 275 245 … … 291 261 292 262 if (!pswarpPixelsLit(output, stats, config)) { 293 psError( PS_ERR_UNKNOWN, false, "Unable to calculate pixel regions.");263 psError(psErrorCodeLast(), false, "Unable to calculate pixel regions."); 294 264 psFree(cells); 295 265 psFree(view); 296 return false;266 goto DONE; 297 267 } 298 268 … … 314 284 } 315 285 316 317 // Set variance factor318 {319 bool mdok; // Status of MD lookup320 float varFactor = psMetadataLookupF32(&mdok, output->analysis, PSWARP_ANALYSIS_VARFACTOR);321 long goodPix = psMetadataLookupS64(&mdok, output->analysis, PSWARP_ANALYSIS_GOODPIX);322 varFactor /= goodPix;323 324 psMetadataItem *vfItem = psMetadataLookup(outCell->concepts, "CELL.VARFACTOR"); ///< Item to update325 psAssert(vfItem && vfItem->type == PS_TYPE_F32, "Concept should be as specified.");326 if (!isfinite(vfItem->data.F32)) {327 vfItem->data.F32 = varFactor;328 } else {329 vfItem->data.F32 *= varFactor;330 }331 }332 286 333 287 // Set covariance matrix for output … … 344 298 345 299 if (!pmConceptsAverageCells(outCell, cells, NULL, NULL, false)) { 346 psError( PS_ERR_UNKNOWN, false, "Unable to average cell concepts.");300 psError(psErrorCodeLast(), false, "Unable to average cell concepts."); 347 301 psFree(stats); 348 302 psFree(cells); 349 303 psFree(view); 350 return false;304 goto DONE; 351 305 } 352 306 psFree(cells); … … 356 310 357 311 if (!psMetadataCopy(outFPA->concepts, input->fpa->concepts)) { 358 psError( PS_ERR_UNKNOWN, false, "Unable to copy FPA concepts from input to output.");312 psError(psErrorCodeLast(), false, "Unable to copy FPA concepts from input to output."); 359 313 psFree(stats); 360 314 psFree(view); 361 return false; 315 goto DONE; 316 } 317 318 // Update ZP from the astrometry 319 { 320 psMetadataItem *item = psMetadataLookup(outFPA->concepts, "FPA.ZP"); 321 item->data.F32 = psMetadataLookupF32(NULL, astrom->fpa->concepts, "FPA.ZP"); 362 322 } 363 323 … … 372 332 pmHDU *skyHDU = pmHDUFromCell(cell); ///< HDU 373 333 if (!skyHDU) { 374 psError(PS _ERR_UNEXPECTED_NULL, false, "Unable to find skycell HDU.");334 psError(PSWARP_ERR_DATA, false, "Unable to find skycell HDU."); 375 335 psFree(view); 376 return false;336 goto DONE; 377 337 } 378 338 hdu->header = psMetadataCopy(hdu->header, skyHDU->header); … … 382 342 383 343 if (!pmAstromWriteWCS(hdu->header, outFPA, outChip, WCS_NONLIN_TOL)) { 384 psError( PS_ERR_UNKNOWN, false, "Unable to generate WCS header.");344 psError(psErrorCodeLast(), false, "Unable to generate WCS header."); 385 345 psFree(stats); 386 return false; 387 } 388 389 pmFPAfileIOChecks(config, view, PM_FPA_AFTER); 346 goto DONE; 347 } 348 349 if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) { 350 psError(psErrorCodeLast(), false, "Unable to write files."); 351 goto DONE; 352 } 390 353 391 354 // Done with the detector side of things 392 fileActivation(config, detectorFiles, false);393 fileActivation(config, independentFiles, false);355 pswarpFileActivation(config, detectorFiles, false); 356 pswarpFileActivation(config, independentFiles, false); 394 357 395 358 // We need a new PSF model for the warped frame. It would be good to generate this analytically, but 396 359 // that's going to be tricky. We have a list of sources, so we use those to redetermine the PSF model. 397 360 398 if (psMetadataLookupBool(&mdok, config->arguments, "PSF")) { 399 fileActivation(config, photFiles, true); 400 ioChecksBefore(config); 361 if (psMetadataLookupBool(&mdok, recipe, "PSF")) { 362 pswarpFileActivation(config, photFiles, true); 363 if (!pswarpIOChecksBefore(config)) { 364 psError(psErrorCodeLast(), false, "Unable to read files."); 365 goto DONE; 366 } 401 367 402 368 // supply the readout and fpa of interest to psphot … … 407 373 view->chip = view->cell = view->readout = 0; 408 374 409 // Need to adjust the weight --- the main operation in psphotReadoutFindPSF is not determining the410 // signficance of sources, but looking at the significance of individual pixels.411 // We can adjust the weight directly since this is a deep copy412 pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa);413 float vf = psMetadataLookupF32(NULL, photRO->parent->concepts, "CELL.VARFACTOR"); // Variance factor414 psBinaryOp(photRO->variance, photRO->variance, "*", psScalarAlloc(vf, PS_TYPE_F32));415 416 375 // grab the sources of interest from the storage location (pmFPAfile PSPHOT.INPUT.CMF) 417 376 psArray *sources = psphotLoadPSFSources (config, view); 418 377 if (!sources) { 419 psError(PS_ERR_UNKNOWN, false, "No sources supplied to measure PSF"); 420 return false; 421 } 378 psError(psErrorCodeLast(), false, "No sources supplied to measure PSF"); 379 goto DONE; 380 } 381 382 pmModelClassSetLimits(PM_MODEL_LIMITS_STRICT); 422 383 423 384 // measure the PSF using these sources … … 434 395 psphotFilesActivate(config, false); 435 396 } 397 398 // Ensure seeing is carried over 399 pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa); // Chip with seeing 400 psMetadataItem *item = psMetadataLookup(outChip->concepts, "CHIP.SEEING"); // Concept with seeing 401 item->data.F32 = psMetadataLookupF32(NULL, photChip->concepts, "CHIP.SEEING"); 436 402 437 403 // XXX EAM : put this in a visualization function … … 457 423 } 458 424 459 fileActivation(config, skycellFiles, true);460 461 425 // Perform statistics on the output image 462 426 if (stats) { … … 480 444 psFree(view); 481 445 482 // Ensure everything is written out, at every level483 ioChecksAfter(config);484 485 // Now done with the skycell side of things486 487 446 DONE: 488 489 // Write out summary statistics490 if (stats) {491 psMetadataAddF32(stats, PS_LIST_TAIL, "DT_WARP", 0, "Time for warp completion",492 psTimerMark("pswarp"));493 494 const char *statsMDC = psMetadataConfigFormat(stats);495 if (!statsMDC) {496 psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");497 psFree(stats);498 fclose(statsFile);499 return false;500 }501 fprintf(statsFile, "%s", statsMDC);502 psFree((void*)statsMDC);503 fclose(statsFile);504 pmConfigRunFilenameAddWrite(config, "STATS", statsName);505 506 psFree(stats);507 }508 509 // Dump configuration510 psString dump_file = psMetadataLookupStr(&status, config->arguments, "DUMP_CONFIG");511 if (dump_file) {512 pmConfigDump(config, dump_file);513 }514 447 515 448 return true;
Note:
See TracChangeset
for help on using the changeset viewer.
