Using the RasterIO module it is possible to develop high level functions to per- form AVHRR specific tasks. The PyAVHRR module contains a series of func- tions used for pre-processing and analysis of AVHRR data using the RasterIO module. Appendix B, Section B.2 contains the documentation and complete source code for the PyAVHRR module.
The most important function in this module is the lst_DLR function, which implements the land surface temperature estimation algorithm developed by the German Aerospace Centre (DLR) (see Section 3.2.12). Listing 4.6 shows the lst_DLR function from the PyAVHRR module. The lst_DLR function takes AVHRR bands 4 and 5 (top of atmosphere brightness temperature) and NDVI derived from bands 1 and 2 in the same AVHRR scene, as NumPy ar- rays, and returns an array of estimated land surface temperature. Listing 4.6 shows that the NDVI inputs are scaled between 0 and 255 (representing a 256 bit image) (Line 82). The scaled NDVI values are then used to derive the emissivity coefficients (Lines 84-87). Estimated land surface tempera- ture (EST) is calculated using a NumPy vectorised iterator to apply the DLR equation to each pixel in-turn, returning a NumPy array containing EST for the same pixels as captured in the original AVHRR scene (Line 88). Listing 4.7 shows an example of using the PyAVHRR lst_DLR function in a Python script.
In the LST script (Listing 4.7) the PyAVHRR readbands and ndvi utility functions are shown. The readbands function (Line 12) is a wrapper for the RasterIO.readband function, which reads each of the five AVHRR bands and returns a tuple of the arrays accessed through band numbers one to five. The ndvi function calculates NDVI from bands 1 and 2 and returns the result (Line 14). The NDVI value along with data from bands four and five is then passed to the lst_DLR function and the output LST array is then written to a new file (Line 19).
68 # Function to calculate estimated land surface temperature from AVHRR
ch4/ch5 (see http://eoweb.dlr.de/short_guide/D-LST.html)
def lst_DLR (b4, b5, ndvi):
70 ’’’Accepts a NDVI raster and AVHRR bands 4 and 5, derives a new
raster of estimated land surface temperature (Kelvin).
72 This function calculates estimated land surface temperature
using a split window method to correct
atmospheric attenuation of thermal bands and NDVI to apply a correct for surface emissivity.
74 Returns a new single raster of Estimated surface temperature in
Kelvin.
76 Developed by Deutsches Zentrum fur Luft- und Raumfahrt (DLR) -
German Aerospace Center
Visit: http://eoweb.dlr.de/short_guide/D-LST.html for more details.
78 >>> rlst = avhrr.lst_DLR(b4, b5, fndvi)
80 ’’’
# first scale any NDVI inputs
82 ndvi255 = (ndvi+1)*127
# second, calculate coefficients
84 e4 = 1.0094 + 0.047*(np.log(ndvi255)) e5 = e4 + 0.01 86 e = (e4 + e5)/2 de = e4 - e5 88 lst_raster=1.274+(b4+b5)/2*(1+0.15616*((1-e)/e)-0.482*de/(np. power(e,2)))+(b4-b5)/2*(6.26+3.989*((1-e)/e)+38.33*de/(np. power(e,2))) return lst_raster
Listing 4.6: The PyAVHRR lst_DLR function to calculate estimated surface temperature from AVHRR bands 4 and 5 using the DLR method.
import RasterIO as rio
2
import PyAVHRR as avhrr
4 # Get GDAL pointer to scene
gpointer = rio.opengdalpointer(’AVHRR_20030812.tif’)
6 # Get geospatial meta-data
driver, xsize, ysize, proj, geot = rio.readrastermeta(gpointer)
8 # Get the EPSG code for the projection
epsg = rio.wkt2epsg(proj)
10 # Read all five AVHRR bands (wrapper for RasterIO functions)
12 scene = avhrr.readbands(gpointer)
# Calculate NDVI ratio from bands one and two
14 ndvi = avhrr.ndvi(scene[1],scene[2]) 16 # Calculate LST lst = avhrr.lst_DLR(scene[4],scene[5],ndvi) 18 # Write the output LST as a single band image
rio.writerasterbands(’lst_image.tif’, driver, xsize, ysize, geot, epsg,
lst)
20 exit(0)
Listing 4.7: An example script using the PyAVHRR module to calculate EST from an AVHRR scene using the lst_DLR function.
4.2.3
Raster Processing Suite
The Raster Processing Suite is an extension to the RasterIO module which provides a graphical user interface for performing raster numerical process- ing within the Quantum GIS (QGIS) package (QGIS, 2012). The advantage of the processing suite is that raster scenes can be visualised in QGIS be- fore and after processing, aiding exploratory analysis. The Raster Processing Suite provided the first “raster calculator” functionality in the QGIS pack- age, prior to the inclusion of the internal raster calculator tool in version 1.6 (Sutton, 2010). The source code for the Raster Processing Suite is shown in
Appendix B, Section B.3.
The Raster Processing Suite contains two interfaces for manipulating raster data, the Processor and the Python script interface. The Processor (Figure 4.5) contains an equation editor which allows the user to develop equations for raster images using NumPy’s vectorised processing functions. The user loads a raster band into memory using the “Load Band” button (Figure 4.5). This creates a NumPy array in the global memory space of the Raster Pro- cessing Suite, using the RasterIO read function to read the values from the file specified (Figure 4.3). The name of the array added to the Equation Editor is based on the name of the file and the band loaded. For example band 1 from ’avhrr_file.tif ’ would be ’avhrr_file_1’. Once a band is loaded it is possible to use the Python globals() dictionary, with the array name acting as the dic- tionary key, to retrieve the array data. If the user loads or refers to the same band more than once, only one instance of the band is created in memory.
Figure 4.5: Screen-shot of the Raster Processing Suite in Quantum GIS.
The equation editor accepts standard NumPy arithmetic for processing op- erations. The user selects an output file and image format for the result. Alternatively, the output can be printed to the console, a useful feature when performing exploratory analysis using spatial aggregates (e.g. an average of all pixels). Once the output is defined the user can press the ’Run’ button
and the Raster Processing Suite will attempt to process the equation in the Equation Editor using the loaded raster bands. The equation is converted to a plain string, and parsed using the Python eval function which interprets the string as a Python expression. References to the loaded raster arrays are detected automatically as they exist in global memory space. The output from the equation is returned as a NumPy array, which is written to a new file on disk if specified by the user. Figure 4.6 shows the Equation Editor being used to generate NDVI from bands 1 and 2 in an AVHRR scene, replicating the process shown in Listing 4.5.
Figure 4.6: Screen-shot of the Raster Processing Suite in Quantum GIS being used to calculate the NDVI using an AVHRR scene.
The Scripting Interface provides a text editor window to create Python scripts for raster processing using the RasterIO and PyAVHRR modules. The code entered into the Scripting Interface is executed as a stand-alone Python pro- cess and can be be run within the Raster Processing Suite. The interface is dynamically linked to the Equation Editor window so that the user may de- velop a process using the graphical user interface in the Equation Editor and view the process as an automatically generated Python script. The script can then be saved as a standard Python file and used at a later date.
terIO processing routines. For example, the user could develop a raster pro- cess using a single scene in the Equation Editor and then use the Python representation in the Scripting Interface to apply the process iteratively over a time-series of scenes. Figure 4.6 shows the Scripting Interface with an auto- matically generated script from the NDVI calculation in the Equation Editor window.
Figure 4.7: Screen-shot of the Raster Processing Suite in Quantum GIS show- ing the auto-generated script to calculate NDVI using an AVHRR scene.