[英]R - extract part of .nc file and convert into raster (similar to WorldClim format)
I have a netcdf file I made which contains percentage values.我有一个包含百分比值的 netcdf 文件。 The file has 1 variable, 5 dimensions and 0 NetCDF attributes.
该文件有 1 个变量、5 个维度和 0 个 NetCDF 属性。 The dimensions are "lon" "lat" "month" "CR" "yearSumm"
维度是“lon”“lat”“month”“CR”“yearSumm”
They were created using它们是使用创建的
lon <- ncdim_def("lon", "modis_degrees", -179.5:179.5, unlim=FALSE,
create_dimvar=TRUE, calendar=NA, longname="Longitude")
lat <- ncdim_def("lat", "modis_degrees", -89.5:89.5, unlim=FALSE,
create_dimvar=TRUE, calendar=NA, longname="Latitude")
month <- ncdim_def("month", "month_name", 1:13, unlim=FALSE,
create_dimvar=TRUE, calendar=NA, longname="Month.and.Annual.Data")
CR <- ncdim_def("CR", "CR_numeric", 1:12, unlim=FALSE,
create_dimvar=TRUE, calendar=NA, longname="Cloud.Regime")
yearSumm <- ncdim_def("yearSumm", "yearOrSummType", 1:21, unlim=FALSE,
create_dimvar=TRUE, calendar=NA, longname="Year.and.Summary.Data")
I want to extract 13 layers (each latxlong with each cell a percentage value) from this and make them into a raster file like the bioclimatic data you can download from worldclim我想从中提取 13 层(每个 latxlong,每个单元格一个百分比值)并将它们制作成一个光栅文件,就像您可以从worldclim下载的生物气候数据一样
I have tried extracting the data I want into an array, to then make a raster.我尝试将我想要的数据提取到一个数组中,然后制作一个栅格。 I did that using
我这样做了
CR_RFO <- ncvar_get(CRnc, attributes(CRnc$var)$names[1])
CR_Ann <- as.array(CR_RFO[1:360, 1:180, 13, 1:12, 18])
This seems to have selected the data I want.这似乎选择了我想要的数据。 I then tried to make that into raster format.
然后我试着把它变成光栅格式。
raster(CR_Ann)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘raster’ for signature ‘"array"’
> CR_R <- as.raster(CR_Ann)
Error in array(if (d[3L] == 3L) rgb(t(x[, , 1L]), t(x[, , 2L]), t(x[, :
a raster array must have exactly 3 or 4 planes
> CR_R <- raster(CR_Ann)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘raster’ for signature ‘"array"’
> CR_R <- stack(CR_Ann)
Error in data.frame(values = unlist(unname(x)), ind, stringsAsFactors = FALSE) :
arguments imply differing number of rows: 777600, 0
> CR_R <- brick(CR_Ann)
Eventually brick worked, but I don't think that is actually what I want.最终砖工作,但我不认为这实际上是我想要的。 When I looked up the WorldClim files I downloaded, it is a zip file of.tifs
当我查看我下载的 WorldClim 文件时,它是一个 zip 文件 of.tifs
I also had tried我也试过
# set path and filename
ncpath <- "data/"
ncname <- "CR_RFO"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "Ann" # note: Ann means Annual
CR_raster <- brick(ncfname, varname="CR_RFO")
CR_raster; class(CR_raster)
which resulted in the error导致错误
CR_RFO has more than 4 dimensions, I do not know what to do with these data
I suspect I am going about it from the wrong angle, and maybe even have made my netcdf file incorrectly, as lat and long are not variables like in some of the examples I have read.我怀疑我从错误的角度进行处理,甚至可能错误地制作了我的 netcdf 文件,因为 lat 和 long 不是我读过的一些示例中的变量。
How can I extract these 13 lat x long layers and output them as.tif as per worldclim?如何按照 worldclim 将这 13 个 lat x long 层和 output 提取为 .tif?
This is how I have ended up doing what I think I needed to.这就是我最终做我认为我需要做的事情的方式。 I haven't tested this in place of worldclim data yet, but I have successfully made the geotiff files.
我还没有测试过这个来代替 worldclim 数据,但是我已经成功地制作了 geotiff 文件。
CRnc <- nc_open("data/CR_RFO.nc")
CR_RFO <- ncvar_get(CRnc, attributes(CRnc$var)$names[1])
Repeat from here for each tif I want, selecting the correct number in the 4th place in the index, and changing the file names accordingly.从这里对我想要的每个 tif 重复,在索引的第 4 位选择正确的数字,并相应地更改文件名。
CR1_Ann <- as.matrix(CR_RFO[1:360, 1:180, 13, 1, 18])
CR1_Ann <- t(CR1_Ann)
CR1_Ann <- flipud(CR1_Ann)
CR1_Annr <- raster(CR1_Ann, ymn = -89.5, ymx = 89.5, xmn = -179.5, xmx = 179.5)
#plot(CR1_Annr)
writeRaster(CR1_Annr, "./data/CR_Ann/CR1_Ann", format = "GTiff")
This is not an elegant solution, so if anyone has a better way, please share.这不是一个优雅的解决方案,所以如果有人有更好的方法,请分享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.