int cfits_read_string_col (char **array, long num_values, long firstrow, const char *colname, cfitsfile *fptr) { char nulval[CFLEN_VALUE]; int status = 0; long firstelem = 1; int anynul, colnum; nulval[0] = '\0'; if (-1 == cfits_get_colnum (&colnum, colname, fptr)) { array = NULL; return -1; } (void) fits_read_col_str ((fitsfile *) fptr, colnum, firstrow, firstelem, num_values, nulval, array, &anynul, &status); cfits_report_error (status); if (status != 0) return -1; return 0; }
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); } }
void c_tbegtt (const IRAFPointer tp, const IRAFPointer cp, int row, char *buffer, int maxch) { /* Read a string value from a table column. arguments: IRAFPointer tp i: table descriptor IRAFPointer cp i: column descriptor int row i: row number (one indexed) char *buffer o: value read from table int maxch i: maximum length of the string (not incl NULL) */ TableDescr *tbl_descr; ColumnDescr *col_descr; int anynul=0; long firstelem=1, nelem=1; char *value; int len; int status = 0; tbl_descr = (TableDescr *)tp; col_descr = (ColumnDescr *)cp; if (col_descr->datatype < 0) { if (col_descr->width >= maxch) len = col_descr->width; else len = maxch; } else { len = SZ_FITS_STR; } value = (char *)calloc (len+1, sizeof(char)); if (col_descr->datatype == IRAF_BOOL) { Bool b_value; c_tbegtb (tp, cp, row, &b_value); if (b_value == True) strcpy (value, "yes"); else strcpy (value, "no"); } else if (col_descr->datatype == IRAF_DOUBLE) { double d_value; c_tbegtd (tp, cp, row, &d_value); if (d_value == IRAF_INDEFD) strcpy (value, "INDEF"); else sprintf (value, "%.16g", d_value); } else if (col_descr->datatype == IRAF_REAL) { float r_value; c_tbegtr (tp, cp, row, &r_value); if (r_value >= 0.99999 * IRAF_INDEFR && r_value <= 1.00001 * IRAF_INDEFR) { strcpy (value, "INDEF"); } else { sprintf (value, "%.7g", r_value); } } else if (col_descr->datatype == IRAF_INT) { int i_value; c_tbegti (tp, cp, row, &i_value); if (i_value == IRAF_INDEFI) strcpy (value, "INDEF"); else sprintf (value, "%d", i_value); } else if (col_descr->datatype == IRAF_SHORT) { short si_value; c_tbegts (tp, cp, row, &si_value); if (si_value == IRAF_INDEFS) strcpy (value, "INDEF"); else sprintf (value, "%hd", si_value); } else { /* fits_read_col_str = ffgcvs */ fits_read_col_str (tbl_descr->fptr, col_descr->colnum, (long)row, firstelem, nelem, "INDEF", &value, &anynul, &status); if (status != 0) setError (status, "c_tbegtt: error reading element"); } copyString (buffer, value, maxch); free (value); }
void processFile(char *fname,tmplStruct *tmpl,FILE *fout,int logit) { pheader *header; ptime_observation *obs; toaStruct toa; fitsfile *fp; int i; float baselineFrac = 0.1; int baselineType = 1; int toaAlgorithm=1; double dPhi; double *offs_sub; double *datFreq; int nSub = 1; FILE *fout_log; char logFname[128]; long double period; sprintf(logFname,"log.ptime.%s",fname); fout_log = fopen(logFname,"w"); offs_sub = (double *)malloc(sizeof(double)*nSub); obs = (ptime_observation *)malloc(sizeof(ptime_observation)); header = (pheader *)malloc(sizeof(pheader)); fp = openFitsFile(fname); loadPrimaryHeader(fp,header); datFreq= (double *)malloc(sizeof(double)*header->nchan); allocateObsMemory(obs,header); readData(obs,header,fp); // Now get an array of subintegration times readSubintOffs(obs,offs_sub,fp); // Get an array of frequencies readDatFreq(obs,datFreq,fp,header->nchan); printf("offs_sub = %g\n",offs_sub[0]); // Calculate the period { FILE *pred_out; int status=0; long nrows,row; char nval[128]="UNKNOWN"; int anynul=0; long double mjd0; long double frequency; char **line; T2Predictor pred; int ret; line = (char **)malloc(sizeof(char *)); line[0] = (char *)malloc(sizeof(char)*1024); if (!(pred_out = fopen("ptime.pred","w"))){ printf("Unable to open file >%s<\n","ptime.pred"); } fits_movnam_hdu(fp,BINARY_TBL,(char *)"T2PREDICT",1,&status); if (status) { printf("No predictor table in PSRFITS file\n"); status=0; } fits_get_num_rows(fp,&nrows,&status); printf("NROWS = %d\n",nrows); for (row = 1; row <= nrows ; row++){ fits_read_col_str(fp,1,row,1,1,nval,line,&anynul,&status); printf("Have read %s\n",line[0]); fprintf(pred_out,"%s\n",line[0]); } free(line[0]); free(line); fclose(pred_out); T2Predictor_Init(&pred); // prepare the predictor if (ret=T2Predictor_Read(&pred,(char *)"ptime.pred")) { printf("Error: unable to read predictor\n"); exit(1); } mjd0 = header->imjd + (header->smjd + header->stt_offs)/86400.0L; frequency = datFreq[0]; //(freq in MHz) WHAT SHOULD THIS BE SET TO!! period = 1.0/T2Predictor_GetFrequency(&pred,mjd0,frequency); if (period==-1){ printf("Error: period returned from predictor = -1. This cannot be correct.\n"); exit(1); } T2Predictor_Destroy(&pred); printf("Period = %.15Lf %.15Lf %.15Lf\n",period,mjd0,frequency); } closeFitsFile(fp); // Must remove a baseline removeBaseline(obs,header,baselineType,baselineFrac); printf("Here\n"); // Now get the shift if (toaAlgorithm == 1) getTOA_alg1(obs,header,tmpl,&toa,fout_log); strcpy(toa.fname,fname); // Calculate the arrival time calcArrivalTime(obs,header,tmpl,&toa,offs_sub,datFreq,period,fout,fout_log); free(header); free(obs); free(offs_sub); free(datFreq); fclose(fout_log); }