EvolEcolGroup / pastclim

An R package providing helper functions to interact with timeseries of worldwide climate reconstructions
https://evolecolgroup.github.io/pastclim/
Creative Commons Attribution 4.0 International
35 stars 3 forks source link

biome value is float when using nn_interpol #31

Closed jlapaijmans closed 1 year ago

jlapaijmans commented 1 year ago

Observed behaviour: biome becomes a float when nn_interpol is used on samples for which no climate is available

Example: floating_biomes.csv

library(pastclim)

# read in data
wolve <- read.csv("./floating_biomes.csv", header = T, sep = ",")

# extract climate data for sample locations
climate_locs <- location_slice(x=wolve[,c("latitude","longitude")], time_bp=0, bio_variables="biome", dataset="Beyer2020")
head(climate_locs)
  latitude longitude time_bp time_bp_slice    biome
1 64.48000 -162.1200       0             0 18.66667
2 64.48000 -162.1200       0             0 18.66667
3 64.48000 -162.1200       0             0 18.66667
4 64.48000 -162.1200       0             0 18.66667
5 59.60000   26.2000       0             0  7.75000
6 70.00499 -144.7539       0             0 22.33333

When nn_interpol is disabled, NA is returned:

climate_locs_no_interpol <- location_slice(x=wolve[,c("latitude","longitude")], time_bp=0, bio_variables="biome", dataset="Beyer2020", nn_interpol = FALSE)
head(climate_locs_no_interpol)
  latitude longitude time_bp time_bp_slice biome
1 64.48000 -162.1200       0             0    NA
2 64.48000 -162.1200       0             0    NA
3 64.48000 -162.1200       0             0    NA
4 64.48000 -162.1200       0             0    NA
5 59.60000   26.2000       0             0    NA
6 70.00499 -144.7539       0             0    NA

Problem appears to be with samples in the sea rather than samples that are under ice although this may be coincidental: image

# plot map with database points, biome, present day
climate_present <- region_slice( time_bp = 0, bio_variables = c("biome"), dataset = "Beyer2020", ext=c(-180,180,30,85))

# extract ice masks and convert to polygons
ice_mask <- get_ice_mask(0, dataset = "Beyer2020")
ice_mask_vect <- as.polygons(ice_mask)

# extract locations from database
locations_vect <- vect(wolve, geom=c("longitude", "latitude"))

# plot
terra::plot(climate_present$biome, 
fun=function() {
points(locations_vect, pch=21, col="black", bg="red", lwd=0.5, cex=1) 
polys(ice_mask_vect, col="grey", lwd=0.5)})
dramanica commented 1 year ago

With the patch mentioned above, I get:

> head(climate_locs)
   ID latitude longitude time_bp time_bp_slice biome
1 361 64.48000 -162.1200       0             0    23
2 374 64.48000 -162.1200       0             0    23
3 443 64.48000 -162.1200       0             0    23
4 445 64.48000 -162.1200       0             0    23
5 871 59.60000   26.2000       0             0     8
6 942 70.00499 -144.7539       0             0    22

I checked a couple by hand, and they look correct.