9.3 Appendix C: Processing Climate Data
9.3.1 Cutting CHELSA v.1.2.1 to CA Domain
The CHELSA v1.2.1 data is a global dataset. The data can be cut to the Central Asia domain in the following way.
<- '/Users/tobiassiegfried/Dropbox (hydrosolutions)/1_HSOL_PROJECTS/PROJECTS/SDC/DKU_WRM_COURSE_CA/CourseMaterials/Data/CLIMATE/CHELSA_V1_2_1/'
dir_CHELSA
# First, we process precipitation data (prec).
<- list.files(paste0(dir_CHELSA,'tp/'))
prec_CHELSA_files for (idx in 1:length(prec_CHELSA_files)){
<- paste0('Processing PREC: ', idx, ' out of ', length(prec_CHELSA_files) )
str2Print print(str2Print)
<- prec_CHELSA_files[idx]
fName <- raster(paste0(dir_CHELSA,'prec/',fName))
global_CHELSA_raster <- raster::crop(global_CHELSA_raster,aoi_CentralAsia_LatLon)
centralAsia_CHELSA_raster # Save the centralAsia_CHELSA_raster
<- paste0(substr(fName,1,19),'_CA',substr(fName,20,30))
fName2Save ::writeRaster(centralAsia_CHELSA_raster,
rasterpaste0(dir_CHELSA,'prec/',fName2Save),
'GTiff',
overwrite = TRUE)
}
# Second, we process mean temperature data (tmean).
<- list.files(paste0(dir_CHELSA,'t2m/'))
tmean_CHELSA_files for (idx in 1:length(tmean_CHELSA_files)){
<- paste0('Processing TMEAN: ', idx, ' out of ', length(tmean_CHELSA_files) )
str2Print print(str2Print)
<- tmean_CHELSA_files[idx]
fName <- raster(paste0(dir_CHELSA,'tmean/',fName))
global_CHELSA_raster <- raster::crop(global_CHELSA_raster,aoi_CentralAsia_LatLon)
centralAsia_CHELSA_raster # Save the centralAsia_CHELSA_raster
<- paste0(substr(fName,1,20),'_CA',substr(fName,21,35))
fName2Save ::writeRaster(centralAsia_CHELSA_raster,
rasterpaste0(dir_CHELSA,'tmean/',fName2Save),
'GTiff',
overwrite = TRUE)
}
raster(paste0(dir_CHELSA,'tmean/',fName2Save))/10 - 273.15) %>% plot() (
9.3.2 Bias Correcting CHELSA v1.2.1 Precipitation Data for Snow Undercatch
## general info - function arguments
<- 'Gunt'
basinName <- 'tp'
dataType_ERA5 #basinShape <- 'to be passed in'
<- "+init=epsg:32642"
targetCRS ## Directories of relevant climate files - function arguments
<- '/Users/tobiassiegfried/Dropbox (hydrosolutions)/1_HSOL_PROJECTS/PROJECTS/SDC/DKU_WRM_COURSE_CA/CourseMaterials/Data/CLIMATE/CHELSA_V1_2_1/'
dir_CHELSA # Basin AOI - function arguments, but check if all can be derived from the basinShape that is anyhow to be passed in!
<- gunt_Shapefile_LatLon %>% extent # GUNT
aoi_Basin_LatLon # ===
<- '/Users/tobiassiegfried/Dropbox (hydrosolutions)/1_HSOL_PROJECTS/PROJECTS/SDC/DKU_WRM_COURSE_CA/CourseMaterials/Data/CLIMATE/PBCOR_V1/CHELSA_V12.nc'
fileCorrFact # Get CHELSA file list - Note, we already have cut to the CA domain!
<- list.files(paste0(dir_CHELSA,dataType_ERA5))
fileList # beginning and end
<- 1981
startY <- 2013
endY # Monthly correction factors (Beck et al., 2020)
#pbcorr_monthly <- nc_open(dir_CorrFact)
<- brick(fileCorrFact, varname="corr_fac_monthly")
pbcorr_monthly <- raster::crop(pbcorr_monthly,aoi_CentralAsia_LatLon)
pbcorr_monthly_basin_longlat # start to loop through years and months for bias correcting the monthly values.
for (yr in startY:endY){
for (mon in 1:12){
# Load corresponding tmean CHELSA Central Asia File
if (mon<10){
<-
chelsa_data_orig raster(paste0(dir_CHELSA,dataType_ERA5,'/CHELSA_prec_',yr,'_0',mon,'_CA_V1.2.1.tif'))
<- '_0'
numbPrefix else {
} <-
chelsa_data_orig raster(paste0(dir_CHELSA,dataType_ERA5,'/CHELSA_prec_',yr,'_',mon,'_CA_V1.2.1.tif'))
<- '_'
numbPrefix
}<- raster::resample(chelsa_data_orig,pbcorr_monthly_basin_longlat)
chelsa_data_orig_resamp <- overlay(chelsa_data_orig_resamp,subset(pbcorr_monthly_basin_longlat,mon),
chelsa_data_bcorr_resamp fun=function(x,y){return(x*y)})
# write bias corrected CHELSA back to the disk.
::writeRaster(chelsa_data_bcorr_resamp,
rasterpaste0(dir_CHELSA,dataType_ERA5,'/CHELSA_tp_bcorr_',yr,numbPrefix,mon,'_CA_V1.2.1.tif'),
'GTiff',
varname = dataType_ERA5,
overwrite = TRUE)
#chelsa_data_bcorr_resamp %>% plot()
} }
9.3.3 Computing Mean Monthly Temperature Climatology from CHELSA v1.2.1 data
The following code take CHELSA_V1.2.1 mean monthly temperature raster data and computes long-term mean monthly temperatures over the Central Asia domain. The resulting file can optionally be stored on the disk.
<- '../HydrologicalModeling_CentralAsia_Data/CentralAsiaDomain/CHELSA_V1.2.1/'
fDir # First, we process and display mean monthly 2 meters temperatures.
<- 't2m/'
varDir <- list.files(paste0(fDir,varDir))
t2m_files # Get monthly fields and average over all data for resulting climatology
<- seq(1,409,12)
idxSeq for (idx in (1:12)){
print(months[idx])
<- t2m_files[idxSeq]
t2m_files_sel for (idxRaster in (1:length(t2m_files_sel))){
if (idxRaster == 1){
<- raster::raster(paste0(fDir,varDir,'/',t2m_files_sel[1]))
raster_res else {
} <- raster::raster(paste0(fDir,varDir,'/',t2m_files_sel[idxRaster]))
raster_n <- raster::addLayer(raster_n,raster_res)
raster_res
}<- calc(raster_res, fun = mean)
raster_res_mean
}if (idx ==1){
<- raster_res_mean
raster_res_mean_stack else {
} <- raster::addLayer(raster_res_mean_stack,raster_res_mean)
raster_res_mean_stack
}<- idxSeq + 1
idxSeq
}
names(raster_res_mean_stack) <- month.abb
# raster::writeRaster(raster_res_mean_stack,
# paste0(fDir,'t2m_climatology/t2m_climatology_CA.tiff'),
# options="INTERLEAVE=BAND",
# 'GTiff',
# varname = 't2m',
# overwrite = TRUE)