Skip to content

Instantly share code, notes, and snippets.

@hrwgc
Last active September 4, 2023 11:15
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save hrwgc/4694661 to your computer and use it in GitHub Desktop.
Save hrwgc/4694661 to your computer and use it in GitHub Desktop.
VIIRS Nighttime Lights 2012 processing
#!/bin/bash
FILES=('FILE2' 'FILE1');
ADDO="2 4 8 16 32 64 128 256 512 1024 2048"
LUTS=('15:0,50:255' '20:0,75:255' '5:0,200:255');
RELPATH="PATH/TO/GEOTIFFS"
for FILE in ${FILES[@]}; do
gdal_translate \
-scale 0 50 \
-ot Byte \
$FILE \
${FILE%.tif}_scale.tif
gdalwarp \
-wm 2000 \
-multi \
-srcnodata 0 \
-dstnodata 0 \
-r lanczos \
-co COMPRESS=LZW \
-wo OPTIMIZE_SIZE=TRUE\
-t_srs EPSG:3785 \
${FILE%.tif}_scale.tif \
${FILE%.tif}_3785.tif
gdaladdo \
-r cubic \
--config COMPRESS_OVERVIEW LZW \
${FILE%.tif}_3785.tif \
$ADDO
rm ${FILE%.tif}_scale.tif
done
gdalbuildvrt \
-srcnodata 0 \
-vrtnodata 0 \
red.vrt \
$RELPATH/nasa/*_3785.tif
gdalbuildvrt \
-srcnodata 0 \
-vrtnodata 0 \
blue.vrt \
$RELPATH/nasa/*_3785.tif
gdalbuildvrt \
-srcnodata 0 \
-vrtnodata 0 \
green.vrt \
$RELPATH/nasa/*_3785.tif
gdalbuildvrt \
-separate \
-srcnodata "0 0 0" \
-vrtnodata "0 0 0" \
-addalpha rgb.vrt \
$RELPATH/nasa/red.vrt \
$RELPATH/nasa/green.vrt \
$RELPATH/nasa/blue.vrt
cat rgb.vrt | sed -E "8s_(<SourceBand>1</SourceBand>)_\1<LUT>${LUTS[0]}</LUT>_1;19s_(<SourceBand>1</SourceBand>)_\1<LUT>${LUTS[1]}</LUT>_1;30s_(<SourceBand>1</SourceBand>)_\1<LUT>${LUTS[2]}</LUT>_1" |
sed -E "s/SimpleSource/ComplexSource/g;s_(<VRTRasterBand dataType=\"Byte\" band=\"1\">)_\1\
<ColorInterp>Red</ColorInterp>_g;s_(<VRTRasterBand dataType=\"Byte\" band=\"2\">)_\1\
<ColorInterp>Green</ColorInterp>_g;s_(<VRTRasterBand dataType=\"Byte\" band=\"3\">)_\1\
<ColorInterp>Blue</ColorInterp>_g;" > tmp.vrt
mv -f tmp.vrt rgb.vrt
gdalwarp \
-dstalpha \
-srcnodata "0 0 0" \
-dstnodata "0 0 0" \
-co COMPRESS=LZW \
-wm 2000 \
-multi \
rgb.vrt \
rgb.tif
gdaladdo \
-ro \
-r gauss \
rgb.tif \
$ADDO
@opb
Copy link

opb commented Feb 6, 2013

I'm getting told that the -addalpha option is not compatible with the -separate option. Any suggestions?

@sylar11
Copy link

sylar11 commented Mar 30, 2015

ftp files at
ftp://ftp.ngdc.noaa.gov/pub/outgoing/eog/viirs_ntl
are not available anymore... is it possible to have them again? thanks in advance

@fractalien
Copy link

New global mosaics are available here: http://ngdc.noaa.gov/eog/viirs/download_monthly.html

@prastut
Copy link

prastut commented Apr 14, 2017

@fractalien the link is also broken :/

@neteler
Copy link

neteler commented Nov 19, 2017

@YangZY2020
Copy link

What a waste of my time to write the damn code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment