IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 7, 2005, 2:57:14 PM (20 years ago)
Author:
Paul Price
Message:

Importing from PAP cvs tree again. This will now be the working tree.

File:
1 edited

Legend:

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

    r3673 r5743  
    1111{
    1212    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              programName
    21         );
     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        );
    2222}
    2323
     
    3838
    3939    // 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
    4243
    4344    // Parse command line
    44     const char *programName = argv[0];  // Program name
     45    const char *programName = argv[0];  // Program name
    4546    /* Variables for getopt */
    4647    int opt;   /* Option, from getopt */
     
    4950
    5051    /* Parse command-line arguments using getopt */
    51     while ((opt = getopt(argc, argv, "hvs:")) != -1) {
     52    while ((opt = getopt(argc, argv, "hvb:s:")) != -1) {
    5253        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':
    5758            verbose++;
    5859            break;
    59           case 's':
     60          case 's':
    6061            if ((sscanf(argv[optind-1], "%d", &outnx) != 1) || (sscanf(argv[optind++], "%d", &outny) != 1)) {
    6162                // Note: incrementing optind, so I can read more than one parameter.
     
    6364                exit(EXIT_FAILURE);
    6465            }
    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        }
    7077    }
    7178
     
    7784        exit(EXIT_FAILURE);
    7885    }
    79     const char *inName = argv[0];       // Input filename
    80     const char *outName = argv[1];      // Output filename
     86    const char *inName = argv[0];       // Input filename
     87    const char *outName = argv[1];      // Output filename
    8188
    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);
    8593    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);
    8896    }
    8997    psTrace("shift", 4, "Image %s is %dx%d\n", inName, image->numCols, image->numRows);
    9098    // Convert to 32-bit floating point, in necessary
    9199    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;
    96104    }
    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    }
    99118
    100119    // Read map
    101     char mapName[MAXCHAR];              // Name of map file
     120    char mapName[MAXCHAR];              // Name of map file
    102121    sprintf(mapName, "%s.map", inName);
    103122    psPlaneTransform *map = stacReadMap(mapName);
    104123
    105124    // 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
    108128    images->data[0] = image;
    109129    maps->data[0] = map;
     130    masks->data[0] = mask;
    110131
    111132    // Get size
    112133    if (outnx == 0 || outny == 0) {
    113         stacSize(&outnx, &outny, NULL, NULL, images, maps);
     134        stacSize(&outnx, &outny, NULL, NULL, images, maps);
    114135    }
    115136
     
    119140    // Transform inputs and errors
    120141    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);
    122150
    123151    // 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);
    127159    }
    128160    psTrace("shift", 1, "Transformed image written to %s\n", outName);
    129     psFree(outFile);
     161    psFitsClose(outFile);
    130162
    131163    // Free everything I've used
    132164    psFree(images);
    133165    psFree(maps);
     166    psFree(masks);
    134167    psFree(inverseMaps);
    135168    psFree(transformed);
Note: See TracChangeset for help on using the changeset viewer.