Changeset 5651 for trunk/archive/scripts/src/phase2/pmFPAMorph.c
- Timestamp:
- Nov 30, 2005, 6:03:51 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/archive/scripts/src/phase2/pmFPAMorph.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/scripts/src/phase2/pmFPAMorph.c
r5633 r5651 3 3 #include <assert.h> 4 4 #include "pslib.h" 5 #include "papmodule.h" 5 6 #include "papStuff.h" 7 #include "pmFPA.h" 8 #include "pmFPAMorph.h" 6 9 7 10 #define MAX(x,y) ((x) > (y) ? (x) : (y)) … … 23 26 } 24 27 break; 28 case PS_TYPE_F32: 29 for (int y = 0; y < image->numRows; y++) { 30 for (int x = 0; x < image->numCols; x++) { 31 flipped->data.F32[y][size - x - 1] = image->data.F32[y][x]; 32 } 33 } 34 break; 25 35 default: 26 36 psError(PS_ERR_IO, true, "Image type %x not yet supported for flip.\n", image->type.type); … … 51 61 } 52 62 63 64 #if 0 53 65 // Return a list of the region strings and the method for interpreting them 54 66 static psList *getRegions(psString *method, // Method for evaluating the regions … … 67 79 return regionStrings; 68 80 } 81 #endif 82 83 84 // Splice an image into another image 85 static psRegion *spliceImage(psImage *target, // Target image onto which to splice 86 int *x0, int *y0, // Starting coordinates to splice to 87 psImage *source, // Source image from which to splice 88 const psRegion *from, // Region to splice from 89 int readdir, // Read direction 90 bool xFlip, bool yFlip // Flip x and y axes? 91 ) 92 { 93 psImage *toSplice = psImageSubset(source, *from); // Subimage, to splice into target 94 if (xFlip) { 95 int size = 0; // Size of flipped image 96 if (readdir == 1) { 97 size = toSplice->numCols; 98 } else if (readdir == 2) { 99 size = target->numCols; 100 } 101 psImage *temp = xFlipImage(toSplice, size); 102 psFree(toSplice); 103 toSplice = temp; 104 } 105 if (yFlip) { 106 int size = 0; // Size of flipped image 107 if (readdir == 1) { 108 size = target->numRows; 109 } else if (readdir == 2) { 110 size = toSplice->numRows; 111 } 112 psImage *temp = yFlipImage(toSplice, size); 113 psFree(toSplice); 114 toSplice = temp; 115 } 116 117 (void)psImageOverlaySection(target, toSplice, *x0, *y0, "="); 118 119 if (readdir == 1) { 120 *x0 += toSplice->numCols; 121 } else if (readdir == 2) { 122 *y0 += toSplice->numRows; 123 } 124 125 // Return the region where we pasted the image 126 psRegion *outRegion = psAlloc(sizeof(psRegion)); 127 // Addding 1 to get FITS standard 128 outRegion->x0 = *x0 + 1; 129 outRegion->x1 = *x0 + toSplice->numCols; 130 outRegion->y0 = *y0 + 1; 131 outRegion->y1 = *y0 + toSplice->numRows; 132 133 psFree(toSplice); 134 return outRegion; 135 } 136 137 // Splice bias regions (overscans) 138 static bool spliceBias(psImage *splice, // Image to which to splice 139 int *x0, int *y0, // Starting position for splice 140 const pmCell *in, // Input cell 141 pmCell *out, // Output cell 142 int xFlip, int yFlip, // Flip the image? 143 int readNum, // Number of readout 144 int readdir // Read direction 145 ) 146 { 147 psArray *readouts = in->readouts; // The readouts; 148 pmReadout *readout = readouts->data[readNum]; // The readout of interest 149 psList *inBiassecs = psMetadataLookupPtr(NULL, in->concepts, "CELL.BIASSEC"); // List of input biassecs 150 psListIterator *inBiassecsIter = psListIteratorAlloc(inBiassecs, PS_LIST_HEAD, false); 151 psRegion *inBiassec = NULL; // BIASSEC from list 152 psList *outBiassecs = psListAlloc(NULL); // List of biassecs for output 153 while (inBiassec = psListGetAndIncrement(inBiassecsIter)) { 154 psRegion *outBiassec = spliceImage(splice, x0, y0, readout->image, inBiassec, readdir, xFlip, 155 yFlip); 156 psListAdd(outBiassecs, PS_LIST_TAIL, outBiassec); 157 } 158 psFree(inBiassecsIter); 159 psMetadataAdd(out->concepts, PS_LIST_TAIL, "CELL.BIASSEC", PS_DATA_LIST | PS_META_REPLACE, 160 "BIASSEC from pmFPAMorph", outBiassecs); 161 162 return true; 163 } 164 69 165 70 166 // Splice a list of cells together … … 79 175 psTrace(__func__, 5, "Splicing together all cells in the bucket.\n"); 80 176 81 if (inCells-> size != outCells->size) {177 if (inCells->n != outCells->n) { 82 178 psError(PS_ERR_IO, true, "Sizes of input (%d) and output (%d) lists of cells don't match.\n", 83 inCells-> size, outCells->size);179 inCells->n, outCells->n); 84 180 return NULL; 85 181 } 86 182 87 int numCells = inCells-> size;// Number of cells183 int numCells = inCells->n; // Number of cells 88 184 int readdir = 0; // Read direction for CCD; currently unknown 89 185 int numReadouts = 0; // Number of readouts in a cell … … 97 193 psVector *yFlip = psVectorAlloc(numCells, PS_TYPE_U8); // Do we need to flip in y? 98 194 195 196 // We go through the cells to make sure everything's kosher, and to get the sizes. 99 197 psListIterator *inCellsIter = psListIteratorAlloc(inCells, PS_LIST_HEAD, false); // Iterator for cells 100 p apCell *inCell = NULL; // Cell from list of input cells198 pmCell *inCell = NULL; // Cell from list of input cells 101 199 psListIterator *outCellsIter = psListIteratorAlloc(outCells, PS_LIST_HEAD, false); // Iterator for cells 102 200 int cellNum = 0; // Cell number; … … 104 202 psArray *readouts = inCell->readouts; // The readouts comprising the cell 105 203 106 // Check the read direction204 // Get and check the read direction 107 205 int cellReaddir = psMetadataLookupS32(NULL, inCell->concepts, "CELL.READDIR"); 108 206 if (readdir == 0) { 207 // Zero means unknown; set it. 109 208 readdir = cellReaddir; 110 209 } else if (readdir != cellReaddir) { 111 psError(PS_ERR_IO, true, "Trying to splice cells with different read directions (%d vs %d) \n",112 readdir, cellReaddir);210 psError(PS_ERR_IO, true, "Trying to splice cells with different read directions (%d vs %d). I " 211 "don't know how to do that!\n", readdir, cellReaddir); 113 212 // XXX Clean up before returning 114 213 return NULL; 115 214 } 116 // Check the number of readouts 215 216 // Get and check the number of readouts 117 217 if (! spliced) { 118 218 numReadouts = readouts->n; … … 131 231 } 132 232 133 // Get the cell parities134 p apCell *outCell = psListGetAndIncrement(outCellsIter); // Corresponding output cell135 int xParityIn = psMetadataLookupS32(NULL, inCell , "CELL.XPARITY");136 int yParityIn = psMetadataLookupS32(NULL, inCell , "CELL.YPARITY");137 int xParityOut = psMetadataLookupS32(NULL, outCell , "CELL.XPARITY");138 int yParityOut = psMetadataLookupS32(NULL, outCell , "CELL.YPARITY");233 // Get and check the cell parities 234 pmCell *outCell = psListGetAndIncrement(outCellsIter); // Corresponding output cell 235 int xParityIn = psMetadataLookupS32(NULL, inCell->concepts, "CELL.XPARITY"); 236 int yParityIn = psMetadataLookupS32(NULL, inCell->concepts, "CELL.YPARITY"); 237 int xParityOut = psMetadataLookupS32(NULL, outCell->concepts, "CELL.XPARITY"); 238 int yParityOut = psMetadataLookupS32(NULL, outCell->concepts, "CELL.YPARITY"); 139 239 if (xParityIn == 0 || xParityOut == 0 || yParityIn == 0 || yParityOut == 0) { 140 240 psError(PS_ERR_IO, false, "Unable to determine parity of cell!\n"); … … 159 259 // Calculate the sizes of the spliced images 160 260 for (int i = 0; i < numReadouts; i++) { 161 p apReadout *readout = readouts->data[i]; // The readout of interest261 pmReadout *readout = readouts->data[i]; // The readout of interest 162 262 psImage *image = readout->image; // The image pixels 163 psList *overscans = readout->overscans; // The overscan regions164 263 165 264 // Check image type … … 173 272 } 174 273 274 psRegion *trimsec = psMetadataLookupPtr(NULL, inCell->concepts, "CELL.TRIMSEC"); // Trim section 275 psList *biassecs = psMetadataLookupPtr(NULL, inCell->concepts, "CELL.BIASSEC"); // Bias section 276 psListIterator *biassecsIter = psListIteratorAlloc(biassecs, PS_LIST_HEAD, false); // Iterator 277 psRegion *biassec = NULL; // Bias section from list iteration 278 175 279 // Get the size of the spliced image 176 280 if (readdir == 1) { 177 psTrace(__func__, 9, "Image size: %dx%d\n", image->numCols, image->numRows); 178 xSize->data.S32[i] += image->numCols; 179 ySize->data.S32[i] = MAX(ySize->data.S32[i], image->numRows); 281 xSize->data.S32[i] += trimsec->x1 - trimsec->x0; 282 ySize->data.S32[i] = MAX(ySize->data.S32[i], trimsec->y1 - trimsec->y0); 283 while (biassec = psListGetAndIncrement(biassecsIter)) { 284 xSize->data.S32[i] += biassec->x1 - biassec->x0; 285 ySize->data.S32[i] = MAX(ySize->data.S32[i], biassec->y1 - biassec->y0); 286 } 180 287 } else if (readdir == 2) { 181 psTrace(__func__, 9, "Image size: %dx%d\n", image->numCols, image->numRows); 182 ySize->data.S32[i] += image->numRows; 183 xSize->data.S32[i] = MAX(xSize->data.S32[i], image->numCols); 288 ySize->data.S32[i] += trimsec->y1 - trimsec->y0; 289 xSize->data.S32[i] = MAX(xSize->data.S32[i], trimsec->x1 - trimsec->x0); 290 while (biassec = psListGetAndIncrement(biassecsIter)) { 291 ySize->data.S32[i] += biassec->y1 - biassec->y0; 292 xSize->data.S32[i] = MAX(ySize->data.S32[i], biassec->x1 - biassec->x0); 293 } 184 294 } else { 185 295 psError(PS_ERR_IO, true, "Invalid read direction: %d\n", readdir); … … 187 297 return NULL; 188 298 } 189 190 psListIterator *overscansIter = psListIteratorAlloc(overscans, PS_LIST_HEAD, false); 191 psImage *overscan = NULL; // Overscan image 192 while (overscan = psListGetAndIncrement(overscansIter)) { 193 194 // Check image type 195 if (type == 0) { 196 type = image->type.type; 197 } else if (image->type.type != type) { 198 psError(PS_ERR_IO, true, "Trying to splice readouts with different image types " 199 "(%d vs %d)\n", type, image->type.type); 200 // XXX Clean up before returning 201 return NULL; 202 } 203 204 // Get the size of the spliced image 205 if (readdir == 1) { 206 psTrace(__func__, 9, "Overscan size: %dx%d\n", image->numCols, image->numRows); 207 xSize->data.S32[i] += overscan->numCols; 208 ySize->data.S32[i] = MAX(ySize->data.S32[i], overscan->numRows); 209 } else if (readdir == 2) { 210 psTrace(__func__, 9, "Overscan size: %dx%d\n", image->numCols, image->numRows); 211 ySize->data.S32[i] += overscan->numRows; 212 xSize->data.S32[i] = MAX(xSize->data.S32[i], overscan->numCols); 213 } 214 } 215 216 } 299 psFree(biassecsIter); 300 } 301 302 // Copy the concepts over 303 psMetadataCopy(outCell->concepts, inCell->concepts); 217 304 } 218 305 psFree(inCellsIter); 219 306 psFree(outCellsIter); 307 220 308 221 309 // Make sure all the readouts have the same size … … 234 322 for (int i = 0; i < numReadouts; i++) { 235 323 psImage *splice = psImageAlloc(numCols, numRows, type); // The spliced image 236 324 int x0 = 0, y0 = 0; // Position at which the splice begins 325 326 // Position-dependent overscans first 327 if (posDep) { 328 for (int cellNum = 0; cellNum < inCells->n / 2; cellNum++) { 329 pmCell *inCell = psListGet(inCells, cellNum); // Input cell of interest 330 pmCell *outCell = psListGet(outCells, cellNum); // Output cell of interest 331 spliceBias(splice, &x0, &y0, inCell, outCell, xFlip->data.U8[i], yFlip->data.U8[i], i, 332 readdir); 333 } 334 } 335 336 // Then the images 237 337 psListIterator *inCellsIter = psListIteratorAlloc(inCells, PS_LIST_HEAD, false);// Iterator for cells 238 p apCell *inCell = NULL; // Cell from the list of cells338 pmCell *inCell = NULL; // Cell from the list of cells 239 339 psListIterator *outCellsIter = psListIteratorAlloc(outCells, PS_LIST_HEAD, false); // Iterator 240 340 int cellNum = 0; // Cell number 241 int x0 = 0, y0 = 0; // Position at which the splice begins242 341 while (inCell = psListGetAndIncrement(inCellsIter)) { 243 342 psArray *readouts = inCell->readouts; // The readouts comprising the cell 244 papReadout *readout = readouts->data[i]; // The specific readout of interest 245 psImage *image = readout->image; // The image pixels 246 psList *overscans = readout->overscans; // The overscan regions 247 papCell *outCell = psListGetAndIncrement(outCellsIter); // Output cell 248 249 psImage *toSplice = psMemIncrRefCounter(image); // Image to splice in 250 if (xFlip->data.U8[cellNum]) { 251 int size = 0; // Size of flipped image 252 if (readdir == 1) { 253 size = toSplice->numCols; 254 } else if (readdir == 2) { 255 size = numCols; 256 } 257 psImage *temp = xFlipImage(toSplice, size); 258 psFree(toSplice); 259 toSplice = temp; 260 } 261 if (yFlip->data.U8[cellNum]) { 262 int size = 0; // Size of flipped image 263 if (readdir == 1) { 264 size = numRows; 265 } else if (readdir == 2) { 266 size = toSplice->numRows; 267 } 268 psImage *temp = yFlipImage(toSplice, size); 269 psFree(toSplice); 270 toSplice = temp; 271 } 272 273 (void)psImageOverlaySection(splice, toSplice, x0, y0, "="); 274 275 if (readdir == 1) { 276 x0 += toSplice->numCols; 277 } else if (readdir == 2) { 278 y0 += toSplice->numRows; 279 } 280 281 // Update the TRIMSEC for the output cell 282 psString trimsecString = psMetadataLookupString(NULL, outCell, "CELL.TRIMSEC"); 283 psString trimsecMethod = NULL; // Method of determining trimsec 284 psList *trimsecs = getRegions(&trimsecMethod, trimsecString); // List of trimsecs 285 if (strncmp(trimsecMethod, "HEADER", 6) != 0 && strncmp(trimsecMethod, "HD", 2) != 0) { 286 psError(PS_ERR_IO, true, "Can only splice cells if the CELL.TRIMSEC (= %s) is determined " 287 "from the header.\n", trimsecString); 288 // XXX Clean up before returning 289 return NULL; 290 } 291 if (trimsecs->size != 1) { 292 psError(PS_ERR_IO, true, "Multiple headers specified for CELL.TRIMSEC: %s\n", trimsecString); 293 // XXX Clean up before returning 294 return NULL; 295 } 296 psString trimsecName = trimsecs->head->data; // Grab header for TRIMSEC off the list 297 psFree(trimsecs); 298 299 // TRIMSEC region; add 1 to get FITS standard 300 psRegion trimsecRegion = {x0 + 1, x0 + toSplice->numCols, y0 + 1, y0 + toSplice->numRows}; 301 psString trimsecValue = psRegionToString(trimsecRegion); 302 psMetadataAddStr(header, PS_LIST_TAIL, trimsecName, 0, "TRIMSEC from pmFPAMorph", trimsecValue); 303 // XXX Does psMetadataAddStr overwrite a previous value? 304 305 // Repeat the above for each overscan (flip, overlay, update BIASSEC) 306 // Prepare the BIASSEC 307 psString biassecString = psMetadataLookupString(NULL, outCell, "CELL.BIASSEC"); 308 psString biassecMethod = NULL; // Method of determining biassec 309 psList *biassecs = getRegions(&biassecMethod, biassecString); // List of biassecs 310 if (strncmp(biassecMethod, "HEADER", 6) != 0 && strncmp(biassecMethod, "HD", 2) != 0) { 311 psError(PS_ERR_IO, true, "Can only splice cells if the CELL.BIASSEC (= %s) is determined " 312 "from the header.\n", biassecString); 313 // XXX Clean up before returning 314 return NULL; 315 } 316 if (biassecs->size != overscans->size); 317 318 psListIterator *overscansIter = psListIteratorAlloc(overscans, PS_LIST_HEAD, false); 319 psImage *overscan = NULL; // Overscan from list 320 psListIterator *biassecsIter = psListIteratorAlloc(biassecs, PS_LIST_HEAD, false); 321 psString biassecName = NULL;// FITS header keyword for biassec 322 while ((overscan = psListGetAndIncrement(overscansIter)) && 323 (biassecName = psListGetAndIncrement(biassecsIter))) { 324 // Splice the overscan on 325 psImage *toSplice = psMemIncrRefCounter(overscan); // Image to splice in 326 if (xFlip->data.U8[cellNum]) { 327 int size = 0; // Size of flipped image 328 if (readdir == 1) { 329 size = toSplice->numCols; 330 } else if (readdir == 2) { 331 size = numCols; 332 } 333 psImage *temp = xFlipImage(toSplice, size); 334 psFree(toSplice); 335 toSplice = temp; 336 } 337 if (yFlip->data.U8[cellNum]) { 338 int size = 0; // Size of flipped image 339 if (readdir == 1) { 340 size = numRows; 341 } else if (readdir == 2) { 342 size = toSplice->numRows; 343 } 344 psImage *temp = yFlipImage(toSplice, size); 345 psFree(toSplice); 346 toSplice = temp; 347 } 348 349 (void)psImageOverlaySection(splice, toSplice, x0, y0, "="); 350 351 if (readdir == 1) { 352 x0 += toSplice->numCols; 353 } else if (readdir == 2) { 354 y0 += toSplice->numRows; 355 } 356 357 // Update the header 358 psRegion biassecRegion = {x0 + 1, x0 + toSplice->numCols, y0 + 1, y0 + toSplice->numRows}; 359 psString biassecValue = psRegionToString(biassecRegion); 360 psMetadataAddStr(header, PS_LIST_TAIL, biassecName, 0, "BIASSEC from pmFPAMorph", 361 biassecValue); 362 } 363 psFree(overscansIter); 364 psFree(biassecsIter); 365 psFree(biassecs); 366 } // Iterating over input cells 343 pmReadout *readout = readouts->data[i]; // The specific readout of interest 344 pmCell *outCell = psListGetAndIncrement(outCellsIter); // Output cell 345 psRegion *inTrimsec = psMetadataLookupPtr(NULL, inCell->concepts, "CELL.TRIMSEC"); // TRIMSEC 346 psRegion *outTrimsec = spliceImage(splice, &x0, &y0, readout->image, inTrimsec, readdir, 347 xFlip->data.U8[cellNum], yFlip->data.U8[cellNum]); 348 349 // Update the output TRIMSEC 350 psMetadataAdd(outCell->concepts, PS_LIST_TAIL, "CELL.TRIMSEC", PS_DATA_UNKNOWN | PS_META_REPLACE, 351 "TRIMSEC from pmFPAMorph", outTrimsec); 352 } 353 psFree(inCellsIter); 354 psFree(outCellsIter); 355 356 // Then the biases 357 if (! posDep) { 358 // Need to only do half the biases 359 for (int cellNum = inCells->n / 2; cellNum < inCells->n; cellNum++) { 360 pmCell *inCell = psListGet(inCells, cellNum); // Input cell of interest 361 pmCell *outCell = psListGet(outCells, cellNum); // Output cell of interest 362 spliceBias(splice, &x0, &y0, inCell, outCell, xFlip->data.U8[i], yFlip->data.U8[i], i, 363 readdir); 364 } 365 } else { 366 // Need to do all the biases 367 for (int cellNum = 0; cellNum < inCells->n; cellNum++) { 368 pmCell *inCell = psListGet(inCells, cellNum); // Input cell of interest 369 pmCell *outCell = psListGet(outCells, cellNum); // Output cell of interest 370 spliceBias(splice, &x0, &y0, inCell, outCell, xFlip->data.U8[i], yFlip->data.U8[i], i, 371 readdir); 372 } 373 } 367 374 368 375 spliced->data[i] = splice; 376 369 377 } // Iterating over readouts 370 378 … … 426 434 psListAdd(sourceCells, PS_LIST_TAIL, fromCell); 427 435 428 int xParityIn = psMetadataLookupS32(NULL, fromCell, "CELL.XPARITY"); 429 int yParityIn = psMetadataLookupS32(NULL, fromCell, "CELL.YPARITY"); 430 int xParityOut = psMetadataLookupS32(NULL, toCell, "CELL.XPARITY"); 431 int yParityOut = psMetadataLookupS32(NULL, toCell, "CELL.YPARITY"); 436 #if 1 437 int xParityIn = psMetadataLookupS32(NULL, fromCell->concepts, "CELL.XPARITY"); 438 int yParityIn = psMetadataLookupS32(NULL, fromCell->concepts, "CELL.YPARITY"); 439 int xParityOut = psMetadataLookupS32(NULL, toCell->concepts, "CELL.XPARITY"); 440 int yParityOut = psMetadataLookupS32(NULL, toCell->concepts, "CELL.YPARITY"); 432 441 psTrace(__func__, 3, "Chip %d, cell %d: in (%d,%d) out (%d,%d)\n", i, j, xParityIn, yParityIn, 433 442 xParityOut, yParityOut); 443 #endif 434 444 435 445 // Copy the cell contents over 436 446 toCell->readouts = psMemIncrRefCounter(fromCell->readouts); 437 447 438 if (toCell-> private && strlen(toCell->private->extname) > 0) {448 if (toCell->hdu && strlen(toCell->hdu->extname) > 0) { 439 449 // Splice the component cells 440 p_pmHDU *hdu = toCell-> private;450 p_pmHDU *hdu = toCell->hdu; 441 451 if (! hdu->header) { 442 452 hdu->header = psMetadataAlloc(); 443 453 } 444 hdu-> pixels = spliceCells(hdu->header,targetCells, sourceCells, positionDependent);454 hdu->images = spliceCells(targetCells, sourceCells, positionDependent); 445 455 // Purge the lists 446 456 while (psListRemove(targetCells, PS_LIST_TAIL)); … … 450 460 } 451 461 452 if (toChip-> private && strlen(toChip->private->extname) > 0) {462 if (toChip->hdu && strlen(toChip->hdu->extname) > 0) { 453 463 // Splice the component cells 454 p_pmHDU *hdu = toChip-> private;464 p_pmHDU *hdu = toChip->hdu; 455 465 if (! hdu->header) { 456 466 hdu->header = psMetadataAlloc(); 457 467 } 458 hdu-> pixels = spliceCells(hdu->header, targetCells, sourceCells);468 hdu->images = spliceCells(targetCells, sourceCells, positionDependent); 459 469 // Purge the lists 460 470 while (psListRemove(targetCells, PS_LIST_TAIL)); … … 464 474 } 465 475 476 if (toFPA->hdu && strlen(toFPA->hdu->extname) > 0) { 477 // Splice the component cells 478 p_pmHDU *hdu = toFPA->hdu; 479 if (! hdu->header) { 480 hdu->header = psMetadataAlloc(); 481 } 482 hdu->images = spliceCells(targetCells, sourceCells, positionDependent); 483 // Purge the lists 484 while (psListRemove(targetCells, PS_LIST_TAIL)); 485 while (psListRemove(sourceCells, PS_LIST_TAIL)); 486 } 487 466 488 return true; 467 489 } 468
Note:
See TracChangeset
for help on using the changeset viewer.
