Skip to content

Commit

Permalink
Merge pull request #273 from texadactyl/master
Browse files Browse the repository at this point in the history
Support output to either .fil or .h5 files in dsamp
  • Loading branch information
texadactyl authored Aug 2, 2022
2 parents d5ad499 + 521d21b commit c46d849
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions VERSION-HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ This file is a version history of blimpy amendments, beginning with version 2.0.
<br>
| Date | Version | Contents |
| :--: | :--: | :-- |
| 2022-08-02 | 2.1.2 | Write .fil files as well as .h5 files (issue #272). |
| 2022-07-22 | 2.1.2 | More container fields needed (issue #270). |
| 2022-07-21 | 2.1.1 | New Waterfall class option, an alternative to file loading (issue #264). |
| 2022-07-08 | 2.1.0 | New utility: dsamp (issue #267). |
Expand Down
32 changes: 24 additions & 8 deletions blimpy/dsamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# External dependencies:
import sys
import pathlib
import time
from argparse import ArgumentParser
import numpy as np
Expand All @@ -21,6 +22,7 @@
# Blimpy functions required:
from blimpy import Waterfall
from blimpy.io.hdf_writer import __write_to_hdf5_heavy as write_to_h5
from blimpy.io.fil_writer import write_to_fil


def downer(in_np_array, in_tsamp, group_size, out_dtype="float32"):
Expand Down Expand Up @@ -104,11 +106,11 @@ def downer(in_np_array, in_tsamp, group_size, out_dtype="float32"):
return out_np_array, out_tsamp, out_nints


def make_h5_file(in_path, out_path, group_size):
def make_output_file(in_path, out_path, group_size, flag_h5):
"""
1. Load input filterbank .fil or .h5 file.
2. Call downer to perform down-sampling.
3. Save result to the specified .h5 file.
3. Save result to the specified file.
Args:
in_path (str): Name of filterbank file to load
Expand All @@ -118,7 +120,7 @@ def make_h5_file(in_path, out_path, group_size):
"""

# Load input filterbank .fil or .h5 file.
wf = Waterfall(in_path, load_data=True)
wf = Waterfall(in_path, max_load=None)

# Down-sample input.
t0 = time.time()
Expand All @@ -136,7 +138,10 @@ def make_h5_file(in_path, out_path, group_size):
wf.data = out_data
LOGGER.info(f"Output data shape: {wf.data.shape}")
t0 = time.time()
write_to_h5(wf, out_path)
if flag_h5:
write_to_h5(wf, out_path)
else:
write_to_fil(wf, out_path)
LOGGER.info(f"Write-output time: {time.time() - t0 :f}s")
return 0

Expand All @@ -147,7 +152,7 @@ def cmd_tool(args=None):

parser = ArgumentParser(description="Downsample an input Filterbank file (.fil or .h5) to an output .h5 Filterbank file.")
parser.add_argument("in_path", type=str, help="Path of input Filterbank file (.fil or .h5)")
parser.add_argument("out_path", type=str, help="Path of output Filterbank file (.h5 only)")
parser.add_argument("out_path", type=str, help="Path of output Filterbank file (.fil or .h5)")
parser.add_argument("-s", "--group_size", dest="group_size", type=int, required=True,
help="Group size for the purpose of summing 2 or more time samples. Required.")

Expand All @@ -160,9 +165,20 @@ def cmd_tool(args=None):
LOGGER.error(f"Input group size = {args.group_size} but it must be at least 2 !!")
sys.exit(1)

rc = make_h5_file(args.in_path,
args.out_path,
args.group_size)
in_ext = pathlib.Path(args.in_path).suffix
out_ext = pathlib.Path(args.out_path).suffix
if in_ext not in [".fil", ".h5"]:
LOGGER.error("Input file extension must be .fil or .h5 !!")
sys.exit(1)
if out_ext not in [".fil", ".h5"]:
LOGGER.error("Output file extension must be .fil or .h5 !!")
sys.exit(1)
flag_h5 = bool(out_ext == ".h5")

rc = make_output_file(args.in_path,
args.out_path,
args.group_size,
flag_h5)

if rc != 0:
sys.exit(rc)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from setuptools import setup, find_packages

__version__ = '2.1.2'
__version__ = '2.1.3'

with open("README.md", "r") as fh:
long_description = fh.read()
Expand Down
18 changes: 15 additions & 3 deletions tests/test_dsamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
import blimpy as bl
from tests.data import voyager_fil, voyager_h5, test_h5
from tests.data import voyager_fil, voyager_h5, test_h5, test_fil


GROUP_SIZE = 3
Expand All @@ -13,13 +13,25 @@
def test_dsamp_fil_to_h5():
""" fil to h5 test.
"""
bl.dsamp.make_h5_file(voyager_fil, test_h5, GROUP_SIZE)
bl.dsamp.make_output_file(voyager_fil, test_h5, GROUP_SIZE, True)


def test_dsamp_h5_to_h5():
""" h5 to h5 test.
"""
bl.dsamp.make_h5_file(voyager_h5, test_h5, GROUP_SIZE)
bl.dsamp.make_output_file(voyager_h5, test_h5, GROUP_SIZE, True)


def test_dsamp_h5_to_fil():
""" h5 to fil test.
"""
bl.dsamp.make_output_file(voyager_h5, test_fil, GROUP_SIZE, False)


def test_dsamp_fil_to_fil():
""" fil to fil test.
"""
bl.dsamp.make_output_file(voyager_fil, test_fil, GROUP_SIZE, False)


def test_cmd_tool():
Expand Down

0 comments on commit c46d849

Please sign in to comment.