-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget.met_em.tvar.r
154 lines (133 loc) · 5.36 KB
/
get.met_em.tvar.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
get.met_em.tvar <- function(varname, vardim, subset, path, pattern, calc = c("ASIS","SUM","MEAN","SD"), prlevs = c(500, 700, 850), return_tstamp = FALSE ){
# if return_tstamp = TRUE, return a vector of POSIXlt time stamps (before SUM/MEAN/SD was performed)
require("RNetCDF")
require("abind")
source("get_met_var.r")
filelist = list.files(path = path, pattern = pattern, full.names = TRUE)
if ( inherits(subset[1],"POSIXct") ){
# do nothing
Timerange = subset
} else if (inherits(subset[1],"POSIXlt")){
Timerange = as.POSIXct(subset, tz = "GMT")
} else {
# treat as string
Timerange = as.POSIXct( strptime(subset, "%Y-%m-%d %H:%M:%S", tz = "GMT") )
}
# decide whether each file is in range
fileinrange = c()
timeinrange = list() # whether the time stamps in each file is in range
for (i in c(1:length(filelist))){
ncid = open.nc(filelist[i])
# get the Time stamps
Times = as.POSIXct( strptime(var.get.nc(ncid, "Times"), "%Y-%m-%d_%H:%M:%S", tz = "GMT") )
timeinrange[[i]] = which(Times >= Timerange[1] & Times<=Timerange[2])
if (length(timeinrange[[i]]) > 0){
fileinrange[i] = TRUE
} else {
fileinrange[i] = FALSE
}
close.nc(ncid)
}
timeinrange = timeinrange[ fileinrange ]
filelist = filelist[ fileinrange ]
for (i in c(1:length(filelist))){
print(paste("Begin process file ",i," = ", filelist[i],sep=""))
ncid = open.nc(filelist[i])
keeptime = timeinrange[[i]]
if (return_tstamp){
Times = as.POSIXct( strptime(var.get.nc(ncid, "Times"), "%Y-%m-%d_%H:%M:%S", tz = "GMT") )
if (i == 1){
time = Times[ keeptime ]
} else {
time = rbind( data.frame(time = time), data.frame(time = Times[ keeptime ]) )[[1]] # only in this way does it keep the time zone
}
}
if (varname == "PSFC" | varname == "U10" | varname == "V10" | varname == "T2" | varname == "RH2" | varname == "SH2"){
# surface variables subset to 1st vertical level
start = c(NA,NA,1,keeptime[1])
count = c(NA,NA,1,length(keeptime))
} else {
if (vardim == 4){
start = c(NA,NA,NA,keeptime[1])
count = c(NA,NA,NA,length(keeptime))
} else if (vardim == 3) {
start = c(NA,NA,NA,keeptime[1])
count = c(NA,NA,NA,length(keeptime))
} else {
stop(paste("Cannot handle vardim = ",vardim,"yet."))
}
}
temp = get_met_var(ncid, varname, start = start, count = count, collapse = FALSE)
if (vardim == 4){
# pressure levels - subset to one column
pr = var.get.nc(ncid, "PRES", start = c(NA,NA,NA,1), count = c(NA,NA,NA,1)) / 100
pr = apply(pr, MARGIN=3, FUN=mean, na.rm=TRUE)
keep = c()
for (j in 1:length(prlevs)){
keep[j] = which( pr == prlevs[j] )
}
temp = temp[,,keep,]
}
# concatenate
if (i == 1){
var = temp
} else {
var = abind(var, temp, along = vardim)
}
# print(dim(temp))
# print(dim(var))
close.nc(ncid)
}
# calculate statistics
if (calc == "ASIS"){
if (return_tstamp){
return( list(var = var, timestamp = time) )
} else {
return( var )
}
} else if (calc == "SUM"){
if (vardim == 3){
if (return_tstamp){
return( list( var = apply(var, MARGIN = c(1,2), FUN = sum, na.rm=TRUE), timestamp = time ) )
} else {
return( apply(var, MARGIN = c(1,2), FUN = sum, na.rm=TRUE) )
}
} else if (vardim == 4) {
if (return_tstamp){
return( list( var = apply(var, MARGIN = c(1,2,3), FUN = sum, na.rm=TRUE), timestamp = time ) )
} else {
return( apply(var, MARGIN = c(1,2,3), FUN = sum, na.rm=TRUE) )
}
}
} else if (calc == "MEAN"){
if (vardim == 3){
if (return_tstamp){
return( list( var = apply(var, MARGIN = c(1,2), FUN = mean, na.rm=TRUE), timestamp = time ) )
} else {
return( apply(var, MARGIN = c(1,2), FUN = mean, na.rm=TRUE) )
}
} else if (vardim == 4) {
if (return_tstamp){
return( list( var = apply(var, MARGIN = c(1,2,3), FUN = mean, na.rm=TRUE), timestamp = time ) )
} else {
return( apply(var, MARGIN = c(1,2,3), FUN = mean, na.rm=TRUE) )
}
}
} else if (calc == "SD"){
if (vardim == 3){
if (return_tstamp){
return( list( var = apply(var, MARGIN = c(1,2), FUN = sd, na.rm=TRUE), timestamp = time ) )
} else {
return( apply(var, MARGIN = c(1,2), FUN = sd, na.rm=TRUE) )
}
} else if (vardim == 4){
if (return_tstamp){
return( list( var = apply(var, MARGIN = c(1,2,3), FUN = sd, na.rm=TRUE), timestamp = time ) )
} else {
return( apply(var, MARGIN = c(1,2,3), FUN = sd, na.rm=TRUE) )
}
}
} else {
stop("Un-recognized statistics")
}
}