Changeset 3669 for trunk/stac/src/shift.c
- Timestamp:
- Apr 5, 2005, 2:08:16 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/stac/src/shift.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/stac/src/shift.c
r3613 r3669 1 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <getopt.h> 5 #include <string.h> 2 6 #include "pslib.h" 3 7 #include "stac.h" 4 #include <sys/time.h>5 8 6 double getTime(void) 7 /* Gets the current time. Got this from Nick Kaiser's fetchpix.c */ 9 void help(const char *programName 10 ) 8 11 { 9 struct timeval tv; 10 gettimeofday(&tv, NULL); 11 return(tv.tv_sec + 1.e-6 * tv.tv_usec); 12 fprintf (stderr, "shift: shift an image, given the transformation\n" 13 "Usage: %s [-h] [-v] [-s NX NY] OUT IN\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 "\tOUT Output image\n" 19 "\tIN Input image, which has associated .map file.\n", 20 programName 21 ); 12 22 } 13 23 … … 15 25 int main(int argc, char **argv) 16 26 { 17 18 double startTime = getTime();19 20 #if 021 psMemAllocateCallbackSet(stacMemPrint);22 psMemAllocateCallbackSetID(185);23 psMemFreeCallbackSet(stacMemPrint);24 psMemFreeCallbackSetID(185);25 #endif26 27 27 // Set trace levels 28 psTraceSetLevel(".",0); 29 psTraceSetLevel("stac.checkMemory",10); 30 psTraceSetLevel("stac.config",10); 31 psTraceSetLevel("stac.read",10); 32 psTraceSetLevel("stac.invertMaps",10); 33 psTraceSetLevel("stac.errors",10); 34 psTraceSetLevel("stac.transform",10); 35 psTraceSetLevel("stac.combine",10); 36 psTraceSetLevel("stac.rejection",10); 37 psTraceSetLevel("stac.time",10); 38 psTraceSetLevel("stac.area",10); 39 psTraceSetLevel("stac.size",10); 28 psTraceSetLevel(".", 0); 29 psTraceSetLevel("stac.checkMemory", 10); 30 psTraceSetLevel("stac.read", 10); 31 psTraceSetLevel("stac.invertMaps", 10); 32 psTraceSetLevel("stac.transform", 10); 33 psTraceSetLevel("stac.size", 10); 34 psTraceSetLevel("shift", 10); 40 35 41 36 // Set logging level 42 37 psLogSetLevel(9); 43 38 39 // Parameters with default values 40 int outnx = 0, outny = 0; // Size of output image 41 int verbose = 0; // Verbosity level 42 44 43 // Parse command line 45 stacConfig *config = stacParseConfig(argc, argv); 44 const char *programName = argv[0]; // Program name 45 /* Variables for getopt */ 46 int opt; /* Option, from getopt */ 47 extern char *optarg; /* Argument accompanying switch */ 48 extern int optind; /* getopt variables */ 46 49 47 // Read input files 48 psArray *inputs = stacReadImages(config); 50 /* Parse command-line arguments using getopt */ 51 while ((opt = getopt(argc, argv, "hvs:")) != -1) { 52 switch (opt) { 53 case 'h': 54 help(programName); 55 exit(EXIT_SUCCESS); 56 case 'v': 57 verbose++; 58 break; 59 case 's': 60 if ((sscanf(argv[optind-1], "%d", &outnx) != 1) || (sscanf(argv[optind++], "%d", &outny) != 1)) { 61 // Note: incrementing optind, so I can read more than one parameter. 62 help(programName); 63 exit(EXIT_FAILURE); 64 } 65 break; 66 default: 67 help(programName); 68 exit(EXIT_FAILURE); 69 } 70 } 49 71 50 // Read maps 51 psArray *maps = stacReadMaps(config); 72 /* Get the remaining, mandatory values */ 73 argc -= optind; 74 argv += optind; 75 if (argc < 2) { 76 help(programName); 77 exit(EXIT_FAILURE); 78 } 79 const char *inName = argv[0]; // Input filename 80 const char *outName = argv[1]; // Output filename 52 81 53 psTrace("stac.time", 1, "I/O completed at %f seconds\n", getTime() - startTime); 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); 85 if (image == NULL) { 86 psErrorStackPrint(stderr, "Fatal error: Unable to read %s\n", inName); 87 exit(EXIT_FAILURE); 88 } 89 psTrace("shift", 4, "Image %s is %dx%d\n", inName, image->numCols, image->numRows); 90 // Convert to 32-bit floating point, in necessary 91 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; 96 } 97 psFree(imageFile); 98 psFree(imageRegion); 99 100 // Read map 101 char mapName[MAXCHAR]; // Name of map file 102 sprintf(mapName, "%s.map", inName); 103 psPlaneTransform *map = stacReadMap(mapName); 104 105 // 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 108 images->data[0] = image; 109 maps->data[0] = map; 54 110 55 111 // Get size 56 stacSize(config, inputs, maps); 112 if (outnx == 0 || outny == 0) { 113 stacSize(&outnx, &outny, NULL, NULL, images, maps); 114 } 57 115 58 116 // Invert maps 59 psArray *inverseMaps = stacInvertMaps(maps, config); 60 61 // Generate errors 62 psArray *errors = stacErrorImages(inputs, config); 117 psArray *inverseMaps = stacInvertMaps(maps, outnx, outny); 63 118 64 119 // Transform inputs and errors 65 120 psArray *transformed = NULL; 66 psArray *transformedErrors = NULL; 67 (void)stacTransform(&transformed, &transformedErrors, inputs, inverseMaps, errors, NULL, NULL, config); 121 (void)stacTransform(&transformed, NULL, images, inverseMaps, NULL, NULL, NULL, NULL, NULL, outnx, outny); 68 122 69 psTrace("stac.time",1,"Transformation completed at %f seconds\n", getTime() - startTime); 70 71 // Combine with rejection 72 psArray *rejected = NULL; 73 psImage *combined = NULL; 74 (void)stacCombine(&combined, &rejected, transformed, transformedErrors, config->nReject, NULL, config); 75 76 psTrace("stac.time",1,"First combination completed at %f seconds\n", getTime() - startTime); 77 78 psFits *outFile = psFitsAlloc(config->output); 79 if (!psFitsWriteImage(outFile, NULL, combined, 0, NULL)) { 80 psErrorStackPrint(stderr, "Unable to write image: %s\n", config->output); 123 // 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); 81 127 } 82 psTrace("s tac", 1, "Combined image written to %s\n", config->output);128 psTrace("shift", 1, "Transformed image written to %s\n", outName); 83 129 psFree(outFile); 84 130 85 131 // Free everything I've used 86 stacConfigFree(config); 87 psFree(inputs); 132 psFree(images); 88 133 psFree(maps); 89 134 psFree(inverseMaps); 90 psFree(errors);91 psFree(transformedErrors);92 135 psFree(transformed); 93 psFree(rejected);94 psFree(combined);95 136 96 psTrace("stac.time",1,"Final combination completed at %f seconds\n", getTime() - startTime); 137 #ifdef TESTING 138 // Check memory for leaks, corruption 139 stacCheckMemory(); 140 #endif 97 141 98 // Check memory for leaks, corruption99 stacCheckMemory();100 142 }
Note:
See TracChangeset
for help on using the changeset viewer.
