Esempio n. 1
0
void polyco(pulsar *psr,int npsr,longdouble polyco_MJD1,longdouble polyco_MJD2,int nspan,int ncoeff,
	    longdouble maxha,char *sitename,longdouble freq,longdouble coeff[MAX_COEFF],int trueDM,char* polyco_file)
{
  longdouble tsid = 1.0L/1.002737909L;
  longdouble tmidMJD;
  double doppler,rms;
  int dspan;
  char binPhase[32];
  int    i;
  longdouble afmjd;
  longdouble val[800];
  char date[12];
  longdouble tmin[800];
  double globalParameter,utc;
  int nsets;
  const char *CVS_verNum = "$Revision: 1.8 $";

  /* buffers for three polyco outputs -- any input from the user is
   * prepended to the default file names */
  char fname1[256];
  char fname2[256];
  char fname3[256];
  strcpy(fname1,polyco_file);
  strcpy(fname2,polyco_file);
  strcpy(fname3,polyco_file);
  strcat(fname1,"polyco_new.dat");
  strcat(fname2,"newpolyco.dat");
  strcat(fname3,"polyco.tim");

  /* erase any existing files */
  fclose(fopen(fname1,"w"));
  fclose(fopen(fname2,"w"));

  /* special flag for barycenter polycos */
  bool bary = strcmp(sitename,"@")==0 || strcasecmp(sitename,"bat")==0;


  if (displayCVSversion == 1) CVSdisplayVersion("polyco.C","polyco()",CVS_verNum);

  binPhase[0]='\0';

  /* Set some defaults */
  psr[0].param[param_track].paramSet[0]=0;
  globalParameter=0; 

  // Zap the output files so we can append days to them later
  fclose(fopen("polyco_new.dat","w"));
  fclose(fopen("newpolyco.dat","w"));

  for (afmjd=polyco_MJD1; afmjd <= polyco_MJD2; afmjd+=tsid)
    {
      /* Create some arrival times spread throughout the day */
      atimfake(psr,npsr,nspan,ncoeff,maxha,sitename,freq,afmjd,tmin,&tmidMJD,&dspan,&nsets,val);
      for (i=0;i<psr[0].nobs;i++)
	{
	  psr->obsn[i].clockCorr=(!bary);
	  psr->obsn[i].delayCorr=(!bary);
	  psr->obsn[i].phaseOffset = 0.0;
	}
    /* Use reference phase TZR information unless at bary.*/
    if (strcmp(psr->tzrsite,"@")!=0) {
      psr->obsn[0].clockCorr=1;
      psr->obsn[0].delayCorr=1;
    }
    else {
      psr->obsn[0].clockCorr=0;
      psr->obsn[0].delayCorr=0;
    }
      writeTim(fname3,psr,"tempo2"); 
      formBatsAll(psr,npsr);
      formResiduals(psr,npsr,0);
      tzFit(psr,npsr,tmin,&doppler,&rms,&utc,tmidMJD,ncoeff,coeff,binPhase,nsets,afmjd,sitename,nspan,
	    freq,date,val,trueDM,polyco_file);
    }      
  if (psr->tempo1==0)
    printf("WARNING: Should probably use -tempo1 option\n");
}
Esempio n. 2
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);      
    }
}
Esempio n. 3
0
int bootstrap(pulsar *psr,int p,int npsr)
{
    longdouble param[MAX_PARAMS],err[MAX_PARAMS],psq[MAX_PARAMS],xmean[MAX_PARAMS];
    longdouble result[MAX_PARAMS][MAX_ITER];
    longdouble fac,x1,x2,xmid,sum,sumwt,wgt,x,dt,mean,meansq,sdev;
    int nFit=0,nFit2,npts,okay;
    int i,j,k,ii,nboot,iter,l;
    int il1,il2,ih1,ih2;
    double globalParam;
    long idum = -999;              /* Should be set be clock, or user */
    const char *CVS_verNum = "$Id: 6b34ebfda6648825ae3d3f51fb5041e0763c9ec1 $";

    if (displayCVSversion == 1) CVSdisplayVersion("bootstrap.C","bootstrap()",CVS_verNum);

    printf("Bootstrap1 = %d\n",psr[0].bootStrap);
    copyPSR(psr,p,npsr);           /* Have a copy of the pulsar */
    for (i=0;i<MAX_PARAMS;i++)
        copyParam(psr[0].param[i],&(psr[npsr].param[i]));

    printf("Bootstrap = %d %d\n",psr[0].bootStrap,psr[1].bootStrap);
    nboot = (int)pow(2,psr[p].bootStrap);

    for (i=0;i<psr[p].nobs;i++)
        psr[p].obsn[i].residual = psr[p].obsn[i].prefitResidual;

    /* Store the current post-fit parameters and their errors */
    for (i=0;i<MAX_PARAMS;i++)
    {
        for (k=0;k<psr[p].param[i].aSize;k++)
        {
            if (psr[p].param[i].fitFlag[k] == 1)
            {
                param[nFit] = psr[p].param[i].val[k]; /* - psr[p].param[i].prefit[k]; */
                ld_printf("Initial param = %s %Lf %Lf\n",psr[p].param[i].label[0],
                        psr[p].param[i].val[k], psr[p].param[i].prefit[k]);
                err[nFit]   = psr[p].param[i].err[k];
                psq[nFit]   = 0.0;
                nFit++;
                psr[p].param[i].val[k] = psr[p].param[i].prefit[k];
            }
        }
    }

    /* Determine number of TOAs */
    npts=0;
    okay=0;
    for (i=0;i<psr[p].nobs;i++)
    {
        if (psr[p].obsn[i].deleted==0)
            okay=1;
        /* Check for START and FINISH flags */
        if (psr[p].param[param_start].paramSet[0]==1 && psr[p].param[param_start].fitFlag[0]==1 &&
                psr[p].param[param_start].val[0] > psr[p].obsn[i].bat)
            okay=0;
        if (psr[p].param[param_finish].paramSet[0]==1 && psr[p].param[param_finish].fitFlag[0]==1 &&
                psr[p].param[param_finish].val[0] < psr[p].obsn[i].bat)
            okay=0;

        if (okay==1)
            npts++;
    }


    /* Do the bootstrap monte-carlo */
    fac  = sqrt((double)npts);
    x1   = 0.342*nboot;
    x2   = 0.477*nboot;
    xmid = 0.5*(nboot+1); 
    il1  = (int)((xmid-x1)+0.5);
    il2  = (int)((xmid-x2)+0.5);
    ih1  = (int)((xmid+x1)+0.5);
    ih2  = (int)((xmid+x2)+0.5);

    for (iter=0;iter<nboot;iter++)
    {
        sum   = 0.0;
        sumwt = 0.0;
        for (j=0;j<nFit;j++)
            xmean[j] = 0.0;

        for (i=0;i<npts;i++)
        {
            if (psr[npsr].fitMode==1) 
                wgt = 1.0 /
                    (1.0e-6*psr[npsr].obsn[i].toaErr*psr[npsr].param[param_f].val[0]*
                     1.0e-6*psr[npsr].obsn[i].toaErr*psr[npsr].param[param_f].val[0]);
            else wgt=1.0/(1.0e-6*psr[npsr].param[param_f].val[0]*1.0e-6*psr[npsr].param[param_f].val[0]);

            dt = psr[npsr].obsn[i].residual;

            ii = (int)(npts*random(&idum));  /* Randomise the data index */
            for (j=0;j<nFit;j++)
                xmean[j]+=wgt;  /* *fctn[j]; --- NEEDS TO BE IN -- WHAT IS THIS FOR ANYWAY?? */
            sum+=wgt*dt;
            sumwt+=wgt;	 	  

            /*                                              */
            /* Randomly change around the observation order */
            /*                                              */
            psr[p].obsn[i].prefitResidual  = psr[npsr].obsn[ii].prefitResidual;
            psr[p].obsn[i].residual        = psr[npsr].obsn[ii].residual;
            psr[p].obsn[i].sat             = psr[npsr].obsn[ii].sat;
            psr[p].obsn[i].bat             = psr[npsr].obsn[ii].bat;
            psr[p].obsn[i].deleted         = psr[npsr].obsn[ii].deleted;
            psr[p].obsn[i].freq            = psr[npsr].obsn[ii].freq;
            psr[p].obsn[i].freqSSB         = psr[npsr].obsn[ii].freqSSB; 
            psr[p].obsn[i].toaErr          = psr[npsr].obsn[ii].toaErr;
            strcpy(psr[p].obsn[i].fname,psr[npsr].obsn[ii].fname);
            strcpy(psr[p].obsn[i].telID,psr[npsr].obsn[ii].telID);	  
            for (l=0;l<3;l++)
            {
                psr[p].obsn[i].earth_ssb[l] = psr[npsr].obsn[ii].earth_ssb[l];
                psr[p].obsn[i].observatory_earth[l] = psr[npsr].obsn[ii].observatory_earth[l];
            }
        }
        writeTim("testout.tim",psr,"fred");
        psr[p].bootStrap = 0;
        doFit(&psr[p],1,0);
        /*   textOutput(psr,npsr,globalParam,0,0,0,""); */ /* Output results to the screen */

        j=0;

        for (i=0;i<MAX_PARAMS;i++)
        {
            for (k=0;k<psr[p].param[i].aSize;k++)
            {
                if (psr[p].param[i].fitFlag[k] == 1)
                {
                    /*		  x = fac*((psr[p].param[i].val[k] - psr[p].param[i].prefit[k])-param[j])/err[j]; */
                    /* WHY IS FACTOR USED HERE? */
                    x = ((psr[p].param[i].val[k] - psr[p].param[i].prefit[k])-param[j]);
                    result[j][iter] = psr[p].param[i].val[k]-param[j];
                    psq[j]+=x*x;
                    j++;
                }
            }
        }
        /* Store the current post-fit parameters and their errors */
        for (i=0;i<psr[p].nobs;i++)
            psr[p].obsn[i].residual = psr[npsr].obsn[i].prefitResidual;

        for (i=0;i<MAX_PARAMS;i++)
        {
            for (k=0;k<psr[p].param[i].aSize;k++)
            {
                if (psr[p].param[i].fitFlag[k] == 1)
                {
                    psr[p].param[i].prefit[k]   = psr[npsr].param[i].prefit[k];
                    psr[p].param[i].val[k]      = psr[npsr].param[i].prefit[k]; 
                }
            }
        }
        printf("Finished iteration %d of %d, so %g percent done.\n", (int)(iter+1.5),(int)(nboot+0.5),(double)((double)(iter+1.5)*100/nboot));
    }

    /* Restore the post-fit parameters, but use the Monte-carlo error estimates */
    nFit2=0;
    for (i=0;i<MAX_PARAMS;i++)
    {
        for (k=0;k<psr[p].param[i].aSize;k++)
        {
            if (psr[p].param[i].fitFlag[k] == 1)
            {
                mean=longdouble(0.0);
                meansq=longdouble(0.0);
                for (l=0;l<nboot;l++)
                {
                    mean  += (result[nFit2][l])/nboot;
                    meansq+= (result[nFit2][l]*result[nFit2][l])/nboot;
                    ld_printf("bootstrap parameters [%s] = %.14Lg\n",psr[p].param[i].shortlabel[k],
                            result[nFit2][l]+param[nFit2]);
                }
                /*	      mean/=(longdouble)nboot;
                          meansq/=(longdouble)nboot; */
                sdev = (longdouble)sqrt(meansq-mean*mean);
                ld_printf("Bootstrap mean difference: %Lf mean squared: %.14Lf rms: %.14Lf; sigma: %.14Lf, mean value: %.14Lf\n",
                        mean,(mean*mean),meansq,sdev,(mean+param[nFit2]));	      
                /* psr[p].param[i].val[k] = psr[npsr].param[i].val[k]; */
                psr[p].param[i].val[k] = mean+param[nFit2];
                psr[p].param[i].err[k] = sdev;

                /* psr[p].param[i].err[k] = err[nFit2]*sqrt(psq[nFit2]/nboot); */
                /* psr[p].param[i].err[k] = sqrt(psq[nFit2]/(nboot-1));        */
                /*	      sort(nboot,a[1][j]);
                          fl1[nFit2] = a[il1][j];
                          fl2[nFit2] = a[il2][j];
                          fh1[nFit2] = a[ih1][j];
                          fh2[nFit2] = a[ih2][j]; */
                nFit2++;
            }
        }
    }


    return 0;
}