S2: SAFE to ENVI

These instructions allow you to convert L1C Sentinel-2 images from ESA's SAFE format to ENVI format.

for i in */GRANULE/*/IMG_DATA/*B01.jp2; do gdal_merge.py -of ENVI -o ${i%????????}_L1C_e -ps 10 10 -ot UInt16 ${i} ${i%??????}02.jp2 ${i%??????}03.jp2 ${i%??????}04.jp2 ${i%??????}05.jp2 ${i%??????}06.jp2 ${i%??????}07.jp2 ${i%??????}08.jp2 ${i%??????}8A.jp2 ${i%??????}11.jp2 ${i%??????}12.jp2 -separate; done

mv -v */*/*/*/*_e* .

for i in *_e.hdr; do echo 'wavelength units = Micrometers' >> ${i}; done

for i in *_e.hdr; do echo 'wavelength = {0.443, 0.490, 0.560, 0.665, 0.705, 0.740, 0.783, 0.842, 0.865, 1.610, 2.190}' >> ${i}; done


These instructions allow you to convert L2A Sentinel-2 images from ESA's SAFE format to ENVI format.

for i in */GRANULE/*/IMG_DATA/*/*B01_20m.jp2; do gdal_merge.py -of ENVI -o ${i%????????????}_L2A_e -ps 10 10 -ot UInt16 ${i} ${i%??????????????????????????????????????}10m/*B02_10m.jp2 ${i%??????????????????????????????????????}10m/*B03_10m.jp2 ${i%??????????????????????????????????????}10m/*B04_10m.jp2 ${i%??????????}05_20m.jp2 ${i%??????????}06_20m.jp2 ${i%??????????}07_20m.jp2 ${i%??????????????????????????????????????}10m/*B08_10m.jp2 ${i%??????????}8A_20m.jp2 ${i%??????????}11_20m.jp2 ${i%??????????}12_20m.jp2 -separate; done

mv -v */*/*/*/*/*_e* .

for i in *_e.hdr; do echo 'wavelength units = Micrometers' >> ${i}; done

for i in *_e.hdr; do echo 'wavelength = {0.443, 0.490, 0.560, 0.665, 0.705, 0.740, 0.783, 0.842, 0.865, 1.610, 2.190}' >> ${i}; done


for i in *_e; do gdal_translate -projwin 455000 1325000 509750 1290250 -of ENVI ${i} ${i}_SubSE; done