```{asis, echo = {{envs_ecoclimate_knit}}, eval = {{envs_ecoclimate_knit}}, include = {{envs_ecoclimate_knit}}}
### Obtain environmental data
Using Ecoclimate (http://www.ecoclimate.org)  dataset at resolution of 0.5 degrees.
```

```{r, echo = {{envs_ecoclimate_knit}}, include = {{envs_ecoclimate_knit}}}
# R code to get environmental data from Ecoclimate
envs_{{spAbr}} <- envs_ecoClimate(
  bcAOGCM = "{{bcAOGCM_rmd}}",
  bcScenario = "{{bcScenario_rmd}}",
  ecoClimSel = {{ecoClimSel_rmd}}) 
##Add envrionmental values to occurrences table
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}})
```
