Esempio n. 1
0
int main(int argc, char *argv[])
{
   FILE *infile, *outfile;
   int ii, numread;
   long long N = 0;
   float offset = 0.0, fbuffer[sizeof(float) * BUFFLEN];
   short sbuffer[sizeof(short) * BUFFLEN];
   char *outname;

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

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

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

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

   infile = chkfopen(argv[1], "rb");
   outfile = chkfopen(outname, "wb");
   while ((numread = chkfread(sbuffer, sizeof(short), BUFFLEN, infile))) {
      N += numread;
      for (ii = 0; ii < numread; ii++)
         fbuffer[ii] = (float) (sbuffer[ii]) + offset;
      fwrite(fbuffer, sizeof(float), numread, outfile);
   }
   printf("done.\n Wrote %lld points.\n\n", N);
   fclose(infile);
   fclose(outfile);
   free(outname);
   exit(0);
}
Esempio n. 2
0
void read_dftvector(dftvector * data, char *filename)
/* Read a dftvector data structure from a binary file */
{
   FILE *infile;
   int ii;
   double dtmp;

   infile = chkfopen(filename, "rb");
   chkfread(&dtmp, sizeof(double), 1, infile);
   data->n = (int) dtmp;
   chkfread(&dtmp, sizeof(double), 1, infile);
   data->numvect = (int) dtmp;
   chkfread(&data->dt, sizeof(double), 1, infile);
   chkfread(&data->r, sizeof(double), 1, infile);
   chkfread(&data->norm, sizeof(double), 1, infile);
   chkfread(&data->T, sizeof(double), 1, infile);
   data->vector = gen_cvect(data->n);
   for (ii = 0; ii < data->numvect; ii++) {
      chkfread(&dtmp, sizeof(double), 1, infile);
      data->vector[ii].r = (float) dtmp;
      chkfread(&dtmp, sizeof(double), 1, infile);
      data->vector[ii].i = (float) dtmp;
   }
   fclose(infile);
}
Esempio n. 3
0
void write_mask(char *maskfilenm, mask * obsmask)
/* Write the contents of an mask structure to a file */
{
   FILE *outfile;
   int ii;

   outfile = chkfopen(maskfilenm, "wb");
   chkfwrite(&(obsmask->timesigma), sizeof(double), 1, outfile);
   chkfwrite(&(obsmask->freqsigma), sizeof(double), 1, outfile);
   chkfwrite(&(obsmask->mjd), sizeof(double), 1, outfile);
   chkfwrite(&(obsmask->dtint), sizeof(double), 1, outfile);
   chkfwrite(&(obsmask->lofreq), sizeof(double), 1, outfile);
   chkfwrite(&(obsmask->dfreq), sizeof(double), 1, outfile);
   chkfwrite(&(obsmask->numchan), sizeof(int), 1, outfile);
   chkfwrite(&(obsmask->numint), sizeof(int), 1, outfile);
   chkfwrite(&(obsmask->ptsperint), sizeof(int), 1, outfile);
   chkfwrite(&(obsmask->num_zap_chans), sizeof(int), 1, outfile);
   if (obsmask->num_zap_chans)
      chkfwrite(obsmask->zap_chans, sizeof(int), obsmask->num_zap_chans, outfile);
   chkfwrite(&(obsmask->num_zap_ints), sizeof(int), 1, outfile);
   if (obsmask->num_zap_ints)
      chkfwrite(obsmask->zap_ints, sizeof(int), obsmask->num_zap_ints, outfile);
   chkfwrite(obsmask->num_chans_per_int, sizeof(int), obsmask->numint, outfile);
   for (ii = 0; ii < obsmask->numint; ii++) {
      if (obsmask->num_chans_per_int[ii] > 0 &&
          obsmask->num_chans_per_int[ii] < obsmask->numchan) {
         chkfwrite(obsmask->chans[ii], sizeof(int),
                   obsmask->num_chans_per_int[ii], outfile);
      }
   }
   fclose(outfile);
}
Esempio n. 4
0
int read_database(void)
/* Reads the full pulsar database into the static array psrdata */
{
   FILE *database;
   char databasenm[200];
   psrdata pdata;
   binpsrdata bpdata;

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

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

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

   have_database = 1;
   return np;
}
Esempio n. 5
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. 6
0
void get_rzw_cand(char *filenm, int candnum, fourierprops * cand)
/*  Read the rzw candidate file 'filenm' and return a        */
/*  pointer to the fourierprops that describes it.           */
{
   FILE *candfile;

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

   candfile = chkfopen(filenm, "rb");
   chkfileseek(candfile, candnum - 1, sizeof(rawbincand), SEEK_SET);
   chkfread(cand, sizeof(rawbincand), 1, candfile);
   fclose(candfile);
}
Esempio n. 8
0
void write_mak_file(makedata * mdata)
/* Write the data for makedata to the makefile.  */
{
   FILE *makefile;
   char makefilenm[200];
   int i;

   sprintf(makefilenm, "%s.mak", mdata->basefilenm);
   makefile = chkfopen(makefilenm, "w");

   fprintf(makefile, "%s\n", mdata->description);
   fprintf(makefile, "Num data pts      = %ld\n", mdata->N);
   fprintf(makefile, "dt per bin (s)    = %25.15g\n", mdata->dt);
   fprintf(makefile, "Pulse shape       = %s", mdata->ptype);
   if (mdata->pnum > 2) {
      fprintf(makefile, " (FWHM = %g)", mdata->fwhm);
   }
   fprintf(makefile, "\nRounding format   = %s\n", mdata->round);
   fprintf(makefile, "Pulse freq (hz)   = %25.15g\n", mdata->f);
   fprintf(makefile, "fdot (s-2)        = %25.15g\n", mdata->fd);
   fprintf(makefile, "fdotdot (s-3)     = %25.15g\n", mdata->fdd);
   fprintf(makefile, "Pulse amp         = %25.15g\n", mdata->amp);
   fprintf(makefile, "Pulse phs (deg)   = %25.15g\n", mdata->phs);
   fprintf(makefile, "DC backgrnd level = %25.15g\n", mdata->dc);
   fprintf(makefile, "Binary period (s) = %25.15g\n",
           ((mdata->binary) ? mdata->orb.p : 0.0));
   fprintf(makefile, "Bin asini/c (s)   = %25.15g\n",
           ((mdata->binary) ? mdata->orb.x : 0.0));
   fprintf(makefile, "Bin eccentricity  = %25.15g\n",
           ((mdata->binary) ? mdata->orb.e : 0.0));
   fprintf(makefile, "Ang of Peri (deg) = %25.15g\n",
           ((mdata->binary) ? mdata->orb.w : 0.0));
   fprintf(makefile, "Tm since peri (s) = %25.15g\n",
           ((mdata->binary) ? mdata->orb.t : 0.0));
   fprintf(makefile, "Amp Mod amplitude = %25.15g\n",
           ((mdata->ampmod) ? mdata->ampmoda : 0.0));
   fprintf(makefile, "Amp Mod phs (deg) = %25.15g\n",
           ((mdata->ampmod) ? mdata->ampmodp : 0.0));
   fprintf(makefile, "Amp Mod freq (hz) = %25.15g\n",
           ((mdata->ampmod) ? mdata->ampmodf : 0.0));
   fprintf(makefile, "Noise type        = %s\n", mdata->noisetype);
   fprintf(makefile, "Noise sigma       = %25.15g\n", mdata->noisesig);
   i = 0;
   do {
      fprintf(makefile, "On/Off Pair %2d    = %g %g\n",
              i + 1, mdata->onoff[2 * i], mdata->onoff[2 * i + 1]);
      i++;
   }
   while (mdata->onoff[2 * i - 1] != 1.0);

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

   outfile = chkfopen(statsfilenm, "wb");
   chkfwrite(&numchan, sizeof(int), 1, outfile);
   chkfwrite(&numint, sizeof(int), 1, outfile);
   chkfwrite(&ptsperint, sizeof(int), 1, outfile);
   chkfwrite(&lobin, sizeof(int), 1, outfile);
   chkfwrite(&numbetween, sizeof(int), 1, outfile);
   chkfwrite(datapow, sizeof(float), numchan * numint, outfile);
   chkfwrite(dataavg, sizeof(float), numchan * numint, outfile);
   chkfwrite(datastd, sizeof(float), numchan * numint, outfile);
   fclose(outfile);
}
Esempio n. 11
0
void read_mask(char *maskfilenm, mask * obsmask)
/* Read the contents of a mask structure from a file */
{
   FILE *infile;
   int ii;

   infile = chkfopen(maskfilenm, "rb");
   chkfread(&(obsmask->timesigma), sizeof(double), 1, infile);
   chkfread(&(obsmask->freqsigma), sizeof(double), 1, infile);
   chkfread(&(obsmask->mjd), sizeof(double), 1, infile);
   chkfread(&(obsmask->dtint), sizeof(double), 1, infile);
   chkfread(&(obsmask->lofreq), sizeof(double), 1, infile);
   chkfread(&(obsmask->dfreq), sizeof(double), 1, infile);
   chkfread(&(obsmask->numchan), sizeof(int), 1, infile);
   chkfread(&(obsmask->numint), sizeof(int), 1, infile);
   chkfread(&(obsmask->ptsperint), sizeof(int), 1, infile);
   chkfread(&(obsmask->num_zap_chans), sizeof(int), 1, infile);
   if (obsmask->num_zap_chans) {
      obsmask->zap_chans = gen_ivect(obsmask->num_zap_chans);
      chkfread(obsmask->zap_chans, sizeof(int), obsmask->num_zap_chans, infile);
   }
   chkfread(&(obsmask->num_zap_ints), sizeof(int), 1, infile);
   if (obsmask->num_zap_ints) {
      obsmask->zap_ints = gen_ivect(obsmask->num_zap_ints);
      chkfread(obsmask->zap_ints, sizeof(int), obsmask->num_zap_ints, infile);
   }
   obsmask->num_chans_per_int = gen_ivect(obsmask->numint);
   chkfread(obsmask->num_chans_per_int, sizeof(int), obsmask->numint, infile);
   obsmask->chans = (int **) malloc(obsmask->numint * sizeof(int *));
   for (ii = 0; ii < obsmask->numint; ii++) {
      if (obsmask->num_chans_per_int[ii] > 0 &&
          obsmask->num_chans_per_int[ii] < obsmask->numchan) {
         obsmask->chans[ii] = gen_ivect(obsmask->num_chans_per_int[ii]);
         chkfread(obsmask->chans[ii], sizeof(int),
                  obsmask->num_chans_per_int[ii], infile);
      } else if (obsmask->num_chans_per_int[ii] == obsmask->numchan) {
         int jj;
         obsmask->chans[ii] = gen_ivect(obsmask->num_chans_per_int[ii]);
         for (jj = 0; jj < obsmask->numchan; jj++)
            obsmask->chans[ii][jj] = jj;
      }
   }
   fclose(infile);
}
Esempio n. 12
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. 13
0
static void write_rfifile(char *rfifilenm, rfi * rfivect, int numrfi,
                          int numchan, int numint, int ptsperint,
                          int lobin, int numbetween, int harmsum,
                          float fracterror, float freqsigma)
{
   FILE *outfile;
   int ii;

   outfile = chkfopen(rfifilenm, "wb");
   chkfwrite(&numchan, sizeof(int), 1, outfile);
   chkfwrite(&numint, sizeof(int), 1, outfile);
   chkfwrite(&ptsperint, sizeof(int), 1, outfile);
   chkfwrite(&lobin, sizeof(int), 1, outfile);
   chkfwrite(&numbetween, sizeof(int), 1, outfile);
   chkfwrite(&harmsum, sizeof(int), 1, outfile);
   chkfwrite(&numrfi, sizeof(int), 1, outfile);
   chkfwrite(&fracterror, sizeof(float), 1, outfile);
   chkfwrite(&freqsigma, sizeof(float), 1, outfile);
   for (ii = 0; ii < numrfi; ii++)
      write_rfi(outfile, rfivect + ii, numchan, numint);
   fclose(outfile);
}
Esempio n. 14
0
void read_wgts_and_offs(char *filenm, int *numchan,
                        float **weights, float **offsets)
{
    FILE *infile;
    int N, chan;
    float wgt, offs;
    char line[80];

    infile = chkfopen(filenm, "r");

    // Read the input file once to count the lines
    N = 0;
    while (!feof(infile)){
        fgets(line, 80, infile);
        if (line[0]!='#') {
            sscanf(line, "%d %f %f\n", &chan, &wgt, &offs);
            N++;
        }
    }
    N--;
    *numchan = N;
    
    // Allocate the output arrays
    *weights = (float *)malloc(N * sizeof(float));
    *offsets = (float *)malloc(N * sizeof(float));
    
    // Rewind and read the EVENTs for real
    rewind(infile);
    N = 0;
    while (!feof(infile)){
        fgets(line, 80, infile);
        if (line[0]!='#') {
            sscanf(line, "%d %f %f\n", &chan, *weights+N, *offsets+N);
            N++;
        }
    }
    fclose(infile);
}
Esempio n. 15
0
static void read_rfifile(char *rfifilenm, rfi ** rfivect, int *numrfi,
                         int *numchan, int *numint, int *ptsperint,
                         int *lobin, int *numbetween, int *harmsum,
                         float *fracterror, float *freqsigma)
{
   FILE *outfile;
   int ii;

   outfile = chkfopen(rfifilenm, "rb");
   chkfread(numchan, sizeof(int), 1, outfile);
   chkfread(numint, sizeof(int), 1, outfile);
   chkfread(ptsperint, sizeof(int), 1, outfile);
   chkfread(lobin, sizeof(int), 1, outfile);
   chkfread(numbetween, sizeof(int), 1, outfile);
   chkfread(harmsum, sizeof(int), 1, outfile);
   chkfread(numrfi, sizeof(int), 1, outfile);
   chkfread(fracterror, sizeof(float), 1, outfile);
   chkfread(freqsigma, sizeof(float), 1, outfile);
   *rfivect = rfi_vector(*rfivect, *numchan, *numint, 0, *numrfi);
   for (ii = 0; ii < *numrfi; ii++)
      read_rfi(outfile, *rfivect + ii, *numchan, *numint);
   fclose(outfile);
}
Esempio n. 16
0
void write_dftvector(dftvector * data, char *filename)
/* Write a dftvector data structure to a binary file */
{
   FILE *outfile;
   int ii;
   double dtmp;

   outfile = chkfopen(filename, "wb");
   dtmp = (double) data->n;
   chkfwrite(&dtmp, sizeof(double), 1, outfile);
   dtmp = (double) data->numvect;
   chkfwrite(&dtmp, sizeof(double), 1, outfile);
   chkfwrite(&data->dt, sizeof(double), 1, outfile);
   chkfwrite(&data->r, sizeof(double), 1, outfile);
   chkfwrite(&data->norm, sizeof(double), 1, outfile);
   chkfwrite(&data->T, sizeof(double), 1, outfile);
   for (ii = 0; ii < data->numvect; ii++) {
      dtmp = (double) data->vector[ii].r;
      chkfwrite(&dtmp, sizeof(double), 1, outfile);
      dtmp = (double) data->vector[ii].i;
      chkfwrite(&dtmp, sizeof(double), 1, outfile);
   }
   fclose(outfile);
}
Esempio n. 17
0
void output_fundamentals(fourierprops * props, GSList * list,
                         accelobs * obs, infodata * idata)
{
   double accel = 0.0, accelerr = 0.0, coherent_pow;
   int ii, jj, numcols = 12, numcands, *width, *error;
   int widths[12] = { 4, 5, 6, 8, 4, 16, 15, 15, 15, 11, 15, 20 };
   int errors[12] = { 0, 0, 0, 0, 0, 1, 1, 2, 1, 2, 2, 0 };
   char tmpstr[30], ctrstr[30], *notes;
   accelcand *cand;
   GSList *listptr;
   rzwerrs errs;
   static char **title;
   static char *titles1[] = { "", "", "Summed", "Coherent", "Num", "Period",
      "Frequency", "FFT 'r'", "Freq Deriv", "FFT 'z'",
      "Accel", ""
   };
   static char *titles2[] = { "Cand", "Sigma", "Power", "Power", "Harm", "(ms)",
      "(Hz)", "(bin)", "(Hz/s)", "(bins)",
      "(m/s^2)", "Notes"
   };

   numcands = g_slist_length(list);
   listptr = list;

   /* Close the old work file and open the cand file */

   if (!obs->dat_input)
      fclose(obs->workfile); /* Why is this here? -A */
   obs->workfile = chkfopen(obs->accelnm, "w");

   /* Set our candidate notes to all spaces */

   notes = (char *) malloc(numcands * widths[numcols - 1]);
   memset(notes, ' ', numcands * widths[numcols - 1]);

   /* Compare the candidates with the pulsar database */

   if (dms2rad(idata->ra_h, idata->ra_m, idata->ra_s) != 0.0 &&
       hms2rad(idata->dec_d, idata->dec_m, idata->dec_s) != 0.0) {
      for (ii = 0; ii < numcands; ii++) {
         comp_psr_to_cand(props + ii, idata, notes + ii * 20, 0);
      }
   }

   /* Compare the candidates with themselves */

   compare_rzw_cands(props, numcands, notes);

   /* Print the header */

   width = widths;
   title = titles1;
   for (ii = 0; ii < numcols - 1; ii++) {
      center_string(ctrstr, *title++, *width++);
      fprintf(obs->workfile, "%s  ", ctrstr);
   }
   center_string(ctrstr, *title++, *width++);
   fprintf(obs->workfile, "%s\n", ctrstr);

   width = widths;
   title = titles2;
   for (ii = 0; ii < numcols - 1; ii++) {
      center_string(ctrstr, *title++, *width++);
      fprintf(obs->workfile, "%s  ", ctrstr);
   }
   center_string(ctrstr, *title++, *width++);
   fprintf(obs->workfile, "%s\n", ctrstr);

   width = widths;
   for (ii = 0; ii < numcols - 1; ii++) {
      memset(tmpstr, '-', *width);
      tmpstr[*width++] = '\0';
      fprintf(obs->workfile, "%s--", tmpstr);
   }
   memset(tmpstr, '-', *width++);
   tmpstr[widths[ii]] = '\0';
   fprintf(obs->workfile, "%s\n", tmpstr);

   /* Print the fundamentals */

   for (ii = 0; ii < numcands; ii++) {
      width = widths;
      error = errors;
      cand = (accelcand *) (listptr->data);
      calc_rzwerrs(props + ii, obs->T, &errs);

      {                         /* Calculate the coherently summed power */
         double coherent_r = 0.0, coherent_i = 0.0;
         double phs0, phscorr, amp;
         rderivs harm;

         /* These phase calculations assume the fundamental is best */
         /* Better to irfft them and check the amplitude */
         phs0 = cand->derivs[0].phs;
         for (jj = 0; jj < cand->numharm; jj++) {
            harm = cand->derivs[jj];
            if (obs->nph > 0.0)
               amp = sqrt(harm.pow / obs->nph);
            else
               amp = sqrt(harm.pow / harm.locpow);
            phscorr = phs0 - fmod((jj + 1.0) * phs0, TWOPI);
            coherent_r += amp * cos(harm.phs + phscorr);
            coherent_i += amp * sin(harm.phs + phscorr);
         }
         coherent_pow = coherent_r * coherent_r + coherent_i * coherent_i;
      }

      sprintf(tmpstr, "%-4d", ii + 1);
      center_string(ctrstr, tmpstr, *width++);
      error++;
      fprintf(obs->workfile, "%s  ", ctrstr);

      sprintf(tmpstr, "%.2f", cand->sigma);
      center_string(ctrstr, tmpstr, *width++);
      error++;
      fprintf(obs->workfile, "%s  ", ctrstr);

      sprintf(tmpstr, "%.2f", cand->power);
      center_string(ctrstr, tmpstr, *width++);
      error++;
      fprintf(obs->workfile, "%s  ", ctrstr);

      sprintf(tmpstr, "%.2f", coherent_pow);
      center_string(ctrstr, tmpstr, *width++);
      error++;
      fprintf(obs->workfile, "%s  ", ctrstr);

      sprintf(tmpstr, "%d", cand->numharm);
      center_string(ctrstr, tmpstr, *width++);
      error++;
      fprintf(obs->workfile, "%s  ", ctrstr);

      write_val_with_err(obs->workfile, errs.p * 1000.0, errs.perr * 1000.0,
                         *error++, *width++);
      write_val_with_err(obs->workfile, errs.f, errs.ferr, *error++, *width++);
      write_val_with_err(obs->workfile, props[ii].r, props[ii].rerr,
                         *error++, *width++);
      write_val_with_err(obs->workfile, errs.fd, errs.fderr, *error++, *width++);
      write_val_with_err(obs->workfile, props[ii].z, props[ii].zerr,
                         *error++, *width++);
      accel = props[ii].z * SOL / (obs->T * obs->T * errs.f);
      accelerr = props[ii].zerr * SOL / (obs->T * obs->T * errs.f);
      write_val_with_err(obs->workfile, accel, accelerr, *error++, *width++);
      fprintf(obs->workfile, "  %.20s\n", notes + ii * 20);
      fflush(obs->workfile);
      listptr = listptr->next;
   }
   fprintf(obs->workfile, "\n\n");
   free(notes);
}
Esempio n. 18
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;
    */
}
Esempio n. 19
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);
}
Esempio n. 20
0
void barycenter(double *topotimes, double *barytimes,
                double *voverc, long N, char *ra, char *dec, char *obs, char *ephem)
/* This routine uses TEMPO to correct a vector of           */
/* topocentric times (in *topotimes) to barycentric times   */
/* (in *barytimes) assuming an infinite observation         */
/* frequency.  The routine also returns values for the      */
/* radial velocity of the observation site (in units of     */
/* v/c) at the barycentric times.  All three vectors must   */
/* be initialized prior to calling.  The vector length for  */
/* all the vectors is 'N' points.  The RA and DEC (J2000)   */
/* of the observed object are passed as strings in the      */
/* following format: "hh:mm:ss.ssss" for RA and             */
/* "dd:mm:ss.ssss" for DEC.  The observatory site is passed */
/* as a 2 letter ITOA code.  This observatory code must be  */
/* found in obsys.dat (in the TEMPO paths).  The ephemeris  */
/* is either "DE200" or "DE405".                            */
{

   FILE *outfile;
   long i;
   double fobs = 1000.0, femit, dtmp;
   char command[100], temporaryfile[100];

   /* Write the free format TEMPO file to begin barycentering */

   strcpy(temporaryfile, "bary.tmp");
   outfile = chkfopen(temporaryfile, "w");
   fprintf(outfile, "C  Header Section\n"
           "  HEAD                    \n"
           "  PSR                 bary\n"
           "  NPRNT                  2\n"
           "  P0                   1.0 1\n"
           "  P1                   0.0\n"
           "  CLK            UTC(NIST)\n"
           "  PEPOCH           %19.13f\n"
           "  COORD              J2000\n"
           "  RA                    %s\n"
           "  DEC                   %s\n"
           "  DM                   0.0\n"
           "  EPHEM                 %s\n"
           "C  TOA Section (uses ITAO Format)\n"
           "C  First 8 columns must have + or -!\n"
           "  TOA\n", topotimes[0], ra, dec, ephem);

   /* Write the TOAs for infinite frequencies */

   for (i = 0; i < N; i++) {
      fprintf(outfile, "topocen+ %19.13f  0.00     0.0000  0.000000  %s\n",
              topotimes[i], obs);
   }
   fprintf(outfile, "topocen+ %19.13f  0.00     0.0000  0.000000  %s\n",
           topotimes[N - 1] + 10.0 / SECPERDAY, obs);
   fprintf(outfile, "topocen+ %19.13f  0.00     0.0000  0.000000  %s\n",
           topotimes[N - 1] + 20.0 / SECPERDAY, obs);
   fclose(outfile);

   /* Call TEMPO */

   /* Check the TEMPO *.tmp and *.lis files for errors when done. */

   sprintf(command, "tempo bary.tmp > tempoout_times.tmp");
   if (system(command)==-1) {
       fprintf(stderr, "\nError calling TEMPO in barycenter.c!\n");
       exit(1);
   }

   /* Now read the TEMPO results */

   strcpy(temporaryfile, "resid2.tmp");
   outfile = chkfopen(temporaryfile, "rb");

   /* Read the barycentric TOAs for infinite frequencies */

   for (i = 0; i < N; i++) {
      read_resid_rec(outfile, &barytimes[i], &dtmp);
   }
   fclose(outfile);

   /* rename("itoa.out", "itoa1.out"); */
   /* rename("bary.tmp", "bary1.tmp"); */
   /* rename("bary.par", "bary1.par"); */

   /* Write the free format TEMPO file to begin barycentering */

   strcpy(temporaryfile, "bary.tmp");
   outfile = chkfopen(temporaryfile, "w");
   fprintf(outfile, "C  Header Section\n"
           "  HEAD                    \n"
           "  PSR                 bary\n"
           "  NPRNT                  2\n"
           "  P0                   1.0 1\n"
           "  P1                   0.0\n"
           "  CLK            UTC(NIST)\n"
           "  PEPOCH           %19.13f\n"
           "  COORD              J2000\n"
           "  RA                    %s\n"
           "  DEC                   %s\n"
           "  DM                   0.0\n"
           "  EPHEM                 %s\n"
           "C  TOA Section (uses ITAO Format)\n"
           "C  First 8 columns must have + or -!\n"
           "  TOA\n", topotimes[0], ra, dec, ephem);

   /* Write the TOAs for finite frequencies */

   for (i = 0; i < N; i++) {
      fprintf(outfile, "topocen+ %19.13f  0.00  %9.4f  0.000000  %s\n",
              topotimes[i], fobs, obs);
   }
   fprintf(outfile, "topocen+ %19.13f  0.00  %9.4f  0.000000  %s\n",
           topotimes[N - 1] + 10.0 / SECPERDAY, fobs, obs);
   fprintf(outfile, "topocen+ %19.13f  0.00  %9.4f  0.000000  %s\n",
           topotimes[N - 1] + 20.0 / SECPERDAY, fobs, obs);
   fclose(outfile);

   /* Call TEMPO */

   /* Insure you check the file tempoout.tmp for  */
   /* errors from TEMPO when complete.            */

   sprintf(command, "tempo bary.tmp > tempoout_vels.tmp");
   if (system(command)==-1) {
       fprintf(stderr, "\nError calling TEMPO in barycenter.c!\n");
       exit(1);
   }

   /* Now read the TEMPO results */

   strcpy(temporaryfile, "resid2.tmp");
   outfile = chkfopen(temporaryfile, "rb");

   /* Determine the radial velocities using the emitted freq */

   for (i = 0; i < N; i++) {
      read_resid_rec(outfile, &dtmp, &femit);
      voverc[i] = femit / fobs - 1.0;
   }
   fclose(outfile);

   /* Cleanup the temp files */

   /* rename("itoa.out", "itoa2.out"); */
   /* rename("bary.tmp", "bary2.tmp"); */
   /* rename("bary.par", "bary2.par"); */

   remove("tempo.lis");
   remove("tempoout_times.tmp");
   remove("tempoout_vels.tmp");
   remove("resid2.tmp");
   remove("bary.tmp");
   remove("matrix.tmp");
   remove("bary.par");
}
Esempio n. 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);
}
Esempio n. 22
0
void read_mak_file(char basefilenm[], makedata * mdata)
/* Read the data for makedata from the makefile. */
{
   FILE *makefile;
   char makefilenm[200], tmp[50];
   double tmponoff[40];
   int i;

   sprintf(makefilenm, "%s.mak", basefilenm);
   printf("Reading make information from \"%s\".\n", makefilenm);
   makefile = chkfopen(makefilenm, "r");

   strcpy(mdata->basefilenm, basefilenm);
   fscanf(makefile, "%[^\n]", mdata->description);

   fscanf(makefile, "%*[^=]= %ld", &mdata->N);
   if (mdata->N <= 0) {
      printf("\nmdata->N must be > 0 in read_mak_file()\n");
      exit(1);
   }

   mdata->next2_to_n = 1;
   while (mdata->next2_to_n < mdata->N) {
      mdata->next2_to_n <<= 1;
   }

   fscanf(makefile, "%*[^=]= %lf", &mdata->dt);
   if (mdata->dt <= 0) {
      printf("\nmdata->dt must be > 0 in read_mak_file()\n");
      exit(1);
   }

   mdata->T = mdata->N * mdata->dt;

   i = fscanf(makefile, "%*[^=]= %s%[^\n]", mdata->ptype, tmp);
   if (strcmp(mdata->ptype, "Sine") == 0)
      mdata->pnum = 1;
   else if (strcmp(mdata->ptype, "Crab-like") == 0)
      mdata->pnum = 2;
   else {
      if (strcmp(mdata->ptype, "Spike") == 0) {
         mdata->pnum = 3;
      } else {
         mdata->pnum = 4;
      }
      /* Default value for fwhm */
      if (i == 2)
         sscanf(tmp, "%*[^=]= %lf", &mdata->fwhm);
      else
         mdata->fwhm = 0.1;
      if (mdata->fwhm <= 0.0 || mdata->fwhm > 0.5) {
         printf("\nmdata->fwhm must be between 0 and 0.5 in ");
         printf("read_mak_file()\n");
         exit(1);
      }
   }

   fscanf(makefile, "%*[^=]= %s", mdata->round);
   if (strcmp(mdata->round, "Fractional") == 0)
      mdata->roundnum = 0;
   else
      mdata->roundnum = 1;

   fscanf(makefile, "%*[^=]= %lf", &mdata->f);
   if (mdata->f <= 0.0) {
      printf("\nmdata->f must be > 0.0 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->fd);
   fscanf(makefile, "%*[^=]= %lf", &mdata->fdd);
   mdata->p = 1.0 / mdata->f;
   mdata->pd = -mdata->fd / (mdata->f * mdata->f);
   if (mdata->fdd == 0.0)
      mdata->pdd = 0.0;
   else
      mdata->pdd = (2 * mdata->fd * mdata->fd / mdata->f -
                    mdata->fdd) / mdata->f * mdata->f;
   mdata->r = mdata->f * mdata->T;
   mdata->z = mdata->fd * mdata->T * mdata->T;
   mdata->w = mdata->fdd * mdata->T * mdata->T * mdata->T;

   fscanf(makefile, "%*[^=]= %lf", &mdata->amp);
   if (mdata->amp < 0.0) {
      printf("\nmdata->amp must be >= 0.0 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->phs);
   if (mdata->phs < 0.0 || mdata->phs >= 360) {
      printf("\nmdata->phs must be 0 <= phs < 360 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->dc);

   fscanf(makefile, "%*[^=]= %lf", &mdata->orb.p);
   if (mdata->orb.p < 0.0) {
      printf("\nmdata->orb.p must be >= 0.0 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->orb.x);
   if (mdata->orb.x < 0.0) {
      printf("\nmdata->orb.x must be >= 0.0 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->orb.e);
   if (mdata->orb.e < 0.0 || mdata->orb.e >= 1.0) {
      printf("\nmdata->e must be 0 <= e < 1.0 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->orb.w);
   if (mdata->orb.w < 0.0 || mdata->orb.w >= 360) {
      printf("\nmdata->orb.w must be 0 <= w < 360 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->orb.t);
   if (mdata->orb.p == 0.0 || mdata->orb.x == 0.0)
      mdata->binary = 0;
   else
      mdata->binary = 1;

   fscanf(makefile, "%*[^=]= %lf", &mdata->ampmoda);
   if (mdata->ampmoda < 0.0) {
      printf("\nmdata->ampmoda must be >= 0.0 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->ampmodp);
   if (mdata->ampmodp < 0.0 || mdata->ampmodp >= 360) {
      printf("\nmdata->ampmodp must be 0 <= phs < 360 in read_mak_file()\n");
      exit(1);
   }
   fscanf(makefile, "%*[^=]= %lf", &mdata->ampmodf);
   if (mdata->ampmodf < 0.0) {
      printf("\nmdata->ampmodf must be >= 0.0 in read_mak_file()\n");
      exit(1);
   }
   if (mdata->ampmoda == 0.0 || mdata->ampmodf == 0.0)
      mdata->ampmod = 0;
   else
      mdata->ampmod = 1;

   fscanf(makefile, "%*[^=]= %s", mdata->noisetype);
   if (strcmp(mdata->noisetype, "Standard") == 0)
      mdata->noise = 1;
   else
      mdata->noise = 2;
   fscanf(makefile, "%*[^=]= %lf", &mdata->noisesig);

   i = 0;
   do {
      fscanf(makefile, "%*[^=]= %lf %lf", &tmponoff[i], &tmponoff[i + 1]);
      i += 2;
   } while (tmponoff[i - 1] < 1.0 && i < 40);
   if (tmponoff[i - 1] > 1.0)
      tmponoff[i - 1] = 1.0;
   mdata->numonoff = i / 2;
   mdata->onoff = (double *) malloc(mdata->numonoff * 2 * sizeof(double));
   for (i = 0; i < mdata->numonoff; i++) {
      mdata->onoff[2 * i] = tmponoff[2 * i];
      mdata->onoff[2 * i + 1] = tmponoff[2 * i + 1];
   }
   fclose(makefile);
}
Esempio n. 23
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;
}
Esempio n. 24
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);
}
Esempio n. 25
0
char *make_polycos(char *parfilenm, infodata * idata)
{
   FILE *tmpfile;
   int tracklen;
   double T, fmid = 0.0, epoch;
   char command[100], *psrname, scopechar;
   psrparams psr;

   /* Read the parfile */
   epoch = idata->mjd_i + idata->mjd_f;
   T = (idata->dt * idata->N) / SECPERDAY;
   if (!get_psr_from_parfile(parfilenm, epoch, &psr)) {
      printf("\nError:  Cannot read parfile '%s'\n\n", parfilenm);
      exit(0);
   }

   /* Write tz.in */
   if (strcmp(idata->telescope, "GBT") == 0) {
      scopechar = '1';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "Arecibo") == 0) {
      scopechar = '3';
      tracklen = 3;
   } else if (strcmp(idata->telescope, "VLA") == 0) {
      scopechar = '6';
      tracklen = 6;
   } else if (strcmp(idata->telescope, "Parkes") == 0) {
      scopechar = '7';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "Jodrell") == 0) {
      scopechar = '8';
      tracklen = 12;
   } else if ((strcmp(idata->telescope, "GB43m") == 0) ||
              (strcmp(idata->telescope, "GB 140FT") == 0)){
      scopechar = 'a';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "Nancay") == 0) {
      scopechar = 'f';
      tracklen = 4;
   } else if (strcmp(idata->telescope, "Effelsberg") == 0) {
      scopechar = 'g';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "LOFAR") == 0) {
      scopechar = 't';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "FR606") == 0) {
      scopechar = 'u';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "UTR-2") == 0) {
      scopechar = 'w';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "DE601") == 0) {
      scopechar = 'x';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "UK608") == 0) {
      scopechar = 'y';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "WSRT") == 0) {
      scopechar = 'i';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "GMRT") == 0) {
      scopechar = 'r';
      tracklen = 12;
   } else if (strcmp(idata->telescope, "Geocenter") == 0) {
      scopechar = 'o';
      tracklen = 12;
   } else {                     /*  Barycenter */
      printf("Defaulting to barycenter for polyco generation...\n");
      scopechar = '@';
      tracklen = 12;
   }
   /* For optical, X-ray, or gamma-ray data */
   if (scopechar != '@' && scopechar != 'o') {
      fmid = idata->freq + (idata->num_chan / 2 - 0.5) * idata->chan_wid;
   } else {
      fmid = 0.0;
   }
   printf("Generating polycos for PSR %s.\n", psr.jname);
   tmpfile = chkfopen("tz.in", "w");
   fprintf(tmpfile, "%c %d 60 12 430\n\n\n%s 60 12 %d %.5f\n",
           scopechar, tracklen, psr.jname, tracklen, fmid);
   fclose(tmpfile);
   sprintf(command, "echo %d %d | tempo -z -f %s > /dev/null",
           idata->mjd_i-1, (int) ceil(epoch + T), parfilenm);
   // printf("making polycos:  '%s'\n", command);
   system(command);
   remove("tz.in");
   psrname = (char *) calloc(strlen(psr.jname) + 1, sizeof(char));
   strcpy(psrname, psr.jname);
   return psrname;
}
Esempio n. 26
0
char *make_polycos(char *parfilenm, infodata * idata, char *polycofilenm)
{
    FILE *tmpfile;
    int tracklen;
    double T, fmid = 0.0, epoch;
    char *command, *psrname, scopechar;
    char *pcpathnm, *pcfilenm;
    psrparams psr;

    /* Get the path and name of the output polycofilenm */
    split_path_file(polycofilenm, &pcpathnm, &pcfilenm);

    /* Read the parfile */
    epoch = idata->mjd_i + idata->mjd_f;
    T = (idata->dt * idata->N) / SECPERDAY;
    if (!get_psr_from_parfile(parfilenm, epoch, &psr)) {
        fprintf(stderr,
                "\nError:  Cannot read parfile '%s' in make_polycos()\n\n",
                parfilenm);
        exit(-1);
    }

    /* Generate temp directory */
    char tmpdir[] = "/tmp/polycoXXXXXX";
    if (mkdtemp(tmpdir) == NULL) {
        fprintf(stderr,
                "\nError:  Cannot generate temp dir '%s' in make_polycos()\n\n",
                tmpdir);
        exit(-1);
    }

    /* Copy the parfile to the temp directory */
    command = (char *) calloc(strlen(parfilenm) + strlen(tmpdir) +
                              strlen(pcfilenm) + strlen(pcpathnm) + 200, 1);
    sprintf(command, "cp %s %s/pulsar.par", parfilenm, tmpdir);
    if (system(command) != 0) {
        fprintf(stderr,
                "\nError:  Cannot copy parfile '%s' to tmpdir '%s' in make_polycos()\n\n",
                parfilenm, tmpdir);
        exit(-1);
    }

    /* change to temp dir */
    char *origdir = getcwd(NULL, 0);
    chdir(tmpdir);

    /* Write tz.in */
    if (strcmp(idata->telescope, "GBT") == 0) {
        scopechar = '1';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "Arecibo") == 0) {
        scopechar = '3';
        tracklen = 3;
    } else if (strcmp(idata->telescope, "VLA") == 0) {
        scopechar = '6';
        tracklen = 6;
    } else if (strcmp(idata->telescope, "Parkes") == 0) {
        scopechar = '7';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "Jodrell") == 0) {
        scopechar = '8';
        tracklen = 12;
    } else if ((strcmp(idata->telescope, "GB43m") == 0) ||
               (strcmp(idata->telescope, "GB 140FT") == 0) ||
               (strcmp(idata->telescope, "NRAO20") == 0)) {
        scopechar = 'a';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "Nancay") == 0) {
        scopechar = 'f';
        tracklen = 4;
    } else if (strcmp(idata->telescope, "Effelsberg") == 0) {
        scopechar = 'g';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "LOFAR") == 0) {
        scopechar = 't';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "WSRT") == 0) {
        scopechar = 'i';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "GMRT") == 0) {
        scopechar = 'r';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "LWA") == 0) {
        scopechar = 'x';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "SRT") == 0) {
        scopechar = 'z';
        tracklen = 12;
    } else if (strcmp(idata->telescope, "Geocenter") == 0) {
        scopechar = 'o';
        tracklen = 12;
    } else {                    /*  Barycenter */
        printf("Defaulting to barycenter for polyco generation...\n");
        scopechar = '@';
        tracklen = 12;
    }
    /* For optical, X-ray, or gamma-ray data */
    if (scopechar != '@' && scopechar != 'o') {
        fmid = idata->freq + (idata->num_chan / 2 - 0.5) * idata->chan_wid;
    } else {
        fmid = 0.0;
    }
    printf("Generating polycos for PSR %s.\n", psr.jname);
    tmpfile = chkfopen("tz.in", "w");
    fprintf(tmpfile, "%c %d 60 12 430\n\n\n%s 60 12 %d %.5f\n",
            scopechar, tracklen, psr.jname, tracklen, fmid);
    fclose(tmpfile);
    //sprintf(command, "echo %d %d | tempo -z -f %s > /dev/null",
    sprintf(command, "echo %d %d | tempo -z -f pulsar.par > /dev/null",
            idata->mjd_i - 1, (int) ceil(epoch + T));
    if (system(command) != 0) {
        fprintf(stderr,
                "\nError:  Problem running TEMPO in '%s' for make_polycos()\n\n",
                tmpdir);
        exit(-1);
    } else {
        sprintf(command, "cp polyco.dat %s/%s", pcpathnm, pcfilenm);
        if (system(command) != 0) {
            fprintf(stderr,
                    "\nError:  Cannot copy polyco.dat in '%s' to '%s' make_polycos()\n\n",
                    tmpdir, polycofilenm);
            exit(-1);
        }
        remove("polyco.dat");
        remove("pulsar.par");
        remove("tempo.lis");
        remove("tz.in");
        remove("tz.tmp");
    }
    chdir(origdir);
    free(origdir);
    free(pcpathnm);
    free(pcfilenm);
    free(command);
    remove(tmpdir);
    psrname = (char *) calloc(strlen(psr.jname) + 1, sizeof(char));
    strcpy(psrname, psr.jname);
    return psrname;
}
Esempio n. 27
0
int main(int argc, char *argv[])
{
   float minval = SMALLNUM, maxval = LARGENUM, inx = 0, iny = 0;
   int centern, offsetn;
   int zoomlevel, maxzoom = 0, minzoom, xid, psid;
   char *rootfilenm, inchar;
   datapart *lodp;
   dataview *dv;
   basicstats *statvals;

   if (argc == 1) {
      printf("\nusage:  exploredat datafilename\n\n");
      exit(0);
   }

   printf("\n\n");
   printf("      Interactive Data Explorer\n");
   printf("         by Scott M. Ransom\n");
   printf("            November, 2001\n");
   print_help();

   {
      int hassuffix = 0;
      char *suffix;

      hassuffix = split_root_suffix(argv[1], &rootfilenm, &suffix);
      if (hassuffix) {
         if (strcmp(suffix, "dat") != 0) {
            printf
                ("\nInput file ('%s') must be a single PRESTO data file ('.dat')!\n\n",
                 argv[1]);
            free(suffix);
            exit(0);
         }
         free(suffix);
      } else {
         printf("\nInput file ('%s') must be a PRESTO data file ('.dat')!\n\n",
                argv[1]);
         exit(0);
      }
   }

   /* Read the info file */

   readinf(&idata, rootfilenm);
   if (idata.object) {
      printf("Examining %s data from '%s'.\n\n",
             remove_whitespace(idata.object), argv[1]);
   } else {
      printf("Examining data from '%s'.\n\n", argv[1]);
   }
#ifdef USEMMAP
   mmap_file = open(argv[1], O_RDONLY);
   {
      int rt;
      struct stat buf;

      rt = fstat(mmap_file, &buf);
      if (rt == -1) {
         perror("\nError in fstat() in exploredat.c");
         printf("\n");
         exit(-1);
      }
      Ndat = buf.st_size / sizeof(float);
   }
   lodp = get_datapart(0, Ndat);
#else
   {
      int numsamp;

      datfile = chkfopen(argv[1], "rb");
      Ndat = chkfilelen(datfile, sizeof(float));
      numsamp = (Ndat > MAXPTS) ? (int) MAXPTS : (int) Ndat;
      lodp = get_datapart(0, numsamp);
   }
#endif

   /* Plot the initial data */

   centern = 0.5 * INITIALNUMPTS;
   if (centern > lodp->nn)
      centern = lodp->nn / 2;
   zoomlevel = LOGMAXDISPNUM - LOGINITIALNUMPTS;
   minzoom = LOGMAXDISPNUM - LOGMAXPTS;
   maxzoom = LOGMAXDISPNUM - LOGMINDISPNUM;
   dv = get_dataview(centern, zoomlevel, lodp);

   /* Prep the XWIN device for PGPLOT */

   xid = cpgopen("/XWIN");
   if (xid <= 0) {
      free_datapart(lodp);
#ifdef USEMMAP
      close(mmap_file);
#else
      fclose(datfile);
#endif
      free(dv);
      exit(EXIT_FAILURE);
   }
   cpgask(0);
   cpgpage();
   offsetn = plot_dataview(dv, minval, maxval, 1.0);

   do {
      cpgcurs(&inx, &iny, &inchar);
      if (DEBUGOUT)
         printf("You pressed '%c'\n", inchar);

      switch (inchar) {
      case ' ':                /* Toggle stats and sample plotting on/off */
         /* 0 = both, 1 = stats only, 2 = data only */
         plotstats++;
         plotstats = plotstats % 3;
         cpgpage();
         offsetn = plot_dataview(dv, minval, maxval, 1.0);
         break;
      case 'M':                /* Toggle between median and average */
      case 'm':
         usemedian = (usemedian) ? 0 : 1;
         free(dv);
         dv = get_dataview(centern, zoomlevel, lodp);
         cpgpage();
         offsetn = plot_dataview(dv, minval, maxval, 1.0);
         break;
      case 'A':                /* Zoom in */
      case 'a':
         centern = inx + offsetn;
      case 'I':
      case 'i':
         if (DEBUGOUT)
            printf("  Zooming in  (zoomlevel = %d)...\n", zoomlevel);
         if (zoomlevel < maxzoom) {
            zoomlevel++;
            free(dv);
            dv = get_dataview(centern, zoomlevel, lodp);
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
         } else
            printf("  Already at maximum zoom level (%d).\n", zoomlevel);
         break;
      case 'X':                /* Zoom out */
      case 'x':
      case 'O':
      case 'o':
         if (DEBUGOUT)
            printf("  Zooming out  (zoomlevel = %d)...\n", zoomlevel);
         if (zoomlevel > minzoom) {
            zoomlevel--;
            free(dv);
            dv = get_dataview(centern, zoomlevel, lodp);
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
         } else
            printf("  Already at minimum zoom level (%d).\n", zoomlevel);
         break;
      case '<':                /* Shift left 1 full screen */
         centern -= dv->numsamps + dv->numsamps / 8;
      case ',':                /* Shift left 1/8 screen */
         if (DEBUGOUT)
            printf("  Shifting left...\n");
         centern -= dv->numsamps / 8;
         {                      /* Should probably get the previous chunk from the datfile... */
            double lowestr;

            lowestr = 0.5 * dv->numsamps;
            if (centern < lowestr)
               centern = lowestr;
         }
         free(dv);
         dv = get_dataview(centern, zoomlevel, lodp);
         cpgpage();
         offsetn = plot_dataview(dv, minval, maxval, 1.0);
         break;
      case '>':                /* Shift right 1 full screen */
         centern += dv->numsamps - dv->numsamps / 8;
      case '.':                /* Shift right 1/8 screen */
         centern += dv->numsamps / 8;
         if (DEBUGOUT)
            printf("  Shifting right...\n");
         {                      /* Should probably get the next chunk from the datfile... */
            double highestr;

            highestr = lodp->nlo + lodp->nn - 0.5 * dv->numsamps;
            if (centern > highestr)
               centern = highestr;
         }
         free(dv);
         dv = get_dataview(centern, zoomlevel, lodp);
         cpgpage();
         offsetn = plot_dataview(dv, minval, maxval, 1.0);
         break;
      case '+':                /* Increase height of top edge */
         {
            float dy;

            if (maxval > 0.5 * LARGENUM) {
               printf("  Auto-scaling of top edge is off.\n");
               if (minval < 0.5 * SMALLNUM)
                  dy = dv->maxval - dv->minval;
               else
                  dy = dv->maxval - minval;
               maxval = dv->maxval + 0.1 * dy;
            } else {
               if (minval < 0.5 * SMALLNUM)
                  dy = maxval - dv->minval;
               else
                  dy = maxval - minval;
               maxval += 0.1 * dy;
            }
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
            break;
         }
      case '_':                /* Decrease height of top edge */
         {
            float dy;

            if (maxval > 0.5 * LARGENUM) {
               printf("  Auto-scaling of top edge is off.\n");
               if (minval < 0.5 * SMALLNUM)
                  dy = dv->maxval - dv->minval;
               else
                  dy = dv->maxval - minval;
               maxval = dv->maxval - 0.1 * dy;
            } else {
               if (minval < 0.5 * SMALLNUM)
                  dy = maxval - dv->minval;
               else
                  dy = maxval - minval;
               maxval -= 0.1 * dy;
            }
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
            break;
         }
      case '=':                /* Increase height of bottom edge */
         {
            float dy;

            if (minval < 0.5 * SMALLNUM) {
               printf("  Auto-scaling of bottom edge is off.\n");
               if (maxval > 0.5 * LARGENUM)
                  dy = dv->maxval - dv->minval;
               else
                  dy = maxval - dv->minval;
               minval = dv->minval + 0.1 * dy;
            } else {
               if (maxval > 0.5 * LARGENUM)
                  dy = dv->maxval - minval;
               else
                  dy = maxval - minval;
               minval += 0.1 * dy;
            }
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
            break;
         }
      case '-':                /* Decrease height of bottom edge */
         {
            float dy;

            if (minval < 0.5 * SMALLNUM) {
               printf("  Auto-scaling of bottom edge is off.\n");
               if (maxval > 0.5 * LARGENUM)
                  dy = dv->maxval - dv->minval;
               else
                  dy = maxval - dv->minval;
               minval = dv->minval - 0.1 * dy;
            } else {
               if (maxval > 0.5 * LARGENUM)
                  dy = dv->maxval - minval;
               else
                  dy = maxval - minval;
               minval -= 0.1 * dy;
            }
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
            break;
         }
      case 'S':                /* Auto-scale */
      case 's':
         printf("  Auto-scaling is on.\n");
         minval = SMALLNUM;
         maxval = LARGENUM;
         cpgpage();
         offsetn = plot_dataview(dv, minval, maxval, 1.0);
         break;
      case 'G':                /* Goto a time */
      case 'g':
         {
            char timestr[50];
            double time = -1.0;

            while (time < 0.0) {
               printf
                   ("  Enter the time (s) from the beginning of the file to go to:\n");
               fgets(timestr, 50, stdin);
               timestr[strlen(timestr) - 1] = '\0';
               time = atof(timestr);
            }
            offsetn = 0.0;
            centern = (int) (time / idata.dt + 0.5);
            printf("  Moving to time %.15g (data point %d).\n", time, centern);
            free(dv);
            dv = get_dataview(centern, zoomlevel, lodp);
            cpgpage();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
         }
         break;
      case '?':                /* Print help screen */
         print_help();
         break;
      case 'P':                /* Print the current plot */
      case 'p':
         {
            int len;
            char filename[200];

            printf("  Enter the filename to save the plot as:\n");
            fgets(filename, 195, stdin);
            len = strlen(filename) - 1;
            filename[len + 0] = '/';
            filename[len + 1] = 'C';
            filename[len + 2] = 'P';
            filename[len + 3] = 'S';
            filename[len + 4] = '\0';
            psid = cpgopen(filename);
            cpgslct(psid);
            cpgpap(10.25, 8.5 / 11.0);
            cpgiden();
            offsetn = plot_dataview(dv, minval, maxval, 1.0);
            cpgclos();
            cpgslct(xid);
            filename[len] = '\0';
            printf("  Wrote the plot to the file '%s'.\n", filename);
         }
         break;
      case 'V':                /* Show the basic statistics for the current dataview */
      case 'v':
         statvals = calc_stats(dv, lodp);
         printf("\n  Statistics:\n"
                "    Low sample               %d\n"
                "    Number of samples        %d\n"
                "    Low time (s)             %.7g\n"
                "    Duration of samples (s)  %.7g\n"
                "    Maximum value            %.7g\n"
                "    Minimum value            %.7g\n"
                "    Average value            %.7g\n"
                "    Median value             %.7g\n"
                "    Standard Deviation       %.7g\n"
                "    Skewness                 %.7g\n"
                "    Kurtosis                 %.7g\n\n",
                dv->lon, dv->numsamps, dv->lon * idata.dt, dv->numsamps * idata.dt,
                statvals->max, statvals->min, statvals->average,
                statvals->median, statvals->stdev,
                statvals->skewness, statvals->kurtosis);
         free(statvals);
         break;
      case 'Q':                /* Quit */
      case 'q':
         printf("  Quitting...\n");
         free(dv);
         cpgclos();
         break;
      default:
         printf("  Unrecognized option '%c'.\n", inchar);
         break;
      }
   } while (inchar != 'Q' && inchar != 'q');

   free_datapart(lodp);
#ifdef USEMMAP
   close(mmap_file);
#else
   fclose(datfile);
#endif
   printf("Done\n\n");
   return 0;
}
Esempio n. 28
0
multifile *fopen_multifile(int numfiles, char **filenames, char *mode,
                           long long maxlen)
/* Open a multifile for use and return the multifile structure.  */
/*  'numfiles' is the number of files in the multifile.          */
/*  'filenames' is an array of the names of the component files  */
/*  'mode' is the method of opening the file (binary is assumed) */
/* 	"r" : read only, do not create (truncate) the files      */
/*      "r+": read+write, do not create (truncate) the files     */
/*      "w" : read+write, create (truncate) the files            */
/*      "a" : read+write, create files or open at end of files   */
/*  'maxlen' is the maximum length in bytes of each file.  This  */
/*      number is only used if a file is opened fo writing.  The */
/*      default value is DEFAULT_MAXLEN.  If you want to use     */
/*      the default, simply set 'maxlen' to 0.                   */
{
   multifile *mfile;
   int ii, append = 0;

   mfile = (multifile *) malloc(sizeof(multifile));
   mfile->numfiles = numfiles;
   mfile->currentfile = 0;
   mfile->currentpos = 0;
   mfile->length = 0;
   mfile->position = 0;
   if (maxlen > 0)
      mfile->maxfilelen = maxlen;
   else
      mfile->maxfilelen = DEFAULT_MAXLEN;
   mfile->filelens = (long long *) malloc(numfiles * sizeof(long long));
   mfile->filenames = (char **) malloc(numfiles * sizeof(char *));
   mfile->fileptrs = (FILE **) malloc(numfiles * sizeof(FILE *));
   if (strncmp(mode, "r", 1) == 0) {
      if (strncmp(mode, "r+", 2) == 0) {
         strcpy(mfile->mode, "rb+");
      } else {
         strcpy(mfile->mode, "rb");
      }
   } else if (strncmp(mode, "w", 1) == 0) {
      if (strncmp(mode, "w+", 2) == 0) {
         strcpy(mfile->mode, "wb+");
      } else {
         strcpy(mfile->mode, "wb");
      }
   } else if (strncmp(mode, "a", 1) == 0) {
      if (strncmp(mode, "a+", 2) == 0) {
         strcpy(mfile->mode, "ab+");
      } else {
         strcpy(mfile->mode, "ab");
      }
      append = 1;
   } else {
      printf("\n'mode' = '%s' in open_multifile() is not valid.\n\n", mode);
      return NULL;
   }
   for (ii = 0; ii < numfiles; ii++) {
      mfile->filenames[ii] = (char *) calloc(strlen(filenames[ii]) + 1, 1);
      strcpy(mfile->filenames[ii], filenames[ii]);
      mfile->fileptrs[ii] = chkfopen(filenames[ii], mfile->mode);
      mfile->filelens[ii] = chkfilelen(mfile->fileptrs[ii], 1);
      mfile->length += mfile->filelens[ii];
   }
   if (append) {
      mfile->currentfile = numfiles - 1;
      chkfileseek(mfile->fileptrs[mfile->currentfile], 0, 1, SEEK_END);
   }
   return mfile;
}
Esempio n. 29
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. 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);
}