Skip to content

Instantly share code, notes, and snippets.

@demiurg
Last active December 16, 2022 17:30
Show Gist options
  • Save demiurg/28ff85e10688bc3ce0e45312ac5977ff to your computer and use it in GitHub Desktop.
Save demiurg/28ff85e10688bc3ce0e45312ac5977ff to your computer and use it in GitHub Desktop.
Compress CMF to UInt16
### Commands to add
# Preserving nodata scaling (0 - 26212 is only for CH4), we will have to have a different factor for CO2
gdal_calc.py --calc "numpy.around(numpy.clip(A, 0, 26212) * 2.5)" --NoDataValue=65535 --format=GTiff --type=UInt16 --co=COMPRESS=DEFLATE --co=ZLEVEL=9 --A_band=4 -A ../cmf/ang20161008t204651_cmf_v1n2_img --outfile=./ang20161008t204651_cmf_v1n2_img_cmf_i16.tif
# Set the scaling metadata
gdal_edit.py -scale 0.4 ./ang20161008t204651_cmf_v1n2_img_cmf_i16.tif
# RGB
gdal_translate -of GTiff -ot Byte -b 1 -b 2 -b 3 -scale -co COMPRESS=DEFLATE -co ZLEVEL=9 ../cmf/ang20161008t204651_cmf_v1n2_img ./ang20161008t204651_cmf_v1n2_img_rgb.tif
##########################
# Unsigned Int 16 gives us values 0-65535
# To utilize the whole range, we pick a scale factor and apply it to some significant range of original values
# Range 0 - 26212 with factor 2.5, gives us 0 - 65530, which leaves 5 more values for flags. I choose the max for NoData.
# The CMF reduction from Float64 is about x10 with compression.
# RGB scaling is exactly the same, automatic min-max to byte, that is used for the KMZ.
###
# unscale uses the scaling metadata and reverts to original range, preserving
gdal_translate -of GTiff -ot Float32 -unscale ./ang20161008t204651_cmf_v1n2_img_cmf_i16.tif ./ang20161008t204651_cmf_v1n2_img_cmf_f32_unscaled.tif
###
# stuff that didn't work
# Compress and scale band - destroys nodata (-9999) when clamping - 2.6M
gdal_translate -of GTiff -ot UInt16 -b 4 -scale 0 26212 1 65531 -co COMPRESS=LZW cmf/ang20161008t204651_cmf_v1n2_img cmf3/ang20161008t204651_cmf_v1n2_img_i16.tif
# Extract band - 13M
gdal_translate -of GTiff -ot Float32 -b 4 cmf/ang20161008t204651_cmf_v1n2_img cmf2/ang20161008t204651_cmf_v1n2_img_f32.tif
# deflate 7.6
gdal_translate -of GTiff -ot Float32 -b 4 -co COMPRESS=DEFLATE -co ZLEVEL=9 cmf/ang20161008t204651_cmf_v1n2_img cmf2/ang20161008t204651_cmf_v1n2_img_f32_deflate.tif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment