dklinges9 / mcera5

mcera5
12 stars 9 forks source link

Error when using extract_clim: '...subexpression for [longitude] results in empty slice' #40

Closed Kasi21 closed 1 week ago

Kasi21 commented 1 week ago

Hello,

I downloaded the era5 data directly from the Copernicus store because I was getting issues when using the API (see this raised issue). However, when I try and use extract_clim, I get the below error:

_Error in hyperfilter.tidync(., longitude = longitude == long, latitude = latitude == : subexpression for [longitude] results in empty slice

I've attached my .nc file here and the code below. Is there anyway around this error or would I need to redownload the data using the API?

Thanks in advance, and all the best

Kas


library(mcera5)
library(dplyr)
library(ecmwfr)
library(ncdf4)
library(curl)
library(keyring)
library(abind)
library(lubridate)
library(tidync)
library(NicheMapR) # remotes::install_github("mrke/NicheMapR")
# library(microctools) # remotes::install_github("ilyamaclean/microctools")

# list the path of the .nc file for a given year
my_nc <- paste0("Data/era5/era5_2011.nc")

# for a single point (make sure it's within the bounds of your .nc file)
x <- 23.352
y <- -19.34

# temporal extent
st_time <- as.POSIXlt("2011-02-26 00:00", tz = "UTC")
en_time <- as.POSIXlt("2011-03-01 23:00", tz = "UTC")

# gather all hourly variables
clim_point <- extract_clim(nc = my_nc, long = x, lat = y,
                           start_time = st_time, end_time = en_time,
                           format = "NicheMapR")
dklinges9 commented 1 week ago

Hi Kas,

Thanks for bringing this to my attention. Yeah, I've faced this issue during internal testing, although usually have been able to get around it. But indeed I received the same error that you did, using your code and data. There's something a tad odd with the behavior of hyper_filter() (relative to, say, terra::extract() ) and I think I will submit an issue myself to tidync to try to figure this out.

But no worries for you. I've just pushed a change that should fix this by providing a bit more flexibility in how the netCDF is hyper_filter()'ed. I just re-installed mcera5 on my end and your code is now working for me. Go ahead and re-install mcera5 and give this a spin. Let me know if you face issues.

And I'll respond to your other issue shortly.

Kasi21 commented 1 week ago

Hi Dave,

That's great - thank you. I've just tested it, and it works great.

Working through the vignette, I just realised that there's a similar issue with the extract_precip() function. Would it be possible to also push a change for that?

Thanks, and all the best,

Kas

dklinges9 commented 5 days ago

Yep, also updated now too, thanks for pointing out. A quick note that I'm building towards deprecating extract_precip() and just building its functionality into extract_clim(), but for now it's still supported.

Cheers, Dave

Kasi21 commented 5 days ago

Perfect - thanks, Dave!

Much appreciated.

Cheers,

Kas