Numpy dType complex, with fixed array of variables, using function fromfile to import binary file

0

I'm reading a complex binary file, where there is a fixed array of values, could anyone help?

The structure is:

   8 bytes     = Long    Current Time (unixtime stamp)
   8 bytes     = Long    Current Time (unixtime stamp in MiliSeconds)
   8 bytes     = Long    Server Time (unixtime stamp)
   8 bytes     = Long    Ticks from windows startup
   8 bytes     = Long    Microseconds from windows startup
   X[
      1 byte   = UChar (sensor on/off)
      8 bytes  = Double (sensor converted value)
      8 bytes  = Long (sensor raw value)
      ]

X is the array with 3 values, the size of X is fixed normally 32 or 16, but inside the file is always the same size, the example I will use is 32:

   dt = np.dtype([('a'         ,np.datetime64),
           ('b'      ,np.datetime64),
           ('c'           ,np.datetime64),
           ('d'        ,'u8'),
           ('e' ,'u8'),

           #aqui esta o meu problema, como faço este trecho se repetir 32 vezes?
           {'name':   ['f', 'g', 'h'] , 'formats':['u1','f8', 'u8']}
   ])

   print(dt,type(dt))
   print(dt.fields)
   print(np.fromfile("sensores.32.bin", dtype=dt))

Thank you!

    
asked by anonymous 20.12.2017 / 16:49

1 answer

0

Hi, if I understand the question I think there is a solution like this

def make_dict(uri):

    my_fields = [
        'X{}-campo1'.format(uri), 
        'X{}-campo2'.format(uri),
        'X{}-campo3'.format(uri)
    ]

    my_formats = ['u1','f8', 'u8']

    return dict(name=my_fields, formats=my_formats)

my_arg  = [('campo1', np.datetime64),
           ('campo2', np.datetime64),
           ('campo3', np.datetime64),
           ('campo4', 'u8'),
           ('campo5', 'u8')]

for i in range(32):
    my_arg.append(make_dict(i+1))

dt = np.dtype(my_arg)

print(dt,type(dt))
print(dt.fields)
print(np.fromfile("sensores.32.bin", dtype=dt))
    
21.12.2017 / 12:20