Changeset 5717 for trunk/pois/src/pois.c
- Timestamp:
- Dec 6, 2005, 6:43:52 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/pois/src/pois.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pois/src/pois.c
r3813 r5717 7 7 8 8 #include <stdio.h> 9 #include <string.h> 9 10 #include <sys/time.h> 10 11 #include "pslib.h" … … 25 26 int main(int argc, char **argv) 26 27 { 27 poisErrorRegister();28 29 28 double startTime = getTime(); 30 29 31 30 // Set trace levels 32 31 psTraceSetLevel(".", 0); 33 psTraceSetLevel("pois", 10);34 psTraceSetLevel("pois.config", 10);35 psTraceSetLevel("pois.time", 10);36 psTraceSetLevel("pois.solution", 0);37 psTraceSetLevel("pois.kernelBasisFunctions", 10);38 psTraceSetLevel("pois.calculateEquation", 10);39 psTraceSetLevel("pois.convolveImage", 10);40 psTraceSetLevel("pois.findStamps", 10);41 psTraceSetLevel("pois.parseConfig", 10);42 psTraceSetLevel("pois.makeMask", 10);43 psTraceSetLevel("pois.extractKernel", 10);44 psTraceSetLevel("pois.calculateDeviations", 10);45 46 // Set logging level47 psLogSetLevel(9);48 32 49 33 // Parse the command line 50 34 poisConfig *config = poisParseConfig(argc, argv); // Configuration values 51 35 36 if (config->verbose) { 37 psTraceSetLevel("pois", 10); 38 psTraceSetLevel("pois.config", 10); 39 psTraceSetLevel("pois.time", 10); 40 psTraceSetLevel("pois.solution", 0); 41 psTraceSetLevel("pois.kernelBasisFunctions", 10); 42 psTraceSetLevel("pois.calculateEquation", 10); 43 psTraceSetLevel("pois.convolveImage", 10); 44 psTraceSetLevel("pois.findStamps", 10); 45 psTraceSetLevel("pois.parseConfig", 10); 46 psTraceSetLevel("pois.makeMask", 10); 47 psTraceSetLevel("pois.extractKernel", 10); 48 psTraceSetLevel("pois.calculateDeviations", 10); 49 psTraceSetLevel("pois.checkKernel", 10); 50 if (config->stampFile) { 51 psTraceSetLevel("pois.checkStamp", 10); 52 } else { 53 psTraceSetLevel("pois.checkStamp", 0); 54 } 55 // Set logging level 56 psLogSetLevel(9); 57 } 58 52 59 // Read inputs 53 psRegion *imageRegion = psRegionAlloc(0, 0, 0, 0); 60 psMetadata *header = NULL; // Header for output image 61 62 psRegion imageRegion = {0, 0, 0, 0}; 54 63 psFits *reference = psFitsAlloc(config->refFile); 55 //psMetadata *refHeader = psFitsReadHeader(NULL, reference); 56 psImage *refImage = psFitsReadImage(NULL, reference, *imageRegion, 0); 64 if (config->reverse) { 65 header = psFitsReadHeader(NULL, reference); 66 } 67 psImage *refImage = psFitsReadImage(NULL, reference, imageRegion, 0); 57 68 if (refImage == NULL) { 58 69 psErrorStackPrint(stderr, "Fatal error: unable to read %s\n", config->refFile); … … 61 72 psTrace("pois", 3, "Read %s: %dx%d\n", config->refFile, refImage->numCols, refImage->numRows); 62 73 psFree(reference); 74 // Convert to 32-bit floating point, in necessary 75 if (refImage->type.type != PS_TYPE_F32) { 76 psTrace("pois", 3, "Converting %s to floating point in memory....\n", config->refFile); 77 psImage *temp = psImageCopy(NULL, refImage, PS_TYPE_F32); 78 psFree(refImage); 79 refImage = temp; 80 } 63 81 64 82 psFits *input = psFitsAlloc(config->inFile); 65 //psMetadata *inHeader = psFitsReadHeader(NULL, input); 66 psImage *inImage = psFitsReadImage(NULL, input, *imageRegion, 0); 83 if (! config->reverse) { 84 header = psFitsReadHeader(NULL, input); 85 } 86 psImage *inImage = psFitsReadImage(NULL, input, imageRegion, 0); 67 87 if (inImage == NULL) { 68 88 psErrorStackPrint(stderr, "Fatal error: unable to read %s\n", config->inFile); … … 71 91 psTrace("pois", 3, "Read %s: %dx%d\n", config->inFile, inImage->numCols, inImage->numRows); 72 92 psFree(input); 73 psFree(imageRegion); 93 // Convert to 32-bit floating point, in necessary 94 if (inImage->type.type != PS_TYPE_F32) { 95 psTrace("pois", 3, "Converting %s to floating point in memory....\n", config->inFile); 96 psImage *temp = psImageCopy(NULL, inImage, PS_TYPE_F32); 97 psFree(inImage); 98 inImage = temp; 99 } 74 100 75 101 if ((refImage->numCols != inImage->numCols) || (refImage->numRows != inImage->numRows)) { … … 102 128 // Write the mask out 103 129 psFits *maskFile = psFitsAlloc("mask.fits"); 104 if (!psFitsWriteImage(maskFile, NULL, mask, 0 , NULL)) {130 if (!psFitsWriteImage(maskFile, NULL, mask, 0)) { 105 131 psErrorStackPrint(stderr, "Unable to write mask: mask.fits\n"); 106 132 } … … 110 136 111 137 psArray *stamps = NULL; // Array of stamps 112 psVector *stampMask = NULL; // Mask for stamps113 138 psVector *solution = NULL; // Solution vector 114 139 140 FILE *stampsFP = NULL; // File pointer for stamps 141 if (config->stampFile) { 142 psTrace("pois", 5, "Opening stamp file, %s\n", config->stampFile); 143 stampsFP = fopen(config->stampFile, "r"); 144 if (! stampsFP) { 145 psError(PS_ERR_IO, true, "Unable to open stamps file, %s!\n", config->stampFile); 146 exit(EXIT_FAILURE); 147 } 148 } 149 115 150 // Iterate for a good solution 116 psList *badStamps = NULL; // Do we have bad stamps, such that we need to continue to iterate?151 bool badStamps = true; // Do we have bad stamps, such that we need to continue to iterate? 117 152 for (int iterNum = 0; iterNum < config->numIter && badStamps; iterNum++) { 118 153 psTrace("pois", 1, "Iteration %d...\n", iterNum); 119 154 120 psFree(badStamps); // left over from last iteration121 122 155 // Find stamps 123 stamps = poisFindStamps(stamps, refImage, mask, config); 156 if (config->stampFile) { 157 stamps = poisReadStamps(stamps, &stampsFP, refImage, mask, config); 158 } else { 159 stamps = poisFindStamps(stamps, refImage, mask, config); 160 } 124 161 int numStamps = 0; 125 162 for (int s = 0; s < stamps->n; s++) { … … 159 196 psImage *kernelImage = poisExtractKernel(solution, kernelBasisFunctions, 0.0, 0.0, config); 160 197 psFits *kernelFile = psFitsAlloc(kernelName); 161 if (!psFitsWriteImage(kernelFile, NULL, kernelImage, 0 , NULL)) {198 if (!psFitsWriteImage(kernelFile, NULL, kernelImage, 0)) { 162 199 psErrorStackPrint(stderr, "Unable to write kernel: %s\n", kernelName); 163 200 } … … 177 214 // If there was rejection on the last round, re-solve the equation using only the good stamps 178 215 if (badStamps) { 179 psFree(badStamps);180 216 solution = poisSolveEquation(solution, stamps, config); 217 } 218 219 if (stampsFP) { 220 fclose(stampsFP); 181 221 } 182 222 … … 185 225 for (int i = 0; i < mask->numCols; i++) { 186 226 if (mask->data.U8[j][i] & POIS_MASK_STAMP) { 187 mask->data.U8[j][i] = POIS_MASK_OK; 188 } 189 } 190 } 227 mask->data.U8[j][i] &= ~POIS_MASK_STAMP; 228 } 229 } 230 } 231 232 // Check the result 233 (void)poisCheckKernel(solution, kernelBasisFunctions, config); 191 234 192 235 // Convolve the input image … … 199 242 snprintf(convName, MAXCHAR, "%s.conv", config->outFile); 200 243 psFits *convFile = psFitsAlloc(convName); 201 if (!psFitsWriteImage(convFile, NULL, convImage, 0 , NULL)) {244 if (!psFitsWriteImage(convFile, NULL, convImage, 0)) { 202 245 psErrorStackPrint(stderr, "Unable to write convolved image: %s\n", convName); 203 246 } … … 208 251 // Do the subtraction 209 252 psImage *subImage = psImageAlloc(config->xImage, config->yImage, PS_TYPE_F32); 210 (void)psBinaryOp(subImage, inImage, "-", convImage); 253 if (config->reverse) { 254 (void)psBinaryOp(subImage, convImage, "-", inImage); 255 } else { 256 (void)psBinaryOp(subImage, inImage, "-", convImage); 257 } 211 258 psTrace("pois.time", 1, "Subtraction completed at %f sec\n", getTime() - startTime); 212 259 … … 231 278 } 232 279 } 233 234 for (int y = 0; y < config->yKernel; y++) {235 for (int x = 0; x < subImage->numCols; x++) {236 subImage->data.F32[y][x] = 0.0;237 }238 }239 for (int y = subImage->numRows - config->yKernel; y < subImage->numRows; y++) {240 for (int x = 0; x < subImage->numCols; x++) {241 subImage->data.F32[y][x] = 0.0;242 }243 }244 } 280 } 281 for (int y = 0; y < config->yKernel; y++) { 282 for (int x = 0; x < subImage->numCols; x++) { 283 subImage->data.F32[y][x] = 0.0; 284 } 285 } 286 for (int y = subImage->numRows - config->yKernel; y < subImage->numRows; y++) { 287 for (int x = 0; x < subImage->numCols; x++) { 288 subImage->data.F32[y][x] = 0.0; 289 } 290 } 291 245 292 246 293 // Mask out bad pixels … … 253 300 } 254 301 302 if (config->mask) { 303 char maskName[80]; // Name of mask image 304 sprintf(maskName, "%s.mask", config->outFile); 305 psFits *maskFile = psFitsAlloc(maskName); 306 if (!psFitsWriteImage(maskFile, NULL, mask, 0)) { 307 psErrorStackPrint(stderr, "Unable to write image: %s\n", maskName); 308 } 309 psTrace("pois", 1, "Mask image written to %s\n", maskName); 310 psFree(maskFile); 311 } 312 313 314 int numNaN = psImageClipNaN(subImage, 0.0); 315 if (numNaN > 0) { 316 printf("Masked %d NAN pixels to zero.\n", numNaN); 317 } 318 255 319 psFits *subFile = psFitsAlloc(config->outFile); 256 if (!psFitsWriteImage(subFile, NULL, subImage, 0, NULL)) {320 if (!psFitsWriteImage(subFile, header, subImage, 0)) { 257 321 psErrorStackPrint(stderr, "Unable to write subtracted image: %s\n", config->outFile); 258 322 } … … 261 325 psTrace("pois.time", 1, "I/O completed at %f sec\n", getTime() - startTime); 262 326 263 264 327 // Clean up 265 (void)psFree(kernelBasisFunctions); 266 (void)psFree(refImage); 267 (void)psFree(inImage); 268 (void)psFree(convImage); 269 (void)psFree(subImage); 270 (void)psFree(config); 328 psFree(header); 329 psFree(kernelBasisFunctions); 330 psFree(refImage); 331 psFree(inImage); 332 psFree(convImage); 333 psFree(subImage); 334 psFree(config); 271 335 } 336
Note:
See TracChangeset
for help on using the changeset viewer.
