Py6S: Run a radiative transfer model with a user-defined input spectrum
Version 1.2 of Py6S has recently been released (including a couple of bug fix releases, taking the most recent version to v1.2.2), and the major new feature in this version is the ability to use any spectrum as the ground reflectance for a model run. Previously, users were restricted to using the built-in 6S ground spectra (vegetation, clear water, lake water and sand) or doing some complicated parameterisation to set the user-defined reflectances up in the correct manner – so complicated that it was almost never worth doing!
Luckily, that has all changed in this version – read on to find out more…
Setting the Ground Reflectance
Setting the ground reflectance parameterisation in Py6S is very simple – you just use one of the methods of the GroundReflectance class. For example, to create a 6S model and set a constant reflectance of 0.5 for all wavelengths, simply run:
from Py6S import * s = SixS() s.ground_reflectance = GroundReflectance.HomogeneousLambertian(0.5) s.run()
- A single reflectance value as a float, which is used as a uniform reflectance for all wavelengths:
s.ground_reflectance = GroundReflectance.HomogeneousLambertian(0.7)
- A constant stating a built-in 6S reflectance value:
s.ground_reflectance = GroundReflectance.HomogeneousLambertian(GroundReflectance.GreenVegetation)
- An array giving a user-defined spectrum given in steps of 2.5nm for the wavelength range used for that simulation (the last bit is important!)
s.ground_reflectance = GroundReflectance.HomogeneousLambertian([0.6, 0.8, 0.34, 0.453])
But now there is one more option:
- A two-dimensional array containing wavelengths (column 0, in micrometres) and reflectance values (column 1, as a fraction). This array will then be taken by Py6S, resampled to 2.5nm, subset to only the range of wavelengths being used for the current simulation, and used with the underlying 6S model.
s.ground_reflectance = GroundReflectance.HomogeneousLambertian(spectrum_array)
This saves a lot of time and extra code, compared to doing all of the resampling yourself and using the user-defined spectrum option which was always available in Py6S. However, there are more benefits than this… Py6S will store the whole spectrum that you give, and just re-sample it to the correct wavelength range every time you run the model, which means you can use the lovely helper methods like run_vnir and run_landsat_tm to run simulations for a spectrum for a number of wavelengths with very little code. For example:
from Py6S import * s = SixS() # Somehow generate a 2D array called spectrum_array here s.ground_reflectance = GroundReflectance.HomogeneousLambertian(spectrum_array) wavelengths, results = SixSHelpers.Wavelengths.run_vnir(s, output_name="apparent_radiance")
Getting spectral data
Of course, that example won’t work until we add in some code to generate this 2D array for us. There are a two ways we could get this array:
- Generate it within our code somehow – for example, using a simple model, or interpolation between some hard-coded values.
- Load it from some sort of external datafile, for example from a CSV file using np.loadtxt
Both of those are ways that you might want to use, but I’ve added functions to Py6S to simplify a couple of ways of doing this that may be useful to people.
Firstly, the functions in the Spectra module make it easy to import spectra from the USGS Spectral Library and the ASTER Spectral Library. These two libraries seem to be the most commonly used – as far as I am aware, at least – and have a very wide range of spectra in them. The two functions are very simple to use – you simply pass the URL or file path to the spectrum data file, and it returns the right sort of array. Thus you can do:
spectrum_array = Spectra.import_from_usgs("http://speclab.cr.usgs.gov/spectral.lib06/ds231/ASCII/V/russianolive.dw92-4.30728.asc") s.ground_reflectance = GroundReflectance.HomogeneousLambertian(spectrum_array)
s.ground_reflectance = GroundReflectance.HomogeneousLambertian(Spectra.import_from_usgs("http://speclab.cr.usgs.gov/spectral.lib06/ds231/ASCII/V/russianolive.dw92-4.30728.asc"))
(Either of the URLs could be replaced with a simple local file path instead, which is particularly useful if you have downloaded the USGS Spectral Library archive file, or obtained the CD of the ASTER Spectral Library).
The other way to get data is to use a model. Many models can produce spectral data, and most will output in a format like CSV that can be imported into a NumPy array really easily – but to make things even easier I have also released a Python interface to the ProSAIL called – you guessed it – PyProSAIL. Again, the methods have been carefully designed to return the right sort of arrays, so you can simply do:
# Make sure you have both PyProSAIL and Py6S installed import pyprosail from Py6S import * spectrum = pyprosail.run(1.5, 40, 8, 0, 0.01, 0.009, 1, 3, 0.01, 30, 0, 10, 0, pyprosail.Planophile) s = SixS() s.ground_reflectance = GroundReflectance.HomogeneousLambertian(spectrum) s.run()
So, hopefully that has been a useful introduction to how to use user-defined spectra with Py6S. I’ll leave you with an example of how to put all of this together to do something useful – that is, to run a Py6S simulation with the ground reflectance set to the spectra from every USGS spectral library file in a directory, and store the results:
from Py6S import * import glob import numpy as np # Create the SixS model and set some parameters s = SixS() s.aero_profile = AeroProfile.PredefinedType(AeroProfile.NoAerosols) s.altitudes.set_sensor_satellite_level() s.altitudes.set_target_sea_level() # Get a list of files that have the extension .asc # (the extension used by the USGS spectral library files) files = glob.glob("./USGSSpecLib/Selected/*.asc") results =  # Run the model using each filename as the ground reflectance spectrum # using the Landsat ETM+ bands for filename in files: s.ground_reflectance = GroundReflectance.HomogeneousLambertian(Spectra.import_from_usgs(filename)) wvs, res = SixSHelpers.Wavelengths.run_landsat_etm(s, output_name='apparent_radiance') results.append(res) # Stack all of the results into an array results_arr = np.vstack(results) # Write the results out to a CSV file for further analysis np.savetxt("results.csv", results_arr, delimeter=",")