Exemple #1
0
int form_toa (char *name_data, char *name_predict, int subint, int chn, int nchn, long int imjd, long int smjd, double offs, double phase, double e_phase, long double *t, long double *e_dt, double *freqout)
// chn is the channel to form toa
// nchn is the total number of subchn
{
	int h;
	h = subint;

	long double dt;  
	double offset;   // offset of each subint
	long double mjd0;  // the mjd of each subint
	T2Predictor pred;
	int ret;
	double period, frequency;
	double freq[nchn];

	// transform phase shift to TOAs
	// get the freq of the subint
	read_freq(name_data, h, freq, nchn);

	frequency = freq[chn];
	(*freqout) = frequency;
    printf ("Frequency is %lf\n", frequency);

	// get the period
    print_t2pred(name_predict);   // output t2pred.dat

	T2Predictor_Init(&pred);  // prepare the predictor
	if (ret=T2Predictor_Read(&pred,(char *)"t2pred.dat"))
    {
		printf("Error: unable to read predictor\n");
		exit(1);
	}

	// get the offset of each subint
	offset = read_offs(name_data, h);

	// get the period at mjd0
	mjd0 = (long double)(imjd) + ((long double)(smjd) + (long double)(offs) + (long double)(offset))/86400.0L;
	printf ("imjd is %ld \n", imjd);
	printf ("mjd0 is %.15Lf \n", mjd0);

	period = 1.0/T2Predictor_GetFrequency(&pred,mjd0,frequency);
    printf ("Period is %.15lf\n", period);
	
	// transform phase shift to time shift
    //dt = (phase/M_PI)*period/2.0;
    //e_dt = (e_phase/M_PI)*period/2.0;
    dt = ((long double)(phase)/M_PI)*((long double)(period))/2.0L;
    (*e_dt) = ((long double)(e_phase)/M_PI)*((long double)(period))/2.0L;
    printf ("dt is %.10Lf +/- %.10Lf\n", dt, e_dt);

	// calculate the TOA
    (*t) = (long double)(imjd) + ((long double)(smjd) + (long double)(offs) - (long double)(dt) + (long double)(offset))/86400.0L;
    //t = imjd;
		
    printf ("offset is %lf\n", offset);
	//fprintf (fp, "%s  %lf  %.15Lf  %Lf  7\n", fname, frequency, t, e_dt*1e+6);

	return 0;
}
Exemple #2
0
int T2Predictor_GetPlan_Ext(char *filename,
			long double mjd_start,
			long double mjd_end,
			long double step, // seconds
			long double freq, // MHz
			// remaining arguments are returned
			char *psrname, char *sitename,
			long double *phase0,
			int *nsegments,
			long double *pulse_frequencies)
{
  int iseg, ret;
  long double lastphase, phase, mjd, lastmjd;
  T2Predictor pred;

  if ((ret=T2Predictor_Read(&pred, filename)))
    return ret;
  if (psrname)
    strcpy(psrname, T2Predictor_GetPSRName(&pred));
  if (sitename)
    strcpy(sitename, T2Predictor_GetSiteName(&pred));

  *nsegments = (int)ceill((mjd_end-mjd_start)*86400.0L/step);
  *phase0 = lastphase = T2Predictor_GetPhase(&pred, mjd_start, freq);
  lastmjd = mjd_start;
  printf("SILLY UPDATE IN POLYCO: REMOVE\n");
  printf("lastmjd = %.20Lf\n",mjd_start);

  for (iseg=0; iseg < *nsegments; iseg++)
  {
    mjd = mjd_start +(iseg+1)*step/86400.0L;
    if (mjd > mjd_end)
      mjd = mjd_end;
    phase = T2Predictor_GetPhase(&pred, mjd, freq);
    pulse_frequencies[iseg] = (phase-lastphase)/(mjd-lastmjd)/86400.0L;
    lastphase = phase;
    lastmjd = mjd;
  }

  T2Predictor_Destroy(&pred);

  return 0;
}
Exemple #3
0
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);
}