12-07-2012 06:03 PM
I would like to essentially convert the following python snippet to Labview. It reads in a binary format and puts it into a mix of integer and floating point values.
I've spent half a day on this and keep getting out nonsense.
thanks
Eric
import sys
import struct
fn=sys.argv[1]
f=open(fn,'r')
ll=f.readlines()
f.close()
l=""
for l1 in ll:
l+=l1
ll=""
i=0
psize = 21
print 'Converted from binary file', fn
print 'file size/particle size: ',len(l)/psize
print
print '{0:7}{1:7}{2:14}{3:9}{4:3}{5:9}{6:10}'.format("Col1","Col2","Col3","Col4","Col5","Col6","Col7")
print '--------------------------------------------------------'
while (i+1)*psize <=len(l):
try:
ty=struct.unpack('b',l[i*psize+0:i*psize+1])[0]
en=struct.unpack('f',l[i*psize+1:i*psize+5])[0]
x=struct.unpack('f',l[i*psize+5:i*psize+9])[0]
y=struct.unpack('f',l[i*psize+9:i*psize+13])[0]
px=struct.unpack('f',l[i*psize+13:i*psize+17])[0]
py=struct.unpack('f',l[i*psize+17:i*psize+21])[0]
i+=1
if (1):
print '{0:5g} {1:5g} {2:8.4f} {3:8.4f} {4:8.4f} {5:8.4f} {6:8.4f}'.format(i,ty,en,x,y,px,py)
except:
print "Error reading particle", i
break
12-07-2012 06:16 PM
Sorry, I am not really familiar with Python, but if you could attach a small VI that contains a suitable typpical input string as well as the desired result, I could probably do it in a few minutes. 😄
12-07-2012 06:51 PM - edited 12-07-2012 06:51 PM
12-07-2012 11:37 PM
Thanks for the suggestion. I tried it, and I think it's close, but the floats I'm getting out don't match. So per your request, I've attached the input and expected output along with the relevant header info.
I should note, that with your code, I wasn't sure how to set the data types, so I set the blue one as I32, and the rest as SIG.
Anyway, thanks again for your time...this one has really stumped me.
Eric
(note, to be compatible with the forum, I had to add an acceptable extension for the binary, so I chose .tar)
12-08-2012 12:51 AM - edited 12-08-2012 12:55 AM
The blue one is I8 (I32 won't work) and the others SGL (look at the wire description I have included in the image). Also make sure the cluster order is correct. (I'll look at your attachmetns later...)
(I cannot read your tar file using 7zip. Maybe it is corrupt. Try to zip all your attachments instead. What is your OS?)
12-08-2012 02:22 AM
I will try your suggestions. As far as the .tar is concerned, that file is actually the binary to be read, but NI wouldn't let me upload it with the native extension, so I chose .tar. I tried to explain this in my last post, but perhaps I wasn't as clear as i could be. You can simply delete the .tar,
Eric
12-08-2012 02:52 AM
Ah, sorry. I though you complressed the binary file as tar.
12-08-2012 03:09 AM - edited 12-08-2012 03:11 AM
OK. you can simply read it directly as that structure as follows (pythonParse2.vi).
If you have the string, my above suggestions works of course too (pythonparse.vi)
(VIs are in 8.2.)
12-08-2012 03:24 AM - edited 12-08-2012 03:24 AM
12-08-2012 03:37 AM
Thank you so much...this seems to have worked. I tried everything today, except the cluster of elements...that's so simple, yet somehow non-obvious. Again, thanks for helping me out with this.
Eric