【发布时间】:2017-06-02 18:35:17
【问题描述】:
我有一个 ENVI 文件 (82_83_test.envi),其中包含从 1982 年到 1983 年的双周栅格图层。即每年 24 层,总共 48 层。我想创建一个 for 循环来应用一个函数来每年执行时间序列分析,即 R 将在一个像素中运行 24 层,并使用函数“fun”为该年的所有像素计算 5 个参数。最终,我希望每年有 5 个地块(5 个参数),所以两年总共有 10 个地块。
我尝试在每个文件中使用 1 个具有 2 年数据的 ENVI 文件和 2 个具有 1 年数据的 ENVI 文件。我使用了库 spatial.tools 中的brickstack_to_raster_list() 来读取文件,我得到了 48 层。但是,我想获得 2 个块(1982 年和 1983 年),每个块由 24 层组成,以便我可以运行方程式。
也许像brickstack_to_raster_list() 之类的东西然后将第1 层到第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)
非常感谢您对如何执行此操作的建议。非常感谢你!
【问题讨论】: