Having accessed the data through the cds.climate.copernicus website, I have tried working with the daily GRIB and NetCDF versions of layers 1 to 4 (subsetted to East Africa) but have not been successful. I am using the raster package in R to import the GRIB file as a raster stack. I can plot the layers, but can not perform processing on the full set. For example, averaging the first 10 layers works, but then trying to average over the month returns the error below.
The NetCDF version only imports into R or QGIS as a single layer.

"ERROR: Ran out of file reading SECT0 There were 82 trailing bytes in the file."

Has anyone come across this error before and have a solution using either R or python?

Alternatively, is there another portal where I can access the data? Perhaps an alternative source will work with my analysis pipeline (provided below).

Or, do you have a python or R script to convert a GRIB file into a series of named tiff files or NetCDF?


Thank you.


R script

library(raster)

#Data extracted for 9am #Also tried into memory with brick()
ERAsta <- stack('adaptor.mars.internal-1588079683.5006306-18401-19-0fdb2266-9a76-49ae-9ada-234b821f89a5.grib')

dates <- seq(as.Date("2016-01-01"), as.Date("2017-12-31"), by = "day")
month <- paste(substr(dates, 1,4), substr(dates, 6,7), "01", sep = "-")
monthsExtract <- unique(month)

for(i in 10:length(monthsExtract)){
endMonth <- (seq(as.Date(monthsExtract[i]),length=2,by="months")-1)[2]

ERAmean <- mean(ERAsta[[which(dates == monthsExtract[i]):which(dates == endMonth)]], na.rm = T)
ERAcv <- cv(ERAsta[[which(dates == monthsExtract[i]):which(dates == endMonth)]], na.rm = T)

Y <- substr(dates[i], 1, 4)
M <- substr(dates[i], 6, 7)

writeRaster(ERAmean, paste0('ERA_EA_mean_', Y, M, ".tif"), overwrite = T)
writeRaster(ERAcv, paste0('ERA_EA_cv___', Y, M, ".tif"), overwrite = T)

}



Download script

import cdsapi
c = cdsapi.Client()
c.retrieve(
    'reanalysis-era5-land',
    {
        'variable': 'volumetric_soil_water_layer_1', #'volumetric_soil_water_layer_1', 'volumetric_soil_water_layer_2', 'volumetric_soil_water_layer_3',
            #'volumetric_soil_water_layer_4',
        'year': [
            '2016', '2017',
        ],
        'month': [
            '01', '02', '03',
            '04', '05', '06',
            '07', '08', '09',
            '10', '11', '12',
        ],
        'day': [
            '01', '02', '03',
            '04', '05', '06',
            '07', '08', '09',
            '10', '11', '12',
            '13', '14', '15',
            '16', '17', '18',
            '19', '20', '21',
            '22', '23', '24',
            '25', '26', '27',
            '28', '29', '30',
            '31',
        ],
        'time': '09:00',
        'format': 'grib',
        'area': [
            15, 28.46, -9.8,
            51.76,
        ],
    },
    'download.grib')