In case helpful for others, here is the relevant section from my bash script. The only variable that is defined outside this section is file, which is just my file prefix.
#!/bin/bash
function normalize_grid {
# Normalize values to 0-1
input=$1
output=$2
range=`gmt grdinfo ${input} -T`
normmin=`echo $range | awk -F "[T/]" '{print $2}'`
max=`echo $range | awk -F "[T/]" '{print $3}'`
normmax="$((${max}-${normmin}))"
# Normalize from 0-1
gmt grdmath ${input} ${normmin} SUB ${normmax} DIV -V = ${output}
}
function clip_grid {
# Clip grid values to 2-98%
input=$1
output=$2
cliprange=`gmt grdinfo ${input} -T+a2`
clipmin=`echo $cliprange | awk -F "[T/]" '{print $2}'`
clipmax=`echo $cliprange | awk -F "[T/]" '{print $3}'`
gmt grdclip ${input} -G${output} -Sb${clipmin}/${clipmin} -Sa${clipmax}/${clipmax} -V
}
function process_band {
# Process band from GeoTiff image for use in grdmix, by converting all 0
# values to NAN, clipping the tails of the distribution using an alpha value of 2,
# and normalizing 0-1.
# Expects:
# - Prefix for input GeoTiff image file.
# - Band number with 0 indexing (i.e., one less than typical band naming conventions) in format "b1".
# Returns:
# Filename for the processed netcdf file.
#
# Set variables for file names
prefix=$1
band=$2
input="${prefix}.tif=gd+${band}"
output0="${prefix}_${band}_int.nc"
output1="${prefix}_${band}_clip.nc"
output2="${prefix}_${band}_clip_norm.nc"
# Convert 0 to NaN
gmt grdmath ${input} 0 NAN = "${output0}=nf"
# Clip to 2 and 98 percentiles
clip_grid ${output0} ${output1}
# Normalize values to 0-1
normalize_grid ${output1} ${output2}
echo ${output2}
}
red_file=`process_band ${file} "b3"`
green_file=`process_band ${file} "b2"`
blue_file=`process_band ${file} "b1"`
gmt grdmix ${red_file} ${green_file} ${blue_file} -G${file}_grdmix.tif -C