Exemplo n.º 1
0
int main()
{
  long seed = TKsetSeed();
  double AA,BB;
  double stokes[4];
  double Ex2,Sx2,Sy2,Ey2,Nx2,ExEys,EyExs,Ny2;
  double ax,c,ay,d;
  double ax_r,ax_i,ax2,c2,ay_r,ay_i,ay2,d2;
  double c_r,c_i,d_r,d_i;
  double noise_axex;
  double noise_axsx;
  double noise_cey;
  double noise_csy;
  double noise_nx;
  double noise_ct1;
  double noise_ct2;
  double noise_ct3;
  double noise_ct4;
  double noise_ct5;
  double noise_ct6;
  double noise_ct7;
  double noise_ct8;
  double noise_ct9;
  double noise_ct10;
  double ndof;
  int nbin;
  int i;
  double c_bw = 256.0e6/1024.0;
  double t_sub = 60;
  double rho2;

  nbin = 256;
  Nx2 = 0.1;  // Noise term for LNA 1
  Ny2 = 0.1;  // Noise term for LNA 2
  Sx2 = 0.1;  // Sky noise
  Sy2 = 0.1;  // Sky noise

  ax_r = 0.01;
  ax_i = 0.04;
  c_r  = 0.02;
  c_i  = 0.005;

  ay_r = 0.02;
  ay_i = 0.05;
  d_r  = 0.02;
  d_i = 0.005;

  ax2 = ax_r*ax_r+ax_i*ax_i;
  c2 = c_r*c_r+c_i*c_i;
  ax =sqrt(ax2);
  c = sqrt(c2);

  ay2 = ay_r*ay_r+ay_i*ay_i;
  d2 = d_r*d_r+d_i*d_i;
  ay =sqrt(ay2);
  d = sqrt(d2);

  for (i=0;i<nbin;i++)
    {
      if (i > 50 && i < 200) // Cal ON
	{
	  Ex2 = 1;
	  Ey2 = 1;
	  stokes[0] = 1;
	  stokes[1] = 0;
	  stokes[2] = 1;
	  stokes[3] = 0;
	}
      else // Cal OFF
	{ 
	  Ex2 = 0;
	  Ey2 = 0;
	  stokes[0] = 1;
	  stokes[1] = 0;
	  stokes[2] = 0;
	  stokes[3] = 0;
	}

      // Calculate mean value of AA
      AA = ax2*(Ex2+Sx2) + c2*(Ey2 + Sy2) + Nx2 + stokes[2]*(ax_r*c_r + ax_i*c_i)
	+ stokes[3]*(ax_i*c_r-ax_r*c_i);

      // Calculate auto noise terms
      ndof = c_bw*t_sub/(double)nbin;
      noise_axex = ax2*Ex2/sqrt(ndof);
      noise_axsx = ax2*Sx2/sqrt(ndof);
      noise_cey  = c2*Ey2/sqrt(ndof);
      noise_csy  = c2*Sy2/sqrt(ndof);
      noise_nx   = Nx2/sqrt(ndof);

      AA += TKgaussDev(&seed)*noise_axex;
      AA += TKgaussDev(&seed)*noise_axsx;
      AA += TKgaussDev(&seed)*noise_cey;
      AA += TKgaussDev(&seed)*noise_csy;
      AA += TKgaussDev(&seed)*noise_nx;

      // Calculate cross noise terms
      
      rho2 = pow(stokes[2]/2.0,2)+pow(stokes[3]/2.0,2);
      if (Ex2 > 0 && Ey2 > 0)
	noise_ct1 = 2*ax*c*sqrt(Ex2)*sqrt(Ey2)/sqrt(ndof)*sqrt(0.5*(1+rho2/Ex2/Ey2));
      else noise_ct1 = 0;

      noise_ct2 = 2*ax*c*sqrt(Sx2)*sqrt(Sy2)/sqrt(ndof);
      noise_ct3 = 2*ax*c*sqrt(Ex2)*sqrt(Sy2)/sqrt(ndof);
      noise_ct4 = 2*ax*c*sqrt(Sx2)*sqrt(Ey2)/sqrt(ndof);
      noise_ct5 = 2*c*c*sqrt(Ey2)*sqrt(Sy2)/sqrt(ndof);
      noise_ct6 = 2*ax*ax*sqrt(Ex2)*sqrt(Sx2)/sqrt(ndof);
      noise_ct7 = 2*ax*sqrt(Ex2)*sqrt(Nx2)/sqrt(ndof);
      noise_ct8 = 2*ax*sqrt(Sx2)*sqrt(Nx2)/sqrt(ndof);
      noise_ct9 = 2*c*sqrt(Ey2)*sqrt(Nx2)/sqrt(ndof);
      noise_ct10 = 2*c*sqrt(Sy2)*sqrt(Nx2)/sqrt(ndof);

      AA += TKgaussDev(&seed)*noise_ct1;
      AA += TKgaussDev(&seed)*noise_ct2;
      AA += TKgaussDev(&seed)*noise_ct3;
      AA += TKgaussDev(&seed)*noise_ct4;
      AA += TKgaussDev(&seed)*noise_ct5;
      AA += TKgaussDev(&seed)*noise_ct6;
      AA += TKgaussDev(&seed)*noise_ct7;
      AA += TKgaussDev(&seed)*noise_ct8;


      // NOW CALCULATE BB
      // Calculate mean value of BB
      BB = ay2*(Ey2+Sy2) + d2*(Ex2 + Sx2) + Ny2 + stokes[2]*(ay_r*d_r + ay_i*d_i)
	+ stokes[3]*(ay_i*d_r-ay_r*d_i);

      // Calculate auto noise terms
      noise_axex = ay2*Ey2/sqrt(ndof);  // This naming scheme is misleading - just copied from AA
      noise_axsx = ay2*Sy2/sqrt(ndof);
      noise_cey  = d2*Ex2/sqrt(ndof);
      noise_csy  = d2*Sx2/sqrt(ndof);
      noise_nx   = Ny2/sqrt(ndof);

      BB += TKgaussDev(&seed)*noise_axex;
      BB += TKgaussDev(&seed)*noise_axsx;
      BB += TKgaussDev(&seed)*noise_cey;
      BB += TKgaussDev(&seed)*noise_csy;
      BB += TKgaussDev(&seed)*noise_nx;

      // Calculate cross noise terms
      
      rho2 = pow(stokes[2]/2.0,2)+pow(stokes[3]/2.0,2);
      if (Ey2 > 0 && Ex2 > 0)
	noise_ct1 = 2*ay*d*sqrt(Ey2)*sqrt(Ex2)/sqrt(ndof)*sqrt(0.5*(1+rho2/Ex2/Ey2));
      else noise_ct1 = 0;

      noise_ct2 = 2*ay*d*sqrt(Sy2)*sqrt(Sx2)/sqrt(ndof);
      noise_ct3 = 2*ay*d*sqrt(Ey2)*sqrt(Sx2)/sqrt(ndof);
      noise_ct4 = 2*ay*d*sqrt(Sy2)*sqrt(Ex2)/sqrt(ndof);
      noise_ct5 = 2*d*d*sqrt(Ex2)*sqrt(Sx2)/sqrt(ndof);
      noise_ct6 = 2*ay*ay*sqrt(Ey2)*sqrt(Sy2)/sqrt(ndof);
      noise_ct7 = 2*ay*sqrt(Ey2)*sqrt(Ny2)/sqrt(ndof);
      noise_ct8 = 2*ay*sqrt(Sy2)*sqrt(Ny2)/sqrt(ndof);
      noise_ct9 = 2*d*sqrt(Ex2)*sqrt(Ny2)/sqrt(ndof);
      noise_ct10 = 2*d*sqrt(Sx2)*sqrt(Ny2)/sqrt(ndof);

      BB += TKgaussDev(&seed)*noise_ct1;
      BB += TKgaussDev(&seed)*noise_ct2;
      BB += TKgaussDev(&seed)*noise_ct3;
      BB += TKgaussDev(&seed)*noise_ct4;
      BB += TKgaussDev(&seed)*noise_ct5;
      BB += TKgaussDev(&seed)*noise_ct6;
      BB += TKgaussDev(&seed)*noise_ct7;
      BB += TKgaussDev(&seed)*noise_ct8;

      
      printf("%d %g %g\n",i,AA,BB);
      //  double noise_axex;
      //  double noise_axsx;
      //  double noise_cey;
      //  double noise_csy;
      //  double noise_nx;


    }
}
Exemplo n.º 2
0
void populateRedNoiseModel(rednoisemodel_t* model,long seed){
    int t_npts;
    int i;
    double freq,A,index;
    double t_samp,f_bin,t_span;
    float *data;
    fftwf_plan plan;
    fftwf_complex *spectrum;
    double secperyear=365*86400.0;

    t_samp=(model->end - model->start)/(float)model->npt;

    model->start-=t_samp;
    model->end+=t_samp*2.0;

    if (seed == 0){
        seed=TKsetSeed();
    }else if (seed > 0){
        seed=-seed;
    }

    t_npts=model->npt*model->nreal;

    spectrum = (fftwf_complex*) fftwf_malloc((t_npts/2+1)*sizeof(fftwf_complex));
    data = (float*) fftwf_malloc(t_npts*sizeof(float));

    t_span=(model->end - model->start)/365.25; // years

    t_samp=t_span/(float)model->npt;
    model->tres=t_samp*365.25;
    f_bin=1.0/(t_span*model->nreal); // frequency in yr^-1


    // To convert PSD to FFT power, need to multiply by N^2/T.
    // BUT FFTW does not divide by N^2 on inverse transform
    // so we need to divide by N^2.
    // The factor of 4, converts one-sided P to 2-sided P as
    // we input one-sided, and FFTW expected 2-sided.
    //
    // I am now 99% sure this is correct. George looked at it too!
    // M. Keith 2013.
    A=model->pwr_1yr /(4*t_span*model->nreal);


    // we are forming "amplitudes" not powers
    // so square root.
    index=model->index/2.0;
    A = sqrt(A);

    // form a complex spectrum, then do a c2r transform.
    spectrum[0]=0;
    for (i=1; i < t_npts/2+1; i++){
        freq=(double)i*f_bin;
        double scale=0;
        if(model->mode==MODE_T2CHOL){
            // same definition as the Cholesky code (except index is negative)
            scale=A*pow(1.0+pow(fabs(freq)/model->flatten,2),index/2.0);
        }
        if(model->mode==MODE_SIMPLE){
            if (freq < model->flatten)
                freq=model->flatten;

            scale = A*pow(freq,index);
            if (freq < model->cutoff)scale=0;
        }
        // complex spectrum
        spectrum[i]=(scale*TKgaussDev(&seed) + I*scale*TKgaussDev(&seed));
    }

    plan=fftwf_plan_dft_c2r_1d(t_npts,spectrum,data,FFTW_ESTIMATE);
    fftwf_execute(plan);
    fftwf_destroy_plan(plan);
    fftwf_free(spectrum);

    model->data=data;
}
Exemplo n.º 3
0
extern "C" int graphicalInterface(int argc,char *argv[],pulsar *psr,int *npsr) 
{
	char parFile[MAX_PSR][MAX_FILELEN];
	char timFile[MAX_PSR][MAX_FILELEN];
	int i,nit,j,p;
	char fname[MAX_FILELEN];
	double globalParameter;
	long double result;
	long seed = TKsetSeed();

	//
	// For the output file
	//
	toasim_header_t* header;
	toasim_header_t* read_header;
	FILE* file;
	double offsets[MAX_OBSN]; // Will change to doubles - should use malloc
	// Create a set of corrections.
	toasim_corrections_t* corr = (toasim_corrections_t*)malloc(sizeof(toasim_corrections_t));

	corr->offsets=offsets;
	corr->params=""; // Normally leave as NULL. Can store this along with each realisation. 
	// Same length string in every iteration - defined in r_param_length see below
	corr->a0=0; // constant
	corr->a1=0; // a1*x
	corr->a2=0; // a2*x*X

	*npsr = 0;
	nit = 1;

	printf("Graphical Interface: addGaussian\n");
	printf("Author:              G. Hobbs, M. Keith\n");
	printf("Version:             1.0\n");

	/* Obtain all parameters from the command line */
	for (i=2;i<argc;i++)
	{

		if (strcmp(argv[i],"-nreal")==0){
			nit=atoi(argv[++i]);
		}
		if (strcmp(argv[i],"-f")==0)
		{
			strcpy(parFile[*npsr],argv[++i]); 
			strcpy(timFile[*npsr],argv[++i]);
			(*npsr)++;
		}
		if (strcmp(argv[i],"-seed")==0){
			sscanf(argv[++i],"%d",&seed);
		}
	}

	if (seed > 0)seed=-seed;

	readParfile(psr,parFile,timFile,*npsr); /* Load the parameters       */
	// Now read in all the .tim files
	readTimfile(psr,timFile,*npsr); /* Load the arrival times    */

	preProcess(psr,*npsr,argc,argv);

	for (p=0;p<*npsr;p++)
	{
		printf("NTOA = %d\n",psr[p].nobs);
		header = toasim_init_header();
		strcpy(header->short_desc,"addGaussian");
		strcpy(header->invocation,argv[0]);
		strcpy(header->timfile_name,timFile[p]);
		strcpy(header->parfile_name,"Unknown");
		header->idealised_toas="NotSet"; // What should this be
		header->orig_parfile="NA";
		header->gparam_desc=""; // Global parameters
		header->gparam_vals="";
		header->rparam_desc=""; // Desciprtion of the parameters
		header->rparam_len=0; // Size of the string
		header->seed = seed;

		header->ntoa = psr[p].nobs;
		header->nrealisations = nit;

		// First we write the header...
		sprintf(fname,"%s.addGauss",timFile[p]);
		file = toasim_write_header(header,fname);


		for (i=0;i<nit;i++)
		{
			if(i%10 == 0){
				printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
				printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
				printf("Iteration %d/%d",i+1,nit);
				fflush(stdout);
			}
			for (j=0;j<psr[p].nobs;j++){
					offsets[j] = (double)(psr[p].obsn[j].toaErr*1.0e-6*TKgaussDev(&seed));
			}
			toasim_write_corrections(corr,header,file);
		}
		printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
		printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
		printf("Iteration %d/%d\n",i,nit);

		printf("Close file\n");
		fclose(file);
	}
	return 0;
}
Exemplo n.º 4
0
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);      
    }
}