Example #1
0
/** The main function of Intermittent.c
 *
 */
int main( int argc, char *argv[] )
{

  UserInput_t uvar = empty_UserInput;           /* user input variables */
  INT4 i, k, m;                                   /* counter */
  CHAR newtemp[LONGSTRINGLENGTH];
  FILE *ifp = NULL;
  CHAR *clargs = NULL;                          /* store the command line args */

  /**********************************************************************************/
  /* register and read all user-variables */
  if ( XLALReadUserVars( argc, argv, &uvar ) ) {
    LogPrintf( LOG_CRITICAL, "%s : XLALReadUserVars() failed with error = %d\n", __func__, xlalErrno );
    return 1;
  }
  if ( uvar.verbose ) {
    fprintf( stdout, "%s : read in uservars\n", __func__ );
  }

  /**********************************************************************************/
  /* make temporary directory */
  if ( uvar.tempdir ) {

    /* initialise the random number generator  - use the clock */
    gsl_rng *q;
    if ( XLALInitgslrand( &q, 0 ) ) {
      LogPrintf( LOG_CRITICAL, "%s: XLALinitgslrand() failed with error = %d\n", __func__, xlalErrno );
      XLAL_ERROR( XLAL_EFAULT );
    }

    INT4 id = ( INT4 )( 1e9 * gsl_rng_uniform( q ) );
    sprintf( newtemp, "%s/%09d", uvar.tempdir, id );
    fprintf( stdout, "temp dir = %s\n", newtemp );
    if ( mkdir( newtemp, 0755 ) ) {
      if ( uvar.verbose ) {
        fprintf( stdout, "%s : Unable to make temporary directory %s.  Might be a problem.\n", __func__, newtemp );
      }
      return 1;
    }
  }

  /**********************************************************************************/
  /* read in the cache file and find the correct entry for the binary file */
  FILE *cachefp = NULL;
  if ( ( cachefp = fopen( uvar.cachefile, "r" ) ) == NULL ) {
    LogPrintf( LOG_CRITICAL, "%s : failed to open binary input file %s\n", __func__, uvar.cachefile );
    return 1;
  }
  i = 0;
  INT4 idx = -1;
  CHAR filename[LONGSTRINGLENGTH];
  CHAR dummy[LONGSTRINGLENGTH];
  LIGOTimeGPS fileStart;
  INT4 dummysec = 0;
  INT4 dummynan = 0;
  while ( fscanf( cachefp, "%s %d %d", dummy, &dummysec, &dummynan ) != EOF ) {
    if ( strstr( dummy, uvar.binfile ) ) {
      idx = i;
      strcpy( filename, dummy );
      fileStart.gpsSeconds = dummysec;
      fileStart.gpsNanoSeconds = ( INT4 )1e9 * ( floor( 1e-9 * dummynan / uvar.tsamp + 0.5 ) * uvar.tsamp ); /* round to make sure we get samples at GPS seconds */
    }
    i++;
  }
  if ( idx < 0 ) {
    LogPrintf( LOG_CRITICAL, "%s : failed to find binary input file %s in cache file %s\n", __func__, filename, uvar.cachefile );
    return 1;
  }
  fclose( cachefp );
  if ( uvar.verbose ) {
    fprintf( stdout, "%s : found the requested binary file entry in the cache file.\n", __func__ );
  }

  /***********************************************************************************/
  /* setup the fixed binaryToSFT parameters */
  BinaryToSFTparams par;
  par.tsamp = uvar.tsamp;
  par.highpassf = uvar.highpassf;
  par.amp_inj = 0.0;
  par.f_inj = 0.0;
  par.asini_inj = 0.0;
  XLALGPSSetREAL8( &( par.tasc_inj ), 0 );
  par.tref = fileStart;
  par.P_inj = 0;
  par.phi_inj = 0;
  par.r = NULL;

  /* setup the gridding over coherent times - which we make sure are integers */
  INT4 dummyT = uvar.Tmin;
  INT4 NT = 0;
  while ( dummyT < uvar.Tmax ) {
    dummyT = ( INT4 )( ( REAL8 )dummyT * ( 1.0 + uvar.mismatch ) );
    NT++;
  }
  if ( uvar.verbose ) {
    fprintf( stdout, "%s : Going to do %d different coherent lengths.\n", __func__, NT );
  }

  /**********************************************************************************/
  /* OPEN INTERMEDIATE RESULTS FILE */
  /**********************************************************************************/
  CHAR intname[LONGSTRINGLENGTH];
  if ( XLALOpenIntermittentResultsFile( &ifp, intname, newtemp, clargs, &uvar, &fileStart ) ) {
    LogPrintf( LOG_CRITICAL, "%s : XLALOpenCoherentResultsFile() failed with error = %d\n", __func__, xlalErrno );
    return 1;
  }
  if ( uvar.verbose ) {
    fprintf( stdout, "%s : opened coherent results file %s.\n", __func__, intname );
  }

  /**********************************************************************************/
  /* loop over the different coherent lengths */
  INT4 currentT = uvar.Tmin;
  for ( i = 0; i < NT; i++ ) {

    if ( uvar.verbose ) {
      fprintf( stdout, "%s : working on segment length %d/%d using a segment time of %d sec\n", __func__, i, NT, currentT );
    }

    /* define SFT frequency bounds */
    REAL8 wings = LAL_TWOPI * uvar.maxasini / uvar.minorbperiod;
    REAL8 fmin_read = MINBAND * floor( ( uvar.freq - WINGS_FACTOR * uvar.freq * wings - ( REAL8 )uvar.blocksize / ( REAL8 )uvar.Tmin ) / MINBAND );
    REAL8 fmax_read = MINBAND * ceil( ( uvar.freq + ( REAL8 )uvar.blocksize / ( REAL8 )uvar.Tmin + uvar.freqband + WINGS_FACTOR * ( uvar.freq + uvar.freqband ) * wings ) / MINBAND );
    REAL8 fband_read = fmax_read - fmin_read;
    if ( uvar.verbose ) {
      fprintf( stdout, "%s : reading in SFT frequency band [%f -> %f]\n", __func__, fmin_read, fmax_read );
    }

    /* define the number of different start times */
    INT4 Ns = ceil( 1.0 / uvar.mismatch );
    INT4 Tstep = ( INT4 )floor( currentT / ( REAL8 )Ns );
    if ( Tstep == 0 ) {
      Ns = 1;
    } else {
      Ns = ( INT4 )ceil( ( REAL8 )currentT / ( REAL8 )Tstep );
    }
    if ( uvar.verbose ) {
      fprintf( stdout, "%s : number of start times is %d and time step is %d sec\n", __func__, Ns, Tstep );
    }
    par.tsft = currentT;
    par.freq = fmin_read;
    par.freqband = fband_read;

    /* loop over different start times - make sure they are integer GPS time for simplicity */
    LIGOTimeGPS currentStart;
    currentStart.gpsSeconds = ( INT4 )ceil( ( REAL8 )fileStart.gpsSeconds + 1e-9 * ( REAL8 )fileStart.gpsNanoSeconds );
    currentStart.gpsNanoSeconds = 0;
    for ( k = 0; k < Ns; k++ ) {

      if ( uvar.verbose ) {
        fprintf( stdout, "%s : working on offset start %d/%d using a start time of %d %d sec\n", __func__, k, Ns, currentStart.gpsSeconds, currentStart.gpsNanoSeconds );
      }
      memcpy( &( par.tstart ), &currentStart, sizeof( LIGOTimeGPS ) );

      ParameterSpace pspace = empty_ParameterSpace;   /* the search parameter space */
      COMPLEX8TimeSeriesArray *dstimevec = NULL;      /* contains the downsampled inverse FFT'd SFTs */
      REAL4DemodulatedPowerVector *dmpower = NULL;   /* contains the demodulated power for all SFTs */
      GridParametersVector *freqgridparams = NULL; /* the coherent grid on the frequency derivitive parameter space */
      SFTVector *sftvec = NULL;
      INT8Vector *np = NULL;
      REAL8Vector *R = NULL;

      /**********************************************************************************/
      /* GENERATE SFTS FROM BINARY INPUT FILE */
      /**********************************************************************************/

      /* converts a binary input file into sfts */
      if ( XLALBinaryToSFTVector( &sftvec, filename, &fileStart, &par, &np, &R ) ) {
        LogPrintf( LOG_CRITICAL, "%s : failed to convert binary input file %s to sfts\n", __func__, uvar.binfile );
        return 1;
      }
      XLALDestroyINT8Vector( np );
      XLALDestroyREAL8Vector( R );
      if ( uvar.verbose ) {
        fprintf( stdout, "%s : generated SFTs for file %s\n", __func__, filename );
      }

      /* if we have any SFTs */
      if ( sftvec->length > 0 ) {

        /* define SFT length and the start and span of the observations plus the definitive segment time */
        pspace.tseg = 1.0 / sftvec->data[0].deltaF;
        memcpy( &( pspace.epoch ), &( sftvec->data[0].epoch ), sizeof( LIGOTimeGPS ) );
        pspace.span = XLALGPSDiff( &( sftvec->data[sftvec->length - 1].epoch ), &( sftvec->data[0].epoch ) ) + pspace.tseg;
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : SFT length = %f seconds\n", __func__, pspace.tseg );
          fprintf( stdout, "%s : entire dataset starts at GPS time %d contains %d SFTS and spans %.0f seconds\n", __func__, pspace.epoch.gpsSeconds, sftvec->length, pspace.span );
        }

        /**********************************************************************************/
        /* NORMALISE THE SFTS */
        /**********************************************************************************/

        /* compute the background noise using the sfts - this routine uses the running median at the edges to normalise the wings */
        if ( XLALNormalizeSFTVect( sftvec, uvar.blocksize, 0 ) ) {
          LogPrintf( LOG_CRITICAL, "%s : XLALNormaliseSFTVect() failed with error = %d\n", __func__, xlalErrno );
          return 1;
        }
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : normalised the SFTs\n", __func__ );
        }

        /**********************************************************************************/
        /* DEFINE THE BINARY PARAMETER SPACE */
        /**********************************************************************************/

        /* define the binary parameter space */
        if ( XLALDefineBinaryParameterSpace( &( pspace.space ), pspace.epoch, pspace.span, &uvar ) ) {
          LogPrintf( LOG_CRITICAL, "%s : XLALDefineBinaryParameterSpace() failed with error = %d\n", __func__, xlalErrno );
          return 1;
        }
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : defined binary parameter prior space\n", __func__ );
        }

        /**********************************************************************************/
        /* COMPUTE THE COARSE GRID ON FREQUENCY DERIVITIVES */
        /**********************************************************************************/

        /* compute the grid parameters for all SFTs */
        INT4 ndim = -1;
        if ( XLALComputeFreqGridParamsVector( &freqgridparams, pspace.space, sftvec, uvar.mismatch, &ndim, BINS_FACTOR ) ) {
          LogPrintf( LOG_CRITICAL, "%s : XLALComputeFreqGridParams() failed with error = %d\n", __func__, xlalErrno );
          return 1;
        }
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : computed the grid parameters for the sfts\n", __func__ );
        }

        /**********************************************************************************/
        /* CONVERT ALL SFTS TO DOWNSAMPLED TIMESERIES */
        /**********************************************************************************/

        if ( XLALSFTVectorToCOMPLEX8TimeSeriesArray( &dstimevec, sftvec ) ) {
          LogPrintf( LOG_CRITICAL, "%s : XLALSFTVectorToCOMPLEX8TimeSeriesArray() failed with error = %d\n", __func__, xlalErrno );
          return 1;
        }
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : converted SFTs to downsampled timeseries\n", __func__ );
        }

        /**********************************************************************************/
        /* COMPUTE THE STATISTICS ON THE COARSE GRID */
        /**********************************************************************************/

        /* compute the demodulated power on the frequency derivitive grid */
        if ( XLALCOMPLEX8TimeSeriesArrayToDemodPowerVector( &dmpower, dstimevec, freqgridparams, ifp ) ) {
          LogPrintf( LOG_CRITICAL, "%s : XLALCOMPLEX8TimeSeriesArrayToDemodPowerVector() failed with error = %d\n", __func__, xlalErrno );
          return 1;
        }
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : computed the demodulated power\n", __func__ );
        }

        /**********************************************************************************/
        /* FREE MEMORY */
        /**********************************************************************************/

        /* free memory inside the loop */
        XLALFreeParameterSpace( &pspace );
        XLALFreeREAL4DemodulatedPowerVector( dmpower );
        for ( m = 0; m < ( INT4 )dstimevec->length; m++ ) {
          XLALDestroyCOMPLEX8TimeSeries( dstimevec->data[m] );
        }
        XLALFree( dstimevec->data );
        XLALFree( dstimevec );
        for ( m = 0; m < ( INT4 )freqgridparams->length; m++ ) {
          XLALFree( freqgridparams->segment[m]->grid );
          XLALFree( freqgridparams->segment[m]->prod );
          XLALFree( freqgridparams->segment[m] );
        }
        XLALFree( freqgridparams->segment );
        XLALFree( freqgridparams );
        if ( uvar.verbose ) {
          fprintf( stdout, "%s : freed memory\n", __func__ );
        }
        XLALDestroySFTVector( sftvec );

      } /* end if statement on whether we have SFTs */

      /* update the start time of the segment */
      XLALGPSAdd( &currentStart, ( REAL8 )Tstep );

    } /* end loop over start times */

    /* update segment length */
    currentT = ( INT4 )( ( REAL8 )currentT * ( 1.0 + uvar.mismatch ) );

  }  /* end loop over segment lengths */

  /* move the temporary directory to the final location */
  if ( uvar.tempdir ) {
    CHAR newoutputfile[LONGSTRINGLENGTH];
    snprintf( newoutputfile, LONGSTRINGLENGTH, "%s/IntermittentResults-%s-%d.txt", uvar.outputdir, uvar.outLabel, fileStart.gpsSeconds );
    if ( rename( intname, newoutputfile ) ) {
      LogPrintf( LOG_CRITICAL, "%s : unable to move final results file %s -> %s.  Exiting.\n", __func__, intname, newoutputfile );
      return 1;
    }
  }

  /**********************************************************************************/
  /* FREE MEMORY */
  /**********************************************************************************/
  fclose( ifp );

  LALCheckMemoryLeaks();

  return 0;

}
/** The main function of semicoherentbinary.c
 *
 */
int main( int argc, char *argv[] )  {

  UserInput_t uvar = empty_UserInput;           /* user input variables */
  CHAR *clargs = NULL;                          /* store the command line args */
  SFTVector *sftvec = NULL;                     /* stores the input SFTs */
  /* REAL4VectorArray *background = NULL;                  /\* running median estimates of the background for each SFT *\/ */
  ParameterSpace pspace = empty_ParameterSpace;    /* the search parameter space */
  COMPLEX8TimeSeriesArray *dstimevec = NULL;       /* contains the downsampled inverse FFT'd SFTs */
  REAL4DemodulatedPowerVector *dmpower = NULL;    /* contains the demodulated power for all SFTs */
  GridParametersVector *freqgridparams = NULL;  /* the coherent grid on the frequency derivitive parameter space */
  GridParameters *bingridparams = NULL;
  CHAR newnewtemp[LONGSTRINGLENGTH];
 /*  REAL8Vector *SemiCo = NULL;                   /\* the semi-coherent statistic results *\/    */
  REAL8 fmin_read,fmax_read,fband_read;         /* the range of frequencies to be read from SFTs */
  UINT4 i;                                      /* counters */
  FILE *sfp = NULL;
  /* FILE *cfp = NULL; */

  vrbflg = 0;                           /* verbose error-messages */

  /* turn off default GSL error handler */
  gsl_set_error_handler_off();

  /* register and read all user-variables */
  if (XLALReadUserVars(argc,argv,&uvar,&clargs)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALReadUserVars() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : read in uservars\n",__func__);

  /* initialise sin-cosine lookup table */
  XLALSinCosLUTInit();

  /* initialise the random number generator */
  if (XLALInitgslrand(&r,uvar.seed)) {
    LogPrintf(LOG_CRITICAL,"%s: XLALinitgslrand() failed with error = %d\n",__func__,xlalErrno);
    XLAL_ERROR(XLAL_EFAULT);
  }

  /* make output directory */
  {
    struct stat st;
    if (stat(uvar.outputdir, &st)) {
      if (mkdir(uvar.outputdir,0755) != 0 && errno != EEXIST) {
        LogPrintf(LOG_DEBUG,"%s : Unable to make output directory %s.  Might be a problem.\n",__func__,uvar.outputdir);
      }
    }
  }

  /* make temporary directory */
  if (uvar.tempdir) {

    struct stat st;
    if (stat(uvar.tempdir, &st)) {
      if (mkdir(uvar.tempdir,0755) != 0 && errno != EEXIST) {
        LogPrintf(LOG_DEBUG,"%s : Unable to make temporary directory %s.  Might be a problem.\n",__func__,uvar.tempdir);
      }
    }

    /* initialise the random number generator  - use the clock */
    gsl_rng * q;
    if (XLALInitgslrand(&q,0)) {
      LogPrintf(LOG_CRITICAL,"%s: XLALinitgslrand() failed with error = %d\n",__func__,xlalErrno);
      XLAL_ERROR(XLAL_EFAULT);
    }

    CHAR newtemp[LONGSTRINGLENGTH];
    INT4 id = (INT4)(1e9*gsl_rng_uniform(q));
    sprintf(newtemp,"%s/%09d",uvar.tempdir,id);
    if (mkdir(newtemp,0755) != 0 && errno != EEXIST) {
      LogPrintf(LOG_DEBUG,"%s : Unable to make temporary directory %s.  Might be a problem.\n",__func__,newtemp);
    }
    sprintf(newnewtemp,"%s/%.3f-%.3f",newtemp,uvar.freq,uvar.freq+uvar.freqband);

  } else {
    sprintf(newnewtemp,"%s/%.3f-%.3f",uvar.outputdir,uvar.freq,uvar.freq+uvar.freqband);
  }

  /* make frequency+band directory inside output/temporary directory */
  if (mkdir(newnewtemp,0755) != 0 && errno != EEXIST) {
    LogPrintf(LOG_CRITICAL,"%s : Unable to make frequency+band directory %s\n",__func__,newnewtemp);
    return 1;
  }

  /* initialise the random number generator */
  if (XLALInitgslrand(&r,uvar.seed)) {
    LogPrintf(LOG_CRITICAL,"%s: XLALinitgslrand() failed with error = %d\n",__func__,xlalErrno);
    XLAL_ERROR(XLAL_EFAULT);
  }

  /* make crude but safe estimate of the bandwidth required for the source - now includes running median wings */
  {
    REAL8 wings = LAL_TWOPI*uvar.maxasini/uvar.minorbperiod;
    fmin_read = MINBAND*floor((uvar.freq - WINGS_FACTOR*uvar.freq*wings - (REAL8)uvar.blocksize/(REAL8)uvar.tsft)/MINBAND);
    fmax_read = MINBAND*ceil((uvar.freq + (REAL8)uvar.blocksize/(REAL8)uvar.tsft + uvar.freqband + WINGS_FACTOR*(uvar.freq + uvar.freqband)*wings)/MINBAND);
    fband_read = fmax_read - fmin_read;
    LogPrintf(LOG_NORMAL,"%s : reading in SFT frequency band [%f -> %f]\n",__func__,fmin_read,fmax_read);
  }

  /* initialise the random number generator */
  if (XLALInitgslrand(&r,uvar.seed)) {
    LogPrintf(LOG_CRITICAL,"%s: XLALinitgslrand() failed with error = %d\n",__func__,xlalErrno);
    XLAL_ERROR(XLAL_EFAULT);
  }

  /**********************************************************************************/
  /* READ THE SFT DATA */
  /**********************************************************************************/

  /* load in the SFTs - also fill in the segment parameters structure */
  if (XLALReadSFTs(&sftvec,uvar.sftbasename,fmin_read,fband_read,uvar.gpsstart,uvar.gpsend,uvar.tsft,uvar.bins_factor)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALReadSFTs() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : read in SFTs\n",__func__);

  /* define SFT length and the start and span of the observations plus the definitive segment time */
  pspace.tseg = 1.0/sftvec->data[0].deltaF;
  memcpy(&(pspace.epoch),&(sftvec->data[0].epoch),sizeof(LIGOTimeGPS));
  pspace.span = XLALGPSDiff(&(sftvec->data[sftvec->length-1].epoch),&(sftvec->data[0].epoch)) + pspace.tseg;
  LogPrintf(LOG_NORMAL,"%s : SFT length = %f seconds\n",__func__,pspace.tseg);
  LogPrintf(LOG_NORMAL,"%s : entire dataset starts at GPS time %d contains %d SFTS and spans %.0f seconds\n",__func__,pspace.epoch.gpsSeconds,sftvec->length,pspace.span);

  /**********************************************************************************/
  /* NORMALISE THE SFTS */
  /**********************************************************************************/

  if (uvar.blocksize>0) {
    /* compute the background noise using the sfts - this routine uses the running median at the edges to normalise the wings */
    if (XLALNormalizeSFTVect(sftvec,uvar.blocksize,0)) {
      LogPrintf(LOG_CRITICAL,"%s : XLALNormaliseSFTVect() failed with error = %d\n",__func__,xlalErrno);
      return 1;
    }
  }
  else {
    REAL8Vector *means = XLALCreateREAL8Vector(sftvec->length);
    if (uvar.blocksize==0) {
      /* compute the background noise using the sfts - this routine simply divides by the median */
      if (XLALNormalizeSFTVectMedian(sftvec,means,1)) {
        LogPrintf(LOG_CRITICAL,"%s : XLALNormaliseSFTVectMedian() failed with error = %d\n",__func__,xlalErrno);
        return 1;
      }
    }
    else {
      /* compute the background noise using the sfts - this routine simply divides by the mean */
      if (XLALNormalizeSFTVectMean(sftvec,means,1)) {
        LogPrintf(LOG_CRITICAL,"%s : XLALNormaliseSFTVectMean() failed with error = %d\n",__func__,xlalErrno);
        return 1;
      }
    }
    XLALDestroyREAL8Vector(means);
  }
  LogPrintf(LOG_NORMAL,"%s : normalised the SFTs\n",__func__);

  /* for (i=0;i<sftvec->length;i++) {
    for (j=0;j<sftvec->data[i].data->length;j++) {
      if (isnan(crealf(sftvec->data[i].data->data[j]))||isinf(crealf(sftvec->data[i].data->data[j]))||isnan(cimagf(sftvec->data[i].data->data[j]))||isinf(cimagf(sftvec->data[i].data->data[j]))) {
        LogPrintf(LOG_DEBUG,"SFT %d : %f %e %e\n",i,sftvec->data[i].f0 + j*sftvec->data[i].deltaF,crealf(sftvec->data[i].data->data[j]),cimagf(sftvec->data[i].data->data[j]));
      }
    }
  } */

  /**********************************************************************************/
  /* DEFINE THE BINARY PARAMETER SPACE */
  /**********************************************************************************/

  /* define the binary parameter space */
  if (XLALDefineBinaryParameterSpace(&(pspace.space),pspace.epoch,pspace.span,&uvar)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALDefineBinaryParameterSpace() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : defined binary parameter prior space\n",__func__);

  /**********************************************************************************/
  /* COMPUTE THE COARSE GRID ON FREQUENCY DERIVITIVES */
  /**********************************************************************************/

  /* compute the grid parameters for all SFTs */
  if (XLALComputeFreqGridParamsVector(&freqgridparams,pspace.space,sftvec,uvar.mismatch,&uvar.ndim,uvar.bins_factor)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALComputeFreqGridParams() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }

  /**********************************************************************************/
  /* COMPUTE THE FINE GRID PARAMETERS */
  /**********************************************************************************/

  /* compute the fine grid on the binary parameters */
  if (XLALComputeBinaryGridParams(&bingridparams,pspace.space,pspace.span,pspace.tseg,uvar.mismatch,uvar.coverage)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALComputeBinaryGridParams() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : computed the binary parameter space grid\n",__func__);

  /**********************************************************************************/
  /* CONVERT ALL SFTS TO DOWNSAMPLED TIMESERIES */
  /**********************************************************************************/

  /* convert sfts to downsample dtimeseries */
  if (XLALSFTVectorToCOMPLEX8TimeSeriesArray(&dstimevec,sftvec)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALSFTVectorToCOMPLEX8TimeSeriesArray() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : converted SFTs to downsampled timeseries\n",__func__);

  /**********************************************************************************/
  /* OPEN INTERMEDIATE RESULTS FILE */
  /**********************************************************************************/

  /* if (XLALOpenSemiCoherentResultsFile(&cfp,newnewtemp,&pspace,clargs,&uvar,1)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALOpenCoherentResultsFile() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : opened coherent results file.\n",__func__); */

   /**********************************************************************************/
  /* COMPUTE THE STATISTICS ON THE COARSE GRID */
  /**********************************************************************************/

  /* compute the demodulated power on the frequency derivitive grid */
  if (XLALCOMPLEX8TimeSeriesArrayToDemodPowerVector(&dmpower,dstimevec,freqgridparams,NULL)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALCOMPLEX8TimeSeriesArrayToDemodPowerVector() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  /* fclose(cfp); */
  LogPrintf(LOG_NORMAL,"%s : computed the demodulated power\n",__func__);

  /**********************************************************************************/
  /* OPEN RESULTS FILE */
  /**********************************************************************************/

  if (XLALOpenSemiCoherentResultsFile(&sfp,newnewtemp,&pspace,clargs,&uvar)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALOutputBayesResults() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_NORMAL,"%s : output results to file.\n",__func__);

   /**********************************************************************************/
  /* COMPUTE THE STATISTICS ON THE FINE GRID */
  /**********************************************************************************/

  /* compute the semi-coherent detection statistic on the fine grid */
  if (XLALComputeSemiCoherentStat(sfp,dmpower,&pspace,freqgridparams,bingridparams,uvar.ntoplist,uvar.with_xbins)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALComputeSemiCoherentStat() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  fclose(sfp);
  LogPrintf(LOG_NORMAL,"%s : computed the semi-coherent statistic\n",__func__);

  /**********************************************************************************/
  /* CLEAN UP */
  /**********************************************************************************/

  /* move the temporary directory to the final location */
  if (uvar.tempdir) {
    CHAR newoutputdir[LONGSTRINGLENGTH];
    sprintf(newoutputdir,"%s/%.3f-%.3f",uvar.outputdir,uvar.freq,uvar.freq+uvar.freqband);
    if (rename(newnewtemp,newoutputdir)) {
      LogPrintf(LOG_CRITICAL,"%s : unable to move final results directory %s -> %s.  Exiting.\n",__func__,newnewtemp,newoutputdir);
      return 1;
    }
  }
  LogPrintf(LOG_DEBUG,"%s : moved the results from the temp space.\n",__func__);

  /* clean up the parameter space */
  if (XLALFreeParameterSpace(&pspace)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALFreeParameterSpace() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_DEBUG,"%s : freed the parameter space\n",__func__);

  /* clean up the demodulated power */
  if (XLALFreeREAL4DemodulatedPowerVector(dmpower)) {
    LogPrintf(LOG_CRITICAL,"%s : XLALFreeREAL4DemodulatedPowerVector() failed with error = %d\n",__func__,xlalErrno);
    return 1;
  }
  LogPrintf(LOG_DEBUG,"%s : freed the demodulated power\n",__func__);

  /* free un-needed downsampled timeseries */
  for (i=0;i<dstimevec->length;i++) {
    XLALDestroyCOMPLEX8TimeSeries(dstimevec->data[i]);
  }
  XLALFree(dstimevec->data);
  XLALFree(dstimevec);
  LogPrintf(LOG_DEBUG,"%s : freed the downsampled timeseries memory\n",__func__);

  /* free frequency grid - the contents of each segment have been moved to the power structure and are freed later */
  XLALFree(freqgridparams->segment);
  XLALFree(freqgridparams);

  /* free un-needed original SFT vector */
  XLALDestroySFTVector(sftvec);
  LogPrintf(LOG_DEBUG,"%s : Freed the SFT memory\n",__func__);

  /* free semi-coherent results */
 /*  XLALDestroyREAL8Vector(SemiCo); */
/*   LogPrintf(LOG_DEBUG,"%s : Freed the semi-coherent results memory\n",__func__); */

  /* Free config-Variables and userInput stuff */
  XLALDestroyUserVars();
  XLALFree(clargs);

  /* did we forget anything ? */
  LALCheckMemoryLeaks();
  LogPrintf(LOG_DEBUG,"%s : successfully checked memory leaks.\n",__func__);

  LogPrintf(LOG_NORMAL,"%s : successfully completed.\n",__func__);
  return 0;

} /* end of main */