forked from EcoForecast/SoilMoisture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_Data_ReOrganizing.R
57 lines (48 loc) · 2.22 KB
/
04_Data_ReOrganizing.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
#Written Chi, 03/27/2016
# 1. this script is going to re-organize our data by date. This is a temporary code to fix the data organizing issues.
# 2. It will take the mean if there are more than one daily observations
# 3. Combine multiple data sources. The starting day of the output will be the first day that we have soilmoisture data.
# 4. The object "combined" will be the final output. Check it and you will understand it.
# 5. Copy this code to your code
data.root.path = './example/'
SMAP <- read.csv(sprintf("%sSMAP.csv",data.root.path)) ## read in soil moisture data
GPM <- read.csv(sprintf("%sGPM.csv",data.root.path)) ## read in precipitation data
MODIS <- read.csv(sprintf("%sMODIS.csv",data.root.path)) ## read in MODIS data
preprocess.Data <- function(x){
x=x[with(x,order(x$Date)),]
value = tapply(x$Data,x$Date,mean,na.rm=TRUE)
out = data.frame(Date=rownames(value),Data=value)
rownames(out)=as.character(seq(1,length(value)))
return(out)
}
MODIS=preprocess.Data(MODIS)
GPM = preprocess.Data(GPM)
SMAP = preprocess.Data(SMAP)
Date.Start = min(as.numeric(as.Date(GPM[,1])),as.numeric(as.Date(MODIS[,1])),as.numeric(as.Date(SMAP[,1])))
Date.End = max(as.numeric(as.Date(GPM[,1])),as.numeric(as.Date(MODIS[,1])),as.numeric(as.Date(SMAP[,1])))
combined = data.frame(Date=as.Date(character()),
NDVI=numeric(),
Precip=numeric(),
SoilMoisture=numeric())
n=0
for (idate in Date.Start:Date.End){
n=n+1
i_this_day = as.Date(idate,origin="1970-01-01")
combined[n,1]=i_this_day
ndvi = MODIS[as.Date(MODIS$Date)==i_this_day,2]
if (is.null(ndvi)|length(ndvi)==0) {
ndvi=NA}
precip = GPM[as.Date(GPM$Date)==i_this_day,2]
if (is.null(precip)|length(precip)==0) {
precip=0} ## note that change precip to NA if necessary
smap = SMAP[as.Date(SMAP $Date)==i_this_day,2]
if (is.null(smap)|length(smap)==0) {
smap=NA}
combined[n,2]=ndvi
combined[n,3]=precip
combined[n,4]=smap
}
NonNAindex <- min(which(!is.na(combined$SoilMoisture)))-1
combined=combined[-(1:NonNAindex),]
row.names(combined) <- 1:nrow(combined)
write.table(combined,file = paste(data.root.path,'combined_data.csv',sep=""),na="NA",row.names=FALSE,col.names=TRUE,sep=",")