예제 #1
0
QValueList<int> fitsDim( fitsfile *fp, int HDU ) {
  
  QValueList<int> dims;
  
  // move to desired HDU
  int ret = 0;
  int hduType;
  if (fits_movabs_hdu(fp, HDU, &hduType, &ret)) {
    return dims;
  }
  
  // for each type of HDU, find the dimensions
  
  int maxDim;
  long rowLen;
  long nRows;
  int nCols;
  long tbCol;
  long *dimAxes = NULL;
  int nAxis;
  switch (hduType) {
    case IMAGE_HDU:
      // find the size of the image and include it in the 
      // generic name.
      maxDim = 100; // no images should have > 100 dimensions...
      dimAxes = (long*)calloc(maxDim, sizeof(long));
      
      if (fits_read_imghdr(fp, maxDim, NULL, NULL, &nAxis, dimAxes, NULL, NULL, NULL, &ret)) {
        return dims;
      }
      for (int i = 0; i < nAxis; i++) {
        dims.append((int)dimAxes[i]);
      }
      free(dimAxes);
      break;
      
    case ASCII_TBL:
      maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
      
      // find number of columns
      if (fits_read_atblhdr(fp, maxDim, &rowLen, &nRows, &nCols, NULL, &tbCol, NULL, NULL, NULL, &ret)) {
        return dims;
      }
      
      dims.append((int)nCols);
      dims.append((int)nRows);
      
      break;
      
    case BINARY_TBL:
      maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
      
      // find number of columns
      if (fits_read_btblhdr(fp, maxDim, &nRows, &nCols, NULL, NULL, NULL, NULL, NULL, &ret)) {
        return dims;
      }
      
      dims.append((int)nCols);
      dims.append((int)nRows);
      
      break;
      
    default:
      return dims;
      break;
  }
  
  return dims;
}
예제 #2
0
int hpic_fits_vecindx_read(char *filename, char *creator, char *extname,
                           hpic_vec_int * indx, hpic_vec_fltarr * vecs,
                           char **vecnames, char **vecunits, hpic_keys * keys)
{

  size_t i, j, k, m;
  fitsfile *fp;
  int ret = 0;
  int bitpix = SHORT_IMG;
  int nax = 0;
  long axes[] = { 0, 0 };
  int total;
  int type;
  int ttype;
  int filetype;
  int tabtype;
  long nrows;
  int tfields;
  long frow = 1;
  long fsamp = 1;
  size_t veclen;
  size_t tvecs;
  char **vectypes;
  long rowlen;
  long *tbcol;
  long pcount = 0;
  char comment[HPIC_STRNL];
  float nullval;
  int nnull;
  char **names;
  char **units;
  size_t nvecs = hpic_vec_fltarr_n_get(vecs);
  hpic_vec_float *tempvec;

  if (nvecs <= 0) {
    HPIC_ERROR(HPIC_ERR_FITS, "must specify more than zero vectors!");
  }
  if (!hpic_fits_vec_test(filename, &tvecs, &veclen, &filetype, &tabtype)) {
    HPIC_ERROR(HPIC_ERR_FITS, "file is not a healpix vector file");
  }
  if (filetype != HPIC_FITS_VEC_INDX) {
    HPIC_ERROR(HPIC_ERR_FITS, "file is not an index vector file - try hpic_fits_vec_read");
  }
  if (tvecs > nvecs) {
    HPIC_ERROR(HPIC_ERR_FITS, "vector array does not have enough room for data in file");
  }
  if (veclen != hpic_vec_int_n_get(indx)) {
    HPIC_ERROR(HPIC_ERR_FITS, "index vector does not have correct length");
  }
  for (i = 0; i < nvecs; i++) {
    tempvec = hpic_vec_fltarr_get(vecs, i);
    if (veclen != hpic_vec_float_n_get(tempvec)) {
      HPIC_ERROR(HPIC_ERR_FITS, "all vectors must have the same length");
    }
  }
  vectypes = hpic_strarr_alloc(nvecs + 1);
  names = hpic_strarr_alloc(nvecs + 1);
  units = hpic_strarr_alloc(nvecs + 1);
  if (tabtype == HPIC_FITS_ASCII) {
    tbcol = (long *)calloc(nvecs + 1, sizeof(long));
  }

  /* open file */
  if (fits_open_file(&fp, filename, READONLY, &ret)) {
    fitserr(ret, "hpic_fits_vecindx_read:  opening file");
  }

  /* read header info */
  fits_read_key(fp, TSTRING, "CREATOR", creator, comment, &ret);
  ret = 0;

  /* read extension info */
  if (fits_movabs_hdu(fp, 2, &type, &ret)) {
    fitserr(ret, "hpic_fits_vecindx_read:  moving to extension");
  }

  if (tabtype == HPIC_FITS_ASCII) {
    if (fits_read_atblhdr
        (fp, (int)(nvecs + 1), &rowlen, &nrows, &tfields, names, tbcol,
         vectypes, units, extname, &ret)) {
      ret = 0;
      fits_close_file(fp, &ret);
      return 0;
    }
  } else {
    if (fits_read_btblhdr
        (fp, (int)(nvecs + 1), &nrows, &tfields, names, vectypes, units,
         extname, &pcount, &ret)) {
      ret = 0;
      fits_close_file(fp, &ret);
      return 0;
    }
  }
  for (i = 0; i < nvecs; i++) {
    strncpy(vecnames[i], names[i + 1], HPIC_STRNL);
    strncpy(vecunits[i], units[i + 1], HPIC_STRNL);
  }

  /* read optional keywords */
  hpic_keys_clear(keys);
  hpic_keys_read(keys, fp, &ret);

  /* read the data */
  nullval = HPIC_NULL;
  nnull = 0;
  if (fits_read_col
      (fp, TINT, 1, 1, 1, (long)(veclen), &nullval, indx->data, &nnull,
       &ret)) {
    fitserr(ret, "hpic_fits_vecindx_read:  reading index");
  }
  for (i = 0; i < nvecs; i++) {
    tempvec = hpic_vec_fltarr_get(vecs, i);
    if (fits_read_col
        (fp, TFLOAT, (int)(i + 2), 1, 1, (long)(veclen), &nullval,
         tempvec->data, &nnull, &ret)) {
      fitserr(ret, "hpic_fits_vecindx_read:  reading data");
    }
  }

  hpic_strarr_free(vectypes, nvecs + 1);
  hpic_strarr_free(names, nvecs + 1);
  hpic_strarr_free(units, nvecs + 1);
  if (tabtype == HPIC_FITS_ASCII) {
    free(tbcol);
  }
  
  if (fits_close_file(fp, &ret)) {
    fitserr(ret, "hpic_fits_vecindx_read:  closing file");
  }

  return ret;
}
예제 #3
0
int fitsNamesUnits( fitsfile *fp, int HDU, QStringList *names, QStringList *units ) {
  
  int nString = 0;
  
  // clear the lists
  names->clear();
  units->clear();
  
  // move to desired HDU
  int ret = 0;
  int hduType;
  if (fits_movabs_hdu(fp, HDU, &hduType, &ret)) {
    return nString;
  }
  
  // for each type of HDU, find the column names or assign
  // a generic image name (since images don't have names)
  
  QString curName;
  QString curUnits;
  int maxDim;
  long rowLen;
  long nRows;
  int nCols;
  long tbCol;
  char **fitsNames;
  char **fitsUnits;
  long *dimAxes = NULL;
  int nAxis;
  switch (hduType) {
    case IMAGE_HDU:
      // find the size of the image and include it in the 
      // generic name.
      maxDim = 100; // no images should have > 100 dimensions...
      dimAxes = (long*)calloc(maxDim, sizeof(long));
      
      if (fits_read_imghdr(fp, maxDim, NULL, NULL, &nAxis, dimAxes, NULL, NULL, NULL, &ret)) {
        return nString;
      }
      curName = "IMAGE (";
      for (int i = 0; i < nAxis; i++) {
        curUnits.sprintf("%ld", dimAxes[i]);
        curName.append(curUnits);
        if (i != nAxis-1) {
          curName.append("x");
        }
      }
      curName.append(")");
      names->append(curName);
      units->append(QString());
      nString++;
      free(dimAxes);
      break;
      
    case ASCII_TBL:
      maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
      
      // find number of columns
      if (fits_read_atblhdr(fp, maxDim, &rowLen, &nRows, &nCols, NULL, &tbCol, NULL, NULL, NULL, &ret)) {
        return nString;
      }
      
      // allocate names and units
      fitsNames = fitsSarrayAlloc(nCols);
      fitsUnits = fitsSarrayAlloc(nCols);
      
      // read names and units
      if (fits_read_atblhdr(fp, maxDim, &rowLen, &nRows, &nCols, fitsNames, &tbCol, NULL, fitsUnits, NULL, &ret)) {
        return nString;
      }
      for (int i = 0; i < nCols; i++) {
        curName = fitsNames[i];
        curUnits = fitsUnits[i];
        names->append(curName);
        units->append(curUnits);
        nString++;
      }
      fitsSarrayFree(fitsNames, nCols);
      fitsSarrayFree(fitsUnits, nCols);
      break;
      
    case BINARY_TBL:
      maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
      
      // find number of columns
      if (fits_read_btblhdr(fp, maxDim, &nRows, &nCols, NULL, NULL, NULL, NULL, NULL, &ret)) {
        return nString;
      }
      
      // allocate names and units
      fitsNames = fitsSarrayAlloc(nCols);
      fitsUnits = fitsSarrayAlloc(nCols);
      
      // read names and units
      if (fits_read_btblhdr(fp, maxDim, &nRows, &nCols, fitsNames, NULL, fitsUnits, NULL, NULL, &ret)) {
        return nString;
      }
      for (int i = 0; i < nCols; i++) {
        curName = fitsNames[i];
        curUnits = fitsUnits[i];
        names->append(curName);
        units->append(curUnits);
        nString++;
      }
      fitsSarrayFree(fitsNames, nCols);
      fitsSarrayFree(fitsUnits, nCols);
      break;
      
    default:
      return nString;
      break;
  }
  
  return nString;
}
예제 #4
0
int hpic_fits_vec_test(char *filename, size_t * nvecs, size_t * length,
                       int *filetype, int *tabtype)
{

  size_t i, j, k, m;
  fitsfile *fp;
  int ret = 0;
  int ttype;
  long nrows;
  long rowlen;
  long pcount;
  char extname[HPIC_STRNL];
  int tfields;
  long repeat;
  long width;

  /* open file */
  if (fits_open_file(&fp, filename, READONLY, &ret)) {
    return 0;
  }

  /* check extension type, number of columns */
  if (fits_movabs_hdu(fp, 2, &ttype, &ret)) {
    ret = 0;
    fits_close_file(fp, &ret);
    return 0;
  }
  if (ttype == ASCII_TBL) {
    (*tabtype) = HPIC_FITS_ASCII;
    if (fits_read_atblhdr
        (fp, HPIC_FITS_MAXCOL, &rowlen, &nrows, &tfields, NULL, NULL, NULL, NULL, extname,
         &ret)) {
      ret = 0;
      fits_close_file(fp, &ret);
      return 0;
    }
  } else {
    (*tabtype) = HPIC_FITS_BIN;
    if (fits_read_btblhdr
        (fp, HPIC_FITS_MAXCOL, &nrows, &tfields, NULL, NULL, NULL, extname, &pcount,
         &ret)) {
      ret = 0;
      fits_close_file(fp, &ret);
      return 0;
    }
  }

  /* determine the types of vectors */

  if (fits_get_coltype(fp, 1, &ttype, &repeat, &width, &ret)) {
    ret = 0;
    fits_close_file(fp, &ret);
    return 0;
  }
  if ((ttype == TINT) || (ttype == TINT32BIT) || (ttype == TLONG) ||
      (ttype == TSHORT)) {
    (*filetype) = HPIC_FITS_VEC_INDX;
  } else if ((ttype == TFLOAT) || (ttype == TDOUBLE)) {
    (*filetype) = HPIC_FITS_VEC;
  } else {
    ret = 0;
    fits_close_file(fp, &ret);
    return 0;
  }
  for (i = 1; i < (size_t) tfields; i++) {
    if (fits_get_coltype(fp, (int)(i + 1), &ttype, &repeat, &width, &ret)) {
      ret = 0;
      fits_close_file(fp, &ret);
      return 0;
    }
    if ((ttype != TFLOAT) && (ttype != TDOUBLE)) {
      ret = 0;
      fits_close_file(fp, &ret);
      return 0;
    }
  }

  if ((*filetype) == HPIC_FITS_VEC_INDX) {
    (*nvecs) = (size_t) tfields - 1;
  } else {
    (*nvecs) = (size_t) tfields;
  }
  (*length) = (size_t) nrows;

  fits_close_file(fp, &ret);
  return 1;
}