I’m trying to subset 4 nc-files to get a shorter time period. Each file contains 94 years, and I’d like to subset 20 years in each file. I’m importing the 4 files with list.files
(which is must I think since I will ultimately have even more files than 4, so I cannot import each file separately) and then converting them into one raster object. I think I should subset the shorter time period when the files are still contained as a list and before they are made into a raster object, since I’m unsure about what happens with the 4 nc-files are stacked into one raster object, i.e. how they are merged.
I can see the time dimension is stored, for each nc-file, as 94 years starting from 345 (=i.e. 2006 from the baseline 1661). But I cannot figure out how to subset the 4 files for a shorter period, e.g. 2006-2007, and then converting to a raster object.
#Open 4 files
leh_rcp6.0 = list.files("heatwave/rcp6.0",pattern='*.nc',full.names=TRUE)
[1] "heatwave/rcp6.0/lange2020_hwmid-humidex_gfdl-esm2m_ewembi_rcp60_nosoc_co2_leh_global_annual_2006_2099.nc4"
[2] "heatwave/rcp6.0/lange2020_hwmid-humidex_hadgem2-es_ewembi_rcp60_nosoc_co2_leh_global_annual_2006_2099.nc4"
[3] "heatwave/rcp6.0/lange2020_hwmid-humidex_ipsl-cm5a-lr_ewembi_rcp60_nosoc_co2_leh_global_annual_2006_2099.nc4"
[4] "heatwave/rcp6.0/lange2020_hwmid-humidex_miroc5_ewembi_rcp60_nosoc_co2_leh_global_annual_2006_2099.nc4" ```
# Combine files into Rasterstack object
leh_rcp6.0_stack <- stack(leh_rcp6.0)
Information for 1 nc-file to see how the dimensions are stored:
test <- nc_open("lange2020_hwmid-humidex_gfdl-esm2m_ewembi_rcp60_nosoc_co2_leh_global_annual_2006_2099.nc4")
(NC_FORMAT_NETCDF4_CLASSIC):
1 variables (excluding dimension variables):
float leh[lon,lat,time] (Chunking: [720,360,1]) (Compression: shuffle,level 5)
_FillValue: 1.00000002004088e+20
missing_value: 1.00000002004088e+20
standard_name: land_area_fraction_exposed_to_heatwave
long_name: Land area fraction exposed to heatwave
units: 1
3 dimensions:
lon Size:720
standard_name: longitude
units: degrees_east
axis: X
long_name: Longitude
lat Size:360
standard_name: latitude
units: degrees_north
axis: Y
long_name: Latitude
time Size:94 *** is unlimited ***
standard_name: time
units: years since 1661-1-1 00:00:00
calendar: 360_day
axis: T
long_name: Time```
time_test <- ncvar_get(test, "time")
head(time_test) # to have a look at the time dimension
[1] 345 346 347 348 349 350