コード例 #1
0
ファイル: bootstrap.C プロジェクト: gdesvignes/tempo2
double random(long *idum)
{
    int j;
    static longdouble r[100],result;
    long m1=259100;
    long ia1=7141;
    long ic1=54773;
    long m2=134456;
    long ia2=8121;
    long ic2=28411;
    long m3=243000;
    long ia3=4561;
    long ic3=51349;
    longdouble rm1,rm2;

    static int iff=0;
    static int ix1=0;
    static int ix2=0;
    static int ix3=0;

    rm1=1./m1;
    rm2=1./m2;

    if(*idum < 0 || iff == 0) 
    {
        iff=1;
        ix1=(int)fortran_mod(ic1-(*idum),m1);
        ix1=(int)fortran_mod(ia1*ix1+ic1,m1);
        ix2=(int)fortran_mod(ix1,m2);
        ix1=(int)fortran_mod(ia1*ix1+ic1,m1);
        ix3=(int)fortran_mod(ix1,m3);

        for (j=0;j<97;j++)
        {
            ix1=(int)fortran_mod(ia1*ix1+ic1,m1);
            ix2=(int)fortran_mod(ia2*ix2+ic2,m2);
            r[j]=(ix1+ix2*rm2)*rm1;
        }
        *idum=1;
    }

    ix1=(int)fortran_mod(ia1*ix1+ic1,m1);
    ix2=(int)fortran_mod(ia2*ix2+ic2,m2);
    ix3=(int)fortran_mod(ia3*ix3+ic3,m3);
    j=(97*ix3)/m3;
    if(j>96 || j<0) {printf("Problem in bootstrap.C (%d)\n",j); exit(1);}

    result = r[j];
    r[j]=(ix1+ix2*rm2)*rm1;
    return result;
}
コード例 #2
0
ファイル: plotMany_plug.C プロジェクト: zhuww/tempo2
// Get sidereal time
double lmst2(double mjd,double olong,double *tsid,double *tsid_der)
{
  double xlst,sdd;
  double gmst0;
  double a = 24110.54841;
  double b = 8640184.812866;
  double c = 0.093104;
  double d = -6.2e-6;
  double bprime,cprime,dprime;
  double tu0,fmjdu1,dtu,tu,seconds_per_jc,gst;
  int nmjdu1;

  nmjdu1 = (int)mjd;
  fmjdu1 = mjd - nmjdu1;

  tu0 = ((double)(nmjdu1-51545)+0.5)/3.6525e4;
  dtu  =fmjdu1/3.6525e4;
  tu = tu0+dtu;
  gmst0 = (a + tu0*(b+tu0*(c+tu0*d)))/86400.0;
  seconds_per_jc = 86400.0*36525.0;

  bprime = 1.0 + b/seconds_per_jc;
  cprime = 2.0 * c/seconds_per_jc;
  dprime = 3.0 * d/seconds_per_jc;

  sdd = bprime+tu*(cprime+tu*dprime);

  gst = gmst0 + dtu*(seconds_per_jc + b + c*(tu+tu0) + d*(tu*tu+tu*tu0+tu0*tu0))/86400;
  xlst = gst - olong/360.0;
  xlst = fortran_mod(xlst,1.0);

  if (xlst<0.0)xlst=xlst+1.0;

  *tsid = xlst;
  *tsid_der = sdd;
  return 0.0;
}
コード例 #3
0
ファイル: tt2tdb.C プロジェクト: ARO-user/work-share
double FB_deltaT(longdouble mjd_tt)
{
  double ctatv;    /* output TDB-TDT */
  longdouble tdt; /* jd1 + jd2  (Julian date) */
  static int tdbnrl=-1;
  int nr,nrecl,j,k,np,nv;
  double jda,jdb,tdbd1,tdbd2,t[2];
  int tdbdt,tdbncf;
  char fname[MAX_FILELEN];
  double dna,dt1,temp,pc[18],tc,twot,dummy;
  static double buf[16];
  int l;

  /* Initialise the TDB-TDT file (tdbinit.f) */
  /* Set up the TDB-TDT ephemeris file for reading */
  nrecl = 4; /* If recl is in bytes (for Sun -- what about LINUX ????) */

  strcpy(fname,getenv(TEMPO2_ENVIRON));
  strcat(fname,TDBTDT_FILE);

  /* Now do the calculations */
  open_file(fname); /* Open a Fortran made file for reading in C */
  tdbd1 = read_double();
  tdbd2 = read_double();
  tdbdt = read_int();
  tdbncf = read_int();
  dummy = read_double();
  dummy = read_double();
  dummy = read_double();
  dummy = read_double();
  dummy = read_double();
	      
  /* Use the corrected TT time and convert to Julian date */
  tdt = mjd_tt + 2400000.5; 
  if (tdt - (int)tdt >= 0.5)
  {
    jda = (int)tdt + 0.5;
    jdb = tdt - (int)tdt - 0.5;
  }
  else
  {
    jda = (int)tdt - 0.5;
    jdb = tdt - (int)tdt + 0.5;
  }
  nr = (int)((jda-tdbd1)/tdbdt)+2; 
  if (nr < 1 || tdt > tdbd2) 
  {
    printf("ERROR [CLK4]: Date %.10f out of range of TDB-TDT table\n",(double)tdt);
    exit(1);
  }
  if (nr!=tdbnrl)
  {
    tdbnrl = nr;
    /* MUST JUMP TO RECORD NR IN THE FILE */
    for (j=0;j<nr-1;j++)
    {
      for (k=0;k<tdbncf;k++)
	buf[k] = read_double();
    }
  }
  t[0] = ((jda-((nr-2)*tdbdt+tdbd1))+jdb)/tdbdt; /* Fraction within record */
  t[1] = 1.0; /* Unused */
	      
	      /* Interpolation: call interp(buf,t,tdbncf,1,  1,   1,   ctatv) */
  np = 2;
  nv = 3;
  twot = 0.0; 
	      
  pc[0] = 1.0; pc[1]=0.0;
	      
  dna = 1.0;
  dt1 = (int)(t[0]);
  temp = dna * t[0];
  l = (int)(temp - dt1)+1;
  tc = 2.0*(fortran_mod(temp,1.0)+dt1)-1.0;
	      
  if (tc != pc[1])
  {
    np = 2;
    nv = 3; 
    pc[1] = tc;
    twot = tc+tc;	  
  } 
  if (np<tdbncf)
  {
    for (k=np+1;k<=tdbncf;k++)
      pc[k-1] = twot*pc[k-2]-pc[k-3];
    np = tdbncf;
  }
  ctatv = 0.0;
  for (j=tdbncf;j>=1;j--)
    ctatv = ctatv +pc[j-1]*buf[j-1];
  close_file();

  return ctatv;
}
コード例 #4
0
ファイル: fake_plug.C プロジェクト: aarchiba/tempo2
extern "C" int graphicalInterface(int argc,char *argv[],pulsar *psr,int *npsr) 
{
  longdouble imjd=-1.0,fmjd=-1.0,ra,grms=0.0;
  longdouble toa,ha0,almst,amjd,hnobs,mjd,trmjd,tstep=0.0;
  longdouble times[MAX_OBSN];
  longdouble out[MAX_OBSN];
  longdouble lowFreq,highFreq,alpha=-3.0,ampPL=1.0e-16;
  int setref=0;
  int nshots,npts;
  int j,count=0,i,k,ii,jj,kk;
  longdouble solsid  = 1.002737909;
  longdouble obslong = 149.0;  /* Longitude of Parkes */
  longdouble freq    = 1440.0;
  long iseed;
  int    site    = 7,p;
  int endit=0;
  long idum = 0;
  int nday = -1;
  float ngap=-1.0;
  char parFile[MAX_PSR][MAX_FILELEN];
  char timFile[MAX_PSR][MAX_FILELEN];
  char str[MAX_FILELEN],str2[MAX_FILELEN];
  double hamax =-1.0;
  char random[100];
  char read_f=0;
  FILE *fout,*fin;
  char addCubic[100]="n",temp[100];
  char formstr[50]="tempo2";
  char smooth[100]="n";
  int  psrNum,giveRMS=-1,setrand=-1,setred=-1,Npsr=0;
  int timesFile=0;
  char fake_fname[100];
  char have_outfile=0;
  char outfile[100];
  char timesfname[100];
  char telID[128]="7"; // Hardcode to Parkes
  int bunching=0; // flag on whether or not observations occur in groups
  // size of gap between observing runs, and length of observing runs.
  // These defaults give 7 observations every 28 days.
  long double gapsize=21,hillsize=7,gapstartmjd;
  // Flag whether or not to ask for red noise variables.
  *npsr = 1;

  strcpy(fake_fname,"fake.rf");

  for (i=0;i<argc;i++){
    if(strcmp(argv[i],"-ndobs")==0){
      sscanf(argv[i+1],"%f",&ngap);
      printf("Have >>%f<< days between observations\n",ngap);
    }
    if(strcmp(argv[i],"-nobsd")==0){
      sscanf(argv[i+1],"%d",&nday);
      printf("Have >>%d<< observations per day\n",nday);
    }
    if (strcmp(argv[i],"-idum")==0){
	sscanf(argv[i+1],"%d",&idum);
	printf("Have idum >>%d<<\n",idum);
    }
    if(strcmp(argv[i],"-ha")==0){
      sscanf(argv[i+1],"%lf",&hamax);
      printf("Have maximum absolute HA >>%lf<<\n",hamax);
    }
    if(strcmp(argv[i],"-randha")==0){
      strcpy(&random[0],argv[i+1]);
      printf("Have random >>%s<<\n",random);
      setrand=1;
    }
    if(strcmp(argv[i],"-start")==0){
      sscanf(argv[i+1],"%Lf",&imjd);
      printf("Have initial MJD >>%lf<<\n",(double)imjd);
    }
    if(strcmp(argv[i],"-tel")==0){
      sscanf(argv[i+1],"%s",telID);
    }
    if(strcmp(argv[i],"-end")==0){
      sscanf(argv[i+1],"%Lf",&fmjd);
      printf("Have final MJD >>%lf<<\n",(double)fmjd);
    }
    if(strcmp(argv[i],"-rms")==0){
      sscanf(argv[i+1],"%Lf",&grms);
      giveRMS=1;
      printf("Have Gaussian noise rms >>%lf<<\n",(double)grms);
    }
    if(strcmp(argv[i],"-format")==0){
	sscanf(argv[i+1],"%s",&formstr);
	printf("Have output format >>%s<<\n",formstr);
    }
    if (strcmp(argv[i],"-times")==0){
      timesFile=1;
      sscanf(argv[i+1],"%s",&timesfname);
      printf("Timesfile = %s\n",timesfname);
    }
    if (strcmp(argv[i],"-setref")==0)
      setref=1;

    if (strcmp(argv[i],"-o")==0){
      have_outfile=1;
      sscanf(argv[i+1],"%s",&outfile);
      printf("outfile = %s\n",outfile);
    }

    if (strcmp(argv[i],"-readtim")==0){
      read_f=1;
      printf("Read name,freq,mjd\n");
    }


    if(strcmp(argv[i],"-group")==0){
	bunching = 1;
	sscanf(argv[i+1],"%Lf",&hillsize);
	sscanf(argv[i+2],"%Lf",&gapsize);
	printf("Will simulate runs of %lg observing days long and leave a gap of %lg days between runs.\n",
	       (double)hillsize,(double)gapsize);
    }
    if(strcmp(argv[i],"-h")==0){
      printf("==========================================================================================\n");
      printf(" fake Tempo2 plugin - usage instructions.\n");
      printf(" tempo2 -gr fake -f file.par: The program will prompt you for parameters.\n");
      printf("\n Command-line arguments:\n");
      printf(" \t -f J0437-4715.par J1909-3744.par J1713+0747.par: specify a number of parfiles.\n");
      printf(" \t -ndobs xxx: specify number of days between observations.\n");
      printf(" \t -nobsd xxx: specify number of observations per day.\n");
      printf(" \t -ha xxx: specify maximal absolute Hour Angle.\n");
      printf(" \t -randha y: specify whether to use random HA coverage or not (y/n).\n");
      printf(" \t -start xxxxx: specify start MJD.\n");
      printf(" \t -end xxxxx: specify final MJD.\n");
      printf(" \t -rms xxx: specify Gaussian noise rms (in ms).\n");
      printf(" \t -times xxx: read observation times from specified file\n");
      printf(" \t           suppresses questions for red noise characteristics.\n");
      printf(" \t -format parkes : sets the tim-file format to tempo. (Default: tempo2).\n");
      printf(" \t -group 7 21 : simulate observations in groups of 7 days, with 21 days between groups.\n");
      printf(" \t               There will be nobsd observations every ndobs days during these 7 days.\n");
      printf("\n\n \t -idum xxx: specify random number seed (default = set from clock)\n");
      printf("\n The program will prompt you for the parameters not defined in the command line.\n");
      printf("\n\n Have a nice day!\n\n");
      printf("==========================================================================================\n");
      exit(0);
    }
    if(strcmp(argv[i],"-f")==0){
      Npsr=0;
      while(argv[i+Npsr+1][0]!='-'){
	strcpy(parFile[Npsr],argv[i+Npsr+1]);
	printf("Have parfile %d: >>%s<<.\n",Npsr+1,parFile[Npsr]);
	Npsr++;
	if(i+Npsr+1>=argc) break;
      }
      printf("Have %d parameter files.\n",Npsr);
    }
  }
  if (timesFile==1)
    {
      nday = 1;
      ngap = 1;
      hamax = 8;
      setrand = 1;
      imjd = 1;
      fmjd = 1;
    }
  printf("Simulate arrival times for parameter file %s\n",argv[3]);
  printf("----------------------------------------------------\n\n");
  if(ngap<0){    printf("Enter number of days between observations . "); scanf("%f",&ngap);}
  if(nday<0) {    printf("Enter number of observations/day .......... "); scanf("%d",&nday);}
  if(hamax<0 && nday!=1)  {  
      printf("Enter max absolute HA...................... "); scanf("%lf", &hamax);}
  if(setrand!=1){ printf("Random HA coverage? (y/n) ................. "); scanf("%s",&random);}
  if(imjd<0)   {  printf("Enter initial MJD ......................... "); scanf("%Lf",&imjd);}
  if(fmjd<0)   {  printf("Enter final MJD ........................... "); scanf("%Lf",&fmjd);}
  if(giveRMS<0){
    printf("Enter Gaussian noise rms  (ms/auto)........ "); scanf("%s",temp);
    giveRMS = sscanf(temp,"%Lf",&grms);
  }

  printf("GIVE RMS = %d\n",giveRMS);
  if (idum==0)
    {
      printf("Setting random number seed from the clock\n");      
      idum = TKsetSeed();
    }


  iseed = idum;
  psrNum = Npsr;
  if (timesFile==1)
    fin = fopen(timesfname,"r");

  hnobs = nday/2.0; 
  for(ii=0;ii<Npsr;ii++){
      count = 0;
      strcpy(parFile[0],parFile[ii]);
      printf("SET: %d\n",psr[0].param[param_pb].val[0]);
      psr[0].nJumps=0;
      psr[0].fitMode=0;
      psr[0].eclCoord=0;
  psr[0].nits=1;
  psr[0].clockFromOverride[0] = '\0';
  psr[0].nCompanion = 0;
  psr[0].bootStrap = 0;
  psr[0].units = SI_UNITS;
  psr[0].ne_sw  = NE_SW_DEFAULT; 
  psr[0].nWhite = 0;  /* No whitening by default */
  psr[0].timeEphemeris = IF99_TIMEEPH;
  psr[0].dilateFreq = 1;
  psr[0].planetShapiro = 1;
  psr[0].correctTroposphere = 1;
  psr[0].t2cMethod = T2C_IAU2000B;
  psr[0].fixedFormat=0;
  psr[0].nStorePrecision=0;
  strcpy(psr[0].deleteFileName,"NONE");
  strcpy(psr[0].tzrsite,"NULL");
  psr[0].calcShapiro=1;
  psr[0].ipm = 1;
  psr[0].swm = 0;
  psr[0].nPhaseJump=0;

      for (i=0;i<MAX_PARAMS;i++)
	{
	  for (j=0;j<psr[0].param[i].aSize;j++)
	    {
	      psr[0].param[i].fitFlag[j] = 0;
	      psr[0].param[i].paramSet[j] = 0;
	      psr[0].param[i].err[j] = 0;
	      psr[0].param[i].val[j] = 0;
	    }
	}
      //      initialise(psr,0);              /* Initialise the structures */      
      //      printf("SET AFTER 1: %d\n",psr[0].param[param_pb].val[0]);
      readParfile(psr,parFile,timFile,*npsr); /* Load the parameters       */
      //      printf("SET AFTER 2: %d %s\n",psr[0].param[param_pb].val[0],psr[0].name);
      ra = (double)psr[0].param[param_raj].val[0]/2.0/M_PI;
      
      /* Code based on fake1.f to calculate the TOA at transit for each of these observations */
      trmjd = imjd;
      /* 47892.0 = 1990??? */
      almst = fortran_mod((trmjd-47892.0)*solsid+0.276105324+obslong/360.0,(longdouble)1.0);
      
      /* Hour angle at 00h UT */
      ha0 = almst - ra;
      /* Approximate transit time */
      if (ha0 < 0.0) ha0+=1.0;
      trmjd += 1.0-ha0;
      
      if (nday > 1)tstep = hamax/12.0/nday;  /* Was 0.4/nday */
      amjd =  trmjd;
      gapstartmjd = imjd+(hillsize)/solsid;

      do {
	  for (j=0;j<nday;j++)
	      {
		if (timesFile==1)
		  {
		    if (read_f){
		      if (fscanf(fin,"%s %Lf %Lf\n",fake_fname,&freq,&mjd)==3)
			{
			  printf("Read %g %f\n",(double)mjd, (double)freq);
			  endit=0;
			}
		      else
			endit=1;
		    } else{
		      if (fscanf(fin,"%Lf",&mjd)==1)
			{
			  printf("Read %g\n",(double)mjd);
			  endit=0;
			}
		      else
			endit=1;
		    }
		  }
		else
		  {
		    if (random[0]=='y'||random[0]=='Y')
		      amjd=trmjd + (rand()/(longdouble)RAND_MAX - 0.5)*hamax/12.0;
		    else if (nday==1)
		      amjd=trmjd;
		    else
		      amjd=trmjd + ((j+1)-hnobs)*tstep;
		    
		    mjd=amjd;
		  }
		if (endit==0)
		  {
		    if (count==0 && setref==1)
		      {
			psr[0].obsn[count].sat    = psr[0].param[param_tzrmjd].val[0];
			strcpy(psr[0].obsn[count].fname,"reference");
			psr[0].obsn[count].freq   = psr[0].param[param_tzrfrq].val[0];
			if (giveRMS!=1) grms = psr[0].param[param_tres].val[0]/1e3;
			//	    else grms=0.0;
			psr[0].obsn[count].toaErr = grms*1000.0;
			psr[0].obsn[count].origErr = grms*1000.0;
			psr[0].obsn[count].phaseOffset = 0.0;
			strcpy(psr[0].obsn[count].telID, psr[0].tzrsite);
			psr[0].obsn[count].deleted = 0;
			psr[0].obsn[count].clockCorr=1;
			psr[0].obsn[count].delayCorr=1;
			psr[0].obsn[count].efac=1;
			count++;
		      }
		    psr[0].obsn[count].sat    = mjd;
		    strcpy(psr[0].obsn[count].fname,fake_fname);
		    psr[0].obsn[count].freq   = freq;
		    if (giveRMS!=1) grms = psr[0].param[param_tres].val[0]/1e3;
		    //	    else grms=0.0;
		    psr[0].obsn[count].toaErr = grms*1000.0;
		    psr[0].obsn[count].origErr = grms*1000.0;
		    psr[0].obsn[count].phaseOffset = 0.0;
		    strcpy(psr[0].obsn[count].telID, telID);
		    psr[0].obsn[count].deleted = 0;
		    psr[0].obsn[count].clockCorr=1;
		    psr[0].obsn[count].delayCorr=1;
		    psr[0].obsn[count].efac=1;
		    count++;
		    if (count>MAX_OBSN)
		      {
			printf("Number of TOAs > MAX_OBSN.\n");
			count--;
		      }
		  }
	      }
	  if((bunching == 1) && (trmjd >= (gapstartmjd-1))){
	    trmjd += gapsize/solsid;
	    gapstartmjd += (gapsize+hillsize)/solsid;
	  }
	  else{
	    trmjd+=ngap/solsid;
	  }
      }while ((timesFile == 0 && amjd<fmjd) || (timesFile == 1 && endit==0));
      if (timesFile==1)
    fclose(fin);


      psr[0].nobs=count;
      
      if (have_outfile){
	      strcpy(str,outfile);
	      strcpy(timFile[0],outfile);
      }else{
	      strcpy(str,parFile[0]);
	      str[strlen(str)-4]='\0';
	      strcat(str,".simulate");
	      strcpy(timFile[0],str);
      }
      
      /* Now run the tempo2 code */
      preProcess(psr,*npsr,argc,argv);
      callFit(psr,*npsr);             /* Do all the fitting routines */
      for (j=0;j<9;j++)
	{
	  /* Now update the site arrival times depending upon the residuals */
	  
	  for (i=0;i<psr[0].nobs;i++)  
	    {
	      psr[0].obsn[i].sat -= psr[0].obsn[i].prefitResidual/SECDAY; 
	      psr->obsn[i].nFlags = 0;
	    } 
	  writeTim(str,psr,"tempo2");
	  //	  initialise(&psr[ii],0);
	  // Reset the jumps
	  psr[ii].nJumps = 0;
	  for(kk=0;kk<MAX_JUMPS;kk++){
	      psr[ii].jumpVal[kk] = 0.0;
	      psr[ii].jumpValErr[kk] = 0.0;
	  }
	  for(jj=0;jj<MAX_PARAMS;jj++){
	      psr[ii].param[jj].nLinkTo = 0;
	      psr[ii].param[jj].nLinkFrom = 0;
	  }
	  readParfile(psr,parFile,timFile,*npsr); /* Load the parameters       */
	  readTimfile(psr,timFile,*npsr); 
	  preProcess(psr,1,argc,argv);
	  /* Now run the superTEMPO code again */
	  callFit(psr,*npsr);             /* Do all the fitting routines */
	}

      printf("Complete 10 iterations\n");
      for (i=0;i<psr[0].nobs;i++)  
	{
	  psr[0].obsn[i].sat -= psr[0].obsn[i].prefitResidual/SECDAY;  
	  psr->obsn[i].nFlags = 0;
	}
      
      
      for (i=0;i<psr[0].nobs;i++)
	{ 
	  times[i] = (psr[0].obsn[i].sat-psr[0].param[param_posepoch].val[0]);
	}
      npts = psr[0].nobs;
      
      /* Add Gaussian noise */
      if (giveRMS!=1) grms = psr[0].param[param_tres].val[0]/1e3;
      if (grms>0.0)
	{
	  printf("Adding Gaussian noise with rms = %f\n",(float)grms);
	  for (i=0;i<psr[0].nobs;i++)
	    psr[0].obsn[i].sat += TKgaussDev(&idum)*grms/1000.0/SECDAY;
	}
      
      printf("Output TOA file written to %s\n",str);
      writeTim(str,psr,formstr);      
    }
}
コード例 #5
0
ファイル: polyco.C プロジェクト: zhuww/tempo2
void tzFit(pulsar *psr,int npsr,longdouble *tmin,double *doppler,double *rms,double *utc,
	   longdouble tmidMJD,int ncoeff, longdouble *coeff,char *binPhase,int nsets,longdouble afmjd,
	   char* sitename,int tspan,double obsFreq,char *date,longdouble *val,int trueDM,char* polyco_file)
{
  FILE *fout,*fout2;
  int ntoas=32; 
  int nb;
  int j,i,k,iref;
  longdouble fac,sum,rphase,dm;
  longdouble c[31],d[31],ph[31],t[31],phase[800];
  longdouble b,a,rtime,sq,phtst,ct,ct2,phifac,phi0;
  struct tm *timePtr;
  time_t tm;
  char fname1[128];
  char fname2[128];

  strcpy(fname1,polyco_file);
  strcpy(fname2,polyco_file);
  strcat(fname1,"polyco_new.dat");
  strcat(fname2,"newpolyco.dat");
  fout = fopen(fname1,"a");
  fout2 = fopen(fname2,"a");

  /* Note: throughout we are ignoring the first 'TOA' */
  for (i=1;i<psr->nobs;i++)
    phase[i]=psr->obsn[i].phase;

  for (nb = 0;nb < nsets; nb++)
    {
      iref   = (nb+1)*(ntoas-1)-(int)((ntoas-1)/2.0);                            
      rtime  = tmin[iref];
      afmjd = (int)psr->obsn[iref].sat;
      tmidMJD = psr->obsn[iref].sat - (int)psr->obsn[iref].sat;

      tm = (time_t)((double)afmjd-40587.0)*SECDAY; /* Number of seconds since 01-Jan-70 */
      timePtr = gmtime(&tm);
      strftime(date,100,"%d-%b-%y",timePtr);
      if (date[0]=='0') date[0]=' ';
      rphase = psr->obsn[iref].phase;
      //      printf("rphase = %d %d %Lg\n",ntoas,iref,rphase);
      i=iref-(int)((ntoas-1)/2.0)-1; /* -1;*/ /* Maybe another -1 */
      for (j=1;j<ntoas;j++) 
	{      
	  i++;
	  t[j]  = tmin[i]-rtime; 
	  ph[j] = phase[i]-rphase-t[j]*psr->param[param_f].val[0]*60.0L;        
	}
      
      /* This bit of code is based on 'chebft' in numerical recipes for fitting
       * a Chebyshev polynomial
       */
      
      fac = 2.0/(ntoas-1);
      for (j=1;j<ntoas;j++)
	{
	  sum = 0.0;
	  for (k=1;k<ntoas;k++)
	    sum+=ph[k]*cos((M_PI*(j-1))*((k-0.5)/(longdouble)(ntoas-1.0)));
	  c[j-1] = fac*sum;
	}
      
      b = tspan/2.0+5.0;
      a = -b;
      
      chebpc(c,d,ncoeff);
      pcshft(a,b,d,ncoeff);
      for (i=0;i<ncoeff;i++)
	coeff[i] = d[i];
      sq=0.0;
            
      longdouble arg;
      for (j=1;j<ntoas;j++)
	{
	  phtst = d[0];
	  arg = t[j];
	  for (k=1;k<ncoeff;k++)
	    {
	      phtst+=d[k]*arg;
	      arg *= t[j];
	    }
	  sq+=(phtst-ph[j])*(phtst-ph[j]);
	}
      *rms = 1.0e6*sqrt(sq/(ntoas-1))/psr->param[param_f].val[0];
            
      /* Calculate UTC */
      {
	int nutsec,nuthrs,nutmin;
	longdouble uts;
	nutsec = (int)fortran_mod((longdouble)(86400.0*tmidMJD+0.005),86400.0);
	nuthrs = (int)(nutsec/3600.0);
	nutmin = (int)((nutsec-3600*nuthrs)/60.0);
	uts = nutsec-3600.0*nuthrs-60.0*nutmin;
	*utc = 10000.0*nuthrs+100.0*nutmin+uts;
      }
      
      /* Calculate binary phase */
      strcpy(binPhase," ");
      if (psr->param[param_pb].paramSet[0]==1) /* If binary pulsar */
	{
	  ct2 = psr->obsn[1].bat;   
	  ct = ct2 + (psr->obsn[iref].sat-psr->obsn[1].sat);
	  phifac=8.64e4/(psr->param[param_pb].val[0]*86400.0);
	  phi0=fortran_mod((ct-psr->param[param_t0].val[0])*phifac+90000.0,1.0); 
	  sprintf(binPhase,"%7.4Lf%9.4Lf",phi0,phifac);
	}
      *doppler = ((psr->obsn[iref].freqSSB-psr->obsn[iref].freq*1.0e6)/(psr->obsn[iref].freq*1.0e6));
      (*doppler)*=1.0e6/100.0; /* WHY THIS SCALING? */
      *rms     = log10(1.0e-6*(*rms)*psr->param[param_f].val[0]);
	  
      /* Output in original TEMPO format */
      if (psr->name[0]=='J') fprintf(fout,"%-10.10s ",(psr->name)+1);
      else fprintf(fout,"%-10.10s ",psr->name);
      fprintf(fout,"%9.9s",date);
      fprintf(fout,"%11.2f",*utc);
      fprintf(fout,"%20.11f",(double)(afmjd+tmidMJD));
      if (trueDM==0) dm = psr->param[param_dm].val[0];
      else dm = (psr->obsn[iref].tdis1+psr->obsn[iref].tdis2)*DM_CONST*1.0e-12*psr->obsn[iref].freqSSB*psr->obsn[iref].freqSSB;
	
      fprintf(fout,"%21.6f ",(double)dm);
      fprintf(fout,"%6.3f",*doppler);
      fprintf(fout,"%7.3f",*rms);
      fprintf(fout,"\n");
      fprintf(fout,"%20.6Lf",rphase);
      fprintf(fout,"%18.12f",(double)psr->param[param_f].val[0]);
      fprintf(fout,"%5s",sitename);
      fprintf(fout,"%5d",tspan);
      fprintf(fout,"%5d",ncoeff);
      fprintf(fout,"%10.3f",obsFreq);
      fprintf(fout,"%16s",binPhase);
      fprintf(fout,"\n");
      for (i=0;i<ncoeff;i++)
	{
	  fprintf(fout,"%25.17le",(double)coeff[i]);
	  if ((i+1)%3==0) fprintf(fout,"\n");
	}
      if ((i%3)!=0) fprintf(fout,"\n");
      
      fprintf(fout2,"TEMPO2: POLYCO TEMPO1 emulation\n");
      if (psr->name[0]=='J') fprintf(fout2,"%-10.10s\n",(psr->name)+1);
      else fprintf(fout2,"%-10.10s\n",psr->name);
      fprintf(fout2,"%-9.9s\n",date);
      fprintf(fout2,"%-11.2f\n",*utc);
      fprintf(fout2,"%-.20Lf\n",(afmjd+tmidMJD));
      fprintf(fout2,"%-25.10Lf\n",dm);
      fprintf(fout2,"%-10.7f\n",*doppler);
      fprintf(fout2,"%-7.3f\n",*rms);
      fprintf(fout2,"%-.15Lf\n",rphase);
      fprintf(fout2,"%-.20Lf\n",psr->param[param_f].val[0]);
      fprintf(fout2,"%-5s\n",sitename);
      fprintf(fout2,"%-5d\n",tspan);
      fprintf(fout2,"%-5d\n",ncoeff);
      fprintf(fout2,"%-10.3f\n",obsFreq);
      if (strlen(binPhase)<2) fprintf(fout2,"0.000000 0.00000\n");
      else fprintf(fout2,"%-16s\n",binPhase);
      
      for (i=0;i<ncoeff;i++)
	fprintf(fout2,"%-.30Le\n",coeff[i]);
    }
  fclose(fout);
  fclose(fout2);
}
コード例 #6
0
ファイル: polyco.C プロジェクト: zhuww/tempo2
/* Based on atimfake.f from original TEMPO */
void atimfake(pulsar *psr,int npsr,int tspan,int ncoeff,longdouble maxha,char *sitename,longdouble freq,
	      longdouble afmjd,longdouble *tmin,longdouble *tmidMJD,int *retTspan,int *nsets,longdouble *val)
{
  int i,k;
  longdouble rax,rajVal,mjd2;
  longdouble hlst,wait;
  longdouble x[33];
  longdouble solsid = 1.002737909; /* 1 solar day = 1.002737909 sidereal days */
                                   /* Note: slightly different in almanac p50 */
  int    ntoas=31;  
  int    nmjd;
  int    j;
  double alng;
  longdouble mjd1,val0=0.0L,bma,bpa;

  *retTspan = tspan;

  /* Get observatory coordinates - if barycenter do nothing */
  if (strcmp(sitename,"@")==0 || strcasecmp(sitename,"bat")==0) {
    alng = 0;
  }
  else {
    observatory *obs = getObservatory(sitename);
    /* See tzinit.f for if icoord = 0 : NOT IMPLEMENTED */
    alng = atan2(-obs->y, obs->x);
  }

  /* Conversion of UT into local sidereal time (WHERE DOES 0.154374 come from????) */
  /* Note: 0.154374 is the GST at MJD 0 = 3.716667309 h = 0.15486*24               */

  /* Local sidereal time */
  hlst = 24.0*fortran_mod((longdouble)(solsid*afmjd + 0.154374 - alng/(2*M_PI)),1.0);
  rajVal = psr->param[param_raj].val[0]/M_PI*12.0; /* RAJ in hours */

  /* Why +2 in the next expression ? */
  /* Convert to RAJ at CURRENT epoch */
  rax = (int)rajVal + ((int)((rajVal - ((int)rajVal))*60.0)+2)/60.0;

  /* local hour angle */
  wait = (rax - hlst)/solsid; /* solar to sidereal */

  /* Add a day if not in range */
  if (wait < -maxha) wait+= (24.0/solsid);

  mjd1 = afmjd + (wait-maxha)/24.0+tspan/(2.0*1440.0); /* Compute start time */
  nmjd = (int)mjd1;
  mjd1 = fortran_nint(48.0*(mjd1-(int)mjd1))/48.0; /* Round to nearest 1/2 hour, accurate precision not required here */
  *nsets = (int)((long double)(120.0L*maxha+tspan-1)/(long double)tspan);

  mjd2 = mjd1;
  /* MJD2 is rounded to 1.e-10 -- WHY DO THIS? */  
  /* IN TEMPO NOT BEING ROUNDED CORRECTLY (being cutoff instead) */
  {
    int ntmp1,ntmp2;
    
    ntmp1 = (int)(mjd2*1.0e8L);
    ntmp2 = (int)((mjd2*1.0e8L-ntmp1)*100.0L);
    mjd2 = ntmp2*1e-10L+ntmp1*1.0e-8L;
  }
  mjd1 = mjd2;
  /* This is the first part of the chebft numerical recipes routine 
   * for carrying out a Chebyshev fit 
   */
  {
    longdouble a,b;
    b = tspan/2.0+5.0;
    a = -b;
    bma = 0.5*(b-a);
    bpa = 0.5*(b+a);
  }

  psr->nobs = (ntoas*(*nsets))+1;
  /* First store the reference TOA */
  if (psr->param[param_tzrmjd].paramSet[0]!=1)
    {
      printf("ERROR: must set tzrmjd in the parameter file\n");
      exit(1);
    }
  psr->obsn[0].sat = psr->param[param_tzrmjd].val[0];
  psr->obsn[0].freq = psr->param[param_tzrfrq].val[0]; 
  psr->obsn[0].deleted = 0;
  psr->obsn[0].toaErr = 0;
  psr->obsn[0].efac=1.0;
  strcpy(psr->obsn[0].fname,"POLYCO_REF");
  strcpy(psr->obsn[0].telID,psr->tzrsite); 
  psr->obsn[0].nFlags = 0;

  for (k=1;k<=ntoas;k++)
    x[k-1]=cosl(M_PI*(k-0.5L)/ntoas)*bma+bpa;

  i = -1;
  for (j=0;j<*nsets;j++)
    {
      /* Part of the Chebyshev fit routine */
      mjd2 = mjd1 + (j)*tspan/1440.0L;

      for (k=0;k<ntoas;k++)
	{
	  i++;
	  if (i>800)
	    {
	      printf("ERROR: Tspan too small\n");
	      exit(1);
	    }
	  /* ************************************* */
	  val[i] = mjd2 + x[k]/1440.0L;
	  /* Do some more rounding */
	  {
	    int ntmp1,ntmp2;
	    
	    ntmp1 = (int)(val[i]*1.0e8L);
	    ntmp2 = (int)((val[i]*1.0e8L-ntmp1)*100.0L);
	    val[i] = ntmp2*1e-10L+ntmp1*1.0e-8L;
	  }	  	  
	  if (i==0) val0=val[i];
	  		  
	  psr->obsn[i+1].sat     = val[i]+nmjd; 
	  tmin[i+1]              = 1440.0*(val[i]-val0); 
	  psr->obsn[i+1].freq    = freq; 
	  psr->obsn[i+1].toaErr = 0.0;
	  psr->obsn[i+1].deleted = 0;
	  psr->obsn[i+1].efac=1.0;
	  strcpy(psr->obsn[i+1].fname,"POLYCO");
	  
	  /* HARDCODED TO PARKES ... */
	  strcpy(psr->obsn[i+1].telID,sitename);
	  psr->obsn[i+1].nFlags = 0;
	}


    }
  *tmidMJD = mjd2;
}