Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to data (without downloading it) #69

Closed
frafra opened this issue Nov 22, 2023 · 1 comment · Fixed by #72
Closed

Access to data (without downloading it) #69

frafra opened this issue Nov 22, 2023 · 1 comment · Fixed by #72
Assignees
Labels
Practical guides Chapter practical guides

Comments

@frafra
Copy link

frafra commented Nov 22, 2023

GDAL supports accessing to data remotely without having to download it in advance.

Should we mention it and provide some code snippets on how to do it in Python/R?

Here is an example of using GDAL virtual file system to access a remote NetCDF file without downloading it:

url='NETCDF:/vsicurl/https://thredds.met.no/thredds/fileServer/senorge/seNorge_2018/Archive/seNorge2018_2006.nc'
data2006 = stars::read_stars(url, sub='rr', proxy=TRUE)
[...]
st_crs(data2006)
reg1_st <- reg1 %>% st_as_sf(.,
                             coords = c("longitude", "latitude"), 
                             crs=4326)

reg1_st <- st_transform(reg1_st, crs=st_crs(data2006))
reg1_st$rr <- st_extract(data2006, reg1_st, time_column="date")

This snippet uses R stars library.

Such a URL works with gdalinfo from command line and on a wide variety of software using GDAL.

Maybe Benjamin Cretois can help with that as well?

@vivigirardin vivigirardin added the Practical guides Chapter practical guides label Nov 22, 2023
@BenCretois
Copy link

A more reproducible code below:

# Create a dataset - assuming the coordinates are in CRS=4326
data <- data.frame(
  longitude = c(11.95501, 11.95501, 11.95498, 11.95493, 11.95487, 11.95497),
  latitude = c(65.67812, 65.67814, 65.67821, 65.67808, 65.67809, 65.67810),
  date = as.Date(rep("2006-03-26", 6))
)

# fetch the ncdf file for a specific year
  url=paste0('NETCDF:/vsicurl/https://thredds.met.no/thredds/fileServer/senorge/seNorge_2018/Archive/seNorge2018_', year,'.nc')
  
  # Read the netcdf file from the url
  netcdf_file=stars::read_stars(url, sub='rr', proxy=TRUE) 
  
  # Transform the dataset into an sf object
  data_st <- data %>% st_as_sf(.,
                               coords = c("longitude", "latitude"), 
                               crs=4326)
  
  # Reproject the CRS to match the CRS of the netcdf
  data_st <- st_transform(data_st, crs=st_crs(netcdf_file))
  data_st$rr <- st_extract(netcdf_file, data_st, time_column="date")

@vivigirardin vivigirardin self-assigned this Dec 4, 2023
@vivigirardin vivigirardin mentioned this issue Dec 4, 2023
8 tasks
vivigirardin added a commit that referenced this issue Jan 16, 2024
@vivigirardin vivigirardin linked a pull request Jan 17, 2024 that will close this issue
vivigirardin added a commit that referenced this issue Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Practical guides Chapter practical guides
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants