int LFIIOSource::readField( double *v, const QString& field, int s, int n ) { double dNan = strtod( "nan", NULL ); fitsfile* ffits; bool bOk; int i; int iCol; int iRead = -1; int iStatus; int iAnyNull; int iResult = 0; if( n < 0 ) { n = 1; /* n < 0 means read one sample, not frame - irrelavent here */ } if( field == "INDEX" ) { for( i = 0; i < n; i++ ) { v[i] = (double)( s + i ); } iRead = n; } else { bOk = getColNumber( field, &iCol ); if( bOk ) { _valid = false; QFile file( _filename ); if( file.exists( ) ) { iResult = fits_open_table( &ffits, _filename.ascii( ), READONLY, &iStatus ); if( iResult == 0 ) { _valid = true; // // copy the data... // N.B. fitsio column indices are 1 based, so we ask for iCol+1 instead of just iCol // fits_modify_vector_len( ffits, iCol+1, 1, &iStatus ); iResult = fits_read_col( ffits, TDOUBLE, iCol+1, s+1, 1, n+1, &dNan, v, &iAnyNull, &iStatus ); if( iResult == 0 ) { iRead = n; } fits_close_file( ffits, &iStatus ); } } } } return iRead; }
int sdfits_create(struct sdfits *sf) { int itmp, *status; char ctmp[40]; struct hdrinfo *hdr; hdr = &(sf->hdr); // dereference the ptr to the header struct status = &(sf->status); // dereference the ptr to the CFITSIO status // Initialize the key variables if needed if (sf->new_file == 1) { // first time writing to the file sf->status = 0; sf->tot_rows = 0; sf->N = 0L; sf->T = 0.0; sf->mode = 'w'; // Create the output directory if needed char datadir[1024]; strncpy(datadir, sf->basefilename, 1023); char *last_slash = strrchr(datadir, '/'); if (last_slash!=NULL && last_slash!=datadir) { *last_slash = '\0'; printf("Using directory '%s' for output.\n", datadir); char cmd[1024]; sprintf(cmd, "mkdir -m 1777 -p %s", datadir); system(cmd); } sf->new_file = 0; } sf->filenum++; sf->rownum = 1; sprintf(sf->filename, "%s_%04d.fits", sf->basefilename, sf->filenum); // Create basic FITS file from our template char *vegas_dir = getenv("VEGAS_DIR"); char template_file[1024]; if (vegas_dir==NULL) { fprintf(stderr, "Error: VEGAS_DIR environment variable not set, exiting.\n"); exit(1); } printf("Opening file '%s'\n", sf->filename); sprintf(template_file, "%s/%s", vegas_dir, SDFITS_TEMPLATE); fits_create_template(&(sf->fptr), sf->filename, template_file, status); // Check to see if file was successfully created if (*status) { fprintf(stderr, "Error creating sdfits file from template.\n"); fits_report_error(stderr, *status); exit(1); } // Go to the primary HDU fits_movabs_hdu(sf->fptr, 1, NULL, status); // Update the keywords that need it fits_get_system_time(ctmp, &itmp, status); // date the file was written fits_update_key(sf->fptr, TSTRING, "DATE", ctmp, NULL, status); // Go to the SINGLE DISH HDU fits_movnam_hdu(sf->fptr, BINARY_TBL, "SINGLE DISH", 0, status); // Update the keywords that need it fits_update_key(sf->fptr, TSTRING, "TELESCOP", hdr->telescope,NULL, status); fits_update_key(sf->fptr, TDOUBLE, "BANDWID", &(hdr->bandwidth), NULL, status); fits_update_key(sf->fptr, TSTRING, "DATE-OBS", hdr->date_obs, NULL, status); fits_update_key(sf->fptr, TDOUBLE, "TSYS", &(hdr->tsys), NULL, status); fits_update_key(sf->fptr, TSTRING, "PROJID", hdr->projid, NULL, status); fits_update_key(sf->fptr, TSTRING, "FRONTEND", hdr->frontend, NULL, status); fits_update_key(sf->fptr, TDOUBLE, "OBSFREQ", &(hdr->obsfreq), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "SCAN", &(hdr->scan), NULL, status); fits_update_key(sf->fptr, TSTRING, "INSTRUME", hdr->instrument, NULL, status); fits_update_key(sf->fptr, TSTRING, "CAL_MODE", hdr->cal_mode, NULL, status); if (strcmp("OFF", hdr->cal_mode) != 0) { fits_update_key(sf->fptr, TDOUBLE, "CAL_FREQ", &(hdr->cal_freq), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "CAL_DCYC", &(hdr->cal_dcyc), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "CAL_PHS", &(hdr->cal_phs), NULL, status); } fits_update_key(sf->fptr, TINT, "NPOL", &(hdr->npol), NULL, status); fits_update_key(sf->fptr, TINT, "NCHAN", &(hdr->nchan), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "CHAN_BW", &(hdr->chan_bw), NULL, status); fits_update_key(sf->fptr, TINT, "NSUBBAND", &(hdr->nsubband), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "EFSAMPFR", &(hdr->efsampfr), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "FPGACLK", &(hdr->fpgaclk), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "HWEXPOSR", &(hdr->hwexposr), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "FILTNEP", &(hdr->filtnep), NULL, status); fits_update_key(sf->fptr, TDOUBLE, "STTMJD", &(hdr->sttmjd), NULL, status); // Update the column sizes for the colums containing arrays itmp = hdr->nsubband * hdr->nchan * 4; //num elements, not bytes fits_modify_vector_len(sf->fptr, 20, itmp, status); // DATA fits_modify_vector_len(sf->fptr, 14, hdr->nsubband, status); // SUBFREQ // Update the TDIM field for the data column sprintf(ctmp, "(%d,%d,4,1,1)", hdr->nchan, hdr->nsubband); fits_update_key(sf->fptr, TSTRING, "TDIM20", ctmp, NULL, status); fits_flush_file(sf->fptr, status); return *status; }
int psrfits_create(struct psrfits *pf) { int itmp, *status; long long lltmp; long double ldtmp; double dtmp; char ctmp[40]; struct hdrinfo *hdr; struct foldinfo *fld; hdr = &(pf->hdr); // dereference the ptr to the header struct status = &(pf->status); // dereference the ptr to the CFITSIO status fld = &(pf->fold); // ptr to foldinfo struct // Figure out what mode this is int mode=0; mode = psrfits_obs_mode(hdr->obs_mode); if (mode==fold) { if (hdr->onlyI) printf("Warning! In folding mode and ONLY_I is set!\n"); if (hdr->ds_time_fact > 1) printf("Warning! In folding mode and DS_TIME is > 1!\n"); if (hdr->ds_freq_fact > 1) printf("Warning! In folding mode and DS_FREQ is > 1!\n"); } // Initialize the key variables if needed if (pf->filenum == 0) { // first time writing to the file pf->status = 0; pf->tot_rows = 0; pf->N = 0L; pf->T = 0.0; hdr->offset_subint = 0; pf->mode = 'w'; // Create the output directory if needed char datadir[1024]; strncpy(datadir, pf->basefilename, 1023); char *last_slash = strrchr(datadir, '/'); if (last_slash!=NULL && last_slash!=datadir) { *last_slash = '\0'; printf("Using directory '%s' for output.\n", datadir); char cmd[1024]; sprintf(cmd, "mkdir -m 1777 -p %s", datadir); system(cmd); } } pf->filenum++; pf->rownum = 1; hdr->offset_subint = pf->tot_rows; // Update the filename - don't include filenum for fold mode // TODO : use rf/cf extensions for psr/cals? if (mode==fold && pf->multifile!=1) sprintf(pf->filename, "%s.fits", pf->basefilename); else sprintf(pf->filename, "%s_%04d.fits", pf->basefilename, pf->filenum); // Create basic FITS file from our template // Fold mode template has additional tables (polyco, ephem) char template_dir[1024]; char template_file[1024]; #ifdef PSRFITS_TEMPLATE_DIR sprintf(template_dir, "%s", PSRFITS_TEMPLATE_DIR); #else char *guppi_dir = getenv("GUPPI_DIR"); if (guppi_dir==NULL) { fprintf(stderr, "Error: GUPPI_DIR environment variable not set, exiting.\n"); exit(1); } sprintf(template_dir, "%s/src", guppi_dir); #endif printf("Opening file '%s' ", pf->filename); if (mode==search) { printf("in search mode.\n"); sprintf(template_file, "%s/%s", template_dir, PSRFITS_SEARCH_TEMPLATE); } else if (mode==fold) { printf("in fold mode.\n"); sprintf(template_file, "%s/%s", template_dir, PSRFITS_FOLD_TEMPLATE); } fits_create_template(&(pf->fptr), pf->filename, template_file, status); // Check to see if file was successfully created if (*status) { fprintf(stderr, "Error creating psrfits file from template.\n"); fits_report_error(stderr, *status); exit(1); } // Go to the primary HDU fits_movabs_hdu(pf->fptr, 1, NULL, status); // Update the keywords that need it fits_get_system_time(ctmp, &itmp, status); // Note: this is the date the file was _written_, not the obs start date fits_update_key(pf->fptr, TSTRING, "DATE", ctmp, NULL, status); fits_update_key(pf->fptr, TSTRING, "TELESCOP", hdr->telescope,NULL, status); fits_update_key(pf->fptr, TSTRING, "OBSERVER", hdr->observer, NULL, status); fits_update_key(pf->fptr, TSTRING, "PROJID", hdr->project_id, NULL, status); fits_update_key(pf->fptr, TSTRING, "FRONTEND", hdr->frontend, NULL, status); fits_update_key(pf->fptr, TSTRING, "BACKEND", hdr->backend, NULL, status); if (hdr->onlyI || hdr->summed_polns) { if (!hdr->onlyI && hdr->npol > 1) { printf("Warning!: Can't have %d polarizations _and_ be summed!\n", hdr->npol); } itmp = 2; fits_update_key(pf->fptr, TINT, "NRCVR", &itmp, NULL, status); } else { if (hdr->npol > 2) { // Can't have more than 2 real polns (i.e. NRCVR) itmp = 2; fits_update_key(pf->fptr, TINT, "NRCVR", &itmp, NULL, status); } else { fits_update_key(pf->fptr, TINT, "NRCVR", &(hdr->npol), NULL, status); } } fits_update_key(pf->fptr, TSTRING, "FD_POLN", hdr->poln_type, NULL, status); fits_update_key(pf->fptr, TINT, "FD_HAND", &(hdr->fd_hand), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "FD_SANG", &(hdr->fd_sang), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "FD_XYPH", &(hdr->fd_xyph), NULL, status); fits_update_key(pf->fptr, TINT, "BE_PHASE", &(hdr->be_phase), NULL, status); fits_update_key(pf->fptr, TSTRING, "DATE-OBS", hdr->date_obs, NULL, status); if (mode==fold && !strcmp("CAL",hdr->obs_mode)) fits_update_key(pf->fptr, TSTRING, "OBS_MODE", hdr->obs_mode, NULL, status); fits_update_key(pf->fptr, TDOUBLE, "OBSFREQ", &(hdr->fctr), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "OBSBW", &(hdr->BW), NULL, status); fits_update_key(pf->fptr, TINT, "OBSNCHAN", &(hdr->orig_nchan), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "CHAN_DM", &(hdr->chan_dm), NULL, status); fits_update_key(pf->fptr, TSTRING, "SRC_NAME", hdr->source, NULL, status); if (!strcmp("UNKNOWN", hdr->track_mode)) { printf("Warning!: Unknown telescope tracking mode!\n"); } fits_update_key(pf->fptr, TSTRING, "TRK_MODE", hdr->track_mode, NULL, status); // TODO: will need to change the following if we aren't tracking! fits_update_key(pf->fptr, TSTRING, "RA", hdr->ra_str, NULL, status); fits_update_key(pf->fptr, TSTRING, "DEC", hdr->dec_str, NULL, status); fits_update_key(pf->fptr, TSTRING, "STT_CRD1", hdr->ra_str, NULL, status); fits_update_key(pf->fptr, TSTRING, "STP_CRD1", hdr->ra_str, NULL, status); // TODO: update these at the end of the file or obs fits_update_key(pf->fptr, TSTRING, "STT_CRD2", hdr->dec_str, NULL, status); fits_update_key(pf->fptr, TSTRING, "STP_CRD2", hdr->dec_str, NULL, status); fits_update_key(pf->fptr, TDOUBLE, "BMAJ", &(hdr->beam_FWHM), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "BMIN", &(hdr->beam_FWHM), NULL, status); if (strcmp("OFF", hdr->cal_mode)) { fits_update_key(pf->fptr, TDOUBLE, "CAL_FREQ", &(hdr->cal_freq), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "CAL_DCYC", &(hdr->cal_dcyc), NULL, status); fits_update_key(pf->fptr, TDOUBLE, "CAL_PHS", &(hdr->cal_phs), NULL, status); } fits_update_key(pf->fptr, TDOUBLE, "SCANLEN", &(hdr->scanlen), NULL, status); itmp = (int) hdr->MJD_epoch; fits_update_key(pf->fptr, TINT, "STT_IMJD", &itmp, NULL, status); ldtmp = (hdr->MJD_epoch - (long double) itmp) * 86400.0L; // in sec itmp = (int) ldtmp; fits_update_key(pf->fptr, TINT, "STT_SMJD", &itmp, NULL, status); ldtmp -= (long double) itmp; dtmp = (double) ldtmp; fits_update_key(pf->fptr, TDOUBLE, "STT_OFFS", &dtmp, NULL, status); fits_update_key(pf->fptr, TDOUBLE, "STT_LST", &(hdr->start_lst), NULL, status); // If fold mode, copy the parfile into the PSRFITS EPHEM table if (mode==fold) { if (strcmp("CAL",hdr->obs_mode)==0) { // CAL mode has no par file, or no par file given psrfits_remove_ephem(pf); } else if (fld->parfile[0]=='\0') { // No par file given fprintf(stderr, "psrfits_create warning: " "Fold mode selected, but no parfile given - " "EPHEM table will be removed.\n" ); psrfits_remove_ephem(pf); } else { FILE *parfile = fopen(fld->parfile, "r"); if (parfile==NULL) { fprintf(stderr, "psrfits_create warning: " "Error opening parfile %s - " "EPHEM table will be removed.\n", fld->parfile ); psrfits_remove_ephem(pf); } else { psrfits_write_ephem(pf, parfile); fclose(parfile); } } } // Go to the SUBINT HDU fits_movnam_hdu(pf->fptr, BINARY_TBL, "SUBINT", 0, status); // Update the keywords that need it if (hdr->onlyI) { itmp = 1; fits_update_key(pf->fptr, TINT, "NPOL", &itmp, NULL, status); } else { fits_update_key(pf->fptr, TINT, "NPOL", &(hdr->npol), NULL, status); } if (!hdr->onlyI && !hdr->summed_polns) { // TODO: These need to be updated for the real machine. if (hdr->npol==1) strcpy(ctmp, "AA"); else if (hdr->npol==2) strcpy(ctmp, "AABB"); else if (hdr->npol==4) { if (strncmp(hdr->poln_order, "AABBCRCI", 8)==0) strcpy(ctmp, hdr->poln_order); else strcpy(ctmp, "IQUV"); } fits_update_key(pf->fptr, TSTRING, "POL_TYPE", ctmp, NULL, status); } else { fits_update_key(pf->fptr, TSTRING, "POL_TYPE", "AA+BB", NULL, status); } // TODO what does TBIN mean in fold mode? dtmp = hdr->dt * hdr->ds_time_fact; fits_update_key(pf->fptr, TDOUBLE, "TBIN", &dtmp, NULL, status); fits_update_key(pf->fptr, TINT, "NSUBOFFS", &(hdr->offset_subint), NULL, status); itmp = hdr->nchan / hdr->ds_freq_fact; fits_update_key(pf->fptr, TINT, "NCHAN", &itmp, NULL, status); dtmp = hdr->df * hdr->ds_freq_fact; fits_update_key(pf->fptr, TDOUBLE, "CHAN_BW", &dtmp, NULL, status); if (mode==search) { int out_nsblk = hdr->nsblk / hdr->ds_time_fact; itmp = 1; fits_update_key(pf->fptr, TINT, "NSBLK", &out_nsblk, NULL, status); fits_update_key(pf->fptr, TINT, "NBITS", &(hdr->nbits), NULL, status); fits_update_key(pf->fptr, TINT, "NBIN", &itmp, NULL, status); } else if (mode==fold) { itmp = 1; fits_update_key(pf->fptr, TINT, "NSBLK", &itmp, NULL, status); fits_update_key(pf->fptr, TINT, "NBITS", &itmp, NULL, status); fits_update_key(pf->fptr, TINT, "NBIN", &(hdr->nbin), NULL, status); fits_update_key(pf->fptr, TSTRING, "EPOCHS", "MIDTIME", NULL, status); } // Update the column sizes for the colums containing arrays { int out_npol = hdr->npol; int out_nchan = hdr->nchan / hdr->ds_freq_fact; if (hdr->onlyI) out_npol = 1; int out_nsblk = hdr->nsblk / hdr->ds_time_fact; fits_modify_vector_len(pf->fptr, 13, out_nchan, status); // DAT_FREQ fits_modify_vector_len(pf->fptr, 14, out_nchan, status); // DAT_WTS itmp = out_nchan * out_npol; fits_modify_vector_len(pf->fptr, 15, itmp, status); // DAT_OFFS fits_modify_vector_len(pf->fptr, 16, itmp, status); // DAT_SCL if (mode==search) { lltmp = out_nsblk; lltmp = (lltmp * hdr->nbits * out_nchan * out_npol) / 8L; } else if (mode==fold) lltmp = (hdr->nbin * out_nchan * out_npol); fits_modify_vector_len(pf->fptr, 17, lltmp, status); // DATA // Update the TDIM field for the data column if (mode==search) sprintf(ctmp, "(1,%d,%d,%d)", out_nchan, out_npol, out_nsblk); else if (mode==fold) sprintf(ctmp, "(%d,%d,%d,1)", hdr->nbin, out_nchan, out_npol); fits_update_key(pf->fptr, TSTRING, "TDIM17", ctmp, NULL, status); } fits_flush_file(pf->fptr, status); return *status; }