Commit 0d40fe67 authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema

Make it work without input file

parent 0a2a4fea
......@@ -13,7 +13,7 @@ import re
import argparse
def create_filterbank(infile, outfile=None, date=None, source=None):
def create_filterbank(infile=None, outfile=None, date=None, source=None):
"""
Create a filterbank file from the CAMRAS-format, which is generated
by pulsar_record.
......@@ -27,9 +27,13 @@ def create_filterbank(infile, outfile=None, date=None, source=None):
"""
if not outfile:
if not infile:
raise RuntimeError("Specify at least one of inputfile or outputfile")
outfile = re.split('[+-\.]', infile)[0] + ".fil"
if not source:
if not infile:
raise RuntimeError("If no input file is given, you must specify source")
match = re.match("[BJ][0-9]*[+-][0-9]*", infile)
if match:
source = "PSR " + match.group(0)
......@@ -37,7 +41,10 @@ def create_filterbank(infile, outfile=None, date=None, source=None):
raise ValueError("Could not guess source name from filename")
if not date:
date = Time(os.path.getctime(infile), format='unix')
if infile:
date = Time(os.path.getctime(infile), format='unix')
else:
date = Time.now()
else:
date = Time(date, format='isot')
......@@ -47,13 +54,18 @@ def create_filterbank(infile, outfile=None, date=None, source=None):
print("Observation time: {}".format(date.isot))
date = date.mjd
if infile:
rawdatafile = infile
else:
rawdatafile = ""
# 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["rawdatafile"] = rawdatafile
fil_header["source_name"] = source
fil_header["barycentric"] = 0
fil_header["pulsarcentric"] = 0
......@@ -72,13 +84,16 @@ def create_filterbank(infile, outfile=None, date=None, source=None):
# Write header
out = filterbank.create_filterbank_file(outfile, fil_header, nbits=32)
if not infile:
out.close()
return
# Read file
f = open(infile)
filesize = os.stat(infile).st_size
print("File size:", filesize)
offset = 0
CHUNKSIZE = 10000
CHUNKSIZE = 30000
while offset < filesize:
# Loop through file in chunks of 256*10000 numbers
# i.e. 256*CHUNKSIZE*4 bytes (10Mb) to avoid memory problems
......@@ -91,8 +106,6 @@ def create_filterbank(infile, outfile=None, date=None, source=None):
print("Packets were dropped:", np.count_nonzero(data[:-1,-1] - data[1:,-1] - 1),
np.sum(data[1:,-1] - data[:-1,-1] - 1))
print("Offset: {}".format(offset))
print("Size: {}".format(os.stat(outfile).st_size))
# Write data
out.append_spectra(data)
......@@ -103,11 +116,11 @@ def create_filterbank(infile, outfile=None, date=None, source=None):
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("-i", "--infile", help="Input CAMRAS pulsar file. If empty, create just the header.")
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("-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)
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)
create_filterbank(infile=args.infile, outfile=args.outfile, date=args.date, source=args.source)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment