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

Asked

Viewed 120 times

1

I am reading a complex binary file, where there is a fixed structure of 'array' of values, someone could 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!

  • Welcome to the Stackoverflow in Portuguese. As the name suggests, the official language used here is Portuguese. So, could you please translate your question? If you prefer, you can also ask the same question on Stackoverflow website in English.

  • how do I change the text I’ve already written? was the first message I posted here

  • Click on the button [Edit]

  • done, thanks @Denisrudneidesouza

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))
  • valeu, is just done like this: Names =["a","b","c","d","and"] formats=["<U8", "<U8", "<U8", "<U8", "<U8"] for i in range(0,X): #Xa; Names.append("a"+str(i)) &##Xa; Xa;Xa; Names.("b"+str(i)) Names.append("c"+str(i)) formats.append("<U1") formats.append("<F8") formats.append("<U8") np.dtype({'Names':formats'formats':}) ' =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.