R语言处理CMIP6数据(个人笔记)
总结了近期我的CMIP6模型数据处理方法,函数可以实现对指定空间范围,指定年份内的CMIP6变量数据的裁剪和合并,并重采样为统一的分辨率。
同时,某些模型每年只有365天,没有设置闰年,会对这部分模型闰年的12.31日进行插值补全(因为数据并不是缺了2.29日)。
全部代码如下,有注释
#-----------------------------------------------------------------------------------
#1.Determine watershed number and basin shapefile and the observed data---------------------
rm(list = ls())
memory.limit(400000)
setwd('./cmip6/')
library(rgdal)
library(sf)
library(raster)
library(ncdf4)
library(abind)
library(stringr)
library(lubridate)
library(fields)
library(terra)
# 中国轮廓,用于裁剪
china_shp <-st_read("./chinaout.shp")
#2. CMIP6 data processing and tailoring-----------------------------------------------
#crop function------------------------------------------------------------------------------
# function 1------------------- 用于判断是否闰年的函数
leap<-function(x){
if(x%%4==0){
if(((x%%100==0)&(x%%400==0))|(x%%100!=0))
return(TRUE) else
return(FALSE)
} else
return(FALSE)
}
# #function2------------- 用于裁剪,合并,并且重采样的函数
crop_year_re <- function(end,s_china,dates,k,ncdata,dateend,dateyear){
if (end ==dateend){
a_2 <-crop(ncdata,s_china)
crs(a_2) <- '+proj=longlat +datum=WGS84 +no_defs'
cc <- resample( a_2, s_china , method = "bilinear")
a <- as.data.frame(cc,xy=TRUE,na.rm=FALSE)
} else {
a_2 <- crop(ncdata,s_china)
crs