IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 17, 2006, 12:00:35 PM (20 years ago)
Author:
magnier
Message:

variety of small changes related to inconsistencies between psModules and psLib

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/fits/psFitsImage.c

    r6767 r6874  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2006-04-04 19:52:42 $
     9 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2006-04-17 22:00:03 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    120120}
    121121
    122 psImage* psFitsReadImage(psImage* output, // a psImage to recycle.
    123                          const psFits* fits,    // the psFits object
     122psImage* psFitsReadImage(const psFits* fits,    // the psFits object
    124123                         psRegion region, // the region in the FITS image to read
    125124                         int z)           // the z-plane in the FITS image cube to read
     
    140139        psError(PS_ERR_BAD_PARAMETER_NULL, true,
    141140                PS_ERRORTEXT_psFits_NULL);
    142         psFree(output);
    143141        return NULL;
    144142    }
     
    166164                PS_ERRORTEXT_psFits_DATATYPE_UNKNOWN,
    167165                fitsErr);
    168         psFree(output);
    169166        return NULL;
    170167    }
     
    176173                PS_ERRORTEXT_psFits_IMAGE_DIM_UNKNOWN,
    177174                fitsErr);
    178         psFree(output);
    179175        return NULL;
    180176    }
     
    185181                PS_ERRORTEXT_psFits_IMAGE_DIMENSION_UNSUPPORTED,
    186182                nAxis);
    187         psFree(output);
    188183        return NULL;
    189184    }
     
    195190                PS_ERRORTEXT_psFits_IMAGE_SIZE_UNKNOWN,
    196191                fitsErr);
    197         psFree(output);
    198192        return NULL;
    199193    }
     
    260254                PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED,
    261255                bitPix);
    262         psFree(output);
    263         return NULL;
    264     }
    265 
    266     output = psImageRecycle(output,
    267                             lastPixel[0]-firstPixel[0]+1,
    268                             lastPixel[1]-firstPixel[1]+1,
    269                             datatype);
    270 
    271     if (output == NULL) {
    272         psError(PS_ERR_UNKNOWN, false,
    273                 "Failed to allocate a properly sized image.");
    274         return false;
    275     }
     256        return NULL;
     257    }
     258
     259    psImage *output = psImageAlloc(lastPixel[0]-firstPixel[0]+1,
     260                                   lastPixel[1]-firstPixel[1]+1,
     261                                   datatype);
    276262
    277263    // n.b., this assumes contiguous image buffer
     
    532518}
    533519
     520psArray *psFitsReadImageCube(const psFits *fits, psRegion region)
     521{
     522    int nAxis = 0;                      // Number of axes
     523    long nAxes[3];                      // Number of pixels on each axis
     524    int status = 0;                     // cfitsio status value
     525    char fitsErr[80] = "";              // CFITSIO error message string
     526
     527    if (fits == NULL) {
     528        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     529                PS_ERRORTEXT_psFits_NULL);
     530        return NULL;
     531    }
     532
     533    // Some of this replicates what is in psFitsReadImage, so it's a little inefficient.  But it saves
     534    // code replication, and should be sufficient for our needs.
     535
     536    if (fits_get_img_dim(fits->fd, &nAxis, &status) != 0) {
     537        (void)fits_get_errstatus(status, fitsErr);
     538        psError(PS_ERR_IO, true,
     539                PS_ERRORTEXT_psFits_IMAGE_DIM_UNKNOWN,
     540                fitsErr);
     541        return NULL;
     542    }
     543
     544    if (nAxis == 2) {
     545        psArray *images = psArrayAlloc(1); // Single image plane
     546        images->data[0] = psFitsReadImage(fits, region, 0);
     547        return images;
     548    }
     549    if (nAxis == 3) {
     550        if (fits_get_img_size(fits->fd, nAxis, nAxes, &status) != 0) {
     551            (void)fits_get_errstatus(status, fitsErr);
     552            psError(PS_ERR_IO, true,
     553                    PS_ERRORTEXT_psFits_IMAGE_SIZE_UNKNOWN,
     554                    fitsErr);
     555            return NULL;
     556        }
     557
     558        psArray *images = psArrayAlloc(nAxes[2]); // Array of image planes
     559        for (int i = 0; i < nAxes[2]; i++) {
     560            images->data[i] = psFitsReadImage(fits, region, i);
     561        }
     562
     563        return images;
     564    }
     565
     566    // Bad dimensionality
     567    psError(PS_ERR_IO, true, PS_ERRORTEXT_psFits_IMAGE_DIMENSION_UNSUPPORTED, nAxis);
     568    return NULL;
     569}
     570
     571bool psFitsWriteImageCube(psFits *fits, psMetadata *header, const psArray *input, const char *extname)
     572{
     573    if (fits == NULL) {
     574        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     575                PS_ERRORTEXT_psFits_NULL);
     576        return false;
     577    }
     578
     579    if (input == NULL) {
     580        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     581                PS_ERRORTEXT_psFits_IMAGE_NULL);
     582        return false;
     583    }
     584
     585    if (input->n == 0) {
     586        psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_EMPTY);
     587        return false;
     588    }
     589
     590    if (input->n == 1) {
     591        // The problem reduces to one already solved
     592        return psFitsWriteImage(fits, header, input->data[0], 1, extname);
     593    }
     594
     595    // Check that all images are of the same size
     596    psImage *testImage = input->data[0];// First image off the array
     597    int numCols = testImage->numCols;   // Number of columns
     598    int numRows = testImage->numRows;   // Number of rows
     599    for (int i = 1; i < input->n; i++) {
     600        testImage = input->data[i];
     601        if (testImage->numCols != numCols || testImage->numRows != numRows) {
     602            psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_SIZE_DIFFER);
     603            return false;
     604        }
     605    }
     606
     607    // Need to check the header to make sure NAXIS and NAXIS[1-3] are correct
     608    psMetadata *headerCopy = NULL;      // Copy of header
     609    if (header) {
     610        headerCopy = psMemIncrRefCounter(header);
     611    } else {
     612        headerCopy = psMetadataAlloc();
     613    }
     614    bool update = psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS", PS_META_REPLACE, "Dimensionality", 3) &&
     615                  psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS1", PS_META_REPLACE, "Number of columns", numCols) &&
     616                  psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS2", PS_META_REPLACE, "Number of rows", numRows) &&
     617                  psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS3", PS_META_REPLACE, "Number of image planes",
     618                                   input->n);
     619    if (! update) {
     620        psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_METADATA_ADD_FAILED,
     621                "NAXIS, NAXIS1, NAXIS2, NAXIS3");
     622        psFree(headerCopy);
     623        return false;
     624    }
     625
     626    // Now we can safely write the images out.
     627    // The first is an psFitsImageWrite to create the extension.
     628    // The next are psFitsImageUpdate to write into the extension.
     629    if (! psFitsWriteImage(fits, headerCopy, input->data[0], input->n, extname)) {
     630        psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_WRITE_PLANE_FAILED, 0);
     631        psFree(headerCopy);
     632        return false;
     633    }
     634    psFree(headerCopy);                 // Free, or drop reference
     635
     636    for (int i = 1; i < input->n; i++) {
     637        if (! psFitsUpdateImage(fits, input->data[i], 0, 0, i)) {
     638            psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_WRITE_PLANE_FAILED, i);
     639            return false;
     640        }
     641    }
     642
     643    return true;
     644}
     645
     646bool psFitsUpdateImageCube(psFits *fits, const psArray *input, int x0, int y0)
     647{
     648    if (fits == NULL) {
     649        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     650                PS_ERRORTEXT_psFits_NULL);
     651        return false;
     652    }
     653
     654    if (input == NULL) {
     655        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     656                PS_ERRORTEXT_psFits_IMAGE_NULL);
     657        return false;
     658    }
     659
     660    if (input->n == 0) {
     661        psError(PS_ERR_BAD_PARAMETER_SIZE, true, PS_ERRORTEXT_psFits_ARRAY_EMPTY);
     662        return false;
     663    }
     664
     665    for (int i = 0; i < input->n; i++) {
     666        if (! psFitsUpdateImage(fits, input->data[i], x0, y0, i)) {
     667            psError(PS_ERR_UNKNOWN, false, PS_ERRORTEXT_psFits_UPDATE_PLANE_FAILED, i);
     668            return false;
     669        }
     670    }
     671
     672    return true;
     673}
     674
Note: See TracChangeset for help on using the changeset viewer.