X
5436

Tutorial for using BINARY_TEMPLATE and READ_BINARY

Topic:

This article provides an example of how to use the BINARY_TEMPLATE and READ_BINARY functions.

Discussion:

First there is an example of how to use the BINARY_TEMPLATE function on the following help pages:

http://www.harrisgeospatial.com/docs/READ_BINARY.html

The example starts in the "Working with a READ_BINARY Data structure" section of the READ_BINARY help which references the steps listed in the BINARY_TEMPLATE example.

An additional example of how to use the BINARY_TEMPLATE is shown below:

1) First, create a binary file using the code below. This will create a binary file with three data sets (float, long, byte) that is composed of 10 elements each.

pro ex_create_binary_4temp

  compile_opt idl2

  a = findgen(10)
  b =lindgen(10)*10
  c =bindgen(10)+10b

  openw,lun,"ex_binary_file_4temp.dat",/get_lun

  writeu,lun,a,b,c
  free_lun, lun

end

2) Enter "file = dialog_pickfile()" into the IDL console and navigate to the "ex_binary_file_4temp.dat" file that you created in step 1.

3) Enter "ex_temp=binary_template(file)" into the IDL console.

4) This will bring up the "Binary Template"dialog. Click the "New Field" button.

5) This will bring up the "New Field" dialog.The first field in your data is the float array. Enter "float_data"as the Field name. Select "Float (32 bits)" as the type and"1" as the Number of dimensions. Enter "10" as the size and click "OK".

6) Repeat steps 3 and 4 for the long field. Enter the following information into the New Field dialog:

Field name : "long_data"
Type: "Long (32 bits)"
Number of dimensions: '1'
Size:10

7) Repeat steps 3 and 4 for the byte field. Enter the following information into the New Field dialog:

Field name: "byte_data"
Type: "Byte (unsigned 8 bits)"
Number of dimensions: '1'
Size: 10

Click "OK" in the "Binary Template"dialog.

8) Enter "rr = read_binary(file,template=ex_temp)" into the IDL console to read the data.

IDL> rr = READ_BINARY(file, TEMPLATE=ex_temp)

IDL> help, rr
** Structure <a260470>, 3 tags, length=92, datalength=90, refs=1:
  FLOAT_DATA      FLOAT    Array[10]
  LONG_DATA       LONG     Array[10]
  BYTE_DATA       BYTE     Array[10]

IDL> print, rr.float_data
     0.000000      1.00000      2.00000      3.00000      4.00000
    5.00000     6.00000      7.00000      8.00000      9.00000

IDL> print, rr.long_data
         0          10          20          30          40          50
       60          70          80          90

IDL> print, rr.byte_data
10  11 12  13  14  15  16 17  18  19

9) You can save the template for future use with the SAVE routine:

save, ex_temp, FILENAME="ex_temp.sav"

You can use the RESTORE routine to load this variable into a new session of IDL:

restore, "ex_temp.sav"

9) You can modify the existing the template using the TEMPLATE keyword when calling BINARY_TEMPLATE:

ex_temp_new = BINARY_TEMPLATE(file, TEMPLATE=ex_temp)


Written by DS and reviewed by JU 02/07/2014