-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStep2_RegionalBFAST.R
62 lines (52 loc) · 1.71 KB
/
Step2_RegionalBFAST.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## Processing MODIS EVI data
## HDF read from Matlab
rm(list=ls())
stime <- system.time({
library('R.matlab')
library('bfast')
library('parallel')
library('MASS')
library('iterators')
library('tictoc')
library('foreach')
library('doParallel')
#if (!requireNamespace("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
#BiocManager::install("rhdf5")
library('rhdf5')
numCores <- detectCores()
registerDoParallel(numCores/2)
print(numCores)
filename <- '/Volumes/XiYangBackUp/Projects/6.CalDrought/subsets/CalEVI_2000_2019_500m_Subset_8_8.mat'
EVI <- h5read(filename,'EVI')
doy <- h5read(filename,'doy')
LC <- h5read(filename,'LC')
EVI <- EVI[,,228:456] #457 for CMG
doy <- doy[,,228:456]
dims <- dim(EVI)
#QA
EVI[EVI==0.0] <-NA
iii <- 1
allresult <- list()
allresult <- foreach(ii=1:dims[1], .packages = c("bfast","foreach")) %dopar% {
allresult1 <- foreach(jj=1:dims[2], .packages = c("bfast","foreach")) %dopar% {
if(sum(is.na(EVI[ii,jj,]))>=110)
{
result <- NA
} else if(LC[ii,jj,10] %in% c(1,4,5,6,7,8,9,10))
{
time1 <- as.Date(doy[ii,jj,],origin="2000-01-01")
years <- unique(substring(time1,1,4))
DF <- data.frame(x=time1,y=EVI[ii,jj,])
DF1 <- na.omit(DF)
ts_example <- ts(data = DF1$y,frequency=23)
result <- bfast(ts_example,h=60/length(ts_example),season="harmonic",max.iter=2,breaks=1,type="OLS-MOSUM",hpc="foreach")
} else
{
result <- NA
}
}
}
})
print(stime)
save.image('/Volumes/XiYangBackUp/Projects/6.CalDrought/Caldorught_8_8.RData') #change the output filename