int is_PSRFITS(char *filename) // Return 1 if the file described by filename is a PSRFITS file // Return 0 otherwise. { fitsfile *fptr; int status = 0; char ctmp[80], comment[120]; // Read the primary HDU fits_open_file(&fptr, filename, READONLY, &status); if (status) return 0; // Make the easy check first fits_read_key(fptr, TSTRING, "FITSTYPE", ctmp, comment, &status); if (status || strcmp(ctmp, "PSRFITS")) return 0; // See if the data are search-mode fits_read_key(fptr, TSTRING, "OBS_MODE", ctmp, comment, &status); if (status || (strcmp(ctmp, "SEARCH") && strcmp(ctmp, "SRCH"))) return 0; fits_close_file(fptr, &status); return 1; // it is search-mode PSRFITS }
/* Load one periodic spectrum from datafile * Space should already be allocated. * idx is 1-offset following cfitsio convention. */ int cyclic_load_ps(fitsfile *f, PS *d, int idx, int *status) { /* Load data */ long fpixel[4]; long long nelem; fpixel[0] = fpixel[1] = fpixel[2] = 1; fpixel[3] = idx; nelem = d->nphase * d->npol * d->nchan; fits_read_pix(f, TFLOAT, fpixel, nelem, NULL, d->data, NULL, status); /* Load header params */ char key[9]; sprintf(key, "IMJD%04d", idx); fits_read_key(f, TINT, key, &d->imjd, NULL, status); sprintf(key, "FMJD%04d", idx); fits_read_key(f, TDOUBLE, key, &d->fmjd, NULL, status); sprintf(key, "PHAS%04d", idx); fits_read_key(f, TDOUBLE, key, &d->ref_phase, NULL, status); sprintf(key, "FREQ%04d", idx); fits_read_key(f, TDOUBLE, key, &d->ref_freq, NULL, status); // TODO get these in the file d->rf = 428.0; d->bw = 4.0; return(*status); }
int ReadHdr(struct ASPHdr *hdr, fitsfile *Fin, struct RunVars *RunMode) { int hdutype, status=0; /* Move to primary header */ if (fits_movabs_hdu(Fin, 1, &hdutype, &status)) { fprintf(stderr, "ERROR ReadPSRFITSHdr: Cannot move to primary HDU\n"); return -1; } /* Header version no. */ fits_read_key(Fin, TSTRING, "HDRVER", hdr->gen.HdrVer, NULL, &status); status=0; /* Header FITS definition type */ fits_read_key(Fin, TSTRING, "FITSTYPE", hdr->gen.FitsType, NULL, &status); status=0; /****** DATE GOES HERE ******/ /* Backend name ASP/ABPP/WAPP/GUPPI etc */ fits_read_key(Fin, TSTRING, "BACKEND", hdr->gen.BEName, NULL, &status); status=0; /* If data comes from ASP then do ReadASPData */ if(!strcmp(hdr->gen.BEName, "xASP")) { /* Read in values for header variables */ if(ReadASPHdr(hdr, Fin) < 0){ /* Return with error */ return -1; } } else { /* Otherwise, if data is in PSRFITS format, then do ReadPSRFITSData */ if(!strcmp(hdr->gen.FitsType, "PSRFITS")) { //printf("PSRFITS Data!!\n"); /* Read in values for header variables */ if(ReadPSRFITSHdr(hdr, Fin, RunMode) < 0){ /* Return with error */ return -1; } } else { /* Do not recognize data format! */ fprintf(stderr, "ReadHdr ERROR: Do not recognize file format.\n"); return -1; } } return 0; }
int read_key(char filename[], char keyword[], char value[], char type[], char comment[]) { fitsfile *fptr; int status=0; int datatype; unsigned short usvalue; int ivalue; float fvalue; double dvalue; /* transform type to cfitsio code */ datatype = get_type(type); /* open FITS file */ if ( fits_open_file(&fptr, filename, READONLY, &status) ) printerror( status ); /* read entry */ switch (datatype) { case TSTRING: if ( fits_read_key(fptr, datatype, keyword, value, comment, &status) ) printerror( status ); break; case TUSHORT: if ( fits_read_key(fptr, datatype, keyword, &usvalue, comment, &status) ) printerror( status ); sprintf(value,"%d",usvalue); break; case TINT: if ( fits_read_key(fptr, datatype, keyword, &ivalue, comment, &status) ) printerror( status ); sprintf(value,"%d",ivalue); break; case TFLOAT: if ( fits_read_key(fptr, datatype, keyword, &fvalue, comment, &status) ) printerror( status ); sprintf(value,"%f",fvalue); break; case TDOUBLE: if ( fits_read_key(fptr, datatype, keyword, &dvalue, comment, &status) ) printerror( status ); sprintf(value,"%lf",dvalue); break; default: printerror( BAD_DATATYPE ); break; } /* close FITS file */ if ( fits_close_file(fptr, &status) ) printerror( status ); return(0); }
int get_nchan ( char *name ) { //double *read_arrival_time( char *input, long *nrows ) fitsfile *fptr; // pointer to the FITS file, defined in fitsio.h int status; status = 0; if ( fits_open_file(&fptr, name, READONLY, &status) ) // open the file { printf( "error while openning file\n" ); } int nchan; if ( fits_read_key(fptr, TINT, (char *)"NCHAN", &nchan, NULL, &status) ) // get the row number { printf( "error while getting the npol number\n" ); //fits_get_colnum(fptr, CASEINSEN, "DATA", &colnum, &status); } printf ("number of nchan: %d\n", nchan); /////////////////////////////////////////////////////////////////////////// if ( fits_close_file(fptr, &status) ) { printf( " error while closing the file " ); } return nchan; }
int cfits_read_int_colkey (int *keyvalue, const char *keyroot, const char *colname, cfitsfile *fptr) { char keyname[CFLEN_KEYWORD]; char comment[CFLEN_COMMENT]; int datatype = TINT; int status = 0; int colnum; if (NULL == fptr || NULL == colname || NULL == keyroot || NULL == keyvalue) return -1; if (-1 == cfits_get_colnum (&colnum, colname, fptr)) return -1; if ((strlen(keyroot) + 3) > CFLEN_KEYWORD) return -1; (void) sprintf (keyname, "%s%i", keyroot, colnum); (void) fits_read_key ((fitsfile *) fptr, datatype, keyname, keyvalue, comment, &status); cfits_report_error (status); if (status != 0) return -1; return 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; }
double FitsReader::readDoubleKey(const char *key) { int status = 0; double value; fits_read_key(_fitsPtr, TDOUBLE, key, &value, 0, &status); checkStatus(status, _filename, std::string("Read float key ") + key); return value; }
std::string FitsReader::readStringKey(const char *key) { int status = 0; char keyStr[256]; fits_read_key(_fitsPtr, TSTRING, key, keyStr, 0, &status); checkStatus(status, _filename, std::string("Read string key ") + key); return std::string(keyStr); }
bool FitsReader::ReadFloatKeyIfExists(const char *key, float &dest) { int status = 0; float floatValue; fits_read_key(_fitsPtr, TFLOAT, key, &floatValue, 0, &status); if(status == 0) dest = floatValue; return status == 0; }
bool FitsReader::ReadDoubleKeyIfExists(const char *key, double &dest) { int status = 0; double doubleValue; fits_read_key(_fitsPtr, TDOUBLE, key, &doubleValue, 0, &status); if(status == 0) dest = doubleValue; return status == 0; }
static int read_key_double (fitsfile *f, char *key, double *v) { int status = 0; if (0 == fits_read_key (f, TDOUBLE, key, v, NULL, &status)) return 0; fprintf (stderr, "Unable to read %s keyword value:\n", key); dump_fits_error (status); return -1; }
bool FitsReader::readDateKeyIfExists(const char *key, double &dest) { int status = 0; char keyStr[256]; fits_read_key(_fitsPtr, TSTRING, key, keyStr, 0, &status); if(status == 0) { dest = FitsReader::ParseFitsDateToMJD(keyStr); return true; } else return false; }
bool FitsReader::ReadStringKeyIfExists(const char *key, std::string& value, std::string& comment) { int status = 0; char valueStr[256], commentStr[256]; fits_read_key(_fitsPtr, TSTRING, key, valueStr, commentStr, &status); if(status == 0) { value = valueStr; comment = commentStr; } return status == 0; }
int append_filter(const char *fname, int ispec, int imjd, double fmjd, struct filter_freq *hf) { int status=0; fitsfile *fptr; fits_open_file(&fptr, fname, READWRITE, &status); fits_movabs_hdu(fptr, 1, NULL, &status); int naxis; long naxes[3]; fits_get_img_dim(fptr, &naxis, &status); if (naxis!=3) { fits_close_file(fptr, &status); return(-1); } fits_get_img_size(fptr, 3, naxes, &status); if (naxes[0]!=2 || naxes[1]!=hf->nchan || naxes[2]<ispec) { fits_close_file(fptr, &status); return(-2); } int imjd0; double fmjd0; fits_read_key(fptr, TINT, "IMJD", &imjd0, NULL, &status); fits_read_key(fptr, TDOUBLE, "FMJD", &fmjd0, NULL, &status); double toffs = (double)(imjd-imjd0) + (fmjd-fmjd0); toffs *= 86400.0; char key[80]; sprintf(key, "DT%5.5d", ispec); fits_write_key(fptr, TDOUBLE, key, &toffs, "[s]", &status); naxes[0] = naxes[1] = 1; naxes[2] = ispec; fits_write_pix(fptr, TFLOAT, naxes, 2*hf->nchan, (float *)hf->data, &status); fits_close_file(fptr, &status); if (status) { fprintf(stderr, "Error in append_filter:\n"); fits_report_error(stderr, status); return(-1); } return(0); }
int cfits_read_int_keyword (int *keyvalue, const char *keyname, cfitsfile *fptr) { int status = 0; int datatype = TINT; char comment[CFLEN_COMMENT]; (void) fits_read_key ((fitsfile *) fptr, datatype, keyname, keyvalue, comment, &status); cfits_report_error (status); if (status != 0) return -1; return 0; }
int nelem(const char* filename, long *length) { int status = 0; fitsfile *fptr; fits_open_file(&fptr, filename, READONLY, &status); fprintf(stderr, "%s :reading.\n", filename); //fits_movnam_hdu(fptr, BINARY_TBL, "DATA", 0, &status); fits_movabs_hdu(fptr, 2, NULL, &status); fits_read_key(fptr, TLONG, "NAXIS2", length , NULL, &status); fits_close_file(fptr, &status); if (status) /* print any error messages */ fits_report_error(stderr, status); return(status); }
std::string FitsFile::GetTableDimensionName(int index) { CheckOpen(); std::ostringstream name; name << "CTYPE" << (index+1); int status = 0; char valueStr[256], commentStr[256]; fits_read_key(_fptr, TSTRING, name.str().c_str(), valueStr, commentStr, &status); std::string val; if(!status) { val = valueStr; } return val; }
static gboolean get_real_and_offset(fitsfile *fptr, gint i, guint res, gdouble *real, gdouble *off) { char keyname[FLEN_KEYWORD]; gdouble delt, refval, refpix; gint status = 0; fits_make_keyn("CDELT", i, keyname, &status); gwy_debug("looking for %s", keyname); if (fits_read_key(fptr, TDOUBLE, keyname, &delt, NULL, &status)) return FALSE; gwy_debug("%s = %g", keyname, delt); if (!(delt != 0.0)) return FALSE; *real = res*delt; *off = 0.0; fits_make_keyn("CRPIX", i, keyname, &status); gwy_debug("looking for %s", keyname); if (fits_read_key(fptr, TDOUBLE, keyname, &refpix, NULL, &status)) return TRUE; gwy_debug("%s = %g", keyname, refpix); fits_make_keyn("CRVAL", i, keyname, &status); gwy_debug("looking for %s", keyname); if (fits_read_key(fptr, TDOUBLE, keyname, &refval, NULL, &status)) return TRUE; gwy_debug("%s = %g", keyname, refval); /* Not sure about their pixel numbering, so offset may be wrong. */ *off = refval + delt*(1.0 - refpix); return TRUE; }
bool Fits2D::readStringKeyword(string keyword, string &value){ char *ptr = NULL; int status = 0; fitsfile *fptr; const char * filename; char v[40]; filename = mFitsPath.c_str(); if(fits_open_file(&fptr, filename, READONLY, &status)){ printerror(status); return false; } char * key = new char[keyword.length()+1]; strcpy(key,keyword.c_str()); if(fits_read_key(fptr, TSTRING, key, v, NULL, &status)){ printerror(status); delete key; return false; } value = string(v); delete key; if(fits_close_file(fptr, &status)){ printerror(status); return false; } return true; }
int getDataType( fitsfile *pFile ) { long int bitpix=0, i; char keycomment[FLEN_COMMENT]; /* string lengths defined in fitsioc.h */ int status=0; int type=0; /* Get BITPIX */ if ( fits_read_key(pFile, TINT, "BITPIX", (void *)&bitpix, keycomment, &status) ) printerror( status, __LINE__ ); for (i=0; i<NTYPES; i++) { if ( bitpix == imgTypes[i] ) { type = dataTypes[i]; } } return type; }
// Function which determines if file is of type spSpec, spPlate int SDSS_filetype(char *infile, int *nspec){ // return value: // 0 = spSpec // 1 = spPlate // 2 = spec int val = 0; int hdunum_exp = 0; fitsfile *infits; int status = 0, hdunum = 0; //printf("Name of File: %s\n", infile); fits_open_file(&infits, infile, READONLY, &status); fits_get_num_hdus(infits, &hdunum, &status); //printf("HDUNumber: %i\n\n", hdunum); // Number of HDUs not necessarily 9. In some cases might be different if( strstr(infile, "spSpec") != NULL ){ val = 0; hdunum_exp = 7; //if(hdunum != hdunum_exp) warnmsg("Number of HDUs is not %d as expected from spSpec", hdunum_exp); } if( strstr(infile, "spPlate") != NULL ){ val = 1; hdunum_exp = 9; //if(hdunum != hdunum_exp) warnmsg("Number of HDUs is not %d as expected from spPlate", hdunum_exp); fits_read_key(infits, TINT, "NAXIS2", nspec, NULL, &status); } if( strstr(infile, "spec-") != NULL ){ val = 2; hdunum_exp = 14; //if(hdunum != hdunum_exp) warnmsg("Number of HDUs is not %d as expected from spec", hdunum_exp); } // report Error. TODO: Decide if want to use errormsg instead. fits_close_file(infits, &status); fits_report_error(stderr, status); return val; }
bool Fits2D::readDoubleKeyword(string keyword, double &value){ char *ptr = NULL; int status = 0; fitsfile *fptr; const char * filename; filename = mFitsPath.c_str(); if(fits_open_file(&fptr, filename, READONLY, &status)){ printerror(status); return false; } char * key = new char[keyword.length()+1]; strcpy(key,keyword.c_str()); if(fits_read_key(fptr, TDOUBLE, key, &value, NULL, &status)){ printerror(status); delete key; return false; } delete key; if(fits_close_file(fptr, &status)){ printerror(status); return false; } return true; }
bool avtFITSFileFormat::GetKeywordValue(const char *keyword, char *value) { std::string key(keyword); int start = key.size(); char comment[FLEN_COMMENT]; int status = 0; for(int i = start; i < FLEN_KEYWORD; ++i) { // Try and get the key value if(fits_read_key(fits, TSTRING, (char *)key.c_str(), value, comment, &status) == 0) { return true; } else break; key += " "; } return false; }
/* 初始化fits信息的结构体 */ void initFitsInfo(fitsfile* fptr,struct Fitsinfo* fi,float realDm,int* status) { int value; float tbin; char strVal[CVALLEN]; //fits_read_key(fptr,TSTRING,"OBS_MODE",strVal,NULL,status); //strcpy(fi->obs_Mode,strVal); fits_movabs_hdu(fptr, 2, NULL, status); fits_read_key(fptr,TSTRING,"POL_TYPE",strVal,NULL,status); strcpy(fi->pol_type,strVal); fits_read_key(fptr,TINT,"NCHAN",&value,NULL,status); fi->nCHAN = value; fits_read_key(fptr,TINT,"NPOL",&value,NULL,status); fi->nPOL = value; fits_read_key(fptr,TINT,"NSBLK",&value,NULL,status); fi->nSBLK = value; fits_read_key(fptr,TINT,"NAXIS2",&value,NULL,status); fi->nROW = value; fits_read_key(fptr,TFLOAT,"TBIN",&tbin,NULL,status); fi->tBIN = tbin; initDmArr(fi->dmArr,realDm); fi->lowF = 280; fi->highF = 343; return; }
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 extractAvePlane (char *cubepath, char *impath, int iplane, int nplaneave, int *splaneave, int *eplaneave, char *errmsg) { struct stat buf; struct FitsHdr hdr; char str[1024]; char cmd[1024]; int bitpix; int istatus; int nhdu, hdutype, hdunum; int nelements; int naxis3; int l; int j; int i; int jj; int nullcnt; int splane; int eplane; long fpixel[4]; long fpixelo[4]; double *fitsbuf; double *imbuff; fitsfile *infptr; fitsfile *outfptr; int debugfile = 1; int debugfile1 = 0; /* Make a NaN value to use setting blank pixels */ union { double d; char c[8]; } value; double nan; for(i=0; i<8; ++i) value.c[i] = 255; nan = value.d; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "\nEnter extractAvePlane: cubepath= [%s]\n", cubepath); fprintf (fp_debug, "iplane= [%d]\n", iplane); fprintf (fp_debug, "nplaneave= [%d]\n", nplaneave); fprintf (fp_debug, "impath= [%s]\n", impath); fflush (fp_debug); } splane = iplane - nplaneave/2; eplane = splane + nplaneave - 1; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "splane= [%d] eplane= [%d]\n", splane, eplane); fflush (fp_debug); } istatus = 0; if (fits_open_file (&infptr, cubepath, READONLY, &istatus)) { if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "istatus= [%d]\n", istatus); fflush (fp_debug); } sprintf (errmsg, "Failed to open FITS file [%s]\n", cubepath); return (-1); } hdunum = 1; nhdu = 0; istatus = 0; istatus = fits_get_num_hdus (infptr, &nhdu, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_get_hdu_num: istatus= [%d] nhdu= [%d]\n", istatus, nhdu); fflush (fp_debug); } if (hdunum > nhdu) { sprintf (errmsg, "fname [%s] doesn't contain any HDU", cubepath); return (-1); } /* Read fits keywords from the first HDU */ hdutype = 0; istatus = 0; istatus = fits_movabs_hdu (infptr, hdunum, &hdutype, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_movabs_hdu: istatus= [%d] hdutype= [%d]\n", istatus, hdutype); fflush (fp_debug); } /* Read fits keywords */ istatus = 0; istatus = fits_read_key (infptr, TSTRING, "simple", str, (char *)NULL, &istatus); if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword SIMPLE not found in fits header"); return (-1); } if ((strcmp (str, "T") != 0) && (strcmp (str, "F") != 0)) { sprintf (errmsg, "keyword SIMPLE must be T or F"); return (-1); } istatus = 0; istatus = fits_read_key (infptr, TSTRING, "bitpix", str, (char *)NULL, &istatus); if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword BITPIX not found in fits header"); return (-1); } istatus = str2Integer (str, &bitpix, errmsg); if (istatus != 0) { sprintf (errmsg, "keyword BITPIX must be an integer"); return (-1); } if ((bitpix != 8) && (bitpix != 16) && (bitpix != 32) && (bitpix != 64) && (bitpix != -32) && (bitpix != -64)) { sprintf (errmsg, "keyword BITPIX value must be 8, 16, 32, 64, -32, -64"); return (-1); } hdr.bitpix = bitpix; istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis", str, (char *)NULL, &istatus); if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.naxis, errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis to integer"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "naxis= [%d]\n", hdr.naxis); fflush (fp_debug); } istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis1", str, (char *)NULL, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_read_key: istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis1 not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.ns, errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis1 string to integer"); return (-1); } hdr.naxes[0] = hdr.ns; istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis2", str, (char *)NULL, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_read_key: istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis2 not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.nl, errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis2 string to integer"); return (-1); } hdr.naxes[1] = hdr.nl; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "ns= [%d] nl= [%d]\n", hdr.ns, hdr.nl); fflush (fp_debug); } hdr.nplane = 1; if (hdr.naxis > 2) { istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis3", str, (char *)NULL, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_read_key: istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis3 not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.naxes[2], errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis3 string to integer"); return (-1); } hdr.nplane = hdr.naxes[2]; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "naxes[2]= [%d]\n", hdr.naxes[2]); fflush (fp_debug); } } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "nplane= [%d]\n", hdr.nplane); fflush (fp_debug); } istatus = stat (impath, &buf); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "impath exists? : istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus >= 0) { sprintf (cmd, "unlink %s", impath); istatus = system (cmd); } /* Create output fits file */ istatus = 0; if (fits_create_file (&outfptr, impath, &istatus)) { sprintf (errmsg, "Failed to create output fitsfile [%s]\n", impath); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "err: [%s]\n", errmsg); fflush (fp_debug); } return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "outptr created\n"); fflush (fp_debug); } /* Copy input fits header to output fitsfile */ istatus = 0; if (fits_copy_header (infptr, outfptr, &istatus)) { strcpy (errmsg, "Failed to copy fitshdr\n"); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "err: [%s]\n", errmsg); fflush (fp_debug); } return (-1); } /* Update header keyword NAXIS3 */ naxis3 = 1; istatus = 0; if (fits_update_key_lng(outfptr, "NAXIS3", naxis3, (char *)NULL, &istatus)) { strcpy (errmsg, "Failed to update keyword NAXIS3\n"); return (-1); } istatus = 0; if (fits_close_file (infptr, &istatus)) { sprintf (errmsg, "Failed to close cubepath [%s]\n", cubepath); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "cubepath= [%s]\n", cubepath); fflush (fp_debug); } istatus = 0; if (fits_open_file (&infptr, cubepath, READONLY, &istatus)) { if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "istatus= [%d]\n", istatus); fflush (fp_debug); } sprintf (errmsg, "Failed to open FITS file [%s]\n", cubepath); return (-1); } /* Create imbuff for average image */ imbuff = (double *)malloc (hdr.ns*hdr.nl*sizeof(double)); for (i=0; i<hdr.nl*hdr.ns; i++) { imbuff[i] = 0.; } /* Read data from nth plane and write to output fitsfile */ nelements = hdr.ns; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "iplane= [%d]\n", iplane); fflush (fp_debug); } fitsbuf = (double *)malloc(hdr.ns*sizeof(double)); if (splane < 1) splane = 1; if (eplane > hdr.nplane) eplane = hdr.nplane; for (l=splane; l<=eplane; l++) { fpixel[0] = 1; fpixel[1] = 1; fpixel[2] = l; fpixel[3] = 1; for (j=0; j<hdr.nl; j++) { if (j == 0) { if ((debugfile1) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "l= [%d] fpixel[2]= [%ld]\n", l, fpixel[2]); fflush (fp_debug); } } if (fits_read_pix (infptr, TDOUBLE, fpixel, nelements, &nan, fitsbuf, &nullcnt, &istatus)) { break; } if (j == 10) { if ((debugfile1) && (fp_debug != (FILE *)NULL)) { for (i=0; i<hdr.ns; i++) { fprintf (fp_debug, "j= [%d] i= [%d] fitsbuf= [%lf]\n", j, i, fitsbuf[i]); fprintf (fp_debug, "fitsbuf/nplaneave= [%lf]\n", fitsbuf[i]/nplaneave); } fflush (fp_debug); } } /* Copy data to imbuff for plane averaging */ jj = hdr.ns*j; if ((debugfile1) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "jj= [%d]\n", jj); fflush (fp_debug); } for (i=0; i<hdr.ns; i++) { imbuff[jj+i] += fitsbuf[i]/nplaneave; } if (j == 10) { if ((debugfile1) && (fp_debug != (FILE *)NULL)) { for (i=0; i<hdr.ns; i++) { fprintf (fp_debug, "j= [%d] i= [%d] imbuff= [%lf]\n", j, i, imbuff[jj+i]); } fflush (fp_debug); } } fpixel[1]++; } } /* Write averaged image to output FITS file */ fpixelo[0] = 1; fpixelo[1] = 1; fpixelo[2] = 1; fpixelo[3] = 1; for (j=0; j<hdr.nl; j++) { jj = hdr.ns*j; for (i=0; i<hdr.ns; i++) { fitsbuf[i] = imbuff[jj+i]; } if (j == 10) { if ((debugfile1) && (fp_debug != (FILE *)NULL)) { for (i=0; i<hdr.ns; i++) { fprintf (fp_debug, "j= [%d] i= [%d] fitsbuf= [%lf]\n", j, i, fitsbuf[i]); } fflush (fp_debug); } } if (fits_write_pix (outfptr, TDOUBLE, fpixelo, nelements, (void *)fitsbuf, &istatus)) { sprintf (errmsg, "fits write error: l= [%d]\n", l); return (-1); } fpixelo[1]++; } istatus = 0; if (fits_close_file (infptr, &istatus)) { sprintf (errmsg, "Failed to close cubepath [%s]\n", cubepath); return (-1); } istatus = 0; if (fits_close_file (outfptr, &istatus)) { sprintf (errmsg, "Failed to close impath [%s]\n", impath); return (-1); } *splaneave = splane; *eplaneave = eplane; return (0); }
int main(int argc, char *argv[]) { fitsfile *infptr = 0, *outfptr = 0; /* FITS file pointers */ int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int icol = 0, incols = 0, outcols = 0, intype = 0, outtype = 0, check = 1; long inrep = 0, outrep = 0, width = 0, inrows = 0, outrows = 0, ii = 0, jj = 0; unsigned char *buffer = 0; int printhelp = (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)); if (printhelp || argc != 3) { fprintf(stderr, "Usage: %s infile1[ext][filter] outfile[ext]\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "Merge 2 tables by copying all the rows from the 1st table\n"); fprintf(stderr, "into the 2nd table. The 2 tables must have identical\n"); fprintf(stderr, "structure, with the same number of columns with the same\n"); fprintf(stderr, "datatypes. This program modifies the output file in place,\n"); fprintf(stderr, "rather than creating a whole new output file.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Examples: \n"); fprintf(stderr, "\n"); fprintf(stderr, "1. %s intab.fit+1 outtab.fit+2\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " merge the table in the 1st extension of intab.fit with\n"); fprintf(stderr, " the table in the 2nd extension of outtab.fit.\n"); fprintf(stderr, "\n"); fprintf(stderr, "2. %s 'intab.fit+1[PI > 45]' outab.fits+2\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " Same as the 1st example, except only rows that have a PI\n"); fprintf(stderr, " column value > 45 will be merged into the output table.\n"); fprintf(stderr, "\n"); return (0); } /* open both input and output files and perform validity checks */ if (fits_open_file(&infptr, argv[1], READONLY, &status) || fits_open_file(&outfptr, argv[2], READWRITE, &status)) { fprintf(stderr, " Couldn't open both files\n"); } else if (fits_get_hdu_type(infptr, &intype, &status) || fits_get_hdu_type(outfptr, &outtype, &status)) { fprintf(stderr, "couldn't get the type of HDU for the files\n"); } else if (intype == IMAGE_HDU) { fprintf(stderr, "The input HDU is an image, not a table\n"); } else if (outtype == IMAGE_HDU) { fprintf(stderr, "The output HDU is an image, not a table\n"); } else if (outtype != intype) { fprintf(stderr, "Input and output HDUs are not the same type of table.\n"); } else if (fits_get_num_cols(infptr, &incols, &status) || fits_get_num_cols(outfptr, &outcols, &status)) { fprintf(stderr, "Couldn't get number of columns in the tables\n"); } else if (incols != outcols) { fprintf(stderr, "Input and output HDUs don't have same # of columns.\n"); } else if (fits_read_key(infptr, TLONG, "NAXIS1", &width, NULL, &status)) { fprintf(stderr, "Couldn't get width of input table\n"); } else if (!(buffer = (unsigned char *) malloc(width))) { fprintf(stderr, "memory allocation error\n"); } else if (fits_get_num_rows(infptr, &inrows, &status) || fits_get_num_rows(outfptr, &outrows, &status)) { fprintf(stderr, "Couldn't get the number of rows in the tables\n"); } else { /* check that the corresponding columns have the same datatypes */ for (icol = 1; icol <= incols; icol++) { fits_get_coltype(infptr, icol, &intype, &inrep, NULL, &status); fits_get_coltype(outfptr, icol, &outtype, &outrep, NULL, &status); if (intype != outtype || inrep != outrep) { fprintf(stderr, "Column %d is not the same in both tables\n", icol); check = 0; } } if (check && !status) { /* insert 'inrows' empty rows at the end of the output table */ fits_insert_rows(outfptr, outrows, inrows, &status); for (ii = 1, jj = outrows +1; ii <= inrows; ii++, jj++) { /* read row from input and write it to the output table */ fits_read_tblbytes(infptr, ii, 1, width, buffer, &status); fits_write_tblbytes(outfptr, jj, 1, width, buffer, &status); if (status) { break; /* jump out of loop if error occurred */ } } /* all done; now free memory and close files */ fits_close_file(outfptr, &status); fits_close_file(infptr, &status); } } if (buffer) { free(buffer); } if (status) { fits_report_error(stderr, status); /* print any error message */ } return (status); }
int main(int argc, char *argv[]) { static struct option long_opts[] = { {"start", 1, NULL, 's'}, {"end", 1, NULL, 'e'}, {0,0,0,0} }; fitsfile *infptr, *outfptr; /* FITS file pointers defined in fitsio.h */ int opt, opti; int i, status = 0, ii = 1, jj=1; /* status must always be initialized = 0 */ int lo_row, hi_row; unsigned char *buffer = 0; long nrows; int ncols; int tfields; long pcount; char extname[FLEN_VALUE]; char *ttype[NFIELDS_I]; char *tform[NFIELDS_I]; char *tunit[NFIELDS_I]; char *tcomm[NFIELDS_I]; float start=0.0, end=1.0; while ((opt=getopt_long(argc,argv,"s:e:h",long_opts,&opti))!=-1) { switch (opt) { case 's': start = atof(optarg); break; case 'e': end = atof(optarg); break; case 'h': default: usage(); exit(0); break; } } if (argc < 3) { usage(); return(0); } //printf("> %s %s\n", argv[1], argv[2]); //printf("> %s %s\n", argv[optind], argv[2]); /* Allocate space for the table parameters and initialize */ for (i=0; i<NFIELDS_I; i++) { ttype[i] = (char *) malloc(FLEN_VALUE*sizeof(char)); tform[i] = (char *) malloc(FLEN_VALUE*sizeof(char)); tunit[i] = (char *) malloc(FLEN_VALUE*sizeof(char)); tcomm[i] = (char *) malloc(FLEN_CARD*sizeof(char)); strcpy(ttype[i], " "); } /* Open the input file */ if ( !fits_open_file(&infptr, argv[optind], READONLY, &status) ) { /* Create the output file */ if ( !fits_create_file(&outfptr, argv[optind+1], &status) ) { /* Copy every HDU until we get an error */ //while( !fits_movabs_hdu(infptr, ii++, NULL, &status) ) /* Copy first HDU */ fits_copy_hdu(infptr, outfptr, 0, &status); fits_movnam_hdu(infptr, BINARY_TBL, "SUBINT", 0, &status); fits_get_num_rows(infptr, &nrows, &status); printf("Number of rows = %ld\n", nrows); fits_get_num_cols(infptr, &ncols, &status); printf("Number of cols = %d\n", ncols); long width; fits_read_key(infptr, TLONG, "NAXIS1", &width, NULL, &status); printf("Width = %ld\n", width); buffer = (unsigned char *) malloc(width); /* Copy the fits header */ fits_copy_header(infptr, outfptr, &status); //#if 0 /* Determine low and high row number ot copy */ lo_row = 1 + start * nrows; hi_row = end * nrows; printf("lo_row=%d hi_row=%d\n", lo_row, hi_row); for (ii=lo_row, jj=1; ii<=hi_row; ii++, jj++) { printf("\rRead %d/%ld", ii, nrows); fits_read_tblbytes( infptr, ii, 1, width, buffer, &status); fits_write_tblbytes(outfptr, jj, 1, width, buffer, &status); } nrows = (hi_row-lo_row)+1; fits_update_key(outfptr, TLONG, "NAXIS2", &nrows, 0, &status); printf("\nDone\n"); //#endif /* Reset status after normal error */ //if (status == END_OF_FILE) status = 0; fits_close_file(outfptr, &status); } fits_close_file(infptr, &status); } /* if error occured, print out error message */ if (status) fits_report_error(stderr, status); return(status); }
int generateMedianPlane (char *cubepath, char *impath, int iplane, int nplane_in, int *startplane, int *endplane, char *errmsg) { struct stat buf; struct FitsHdr hdr; char str[1024]; char cmd[1024]; int bitpix; int istatus; int nhdu, hdutype, hdunum; long nelements; int naxis3; int l; int i; int j; int jj; int nullcnt; int splane; int eplane; int nplane; int indx; int indx1; int indx2; int npixel; int midpoint; long fpixel[4]; long fpixelo[4]; double *fitscubebuf; double *outbuf; double *fitsbuf1d; double *wavebuf; fitsfile *infptr; fitsfile *outfptr; int debugfile = 1; /* Make a NaN value to use setting blank pixels */ union { double d; char c[8]; } value; double nan; for(i=0; i<8; ++i) value.c[i] = 255; nan = value.d; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "\nEnter generateMedianPlane: cubepath= [%s]\n", cubepath); fprintf (fp_debug, "impath= [%s]\n", impath); fprintf (fp_debug, "iplane= [%d]\n", iplane); fprintf (fp_debug, "nplane_in= [%d]\n", nplane_in); fflush (fp_debug); } nplane = nplane_in; splane = iplane - nplane/2; eplane = splane + nplane - 1; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "splane= [%d] eplane= [%d]\n", splane, eplane); fflush (fp_debug); } /* Open input fits cube */ istatus = 0; if (fits_open_file (&infptr, cubepath, READONLY, &istatus)) { if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "istatus= [%d]\n", istatus); fflush (fp_debug); } sprintf (errmsg, "Failed to open FITS file [%s]\n", cubepath); return (-1); } hdunum = 1; nhdu = 0; istatus = 0; istatus = fits_get_num_hdus (infptr, &nhdu, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_get_hdu_num: istatus= [%d] nhdu= [%d]\n", istatus, nhdu); fflush (fp_debug); } if (hdunum > nhdu) { sprintf (errmsg, "fname [%s] doesn't contain any HDU", cubepath); return (-1); } /* Read fits keywords from the first HDU */ hdutype = 0; istatus = 0; istatus = fits_movabs_hdu (infptr, hdunum, &hdutype, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_movabs_hdu: istatus= [%d] hdutype= [%d]\n", istatus, hdutype); fflush (fp_debug); } /* Read fits keywords */ istatus = 0; istatus = fits_read_key (infptr, TSTRING, "simple", str, (char *)NULL, &istatus); if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword SIMPLE not found in fits header"); return (-1); } if ((strcmp (str, "T") != 0) && (strcmp (str, "F") != 0)) { sprintf (errmsg, "keyword SIMPLE must be T or F"); return (-1); } istatus = 0; istatus = fits_read_key (infptr, TSTRING, "bitpix", str, (char *)NULL, &istatus); if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword BITPIX not found in fits header"); return (-1); } istatus = str2Integer (str, &bitpix, errmsg); if (istatus != 0) { sprintf (errmsg, "keyword BITPIX must be an integer"); return (-1); } if ((bitpix != 8) && (bitpix != 16) && (bitpix != 32) && (bitpix != 64) && (bitpix != -32) && (bitpix != -64)) { sprintf (errmsg, "keyword BITPIX value must be 8, 16, 32, 64, -32, -64"); return (-1); } hdr.bitpix = bitpix; istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis", str, (char *)NULL, &istatus); if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.naxis, errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis to integer"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "naxis= [%d]\n", hdr.naxis); fflush (fp_debug); } istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis1", str, (char *)NULL, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_read_key: istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis1 not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.ns, errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis1 string to integer"); return (-1); } hdr.naxes[0] = hdr.ns; istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis2", str, (char *)NULL, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_read_key: istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis2 not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.nl, errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis2 string to integer"); return (-1); } hdr.naxes[1] = hdr.nl; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "ns= [%d] nl= [%d]\n", hdr.ns, hdr.nl); fflush (fp_debug); } hdr.nplane = 1; if (hdr.naxis > 2) { istatus = 0; istatus = fits_read_key (infptr, TSTRING, "naxis3", str, (char *)NULL, &istatus); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "returned fits_read_key: istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus == KEY_NO_EXIST) { sprintf (errmsg, "keyword naxis3 not found in fits header"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "str= [%s]\n", str); fflush (fp_debug); } istatus = str2Integer (str, &hdr.naxes[2], errmsg); if (istatus < 0) { sprintf (errmsg, "Failed to convert naxis3 string to integer"); return (-1); } hdr.nplane = hdr.naxes[2]; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "naxes[2]= [%d]\n", hdr.naxes[2]); fflush (fp_debug); } } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "hdr.nplane= [%d]\n", hdr.nplane); fflush (fp_debug); } if (splane < 1) splane = 1; if (eplane > hdr.nplane) eplane = hdr.nplane; nplane = eplane - splane + 1; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "hdr.nplane= [%d]\n", hdr.nplane); fprintf (fp_debug, "splane= [%d] eplane= [%d]\n", splane, eplane); fprintf (fp_debug, "nplane= [%d]\n", nplane); fflush (fp_debug); } /* malloc arrays for reading data */ fitscubebuf = (double *)malloc(hdr.ns*hdr.nl*nplane*sizeof(double)); outbuf = (double *)malloc(hdr.ns*hdr.nl*sizeof(double)); wavebuf = (double *)malloc(nplane*sizeof(double)); fitsbuf1d = (double *)malloc(hdr.ns*sizeof(double)); nelements = hdr.ns; npixel = hdr.ns*hdr.nl; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "npixel= [%d]\n", npixel); fflush (fp_debug); } /* Read cube data to fitscubebuf */ fpixel[0] = 1; fpixel[3] = 1; indx = 0; for (l=splane; l<=eplane; l++) { indx2 = hdr.nl*hdr.ns*(l-splane); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "l= [%d]\n", l); fflush (fp_debug); } fpixel[1] = 1; fpixel[2] = l; for (j=0; j<hdr.nl; j++) { indx1 = hdr.ns*j; if (fits_read_pix (infptr, TDOUBLE, fpixel, nelements, &nan, fitsbuf1d, &nullcnt, &istatus)) { break; } for (i=0; i<nelements; i++) { fitscubebuf[indx2+indx1+i] = fitsbuf1d[i]; if ((debugfile) && (fp_debug != (FILE *)NULL)) { if ((i == 30) && (j == 25)) { fprintf (fp_debug, "i=[%d] j=[%d] l=[%d] pixel=[%lf]\n", i, j, l, fitscubebuf[indx2+indx1+i]); fflush (fp_debug); } } } fpixel[1]++; } } /* istatus = 0; if (fits_close_file (infptr, &istatus)) { sprintf (errmsg, "Failed to close cubepath [%s]\n", cubepath); return (-1); } */ /* Extract wave axis and compute median values */ midpoint = nplane / 2; if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "midpint= [%d]\n", midpoint); fflush (fp_debug); } for (j=0; j<hdr.nl; j++) { indx1 = hdr.ns*j; for (i=0; i<hdr.ns; i++) { for (l=0; l<nplane; l++) { indx = npixel*l + indx1 + i; wavebuf[l] = fitscubebuf[indx]; if ((debugfile) && (fp_debug != (FILE *)NULL)) { if ((i == 30) && (j == 25)) { fprintf (fp_debug, "i=[%d] j=[%d] l=[%d] pixel=[%lf]\n", i, j, l, wavebuf[l]); fflush (fp_debug); } } } sort (wavebuf, nplane); if ((debugfile) && (fp_debug != (FILE *)NULL)) { if ((i == 30) && (j == 25)) { fprintf (fp_debug, "after qsort\n"); for (l=0; l<nplane; l++) { fprintf (fp_debug, "l=[%d] pixel=[%lf]\n", l, wavebuf[l]); } fflush (fp_debug); } } outbuf[indx1+i] = wavebuf[midpoint]; if ((debugfile) && (fp_debug != (FILE *)NULL)) { if ((i == 30) && (j == 25)) { fprintf (fp_debug, "outbuf= [%lf]\n", outbuf[indx1+i]); fflush (fp_debug); } } } } /* Create output fits file */ istatus = stat (impath, &buf); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "impath exists? : istatus= [%d]\n", istatus); fflush (fp_debug); } if (istatus >= 0) { sprintf (cmd, "unlink %s", impath); istatus = system (cmd); } istatus = 0; if (fits_create_file (&outfptr, impath, &istatus)) { sprintf (errmsg, "Failed to create output fitsfile [%s]\n", impath); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "err: [%s]\n", errmsg); fflush (fp_debug); } return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "outptr created\n"); fflush (fp_debug); } /* Copy input fits header to output fitsfile */ istatus = 0; if (fits_copy_header (infptr, outfptr, &istatus)) { strcpy (errmsg, "Failed to copy fitshdr\n"); if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "err: [%s]\n", errmsg); fflush (fp_debug); } return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "header copied\n"); fflush (fp_debug); } /* Update header keyword NAXIS3 */ naxis3 = 1; istatus = 0; if (fits_update_key_lng(outfptr, "NAXIS3", naxis3, (char *)NULL, &istatus)) { strcpy (errmsg, "Failed to update keyword NAXIS3\n"); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "naxis3 updated\n"); fflush (fp_debug); } /* Write to output fitsfile */ nelements = hdr.ns; fpixelo[0] = 1; fpixelo[1] = 1; fpixelo[2] = 1; fpixelo[3] = 1; for (j=0; j<hdr.nl; j++) { jj = hdr.ns*j; for (i=0; i<hdr.ns; i++) { fitsbuf1d[i] = outbuf[jj+i]; } if (fits_write_pix (outfptr, TDOUBLE, fpixelo, nelements, (void *)fitsbuf1d, &istatus)) { sprintf (errmsg, "fits write error: l= [%d] j= [%d]\n", l, j); return (-1); } fpixelo[1]++; } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "here2\n"); fflush (fp_debug); } istatus = 0; if (fits_close_file (infptr, &istatus)) { sprintf (errmsg, "Failed to close cubepath [%s]\n", cubepath); return (-1); } if ((debugfile) && (fp_debug != (FILE *)NULL)) { fprintf (fp_debug, "here3\n"); fflush (fp_debug); } istatus = 0; if (fits_close_file (outfptr, &istatus)) { sprintf (errmsg, "Failed to close impath [%s]\n", impath); return (-1); } *startplane = splane; *endplane = eplane; return (0); }