bcgov / climr

An R package for downscaling monthly climate data for North America
https://bcgov.github.io/climr/
16 stars 6 forks source link

NAs returned for Haida Gwaii in observed 2001_2020 period. #151

Closed cmahony closed 10 months ago

cmahony commented 11 months ago

@kdaust when i run climr for 1961-1990 observed it returns all locations. when i run it for 2001-2020 observed it returns NaN for most of Haida Gwaii and a small corner of NW BC. i haven't tried it for GCMs. perhaps this is the root of the Haida Gwaii enigma in the CCISS tool?? this problem can be replicated by running the CCISS_Spatial_exports.R file down to line 359 (pushed to main in CCISS_ShinyApp).

2001-2020 hist bgc predictions hist

1961-1990 bgc predictions ref

kdaust commented 11 months ago

Nice catch Colin! I'll investigate

CeresBarros commented 11 months ago

@cmahony if we always expect NAs to be in the same places between the normals period and the historic 2001-2020, this should become a test.

cmahony commented 11 months ago

@cmahony if we always expect NAs to be in the same places between the normals period and the historic 2001-2020, this should become a test.

@CeresBarros sounds good. NAs should be in the same places across all periods (GCMs too). also, i got one NA in the reference period downscaling and we should investigate that too.

CeresBarros commented 11 months ago

@cmahony can you whip up a reprex that gets all runs/periods/GCMs/etc into a/several SpatRaster/s that i can then use to make the test?

cmahony commented 11 months ago

@cmahony can you whip up a reprex that gets all runs/periods/GCMs/etc into a/several SpatRaster/s that i can then use to make the test?

@CeresBarros does this do the trick? @kdaust there are a few problems that show up with this example:

#reproducible example of basic downscaling at North america scale

library(terra)
library(climr)

# create the input DEM
dem <- rast("tests/GTOPO30.tif") #30-arcsecond DEM of north america. pushed to "tests" folder on climr main
dem <- aggregate(dem, fact=4) #upsample to 2-arcminute

## make the climr input file
points <- as.data.frame(dem, cells=T, xy=T)
colnames(points) <- c("id", "x", "y", "el")
points <- points[,c(2,3,4,1)] #restructure for climr input
str(points)

#climr outputs for reference period, recent, and one GCM simulation
var <- "Tmax01"
clim <- climr_downscale(points,
                        which_normal = "auto",
                        gcm_models = list_gcm()[1],
                        ssp = list_ssp()[2],
                        gcm_period = list_gcm_period()[2],
                        max_run = 1L,
                        historic_period = "2001_2020",
                        return_normal = TRUE,
                        vars = var
)

# compare extent of downscaled values for each of the layers
X <- dem
par(mfrow=c(2,2), mar=c(0,0,0,0))
values(X) <- NA
X[clim[PERIOD=="1961_1990",ID]] <- clim[PERIOD=="1961_1990",get(var)]
plot(X, legend=F,  axes=F, box=T, main="1961_1990")
values(X) <- NA
X[clim[PERIOD=="1961_1990",ID]] <- clim[PERIOD=="2001_2020",get(var)]
plot(X, legend=F,  axes=F, box=T, main="2001_2020")
values(X) <- NA
X[clim[RUN=="ensembleMean",ID]] <- clim[RUN=="ensembleMean",get(var)]
plot(X, legend=F,  axes=F, box=T, main="ensembleMean")
values(X) <- NA
X[clim[RUN=="r10i1p1f1",ID]] <- clim[RUN=="r10i1p1f1",get(var)]
plot(X, legend=F,  axes=F, box=T, main="r10i1p1f1")

Rplot

CeresBarros commented 11 months ago

thanks @cmahony , I'll have a look tomorrow. Quick thought about caching: are you using the latest commit on devl? @kdaust made a recent change that may (or not help).

cmahony commented 11 months ago

Quick thought about caching: are you using the latest commit on devl?

@CeresBarros, no. i'd need you to show me how.

CeresBarros commented 11 months ago

for future reference: at the top of your script you can add:

remotes::install_github("bcgov/climr@devl")
library(climr)

Make sure you do this from a fresh R session.

cmahony commented 10 months ago

@kdaust @CeresBarros this haida gwaii bug is still there in the new version of climr. My guess is that it is a problem with the anomaly surface being used to generate the 2001-2020 normals. i'll provide new anomaly surfaces.

cmahony commented 10 months ago

i created a set of anomaly surfaces relative to 1961-1990 and moved all anomaly surfaces to //objectstore2.nrs.bcgov/ffec/TransferAnomalies/ (these anomalies are using the CRU TS v4.05 for temperature and GPCC v2020 for precipitation). next step is for @kdaust to update the calculation of 2001-2020 normals based on these new anomalies. if easy enough, it would also be nice to have some of the other time periods added to climr as well (e.g., 1951-1980, 1981-2010, 2011-2020, etc).

cmahony commented 10 months ago

@kdaust added the new anomaly surface to climr and it fixed the issue.