Beispiel #1
0
def convert_tim_to_dat(tim):
    """Convert a SIGPROC time series .tim file to a
        PRESTO .dat time series

        Input:
            tim: The SIGPROC .tim time series file to convert.

        Output:
            datfn: The PRESTO .dat time series file
    """
    if not tim.endswith(".tim"):
        raise ValueError("Was expecting a file name ending with '.tim'. "
                         "Got: %s" % tim)
    path, fn = os.path.split(tim)
    basenm = fn[:-4]
    outfn = os.path.join(path, basenm+".dat")
    hdr, hdrlen = sigproc.read_header(tim)

    N = sigproc.samples_per_file(tim, hdr, hdrlen)
    Ndone = 0
    status = -1
    with open(tim, 'rb') as inff, open(outfn, 'wb') as outff:
        inff.seek(hdrlen)

        data = np.fromfile(inff, dtype='float32', count=BLOCKSIZE)
        while data.size:
            data.tofile(outff)
            Ndone += data.size
            data = np.fromfile(inff, dtype='float32', count=BLOCKSIZE)
            newstatus = int(100.0*Ndone/N)
            if newstatus > status:
                sys.stdout.write(" %d %%\r" % newstatus)
                sys.stdout.flush()
                status = newstatus
    return outfn
Beispiel #2
0
def read_jobs_queue(jobs):
    nb_jobs_sub=0
    try:
        file = open("%s/queue.dat"%basedir, "r")
        for line in file.readlines():

	    #
	    (job,box) = line.split()
	    print job,box
            box=box.replace("\n","")

            jobs_list.append(job)
            jobs_wlist.append(job)
	    jobs[job]=Job()
	    jobs[job].box=box

	    # Determine size 
	    fil_filenm = "/data5/%s"%job.replace(".fbk","_p00.fbk")
	    filhdr, hdrlen = sigproc.read_header(fil_filenm)
	    orig_N = sigproc.samples_per_file(fil_filenm, filhdr, hdrlen)

	    if orig_N > 4194304:
	      logfile.write('Job %s detected L'%job);logfile.flush()
	      jobs[job].long=1  
	    ###################  

	    nb_jobs_sub += 1
        file.close()
        return nb_jobs_sub 	
    except:
        return 0
    def __init__(self, fil_filenm, box):
        self.fil_filenm = fil_filenm
        self.basefilenm = fil_filenm.rstrip(".sig")
        #self.beam = int(self.basefilenm[-1])
        self.beam = int(self.basefilenm)
        filhdr, self.hdrlen = sigproc.read_header(fil_filenm)
        self.orig_filenm = filhdr['rawdatafile']
        self.MJD = filhdr['tstart']
        self.nchans = filhdr['nchans']
        self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
        self.ra_string = psr_utils.coord_to_string(\
            *psr_utils.rad_to_hms(self.ra_rad))
        self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
        self.dec_string = psr_utils.coord_to_string(\
            *psr_utils.rad_to_dms(self.dec_rad))
        self.az = filhdr['az_start']
        self.el = 90.0-filhdr['za_start']
        self.BW = abs(filhdr['foff']) * filhdr['nchans']
        self.dt = filhdr['tsamp']
        self.orig_N = sigproc.samples_per_file(fil_filenm, filhdr, self.hdrlen)
        self.orig_T = self.orig_N * self.dt
        self.N = choose_N(self.orig_N)
        #self.N = 2097152 
        self.T = self.N * self.dt
        # Update the RA and DEC from the database file if required
        #newposn = read_db_posn(self.orig_filenm, self.beam)
        #if newposn is not None:
        #    self.ra_string, self.dec_string = newposn
            # ... and use them to update the filterbank file
        #    fix_fil_posn(fil_filenm, self.hdrlen,
        #                 self.ra_string, self.dec_string)
        # Determine the average barycentric velocity of the observation
        #self.baryv = get_baryv(self.ra_string, self.dec_string,
        #                       self.MJD, self.T, obs="NC")
        # Where to dump all the results
        # Directory structure is under the base_output_directory
        # according to base/MJD/filenmbase/beam
        self.outputdir = os.path.join(base_output_directory,box,
                                      self.basefilenm)
        # Figure out which host we are processing on
        self.hostname = socket.gethostname()
        # The fraction of the data recommended to be masked by rfifind
        self.masked_fraction = 0.0
        # Initialize our timers
        self.rfifind_time = 0.0
        self.downsample_time = 0.0
        self.subbanding_time = 0.0
        self.dedispersing_time = 0.0
        self.FFT_time = 0.0
        self.lo_accelsearch_time = 0.0
        self.hi_accelsearch_time = 0.0
        self.singlepulse_time = 0.0
        self.sifting_time = 0.0
        self.folding_time = 0.0
        self.total_time = 0.0
        # Inialize some candidate counters
        self.num_sifted_cands = 0
        self.num_folded_cands = 0
        self.num_single_cands = 0
	self.nb_sp = 0
Beispiel #4
0
def convert_tim_to_dat(tim):
    """Convert a SIGPROC time series .tim file to a
        PRESTO .dat time series

        Input:
            tim: The SIGPROC .tim time series file to convert.

        Output:
            datfn: The PRESTO .dat time series file
    """
    if not tim.endswith(".tim"):
        raise ValueError("Was expecting a file name ending with '.tim'. "
                         "Got: %s" % tim)
    path, fn = os.path.split(tim)
    basenm = fn[:-4]
    outfn = os.path.join(path, basenm + ".dat")
    hdr, hdrlen = sigproc.read_header(tim)

    N = sigproc.samples_per_file(tim, hdr, hdrlen)
    Ndone = 0
    status = -1
    with open(tim, 'rb') as inff, open(outfn, 'wb') as outff:
        inff.seek(hdrlen)

        data = np.fromfile(inff, dtype='float32', count=BLOCKSIZE)
        while data.size:
            data.tofile(outff)
            Ndone += data.size
            data = np.fromfile(inff, dtype='float32', count=BLOCKSIZE)
            newstatus = int(100.0 * Ndone / N)
            if newstatus > status:
                sys.stdout.write(" %d %%\r" % newstatus)
                sys.stdout.flush()
                status = newstatus
    return outfn
Beispiel #5
0
 def __init__(self, fil_filenm):
     self.fil_filenm = fil_filenm
     self.basefilenm = fil_filenm[:fil_filenm.find(".fil")]
     filhdr, hdrlen = sigproc.read_header(fil_filenm)
     self.MJD = filhdr['tstart']
     self.nchans = filhdr['nchans']
     self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
     self.ra_string = pu.coord_to_string(*pu.rad_to_hms(self.ra_rad))
     self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
     self.dec_string = pu.coord_to_string(*pu.rad_to_dms(self.dec_rad))
     self.str_coords = "J" + "".join(self.ra_string.split(":")[:2])
     if self.dec_rad >= 0.0: self.str_coords += "+"
     self.str_coords += "".join(self.dec_string.split(":")[:2])
     self.az = filhdr['az_start']
     self.el = 90.0 - filhdr['za_start']
     fillen = os.stat(fil_filenm)[6]
     self.raw_N = (fillen - hdrlen) / (filhdr['nbits'] /
                                       8) / filhdr['nchans']
     self.dt = filhdr['tsamp']
     self.raw_T = self.raw_N * self.dt
     self.N = orig_N
     self.T = self.N * self.dt
     # Determine the average barycentric velocity of the observation
     self.baryv = get_baryv(self.ra_string,
                            self.dec_string,
                            self.MJD,
                            self.T,
                            obs="GB")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_dir, str(int(self.MJD)),
                                   self.str_coords)
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
Beispiel #6
0
    def get_pulsar_parameters(self):
        # select .fil file
        fils = glob.glob(self.dir_path + '/*.fil')
        fil = fils[0]

        # grab name of pulsar from the .fil with sigproc function read_header (dictionary)
        fil_dic = sigproc.read_header(fil)[0]
        pname = fil_dic['source_name'][:-3]
        antenna = fil_dic['source_name'][-2:]
        mjd = fil_dic['tstart']
        nchans = fil_dic['nchans']

        return pname, antenna, mjd, nchans
 def __init__(self, fil_filenm):
     self.fil_filenm = fil_filenm
     self.basefilenm = fil_filenm[:fil_filenm.find(".fil")]
     filhdr, hdrlen = sigproc.read_header(fil_filenm)
     self.MJD = filhdr['tstart']
     self.nchans = filhdr['nchans']
     self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
     self.ra_string = pu.coord_to_string(*pu.rad_to_hms(self.ra_rad))
     self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
     self.dec_string = pu.coord_to_string(*pu.rad_to_dms(self.dec_rad))
     self.str_coords = "J"+"".join(self.ra_string.split(":")[:2])
     if self.dec_rad >= 0.0:  self.str_coords += "+"
     self.str_coords += "".join(self.dec_string.split(":")[:2])
     self.az = filhdr['az_start']
     self.el = 90.0-filhdr['za_start']
     fillen = os.stat(fil_filenm)[6]
     self.raw_N = (fillen-hdrlen)/(filhdr['nbits']/8)/filhdr['nchans']
     self.dt = filhdr['tsamp']
     self.raw_T = self.raw_N * self.dt
     self.N = orig_N
     self.T = self.N * self.dt
     # Determine the average barycentric velocity of the observation
     self.baryv = get_baryv(self.ra_string, self.dec_string,
                            self.MJD, self.T, obs="GB")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_dir,
                                   str(int(self.MJD)),
                                   self.str_coords)
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
Beispiel #8
0
def main():
    for tim in args.timfiles:
        print "Working on %s" % tim
        if args.write_dat:
            try:
                datfn = convert_tim_to_dat(tim)
                print "    Wrote PRESTO time series: %s" % datfn
            except ValueError, e:
                sys.stderr.write("Error encountered when converting on %s" % tim)
                sys.stderr.write(str(e))
        else:
            datfn = tim[-3:]+"dat"
        hdr, hdrlen = sigproc.read_header(tim)
        inffn = write_inf_file(datfn, hdr, hdrlen)
        print "    Wrote info data: %s" % inffn
Beispiel #9
0
    def read_header(self, return_idxs=False):
        """ Read blimpy header and return a Python dictionary of key:value pairs

        Args:
            filename (str): name of file to open

        Optional args:
            return_idxs (bool): Default False. If true, returns the file offset indexes
                                for values

        Returns:
            Python dict of key:value pairs, OR returns file offset indexes for values.

        """
        self.header = sigproc.read_header(self.filename, return_idxs=return_idxs)
        return self.header
Beispiel #10
0
def main():
    for tim in args.timfiles:
        print "Working on %s" % tim
        if args.write_dat:
            try:
                datfn = convert_tim_to_dat(tim)
                print "    Wrote PRESTO time series: %s" % datfn
            except ValueError, e:
                sys.stderr.write("Error encountered when converting on %s" %
                                 tim)
                sys.stderr.write(str(e))
        else:
            datfn = tim[-3:] + "dat"
        hdr, hdrlen = sigproc.read_header(tim)
        inffn = write_inf_file(datfn, hdr, hdrlen)
        print "    Wrote info data: %s" % inffn
    def read_header(self, return_idxs=False):
        """ Read blimpy header and return a Python dictionary of key:value pairs

        Args:
            filename (str): name of file to open

        Optional args:
            return_idxs (bool): Default False. If true, returns the file offset indexes
                                for values

        Returns:
            Python dict of key:value pairs, OR returns file offset indexes for values.

        """
        self.header = sigproc.read_header(self.filename, return_idxs=return_idxs)
        return self.header
Beispiel #12
0
def fb_samp(flist):
    """
    Count the number of samples in a SIGPROC filterbank file.
    
    Currently only handles one file, but would not be too hard
    to update for multifile.
    """
    fillist = flist

    if len(fillist) > 1:
        print "Currently can handle only one .fil file"
        sys.exit(0)

    fil_filenm = fillist[0]
    filhdr, hdrlen = sigproc.read_header(fil_filenm)
    n_samp = sigproc.samples_per_file(fil_filenm, filhdr, hdrlen)

    return n_samp
Beispiel #13
0
def submit_jobs(filename):
  """
  To Submit a Job : 

  jobs_list(job_id)
  jobs_wlist(job_id)
  jobs[job_id]=Job()
  jobs[job_id].box_id
  """

  nb_jobs=0
  try:
    print "Try to open %s"%filename
    file = open(filename, "r")
    for job in file.readlines():

        job=job.replace("\n","")

        jobs_list.append(job)
        jobs_wlist.append(job)

	jobs[job]=Job()

	# Determine size 
	fil_filenm = "/data5/%s"%job.replace(".fbk","_p00.fbk")
	filhdr, hdrlen = sigproc.read_header(fil_filenm)
	orig_N = sigproc.samples_per_file(fil_filenm, filhdr, hdrlen)

	if orig_N > 4194304:
	  logfile.write('Job %s detected L\n'%job);logfile.flush()
	  jobs[job].long=1  
	###################  

        c=filename.index("/")
	jobs[job].box=filename[c+1:]

	nb_jobs += 1
    file.close()

    write_jobs_queue()	
    return nb_jobs 	
  except: return 0
Beispiel #14
0
def submit_a_job(job,box):
    """
    To resubmit a job after deletion by 'remove_a_job'
    """

    jobs_list.append(job)
    jobs_wlist.append(job)
    jobs[job]=Job()
    jobs[job].box=box

    # Determine size 
    fil_filenm = "/data5/%s"%job.replace(".fbk","_p00.fbk")
    filhdr, hdrlen = sigproc.read_header(fil_filenm)
    orig_N = sigproc.samples_per_file(fil_filenm, filhdr, hdrlen)

    if orig_N > 4194304:
      logfile.write('Job %s detected L'%job);logfile.flush()
      jobs[job].long=1  
    else:
      jobs[job].long=0
    ###################  

    write_jobs_queue()
Beispiel #15
0
                print "skip           = ", skip
                print "1st_file_samps = ", first_file_samples
                print "numfiles       = ", numfiles
            break
        else:
            accum_samples += samples_per_file[ii]

    # Now make a command line option for spigot2filterbank
    tmpfilenm = "tmp%d.fil" % random.randint(0, 2**30)
    cmd = "spigot2filterbank -skip %d -numout %d -o %s " % \
          (skip, raw_N, tmpfilenm)
    for goodfile in infilenms[ii:ii + numfiles]:
        cmd += "%s " % goodfile
    os.system(cmd)

    # Now read the header to determine what the correct filename
    # should be.  Use that to rename the fil file.

    filhdr, hdrlen = sigproc.read_header(tmpfilenm)
    MJDi = int(filhdr['tstart'])
    ra_rad = sigproc.ra2radians(filhdr['src_raj'])
    ra_string = pu.coord_to_string(*pu.rad_to_hms(ra_rad))
    dec_rad = sigproc.dec2radians(filhdr['src_dej'])
    dec_string = pu.coord_to_string(*pu.rad_to_dms(dec_rad))
    str_coords = "".join(ra_string.split(":")[:2])
    if dec_rad >= 0.0: str_coords += "+"
    str_coords += "".join(dec_string.split(":")[:2])
    filfilenm = "GBT350drift_%d_%s.fil" % (MJDi, str_coords)
    os.rename(tmpfilenm, filfilenm)
    print "Renamed '%s' to '%s'." % (tmpfilenm, filfilenm)
Beispiel #16
0
		pngname = datfile.split(".dat")[0] + "." + opts.imageext
	else:
		import matplotlib

	import matplotlib.pyplot as plt
	import matplotlib.ticker as ticker
	import matplotlib.cm as cm
	import matplotlib.collections as collections
	import matplotlib.patches as patches
	import matplotlib.font_manager as fm

	headersize = 0  # size in bytes of the header
	# if input file is Sigproc-style tim-file
	if opts.is_timfile:
		try:
    			filhdr, headersize = sigproc.read_header(datfile)
    			startmjd = filhdr['tstart']
    			tsamp = filhdr['tsamp']
                	source = filhdr['source_name']
		except:
			print("Error: Can't open the tim-file '%s'!" % (datfile,))
			sys.exit(1)

	elif opts.is_events:
		opts.is_no_detrend = True  # we don't do detrending for events
		if opts.mjd == '' and not opts.is_chandra:
			print("Error: for events' file start MJD _must_ be given with --mjd option or --chandra option!")
			sys.exit(1)
		if opts.nbins == -1:
			print("Error: number of bins _must_ be given with --nbins option!")
			sys.exit(1)
Beispiel #17
0
# Check the name of the pulsar and fils

# Grab name of .fils

fils = glob.glob('*.fil')

# Count how many fils are in the folder
nfils = len(fils)

#Warning if there are more than one fil
if nfils > 1:
    print ('WARNING: more than one fil in this folder. I will fold them all.')
    
# Grab name of pulsar from the .fil with sigproc function read_header (dictionary)

fil_dic= sigproc.read_header(fils[0])[0]
pulsarname = fil_dic['source_name'][:-3]
date= fil_dic['rawdatafile'][-19:-4]

# Grab configuration file with same name than the pulsar

configdest = '/opt/pulsar/puma/config/'
configfile = SafeConfigParser()
configfile.read(configdest+pulsarname+'.ini') 

# If we are not using manual mode, take all parameters in the config file

# RFI parameters
rfitime = configfile.get('rfi','nint')
checkmask = configfile.getboolean('main','rfimask')
checkreuse = configfile.getboolean('rfi','reuse')
            break
        else:
            accum_samples += samples_per_file[ii]
    
    # Now make a command line option for spigot2filterbank
    tmpfilenm = "tmp%d.fil"%random.randint(0,2**30)
    cmd = "spigot2filterbank -skip %d -numout %d -o %s " % \
          (skip, raw_N, tmpfilenm)
    for goodfile in infilenms[ii:ii+numfiles]:
        cmd += "%s "%goodfile
    os.system(cmd)

    # Now read the header to determine what the correct filename
    # should be.  Use that to rename the fil file.

    filhdr, hdrlen = sigproc.read_header(tmpfilenm)
    MJDi = int(filhdr['tstart'])
    ra_rad = sigproc.ra2radians(filhdr['src_raj'])
    ra_string = pu.coord_to_string(*pu.rad_to_hms(ra_rad))
    dec_rad = sigproc.dec2radians(filhdr['src_dej'])
    dec_string = pu.coord_to_string(*pu.rad_to_dms(dec_rad))
    str_coords = "".join(ra_string.split(":")[:2])
    if dec_rad >= 0.0:  str_coords += "+"
    str_coords += "".join(dec_string.split(":")[:2])
    filfilenm = "GBT350drift_%d_%s.fil" % (MJDi, str_coords)
    os.rename(tmpfilenm, filfilenm)
    print("Renamed '%s' to '%s'." % (tmpfilenm, filfilenm))
    

    
Beispiel #19
0
 def __init__(self, fil_filenm):
     self.fil_filenm = fil_filenm
     self.basefilenm = fil_filenm.rstrip(".fil")
     self.beam = int(self.basefilenm[-1])
     filhdr, self.hdrlen = sigproc.read_header(fil_filenm)
     self.orig_filenm = filhdr['rawdatafile']
     self.MJD = filhdr['tstart']
     self.nchans = filhdr['nchans']
     self.ra_rad = sigproc.ra2radians(filhdr['src_raj'])
     self.ra_string = psr_utils.coord_to_string(\
         *psr_utils.rad_to_hms(self.ra_rad))
     self.dec_rad = sigproc.dec2radians(filhdr['src_dej'])
     self.dec_string = psr_utils.coord_to_string(\
         *psr_utils.rad_to_dms(self.dec_rad))
     self.az = filhdr['az_start']
     self.el = 90.0 - filhdr['za_start']
     self.BW = abs(filhdr['foff']) * filhdr['nchans']
     self.dt = filhdr['tsamp']
     self.orig_N = sigproc.samples_per_file(fil_filenm, filhdr, self.hdrlen)
     self.orig_T = self.orig_N * self.dt
     self.N = psr_utils.choose_N(self.orig_N)
     self.T = self.N * self.dt
     # Update the RA and DEC from the database file if required
     newposn = read_db_posn(self.orig_filenm, self.beam)
     if newposn is not None:
         self.ra_string, self.dec_string = newposn
         # ... and use them to update the filterbank file
         fix_fil_posn(fil_filenm, self.hdrlen, self.ra_string,
                      self.dec_string)
     # Determine the average barycentric velocity of the observation
     self.baryv = presto.get_baryv(self.ra_string,
                                   self.dec_string,
                                   self.MJD,
                                   self.T,
                                   obs="AO")
     # Where to dump all the results
     # Directory structure is under the base_output_directory
     # according to base/MJD/filenmbase/beam
     self.outputdir = os.path.join(base_output_directory,
                                   str(int(self.MJD)), self.basefilenm[:-2],
                                   str(self.beam))
     # Figure out which host we are processing on
     self.hostname = socket.gethostname()
     # The fraction of the data recommended to be masked by rfifind
     self.masked_fraction = 0.0
     # Initialize our timers
     self.rfifind_time = 0.0
     self.downsample_time = 0.0
     self.subbanding_time = 0.0
     self.dedispersing_time = 0.0
     self.FFT_time = 0.0
     self.lo_accelsearch_time = 0.0
     self.hi_accelsearch_time = 0.0
     self.singlepulse_time = 0.0
     self.sifting_time = 0.0
     self.folding_time = 0.0
     self.total_time = 0.0
     # Inialize some candidate counters
     self.num_sifted_cands = 0
     self.num_folded_cands = 0
     self.num_single_cands = 0
Beispiel #20
0
parser.add_argument('-cl', type=int, dest='cl', help="channel low", default = 0);
parser.add_argument("-cu", type=int, dest='cu', help="channel high", default = 1023);
parser.add_argument('-il', type=int, dest='il', help="integration low", default = 0);
parser.add_argument("-iu", type=int, dest='iu', help="integration high", default = 4095);
parser.add_argument('-bl', type=int, dest='bl', help="beam low", default = 0);
parser.add_argument("-bu", type=int, dest='bu', help="beam high", default = 63);
args = parser.parse_args();

cl = args.cl;
cu = args.cu;
bl = args.bl;
bu = args.bu;
il = args.il;
iu = args.iu;

header = sigproc.read_header(args.infile);
nBeams = header['nbeams'];
nChans = header['nchans'];
nInts = header['nsamples'];

print("\nfile contains:");
print(str(nBeams) + " beams");
print(str(nChans) + " channels");
print(str(nInts) + " time samples\n");

if cl > cu:
    cl , cu = cu , cl;
if bl > bu:
    bl , bu = bu , bl;
if il > iu:
    il , iu = iu , il;
Beispiel #21
0
def ordenMJD(filename, opcion, base, orden):

    #filename = nombre del .fil
    #opcion = 1 es potencia, 0 es espectro
    #base = 1 es quitar linea de base y 0 es no quitarla
    #orden para ordenar por: RA = ascención recta, DEC = por declinación, MJD = por tiempo.

    import numpy as np
    import os
    from astropy.time import Time
    import struct
    import sigproc
    from BaselineRemoval import BaselineRemoval
    from peakutils import baseline

    header = sigproc.read_header(filename)  # leemos el header del .fil
    f = open(filename, "rb")
    nchans = int(header[0].get("nchans"))  # numero de canales
    f.seek(
        header[1]
    )  # le dice a la estructura f que ubique al puntero en una cierta posicion

    # abrimos el archivo con los valores de tiempo ltf.
    time_name = filename[0:len(filename) -
                         4] + '.ltf'  # nombre del archivo .ltf
    time_file = open(time_name, "r")  # abre el archivo .ltf

    # abrimos el archivo con las posiciones de apuntamiento
    pos_name = filename[0:len(filename) - 4] + '.txt'

    MJD_pos = np.loadtxt(pos_name,
                         usecols=0)  # guardamos los valores de MJD de .txt
    RA_pos = np.loadtxt(pos_name,
                        usecols=5)  # guardamos los valores de RA de .txt
    DEC_pos = np.loadtxt(pos_name,
                         usecols=7)  # guardamos los valores de DEC de .txt
    t_i = MJD_pos[0]  # primer valor de tiempo del .txt
    t_f = MJD_pos[-1:][0]  # último valor de tiempo del .txt

    b = os.path.getsize(filename)
    size = int(header[0].get("nbits"))  # del header saca el tamaño en bits
    pack = size / 8
    n_espectros = (
        ((b - header[1]) / pack) /
        nchans) - 1  # Calculo del total de espectos disponibles en el .fil
    tiempos = np.empty(
        n_espectros)  # Array que va a guardar los tiempos de .lrf

    n_real = 0  # Número de espectros correspondiente a una posición bien apuntada
    for i in range(n_espectros):  # recorremos todos los espectros del .fil
        t_aux = str(time_file.readline())[
            0:26]  # sacamos el valor de tiempo del .ltf (dd/mm/yyyy)
        tiempos[i] = Time(t_aux, format='isot').mjd  # y lo pasamos a MJD

        if tiempos[i] >= t_i and tiempos[
                i] <= t_f:  # si el tiempo del .fil está dentro del intervalo en que la antena estaba apuntada
            n_real += 1  # aumentamos en 1 el número de espectros bien apuntados

    espectros = np.empty(
        [n_real, nchans + 3]
    )  # archivo de salida. Las columnas son: MJD, RA, DEC, y luego vienen los canales
    potencias = np.empty([n_real, 4])  # archivo de salida si piden potencia
    temp = np.empty(nchans)  # archivo temporal para guardar cada espectro

    j = 0  # para recorrer las filas de espectros y potencias

    for i in range(
            n_espectros):  # para cada instante de tiempo en la observación

        for k in range(nchans):
            temp[k] = struct.unpack(
                'f', f.read(pack)
            )[0]  # leemos la medición en cada canal de frecuencia para ese instante

        if t_i <= tiempos[i] <= t_f:

            if t_pos < tiempos[
                    i]:  # si el tiempo del .fil está dentro del intervalo en que la antena estaba apuntada
                m = find_nearest(
                    MJD_pos, tiempos[i]
                )  # buscamos el tiempo del .txt más cercano a ese tiempo
                t_pos = MJD_pos[m]

            espectros[j, 0] = tiempos[
                i]  # en la primera columna guardamos el t del .fil
            espectros[j, 1] = RA_pos[
                m]  # en la segunda columna guardamos el RA del .txt
            espectros[j, 2] = DEC_pos[
                m]  # en la tercera columna guardamos el DEC del .txt

            espectros[
                j, 3:] = temp  # guardamos las mediciones en todos los canales

            if opcion == 1:  # si piden calcular potencia
                potencias[j, 0] = tiempos[
                    i]  # en la primera columna guardamos el t del .fil
                potencias[j, 1] = RA_pos[
                    m]  # en la segunda columna guardamos el RA del .txt
                potencias[j, 2] = DEC_pos[
                    m]  # en la tercera columna guardamos el DEC del .txt
                potencias[j,
                          3] = np.mean(espectros[j,
                                                 3:])  # calculamos la potencia

            j += 1

    if base == 1:
        potencias[:,
                  3] -= baseline(potencias[:, 3],
                                 1)  # calculamos la base y luego se la sacamos

    if orden == "RA":
        potencias = potencias[np.argsort(potencias[:, 1])]

    elif orden == "DEC":
        potencias = potencias[np.argsort(potencias[:, 2])]

    elif orden == "MJD":
        pass

    if opcion == 0:
        np.save(filename[0:len(filename) - 4] + "_espectro.npy", espectros)
        return espectros
    elif opcion == 1:
        np.save(filename[0:len(filename) - 4] + "_potencia.npy", potencias)
        return potencias
Beispiel #22
0
from scipy import stats
import numpy.matlib as npm
import sys

dSNR = 20
# fake signal strength
threshold = 5
filtsize = 21
# seems to work, can be increased if needed

nBeam = 0
nBlock = 0
#infile = '/home/user/vikram/scratch/bsfil_B0531_1.fil';
infile = '/home/user/vikram/scratch/bsfil_TEST_2.fil'

header = sigproc.read_header(infile)
nBeams = 64
#header['nbeams'];
nChans = 1024
#header['nchans'];
nInts = 4096
#header['nsamples'];

nBlockSize = nBeams * nChans * nInts
FilSize = Path(infile).stat().st_size
nTotBlocks = round(FilSize / nBlockSize)

fh = open(infile, 'rb')
while True:
    keyword, value, idx = sigproc.read_next_header_keyword(fh)
    if keyword == 'HEADER_END':
#://github.com/UCBerkeleySETI/blimpy/blob/master/blimpy/io/fil_reader.py
# plots filterbank file and extracts candidates

import matplotlib.pyplot as plt
import numpy as np
import scipy.signal
from scipy import stats
from pathlib import Path
import sigproc
import sys


#fname = '/datax2/devfil/beam_data/20201110104110_HAT-P-44C.fil';
fname = sys.argv[1];
fhead = sigproc.read_header(fname);
headlen = sigproc.len_header(fname);

nSpec = int((Path(fname).stat().st_size - headlen)/fhead['nchans']/4);

spec = np.zeros((fhead['nchans']));
for k in range(nSpec):
    print(str(k+1) + ' / ' + str(nSpec));
    spec += np.fromfile(fname, dtype=np.uint32, count=int(fhead['nchans']), offset=headlen+int(4*k*fhead['nchans']));

plt.figure();
plt.plot(np.linspace(fhead['fch1'],fhead['fch1']+fhead['foff']*fhead['nchans'],fhead['nchans']),10.*np.log10(spec));
plt.grid();
plt.xlabel('frequency [MHz]');
plt.ylabel('power [dB]');
plt.suptitle(fhead['source_name']);
plt.show();
Beispiel #24
0
def get_pulsar_info(path, dotpar_path):

    # stuff to return
    ierr = 0
    Main, Parameters, Rfi = {}, {}, {}

    # check the name of the pulsar and fils
    # - grab name of .fils
    fils = glob.glob(path + '/*.fil')
    fils.sort()
    # - count how many fils are in the folder
    nfils = len(fils)

    # warning if there are more than one fil
    if nfils <= 0:
        print('\n ERROR: no *.fil(s) found in ' + args.folder + '\n')
        ierr = -1
        return Main, Parameters, Rfi, ierr
    elif nfils > 1:
        print(
            '\n WARNING: more than one fil in this folder. I will fold them all.\n'
        )

    # grab name of pulsar from the .fil with sigproc function read_header (dictionary)
    fil_dic = sigproc.read_header(fils[0])[0]
    pulsarname = fil_dic['source_name'][:-3]

    # grab configuration file with same name than the pulsar
    configdest = '/opt/pulsar/puma/config/'
    configfile = SafeConfigParser()
    configfile.read(configdest + pulsarname + '.ini')

    # if we are not using manual mode, take all parameters in the config file,
    # this file contains 3 sections: main, parameters and rfi. Each of them will
    # be stored in different dictionaries, Main, Parameters and Rfi such that
    # this will be returned by get_pulsar_info function

    # Main information
    Main['timing'] = configfile.getboolean('main', 'timing')
    Main['dmsearch'] = configfile.getboolean('main', 'dmsearch')
    Main['rfimask'] = configfile.getboolean('main', 'rfimask')
    Main['gvoutput'] = configfile.getboolean('main', 'gvoutput')
    Main['movephase'] = configfile.getboolean('main', 'movephase')
    Main['name'] = pulsarname
    Main['date'] = fil_dic['rawdatafile'][-19:-4]
    Main['fils'] = fils

    # Parameters information
    Parameters['nbins'] = configfile.get('parameters', 'nbins')
    Parameters['nchan'] = str(fil_dic['nchans'])
    Parameters['phase'] = configfile.get('parameters', 'phase')
    Parameters['npart'] = configfile.get('parameters', 'npart')
    Parameters['pstep'] = configfile.get('parameters', 'pstep')

    # Rfi information
    Rfi['nint'] = configfile.get('rfi', 'nint')
    Rfi['reuse'] = configfile.getboolean('rfi', 'reuse')

    # path to .par file
    dotpar = dotpar_path + '/' + pulsarname + '.par'
    if os.path.isfile(dotpar) is False:
        print('\n ERROR: no .par file found in ' + pardest + '\n')
        ierr = -1
        return Main, Parameters, Rfi, ierr
    else:
        Main['dotpar'] = dotpar

    return Main, Parameters, Rfi, ierr