如何使用cdo或任何其他软件添加时间维度并合并TRMM每日数据?
How to add time dimension and merge TRMM daily data using cdo or any other software?
我已经下载了不包含时间维度的TRMM每日数据,我试过了
cdo mergetime *nc4 out.nc4
但是如果我将原始文件作为组合进行比较并且没有时间维度并且在 outfile 全局属性中它显示所有输入文件名称,则文件大小太小。我有 5 年的数据,大约有 1825 个文件。
所以,有人可以帮我解决这个问题吗?
谢谢,
乌特卡什
可能晚了,但我在这个问题上苦苦挣扎了很长时间,所以:
- 我已经下载了 TRMM_3B42_Daily data. Each NetCDF file contains one-day data without a time attribute or axis. Therefore Climate Data Operators (CDO) 不行。
- R中的这段代码应该读取每个文件并从文件名中获取及时的信息。然后写入带有时间属性的新 NetCDF 文件。
#1.LIBRARIES=================================================================
library(ncdf4)
library(ncdf4.helpers)
library(stringr)
library(stringi)
# Set directory where TRMM data are.
setwd()
#list all .nc4 files
nc.files<- list.files(pattern = ".nc4$")
# open first file to obtain lon and lat information
ncin<- nc_open(nc.files[1])
lon<- ncvar_get(nc = ncin,"lon")
lat<- ncvar_get(nc = ncin,"lat")
Time<-vector(length = length(nc.files))
#it better to close NetCDF files in R especially when writing them.
nc_close(ncin)
for ( i in 1:length(nc.files)){
ncin<-nc_open(nc.files[i])
med<-ncvar_get(nc = ncin,"precipitation")
raw_time<-unlist(str_split(nc.files[i], "[.]"))[2]
Time[i] <- as.Date(strptime(raw_time, "%Y%m%d"))
nc_close(ncin)
#=======================================================================
# Writing NetCDF file of the
dimLON <- ncdim_def('lon', "degree",
longname='longitude',
vals=lon)
dimLAT <- ncdim_def('lat', "degree",
longname='latitude',
vals=lat)
dimTime <- ncdim_def('time', "days since 1970-01-01 00:00:0.0",
longname='time',
calendar="standard",
vals=Time[i],unlim=TRUE)
# define variables
fillvalue <- 1e32
dlname <- "Precipitation"
var_def <- ncvar_def(name = "precip",
units = "mm day -1",
list(dimLAT,dimLON,dimTime),
fillvalue,dlname,prec="double")
#netcdf file name
ncname<-paste0("precip_TRMM_",raw_time,"_time_.nc")
#netCDF file location
# where to write the netcdf file
ncpath <- ""
ncfname <- paste0(ncpath, ncname)
#create netCDF file and put arrays
ncoutput<-nc_create(ncfname,list(var_def),force_v4=TRUE,verbose =F)
#put variable
ncvar_put(ncoutput,var_def,med)
#put additional attributes into dimension and data variables
ncatt_put(ncoutput,"lon","axis","X")
ncatt_put(ncoutput,"lat","axis","Y")
ncatt_put(ncoutput,"time","axis","T")
nc_close(ncoutput)
# I Use R CMD BATCH and this just for control
print(c(i,raw_time))
rm(var_def,ncoutput,med)
}
- 在此之后 Climate Data Operators (CDO) 可以实施并且应该工作:)(例如,合并文件夹中的所有文件:
cdo mergetime *.nc outfile.nc
我已经下载了不包含时间维度的TRMM每日数据,我试过了
cdo mergetime *nc4 out.nc4
但是如果我将原始文件作为组合进行比较并且没有时间维度并且在 outfile 全局属性中它显示所有输入文件名称,则文件大小太小。我有 5 年的数据,大约有 1825 个文件。
所以,有人可以帮我解决这个问题吗?
谢谢, 乌特卡什
可能晚了,但我在这个问题上苦苦挣扎了很长时间,所以:
- 我已经下载了 TRMM_3B42_Daily data. Each NetCDF file contains one-day data without a time attribute or axis. Therefore Climate Data Operators (CDO) 不行。
- R中的这段代码应该读取每个文件并从文件名中获取及时的信息。然后写入带有时间属性的新 NetCDF 文件。
#1.LIBRARIES=================================================================
library(ncdf4)
library(ncdf4.helpers)
library(stringr)
library(stringi)
# Set directory where TRMM data are.
setwd()
#list all .nc4 files
nc.files<- list.files(pattern = ".nc4$")
# open first file to obtain lon and lat information
ncin<- nc_open(nc.files[1])
lon<- ncvar_get(nc = ncin,"lon")
lat<- ncvar_get(nc = ncin,"lat")
Time<-vector(length = length(nc.files))
#it better to close NetCDF files in R especially when writing them.
nc_close(ncin)
for ( i in 1:length(nc.files)){
ncin<-nc_open(nc.files[i])
med<-ncvar_get(nc = ncin,"precipitation")
raw_time<-unlist(str_split(nc.files[i], "[.]"))[2]
Time[i] <- as.Date(strptime(raw_time, "%Y%m%d"))
nc_close(ncin)
#=======================================================================
# Writing NetCDF file of the
dimLON <- ncdim_def('lon', "degree",
longname='longitude',
vals=lon)
dimLAT <- ncdim_def('lat', "degree",
longname='latitude',
vals=lat)
dimTime <- ncdim_def('time', "days since 1970-01-01 00:00:0.0",
longname='time',
calendar="standard",
vals=Time[i],unlim=TRUE)
# define variables
fillvalue <- 1e32
dlname <- "Precipitation"
var_def <- ncvar_def(name = "precip",
units = "mm day -1",
list(dimLAT,dimLON,dimTime),
fillvalue,dlname,prec="double")
#netcdf file name
ncname<-paste0("precip_TRMM_",raw_time,"_time_.nc")
#netCDF file location
# where to write the netcdf file
ncpath <- ""
ncfname <- paste0(ncpath, ncname)
#create netCDF file and put arrays
ncoutput<-nc_create(ncfname,list(var_def),force_v4=TRUE,verbose =F)
#put variable
ncvar_put(ncoutput,var_def,med)
#put additional attributes into dimension and data variables
ncatt_put(ncoutput,"lon","axis","X")
ncatt_put(ncoutput,"lat","axis","Y")
ncatt_put(ncoutput,"time","axis","T")
nc_close(ncoutput)
# I Use R CMD BATCH and this just for control
print(c(i,raw_time))
rm(var_def,ncoutput,med)
}
- 在此之后 Climate Data Operators (CDO) 可以实施并且应该工作:)(例如,合并文件夹中的所有文件:
cdo mergetime *.nc outfile.nc