courtiol / IsoriX

This is the website dedicated to the development of the R package IsoriX
12 stars 5 forks source link

Message errors from "isoscape" #146

Closed lamartinelli closed 1 year ago

lamartinelli commented 1 year ago

Dear Courtiol et al.

First, thank you very much for making available "IsoriX". It is a very useful R package.

I am trying to build an isoscape using d18O data from Brazil generated by the GNIP network, but I am getting two errors as I try to run the "isoscape" function.

"in required variables from 'newdata'. Prediction not always possible. Error: Invalid grouping factor specification, source_ID"

Any help on this issue would be very much appreciated.

Best regards,

Luiz A Martinelli University of São Paulo Brazil

courtiol commented 1 year ago

Hi @lamartinelli, this is probably not the right place for this question (you should use the user mailing group instead: https://groups.google.com/g/isorix/)... unless this is a bug :)

I don't think this is a bug, but I do agree that I should have caught whatever you did wrong and displayed a more informative message, so let's keep discussing here (but perhaps do subscribe to the mailing list as well; it is free).

It would be good for me to be able to reproduce all the problems that you got... The best would be for you to send me a script that is quite small but reproduces the issue.

The easiest is perhaps that you share a dropbox folder with me, with the data in it. Send that to my email (given by running maintainer("IsoriX") in R).

I will make sure to sort you out quickly. ++

Alex

courtiol commented 1 year ago

Hi, I had a quick look.

The root of the problems is that your elevation raster does not know the elevation at some of the sampling locations:

> raster::extract(elev_BR, cbind(GNIPDataBRagg$long, GNIPDataBRagg$lat))
 [1]   15.72  796.07   66.67  511.36   67.12 1030.92  426.89  566.96  171.45      NA  173.08  199.07  320.08      NA
[15]  103.14   58.04   52.63  107.50   76.80  148.97  624.61      NA   46.69   12.18   87.66  444.98   70.16

This is probably because your elevation file is strange and restricted:

> elev_BR
class      : RasterLayer 
band       : 1  (of  2  bands)
dimensions : 4685, 5421, 25397385  (nrow, ncol, ncell)
resolution : 0.008333, 0.008333  (x, y)
extent     : -74.00537, -28.83218, -33.75698, 5.283122  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : MDE_Brasil.tif 
names      : MDE_Brasil

As you can see, it contains 2 "bands" and this is unusual... It also contains NAN at the border of Brazil where some sampling locations are situated.

So here are 2 possible solutions depending on the version of IsoriX you want to use:

Solution 1

For any version of IsoriX, you can/should use `prepraster() which aggregates and recodes the elevation raster properly. Then replace the missing values by 0 (the oceans and countries outside Brazil, mostly).

elev_BR <- raster::raster("MDE_Brasil.tif")
elev_BR_small <- prepraster(raster = elev_BR, isofit = BRFit, aggregation_factor = 10)
raster::values(elev_BR_small)[raster::values(elev_BR_small) == "NaN"] <- 0
levelplot(elev_BR_small,
          margin = FALSE,
          main = "Brazil")

BRIsoscape <- isoscape(raster = elev_BR_small, isofit = BRFit)
plot(BRIsoscape, palette = list(range = c(-7, 0)))

Solution 2

The new IsoriX devel version now download elevation raster using a package via getelev(), so you can directly download the elevation for Brazil and around. Since you can aggregate before download, the downloaded file can be quite small.

getelev(filename = "MDE_Brasil2.tif", long = c(-75, -32), lat = c(-32, 4.5), z = 3, overwrite = TRUE)
elev_BR2 <- raster::raster("MDE_Brasil2.tif")
BRIsoscape2 <- isoscape(raster = elev_BR2, isofit = BRFit)
plot(BRIsoscape2, palette = list(range = c(-7, 0)))

The map produced by both solution are similar for Brazil, but differ for the other countries around. This is because solution 2 leads to non 0 elevations in countries outside Brazil. I constrained the range of the y axis on both plots to make this clear.

I general, do not attempt to build an isoscape based on an elevation raster with a crazy high resolution. Start with something coarse that produces speedy results and is easier to debug, then aggregate less, but going way below the size of one screen pixel is probably an overkill and requires a beefy computer.

I let you close this issue if everything is clear and running on your side. Best, Alex ++

lamartinelli commented 1 year ago

Hi Alex,

Thank you so much for your help!!!

I will try to follow your advice and let you know the outputs!

All my best,

Luiz

Em dom., 28 de ago. de 2022 às 09:40, Alexandre Courtiol < @.***> escreveu:

Hi, I had a quick look.

The root of the problems is that your elevation raster does not know the elevation at some of the sampling locations:

raster::extract(elev_BR, cbind(GNIPDataBRagg$long, GNIPDataBRagg$lat)) [1] 15.72 796.07 66.67 511.36 67.12 1030.92 426.89 566.96 171.45 NA 173.08 199.07 320.08 NA [15] 103.14 58.04 52.63 107.50 76.80 148.97 624.61 NA 46.69 12.18 87.66 444.98 70.16

This is probably because your elevation file is strange and restricted:

elev_BRclass : RasterLayer band : 1 (of 2 bands)dimensions : 4685, 5421, 25397385 (nrow, ncol, ncell)resolution : 0.008333, 0.008333 (x, y)extent : -74.00537, -28.83218, -33.75698, 5.283122 (xmin, xmax, ymin, ymax)crs : +proj=longlat +datum=WGS84 +no_defs source : MDE_Brasil.tif names : MDE_Brasil

As you can see, it contains 2 "bands" and this is unusual... It also contains NAN at the border of Brazil where some sampling locations are situated.

So here are 2 possible solutions depending on the version of IsoriX you want to use: Solution 1

For any version of IsoriX, you can/should use `prepraster() which aggregates and recodes the elevation raster properly. Then replace the missing values by 0 (the oceans and countries outside Brazil, mostly).

elev_BR <- raster::raster("MDE_Brasil.tif")elev_BR_small <- prepraster(raster = elev_BR, isofit = BRFit, aggregation_factor = 10)raster::values(elev_BR_small)[raster::values(elev_BR_small) == "NaN"] <- 0 levelplot(elev_BR_small, margin = FALSE, main = "Brazil") BRIsoscape <- isoscape(raster = elev_BR_small, isofit = BRFit) plot(BRIsoscape, palette = list(range = c(-7, 0)))

Solution 2

The new IsoriX devel version now download elevation raster using a package via getelev(), so you can directly download the elevation for Brazil and around. Since you can aggregate before download, the downloaded file can be quite small.

getelev(filename = "MDE_Brasil2.tif", long = c(-75, -32), lat = c(-32, 4.5), z = 3, overwrite = TRUE)elev_BR2 <- raster::raster("MDE_Brasil2.tif")BRIsoscape2 <- isoscape(raster = elev_BR2, isofit = BRFit) plot(BRIsoscape2, palette = list(range = c(-7, 0)))

The map produced by both solution are similar for Brazil, but differ for the other countries around. This is because solution 2 leads to non 0 elevations in countries outside Brazil. I constrained the range of the y axis on both plots to make this clear.

I general, do not attempt to build an isoscape based on an elevation raster with a crazy high resolution. Start with something coarse that produces speedy results and is easier to debug, then aggregate less, but going way below the size of one screen pixel is probably an overkill and requires a beefy computer.

I let you close this issue if everything is clear and running on your side. Best, Alex ++

— Reply to this email directly, view it on GitHub https://github.com/courtiol/IsoriX/issues/146#issuecomment-1229449053, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJHPEWYYB4DKEYDWHP6LFZ3V3NM4NANCNFSM57XBHN2A . You are receiving this because you were mentioned.Message ID: @.***>

courtiol commented 1 year ago

Solved.