bioclim-aml

/* MkBCvars.AML
/* /*
/* Author Robert Hijmans
/* January 2006
/* Museum of Vertebrate Zoology, UC Berkeley
/* rhijmans@ucdavis.edu
/*
/* Version 2.3
/*
/* This AML creates the 19 BIOCLIM variables from
/* monthly Tmin, Tmax, and Precipitation grids
/* The results are rounded where integers would become reals
/* (I assume that input values were multiplied by 10
/* and stored as Integers to begin with)
/* P2 is first multiplied by 10
/* CVs are first multiplied by 100.
/*
/* rounding of "x" is done with "int(floor(x + 0.5))"
/* because "int(x+0.5)" as suggested by ESRI (see INT in Arc Help), does not
/* round negative numbers correctly (-2.6 -> -2 intstead of -3)
/*
/* You must change the first four lines (input files and output directory)
/* If you do not have average temperature, create it with the lines that follow
/*
/* Also note that the AML removes some temporary grids if they exist
/* (the first "&do i = 0 &to 15" bit)
/* Please make sure that you do not have files
/* with those names that you want to keep.
/*
/* 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
/* 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
/*
/*
/* Temperature data is in units of °C * 10 because that allows me to store the data as Integer values,
/* (with 0.1 °C precision) which is more efficient than storing the data as Real values.
/* However, you will want to report the data in °C. Precipitation data is in mm.
/*
/*

&sv scen &list ab a2 b1
&sv models &list mi in gi cs cs bc
&sv years &list 10 30 50 70 90

grid

&s inbase = ../mdg/

&do sc %scen%
&do mod %models%
&do y %years%
&sv tn = %sc%%mod%%tn%%y%
&sv tx = %sc%%mod%%tx%%y%
&sv ta = %sc%%mod%%ta%%y%
&sv pr = %sc%%mod%%pr%%y%
&call BIOCLIM
&end
&end
&end

q

&return

&routine BIOCLIM

&s program [locase [show program]]
&if %program% ^= grid &then grid

/* &sv tn = tmin\tmin_
/* &sv tx = tmax\tmax_
/* &sv ta = tmean\tmean_
/* &sv pt = prec\prec_

/* if TAVG does not exist.....
&do j = 1 &to 12
&if [EXISTS %ta%%j% -grid] &then &type %ta%%j%
&else %ta%%j% = (%tn%%j% + %tx%%j%) / 2
&end

&do i = 0 &to 20
/* &if [exists BIO%i% -grid] &then kill P%i%
/* &if [exists P%i% -grid] &then kill P%i%
/* &if [exists tmp%i% -grid] &then kill tmp%i%
/* &if [exists x%i% -grid] &then kill x%i%
/* &if [exists q%i% -grid] &then kill q%i%
/* &if [exists t%i% -grid] &then kill t%i%
/* &if [exists mnt%i% -grid] &then kill mnt%i%
/* &if [exists dry%i% -grid] &then kill dry%i%
/* &if [exists wet%i% -grid] &then kill wet%i%
/* &if [exists hot%i% -grid] &then kill hot%i%
/* &if [exists cld%i% -grid] &then kill cld%i%
/* &if [exists x%i% -grid] &then kill x%i%
/* &if [exists y%i% -grid] &then kill y%i%
/* &if [exists rg%i% -grid] &then kill rg%i%
&end

&if [exists drym -grid] &then kill drym
&if [exists wetm -grid] &then kill wetm

&sv TAvar = %ta%1
&sv TXvar = %tx%1
&sv TNvar = %tn%1
&sv PTvar = %pt%1

&do j = 2 &to 12
&sv tavar = %tavar%,%ta%%j%
&sv txvar = %txvar%,%tx%%j%
&sv tnvar = %tnvar%,%tn%%j%
&sv ptvar = %ptvar%,%pt%%j%
&end

/* P1. Annual Mean Temperature
&if [exists p1 -grid] &then &type P1 exists
&else
&do
P1 = int(floor(mean(%tavar%) + 0.5))
&type P1 done
&end

/* P4. Temperature Seasonality (standard deviation)
&if [exists p4 -grid] &then &type P4 exists
&else
&do
P4 = int(floor(100 * std(%tavar%) + 0.5))
&type P4 done
&end

/* P5. Max Temperature of Warmest Period
&if [exists p5 -grid] &then &type P5 exists
&else
&do
P5 = max(%txvar%)
&type P5 done
&end

/* P6. Min Temperature of Coldest Period
&if [exists p6 -grid] &then &type P6 exists
&else
&do
P6 = min(%tnvar%)
&type P6 done
&end

/* P7. Temperature Annual Range (P5-P6)
&if [exists p7 -grid] &then &type P7 exists
&else
&do
P7 = P5 - P6
&type P7 done
&end

/* P12. Annual Precipitation
&if [exists p12 -grid] &then &type P12 exists
&else
&do
P12 = sum(%ptvar%)
&type P12 done
&end

/* P13. Precipitation of Wettest Period
&if [exists p13 -grid] &then &type P13 exists
&else
&do
P13 = max(%ptvar%)
&type P13 done
&end

/* P14. Precipitation of Driest Period
&if [exists p14 -grid] &then &type P14 exists
&else
&do
P14 = min(%ptvar%)
&type P14 done
&end

/* P15. Precipitation Seasonality(Coefficient of Variation)
/* the "1 +" is to avoid strange CVs for areas where mean rainfaill is < 1)
&if [exists p15 -grid] &then &type P15 exists
&else
&do
P15 = int(floor(100 * std(%ptvar%) / (1 + P12 / 12) + 0.5))
&type P15 done
&end

&do i = 1 &to 12
&if [exists rg%i% -grid] &then &type rg%i% exists
&else rg%i% = %tx%%i% - %tn%%i%
&end

/* P2. Mean Diurnal Range(Mean(period max-min))
&if [exists p2 -grid] &then &type P2 exists
&else
&do
P2 = int(floor(mean(rg1,rg2,rg3,rg4,rg5,rg6,rg7,rg8,rg9,rg10,rg11,rg12) + 0.5))
&type P2 done
&end

/* P3. Isothermality (P2 / P7)
&if [exists p3 -grid] &then &type P3 exists
&else
&do
P3 = int(floor(100 * P2 / P7) + 0.5)
&type P3 done
&end

&do i = 1 &to 12
kill rg%i%
&end

&do i = 1 &to 12
&sv j = %i%
&sv k = [calc %i% + 1]
&sv l = [calc %i% + 2]
&if %k% > 12 &then &sv k = [calc %k% - 12]
&if %l% > 12 &then &sv l = [calc %l% - 12]
q%i% = %pt%%j% + %pt%%k% + %pt%%l%
t%i% = %ta%%j% + %ta%%k% + %ta%%l%
&end

mnt0 = con(isnull(q1),0,100)
mnt1 = setnull(mnt0 < 1, 1)
wet1 = q1

&do i = 1 &to 11
&sv j = [calc %i% + 1]
/* &type i = %i% and j = %j%
mnt%j% = con(q%j% > wet%i%, [calc %j%], mnt%i%)
wet%j% = con(q%j% > wet%i%, q%j%, wet%i%)
&end
wetm = mnt12

/* P16. Precipitation of Wettest Quarter
&if [exists p16 -grid] &then &type P16 exists
&else
&do
P16 = wet12
&type P16 done
&end

&do i = 1 &to 12
kill mnt%i%
kill wet%i%
&end

mnt1 = setnull(mnt0 < 1, 1)
dry1 = q1

&do i = 1 &to 11
&sv j = [calc %i% + 1]
mnt%j% = con(q%j% < dry%i%, [calc %j%], mnt%i%)
dry%j% = con(q%j% < dry%i%, q%j%, dry%i%)
&end
drym = mnt12

/* P17. Precipitation of Driest Quarter
&if [exists p17 -grid] &then &type P17 exists
&else
&do
P17 = dry12
&type P17 done
&end

&do i = 1 &to 12
kill mnt%i%
kill dry%i%
&end
kill mnt0

&do i = 1 &to 12
x%i% = con(wetm == %i%, t%i%, -9999)
y%i% = con(drym == %i%, t%i%, -9999)
&end

/* tmp1 = max(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)
/* tmp2 = tmp1 / 3
/*P8 = int(floor(tmp2 + 0.5))

/* P8. Mean Temperature of Wettest Quarter
&if [exists p8 -grid] &then &type P8 exists
&else
&do
P8 = int(floor(max(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12) / 3 + 0.5))
&type P8 done
&end

/* tmp3 = max(y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12)
/* tmp4 = tmp3 / 3
/* P9 = int(floor(tmp4 + 0.5))

/* P9. Mean Temperature of Driest Quarter
P9 = int(floor(max(y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12) / 3 + 0.5))
&type P9 done

&do i = 1 &to 12
kill x%i%
kill y%i%
&end
&do i = 1 &to 4
&if [exists tmp%i% -grid] &then kill tmp%i%
&end

mnt0 = con(isnull(t1),0,100)
mnt1 = setnull(mnt0 < 1, 1)
hot1 = t1
&do i = 1 &to 11
&sv j = [calc %i% + 1]
mnt%j% = con(t%j% > hot%i%, [calc %j%], mnt%i%)
hot%j% = con(t%j% > hot%i%, t%j%, hot%i%)
&end
hotm = mnt12

/* P10 Mean Temperature of Warmest Quarter
&if [exists p10 -grid] &then &type P10 exists
&else
&do
P10 = int(floor(hot12 / 3 + 0.5))
&type P10 done
&end

&do i = 1 &to 12
kill mnt%i%
kill hot%i%
&end

mnt1 = setnull(mnt0 < 1, 1)
cld1 = t1

&do i = 1 &to 11
&sv j = [calc %i% + 1]
mnt%j% = con(t%j% < cld%i%, [calc %j%], mnt%i%)
cld%j% = con(t%j% < cld%i%, t%j%, cld%i%)
&end
cldm = mnt12

/* P11 Mean Temperature of Coldest Quarter
&if [exists p11 -grid] &then &type P11 exists
&else
&do
P11 = int(floor(cld12 / 3 + 0.5))
&type P11 done
&end

&do i = 1 &to 12
kill mnt%i%
kill cld%i%
&end
kill mnt0

&do i = 1 &to 12
x%i% = con(hotm == %i%, q%i%, -9999)
y%i% = con(cldm == %i%, q%i%, -9999)
&end

tmp1 = max(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)

/* P18. Precipitation of Warmest Quarter
&if [exists p18 -grid] &then &type P18 exists
&else
&do
P18 = int(floor(tmp1 + 0.5))
&type P18 done
&end

tmp2 = max(y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12)

/* P19. Precipitation of Coldest Quarter
&if [exists p19 -grid] &then &type P19 exists
&else
&do
P19 = int(floor(tmp2 + 0.5))
&type P19 done
&end

&do i = 1 &to 12
kill x%i%
kill y%i%
&end

kill hotm
kill cldm
kill drym
kill wetm

kill tmp1
kill tmp2

&do i = 1 &to 12
kill q%i%
kill t%i%
&end

&do i = 1 &to 19
rename p%i% bio_%i%
&end
&type Done!
&return