Beispiel #1
0
int prune_powers(float *arr, int n, int numsumpows)
/* Sets powers that are more than approx PRUNELEV standard */
/* devs above the median value to NEWLEV times the median  */
/* value.  This helps 'clean' the spectrum of high power   */
/* signals that probably have nothing to do with a phase   */
/* modulation spectrum (i.e. they are RF noise or strong   */
/* solitary pulsars.                                       */
{
    int ii, ct = 0;
    float med, cutoff, *tmparr;

    /* Determine the median power */

    tmparr = gen_fvect(n);
    memcpy(tmparr, arr, sizeof(float) * n);
    med = median(tmparr, n);
    vect_free(tmparr);

    /* Throw away powers that are bigger that PRUNELEV * median */

    cutoff = med * PRUNELEV / sqrt((float) numsumpows);
    for (ii = 0; ii < n; ii++) {
        if (arr[ii] > cutoff) {
            arr[ii] = NEWLEV * med;
            ct++;
        }
    }
    return ct;
}
void write_padding(FILE * outfiles[], int numfiles, float value, int numtowrite)
{
    int ii;

    if (numtowrite <= 0) {
        return;
    } else if (numtowrite == 1) {
        for (ii = 0; ii < numfiles; ii++)
            chkfwrite(&value, sizeof(float), 1, outfiles[ii]);
    } else {
        int maxatonce = 8192, veclen, jj;
        float *buffer;
        veclen = (numtowrite > maxatonce) ? maxatonce : numtowrite;
        buffer = gen_fvect(veclen);
        for (ii = 0; ii < veclen; ii++)
            buffer[ii] = value;
        if (veclen == numtowrite) {
            for (ii = 0; ii < numfiles; ii++)
                chkfwrite(buffer, sizeof(float), veclen, outfiles[ii]);
        } else {
            for (ii = 0; ii < numtowrite / veclen; ii++) {
                for (jj = 0; jj < numfiles; jj++)
                    chkfwrite(buffer, sizeof(float), veclen, outfiles[jj]);
            }
            for (jj = 0; jj < numfiles; jj++)
                chkfwrite(buffer, sizeof(float), numtowrite % veclen, outfiles[jj]);
        }
        vect_free(buffer);
    }
}
Beispiel #3
0
void calc_avgmedstd(float *arr, int numarr, float fraction,
                    int step, float *avg, float *med, float *std)
/* Calculates the median and middle-'fraction' std deviation  */
/* and average of the array 'arr'.  Values are returned in    */
/* 'avg', 'med' and 'std'.  The array is not modified.        */
{
   int ii, jj, len, start;
   float *tmparr;
   double davg, dstd;

   len = (int) (numarr * fraction + 0.5);
   if (len > numarr || len < 0) {
      printf("fraction (%g) out-of-bounds in calc_avgmedstd()\n", fraction);
      exit(1);
   }
   start = (numarr - len) / 2;
   tmparr = gen_fvect(numarr);
   for (ii = 0, jj = 0; ii < numarr; ii++, jj += step)
      tmparr[ii] = arr[jj];
   qsort(tmparr, numarr, sizeof(float), compare_floats);
   avg_var(tmparr + start, len, &davg, &dstd);
   *avg = (float) davg;
   *med = tmparr[numarr / 2];
   *std = sqrt(dstd);
   vect_free(tmparr);
}
Beispiel #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;
    }
}
Beispiel #5
0
static fftpart *get_fftpart(int rlo, int numr)
{
   int ii, jj, index;
   float powargr, powargi, tmppwr, chunk[LOCALCHUNK];
   fftpart *fp;

   if (rlo + numr > Nfft) {
      return NULL;
   } else {
      fp = (fftpart *) malloc(sizeof(fftpart));
      fp->rlo = rlo;
      fp->numamps = numr;
#ifdef USEMMAP
      fp->amps = (fcomplex *) mmap(0, sizeof(fcomplex) * numr, PROT_READ,
                                   MAP_SHARED, mmap_file, 0);
#else
      fp->amps = read_fcomplex_file(fftfile, rlo, numr);
#endif
      if (rlo == 0)
         r0 = fp->amps[0].r;
      fp->rawpowers = gen_fvect(fp->numamps);
      fp->medians = gen_fvect(fp->numamps / LOCALCHUNK);
      fp->normvals = gen_fvect(fp->numamps / LOCALCHUNK);
      fp->maxrawpow = 0.0;
      for (ii = 0; ii < fp->numamps / LOCALCHUNK; ii++) {
         index = ii * LOCALCHUNK;
         for (jj = 0; jj < LOCALCHUNK; jj++, index++) {
            tmppwr = POWER(fp->amps[index].r, fp->amps[index].i);
            if (tmppwr > fp->maxrawpow)
               if (rlo || (rlo == 0 && index > 0))
                  fp->maxrawpow = tmppwr;
            fp->rawpowers[index] = tmppwr;
            chunk[jj] = tmppwr;
         }
         if (rlo == 0 && ii == 0) {
            chunk[0] = 1.0;
            fp->rawpowers[0] = 1.0;
         }
         fp->medians[ii] = median(chunk, LOCALCHUNK);
         fp->normvals[ii] = 1.0 / (1.4426950408889634 * fp->medians[ii]);
      }
      return fp;
   }
}
Beispiel #6
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;
      }
   }
}
Beispiel #7
0
void plot_profile(int proflen, float *profile, const char *title,
                  const char *probtxt, const char *foldtxt,
                  int showerr, float *errors, int showid)
{
    int ii;
    float *x, overy, ymin, ymax;
    float errmin = 0.0, errmax = 0.0, offset, avg = 0.0, av[2];

    find_min_max_arr(proflen, profile, &ymin, &ymax);
    if (showerr)
        find_min_max_arr(proflen, errors, &errmin, &errmax);
    overy = 0.1 * (ymax + errmax - ymin - errmin);
    ymax = ymax + overy + errmax;
    ymin = ymin - overy - errmin;
    x = gen_fvect(proflen);
    for (ii = 0; ii < proflen; ii++)
        x[ii] = (float) ii / (float) proflen;
    cpgenv(0.0, 1.00001, ymin, ymax, 0, 0);
    cpgscf(2);
    cpglab("Pulse Phase", "Counts", "");
    if (showid)
        cpgiden();
    cpgslw(5);
    if (showerr) {
        cpgbin(proflen, x, profile, 0);
    } else {
        cpgline(proflen, x, profile);
    }
    cpgslw(1);
    if (showerr) {
        offset = 0.5 / (float) proflen;
        for (ii = 0; ii < proflen; ii++)
            x[ii] += offset;
        cpgerrb(6, proflen, x, profile, errors, 2);
        cpgpt(proflen, x, profile, 5);
    }
    for (ii = 0; ii < proflen; ii++)
        avg += profile[ii];
    avg /= proflen;
    cpgsls(4);
    x[0] = 0.0;
    x[1] = 1.0;
    av[0] = avg;
    av[1] = avg;
    cpgline(2, x, av);
    cpgsls(1);
    cpgsch(1.3);
    cpgmtxt("T", +2.0, 0.5, 0.5, title);
    cpgsch(1.0);
    cpgmtxt("T", +0.8, 0.5, 0.5, foldtxt);
    cpgmtxt("T", -1.5, 0.5, 0.5, probtxt);
    vect_free(x);
}
Beispiel #8
0
static void plot_rfi(rfi * plotrfi, float top, int numint, int numchan,
                     float T, float lof, float hif)
{
   int ii;
   float period, perioderr, dy = 0.035, *temparr;
   float tr[6] = { -0.5, 1.0, 0.0, -0.5, 0.0, 1.0 };
   char temp[40];

   if (plotrfi->freq_avg == 0.0)
      period = 0.0;
   else
      period = 1000.0 / plotrfi->freq_avg;
   if (plotrfi->freq_var == 0.0)        /* Why are these zero? */
      perioderr = 0.0;
   else
      perioderr = 1000.0 * sqrt(plotrfi->freq_var) /
          (plotrfi->freq_avg * plotrfi->freq_avg);
   cpgsvp(0.0, 1.0, 0.0, 1.0);
   cpgswin(0.0, 1.0, 0.0, 1.0);
   cpgnice_output_2(temp, plotrfi->freq_avg, sqrt(plotrfi->freq_var), 0);
   cpgptxt(0.03, top - 0.6 * dy, 0.0, 0.0, temp);
   cpgnice_output_2(temp, period, perioderr, 0);
   cpgptxt(0.12, top - 0.6 * dy, 0.0, 0.0, temp);
   sprintf(temp, "%-5.2f", plotrfi->sigma_avg);
   cpgptxt(0.21, top - 0.6 * dy, 0.0, 0.0, temp);
   sprintf(temp, "%d", plotrfi->numobs);
   cpgptxt(0.27, top - 0.6 * dy, 0.0, 0.0, temp);
   ii = (numint > numchan) ? numint : numchan;
   temparr = gen_fvect(ii);
   for (ii = 0; ii < numchan; ii++)
      temparr[ii] = GET_BIT(plotrfi->chans, ii);
   cpgsvp(0.33, 0.64, top - dy, top);
   cpgswin(0.0, numchan, 0.0, 1.0);
   cpgimag(temparr, numchan, 1, 1, numchan, 1, 1, 0.0, 1.0, tr);
   cpgswin(0.0, numchan, 0.0, 1.0);
   cpgbox("BST", 0.0, 0, "BC", 0.0, 0);
   cpgswin(lof, hif, 0.0, 1.0);
   cpgbox("CST", 0.0, 0, "", 0.0, 0);
   for (ii = 0; ii < numint; ii++)
      temparr[ii] = GET_BIT(plotrfi->times, ii);
   cpgsvp(0.65, 0.96, top - dy, top);
   cpgswin(0.0, numint, 0.0, 1.0);
   cpgimag(temparr, numint, 1, 1, numint, 1, 1, 0.0, 1.0, tr);
   cpgswin(0.0, numint, 0.0, 1.0);
   cpgbox("BST", 0.0, 0, "BC", 0.0, 0);
   cpgswin(0.0, T, 0.0, 1.0);
   cpgbox("CST", 0.0, 0, "", 0.0, 0);
   vect_free(temparr);
}
Beispiel #9
0
float *gen_freqs(long numfreqs, double lof, double df)
/* This routine generates a float vector of length numfreqs */
/* with values set to lof, lof+df, lof+2df, ...             */
/* It is normally used when generating a list of freqs      */
/* for an x-y plot of spectral data.                        */
{
   long i;
   float *freqs;

   freqs = gen_fvect(numfreqs);
   for (i = 0; i < numfreqs; i++) {
      freqs[i] = lof + i * df;
   }
   return freqs;
}
Beispiel #10
0
float estimate_offpulse_redchi2(double *inprofs, foldstats *stats,
                                int numparts, int numsubbands, 
                                int proflen, int numtrials, double dofeff)
// Randomly offset each pulse profile in a .pfd data square or cube
// and combine them to estimate a "true" off-pulse level.  Do this
// numtrials times in order to improve the statistics.  Return the
// inverse of the average of the off-pulse reduced-chi^2 (i.e. the
// correction factor).  dofeff is the effective number of DOF as
// returned by DOF_corr().
{
    int ii, jj, kk, offset, trialnum, phsindex, statindex;
    float *chis;
    double chi_avg, chi_var, redchi;
    double prof_avg, prof_var, *prof_ptr, *sumprof;

    sumprof = gen_dvect(proflen);
    chis = gen_fvect(numtrials);

    for (trialnum = 0; trialnum < numtrials; trialnum++) {
        // Initialize the summed profile
        for (ii = 0; ii < proflen; ii++)
            sumprof[ii] = 0.0;
        prof_avg = 0.0;
        prof_var = 0.0;
        prof_ptr = inprofs;
        for (ii = 0; ii < numparts; ii++) {  // parts
            for (jj = 0; jj < numsubbands; jj++) {  // subbands
                statindex = ii * numsubbands + jj;
                offset = random() % proflen;
                phsindex = 0;
                for (kk = offset; kk < proflen; kk++, phsindex++) // phases
                    sumprof[phsindex] += prof_ptr[kk];
                for (kk = 0; kk < offset; kk++, phsindex++) // phases
                    sumprof[phsindex] += prof_ptr[kk];
                prof_ptr += proflen;
                prof_avg += stats[statindex].prof_avg;
                prof_var += stats[statindex].prof_var;
            }
        }
        /* Calculate the current chi-squared */
        redchi = chisqr(sumprof, proflen, prof_avg, prof_var) / dofeff;
        chis[trialnum] = (float) redchi;
    }
    avg_var(chis, numtrials, &chi_avg, &chi_var);
    vect_free(chis);
    vect_free(sumprof);
    return 1.0/chi_avg;
}
Beispiel #11
0
int read_subbands(float *fdata, int *delays, int numsubbands,
                  struct spectra_info *s, int transpose, int *padding,
                  int *maskchans, int *nummasked, mask * obsmask)
// This routine reads a spectral block/subint from the input raw data
// files. The routine uses dispersion delays in 'delays' to
// de-disperse the data into 'numsubbands' subbands.  It stores the
// resulting data in vector 'fdata' of length 'numsubbands' *
// 's->spectra_per_subint'.  The low freq subband is stored first,
// then the next highest subband etc, with 's->spectra_per_subint'
// floating points per subband.  It returns the # of points read if
// successful, 0 otherwise. If padding is returned as 1, then padding
// was added and statistics should not be calculated.  'maskchans' is
// an array of length numchans which contains a list of the number of
// channels that were masked.  The # of channels masked is returned in
// 'nummasked'.  'obsmask' is the mask structure to use for masking.
// If 'transpose'==0, the data will be kept in time order instead of
// arranged by subband as above.
{
   static int firsttime = 1;
   static float *frawdata;

   if (firsttime) {
       // Needs to be twice as large for buffering if adding observations together
       frawdata = gen_fvect(2 * s->num_channels * s->spectra_per_subint);
       if (!s->get_rawblock(frawdata, s, padding)) {
           fprintf(stderr, "Error: problem reading the raw data file in read_subbands()\n");
           return 0;
       }
       if (0 != prep_subbands(fdata, frawdata, delays, numsubbands, s,
                              transpose, maskchans, nummasked, obsmask)) {
           fprintf(stderr, "Error: problem initializing prep_subbands() in read_subbands()\n");
           return 0;
       }
       firsttime = 0;
   }
   if (!s->get_rawblock(frawdata, s, padding)) {
       return 0;
   }
   if (prep_subbands(fdata, frawdata, delays, numsubbands, s, transpose,
                     maskchans, nummasked, obsmask)==s->spectra_per_subint) {
       currentspectra += s->spectra_per_subint;
       return s->spectra_per_subint;
   } else {
       return 0;
   }
}
Beispiel #12
0
void rotate_1d(float *data, long numbins, long bins_to_left)
/* Rotates a vector by bins_to_left places to the left.    */
/* numbins is the number of FLOATING points to move.       */
/* frotate is better.  Use it.                        */
{
   float *tmp;

   if (bins_to_left < 0 || bins_to_left >= numbins) {
      printf("\nNumber of bins to rotate array in rotate_1d is\n");
      printf("\nout of bounds.  Tried to rotate %ld bins.  Exiting.\n",
             bins_to_left);
      exit(1);
   }
   tmp = gen_fvect(bins_to_left);
   memcpy(tmp, data, sizeof(float) * bins_to_left);
   memmove(data, data + bins_to_left, sizeof(float) * (numbins - bins_to_left));
   memcpy(data + bins_to_left, tmp, sizeof(float) * bins_to_left);
   free(tmp);
}
Beispiel #13
0
void frotate(float *data, long numbins, float bins_to_left)
/* Rotates a vector by bins_to_left places to the left.    */
/* numbins is the number of FLOATING points to move.       */
{
   float *tmp;
   double lopart, hipart, intpart;
   long i, index;

   bins_to_left = fmod(bins_to_left, (double) numbins);
   if (bins_to_left < 0.0)
      bins_to_left += numbins;
   tmp = gen_fvect(numbins);
   lopart = modf(bins_to_left, &intpart);
   hipart = 1.0 - lopart;
   index = (long) floor(intpart + 1.0E-20);
   for (i = 0; i < numbins; i++)
      tmp[i] = hipart * data[(index + i) % numbins] +
          lopart * data[(index + i + 1) % numbins];
   memcpy(data, tmp, sizeof(float) * numbins);
   free(tmp);
}
Beispiel #14
0
int read_rawblocks(float *fdata, int numsubints, struct spectra_info *s, int *padding)
// This routine reads numsubints rawdata blocks from raw radio pulsar
// data. The floating-point filterbank data is returned in rawdata
// which must have a size of numsubints * s->samples_per_subint.  The
// number of blocks read is returned.  If padding is returned as 1,
// then padding was added and statistics should not be calculated.
{
    int ii, retval = 0, gotblock = 0, pad = 0, numpad = 0, numvals;
    static float *rawdata = NULL;
    static int firsttime = 1;

    numvals = s->spectra_per_subint * s->num_channels;
    if (firsttime) {
        // Needs to be twice as large for buffering if adding observations together
        rawdata = gen_fvect(2 * numvals);
        firsttime = 0;
    }
    *padding = 0;
    for (ii = 0; ii < numsubints; ii++) {
        gotblock = s->get_rawblock(rawdata, s, &pad);
        if (gotblock==0) break;
        retval += gotblock;
        memcpy(fdata + ii * numvals, rawdata, numvals * sizeof(float));
        if (pad) numpad++;
    }
    if (gotblock==0) {  // Now fill the rest of the data with padding
        for (; ii < numsubints; ii++) {
            int jj;
            for (jj = 0; jj < s->spectra_per_subint; jj++)
                memcpy(fdata + ii * numvals + jj * s->num_channels, 
                       s->padvals, s->num_channels * sizeof(float));
        }
        numpad++;
    }

    /* Return padding 'true' if any block was padding */
    if (numpad) *padding = 1;
    return retval;
}
Beispiel #15
0
static basicstats *calc_stats(dataview * dv, datapart * dp)
{
   int ii, jj;
   float *tmpdata;
   basicstats *tmpstats;

   tmpstats = (basicstats *) malloc(sizeof(basicstats));
   tmpstats->max = SMALLNUM;
   tmpstats->min = LARGENUM;
   tmpdata = gen_fvect(dv->numsamps);
   for (ii = 0, jj = dv->lon - dp->nlo; ii < dv->numsamps; ii++, jj++) {
      tmpdata[ii] = dp->data[jj];
      if (tmpdata[ii] > tmpstats->max)
         tmpstats->max = tmpdata[ii];
      if (tmpdata[ii] < tmpstats->min)
         tmpstats->min = tmpdata[ii];
   }
   stats(tmpdata, dv->numsamps, &tmpstats->average, &tmpstats->stdev,
         &tmpstats->skewness, &tmpstats->kurtosis);
   tmpstats->stdev = sqrt(tmpstats->stdev);
   tmpstats->median = median(tmpdata, dv->numsamps);
   vect_free(tmpdata);
   return tmpstats;
}
Beispiel #16
0
float *periodogram(double *xx, double *tt, int nn, 
                   double lof, double df, int numf)
/* Return the normalized Lomb-Scargle Periodogram powers of 'numf'
   frequencies (Hz) from the lowest freq 'lof' upwards by stepsize
   'df'.  There are 'nn' input data points with amplitudes 'xx' and
   times 'tt' (s).  The returned power vector is dynamically
   allocated.  */
{
    int ii, jj;
    float *pows;
    double avg, var, ivar, c, cc, cwtau;
    double s, ss, sumc, sumcxx, sums, sumsh, sumsxx,swtau;
    double wtau, ttavg, ttmax, ttmin;
    double arg, wtemp, *xxnorm, *wi, *wpi, *wpr, *wr;
    
    /* Set-up */
    davg_dvar(xx, nn, &avg, &var);
    if (var==0.0)
        ivar = 0.5;
    else
        ivar = 0.5 / var;
    wr = gen_dvect(nn);
    wi = gen_dvect(nn);
    wpr = gen_dvect(nn);
    wpi = gen_dvect(nn);
    xxnorm = gen_dvect(nn);
    pows = gen_fvect(numf);
    
    /* Scale the times around the midpt */
    ttmax = ttmin = tt[0];
    for (ii = 0; ii < nn; ii++){
        if (tt[ii] > ttmax) ttmax = tt[ii];
        if (tt[ii] < ttmin) ttmin = tt[ii];
    }
    ttavg = 0.5 * (ttmax + ttmin);
    
    /* Generate the trig recurrence values */
    c = cos(TWOPI * lof);
    s = sin(TWOPI * lof);
    for (ii = 0; ii < nn; ii++){
        arg = TWOPI * ((tt[ii] - ttavg) * df);
        wtemp = sin(0.5 * arg);
        wpr[ii] = -2.0 * wtemp * wtemp;
        wpi[ii] = sin(arg);
        wtemp = TWOPI * ((tt[ii] - ttavg) * lof);
        wr[ii] = cos(wtemp);
        wi[ii] = sin(wtemp);
        if (var==0.0)
            xxnorm[ii] = 1.0;
        else
            xxnorm[ii] = xx[ii] - avg;
    }
    
    /* Calculate the periodogram */
    for (ii = 0; ii < numf; ii++){
        sumsh = sumc = 0.0;
        for (jj=0; jj<nn; jj++){
            c = wr[jj];
            s = wi[jj];
            sumsh += s * c;
            sumc += (c - s) * (c + s);
        }
        wtau = 0.5 * atan2(2.0 * sumsh, sumc);
        cwtau = cos(wtau);
        swtau = sin(wtau);
        sums = sumc = sumsxx = sumcxx = 0.0;
        
        /* Step through the data points */
        for (jj = 0; jj < nn; jj++){
            c = wr[jj];
            s = wi[jj];
            ss = s * cwtau - c * swtau;
            cc = c * cwtau + s * swtau;
            sums += ss * ss;
            sumc += cc * cc;
            sumsxx += xxnorm[jj] * ss;
            sumcxx += xxnorm[jj] * cc;
            wr[jj] += c * wpr[jj] - s * wpi[jj];
            wi[jj] += s * wpr[jj] + c * wpi[jj];
        }
        
        /* Set the current power */
        pows[ii] = ivar * (sumcxx * sumcxx / sumc + 
                           sumsxx * sumsxx / sums);
    }
    
    /* Free the temp arrays and return */
    free(wr);
    free(wi);
    free(wpr);
    free(wpi);
    free(xxnorm);
    return(pows);
}
Beispiel #17
0
void read_PSRFITS_files(struct spectra_info *s)
// Read and convert PSRFITS information from a group of files 
// and place the resulting info into a spectra_info structure.
{
    int IMJD, SMJD, itmp, ii, status = 0;
    double OFFS, dtmp;
    long double MJDf;
    char ctmp[80], comment[120];
    
    s->datatype = PSRFITS;
    s->fitsfiles = (fitsfile **)malloc(sizeof(fitsfile *) * s->num_files);
    s->start_subint = gen_ivect(s->num_files);
    s->num_subint = gen_ivect(s->num_files);
    s->start_spec = (long long *)malloc(sizeof(long long) * s->num_files);
    s->num_spec = (long long *)malloc(sizeof(long long) * s->num_files);
    s->num_pad = (long long *)malloc(sizeof(long long) * s->num_files);
    s->start_MJD = (long double *)malloc(sizeof(long double) * s->num_files);
    s->N = 0;
    s->num_beams = 1;
    s->get_rawblock = &get_PSRFITS_rawblock;
    s->offset_to_spectra = &offset_to_PSRFITS_spectra;

    // By default, don't flip the band.  But don't change
    // the input value if it is aleady set to flip the band always
    if (s->apply_flipband==-1) s->apply_flipband = 0;

    // Step through the other files
    for (ii = 0 ; ii < s->num_files ; ii++) {

        // Is the file a PSRFITS file?
        if (!is_PSRFITS(s->filenames[ii])) {
            fprintf(stderr, 
                    "\nError!  File '%s' does not appear to be PSRFITS!\n", 
                    s->filenames[ii]);
            exit(1);
        }
        
        // Open the PSRFITS file
        fits_open_file(&(s->fitsfiles[ii]), s->filenames[ii], READONLY, &status);

        // Is the data in search mode?
        fits_read_key(s->fitsfiles[ii], TSTRING, "OBS_MODE", ctmp, comment, &status);
        // Quick fix for Parkes DFB data (SRCH?  why????)...
        if (strcmp("SRCH", ctmp)==0) {
            strncpy(ctmp, "SEARCH", 40);
        }
        if (strcmp(ctmp, "SEARCH")) {
            fprintf(stderr, 
                    "\nError!  File '%s' does not contain SEARCH-mode data!\n", 
                    s->filenames[ii]);
            exit(1);
        }

        // Now get the stuff we need from the primary HDU header
        fits_read_key(s->fitsfiles[ii], TSTRING, "TELESCOP", ctmp, comment, &status); \
        // Quick fix for MockSpec data...
        if (strcmp("ARECIBO 305m", ctmp)==0) {
            strncpy(ctmp, "Arecibo", 40);
        }
        // Quick fix for Parkes DFB data...
        {
            char newctmp[80];

            // Copy ctmp first since strlower() is in-place
            strcpy(newctmp, ctmp);
            if (strcmp("parkes", strlower(remove_whitespace(newctmp)))==0) {
                strncpy(ctmp, "Parkes", 40);
            }
        }
        if (status) {
            printf("Error %d reading key %s\n", status, "TELESCOP");
            if (ii==0) s->telescope[0]='\0';
            if (status==KEY_NO_EXIST) status=0;
        } else {
            if (ii==0) strncpy(s->telescope, ctmp, 40);
            else if (strcmp(s->telescope, ctmp)!=0)
                printf("Warning!:  %s values don't match for files 0 and %d!\n",
                       "TELESCOP", ii);
        }

        get_hdr_string("OBSERVER", s->observer);
        get_hdr_string("SRC_NAME", s->source);
        get_hdr_string("FRONTEND", s->frontend);
        get_hdr_string("BACKEND", s->backend);
        get_hdr_string("PROJID", s->project_id);
        get_hdr_string("DATE-OBS", s->date_obs);
        get_hdr_string("FD_POLN", s->poln_type);
        get_hdr_string("RA", s->ra_str);
        get_hdr_string("DEC", s->dec_str);
        get_hdr_double("OBSFREQ", s->fctr);
        get_hdr_int("OBSNCHAN", s->orig_num_chan);
        get_hdr_double("OBSBW", s->orig_df);
        //get_hdr_double("CHAN_DM", s->chan_dm);
        get_hdr_double("BMIN", s->beam_FWHM);

        /* This is likely not in earlier versions of PSRFITS so */
        /* treat it a bit differently                           */
        fits_read_key(s->fitsfiles[ii], TDOUBLE, "CHAN_DM", 
                      &(s->chan_dm), comment, &status);
        if (status==KEY_NO_EXIST) {
            status = 0;
            s->chan_dm = 0.0;
        }

        // Don't use the macros unless you are using the struct!
        fits_read_key(s->fitsfiles[ii], TINT, "STT_IMJD", &IMJD, comment, &status);
        s->start_MJD[ii] = (long double) IMJD;
        fits_read_key(s->fitsfiles[ii], TINT, "STT_SMJD", &SMJD, comment, &status);
        fits_read_key(s->fitsfiles[ii], TDOUBLE, "STT_OFFS", &OFFS, comment, &status);
        s->start_MJD[ii] += ((long double) SMJD + (long double) OFFS) / SECPERDAY;

        // Are we tracking?
        fits_read_key(s->fitsfiles[ii], TSTRING, "TRK_MODE", ctmp, comment, &status);
        itmp = (strcmp("TRACK", ctmp)==0) ? 1 : 0;
        if (ii==0) s->tracking = itmp;
        else if (s->tracking != itmp)
            printf("Warning!:  TRK_MODE values don't match for files 0 and %d!\n", ii);

        // Now switch to the SUBINT HDU header
        fits_movnam_hdu(s->fitsfiles[ii], BINARY_TBL, "SUBINT", 0, &status);
        get_hdr_double("TBIN", s->dt);
        get_hdr_int("NCHAN", s->num_channels);
        get_hdr_int("NPOL", s->num_polns);
        get_hdr_string("POL_TYPE", s->poln_order);
        fits_read_key(s->fitsfiles[ii], TINT, "NCHNOFFS", &itmp, comment, &status);
        if (itmp > 0)
            printf("Warning!:  First freq channel is not 0 in file %d!\n", ii);
        get_hdr_int("NSBLK", s->spectra_per_subint);
        get_hdr_int("NBITS", s->bits_per_sample);
        fits_read_key(s->fitsfiles[ii], TINT, "NAXIS2", 
                      &(s->num_subint[ii]), comment, &status);
        fits_read_key(s->fitsfiles[ii], TINT, "NSUBOFFS", 
                      &(s->start_subint[ii]), comment, &status);
        s->time_per_subint = s->dt * s->spectra_per_subint;

        /* This is likely not in earlier versions of PSRFITS so */
        /* treat it a bit differently                           */
        fits_read_key(s->fitsfiles[ii], TFLOAT, "ZERO_OFF", 
                      &(s->zero_offset), comment, &status);
        if (status==KEY_NO_EXIST) {
            status = 0;
            s->zero_offset = 0.0;
        }
        s->zero_offset = fabs(s->zero_offset);

        // Get the time offset column info and the offset for the 1st row
        {
            double offs_sub;
            int colnum, anynull, numrows;

            // Identify the OFFS_SUB column number
            fits_get_colnum(s->fitsfiles[ii], 0, "OFFS_SUB", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the OFFS_SUB column!\n");
                status = 0; // Reset status
            } else {
                if (ii==0) {
                    s->offs_sub_col = colnum;
                } else if (colnum != s->offs_sub_col) {
                    printf("Warning!:  OFFS_SUB column changes between files!\n");
                }
            }

            // Read the OFFS_SUB column value for the 1st row
            fits_read_col(s->fitsfiles[ii], TDOUBLE,
                          s->offs_sub_col, 1L, 1L, 1L,
                          0, &offs_sub, &anynull, &status);

            numrows = (int)((offs_sub - 0.5 * s->time_per_subint) /
                            s->time_per_subint + 1e-7);
            // Check to see if any rows have been deleted or are missing
            if (numrows > s->start_subint[ii]) {
                printf("Warning: NSUBOFFS reports %d previous rows\n"
                       "         but OFFS_SUB implies %d.  Using OFFS_SUB.\n"
                       "         Will likely be able to correct for this.\n",
                       s->start_subint[ii], numrows);
            }
            s->start_subint[ii] = numrows;
        }

        // This is the MJD offset based on the starting subint number
        MJDf = (s->time_per_subint * s->start_subint[ii]) / SECPERDAY;
        // The start_MJD values should always be correct
        s->start_MJD[ii] += MJDf;

        // Compute the starting spectra from the times
        MJDf = s->start_MJD[ii] - s->start_MJD[0];
        if (MJDf < 0.0) {
            fprintf(stderr, "Error!: File %d seems to be from before file 0!\n", ii); 
            exit(1);
        }
        s->start_spec[ii] = (long long)(MJDf * SECPERDAY / s->dt + 0.5);

        // Now pull stuff from the other columns
        {
            float ftmp;
            long repeat, width;
            int colnum, anynull;
            
            // Identify the data column and the data type
            fits_get_colnum(s->fitsfiles[ii], 0, "DATA", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the DATA column!\n");
                status = 0; // Reset status
            } else {
                if (ii==0) {
                    s->data_col = colnum;
                    fits_get_coltype(s->fitsfiles[ii], colnum, &(s->FITS_typecode), 
                                     &repeat, &width, &status);
                } else if (colnum != s->data_col) {
                    printf("Warning!:  DATA column changes between files!\n");
                }
            }
            
            // Telescope azimuth
            fits_get_colnum(s->fitsfiles[ii], 0, "TEL_AZ", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                s->azimuth = 0.0;
                status = 0; // Reset status
            } else {
                fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 
                              1L, 1L, 1L, 0, &ftmp, &anynull, &status);
                if (ii==0) s->azimuth = (double) ftmp;
            }
            
            // Telescope zenith angle
            fits_get_colnum(s->fitsfiles[ii], 0, "TEL_ZEN", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                s->zenith_ang = 0.0;
                status = 0; // Reset status
            } else {
                fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 
                              1L, 1L, 1L, 0, &ftmp, &anynull, &status);
                if (ii==0) s->zenith_ang = (double) ftmp;
            }
            
            // Observing frequencies
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_FREQ", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel freq column!\n");
                status = 0; // Reset status
            } else {
                int jj;
                float *freqs = (float *)malloc(sizeof(float) * s->num_channels);
                fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 1L, 1L, 
                              s->num_channels, 0, freqs, &anynull, &status);
                
                if (ii==0) {
                    s->df = freqs[1]-freqs[0];
                    s->lo_freq = freqs[0];
                    s->hi_freq = freqs[s->num_channels-1];
                    // Now check that the channel spacing is the same throughout
                    for (jj = 0 ; jj < s->num_channels - 1 ; jj++) {
                        ftmp = freqs[jj+1] - freqs[jj];
                        if (fabs(ftmp - s->df) > 1e-7)
                            printf("Warning!:  Channel spacing changes in file %d!\n", ii);
                    }
                } else {
                    ftmp = fabs(s->df-(freqs[1]-freqs[0]));
                    if (ftmp > 1e-7)
                        printf("Warning!:  Channel spacing changes between files!\n");
                    ftmp = fabs(s->lo_freq-freqs[0]);
                    if (ftmp > 1e-7)
                        printf("Warning!:  Low channel changes between files!\n");
                    ftmp = fabs(s->hi_freq-freqs[s->num_channels-1]);
                    if (ftmp > 1e-7)
                        printf("Warning!:  High channel changes between files!\n");
                }
                free(freqs);
            }
            
            // Data weights
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_WTS", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel weights!\n");
                status = 0; // Reset status
            } else {
                if (s->apply_weight < 0) { // Use the data to decide
                    int jj;
                    if (ii==0) {
                        s->dat_wts_col = colnum;
                    } else if (colnum != s->dat_wts_col) {
                        printf("Warning!:  DAT_WTS column changes between files!\n");
                    }
                    float *fvec = (float *)malloc(sizeof(float) * s->num_channels);
                    fits_read_col(s->fitsfiles[ii], TFLOAT, s->dat_wts_col, 1L, 1L, 
                                  s->num_channels, 0, fvec, &anynull, &status);
                    for (jj = 0 ; jj < s->num_channels ; jj++) {
                        // If the weights are not 1, apply them
                        if (fvec[jj] != 1.0) {
                            s->apply_weight = 1;
                            break;
                        }
                    }
                    free(fvec);
                }
                if (s->apply_weight < 0) s->apply_weight = 0;  // not needed
            }
            
            // Data offsets
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_OFFS", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel offsets!\n");
                status = 0; // Reset status
            } else {
                if (s->apply_offset < 0) { // Use the data to decide
                    int jj;
                    if (ii==0) {
                        s->dat_offs_col = colnum;
                    } else if (colnum != s->dat_offs_col) {
                        printf("Warning!:  DAT_OFFS column changes between files!\n");
                    }
                    float *fvec = (float *)malloc(sizeof(float) * 
                                                  s->num_channels * s->num_polns);
                    fits_read_col(s->fitsfiles[ii], TFLOAT, s->dat_offs_col, 1L, 1L, 
                                  s->num_channels * s->num_polns, 
                                  0, fvec, &anynull, &status);
                    for (jj = 0 ; jj < s->num_channels * s->num_polns ; jj++) {
                        // If the offsets are not 0, apply them
                        if (fvec[jj] != 0.0) {
                            s->apply_offset = 1;
                            break;
                        }
                    }
                    free(fvec);
                }
                if (s->apply_offset < 0) s->apply_offset = 0; // not needed
            }
            
            // Data scalings
            fits_get_colnum(s->fitsfiles[ii], 0, "DAT_SCL", &colnum, &status);
            if (status==COL_NOT_FOUND) {
                printf("Warning!:  Can't find the channel scalings!\n");
                status = 0; // Reset status
            } else {
                if (s->apply_scale < 0) { // Use the data to decide
                    int jj;
                    if (ii==0) {
                        s->dat_scl_col = colnum;
                    } else if (colnum != s->dat_scl_col) {
                        printf("Warning!:  DAT_SCL column changes between files!\n");
                    }
                    float *fvec = (float *)malloc(sizeof(float) * 
                                                  s->num_channels * s->num_polns);
                    fits_read_col(s->fitsfiles[ii], TFLOAT, colnum, 1L, 1L, 
                                  s->num_channels * s->num_polns, 
                                  0, fvec, &anynull, &status);
                    for (jj = 0 ; jj < s->num_channels * s->num_polns ; jj++) {
                        // If the scales are not 1, apply them
                        if (fvec[jj] != 1.0) {
                            s->apply_scale = 1;
                            break;
                        }
                    }
                    free(fvec);
                }
                if (s->apply_scale < 0) s->apply_scale = 0; // not needed
            }
        }
        
        // Compute the samples per file and the amount of padding
        // that the _previous_ file has
        s->num_pad[ii] = 0;
        s->num_spec[ii] = s->spectra_per_subint * s->num_subint[ii];
        if (ii > 0) {
            if (s->start_spec[ii] > s->N) { // Need padding
                s->num_pad[ii-1] = s->start_spec[ii] - s->N;
                s->N += s->num_pad[ii-1];
            }
        }
        s->N += s->num_spec[ii];
    }

    // Convert the position strings into degrees
    {
        int d, h, m;
        double sec;
        ra_dec_from_string(s->ra_str, &h, &m, &sec);
        s->ra2000 = hms2rad(h, m, sec) * RADTODEG;
        ra_dec_from_string(s->dec_str, &d, &m, &sec);
        s->dec2000 = dms2rad(d, m, sec) * RADTODEG;
    }

    // Are the polarizations summed?
    if ((strncmp("AA+BB", s->poln_order, 5)==0) ||
        (strncmp("INTEN", s->poln_order, 5)==0))
        s->summed_polns = 1;
    else
        s->summed_polns = 0;

    // Calculate some others
    s->T = s->N * s->dt;
    s->orig_df /= (double) s->orig_num_chan;
    s->samples_per_spectra = s->num_polns * s->num_channels;
    // Note:  the following is the number of bytes that will be in
    //        the returned array from CFITSIO.
    //        CFITSIO turns bits into bytes when FITS_typecode=1
    //        and we turn 2-bits or 4-bits into bytes if bits_per_sample < 8
    if (s->bits_per_sample < 8)
        s->bytes_per_spectra = s->samples_per_spectra;
    else
        s->bytes_per_spectra = (s->bits_per_sample * s->samples_per_spectra) / 8;
    s->samples_per_subint = s->samples_per_spectra * s->spectra_per_subint;
    s->bytes_per_subint = s->bytes_per_spectra * s->spectra_per_subint;
    
    // Flip the band?
    if (s->hi_freq < s->lo_freq) {
        float ftmp = s->hi_freq;
        s->hi_freq = s->lo_freq;
        s->lo_freq = ftmp;
        s->df *= -1.0;
        s->apply_flipband = 1;
    }
    // Compute the bandwidth
    s->BW = s->num_channels * s->df;

    // Flip the bytes for Parkes FB_1BIT data
    if (s->bits_per_sample==1 &&
        strcmp(s->telescope, "Parkes")==0 &&
        strcmp(s->backend, "FB_1BIT")==0) {
        printf("Flipping bit ordering since Parkes FB_1BIT data.\n");
        s->flip_bytes = 1;
    } else {
        s->flip_bytes = 0;
    }

    // Allocate the buffers
    cdatabuffer = gen_bvect(s->bytes_per_subint);
    // Following is twice as big because we use it as a ringbuffer too
    fdatabuffer = gen_fvect(2 * s->spectra_per_subint * s->num_channels);
    s->padvals = gen_fvect(s->num_channels);
    for (ii = 0 ; ii < s->num_channels ; ii++)
        s->padvals[ii] = 0.0;
    offsets = gen_fvect(s->num_channels * s->num_polns);
    scales = gen_fvect(s->num_channels * s->num_polns);
    weights = gen_fvect(s->num_channels);
    // Initialize these if we won't be reading them from the file
    if (s->apply_offset==0) 
        for (ii = 0 ; ii < s->num_channels * s->num_polns ; ii++)
            offsets[ii] = 0.0;
    if (s->apply_scale==0)
        for (ii = 0 ; ii < s->num_channels * s->num_polns ; ii++)
            scales[ii] = 1.0;
    if (s->apply_weight==0)
        for (ii = 0 ; ii < s->num_channels ; ii++)
            weights[ii] = 1.0;
}
Beispiel #18
0
static dataview *get_dataview(int centern, int zoomlevel, datapart * dp)
{
   int ii, jj, offset;
   double tmpavg, tmpvar;
   float *tmpchunk;
   dataview *dv;

   dv = (dataview *) malloc(sizeof(dataview));
   dv->zoomlevel = zoomlevel;
   dv->numsamps = (1 << (LOGMAXDISPNUM - zoomlevel));
   if (dv->numsamps > dp->nn)
      dv->numsamps = next2_to_n(dp->nn) / 2;
   dv->chunklen = (zoomlevel < -LOGMINCHUNKLEN) ?
       (1 << abs(zoomlevel)) : (1 << LOGMINCHUNKLEN);
   dv->dispnum = (dv->numsamps > MAXDISPNUM) ? MAXDISPNUM : dv->numsamps;
   if (DEBUGOUT)
      printf("zoomlevel = %d  numsamps = %d  chunklen = %d  dispnum %d  nn = %d\n",
             dv->zoomlevel, dv->numsamps, dv->chunklen, dv->dispnum, dp->nn);
   dv->numchunks = dv->numsamps / dv->chunklen;
   dv->centern = centern;
   dv->lon = centern - dv->numsamps / 2;
   dv->vdt = dv->chunklen * idata.dt;
   dv->maxval = SMALLNUM;
   dv->minval = LARGENUM;
   if (dv->lon < 0) {
      dv->lon = 0;
      dv->centern = dv->lon + dv->numsamps / 2;
   }
   if (dv->lon + dv->numsamps >= dp->nn) {
      dv->lon = dp->nn - dv->numsamps;
      dv->centern = dv->lon + dv->numsamps / 2;
   }
   tmpchunk = gen_fvect(dv->chunklen);
   for (ii = 0; ii < dv->numchunks; ii++) {
      float tmpmin = LARGENUM, tmpmax = SMALLNUM, tmpval;
      offset = dv->lon + ii * dv->chunklen;
      memcpy(tmpchunk, dp->data + offset, sizeof(float) * dv->chunklen);
      avg_var(dp->data + offset, dv->chunklen, &tmpavg, &tmpvar);
      if (usemedian)
         dv->avgmeds[ii] = median(tmpchunk, dv->chunklen);
      else
         dv->avgmeds[ii] = tmpavg;
      dv->stds[ii] = sqrt(tmpvar);
      for (jj = 0; jj < dv->chunklen; jj++, offset++) {
         tmpval = dp->data[offset];
         if (tmpval > tmpmax)
            tmpmax = tmpval;
         if (tmpval < tmpmin)
            tmpmin = tmpval;
      }
      dv->maxs[ii] = tmpmax;
      if (tmpmax > dv->maxval)
         dv->maxval = tmpmax;
      dv->mins[ii] = tmpmin;
      if (tmpmin < dv->minval)
         dv->minval = tmpmin;
   }
   vect_free(tmpchunk);
   offset = dv->lon;
   if (zoomlevel > 0) {
      for (ii = 0, offset = dv->lon; ii < dv->numsamps; ii++, offset++)
         *(dv->vals + ii) = *(dp->data + offset);
   }
   return dv;
}
Beispiel #19
0
float *real_corr_conv(float *data, float *kernel, int numdata,
                      presto_ffts ffts, presto_optype type)
  /* Perform and return a real-valued correlation or convolution.   */
  /* Arguments:                                                     */
  /*   'data' is the complex array to correlate/convolve.           */
  /*   'kernel' is the correlation/convolution kernel.              */
  /*   'numdata' is the length of 'data', 'kernel' and the result.  */
  /*   'ffts' describes how to perform the convolution/correlation. */
  /*      'ffts' = FFTDK:  FFT both the 'data' and the 'kernel'.    */
  /*      'ffts' = FFTD:  FFT only the 'data' not the 'kernel'.     */
  /*      'ffts' = FFTK:  FFT only the 'kernel' not the 'data'.     */
  /*      'ffts' = NOFFTS:  Don't FFT the 'data' or the 'kernel'.   */
  /*   'type' is the type of operation to perform.                  */
  /*      'type' = CONV:  return a convolution in a new vector.     */
  /*      'type' = CORR:  return a correlation in a new vector.     */
  /*      'type' = INPLACE_CONV:  convolution over-writes 'data'.   */
  /*      'type' = INPLACE_CORR:  correlation over-writes 'data'.   */
{
   int ii;
   float normal, tmpd, tmpk, *tmpdat;
   fcomplex *fcdata, *fckern;

   /* Get the normalization factor */

   normal = 2.0 / numdata;

   /* Set up all our parameters */

   if (ffts > 3) {
      printf("\nIllegal 'ffts' option (%d) in real_corr_conv().\n", ffts);
      printf("Exiting\n\n");
      exit(1);
   }
   if (type > 3) {
      printf("\nIllegal 'type' option (%d) in real_corr_conv().\n", ffts);
      printf("Exiting\n\n");
      exit(1);
   }
   if (type == INPLACE_CONV || type == INPLACE_CORR) {
      tmpdat = data;
   } else {
      tmpdat = gen_fvect(numdata);
      memcpy(tmpdat, data, sizeof(float) * numdata);
   }
   if (ffts == FFTDK || ffts == FFTD) {
      realfft(tmpdat, numdata, -1);
   }
   if (ffts == FFTDK || ffts == FFTK) {
      realfft(kernel, numdata, -1);
   }

   // Act like our packed-complex floats are really fcomplex values

   fcdata = (fcomplex *)tmpdat;
   fckern = (fcomplex *)kernel;

   // Do the complex multiplications

   if (type == CORR || type == INPLACE_CORR) {
      for (ii = 1; ii < numdata / 2; ii++) {
         tmpd = fcdata[ii].r;
         tmpk = fckern[ii].r;
         fcdata[ii].r = (tmpd * tmpk + fcdata[ii].i * fckern[ii].i)
             * normal;
         fcdata[ii].i = (fcdata[ii].i * tmpk - fckern[ii].i * tmpd)
             * normal;
      }
   } else {
      for (ii = 1; ii < numdata / 2; ii++) {
         tmpd = fcdata[ii].r;
         tmpk = fckern[ii].r;
         fcdata[ii].r = (tmpd * tmpk - fcdata[ii].i * fckern[ii].i)
             * normal;
         fcdata[ii].i = (fcdata[ii].i * tmpk + fckern[ii].i * tmpd)
             * normal;
      }
   }

   // Now handle bin zero

   fcdata[0].r *= fckern[0].r * normal;
   fcdata[0].i *= fckern[0].i * normal;

   // Perform the inverse FFT on the result and return

   realfft(tmpdat, numdata, 1);
   return tmpdat;
}
Beispiel #20
0
ffdotpows *subharm_ffdot_plane(int numharm, int harmnum,
                               double fullrlo, double fullrhi,
                               subharminfo * shi, accelobs * obs)
{
   int ii, lobin, hibin, numdata, nice_numdata, nrs, fftlen, binoffset;
   static int numrs_full = 0, numzs_full = 0;
   float powargr, powargi;
   double drlo, drhi, harm_fract;
   ffdotpows *ffdot;
   fcomplex *data, **result;
   presto_datainf datainf;

   if (numrs_full == 0) {
      if (numharm == 1 && harmnum == 1) {
         numrs_full = ACCEL_USELEN;
         numzs_full = shi->numkern;
      } else {
         printf("You must call subharm_ffdot_plane() with numharm=1 and\n");
         printf("harnum=1 before you use other values!  Exiting.\n\n");
         exit(0);
      }
   }
   ffdot = (ffdotpows *) malloc(sizeof(ffdotpows));

   /* Calculate and get the required amplitudes */

   harm_fract = (double) harmnum / (double) numharm;
   drlo = calc_required_r(harm_fract, fullrlo);
   drhi = calc_required_r(harm_fract, fullrhi);
   ffdot->rlo = (int) floor(drlo);
   ffdot->zlo = calc_required_z(harm_fract, obs->zlo);

   /* Initialize the lookup indices */
   if (numharm > 1) {
      double rr, subr;
      for (ii = 0; ii < numrs_full; ii++) {
         rr = fullrlo + ii * ACCEL_DR;
         subr = calc_required_r(harm_fract, rr);
         shi->rinds[ii] = index_from_r(subr, ffdot->rlo);
      }
   }
   ffdot->rinds = shi->rinds;
   ffdot->numrs = (int) ((ceil(drhi) - floor(drlo))
                         * ACCEL_RDR + DBLCORRECT) + 1;
   if (numharm == 1 && harmnum == 1) {
      ffdot->numrs = ACCEL_USELEN;
   } else {
      if (ffdot->numrs % ACCEL_RDR) {
         ffdot->numrs = (ffdot->numrs / ACCEL_RDR + 1) * ACCEL_RDR;
      }
   }
   ffdot->numzs = shi->numkern;
   binoffset = shi->kern[0].kern_half_width;
   fftlen = shi->kern[0].fftlen;
   lobin = ffdot->rlo - binoffset;
   hibin = (int) ceil(drhi) + binoffset;
   numdata = hibin - lobin + 1;
   nice_numdata = next2_to_n(numdata);  // for FFTs
   data = get_fourier_amplitudes(lobin, nice_numdata, obs);
   if (!obs->mmap_file && !obs->dat_input && 0)
       printf("This is newly malloc'd!\n");

   // Normalize the Fourier amplitudes

   if (obs->nph > 0.0) {
       //  Use freq 0 normalization if requested (i.e. photons)
       double norm = 1.0 / sqrt(obs->nph);
       for (ii = 0; ii < numdata; ii++) {
           data[ii].r *= norm;
           data[ii].i *= norm;
       }
   } else if (obs->norm_type == 0) {
       //  old-style block median normalization
       float *powers;
       double norm;

       powers = gen_fvect(numdata);
       for (ii = 0; ii < numdata; ii++)
           powers[ii] = POWER(data[ii].r, data[ii].i);
       norm = 1.0 / sqrt(median(powers, numdata)/log(2.0));
       free(powers);
       for (ii = 0; ii < numdata; ii++) {
           data[ii].r *= norm;
           data[ii].i *= norm;
       }
   } else {
       //  new-style running double-tophat local-power normalization
       float *powers, *loc_powers;

       powers = gen_fvect(nice_numdata);
       for (ii = 0; ii < nice_numdata; ii++) {
           powers[ii] = POWER(data[ii].r, data[ii].i);
       }
       loc_powers = corr_loc_pow(powers, nice_numdata);
       for (ii = 0; ii < numdata; ii++) {
           float norm = invsqrt(loc_powers[ii]);
           data[ii].r *= norm;
           data[ii].i *= norm;
       }
       free(powers);
       free(loc_powers);
   }

   /* Perform the correlations */

   result = gen_cmatrix(ffdot->numzs, ffdot->numrs);
   datainf = RAW;
   for (ii = 0; ii < ffdot->numzs; ii++) {
      nrs = corr_complex(data, numdata, datainf,
                         shi->kern[ii].data, fftlen, FFT,
                         result[ii], ffdot->numrs, binoffset,
                         ACCEL_NUMBETWEEN, binoffset, CORR);
      datainf = SAME;
   }

   // Always free data
   free(data);

   /* Convert the amplitudes to normalized powers */

   ffdot->powers = gen_fmatrix(ffdot->numzs, ffdot->numrs);
   for (ii = 0; ii < (ffdot->numzs * ffdot->numrs); ii++)
      ffdot->powers[0][ii] = POWER(result[0][ii].r, result[0][ii].i);
   free(result[0]);
   free(result);
   return ffdot;
}
Beispiel #21
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);
}
Beispiel #22
0
void deredden(fcomplex * fft, int numamps)
/* Attempt to remove rednoise from a time series by using   */
/* a median-filter of logarithmically increasing width.     */
/* Thanks to Jason Hessels and Maggie Livingstone for the   */
/* initial implementation (in rednoise.c)                   */
{
   int ii, initialbuflen = 6, lastbuflen, newbuflen, maxbuflen = 200;
   int newoffset = 1, fixedoffset = 1;
   float *powers, mean_old, mean_new;
   double slope, lineval, scaleval = 1.0, lineoffset = 0;

   powers = gen_fvect(numamps);

   /* Takes care of the DC term */
   fft[0].r = 1.0;
   fft[0].i = 0.0;

   /* Step through the input FFT and create powers */
   for (ii = 0; ii < numamps; ii++) {
      float powargr, powargi;
      powers[ii] = POWER(fft[ii].r, fft[ii].i);
   }

   /* Calculate initial values */
   mean_old = median(powers + newoffset, initialbuflen) / log(2.0);
   newoffset += initialbuflen;
   lastbuflen = initialbuflen;
   newbuflen = initialbuflen * log(newoffset);
   if (newbuflen > maxbuflen)
      newbuflen = maxbuflen;

   while (newoffset + newbuflen < numamps) {
      /* Calculate the next mean */
      mean_new = median(powers + newoffset, newbuflen) / log(2.0);
      //slope = (mean_new-mean_old)/(0.5*(newbuflen+lastbuflen));
      slope = (mean_new - mean_old) / (newbuflen + lastbuflen);

      /* Correct the previous segment */
      lineoffset = 0.5 * (newbuflen + lastbuflen);
      for (ii = 0; ii < lastbuflen; ii++) {
         lineval = mean_old + slope * (lineoffset - ii);
         scaleval = 1.0 / sqrt(lineval);
         fft[fixedoffset + ii].r *= scaleval;
         fft[fixedoffset + ii].i *= scaleval;
      }

      /* Update our values */
      fixedoffset += lastbuflen;
      lastbuflen = newbuflen;
      mean_old = mean_new;
      newoffset += lastbuflen;
      newbuflen = initialbuflen * log(newoffset);
      if (newbuflen > maxbuflen)
         newbuflen = maxbuflen;
   }

   /* Scale the last (partial) chunk the same way as the last point */

   while (fixedoffset < numamps) {
      fft[fixedoffset].r *= scaleval;
      fft[fixedoffset].i *= scaleval;
      fixedoffset++;
   }

   /* Free the powers */
   free(powers);
}
Beispiel #23
0
void fill_psrfits_struct(int numwapps, int numbits, struct HEADERP *h,
                         struct wappinfo *w, struct psrfits *pf)
{
    int slen, ii;
    char *cptr;

    pf->filenum = 0;            // Crucial for initialization
    pf->hdr.nsblk = (int) (1.0 / w->dt);        // _might_ be a problem...

    // Now set values for our hdrinfo structure
    strcpy(pf->hdr.telescope, "Arecibo");
    cptr = get_hdr_string(h, "obs_type", &slen);
    if (strncmp("PULSAR_SEARCH", cptr, slen) == 0) {
        strcpy(pf->hdr.obs_mode, "SEARCH");
    } else {
        printf("Error:  Wapp data is not in search format!\n\n");
        exit(1);
    }
    strcpy(pf->hdr.backend, "WAPP");
    cptr = get_hdr_string(h, "frontend", &slen);
    if(cptr != NULL)
      strncpy(pf->hdr.frontend, cptr, slen);
    else
      strncpy(pf->hdr.frontend, "alfa", 4);
    cptr = get_hdr_string(h, "observers", &slen);
    strncpy(pf->hdr.observer, cptr, slen);
    cptr = get_hdr_string(h, "project_id", &slen);
    strncpy(pf->hdr.project_id, cptr, slen);
    cptr = get_hdr_string(h, "src_name", &slen);
    strncpy(pf->hdr.source, cptr, slen);
    strcpy(pf->hdr.date_obs, w->date_obs);
    pf->hdr.scanlen = get_hdr_double(h, "obs_time");

    strcpy(pf->hdr.poln_type, "LIN");   // set based on known receivers
    if (get_hdr_int(h, "sum")) {
        strcpy(pf->hdr.poln_order, "AA+BB");
        pf->hdr.summed_polns = 1;
    } else if (w->numifs == 1) {
        strcpy(pf->hdr.poln_order, "AA");
        pf->hdr.summed_polns = 0;
    }
    strcpy(pf->hdr.track_mode, "TRACK");  // Potentially not-true?
    strcpy(pf->hdr.cal_mode, "OFF");      // Potentially not-true?
    strcpy(pf->hdr.feed_mode, "FA");      // check this...

    pf->hdr.beamnum = 0;
    if (get_hdr_int(h, "isalfa"))
        pf->hdr.beamnum = w->beamnum;

    pf->hdr.dt = w->dt;
    pf->hdr.fctr = w->fctr + 0.5 * (numwapps - 1.0) * w->BW;
    pf->hdr.BW = w->BW * numwapps;
    pf->hdr.beam_FWHM = beam_FWHM(pf->hdr.fctr, 300.0);
    pf->hdr.nchan = w->numchans * numwapps;
    pf->hdr.orig_nchan = w->numchans * numwapps;
    pf->hdr.orig_df = pf->hdr.df = pf->hdr.BW / pf->hdr.nchan;
    pf->hdr.nbits = numbits;
    pf->hdr.npol = w->numifs;
    pf->hdr.MJD_epoch = w->MJD_epoch;
    pf->hdr.start_day = (int) (w->MJD_epoch);
    pf->hdr.start_sec = (w->MJD_epoch - pf->hdr.start_day) * 86400.0;
    pf->hdr.scan_number = get_hdr_int(h, "scan_number");
    pf->hdr.ra2000 = w->ra;
    dec2hms(pf->hdr.ra_str, pf->hdr.ra2000 / 15.0, 0);
    pf->hdr.dec2000 = w->dec;
    dec2hms(pf->hdr.dec_str, pf->hdr.dec2000, 1);
    pf->hdr.azimuth = get_hdr_double(h, "start_az");
    pf->hdr.zenith_ang = get_hdr_double(h, "start_za");
    pf->hdr.rcvr_polns = 2;
    pf->hdr.offset_subint = 0;
    pf->hdr.onlyI = 0;
    pf->hdr.ds_time_fact = 1;
    pf->hdr.ds_freq_fact = 1;
    pf->hdr.chan_dm = 0.0;
    pf->hdr.fd_hand = pf->hdr.be_phase = 0;     // This is almost certainly not correct
    pf->hdr.fd_sang = pf->hdr.fd_xyph = 0.0;    // This is almost certainly not correct
    pf->hdr.feed_angle = 0.0;   // This is almost certainly not correct
    pf->hdr.cal_freq = pf->hdr.cal_dcyc = pf->hdr.cal_phs = 0.0;        //  ditto

    // Now set values for our subint structure
    pf->sub.tel_az = get_hdr_double(h, "start_az");
    pf->sub.tel_zen = get_hdr_double(h, "start_za");
    pf->sub.lst = get_hdr_double(h, "start_lst");
    pf->hdr.start_lst = pf->sub.lst;
    pf->sub.tsubint = pf->hdr.nsblk * pf->hdr.dt;
    pf->sub.ra = pf->hdr.ra2000;
    pf->sub.dec = pf->hdr.dec2000;
    pf->sub.offs = 0.5 * pf->sub.tsubint;
    slaEqgal(pf->hdr.ra2000 * DEGTORAD, pf->hdr.dec2000 * DEGTORAD,
             &pf->sub.glon, &pf->sub.glat);
    pf->sub.glon *= RADTODEG;
    pf->sub.glat *= RADTODEG;
    // The following three are unknown or hard to get, I think (SMR)
    pf->sub.feed_ang = 0.0;
    pf->sub.pos_ang = 0.0;
    pf->sub.par_ang = 0.0;
    pf->sub.bytes_per_subint = (pf->hdr.nbits * pf->hdr.nchan *
                                pf->hdr.npol * pf->hdr.nsblk) / 8;
    pf->sub.FITS_typecode = TBYTE;      // 11 = byte

    // Create and initialize the subint arrays
    pf->sub.dat_freqs = gen_fvect(pf->hdr.nchan);
    pf->sub.dat_weights = gen_fvect(pf->hdr.nchan);
    for (ii = 0; ii < pf->hdr.nchan; ii++) {
        pf->sub.dat_freqs[ii] = w->lofreq + ii * pf->hdr.df;
        pf->sub.dat_weights[ii] = 1.0;
    }

    // The following are re-set to try to preserve the band shape later
    pf->sub.dat_offsets = gen_fvect(pf->hdr.nchan * pf->hdr.npol);
    pf->sub.dat_scales = gen_fvect(pf->hdr.nchan * pf->hdr.npol);
    for (ii = 0; ii < pf->hdr.nchan * pf->hdr.npol; ii++) {
        pf->sub.dat_offsets[ii] = 0.0;
        pf->sub.dat_scales[ii] = 1.0;
    }

    // This is the raw data block that will be updated 
    // for each row of the PSRFITS file
    pf->sub.data = gen_bvect(pf->sub.bytes_per_subint);
}
Beispiel #24
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);
}
Beispiel #25
0
void rfifind_plot(int numchan, int numint, int ptsperint,
                  float timesigma, float freqsigma,
                  float inttrigfrac, float chantrigfrac,
                  float **dataavg, float **datastd, float **datapow,
                  int *userchan, int numuserchan,
                  int *userints, int numuserints,
                  infodata * idata, unsigned char **bytemask,
                  mask * oldmask, mask * newmask,
                  rfi * rfivect, int numrfi, int rfixwin, int rfips, int xwin)
/* Make the beautiful multi-page rfifind plots */
{
   int ii, jj, ct, loops = 1;
   float *freqs, *chans, *times, *ints;
   float *avg_chan_avg, *std_chan_avg, *pow_chan_avg;
   float *avg_chan_med, *std_chan_med, *pow_chan_med;
   float *avg_chan_std, *std_chan_std, *pow_chan_std;
   float *avg_int_avg, *std_int_avg, *pow_int_avg;
   float *avg_int_med, *std_int_med, *pow_int_med;
   float *avg_int_std, *std_int_std, *pow_int_std;
   float dataavg_avg, datastd_avg, datapow_avg;
   float dataavg_med, datastd_med, datapow_med;
   float dataavg_std, datastd_std, datapow_std;
   float avg_reject, std_reject, pow_reject;
   double inttim, T, lof, hif;

   inttim = ptsperint * idata->dt;
   T = inttim * numint;
   lof = idata->freq - 0.5 * idata->chan_wid;
   hif = lof + idata->freqband;
   avg_chan_avg = gen_fvect(numchan);
   std_chan_avg = gen_fvect(numchan);
   pow_chan_avg = gen_fvect(numchan);
   avg_int_avg = gen_fvect(numint);
   std_int_avg = gen_fvect(numint);
   pow_int_avg = gen_fvect(numint);
   avg_chan_med = gen_fvect(numchan);
   std_chan_med = gen_fvect(numchan);
   pow_chan_med = gen_fvect(numchan);
   avg_int_med = gen_fvect(numint);
   std_int_med = gen_fvect(numint);
   pow_int_med = gen_fvect(numint);
   avg_chan_std = gen_fvect(numchan);
   std_chan_std = gen_fvect(numchan);
   pow_chan_std = gen_fvect(numchan);
   avg_int_std = gen_fvect(numint);
   std_int_std = gen_fvect(numint);
   pow_int_std = gen_fvect(numint);
   chans = gen_fvect(numchan);
   freqs = gen_fvect(numchan);
   for (ii = 0; ii < numchan; ii++) {
      chans[ii] = ii;
      freqs[ii] = idata->freq + ii * idata->chan_wid;
   }
   ints = gen_fvect(numint);
   times = gen_fvect(numint);
   for (ii = 0; ii < numint; ii++) {
      ints[ii] = ii;
      times[ii] = 0.0 + ii * inttim;
   }

   /* Calculate the statistics of the full set */

   ct = numchan * numint;
   calc_avgmedstd(dataavg[0], ct, 0.8, 1, &dataavg_avg, &dataavg_med, &dataavg_std);
   calc_avgmedstd(datastd[0], ct, 0.8, 1, &datastd_avg, &datastd_med, &datastd_std);
   calc_avgmedstd(datapow[0], ct, 0.5, 1, &datapow_avg, &datapow_med, &datapow_std);
   avg_reject = timesigma * dataavg_std;
   std_reject = timesigma * datastd_std;
   pow_reject = power_for_sigma(freqsigma, 1, ptsperint / 2);

   /* Calculate the channel/integration statistics vectors */

   for (ii = 0; ii < numint; ii++) {
      calc_avgmedstd(dataavg[0] + ii * numchan, numchan, 0.8, 1,
                     avg_int_avg + ii, avg_int_med + ii, avg_int_std + ii);
      calc_avgmedstd(datastd[0] + ii * numchan, numchan, 0.8, 1,
                     std_int_avg + ii, std_int_med + ii, std_int_std + ii);
      calc_avgmedstd(datapow[0] + ii * numchan, numchan, 0.5, 1,
                     pow_int_avg + ii, pow_int_med + ii, pow_int_std + ii);
   }
   for (ii = 0; ii < numchan; ii++) {
      calc_avgmedstd(dataavg[0] + ii, numint, 0.8, numchan,
                     avg_chan_avg + ii, avg_chan_med + ii, avg_chan_std + ii);
      calc_avgmedstd(datastd[0] + ii, numint, 0.8, numchan,
                     std_chan_avg + ii, std_chan_med + ii, std_chan_std + ii);
      calc_avgmedstd(datapow[0] + ii, numint, 0.5, numchan,
                     pow_chan_avg + ii, pow_chan_med + ii, pow_chan_std + ii);
      /*
         fprintf(stderr, "%12.7g  %12.7g  %12.7g    %12.7g  %12.7g  %12.7g    %12.7g  %12.7g  %12.7g    \n", 
         avg_chan_avg[ii], avg_chan_med[ii], avg_chan_std[ii],
         std_chan_avg[ii], std_chan_med[ii], std_chan_std[ii],
         pow_chan_avg[ii], pow_chan_med[ii], pow_chan_std[ii]);
       */
   }

   /* Generate the byte mask */

   /* Set the channels/intervals picked by the user */
   if (numuserints)
      for (ii = 0; ii < numuserints; ii++)
         if (userints[ii] >= 0 && userints[ii] < numint)
            for (jj = 0; jj < numchan; jj++)
               bytemask[userints[ii]][jj] |= USERINTS;
   if (numuserchan)
      for (ii = 0; ii < numuserchan; ii++)
         if (userchan[ii] >= 0 && userchan[ii] < numchan)
            for (jj = 0; jj < numint; jj++)
               bytemask[jj][userchan[ii]] |= USERCHAN;

   /* Compare each point in an interval (or channel) with   */
   /* the interval's (or channel's) median and the overall  */
   /* standard deviation.  If the channel/integration       */
   /* medians are more than sigma different than the global */
   /* value, set them to the global.                        */
   {
      float int_med, chan_med;

      for (ii = 0; ii < numint; ii++) {
         for (jj = 0; jj < numchan; jj++) {
            {                   /* Powers */
               if (datapow[ii][jj] > pow_reject)
                  if (!(bytemask[ii][jj] & PADDING))
                     bytemask[ii][jj] |= BAD_POW;
            }
            {                   /* Averages */
               if (fabs(avg_int_med[ii] - dataavg_med) > timesigma * dataavg_std)
                  int_med = dataavg_med;
               else
                  int_med = avg_int_med[ii];
               if (fabs(avg_chan_med[jj] - dataavg_med) > timesigma * dataavg_std)
                  chan_med = dataavg_med;
               else
                  chan_med = avg_chan_med[jj];
               if (fabs(dataavg[ii][jj] - int_med) > avg_reject ||
                   fabs(dataavg[ii][jj] - chan_med) > avg_reject)
                  if (!(bytemask[ii][jj] & PADDING))
                     bytemask[ii][jj] |= BAD_AVG;
            }
            {                   /* Standard Deviations */
               if (fabs(std_int_med[ii] - datastd_med) > timesigma * datastd_std)
                  int_med = datastd_med;
               else
                  int_med = std_int_med[ii];
               if (fabs(std_chan_med[jj] - datastd_med) > timesigma * datastd_std)
                  chan_med = datastd_med;
               else
                  chan_med = std_chan_med[jj];
               if (fabs(datastd[ii][jj] - int_med) > std_reject ||
                   fabs(datastd[ii][jj] - chan_med) > std_reject)
                  if (!(bytemask[ii][jj] & PADDING))
                     bytemask[ii][jj] |= BAD_STD;
            }
         }
      }
   }

   /* Step over the intervals and channels and count how many are set "bad". */
   /* For a given interval, if the number of bad channels is greater than    */
   /* chantrigfrac*numchan then reject the whole interval.                   */
   /* For a given channel, if the number of bad intervals is greater than    */
   /* inttrigfrac*numint then reject the whole channel.                      */
   {
      int badnum, trignum;

      /* Loop over the intervals */
      trignum = (int) (numchan * chantrigfrac);
      for (ii = 0; ii < numint; ii++) {
         if (!(bytemask[ii][0] & USERINTS)) {
            badnum = 0;
            for (jj = 0; jj < numchan; jj++)
               if (bytemask[ii][jj] & BADDATA)
                  badnum++;
            if (badnum > trignum) {
               userints[numuserints++] = ii;
               for (jj = 0; jj < numchan; jj++)
                  bytemask[ii][jj] |= USERINTS;
            }
         }
      }

      /* Loop over the channels */
      trignum = (int) (numint * inttrigfrac);
      for (ii = 0; ii < numchan; ii++) {
         if (!(bytemask[0][ii] & USERCHAN)) {
            badnum = 0;
            for (jj = 0; jj < numint; jj++)
               if (bytemask[jj][ii] & BADDATA)
                  badnum++;
            if (badnum > trignum) {
               userchan[numuserchan++] = ii;
               for (jj = 0; jj < numint; jj++)
                  bytemask[jj][ii] |= USERCHAN;
            }
         }
      }
   }

   /* Generate the New Mask */

   fill_mask(timesigma, freqsigma, idata->mjd_i + idata->mjd_f,
             ptsperint * idata->dt, idata->freq, idata->chan_wid,
             numchan, numint, ptsperint, numuserchan, userchan,
             numuserints, userints, bytemask, newmask);

   /* Place the oldmask over the newmask for plotting purposes */

   if (oldmask->numchan)
      set_oldmask_bits(oldmask, bytemask);

   /*
    *  Now plot the results
    */

   if (xwin)
      loops = 2;
   for (ct = 0; ct < loops; ct++) {     /* PS/XWIN Plot Loop */
      float min, max, tr[6], locut, hicut;
      float left, right, top, bottom;
      float xl, xh, yl, yh;
      float tt, ft, th, fh;     /* thin and fat thicknesses and heights */
      float lm, rm, tm, bm;     /* LRTB margins */
      float xarr[2], yarr[2];
      char outdev[100];
      int ii, mincol, maxcol, numcol;

      /*Set the PGPLOT device to an X-Window */

      if (ct == 1)
         strcpy(outdev, "/XWIN");
      else
         sprintf(outdev, "%s.ps/CPS", idata->name);

      /* Open and prep our device */

      cpgopen(outdev);
      cpgpap(10.25, 8.5 / 11.0);
      cpgpage();
      cpgiden();
      cpgsch(0.7);
      cpgqcir(&mincol, &maxcol);
      numcol = maxcol - mincol + 1;
      for (ii = mincol; ii <= maxcol; ii++) {
         float color;
         color = (float) (maxcol - ii) / (float) numcol;
         cpgscr(ii, color, color, color);
      }

      /* Set thicknesses and margins */

      lm = 0.04;
      rm = 0.04;
      bm = 0.08;
      tm = 0.05;
      ft = 3.0;                 /* This sets fat thickness = 3 x thin thickness */
      tt = 0.92 / (6.0 + 4.0 * ft);
      ft *= tt;
      fh = 0.55;
      th = tt * 11.0 / 8.5;

      {                         /* Powers Histogram */
         float *theo, *hist, *hpows, *tpows, maxhist = 0.0, maxtheo = 0.0;
         int numhist = 40, numtheo = 200, bin, numpows;
         double dtheo, dhist, spacing;

         /* Calculate the predicted distribution of max powers */

         numpows = numint * numchan;
         find_min_max_arr(numpows, datapow[0], &min, &max);
         min = (min < 5.0) ? log10(5.0 * 0.95) : log10(min * 0.95);
         max = log10(max * 1.05);
         dhist = (max - min) / numhist;
         theo = gen_fvect(numtheo);
         tpows = gen_fvect(numtheo);
         hist = gen_fvect(numhist);
         hpows = gen_fvect(numhist);
         for (ii = 0; ii < numhist; ii++) {
            hist[ii] = 0.0;
            hpows[ii] = min + ii * dhist;
         }
         for (ii = 0; ii < numpows; ii++) {
            bin = (*(datapow[0] + ii) == 0.0) ? 0 :
                (log10(*(datapow[0] + ii)) - min) / dhist;
            if (bin < 0)
               bin = 0;
            if (bin >= numhist)
               bin = numhist;
            hist[bin] += 1.0;
         }
         for (ii = 0; ii < numhist; ii++)
            if (hist[ii] > maxhist)
               maxhist = hist[ii];
         maxhist *= 1.1;
         dtheo = (max - min) / (double) (numtheo - 1);
         for (ii = 0; ii < numtheo; ii++) {
            tpows[ii] = min + ii * dtheo;
            theo[ii] = single_power_pdf(pow(10.0, tpows[ii]),
                                        ptsperint / 2) * numpows;
            spacing = (pow(10.0, tpows[ii] + dhist) - pow(10.0, tpows[ii]));
            theo[ii] *= spacing;
            if (theo[ii] > maxtheo)
               maxtheo = theo[ii];
         }
         maxtheo *= 1.1;
         if (maxtheo > maxhist)
            maxhist = maxtheo;
         left = lm;
         right = lm + ft + tt;
         bottom = 0.80;
         top = 0.96;
         cpgsvp(left, right, bottom, top);
         xl = min;
         xh = max;
         yl = 0.0;
         yh = maxhist;
         cpgswin(xl, xh, yl, yh);
         cpgmtxt("L", 1.1, 0.5, 0.5, "Number");
         cpgmtxt("B", 2.1, 0.5, 0.5, "Max Power");
         cpgbin(numhist, hpows, hist, 0);
         cpgscr(maxcol, 0.5, 0.5, 0.5);
         cpgsci(maxcol);        /* Grey */
         cpgline(numtheo, tpows, theo);
         xarr[0] = log10(power_for_sigma(freqsigma, 1, ptsperint / 2));
         xarr[1] = xarr[0];
         yarr[0] = yl;
         yarr[1] = yh;
         cpgsls(4);             /* Dotted line */
         cpgscr(maxcol, 1.0, 0.0, 0.0);
         cpgsci(maxcol);        /* Red */
         cpgline(2, xarr, yarr);
         cpgsls(1);             /* Solid line */
         cpgsci(1);             /* Default color */
         cpgbox("BCLNST", 0.0, 0, "BC", 0.0, 0);
         vect_free(hist);
         vect_free(theo);
         vect_free(tpows);
         vect_free(hpows);
      }

      /* Maximum Powers */

      left = lm;
      right = lm + ft;
      bottom = bm;
      top = bm + fh;
      xl = 0.0;
      xh = numchan;
      yl = 0.0;
      yh = T;
      cpgsvp(left, right, bottom, top);
      cpgswin(xl, xh, yl, yh);
      cpgscr(maxcol, 1.0, 0.0, 0.0);    /* Red */
      locut = 0.0;
      hicut = pow_reject;
      tr[2] = tr[4] = 0.0;
      tr[1] = (xh - xl) / numchan;
      tr[0] = xl - (tr[1] / 2);
      tr[5] = (yh - yl) / numint;
      tr[3] = yl - (tr[5] / 2);
      cpgimag(datapow[0], numchan, numint, 1, numchan, 1, numint, locut, hicut, tr);
      cpgswin(xl, xh, yl, yh);
      cpgbox("BNST", 0.0, 0, "BNST", 0.0, 0);
      cpgmtxt("B", 2.6, 0.5, 0.5, "Channel");
      cpgmtxt("L", 2.1, 0.5, 0.5, "Time (s)");
      xl = lof;
      xh = hif;
      yl = 0.0;
      yh = numint;
      cpgswin(xl, xh, yl, yh);
      cpgbox("CST", 0.0, 0, "CST", 0.0, 0);

      /* Max Power Label */

      left = lm + ft;
      right = lm + ft + tt;
      bottom = bm + fh;
      top = bm + fh + th;
      cpgsvp(left, right, bottom, top);
      cpgswin(0.0, 1.0, 0.0, 1.0);
      cpgscr(maxcol, 1.0, 0.0, 0.0);
      cpgsci(maxcol);           /* Red */
      cpgptxt(0.5, 0.7, 0.0, 0.5, "Max");
      cpgptxt(0.5, 0.3, 0.0, 0.5, "Power");
      cpgsci(1);                /* Default color */

      /*  Max Power versus Time */

      left = lm + ft;
      right = lm + ft + tt;
      bottom = bm;
      top = bm + fh;
      cpgsvp(left, right, bottom, top);
      find_min_max_arr(numint, pow_int_med, &min, &max);
      xl = 0.0;
      xh = 1.5 * pow_reject;
      yl = 0.0;
      yh = T;
      cpgswin(xl, xh, yl, yh);
      cpgbox("BCST", 0.0, 0, "BST", 0.0, 0);
      cpgscr(maxcol, 1.0, 0.0, 0.0);
      cpgsci(maxcol);           /* Red */
      yarr[0] = yl;
      yarr[1] = yh;
      xarr[0] = xarr[1] = datapow_med;
      cpgline(2, xarr, yarr);
      cpgsls(4);                /* Dotted line */
      xarr[0] = xarr[1] = pow_reject;
      cpgline(2, xarr, yarr);
      cpgsls(1);                /* Solid line */
      cpgsci(1);                /* Default color */
      cpgline(numint, pow_int_med, times);
      yl = 0.0;
      yh = numint;
      cpgswin(xl, xh, yl, yh);
      cpgbox("", 0.0, 0, "CMST", 0.0, 0);
      /* cpgmtxt("R", 2.3, 0.5, 0.5, "Interval Number"); */

      /*  Max Power versus Channel */

      left = lm;
      right = lm + ft;
      bottom = bm + fh;
      top = bm + fh + th;
      cpgsvp(left, right, bottom, top);
      find_min_max_arr(numchan, pow_chan_med, &min, &max);
      xl = 0.0;
      xh = numchan;
      yl = 0.0;
      yh = 1.5 * pow_reject;
      cpgswin(xl, xh, yl, yh);
      cpgbox("BST", 0.0, 0, "BCST", 0.0, 0);
      cpgscr(maxcol, 1.0, 0.0, 0.0);
      cpgsci(maxcol);           /* Red */
      xarr[0] = xl;
      xarr[1] = xh;
      yarr[0] = yarr[1] = datapow_med;
      cpgline(2, xarr, yarr);
      cpgsls(4);                /* Dotted line */
      yarr[0] = yarr[1] = pow_reject;
      cpgline(2, xarr, yarr);
      cpgsls(1);                /* Solid line */
      cpgsci(1);                /* Default color */
      cpgline(numchan, chans, pow_chan_med);
      xl = lof;
      xh = hif;
      cpgswin(xl, xh, yl, yh);
      cpgbox("CMST", 0.0, 0, "", 0.0, 0);
      cpgmtxt("T", 1.8, 0.5, 0.5, "Frequency (MHz)");

      /* Standard Deviations */

      left = lm + ft + 2.0 * tt;
      right = lm + 2.0 * ft + 2.0 * tt;
      bottom = bm;
      top = bm + fh;
      xl = 0.0;
      xh = numchan;
      yl = 0.0;
      yh = T;
      cpgsvp(left, right, bottom, top);
      cpgswin(xl, xh, yl, yh);
      cpgscr(mincol, 0.7, 1.0, 0.7);    /* Light Green */
      cpgscr(maxcol, 0.3, 1.0, 0.3);    /* Dark Green */
      locut = datastd_med - timesigma * datastd_std;
      hicut = datastd_med + timesigma * datastd_std;
      tr[2] = tr[4] = 0.0;
      tr[1] = (xh - xl) / numchan;
      tr[0] = xl - (tr[1] / 2);
      tr[5] = (yh - yl) / numint;
      tr[3] = yl - (tr[5] / 2);
      cpgimag(datastd[0], numchan, numint, 1, numchan, 1, numint, locut, hicut, tr);
      cpgswin(xl, xh, yl, yh);
      cpgbox("BNST", 0.0, 0, "BNST", 0.0, 0);
      cpgmtxt("B", 2.6, 0.5, 0.5, "Channel");
      xl = lof;
      xh = hif;
      yl = 0.0;
      yh = numint;
      cpgswin(xl, xh, yl, yh);
      cpgbox("CST", 0.0, 0, "CST", 0.0, 0);

      /* Data Sigma Label */

      left = lm + 2.0 * ft + 2.0 * tt;
      right = lm + 2.0 * ft + 3.0 * tt;
      bottom = bm + fh;
      top = bm + fh + th;
      cpgsvp(left, right, bottom, top);
      cpgswin(0.0, 1.0, 0.0, 1.0);
      cpgscr(maxcol, 0.0, 1.0, 0.0);
      cpgsci(maxcol);           /* Green */
      cpgptxt(0.5, 0.7, 0.0, 0.5, "Data");
      cpgptxt(0.5, 0.3, 0.0, 0.5, "Sigma");
      cpgsci(1);                /* Default color */

      /*  Data Sigma versus Time */

      left = lm + 2.0 * ft + 2.0 * tt;
      right = lm + 2.0 * ft + 3.0 * tt;
      bottom = bm;
      top = bm + fh;
      cpgsvp(left, right, bottom, top);
      xl = datastd_med - 2.0 * std_reject;
      xh = datastd_med + 2.0 * std_reject;
      yl = 0.0;
      yh = T;
      cpgswin(xl, xh, yl, yh);
      cpgbox("BCST", 0.0, 0, "BST", 0.0, 0);
      cpgscr(maxcol, 0.0, 1.0, 0.0);
      cpgsci(maxcol);           /* Green */
      yarr[0] = yl;
      yarr[1] = yh;
      xarr[0] = xarr[1] = datastd_med;
      cpgline(2, xarr, yarr);
      cpgsls(4);                /* Dotted line */
      xarr[0] = xarr[1] = datastd_med + std_reject;
      cpgline(2, xarr, yarr);
      xarr[0] = xarr[1] = datastd_med - std_reject;
      cpgline(2, xarr, yarr);
      cpgsls(1);                /* Solid line */
      cpgsci(1);                /* Default color */
      cpgline(numint, std_int_med, times);
      yl = 0.0;
      yh = numint;
      cpgswin(xl, xh, yl, yh);
      cpgbox("", 0.0, 0, "CMST", 0.0, 0);
      /* cpgmtxt("R", 2.3, 0.5, 0.5, "Interval Number"); */

      /*  Data Sigma versus Channel */

      left = lm + ft + 2.0 * tt;
      right = lm + 2.0 * ft + 2.0 * tt;
      bottom = bm + fh;
      top = bm + fh + th;
      cpgsvp(left, right, bottom, top);
      xl = 0.0;
      xh = numchan;
      yl = datastd_med - 2.0 * std_reject;
      yh = datastd_med + 2.0 * std_reject;
      cpgswin(xl, xh, yl, yh);
      cpgbox("BST", 0.0, 0, "BCST", 0.0, 0);
      cpgscr(maxcol, 0.0, 1.0, 0.0);
      cpgsci(maxcol);           /* Green */
      xarr[0] = xl;
      xarr[1] = xh;
      yarr[0] = yarr[1] = datastd_med;
      cpgline(2, xarr, yarr);
      cpgsls(4);                /* Dotted line */
      yarr[0] = yarr[1] = datastd_med + std_reject;
      cpgline(2, xarr, yarr);
      yarr[0] = yarr[1] = datastd_med - std_reject;
      cpgline(2, xarr, yarr);
      cpgsls(1);                /* Solid line */
      cpgsci(1);                /* Default color */
      cpgline(numchan, chans, std_chan_med);
      xl = lof;
      xh = hif;
      cpgswin(xl, xh, yl, yh);
      cpgbox("CMST", 0.0, 0, "", 0.0, 0);
      cpgmtxt("T", 1.8, 0.5, 0.5, "Frequency (MHz)");

      /* Data Mean */

      left = lm + 2.0 * ft + 4.0 * tt;
      right = lm + 3.0 * ft + 4.0 * tt;
      bottom = bm;
      top = bm + fh;
      xl = 0.0;
      xh = numchan;
      yl = 0.0;
      yh = T;
      cpgsvp(left, right, bottom, top);
      cpgswin(xl, xh, yl, yh);
      cpgscr(mincol, 0.7, 0.7, 1.0);    /* Light Blue */
      cpgscr(maxcol, 0.3, 0.3, 1.0);    /* Dark Blue */
      locut = dataavg_med - timesigma * dataavg_std;
      hicut = dataavg_med + timesigma * dataavg_std;
      tr[2] = tr[4] = 0.0;
      tr[1] = (xh - xl) / numchan;
      tr[0] = xl - (tr[1] / 2);
      tr[5] = (yh - yl) / numint;
      tr[3] = yl - (tr[5] / 2);
      cpgimag(dataavg[0], numchan, numint, 1, numchan, 1, numint, locut, hicut, tr);
      cpgswin(xl, xh, yl, yh);
      cpgbox("BNST", 0.0, 0, "BNST", 0.0, 0);
      cpgmtxt("B", 2.6, 0.5, 0.5, "Channel");
      xl = lof;
      xh = hif;
      yl = 0.0;
      yh = numint;
      cpgswin(xl, xh, yl, yh);
      cpgbox("CST", 0.0, 0, "CST", 0.0, 0);

      /* Data Mean Label */

      left = lm + 3.0 * ft + 4.0 * tt;
      right = lm + 3.0 * ft + 5.0 * tt;
      bottom = bm + fh;
      top = bm + fh + th;
      cpgsvp(left, right, bottom, top);
      cpgswin(0.0, 1.0, 0.0, 1.0);
      cpgscr(maxcol, 0.0, 0.0, 1.0);
      cpgsci(maxcol);           /* Blue */
      cpgptxt(0.5, 0.7, 0.0, 0.5, "Data");
      cpgptxt(0.5, 0.3, 0.0, 0.5, "Mean");
      cpgsci(1);                /* Default color */

      /*  Data Mean versus Time */

      left = lm + 3.0 * ft + 4.0 * tt;
      right = lm + 3.0 * ft + 5.0 * tt;
      bottom = bm;
      top = bm + fh;
      cpgsvp(left, right, bottom, top);
      xl = dataavg_med - 2.0 * avg_reject;
      xh = dataavg_med + 2.0 * avg_reject;
      yl = 0.0;
      yh = T;
      cpgswin(xl, xh, yl, yh);
      cpgbox("BCST", 0.0, 0, "BST", 0.0, 0);
      cpgscr(maxcol, 0.0, 0.0, 1.0);
      cpgsci(maxcol);           /* Blue */
      yarr[0] = yl;
      yarr[1] = yh;
      xarr[0] = xarr[1] = dataavg_med;
      cpgline(2, xarr, yarr);
      cpgsls(4);                /* Dotted line */
      xarr[0] = xarr[1] = dataavg_med + avg_reject;
      cpgline(2, xarr, yarr);
      xarr[0] = xarr[1] = dataavg_med - avg_reject;
      cpgline(2, xarr, yarr);
      cpgsls(1);                /* Solid line */
      cpgsci(1);                /* Default color */
      cpgline(numint, avg_int_med, times);
      yl = 0.0;
      yh = numint;
      cpgswin(xl, xh, yl, yh);
      cpgbox("", 0.0, 0, "CMST", 0.0, 0);

      /*  Data Mean versus Channel */

      left = lm + 2.0 * ft + 4.0 * tt;
      right = lm + 3.0 * ft + 4.0 * tt;
      bottom = bm + fh;
      top = bm + fh + th;
      cpgsvp(left, right, bottom, top);
      xl = 0.0;
      xh = numchan;
      yl = dataavg_med - 2.0 * avg_reject;
      yh = dataavg_med + 2.0 * avg_reject;
      cpgswin(xl, xh, yl, yh);
      cpgbox("BST", 0.0, 0, "BCST", 0.0, 0);
      cpgscr(maxcol, 0.0, 0.0, 1.0);
      cpgsci(maxcol);           /* Blue */
      xarr[0] = xl;
      xarr[1] = xh;
      yarr[0] = yarr[1] = dataavg_med;
      cpgline(2, xarr, yarr);
      cpgsls(4);                /* Dotted line */
      yarr[0] = yarr[1] = dataavg_med + avg_reject;
      cpgline(2, xarr, yarr);
      yarr[0] = yarr[1] = dataavg_med - avg_reject;
      cpgline(2, xarr, yarr);
      cpgsls(1);                /* Solid line */
      cpgsci(1);                /* Default color */
      cpgline(numchan, chans, avg_chan_med);
      xl = lof;
      xh = hif;
      cpgswin(xl, xh, yl, yh);
      cpgbox("CMST", 0.0, 0, "", 0.0, 0);
      cpgmtxt("T", 1.8, 0.5, 0.5, "Frequency (MHz)");

      {                         /* Add the Data Info area */
         char out[200], out2[100];
         float dy = 0.025;

         cpgsvp(0.0, 1.0, 0.0, 1.0);
         cpgswin(0.0, 1.0, 0.0, 1.0);
         left = lm + ft + 1.5 * tt;
         top = 1.0 - tm;
         cpgsch(1.0);
         sprintf(out, "%-s", idata->name);
         cpgptxt(0.5, 1.0 - 0.5 * tm, 0.0, 0.5, out);
         cpgsch(0.8);

         sprintf(out, "Object:");
         cpgtext(left + 0.0, top - 0 * dy, out);
         sprintf(out, "%-s", idata->object);
         cpgtext(left + 0.1, top - 0 * dy, out);
         sprintf(out, "Telescope:");
         cpgtext(left + 0.0, top - 1 * dy, out);
         sprintf(out, "%-s", idata->telescope);
         cpgtext(left + 0.1, top - 1 * dy, out);
         sprintf(out, "Instrument:");
         cpgtext(left + 0.0, top - 2 * dy, out);
         sprintf(out, "%-s", idata->instrument);
         cpgtext(left + 0.1, top - 2 * dy, out);
         ra_dec_to_string(out2, idata->ra_h, idata->ra_m, idata->ra_s);
         sprintf(out, "RA\\dJ2000\\u");
         cpgtext(left + 0.0, top - 3 * dy, out);
         sprintf(out, "= %-s", out2);
         cpgtext(left + 0.08, top - 3 * dy, out);
         ra_dec_to_string(out2, idata->dec_d, idata->dec_m, idata->dec_s);
         sprintf(out, "DEC\\dJ2000\\u");
         cpgtext(left + 0.0, top - 4 * dy, out);
         sprintf(out, "= %-s", out2);
         cpgtext(left + 0.08, top - 4 * dy, out);
         sprintf(out, "Epoch\\dtopo\\u");
         cpgtext(left + 0.0, top - 5 * dy, out);
         sprintf(out, "= %-.11f", idata->mjd_i + idata->mjd_f);
         cpgtext(left + 0.08, top - 5 * dy, out);
         sprintf(out, "T\\dsample\\u (s)");
         cpgtext(left + 0.0, top - 6 * dy, out);
         sprintf(out, "= %g", idata->dt);
         cpgtext(left + 0.08, top - 6 * dy, out);
         sprintf(out, "T\\dtotal\\u (s)");
         cpgtext(left + 0.0, top - 7 * dy, out);
         sprintf(out, "= %g", T);
         cpgtext(left + 0.08, top - 7 * dy, out);

         left = lm + ft + 7.8 * tt;
         sprintf(out, "Num channels");
         cpgtext(left + 0.0, top - 0 * dy, out);
         sprintf(out, "= %-d", numchan);
         cpgtext(left + 0.12, top - 0 * dy, out);
         sprintf(out, "Pts per int");
         cpgtext(left + 0.19, top - 0 * dy, out);
         sprintf(out, "= %-d", ptsperint);
         cpgtext(left + 0.29, top - 0 * dy, out);
         sprintf(out, "Num intervals");
         cpgtext(left + 0.0, top - 1 * dy, out);
         sprintf(out, "= %-d", numint);
         cpgtext(left + 0.12, top - 1 * dy, out);
         sprintf(out, "Time per int");
         cpgtext(left + 0.19, top - 1 * dy, out);
         sprintf(out, "= %-g", inttim);
         cpgtext(left + 0.29, top - 1 * dy, out);
         sprintf(out, "Power:");
         cpgtext(left + 0.0, top - 2 * dy, out);
         sprintf(out, "median");
         cpgtext(left + 0.06, top - 2 * dy, out);
         sprintf(out, "= %-.3f", datapow_med);
         cpgtext(left + 0.12, top - 2 * dy, out);
         sprintf(out, "\\gs");
         cpgtext(left + 0.21, top - 2 * dy, out);
         sprintf(out, "= %-.3g", datapow_std);
         cpgtext(left + 0.245, top - 2 * dy, out);
         find_min_max_arr(numint * numchan, datapow[0], &min, &max);
         sprintf(out, "min");
         cpgtext(left + 0.06, top - 3 * dy, out);
         sprintf(out, "= %-.3f", min);
         cpgtext(left + 0.12, top - 3 * dy, out);
         sprintf(out, "max");
         cpgtext(left + 0.21, top - 3 * dy, out);
         sprintf(out, "= %-.3f", max);
         cpgtext(left + 0.245, top - 3 * dy, out);
         sprintf(out, "Sigma:");
         cpgtext(left + 0.0, top - 4 * dy, out);
         sprintf(out, "median");
         cpgtext(left + 0.06, top - 4 * dy, out);
         sprintf(out, "= %-.3f", datastd_med);
         cpgtext(left + 0.12, top - 4 * dy, out);
         sprintf(out, "\\gs");
         cpgtext(left + 0.21, top - 4 * dy, out);
         sprintf(out, "= %-.3g", datastd_std);
         cpgtext(left + 0.245, top - 4 * dy, out);
         find_min_max_arr(numint * numchan, datastd[0], &min, &max);
         sprintf(out, "min");
         cpgtext(left + 0.06, top - 5 * dy, out);
         sprintf(out, "= %-.3f", min);
         cpgtext(left + 0.12, top - 5 * dy, out);
         sprintf(out, "max");
         cpgtext(left + 0.21, top - 5 * dy, out);
         sprintf(out, "= %-.3f", max);
         cpgtext(left + 0.245, top - 5 * dy, out);
         sprintf(out, "Mean:");
         cpgtext(left + 0.0, top - 6 * dy, out);
         sprintf(out, "median");
         cpgtext(left + 0.06, top - 6 * dy, out);
         sprintf(out, "= %-.3f", dataavg_med);
         cpgtext(left + 0.12, top - 6 * dy, out);
         sprintf(out, "\\gs");
         cpgtext(left + 0.21, top - 6 * dy, out);
         sprintf(out, "= %-.3g", dataavg_std);
         cpgtext(left + 0.245, top - 6 * dy, out);
         find_min_max_arr(numint * numchan, dataavg[0], &min, &max);
         sprintf(out, "min");
         cpgtext(left + 0.06, top - 7 * dy, out);
         sprintf(out, "= %-.3f", min);
         cpgtext(left + 0.12, top - 7 * dy, out);
         sprintf(out, "max");
         cpgtext(left + 0.21, top - 7 * dy, out);
         sprintf(out, "= %-.3f", max);
         cpgtext(left + 0.245, top - 7 * dy, out);
      }

      {                         /* Plot the Mask */
         unsigned char byte;
         char temp[200];
         float **plotmask, rr, gg, bb, page;

         plotmask = gen_fmatrix(numint, numchan);
         for (ii = 0; ii < numint; ii++) {
            for (jj = 0; jj < numchan; jj++) {
               byte = bytemask[ii][jj];
               plotmask[ii][jj] = 0.0;
               if (byte & PADDING)
                  plotmask[ii][jj] = 1.0;
               if (byte & OLDMASK)
                  plotmask[ii][jj] = 2.0;
               if (byte & USERZAP)
                  plotmask[ii][jj] = 3.0;
               if (byte & BAD_POW)
                  plotmask[ii][jj] = 4.0;
               else if (byte & BAD_AVG)
                  plotmask[ii][jj] = 5.0;
               else if (byte & BAD_STD)
                  plotmask[ii][jj] = 6.0;
            }
         }
         /* Set the colors */
         numcol = 7;
         maxcol = mincol + numcol - 1;
         cpgscir(mincol, maxcol);
         cpgqcr(0, &rr, &gg, &bb);
         cpgscr(mincol + 0, rr, gg, bb);        /* GOODDATA = background */
         cpgscr(mincol + 1, 0.7, 0.7, 0.7);     /* PADDING  = light grey */
         cpgscr(mincol + 2, 0.3, 0.3, 0.3);     /* OLDMASK  = dark grey */
         cpgqcr(1, &rr, &gg, &bb);
         cpgscr(mincol + 3, rr, gg, bb);        /* USERZAP  = foreground */
         cpgscr(mincol + 4, 1.0, 0.0, 0.0);     /* BAD+POW  = red */
         cpgscr(mincol + 5, 0.0, 0.0, 1.0);     /* BAD+AVG  = blue */
         cpgscr(mincol + 6, 0.0, 1.0, 0.0);     /* BAD+STD  = green */
         /* Prep the image */
         for (page = 0; page <= 1; page++) {
            xl = 0.0;
            xh = numchan;
            yl = 0.0;
            yh = T;
            locut = 0.0;
            hicut = 6.0;
            tr[2] = tr[4] = 0.0;
            tr[1] = (xh - xl) / numchan;
            tr[0] = xl - (tr[1] / 2);
            tr[5] = (yh - yl) / numint;
            tr[3] = yl - (tr[5] / 2);
            if (page == 0) {
               left = lm + 3.0 * ft + 6.0 * tt;
               right = lm + 4.0 * ft + 6.0 * tt;
               bottom = bm;
               top = bm + fh;
            } else {
               cpgpage();
               cpgiden();
               left = 0.06;
               right = 0.94;
               bottom = 0.06;
               top = 0.88;
            }
            cpgsvp(left, right, bottom, top);
            cpgswin(xl, xh, yl, yh);
            cpgimag(plotmask[0], numchan, numint, 1,
                    numchan, 1, numint, locut, hicut, tr);
            cpgswin(xl, xh, yl, yh);
            cpgbox("BNST", 0.0, 0, "BNST", 0.0, 0);
            cpgmtxt("B", 2.6, 0.5, 0.5, "Channel");
            if (page)
               cpgmtxt("L", 2.1, 0.5, 0.5, "Time (s)");
            xl = lof;
            xh = hif;
            yl = 0.0;
            yh = numint;
            cpgswin(xl, xh, yl, yh);
            cpgbox("CMST", 0.0, 0, "CMST", 0.0, 0);
            cpgmtxt("T", 1.8, 0.5, 0.5, "Frequency (MHz)");
            cpgmtxt("R", 2.3, 0.5, 0.5, "Interval Number");
            /* Add the Labels */
            cpgsvp(0.0, 1.0, 0.0, 1.0);
            cpgswin(0.0, 1.0, 0.0, 1.0);
            cpgsch(0.8);
            if (page == 0) {
               cpgsci(mincol + 1);
               cpgptxt(left, top + 0.1, 0.0, 0.0, "Padding");
               cpgsci(mincol + 2);
               cpgptxt(left, top + 0.08, 0.0, 0.0, "Old Mask");
               cpgsci(mincol + 3);
               cpgptxt(left, top + 0.06, 0.0, 0.0, "User Zap");
               cpgsci(mincol + 4);
               cpgptxt(right, top + 0.1, 0.0, 1.0, "Power");
               cpgsci(mincol + 6);
               cpgptxt(right, top + 0.08, 0.0, 1.0, "Sigma");
               cpgsci(mincol + 5);
               cpgptxt(right, top + 0.06, 0.0, 1.0, "Mean");
               cpgsci(1);
            } else {
               cpgsci(mincol + 1);
               cpgptxt(1.0 / 12.0, 0.955, 0.0, 0.5, "Padding");
               cpgsci(mincol + 2);
               cpgptxt(3.0 / 12.0, 0.955, 0.0, 0.5, "Old Mask");
               cpgsci(mincol + 3);
               cpgptxt(5.0 / 12.0, 0.955, 0.0, 0.5, "User Zap");
               cpgsci(mincol + 4);
               cpgptxt(7.0 / 12.0, 0.955, 0.0, 0.5, "Max Power");
               cpgsci(mincol + 6);
               cpgptxt(9.0 / 12.0, 0.955, 0.0, 0.5, "Data Sigma");
               cpgsci(mincol + 5);
               cpgptxt(11.0 / 12.0, 0.955, 0.0, 0.5, "Data Mean");
               cpgsci(1);
               cpgsch(0.9);
               sprintf(temp, "Recommended Mask for '%-s'", idata->name);
               cpgptxt(0.5, 0.985, 0.0, 0.5, temp);
            }
         }
         vect_free(plotmask[0]);
         vect_free(plotmask);
      }

      if (ct == 0)
         printf("There are %d RFI instances.\n\n", numrfi);

      if ((ct == 0 && rfips) || (ct == 1 && rfixwin)) { /* Plot the RFI instances */
         int maxcol, mincol, numperpage = 25, numtoplot;
         float dy = 0.035, top = 0.95, rr, gg, bb;
         char temp[200];

         qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_freq);
         /* qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_sigma); */
         for (ii = 0; ii <= (numrfi - 1) / numperpage; ii++) {
            cpgpage();
            cpgiden();
            cpgsvp(0.0, 1.0, 0.0, 1.0);
            cpgswin(0.0, 1.0, 0.0, 1.0);
            cpgsch(0.8);
            sprintf(temp, "%-s", idata->name);
            cpgtext(0.05, 0.985, temp);
            cpgsch(0.6);
            sprintf(temp, "Freq (Hz)");
            cpgptxt(0.03, 0.96, 0.0, 0.0, temp);
            sprintf(temp, "Period (ms)");
            cpgptxt(0.12, 0.96, 0.0, 0.0, temp);
            sprintf(temp, "Sigma");
            cpgptxt(0.21, 0.96, 0.0, 0.0, temp);
            sprintf(temp, "Number");
            cpgptxt(0.27, 0.96, 0.0, 0.0, temp);
            cpgsvp(0.33, 0.64, top - dy, top);
            cpgswin(lof, hif, 0.0, 1.0);
            cpgbox("CIMST", 0.0, 0, "", 0.0, 0);
            cpgmtxt("T", 2.5, 0.5, 0.5, "Frequency (MHz)");
            cpgsvp(0.65, 0.96, top - dy, top);
            cpgswin(0.0, T, 0.0, 1.0);
            cpgbox("CIMST", 0.0, 0, "", 0.0, 0);
            cpgmtxt("T", 2.5, 0.5, 0.5, "Time (s)");
            cpgqcir(&mincol, &maxcol);
            maxcol = mincol + 1;
            cpgscir(mincol, maxcol);
            cpgqcr(0, &rr, &gg, &bb);
            cpgscr(mincol, rr, gg, bb); /* background */
            cpgqcr(1, &rr, &gg, &bb);
            /* cpgscr(maxcol, rr, gg, bb);  foreground */
            cpgscr(maxcol, 0.5, 0.5, 0.5);      /* grey */
            if (ii == (numrfi - 1) / numperpage)
               numtoplot = numrfi % numperpage;
            else
               numtoplot = numperpage;
            for (jj = 0; jj < numtoplot; jj++)
               plot_rfi(rfivect + ii * numperpage + jj,
                        top - jj * dy, numint, numchan, T, lof, hif);
            cpgsvp(0.33, 0.64, top - jj * dy, top - (jj - 1) * dy);
            cpgswin(0.0, numchan, 0.0, 1.0);
            cpgbox("BINST", 0.0, 0, "", 0.0, 0);
            cpgmtxt("B", 2.5, 0.5, 0.5, "Channel");
            cpgsvp(0.65, 0.96, top - jj * dy, top - (jj - 1) * dy);
            cpgswin(0.0, numint, 0.0, 1.0);
            cpgbox("BINST", 0.0, 0, "", 0.0, 0);
            cpgmtxt("B", 2.5, 0.5, 0.5, "Interval");
         }
      }
      cpgclos();
   }                            /* Plot for loop */

   /* Free our arrays */

   vect_free(freqs);
   vect_free(chans);
   vect_free(times);
   vect_free(ints);
   vect_free(avg_chan_avg);
   vect_free(std_chan_avg);
   vect_free(pow_chan_avg);
   vect_free(avg_int_avg);
   vect_free(std_int_avg);
   vect_free(pow_int_avg);
   vect_free(avg_chan_med);
   vect_free(std_chan_med);
   vect_free(pow_chan_med);
   vect_free(avg_int_med);
   vect_free(std_int_med);
   vect_free(pow_int_med);
   vect_free(avg_chan_std);
   vect_free(std_chan_std);
   vect_free(pow_chan_std);
   vect_free(avg_int_std);
   vect_free(std_int_std);
   vect_free(pow_int_std);
}
Beispiel #26
0
/* OLD Clipping Routine (uses channel medians) */
int old_clip_times(float *rawdata, int ptsperblk, int numchan, float clip_sigma,
                   float *good_chan_levels)
// Perform time-domain clipping of rawdata.  This is a 2D array with
// ptsperblk*numchan points, each of which is a float.  The clipping
// is done at clip_sigma sigma above/below the running mean.  The
// up-to-date running averages of the channels are returned in
// good_chan_levels (which must be pre-allocated).
{
    static float *median_chan_levels;
    static float running_avg = 0.0, running_std = 0.0, median_sum = 0.0;
    static int blocksread = 0, firsttime = 1;
    float *zero_dm_block, *median_temp;
    float current_med, trigger, running_wgt = 0.05;
    double current_avg = 0.0, current_std = 0.0, scaling;
    float *powptr;
    int ii, jj, clipit = 0, clipped = 0;
    if (firsttime) {
        median_chan_levels = gen_fvect(numchan);
        firsttime = 0;
    }
    zero_dm_block = gen_fvect(ptsperblk);
    median_temp = gen_fvect(ptsperblk);

    /* Calculate the zero DM time series */
    for (ii = 0; ii < ptsperblk; ii++) {
        zero_dm_block[ii] = 0.0;
        powptr = rawdata + ii * numchan;
        for (jj = 0; jj < numchan; jj++)
            zero_dm_block[ii] += *powptr++;
        median_temp[ii] = zero_dm_block[ii];
    }
    current_med = median(median_temp, ptsperblk);

    /* Calculate the current standard deviation and mean  */
    /* but only for data points that are within a certain */
    /* fraction of the median value.  This removes the    */
    /* really strong RFI from the calculation.            */
    {
        float lo_cutoff, hi_cutoff;
        int numgoodpts = 0;
        lo_cutoff = 0.7 * current_med;
        hi_cutoff = 1.3 * current_med;

        /* Find the "good" points */
        for (ii = 0; ii < ptsperblk; ii++) {
            if (zero_dm_block[ii] > lo_cutoff && zero_dm_block[ii] < hi_cutoff) {
                median_temp[numgoodpts] = zero_dm_block[ii];
                numgoodpts++;
            }
        }

        /* Calculate the current average and stddev */
        if (numgoodpts < 1) {
            current_avg = running_avg;
            current_std = running_std;
        } else {
            avg_var(median_temp, numgoodpts, &current_avg, &current_std);
            current_std = sqrt(current_std);
        }
    }

    /* Update a pseudo running average and stdev */
    if (blocksread) {
        running_avg =
            (running_avg * (1.0 - running_wgt) + running_wgt * current_avg);
        running_std =
            (running_std * (1.0 - running_wgt) + running_wgt * current_std);
    } else {
        running_avg = current_avg;
        running_std = current_std;
        if (running_avg == 0.0 || current_avg == 0.0)
            printf("BAD RFI IN BLOCK#1!!!\n\n");
    }

    /* See if any points need clipping */
    trigger = clip_sigma * running_std;
    for (ii = 0; ii < ptsperblk; ii++) {
        if (fabs(zero_dm_block[ii] - running_avg) > trigger) {
            clipit = 1;
            break;
        }
    }

    /* Calculate the channel medians if required */
    if ((blocksread % BLOCKSTOSKIP == 0 && clipit == 0) || blocksread == 0) {
        median_sum = 0.0;
        for (ii = 0; ii < numchan; ii++) {
            powptr = rawdata + ii;
            for (jj = 0; jj < ptsperblk; jj++)
                median_temp[jj] = *(powptr + jj * numchan);
            median_chan_levels[ii] = median(median_temp, ptsperblk);
            median_sum += median_chan_levels[ii];
        }
    }

    /* Update the good channel levels */
    scaling = running_avg / median_sum;
    for (ii = 0; ii < numchan; ii++)
        good_chan_levels[ii] = median_chan_levels[ii] * scaling;

    /* Replace the bad channel data with channel median values */
    /* that are scaled to equal the running_avg.               */
    if (clipit) {
        for (ii = 0; ii < ptsperblk; ii++) {
            if (fabs(zero_dm_block[ii] - running_avg) > trigger) {
                powptr = rawdata + ii * numchan;
                for (jj = 0; jj < numchan; jj++)
                    *powptr++ = good_chan_levels[jj];
                clipped++;
            }
        }
    }
    blocksread++;
    vect_free(zero_dm_block);
    vect_free(median_temp);
    return clipped;
}
Beispiel #27
0
/* NEW Clipping Routine (uses channel running averages) */
int clip_times(float *rawdata, int ptsperblk, int numchan, float clip_sigma,
               float *good_chan_levels)
// Perform time-domain clipping of rawdata.  This is a 2D array with
// ptsperblk*numchan points, each of which is a float.  The clipping
// is done at clip_sigma sigma above/below the running mean.  The
// up-to-date running averages of the channels are returned in
// good_chan_levels (which must be pre-allocated).
{
    static float *chan_running_avg;
    static float running_avg = 0.0, running_std = 0.0;
    static int blocksread = 0, firsttime = 1;
    static long long current_point = 0;
    static int numonoff = 0, onoffindex = 0;
    static long long *onbins = NULL, *offbins = NULL;
    float *zero_dm_block, *ftmp, *powptr;
    double *chan_avg_temp;
    float current_med, trigger;
    double current_avg = 0.0, current_std = 0.0;
    int ii, jj, clipit = 0, clipped = 0;
    if (firsttime) {
        chan_running_avg = gen_fvect(numchan);
        firsttime = 0;
        {                       // This is experimental code to zap radar-filled data
            char *envval = getenv("CLIPBINSFILE");
            if (envval != NULL) {
                FILE *onofffile = chkfopen(envval, "r");
                numonoff = read_onoff_paris(onofffile, &onbins, &offbins);
                fclose(onofffile);
                printf("\nRead %d bin clipping pairs from '%s'.\n", numonoff,
                       envval);

                //for (ii=0;ii<numonoff;ii++) printf("%lld %lld\n", onbins[ii], offbins[ii]);
            }
        }
    }
    chan_avg_temp = gen_dvect(numchan);
    zero_dm_block = gen_fvect(ptsperblk);
    ftmp = gen_fvect(ptsperblk);

    /* Calculate the zero DM time series */
    for (ii = 0; ii < ptsperblk; ii++) {
        zero_dm_block[ii] = 0.0;
        powptr = rawdata + ii * numchan;
        for (jj = 0; jj < numchan; jj++)
            zero_dm_block[ii] += *powptr++;
        ftmp[ii] = zero_dm_block[ii];
    }
    avg_var(ftmp, ptsperblk, &current_avg, &current_std);
    current_std = sqrt(current_std);
    current_med = median(ftmp, ptsperblk);

    /* Calculate the current standard deviation and mean  */
    /* but only for data points that are within a certain */
    /* fraction of the median value.  This removes the    */
    /* really strong RFI from the calculation.            */
    {
        float lo_cutoff, hi_cutoff;
        int numgoodpts = 0;
        lo_cutoff = current_med - 3.0 * current_std;
        hi_cutoff = current_med + 3.0 * current_std;;
        for (jj = 0; jj < numchan; jj++)
            chan_avg_temp[jj] = 0.0;

        /* Find the "good" points */
        for (ii = 0; ii < ptsperblk; ii++) {
            if (zero_dm_block[ii] > lo_cutoff && zero_dm_block[ii] < hi_cutoff) {
                ftmp[numgoodpts] = zero_dm_block[ii];
                powptr = rawdata + ii * numchan;
                for (jj = 0; jj < numchan; jj++)
                    chan_avg_temp[jj] += *powptr++;
                numgoodpts++;
            }
        }

        //printf("avg = %f  med = %f  std = %f  numgoodpts = %d\n", 
        //       current_avg, current_med, current_std, numgoodpts);

        /* Calculate the current average and stddev */
        if (numgoodpts < 1) {
            current_avg = running_avg;
            current_std = running_std;
            for (jj = 0; jj < numchan; jj++)
                chan_avg_temp[jj] = chan_running_avg[jj];
        } else {
            avg_var(ftmp, numgoodpts, &current_avg, &current_std);
            current_std = sqrt(current_std);
            for (jj = 0; jj < numchan; jj++)
                chan_avg_temp[jj] /= numgoodpts;
        }
    }

    /* Update a pseudo running average and stdev */
    if (blocksread) {
        running_avg = 0.9 * running_avg + 0.1 * current_avg;
        running_std = 0.9 * running_std + 0.1 * current_std;
        for (ii = 0; ii < numchan; ii++)
            chan_running_avg[ii] =
                0.9 * chan_running_avg[ii] + 0.1 * chan_avg_temp[ii];
    } else {
        running_avg = current_avg;
        running_std = current_std;
        for (ii = 0; ii < numchan; ii++)
            chan_running_avg[ii] = chan_avg_temp[ii];
        if (current_avg == 0.0)
            printf("Warning:  problem with clipping in first block!!!\n\n");
    }

    /* See if any points need clipping */
    trigger = clip_sigma * running_std;
    for (ii = 0; ii < ptsperblk; ii++) {
        if (fabs(zero_dm_block[ii] - running_avg) > trigger) {
            clipit = 1;
            break;
        }
    }

    /* or alternatively from the CLIPBINSFILE */
    if (numonoff && ((current_point > onbins[onoffindex]
                      && current_point <= offbins[onoffindex])
                     || (current_point + ptsperblk > onbins[onoffindex]
                         && current_point + ptsperblk <= offbins[onoffindex])
                     || (current_point < onbins[onoffindex]
                         && current_point + ptsperblk > offbins[onoffindex])))
        clipit = 1;

    /* Update the good channel levels */
    for (ii = 0; ii < numchan; ii++)
        good_chan_levels[ii] = chan_running_avg[ii];

    /* Replace the bad channel data with channel median values */
    /* that are scaled to equal the running_avg.               */
    if (clipit) {
        for (ii = 0; ii < ptsperblk; ii++) {
            if ((fabs(zero_dm_block[ii] - running_avg) > trigger) ||
                (numonoff && (current_point > onbins[onoffindex]
                              && current_point <= offbins[onoffindex]))) {
                powptr = rawdata + ii * numchan;
                for (jj = 0; jj < numchan; jj++)
                    *powptr++ = good_chan_levels[jj];
                clipped++;

                //fprintf(stderr, "zapping %lld\n", current_point);
            }
            current_point++;
            if (numonoff && current_point > offbins[onoffindex]
                && onoffindex < numonoff - 1) {
                while (current_point > offbins[onoffindex]
                       && onoffindex < numonoff - 1)
                    onoffindex++;

                //printf("updating index to %d\n", onoffindex);
            }
        }
    } else {
        current_point += ptsperblk;
        if (numonoff && current_point > offbins[onoffindex]
            && onoffindex < numonoff - 1) {
            while (current_point > offbins[onoffindex]
                   && onoffindex < numonoff - 1)
                onoffindex++;

            //printf("updating index to %d\n", onoffindex);
        }
    }
    blocksread++;
    vect_free(chan_avg_temp);
    vect_free(zero_dm_block);
    vect_free(ftmp);
    return clipped;
}
Beispiel #28
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;
    */
}
Beispiel #29
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);
}
Beispiel #30
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);
}