Newer
Older
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
context("Generic tests")
test_that("Load", {
library(s2dv)
#source("R/zzz.R")
#source("R/SelectPeriodOnDates.R")
#source("R/SelectPeriodOnData.R")
StartDates <- paste0(1993:1995, '1101')
exp <- list(name = 'ecmwfS5', path = "/esarchive/exp/ecmwf/system5c3s/$STORE_FREQ$_mean/$VAR_NAME$_s0-24h/$VAR_NAME$_$START_DATE$.nc")
exp <- Load(var = 'prlr', exp = list(exp), obs = NULL,
sdates = StartDates, nmember = 25,
storefreq = "daily", sampleperiod = 1,
latmin = 42, latmax = 45, lonmin = 4, lonmax = 11,
output = 'lonlat', nprocs = 1)
dates <- exp$Dates$start
dim(dates) <- c(ftime = 214, sdate = 3)
output <- dates[116:121,]
output[6, 1:2] <- NA
expect_equal(
SelectPeriodOnDates(dates, start = list(25,2), end = list(1,3)),
output)
})
test_that("Decadal", {
# -------- DECADAL ----------#
# decadal: 1 sdate several consequtive years:
dates <- seq(as.Date("01-01-2000", "%d-%m-%Y", tz = 'UTC'),
as.Date("31-12-2005","%d-%m-%Y", tz = 'UTC'), "day")
# No dims -> test .position
output <- c(
seq(as.Date("2000-02-01", "%Y-%m-%d"), as.Date("2000-02-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2001-02-01", "%Y-%m-%d"), as.Date("2001-02-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2002-02-01", "%Y-%m-%d"), as.Date("2002-02-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2003-02-01", "%Y-%m-%d"), as.Date("2003-02-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2004-02-01", "%Y-%m-%d"), as.Date("2004-02-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2005-02-01", "%Y-%m-%d"), as.Date("2005-02-10", "%Y-%m-%d"), 'day'))
dim(output) <- c(ftime = 60)
expect_equal(
SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
output)
data <- array(1:(length(dates)*3),
c(memb = 1, ftime = length(dates), lon = 3))
expect_equal(
SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 2)),
array(c(c(32:41, 398:407, 763:772, 1128:1137, 1493:1502, 1859:1868),
c(32:41, 398:407, 763:772, 1128:1137, 1493:1502, 1859:1868) + 2192,
c(32:41, 398:407, 763:772, 1128:1137, 1493:1502, 1859:1868) + 2 * 2192),
c(ftime = 60, memb = 1, lon = 3)))
output2 <- c(
seq(as.Date("2000-02-01", "%Y-%m-%d"), as.Date("2000-04-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2001-02-01", "%Y-%m-%d"), as.Date("2001-04-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2002-02-01", "%Y-%m-%d"), as.Date("2002-04-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2003-02-01", "%Y-%m-%d"), as.Date("2003-04-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2004-02-01", "%Y-%m-%d"), as.Date("2004-04-10", "%Y-%m-%d"), 'day'),
seq(as.Date("2005-02-01", "%Y-%m-%d"), as.Date("2005-04-10", "%Y-%m-%d"), 'day'))
dim(output2) <- c(ftime = 416)
expect_equal(
SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
output2)
expect_equal(
SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 4)),
array(c(c(32:101, 398:466, 763:831, 1128:1196, 1493:1562, 1859:1927),
c(32:101, 398:466, 763:831, 1128:1196, 1493:1562, 1859:1927) + 2192,
c(32:101, 398:466, 763:831, 1128:1196, 1493:1562, 1859:1927) + 2 * 2192),
c(ftime = 416, memb = 1, lon = 3)))
# 1 dim -> test Apply
dim(dates) <- c(ftime = length(dates))
expect_equal(
SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
output) # no need to check on Data, repited
expect_equal(
SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
output2) # no need to check on Data, repited
# decadal: 5 sdates several consequtive years
dates <- rep(seq(as.Date("01-01-2000", "%d-%m-%Y", tz = 'UTC'),
as.Date("31-12-2005","%d-%m-%Y", tz = 'UTC'), "day"), 5)
dim(dates) <- c(ftime = 2192, sdate = 5)
output3 <- rep(output, 5)
dim(output3) <- c(ftime = 60, sdate = 5)
expect_equal(
SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 2)),
output3)
data <- array(1:(length(dates)*3),
c(memb = 1, sdate = 5, ftime = length(dates)/5, lon = 3))
expect_equal( #To be extended for all sdate dimensions:
SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 2))[,1,1,1],
c(1:10 * 5 + 151, 1:10 * 5 + 1981, 1:10 * 5 + 3806,
1:10 * 5 + 5631, 1:10 * 5 + 7456, 1:10 * 5 + 9286))
output4 <- rep(output2, 5)
dim(output4) <- c(ftime = 416, sdate = 5)
expect_equal(
SelectPeriodOnDates(dates, start = list(1, 2), end = list(10, 4)),
output4)
expect_equal( #To be extended for all ftime dimensions:
SelectPeriodOnData(data, dates, start = list(1, 2), end = list(10, 4))[1,1,,1],
156:160)
# TO BE TRANSFORM INTO TESTS
# Multiple dims: sdate, fyear, ftime
#dates <- CSTools:::SplitDim(dates, indices = dates[,1],
# split_dim = 'ftime', freq = 'year')
#dates <- as.POSIXct(dates*24*3600, origin = '1970-01-01', tz = 'UTC')
#dates[, 1, 1] # no NA is leap year
#dates[, 1, 2] # NA at the end of the year
#res <- Apply(list(dates), target_dims = 'ftime', fun = .position,
# ini_day = 1, ini_month = 2,
# end_day = 10, end_month = 2)$output1
#dim(res) # fine
#res # fine all index the same before 29 Feb
# a <- SelectPeriodOnDates(dates, ini_day = 1, ini_month = 2,
# end_day = 10, end_month = 2)
#all(a[,1,] == a[,2,])
#dim(a)
# data <- array(1:(366*6*5*3),
# c(memb = 1, sdate = 5, year = 6, ftime = 366, lon = 3))
# data[1, ,c(2,3,4,6),366,] <- NA
#b <- SelectPeriodOnData(data, dates, ini_day = 1, ini_month = 2,
# end_day = 10, end_month = 2)
# including leap day:
# res <- Apply(list(dates), target_dims = 'ftime', fun = .position,
# ini_day = 1, ini_month = 2,
# end_day = 10, end_month = 4)$output1
# dim(res) # fine
# dates[res[,1,1],1, 1] # fine
#a <- SelectPeriodOnDates(dates, ini_day = 1, ini_month = 2,
# end_day = 10, end_month = 4)
#dim(a)
#all(a[,1,], a[,2,])
#b <- SelectPeriodOnData(data, dates, ini_day = 1, ini_month = 2,
i# end_day = 10, end_month = 4)
})