IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 25, 2007, 4:18:23 PM (19 years ago)
Author:
eugene
Message:

convert gfits_read_segment to gfits_read_matrix_segment; add gfits_fread_matrix_segment

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libfits/matrix/F_read_segment.c

    r7054 r13039  
    22# include <gfitsio.h>
    33
    4 /** warning: this function requires 'dparse' which is included in libohana.a **/
    5 int dparse (double *, int, char *region);
    6 
    74/*********************** fits read matrix ***********************************/
    8 int gfits_read_segment (char *filename, Matrix *matrix, char *region) {
     5int gfits_read_matrix_segment (char *filename, Matrix *matrix, char *region) {
    96
    107  FILE *f;
    118  Header header;
     9  int status;
     10
     11  f = fopen (filename, "r");
     12  if (f == NULL) {
     13    return (FALSE);
     14  }
     15
     16  status = gfits_fread_header (f, &header);
     17  if (!status) {
     18    fclose (f);
     19    fprintf (stderr, "error reading header of FITS file %s\n", filename);
     20    return (FALSE);
     21  }
     22
     23  status = gfits_fread_matrix_segment (f, matrix, &header, region);
     24  if (!status) {
     25    fclose (f);
     26    fprintf (stderr, "error reading header of FITS file %s\n", filename);
     27    return (FALSE);
     28  }
     29
     30  fclose (f);
     31  return (TRUE);
     32}
     33
     34// read header before calling this function
     35// file pointer should be at start of Matrix data segment
     36int gfits_fread_matrix_segment (FILE *f, Matrix *matrix, Header *header, char *region) {
     37
    1238  int status, i, nbytes, Nbytes, Nskip, NbytesData;
    1339  int wantaxis[FT_MAX_NAXES][2];
     
    1642  int perpix;
    1743
    18   status = gfits_read_header (filename, &header);
    19   if (!status) {
    20     fprintf (stderr, "error reading header of FITS file %s\n", filename);
    21     return (FALSE);
    22   }
    23 
    24   f = fopen (filename, "r");
    25   if (f == NULL) {
    26     return (FALSE);
    27   }
    28 
    2944  for (i = 0; i < FT_MAX_NAXES; i++) {
    3045    wantaxis[i][0] = wantaxis[i][1] = 0;
    3146  }
    3247
    33   for (i = 0; i < header.Naxes; i++) {
     48  for (i = 0; i < header[0].Naxes; i++) {
    3449    status = dparse (&tmp, 2*i + 1, region);
    3550    if (!status || (tmp < 0))
    3651      wantaxis[i][0] = 0;
    3752    else
    38       wantaxis[i][0] = (tmp > header.Naxis[i] - 1) ? header.Naxis[i] - 1 : tmp;
     53      wantaxis[i][0] = (tmp > header[0].Naxis[i] - 1) ? header[0].Naxis[i] - 1 : tmp;
    3954    status = dparse (&tmp, 2*i + 2, region);
    4055    if (!status || (tmp < 0))
    41       wantaxis[i][1] = header.Naxis[i];
     56      wantaxis[i][1] = header[0].Naxis[i];
    4257    else
    43       wantaxis[i][1] = (tmp > header.Naxis[i]) ? header.Naxis[i] : tmp;
     58      wantaxis[i][1] = (tmp > header[0].Naxis[i]) ? header[0].Naxis[i] : tmp;
    4459  }
    4560
    46   fseek (f, header.size, 0);
    47 
    48   matrix[0].bitpix = header.bitpix;
    49   matrix[0].unsign = header.unsign;
    50   matrix[0].bscale = header.bscale;
    51   matrix[0].bzero  = header.bzero;
    52   matrix[0].Naxes  = header.Naxes;
     61  matrix[0].bitpix = header[0].bitpix;
     62  matrix[0].unsign = header[0].unsign;
     63  matrix[0].bscale = header[0].bscale;
     64  matrix[0].bzero  = header[0].bzero;
     65  matrix[0].Naxes  = header[0].Naxes;
    5366  for (i = 0; i < FT_MAX_NAXES; i++)
    5467    matrix[0].Naxis[i] = wantaxis[i][1] - wantaxis[i][0];
     
    7689  /* currently only good for reading in full planes in 3-D */
    7790  fseek (f, Nskip, SEEK_CUR);
    78 
    7991  nbytes = fread (matrix[0].buffer, sizeof(char), Nbytes, f);
    80   fclose (f);
    8192  matrix[0].size = Nbytes;
    8293
    8394# ifdef BYTE_SWAP 
    84   perpix = abs(header.bitpix) / 8;
     95  perpix = abs(header[0].bitpix) / 8;
    8596  if (perpix > 1) {
    8697    byte0 = (unsigned char *) matrix[0].buffer;
     
    128139# endif
    129140
    130   gfits_free_header (&header);
    131 
    132141  if (nbytes < Nbytes - 2880) {  /* this is a bad FITS error: image is not OK */
    133     fprintf (stderr, "error reading in matrix data from FITS file %s (%d < %d - 2880)\n", filename, nbytes, Nbytes);
     142    fprintf (stderr, "error reading in matrix data from FITS file (%d < %d - 2880)\n", nbytes, Nbytes);
    134143    return (FALSE);
    135144  }
    136145  if (nbytes != Nbytes) {  /* this is a FITS error, but often the image is OK */
    137     fprintf (stderr, "incomplete block in %s: (%d, %d)\n", filename, nbytes, Nbytes);
     146    fprintf (stderr, "incomplete block in FITS file: (%d, %d)\n", nbytes, Nbytes);
    138147    return (TRUE);
    139148  }
     
    142151
    143152}
    144 
Note: See TracChangeset for help on using the changeset viewer.