Changeset 5743 for trunk/stac/src/shift.c
- Timestamp:
- Dec 7, 2005, 2:57:14 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/stac/src/shift.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/stac/src/shift.c
r3673 r5743 11 11 { 12 12 fprintf (stderr, "shift: shift an image, given the transformation\n" 13 "Usage: %s [-h] [-v] [-s NX NY] IN OUT\n"14 "where\n"15 "\t-h Help (this info)\n"16 "\t-v Increase verbosity level\n"17 "\t-s NX NY Size of output image\n"18 "\tIN Input image, which has associated .map file\n"19 "\tOUT Output image\n",20 programName21 );13 "Usage: %s [-h] [-v] [-s NX NY] IN OUT\n" 14 "where\n" 15 "\t-h Help (this info)\n" 16 "\t-v Increase verbosity level\n" 17 "\t-s NX NY Size of output image\n" 18 "\tIN Input image, which has associated .map file\n" 19 "\tOUT Output image\n", 20 programName 21 ); 22 22 } 23 23 … … 38 38 39 39 // Parameters with default values 40 int outnx = 0, outny = 0; // Size of output image 41 int verbose = 0; // Verbosity level 40 int outnx = 0, outny = 0; // Size of output image 41 int verbose = 0; // Verbosity level 42 float bad = 0.0; // Bad level 42 43 43 44 // Parse command line 44 const char *programName = argv[0]; // Program name45 const char *programName = argv[0]; // Program name 45 46 /* Variables for getopt */ 46 47 int opt; /* Option, from getopt */ … … 49 50 50 51 /* Parse command-line arguments using getopt */ 51 while ((opt = getopt(argc, argv, "hv s:")) != -1) {52 while ((opt = getopt(argc, argv, "hvb:s:")) != -1) { 52 53 switch (opt) { 53 case 'h':54 help(programName);55 exit(EXIT_SUCCESS);56 case 'v':54 case 'h': 55 help(programName); 56 exit(EXIT_SUCCESS); 57 case 'v': 57 58 verbose++; 58 59 break; 59 case 's':60 case 's': 60 61 if ((sscanf(argv[optind-1], "%d", &outnx) != 1) || (sscanf(argv[optind++], "%d", &outny) != 1)) { 61 62 // Note: incrementing optind, so I can read more than one parameter. … … 63 64 exit(EXIT_FAILURE); 64 65 } 65 break; 66 default: 67 help(programName); 68 exit(EXIT_FAILURE); 69 } 66 break; 67 case 'b': 68 if (sscanf(optarg, "%f", &bad) != 1) { 69 help(programName); 70 exit(EXIT_FAILURE); 71 } 72 break; 73 default: 74 help(programName); 75 exit(EXIT_FAILURE); 76 } 70 77 } 71 78 … … 77 84 exit(EXIT_FAILURE); 78 85 } 79 const char *inName = argv[0]; // Input filename80 const char *outName = argv[1]; // Output filename86 const char *inName = argv[0]; // Input filename 87 const char *outName = argv[1]; // Output filename 81 88 82 psFits *imageFile = psFitsAlloc(inName); 83 psRegion *imageRegion = psRegionAlloc(0, 0, 0, 0); // Region of image to read 84 psImage *image = psFitsReadImage(NULL, imageFile, *imageRegion, 0); 89 psFits *imageFile = psFitsOpen(inName, "r"); 90 psRegion imageRegion = {0, 0, 0, 0}; // Region of image to read 91 psMetadata *header = psFitsReadHeader(NULL, imageFile); // FITS header 92 psImage *image = psFitsReadImage(NULL, imageFile, imageRegion, 0); 85 93 if (image == NULL) { 86 psErrorStackPrint(stderr, "Fatal error: Unable to read %s\n", inName);87 exit(EXIT_FAILURE);94 psErrorStackPrint(stderr, "Fatal error: Unable to read %s\n", inName); 95 exit(EXIT_FAILURE); 88 96 } 89 97 psTrace("shift", 4, "Image %s is %dx%d\n", inName, image->numCols, image->numRows); 90 98 // Convert to 32-bit floating point, in necessary 91 99 if (image->type.type != PS_TYPE_F32) { 92 psTrace("shift", 5, "Converting %s to floating point in memory....", inName);93 psImage *temp = psImageCopy(NULL, image, PS_TYPE_F32);94 psFree(image);95 image = temp;100 psTrace("shift", 5, "Converting %s to floating point in memory....", inName); 101 psImage *temp = psImageCopy(NULL, image, PS_TYPE_F32); 102 psFree(image); 103 image = temp; 96 104 } 97 psFree(imageFile); 98 psFree(imageRegion); 105 psFitsClose(imageFile); 106 107 // Generate masks 108 psImage *mask = psImageAlloc(image->numCols, image->numRows, PS_TYPE_U8); 109 for (int y = 0; y < image->numRows; y++) { 110 for (int x = 0; x < image->numCols; x++) { 111 if (image->data.F32[y][x] <= bad) { 112 mask->data.U8[y][x] = 1; 113 } else { 114 mask->data.U8[y][x] = 0; 115 } 116 } 117 } 99 118 100 119 // Read map 101 char mapName[MAXCHAR]; // Name of map file120 char mapName[MAXCHAR]; // Name of map file 102 121 sprintf(mapName, "%s.map", inName); 103 122 psPlaneTransform *map = stacReadMap(mapName); 104 123 105 124 // Functions work on array of images, so: 106 psArray *images = psArrayAlloc(1); // An array of one 107 psArray *maps = psArrayAlloc(1);// An array of one 125 psArray *images = psArrayAlloc(1); // An array of one 126 psArray *maps = psArrayAlloc(1); // An array of one 127 psArray *masks = psArrayAlloc(1); // An array of one 108 128 images->data[0] = image; 109 129 maps->data[0] = map; 130 masks->data[0] = mask; 110 131 111 132 // Get size 112 133 if (outnx == 0 || outny == 0) { 113 stacSize(&outnx, &outny, NULL, NULL, images, maps);134 stacSize(&outnx, &outny, NULL, NULL, images, maps); 114 135 } 115 136 … … 119 140 // Transform inputs and errors 120 141 psArray *transformed = NULL; 121 (void)stacTransform(&transformed, NULL, images, inverseMaps, NULL, NULL, NULL, NULL, NULL, outnx, outny); 142 (void)stacTransform(&transformed, NULL, images, inverseMaps, NULL, masks, NULL, NULL, NULL, outnx, outny); 143 144 // Update FITS header appropriately 145 psMetadataAddS32(header, PS_LIST_TAIL, "NAXIS1", PS_META_REPLACE, "Number of pixels in x", 146 (int)((psImage*)transformed->data[0])->numCols); 147 psMetadataAddS32(header, PS_LIST_TAIL, "NAXIS2", PS_META_REPLACE, "Number of pixels in y", 148 (int)((psImage*)transformed->data[0])->numRows); 149 psMetadataAddS32(header, PS_LIST_TAIL, "BITPIX", PS_META_REPLACE, "Bits per pixel", -32); 122 150 123 151 // Write out transformed image 124 psFits *outFile = psFitsAlloc(outName); 125 if (!psFitsWriteImage(outFile, NULL, transformed->data[0], 0, NULL)) { 126 psErrorStackPrint(stderr, "Unable to write image: %s\n", outName); 152 psFits *outFile = psFitsOpen(outName, "w"); 153 int numPix = psImageClipNaN(transformed->data[0], 0.0); 154 if (numPix > 0) { 155 psTrace("stac", 3, "Clipping %d NaN pixels to zero.\n", numPix); 156 } 157 if (!psFitsWriteImage(outFile, header, transformed->data[0], 0)) { 158 psErrorStackPrint(stderr, "Unable to write image: %s\n", outName); 127 159 } 128 160 psTrace("shift", 1, "Transformed image written to %s\n", outName); 129 psF ree(outFile);161 psFitsClose(outFile); 130 162 131 163 // Free everything I've used 132 164 psFree(images); 133 165 psFree(maps); 166 psFree(masks); 134 167 psFree(inverseMaps); 135 168 psFree(transformed);
Note:
See TracChangeset
for help on using the changeset viewer.
