R:将栅格图层作为一个对象读取,以在每个ENVI文件中应用函数

时间:2017-06-02 18:35:18

标签: r spatial raster r-raster

我有一个ENVI文件(82_83_test.envi),其中包含1982年至1983年的双周栅格图层。这是每年24层,总共48层。我想创建一个for循环来应用一个函数来执行每年的时间序列分析,即R将在一个像素中运行24个图层并使用函数计算5个参数" fun"对于那一年的所有像素。最后,我希望每年有5个地块(5个参数),所以总共10个地块,为期两年。

我尝试使用1个ENVI文件,包含2年的数据和2个ENVI文件,每个文件中包含1年的数据。我使用库spatial.tools中的brickstack_to_raster_list()来读取文件,然后得到48层。但是,我想获得2个块(1982年和1983年),每个块包含24个层,这样我就可以运行这个等式。

也许像brickstack_to_raster_list()这样的东西然后将第一层到第24层合并为一个,然后将第25层到第48层合并为一个?

new <- stack("82_83_test.envi")
new1<- brickstack_to_raster_list(new)

new1返回48个栅格图层。例如,

new1
[[1]]
class       : RasterLayer 
band        : 1  (of  48  bands)
dimensions  : 151, 101, 15251  (nrow, ncol, ncell)
resolution  : 0.08333333, 0.08333333  (x, y)
extent      : -105.0833, -96.66667, 56.66667, 69.25  (xmin, xmax, ymin, 
ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
+towgs84=0,0,0 
data source : C:\*\82_83_test.envi 
names       : Band.1 
values      : -32768, 5038  (min, max)

另一种方法是将多个年度ENVI文件连接到一个列表中。

new <- stack("1982_test.envi")
new1<- stack(new,new)
new2<- brickstack_to_raster_list(new1)

上述两种方法都会产生相同的结果,但我不确定其效率。因为在设置完成后,我将从1982年到2015年生成数据,因此效率非常重要。

以下是我想在for循环中应用的函数。

# A is an unknown that will be the number of components in the list. 
for (i in length(A)) {
new1[new1<=-1000]<-0
Data_value<-new1/10000
# assign 0 to pixel value that is less than -1000 and divide by 10000 in order to use the equation
DOY<-(1:nlayers(new1)*15)
# so that the unit will be in days instead of the number of weeks.

fun<- function(x) { if (all(is.na(x[1]))) { return(rep(NA,5)) } else { 
fitForThisData  <-nls(x~ a+((b/(1+ exp(-c*(DOY-e))))- (g/(1+ exp(-d*(DOY-
f))))), alg="port",start=list(a=0.1,b=1,g=1,c=0.04,d=0.04,e=112,f=218),
lower=list(a=0,b=0.3,g=0.3,c=-1,d=-1,e=20,f=100),
upper=list(a=0.4,b=2,g=2,c=1,d=1,e=230,f=365),
control=nls.control(maxiter=2000, tol = 1e-15, minFactor = 1/1024, 
warnOnly=TRUE))
SOS<-(coef(fitForThisData)[6] -(4.562/(2*coef(fitForThisData)[4])))
EOS<-(coef(fitForThisData)[7] -(4.562/(2*coef(fitForThisData)[5])))
LOS<-(EOS-SOS)
SPUDOY<-(1.317*((-1/coef(fitForThisData)[4])+ coef(fitForThisData)[6]))
P_TAmplitude<-(SPUDOY-SOS)
return (c(SOS,EOS,LOS,SPUDOY,P_TAmplitude))
}
}
}

equation<-calc(Data_value,fun,forceapply=TRUE)
plot(equation)

我真的很感激您对如何做到这一点的建议。非常感谢你!

2 个答案:

答案 0 :(得分:0)

读完你的筹码后:

library(raster)    
new <- stack("82_83_test.envi")

使用基本索引简单地将堆栈拆分为年度子堆:

year1 <- new[[1:24]]
year2 <- new[[25:48]]

答案 1 :(得分:0)

更新:我能够循环该功能,但我的猜测是,在将它与真值进行比较后,计算在所有栅格图层上完成。但是,会生成两个具有不同文件名的相同内容的文件,因为两个文件具有相同的摘要。

new <- stack("82_83_test.envi")
new[new<=-1000]<-0
Data_value<-new/10000
nlayers <- nlayers(new)
nyears <- nlayers(new)/24
DOY<-((1:nlayers(new))/nyears)*15
dummy<- FALSE

for (i in 1:nyears) {
  for (j in (1+24*(i-1)):(24*i)) { 
    fun<-function (x)
    equation<-calc(Data_value,fun,forceapply=TRUE)
    date<- 1981+i
    writeRaster(equation,filename=paste("Output",date,".envi",sep=""),
    format="ENVI",overwrite=T)
    if (j == nlayers){
    dummy<-TRUE
    break
    if (dummy) {break}      
}
}
相关问题