IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 5, 2005, 2:08:16 PM (21 years ago)
Author:
Paul Price
Message:

Remaking shift into a program to shift a single image. It seems to work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/stac/src/shift.c

    r3613 r3669  
    11#include <stdio.h>
     2#include <stdlib.h>
     3#include <unistd.h>
     4#include <getopt.h>
     5#include <string.h>
    26#include "pslib.h"
    37#include "stac.h"
    4 #include <sys/time.h>
    58
    6 double getTime(void)
    7 /* Gets the current time.  Got this from Nick Kaiser's fetchpix.c */
     9void help(const char *programName
     10    )
    811{
    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        );
    1222}
    1323
     
    1525int main(int argc, char **argv)
    1626{
    17 
    18     double startTime = getTime();
    19 
    20 #if 0
    21     psMemAllocateCallbackSet(stacMemPrint);
    22     psMemAllocateCallbackSetID(185);
    23     psMemFreeCallbackSet(stacMemPrint);
    24     psMemFreeCallbackSetID(185);
    25 #endif
    26 
    2727    // 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);
    4035
    4136    // Set logging level
    4237    psLogSetLevel(9);
    4338
     39    // Parameters with default values
     40    int outnx = 0, outny = 0;           // Size of output image
     41    int verbose = 0;                    // Verbosity level
     42
    4443    // 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 */
    4649
    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    }
    4971
    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
    5281
    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;
    54110
    55111    // Get size
    56     stacSize(config, inputs, maps);
     112    if (outnx == 0 || outny == 0) {
     113        stacSize(&outnx, &outny, NULL, NULL, images, maps);
     114    }
    57115
    58116    // 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);
    63118
    64119    // Transform inputs and errors
    65120    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);
    68122
    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);
    81127    }
    82     psTrace("stac", 1, "Combined image written to %s\n", config->output);
     128    psTrace("shift", 1, "Transformed image written to %s\n", outName);
    83129    psFree(outFile);
    84130
    85131    // Free everything I've used
    86     stacConfigFree(config);
    87     psFree(inputs);
     132    psFree(images);
    88133    psFree(maps);
    89134    psFree(inverseMaps);
    90     psFree(errors);
    91     psFree(transformedErrors);
    92135    psFree(transformed);
    93     psFree(rejected);
    94     psFree(combined);
    95136
    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
    97141
    98    // Check memory for leaks, corruption
    99     stacCheckMemory();
    100142}
Note: See TracChangeset for help on using the changeset viewer.