Skip to content

Instantly share code, notes, and snippets.

@mutolisp
Created November 9, 2010 11:00
Show Gist options
  • Save mutolisp/668963 to your computer and use it in GitHub Desktop.
Save mutolisp/668963 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Lin, Cheng-Tao 2010
# School of Forestry and Resource Conservation, National Taiwan University
# For GRASS GIS
# Variable explanations, derived from http://www.worldclim.org/bioclim-aml
# BIO1 = Annual Mean Temperature
# BIO2 = Mean Diurnal Range (Mean of monthly (max temp - min temp))
# BIO3 = Isothermality (P2/P7) (* 100)
# BIO4 = Temperature Seasonality (standard deviation *100)
# BIO5 = Max Temperature of Warmest Month
# BIO6 = Min Temperature of Coldest Month (TMC)
# BIO7 = Temperature Annual Range (P5-P6)
# BIO8 = Mean Temperature of Wettest Quarter
# BIO9 = Mean Temperature of Driest Quarter
# BIO10 = Mean Temperature of Warmest Quarter
# BIO11 = Mean Temperature of Coldest Quarter
# BIO12 = Annual Precipitation
# BIO13 = Precipitation of Wettest Month
# BIO14 = Precipitation of Driest Month
# BIO15 = Precipitation Seasonality (Coefficient of Variation)
# BIO16 = Precipitation of Wettest Quarter
# BIO17 = Precipitation of Driest Quarter
# BIO18 = Precipitation of Warmest Quarter
# BIO19 = Precipitation of Coldest Quarter
#
# These summary Bioclimatic variables are after:
# Nix, 1986. A biogeographic analysis of Australian elapid snakes. In: R. Longmore (ed.).
# Atlas of elapid snakes of Australia. Australian Flora and Fauna Series 7.
# Australian Government Publishing Service, Canberra.
#
# and Expanded following the ANUCLIM manual
GCN=CCCMA
F=EA_wc_30s_${GCN}
MVAR=(tmax tmin tmean)
YEAR=(2020 2050 2080)
SCENE=(A2a B2a)
# define resolution and region first
g.region res=00:00:30 n=60N s=20N e=170E w=90E
# FUNCTIONS
quart() {
for (( q=1; q<=12; q++ ))
do
if [ ${q} -lt 11 ];then
q2=$((${q}+1))
q3=$((${q}+2))
if g.list type=rast | grep QPREC_${j}_${i}_${q} ;then
echo "QPREC_${j}_${i}_${q} exists!"
else
r.mapcalc "QPREC_${j}_${i}_${q}=${F}_${j}_${i}_prec_${q}+${F}_${j}_${i}_prec_${q2}+${F}_${j}_${i}_prec_${q3}"
fi
if g.list type=rast | grep QTMEAN_${j}_${i}_${q} ;then
echo "QTMEAN_${j}_${i}_${q} exists!"
else
r.mapcalc "QTMEAN_${j}_${i}_${q}=${F}_${j}_${i}_tmean_${q}+${F}_${j}_${i}_tmean_${q2}+${F}_${j}_${i}_tmean_${q3}"
fi
elif [ ${q} -eq 11 ]; then
q2=12; q3=1
if g.list type=rast | grep QPREC_${j}_${i}_${q} ;then
echo "QPREC_${j}_${i}_${q} exists!"
else
r.mapcalc "QPREC_${j}_${i}_${q}=${F}_${j}_${i}_prec_${q}+${F}_${j}_${i}_prec_${q2}+${F}_${j}_${i}_prec_${q3}"
fi
if g.list type=rast | grep QTMEAN_${j}_${i}_${q} ;then
echo "QTMEAN_${j}_${i}_${q} exists!"
else
r.mapcalc "QTMEAN_${j}_${i}_${q}=${F}_${j}_${i}_tmean_${q}+${F}_${j}_${i}_tmean_${q2}+${F}_${j}_${i}_tmean_${q3}"
fi
elif [ ${q} -eq 12 ]; then
q2=1; q3=2
if g.list type=rast | grep QPREC_${j}_${i}_${q} ;then
echo "QPREC_${j}_${i}_${q} exists!"
else
r.mapcalc "QPREC_${j}_${i}_${q}=${F}_${j}_${i}_prec_${q}+${F}_${j}_${i}_prec_${q2}+${F}_${j}_${i}_prec_${q3}"
fi
if g.list type=rast | grep QTMEAN_${j}_${i}_${q} ;then
echo "QTMEAN_${j}_${i}_${q} exists!"
else
r.mapcalc "QTMEAN_${j}_${i}_${q}=${F}_${j}_${i}_tmean_${q}+${F}_${j}_${i}_tmean_${q2}+${F}_${j}_${i}_tmean_${q3}"
fi
else
echo "ERROR! FUNC quart(): Exception caught!"
fi
done
}
# wettest quarter calculation
wetquarter() {
echo "Step 1 of 3: (WETTEST QUARTER) compare precipitation for each quarter"
# initial values of comparison base layer
# set the terrain value to 1, otherwise 0, this is used to compare
# the wettest/warmest quarter
r.mapcalc "cf_0=if(isnull(QPREC_${j}_${i}_1),0,100)"
r.mapcalc "cf_1=if(cf_0<1,null(),1)"
r.mapcalc "wet_1=QPREC_${j}_${i}_1"
r.mapcalc "dry_1=wet_1"
r.mapcalc "wtemp_1=QTMEAN_${j}_${i}_1"
for (( w=1; w<=11; w++ ))
do
x=$((w+1))
# surrogate value i.e.: number of the quarter
echo "6 Calculating cf_${x}=if(QPREC_${j}_${i}_${x} > wet_${w}, ${x}, cf_${w})..."
r.mapcalc "cf_${x}=if(QPREC_${j}_${i}_${x} > wet_${w}, ${x}, cf_${w})"
# real value of wettest quarter
echo "7 Calculating wet_${x}=if(QPREC_${j}_${i}_${x} > wet_${w}, QPREC_${j}_${i}_${x}, wet_${w})..."
r.mapcalc "wet_${x}=if(QPREC_${j}_${i}_${x} > wet_${w}, QPREC_${j}_${i}_${x}, wet_${w})"
echo "8 Calculating wtemp_${x}=if(QTMEAN_${j}_${i}_${x} > wtemp_${w}, QTMEAN_${j}_${i}_${x}, wtemp_${w})..."
r.mapcalc "wtemp_${x}=if(QTMEAN_${j}_${i}_${x} > wtemp_${w}, QTMEAN_${j}_${i}_${x}, wtemp_${w})"
done
# BIO16: Precipitation of wettest quarter
echo "Step 1.1 Calculating BIO16: Precipitation of wettest quarter"
r.mapcalc "${F}_${j}_${i}_BIO16=wet_12"
# intermediate to calculate the BIO8
r.mapcalc "${F}_${j}_${i}_QWET=cf_12"
echo "Step 2 of 3: (DRIEST QUARTER) compare precipitation for each quarter"
# calculate for driest quarter (36)
for (( w=1; w<=11; w++ ))
do
x=$((w+1))
# surrogate value i.e.: number of the quarter
echo "11 Calculating cf_${x}=if(QPREC_${j}_${i}_${x} > dry_${w}, ${x}, cf_${w})..."
r.mapcalc "cf_${x}=if(QPREC_${j}_${i}_${x} < dry_${w}, ${x}, cf_${w})"
# real value of wettest quarter
echo "12 Calculating dry_${x}=if(QPREC_${j}_${i}_${x} < dry_${w}, QPREC_${j}_${i}_${x}, dry_${w})..."
r.mapcalc "dry_${x}=if(QPREC_${j}_${i}_${x} < dry_${w}, QPREC_${j}_${i}_${x}, dry_${w})"
echo "13 Calculating wtemp_${x}=if(QTMEAN_${j}_${i}_${x} < wtemp_${w}, QTMEAN_${j}_${i}_${x}, wtemp_${w})..."
r.mapcalc "wtemp_${x}=if(QTMEAN_${j}_${i}_${x} < wtemp_${w}, QTMEAN_${j}_${i}_${x}, wtemp_${w})"
done
# BIO17: Precipitation of driest quarter (1)
echo "14 Step 2.1 Calculating BIO17: Precipitation of driest quarter ..."
r.mapcalc "${F}_${j}_${i}_BIO17=dry_12"
# output the driest quarter (1)
r.mapcalc "${F}_${j}_${i}_QDRY=cf_12"
# (24)
echo "Step 3 of 3: Calculating BIO8 & BIO9: mean temperature of wettest/driest quarter ..."
for (( y=1; y<=12; y++ ))
do
echo "15 Calculating qw${y}=if(${F}_${j}_${i}_QWET==${y},QTMEAN_${j}_${i}_${y},-9999)..."
r.mapcalc "qw${y}=if(${F}_${j}_${i}_QWET==${y},QTMEAN_${j}_${i}_${y},-9999)"
echo "16 Calculating qd${y}=if(${F}_${j}_${i}_QDRY==${y},QTMEAN_${j}_${i}_${y},-9999)..."
r.mapcalc "qd${y}=if(${F}_${j}_${i}_QDRY==${y},QTMEAN_${j}_${i}_${y},-9999)"
done
}
###################### WARMEST / COLDEST quarter #######
warmquarter() {
echo "Step 1 of 3: (WARMEST QUARTER) compare precipitation for each quarter"
# initial values of comparison base layer
# set the terrain value to 1, otherwise null, this is used to compare
# the wettest/warmest quarter
r.mapcalc "wf_0=if(isnull(QTMEAN_${j}_${i}_1),0,100)"
r.mapcalc "wf_1=if(wf_0<1,null(),1)"
r.mapcalc "warm_1=QTMEAN_${j}_${i}_1"
r.mapcalc "cold_1=warm_1"
r.mapcalc "wprec_1=QPREC_${j}_${i}_1"
for (( w=1; w<=11; w++ ))
do
x=$((w+1))
# surrogate value i.e.: number of the quarter
r.mapcalc "wf_${x}=if(QTMEAN_${j}_${i}_${x} > warm_${w}, ${x}, wf_${w})"
# real value of warmest quarter
r.mapcalc "warm_${x}=if(QTMEAN_${j}_${i}_${x} > warm_${w}, QTMEAN_${j}_${i}_${x}, warm_${w})"
r.mapcalc "wprec_${x}=if(QPREC_${j}_${i}_${x} > wprec_${w}, QPREC_${j}_${i}_${x}, wprec_${w})"
done
# intermediate to calculate the BIO18
r.mapcalc "${F}_${j}_${i}_QWARM=wf_12"
# BIO10: Mean temperature of warmest quarter
r.mapcalc "${F}_${j}_${i}_BIO10=warm_12/3"
# calculate for driest quarter
for (( w=1; w<=11; w++ ))
do
x=$((w+1))
# surrogate value i.e.: number of the quarter
r.mapcalc "wf_${x}=if(QTMEAN_${j}_${i}_${x} < cold_${w}, ${x}, wf_${w})"
# real value of wettest quarter
r.mapcalc "cold_${x}=if(QTMEAN_${j}_${i}_${x} < cold_${w}, QTMEAN_${j}_${i}_${x}, cold_${w})"
r.mapcalc "wprec_${x}=if(QPREC_${j}_${i}_${x} < wprec_${w}, QPREC_${j}_${i}_${x}, wprec_${w})"
done
# intermediate to calculate the BIO19
r.mapcalc "${F}_${j}_${i}_QCOLD=wf_12"
# BIO11: Mean temperature of coldest quarter
r.mapcalc "${F}_${j}_${i}_BIO11=cold_12/3"
for (( y=1; y<=12; y++ ))
do
r.mapcalc "qh${y}=if(${F}_${j}_${i}_QWARM==${y},QPREC_${j}_${i}_${y},-9999)"
r.mapcalc "qc${y}=if(${F}_${j}_${i}_QCOLD==${y},QPREC_${j}_${i}_${y},-9999)"
done
}
for i in "${YEAR[@]}"
do
for j in "${SCENE[@]}"
do
# Maximum, minimum temperature & precipitation arrays
NTMAX=(${F}_${j}_${i}_tmax_1,${F}_${j}_${i}_tmax_2,${F}_${j}_${i}_tmax_3,${F}_${j}_${i}_tmax_4,${F}_${j}_${i}_tmax_5,${F}_${j}_${i}_tmax_6,${F}_${j}_${i}_tmax_7,${F}_${j}_${i}_tmax_8,${F}_${j}_${i}_tmax_9,${F}_${j}_${i}_tmax_10,${F}_${j}_${i}_tmax_11,${F}_${j}_${i}_tmax_12)
NTMIN=(${F}_${j}_${i}_tmin_1,${F}_${j}_${i}_tmin_2,${F}_${j}_${i}_tmin_3,${F}_${j}_${i}_tmin_4,${F}_${j}_${i}_tmin_5,${F}_${j}_${i}_tmin_6,${F}_${j}_${i}_tmin_7,${F}_${j}_${i}_tmin_8,${F}_${j}_${i}_tmin_9,${F}_${j}_${i}_tmin_10,${F}_${j}_${i}_tmin_11,${F}_${j}_${i}_tmin_12)
NPREC=(${F}_${j}_${i}_prec_1,${F}_${j}_${i}_prec_2,${F}_${j}_${i}_prec_3,${F}_${j}_${i}_prec_4,${F}_${j}_${i}_prec_5,${F}_${j}_${i}_prec_6,${F}_${j}_${i}_prec_7,${F}_${j}_${i}_prec_8,${F}_${j}_${i}_prec_9,${F}_${j}_${i}_prec_10,${F}_${j}_${i}_prec_11,${F}_${j}_${i}_prec_12)
case "$1" in
bio1)
# BIO1: Annual mean temperature of monthly period
MVAR=tmean
echo "(${F}_${j}_${i}_${MVAR}_1+${F}_${j}_${i}_${MVAR}_2+${F}_${j}_${i}_${MVAR}_3+${F}_${j}_${i}_${MVAR}_4+\
${F}_${j}_${i}_${MVAR}_5+${F}_${j}_${i}_${MVAR}_6+${F}_${j}_${i}_${MVAR}_7+${F}_${j}_${i}_${MVAR}_8+\
${F}_${j}_${i}_${MVAR}_9+${F}_${j}_${i}_${MVAR}_10+${F}_${j}_${i}_${MVAR}_11+${F}_${j}_${i}_${MVAR}_12)/12"
r.mapcalc "${F}_${j}_${i}_BIO1=(${F}_${j}_${i}_${MVAR}_1+${F}_${j}_${i}_${MVAR}_2+${F}_${j}_${i}_${MVAR}_3+${F}_${j}_${i}_${MVAR}_4+\
${F}_${j}_${i}_${MVAR}_5+${F}_${j}_${i}_${MVAR}_6+${F}_${j}_${i}_${MVAR}_7+${F}_${j}_${i}_${MVAR}_8+\
${F}_${j}_${i}_${MVAR}_9+${F}_${j}_${i}_${MVAR}_10+${F}_${j}_${i}_${MVAR}_11+${F}_${j}_${i}_${MVAR}_12)/12"
;;
bio2)
#BIO2: Mean diurnal range, i.e. mean(period tmax-tmin)
if g.list type=rast | grep ${F}_${j}_${i}_BIO2 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO2 exists!"
else
echo "(rg_${j}_${i}_1+rg_${j}_${i}_2+rg_${j}_${i}_3+rg_${j}_${i}_4+rg_${j}_${i}_5+rg_${j}_${i}_6+\
rg_${j}_${i}_7+rg_${j}_${i}_8+rg_${j}_${i}_9+rg_${j}_${i}_10+rg_${j}_${i}_11+rg_${j}_${i}_12)/12"
# if the rg_${j}_${i}_${m} exists, skip to generate layers
if g.list type=rast | grep rg_${j}_${i}_${m} 1>/dev/null ; then
echo "The rg_${j}_${i}_${m} (max temperature - min temp) exists! I will skip this step"
else
for (( m=1; m<=12; m++ )); do
# calculate the differences of Tmax and Tmin
r.mapcalc "rg_${j}_${i}_${m}=${F}_${j}_${i}_tmax_${m}-${F}_${j}_${i}_tmin_${m}"
done
fi
r.mapcalc "${F}_${j}_${i}_BIO2=(rg_${j}_${i}_1+rg_${j}_${i}_2+rg_${j}_${i}_3+rg_${j}_${i}_4+rg_${j}_${i}_5+rg_${j}_${i}_6+\
rg_${j}_${i}_7+rg_${j}_${i}_8+rg_${j}_${i}_9+rg_${j}_${i}_10+rg_${j}_${i}_11+rg_${j}_${i}_12)/12"
fi
;;
bio4)
for (( z=1; z<=12; z++ ))
do
r.mapcalc "x${z}=(${F}_${j}_${i}_tmean_${z}-${F}_${j}_${i}_BIO1+273.15)^2"
done
r.mapcalc "${F}_${j}_${i}_BIO4=sqrt((x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12)/12)-273.15"
g.remove rast=x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12
;;
bio5|bio6)
# BIO5 & BIO6
# Maximum temperature of Warmest Month & Minimum temperature of coldest month
if g.list type=rast | grep ${F}_${j}_${i}_BIO5 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO5 & 6 exists!"
else
echo "Calculating ${F}_${j}_${i}_BIO5 & ..._BIO6..."
r.mapcalc "${F}_${j}_${i}_BIO5=max($NTMAX)"
r.mapcalc "${F}_${j}_${i}_BIO6=min($NTMIN)"
fi
;;
bio7)
# BIO7: Temperature annual range = BIO5-BIO6
if g.list type=rast | grep ${F}_${j}_${i}_BIO5 1>/dev/null ; then
if g.list type=rast | grep ${F}_${j}_${i}_BIO7 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO7 exists!"
else
echo "Calculating ${F}_${j}_${i}_BIO7..."
r.mapcalc "${F}_${j}_${i}_BIO7=${F}_${j}_${i}_BIO5-${F}_${j}_${i}_BIO6"
fi
else
./calc_bio.sh bio5_6
echo "Calculating ${F}_${j}_${i}_BIO7..."
r.mapcalc "${F}_${j}_${i}_BIO7=${F}_${j}_${i}_BIO5-${F}_${j}_${i}_BIO6"
fi
;;
bio3)
# BIO3: the mean diurnal range (parameter 2) divided by the Annual Temperature Range (parameter 7).
if g.list type=rast | grep ${F}_${j}_${i}_BIO3 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO3 exists!"
else
echo "Calculating ${F}_${j}_${i}_BIO3 ..."
r.mapcalc "${F}_${j}_${i}_BIO3=${F}_${j}_${i}_BIO2/${F}_${j}_${i}_BIO7"
fi
;;
bio12)
# BIO12: Annual precipitation
if g.list type=rast | grep ${F}_${j}_${i}_BIO12 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO12 exists!"
else
sNPREC=`echo $NPREC | sed -e 's/,/+/g'`
echo "Calculating ${F}_${j}_${i}_BIO12 ..."
r.mapcalc "${F}_${j}_${i}_BIO12=(${sNPREC})"
fi
;;
bio13)
# BIO13: Precipitation of wettest period (depending on timestamp, week or month)
if g.list type=rast | grep ${F}_${j}_${i}_BIO13 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO13 exists!"
else
echo "Calculating ${F}_${j}_${i}_BIO13 ..."
r.mapcalc "${F}_${j}_${i}_BIO13=max(${NPREC})"
fi
;;
bio14)
# BIO14: Precipitation of driest period (dpending on timestamp, week or month)
if g.list type=rast | grep ${F}_${j}_${i}_BIO14 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO14 exists!"
else
echo "Calculating ${F}_${j}_${i}_BIO14 ..."
r.mapcalc "${F}_${j}_${i}_BIO14=min(${NPREC})"
fi
;;
bio15)
r.mapcalc "${F}_${j}_${i}_BIO12_1=${F}_${j}_${i}_BIO12/12"
for (( z=1; z<=12; z++ ))
do
r.mapcalc "x${z}=(${F}_${j}_${i}_prec_${z}-${F}_${j}_${i}_BIO12_1)^2"
done
r.mapcalc "${F}_${j}_${i}_BIO15=sqrt((x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12)/12)"
g.remove rast=x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12
;;
bio8|bio9|bio16|bio17)
# BIO8 & BIO16
if g.list type=rast | grep ${F}_${j}_${i}_BIO8 1>/dev/null ; then
echo "${F}_${j}_${i}_BIO8, 9, 16,17 exists!"
else
echo "Calculating ${F}_${j}_${i}_BIO8, 9,16 and 17 ..."
quart
wetquarter
# BIO8: Monthly mean temperature of wettest quarter (1)
echo "Calculating ${F}_${j}_${i}_BIO8: monthly mean temperature of wettest quarter..."
QWETARRAY=(qw1,qw2,qw3,qw4,qw5,qw6,qw7,qw8,qw9,qw10,qw11,qw12)
r.mapcalc "${F}_${j}_${i}_BIO8=if(max(${QWETARRAY})==-9999,null(),max(${QWETARRAY})/3)"
# BIO9: Monthly mean temperature of driest quarter (1)
echo "Calculating ${F}_${j}_${i}_BIO9: monthly mean temperature of driest quarter..."
QDRYARRAY=(qd1,qd2,qd3,qd4,qd5,qd6,qd7,qd8,qd9,qd10,qd11,qd12)
r.mapcalc "${F}_${j}_${i}_BIO9=if(max(${QDRYARRAY})==-9999,null(),max(${QDRYARRAY})/3)"
fi
;;
bio10|bio11|bio18|bio19)
quart
warmquarter
# BIO18: Precipitation of warmest quarter
echo "Calculating ${F}_${j}_${i}_BIO18: precipitation of warmest quarter"
QWARMARRAY=(qh1,qh2,qh3,qh4,qh5,qh6,qh7,qh8,qh9,qh10,qh11,qh12)
r.mapcalc "${F}_${j}_${i}_BIO18=if(max(${QWARMARRAY})==-9999,null(),max(${QWARMARRAY})/3)"
# BIO19: Precipitation of coldest quarter
echo "Calculating ${F}_${j}_${i}_BIO19: precipitation of coldest quarter"
QCOLDARRAY=(qc1,qc2,qc3,qc4,qc5,qc6,qc7,qc8,qc9,qc10,qc11,qc12)
r.mapcalc "${F}_${j}_${i}_BIO19=if(max(${QCOLDARRAY})==-9999,null(),max(${QCOLDARRAY})/3)"
;;
all)
./calc_bio.sh bio1
./calc_bio.sh bio2
./calc_bio.sh bio4
./calc_bio.sh bio5
./calc_bio.sh bio7
./calc_bio.sh bio3
./calc_bio.sh bio12
./calc_bio.sh bio13
./calc_bio.sh bio14
./calc_bio.sh bio15
./calc_bio.sh bio8
./calc_bio.sh bio10
;;
*)
echo "Use ./calc_bio.sh bio1-19 or all to calculate bioclim parameters"
echo "Example ./calc_bio.sh bio8"
exit
;;
esac
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment