def main():
    args = parse_args()
    with open(args.dest_file_path, 'wb') as dest_file_obj:
        data_gen = slice_file(
            open_raw_data(args.source_file_path),
            slice(args.slice_start, args.slice_stop),
            words_to_read=args.words_to_read,
            buffer_size=args.buffer_size,
        )
        for data in data_gen:
            dest_file_obj.write(data)
Esempio n. 2
0
def copy_file_part(src_path, percent_start=0, percent_stop=100):
    '''
    Copies percentage of the source path to a new destination file. If source
    is compressed, output is read out into a decompressed file.

    src_path can be either a zip (.SAC), bz2 or uncompressed data file

    NOTE: Reads data into memory
    TODO: Move to flightdatautilities.filesystem_tools ?
    '''

    from flightdatautilities.filesystem_tools import open_raw_data
    ext = '_%d-%d.dat' % (percent_start, percent_stop)
    dest_path = os.path.splitext(src_path)[0] + ext
    if os.path.isfile(dest_path) and os.path.getsize(dest_path):
        print 'Partial file already exists; using: %s' % dest_path
        return dest_path
    try:
        src = open_raw_data(src_path)
        src.seek(0, 2)
        size = src.tell()
        src.seek(0)
        offset = int(percent_start * size / 100.0)
        if offset % 2:
            offset += 1  # make sure the start is even
        read_end = int(percent_stop * size / 100.0)
        amount = read_end - offset
        if amount % 2:
            amount -= 1  # make multiple of np.short (2 bytes)
        src.seek(offset)
        data = src.read(amount)
    finally:
        src.close()
    with open(dest_path, 'wb') as dest:
        dest.write(data)
    return dest_path