Unmixing S2
This describes how to unmix a large number of Sentinel-2 images from ENVI-formatted reflectance, and simultaneously compute three output images:
Unmixed image, where the bands correspond to subpixel fractions of each endmember;
Modeled image, which represents what the reflectance spectrum of each pixel would have been if the mixture model were perfectly accurate;
Mixture Residual image, which represents the wavelength-specific difference between the observed and modeled reflectance images.
To do this, you already need to have the following things in hand:
A folder of ENVI-formatted reflectance images
A file with the endmembers you would like to use to do the unmixing
This is usually in the form of columns, with spaces separating the columns. The first column should be the center wavelength of each band. The next columns are the endmember spectra.
Once this is ready, execute the following steps:
Download this script: batch_HLS_L30_MixtureResidual.py
Create an extra copy of your endmember file. Open it in a text editor and remove all the header lines.
Change the following variables within the script:
Line 20: directory (should be the folder holding your files)
Line 21: endmember file (should be the path to the headerless endmember file you created above)
Line 23: data file (should be the path to your files)
Line 31: unit sum weight (change according to data scaling)
Line 34: rescale (change according to data scaling)
Run the code.
Run the following commands to generate header files for each of the output files:
for i in *L2A_e.hdr; do sed 's/bands = 11/bands = 11/' ${i} > ${i%????}_Modeled.hdr; done
for i in *_Modeled.hdr; do sed -i.bak 's/data type = 12/data type = 4/' ${i}; done
for i in *L2A_e.hdr; do sed 's/bands = 11/bands = 11/' ${i} > ${i%????}_MixtureResidual.hdr; done
for i in *_MixtureResidual.hdr; do sed -i.bak 's/data type = 12/data type = 4/' ${i}; done
for i in *L2A_e.hdr; do sed 's/bands = 11/bands = 3/' ${i} > ${i%????}_Unmixed.hdr; done
for i in *_Unmixed.hdr; do sed -i.bak 's/data type = 12/data type = 4/' ${i}; done
for i in *_Unmixed.hdr; do sed -i.bak 's/Band 1,/Substrate,/g' ${i}; sed -i.bak 's/Band 2,/Vegetation,/g' ${i}; sed -i.bak 's/Band 3,/Dark}/g' ${i}; sed -i.bak 's/Band 4,//g' ${i}; sed -i.bak 's/Band 5,//g' ${i}; sed -i.bak 's/Band 6,//g' ${i}; sed -i.bak 's/Band 7,//g' ${i}; sed -i.bak 's/Band 8,//g' ${i}; sed -i.bak 's/Band 9,//g' ${i}; sed -i.bak 's/Band 10,//g' ${i}; sed -i.bak 's/Band 11}//g' ${i}; done ; rm -v -r *.bak