Exemple #1
0
int main(int argc,char *argv[])
{
  int i;
  char fname[128];
  dSet *data; 
  float freq,bw,chanbw;
  int nchan,npol;
  float bpass[4096];
  float fx[4096];
  float miny,maxy,minx,maxx;
  float ominy,omaxy,ominx,omaxx;
  float mx,my,mx2,my2;
  float binw;
  char key;
  char grDev[128]="/xs";
  int interactive=1;
  int noc1=0;
  int zapChannels[4096];
  int nzap=0;
  int overlay=-1;
  float overlayVal[MAX_OVERLAY];
  char overlayStr[MAX_OVERLAY][128];
  char overlayFile[128];
  int noverlay=0;
  fitsfile *fp;

  data = initialiseDset();
  

  for (i=0;i<argc;i++)
    {
      if (strcmp(argv[i],"-f")==0)
	strcpy(fname,argv[++i]);
      else if (strcmp(argv[i],"-noc1")==0)
	noc1=1;
      else if (strcmp(argv[i],"-g")==0)
	{
	  strcpy(grDev,argv[++i]);
	  interactive=0;
	}
      else if (strcmp(argv[i],"-h")==0)
	help();
      else if (strcmp(argv[i],"-overlay")==0)
	{
	  strcpy(overlayFile,argv[++i]);
	  overlay=1;
	}
    }
  if (overlay==1)
    {
      FILE *fin;
      char line[1024];
      noverlay=0;

      if (!(fin = fopen(overlayFile,"r")))
	  printf("Unable to open overlay file >%s<\n",overlayFile);
      else
	{
	  while (!feof(fin))
	    {
	      fgets(overlayStr[noverlay],1024,fin);
	      if (fscanf(fin,"%f",&overlayVal[noverlay])==1)
		{
		  if (overlayStr[noverlay][strlen(overlayStr[noverlay])-1] == '\n')
		    overlayStr[noverlay][strlen(overlayStr[noverlay])-1]='\0';
		  noverlay++;
		}
	    }
	  fclose(fin);
	}
    }
  fp   = openFitsFile(fname); 
  loadPrimaryHeader(fp,data);
  displayHeaderInfo(data);
  readBandpass(fp,bpass);
  nchan = data->phead.nchan;
  freq  = data->phead.freq;
  bw    = data->phead.bw;
  chanbw = data->phead.chanbw;

  for (i=0;i<nchan;i++)
    {
      fx[i] = freq-bw/2+(i+0.5)*chanbw;
      if (i==noc1)
	{
	  miny = maxy = bpass[i];
	  minx = maxx = fx[i];
	}
      else if (i!=0)
	{
	  if (bpass[i] > maxy) maxy = bpass[i];
	  if (bpass[i] < miny) miny = bpass[i];
	  if (fx[i] > maxx) maxx = fx[i];
	  if (fx[i] < minx) minx = fx[i];
	}
    }
  ominx = minx;
  omaxx = maxx;
  ominy = miny;
  omaxy = maxy;
  binw = fx[1]-fx[0];
  printf("Complete\n");

  cpgbeg(0,grDev,1,1);
  cpgask(0);
  do {
    cpgenv(minx,maxx,miny,maxy,0,1);
    cpglab("Frequency (MHz)","Amplitude (arbitrary)",fname);
    cpgbin(nchan-noc1,fx+noc1,bpass+noc1,0);
    if (overlay==1)
      {
	float tx[2],ty[2];
	cpgsls(4); cpgsci(2); cpgsch(0.8);
	for (i=0;i<noverlay;i++)
	  {
	    tx[0] = tx[1] = overlayVal[i];
	    ty[0] = miny;
	    ty[1] = maxy;
	    if (tx[1] > minx && tx[1] < maxx)
	      {
		cpgline(2,tx,ty);
		//		cpgtext(tx[1],ty[1]-0.05*(maxy-miny),overlayStr[i]);
		cpgptxt(tx[1]-0.004*(maxx-minx),ty[0]+0.05*(maxy-miny),90,0.0,overlayStr[i]);
	      }
	  }
	cpgsci(1); cpgsls(1); cpgsch(1);
      }
    if (interactive==1)
      {
	cpgcurs(&mx,&my,&key);
	if (key=='A')
	  {
	    int cc=-1;
	    int i;
	    for (i=0;i<nchan-1;i++)
	      {
		//		if ((bw > 0 && (mx > fx[i]-binw/2 && mx < fx[i]+binw/2)) ||
		//		    (bw < 0 && (mx > fx[i]+binw/2 && mx < fx[i]-binw/2)))
		if ((bw > 0 && (mx > fx[i] && mx < fx[i]+binw)) ||
		    (bw < 0 && (mx > fx[i] && mx < fx[i]+binw)))
		  {
		    cc = i;
		    break;
		  }
	      }
	    printf("mouse x = %g MHz, mouse y = %g, channel = %d, channel frequency = %g MHz\n",mx,my,cc,fx[cc]);
	  }
	else if (key=='X')
	  {
	    int cc=-1;
	    int i;
	    printf("Deleting %g %g %g\n",mx,fx[10],binw);
	    for (i=0;i<nchan-1;i++)
	      {
		//		if ((bw > 0 && (mx > fx[i]-binw/2 && mx < fx[i]+binw/2)) ||
		//		    (bw < 0 && (mx > fx[i]+binw/2 && mx < fx[i]-binw/2)))
		if ((bw > 0 && (mx > fx[i] && mx < fx[i]+binw)) ||
		    (bw < 0 && (mx > fx[i] && mx < fx[i]+binw)))
		  {
		    cc = i;
		    break;
		  }
	      }
	    printf("Want to delete = %d\n",cc);
	    if (cc != -1)
	      {
		bpass[cc] = 0;
		omaxy = bpass[noc1];
		zapChannels[nzap++] = cc;
		for (i=noc1;i<nchan;i++)
		  {
		    if (omaxy < bpass[i]) omaxy = bpass[i];
		  }
	      }
	  }
	else if (key=='z')
	  {
	    cpgband(2,0,mx,my,&mx2,&my2,&key);
	    if (mx > mx2) {maxx = mx; minx = mx2;}
	    else {maxx = mx2; minx = mx;}
	    
	    if (my > my2) {maxy = my; miny = my2;}
	    else {maxy = my2; miny = my;}	   
	  }
	else if (key=='u')
	  {
	    minx = ominx;
	    maxx = omaxx;
	    miny = ominy;
	    maxy = omaxy;
	  }
	else if (key=='l') // List the channels and frequencies to zap
	  {
	    int i;
	    sortInt(zapChannels,nzap);
	    printf("-------------------------------------------------------\n");
	    printf("Zap channels with first channel = 0\n\n");
	    for (i=0;i<nzap;i++)
	      printf("%d ",zapChannels[i]);
	    printf("\n\n");
	    printf("Zap channels with first channel = 1\n\n");
	    for (i=0;i<nzap;i++)
	      printf("%d ",zapChannels[i]+1);
	    printf("\n\n");
	    printf("Zap channels frequencies:\n\n");
	    for (i=0;i<nzap;i++)
	      printf("%g ",fx[zapChannels[i]]);
	    printf("\n\n");
	    printf("-------------------------------------------------------\n");
	  }
	else if (key=='%') // Enter percentage of the band edges to zap
	  {
	    float percent;
	    int i;

	    printf("Enter band edge percentage to zap ");
	    scanf("%f",&percent);
	    for (i=0;i<nchan;i++)
	      {
		if (i < nchan*percent/100.0 || i > nchan-(nchan*percent/100.0))
		  {
		    bpass[i] = 0;		    
		    zapChannels[nzap++] = i;

		  }
	      }
	    omaxy = bpass[noc1];
	    for (i=noc1;i<nchan;i++)
	      {
		if (omaxy < bpass[i]) omaxy = bpass[i];
	      }

	    // Unzoom
	    minx = ominx;
	    maxx = omaxx;
	    miny = ominy;
	    maxy = omaxy;
	  }

      }
  } while (key != 'q' && interactive==1);
  cpgend();
}
Exemple #2
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);
}