コード例 #1
0
int main(int argc, char *argv[])
{
   FILE **infiles, *outfile = NULL, *scalingfile;
   int filenum, argnum = 1, ii = 0, ptsperblock, numlags, numfiles;
   int bytes_per_read, scaling = 0, numscalings, output = 1;
   long long N;
   char *path, *filenm;
   char outfilenm[200], filenmbase[200], scalingnm[200], rawlags[4096];
   unsigned char output_samples[2048];
   float *scalings = NULL;
   double dt, T;
   SPIGOT_INFO *spigots;
   sigprocfb fb;
   infodata idata;

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

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

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

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

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

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

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

      /* Loop over the samples in the file */
      while (chkfread(rawlags, bytes_per_read, 1, infiles[filenum])) {
         if (scaling)
            convert_SPIGOT_point(rawlags, output_samples, SUMIFS, scalings[ii]);
         else
            convert_SPIGOT_point(rawlags, output_samples, SUMIFS, 1.0);
         ii++;
         /* Invert the band so that the high freqs are first */
         /* This is how SIGPROC stores its data.             */
         {
            int jj;
            unsigned char tempzz = 0.0, *loptr, *hiptr;
            loptr = output_samples + 0;
            hiptr = output_samples + numlags - 1;
            for (jj = 0; jj < numlags / 2; jj++, loptr++, hiptr--) {
               SWAP(*loptr, *hiptr);
            }
         }
         chkfwrite(output_samples, sizeof(unsigned char), fb.nchans, outfile);
      }
      fclose(infiles[filenum]);
      if (outfile != stdout)
         fclose(outfile);
   }
   if (outfile != stdout)
      fprintf(stderr, "Converted and wrote %d samples.\n\n", ii);
   if (scaling)
      vect_free(scalings);
   free(spigots);
   free(path);
   free(filenm);
   free(infiles);
   return 0;
}
コード例 #2
0
ファイル: spigot2filterbank.c プロジェクト: aarchiba/presto
int main(int argc, char *argv[])
{
   FILE **infiles, *outfile = NULL, *scalingfile, *zerolagfile = NULL;
   int filenum, ptsperblock, numlags, numfiles, outnumlags;
   int bytes_per_read, scaling = 0, numscalings, firstfile, firstspec;
   int write_data = 1, update_posn = 0;
   long long N, numconverted = 0, numwritten = 0;
   char *path, *filenm, rawlags[4096];
   char outfilenm[200], filenmbase[200], zerolagnm[200], scalingnm[200];
   unsigned char output_samples[2048];
   float *scalings = NULL, lags[2048], tmp_floats[2048];
   double dt, T, time_offset;
   SPIGOT_INFO *spigots;
   sigprocfb fb;
   infodata idata;
   Cmdline *cmd;

   /* Call usage() if we have no command line arguments */
   if (argc == 1) {
      Program = argv[0];
      usage();
      exit(0);
   }

   /* Parse the command line using the excellent program Clig */
   cmd = parseCmdline(argc, argv);
   // showOptionValues();
   numfiles = cmd->argc;

   /* If requesting that we skip values or only write          */
   /* a specific number of values, require an output file name */
   if ((cmd->skip > 0) || cmd->numoutP){
      if (!cmd->outfileP && !cmd->stdoutP) {
         fprintf(stderr,
                 "\nspigot2filterbank ERROR:  You need to specify an output\n"
                 "     filename (or -stdout) when using the -skip and/or \n"
                 "     -numout options!\n\n");
         exit(1);
      }
   }

   /* Give an error if specifying stdout and an output file */
   if (cmd->stdoutP && cmd->outfileP){
      fprintf(stderr, 
              "\nspigot2filterbank ERROR:  You cannot specify both an output\n"
              "     file and that the data should go to STDOUT!\n\n");
      exit(1);
   }

   if (!cmd->stdoutP)
      printf("\nConverting SPIGOT FITs lags into SIGPROC filterbank format...\n\n");
   
   /* Determine the filename base from the first spigot file */
   split_path_file(cmd->argv[0], &path, &filenm);
   strncpy(filenmbase, filenm, strlen(filenm) - 5);
   filenmbase[strlen(filenm) - 5] = '\0';
   free(path);
   free(filenm);

   /* Open a file to store the zerolags */
   if (cmd->zerolagsP) {
      if (cmd->outfileP) {
         sprintf(zerolagnm, "%s.zerolags", cmd->outfile);
      } else {
         sprintf(zerolagnm, "%s.zerolags", filenmbase);
      }
      zerolagfile = chkfopen(zerolagnm, "wb");
   }

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

   /* Read and convert the basic SPIGOT file information */
   if (!cmd->stdoutP)
      printf("Spigot card input file information:\n");
   spigots = (SPIGOT_INFO *) malloc(sizeof(SPIGOT_INFO) * numfiles);
   infiles = (FILE **) malloc(sizeof(FILE *) * numfiles);
   for (filenum = 0; filenum < numfiles; filenum++) {
      if (!cmd->stdoutP)
         printf("  '%s'\n", cmd->argv[filenum]);
      infiles[filenum] = chkfopen(cmd->argv[filenum], "rb");
      read_SPIGOT_header(cmd->argv[filenum], spigots + filenum);
      rewind(infiles[filenum]);
   }
   if (!cmd->stdoutP) 
      printf("\n");

   /* The following is necessary in order to initialize all the */
   /* static variables in spigot.c                              */
   get_SPIGOT_file_info(infiles, spigots, numfiles, 0, 0, &N,
                        &ptsperblock, &numlags, &dt, &T, &idata, 
                        !cmd->stdoutP);
   
   /* Compute the first file required */
   firstfile = cmd->skip / spigots[0].samples_per_file;
   firstspec = cmd->skip % spigots[0].samples_per_file;
   if (!cmd->numoutP) {
      cmd->numout = (N - cmd->skip) / cmd->downsamp;
   }
   bytes_per_read = numlags * spigots[0].bits_per_lag / 8;
   outnumlags = numlags - cmd->lokill - cmd->hikill;

   /* Step through the SPIGOT files */
   filenum = firstfile;
   while (numwritten < cmd->numout){
      split_path_file(cmd->argv[filenum], &path, &filenm);
      strncpy(filenmbase, filenm, strlen(filenm) - 5);
      filenmbase[strlen(filenm) - 5] = '\0';
      if (cmd->outfileP) {
         if (filenum==firstfile) {
            sprintf(outfilenm, "%s", cmd->outfile);
            outfile = chkfopen(outfilenm, "wb");
         }
      } else {
         sprintf(outfilenm, "%s.fil", filenmbase);
         if (cmd->stdoutP) {
            outfile = stdout;
         } else {
            outfile = chkfopen(outfilenm, "wb");
         }
      }
      if (!cmd->stdoutP) {
         if (filenum==firstfile)
            printf("Reading Spigot lags from '%s' (starting at sample %d)\n", 
                   cmd->argv[filenum], firstspec);
         else
            printf("Reading Spigot lags from '%s'\n", 
                   cmd->argv[filenum]);
         printf("Writing filterbank spectra to   '%s'\n\n", outfilenm);
      }
      
      /* Update the filterbank header information for each file */
      if (!spigots[filenum].tracking) {
         if (cmd->outfileP || cmd->stdoutP) {
            time_offset = 0.5 * cmd->numout *                   \
               spigots[filenum].dt_us * 1e-6 * cmd->downsamp;
         } else { // Just normal files
            time_offset = 0.5 * spigots[filenum].file_duration;
         }
         update_posn = 1;
      } else {
         update_posn = 0;
         time_offset = 0.0;
      }
      /* Adjust the Spigot start time for the skip */
      spigots[filenum].elapsed_time += cmd->skip * spigots[filenum].dt_us * 1e-6;
      /* Determine the SIGPROC header */
      spigot2sigprocfb(&(spigots[filenum]), &fb, filenmbase, 
                       cmd->lokill, cmd->hikill, cmd->downsamp, 
                       update_posn, time_offset);
      /* Correct the structure if we are using floats */
      if (cmd->floatsP) fb.nbits = 32;
      
      /* Write a filterbank header if we have not been told not to.   */
      /* Don't write it, though, if using stdio or a specified output */
      /* file and the input file is not the first.                    */
      if (!cmd->nohdrP) {
         if ((!cmd->stdoutP && !cmd->outfileP) ||
             ((cmd->stdoutP || cmd->outfileP) && filenum==firstfile)) {
            write_filterbank_header(&fb, outfile);
         }
      }

      /* Go to the correct autocorrelation in the correct first FITs file */
      chkfseek(infiles[filenum], 
               spigots[filenum].header_len + bytes_per_read*firstspec,
               SEEK_SET);
      
      /* Loop over the samples in the file */
      while ((numwritten < cmd->numout) && 
             (chkfread(rawlags, bytes_per_read, 1, infiles[filenum]))) {
         if (cmd->zerolagsP) {
            /* Correct the lags so we can write the zerolag */
            get_calibrated_lags(rawlags, lags);
            chkfwrite(lags, sizeof(float), 1, zerolagfile);
         }
         if (scaling) {
            convert_SPIGOT_point(rawlags, output_samples, SUMIFS, 
                                 scalings[cmd->skip+numconverted]);
         } else {
            convert_SPIGOT_point(rawlags, output_samples, SUMIFS, 1.0);
         }
         /* If downsampling, average the current spectra */
         if (cmd->downsamp > 1) {
            int ii;
            if (numconverted % cmd->downsamp == 0) {
               write_data = 0;
               /* Zero the array used for averaging */
               for (ii = 0; ii < outnumlags; ii++)
                  tmp_floats[ii] = 0.0;
            } else {
               /* Add the current data to the array used for averaging */
               for (ii = 0; ii < outnumlags; ii++)
                  tmp_floats[ii] += (float) output_samples[ii+cmd->lokill];
               /* If that was the last sample to be added, average them */
               /* and put them back into output_samples */
               if (numconverted % cmd->downsamp == (cmd->downsamp-1)) {
                  write_data = 1;
                  for (ii = 0; ii < outnumlags; ii++) {
                     tmp_floats[ii] /= (float) cmd->downsamp;
                     output_samples[ii+cmd->lokill] = \
                        (unsigned char)(tmp_floats[ii]+0.5);
                  }
               }
            }
         }
         numconverted++;

         if (write_data) {
            int ii;

            /* Invert the band so that the high freqs are first */
            /* This is how SIGPROC stores its data.             */
            if (cmd->floatsP) {
               float tempzz = 0.0, *loptr, *hiptr;
               loptr = tmp_floats;  //  killed channels are already gone
               hiptr = tmp_floats + outnumlags - 1;
               for (ii = 0; ii < outnumlags / 2; ii++, loptr++, hiptr--) {
                  SWAP(*loptr, *hiptr);
               }
            } else {
               unsigned char tempzz = 0.0, *loptr, *hiptr;
               loptr = output_samples + cmd->lokill;
               hiptr = output_samples + cmd->lokill + outnumlags - 1;
               for (ii = 0; ii < outnumlags / 2; ii++, loptr++, hiptr--) {
                  SWAP(*loptr, *hiptr);
               }
            }

            /* Now actually write the data */
            if (cmd->floatsP) {
               /* Copy the bytes to floats */
               for (ii = 0; ii < outnumlags; ii++)
                  tmp_floats[ii] = (float) output_samples[ii+cmd->lokill];
               chkfwrite(tmp_floats, sizeof(float), fb.nchans, outfile);
            } else {
               chkfwrite(output_samples+cmd->lokill, 
                         sizeof(unsigned char), fb.nchans, outfile);
            }
            numwritten++;
         }
      }
      if ((!cmd->stdoutP) && (!cmd->outfileP))
         fclose(outfile);
      fclose(infiles[filenum]);
      firstspec = 0;
      filenum++;
      free(path);
      free(filenm);
   }
   if ((!cmd->stdoutP) && (cmd->outfileP))
      fclose(outfile);
   if (cmd->zerolagsP)
      fclose(zerolagfile);
   if (!cmd->stdoutP)
      fprintf(stderr, "Converted %lld samples and wrote %lld.\n\n", 
              numconverted, numwritten);
   if (scaling)
      free(scalings);
   free(spigots);
   free(infiles);
   return 0;
}