Python Forum
Parse Binary Data File and convert Epoch Time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse Binary Data File and convert Epoch Time
#1
Hello All!

I am trying to extract and convert data from a binary data file. Each data entry follows this order:
  • sample time in seconds since unix epoch, 4 bytes
  • 2 byte integer
  • 2 byte integer
  • 2 byte integer
  • 2 byte integer

All values are integers

Below is the code I'm using based off a different binary data extraction project. It exports the correct number of data entries but the values do not make sense. I know I'm not parsing the binary file correctly but I;m not sure how to do the date conversion. Thanks!

import math, struct, time

field_names = ('time1', 'vol_conc', 'part_size', \
          'opt_trans', 'laser_ref')

def decode(data, offset=0):
    '''Decipher a lisst datagram from binary'''

    # Offset now tells it how far to start
    with open('li0002a', 'rb') as data:
        values = struct.unpack('i4h', data.read(12))

    # Create a dictionary for all the values
    lisst_values = dict(zip (field_names, values))

    return lisst_values

def lisst_print(lisst_values):
    'Print out all the values of a lisst dictionary'
    print 'results:'
    for key in lisst_values:
        print '    ', key, lisst_values[key]

datagram_size = 12 # 2*6 bytes per datagram

def num_datagrams(data):
    #How many packets are in data'

    # Make sure we have an even number of datagrams
    #assert (len(data) % datagram_size == 0)

    return len(data) / datagram_size

def get_offset(datagram_number):
    'Calculate the starting offset of a datagram'
    return datagram_number * datagram_size

def main():
    lisst_file = open('li0002a')
    lisst_data = lisst_file.read()

    print 'Number of datagrams:', num_datagrams(lisst_data)

    print 'Datagram Number, Time, Volume Concentration, Particle Size,\
 Optical Transmission, Laser Reference '

    for datagram_index in range( num_datagrams(lisst_data) ):
        offset = get_offset(datagram_index)
        datagram = decode(lisst_data,offset)

        print datagram_index, datagram['time1'], datagram['vol_conc'], datagram['part_size'], \
             datagram['opt_trans'], datagram['laser_ref']

if __name__=='__main__':
    main()
Reply
#2
pure binary as in a block of memory is arranged in machine addressing order, big or little endian, so your 4 byte value.
so you may have to do some transformations, Here's a good doc for that: https://wiki.python.org/moin/BitManipulation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 800 Feb-29-2024, 12:30 AM
Last Post: Winfried
  parse json field from csv file lebossejames 4 725 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 1,151 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  parse/read from file seperated by dots giovanne 5 1,105 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  How do I read and write a binary file in Python? blackears 6 6,506 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Script to convert Json to CSV file chvsnarayana 8 2,496 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  [SOLVED] Epoch timestamp without milliseconds? Winfried 5 2,979 Jan-27-2023, 04:35 PM
Last Post: deanhystad
  openpyxl convert data to float jacklee26 13 5,946 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,324 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,541 Sep-13-2022, 01:31 PM
Last Post: ManuRaval

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020