-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGetNowcast.r
96 lines (86 loc) · 4.48 KB
/
GetNowcast.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Decision tree to figure out which path to take for creating a Nowcast based on data completeness & # of 0's.
GetNowcast<-function(tdata,rowcount,dataStreamId,rowCountToUseSurrogate=252,op.mode=F){
# op.mode: set to F for testing on local computeres; T for public reporting based on data from the AirNow database
#rowCountToUseSurrogate <- 252; # 75% completeness
#print(paste('DataStreamID =', dataStreamId, 'LST =', max(tdata$LST), 'TotalRowCount =', nrow(tdata), 'NumRowsWithData =', rowcount));
# if all values are null, then a NowCast value cannot be generated
if(rowcount == 0) {
curr.message<-"all values are null. return null"
out_df = data.frame(DataStreamID = NA, Date = NA, NowCast = NA, MethodID = 8)
} else if (rowcount < rowCountToUseSurrogate) {
curr.message<-"does not meet 75% completeness criteria. Using surrogate instead."
surrogateData <- generateSurrogate(tdata)
print(surrogateData)
out_df = data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST),NowCast = surrogateData$surrogateValue, MethodID = surrogateData$methodID);
} else if (get.max.na.stream(tdata$Value) > 7) { ## New condition from AR 4/24/19
curr.message<-"too many consecutive missing values. Using surrogate instead."
surrogateData <- generateSurrogate(tdata)
print(surrogateData)
out_df = data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST),NowCast = surrogateData$surrogateValue, MethodID = surrogateData$methodID);
} else if(sum(as.numeric(tdata$Value), na.rm = TRUE) == 0) {
curr.message<-"all values are 0; returning 0 as nowcast value."
# this usually indicates a bad instrument, but it can happen
out_df = data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST), NowCast = 0, MethodID = 1);
} else if(is.na(tdata[nrow(tdata)-0,]$Value)) { # current hour is null
if(!is.na(tdata[nrow(tdata)-1,]$Value)){ #try to use previous hour
previousHour = tdata[nrow(tdata)-1,]$LST
curr.message<-paste("Meets 75% completeness, but missing current hour. Use previous hour NowCast value instead; ", previousHour)
# retrieve previous hour's data
if (!op.mode){airIndexData<-data.frame()}
if (op.mode){airIndexData <- GetEarlierHour(mycon,dataStreamId,previousHour)};
airIndexValue = NA;
if(nrow(airIndexData) > 0) {
airIndexValue = airIndexData$AirIndexValue
} else {
curr.message<-"No Value returned."
}
out_df <- data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST), NowCast = airIndexValue, MethodID = 4);
}
else if(!is.na(tdata[nrow(tdata)-2,]$Value)) {
# use value from 2 hours previous
dt = tdata[nrow(tdata)-2,]$LST
curr.message<-paste("Meets 75% completeness, but missing current and previous hours. Using value from 2 hours previous instead; ", dt)
# retrieve previous hour's data
if (!op.mode){airIndexData<-data.frame()}
if (op.mode){airIndexData <- GetEarlierHour(mycon,dataStreamId,dt)};
airIndexValue = NA;
if(nrow(airIndexData) > 0) {
airIndexValue = airIndexData$AirIndexValue
} else {
curr.message<-"No Value returned."
}
out_df <- data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST), NowCast = airIndexValue, MethodID = 6);
}
else {
curr.message<-"No value from previous three hours. Returning NULL"
out_df <- data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST), NowCast = NA, MethodID = 8);
}
} else {
# Generate a NowCast value
curr.message<-"Nowcast generated through the PLS method."
tnowcast <- c()
methodID <- 2
for (i in 1:(nrow(tdata)-335)) {
tpred <- pls.nowcast(tdata$Value[i:(i+335)])
tnowcast <- c(tnowcast, tpred[!is.na(tpred)])
}
# Check that NowCast value was generated correctly.
if(length(tnowcast) == 0) {
curr.message<-"Failed to generate a NowCast value."
tnowcast <- NA;
methodID <- 8;
} else if(tnowcast[length(tnowcast)] < 0) {
curr.message<-"Nowcast was < 0, returning 0."
#return 0 if generated NowCast value is less than 0
tnowcast <- 0;
methodID <- 1;
} else {
tnowcast <- tnowcast[length(tnowcast)];
}
out_df <- data.frame(DataStreamID = dataStreamId, Date = max(tdata$LST), NowCast = tnowcast, MethodID = methodID);
}
print(curr.message)
## Add message to stored data if possible, comment out the line below if not.
out_df<-data.frame(out_df,Message=curr.message,stringsAsFactors=F)
return(out_df)
}