Set output file type and options in RIOS

RIOS allows you to set the default file type and creation options using environmental variables, rather than having to set them each time, meaning these are set based on the system rather than the script and you only have to worry about them once. The following are used to set the file type:

export RIOS_DFLT_DRIVER="GTiff"

And creation options:

export RIOS_DFLT_DRIVEROPTIONS="COMPRESS=DEFLATE"

If you are using EnvMaster (which is awesome, see earlier post) you can set this as part of the EnvMaster file using:

module.setVar('GTiff', 'RIOS_DFLT_DRIVER')
module.setVar('COMPRESS=DEFLATE', 'RIOS_DFLT_DRIVEROPTIONS')

However, sometimes it is necessary to override these default variables, this can be done using the ApplierControls class:

controls = applier.ApplierControls()

controls.setOutputDriverName("GTiff")
controls.setCreationOptions(["COMPRESS=DEFLATE"])

Recently I wanted to create a script that would set the output file type and creation options based on the extension of the output file the user provided. To accomplish this I wrote following function to set the file type and creation options based on the extension of the output file.

def getOutDriver(outFileName):

    """ Set output driver type and creation options
        based on file extension.

        Returns driver name and list of creation options
        as dictionary.
    """

    outControls = {}

    gdalFormat = 'ENVI'
    gdalCOOptions = []
    calcStats = False

    extension = os.path.splitext(outFileName)[-1]
    if extension == '.kea':
        gdalFormat = 'KEA'
        calcStats = True
    elif extension == '.tif':
        gdalFormat = 'GTiff'
        gdalCOOptions = ['COMPRESS=DEFLATE']
        calcStats = True
    elif extension == '.img':
        gdalFormat = 'HFA'
        gdalCOOptions = ['COMPRESSED=YES']
        calcStats = True
    elif extension == '.pix':
        gdalFormat = 'PCIDSK'
        gdalCOOptions = ['COMPRESSION=RLE']
        calcStats = True

    outControls['gdalFormat'] = gdalFormat
    outControls['gdalCOOptions'] = gdalCOOptions
    outControls['calcStats'] = gdalCOOptions

    return outControls

Which is called using:

# Set format for output image
outControls = getOutDriver('outImageFile.kea')

# Set options
controls.setOutputDriverName(outControls['gdalFormat'])
controls.setCreationOptions(outControls['gdalCOOptions'])
controls.setCalcStats(outControls['calcStats'])

This will also calculate stats and overviews for formats which support them.

For a full description of environmental variables used by RIOS see the manual

The full script I created this function for (for topographically correcting SAR data) can be downloaded from here.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s