Add ENVI header to binary files

Quite often remote sensing data is supplied as a binary file with the dimensions and geographical information in a separate text file. If you can add the relevant information into an ENVI format header file you can easily read the data using GDAL. If you have a lot of files in this format it’s not too tricky to make a python script which will pull information from the supplied text file and populate the relevant fields in an ENVI header file.

  1. If you have ENVI the easiest thing to do is open one of the files, fill in the parameters in the dialogue and then use the header file (‘*.hdr’) generated as a template. This might require playing around with parameters (data types, byte order etc.,) if not all the information you need is with your data. Make sure the data displays OK and it’s in the correct geographic location.
  2. If you don’t have ENVI you can manually generate the file using template below as a guide or if you have another file which is similar (same projection etc.,) translate to ENVI format using gdal:
    gdal_translate -of ENVI infile.kea outfile.env

    Then use the header file you have just created as a template. It needs to have the same filename as the data with the extension hdr . A list of the data types the ‘data type’ number in the header corresponds to is available on the IDL website. You may need to experiment with the parameters a little, you can use TuiView to open the data and check everything looks OK.

  3. Once you have a template you write a python script, similar to that below, to create a header replacing the parameters (size, upper left coordinate) as needed.

headerParameters = {}
headerParameters['fileName'] = 'image'
headerParameters['samples'] = '500'
headerParameters['lines'] = '150'
headerParameters['ULlon'] = '147.39'
headerParameters['ULlat'] = '-25.79'
headerParameters['pixelSize'] = '9.9e-06'

headerText = '''ENVI
description = {{{fileName}}}
samples = {samples}
lines = {lines}
bands = 1
header offset = 0
data type = 4
interleave = bsq
sensor type = Unknown
byte order = 0
map info = {{Geographic Lat/Lon, 1.000, 1.000, {ULlon}, {ULlat}, {pixelSize}, {pixelSize}, WGS-84, units=Degrees}}
wavelength units = Unknown'''.format(**headerParameters)

headerFile = open('image.hdr','w')

  1. To create header files for all data in directory you need to loop through the data, and read parameters from the corresponding text file. There are lots of string functions build into python, the split function is particularly handy. Assuming a text file of the form:

    You could extract the parameters using:

    textFile = open('metadata.txt','rU')
    for line in textFile:
        elements = eachLine.split(':')
        elements[0] = elements[0].strip()
        elements[1] = elements[1].strip()
        if(elements[0] == 'width'):
            headerParameters['samples'] = elements[1]
        elif(elements[0] == 'height'):
            headerParameters['lines'] = elements[1]

There are a couple of examples of scripts to do this on our bitbucket repository:

  • – For JAXA ALOS PALSAR tiles, untars, creates header and optionally creates KMZ, using RSGISLib to stack bands and stretch.
  • – Generate ENVI header for files produced by GAMMA. Note for GAMMA the upper left coordinate is the centre of the first pixel (1.5, 1.5 in map info).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s