Ejemplo n.º 1
0
def read_header(filename, verbose=False):
    """Read the header of a filterbank file, and return
        a dictionary of header paramters and the header's
        size in bytes.

        Inputs:
            filename: Name of the filterbank file.
            verbose: If True, be verbose. (Default: be quiet)

        Outputs:
            header: A dictionary of header paramters.
            header_size: The size of the header in bytes.
    """
    header = {}
    filfile = open(filename, 'rb')
    filfile.seek(0)
    paramname = ""
    while (paramname != 'HEADER_END'):
        if verbose:
            print "File location: %d" % filfile.tell()
        paramname, val = sigproc.read_hdr_val(filfile, stdout=verbose)
        if verbose:
            print "Read param %s (value: %s)" % (paramname, val)
        if paramname not in ["HEADER_START", "HEADER_END"]:
            header[paramname] = val
    header_size = filfile.tell()
    filfile.close()
    return header, header_size
Ejemplo n.º 2
0
def read_header(filename, verbose=False):
    """Read the header of a filterbank file, and return
        a dictionary of header paramters and the header's
        size in bytes.

        Inputs:
            filename: Name of the filterbank file.
            verbose: If True, be verbose. (Default: be quiet)

        Outputs:
            header: A dictionary of header paramters.
            header_size: The size of the header in bytes.
    """
    header = {}
    filfile = open(filename, 'rb')
    filfile.seek(0)
    paramname = ""
    while (paramname != 'HEADER_END'):
        if verbose:
            print "File location: %d" % filfile.tell()
        paramname, val = sigproc.read_hdr_val(filfile, stdout=verbose)
        if verbose:
            print "Read param %s (value: %s)" % (paramname, val)
        if paramname not in ["HEADER_START", "HEADER_END"]:
            header[paramname] = val
    header_size = filfile.tell()
    filfile.close()
    return header, header_size
Ejemplo n.º 3
0
def read_filterbank(filename,DM,bin_start,duration,offset,RFI_reduct=False,mask=False):
  if (not 'filterbank' in sys.modules) or (not 'sigproc' in sys.modules):
    logging.warning("Utilities - Additional modules missing")
    return
  
  bin_start = np.int(bin_start)
  
  #Read data from filterbank file
  head = filterbank.read_header(filename)[0]

  nbits = head['nbits']
  nchans = head['nchans']
  dtype = filterbank.get_dtype(nbits)

  filfile = open(filename, 'rb')
  filfile.seek(0)
  paramname = ""
  while (paramname != 'HEADER_END'): paramname, val = sigproc.read_hdr_val(filfile)
  header_size = filfile.tell()
  filfile.close()

  file_size = os.stat(filename)[6]
  data_size = file_size - header_size
  bytes_per_spectrum= nchans * nbits / 8
  nspec = data_size / bytes_per_spectrum
  
  bin_start -= offset
  if bin_start<0: bin_start = 0
  bin_end = bin_start + DM2delay(DM) + duration + 2*offset
  if bin_end >= nspec: bin_end = nspec
  
  spectrum = np.memmap(filename, dtype=dtype, mode='r', offset=header_size, shape=(nspec, nchans))
  spectrum = spectrum[bin_start:bin_end].copy()
  spectrum = np.fliplr(spectrum)
  
  if mask:
    #Zap the channels from the mask file
    zap_chans, zap_ints, chans_per_int, ptsperint = read_mask(mask,bin_start,bin_end)
    med_value = np.median(spectrum)
    
    spectrum[:,zap_chans] = med_value
    zap_ints = zap_ints[(zap_ints>=bin_start)&(zap_ints<=bin_end)]
    zap_ints -= bin_start
    spectrum[zap_ints,:] = med_value
    
    mask_start = int(np.floor(float(bin_start)/ptsperint)) * ptsperint - bin_start
    for i,chans in enumerate(chans_per_int):
      idx_start = mask_start + i * ptsperint
      idx_end = mask_start + (i + 1) * ptsperint
      spectrum[idx_start:idx_end,chans] = med_value

  if RFI_reduct:
      np.clip(spectrum - np.median(spectrum,axis=0) + 128, 0, 255, out=spectrum)    
  
  return spectrum
Ejemplo n.º 4
0
    def read_header(self):
        if not self.already_read_header:
            self.header_params = []
            self.header = {}
            self.already_read_header = True
            self.seek_to_header_start()
            param = ""
            while (param != 'HEADER_END'):
                param, val = sigproc.read_hdr_val(self.filfile)
                self.header[param] = val
                self.header_params.append(param)

            # Calculate additional information
            # Such as: datatype, numsamps, datasize, hdrsize
            if self.nbits == 32:
                self.dtype = 'float32'
            else:
                self.dtype = 'uint%d' % self.nbits
            self.header_size = self.filfile.tell()
            self.data_size = os.stat(self.filename)[6] - self.header_size
            bytes_per_sample = self.nchans * (self.nbits / 8)
            if self.data_size % bytes_per_sample:
                warnings.warn("Not an integer number of samples in file.")
            self.number_of_samples = self.data_size / bytes_per_sample
Ejemplo n.º 5
0
 def read_header(self):
     if not self.already_read_header:
         self.header_params = []
         self.header = {}
         self.already_read_header = True
         self.seek_to_header_start()
         param = ""
         while (param != 'HEADER_END'):
             param, val = sigproc.read_hdr_val(self.filfile)
             self.header[param] = val
             self.header_params.append(param)
         
         # Calculate additional information
         # Such as: datatype, numsamps, datasize, hdrsize
         if self.nbits == 32:
             self.dtype = 'float32'
         else:
             self.dtype = 'uint%d' % self.nbits
         self.header_size = self.filfile.tell()
         self.data_size = os.stat(self.filename)[6] - self.header_size
         bytes_per_sample = self.nchans * (self.nbits/8)
         if self.data_size % bytes_per_sample:
             warnings.warn("Not an integer number of samples in file.")
         self.number_of_samples = self.data_size / bytes_per_sample
Ejemplo n.º 6
0
    basefilenm = sys.argv[2][:sys.argv[2].find(".fil")]
    
    filhdr = {}
    newhdr = ""
    infile = open(sys.argv[2], 'rb')

    # Determine the full size of the file
    infile.seek(0, 2)
    infilelen = infile.tell()
    infile.seek(0, 0)
    
    outfile = open(basefilenm+"_DS%d.fil"%DS_fact, 'wb')

    # Loop over the values in the .fil file
    while 1:
        param, val = sigproc.read_hdr_val(infile, stdout=False)
        filhdr[param] = val

        if param=="tsamp":
            val *= DS_fact

        # Append to the new hdr string
        newhdr += sigproc.addto_hdr(param, val)

        # Break out of the loop if the header is over
        if param=="HEADER_END":
            break

    # Write the new header to the output file
    outfile.write(newhdr)