Re: Reading binary (.ply) file of varying header size [message #89656 is a reply to message #89654] |
Thu, 06 November 2014 10:03  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On Thursday, November 6, 2014 11:40:19 AM UTC-5, Cyndy wrote:
> Hi folks -
>
> Way out of practice IDL programmer, here. I need to be able to read binary files, with potentially variable length header files, in a function in IDL.
> I'm not usre what the most efficient way to do that is; it seems it's easy if one knows a priori the number of bytes in the header, but i can't make that assumption. The files are .ply files, by the way, the header will tell me how much data follows, and the header ends with a specific set of characters. So, if I can read the header line by line, I can scan for the proper 'text' indicating end of header, and I know how to read in the rest. It's the header part I don't know how to do.
>
> Anyone have a few hints? Or if you know where I can find a binary ply to idl data structure procedure and can point me there, I'm eternally grateful :)
You can just read the data one byte at a time with READU. That is brute force, but it works.
If you know the maximum possible size of the header, then you can just read a stream of bytes and look for your signature.
;; Signature is 4 bytes 'deadbeaf' in hex
signature = ['de'xb, 'ad'xb, 'be'xb, 'ef'xb]
bb = bytarr(10000) ;; Assume maximum possible header size of 10000
readu, unit, bb
wh = where(bb[0:*] EQ signature[0] AND bb[1:*] EQ signature[1] AND $
bb[2:*] EQ signature[2] AND bb[3:*] EQ signature[3], ct)
;; Error checking
if ct EQ 0 then message, 'ERROR: signature not found'
;; We found it. wh[0] points to the first byte of signature in the header
pt_data = wh[0] + 4 ;; First character of data past the header
header = bb(0:pt_data-1)
;; Read the rest of the data
;; rewind to the start of the data
point_lun, unit, pt_data
;; ... now read the rest of the file however you want
Craig
|
|
|