tofil.py 3.46 KB
Newer Older
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python2
from __future__ import print_function

import numpy as np
import filterbank
import sigproc
from astropy.time import Time

import os.path
import sys
import re

import argparse

def create_filterbank(infile, outfile=None, date=None, source=None):
    """
    Create a filterbank file from the CAMRAS-format, which is generated
    by pulsar_record.

    infile:   name of the file generated by pulsar_record
    outfile: name of the filterbank file to be created; if not given, a name
              will be created from the infile
    date:     the date to be used; default is the file creation date of infile
              if given, the date must be in a format the astropy.time understands
    source:   name of the source to be used; default is guessed from filename
    """

    if not outfile:
        outfile = re.split('[+-\.]', infile)[0] + ".fil"

    if not source:
        match = re.match("[BJ][0-9]*[+-][0-9]*", infile)
        if match:
            source = "PSR " + match.group(0)
        else:
            raise ValueError("Could not guess source name from filename")

    if not date:
        date = Time(os.path.getctime(infile), format='unix')
    else:
        date = Time(date, format='isot')

    print("Creating filterbank file {} from {}".format(outfile, infile))
    print("Source name: {}".format(source))
    print("Observation time: {}".format(date))

    date = date.mjd
 
    # Generate filterbank header
    fil_header = {}
    fil_header["telescope_id"]  = sigproc.telescope_ids["Effelsberg"]
    fil_header["machine_id"]    = sigproc.machine_ids["FAKE"]
    fil_header["data_type"]     = 1
    fil_header["rawdatafile"]   = infile
    fil_header["source_name"]   = source
    fil_header["barycentric"]   = 0
    fil_header["pulsarcentric"] = 0
    fil_header["az_start"]      = 0.0
    fil_header["za_start"]      = 0.0
    fil_header["src_raj"]       = 0.0
    fil_header["src_dej"]       = 0.0
    fil_header["tstart"]        = date
    fil_header["tsamp"]         = 0.00046811428571414528 # 1.0/35e6*256 chans*64 decimation
    fil_header["nbits"]         = 32
    fil_header["fch1"]          = 441.4
    fil_header["foff"]          = -35.0/256.0
    fil_header["nchans"]        = 256
    fil_header["nifs"]          = 1

    # Write header
    out = filterbank.create_filterbank_file(outfile, fil_header, nbits=32)

    # Read file
    data = np.flipud(np.fromfile(infile, dtype='>u4'))
    print(data.shape)

    data = np.flipud(data.reshape(-1, 256))

    # Check that no packets were dropped
    if np.sum(data[:-1,-1]-data[1:,-1]-1) > 0.1:
        print("Packets were dropped:", np.count_nonzero(data[:-1,0]-data[1:,0]-1))

    # Write data
    out.append_spectra(data)

    # Close file
    out.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Convert a camras pulsar file (output of pulsar_record) to filterbank format")
    parser.add_argument("infile", help="Input CAMRAS pulsar file")
    parser.add_argument("-o", "--outfile", help="Name of output file, e.g. 'B0329.fil' (default: guessed from input filename)", default=None)
    parser.add_argument("-d", "--date", help="Start date/time of observation, in 'isot' format (defaults to creation date of file)", default=None)
    parser.add_argument("-s", "--source", help="Name of the source, e.g. 'PSR B0329+54' (default: guessed from input filename)", default=None)

    args = parser.parse_args()

    create_filterbank(args.infile, outfile=args.outfile, date=args.date, source=args.source)