tofil.py 5.23 KB
Newer Older
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
1
2
3
4
5
6
7
#!/usr/bin/env python2
from __future__ import print_function

import numpy as np
import filterbank
import sigproc
from astropy.time import Time
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
8
from astropy import units as u
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
9
10

import os.path
11
import os
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
12
13
14
15
16
import sys
import re

import argparse

17
18
19
20
21
22
23
24
telescope_found = False
try:
    from telescope import telescope
    dt = telescope(consoleHost='console')
    telescope_found = True
except:
    print("Could not connect to console, no RA/DEC recorded")

25
def create_filterbank(infile=None, outfile=None, date=None, source=None):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
26
27
28
29
30
31
32
33
34
35
36
37
38
    """
    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:
39
40
        if not infile:
            raise RuntimeError("Specify at least one of inputfile or outputfile")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
41
42
43
        outfile = re.split('[+-\.]', infile)[0] + ".fil"

    if not source:
44
45
        if not infile:
            raise RuntimeError("If no input file is given, you must specify source")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
46
47
48
49
50
51
52
        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:
53
54
55
56
        if infile:
            date = Time(os.path.getctime(infile), format='unix')
        else:
            date = Time.now()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
57
58
59
    else:
        date = Time(date, format='isot')

60
61
62
63
64
65
66
67
68
69
    az_start = 0.
    za_start = 0.
    src_raj = 0.
    src_dej = 0.
    if telescope_found:
        az_start = (180*u.deg + dt.getAzEl()[0]).to(u.rad).value
        za_start = (90*u.deg - dt.getAzEl()[1]).to(u.rad).value
        src_raj = dt.getRaDec().ra.to_string(unit=u.hour, sep='')
        src_dej = dt.getRaDec().dec.to_string(unit=u.degree, sep='')

70
71
    print("Input file: {}".format(infile))
    print("Output file: {}".format(outfile))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
72
    print("Source name: {}".format(source))
73
    print("Observation time: {}".format(date.isot))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
74
75

    date = date.mjd
76
77
78
79
80

    if infile:
        rawdatafile = infile
    else:
        rawdatafile = ""
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
81
82
83
84
85
86
 
    # 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
87
    fil_header["rawdatafile"]   = rawdatafile
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
88
89
90
    fil_header["source_name"]   = source
    fil_header["barycentric"]   = 0
    fil_header["pulsarcentric"] = 0
91
92
93
94
    fil_header["az_start"]      = az_start
    fil_header["za_start"]      = za_start
    fil_header["src_raj"]       = src_raj
    fil_header["src_dej"]       = src_dej
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
95
96
    fil_header["tstart"]        = date
    fil_header["tsamp"]         = 0.00046811428571414528 # 1.0/35e6*256 chans*64 decimation
97
    fil_header["nbits"]         = 16
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
98
99
100
101
102
103
    fil_header["fch1"]          = 441.4
    fil_header["foff"]          = -35.0/256.0
    fil_header["nchans"]        = 256
    fil_header["nifs"]          = 1

    # Write header
104
    out = filterbank.create_filterbank_file(outfile, fil_header, nbits=16)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
105

106
107
108
109
    if not infile:
        out.close()
        return

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
110
    # Read file
111
    f = open(infile)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
112

113
114
    filesize = os.stat(infile).st_size
    offset = 0
115
    CHUNKSIZE = 30000
116
    while offset < filesize:
117
        # Loop through file in chunks of 256*10000 numbers
118
        # i.e. 256*CHUNKSIZE*4 bytes (10Mb) to avoid memory problems
119
        f.seek(offset, os.SEEK_SET)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
120

121
        data = np.fliplr(np.fromfile(f, dtype='>u4', count=256*CHUNKSIZE).reshape(-1,256))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
122

123
        # Check that no packets were dropped
124
125
126
        if np.sum(data[1:,-1] - data[:-1,-1] - 1) > 0.1:
            print("Packets were dropped:", np.count_nonzero(data[:-1,-1] - data[1:,-1] - 1),
                                           np.sum(data[1:,-1] - data[:-1,-1] - 1))
127
128

        # Write data
129
        out.append_spectra(data.astype('u2'))
130

131
        offset += 256*CHUNKSIZE*4
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
132
133
134
135
136
137

    # Close file
    out.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Convert a camras pulsar file (output of pulsar_record) to filterbank format")
138
    parser.add_argument("-i", "--infile", help="Input CAMRAS pulsar file. If empty, create just the header.")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
139
    parser.add_argument("-o", "--outfile", help="Name of output file, e.g. 'B0329.fil' (default: guessed from input filename)", default=None)
140
    parser.add_argument("-d", "--date", help="Start date/time of observation, in 'isot' format (defaults to creation date of file, or now if no infile given)", default=None)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
141
142
143
144
    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()

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
145
146
147
148
149
    if args.infile is None and args.outfile is None:
        parser.error("One of --infile or --outfile is required")
    if args.infile is None and args.source is None:
        parser.error("Without infile, --source must be specified")
 
150
    create_filterbank(infile=args.infile, outfile=args.outfile, date=args.date, source=args.source)