CMIP6 如何在 R 中合并多个 .nc 文件

问题描述 投票:0回答:1

我要感谢大家花时间回答这个问题。我似乎无法找到如何在 R 中合并 2 个或更多 .nc 文件——这是在尝试了几天的不同解决方案之后,从这里、youtube、书籍以及与同事交谈等..

在此示例中,我有来自单个气候模型的 10 个文件,其中包含 1850 年至 1949 年的温度数据。每个文件都包含 9 年的地理参考数据。我希望将所有年份中的一个月平均温度保存在一个 .nc 文件中。请参阅下面的示例代码:

####I am testing 2 months, then will include all the files once I know it works. 
Basic idea, open files, create array, average across years for a given month, 
then merge the output and convert to raster for further processing.###

ds1 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_185001-185912.nc")
ds2 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_186001-186912.nc")
ds3 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_187001-187912.nc")
ds4 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_188001-188912.nc")
ds5 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_189001-189912.nc")
ds6 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_190001-190912.nc")
ds7 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_191001-191912.nc")
ds8 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_192001-192912.nc")
ds9 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_193001-193912.nc")
ds10 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_194001-194912.nc")

ds1 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_185001-185912.nc")
ts <- ds1[["ts"]]
d <- ts$data()
d
t <- d$axes[["time"]]
t
arr <- d$array()
str(arr)
cft <- t$values
fac <- CFfactor(cft, epoch = 1850:1859)
res <- apply(arr, 1:2, tapply, fac, mean)
res <- aperm(res, c(2, 3, 1))
str(res)

### Looking good:

> str(res)
 num [1:192, 1:288, 1:12] 234 234 235 235 235 ...
 - attr(*, "dimnames")=List of 3
  ..$ lat: chr [1:192] "90" "89.05759" "88.11518" "87.17277" ...
  ..$ lon: chr [1:288] "0" "1.25" "2.5" "3.75" ...
  ..$    : chr [1:12] "01" "02" "03" "04" ...

###


ds2 <- open_ncdf("ts_Amon_SAM0-UNICON_historical_r1i1p1f1_gn_186001-186912.nc")
ts <- ds2[["ts"]]
d <- ts$data()
d
t <- d$axes[["time"]]
t
arr <- d$array()
str(arr)
cft <- t$values
fac <- CFfactor(cft, epoch = 1860:1869)
res2 <- apply(arr, 1:2, tapply, fac, mean)
res2 <- aperm(res2, c(2, 3, 1))
str(res2)

###Also checks out:

> str(res2)
 num [1:192, 1:288, 1:12] 234 235 235 235 236 ...
 - attr(*, "dimnames")=List of 3
  ..$ lat: chr [1:192] "90" "89.05759" "88.11518" "87.17277" ...
  ..$ lon: chr [1:288] "0" "1.25" "2.5" "3.75" ...
  ..$    : chr [1:12] "01" "02" "03" "04" ...
###

##To merge the separate datasets, I am trying to use abind###


test<-abind(res, res2, rev.along=0)
str(test)

###Looks good, I see 12 months listed (previous iterations/attempts gave me 24 time steps
 using rev.along=1 or along=3). BUT, I now have NULL as an added dimension.

> str(test)
 num [1:192, 1:288, 1:12, 1:2] 234 234 235 235 235 ...
 - attr(*, "dimnames")=List of 4
  ..$ : chr [1:192] "90" "89.05759" "88.11518" "87.17277" ...
  ..$ : chr [1:288] "0" "1.25" "2.5" "3.75" ...
  ..$ : chr [1:12] "01" "02" "03" "04" ...
  ..$ : NULL
###

res.jan <- test[, , 1, NULL]
Error in test[, , 1, NULL] : incorrect number of dimensions
 
> res.jan <- test[, , 1, ,]
Error in test[, , 1, , ] : incorrect number of dimensions

###and purely for learning/curiousity:###

> res.jan <- test[1, 1, 1, NULL] 
> resr.jan<- raster(t(res.jan), xmn=min(lon), xmx=max(lon), ymn=min(lat), ymx=max(lat), crs=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'extent': object 'lon' not found

###Incidentally, I have run into this last error when working with other files that did not
 need to be merged, sometimes I get the error, sometimes I don't. Not sure what seems to 
cause it. This last point/error of a missing 'lon' value is secondarily important to my
 primary issue of merging the files; I brought it up because I would like to learn how to 
fix/debug for the future. Closing R, reopening, rm=(), then crossing fingers and running
 code sometimes works and sometimes doesn't, but CLEARLY is a newb solution and less than
 optimal.###
 

谢谢!! 有什么想法吗?谢谢。

r arrays bigdata ncdf4
1个回答
0
投票

按照您的上一个问题和那里接受的答案,您可以简单地从函数中绑定数组,如下所示:

# `res` is the output from the function in the previously accepted answer
test <- abind(res, along = 3)

如果您在上一个答案中建议的解决方案中使用从 1850 年到 1949 年的全套数据文件,那么最后一个

abind()
函数调用会将 1850 年以来的一个世纪中沿着“时间”维度的所有内容粘合在一起。

您不应该像以前那样使用

abind()
,因为您处理的每个文件都会获得 12 个月的第四维,而是要将它们合并到第三维。

© www.soinside.com 2019 - 2024. All rights reserved.