将多维NETCDF转换为r中的TIF

发布于 2025-01-22 13:53:08 字数 816 浏览 5 评论 0 原文

我的.NC文件大约为651 MB,其中有几个数据集(每日)(RR_MRG_19810101_20181231_ENACT.NC)数据。我需要将(RR_MRG_198101011_20181231_ENACT.NC)转换为多个geotiff(每个时间切片,每月一个.tif)。同样,我想阅读时间序列。但是我发现

Error in .local(x, time, ...) : 
time must has the same length as the number of layers in RasterBrick][1]

这是我所做的,

library(raster)
library(zoo)
library(rts)
       
TRF = brick("rr_mrg_19810101_20181231_ENACT.nc")
crs(TRF) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
+towgs84=0,0,0"
Awash_Extent<-c(37,44, 7,12)
Awash_E_resize<- crop(TRF,Awash_Extent)

Awash_Month<-seq(as.Date('1981-01-01'),as.Date('2018-12-31'),by = 
"month")

rt <- rts(Awash_E_resize, Awash_Month) 

write.rts(Awash_E_resize, filename='Awash_TRF_1981_2018_mon.tif', 
overwrite=TRUE)

您能帮我解决这个问题吗?

I have .nc file sizing around 651 MB with a couple of datasets (daily) (rr_mrg_19810101_20181231_ENACT.nc) data. I need to convert (rr_mrg_19810101_20181231_ENACT.nc) dataset to multiple GeoTIFF (one .tif for each time slice, monthly). Similarly, i want to read the time series. But I found

Error in .local(x, time, ...) : 
time must has the same length as the number of layers in RasterBrick][1]

Here is what i did

library(raster)
library(zoo)
library(rts)
       
TRF = brick("rr_mrg_19810101_20181231_ENACT.nc")
crs(TRF) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
+towgs84=0,0,0"
Awash_Extent<-c(37,44, 7,12)
Awash_E_resize<- crop(TRF,Awash_Extent)

Awash_Month<-seq(as.Date('1981-01-01'),as.Date('2018-12-31'),by = 
"month")

rt <- rts(Awash_E_resize, Awash_Month) 

write.rts(Awash_E_resize, filename='Awash_TRF_1981_2018_mon.tif', 
overwrite=TRUE)

Can you help me on the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2025-01-29 13:53:09

这样简单的事情可以

library(raster)
b <- brick("rr_mrg_19810101_20181231_ENACT.nc")
writeRaster(b, "timeslice.tif", bylayer=TRUE)

根据您的扩​​展问题来完成此操作:

使用 Terra (不需要其他软件包)

library(terra)    
TRF = rast("rr_mrg_19810101_20181231_ENACT.nc")
Awash_Extent<-c(37,44, 7,12)
Awash_E_resize<- crop(TRF,Awash_Extent)

我在这里创建类似的内容:

A <- rast(ext(37,44, 7,12), nlyr=365)
values(A) <- runif(size(A))
terra::time(A) <- seq(as.Date('1981-01-01'), as.Date('1981-12-31'), 1)

现在按月汇总:

m <- months(time(A))
f <- factor(m, levels=unique(m))

B <- tapp(A, m, fun=sum)
B
#class       : SpatRaster 
#dimensions  : 10, 10, 12  (nrow, ncol, nlyr)
#resolution  : 0.7, 0.5  (x, y)
#extent      : 37, 44, 7, 12  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : memory 
#names       :  January, February,    March,    April,      May,     June, ... 
#min values  : 12.49764, 10.47718, 11.80974, 11.29624, 11.01105, 10.83298, ... 
#max values  : 18.90536, 16.95743, 20.57114, 18.12099, 18.46543, 18.98500, ... 
 

您可以添加 filename = tapp 的参数,但是如果要将这些层保存为单独的文件,则可以使用 writeraster 。但是有了Terra,您需要自己提供文件名。

 fnames <- paste0("rain_", 1:nlyr(B), ".tif")
 writeRaster(B, fnames, overwrite=T)

(有一个关于file_ext的警告,您可以忽略)

Something as simple as this can do that

library(raster)
b <- brick("rr_mrg_19810101_20181231_ENACT.nc")
writeRaster(b, "timeslice.tif", bylayer=TRUE)

Based on your expanded question:

Read the values with terra (no need for other packages)

library(terra)    
TRF = rast("rr_mrg_19810101_20181231_ENACT.nc")
Awash_Extent<-c(37,44, 7,12)
Awash_E_resize<- crop(TRF,Awash_Extent)

I create something similar here:

A <- rast(ext(37,44, 7,12), nlyr=365)
values(A) <- runif(size(A))
terra::time(A) <- seq(as.Date('1981-01-01'), as.Date('1981-12-31'), 1)

Now aggregate by month:

m <- months(time(A))
f <- factor(m, levels=unique(m))

B <- tapp(A, m, fun=sum)
B
#class       : SpatRaster 
#dimensions  : 10, 10, 12  (nrow, ncol, nlyr)
#resolution  : 0.7, 0.5  (x, y)
#extent      : 37, 44, 7, 12  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : memory 
#names       :  January, February,    March,    April,      May,     June, ... 
#min values  : 12.49764, 10.47718, 11.80974, 11.29624, 11.01105, 10.83298, ... 
#max values  : 18.90536, 16.95743, 20.57114, 18.12099, 18.46543, 18.98500, ... 
 

You could add a filename= argument to tapp, but if you want to save the layers as separate files you can use writeRaster instead. But with terra you need to provide the filenames yourself.

 fnames <- paste0("rain_", 1:nlyr(B), ".tif")
 writeRaster(B, fnames, overwrite=T)

(there is a warning about file_ext that you can ignore)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文