Models frequently need parameters that can be found in an image's metadata file. This script shows an example of how to use a Python Script to parse a Landsat *mtl.txt file into a Dictionary so it can be used to extract values that could be used elsewhere in a Model.
Please refer to the earlier "" article showing how to calculate the ToA reflectance values for Landsat 8 imagery. That article mentions one limitation of the approach described, which is that the correction values must either be assumed, or need to be manually read from the input image's metadata header file and entered into the model each time.
An alternative approach is to provide a means to parse the metadata file, extract the necessary parameters and automatically ingest those within the model. With the introduction of ERDAS IMAGINE 2016 we have a simple way of doing this by using the Python Script Operator. A Python script was written which is capable of parsing the metadata file and converting it into a Dictionary. This Dictionary can then be interrogated by the Dictionary Item operator to extract the necessary parameters and pass them into the remainder of the model. The screenshot below shows the basic operation of extracting information from a Landsat 8 *_mtl.txt file.
The section below shows the contents of the Python Script operator.
|Content of the GetLandsatMetadata Python Script operator|
def GetLine(s, newline='\n'):
The ParseMetadata model can then be combined with the old Landsat8 Top of Atmosphere Correction model to create a new model which automatically extracts the information necessary to correct the input imagery to Top of Atmosphere reflectance.
The main deviation from that older methodology is the inclusion of an option to rescale the Reflectance values from their normal floating point values to unsigned 16-bit integers (generally to conserve disk space). This is done by the industry-standard approach of multiplying the reflectance values by 10000 and truncating to the nearest integer. This is achieved using the If Else operator introduced with ERDAS IMAGINE 2016 - if the user opts for True (1) on the Scale to Integer prompt the data is pulled through the branch of the model which scales by 10,000 and outputs a 16-bit integer data set. If the user selects False (0) the data instead goes through a branch which leaves the reflectance values un-scaled and writes out a 64-bit Float data set instead.
To execute this model you will need to have Python 3.4.x installed and configured for use with ERDAS IMAGINE. See here for instructions on how to install Python and also how to start using it within Spatial Modeler.
L8 OLI MSI: Select an input image file containing the Multi-spectral (MSI) bands (1 to 7) from the Landsat 8 OLI sensor
Landsat 8 Metadata File: Select the *_MTL.txt file corresponding to the input image.
Scale to Integer: If set to true (1), the output ToA Reflectance data will be scaled up by a factor of 10,000 and truncated to a 16-bit integer to save disk space. Defaults to false (float output).
Reflectance Out: Name of the output image file containing the ToA Reflectance values.