Skip to content

Commit

Permalink
Issue #241 - update fil_writer.py, hdf_writer.py
Browse files Browse the repository at this point in the history
  • Loading branch information
texadactyl authored Sep 16, 2021
1 parent 75c3d5c commit 246e023
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
24 changes: 17 additions & 7 deletions blimpy/io/fil_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from .sigproc import generate_sigproc_header


def write_to_fil(wf, filename_out, *args, **kwargs):
def write_to_fil(wf, filename_out):
""" Write data to .fil file.
It check the file size then decides how to write the file.
It checks the file size then decides how to write the file.
Args:
filename_out (str): Name of output file
wf : Waterfall object
filename_out : str
Name of output file
"""

# For timing how long it takes to write a file.
Expand All @@ -33,7 +35,9 @@ def __write_to_fil_heavy(wf, filename_out):
""" Write data to .fil file.
Args:
filename_out (str): Name of output file
wf : Waterfall object
filename_out : str
Name of output file
"""

# Note that a chunk is not a blob!!
Expand All @@ -44,7 +48,9 @@ def __write_to_fil_heavy(wf, filename_out):
# Calculate number of bytes per data element
n_bytes = wf.header['nbits'] / 8

wf.logger.info('Using %i n_blobs to write the data.' % n_blobs)
wf.logger.info("__write_to_fil_heavy: For {}, chunk_dim={}, blob_dim={}, n_blobs={}"
.format(filename_out, chunk_dim, blob_dim, n_blobs))

with open(filename_out, "wb") as fileh:

# Write header of .fil file
Expand All @@ -53,7 +59,7 @@ def __write_to_fil_heavy(wf, filename_out):
# For each blob
for ii in range(0, n_blobs):

wf.logger.info('Reading %i of %i' % (ii + 1, n_blobs))
wf.logger.info('__write_to_fil_heavy: Processing %i of %i' % (ii + 1, n_blobs))
bob = wf.container.read_blob(blob_dim, n_blob=ii)

# Write data of .fil file.
Expand All @@ -69,9 +75,13 @@ def __write_to_fil_light(wf, filename_out):
""" Write data to .fil file.
Args:
filename_out (str): Name of output file
wf : Waterfall object
filename_out : str
Name of output file
"""

wf.logger.info("__write_to_fil_light: Writing the spectra matrix for {} in one go."
.format(filename_out))
n_bytes = wf.header['nbits'] / 8
with open(filename_out, "wb") as fileh:
fileh.write(generate_sigproc_header(wf))
Expand Down
16 changes: 9 additions & 7 deletions blimpy/io/hdf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def __write_to_hdf5_heavy(wf, filename_out, f_scrunch=None, *args, **kwargs):
chunk_dim = tuple(chunk_list)
blob_dim = wf._get_blob_dimensions(chunk_dim)
n_blobs = wf.container.calc_n_blobs(blob_dim)
wf.logger.info("__write_to_hdf5_heavy: For {}, chunk_dim={}, blob_dim={}, n_blobs={}"
.format(filename_out, chunk_dim, blob_dim, n_blobs))

# ===============================
# Attempt to write the HDF5 file.
Expand Down Expand Up @@ -103,9 +105,8 @@ def __write_to_hdf5_heavy(wf, filename_out, f_scrunch=None, *args, **kwargs):

if blob_dim[wf.freq_axis] < wf.selection_shape[wf.freq_axis]:

wf.logger.info('Using %i n_blobs to write the data.'% n_blobs)
for ii in range(0, n_blobs):
wf.logger.info('Reading %i of %i' % (ii + 1, n_blobs))
wf.logger.info('__write_to_hdf5_heavy: Processing blob %i of %i' % (ii + 1, n_blobs))

bob = wf.container.read_blob(blob_dim, n_blob=ii)

Expand All @@ -129,9 +130,8 @@ def __write_to_hdf5_heavy(wf, filename_out, f_scrunch=None, *args, **kwargs):

else:

wf.logger.info('Using %i n_blobs to write the data.'% n_blobs)
for ii in range(0, n_blobs):
wf.logger.info('Reading %i of %i' % (ii + 1, n_blobs))
wf.logger.info('__write_to_hdf5_heavy: Processing blob %i of %i' % (ii + 1, n_blobs))

bob = wf.container.read_blob(blob_dim, n_blob=ii)
t_start = wf.container.t_start + ii * blob_dim[wf.time_axis]
Expand All @@ -152,18 +152,18 @@ def __write_to_hdf5_heavy(wf, filename_out, f_scrunch=None, *args, **kwargs):
# =================================

except Exception as ex1: # Something went wrong!
wf.logger.error("blimpy __write_to_hdf5_heavy: Writing the output HDF5 file {} failed!"
wf.logger.error("__write_to_hdf5_heavy: Writing the output HDF5 file {} failed!"
.format(filename_out))
print(repr(ex1))
try:
if os.path.exists(filename_out):
os.remove(filename_out) # scrap a potentially corrupted HDF5 file.
# Removal succeeded. Exit to the O/S with a nonzero exit code.
wf.logger.info("blimpy __write_to_hdf5_heavy: Removal of partial HDF5 file {} succeeded."
wf.logger.info("__write_to_hdf5_heavy: Removal of partial HDF5 file {} succeeded."
.format(filename_out))
sys.exit(86)
except Exception as ex2:
wf.logger.error("blimpy __write_to_hdf5_heavy: Removal of partial HDF5 file {} failed!"
wf.logger.error("__write_to_hdf5_heavy: Removal of partial HDF5 file {} failed!"
.format(filename_out))
print(repr(ex2))
sys.exit(86)
Expand All @@ -177,6 +177,8 @@ def __write_to_hdf5_light(wf, filename_out, f_scrunch=None, *args, **kwargs):
f_scrunch (int or None): Average (scrunch) N channels together
"""

wf.logger.info("__write_to_hdf5_light: Writing the spectra matrix for {} in one go."
.format(filename_out))
try:
os.remove(filename_out) # Try to pre-remove output .h5 file.
except:
Expand Down

0 comments on commit 246e023

Please sign in to comment.