/* 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; }
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; }
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; }
static int get_col_num (fitsfile *f, char *name, int *col) { int status = 0; if (0 == fits_get_colnum (f, CASEINSEN, name, col, &status)) return 0; dump_fits_error (status); return -1; }
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; }
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; }
static int cfits_get_colnum (int *colnum, const char *col_name, cfitsfile *fptr) { int status = 0; int casesen = CASEINSEN; (void) fits_get_colnum ((fitsfile *) fptr, casesen, (char *) col_name, colnum, &status); cfits_report_error (status); if (status != 0) return -1; return 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; }
/* 如果是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); } }
int cfits_get_column_numbers (cfitsfile *ft, unsigned int num, const char **names, int *cols) { unsigned int i; int status = 0; if (NULL == ft) return -1; for (i = 0; i < num; i++) { if (fits_get_colnum ((fitsfile *)ft, 1, (char *)names[i], cols + i, &status)) { status = 0; cols[i] = -1; } } return 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; }
/* 初始化整个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; }
int cfits_col_exist (const char *col_name, cfitsfile *fptr) { int status = 0; int casesen = CASEINSEN; int colnum; if (NULL == col_name || NULL == fptr) return -1; (void) fits_get_colnum ((fitsfile *) fptr, casesen, (char *)col_name, &colnum, &status); if (status == 0) return 1; /*yes, column exists */ else { fits_clear_errmsg (); return 0; /*no, column doesn't exist */ } }
int quickfits_read_map_header(const char* filename , fitsinfo_map* fitsi) { /* Read in map header information INPUTS: char* tfilename : c string = name of FITS file to be read OUTPUTS: ra = right ascention dec = declination object = name of source freq = frequency cell = cellsize (degrees) dim = image size bmaj, bmin, bpa = beam information (degrees) */ fitsfile *fptr; int status,i,j; int err; char comment[FLEN_VALUE]; char key_name[FLEN_VALUE]; char key_type[FLEN_VALUE]; char beamhdu[]="AIPS CG "; char cchdu[]="AIPS CC "; char bmajname[]="BMAJ"; char bminname[]="BMIN"; char bpaname[]="BPA"; int colnum; double temp; float floatbuff; float float_null=0; int int_null=0; long longbuff; status = 0; // for error processing err=0; if ( fits_open_file(&fptr,filename, READONLY, &status) ) // open file and make sure it's open { printf("ERROR : quickfits_read_map_header --> Error opening FITS file, error = %d\n",status); return(status); } if (fits_movabs_hdu(fptr,1,&i,&status)) // move to main AIPS image HDU (assuming it's the first one) { printf("ERROR : quickfits_read_map_header --> Error locating AIPS ACSII table extension, error = %d\n",status); printf("ERROR : quickfits_read_map_header --> Did you remember to use the AIPS FITAB task instead of FITTP?\n"); return(status); } // read in some optional keys (these may fail on strange files - but are not that important) fits_read_key(fptr,TSTRING,"OBJECT",fitsi[0].object,comment,&status); fits_read_key(fptr,TSTRING,"OBSERVER",fitsi[0].observer,comment,&status); fits_read_key(fptr,TSTRING,"TELESCOP",fitsi[0].telescope,comment,&status); fits_read_key(fptr,TDOUBLE,"EQUINOX",&fitsi[0].equinox,comment,&status); fits_read_key(fptr,TSTRING,"DATE-OBS",fitsi[0].date_obs,comment,&status); // Now iterate through CTYPE coords to get important information about RA, DEC, cellsize etc. // Most of this data is important - at least notify the user if some is missing i=1; status=0; j=0; while(status!=KEY_NO_EXIST) { if(status != 0) { printf("ERROR : quickfits_read_map_header --> Error reading from %s\n", filename); printf("ERROR : quickfits_read_map_header --> FITSIO error code: %d\n", status); return(1); } sprintf(key_name,"CTYPE%d",i); fits_read_key(fptr,TSTRING,key_name,key_type,comment,&status); if( !strncmp(key_type,"RA---SIN",8) ) { j++; sprintf(key_name,"CRVAL%d",i); fits_read_key(fptr,TDOUBLE,key_name,&fitsi[0].ra,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing RA information %s\n",key_name); status= 0; } sprintf(key_name,"CDELT%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing RA information %s\n",key_name); status= 0; } else { fitsi[0].cell_ra=fabs(temp); } sprintf(key_name,"CRPIX%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing RA information %s\n",key_name); status= 0; } else { fitsi[0].centre_shift[0]=temp; } sprintf(key_name,"CROTA%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing RA information %s\n",key_name); status= 0; } else { fitsi[0].rotations[0]=temp; } } if( !strncmp(key_type,"DEC--SIN",8) ) { j++; sprintf(key_name,"CRVAL%d",i); fits_read_key(fptr,TDOUBLE,key_name,&fitsi[0].dec,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing DEC information %s\n",key_name); status = 0; } sprintf(key_name,"CDELT%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing RA information %s\n",key_name); status= 0; } else { fitsi[0].cell_dec=fabs(temp); } sprintf(key_name,"CRPIX%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing DEC information %s\n",key_name); status = 0; } else { fitsi[0].centre_shift[1]=temp; } sprintf(key_name,"CROTA%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing DEC information %s\n",key_name); status = 0; } else { fitsi[0].rotations[1]=temp; } } if( !strncmp(key_type,"FREQ",4) ) { j++; sprintf(key_name,"CRVAL%d",i); fits_read_key(fptr,TDOUBLE,key_name,&fitsi[0].freq,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing FREQ information %s\n",key_name); status = 0; } sprintf(key_name,"CDELT%d",i); fits_read_key(fptr,TDOUBLE,key_name,&fitsi[0].freq_delta,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing FREQ information %s\n",key_name); status = 0; } } if( !strncmp(key_type,"STOKES",6) ) { sprintf(key_name,"CRVAL%d",i); fits_read_key(fptr,TDOUBLE,key_name,&temp,comment,&status); if(status==KEY_NO_EXIST) { printf("WARNING : quickfits_read_map_header --> Missing Stokes information %s\n",key_name); status = 0; } else { fitsi[0].stokes=(int)(temp); } } i++; } status=0; if(j!=3) { printf("WARNING : quickfits_read_map_header --> Error reading RA, DEC, FREQ information\n"); printf("\t Only %d out of 3 read.\n",j); } fits_read_key(fptr,TDOUBLE,"NAXIS1",&temp,comment,&status); fitsi[0].imsize_ra=(int)(temp); // dim is stored as a double in the FITS file fits_read_key(fptr,TDOUBLE,"NAXIS2",&temp,comment,&status); fitsi[0].imsize_dec=(int)(temp); // dim is stored as a double in the FITS file if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error reading image size from NAXIS1, error = %d\n",err); return(err); } fitsi[0].have_beam = true; // assume true until proved otherwise fits_read_key(fptr,TDOUBLE,"BMAJ",&fitsi[0].bmaj,comment,&status); err += status; fits_read_key(fptr,TDOUBLE,"BMIN",&fitsi[0].bmin,comment,&status); err += status; fits_read_key(fptr,TDOUBLE,"BPA",&fitsi[0].bpa,comment,&status); err += status; if(err!=0) // if the beam information isn't in the main header, move to AIPS CG HDU for beam information { status=0; if (fits_movnam_hdu(fptr,BINARY_TBL,beamhdu,0,&status)) // move to beam information hdu { printf("WARNING : quickfits_read_map_header --> No beam information found.\n"); fitsi[0].bmaj = 0.0; fitsi[0].bmin = 0.0; // changed this because model files don't have any beam information. Should check to make sure beam info is valid in other code fitsi[0].bpa = 0.0; fitsi[0].have_beam = false; } else { fits_get_colnum(fptr,CASEINSEN,bmajname,&colnum,&status); if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error locating BMAJ information, error = %d\n",status); return(err); } fits_read_col(fptr,TFLOAT,colnum,1,1,1,&float_null,&floatbuff,&int_null,&status); if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error reading BMAJ information, error = %d\n",status); return(err); } else { fitsi[0].bmaj=(double)(floatbuff); } fits_get_colnum(fptr,CASEINSEN,bminname,&colnum,&status); if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error locating BMIN information, error = %d\n",status); return(err); } fits_read_col(fptr,TFLOAT,colnum,1,1,1,&float_null,&floatbuff,&int_null,&status); if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error reading BMIN information, error = %d\n",status); return(err); } else { fitsi[0].bmin=(double)(floatbuff); } fits_get_colnum(fptr,CASEINSEN,bpaname,&colnum,&status); if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error locating BPA information, error = %d\n",status); return(err); } fits_read_col(fptr,TFLOAT,colnum,1,1,1,&float_null,&floatbuff,&int_null,&status); if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error reading BPA information, error = %d\n",status); return(err); } else { fitsi[0].bpa=(double)(floatbuff); } } } // move to AIPS CC HDU for clean component information if requested. Read, or give error if a failure occurs. Allow for possibility of the outdated "A3DTABLE" table type 6 as well as normal BINARY_TBL if (fitsi[0].cc_table_version >=0 ) { fits_movnam_hdu(fptr,ANY_HDU,cchdu,fitsi[0].cc_table_version,&status); if (status==0) // move to main AIPS UV hdu { fits_get_num_rows(fptr,&longbuff,&status); fitsi[0].ncc=longbuff; if(status!=0) { printf("ERROR : quickfits_read_map_header --> Error reading number of clean components, error = %d\n",status); return(status); } } else { printf("WARNING : quickfits_read_map_header --> No clean component table detected.\n"); fitsi[0].ncc=0; return(status); } } else { fitsi[0].ncc=0; } status=0; if ( fits_close_file(fptr, &status) ) { printf("ERROR : quickfits_read_map_header --> Error closing FITS file, error = %d\n",status); return(status); } return(status); }
int fits_read_wcstab( fitsfile *fptr, int nwtb, wtbarr *wtb, int *status) { int anynul, colnum, hdunum, iwtb, m, naxis, nostat; long *naxes = 0, nelem; wtbarr *wtbp; if (*status) return *status; if (fptr == 0) { return (*status = NULL_INPUT_PTR); } if (nwtb == 0) return 0; /* Zero the array pointers. */ wtbp = wtb; for (iwtb = 0; iwtb < nwtb; iwtb++, wtbp++) { *wtbp->arrayp = 0x0; } /* Save HDU number so that we can move back to it later. */ fits_get_hdu_num(fptr, &hdunum); wtbp = wtb; for (iwtb = 0; iwtb < nwtb; iwtb++, wtbp++) { /* Move to the required binary table extension. */ if (fits_movnam_hdu(fptr, BINARY_TBL, (char *)(wtbp->extnam), wtbp->extver, status)) { goto cleanup; } /* Locate the table column. */ if (fits_get_colnum(fptr, CASEINSEN, (char *)(wtbp->ttype), &colnum, status)) { goto cleanup; } /* Get the array dimensions and check for consistency. */ if (wtbp->ndim < 1) { *status = NEG_AXIS; goto cleanup; } if (!(naxes = calloc(wtbp->ndim, sizeof(long)))) { *status = MEMORY_ALLOCATION; goto cleanup; } if (fits_read_tdim(fptr, colnum, wtbp->ndim, &naxis, naxes, status)) { goto cleanup; } if (naxis != wtbp->ndim) { if (wtbp->kind == 'c' && wtbp->ndim == 2) { /* Allow TDIMn to be omitted for degenerate coordinate arrays. */ naxis = 2; naxes[1] = naxes[0]; naxes[0] = 1; } else { *status = BAD_TDIM; goto cleanup; } } if (wtbp->kind == 'c') { /* Coordinate array; calculate the array size. */ nelem = naxes[0]; for (m = 0; m < naxis-1; m++) { *(wtbp->dimlen + m) = naxes[m+1]; nelem *= naxes[m+1]; } } else { /* Index vector; check length. */ if ((nelem = naxes[0]) != *(wtbp->dimlen)) { /* N.B. coordinate array precedes the index vectors. */ *status = BAD_TDIM; goto cleanup; } } free(naxes); naxes = 0; /* Allocate memory for the array. */ if (!(*wtbp->arrayp = calloc((size_t)nelem, sizeof(double)))) { *status = MEMORY_ALLOCATION; goto cleanup; } /* Read the array from the table. */ if (fits_read_col_dbl(fptr, colnum, wtbp->row, 1L, nelem, 0.0, *wtbp->arrayp, &anynul, status)) { goto cleanup; } } cleanup: /* Move back to the starting HDU. */ nostat = 0; fits_movabs_hdu(fptr, hdunum, 0, &nostat); /* Release allocated memory. */ if (naxes) free(naxes); if (*status) { wtbp = wtb; for (iwtb = 0; iwtb < nwtb; iwtb++, wtbp++) { if (*wtbp->arrayp) free(*wtbp->arrayp); } } return *status; }
void loadPrimaryHeader(fitsfile *fp,dSet *data) { int status=0; int nkey=-1; int morekeys=-1; int i; char keyname[128],val[128],comment[128]; fits_get_hdrspace(fp,&nkey,&morekeys,&status); data->pheaderSet = 1; data->phead.nhead = nkey; // Allocate memory data->phead.keyname = (char **)malloc(sizeof(char *)*nkey); data->phead.val = (char **)malloc(sizeof(char *)*nkey); data->phead.comment = (char **)malloc(sizeof(char *)*nkey); for (i=0;i<nkey;i++) { data->phead.keyname[i] = (char *)malloc(sizeof(char)*128); data->phead.val[i] = (char *)malloc(sizeof(char)*128); data->phead.comment[i] = (char *)malloc(sizeof(char)*128); } data->pheaderSet=1; // Complete allocating memory for (i=1;i<=nkey;i++) { fits_read_keyn(fp,i+1,data->phead.keyname[i-1],data->phead.val[i-1],data->phead.comment[i-1],&status); if (strcmp(data->phead.keyname[i-1],"OBSFREQ")==0) sscanf(data->phead.val[i-1],"%f",&(data->phead.freq)); else if (strcmp(data->phead.keyname[i-1],"STT_IMJD")==0) sscanf(data->phead.val[i-1],"%d",&(data->phead.imjd)); else if (strcmp(data->phead.keyname[i-1],"STT_SMJD")==0) sscanf(data->phead.val[i-1],"%f",&(data->phead.smjd)); else if (strcmp(data->phead.keyname[i-1],"STT_OFFS")==0) sscanf(data->phead.val[i-1],"%f",&(data->phead.stt_offs)); else if (strcmp(data->phead.keyname[i-1],"OBSBW")==0) sscanf(data->phead.val[i-1],"%f",&(data->phead.bw)); } // Read specific parameters fits_read_key(fp,TSTRING,"OBS_MODE",data->phead.obsMode,NULL,&status); fits_read_key(fp,TSTRING,"SRC_NAME",data->phead.source,NULL,&status); if (status) { fits_report_error(stderr,status); exit(1); } // Now load information from the subintegration table fits_movnam_hdu(fp,BINARY_TBL,"SUBINT",1,&status); if (status) { printf("No subintegration table\n"); data->subintTable=0; status=0; } else { data->subintTable=1; fits_read_key(fp,TINT,"NAXIS2",&(data->phead.nsub),NULL,&status); if (status) { printf("Reading naxis2\n"); fits_report_error(stderr,status); exit(1); } fits_read_key(fp,TINT,"NCHAN",&(data->phead.nchan),NULL,&status); if (status) { printf("Reading nchan\n"); fits_report_error(stderr,status); exit(1); } fits_read_key(fp,TFLOAT,"ZERO_OFF",&(data->phead.zeroOff),NULL,&status); if (status) { printf("Reading zero_off\n"); fits_report_error(stderr,status); data->phead.zeroOff = 0; status=0; } fits_read_key(fp,TINT,"NBITS",&(data->phead.nbits),NULL,&status); if (status) { printf("Reading nbits\n"); fits_report_error(stderr,status); exit(1); } fits_read_key(fp,TINT,"NPOL",&(data->phead.npol),NULL,&status); if (status) { printf("Reading npol\n"); fits_report_error(stderr,status); exit(1); } fits_read_key(fp,TINT,"NSBLK",&(data->phead.nsblk),NULL,&status); if (status) { printf("Reading nsblk\n"); fits_report_error(stderr,status); exit(1); } fits_read_key(fp,TINT,"NBIN",&(data->phead.nbin),NULL,&status); if (status) { printf("Reading nbin\n"); fits_report_error(stderr,status); exit(1); } // printf("nbin = %d (%d)\n",data->phead.nbin,status); fits_read_key(fp,TFLOAT,"CHAN_BW",&(data->phead.chanbw),NULL,&status); if (data->phead.chanbw < 0 && data->phead.bw > 0) data->phead.bw*=-1; fits_read_key(fp,TFLOAT,"TBIN",&(data->phead.tsamp),NULL,&status); } fits_movnam_hdu(fp,BINARY_TBL,"PSRPARAM",1,&status); if (status) { printf("No PSRPARM table\n"); data->psrparamTable=0; status=0; } else { int len,i,colnum; char **line,str1[1024],str2[1024]; data->psrparamTable=1; char nval[128]="UNKNOWN"; int anynul=0; float tt; fits_read_key(fp,TINT,"NAXIS2",&len,NULL,&status); fits_get_colnum(fp,CASEINSEN,"PARAM",&colnum,&status); if (status) { printf("Unable to find data in the psrparam table in FITS file\n"); exit(1); } line = (char **)malloc(sizeof(char *)); line[0] = (char *)malloc(sizeof(char)*1024); for (i=0;i<len;i++) { fits_read_col_str(fp,colnum,i+1,1,1,nval,line,&anynul,&status); if (sscanf(line[0],"%s %s",str1,str2)==2) { if (strcasecmp(str1,"DM")==0) sscanf(str2,"%f",&(data->phead.dm)); if (strcasecmp(str1,"F0")==0) { sscanf(str2,"%f",&tt); data->phead.period = 1.0/tt; } } // printf("Read: %s\n",line[0]); } // printf("Lenght = %d\n",len); free(line[0]); free(line); } }
int writeData(fitsfile *fp,FILE *fout,dSet *data,long s1,long s2,float dm) { long sub_1,samp_1; long sub_2,samp_2; int s,r1,r2; int nsblk; int nchan = data->phead.nchan; int nbits = data->phead.nbits; int npol = data->phead.npol; int samplesperbyte = 8/data->phead.nbits; float chanVal[nchan]; long ipos=0; int status=0; int colnum; int initflag=0; unsigned char *cval; unsigned char nval = '0'; int i,c,count=0,sa; unsigned char chVals[nchan]; int smoothSamp=1; int sm; unsigned int t=0; unsigned int j; float tempVal; int nsampDM; float tDM; float bw = data->phead.bw; // MHz float f0 = data->phead.freq+fabs(data->phead.bw)/2.0; // MHz // Highest frequency float chanbw = data->phead.bw/data->phead.nchan; unsigned char *ring_s; unsigned char *ring_e; unsigned char *ring_pos; unsigned char *ring_last; unsigned int bitCount; int cdelay,k,l; float tdelay; unsigned int pos; unsigned int **cde_byte; unsigned char **cde_bit; unsigned int nchanbyte; nchanbyte = nchan/samplesperbyte; // Do we require smoothing? printf("At here: smoothSamp = %d\n",smoothSamp); nsblk = data->phead.nsblk; findPosSample(data,s1,&sub_1,&samp_1); findPosSample(data,s2,&sub_2,&samp_2); // printf("Here with %d %d %d %d\n",sub_1,samp_1,sub_2,samp_2); // Go to the subint table fits_movnam_hdu(fp,BINARY_TBL,"SUBINT",1,&status); if (status) { printf("Unable to move to subint table in FITS file\n"); exit(1); } fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); if (status) { printf("Unable to find data in the subint table in FITS file\n"); exit(1); } // Number of extra samples required to process // to de-disperse across the band tDM = fabs(4.15e-3*dm*(pow((f0)/1000.0,-2)-pow((f0-fabs(bw))/1000.0,-2))); nsampDM = ceil(tDM/data->phead.tsamp); cval = (unsigned char *)malloc(sizeof(unsigned char)*nchanbyte*npol*nsampDM); printf("Trying to allocate memory\n"); cde_byte = (unsigned int **)malloc(sizeof(unsigned int *)*nsampDM); cde_bit = (unsigned char **)malloc(sizeof(unsigned char *)*nsampDM); for (i=0;i<nsampDM;i++) { cde_byte[i] = (unsigned int *)malloc(sizeof(unsigned int)*nchan*npol*nsampDM); cde_bit[i] = (unsigned char *)malloc(sizeof(unsigned char)*nchan*npol*nsampDM); } printf("Allocated memory\n"); // Now turn on the bits that correspond to the DM j=0; for (i=0;i<nchan;i++) { tdelay = 4.15e-3*dm*(pow(f0/1000.0,-2)-pow((f0-fabs(chanbw)*i)/1000.0,-2)); cdelay = nint(-tdelay/data->phead.tsamp); // printf("Have %g %d\n",tdelay,cdelay); for (j=0;j<nsampDM;j++) { k = (int)((float)i/(float)samplesperbyte)+cdelay*nchanbyte+j*nchan/samplesperbyte; if (k>(nsampDM)*nchanbyte) k-=nsampDM*nchanbyte; if (k>(nsampDM)*nchanbyte) { printf("ERROR\n"); exit(1); } cde_byte[j][i] = k; cde_bit[j][i] = i%8; } } printf("Ready\n"); ring_s = cval; ring_pos = cval; ring_e = &cval[nchanbyte*npol*(nsampDM)]; printf("Got here 2\n"); // Should check if I'm running off the end of a subint s = sub_1; sa = samp_1; // Use a ring buffer to store the data // Fill it up count = 0; t=0; pos = 0; printf("Got here 3\n"); fprintf(fout,"# %s %d %g %g\n",data->phead.source,data->phead.imjd,data->phead.freq,data->phead.tsamp); for (i=0;i<s2-s1-nsampDM;i++) { // printf("i = %d, s = %d, sa = %d\n",i,s,sa); fits_read_col_byt(fp,colnum,s+1,(sa)*nchanbyte+1,nchanbyte,nval,ring_pos,&initflag,&status); sa++; if (i >= nsampDM-1) { bitCount = 0; // for (k=0;k<35;k++) { for (j=0;j<nchan;j++) { // bitCount += extractBit(cval[cde_byte[t][j]],cde_bit[t][j]); // ONLY 1-BIT DATA bitCount += extractBit(cval[cde_byte[t][j]],j%8); // bitCount+=cde_byte[t][j]+cde_bit[t][j]; } } fprintf(fout,"%d\n",bitCount); t++; } ring_pos += nchanbyte*npol; if (ring_pos == ring_e) { // printf("Got to end of ring buffer %d %d\n",i,nsampDM); ring_pos = ring_s; t=0; } if (sa == nsblk) { // printf("Running off the end of a block\n"); s++; sa = 0; } } tempVal=0; // printf("Got here\n"); // exit(1); if (status) { fits_report_error(stderr,status); exit(1); } // printf("Complete %d %d\n",t,count); free(cval); for (i=0;i<nsampDM;i++) { free(cde_byte[i]); free(cde_bit[i]); } free(cde_byte); free(cde_bit); return count; }
int extractPolData(fitsfile *fp,dSet *data,int pol,float *arr,float t1,float t2) { int nsblk; long s1,s2,sub_1,samp_1,sub_2,samp_2,s,r1,r2; int nchan = data->phead.nchan; int nbits = data->phead.nbits; int npol = data->phead.npol; int samplesperbyte = 8/data->phead.nbits; float chanVal[nchan]; long ipos=0; int status=0; int colnum; int initflag=0; unsigned char *cval; unsigned char nval = '0'; int i,c,count=0,sa; unsigned char chVals[nchan]; int smoothSamp=1; int sm; int t=0; float tempVal; long pos=0,p,cc=0; printf("Have t1/t2 %g %g %g\n",t1,t2,(double)data->phead.tsamp); s1 = (long)(t1/data->phead.tsamp); s2 = (long)(t2/data->phead.tsamp); printf("samples per byte = %d\n",samplesperbyte); printf("Searching for %g %g\n",(double)s1,(double)s2); nsblk = data->phead.nsblk; if (s1 < 0) s1=0; findPosSample(data,s1,&sub_1,&samp_1); findPosSample(data,s2,&sub_2,&samp_2); if (sub_1 < 0) sub_1=0; printf("Have samples (%d,%d) (%d,%d)\n",sub_1,samp_1,sub_2,samp_2); fits_movnam_hdu(fp,BINARY_TBL,"SUBINT",1,&status); if (status) { printf("Unable to move to subint table in FITS file\n"); exit(1); } fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); if (status) { printf("Unable to find data in the subint table in FITS file\n"); exit(1); } cval = (unsigned char *)malloc(sizeof(unsigned char)*nsblk*nchan/samplesperbyte*npol); // printf("Allocated memory\n"); t=0; printf("Have sub1/2 = %g %g\n",(double)sub_1,(double)sub_2); for (s=sub_1;s<=sub_2;s++) { // printf("Loading sub: %d\n",s); if (s==sub_1) r1 = samp_1; else r1 = 0; if (s==sub_2) r2 = samp_2; else r2 = nsblk; printf("Reading from %d to %d for subint %d\n",r1,r2,s); // Read nchan data points for (sa=r1;sa<r2;sa++) { fits_read_col_byt(fp,colnum,s+1,sa*nchan/samplesperbyte+1,nchan/samplesperbyte,nval,cval,&initflag,&status); if (status) { fits_report_error(stderr,status); exit(1); } bytesToValues(samplesperbyte, nchan, cval, chVals); tempVal = 0.0; // val[count] = 0.0; for (i=0;i<nchan;i++) { // printf("Here with %g\n",(float)chVals[i]); p = cc+(s2-s1)*i; // printf("pos-p = %d\n",p); // p=pos; if (samplesperbyte==8) { if (chVals[i]==0) arr[p] = 0.5; else arr[p] = -0.5; } else if (samplesperbyte==4) { if (chVals[i]==0) arr[p]=-2.5; else if (chVals[i]==1) arr[p] = -0.5; else if (chVals[i]==2) arr[p] = 0.5; else arr[p] = 2.5; } else { arr[p] = chVals[i]; // printf("Have got %g\n",arr[p]); } // if (chVals[i]==0) arr[pos] = 0.5; // else arr[pos] =- 0.5; // pos++; pos++; } cc++; // pos++; } } printf("Complete %d %d %d\n",pos,s2-s1,nchan); free(cval); return cc; //return pos; }
int extractDataZeroDM(fitsfile *fp,dSet *data,long s1,long s2,float *mean,float *min,float *max,long maxVal,int *nsmooth) { long sub_1,samp_1; long sub_2,samp_2; int s,r1,r2; int nsblk; int nchan = data->phead.nchan; int nbits = data->phead.nbits; int npol = data->phead.npol; int samplesperbyte = 8/data->phead.nbits; float chanVal[nchan]; long ipos=0; int status=0; int colnum; int initflag=0; unsigned char *cval; unsigned char nval = '0'; int i,c,count=0,sa; unsigned char chVals[nchan]; int smoothSamp=1; int sm; int t=0; float tempVal; printf("Extract zeroDM smaplesperbyte = %d\n",samplesperbyte); // Do we require smoothing? if (s2 - s1 > maxVal) { smoothSamp = ceil((double)(s2-s1)/(double)maxVal); // printf("smoothSamp = %d\n",smoothSamp); } nsblk = data->phead.nsblk; findPosSample(data,s1,&sub_1,&samp_1); findPosSample(data,s2,&sub_2,&samp_2); // printf("Here with %d %d %d %d\n",sub_1,samp_1,sub_2,samp_2); // Go to the subint table fits_movnam_hdu(fp,BINARY_TBL,"SUBINT",1,&status); if (status) { printf("Unable to move to subint table in FITS file\n"); exit(1); } fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); if (status) { printf("Unable to find data in the subint table in FITS file\n"); exit(1); } cval = (unsigned char *)malloc(sizeof(unsigned char)*nsblk*nchan/samplesperbyte*npol); // printf("Allocated memory\n"); t=0; for (s=sub_1;s<=sub_2;s++) { // printf("Loading sub: %d\n",s); if (s==sub_1) r1 = samp_1; else r1 = 0; if (s==sub_2) r2 = samp_2; else r2 = nsblk; // printf("Reading from %d to %d\n",r1,r2); // Read nchan data points for (sa=r1;sa<r2;sa++) { fits_read_col_byt(fp,colnum,s+1,sa*nchan/samplesperbyte+1,nchan/samplesperbyte,nval,cval,&initflag,&status); if (status) { fits_report_error(stderr,status); exit(1); } bytesToValues(samplesperbyte, nchan, cval, chVals); tempVal = 0.0; // val[count] = 0.0; for (i=0;i<nchan;i++) { if (samplesperbyte==8) { if (chVals[i]==0) tempVal += 0.5; else tempVal -= 0.5; // printf("Adding %d %d %d %d\n",s,sa,i,chVals[i]); } else { tempVal+=chVals[i]; } } tempVal/=(float)nchan; if (t==0) { mean[count] = min[count] = max[count] = tempVal; } else { if (min[count] > tempVal) min[count] = tempVal; if (max[count] < tempVal) max[count] = tempVal; mean[count] += tempVal; } t++; if (t==smoothSamp) { mean[count]/=(double)smoothSamp; count++; t=0; } } } // printf("Complete %d %d\n",t,count); free(cval); *nsmooth = smoothSamp; if (count > maxVal) { printf("In pfits.c -- this should not have happened\n"); printf("count = %d, maxVal = %d\n",(int)count,(int)maxVal); printf("s2-s1 = %d\n",(int)(s2-s1)); printf("Exit ...\n"); exit(1); } // printf("Returning\n"); return count; }
// 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; }
// Routine to read the header from a PSRFITS file void readpsrfits_hd(char *filename,int *machine_id,int *telescope_id,int *data_type,char *source_name, double *fch1,int *nchans,double *foff,int *nifs,double *tsamp, double *src_raj,double *src_dej,double *tstart,int *nbits,int *ibeam) { fitsfile *fp; int status; float obsbw,fc; char sobsbw[100],sfc[100],ras[100],decs[100]; char name[100],telescope[100]; double h,m,sec,chanbw,smjd,offs; int imjd; int ncol, NCHANS, anynul; unsigned char nulval = 0; long long row, firstelem, nelem; float ch_freq[MX_NCHAN]; // Defaults that are not being set *machine_id=-1; *data_type = 0; *ibeam = 1; status=0; fits_open_file(&fp,filename,READONLY,&status); fits_report_error(stderr,status); fits_movabs_hdu( fp, 1, NULL, &status ); fits_read_key(fp, TSTRING, "SRC_NAME", source_name, NULL, &status); if(strlen(source_name)==0){ strcpy(source_name,"UNKNOWN "); } fits_report_error(stderr,status); if (fits_read_key(fp, TINT, "IBEAM", ibeam, NULL, &status)) { *ibeam = 1; status=0; } fits_report_error(stderr,status); //fits_read_key(fp, TINT, "OBSNCHAN", nchans, NULL, &status); //fits_report_error(stderr,status); fits_read_key(fp, TFLOAT, "OBSBW", &obsbw, NULL, &status); // if (obsbw > 0) obsbw = -obsbw; // We must invert the data // The bandwidth in FITS is always positive // In order to determine whether sigproc should be given a negative bandwidth // the sign of the CHANBW parameter needs to be determined // This is done below fits_report_error(stderr,status); fits_read_key(fp, TFLOAT, "OBSFREQ", &fc, NULL, &status); fits_report_error(stderr,status); fits_read_key(fp, TINT, "STT_IMJD", &imjd, NULL, &status); fits_report_error(stderr,status); fits_read_key(fp, TDOUBLE, "STT_SMJD", &smjd, NULL, &status); fits_report_error(stderr,status); fits_read_key(fp, TDOUBLE, "STT_OFFS", &offs, NULL, &status); fits_report_error(stderr,status); fits_read_key(fp, TSTRING, "TELESCOP", telescope, NULL, &status); fits_report_error(stderr,status); if (strcasecmp(telescope,"PARKES")==0) *telescope_id=4; else if (strcasecmp(telescope,"ARECIBO")==0) *telescope_id=1; else if (strcasecmp(telescope,"JODRELL")==0) *telescope_id=5; else if (strcasecmp(telescope,"GBT")==0) *telescope_id=6; else if (strcasecmp(telescope,"EFFELSBERG")==0) *telescope_id=8; else if (strcasecmp(telescope,"SRT")==0) *telescope_id=10; else *telescope_id = -1; // Start time *tstart = imjd+(smjd+offs)/86400.0; fits_read_key(fp, TSTRING, "RA", ras, NULL, &status); sscanf(ras,"%lf:%lf:%lf",&h,&m,&sec); *src_raj = (double)h*10000.0 + (double)m*100.0 + (double)sec; fits_read_key(fp, TSTRING, "DEC", decs, NULL, &status); sscanf(decs,"%lf:%lf:%lf",&h,&m,&sec); *src_dej = (double)fabs(h)*10000.0 + (double)m*100.0 + (double)sec; if (h < 0) (*src_dej)*=-1; // Now get information from the subint header strcpy(name,"SUBINT"); fits_movnam_hdu(fp, BINARY_TBL, name, 0, &status); fits_read_key(fp, TDOUBLE, "TBIN", tsamp, NULL, &status ); fits_read_key(fp, TINT, "NPOL", nifs, NULL, &status ); fits_read_key(fp, TINT, "NBITS", nbits, NULL, &status ); fits_read_key(fp, TDOUBLE, "CHAN_BW", &chanbw, NULL, &status ); // if (chanbw < 0) obsbw=-(obsbw); fits_movnam_hdu( fp, BINARY_TBL, "SUBINT", 0, &status ); fits_read_key( fp, TINT, "NCHAN", &NCHANS, NULL, &status); fits_get_colnum( fp, CASESEN, "DAT_FREQ", &ncol, &status ); firstelem = 1LL; nelem = NCHANS; *nchans = NCHANS; row = 1LL; fits_read_col( fp, TFLOAT, ncol, row, firstelem, nelem, &nulval, ch_freq, &anynul, &status ); //*fch1 = ch_freq[0] > ch_freq[nelem-1] ? ch_freq[0] : ch_freq[nelem-1]; //*fch1 = fc-obsbw/2.0 + obsbw/(*nchans)/2.0; //*fch1 = fc - ((*nchans-1)/ 2.0)*chanbw; *fch1=ch_freq[0]; *foff = chanbw; fprintf(stderr,"Native fch1=%f, foff=%f\n",*fch1,*foff); if(chanbw > 0){ fprintf(stderr,"Will flip the band!\n"); } fits_close_file(fp,&status); fits_report_error(stderr,status); }
//int main ( int argc, char *argv[] ) int read_value ( char *name, int subint, double *value, int nphase, int nchan, int npol) { //double *read_arrival_time( char *input, long *nrows ) //int subint = 1; //double profile[8*1024]; fitsfile *fptr; // pointer to the FITS file, defined in fitsio.h int status; int colnum; long int nrows; status = 0; //if ( fits_open_file(&fptr, argv[1], READONLY, &status) ) // open the file if ( fits_open_file(&fptr, name, READONLY, &status) ) // open the file { printf( "error while openning file\n" ); } if ( fits_get_num_rows(fptr, &nrows, &status) ) // get the row number { printf( "error while getting the row number\n" ); } //printf ("%ld\n", nrows); //if ( fits_get_colnum(fptr, CASEINSEN, "TSUBINT", &colnum, &status) ) // get the row number if ( fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status) ) // get the row number { printf( "error while getting the colnum number\n" ); //fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status); } //printf ("%d\n", colnum); /////////////////////////////////////////////////////////////////////////// int nbin; int frow; int felem; int nelem; int null; int anynull; //double *profile; // the array to store the profile nbin = nphase; //profile = ( double *)malloc( (nchan*npol*nbin) * sizeof( double ) ); // allocate space for column value frow = subint; felem = 1; nelem = nbin*nchan*npol; //nelem = 1024; null = 0; anynull = 0; fits_read_col(fptr, TDOUBLE, colnum, frow, felem, nelem, &null, value, &anynull, &status); // read the column //int i; //for (i = 0; i < (nchan*npol*nbin); i++) // print the results // printf("%d %lf \n", i, profile[i]); if ( fits_close_file(fptr, &status) ) { printf( " error while closing the file " ); } return 0; }
int understands_wmap( KConfig*, const QString& filename ) { fitsfile* ffits; int iStatus = 0; int iRetVal = 0; // // determine if it is a WMAP file... // if( fits_open_file( &ffits, filename.ascii( ), READONLY, &iStatus ) == 0 ) { int iNumHeaderDataUnits; if( fits_get_num_hdus( ffits, &iNumHeaderDataUnits, &iStatus ) == 0 ) { if( iNumHeaderDataUnits == 6 ) { char value[FLEN_VALUE]; char comment[FLEN_COMMENT]; int iHDUType; int iColNum; int i; int j; for( i=0; i<iNumKeywords; ++i ) { if( iStatus == 0 ) { fits_read_keyword( ffits, keywords[i], value, comment, &iStatus ); } else { break; } } if( iStatus == 0 ) { char **entries = metaDataHeaders; int iNumEntries = iNumMetaDataHeaders; for( i=0; i<iNumHeaderDataUnits-1; i++ ) { switch (i) { case 0: iNumEntries = iNumMetaDataHeaders; entries = metaDataHeaders; break; case 1: iNumEntries = iNumScienceDataHeaders; entries = scienceDataHeaders; break; case 2: iNumEntries = iNumAIHKDataHeaders; entries = AIHKDataHeaders; break; case 3: iNumEntries = iNumDIHKDataHeaders; entries = DIHKDataHeaders; break; case 4: iNumEntries = iNumLOSDataHeaders; entries = LOSDataHeaders; break; default: iNumEntries = iNumMetaDataHeaders; entries = metaDataHeaders; break; } fits_movrel_hdu( ffits, 1, &iHDUType, &iStatus); if( iStatus == 0 && iHDUType == BINARY_TBL ) { for( j=0; j<iNumEntries; ++j ) { if( iStatus == 0 ) { fits_get_colnum( ffits, 0, entries[j], &iColNum, &iStatus ); if( strchr( entries[j], '#' ) != 0L && iStatus == COL_NOT_UNIQUE ) { iStatus = 0; } } else { break; } } } } } if( iStatus == 0 ) { iRetVal = 99; } } } iStatus = 0; fits_close_file( ffits, &iStatus ); } else { // // failed to open the file, so we can't understand it... // } return iRetVal; }
int check_std ( char *name, int subint, int mode, int nchn, int nphase, int npol) // check if the template has right number of channel, polarization and nphase { // nchn and npol are the number of channel and polarization of the data profile if (mode == 0) { fitsfile *fptr; // pointer to the FITS file, defined in fitsio.h int status; int colnum; status = 0; //if ( fits_open_file(&fptr, argv[1], READONLY, &status) ) // open the file if ( fits_open_file(&fptr, name, READONLY, &status) ) // open the file { printf( "error while openning file\n" ); } if ( fits_get_colnum(fptr, CASEINSEN, "DATA", &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 npola; if ( fits_read_key(fptr, TINT, (char *)"NPOL", &npola, NULL, &status) ) // get the pol number { printf( "error while getting the npol number\n" ); //fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status); } //printf ("%d\n", npol); int nchan; if ( fits_read_key(fptr, TINT, (char *)"NCHAN", &nchan, NULL, &status) ) // get the chan number { printf( "error while getting the npol number\n" ); //fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status); } printf ("STD %d\n", nchan); /////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// int nbin; if ( fits_read_key(fptr, TINT, (char *)"NBIN", &nbin, NULL, &status) ) // get the row number { printf( "error while getting the nbin number\n" ); //fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status); } printf ("number of nbin of std: %d\n", nbin); /////////////////////////////////////////////////////////////////////////// if ( fits_close_file(fptr, &status) ) { printf( " error while closing the file\n" ); } // check if nbin = nphase if ( nbin != nphase ) { printf ("The phase bin number of template != the phase bin number of data\n"); exit (0); } // check if nchan = nchn if ( nchan == nchn ) { printf ("The channel number of template = the channel number of data\n"); } else { printf ("The channel number of template != the channel number of data\n"); if ( nchan != 1 ) { printf ("Can't not do template matching! The channel number of template should be 1 or equal to that of data.\n"); exit (0); } else { printf ("The template has only one channel. The same template will be used for all channels.\n"); } } // check if npola = npol if ( npola == npol ) { printf ("The polarization number of template = the polarization number of data\n"); } else { printf ("The polarization number of template != the polarization number of data\n"); exit (0); } } else if (mode == 1) { tmplStruct tmpl; initialiseTemplate(&tmpl); printf("Reading template\n"); readTemplate_ptime(name,&tmpl); printf("Complete reading template\n"); if ( tmpl.nchan == nchn ) { printf ("The channel number of template = the channel number of data\n"); } else { printf ("The channel number of template != the channel number of data\n"); if ( tmpl.nchan != 1 ) { printf ("Can't not do template matching! The channel number of template should be 1 or equal to that of data.\n"); exit (0); } else { printf ("The template has only one channel. The same template will be used for all channels.\n"); } } int npola; npola = tmpl.channel[0].nstokes; // check if npola = npol if ( npola == npol) { printf ("The polarization number of template = the polarization number of data\n"); } else { printf ("The polarization number of template != the polarization number of data\n"); exit (0); } } return 0; }
int psrfits_write_ephem(struct psrfits *pf, FILE *parfile) { // Read a pulsar ephemeris (par file) and put it into // the psrfits PSREPHEM table. Only minimal checking // is done. // Get status int *status = &(pf->status); // Save current HDU, move to psrephem table int hdu; fits_get_hdu_num(pf->fptr, &hdu); fits_movnam_hdu(pf->fptr, BINARY_TBL, "PSREPHEM", 0, status); // Loop over lines in par file int row=1, col, dtype; double dval; int ival; long double ldval; char line[256], *ptr, *saveptr, *key, *val; while (fgets(line, 256, parfile)!=NULL) { // Convert tabs to spaces while ((ptr=strchr(line,'\t'))!=NULL) { *ptr=' '; } // strip leading whitespace ptr = line; while (*ptr==' ') { ptr++; } // Identify comments or blank lines if (line[0]=='\n' || line[0]=='#' || (line[0]=='C' && line[1]==' ')) continue; // Split into key/val (ignore fit flag and error) key = strtok_r(line, " ", &saveptr); val = strtok_r(NULL, " ", &saveptr); if (key==NULL || val==NULL) continue; // TODO : complain? // Deal with any special cases here if (strncmp(key, "PSR", 3)==0) { // PSR(J) -> PSR_NAME fits_get_colnum(pf->fptr,CASEINSEN,"PSR_NAME",&col,status); fits_write_col(pf->fptr,TSTRING,col,row,1,1,&val,status); } else if (strncmp(key, "RA", 2)==0) { // RA -> RAJ fits_get_colnum(pf->fptr,CASEINSEN,"RAJ",&col,status); fits_write_col(pf->fptr,TSTRING,col,row,1,1,&val,status); } else if (strncmp(key, "DEC", 3)==0) { // DEC -> DECJ fits_get_colnum(pf->fptr,CASEINSEN,"DECJ",&col,status); fits_write_col(pf->fptr,TSTRING,col,row,1,1,&val,status); } else if (key[0]=='E' && key[1]=='\0') { // E -> ECC dval = atof(val); fits_get_colnum(pf->fptr,CASEINSEN,"ECC",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dval,status); } else if (strncmp(key, "F0", 2)==0) { // F is converted to mHz and split into int/frac ldval = strtold(val,NULL) * 1000.0; // Hz->mHz ival = (int)ldval; dval = ldval - (long double)ival; fits_get_colnum(pf->fptr,CASEINSEN,"IF0",&col,status); fits_write_col(pf->fptr,TINT,col,row,1,1,&ival,status); fits_get_colnum(pf->fptr,CASEINSEN,"FF0",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dval,status); } else if (strncmp(key, "TZRMJD", 6)==0) { // TZRMJD is split into int/frac ldval = strtold(val,NULL); ival = (int)ldval; dval = ldval - (long double)ival; fits_get_colnum(pf->fptr,CASEINSEN,"TZRIMJD",&col,status); fits_write_col(pf->fptr,TINT,col,row,1,1,&ival,status); fits_get_colnum(pf->fptr,CASEINSEN,"TZRFMJD",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dval,status); } else { // Find column, skip/warn if this one isn't present fits_get_colnum(pf->fptr,CASEINSEN,key,&col,status); if (*status==COL_NOT_FOUND) { #if (DEBUGOUT) fprintf(stderr, "psrfits_write_epherm warning: Couldn't find keyword %s " "in ephemeris table.\n", key); #endif *status=0; continue; } // Need to convert string to appropriate column data type // and then write it to the column. These should all be // either double int or string. fits_get_coltype(pf->fptr,col,&dtype,NULL,NULL,status); if (dtype==TDOUBLE || dtype==TFLOAT) { dval = atof(val); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dval,status); } else if (dtype==TINT || dtype==TLONG || dtype==TSHORT) { ival = atoi(val); fits_write_col(pf->fptr,TINT,col,row,1,1,&ival,status); } else if (dtype==TSTRING) { fits_write_col(pf->fptr,TSTRING,col,row,1,1,&val,status); } else { fprintf(stderr, "psrfits_write_ephem warning: " "Unhandled column datatype (key=%s)\n", key); continue; } } // sucess/failure if (*status) { fprintf(stderr, "psrfits_write_ephem failed: key=%s val=%s\n", key, val); fits_report_error(stderr, *status); *status=0; } #if 0 // DEBUG else { fprintf(stderr, "psrfits_write_ephem success: key=%s val=%s\n", key, val); } #endif } // Go back to orig HDU fits_movabs_hdu(pf->fptr, hdu, NULL, status); return *status; }
/** * Function: load_SED_from_fitsext * The function creates a energy distribution from the data stored * in a fits file extension. The data must be stored in the columns * "wavelength" and "flux". * * Parameters: * @param spectral_models_file - pathname to the spectral models file * @param s_models - pointer to the fits file extension * * Returns: * @return sed - the energy distribution created */ energy_distrib * load_SED_from_fitsext(const char spectral_models_file[], fitsfile *s_models) { int f_status=0; int anynul; long nrows=0; int colnum1; int colnum2; energy_distrib *sed; double *sed_wavs; double *sed_flux; // allocate memory for the energy distribution sed = (energy_distrib *) malloc(sizeof(energy_distrib)); // get number of rows fits_get_num_rows (s_models, &nrows, &f_status); if (f_status) { ffrprt (stderr, f_status); aXe_message (aXe_M_FATAL, __FILE__, __LINE__, "load_SED_from_fitsext: " "Could not determine the number of rows in" " table %s",spectral_models_file); } // allocate memory for the data sed_wavs = (double *) malloc(nrows*sizeof(double)); if (!sed_wavs) { aXe_message (aXe_M_ERROR, __FILE__, __LINE__, "Memory allocation failed"); } sed_flux = (double *) malloc(nrows*sizeof(double)); if (!sed_flux) { aXe_message (aXe_M_ERROR, __FILE__, __LINE__, "Memory allocation failed"); } // get the column number for the wavelength fits_get_colnum (s_models, CASEINSEN, "WAV_NM", &colnum1, &f_status); if (f_status) { ffrprt (stderr, f_status); aXe_message (aXe_M_FATAL, __FILE__, __LINE__, "create_interp_ftable: " "Could not determine column %s in " " table %s", "WAV_NM", spectral_models_file); } // read the wavelength fits_read_col (s_models, TDOUBLE, colnum1, 1, 1, nrows, NULL, sed_wavs, &anynul, &f_status); if (f_status) { ffrprt (stderr, f_status); aXe_message (aXe_M_FATAL, __FILE__, __LINE__, "load_SED_from_fitsext: " "Could not read content of WAVELENGTH column " " from BINARY table %s", spectral_models_file); } // get the column number for the flux fits_get_colnum (s_models, CASEINSEN, "FLUX", &colnum2, &f_status); if (f_status) { ffrprt (stderr, f_status); aXe_message (aXe_M_FATAL, __FILE__, __LINE__, "create_interp_ftable: " "Could not determine column %s in " " table %s", "FLUX", spectral_models_file); } // read the flux column fits_read_col (s_models, TDOUBLE, colnum2, 1, 1, nrows, NULL, sed_flux, &anynul, &f_status); if (f_status) { ffrprt (stderr, f_status); aXe_message (aXe_M_FATAL, __FILE__, __LINE__, "load_SED_from_fitsext: " "Could not read content of FLUX column " " from BINARY table %s", spectral_models_file); } // transfer the vector and length information // to the SED object sed->npoints = nrows; sed->wavelength = sed_wavs ; sed->flux = sed_flux; // allocate some structures for the interpolator sed->interp = gsl_interp_alloc (SMODEL_INTERP_TYPE, (size_t)sed->npoints ); sed->accel = gsl_interp_accel_alloc (); // initialize the iterpolator gsl_interp_init (sed->interp, sed->wavelength, sed->flux, (size_t)sed->npoints); // return the energy distribution return sed; }
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; }
int extractFoldData(fitsfile *fp,dSet *data,float dm,float *fx,float *fy,float *freq_y,float *time_y,float *bpass, int sub0) { int n=0; int status=0; int colnum; int i,j,k,l; int initflag=0; float nval=0; float ty[data->phead.nbin]; float **offs; // [data->phead.nsub]; float **dat_scl; // [data->phead.nsub]; double f0,chanbw,tdelay; int bn,cdelay; double bintime; int addDelay = 0; //500; // float bpass[data->phead.nchan*2]; float bpass_offs[2]; float bpass_scl[2]; float meanVal,rmsVal; // get mean/RMS of off-pulse for scaling. no longer need OFFS/DAT_SCL printf("sub0 = %d\n",sub0); if (dm < 0) dm = data->phead.dm; // Need to remove a baseline from each polarisation channel before summing // Get first frequency channel // Central frequency f0 = data->phead.freq; //-data->phead.chanbw*data->phead.nchan/2; // chanbw = data->phead.bw/data->phead.nchan; chanbw = data->phead.chanbw; bintime = (double)data->phead.period/(double)data->phead.nbin; 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_flt(fp,colnum,1,1,2,nval,bpass_offs,&initflag,&status); fits_get_colnum(fp,CASEINSEN,"DAT_SCL",&colnum,&status); fits_read_col_flt(fp,colnum,1,1,2,nval,bpass_scl,&initflag,&status); // Now read the bandpass fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); // NOTE: Starting at element 2 as element 1 is junk fits_read_col_flt(fp,colnum,1,2,data->phead.nchan,nval,bpass,&initflag,&status); fits_read_col_flt(fp,colnum,1,3+data->phead.nchan,data->phead.nchan,nval,bpass+data->phead.nchan,&initflag,&status); for (i=0;i<data->phead.nchan*2;i++) { if (i<data->phead.nchan) bpass[i] = bpass[i]*bpass_scl[0] + bpass_offs[0]; else bpass[i] = bpass[i]*bpass_scl[1] + bpass_offs[1]; // printf("bpass: %d %g %g %g %g %g \n",i,bpass[i],bpass_scl[0],bpass_scl[1],bpass_offs[0],bpass_offs[1]); } // exit(1); fits_movnam_hdu(fp,BINARY_TBL,"SUBINT",1,&status); if (status) { printf("Unable to move to subint table in FITS file\n"); exit(1); } // REMOVED: No longer need dat_scl or offs. OFFS/DAT_SCL // offs = (float **)malloc(sizeof(float *)*data->phead.nsub); // dat_scl = (float **)malloc(sizeof(float *)*data->phead.nsub); // for (i=0;i<data->phead.nsub;i++) // { // offs[i] = (float *)malloc(sizeof(float)*data->phead.nchan*data->phead.npol); // dat_scl[i] = (float *)malloc(sizeof(float)*data->phead.nchan*data->phead.npol); // } // fits_get_colnum(fp,CASEINSEN,"DAT_OFFS",&colnum,&status); // if (status) { // printf("Unable to find DAT_OFFS in the subint table in FITS file\n"); // exit(1); // } // for (i=0;i<data->phead.nsub;i++) // { // fits_read_col_flt(fp,colnum,i+1,1,data->phead.nchan*data->phead.npol,nval,offs[i],&initflag,&status); // // printf("offs = %g\n",offs[i][5]); // // offs[i] =0; // } // // fits_get_colnum(fp,CASEINSEN,"DAT_SCL",&colnum,&status); // if (status) { // printf("Unable to find DAT_SCL in the subint table in FITS file\n"); // exit(1); // } // for (i=0;i<data->phead.nsub;i++) // { // fits_read_col_flt(fp,colnum,i+1,1,data->phead.nchan*data->phead.npol,nval,dat_scl[i],&initflag,&status); // // printf("dat_scl = %g\n",dat_scl[i][5]); // // dat_scl[i]=1.0; // } fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); if (status) { printf("Unable to find data in the subint table in FITS file\n"); exit(1); } if (sub0==0) { for (i=0;i<data->phead.nbin;i++) { fx[i] = i; fy[i] = 0; } // printf("Loading %d subintegrations\n",data->phead.nsub); printf("Number of frequency channels = %d\n",data->phead.nchan); printf("Number of polarisations = %d\n",data->phead.npol); for (i=0;i<data->phead.nchan*data->phead.nbin;i++) freq_y[i] = 0; } for (i=sub0;i<data->phead.nsub;i++) // *data->phead.nbin;i++) { for (j=0;j<data->phead.nbin;j++) time_y[i*data->phead.nbin+j] = 0; } for (l=sub0;l<data->phead.nsub;l++) { for (j=0;j<data->phead.npol && j < 2;j++) // Do not add cross terms! { for (i=0;i<data->phead.nchan;i++) { // Must calculate the frequency of this channel // ... calculate the delay caused by the DM // ... dedisperse the subintegration // tdelay = 4.15e-3*dm*(pow(f0/1000.0,-2)-pow((f0+chanbw*i)/1000.0,-2)); tdelay = 4.15e-3*dm*(pow(f0/1000.0,-2)-pow((f0+(chanbw*i-chanbw*data->phead.nchan/2.0))/1000.0,-2)); cdelay = nint(-tdelay/bintime); // if (l==0 && j==0) // printf("Have %g %g %g %d %d\n",dm,f0,f0+chanbw*i,i,cdelay); fits_read_col_flt(fp,colnum,l+1,j*(data->phead.nchan*data->phead.nbin)+i*data->phead.nbin+1,data->phead.nbin,nval,ty,&initflag,&status); meanVal=0; //for (k=0;k<data->phead.nbin;k++) //{ // ty[k] = ((ty[k]+offs[l][j*data->phead.nchan+i])*dat_scl[l][j*data->phead.nchan+i]); //+offs[l][j*data->phead.nchan+i]); /* if (j==0) ty[k] -= bpass[i]; else if (j==1) ty[k] -= bpass[data->phead.nchan+i]; else { ty[k] -= sqrt(bpass[data->phead.nchan+i]*bpass[i]); } */ //meanVal+=ty[k]; // } getbaseline(ty,data->phead.nbin,0.35,&meanVal,&rmsVal); if (rmsVal == 0.0 ) rmsVal=1.0; // printf("Val = %g\n",ty[10]); for (k=0;k<data->phead.nbin;k++) { // if (i==10 && l==10) // printf("Orig value = %g\n",ty[k]); //ty[k] = ((ty[k]+offs[l][j*data->phead.nchan+i])*dat_scl[l][j*data->phead.nchan+i]); //+offs[l][j*data->phead.nchan+i]); ty[k] -= meanVal; ty[k] /= rmsVal; // Subtract bandpass /* if (j==0) ty[k] -= bpass[i]; else if (j==1) ty[k] -= bpass[data->phead.nchan+i]; else { ty[k] -= sqrt(bpass[data->phead.nchan+i]*bpass[i]); } */ // if (l==10) // printf("New value = %g\n",ty[k]); bn = k-cdelay + addDelay; // bn = nint(fmod(k-tdelay/bintime,data->phead.nbin)); while (bn >= data->phead.nbin) bn -= data->phead.nbin; while (bn < 0) bn += data->phead.nbin; freq_y[i*data->phead.nbin+k]+=(ty[k]); ///(float)(data->phead.npol*data->phead.nsub)); time_y[l*data->phead.nbin+bn]+=(ty[k]);///(float)(data->phead.npol*data->phead.nchan)); // printf("timey = %g\n",time_y[l*data->phead.nbin+bn]); fy[bn]+=(ty[k]/(float)(data->phead.nchan*data->phead.npol*data->phead.nsub)); } } } } // REMOVED: OFFS/DAT_SCL table no longer needed. //for (i=0;i<data->phead.nsub;i++) // { // free(offs[i]); // free(dat_scl[i]); // } //free(offs); //free(dat_scl);*/ printf("Status = %d\n",status); }
int psrfits_write_polycos(struct psrfits *pf, struct polyco *pc, int npc) { // Usual setup int *status = &(pf->status); // If mode!=fold, exit? // Save current HDU, move to polyco table int hdu; fits_get_hdu_num(pf->fptr, &hdu); fits_movnam_hdu(pf->fptr, BINARY_TBL, "POLYCO", 0, status); int itmp; double dtmp; char datestr[32], ctmp[32]; char *cptr; fits_get_system_time(datestr, &itmp, status); int i, col, n_written=0; long row; fits_get_num_rows(pf->fptr, &row, status); // Start at end of table for (i=0; i<npc; i++) { // Only write polycos that were used if (!pc[i].used) continue; // Go to next row (1-based index) row++; cptr = datestr; fits_get_colnum(pf->fptr,CASEINSEN,"DATE_PRO",&col,status); fits_write_col(pf->fptr,TSTRING,col,row,1,1,&cptr,status); sprintf(ctmp, "11.005"); // Tempo version? cptr = ctmp; fits_get_colnum(pf->fptr,CASEINSEN,"POLYVER",&col,status); fits_write_col(pf->fptr,TSTRING,col,row,1,1,&cptr,status); fits_get_colnum(pf->fptr,CASEINSEN,"NSPAN",&col,status); fits_write_col(pf->fptr,TINT,col,row,1,1,&(pc[i].nmin),status); fits_get_colnum(pf->fptr,CASEINSEN,"NCOEF",&col,status); fits_write_col(pf->fptr,TINT,col,row,1,1,&(pc[i].nc),status); sprintf(ctmp,"%d", pc[i].nsite); // XXX convert to letter? cptr = ctmp; fits_get_colnum(pf->fptr,CASEINSEN,"NSITE",&col,status); fits_write_col(pf->fptr,TSTRING,col,row,1,1,&cptr,status); fits_get_colnum(pf->fptr,CASEINSEN,"REF_FREQ",&col,status); fits_write_col(pf->fptr,TFLOAT,col,row,1,1,&(pc[i].rf),status); // XXX needs to be accurate?? dtmp=0.0; fits_get_colnum(pf->fptr,CASEINSEN,"PRED_PHS",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dtmp,status); dtmp = (double)pc[i].mjd + pc[i].fmjd; fits_get_colnum(pf->fptr,CASEINSEN,"REF_MJD",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dtmp,status); fits_get_colnum(pf->fptr,CASEINSEN,"REF_PHS",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&(pc[i].rphase),status); fits_get_colnum(pf->fptr,CASEINSEN,"REF_F0",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&(pc[i].f0),status); // XXX don't parse this yet dtmp=-6.0; fits_get_colnum(pf->fptr,CASEINSEN,"LGFITERR",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,1,&dtmp,status); fits_get_colnum(pf->fptr,CASEINSEN,"COEFF",&col,status); fits_write_col(pf->fptr,TDOUBLE,col,row,1,pc[i].nc,pc[i].c,status); n_written++; } // Update polyco block count, only if new info was added if (n_written) { itmp = row; fits_get_colnum(pf->fptr,CASEINSEN,"NPBLK",&col,status); for (i=1; i<=row; i++) fits_write_col(pf->fptr,TINT,col,i,1,1,&itmp,status); } // Flush buffers (so files are valid as they are created) fits_flush_file(pf->fptr, status); // Go back to orig HDU fits_movabs_hdu(pf->fptr, hdu, NULL, status); return *status; }
int main(int argc, char *argv[]) { fitsfile *infile; int ii, jj, kk, status = 0; int nchan, nchan2, npol, wgts_col, offs_col; long nrows; float *weights, *offsets; char comment[120]; // Read the weights and offsets read_wgts_and_offs(argv[1], &nchan, &weights, &offsets); printf("Read in %d channels of weights and offsets from\n\t'%s'\n", nchan, argv[1]); // Step through the FITS files for (ii = 0 ; ii < argc-2 ; ii++) { printf("Updating '%s'\n", argv[ii+2]); // Is the file a PSRFITS file? if (!is_PSRFITS(argv[ii+2])) { fprintf(stderr, " Error! '%s' does not appear to be PSRFITS!\n", argv[ii+2]); exit(1); } // Open the PSRFITS file fits_open_file(&infile, argv[ii+2], READWRITE, &status); if (status) { printf(" Error! Cannot open '%s'!\n", argv[ii+2]); exit(1); } // Move to the SUBINT HDU fits_movnam_hdu(infile, BINARY_TBL, "SUBINT", 0, &status); if (status) { printf(" Warning! Cannot find NPOL in '%s'! Assuming NPOL=1\n", argv[ii+2]); status = 0; } // Read the number of channels and polarizations fits_read_key(infile, TINT, "NCHAN", &nchan2, comment, &status); \ if (status) { printf(" Warning! Cannot find NCHAN in '%s'!\n", argv[ii+2]); status = 0; } else if (nchan != nchan2) { printf(" Error! The number of channels in '%s'\n", argv[1]); printf(" and in '%s' do not match!\n", argv[ii+2]); exit(1); } fits_read_key(infile, TINT, "NPOL", &npol, comment, &status); \ if (status) { printf(" Warning! Cannot find NPOL in '%s'! Assuming NPOL=1\n", argv[ii+2]); npol = 1; status = 0; } // How many rows are there? fits_get_num_rows(infile, &nrows, &status); if (status) { printf(" Error! Cannot read the number of rows in '%s'!\n", argv[ii+2]); exit(1); } // Get the column numbers for the weights fits_get_colnum(infile, 0, "DAT_WTS", &wgts_col, &status); if (status==COL_NOT_FOUND) { printf(" Warning!: Can't find the channel weights!\n"); status = 0; } else { // update the weights, row by row for (jj = 1 ; jj < nrows+1 ; jj++) fits_write_col(infile, TFLOAT, wgts_col, jj, 1L, nchan, weights, &status); } // Get the column numbers for the offsets if (0) { fits_get_colnum(infile, 0, "DAT_OFFS", &offs_col, &status); if (status==COL_NOT_FOUND) { printf(" Warning!: Can't find the channel offsets!\n"); status = 0; } else { // update the offsets, row by row for (jj = 1 ; jj < nrows+1 ; jj++) for (kk = 0 ; kk < npol ; kk++) fits_write_col(infile, TFLOAT, offs_col, jj, kk*nchan+1L, nchan, offsets, &status); } } // Close the file fits_close_file(infile, &status); if (status) { printf(" Warning!: Cannot properly close '%s' (status=%d)!\n", argv[ii+2], status); status = 0; } } free(weights); free(offsets); printf("Finished.\n"); exit(0); }