-
Notifications
You must be signed in to change notification settings - Fork 3
/
creatingSysData.R
285 lines (238 loc) · 7.3 KB
/
creatingSysData.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
###############################################################################
## The following objects are built and then saved in R/sysdata.rda
###############################################################################
## Define generic constants
# Constant to convert from radians to degrees
.kRadToDegree <- 180 / pi
###############################################################################
## Define special constants
# Flattening constant, f
.kFlatteningConstant <- 1 / 298.257223563
# Eccentricity, e
.kEccentricity <- sqrt(.kFlatteningConstant * (2 - .kFlatteningConstant))
# Earth's semi-major axis, A
.kEarthSemimajorAxis <- 6378137
# Geomagnetic reference radius, a
.kGeomagneticRadius <- 6371200
###############################################################################
## Build lookup table used to speed up calculation of P_{n,m}(mu)
# Import WMM Gauss coefficients
.folderExtdata <- file.path(
'inst',
'extdata'
)
# The following files are in the extdata folder and are formatted versions of
# the WMM coefficients.
.filenamesWMM <- c(
'WMM2000.csv',
'WMM2005.csv',
'WMM2010.csv',
'WMM2015.csv',
'WMM2015v2.csv',
'WMM2020.csv'
)
.pathsWMM <- file.path(
.folderExtdata,
.filenamesWMM
)
.kCoefficientsWMM <- data.table::rbindlist(
lapply(
.pathsWMM,
function(path) {
versionWMM <- tools::file_path_sans_ext(basename(path))
output <- data.table::fread(
path,
sep = '|',
header = TRUE,
colClasses = rep('numeric', 6)
)[
, version := (versionWMM)
]
return(output)
}
),
use.names = TRUE
)
# For programming ease, replace NA values for h and h_dot with 0
.kCoefficientsWMM[
is.na(h)
, h := 0
][
is.na(h_dot)
, h_dot := 0
]
data.table::setkey(
.kCoefficientsWMM,
n, m, version
)
# Partition data.table into list of data.tables split by WMM version.
.kCoefficientsWMM <- split(.kCoefficientsWMM, by = 'version')
# Restate each data.table as a matrix with indices (n, m) for each Gauss
# coefficient
.RestateGaussCoefficient <- function(coefName, coefTable = .kCoefficientsWMM) {
output <- lapply(
coefTable,
function(coefWMM)
as.matrix(
data.table::dcast(coefWMM, n ~ m, value.var = coefName)[
, -c('n')
]
)
)
return(output)
}
.kCoefficientsWMMg <- .RestateGaussCoefficient('g')
.kCoefficientsWMMh <- .RestateGaussCoefficient('h')
.kCoefficientsWMMgDot <- .RestateGaussCoefficient('g_dot')
.kCoefficientsWMMhDot <- .RestateGaussCoefficient('h_dot')
# n is degree & m is order.
# Note: nDegree = 13 needed to calculate P_Schmidt_muDeriv, even though only the
# first 12 degrees are summed.
.kLegendreTemplate <- data.table::data.table(n = 1:13)
.kLegendreTemplate <- .kLegendreTemplate[
, .(m = 0:n)
, by = n
]
# Reshape .kLegendreTemplate and cast as matrix with indices (n, m) and values
# equal to m. This will be used as a template to store computed Legendre values,
# and so the values of order m are not important.
#
# Note: The column names are intentionally off from the column number in order
# to be consistent with the Legendre indices. The column names will be used for
# the order m.
.kLegendreTemplate <- as.matrix(
data.table::dcast(.kLegendreTemplate, n ~ m, value.var = 'm')[
, -c('n')
]
)
###############################################################################
# Index sequence to compute all needed associated legendre functions
.kLegendreSequence <- list(
# n values
'n' = c(
1:13, 1:13, 2:13, 3:13, 4:13, 5:13, 6:13, 7:13, 8:13, 9:13, 10:13, 11:13,
12:13
)
, #m values
'm' = c(
rep(0, 13), rep(1, 13), rep(2, 12), rep(3, 11), rep(4, 10), rep(5, 9),
rep(6, 8), rep(7, 7), rep(8, 6), rep(9, 5), rep(10, 4), rep(11, 3),
rep(12, 2)
)
)
# Define index used for recursion:
# Indices <= 5 mean the first 5 associated Legendre functions are used.
# Index = 6 means the constant m recursion relation is used.
# Index == 7 means the constant n recursion relation is used.
.kLegendreSequence <- data.table::as.data.table(.kLegendreSequence)[
, index := ifelse(
n == 1 & m == 0, 1, ifelse(
n == 1 & m == 1, 2, ifelse(
n == 2 & m == 0, 3, ifelse(
n == 2 & m == 1, 4, ifelse(
n == 2 & m == 2, 5, ifelse(
n > 2 & m <= 1, 6, 7
))))))
]
# Calculate Schmidt normalization factor
.kLegendreSequence[
, normalizationFactor := ifelse(
m == 0,
1,
sqrt(2 * factorial(n - m) / factorial(n + m))
)
]
# Create matrix of normalization factors with indices (n, m)
.kNormalizationFactors <- .kLegendreTemplate
.kNormalizationFactors[1:13, 1:13] <- as.matrix(
data.table::dcast(
.kLegendreSequence,
n ~ m,
value.var = 'normalizationFactor'
)[
, -c('n')
]
)
# .kLegendreSequence[, normalizationFactor := NULL]
# Restate .kLegendreSequence back as list of vectors to use in downstream
# loop, i.e., don't lookup values in data.table, just pull the needed values
# in order of .kLegendreSequence.
# .kLegendreSequence <- as.list(.kLegendreSequence)
###############################################################################
## Create grid of constants representing Legendre degree and order indices
.kDegreeIndexMatrix <- outer(
1:13,
0:13,
FUN = function(n, m) n
)
.kOrderIndexMatrix <- outer(
1:13,
0:13,
FUN = function(n, m) m
)
# Prevent calculating complex numbers for a few lines of code by removing
# values for unneeded Legendre indices
filterUnneededIndices <- as.vector(.kDegreeIndexMatrix < .kOrderIndexMatrix)
.kDegreeIndexMatrix[filterUnneededIndices] <- NA
.kOrderIndexMatrix[filterUnneededIndices] <- NA
###############################################################################
## Create 3-dimensional array to store pre-computed values dependent on Legendre
## degree and order indices
.kLegendreComponents <- array(dim = c(13, 14, 7))
.kIndices <- as.matrix(expand.grid(
1:13,
0:13
))
for (
index in seq(nrow(.kIndices))
) {
n <- .kIndices[index, 1]
m <- .kIndices[index, 2]
legendreSubComponents <- .CalcLegendreComponents(n, m)
.kLegendreComponents[n, m + 1, ] <- c(
legendreSubComponents,
rep(0, 7 - length(legendreSubComponents))
)
}
###############################################################################
.kSelectedIndicesM <- array(dim = c(13, 14, 7))
.kSelectedExponentsM <- array(dim = c(13, 14, 7))
for (
index in seq(nrow(.kIndices))
) {
n <- .kIndices[index, 1]
m <- .kIndices[index, 2]
mSequence <- seq(from = m, to = n)
# Keep only the indices where the binomial coefficient,
# choose((n + k - 1)/2, n), is non-zero.
mSequence <- mSequence[which((n + mSequence - 1) %% 2 == 1)] - m
mRange <- length(mSequence)
.kSelectedIndicesM[n, m + 1, ] <- c(
mSequence,
rep(NA, 7 - mRange)
)
.kSelectedExponentsM[n, m + 1, ] <- c(
rep(m/2, mRange),
rep(NA, 7 - mRange)
)
}
###############################################################################
## Save Objects
# Store objects not directly accessible to user
usethis::use_data(
.kLegendreTemplate,
.kCoefficientsWMMg,
.kCoefficientsWMMh,
.kCoefficientsWMMgDot,
.kCoefficientsWMMhDot,
.kEccentricity,
.kEarthSemimajorAxis,
.kGeomagneticRadius,
.kRadToDegree,
.kLegendreComponents,
.kSelectedIndicesM,
.kSelectedExponentsM,
.kDegreeIndexMatrix,
.kOrderIndexMatrix,
.kNormalizationFactors
,internal = TRUE, overwrite = TRUE)