```{asis, echo = {{envs_worldclim_knit}}, eval = {{envs_worldclim_knit}}, include = {{envs_worldclim_knit}}}
### Obtain environmental data
Using WorldClim (http://www.worldclim.org/) bioclimatic dataset at resolution of `r {{wcRes_rmd}}` arcmin.
```

```{r, echo = {{envs_worldclim_knit}}, include = {{envs_worldclim_knit}}}
# Download environmental data 
envs_{{spAbr}} <- envs_worldclim(
  bcRes = {{wcRes_rmd}}, 
  bcSel = {{bcSel_rmd}}, 
  mapCntr = {{mapCntr_rmd}}, # Mandatory for 30 arcsec resolution   
  doBrick = {{wcBrick_rmd}})
occs_xy_{{spAbr}} <- occs_{{spAbr}}[c('longitude', 'latitude')]
occs_vals_{{spAbr}} <- as.data.frame(raster::extract(envs_{{spAbr}}, occs_xy_{{spAbr}}, cellnumbers = TRUE))
# Remove duplicated same cell values
occs_{{spAbr}} <- occs_{{spAbr}}[!duplicated(occs_vals_{{spAbr}}[, 1]), ]
occs_vals_{{spAbr}} <- occs_vals_{{spAbr}}[!duplicated(occs_vals_{{spAbr}}[, 1]), -1]
# remove occurrence records with NA environmental values
occs_{{spAbr}} <- occs_{{spAbr}}[!(rowSums(is.na(occs_vals_{{spAbr}})) >= 1), ]
# also remove variable value rows with NA environmental values
occs_vals_{{spAbr}} <- na.omit(occs_vals_{{spAbr}})
# add columns for env variable values for each occurrence record
occs_{{spAbr}} <- cbind(occs_{{spAbr}}, occs_vals_{{spAbr}})
```
