示例#1
0
flouble *he_read_healpix_map(char *fname,long *nside,int nfield)
{
  //////
  // Reads a healpix map from file fname. The map will be
  // read from column #nfield. It also returns the map's nside.
  int status=0,hdutype,nfound,anynul;
  long naxes,*naxis,npix;
  fitsfile *fptr;
  flouble *map,nulval;
  char order_in_file[32];
  int nested_in_file=0;

  fits_open_file(&fptr,fname,READONLY,&status);
  fits_movabs_hdu(fptr,2,&hdutype,&status);
  fits_read_key_lng(fptr,"NAXIS",&naxes,NULL,&status);
  naxis=(long *)malloc(naxes*sizeof(long));
  fits_read_keys_lng(fptr,"NAXIS",1,naxes,naxis,&nfound,&status);
  fits_read_key_lng(fptr,"NSIDE",nside,NULL,&status);
  npix=12*(*nside)*(*nside);
  if(npix%naxis[1]!=0) {
    fprintf(stderr,"CRIME: WTFFF\n");
    exit(1);
  }

  if (fits_read_key(fptr, TSTRING, "ORDERING", order_in_file, NULL, &status)) {
    fprintf(stderr, "WARNING: Could not find %s keyword in in file %s\n",
            "ORDERING",fname);
    exit(1);
  }
  if(!strncmp(order_in_file,"NEST",4))
    nested_in_file=1;

  map=(flouble *)my_malloc(npix*sizeof(flouble));
#ifdef _SPREC
  fits_read_col(fptr,TFLOAT,nfield+1,1,1,npix,&nulval,map,&anynul,&status);
#else //_SPREC
  fits_read_col(fptr,TDOUBLE,nfield+1,1,1,npix,&nulval,map,&anynul,&status);
#endif //_SPREC
  free(naxis);

  fits_close_file(fptr,&status);

  flouble *map_ring;
  if(nested_in_file) {
    long ipring,ipnest;

    printf("read_healpix_map: input is nested. Transforming to ring.\n");
    map_ring=(flouble *)my_malloc(npix*sizeof(flouble));
    for(ipnest=0;ipnest<npix;ipnest++) {
      nest2ring(*nside,ipnest,&ipring);
      map_ring[ipring]=map[ipnest];
    }
    free(map);
  }
  else
    map_ring=map;

  return map_ring;
}
示例#2
0
/* Read only part (N "spectra" in time) of the DATA column from the
 * current subint from the set of files described by the psrfits
 * struct.  Put the data into the buffer "buffer".  It is assumed that
 * all files form a consistent set.  Read automatically goes to the
 * next file when one ends.  Arrays should be allocated outside this
 * routine.  Counters are _not_ updated as they are in
 * psrfits_read_subint().
 */
int psrfits_read_part_DATA(struct psrfits *pf, int N, int numunsigned, 
                           float *fbuffer) {

    struct hdrinfo *hdr = &(pf->hdr);
    int colnum = 0, *status = &(pf->status);

    // See if we need to move to next file
    if (pf->rownum > pf->rows_per_file) {
        printf("Closing file '%s'\n", pf->filename);
        fits_close_file(pf->fptr, status);
        pf->filenum++;
        psrfits_open(pf);
        if (*status==FILE_NOT_OPENED) {
            printf("Finished with all input files.\n");
            pf->filenum--;
            *status = 1;
            return *status;
        }
    }

    int nivals = hdr->nchan * hdr->npol;
    long long numdata = (long long) nivals * (long long) N;
    long long bytes_to_read = (hdr->nbits * numdata) / 8L;
    float *offsets = (float *)malloc(sizeof(float) * nivals);
    float *scales = (float *)malloc(sizeof(float) * nivals);
    unsigned char *buffer = (unsigned char *)malloc(numdata);
    unsigned char *rawbuffer = buffer;
    if (hdr->nbits==4) {
        rawbuffer = (unsigned char *)malloc(bytes_to_read);
    }

    // Now read the data
    fits_get_colnum(pf->fptr, 0, "DAT_OFFS", &colnum, status);
    fits_read_col(pf->fptr, TFLOAT, colnum, pf->rownum, 1, nivals,
                  NULL, offsets, NULL, status);
    fits_get_colnum(pf->fptr, 0, "DAT_SCL", &colnum, status);
    fits_read_col(pf->fptr, TFLOAT, colnum, pf->rownum, 1, nivals,
                  NULL, scales, NULL, status);
    fits_get_colnum(pf->fptr, 0, "DATA", &colnum, status);
    fits_read_col(pf->fptr, TBYTE, colnum, pf->rownum, 1, bytes_to_read,
                  NULL, rawbuffer, NULL, status);
    if (hdr->nbits==4) {
        convert_4bit_to_8bit(rawbuffer, (unsigned char *)buffer, numdata);
        free(rawbuffer);
    }
    
    // Now convert the 8-bit data to floats using the scales and offsets
    apply_scales_and_offsets(hdr->nchan, hdr->npol, N, numunsigned,
                             scales, offsets, buffer, fbuffer);
    free(offsets);
    free(scales);
    free(buffer);

    // Complain on error
    fits_report_error(stderr, *status);
    
    return *status;
}
示例#3
0
void get_PSRFITS_subint(float *fdata, unsigned char *cdata, struct spectra_info *s)
{
    unsigned char *ctmp = cdata;
    int ii, status = 0, anynull;
    int numtoread = s->samples_per_subint;

    // The following allows us to read byte-packed data
    if (s->bits_per_sample < 8) {
        numtoread = (s->samples_per_subint * s->bits_per_sample) / 8;
        ctmp = gen_bvect(numtoread);
    }
    // or 16-bit data that is listed as being bytes
    if (s->bits_per_sample == 16 && s->FITS_typecode == 11)
        numtoread = s->samples_per_subint * 2;

    // Read the weights, offsets, and scales if required
    if (s->apply_weight)
        fits_read_col(s->fitsfiles[cur_file], TFLOAT, s->dat_wts_col, cur_subint, 1L,
                      s->num_channels, 0, weights, &anynull, &status);
    if (s->apply_offset)
        fits_read_col(s->fitsfiles[cur_file], TFLOAT, s->dat_offs_col, cur_subint,
                      1L, s->num_channels * s->num_polns, 0, offsets, &anynull,
                      &status);
    if (s->apply_scale)
        fits_read_col(s->fitsfiles[cur_file], TFLOAT, s->dat_scl_col, cur_subint, 1L,
                      s->num_channels * s->num_polns, 0, scales, &anynull, &status);

    // Now actually read the subint into the temporary buffer
    fits_read_col(s->fitsfiles[cur_file], s->FITS_typecode,
                  s->data_col, cur_subint, 1L, numtoread,
                  0, ctmp, &anynull, &status);

    if (status) {
        fprintf(stderr, "Error!:  Problem reading record from PSRFITS data file\n"
                "\tfilename = '%s', subint = %d.  FITS status = %d.  Exiting.\n",
                s->filenames[cur_file], cur_subint, status);
        exit(1);
    }
    // The following converts that byte-packed data into bytes
    if (s->bits_per_sample == 4) {
#ifdef _OPENMP
#pragma omp parallel for default(none) shared(numtoread,cdata,ctmp)
#endif
        for (ii = 0; ii < numtoread; ii++) {
            const unsigned char uctmp = ctmp[ii];
            const int jj = 2 * ii;
            cdata[jj] = uctmp >> 4;
            cdata[jj + 1] = uctmp & 0x0F;
        }
    } else if (s->bits_per_sample == 2) {
示例#4
0
文件: uvsubs_old.c 项目: guille-c/VIR
int get_fqtable(fitsfile *fptr, int hdu, struct uvf_header *header)
{
  int status = 0, i;
  int hdutype, col_iff, type_iff;
  long nrows, rpt_iff, wid_iff;
  double f;

  fits_movabs_hdu(fptr, hdu, &hdutype, &status);
  fits_get_num_rows(fptr, &nrows,  &status);
  fits_get_colnum(fptr, CASEINSEN, "IF FREQ", &col_iff, &status);
  fits_get_coltype(fptr, col_iff, &type_iff, &rpt_iff, &wid_iff,
		   &status);
  if (status || type_iff != TDOUBLE || nrows != 1 || rpt_iff !=
      header->nif)
    fprintf(stderr, "    Warning: Invalid AIPS frequency table\n");
  for (i=0; i < rpt_iff; i++) {
    fits_read_col(fptr, TDOUBLE, col_iff, 1, i+1, 1,
		  NULL, &f, NULL, &status);
    header->iffreq[i] = header->freq + f;
  }
  fprintf(stderr, "    %d IFs ", header->nif);
  for (i=0; i < header->nif; i++)
    fprintf(stderr, " %.2f", header->iffreq[i]*1e-9);
  fprintf(stderr, "\n");
  return status ? 1 : 0;
}
示例#5
0
void FitsFile::ReadTableCell(int row, int col, double *output, size_t size)
{
	int status = 0;
	double nulValue = std::numeric_limits<double>::quiet_NaN();
	int anynul = 0;
	fits_read_col(_fptr, TDOUBLE, col, row, 1, size, &nulValue, output, &anynul, &status);
}
示例#6
0
void FitsFile::ReadTableCell(int row, int col, char *output)
{
	int status = 0;
	double nulValue = std::numeric_limits<double>::quiet_NaN();
	int anynul = 0;
	fits_read_col(_fptr, TSTRING, col, row, 1, 1, &nulValue, &output, &anynul, &status);
}
示例#7
0
int LFIIOSource::readField( double *v, const QString& field, int s, int n ) 
{
  double    dNan = strtod( "nan", NULL );
  fitsfile* ffits;
  bool      bOk;
  int       i;
  int       iCol;
  int       iRead = -1;
  int       iStatus = 0;
  int       iAnyNull;
  int       iResult = 0;

  if( n < 0 ) 
  {
    n = 1; /* n < 0 means read one sample, not frame - irrelavent here */
  }

  if( field == "INDEX" ) 
  {
    for( i = 0; i < n; i++ ) 
    {
      v[i] = (double)( s + i );
    }
    iRead =  n;
  }
  else
  {
    memset( v, 0, n * sizeof( double ) );
    
    bOk = getColNumber( field, &iCol );
    if( bOk ) 
    {
      _valid = false;

      if( !_filename.isNull( ) && !_filename.isEmpty( ) )
      {
        iResult = fits_open_table( &ffits, _filename.ascii( ), READONLY, &iStatus );
        if( iResult == 0 )
        {
          _valid = true;
          
          //
          // copy the data... 
          // N.B. fitsio column indices are 1 based, so we ask for iCol+1 instead of just iCol
          //
          iResult = fits_read_col( ffits, TDOUBLE, iCol+1, s+1, 1, n, &dNan, v, &iAnyNull, &iStatus );
          if( iResult == 0 )
          {
            iRead = n;
          }
          
          iStatus = 0;
          fits_close_file( ffits, &iStatus );      
        }
      }
    }
  }
  
  return iRead;
}
void InputFileFITS::_readv(int ncol, std::vector< std::vector<T> >& buff, int type, long frow, long lrow, int vsize) {
	int status = 0;
	if(!isOpened())
		throwException("Error in InputFileFITS::_readv() ", status);

	int anynull;
	long nelem = (lrow - frow + 1);
	long null = 0;

	T* buffptr = new T[nelem*vsize];
	fits_read_col(infptr, type, ncol+1, frow+1, 1, nelem*vsize, &null,  buffptr, &anynull, &status);

	// conversion from row pointers to std::vector< std::vector<T> >
	buff.resize(nelem);
	for(unsigned int i=0; i<nelem; i++)
		buff[i].resize(vsize);

	for(unsigned int row = frow; row < lrow+1; row++)
		memcpy(&buff[row][0], &buffptr[row*vsize], vsize*sizeof(T));

	delete[] buffptr;

	if(status)
		throwException("Error in InputFileFITS::_readv() ", status);
}
示例#9
0
void get_PSRFITS_subint(float *fdata, unsigned char *cdata, 
                        struct spectra_info *s)
{
    float *fptr;
    unsigned char *cptr;
    short *sptr;
    float *ftptr;
    int ii, jj, status = 0, anynull;
    int numtoread = s->samples_per_subint;
    
    // The following allows us to read byte-packed data
    if (s->bits_per_sample < 8)
        numtoread = s->samples_per_subint * s->bits_per_sample / 8;

    // Read the weights, offsets, and scales if required
    if (s->apply_weight)
        fits_read_col(s->fitsfiles[cur_file], TFLOAT, s->dat_wts_col, cur_subint, 1L, 
                      s->num_channels, 0, weights, &anynull, &status);
    if (s->apply_offset)
        fits_read_col(s->fitsfiles[cur_file], TFLOAT, s->dat_offs_col, cur_subint, 1L, 
                      s->num_channels*s->num_polns, 0, offsets, &anynull, &status);
    if (s->apply_scale)
        fits_read_col(s->fitsfiles[cur_file], TFLOAT, s->dat_scl_col, cur_subint, 1L, 
                      s->num_channels*s->num_polns, 0, scales, &anynull, &status);

    // Now actually read the subint into the temporary buffer
    fits_read_col(s->fitsfiles[cur_file], s->FITS_typecode, 
                  s->data_col, cur_subint, 1L, numtoread, 
                  0, cdata, &anynull, &status);

    if (status) {
        fprintf(stderr, "Error!:  Problem reading record from PSRFITS data file\n"
                "\tfilename = '%s', subint = %d.  FITS status = %d.  Exiting.\n",
                s->filenames[cur_file], cur_subint, status);
        exit(1);
    }

    // The following converts that byte-packed data into bytes, in place
    if (s->bits_per_sample == 4) {
        unsigned char uctmp;
        for (ii = numtoread - 1, jj = 2 * numtoread - 1 ; 
             ii >= 0 ; ii--, jj -= 2) {
            uctmp = (unsigned char)cdata[ii];
            cdata[jj] = uctmp & 0x0F;
            cdata[jj-1] = uctmp >> 4;
        }
    } else if (s->bits_per_sample == 2) {
示例#10
0
static int read_row_value (fitsfile *f, int row, int col, double *val)
{
   int status = 0;
   int anynul = 0;
   if (0 == fits_read_col (f, TDOUBLE, col, row, 1, 1, NULL, val, &anynul, &status))
     return 0;
   /* dump_fits_error (status); */
   return -1;
}
示例#11
0
static int read_row_value_short (fitsfile *f, int row, int col, short *val)
{
   int status = 0;
   int anynul = 0;
   if (0 == fits_read_col (f, TSHORT, col, row, 1, 1, NULL, val, &anynul, &status))
     return 0;
   /* dump_fits_error (status); */
   return -1;
}
示例#12
0
static int read_row_value_long (fitsfile *f, int row, int col, long *val)
{
   int status = 0;
   int anynul = 0;
   if (0 == fits_read_col (f, TLONG, col, row, 1, 1, NULL, val, &anynul, &status))
     return 0;
   /* dump_fits_error (status); */
   return -1;
}
示例#13
0
文件: FindSig.c 项目: iGoab/rrat_code
/*
初始化整个aa+bb序列,为了省内存,用unsigned char类型存储,并除2使之不超过范围
*/
void initTotalAABB(fitsfile* fptr,struct Fitsinfo* fi,unsigned char** abptr,int* status) {
	int n = fi->nROW;int k = fi->nSBLK; int m = fi->nCHAN;
	int step = fi->nCHAN*fi->nPOL; int start;
	unsigned char aa[m]; unsigned char bb[m];
	int dataCol; int nulval = 0; int anynull; int i,j,p;
	fits_get_colnum(fptr,CASEINSEN,"DATA",&dataCol,status);
	for (i=0; i<n; i++) {
		for (j=0; j<k; j++) {
			start = 1+j*step;
			fits_read_col(fptr,TBYTE,dataCol,i+1,start,m,&nulval,aa,&anynull,status);
			fits_read_col(fptr,TBYTE,dataCol,i+1,start+m,m,&nulval,bb,&anynull,status);
			for (p=0; p<m; p++) {
				abptr[j+i*k][p] = (aa[p]+bb[p])/2;
			}
		}
		printf("All Freq AABB init:Totalrow %d,row completed %d\n",n,i);
	}
	return;
}
示例#14
0
void FitsFile::ReadTableCell(int row, int col, long double *output, size_t size)
{
	std::vector<double> data(size);
	int status = 0;
	double nulValue = std::numeric_limits<double>::quiet_NaN();
	int anynul = 0;
	fits_read_col(_fptr, TDOUBLE, col, row, 1, size, &nulValue, data.data(), &anynul, &status);
	for(size_t i = 0;i<size;++i)
		output[i] = data[i];
}
示例#15
0
文件: FindSig.c 项目: iGoab/rrat_code
void initFreqDataWithZeroDm(fitsfile* fptr,struct FreqData* fd,unsigned char** ptr,int freqIndex,long samNum,int chanNum,int* status) {
	int freqCol; int nulval = 0; int anynull; float tmpFreq; int i;
	fits_get_colnum(fptr,CASEINSEN,"DAT_FREQ",&freqCol,status);
	fits_read_col(fptr,TFLOAT,freqCol,1,freqIndex+1,1,&nulval,&tmpFreq,&anynull,status);
	fd->freq = tmpFreq;
	for (i=0; i<samNum; i++) {
		fd->data[i] = ptr[i][freqIndex]-calcZeroDm(ptr,chanNum,i);
	}
	return;
}
示例#16
0
void FitsFile::ReadTableCell(int row, int col, bool *output, size_t size)
{
	std::vector<char> data(size);
	int status = 0;
	char nulValue = 0;
	int anynul = 0;
	fits_read_col(_fptr, TBIT, col, row, 1, size, &nulValue, data.data(), &anynul, &status);
	for(size_t i = 0;i<size;++i)
		output[i] = data[i]!=0;
}
示例#17
0
文件: fitsfile.cpp 项目: jjdmol/LOFAR
void FitsFile::ReadTableCell(int row, int col, bool *output, size_t size)
{
	char *data = new char[size];
	int status = 0;
	char nulValue = 0;
	int anynul = 0;
	fits_read_col(_fptr, TBIT, col, row, 1, size, &nulValue, data, &anynul, &status);
	for(size_t i = 0;i<size;++i)
		output[i] = data[i]!=0;
	delete[] data;
}
示例#18
0
float *
supcam_read_data( fitsfile * fits, size_t * numRows, size_t * numChans, int * status ) {

  int colnum = 0;    /* DATA column */
  int fitsStatus = 0; /* CFITSIO status */
  long nRows = 0;
  long nChans = 0;
  long dummy;
  float * data = NULL;
  int typecode = 0;

  /* Initialise the return values */
  *numChans = 0;
  *numRows = 0;

  if (*status != SAI__OK) return NULL;

  /* Prepare to read the Binary Table */
  /* Check number of rows */
  CALLCFITSIO( fits_get_num_rows(fits, &nRows, &fitsStatus), "Error getting number of rows" );

  /* Find DATA column and check its repeat count */
  /* cast because cfitsio does not use const */
  CALLCFITSIO( fits_get_colnum(fits, CASEINSEN, (char *)"DATA", &colnum, &fitsStatus),
               "Error getting column number for DATA" );
  CALLCFITSIO( fits_get_coltype(fits, colnum, &typecode, &nChans, &dummy, &fitsStatus),
               "Error getting column information" );
  if (*status != SAI__OK) return NULL;

  /* Get some memory */
  data = astMalloc( nChans * nRows * sizeof(*data) );

  /* Read DATA column */
  CALLCFITSIO( fits_read_col(fits, TFLOAT, colnum, 1, 1,
                             nChans*nRows, NULL, data, NULL, &fitsStatus),
               "Error reading spectra from Binary Column DATA" );

  if (*status == SAI__OK) {

    /* It looks like there is a bad value of (-1)*VAL__BADI */
    size_t i;
    size_t nelems = nChans * nRows;
    for (i = 0; i < nelems; i++ ) {
      if (data[i] >= (float)(VAL__MAXI - 1)) {
        data[i] = VAL__BADR;
      }
    }

    *numChans = nChans;
    *numRows = nRows;
  }

  return data;
}
示例#19
0
文件: uvsubs_old.c 项目: guille-c/VIR
int get_antable(fitsfile *fptr, int hdu, struct uvf_header *header)
{
  int status = 0, i;
  int hdutype, col_name, col_xyz, type_name, type_xyz;
  long nrows, rpt_name, rpt_xyz, wid_name, wid_xyz;
  char *string[1];
  int anynull;

  fits_movabs_hdu(fptr, hdu, &hdutype, &status);
  fits_get_num_rows(fptr, &nrows,  &status);
  header->n_antennas = (int) nrows;
  fits_get_colnum(fptr, CASEINSEN, "ANNAME", &col_name, &status);
  fits_get_coltype(fptr, col_name, &type_name, &rpt_name, &wid_name,
		   &status);
  fits_get_colnum(fptr, CASEINSEN, "STABXYZ", &col_xyz, &status);
  fits_get_coltype(fptr, col_xyz, &type_xyz, &rpt_xyz, &wid_xyz,
		   &status);
  if (status || type_name != TSTRING ||
                type_xyz != TDOUBLE || rpt_xyz != 3)
    fprintf(stderr, "    Warning: Invalid AIPS antenna table: %d %ld %d %ld\n",
	    type_name, rpt_name, type_xyz, rpt_xyz);
  for (i=0; i < nrows; i++) {
    string[0] = header->antenna[i].name;
    fits_read_col(fptr, TSTRING, col_name, i+1, 1, 1,
		  "NONAME", string, &anynull, &status);
    fits_read_col(fptr, TDOUBLE, col_xyz, i+1, 1, 1,
		  NULL, &(header->antenna[i].x), NULL, &status);
    fits_read_col(fptr, TDOUBLE, col_xyz, i+1, 2, 1,
		  NULL, &(header->antenna[i].y), NULL, &status);
    fits_read_col(fptr, TDOUBLE, col_xyz, i+1, 3, 1,
		  NULL, &(header->antenna[i].z), NULL, &status);
    if (DEBUG)
      fprintf(stderr, "    %s %8.3f %8.3f %8.3f\n", header->antenna[i].name,
	      header->antenna[i].x, header->antenna[i].y, header->antenna[i].z);
  }
  fprintf(stderr, "    %d antennas ", (int) nrows);
  for (i=0; i < nrows; i++)
    fprintf(stderr, " %s", header->antenna[i].name);
  fprintf(stderr, "\n");
  return status ? 1 : 0;
}
示例#20
0
int read_data(const char *filename,const char *extname, int colnum, long firstelem, long nelements, double *data)
{
    firstelem++; //from 0 to 1 based
    int status = 0, anynul;
    double doublenull = 0.;
    fitsfile *fptr;         
    fits_open_file(&fptr, filename, READONLY, &status);
    fits_movnam_hdu(fptr, BINARY_TBL, extname, 0, &status);
    fits_read_col(fptr, TDOUBLE, colnum, firstelem, 1, nelements, &doublenull, data, &anynul, &status);
    fits_close_file(fptr, &status);
    if (status)          /* print any error messages */
        fits_report_error(stderr, status);
    return(status);
}
示例#21
0
int read_offs ( char *name, int subint, double *offs, int nchan, int npol)
//int main (int argc, char *argv[] )
{  
    fitsfile *fptr;       // pointer to the FITS file, defined in fitsio.h 
    int status;
    int colnum;

    status = 0;

    if ( fits_open_file(&fptr, name, READONLY, &status) )          // open the file
    //if ( fits_open_file(&fptr, argv[1], READONLY, &status) )          // open the file
    {
        printf( "error while openning file\n" );
    }

    if ( fits_get_colnum(fptr, CASEINSEN, "DAT_OFFS", &colnum, &status) )           // get the colnum number
    {
        printf( "error while getting the colnum number\n" );
		//fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status);
	}
    //printf ("%d\n", colnum);

    int frow;
    int	felem = 1;
    int nelem = nchan*npol;
    int null = 0;
    int	anynull = 0;

	//int subint = 1;
	//int nchan = 8;
	//double wts[nchan];
	frow = subint;

    fits_read_col(fptr, TDOUBLE, colnum, frow, felem, nelem, &null, offs, &anynull, &status);           // read the column

	//int i;
    //for (i = 0; i < nchan; i++)                             // print the results
	//{
		//puts(line[0]);
    //    printf("%lf\n", wts[i]);
		//fprintf (fp, "%s\n", line[0]);
	//}

    if ( fits_close_file(fptr, &status) )
    {
        printf( " error while closing the file \n" );
    }

    return 0;
}
示例#22
0
void WMAPSource::addToMetadata( fitsfile *ffits, const int iNumCols, int &iStatus )
{
  QString str;
  char charTemplate[ FLEN_CARD ];
  char charName[ FLEN_CARD ];
  long lRepeat;
  long lWidth;
  int iTypeCode;
  int iColNumber;
  int iResult;
  int entry;
  int col;

  for( col=0; col<iNumCols; col++ )
  {
    iResult = fits_get_coltype( ffits, col+1, &iTypeCode, &lRepeat, &lWidth, &iStatus );
    if( iResult == 0 )
    {
      sprintf( charTemplate, "%d", col+1 );
      iResult = fits_get_colname( ffits, CASEINSEN, charTemplate, charName, &iColNumber, &iStatus );
      if( iResult == 0 )
      {
        if( lRepeat == 3 )
        {
          QString strValue;
          double dNan = strtod( "nan", NULL );
          double value;
          int iAnyNull;

          for( entry=0; entry<lRepeat; entry++ )
          {
            iResult = fits_read_col( ffits, TDOUBLE, iColNumber, 1, entry+1, 1, &dNan, &value, &iAnyNull, &iStatus );
            if( iResult == 0 )
            {
              KstString *metaString;
              QString keyname = QString("%1_%2").arg(charName).arg(QChar('X'+entry));
              KstObjectTag newTag( keyname, tag( ) );

              strValue = QString("%1").arg(value);
              metaString = new KstString( newTag, this, strValue );
              _metaData.insert( keyname, metaString );
            }
          }
        }
      }
    }
  }
}
void InputFileFITS::_read(int ncol, std::vector<T>& buff, int type, long frow, long lrow) {
	int status = 0;
	if(!isOpened())
		throwException("Error in InputFileFITS::read() ", status);

	int anynull;
	long nelem = lrow - frow + 1;
	long null = 0;

	buff.resize(nelem);

	fits_read_col(infptr, type, ncol+1, frow+1, 1, nelem, &null,  &buff[0], &anynull, &status);

	if(status)
		throwException("Error in InputFileFITS::read() ", status);
}
示例#24
0
文件: FindSig.c 项目: iGoab/rrat_code
/*
如果是IQUV模式,则按此函数来初始化一个二维unsigned char型数组
*/
void initTotalI(fitsfile* fptr,struct Fitsinfo* fi,unsigned char** iptr,int* status) {
	int n = fi->nROW; int k = fi->nSBLK; int m = fi->nCHAN;
	int step = fi->nCHAN*fi->nPOL; int start;
	unsigned char fluxI[m];
	int dataCol; int nulval = 0; int anynull; int i,j,p;
	fits_get_colnum(fptr,CASEINSEN,"DATA",&dataCol,status);
	for (i=0; i<n; i++) {
		for (j=0; j<k; j++) {
			start = 1+j*step;
			fits_read_col(fptr,TBYTE,dataCol,i+1,start,m,&nulval,fluxI,&anynull,status);
			for (p=0; p<m; p++) {
				iptr[j+i*k][p] = fluxI[p];
			}
		}
		printf("All Freq I init:Totalrow %d,row completed %d\n",n,i);
	}
}
示例#25
0
/**
 * Function: get_lcolumn_from_SPC_opened
 * The function reads the values from a column with long specified
 * by its name into a array of type long. The array is returned.
 *
 * Parameters:
 * @param SPC_ptr   - pointer to the SPC extension
 * @param count_col - name of the column to load
 * @param nelems    - number of elements in the column
 *
 * Returns:
 * @return values - pointer to a filled array
 */
long *
get_lcolumn_from_SPC_opened(fitsfile *SPC_ptr, char colname[], int nelems)
{
  int colnum=0;
  int anynul;
  int f_status=0;
  long *values;

  // allocate the return array;
  // give an error if allocation fails
  values = (long *) malloc(nelems * sizeof(long));
  if (!values) {
    aXe_message (aXe_M_ERROR, __FILE__, __LINE__,
		 "Memory allocation failed");
  }

  // get the desired column number;
  // give an error if the column name
  // can not be read
  fits_get_colnum (SPC_ptr, CASEINSEN, colname, &colnum, &f_status);
  if (f_status)
    {
      ffrprt (stderr, f_status);
      aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		   "get_dcolumn_from_SPC_opened: "
		   "Could not determine column %s in "
		   " table!\n", colname);
    }

  // read all data in the column
  fits_read_col (SPC_ptr, TLONG, colnum, 1, 1, nelems, NULL, values,
                    &anynul, &f_status);
  if (f_status)
    {
      ffrprt (stderr, f_status);
      aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		   "get_dcolumn_from_SPC_opened: "
		   "Could not read column %s"
		   " from BINARY table!",colname);
    }

  // return the filled array
  return values;
}
示例#26
0
// Reads a Healpix FITS file containing the map2alm weights into a double array:
int ReadHealpixWeights(int col, int nside, double *weights) {
  char message[200];
  std::string filename;
  fitsfile *fpointer;
  int status=0, anynul=0;
  long i, firstrow=1, firstelem=1, nelements=2*nside;
  double *nulval;
  
  filename = HEALPIX_DATA;
  if (filename.at(filename.length()-1)!='/') filename = filename+"/";
  filename = filename+"weight_ring_n"+ZeroPad(nside, 10000)+".fits";

  // Open file:
  fits_open_table(&fpointer, filename.c_str(), READONLY, &status);
  if (status!=0) {
    sprintf(message, "ReadHealpixWeights: could not open table in FITS file, ERR=%d", status);
    warning(message);
  }
  
  // Prepare to, read and check for errors:
  nulval = vector<double>(0, nelements-1);
  for(i=0; i<nelements; i++) nulval[i]=666.0;
  fits_read_col(fpointer, TDOUBLE, col, firstrow, firstelem, nelements, nulval, weights, &anynul, &status);
  if (status!=0) {
    sprintf(message, "ReadHealpixWeights: problem reading column in FITS file table, ERR=%d", status);
    warning(message);
  }
  if(anynul!=0) {
    warning("ReadHealpixWeights: found NULL values in FITS table");
    printf("They are:\n");
    for (i=0; i<nelements; i++) printf("%g ",nulval[i]);
    printf("\n");
  }
  free_vector(nulval, 0, nelements-1);

  // Close file and exit:
  fits_close_file(fpointer, &status);
  if (status!=0) {
    sprintf(message, "ReadHealpixWeights: could not close FITS file, ERR=%d", status);
    warning(message);
  }
  return status;
}
示例#27
0
int cfits_read_int_col (int *column_values, long num_values,
                       long firstrow, const char *colname, cfitsfile *fptr)
{
   int datatype = TINT;
   int nulval = 0;

   int status = 0;
   long firstelem = 1;
   int anynul, colnum;

   if (-1 == cfits_get_colnum (&colnum, colname, fptr))
     {
       column_values = NULL;
       return -1;
     }

   (void) fits_read_col ((fitsfile *) fptr, datatype, colnum, firstrow,
                        firstelem, num_values, (void *) &nulval,
                        (void *) column_values, &anynul, &status);
   cfits_report_error (status);

   if (status != 0) return -1;
   return 0;
}
示例#28
0
int get_PSRFITS_rawblock(float *fdata, struct spectra_info *s, int *padding)
// This routine reads a single block (i.e subint) from the input files
// which contain raw data in PSRFITS format.  If padding is
// returned as 1, then padding was added and statistics should not be
// calculated.  Return 1 on success.
{
    int numtopad = 0, numtoread, status = 0, anynull;
    float *fdataptr = fdata;
    double offs_sub;
    
    fdataptr = fdata + numbuffered * s->num_channels;
    // numtoread is always this size since we need to read
    // full PSRFITS subints...
    numtoread = s->spectra_per_subint;
    
    // If our buffer array is offset from last time, 
    // copy the previously offset part into the beginning.
    // New data comes after the old data in the buffer.
    if (numbuffered)
        memcpy(fdata, fdata + numtoread * s->num_channels, 
               numbuffered * s->num_channels * sizeof(float));
    
    // Make sure our current file number is valid
    if (cur_file >= s->num_files) return 0;
    
    // Read a subint of data from the DATA col
    if (cur_subint <= s->num_subint[cur_file]) {
        
        // Read the OFFS_SUB column value in case there were dropped blocks
        fits_read_col(s->fitsfiles[cur_file], TDOUBLE, 
                      s->offs_sub_col, cur_subint, 1L, 1L, 
                      0, &offs_sub, &anynull, &status);
        
        // Set last_offs_sub to proper value
        if (cur_subint==1 && (offs_sub < s->time_per_subint)) {
            if (cur_file==0) {
                // This is for the first time
                last_offs_sub = offs_sub - s->time_per_subint;
            } else {
                // And this is if we are combining observations
                last_offs_sub = (s->start_spec[cur_file] - 
                                 (cur_spec + numbuffered)) * s->dt - 
                    0.5 * s->time_per_subint;
            }
        }
        
        // The following determines if there were lost blocks.  We should
        // always be within 0.5 sample (and only that big if we are putting
        // two different observations together).
        if (fabs((offs_sub - last_offs_sub) - s->time_per_subint) < 0.5 * s->dt) {
            // if things look good, with no missing blocks, read the data
            get_PSRFITS_subint(fdataptr, cdatabuffer, s);
            last_offs_sub = offs_sub;
            cur_subint++;
            goto return_block;
        } else {
            if (offs_sub < last_offs_sub) { // Files out of order?  Shouldn't get here.
                fprintf(stderr, "Error!:  Current subint has earlier time than previous!\n\n"
                        "\tfilename = '%s', subint = %d\n"
                        "\tlast_offs_sub = %20.15f  offs_sub = %20.15f\n",
                        s->filenames[cur_file], cur_subint, last_offs_sub, offs_sub);
                exit(1);
            } else {  // there is some missing data, use padding
                numtopad = (int)round((offs_sub - last_offs_sub) / s->dt) - numbuffered;
                if (numtopad > s->spectra_per_subint) numtopad = s->spectra_per_subint;
                add_padding(fdataptr, s->padvals, s->num_channels, numtopad);
                // Update the time of the last subs based on this padding
                last_offs_sub += numtopad * s->dt;
                // Update pointer into the buffer
                numbuffered = (numbuffered + numtopad) % s->spectra_per_subint;
                // Set the padding flag
                *padding = 1;
                // If we haven't gotten a full block, or completed the buffered one
                // then recursively call get_PSRFITS_rawblock()
                if (numbuffered)
                    return get_PSRFITS_rawblock(fdata, s, padding);
                else
                    goto return_block;
            }
        }
        
    } else {
        // We can't read anymore...  so read OFFS_SUB for the last row
        // of the current file to see about padding
        fits_read_col(s->fitsfiles[cur_file], TDOUBLE, 
                      s->offs_sub_col, s->num_subint[cur_file], 1L, 1L, 
                      0, &offs_sub, &anynull, &status);
    }
    
    if (s->num_pad[cur_file]==0 ||
        (fabs(last_offs_sub - offs_sub) < 0.5 * s->dt)) {
        // No padding is necessary.  The second check means that the
        // lack of data we noticed upon reading the file was due to
        // dropped data in the middle of the file that we already
        // fixed.  So no padding is really necessary.
        cur_file++;
        cur_subint = 1;
        return get_PSRFITS_rawblock(fdata, s, padding);
    } else { // add padding
        // Set offs_sub to the correct offs_sub time for the first
        // row of the next file.  Since that might or might not
        // be from a different observation, use absolute times from
        // the start of the observation for both offs_sub and last_offs_sub
        offs_sub = s->start_spec[cur_file+1] * s->dt + 0.5 * s->time_per_subint;
        last_offs_sub = (cur_spec + numbuffered) * s->dt - 0.5 * s->time_per_subint;
        // Compute the amount of required padding
        numtopad = (int)round((offs_sub - last_offs_sub) / s->dt) - numbuffered;
        if (numtopad > s->spectra_per_subint) numtopad = s->spectra_per_subint;
        add_padding(fdataptr, s->padvals, s->num_channels, numtopad);
        // Update the time of the last subs based on this padding
        last_offs_sub += numtopad * s->dt;
        // Update pointer into the buffer
        numbuffered = (numbuffered + numtopad) % s->spectra_per_subint;
        // Set the padding flag
        *padding = 1;
        // If we haven't gotten a full block, or completed the buffered one
        // then recursively call get_PSRFITS_rawblock()
        if (numbuffered)
            return get_PSRFITS_rawblock(fdata, s, padding);
        else
            goto return_block;
    }
    
return_block:
    // Apply the corrections that need a full block

    // Invert the band if needed
    if (s->apply_flipband) flip_band(fdata, s);

    // Perform Zero-DMing if requested
    if (s->remove_zerodm) remove_zerodm(fdata, s);

    // Increment our static counter (to determine how much data we
    // have written on the fly).
    cur_spec += s->spectra_per_subint;

    return 1;
}
示例#29
0
void read_PSRFITS_files(struct spectra_info *s)
// Read and convert PSRFITS information from a group of files 
// and place the resulting info into a spectra_info structure.
{
    int IMJD, SMJD, itmp, ii, status = 0;
    double OFFS, dtmp;
    long double MJDf;
    char ctmp[80], comment[120];
    
    s->datatype = PSRFITS;
    s->fitsfiles = (fitsfile **)malloc(sizeof(fitsfile *) * s->num_files);
    s->start_subint = gen_ivect(s->num_files);
    s->num_subint = gen_ivect(s->num_files);
    s->start_spec = (long long *)malloc(sizeof(long long) * s->num_files);
    s->num_spec = (long long *)malloc(sizeof(long long) * s->num_files);
    s->num_pad = (long long *)malloc(sizeof(long long) * s->num_files);
    s->start_MJD = (long double *)malloc(sizeof(long double) * s->num_files);
    s->N = 0;
    s->num_beams = 1;
    s->get_rawblock = &get_PSRFITS_rawblock;
    s->offset_to_spectra = &offset_to_PSRFITS_spectra;

    // By default, don't flip the band.  But don't change
    // the input value if it is aleady set to flip the band always
    if (s->apply_flipband==-1) s->apply_flipband = 0;

    // Step through the other files
    for (ii = 0 ; ii < s->num_files ; ii++) {

        // Is the file a PSRFITS file?
        if (!is_PSRFITS(s->filenames[ii])) {
            fprintf(stderr, 
                    "\nError!  File '%s' does not appear to be PSRFITS!\n", 
                    s->filenames[ii]);
            exit(1);
        }
        
        // Open the PSRFITS file
        fits_open_file(&(s->fitsfiles[ii]), s->filenames[ii], READONLY, &status);

        // Is the data in search mode?
        fits_read_key(s->fitsfiles[ii], TSTRING, "OBS_MODE", ctmp, comment, &status);
        // Quick fix for Parkes DFB data (SRCH?  why????)...
        if (strcmp("SRCH", ctmp)==0) {
            strncpy(ctmp, "SEARCH", 40);
        }
        if (strcmp(ctmp, "SEARCH")) {
            fprintf(stderr, 
                    "\nError!  File '%s' does not contain SEARCH-mode data!\n", 
                    s->filenames[ii]);
            exit(1);
        }

        // Now get the stuff we need from the primary HDU header
        fits_read_key(s->fitsfiles[ii], TSTRING, "TELESCOP", ctmp, comment, &status); \
        // Quick fix for MockSpec data...
        if (strcmp("ARECIBO 305m", ctmp)==0) {
            strncpy(ctmp, "Arecibo", 40);
        }
        // Quick fix for Parkes DFB data...
        {
            char newctmp[80];

            // Copy ctmp first since strlower() is in-place
            strcpy(newctmp, ctmp);
            if (strcmp("parkes", strlower(remove_whitespace(newctmp)))==0) {
                strncpy(ctmp, "Parkes", 40);
            }
        }
        if (status) {
            printf("Error %d reading key %s\n", status, "TELESCOP");
            if (ii==0) s->telescope[0]='\0';
            if (status==KEY_NO_EXIST) status=0;
        } else {
            if (ii==0) strncpy(s->telescope, ctmp, 40);
            else if (strcmp(s->telescope, ctmp)!=0)
                printf("Warning!:  %s values don't match for files 0 and %d!\n",
                       "TELESCOP", ii);
        }

        get_hdr_string("OBSERVER", s->observer);
        get_hdr_string("SRC_NAME", s->source);
        get_hdr_string("FRONTEND", s->frontend);
        get_hdr_string("BACKEND", s->backend);
        get_hdr_string("PROJID", s->project_id);
        get_hdr_string("DATE-OBS", s->date_obs);
        get_hdr_string("FD_POLN", s->poln_type);
        get_hdr_string("RA", s->ra_str);
        get_hdr_string("DEC", s->dec_str);
        get_hdr_double("OBSFREQ", s->fctr);
        get_hdr_int("OBSNCHAN", s->orig_num_chan);
        get_hdr_double("OBSBW", s->orig_df);
        //get_hdr_double("CHAN_DM", s->chan_dm);
        get_hdr_double("BMIN", s->beam_FWHM);

        /* This is likely not in earlier versions of PSRFITS so */
        /* treat it a bit differently                           */
        fits_read_key(s->fitsfiles[ii], TDOUBLE, "CHAN_DM", 
                      &(s->chan_dm), comment, &status);
        if (status==KEY_NO_EXIST) {
            status = 0;
            s->chan_dm = 0.0;
        }

        // Don't use the macros unless you are using the struct!
        fits_read_key(s->fitsfiles[ii], TINT, "STT_IMJD", &IMJD, comment, &status);
        s->start_MJD[ii] = (long double) IMJD;
        fits_read_key(s->fitsfiles[ii], TINT, "STT_SMJD", &SMJD, comment, &status);
        fits_read_key(s->fitsfiles[ii], TDOUBLE, "STT_OFFS", &OFFS, comment, &status);
        s->start_MJD[ii] += ((long double) SMJD + (long double) OFFS) / SECPERDAY;

        // Are we tracking?
        fits_read_key(s->fitsfiles[ii], TSTRING, "TRK_MODE", ctmp, comment, &status);
        itmp = (strcmp("TRACK", ctmp)==0) ? 1 : 0;
        if (ii==0) s->tracking = itmp;
        else if (s->tracking != itmp)
            printf("Warning!:  TRK_MODE values don't match for files 0 and %d!\n", ii);

        // Now switch to the SUBINT HDU header
        fits_movnam_hdu(s->fitsfiles[ii], BINARY_TBL, "SUBINT", 0, &status);
        get_hdr_double("TBIN", s->dt);
        get_hdr_int("NCHAN", s->num_channels);
        get_hdr_int("NPOL", s->num_polns);
        get_hdr_string("POL_TYPE", s->poln_order);
        fits_read_key(s->fitsfiles[ii], TINT, "NCHNOFFS", &itmp, comment, &status);
        if (itmp > 0)
            printf("Warning!:  First freq channel is not 0 in file %d!\n", ii);
        get_hdr_int("NSBLK", s->spectra_per_subint);
        get_hdr_int("NBITS", s->bits_per_sample);
        fits_read_key(s->fitsfiles[ii], TINT, "NAXIS2", 
                      &(s->num_subint[ii]), comment, &status);
        fits_read_key(s->fitsfiles[ii], TINT, "NSUBOFFS", 
                      &(s->start_subint[ii]), comment, &status);
        s->time_per_subint = s->dt * s->spectra_per_subint;

        /* This is likely not in earlier versions of PSRFITS so */
        /* treat it a bit differently                           */
        fits_read_key(s->fitsfiles[ii], TFLOAT, "ZERO_OFF", 
                      &(s->zero_offset), comment, &status);
        if (status==KEY_NO_EXIST) {
            status = 0;
            s->zero_offset = 0.0;
        }
        s->zero_offset = fabs(s->zero_offset);

        // Get the time offset column info and the offset for the 1st row
        {
            double offs_sub;
            int colnum, anynull, numrows;

            // Identify the OFFS_SUB column number
            fits_get_colnum(s->fitsfiles[ii], 0, "OFFS_SUB", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the OFFS_SUB column!\n");
                status = 0; // Reset status
            } else {
                if (ii==0) {
                    s->offs_sub_col = colnum;
                } else if (colnum != s->offs_sub_col) {
                    printf("Warning!:  OFFS_SUB column changes between files!\n");
                }
            }

            // Read the OFFS_SUB column value for the 1st row
            fits_read_col(s->fitsfiles[ii], TDOUBLE,
                          s->offs_sub_col, 1L, 1L, 1L,
                          0, &offs_sub, &anynull, &status);

            numrows = (int)((offs_sub - 0.5 * s->time_per_subint) /
                            s->time_per_subint + 1e-7);
            // Check to see if any rows have been deleted or are missing
            if (numrows > s->start_subint[ii]) {
                printf("Warning: NSUBOFFS reports %d previous rows\n"
                       "         but OFFS_SUB implies %d.  Using OFFS_SUB.\n"
                       "         Will likely be able to correct for this.\n",
                       s->start_subint[ii], numrows);
            }
            s->start_subint[ii] = numrows;
        }

        // This is the MJD offset based on the starting subint number
        MJDf = (s->time_per_subint * s->start_subint[ii]) / SECPERDAY;
        // The start_MJD values should always be correct
        s->start_MJD[ii] += MJDf;

        // Compute the starting spectra from the times
        MJDf = s->start_MJD[ii] - s->start_MJD[0];
        if (MJDf < 0.0) {
            fprintf(stderr, "Error!: File %d seems to be from before file 0!\n", ii); 
            exit(1);
        }
        s->start_spec[ii] = (long long)(MJDf * SECPERDAY / s->dt + 0.5);

        // Now pull stuff from the other columns
        {
            float ftmp;
            long repeat, width;
            int colnum, anynull;
            
            // Identify the data column and the data type
            fits_get_colnum(s->fitsfiles[ii], 0, "DATA", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the DATA column!\n");
                status = 0; // Reset status
            } else {
                if (ii==0) {
                    s->data_col = colnum;
                    fits_get_coltype(s->fitsfiles[ii], colnum, &(s->FITS_typecode), 
                                     &repeat, &width, &status);
                } else if (colnum != s->data_col) {
                    printf("Warning!:  DATA column changes between files!\n");
                }
            }
            
            // Telescope azimuth
            fits_get_colnum(s->fitsfiles[ii], 0, "TEL_AZ", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                s->azimuth = 0.0;
                status = 0; // Reset status
            } else {
                fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 
                              1L, 1L, 1L, 0, &ftmp, &anynull, &status);
                if (ii==0) s->azimuth = (double) ftmp;
            }
            
            // Telescope zenith angle
            fits_get_colnum(s->fitsfiles[ii], 0, "TEL_ZEN", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                s->zenith_ang = 0.0;
                status = 0; // Reset status
            } else {
                fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 
                              1L, 1L, 1L, 0, &ftmp, &anynull, &status);
                if (ii==0) s->zenith_ang = (double) ftmp;
            }
            
            // Observing frequencies
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_FREQ", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel freq column!\n");
                status = 0; // Reset status
            } else {
                int jj;
                float *freqs = (float *)malloc(sizeof(float) * s->num_channels);
                fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 1L, 1L, 
                              s->num_channels, 0, freqs, &anynull, &status);
                
                if (ii==0) {
                    s->df = freqs[1]-freqs[0];
                    s->lo_freq = freqs[0];
                    s->hi_freq = freqs[s->num_channels-1];
                    // Now check that the channel spacing is the same throughout
                    for (jj = 0 ; jj < s->num_channels - 1 ; jj++) {
                        ftmp = freqs[jj+1] - freqs[jj];
                        if (fabs(ftmp - s->df) > 1e-7)
                            printf("Warning!:  Channel spacing changes in file %d!\n", ii);
                    }
                } else {
                    ftmp = fabs(s->df-(freqs[1]-freqs[0]));
                    if (ftmp > 1e-7)
                        printf("Warning!:  Channel spacing changes between files!\n");
                    ftmp = fabs(s->lo_freq-freqs[0]);
                    if (ftmp > 1e-7)
                        printf("Warning!:  Low channel changes between files!\n");
                    ftmp = fabs(s->hi_freq-freqs[s->num_channels-1]);
                    if (ftmp > 1e-7)
                        printf("Warning!:  High channel changes between files!\n");
                }
                free(freqs);
            }
            
            // Data weights
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_WTS", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel weights!\n");
                status = 0; // Reset status
            } else {
                if (s->apply_weight < 0) { // Use the data to decide
                    int jj;
                    if (ii==0) {
                        s->dat_wts_col = colnum;
                    } else if (colnum != s->dat_wts_col) {
                        printf("Warning!:  DAT_WTS column changes between files!\n");
                    }
                    float *fvec = (float *)malloc(sizeof(float) * s->num_channels);
                    fits_read_col(s->fitsfiles[ii], TFLOAT, s->dat_wts_col, 1L, 1L, 
                                  s->num_channels, 0, fvec, &anynull, &status);
                    for (jj = 0 ; jj < s->num_channels ; jj++) {
                        // If the weights are not 1, apply them
                        if (fvec[jj] != 1.0) {
                            s->apply_weight = 1;
                            break;
                        }
                    }
                    free(fvec);
                }
                if (s->apply_weight < 0) s->apply_weight = 0;  // not needed
            }
            
            // Data offsets
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_OFFS", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel offsets!\n");
                status = 0; // Reset status
            } else {
                if (s->apply_offset < 0) { // Use the data to decide
                    int jj;
                    if (ii==0) {
                        s->dat_offs_col = colnum;
                    } else if (colnum != s->dat_offs_col) {
                        printf("Warning!:  DAT_OFFS column changes between files!\n");
                    }
                    float *fvec = (float *)malloc(sizeof(float) * 
                                                  s->num_channels * s->num_polns);
                    fits_read_col(s->fitsfiles[ii], TFLOAT, s->dat_offs_col, 1L, 1L, 
                                  s->num_channels * s->num_polns, 
                                  0, fvec, &anynull, &status);
                    for (jj = 0 ; jj < s->num_channels * s->num_polns ; jj++) {
                        // If the offsets are not 0, apply them
                        if (fvec[jj] != 0.0) {
                            s->apply_offset = 1;
                            break;
                        }
                    }
                    free(fvec);
                }
                if (s->apply_offset < 0) s->apply_offset = 0; // not needed
            }
            
            // Data scalings
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_SCL", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel scalings!\n");
                status = 0; // Reset status
            } else {
                if (s->apply_scale < 0) { // Use the data to decide
                    int jj;
                    if (ii==0) {
                        s->dat_scl_col = colnum;
                    } else if (colnum != s->dat_scl_col) {
                        printf("Warning!:  DAT_SCL column changes between files!\n");
                    }
                    float *fvec = (float *)malloc(sizeof(float) * 
                                                  s->num_channels * s->num_polns);
                    fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 1L, 1L, 
                                  s->num_channels * s->num_polns, 
                                  0, fvec, &anynull, &status);
                    for (jj = 0 ; jj < s->num_channels * s->num_polns ; jj++) {
                        // If the scales are not 1, apply them
                        if (fvec[jj] != 1.0) {
                            s->apply_scale = 1;
                            break;
                        }
                    }
                    free(fvec);
                }
                if (s->apply_scale < 0) s->apply_scale = 0; // not needed
            }
        }
        
        // Compute the samples per file and the amount of padding
        // that the _previous_ file has
        s->num_pad[ii] = 0;
        s->num_spec[ii] = s->spectra_per_subint * s->num_subint[ii];
        if (ii > 0) {
            if (s->start_spec[ii] > s->N) { // Need padding
                s->num_pad[ii-1] = s->start_spec[ii] - s->N;
                s->N += s->num_pad[ii-1];
            }
        }
        s->N += s->num_spec[ii];
    }

    // Convert the position strings into degrees
    {
        int d, h, m;
        double sec;
        ra_dec_from_string(s->ra_str, &h, &m, &sec);
        s->ra2000 = hms2rad(h, m, sec) * RADTODEG;
        ra_dec_from_string(s->dec_str, &d, &m, &sec);
        s->dec2000 = dms2rad(d, m, sec) * RADTODEG;
    }

    // Are the polarizations summed?
    if ((strncmp("AA+BB", s->poln_order, 5)==0) ||
        (strncmp("INTEN", s->poln_order, 5)==0))
        s->summed_polns = 1;
    else
        s->summed_polns = 0;

    // Calculate some others
    s->T = s->N * s->dt;
    s->orig_df /= (double) s->orig_num_chan;
    s->samples_per_spectra = s->num_polns * s->num_channels;
    // Note:  the following is the number of bytes that will be in
    //        the returned array from CFITSIO.
    //        CFITSIO turns bits into bytes when FITS_typecode=1
    //        and we turn 2-bits or 4-bits into bytes if bits_per_sample < 8
    if (s->bits_per_sample < 8)
        s->bytes_per_spectra = s->samples_per_spectra;
    else
        s->bytes_per_spectra = (s->bits_per_sample * s->samples_per_spectra) / 8;
    s->samples_per_subint = s->samples_per_spectra * s->spectra_per_subint;
    s->bytes_per_subint = s->bytes_per_spectra * s->spectra_per_subint;
    
    // Flip the band?
    if (s->hi_freq < s->lo_freq) {
        float ftmp = s->hi_freq;
        s->hi_freq = s->lo_freq;
        s->lo_freq = ftmp;
        s->df *= -1.0;
        s->apply_flipband = 1;
    }
    // Compute the bandwidth
    s->BW = s->num_channels * s->df;

    // Flip the bytes for Parkes FB_1BIT data
    if (s->bits_per_sample==1 &&
        strcmp(s->telescope, "Parkes")==0 &&
        strcmp(s->backend, "FB_1BIT")==0) {
        printf("Flipping bit ordering since Parkes FB_1BIT data.\n");
        s->flip_bytes = 1;
    } else {
        s->flip_bytes = 0;
    }

    // Allocate the buffers
    cdatabuffer = gen_bvect(s->bytes_per_subint);
    // Following is twice as big because we use it as a ringbuffer too
    fdatabuffer = gen_fvect(2 * s->spectra_per_subint * s->num_channels);
    s->padvals = gen_fvect(s->num_channels);
    for (ii = 0 ; ii < s->num_channels ; ii++)
        s->padvals[ii] = 0.0;
    offsets = gen_fvect(s->num_channels * s->num_polns);
    scales = gen_fvect(s->num_channels * s->num_polns);
    weights = gen_fvect(s->num_channels);
    // Initialize these if we won't be reading them from the file
    if (s->apply_offset==0) 
        for (ii = 0 ; ii < s->num_channels * s->num_polns ; ii++)
            offsets[ii] = 0.0;
    if (s->apply_scale==0)
        for (ii = 0 ; ii < s->num_channels * s->num_polns ; ii++)
            scales[ii] = 1.0;
    if (s->apply_weight==0)
        for (ii = 0 ; ii < s->num_channels ; ii++)
            weights[ii] = 1.0;
}
示例#30
0
文件: pfits.c 项目: SixByNine/pfits
// Return the number of channels
int readBandpass(fitsfile *fp,float *bpass)
{
  int status=0;
  int samplesperbyte;
  long nrows;
  int colnum;
  int j,pol,subint;
  int initflag=0;
  float val=1;
  float nval = 0;
  long npts;
  int start,end;
  int i,anynul;
  float dat_offs,dat_scl;
  float freq,bw,chanbw;
  long nchan;
  int naxis;
  int typecode;
  long repeat,width;

  // Go to bandpass table
  fits_movnam_hdu(fp,BINARY_TBL,"BANDPASS",1,&status);
  if (status) {
    printf("Unable to move to bandpass table in FITS file\n");
    exit(1);
  }

  fits_get_colnum(fp,CASEINSEN,"DAT_OFFS",&colnum,&status);
  fits_read_col(fp,TFLOAT,colnum,1,1,1,&nval,&dat_offs,&anynul,&status);
  if (status){
    printf("Error in bandpass header 1\n");
    fits_report_error(stderr,status);
    exit(1);
  }
  fits_get_colnum(fp,CASEINSEN,"DAT_SCL",&colnum,&status);
  fits_read_col(fp,TFLOAT,colnum,1,1,1,&nval,&dat_scl,&anynul,&status);
  if (status){
    printf("Error in bandpass header\n");
    fits_report_error(stderr,status);
    exit(1);
    }

  fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status);
  if (status){
    fits_report_error(stderr,status);
    exit(1);
  }
  fits_get_coltype(fp,colnum,&typecode,&repeat,&width,&status);
  nchan = (int)repeat-1; // Don't read the first channel
  printf("nc = %d, na = %d\n",(int)nchan,(int)naxis);
  for (i=1;i<(int)nchan;i++)
    {
      fits_read_col(fp,TFLOAT,colnum,1,i+1,1,&nval,&val,&anynul,&status);
      bpass[i-1] = val*dat_scl + dat_offs;
      if (status){
	fits_report_error(stderr,status);
	exit(1);
      }
    }
  fits_close_file(fp,&status);  
  return nchan;
}