How to subset NetCDF files with conditionals in one dimension in R

301 Views Asked by At

I have a NetCDF file of monthly temperatures that range from 1979 to 2019 with 3 dimensions (lon,lat,time).I dowloaded the file from this website: https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels-monthly-means?tab=form. I would like to create a function that allows me to pick all the grid cells with latitudes that are higher than 23 degrees, and for those grid cells just use for example the months December, January, February to calculate annual average. Any ideas will be really appreciated.

So far I just managed to perform the tasks separately but I am looking for more like:

mynetcdf <- 'lake.nc'
ncFile <- nc_open(mynetcdf)
lake_temp <- ncvar_get(ncFile,"p0001")
lon <- ncvar_get(ncFile,"longitude")
lat <- ncvar_get(ncFile,"latitude")
time <- ncvar_get)ncFile,"time"

I am looking for something like:

if (lat >23) { "set all values in lake_temp to NA except in the months December, January, February"}

So far I just managed to subset those months for each year but now I need to incorporate the condition explained above. Thank you so much!

0

There are 0 best solutions below