Skip to content

Commit

Permalink
#8 Add vdif-convert script
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Nov 5, 2020
1 parent ea5688d commit 77d357e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
48 changes: 48 additions & 0 deletions vdif-convert
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python


"""Convert VDIF to NumPy format.
Usage: vdif-convert <vdif> [<npz>] [--chbin <N>]
<vdif> Input VDIF file (*.vdif).
<npz> Output NumPy file (*.npz).
--chbin <N> Number of channels to bin [default: 1].
-h --help Show this screen and exit.
-v --version Show version and exit.
"""


__author__ = "Akio Taniguchi"
__version__ = "0.1.0"


# standard library
from pathlib import Path


# dependencies
import numpy as np
from docopt import docopt
from vdif_reader import get_all_spectra


# main functions
def main() -> None:
args = docopt(__doc__, version=__version__)

vdif = Path(args["<vdif>"]).resolve()

try:
npz = Path(args["<npz>"]).resolve()
except TypeError:
npz = vdif.with_suffix(".npz")

spectra = get_all_spectra(vdif, int(args["--chbin"]))
np.savez(npz, spectra=spectra)


# run command line interface
if __name__ == "__main__":
main()
26 changes: 25 additions & 1 deletion vdif_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
__all__ = ["get_spectrum", "get_cal_spectrum"]
__all__ = [
"get_spectrum",
"get_all_spectra",
"get_cal_spectrum",
]


# standard library
Expand All @@ -12,6 +16,7 @@

# dependent packages
import numpy as np
from tqdm import tqdm


# constants
Expand Down Expand Up @@ -53,6 +58,25 @@ def get_cal_spectrum(
return get_spectrum(path, cal, delay, chbin)


def get_all_spectra(path: Path, chbin: int = 1) -> np.ndarray:
n_units = path.stat().st_size // N_BYTES_PER_UNIT
n_scans = n_units // N_UNITS_PER_SCAN
n_chans = N_ROWS_CORR_DATA // 2

spectra = np.empty([n_units, n_chans], dtype=np.complex64)

with open(path, "rb") as f:
for i in tqdm(range(n_units)):
read_vdif_head(f)
read_corr_head(f)
corr_data = read_corr_data(f)
spectra[i] = parse_corr_data(corr_data)

n_chans = n_chans * N_UNITS_PER_SCAN
spectra = spectra.reshape([n_scans, n_chans])
return spectra.reshape([n_scans, n_chans // chbin, chbin]).mean(2)


# sub features
def get_spectra(path: Path, integ: float = 1.0, delay: float = 0.0) -> np.ndarray:
n_scans = int(get_elapsed_time_from_start(path, delay) / TIME_PER_SCAN)
Expand Down

0 comments on commit 77d357e

Please sign in to comment.