Esempio n. 1
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;
      }
   }
}
Esempio n. 2
0
static void read_statsfile(char *statsfilenm, float ***datapow,
                           float ***dataavg, float ***datastd,
                           int *numchan, int *numint, int *ptsperint,
                           int *lobin, int *numbetween)
{
   FILE *outfile;

   outfile = chkfopen(statsfilenm, "rb");
   chkfread(numchan, sizeof(int), 1, outfile);
   chkfread(numint, sizeof(int), 1, outfile);
   chkfread(ptsperint, sizeof(int), 1, outfile);
   chkfread(lobin, sizeof(int), 1, outfile);
   chkfread(numbetween, sizeof(int), 1, outfile);
   *dataavg = gen_fmatrix(*numint, *numchan);
   *datastd = gen_fmatrix(*numint, *numchan);
   *datapow = gen_fmatrix(*numint, *numchan);
   chkfread(*(datapow[0]), sizeof(float), *numchan * *numint, outfile);
   chkfread(*(dataavg[0]), sizeof(float), *numchan * *numint, outfile);
   chkfread(*(datastd[0]), sizeof(float), *numchan * *numint, outfile);
   fclose(outfile);
}
Esempio n. 3
0
ffdotpows *copy_ffdotpows(ffdotpows * orig)
{
   int ii;
   ffdotpows *copy;

   copy = (ffdotpows *) malloc(sizeof(ffdotpows));
   copy->numrs = orig->numrs;
   copy->numzs = orig->numzs;
   copy->rlo = orig->rlo;
   copy->zlo = orig->zlo;
   copy->powers = gen_fmatrix(orig->numzs, orig->numrs);
   for (ii = 0; ii < (orig->numzs * orig->numrs); ii++)
      copy->powers[0][ii] = orig->powers[0][ii];
   return copy;
}
Esempio n. 4
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;
}
Esempio n. 5
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);
}
Esempio n. 6
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);
}
Esempio n. 7
0
int main(int argc, char *argv[])
{
   /* Any variable that begins with 't' means topocentric */
   /* Any variable that begins with 'b' means barycentric */
   FILE **outfiles = NULL;
   float **outdata;
   double dtmp, *dms, avgdm = 0.0, dsdt = 0, maxdm;
   double *dispdt, tlotoa = 0.0, blotoa = 0.0, BW_ddelay = 0.0;
   double max = -9.9E30, min = 9.9E30, var = 0.0, avg = 0.0;
   double *btoa = NULL, *ttoa = NULL, avgvoverc = 0.0;
   char obs[3], ephem[10], rastring[50], decstring[50];
   long totnumtowrite, totwrote = 0, padwrote = 0, datawrote = 0;
   int *idispdt, **offsets;
   int ii, jj, numadded = 0, numremoved = 0, padding = 0, good_inputs = 1;
   int numbarypts = 0, numread = 0, numtowrite = 0;
   int padtowrite = 0, statnum = 0;
   int numdiffbins = 0, *diffbins = NULL, *diffbinptr = NULL, good_padvals = 0;
   double local_lodm;
   char *datafilenm, *outpath, *outfilenm, *hostname;
   struct spectra_info s;
   infodata idata;
   mask obsmask;

   MPI_Init(&argc, &argv);
   MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
   MPI_Comm_rank(MPI_COMM_WORLD, &myid);
#ifdef _OPENMP
   omp_set_num_threads(1); // Explicitly turn off OpenMP
#endif
   set_using_MPI();
   {
      FILE *hostfile;
      char tmpname[100];
      int retval;

      hostfile = chkfopen("/etc/hostname", "r");
      retval = fscanf(hostfile, "%s\n", tmpname);
      if (retval==0) {
          printf("Warning:  error reading /etc/hostname on proc %d\n", myid);
      }
      hostname = (char *) calloc(strlen(tmpname) + 1, 1);
      memcpy(hostname, tmpname, strlen(tmpname));
      fclose(hostfile);
   }

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

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

   make_maskbase_struct();
   make_spectra_info_struct();

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

   cmd = parseCmdline(argc, argv);
   spectra_info_set_defaults(&s);
   // If we are zeroDMing, make sure that clipping is off.
   if (cmd->zerodmP) cmd->noclipP = 1;
   s.clip_sigma = cmd->clip;
   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 + 1;
   }
   if (!cmd->numoutP)
      cmd->numout = LONG_MAX;

#ifdef DEBUG
   showOptionValues();
#endif

   if (myid == 0) {             /* Master node only */
      printf("\n\n");
      printf("      Parallel Pulsar Subband De-dispersion Routine\n");
      printf("                 by Scott M. Ransom\n\n");

      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 (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("\nError:  Unable to identify input data files.  Please specify type.\n\n");
              good_inputs = 0;
          }
      }
      // So far we can only handle PSRFITS, filterbank, and subbands
      if (s.datatype!=PSRFITS && 
          s.datatype!=SIGPROCFB && 
          s.datatype!=SUBBAND) good_inputs = 0;

      // For subbanded data
      if (!RAWDATA) s.files = (FILE **)malloc(sizeof(FILE *) * s.num_files);

      if (good_inputs && (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);
          for (ii = 0; ii < s.num_files; ii++) {
              printf("  '%s'\n", cmd->argv[ii]);
              if (insubs) s.files[ii] = chkfopen(s.filenames[ii], "rb");
          }
          printf("\n");
          if (RAWDATA) {
              read_rawdata_files(&s);
              print_spectra_info_summary(&s);
              spectra_info_to_inf(&s, &idata);
          } else { // insubs
              char *root, *suffix;
              cmd->nsub = s.num_files;
              s.N = chkfilelen(s.files[0], sizeof(short));
              s.start_subint = gen_ivect(1);
              s.num_subint = gen_ivect(1);
              s.start_MJD = (long double *)malloc(sizeof(long double));
              s.start_spec = (long long *)malloc(sizeof(long long));
              s.num_spec = (long long *)malloc(sizeof(long long));
              s.num_pad = (long long *)malloc(sizeof(long long));
              s.start_spec[0] = 0L;
              s.start_subint[0] = 0;
              s.num_spec[0] = s.N;
              s.num_subint[0] = s.N / SUBSBLOCKLEN;
              s.num_pad[0] = 0L;
              s.padvals = gen_fvect(s.num_files);
              for (ii = 0 ; ii < ii ; ii++)
                  s.padvals[ii] = 0.0;
              if (split_root_suffix(s.filenames[0], &root, &suffix) == 0) {
                  printf("\nError:  The input filename (%s) must have a suffix!\n\n", s.filenames[0]);
                  exit(1);
              }
              if (strncmp(suffix, "sub", 3) == 0) {
                  char *tmpname;
                  tmpname = calloc(strlen(root) + 10, 1);
                  sprintf(tmpname, "%s.sub", root);
                  readinf(&idata, tmpname);
                  free(tmpname);
                  strncpy(s.telescope, idata.telescope, 40);
                  strncpy(s.backend, idata.instrument, 40);
                  strncpy(s.observer, idata.observer, 40);
                  strncpy(s.source, idata.object, 40);
                  s.ra2000 = hms2rad(idata.ra_h, idata.ra_m,
                                     idata.ra_s) * RADTODEG;
                  s.dec2000 = dms2rad(idata.dec_d, idata.dec_m,
                                      idata.dec_s) * RADTODEG;
                  ra_dec_to_string(s.ra_str,
                                   idata.ra_h, idata.ra_m, idata.ra_s);
                  ra_dec_to_string(s.dec_str,
                                   idata.dec_d, idata.dec_m, idata.dec_s);
                  s.num_channels = idata.num_chan;
                  s.start_MJD[0] = idata.mjd_i + idata.mjd_f;
                  s.dt = idata.dt;
                  s.T = s.N * s.dt;
                  s.lo_freq = idata.freq;
                  s.df = idata.chan_wid;
                  s.hi_freq = s.lo_freq + (s.num_channels - 1.0) * s.df;
                  s.BW = s.num_channels * s.df;
                  s.fctr = s.lo_freq - 0.5 * s.df + 0.5 * s.BW;
                  s.beam_FWHM = idata.fov / 3600.0;
                  s.spectra_per_subint = SUBSBLOCKLEN;
                  print_spectra_info_summary(&s);
              } else {
                  printf("\nThe input files (%s) must be subbands!  (i.e. *.sub##)\n\n",
                         cmd->argv[0]);
                  MPI_Finalize();
                  exit(1);
              }
              free(root);
              free(suffix);
          }
      }
   }

   //  If we don't have good input data, exit
   MPI_Bcast(&good_inputs, 1, MPI_INT, 0, MPI_COMM_WORLD);
   if (!good_inputs) {
       MPI_Finalize();
       exit(1);
   }
   
   MPI_Bcast(&insubs, 1, MPI_INT, 0, MPI_COMM_WORLD);
   if (insubs)
       cmd->nsub = cmd->argc;

   /* Determine the output file names and open them */

   local_numdms = cmd->numdms / (numprocs - 1);
   dms = gen_dvect(local_numdms);
   if (cmd->numdms % (numprocs - 1)) {
       if (myid == 0)
           printf
               ("\nThe number of DMs must be divisible by (the number of processors - 1).\n\n");
       MPI_Finalize();
       exit(1);
   }
   local_lodm = cmd->lodm + (myid - 1) * local_numdms * cmd->dmstep;
   
   split_path_file(cmd->outfile, &outpath, &outfilenm);
   datafilenm = (char *) calloc(strlen(outfilenm) + 20, 1);
   if (myid > 0) {
       if (chdir(outpath) == -1) {
           printf("\nProcess %d on %s cannot chdir() to '%s'.  Exiting.\n\n", 
                  myid, hostname, outpath);
           MPI_Finalize();
           exit(1);
       }
       outfiles = (FILE **) malloc(local_numdms * sizeof(FILE *));
       for (ii = 0; ii < local_numdms; ii++) {
           dms[ii] = local_lodm + ii * cmd->dmstep;
           avgdm += dms[ii];
           sprintf(datafilenm, "%s_DM%.2f.dat", outfilenm, dms[ii]);
           outfiles[ii] = chkfopen(datafilenm, "wb");
       }
       avgdm /= local_numdms;
   }
   
   // Broadcast the raw data information

   broadcast_spectra_info(&s, myid);
   if (myid > 0) {
       spectra_info_to_inf(&s, &idata);
       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;
   }
   s.filenames = cmd->argv;

   /* Read an input mask if wanted */
   
   if (myid > 0) {
       int numpad = s.num_channels;
       if (insubs)
           numpad = s.num_files;
       s.padvals = gen_fvect(numpad);
       for (ii = 0 ; ii < numpad ; ii++)
           s.padvals[ii] = 0.0;
   }
   if (cmd->maskfileP) {
       if (myid == 0) {
           read_mask(cmd->maskfile, &obsmask);
           printf("Read mask information from '%s'\n\n", cmd->maskfile);
           good_padvals = determine_padvals(cmd->maskfile, &obsmask, s.padvals);
       }
       broadcast_mask(&obsmask, myid);
       MPI_Bcast(&good_padvals, 1, MPI_INT, 0, MPI_COMM_WORLD);
       MPI_Bcast(s.padvals, obsmask.numchan, MPI_FLOAT, 0, MPI_COMM_WORLD);
   } else {
       obsmask.numchan = obsmask.numint = 0;
       MPI_Bcast(&good_padvals, 1, MPI_INT, 0, MPI_COMM_WORLD);
   }

   // The number of topo to bary time points to generate with TEMPO
   numbarypts = (int) (s.T * 1.1 / TDT + 5.5) + 1;

   // Identify the TEMPO observatory code
   {
       char *outscope = (char *) calloc(40, sizeof(char));
       telescope_to_tempocode(idata.telescope, outscope, obs);
       free(outscope);
   }

   // Broadcast or calculate a few extra important values
   if (insubs) avgdm = idata.dm;
   idata.dm = avgdm;
   dsdt = cmd->downsamp * idata.dt;
   maxdm = cmd->lodm + cmd->numdms * cmd->dmstep;
   BW_ddelay = delay_from_dm(maxdm, idata.freq) - 
       delay_from_dm(maxdm, idata.freq + (idata.num_chan-1) * idata.chan_wid);
   blocksperread = ((int) (BW_ddelay / idata.dt) / s.spectra_per_subint + 1);
   worklen = s.spectra_per_subint * blocksperread;
   
   if (cmd->nsub > s.num_channels) {
      printf
          ("Warning:  The number of requested subbands (%d) is larger than the number of channels (%d).\n",
           cmd->nsub, s.num_channels);
      printf("          Re-setting the number of subbands to %d.\n\n", s.num_channels);
      cmd->nsub = s.num_channels;
   }

   if (s.spectra_per_subint % cmd->downsamp) {
       if (myid == 0) {
           printf
               ("\nError:  The downsample factor (%d) must be a factor of the\n",
                cmd->downsamp);
           printf("        blocklength (%d).  Exiting.\n\n", s.spectra_per_subint);
       }
       MPI_Finalize();
       exit(1);
   }

   tlotoa = idata.mjd_i + idata.mjd_f;  /* Topocentric epoch */

   if (cmd->numoutP)
      totnumtowrite = cmd->numout;
   else
      totnumtowrite = (long) idata.N / cmd->downsamp;

   if (cmd->nobaryP) {          /* Main loop if we are not barycentering... */

      /* Dispersion delays (in bins).  The high freq gets no delay   */
      /* All other delays are positive fractions of bin length (dt)  */

      dispdt = subband_search_delays(s.num_channels, cmd->nsub, avgdm,
                                     idata.freq, idata.chan_wid, 0.0);
      idispdt = gen_ivect(s.num_channels);
      for (ii = 0; ii < s.num_channels; ii++)
          idispdt[ii] = NEAREST_LONG(dispdt[ii] / idata.dt);
      vect_free(dispdt);

      /* The subband dispersion delays (see note above) */

      offsets = gen_imatrix(local_numdms, cmd->nsub);
      for (ii = 0; ii < local_numdms; ii++) {
         double *subdispdt;

         subdispdt = subband_delays(s.num_channels, cmd->nsub, dms[ii],
                                    idata.freq, idata.chan_wid, 0.0);
         dtmp = subdispdt[cmd->nsub - 1];
         for (jj = 0; jj < cmd->nsub; jj++)
            offsets[ii][jj] = NEAREST_LONG((subdispdt[jj] - dtmp) / dsdt);
         vect_free(subdispdt);
      }

      /* Allocate our data array and start getting data */

      if (myid == 0) {
         printf("De-dispersing using %d subbands.\n", cmd->nsub);
         if (cmd->downsamp > 1)
            printf("Downsampling by a factor of %d (new dt = %.10g)\n",
                   cmd->downsamp, dsdt);
         printf("\n");
      }
      
      /* Print the nodes and the DMs they are handling */
      print_dms(hostname, myid, numprocs, local_numdms, dms);

      outdata = gen_fmatrix(local_numdms, worklen / cmd->downsamp);
      numread = get_data(outdata, blocksperread, &s,
                         &obsmask, idispdt, offsets, &padding);

      while (numread == worklen) {

         numread /= cmd->downsamp;
         if (myid == 0)
            print_percent_complete(totwrote, totnumtowrite);

         /* Write the latest chunk of data, but don't   */
         /* write more than cmd->numout points.         */

         numtowrite = numread;
         if (cmd->numoutP && (totwrote + numtowrite) > cmd->numout)
            numtowrite = cmd->numout - totwrote;
         if (myid > 0) {
            write_data(outfiles, local_numdms, outdata, 0, numtowrite);
            /* Update the statistics */
            if (!padding) {
               for (ii = 0; ii < numtowrite; ii++)
                  update_stats(statnum + ii, outdata[0][ii], &min, &max, &avg, &var);
               statnum += numtowrite;
            }
         }
         totwrote += numtowrite;

         /* Stop if we have written out all the data we need to */

         if (cmd->numoutP && (totwrote == cmd->numout))
            break;

         numread = get_data(outdata, blocksperread, &s,
                            &obsmask, idispdt, offsets, &padding);
      }
      datawrote = totwrote;

   } else {                     /* Main loop if we are barycentering... */

      /* What ephemeris will we use?  (Default is DE405) */
      strcpy(ephem, "DE405");

      /* Define the RA and DEC of the observation */

      ra_dec_to_string(rastring, idata.ra_h, idata.ra_m, idata.ra_s);
      ra_dec_to_string(decstring, idata.dec_d, idata.dec_m, idata.dec_s);

      /* Allocate some arrays */

      btoa = gen_dvect(numbarypts);
      ttoa = gen_dvect(numbarypts);
      for (ii = 0; ii < numbarypts; ii++)
         ttoa[ii] = tlotoa + TDT * ii / SECPERDAY;

      /* Call TEMPO for the barycentering */

      if (myid == 0) {
         double maxvoverc = -1.0, minvoverc = 1.0, *voverc = NULL;

         printf("\nGenerating barycentric corrections...\n");
         voverc = gen_dvect(numbarypts);
         barycenter(ttoa, btoa, voverc, numbarypts, rastring, decstring, obs, ephem);
         for (ii = 0; ii < numbarypts; ii++) {
            if (voverc[ii] > maxvoverc)
               maxvoverc = voverc[ii];
            if (voverc[ii] < minvoverc)
               minvoverc = voverc[ii];
            avgvoverc += voverc[ii];
         }
         avgvoverc /= numbarypts;
         vect_free(voverc);

         printf("   Average topocentric velocity (c) = %.7g\n", avgvoverc);
         printf("   Maximum topocentric velocity (c) = %.7g\n", maxvoverc);
         printf("   Minimum topocentric velocity (c) = %.7g\n\n", minvoverc);
         printf("De-dispersing using %d subbands.\n", cmd->nsub);
         if (cmd->downsamp > 1) {
             printf("     Downsample = %d\n", cmd->downsamp);
             printf("  New sample dt = %.10g\n", dsdt);
         }
         printf("\n");
      }

      /* Print the nodes and the DMs they are handling */
      print_dms(hostname, myid, numprocs, local_numdms, dms);

      MPI_Bcast(btoa, numbarypts, MPI_DOUBLE, 0, MPI_COMM_WORLD);
      MPI_Bcast(&avgvoverc, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
      blotoa = btoa[0];

      /* Dispersion delays (in bins).  The high freq gets no delay   */
      /* All other delays are positive fractions of bin length (dt)  */

      dispdt = subband_search_delays(s.num_channels, cmd->nsub, avgdm,
                                     idata.freq, idata.chan_wid, avgvoverc);
      idispdt = gen_ivect(s.num_channels);
      for (ii = 0; ii < s.num_channels; ii++)
          idispdt[ii] = NEAREST_LONG(dispdt[ii] / idata.dt);
      vect_free(dispdt);

      /* The subband dispersion delays (see note above) */

      offsets = gen_imatrix(local_numdms, cmd->nsub);
      for (ii = 0; ii < local_numdms; ii++) {
         double *subdispdt;

         subdispdt = subband_delays(s.num_channels, cmd->nsub, dms[ii],
                                    idata.freq, idata.chan_wid, avgvoverc);
         dtmp = subdispdt[cmd->nsub - 1];
         for (jj = 0; jj < cmd->nsub; jj++)
            offsets[ii][jj] = NEAREST_LONG((subdispdt[jj] - dtmp) / dsdt);
         vect_free(subdispdt);
      }

      /* Convert the bary TOAs to differences from the topo TOAs in */
      /* units of bin length (dt) rounded to the nearest integer.   */

      dtmp = (btoa[0] - ttoa[0]);
      for (ii = 0; ii < numbarypts; ii++)
         btoa[ii] = ((btoa[ii] - ttoa[ii]) - dtmp) * SECPERDAY / dsdt;

      /* Find the points where we need to add or remove bins */
      {
         int oldbin = 0, currentbin;
         double lobin, hibin, calcpt;

         numdiffbins = abs(NEAREST_LONG(btoa[numbarypts - 1])) + 1;
         diffbins = gen_ivect(numdiffbins);
         diffbinptr = diffbins;
         for (ii = 1; ii < numbarypts; ii++) {
            currentbin = NEAREST_LONG(btoa[ii]);
            if (currentbin != oldbin) {
               if (currentbin > 0) {
                  calcpt = oldbin + 0.5;
                  lobin = (ii - 1) * TDT / dsdt;
                  hibin = ii * TDT / dsdt;
               } else {
                  calcpt = oldbin - 0.5;
                  lobin = -((ii - 1) * TDT / dsdt);
                  hibin = -(ii * TDT / dsdt);
               }
               while (fabs(calcpt) < fabs(btoa[ii])) {
                  /* Negative bin number means remove that bin */
                  /* Positive bin number means add a bin there */
                  *diffbinptr =
                      NEAREST_LONG(LININTERP
                                  (calcpt, btoa[ii - 1], btoa[ii], lobin, hibin));
                  diffbinptr++;
                  calcpt = (currentbin > 0) ? calcpt + 1.0 : calcpt - 1.0;
               }
               oldbin = currentbin;
            }
         }
         *diffbinptr = cmd->numout; /* Used as a marker */
      }
      diffbinptr = diffbins;

      /* Now perform the barycentering */

      outdata = gen_fmatrix(local_numdms, worklen / cmd->downsamp);
      numread = get_data(outdata, blocksperread, &s, 
                         &obsmask, idispdt, offsets, &padding);

      while (numread == worklen) {      /* Loop to read and write the data */
         int numwritten = 0;
         double block_avg, block_var;

         numread /= cmd->downsamp;
         /* Determine the approximate local average */
         avg_var(outdata[0], numread, &block_avg, &block_var);
         if (myid == 0)
            print_percent_complete(totwrote, totnumtowrite);

         /* Simply write the data if we don't have to add or */
         /* remove any bins from this batch.                 */
         /* OR write the amount of data up to cmd->numout or */
         /* the next bin that will be added or removed.      */

         numtowrite = abs(*diffbinptr) - datawrote;
         if (cmd->numoutP && (totwrote + numtowrite) > cmd->numout)
            numtowrite = cmd->numout - totwrote;
         if (numtowrite > numread)
            numtowrite = numread;
         if (myid > 0) {
            write_data(outfiles, local_numdms, outdata, 0, numtowrite);
            /* Update the statistics */
            if (!padding) {
               for (ii = 0; ii < numtowrite; ii++)
                  update_stats(statnum + ii, outdata[0][ii], &min, &max, &avg, &var);
               statnum += numtowrite;
            }
         }
         datawrote += numtowrite;
         totwrote += numtowrite;
         numwritten += numtowrite;

         if ((datawrote == abs(*diffbinptr)) && 
             (numwritten != numread) && 
             (totwrote < cmd->numout)) {  /* Add/remove a bin */
            int skip, nextdiffbin;

            skip = numtowrite;

            /* Write the rest of the data after adding/removing a bin  */
            do {

               if (*diffbinptr > 0) {
                  /* Add a bin */
                  if (myid > 0)
                     write_padding(outfiles, local_numdms, block_avg, 1);
                  numadded++;
                  totwrote++;
               } else {
                  /* Remove a bin */
                  numremoved++;
                  datawrote++;
                  numwritten++;
                  skip++;
               }
               diffbinptr++;

               /* Write the part after the diffbin */

               numtowrite = numread - numwritten;
               if (cmd->numoutP && (totwrote + numtowrite) > cmd->numout)
                  numtowrite = cmd->numout - totwrote;
               nextdiffbin = abs(*diffbinptr) - datawrote;
               if (numtowrite > nextdiffbin)
                  numtowrite = nextdiffbin;
               if (myid > 0) {
                  write_data(outfiles, local_numdms, outdata, skip, numtowrite);
                  /* Update the statistics and counters */
                  if (!padding) {
                     for (ii = 0; ii < numtowrite; ii++)
                        update_stats(statnum + ii,
                                     outdata[0][skip + ii], &min, &max, &avg, &var);
                     statnum += numtowrite;
                  }
               }
               numwritten += numtowrite;
               datawrote += numtowrite;
               totwrote += numtowrite;
               skip += numtowrite;

               /* Stop if we have written out all the data we need to */

               if (cmd->numoutP && (totwrote == cmd->numout))
                  break;
            } while (numwritten < numread);
         }
         /* Stop if we have written out all the data we need to */

         if (cmd->numoutP && (totwrote == cmd->numout))
            break;

         numread = get_data(outdata, blocksperread, &s,
                            &obsmask, idispdt, offsets, &padding);
      }
   }

   if (myid > 0) {

      /* Calculate the amount of padding we need  */

      if (cmd->numoutP && (cmd->numout > totwrote))
         padwrote = padtowrite = cmd->numout - totwrote;

      /* Write the new info file for the output data */

      idata.dt = dsdt;
      update_infodata(&idata, totwrote, padtowrite, diffbins,
                      numdiffbins, cmd->downsamp);
      for (ii = 0; ii < local_numdms; ii++) {
         idata.dm = dms[ii];
         if (!cmd->nobaryP) {
            double baryepoch, barydispdt, baryhifreq;

            baryhifreq = idata.freq + (s.num_channels - 1) * idata.chan_wid;
            barydispdt = delay_from_dm(dms[ii], doppler(baryhifreq, avgvoverc));
            baryepoch = blotoa - (barydispdt / SECPERDAY);
            idata.bary = 1;
            idata.mjd_i = (int) floor(baryepoch);
            idata.mjd_f = baryepoch - idata.mjd_i;
         }
         sprintf(idata.name, "%s_DM%.2f", outfilenm, dms[ii]);
         writeinf(&idata);
      }

      /* Set the padded points equal to the average data point */

      if (idata.numonoff >= 1) {
         int index, startpad, endpad;

         for (ii = 0; ii < local_numdms; ii++) {
            fclose(outfiles[ii]);
            sprintf(datafilenm, "%s_DM%.2f.dat", outfilenm, dms[ii]);
            outfiles[ii] = chkfopen(datafilenm, "rb+");
         }
         for (ii = 0; ii < idata.numonoff; ii++) {
            index = 2 * ii;
            startpad = idata.onoff[index + 1];
            if (ii == idata.numonoff - 1)
               endpad = idata.N - 1;
            else
               endpad = idata.onoff[index + 2];
            for (jj = 0; jj < local_numdms; jj++)
               chkfseek(outfiles[jj], (startpad + 1) * sizeof(float), SEEK_SET);
            padtowrite = endpad - startpad;
            write_padding(outfiles, local_numdms, avg, padtowrite);
         }
      }
   }

   /* Print simple stats and results */

   var /= (datawrote - 1);
   if (myid == 0)
      print_percent_complete(1, 1);
   if (myid == 1) {
      printf("\n\nDone.\n\nSimple statistics of the output data:\n");
      printf("             Data points written:  %ld\n", totwrote);
      if (padwrote)
         printf("          Padding points written:  %ld\n", padwrote);
      if (!cmd->nobaryP) {
         if (numadded)
            printf("    Bins added for barycentering:  %d\n", numadded);
         if (numremoved)
            printf("  Bins removed for barycentering:  %d\n", numremoved);
      }
      printf("           Maximum value of data:  %.2f\n", max);
      printf("           Minimum value of data:  %.2f\n", min);
      printf("              Data average value:  %.2f\n", avg);
      printf("         Data standard deviation:  %.2f\n", sqrt(var));
      printf("\n");
   }

   /* Close the files and cleanup */

   if (cmd->maskfileP)
      free_mask(obsmask);
   if (myid > 0) {
      for (ii = 0; ii < local_numdms; ii++)
         fclose(outfiles[ii]);
      free(outfiles);
   }
   vect_free(outdata[0]);
   vect_free(outdata);
   vect_free(dms);
   free(hostname);
   vect_free(idispdt);
   vect_free(offsets[0]);
   vect_free(offsets);
   free(datafilenm);
   free(outfilenm);
   free(outpath);
   if (!cmd->nobaryP) {
      vect_free(btoa);
      vect_free(ttoa);
      vect_free(diffbins);
   }
   MPI_Finalize();
   return (0);
}