There are two ways to get this done:
1- you can select the specific domain of interest when downloading the files. This is highly preferred when you have to download a large number of variables and years. ERA5 netCDF are offered in two different formats: either at hourly time steps, or at monthly averages (see this). If you need a sub-monthly time steps the global files for many variables for long time periods are extremely heavy in terms of disk space. You could set up a python script to download the data selecting only the domain by following a procedure like the one suggested in this nice post by Reto Stauffer (of course there are other ways to do this).
2- you can download the global netCDF of the variable and year under consideration ad use a software to extract your domain of interest.
One of the most efficient systems to do that is using the Climate Data Operator CDO. you could use the sellonlatbox
with a command like
cdo sellonlatbox,LON1,LON2,LAT1,LAT2 Input_file_Name.nc Output_file_Name.nc
Or you could use R
, as for your case, doing something like:
library(raster)
library(rgdal)
#load your area of interest
setwd("path_domain_shapefile")
domain_shp<-readOGR("shapefile.shp")
#read netCDF
setwd("path_to_ERA5_files_folder")
pr_data<-stack("ERA5_file.nc")
#extract the data for your area of interest
pr_data_domain<-extract(pr_data, domain_shp)
#then you can transform this in a data frame and write it as a csv
As reference for basic operations with spatial data in R
, I would suggest this introduction guide.
Hope this helps.