Example #1
0
int read_WAPP_lags(FILE * infiles[], int numfiles, int numwapps,
                   unsigned char *data, struct wappinfo *w)
// This routine reads a set of lags from the input files *infiles
// which contain 16 or 32 bit lag data from the WAPP correlator at
// Arecibo.  A set of WAPP lags is bytes_per_sample * numchans * #bits
// * numwapps long.  *data must be at least that long
{
    int ii;
    static int currentfile = 0, numread = 0;

    // Make sure our current file number is valid
    if (currentfile >= numfiles / numwapps)
        return 0;

    // First, attempt to read data from the current file
    if (chkfread(data, w->bytes_per_sample, 1, infiles[currentfile * numwapps])) {      // Got data
        // Get data from other WAPPs
        for (ii = 1; ii < numwapps; ii++)
            chkfread(data + ii * w->bytes_per_sample, w->bytes_per_sample, 1,
                     infiles[currentfile * numwapps + ii]);
        numread++;
        return 1;
    } else {                    // Didn't get data
        if (feof(infiles[currentfile * numwapps])) {    // End of file?
            currentfile++;
            return read_WAPP_lags(infiles, numfiles, numwapps, data, w);
        } else {
            printf("\nProblem reading record from WAPP data file:\n");
            printf("   currentfile = %d.  Exiting.\n", currentfile);
            exit(1);
        }
    }
}
Example #2
0
int read_database(void)
/* Reads the full pulsar database into the static array psrdata */
{
   FILE *database;
   char databasenm[200];
   psrdata pdata;
   binpsrdata bpdata;

   /* Open the binary data file */
   sprintf(databasenm, "%s/lib/pulsars.cat", getenv("PRESTO"));
   database = chkfopen(databasenm, "rb");

   while (chkfread(&pdata, sizeof(psrdata), 1, database)) {
      if (np >= NP) {
          printf("NP value set to small (%d) in $PRESTO/include/database.h\n", NP);
          printf("Please increase it and recompile\n");
          exit(-1);
      }
      strncpy(pulsardata[np].jname, pdata.jname, 13);
      strncpy(pulsardata[np].bname, pdata.bname, 9);
      strncpy(pulsardata[np].alias, pdata.alias, 10);
      pulsardata[np].ra2000 = pdata.ra2000;
      pulsardata[np].dec2000 = pdata.dec2000;
      pulsardata[np].dm = pdata.dm;
      pulsardata[np].timepoch = pdata.timepoch;
      pulsardata[np].p = pdata.p;
      pulsardata[np].pd = pdata.pd;
      pulsardata[np].pdd = 0.0;
      pulsardata[np].f = 1.0 / pdata.p;
      pulsardata[np].fd = -pdata.pd / (pdata.p * pdata.p);
      pulsardata[np].fdd = 0.0;
      if (pdata.binary == 1.0) {
         chkfread(&bpdata, sizeof(binpsrdata), 1, database);
         pulsardata[np].orb.p = bpdata.pb;
         pulsardata[np].orb.e = bpdata.e;
         pulsardata[np].orb.x = bpdata.x;
         pulsardata[np].orb.w = bpdata.w;
         pulsardata[np].orb.t = bpdata.To;
      } else {
         pulsardata[np].orb.p = 0.0;
         pulsardata[np].orb.e = 0.0;
         pulsardata[np].orb.x = 0.0;
         pulsardata[np].orb.w = 0.0;
         pulsardata[np].orb.t = 0.0;
      }
      pulsardata[np].orb.pd = 0.0;
      pulsardata[np].orb.wd = 0.0;
      np++;
   };

   /* Close the database */
   fclose(database);

   have_database = 1;
   return np;
}
Example #3
0
void read_dftvector(dftvector * data, char *filename)
/* Read a dftvector data structure from a binary file */
{
   FILE *infile;
   int ii;
   double dtmp;

   infile = chkfopen(filename, "rb");
   chkfread(&dtmp, sizeof(double), 1, infile);
   data->n = (int) dtmp;
   chkfread(&dtmp, sizeof(double), 1, infile);
   data->numvect = (int) dtmp;
   chkfread(&data->dt, sizeof(double), 1, infile);
   chkfread(&data->r, sizeof(double), 1, infile);
   chkfread(&data->norm, sizeof(double), 1, infile);
   chkfread(&data->T, sizeof(double), 1, infile);
   data->vector = gen_cvect(data->n);
   for (ii = 0; ii < data->numvect; ii++) {
      chkfread(&dtmp, sizeof(double), 1, infile);
      data->vector[ii].r = (float) dtmp;
      chkfread(&dtmp, sizeof(double), 1, infile);
      data->vector[ii].i = (float) dtmp;
   }
   fclose(infile);
}
Example #4
0
void read_offsets(float **loptr, float **optr, int numpts, int numchan)
{
    static int firsttime = 1, useoffsets = 0;
    static float *offsets1, *offsets2, *tempzz;
    static FILE *offsetfile=NULL; //, *bandpassfile;
    int ii;
    
    if (firsttime) {
        char *envval = getenv("OFFSETFILE");
        if (envval != NULL) {
            offsetfile = chkfopen(envval, "r");
            if (offsetfile) {
                printf("\nUsing offsets from the file '%s'.\n", envval);
                useoffsets = 1;
            }
        }
        /* Read the bandpass file */
        //*bandpass = gen_fvect(numchan);
        //printf("Reading 'bandpass.dat'\n");
        //bandpassfile = chkfopen("bandpass.dat", "r");
        //chkfread(*bandpass, sizeof(float), numchan, bandpassfile);
        //fclose(bandpassfile);
        offsets1 = gen_fvect(numpts);
        offsets2 = gen_fvect(numpts);
        *loptr = offsets2;  // Start "backwards" since we'll switch them below
        *optr  = offsets1;
        if (useoffsets) {  // Read the data into offsets1
            chkfread(*optr, sizeof(float), numpts, offsetfile);
            for (ii = 0 ; ii < numpts ; ii++)
                offsets1[ii] *= 1.0/(float)numchan;
        } else { // Just fill the arrays with zeros 
            for (ii = 0 ; ii < numpts ; ii++) {
                offsets1[ii] = 0.0;
                offsets2[ii] = 0.0;
            }
        }
        firsttime = 0;
        return;
    }
    if (useoffsets) {
        SWAP(*loptr, *optr);  // Swap the buffer pointers
        // Read the offsets and scale by the number of channels
        chkfread(*optr, sizeof(float), numpts, offsetfile);
        for (ii = 0 ; ii < numpts ; ii++)
            (*optr)[ii] *= 1.0/(float)numchan;
    }
}
Example #5
0
int read_floats(FILE * file, float *data, int numpts, int numchan)
/* This routine reads a numpts records of numchan each from */
/* the input file *file which contains normal floating      */
/* point data.                                              */
/* It returns the number of points read.                    */
{
   return chkfread(data, sizeof(float),
                   (unsigned long) (numpts * numchan), file) / numchan;
}
Example #6
0
static int read_floats(FILE * file, float *data, int numpts, int numchan)
/* This routine reads a numpts records of numchan each from */
/* the input file *file which contains normal floating      */
/* point data.                                              */
/* It returns the number of points read.                    */
{
    /* Read the raw data and return numbar read */

    return chkfread(data, sizeof(float), (numpts * numchan), file) / numchan;
}
Example #7
0
void get_rzw_cand(char *filenm, int candnum, fourierprops * cand)
/*  Read the rzw candidate file 'filenm' and return a        */
/*  pointer to the fourierprops that describes it.           */
{
   FILE *candfile;

   candfile = chkfopen(filenm, "rb");
   chkfileseek(candfile, candnum - 1, sizeof(fourierprops), SEEK_SET);
   chkfread(cand, sizeof(fourierprops), 1, candfile);
   fclose(candfile);
}
Example #8
0
void get_rawbin_cand(char *filenm, int candnum, rawbincand * cand)
/*  Read the rawbin candidate file 'filenm' and return a     */
/*  pointer to the rawbincand that describe it.              */
{
   FILE *candfile;

   candfile = chkfopen(filenm, "rb");
   chkfileseek(candfile, candnum - 1, sizeof(rawbincand), SEEK_SET);
   chkfread(cand, sizeof(rawbincand), 1, candfile);
   fclose(candfile);
}
Example #9
0
static int simple_read_subbands(FILE * infiles[], int numfiles, short *subbanddata)
/* Read short int subband data written by prepsubband */
{
   int ii, numread = 0;

   /* Read the data */
   for (ii = 0; ii < numfiles; ii++) {
       numread = chkfread(subbanddata + ii * SUBSBLOCKLEN, 
                          sizeof(short), SUBSBLOCKLEN, infiles[ii]);
   }
   return numread;
}
Example #10
0
int determine_padvals(char *maskfilenm, mask * obsmask, float *padvals[])
/* Determine reasonable padding values from the rfifind produced  */
/* *.stats file if it is available.  Return the allocated vector  */
/* (of length numchan) in padvals.  Return a '1' if the routine   */
/* used the stats file, return 0 if the padding was set to aeros. */
{
   FILE *statsfile;
   int ii, numchan, numint, ptsperint, lobin, numbetween;
   float **dataavg, tmp1, tmp2;
   char *statsfilenm, *root, *suffix;

   if (split_root_suffix(maskfilenm, &root, &suffix) == 0) {
      printf("\nThe mask filename (%s) must have a suffix!\n\n", maskfilenm);
      exit(1);
   } else {
      /* Determine the stats file name */
      statsfilenm = calloc(strlen(maskfilenm) + 2, sizeof(char));
      sprintf(statsfilenm, "%s.stats", root);
      free(root);
      free(suffix);
      *padvals = gen_fvect(obsmask->numchan);
      /* Check to see if the file exists */
      printf("Attempting to read the data statistics from '%s'...\n", statsfilenm);
      statsfile = chkfopen(statsfilenm, "rb");
      free(statsfilenm);
      if (statsfile) {          /* Read the stats */
         chkfread(&numchan, sizeof(int), 1, statsfile);
         chkfread(&numint, sizeof(int), 1, statsfile);
         chkfread(&ptsperint, sizeof(int), 1, statsfile);
         chkfread(&lobin, sizeof(int), 1, statsfile);
         chkfread(&numbetween, sizeof(int), 1, statsfile);
         dataavg = gen_fmatrix(numint, numchan);
         /* These are the powers */
         chkfread(dataavg[0], sizeof(float), numchan * numint, statsfile);
         /* These are the averages */
         chkfread(dataavg[0], sizeof(float), numchan * numint, statsfile);
         /* Set the padding values equal to the mid-80% channel averages */
         for (ii = 0; ii < numchan; ii++)
            calc_avgmedstd(dataavg[0] + ii, numint, 0.8, numchan,
                           *padvals + ii, &tmp1, &tmp2);
         printf
             ("...succeded.  Set the padding values equal to the mid-80%% channel averages.\n");
         vect_free(dataavg[0]);
         vect_free(dataavg);
         fclose(statsfile);
         return 1;
      } else {
         /* This is a temporary solution */
         for (ii = 0; ii < obsmask->numchan; ii++)
            (*padvals)[ii] = 0.0;
         printf("...failed.\n  Set the padding values to 0.\n");
         return 0;
      }
   }
}
Example #11
0
int fread_multifile(void *data, size_t type, size_t number, multifile * mfile)
/* Read binary data from a multifile.                        */
/*   'data' is an array of the proper type to store the data */
/*   'type' is the size of each nugget of data to read       */
/*   'number' is the number of nuggets to read               */
/*   'mfile' is a pointer to a valid multifile structure     */
{
   int findex;
   size_t readbytes, bytesread, tmpbytesread;

   findex = mfile->currentfile;
   readbytes = number * type;
   bytesread = chkfread((char *) data, 1, readbytes, mfile->fileptrs[findex]);
   mfile->currentpos += bytesread;
   mfile->position += bytesread;
   readbytes -= bytesread;
   while (readbytes) {
      if (feof(mfile->fileptrs[findex])) {
         if (findex == mfile->numfiles - 1) {
            return bytesread / type;
         } else {
            findex++;
            mfile->currentfile++;
            mfile->currentpos = 0;
            tmpbytesread = chkfread((char *) data + bytesread, 1,
                                    readbytes, mfile->fileptrs[findex]);
            bytesread += tmpbytesread;
            mfile->currentpos += tmpbytesread;
            mfile->position += tmpbytesread;
            readbytes -= tmpbytesread;
         }
      } else {
         printf("\nRead error in read_multifile():\n");
         printf("\tTried to read %zd bytes, only read %zd!\n\n",
                number * type, bytesread);
         return bytesread / type;
      }
   }
   return bytesread / type;
}
Example #12
0
int main(int argc, char *argv[])
{
   FILE *infile, *outfile;
   int ii, numread;
   long long N = 0;
   float offset = 0.0, fbuffer[sizeof(float) * BUFFLEN];
   short sbuffer[sizeof(short) * BUFFLEN];
   char *outname;

   if (argc != 2 && argc != 3) {
      printf("\nUsage:  sdat2dat sdatfilename [offset to add]\n\n");
      exit(1);
   }
   printf("\n   Shorts to Floats Data Conversion Program\n\n");

   /* Get the root of the input filename and */
   /* generate the output filenames from it. */
   {
      char *rootnm, *suffix;

      split_root_suffix(argv[1], &rootnm, &suffix);
      outname = (char *) calloc(strlen(rootnm) + 5, sizeof(char));
      sprintf(outname, "%s.dat", rootnm);
      free(rootnm);
      free(suffix);
   }

   if (argc == 3) {
      offset = strtod(argv[2], NULL);
      printf(" Converting, adding %g, and writing to '%s'...", offset, outname);
   } else {
      printf(" Converting the data and writing to '%s'...", outname);
   }
   fflush(NULL);

   infile = chkfopen(argv[1], "rb");
   outfile = chkfopen(outname, "wb");
   while ((numread = chkfread(sbuffer, sizeof(short), BUFFLEN, infile))) {
      N += numread;
      for (ii = 0; ii < numread; ii++)
         fbuffer[ii] = (float) (sbuffer[ii]) + offset;
      fwrite(fbuffer, sizeof(float), numread, outfile);
   }
   printf("done.\n Wrote %lld points.\n\n", N);
   fclose(infile);
   fclose(outfile);
   free(outname);
   exit(0);
}
Example #13
0
int read_subband_rawblocks(FILE * infiles[], int numfiles, short *subbanddata,
                           int numsamples, int *padding)
{
   int ii, jj, index, numread = 0;

   for (ii = 0; ii < numfiles; ii++) {
      index = ii * numsamples;
      numread =
          chkfread(subbanddata + index, sizeof(short), numsamples, infiles[ii]);
      for (jj = numread; jj < numsamples; jj++)
         subbanddata[index + jj] = 0.0;
   }
   /* Subband data cannot currently be padded */
   *padding = 0;
   return numread;
}
Example #14
0
float get_numphotons(FILE * file)
  /* Return the total number of photons in the FFT file      */
  /* i.e.  it returns the value of the 0th frequency bin.    */
  /* Arguments:                                              */
  /*   'file' is a pointer to the file you want to access.   */
{
    float nph;

    chkfileseek(file, 0, sizeof(fcomplex), SEEK_SET);
    chkfread(&nph, sizeof(float), 1, file);

    /* The following protects against pre-normalized time-series */

    if (nph <= 0)
        nph = 1.0;
    return nph;
}
Example #15
0
static void read_rfifile(char *rfifilenm, rfi ** rfivect, int *numrfi,
                         int *numchan, int *numint, int *ptsperint,
                         int *lobin, int *numbetween, int *harmsum,
                         float *fracterror, float *freqsigma)
{
   FILE *outfile;
   int ii;

   outfile = chkfopen(rfifilenm, "rb");
   chkfread(numchan, sizeof(int), 1, outfile);
   chkfread(numint, sizeof(int), 1, outfile);
   chkfread(ptsperint, sizeof(int), 1, outfile);
   chkfread(lobin, sizeof(int), 1, outfile);
   chkfread(numbetween, sizeof(int), 1, outfile);
   chkfread(harmsum, sizeof(int), 1, outfile);
   chkfread(numrfi, sizeof(int), 1, outfile);
   chkfread(fracterror, sizeof(float), 1, outfile);
   chkfread(freqsigma, sizeof(float), 1, outfile);
   *rfivect = rfi_vector(*rfivect, *numchan, *numint, 0, *numrfi);
   for (ii = 0; ii < *numrfi; ii++)
      read_rfi(outfile, *rfivect + ii, *numchan, *numint);
   fclose(outfile);
}
Example #16
0
int read_PRESTO_subbands(FILE * infiles[], int numfiles, float *subbanddata,
                         double timeperblk, int *maskchans,
                         int *nummasked, mask * obsmask, float *padvals)
/* Read short int subband data written by prepsubband     */
/* Note:  This version returns a transpose of the version */
/*        listed in prepsubband.c                         */
{
   int ii, jj, index, numread = 0;
   short subsdata[SUBSBLOCKLEN];
   static int currentblock = 0;

   /* Read the data */
   for (ii = 0; ii < numfiles; ii++) {
      index = ii * SUBSBLOCKLEN;
      numread = chkfread(subsdata, sizeof(short), SUBSBLOCKLEN, infiles[ii]);
      for (jj = 0; jj < numread; jj++, index++)
         subbanddata[index] = (float) subsdata[jj];
      for (jj = numread; jj < SUBSBLOCKLEN; jj++, index++)
         subbanddata[index] = 0.0;
   }

   /* Mask it if required */
   if (obsmask->numchan && numread) {
      double starttime;
      starttime = currentblock * timeperblk;
      *nummasked = check_mask(starttime, timeperblk, obsmask, maskchans);
      if (*nummasked == -1) {   /* If all channels are masked */
         for (ii = 0; ii < numfiles; ii++) {
            index = ii * SUBSBLOCKLEN;
            for (jj = 0; jj < SUBSBLOCKLEN; jj++, index++)
               subbanddata[index] = padvals[ii];
         }
      } else if (*nummasked > 0) {      /* Only some of the channels are masked */
         int channum;
         for (ii = 0; ii < *nummasked; ii++) {
            channum = maskchans[ii];
            index = channum * SUBSBLOCKLEN;
            for (jj = 0; jj < SUBSBLOCKLEN; jj++, index++)
               subbanddata[index] = padvals[channum];
         }
      }
   }
   currentblock += 1;
   return numread;
}
Example #17
0
static void read_statsfile(char *statsfilenm, float ***datapow,
                           float ***dataavg, float ***datastd,
                           int *numchan, int *numint, int *ptsperint,
                           int *lobin, int *numbetween)
{
   FILE *outfile;

   outfile = chkfopen(statsfilenm, "rb");
   chkfread(numchan, sizeof(int), 1, outfile);
   chkfread(numint, sizeof(int), 1, outfile);
   chkfread(ptsperint, sizeof(int), 1, outfile);
   chkfread(lobin, sizeof(int), 1, outfile);
   chkfread(numbetween, sizeof(int), 1, outfile);
   *dataavg = gen_fmatrix(*numint, *numchan);
   *datastd = gen_fmatrix(*numint, *numchan);
   *datapow = gen_fmatrix(*numint, *numchan);
   chkfread(*(datapow[0]), sizeof(float), *numchan * *numint, outfile);
   chkfread(*(dataavg[0]), sizeof(float), *numchan * *numint, outfile);
   chkfread(*(datastd[0]), sizeof(float), *numchan * *numint, outfile);
   fclose(outfile);
}
Example #18
0
int read_shorts(FILE * file, float *data, int numpts, int numchan)
/* This routine reads a numpts records of numchan each from */
/* the input file *file which contains short integer data.  */
/* The equivalent floats are placed in *data.               */
/* It returns the number of points read.                    */
{
   short *sdata;
   int ii, numread;

   sdata = (short *) malloc((size_t) (sizeof(short) * (numpts * numchan)));
   if (!sdata) {
      perror("\nError allocating short array in read_shorts()");
      printf("\n");
      exit(-1);
   }
   numread = chkfread(sdata, sizeof(short),
                      (unsigned long) (numpts * numchan), file) / numchan;
   for (ii = 0; ii < numread; ii++)
      data[ii] = (float) sdata[ii];
   free(sdata);
   return numread;
}
Example #19
0
int read_rawbin_cand(FILE * file, rawbincand * cands)
/* Read the next rawbin candidate from the file */
/* If successful, return 1, else 0              */
{
   return chkfread(cands, sizeof(rawbincand), 1, file);
}
Example #20
0
int read_bin_cand(FILE * file, binaryprops * cands)
/* Read the next binary candidate from the file */
/* If successful, return 1, else 0              */
{
   return chkfread(cands, sizeof(binaryprops), 1, file);
}
Example #21
0
int read_resid_rec(FILE * file, double *toa, double *obsf)
/* This routine reads a single record (i.e. 1 TOA) from */
/* the file resid2.tmp which is written by TEMPO.       */
/* It returns 1 if successful, 0 if unsuccessful.       */
{
   static int firsttime = 1, use_ints = 0;
   static double d[9];

   // The default Fortran binary block marker has changed
   // several times in recent versions of g77 and gfortran.
   // g77 used 4 bytes, gfortran 4.0 and 4.1 used 8 bytes
   // and gfortrans 4.2 and higher use 4 bytes again.
   // So here we try to auto-detect what is going on.
   // The current version should be OK on 32- and 64-bit systems

   if (firsttime) {
       int ii;
       long long ll;
       double dd;

       chkfread(&ll, sizeof(long long), 1, file);
       chkfread(&dd, sizeof(double), 1, file);
       if (0) printf("(long long) index = %lld  (MJD = %17.10f)\n", ll, dd);
       if (ll != 72 || dd < 40000.0 || dd > 70000.0) { // 9 * doubles
           rewind(file);
           chkfread(&ii, sizeof(int), 1, file);
           chkfread(&dd, sizeof(double), 1, file);
           if (0) printf("(int) index = %d    (MJD = %17.10f)\n", ii, dd);
           if (ii == 72 && (dd > 40000.0 && dd < 70000.0)) {
               use_ints = 1;
           } else {
               fprintf(stderr, "\nError:  Can't read the TEMPO residuals correctly!\n");
               exit(1);
           }
       }
       rewind(file);
       firsttime = 0;
   }
   if (use_ints) {
       int ii;
       chkfread(&ii, sizeof(int), 1, file);
   } else {
       long long ll;
       chkfread(&ll, sizeof(long long), 1, file);
   }
   //  Now read the rest of the binary record
   chkfread(&d, sizeof(double), 9, file);
   if (0) { // For debugging
       printf("Barycentric TOA = %17.10f\n", d[0]);
       printf("Postfit residual (pulse phase) = %g\n", d[1]);
       printf("Postfit residual (seconds) = %g\n", d[2]);
       printf("Orbital phase = %g\n", d[3]);
       printf("Barycentric Observing freq = %g\n", d[4]);
       printf("Weight of point in the fit = %g\n", d[5]);
       printf("Timing uncertainty = %g\n", d[6]);
       printf("Prefit residual (seconds) = %g\n", d[7]);
       printf("??? = %g\n\n", d[8]);
   }
   *toa = d[0];
   *obsf = d[4];
   if (use_ints) {
       int ii;
       return chkfread(&ii, sizeof(int), 1, file);
   } else {
       long ll;
       return chkfread(&ll, sizeof(long), 1, file);
   }
}
Example #22
0
int main(int argc, char *argv[])
{
   FILE *bytemaskfile;
   float **dataavg = NULL, **datastd = NULL, **datapow = NULL;
   float *chandata = NULL, powavg, powstd, powmax;
   float inttime, norm, fracterror = RFI_FRACTERROR;
   float *rawdata = NULL;
   unsigned char **bytemask = NULL;
   short *srawdata = NULL;
   char *outfilenm, *statsfilenm, *maskfilenm;
   char *bytemaskfilenm, *rfifilenm;
   int numchan = 0, numint = 0, newper = 0, oldper = 0, good_padvals = 0;
   int blocksperint, ptsperint = 0, ptsperblock = 0, padding = 0;
   int numcands, candnum, numrfi = 0, numrfivect = NUM_RFI_VECT;
   int ii, jj, kk, slen, numread = 0, insubs = 0;
   int harmsum = RFI_NUMHARMSUM, lobin = RFI_LOBIN, numbetween = RFI_NUMBETWEEN;
   double davg, dvar, freq;
   struct spectra_info s;
   presto_interptype interptype;
   rfi *rfivect = NULL;
   mask oldmask, newmask;
   fftcand *cands;
   infodata idata;
   Cmdline *cmd;

   /* Call usage() if we have no command line arguments */

   if (argc == 1) {
      Program = argv[0];
      printf("\n");
      usage();
      exit(0);
   }

   /* Parse the command line using the excellent program Clig */

   cmd = parseCmdline(argc, argv);
   spectra_info_set_defaults(&s);
   s.filenames = cmd->argv;
   s.num_files = cmd->argc;
   s.clip_sigma = cmd->clip;
   // -1 causes the data to determine if we use weights, scales, & 
   // offsets for PSRFITS or flip the band for any data type where
   // we can figure that out with the data
   s.apply_flipband = (cmd->invertP) ? 1 : -1;
   s.apply_weight = (cmd->noweightsP) ? 0 : -1;
   s.apply_scale  = (cmd->noscalesP) ? 0 : -1;
   s.apply_offset = (cmd->nooffsetsP) ? 0 : -1;
   s.remove_zerodm = (cmd->zerodmP) ? 1 : 0;
   if (cmd->noclipP) {
       cmd->clip = 0.0;
       s.clip_sigma = 0.0;
   }
   if (cmd->ifsP) {
       // 0 = default or summed, 1-4 are possible also
       s.use_poln = cmd->ifs;
   }
   slen = strlen(cmd->outfile) + 20;

#ifdef DEBUG
   showOptionValues();
#endif

   printf("\n\n");
   printf("               Pulsar Data RFI Finder\n");
   printf("                 by Scott M. Ransom\n\n");

   /* The following is the root of all the output files */

   outfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(outfilenm, "%s_rfifind", cmd->outfile);

   /* And here are the output file names */

   maskfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(maskfilenm, "%s.mask", outfilenm);
   bytemaskfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(bytemaskfilenm, "%s.bytemask", outfilenm);
   rfifilenm = (char *) calloc(slen, sizeof(char));
   sprintf(rfifilenm, "%s.rfi", outfilenm);
   statsfilenm = (char *) calloc(slen, sizeof(char));
   sprintf(statsfilenm, "%s.stats", outfilenm);
   sprintf(idata.name, "%s", outfilenm);

   if (RAWDATA) {
       if (cmd->filterbankP) s.datatype = SIGPROCFB;
       else if (cmd->psrfitsP) s.datatype = PSRFITS;
       else if (cmd->pkmbP) s.datatype = SCAMP;
       else if (cmd->bcpmP) s.datatype = BPP;
       else if (cmd->wappP) s.datatype = WAPP;
       else if (cmd->spigotP) s.datatype = SPIGOT;
   } else {  // Attempt to auto-identify the data
       identify_psrdatatype(&s, 1);
       if (s.datatype==SIGPROCFB) cmd->filterbankP = 1;
       else if (s.datatype==PSRFITS) cmd->psrfitsP = 1;
       else if (s.datatype==SCAMP) cmd->pkmbP = 1;
       else if (s.datatype==BPP) cmd->bcpmP = 1;
       else if (s.datatype==WAPP) cmd->wappP = 1;
       else if (s.datatype==SPIGOT) cmd->spigotP = 1;
       else if (s.datatype==SUBBAND) insubs = 1;
       else {
           printf("Error:  Unable to identify input data files.  Please specify type.\n\n");
           exit(1);
       }
   }

   if (!cmd->nocomputeP) {

       if (RAWDATA || insubs) {
           char description[40];
           psrdatatype_description(description, s.datatype);
           if (s.num_files > 1)
               printf("Reading %s data from %d files:\n", description, s.num_files);
           else
               printf("Reading %s data from 1 file:\n", description);
           if (insubs) s.files = (FILE **)malloc(sizeof(FILE *) * s.num_files);
           for (ii = 0; ii < s.num_files; ii++) {
               printf("  '%s'\n", cmd->argv[ii]);
               if (insubs) s.files[ii] = chkfopen(cmd->argv[ii], "rb");
           }
           printf("\n");
       }           

       if (RAWDATA) {
           read_rawdata_files(&s);
           print_spectra_info_summary(&s);
           spectra_info_to_inf(&s, &idata);
           ptsperblock = s.spectra_per_subint;
           numchan = s.num_channels;
           idata.dm = 0.0;
       }
       
       if (insubs) {
           /* Set-up values if we are using subbands */
           char *tmpname, *root, *suffix;
           if (split_root_suffix(s.filenames[0], &root, &suffix) == 0) {
               printf("Error:  The input filename (%s) must have a suffix!\n\n", s.filenames[0]);
               exit(1);
           }
           if (strncmp(suffix, "sub", 3) == 0) {
               tmpname = calloc(strlen(root) + 6, 1);
               sprintf(tmpname, "%s.sub", root);
               readinf(&idata, tmpname);
               free(tmpname);
           } else {
               printf("\nThe input files (%s) must be subbands!  (i.e. *.sub##)\n\n",
                      s.filenames[0]);
               exit(1);
           }
           free(root);
           free(suffix);
           ptsperblock = 1;
           /* Compensate for the fact that we have subbands and not channels */
           idata.freq = idata.freq - 0.5 * idata.chan_wid +
               0.5 * idata.chan_wid * (idata.num_chan / s.num_files);
           idata.chan_wid = idata.num_chan / s.num_files * idata.chan_wid;
           idata.num_chan = numchan = s.num_files;
           idata.dm = 0.0;
           sprintf(idata.name, "%s", outfilenm);
           writeinf(&idata);
           s.padvals = gen_fvect(s.num_files);
           for (ii = 0 ; ii < s.num_files ; ii++)
               s.padvals[ii] = 0.0;
       }

       /* Read an input mask if wanted */
       if (cmd->maskfileP) {
           read_mask(cmd->maskfile, &oldmask);
           printf("Read old mask information from '%s'\n\n", cmd->maskfile);
           good_padvals = determine_padvals(cmd->maskfile, &oldmask, s.padvals);
       } else {
           oldmask.numchan = oldmask.numint = 0;
       }

      /* The number of data points and blocks to work with at a time */

      if (cmd->blocksP) {
         blocksperint = cmd->blocks;
         cmd->time = blocksperint * ptsperblock * idata.dt;
      } else {
         blocksperint = (int) (cmd->time / (ptsperblock * idata.dt) + 0.5);
      }
      ptsperint = blocksperint * ptsperblock;
      numint = (long long) idata.N / ptsperint;
      if ((long long) idata.N % ptsperint)
         numint++;
      inttime = ptsperint * idata.dt;
      printf("Analyzing data sections of length %d points (%.6g sec).\n",
             ptsperint, inttime);
      {
         int *factors, numfactors;

         factors = get_prime_factors(ptsperint, &numfactors);
         printf("  Prime factors are:  ");
         for (ii = 0; ii < numfactors; ii++)
            printf("%d ", factors[ii]);
         printf("\n");
         if (factors[numfactors - 1] > 13) {
            printf("  WARNING:  The largest prime factor is pretty big!  This will\n"
                   "            cause the FFTs to take a long time to compute.  I\n"
                   "            recommend choosing a different -time value.\n");
         }
         printf("\n");
         free(factors);
      }

      /* Allocate our workarrays */

      if (RAWDATA)
          rawdata = gen_fvect(idata.num_chan * ptsperblock * blocksperint);
      else if (insubs)
          srawdata = gen_svect(idata.num_chan * ptsperblock * blocksperint);
      dataavg = gen_fmatrix(numint, numchan);
      datastd = gen_fmatrix(numint, numchan);
      datapow = gen_fmatrix(numint, numchan);
      chandata = gen_fvect(ptsperint);
      bytemask = gen_bmatrix(numint, numchan);
      for (ii = 0; ii < numint; ii++)
         for (jj = 0; jj < numchan; jj++)
            bytemask[ii][jj] = GOODDATA;
      rfivect = rfi_vector(rfivect, numchan, numint, 0, numrfivect);
      if (numbetween == 2)
         interptype = INTERBIN;
      else
         interptype = INTERPOLATE;

      /* Main loop */

      printf("Writing mask data  to '%s'.\n", maskfilenm);
      printf("Writing  RFI data  to '%s'.\n", rfifilenm);
      printf("Writing statistics to '%s'.\n\n", statsfilenm);
      printf("Massaging the data ...\n\n");
      printf("Amount Complete = %3d%%", oldper);
      fflush(stdout);

      for (ii = 0; ii < numint; ii++) { /* Loop over the intervals */
         newper = (int) ((float) ii / numint * 100.0 + 0.5);
         if (newper > oldper) {
            printf("\rAmount Complete = %3d%%", newper);
            fflush(stdout);
            oldper = newper;
         }

         /* Read a chunk of data */

         if (RAWDATA)
             numread = read_rawblocks(rawdata, blocksperint, &s, &padding);
         else if (insubs)
             numread = read_subband_rawblocks(s.files, s.num_files,
                                              srawdata, blocksperint, &padding);

         if (padding)
            for (jj = 0; jj < numchan; jj++)
               bytemask[ii][jj] |= PADDING;

         for (jj = 0; jj < numchan; jj++) {     /* Loop over the channels */

             if (RAWDATA)
                 get_channel(chandata, jj, blocksperint, rawdata, &s);
             else if (insubs)
                 get_subband(jj, chandata, srawdata, blocksperint);

            /* Calculate the averages and standard deviations */
            /* for each point in time.                        */

            if (padding) {
                dataavg[ii][jj] = 0.0;
                datastd[ii][jj] = 0.0;
                datapow[ii][jj] = 1.0;
            } else {
               avg_var(chandata, ptsperint, &davg, &dvar);
               dataavg[ii][jj] = davg;
               datastd[ii][jj] = sqrt(dvar);
               realfft(chandata, ptsperint, -1);
               numcands = 0;
               norm = datastd[ii][jj] * datastd[ii][jj] * ptsperint;
               if (norm == 0.0)
                  norm = (chandata[0] == 0.0) ? 1.0 : chandata[0];
               cands = search_fft((fcomplex *) chandata, ptsperint / 2,
                                  lobin, ptsperint / 2, harmsum,
                                  numbetween, interptype, norm, cmd->freqsigma,
                                  &numcands, &powavg, &powstd, &powmax);
               datapow[ii][jj] = powmax;

               /* Record the birdies */

               if (numcands) {
                  for (kk = 0; kk < numcands; kk++) {
                     freq = cands[kk].r / inttime;
                     candnum = find_rfi(rfivect, numrfi, freq, RFI_FRACTERROR);
                     if (candnum >= 0) {
                        update_rfi(rfivect + candnum, freq, cands[kk].sig, jj, ii);
                     } else {
                        update_rfi(rfivect + numrfi, freq, cands[kk].sig, jj, ii);
                        numrfi++;
                        if (numrfi == numrfivect) {
                           numrfivect *= 2;
                           rfivect = rfi_vector(rfivect, numchan, numint,
                                                numrfivect / 2, numrfivect);
                        }
                     }
                  }
                  free(cands);
               }
            }
         }
      }
      printf("\rAmount Complete = 100%%\n");

      /* Write the data to the output files */

      write_rfifile(rfifilenm, rfivect, numrfi, numchan, numint,
                    ptsperint, lobin, numbetween, harmsum,
                    fracterror, cmd->freqsigma);
      write_statsfile(statsfilenm, datapow[0], dataavg[0], datastd[0],
                      numchan, numint, ptsperint, lobin, numbetween);

   } else {                     /* If "-nocompute" */
      float freqsigma;

      /* Read the data from the output files */

      printf("Reading  RFI data  from '%s'.\n", rfifilenm);
      printf("Reading statistics from '%s'.\n", statsfilenm);
      readinf(&idata, outfilenm);
      read_rfifile(rfifilenm, &rfivect, &numrfi, &numchan, &numint,
                   &ptsperint, &lobin, &numbetween, &harmsum,
                   &fracterror, &freqsigma);
      numrfivect = numrfi;
      read_statsfile(statsfilenm, &datapow, &dataavg, &datastd,
                     &numchan, &numint, &ptsperint, &lobin, &numbetween);
      bytemask = gen_bmatrix(numint, numchan);
      printf("Reading  bytemask  from '%s'.\n\n", bytemaskfilenm);
      bytemaskfile = chkfopen(bytemaskfilenm, "rb");
      chkfread(bytemask[0], numint * numchan, 1, bytemaskfile);
      fclose(bytemaskfile);
      for (ii = 0; ii < numint; ii++)
         for (jj = 0; jj < numchan; jj++)
            bytemask[ii][jj] &= PADDING;        /* Clear all but the PADDING bits */
      inttime = ptsperint * idata.dt;
   }

   /* Make the plots and set the mask */

   {
      int *zapints, *zapchan;
      int numzapints = 0, numzapchan = 0;

      if (cmd->zapintsstrP) {
         zapints = ranges_to_ivect(cmd->zapintsstr, 0, numint - 1, &numzapints);
         zapints = (int *) realloc(zapints, (size_t) (sizeof(int) * numint));
      } else {
         zapints = gen_ivect(numint);
      }
      if (cmd->zapchanstrP) {
         zapchan = ranges_to_ivect(cmd->zapchanstr, 0, numchan - 1, &numzapchan);
         zapchan = (int *) realloc(zapchan, (size_t) (sizeof(int) * numchan));
      } else {
         zapchan = gen_ivect(numchan);
      }
      rfifind_plot(numchan, numint, ptsperint, cmd->timesigma, cmd->freqsigma,
                   cmd->inttrigfrac, cmd->chantrigfrac,
                   dataavg, datastd, datapow, zapchan, numzapchan,
                   zapints, numzapints, &idata, bytemask,
                   &oldmask, &newmask, rfivect, numrfi,
                   cmd->rfixwinP, cmd->rfipsP, cmd->xwinP);

      vect_free(zapints);
      vect_free(zapchan);
   }

   /* Write the new mask and bytemask to the file */

   write_mask(maskfilenm, &newmask);
   bytemaskfile = chkfopen(bytemaskfilenm, "wb");
   chkfwrite(bytemask[0], numint * numchan, 1, bytemaskfile);
   fclose(bytemaskfile);

   /* Determine the percent of good and bad data */

   {
      int numpad = 0, numbad = 0, numgood = 0;

      for (ii = 0; ii < numint; ii++) {
         for (jj = 0; jj < numchan; jj++) {
            if (bytemask[ii][jj] == GOODDATA) {
               numgood++;
            } else {
               if (bytemask[ii][jj] & PADDING)
                  numpad++;
               else
                  numbad++;
            }
         }
      }
      printf("\nTotal number of intervals in the data:  %d\n\n", numint * numchan);
      printf("  Number of padded intervals:  %7d  (%6.3f%%)\n",
             numpad, (float) numpad / (float) (numint * numchan) * 100.0);
      printf("  Number of  good  intervals:  %7d  (%6.3f%%)\n",
             numgood, (float) numgood / (float) (numint * numchan) * 100.0);
      printf("  Number of  bad   intervals:  %7d  (%6.3f%%)\n\n",
             numbad, (float) numbad / (float) (numint * numchan) * 100.0);
      qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_sigma);
      printf("  Ten most significant birdies:\n");
      printf("#  Sigma     Period(ms)      Freq(Hz)       Number \n");
      printf("----------------------------------------------------\n");
      for (ii = 0; ii < 10; ii++) {
         double pperr;
         char temp1[40], temp2[40];

         if (rfivect[ii].freq_var == 0.0) {
            pperr = 0.0;
            sprintf(temp1, " %-14g", rfivect[ii].freq_avg);
            sprintf(temp2, " %-14g", 1000.0 / rfivect[ii].freq_avg);
         } else {
            pperr = 1000.0 * sqrt(rfivect[ii].freq_var) /
                (rfivect[ii].freq_avg * rfivect[ii].freq_avg);
            nice_output_2(temp1, rfivect[ii].freq_avg, sqrt(rfivect[ii].freq_var),
                          -15);
            nice_output_2(temp2, 1000.0 / rfivect[ii].freq_avg, pperr, -15);
         }
         printf("%-2d %-8.2f %13s %13s %-8d\n", ii + 1, rfivect[ii].sigma_avg,
                temp2, temp1, rfivect[ii].numobs);
      }
      qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_numobs);
      printf("\n  Ten most numerous birdies:\n");
      printf("#  Number    Period(ms)      Freq(Hz)       Sigma \n");
      printf("----------------------------------------------------\n");
      for (ii = 0; ii < 10; ii++) {
         double pperr;
         char temp1[40], temp2[40];

         if (rfivect[ii].freq_var == 0.0) {
            pperr = 0.0;
            sprintf(temp1, " %-14g", rfivect[ii].freq_avg);
            sprintf(temp2, " %-14g", 1000.0 / rfivect[ii].freq_avg);
         } else {
            pperr = 1000.0 * sqrt(rfivect[ii].freq_var) /
                (rfivect[ii].freq_avg * rfivect[ii].freq_avg);
            nice_output_2(temp1, rfivect[ii].freq_avg, sqrt(rfivect[ii].freq_var),
                          -15);
            nice_output_2(temp2, 1000.0 / rfivect[ii].freq_avg, pperr, -15);
         }
         printf("%-2d %-8d %13s %13s %-8.2f\n", ii + 1, rfivect[ii].numobs,
                temp2, temp1, rfivect[ii].sigma_avg);
      }
      printf("\nDone.\n\n");
   }

   /* Close the files and cleanup */

   free_rfi_vector(rfivect, numrfivect);
   free_mask(newmask);
   if (cmd->maskfileP)
      free_mask(oldmask);
   free(outfilenm);
   free(statsfilenm);
   free(bytemaskfilenm);
   free(maskfilenm);
   free(rfifilenm);
   vect_free(dataavg[0]);
   vect_free(dataavg);
   vect_free(datastd[0]);
   vect_free(datastd);
   vect_free(datapow[0]);
   vect_free(datapow);
   vect_free(bytemask[0]);
   vect_free(bytemask);
   if (!cmd->nocomputeP) {
       //  Close all the raw files and free their vectors
       close_rawfiles(&s);
       vect_free(chandata);
       if (insubs)
           vect_free(srawdata);
       else
           vect_free(rawdata);
   }
   return (0);
}
Example #23
0
int main(int argc, char *argv[])
{
   float *data;
   int status, isign;
   unsigned long numdata;
/*   unsigned long next2ton; */
   FILE *datafile, *scratchfile;
   char datafilenm[100], scratchfilenm[100], resultfilenm[100];
   char command[80];
   struct tms runtimes;
   double ttim, stim, utim, tott;

   tott = times(&runtimes) / (double) CLK_TCK;
   printf("\n\n");
   printf("   Real-Valued Data FFT Program v2.0\n");
   printf("        by Scott M. Ransom\n");
   printf("           8 June, 1997\n\n");

   if ((argc > 1) && (argc < 7)) {

      /*      Open and check data file.    */

      if (argc == 3) {
         sprintf(datafilenm, "%s.", argv[2]);
         sprintf(scratchfilenm, "%s.tmp", argv[2]);
         sprintf(resultfilenm, "%s.", argv[2]);
      }
      if (argc == 4) {
         sprintf(datafilenm, "%s/%s.", argv[3], argv[2]);
         sprintf(scratchfilenm, "%s/%s.tmp", argv[3], argv[2]);
         sprintf(resultfilenm, "%s/%s.", argv[3], argv[2]);
      }
      if (argc == 5) {
         sprintf(datafilenm, "%s/%s.", argv[3], argv[2]);
         sprintf(scratchfilenm, "%s/%s.tmp", argv[4], argv[2]);
         sprintf(resultfilenm, "%s/%s.", argv[3], argv[2]);
      }
      isign = atoi(argv[1]);

      /*  Add the appropriate suffix to the filenames. */

      if (isign == 1) {
         strcat(datafilenm, "fft");
         strcat(resultfilenm, "dat");
      } else {
         strcat(datafilenm, "dat");
         strcat(resultfilenm, "fft");
      }

      /*  Check the input data set...  */

      printf("Checking data in \"%s\".\n", datafilenm);
      datafile = chkfopen(datafilenm, "rb");

      /* # of real data points */

      numdata = chkfilelen(datafile, sizeof(float));
/*     next2ton = next2_to_n(numdata); */
/*     if (numdata != next2ton) { */
/*       printf("\nNumber of data pts must be an integer power of two,\n"); */
/*       printf("     or data must be single precision floats.\n"); */
/*       printf("Exiting.\n\n"); */
/*       fclose(datafile); */
/*       exit(1); */
/*     } */
      printf("Data OK.  There are %ld points.\n\n", numdata);
      fclose(datafile);
   } else {
      printf("\nUsage:  realfft sign datafilename [ data path] ");
      printf("[scratch path]\n\n");
      printf("  This program calculates the FFT of a file containing\n");
      printf("  a power-of-two number of floats representing\n");
      printf("  real numbers.  (i.e. a normal time series)\n\n");
      printf("  THE INPUT FILE WILL BE OVERWRITTEN.\n\n");
      printf("  \"sign\" is the sign of the exponential during the FFT. \n");
      printf("  (i.e. -1 is the standard forward transform, 1 is the\n");
      printf("  inverse transform times N (the number of floats))\n");
      printf("  If both paths are omitted, the data is assumed to be\n");
      printf("  located in the working directory, and the scratch space\n");
      printf("  if needed will be located there. If only one path is\n");
      printf("  given, both input and scratch files will reside there.\n\n");
      printf("  Notes:\n");
      printf("    - datafilename must not include \".dat\" or \".fft\"\n");
      printf("        suffix. It will be added by the program.\n");
      printf("    - Do not end paths in \"/\".\n");
      printf("    - The scratch file is the same size as the input file.\n");
      printf("    - If \"sign\"=1, the file to be transformed should end\n");
      printf("        with \".fft\".  Otherwise, it should end with\n");
      printf("        \".dat\".\n\n");
      exit(0);
   }

   /*     Start the transform sequence               */

   if (numdata > MAXREALFFT) {

      /*  Perform Two-Pass, Out-of-Core, FFT  */

      printf("Performing Out-of-Core Two-Pass FFT on data.\n\n");
      printf("Result will be stored in the file \"%s\".\n", resultfilenm);

      /*  Initialize files. */

      datafile = chkfopen(datafilenm, "rb+");
      scratchfile = chkfopen(scratchfilenm, "wb+");

      printf("\nTransforming...\n");

      /*     Call Two-Pass routine  */

      if (isign == 1) {
         realfft_scratch_inv(datafile, scratchfile, numdata);
      } else {
         realfft_scratch_fwd(datafile, scratchfile, numdata);
      }

      fclose(scratchfile);

      sprintf(command, "rm -f %s\n", scratchfilenm);
      if ((status = (system(command))) == -1 || status == 127) {
         perror("\nSystem call (rm) failed");
         printf("\n");
         exit(1);
      }
      printf("Finished.\n\n");

      printf("Timing summary:\n");
      tott = times(&runtimes) / (double) CLK_TCK - tott;
      utim = runtimes.tms_utime / (double) CLK_TCK;
      stim = runtimes.tms_stime / (double) CLK_TCK;
      ttim = utim + stim;
      printf("CPU usage: %.3f sec total (%.3f sec user, %.3f sec system)\n",
             ttim, utim, stim);
      printf("Total time elapsed:  %.3f sec.\n\n", tott);

   } else {

      /* Perform standard FFT for real functions  */

      printf("Performing in-core FFT for real functions on data.\n\n");
      printf("FFT will be stored in the file \"%s\".\n\n", resultfilenm);

      /* Open the data and fft results files.    */

      datafile = chkfopen(datafilenm, "rb+");

      /* Read data from file to data array   */

      printf("Reading data.\n\n");
      data = gen_fvect(numdata);
      chkfread(data, sizeof(float), (unsigned long) numdata, datafile);
      chkfileseek(datafile, 0L, sizeof(char), SEEK_SET);

      /* Start and time the transform   */

      printf("Transforming...\n");
      realfft(data, numdata, isign);
      printf("Finished.\n\n");

      /* Write data from FFT array to file  */

      printf("Writing FFT data.\n\n");
      chkfwrite(data, sizeof(float), (unsigned long) numdata, datafile);
      vect_free(data);

      /* Output the timing information */

      printf("Timing summary:\n");
      tott = times(&runtimes) / (double) CLK_TCK - tott;
      utim = runtimes.tms_utime / (double) CLK_TCK;
      stim = runtimes.tms_stime / (double) CLK_TCK;
      ttim = utim + stim;
      printf("CPU usage: %.3f sec total (%.3f sec user, %.3f sec system)\n",
             ttim, utim, stim);
      printf("Total time elapsed:  %.3f sec.\n\n", tott);

   }

   sprintf(command, "mv %s %s\n", datafilenm, resultfilenm);
   if ((status = (system(command))) == -1 || status == 127) {
      perror("\nSystem call (mv) failed");
      printf("\n");
      exit(1);
   }
   fclose(datafile);
   /*
      fftw_print_max_memory_usage();
      fftw_check_memory_leaks();
    */
   exit(0);
}
Example #24
0
int main(int argc, char **argv)
{
   int index = -1, need_type = 0;
   int objs_read, objs_to_read, has_suffix;
   long i, j, ct;
   char *cptr, *data, *short_filenm, *extension, key = '\n';
   FILE *infile;
   Cmdline *cmd;
   infodata inf;

   /* Call usage() if we have no command line arguments */

   if (argc == 1) {
      Program = argv[0];
      usage();
      exit(0);
   }

   /* Parse the command line using the excellent program Clig */

   cmd = parseCmdline(argc, argv);

#ifdef DEBUG
   showOptionValues();
#endif

   //fprintf(stdout, "\n\n  PRESTO Binary File Reader\n");
   //fprintf(stdout, "     by Scott M. Ransom\n\n");

   /* Set our index value */

   if (cmd->bytP || cmd->sbytP)
      index = BYTE;
   else if (cmd->fltP || cmd->sfltP)
      index = FLOAT;
   else if (cmd->dblP || cmd->sdblP)
      index = DOUBLE;
   else if (cmd->fcxP || cmd->sfcxP)
      index = FCPLEX;
   else if (cmd->dcxP || cmd->sdcxP)
      index = DCPLEX;
   else if (cmd->shtP || cmd->sshtP)
      index = SHORT;
   else if (cmd->igrP || cmd->sigrP)
      index = INT;
   else if (cmd->lngP || cmd->slngP)
      index = LONG;
   else if (cmd->rzwP || cmd->srzwP)
      index = RZWCAND;
   else if (cmd->binP || cmd->sbinP)
      index = BINCAND;
   else if (cmd->posP || cmd->sposP)
      index = POSITION;
   else if (cmd->pkmbP)
      index = PKMBHDR;
   else if (cmd->bcpmP)
      index = BCPMHDR;
   else if (cmd->wappP)
      index = WAPPHDR;
   else if (cmd->spigotP)
      index = SPIGOTHDR;
   else if (cmd->filterbankP)
      index = SPECTRAINFO;
#ifdef USELOFAR   
   else if (cmd->lofarhdf5P)
      index = SPECTRAINFO;
#endif
   else if (cmd->psrfitsP)
      index = SPECTRAINFO;
   
   /* Try to determine the data type from the file name */

   if (index == -1) {
      has_suffix = split_root_suffix(cmd->argv[0], &short_filenm, &extension);
      if (!has_suffix) {
         need_type = 1;
      } else {
         if (strlen(extension) < 2) {
            need_type = 1;
         } else {
            if (0 == strcmp(extension, "dat")) {
               index = FLOAT;
               fprintf(stdout, "Assuming the data is floating point.\n\n");
            } else if (0 == strcmp(extension, "sdat")) {
               index = SHORT;
               fprintf(stdout, "Assuming the data is short integers.\n\n");
            } else if (0 == strcmp(extension, "fft")) {
               index = FCPLEX;
               fprintf(stdout, "Assuming the data is single precision complex.\n\n");
            } else if ((0 == strcmp(extension, "fits")) ||
                       (0 == strcmp(extension, "sf"))) {
                if (strstr(short_filenm, "spigot_5") != NULL) {
                    cmd->spigotP = 1;
                    index = SPIGOTHDR;
                    fprintf(stdout,
                            "Assuming the data is from the Caltech/NRAO Spigot.\n\n");
                } else if (is_PSRFITS(cmd->argv[0])) {
                    cmd->psrfitsP = 1;
                    index = SPECTRAINFO;
                    fprintf(stdout,
                            "Assuming the data is in PSRFITS format.\n\n");
                }
            } else if (0 == strcmp(extension, "bcpm1") ||
                       0 == strcmp(extension, "bcpm2")) {
               cmd->bcpmP = 1;
               index = BCPMHDR;
               fprintf(stdout, "Assuming the data is from a BCPM machine.\n\n");
            } else if (0 == strcmp(extension, "pkmb")) {
               cmd->pkmbP = 1;
               index = PKMBHDR;
               fprintf(stdout,
                       "Assuming the data is from the Parkes Multibeam machine.\n\n");
            } else if (0 == strcmp(extension, "fil") || 0 == strcmp(extension, "fb")) {
               cmd->filterbankP = 1;
               index = SPECTRAINFO;
               fprintf(stdout,
                       "Assuming the data is a SIGPROC filterbank file.\n\n");
            } else if (0 == strcmp(extension, "h5")) {
               cmd->lofarhdf5P = 1;
               index = SPECTRAINFO;
               fprintf(stdout,
                       "Assuming the data is a LOFAR HDF5 file.\n\n");
            } else if (isdigit(extension[0]) &&
                       isdigit(extension[1]) && isdigit(extension[2])) {
               cmd->wappP = 1;
               index = WAPPHDR;
               fprintf(stdout, "Assuming the data is from a WAPP machine.\n\n");
            } else if (0 == strcmp(extension, "pos")) {
               index = POSITION;
               fprintf(stdout,
                       "Assuming the data contains 'position' structures.\n\n");
            } else if (0 == strcmp(extension, "cand")) {
               /* A binary or RZW search file? */
               if (NULL != (cptr = strstr(cmd->argv[0], "_bin"))) {
                  index = BINCAND;
                  fprintf(stdout,
                          "Assuming the file contains binary candidates.\n\n");
               } else if (NULL != (cptr = strstr(cmd->argv[0], "_rzw"))) {
                  index = RZWCAND;
                  ct = (long) (cptr - cmd->argv[0]);
                  fprintf(stdout, "Assuming the file contains 'RZW' candidates.\n");
                  free(short_filenm);
                  short_filenm = (char *) malloc(ct + 1);
                  short_filenm[ct] = '\0';
                  strncpy(short_filenm, cmd->argv[0], ct);
                  fprintf(stdout, "\nAttempting to read '%s.inf'.  ", short_filenm);
                  readinf(&inf, short_filenm);
                  fprintf(stdout, "Successful.\n");
                  N = (long) (inf.N + DBLCORRECT);
                  dt = inf.dt;
                  if (cmd->nphP)
                     nph = cmd->nph;
                  else
                     nph = 1.0;
                  fprintf(stdout,
                          "\nUsing N = %ld, dt = %g, and DC Power = %f\n\n",
                          N, dt, nph);
               } else if (NULL != (cptr = strstr(cmd->argv[0], "_ACCEL"))) {
                  index = RZWCAND;
                  ct = (long) (cptr - cmd->argv[0]);
                  fprintf(stdout, "Assuming the file contains 'RZW' candidates.\n");
                  free(short_filenm);
                  short_filenm = (char *) malloc(ct + 1);
                  short_filenm[ct] = '\0';
                  strncpy(short_filenm, cmd->argv[0], ct);
                  fprintf(stdout, "\nAttempting to read '%s.inf'.  ", short_filenm);
                  readinf(&inf, short_filenm);
                  fprintf(stdout, "Successful.\n");
                  N = (long) (inf.N + DBLCORRECT);
                  dt = inf.dt;
                  if (cmd->nphP)
                     nph = cmd->nph;
                  else
                     nph = 1.0;
                  fprintf(stdout,
                          "\nUsing N = %ld, dt = %g, and DC Power = %f\n\n",
                          N, dt, nph);
               } else
                  need_type = 1;
            } else
               need_type = 1;
         }
      }

      /* If no file extension or if we don't understand the extension, exit */

      if (need_type) {
         fprintf(stdout, "You must specify a data type for this file.\n\n");
         free(short_filenm);
         exit(-1);
      }
      free(short_filenm);
      if (has_suffix)
         free(extension);
   }

   if (cmd->index[1] == -1 || cmd->index[1] == 0)
      cmd->index[1] = INT_MAX;
   if (cmd->index[1] < cmd->index[0]) {
      fprintf(stdout, "\nThe high index must be >= the low index.");
      fprintf(stdout, "  Exiting.\n\n");
      exit(-1);
   }

   // Use new-style backend reading stuff
   if (cmd->psrfitsP || cmd->filterbankP || cmd->lofarhdf5P) {
       struct spectra_info s;

       // Eventually we should use this...
       // identify_psrdatatype(struct spectra_info *s, int output);
       spectra_info_set_defaults(&s);
       if (cmd->psrfitsP) s.datatype=PSRFITS;
       if (cmd->filterbankP) s.datatype=SIGPROCFB;
       if (cmd->lofarhdf5P) s.datatype=LOFARHDF5;
       s.filenames = cmd->argv;
       s.num_files = cmd->argc;
       s.clip_sigma = 0.0;
       s.apply_flipband = s.apply_weight = s.apply_scale = s.apply_offset = -1;
       s.remove_zerodm = 0;
       read_rawdata_files(&s);
       SPECTRAINFO_print(0, (char *)(&s));
       printf("\n");
       exit(0);
   }

   if (cmd->spigotP) {
      SPIGOT_INFO spigot;

      if (read_SPIGOT_header(cmd->argv[0], &spigot)) {
         print_SPIGOT_header(&spigot);
         printf("\n");
      } else {
         printf("\n  Error reading spigot file!\n\n");
      }
      exit(0);
   }

   if (cmd->wappP) {
       struct HEADERP *hdr = NULL;
       
       infile = chkfopen(cmd->argv[0], "rb");
       hdr = head_parse(infile);
       set_WAPP_HEADER_version(hdr);
       if (hdr) {
           print_WAPP_hdr(hdr);
           printf("\n");
       } else {
           printf("\n  Error reading WAPP file!\n\n");
       }
       exit(0);
   }

   /* Open the file */

   infile = chkfopen(cmd->argv[0], "rb");

   if (cmd->fortranP) {
      chkfileseek(infile, 1, sizeof(long), SEEK_SET);
   }

   /* Skip to the correct first object */

   if (cmd->index[0] > 0) {
      chkfileseek(infile, (long) (cmd->index[0]), type_sizes[index], SEEK_CUR);
   }

   /* Read the file */

   objs_to_read = objs_at_a_time[index];
   data = (char *) malloc(type_sizes[index] * objs_at_a_time[index]);

   i = cmd->index[0];
   do {
      if (objs_to_read > cmd->index[1] - i)
         objs_to_read = cmd->index[1] - i;
      objs_read = chkfread(data, type_sizes[index], objs_to_read, infile);
      for (j = 0; j < objs_read; j++)
         print_funct_ptrs[index] (i + j, data + j * type_sizes[index]);
      /* Just print 1 header for BCPM and WAPP files */
      if (index == BCPMHDR || index == WAPPHDR || index == SPIGOTHDR)
         break;
      i += objs_read;
      if (cmd->pageP) {
         fflush(NULL);
         fprintf(stdout, "\nPress ENTER for next page, or any other key and ");
         fprintf(stdout, "then ENTER to exit.\n\n");
         key = getchar();
      }
   } while (!feof(infile) && i < cmd->index[1] && key == '\n');

   fflush(NULL);
   if (feof(infile)) {
      fprintf(stdout, "\nEnd of file.\n\n");
   }

   free(data);
   fclose(infile);
   exit(0);
}
Example #25
0
int main(int argc, char *argv[])
{
    FILE *fftfile, *candfile = NULL, *psfile = NULL;
    char filenm[100], candnm[100], psfilenm[120];
    float locpow, norm, powargr, powargi;
    float *powr, *spreadpow, *minizoompow, *freqs;
    fcomplex *data, *minifft, *minizoom, *spread;
    fcomplex *resp, *kernel;
    double T, dr, ftobinp;
    int ii, nbins, ncands, candnum, lofreq = 0, nzoom, numsumpow = 1;
    int numbetween, numkern, kern_half_width;
    binaryprops binprops;
    infodata idata;

    if (argc < 3 || argc > 6) {
        usage();
        exit(1);
    }
    printf("\n\n");
    printf("         Binary Candidate Display Routine\n");
    printf("              by Scott M. Ransom\n\n");

    /* Initialize the filenames: */

    sprintf(filenm, "%s.fft", argv[1]);
    sprintf(candnm, "%s_bin.cand", argv[1]);

    /* Read the info file */

    readinf(&idata, argv[1]);
    if (idata.object) {
        printf("Plotting a %s candidate from '%s'.\n", idata.object, filenm);
    } else {
        printf("Plotting a candidate from '%s'.\n", filenm);
    }
    T = idata.N * idata.dt;

    /* Open the FFT file and get its length */

    fftfile = chkfopen(filenm, "rb");
    nbins = chkfilelen(fftfile, sizeof(fcomplex));

    /* Open the candidate file and get its length */

    candfile = chkfopen(candnm, "rb");
    ncands = chkfilelen(candfile, sizeof(binaryprops));

    /* The candidate number to examine */

    candnum = atoi(argv[2]);

    /* Check that candnum is in range */

    if ((candnum < 1) || (candnum > ncands)) {
        printf("\nThe candidate number is out of range.\n\n");
        exit(1);
    }
    /* The lowest freq present in the FFT file */

    if (argc >= 4) {
        lofreq = atoi(argv[3]);
        if ((lofreq < 0) || (lofreq > nbins - 1)) {
            printf("\n'lofreq' is out of range.\n\n");
            exit(1);
        }
    }
    /* Is the original FFT a sum of other FFTs with the amplitudes added */
    /* in quadrature?  (i.e. an incoherent sum)                          */

    if (argc >= 5) {
        numsumpow = atoi(argv[4]);
        if (numsumpow < 1) {
            printf("\nNumber of summed powers must be at least one.\n\n");
            exit(1);
        }
    }
    /* Initialize PGPLOT using Postscript if requested  */

    if ((argc == 6) && (!strcmp(argv[5], "ps"))) {
        sprintf(psfilenm, "%s_bin_cand_%d.ps", argv[1], candnum);
        cpgstart_ps(psfilenm, "landscape");
    } else {
        cpgstart_x("landscape");
    }

    /* Read the binary candidate */

    chkfileseek(candfile, (long) (candnum - 1), sizeof(binaryprops), SEEK_SET);
    chkfread(&binprops, sizeof(binaryprops), 1, candfile);
    fclose(candfile);

    /* Output the binary candidate */

    print_bin_candidate(&binprops, 2);

    /* Allocate some memory */

    powr = gen_fvect(binprops.nfftbins);
    minifft = gen_cvect(binprops.nfftbins / 2);
    spread = gen_cvect(binprops.nfftbins);
    spreadpow = gen_fvect(binprops.nfftbins);
    nzoom = 2 * ZOOMFACT * ZOOMNEIGHBORS;
    minizoom = gen_cvect(nzoom);
    minizoompow = gen_fvect(nzoom);

    /* Allocate and initialize our interpolation kernel */

    numbetween = 2;
    kern_half_width = r_resp_halfwidth(LOWACC);
    numkern = 2 * numbetween * kern_half_width;
    resp = gen_r_response(0.0, numbetween, numkern);
    kernel = gen_cvect(binprops.nfftbins);
    place_complex_kernel(resp, numkern, kernel, binprops.nfftbins);
    COMPLEXFFT(kernel, binprops.nfftbins, -1);

    /* Read the data from the FFT file */

    data = read_fcomplex_file(fftfile, binprops.lowbin - lofreq, binprops.nfftbins);

    /* Turn the Fourier amplitudes into powers */

    for (ii = 0; ii < binprops.nfftbins; ii++)
        powr[ii] = POWER(data[ii].r, data[ii].i);

    /* Chop the powers that are way above the median level */

    prune_powers(powr, binprops.nfftbins, numsumpow);

    /* Perform the minifft */

    memcpy((float *) minifft, powr, sizeof(float) * binprops.nfftbins);
    realfft((float *) minifft, binprops.nfftbins, -1);

    /* Calculate the normalization constant */

    norm = sqrt((double) binprops.nfftbins * (double) numsumpow) / minifft[0].r;
    locpow = minifft[0].r / binprops.nfftbins;

    /* Divide the original power spectrum by the local power level */

    for (ii = 0; ii < binprops.nfftbins; ii++)
        powr[ii] /= locpow;

    /* Now normalize the miniFFT */

    minifft[0].r = 1.0;
    minifft[0].i = 1.0;
    for (ii = 1; ii < binprops.nfftbins / 2; ii++) {
        minifft[ii].r *= norm;
        minifft[ii].i *= norm;
    }

    /* Interpolate the minifft and convert to power spectrum */

    corr_complex(minifft, binprops.nfftbins / 2, RAW,
                 kernel, binprops.nfftbins, FFT,
                 spread, binprops.nfftbins, kern_half_width,
                 numbetween, kern_half_width, CORR);
    for (ii = 0; ii < binprops.nfftbins; ii++)
        spreadpow[ii] = POWER(spread[ii].r, spread[ii].i);

    /* Plot the initial data set */

    freqs = gen_freqs(binprops.nfftbins, binprops.lowbin / T, 1.0 / T);
    xyline(binprops.nfftbins, freqs, powr, "Pulsar Frequency (hz)",
           "Power / Local Power", 1);
    vect_free(freqs);
    printf("The initial data set (with high power outliers removed):\n\n");

    /* Plot the miniFFT */

    freqs = gen_freqs(binprops.nfftbins, 0.0, T / (2 * binprops.nfftbins));
    xyline(binprops.nfftbins, freqs, spreadpow, "Binary Period (sec)",
           "Normalized Power", 1);
    vect_free(freqs);
    printf("The miniFFT:\n\n");

    /* Interpolate and plot the actual candidate peak */

    ftobinp = T / binprops.nfftbins;
    freqs = gen_freqs(nzoom, (binprops.rdetect - ZOOMNEIGHBORS) *
                      ftobinp, ftobinp / (double) ZOOMFACT);
    for (ii = 0; ii < nzoom; ii++) {
        dr = -ZOOMNEIGHBORS + (double) ii / ZOOMFACT;
        rz_interp(minifft, binprops.nfftbins / 2, binprops.rdetect + dr,
                  0.0, kern_half_width, &minizoom[ii]);
        minizoompow[ii] = POWER(minizoom[ii].r, minizoom[ii].i);
    }
    xyline(nzoom, freqs, minizoompow, "Binary Period (sec)", "Normalized Power", 1);
    vect_free(freqs);
    printf("The candidate itself:\n\n");
    printf("Done.\n\n");

    /* Cleanup */

    cpgend();
    vect_free(data);
    vect_free(powr);
    vect_free(resp);
    vect_free(kernel);
    vect_free(minifft);
    vect_free(spread);
    vect_free(spreadpow);
    vect_free(minizoom);
    vect_free(minizoompow);
    fclose(fftfile);
    if ((argc == 6) && (!strcmp(argv[5], "ps"))) {
        fclose(psfile);
    }
    return (0);
}
Example #26
0
int main(int argc, char *argv[])
{
   FILE **infiles, *outfile = NULL, *scalingfile;
   int filenum, argnum = 1, ii = 0, ptsperblock, numlags, numfiles;
   int bytes_per_read, scaling = 0, numscalings, output = 1;
   long long N;
   char *path, *filenm;
   char outfilenm[200], filenmbase[200], scalingnm[200], rawlags[4096];
   unsigned char output_samples[2048];
   float *scalings = NULL;
   double dt, T;
   SPIGOT_INFO *spigots;
   sigprocfb fb;
   infodata idata;

   if (argc == 1) {
      fprintf(stderr, "Usage: spigotSband2filterbank [-stdout] SPIGOT_files\n");
      exit(0);
   }

   if (!strcmp(argv[argnum], "-stdout")) {      /* Use STDOUT */
      argnum++;
      output = 0;
      outfile = stdout;
   } else {
      printf
          ("\nConverting raw SPIGOT S-band FITs data into SIGPROC\n"
           "filterbank format and throwing out the bottom 200MHz...\n\n");
   }

   /* Attempt to read a file with lag scalings in it */
   sprintf(scalingnm, "%s.scaling", filenmbase);
   if ((scalingfile = fopen(scalingnm, "rb"))) {
      /* Determine the length of the file */
      numscalings = (int) chkfilelen(scalingfile, sizeof(float));
      /* Create the array and read 'em */
      scalings = gen_fvect(numscalings);
      chkfread(scalings, sizeof(float), numscalings, scalingfile);
      scaling = 1;
      /* close the scaling file */
      fclose(scalingfile);
      if (outfile != stdout)
         printf("Scaling the lags with the %d values found in '%s'\n\n",
                numscalings, scalingnm);
   }

   /* Read and convert the basic SPIGOT file information */

   numfiles = argc - 1;
   if (outfile == stdout)
      numfiles--;
   else
      printf("Spigot card input file information:\n");
   spigots = (SPIGOT_INFO *) malloc(sizeof(SPIGOT_INFO) * numfiles);
   infiles = (FILE **) malloc(sizeof(FILE *) * numfiles);
   for (filenum = 0; filenum < numfiles; filenum++, argnum++) {
      if (outfile != stdout)
         printf("  '%s'\n", argv[argnum]);
      infiles[filenum] = chkfopen(argv[argnum], "rb");
      read_SPIGOT_header(argv[argnum], spigots + filenum);
      rewind(infiles[filenum]);
   }
   if (outfile != stdout)
      printf("\n");

   /* The following is necessary in order to initialize all the */
   /* static variables in spigot.c                              */
   get_SPIGOT_file_info(infiles, spigots, numfiles, 0, 0, &N,
                        &ptsperblock, &numlags, &dt, &T, &idata, output);

   /* Step through the SPIGOT files */
   ii = 0;
   if (outfile == stdout)
      argnum = 2;
   else
      argnum = 1;
   for (filenum = 0; filenum < numfiles; filenum++, argnum++) {
      split_path_file(argv[argnum], &path, &filenm);
      strncpy(filenmbase, filenm, strlen(filenm) - 5);
      filenmbase[strlen(filenm) - 5] = '\0';
      sprintf(outfilenm, "%s.fil", filenmbase);
      if (outfile != stdout){
         printf("Reading S-band Spigot lags from '%s'\n", argv[argnum]);
         printf("Writing filterbank spectra to   '%s'\n\n", outfilenm);
         outfile = chkfopen(outfilenm, "wb");
      }
      // Update the filterbank header information for each file
      spigot2sigprocfb(&(spigots[filenum]), &fb, filenmbase);
      write_filterbank_header(&fb, outfile);
      chkfseek(infiles[filenum], spigots[filenum].header_len, SEEK_SET);
      bytes_per_read = numlags * spigots[filenum].bits_per_lag / 8;

      /* Loop over the samples in the file */
      while (chkfread(rawlags, bytes_per_read, 1, infiles[filenum])) {
         if (scaling)
            convert_SPIGOT_point(rawlags, output_samples, SUMIFS, scalings[ii]);
         else
            convert_SPIGOT_point(rawlags, output_samples, SUMIFS, 1.0);
         ii++;
         /* Invert the band so that the high freqs are first */
         /* This is how SIGPROC stores its data.             */
         {
            int jj;
            unsigned char tempzz = 0.0, *loptr, *hiptr;
            loptr = output_samples + 0;
            hiptr = output_samples + numlags - 1;
            for (jj = 0; jj < numlags / 2; jj++, loptr++, hiptr--) {
               SWAP(*loptr, *hiptr);
            }
         }
         chkfwrite(output_samples, sizeof(unsigned char), fb.nchans, outfile);
      }
      fclose(infiles[filenum]);
      if (outfile != stdout)
         fclose(outfile);
   }
   if (outfile != stdout)
      fprintf(stderr, "Converted and wrote %d samples.\n\n", ii);
   if (scaling)
      vect_free(scalings);
   free(spigots);
   free(path);
   free(filenm);
   free(infiles);
   return 0;
}
Example #27
0
int read_rzw_cand(FILE * file, fourierprops * cands)
/* Read the next rzw candidate from the file */
/* If successful, return 1, else 0           */
{
   return chkfread(cands, sizeof(fourierprops), 1, file);
}
Example #28
0
int main(int argc, char *argv[])
/* dftfold:  Does complex plane vector addition of a DFT freq */
/* Written by Scott Ransom on 31 Aug 00 based on Ransom and   */
/* Eikenberry paper I (to be completed sometime...).          */
{
   FILE *infile;
   char infilenm[200], outfilenm[200];
   int dataperread;
   unsigned long N;
   double T, rr = 0.0, norm = 1.0;
   dftvector dftvec;
   infodata idata;
   Cmdline *cmd;

   /* Call usage() if we have no command line arguments */

   if (argc == 1) {
      Program = argv[0];
      usage();
      exit(1);
   }

   /* Parse the command line using the excellent program Clig */

   cmd = parseCmdline(argc, argv);

#ifdef DEBUG
   showOptionValues();
#endif

   printf("\n\n");
   printf("        DFT Vector Folding Routine\n");
   printf("            by Scott M. Ransom\n");
   printf("              31 August, 2000\n\n");

   /* Open the datafile and read the info file */

   sprintf(infilenm, "%s.dat", cmd->argv[0]);
   infile = chkfopen(infilenm, "rb");
   readinf(&idata, cmd->argv[0]);

   /* The number of points in datafile */

   N = chkfilelen(infile, sizeof(float));
   dataperread = N / cmd->numvect;
/*   N = cmd->numvect * dataperread; */
   T = N * idata.dt;

   /* Calculate the Fourier frequency */

   if (!cmd->rrP) {
      if (cmd->ffP)
         rr = cmd->ff;
      else if (cmd->ppP)
         rr = T / cmd->pp;
      else {
         printf("\n  You must specify a frequency to fold!  Exiting.\n\n");
      }
   } else
      rr = cmd->rr;

   /* Calculate the amplitude normalization if required */

   if (cmd->normP)
      norm = 1.0 / sqrt(cmd->norm);
   else if (cmd->fftnormP) {
      FILE *fftfile;
      int kern_half_width, fftdatalen, startbin;
      double rrfrac, rrint;
      char fftfilenm[200];
      fcomplex *fftdata;

      sprintf(fftfilenm, "%s.fft", cmd->argv[0]);
      fftfile = chkfopen(fftfilenm, "rb");
      kern_half_width = r_resp_halfwidth(HIGHACC);
      fftdatalen = 2 * kern_half_width + 10;
      rrfrac = modf(rr, &rrint);
      startbin = (int) rrint - fftdatalen / 2;
      fftdata = read_fcomplex_file(fftfile, startbin, fftdatalen);
      norm = 1.0 / sqrt(get_localpower3d(fftdata, fftdatalen,
                                         rrfrac + fftdatalen / 2, 0.0, 0.0));
      vect_free(fftdata);
      fclose(fftfile);
   }

   /* Initialize the dftvector */

   init_dftvector(&dftvec, dataperread, cmd->numvect, idata.dt, rr, norm, T);

   /* Show our folding values */

   printf("\nFolding data from '%s':\n", infilenm);
   printf("   Folding Fourier Freq = %.5f\n", rr);
   printf("      Folding Freq (Hz) = %-.11f\n", rr / T);
   printf("     Folding Period (s) = %-.14f\n", T / rr);
   printf("  Points per sub-vector = %d\n", dftvec.n);
   printf("  Number of sub-vectors = %d\n", dftvec.numvect);
   printf(" Normalization constant = %g\n", norm * norm);

   /* Perform the actual vector addition */

   {
      int ii, jj;
      float *data;
      double real, imag, sumreal = 0.0, sumimag = 0.0;
      double theta, aa, bb, cc, ss, dtmp;
      double powargr, powargi, phsargr, phsargi, phstmp;

      data = gen_fvect(dftvec.n);
      theta = -TWOPI * rr / (double) N;
      dtmp = sin(0.5 * theta);
      aa = -2.0 * dtmp * dtmp;
      bb = sin(theta);
      cc = 1.0;
      ss = 0.0;
      for (ii = 0; ii < dftvec.numvect; ii++) {
         chkfread(data, sizeof(float), dftvec.n, infile);
         real = 0.0;
         imag = 0.0;
         for (jj = 0; jj < dftvec.n; jj++) {
            real += data[jj] * cc;
            imag += data[jj] * ss;
            cc = aa * (dtmp = cc) - bb * ss + cc;
            ss = aa * ss + bb * dtmp + ss;
         }
         dftvec.vector[ii].r = norm * real;
         dftvec.vector[ii].i = norm * imag;
         sumreal += dftvec.vector[ii].r;
         sumimag += dftvec.vector[ii].i;
      }
      vect_free(data);
      printf("\nDone:\n");
      printf("             Vector sum = %.3f + %.3fi\n", sumreal, sumimag);
      printf("      Total phase (deg) = %.2f\n", PHASE(sumreal, sumimag));
      printf("            Total power = %.2f\n", POWER(sumreal, sumimag));
      printf("\n");
   }
   fclose(infile);

   /* Write the output structure */

   sprintf(outfilenm, "%s_%.3f.dftvec", cmd->argv[0], rr);
   write_dftvector(&dftvec, outfilenm);

   /* Free our vector and return */

   free_dftvector(&dftvec);
   return (0);
}
Example #29
0
void create_accelobs(accelobs * obs, infodata * idata, Cmdline * cmd, int usemmap)
{
   int ii, rootlen, input_shorts = 0;

   {
      int hassuffix = 0;
      char *suffix;

      hassuffix = split_root_suffix(cmd->argv[0], &(obs->rootfilenm), &suffix);
      if (hassuffix) {
         if (strcmp(suffix, "fft") != 0 &&
             strcmp(suffix, "dat") != 0 && strcmp(suffix, "sdat") != 0) {
            printf("\nInput file ('%s') must be an '.fft' or '.[s]dat' file!\n\n",
                   cmd->argv[0]);
            free(suffix);
            exit(0);
         }
         /* If the input file is a time series */
         if (strcmp(suffix, "dat") == 0 || strcmp(suffix, "sdat") == 0) {
            obs->dat_input = 1;
            obs->mmap_file = 0;
            if (strcmp(suffix, "sdat") == 0)
               input_shorts = 1;
         } else {
            obs->dat_input = 0;
         }
         free(suffix);
      } else {
         printf("\nInput file ('%s') must be an '.fft' or '.[s]dat' file!\n\n",
                cmd->argv[0]);
         exit(0);
      }
   }

   if (cmd->noharmpolishP)
       obs->use_harmonic_polishing = 0;
   else
       obs->use_harmonic_polishing = 1;  // now default

   /* Read the info file */

   readinf(idata, obs->rootfilenm);
   if (idata->object) {
      printf("Analyzing %s data from '%s'.\n\n",
             remove_whitespace(idata->object), cmd->argv[0]);
   } else {
      printf("Analyzing data from '%s'.\n\n", cmd->argv[0]);
   }

   /* Prepare the input time series if required */

   if (obs->dat_input) {
      FILE *datfile;
      long long filelen;
      float *ftmp;

      printf("Reading and FFTing the time series...");
      fflush(NULL);
      datfile = chkfopen(cmd->argv[0], "rb");

      /* Check the length of the file to see if we can handle it */
      filelen = chkfilelen(datfile, sizeof(float));
      if (input_shorts)
         filelen *= 2;
      if (filelen > 67108864) { /* Small since we need memory for the templates */
         printf("\nThe input time series is too large.  Use 'realfft' first.\n\n");
         exit(0);
      }

      /* Read the time series into a temporary buffer */
      /* Note:  The padding allows us to search very short time series */
      /*        using correlations without having to worry about       */
      /*        accessing data before or after the valid FFT freqs.    */
      if (input_shorts) {
         short *stmp = gen_svect(filelen);
         ftmp = gen_fvect(filelen+2*ACCEL_PADDING);
         for (ii = 0; ii < ACCEL_PADDING; ii++) {
             ftmp[ii] = 0.0;
             ftmp[ii+filelen+ACCEL_PADDING] = 0.0;
         }
         chkfread(stmp, sizeof(short), filelen, datfile);
         for (ii = 0; ii < filelen; ii++)
            ftmp[ii+ACCEL_PADDING] = (float) stmp[ii];
         free(stmp);
      } else {
         ftmp = read_float_file(datfile, -ACCEL_PADDING, filelen+2*ACCEL_PADDING);
      }
      /* Now, offset the pointer so that we are pointing at the first */
      /* bits of valid data.                                          */
      ftmp += ACCEL_PADDING;
      fclose(datfile);

      /* FFT it */
      realfft(ftmp, filelen, -1);
      obs->fftfile = NULL;
      obs->fft = (fcomplex *) ftmp;
      obs->numbins = filelen / 2;
      printf("done.\n");

      /* De-redden it */
      printf("Removing red-noise...");
      deredden(obs->fft, obs->numbins);
      printf("done.\n\n");
   }

   /* Determine the output filenames */

   rootlen = strlen(obs->rootfilenm) + 25;
   obs->candnm = (char *) calloc(rootlen, 1);
   obs->accelnm = (char *) calloc(rootlen, 1);
   obs->workfilenm = (char *) calloc(rootlen, 1);
   sprintf(obs->candnm, "%s_ACCEL_%d.cand", obs->rootfilenm, cmd->zmax);
   sprintf(obs->accelnm, "%s_ACCEL_%d", obs->rootfilenm, cmd->zmax);
   sprintf(obs->workfilenm, "%s_ACCEL_%d.txtcand", obs->rootfilenm, cmd->zmax);

   /* Open the FFT file if it exists appropriately */
   if (!obs->dat_input) {
      obs->fftfile = chkfopen(cmd->argv[0], "rb");
      obs->numbins = chkfilelen(obs->fftfile, sizeof(fcomplex));
      if (usemmap) {
         fclose(obs->fftfile);
         obs->fftfile = NULL;
         printf("Memory mapping the input FFT.  This may take a while...\n");
         obs->mmap_file = open(cmd->argv[0], O_RDONLY);
         if (obs->mmap_file == -1) {
            perror("\nError in open() in accel_utils.c");
            printf("\n");
            exit(-1);
         }
         obs->fft = (fcomplex *) mmap(0, sizeof(fcomplex) * obs->numbins, PROT_READ,
                                      MAP_SHARED, obs->mmap_file, 0);
         if (obs->fft == MAP_FAILED) {
            perror("\nError in mmap() in accel_utils.c");
            printf("Falling back to a non-mmaped approach\n");
            obs->fftfile = chkfopen(cmd->argv[0], "rb");
            obs->mmap_file = 0;
         }
      } else {
         obs->mmap_file = 0;
      }
   }

   /* Determine the other parameters */

   if (cmd->zmax % ACCEL_DZ)
      cmd->zmax = (cmd->zmax / ACCEL_DZ + 1) * ACCEL_DZ;
   if (!obs->dat_input)
      obs->workfile = chkfopen(obs->workfilenm, "w");
   obs->N = (long long) idata->N;
   if (cmd->photonP) {
      if (obs->mmap_file || obs->dat_input) {
         obs->nph = obs->fft[0].r;
      } else {
         obs->nph = get_numphotons(obs->fftfile);
      }
      printf("Normalizing powers using %.0f photons.\n\n", obs->nph);
   } else {
      obs->nph = 0.0;
      /* For short FFTs insure that we don't pick up the DC */
      /* or Nyquist component as part of the interpolation  */
      /* for higher frequencies.                            */
      if (cmd->locpowP) {
          obs->norm_type = 1;
          printf("Normalizing powers using local-power determination.\n\n");
      } else if (cmd->medianP) {
          obs->norm_type = 0;
          printf("Normalizing powers using median-blocks.\n\n");
      } else {
          obs->norm_type = 0;
          printf("Normalizing powers using median-blocks (default).\n\n");
      }
      if (obs->dat_input) {
         obs->fft[0].r = 1.0;
         obs->fft[0].i = 1.0;
      }
   }
   obs->lobin = cmd->lobin;
   if (obs->lobin > 0) {
      obs->nph = 0.0;
      if (cmd->lobin > obs->numbins - 1) {
         printf("\n'lobin' is greater than the total number of\n");
         printf("   frequencies in the data set.  Exiting.\n\n");
         exit(1);
      }
   }
   if (cmd->numharm != 1 &&
       cmd->numharm != 2 &&
       cmd->numharm != 4 && cmd->numharm != 8 && cmd->numharm != 16) {
      printf("\n'numharm' = %d must be a power-of-two!  Exiting\n\n", cmd->numharm);
      exit(1);
   }
   obs->numharmstages = twon_to_index(cmd->numharm) + 1;
   obs->dz = ACCEL_DZ;
   obs->numz = cmd->zmax * 2 + 1;
   obs->numbetween = ACCEL_NUMBETWEEN;
   obs->dt = idata->dt;
   obs->T = idata->dt * idata->N;
   if (cmd->floP) {
      obs->rlo = floor(cmd->flo * obs->T);
      if (obs->rlo < obs->lobin)
         obs->rlo = obs->lobin;
      if (obs->rlo > obs->numbins - 1) {
         printf("\nLow frequency to search 'flo' is greater than\n");
         printf("   the highest available frequency.  Exiting.\n\n");
         exit(1);
      }
   } else {
      if (cmd->rloP)
         obs->rlo = cmd->rlo;
      else
         obs->rlo = 1.0;
      if (obs->rlo < obs->lobin)
         obs->rlo = obs->lobin;
      if (obs->rlo > obs->numbins - 1) {
         printf("\nLow frequency to search 'rlo' is greater than\n");
         printf("   the available number of points.  Exiting.\n\n");
         exit(1);
      }
   }
   obs->highestbin = obs->numbins - 1;
   if (cmd->fhiP) {
      obs->highestbin = ceil(cmd->fhi * obs->T);
      if (obs->highestbin > obs->numbins - 1)
         obs->highestbin = obs->numbins - 1;
      obs->rhi = obs->highestbin;
      if (obs->highestbin < obs->rlo) {
         printf("\nHigh frequency to search 'fhi' is less than\n");
         printf("   the lowest frequency to search 'flo'.  Exiting.\n\n");
         exit(1);
      }
   } else if (cmd->rhiP) {
      obs->highestbin = cmd->rhi;
      if (obs->highestbin > obs->numbins - 1)
         obs->highestbin = obs->numbins - 1;
      obs->rhi = obs->highestbin;
      if (obs->highestbin < obs->rlo) {
         printf("\nHigh frequency to search 'rhi' is less than\n");
         printf("   the lowest frequency to search 'rlo'.  Exiting.\n\n");
         exit(1);
      }
   }
   obs->dr = ACCEL_DR;
   obs->zhi = cmd->zmax;
   obs->zlo = -cmd->zmax;
   obs->sigma = cmd->sigma;
   obs->powcut = (float *) malloc(obs->numharmstages * sizeof(float));
   obs->numindep = (long long *) malloc(obs->numharmstages * sizeof(long long));
   for (ii = 0; ii < obs->numharmstages; ii++) {
      if (obs->numz == 1)
         obs->numindep[ii] = (obs->rhi - obs->rlo) / index_to_twon(ii);
      else
         /* The numz+1 takes care of the small amount of  */
         /* search we get above zmax and below zmin.      */
         obs->numindep[ii] = (obs->rhi - obs->rlo) * (obs->numz + 1) *
             (obs->dz / 6.95) / index_to_twon(ii);
      obs->powcut[ii] = power_for_sigma(obs->sigma,
                                        index_to_twon(ii), obs->numindep[ii]);
   }
   obs->numzap = 0;
   /*
      if (zapfile!=NULL)
      obs->numzap = get_birdies(cmd->zapfile, obs->T, obs->baryv, 
      &(obs->lobins), &(obs->hibins));
      else
      obs->numzap = 0;
    */
}
Example #30
0
int main(int argc, char *argv[])
{
   FILE *infile, *outfile;
   int ii, jj, bufflen = 10000, numread;
   long long N = 0;
   float *inbuffer = NULL, *outbuffer = NULL;
   short useshorts = 0, *sinbuffer = NULL, *soutbuffer = NULL;
   char *rootfilenm, *outname;
   infodata idata;
   Cmdline *cmd;

   /* Call usage() if we have no command line arguments */

   if (argc == 1) {
      Program = argv[0];
      printf("\n");
      usage();
      exit(1);
   }

   /* Parse the command line using the excellent program Clig */

   cmd = parseCmdline(argc, argv);

#ifdef DEBUG
   showOptionValues();
#endif

   printf("\n\n");
   printf("     Time Series Downsampling Routine\n");
   printf("               Sept, 2002\n\n");

   {
      int hassuffix = 0;
      char *suffix;

      hassuffix = split_root_suffix(cmd->argv[0], &rootfilenm, &suffix);
      if (hassuffix) {
         if (strcmp(suffix, "sdat") == 0)
            useshorts = 1;
         if (strcmp(suffix, "dat") != 0 && strcmp(suffix, "sdat") != 0) {
            printf
                ("\nInput file ('%s') must be a time series ('.dat' or '.sdat')!\n\n",
                 cmd->argv[0]);
            free(suffix);
            exit(0);
         }
         free(suffix);
      } else {
         printf("\nInput file ('%s') must be a time series ('.dat' or '.sdat')!\n\n",
                cmd->argv[0]);
         exit(0);
      }
      if (cmd->outfileP) {
         outname = cmd->outfile;
      } else {
         outname = (char *) calloc(strlen(rootfilenm) + 11, sizeof(char));
         if (useshorts)
            sprintf(outname, "%s_D%d.sdat", rootfilenm, cmd->factor);
         else
            sprintf(outname, "%s_D%d.dat", rootfilenm, cmd->factor);
      }
   }

   /* Read the info file */

   readinf(&idata, rootfilenm);
   if (idata.object) {
      printf("Downsampling %s data from '%s'.\n\n",
             remove_whitespace(idata.object), cmd->argv[0]);
   } else {
      printf("Downsampling data from '%s'.\n\n", cmd->argv[0]);
   }

   /* Open files and create arrays */

   infile = chkfopen(argv[1], "rb");
   outfile = chkfopen(outname, "wb");

   /* Read and downsample */

   if (useshorts) {
      sinbuffer = gen_svect(bufflen * cmd->factor);
      soutbuffer = gen_svect(bufflen);
      while ((numread =
              chkfread(sinbuffer, sizeof(short), bufflen * cmd->factor, infile))) {
         for (ii = 0; ii < numread / cmd->factor; ii++) {
            soutbuffer[ii] = 0;
            for (jj = 0; jj < cmd->factor; jj++)
               soutbuffer[ii] += sinbuffer[cmd->factor * ii + jj];
         }
         chkfwrite(soutbuffer, sizeof(short), numread / cmd->factor, outfile);
         N += numread / cmd->factor;
      }
      vect_free(sinbuffer);
      vect_free(soutbuffer);
   } else {
      inbuffer = gen_fvect(bufflen * cmd->factor);
      outbuffer = gen_fvect(bufflen);
      while ((numread =
              chkfread(inbuffer, sizeof(float), bufflen * cmd->factor, infile))) {
         for (ii = 0; ii < numread / cmd->factor; ii++) {
            outbuffer[ii] = 0;
            for (jj = 0; jj < cmd->factor; jj++)
               outbuffer[ii] += inbuffer[cmd->factor * ii + jj];
         }
         chkfwrite(outbuffer, sizeof(float), numread / cmd->factor, outfile);
         N += numread / cmd->factor;
      }
      vect_free(inbuffer);
      vect_free(outbuffer);
   }
   printf("Done.  Wrote %lld points.\n\n", N);

   /* Write the new info file */

   idata.dt = idata.dt * cmd->factor;
   idata.numonoff = 0;
   idata.N = (double) N;
   strncpy(idata.name, outname, strlen(outname) - 4);
   if (useshorts)
      idata.name[strlen(outname) - 5] = '\0';
   else
      idata.name[strlen(outname) - 4] = '\0';
   writeinf(&idata);

   fclose(infile);
   fclose(outfile);
   free(rootfilenm);
   if (!cmd->outfileP)
      free(outname);
   exit(0);
}