forked from WilliamCooper/KalmanFilter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
KalmanFilter.R
executable file
·1157 lines (1092 loc) · 47.9 KB
/
KalmanFilter.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Running the KalmanFilter.R script:
## This script can be run in three ways:
## 1. As an R script using a command in a terminal window following this pattern:
## Rscript KalmanFilter.R Project Flight UpdateAKRD[y/N] UpdateSSRD[y,N] Simple[y/N] Interval[10]")
## Example: Rscript KalmanFilter.R CSET rf01 y y n 15
## You can also append a directory for output. The default is DataDirectory()/Project/KF/,
## e.g., /scr/raf_data/WCR-TEST/KF/
## 2. Interactively from an R console or RStudio console:
## Set working directory to the KalmanFilter directory.
## Using R:
## Start R;
## Enter the command "source('KalmanFilter.R')" (without double-quote marks)
## Answer the questions interactively to select the project/flight, etc.
## Using RStudio:
## Set the working directory to the KalmanFilter directory.
## Load the KalmanFilter.R file.
## Click "Source" and answer questions interactively.
## (The above instructions for "Using R" also work from the RStudio console.)
## 3. Use the KalmanFilter Shiny app:
## Start the Shiny app on datavis using this address:
## https://datavis.eol.ucar.edu:3838/KalmanFilter
## Optionally, click the 'help' button.
## Enter the appropriate parameters for processing in the window that appears.
## Click the "Click here ..." button.
##
##
## The run argument form "Flight" can be entered in several different ways:
## a). To process rf05, enter either "5" or "rf05".
## b). To process rf05h, enter "rf05h".
## c). To process tf02, enter "tf02". ("tf" or "ff" is required for test/ferry flights)
## d). To process the next unprocessed low-rate flight, enter "NEXT" (without the double-quote marks)
## e). To process all unprocessed low-rate flights in a given project, enter "ALL".
## f). To process the next unprocessed high-rate flight, enter "NEXTHR".
## g). To process all unprocessed high-rate flights, enter "ALLHR".
## The action differs for these choices if a processed file already exists:
## For a), b) or c) the processed file will be overwritten without warning.
## For d), e) f) or g) only files not previously processed will be processed.
print (sprintf ('Kalman Processor is starting -- %s', Sys.time()))
thisFileName <- "KalmanFilter"
require(Ranadu, quietly = TRUE, warn.conflicts=FALSE)
require(numDeriv, quietly = TRUE, warn.conflicts=FALSE) ## needed for the jacobian() function
# library(signal) ## used for filtering -- but ::signal avoids error message
library(reshape2) ## used with ggplot facet plots
library(grid)
options(stringsAsFactors=FALSE)
## See if this is being run as user "shiny":
SHINY <- ifelse(Sys.info()['user'] == 'shiny', TRUE, FALSE)
# setwd ('~/RStudio/KalmanFilter')
##----------------------------------------------------------
## These are the run options to set via command-line or UI:
GeneratePlots <- TRUE
ShowPlots <- FALSE ## leave FALSE for batch runs or shiny-app runs
UpdateAKRD <- FALSE
UpdateSSRD <- FALSE
SimpleOnly <- FALSE ## set TRUE to use CorrectPitch/CorrectHeading w/o KF
ALL <- FALSE
ALLHR <- FALSE
NEXT <- FALSE
NEXTHR <- FALSE
NSTEP <- 15 ## update interval
HighRate <- FALSE
Directory <- DataDirectory ()
KFDirectory <- ifelse(SHINY, 'KFoutput', Directory)
##----------------------------------------------------------
Project <- 'WCR-TEST'
ProjectDir <- Project
Flight <- 1
## Functions to supply next flight for which processing is needed:
getNext <- function(ProjectDir, Project) {
Fl <- sort (list.files (sprintf ("%s%s/", Directory, ProjectDir),
sprintf ("%srf..KF.nc", Project)), decreasing = TRUE)[1]
## Consider also if a processed version exists in KF or KFoutput:
Fl <- sort (c(Fl,
list.files (sprintf ("%s%s/KF/", Directory, ProjectDir),
sprintf ("%srf..KF.nc", Project)),
list.files ("KFoutput", sprintf ("%srf..KF.nc", Project))),
decreasing = TRUE)[1]
if (is.na (Fl)) {
Flight <- 1
} else {
Flight <- sub (".*rf", '', sub ("KF.nc", '', Fl))
Flight <- as.numeric(Flight)+1
}
return (Flight)
}
getNextHR <- function(ProjectDir, Project) {
Fl <- sort (list.files (sprintf ("%s%s/", DataDirectory (), ProjectDir),
sprintf ("%srf..hKF.nc", Project)), decreasing = TRUE)[1]
## Consider also if a processed version exists in KF or KFoutput:
Fl <- sort (c(Fl,
list.files (sprintf ("%s%s/KF/", DataDirectory (), ProjectDir),
sprintf ("%srf..hKF.nc", Project)),
list.files ("KFoutput", sprintf ("%srf..hKF.nc", Project))),
decreasing = TRUE)[1]
if (is.na (Fl)) {
Flight <- 1
} else {
Flight <- sub (".*rf", '', sub ("hKF.nc", '', Fl))
Flight <- as.numeric(Flight)+1
}
return (Flight)
}
if (!interactive()) { ## can run interactively or via Rscript; this is for Rscript:
run.args <- commandArgs (TRUE)
if (length (run.args) > 0) {
if (nchar(run.args[1]) > 1) {
Project <- run.args[1]
ProjectDir <- Project
}
} else {
print ("Usage: Rscript KalmanFilter.R Project Flight UpdateAKRD[y/N] UpdateSSRD[y,N] Simple[y/N] Interval[10]")
print ("Example: Rscript KalmanFilter.R CSET rf01 y y n 15")
stop("exiting...")
}
## Flight
if (length (run.args) > 1) {
if (run.args[2] == 'ALL') {
flight <- run.args[2]
ALL <- TRUE
} else if (run.args[2] == 'ALLHR') {
flight <- run.args[2]
ALLHR <- TRUE
HighRate <- TRUE
} else if (run.args[2] == 'NEXT') {
## Find max rf in data directory,
## Use as default if none supplied via command line:
flight <- sprintf('rf%02d', getNext(ProjectDir, Project))
} else if (run.args[2] == 'NEXTHR') {
HighRate <- TRUE
## Find max rf in data directory,
## Use as default if none supplied via command line:
flight <- sprintf('rf%02d', getNextHR(ProjectDir, Project))
HighRate <- TRUE
} else {
## Possible assignments are 1,2,..., rf01,..., rf01h,..., tf01,..., ff01..., tf01h,..., ff01h,...
if (!grepl('f', run.args[2])) { ## only number is supplied; interpret as rf
fno <- as.numeric(run.args[2])
flight <- paste0('rf', sprintf('%02d', fno))
} else {
flight <- run.args[2]
if (grepl('h', run.args[2])) {
HighRate <- TRUE
}
}
}
}
if (length (run.args) > 2) {
UpdateAKRD <- run.args[3]
if (UpdateAKRD == 'y' || UpdateAKRD == 'Y' || UpdateAKRD == 'TRUE') {
UpdateAKRD <- TRUE
} else {
UpdateAKRD <- FALSE
}
}
if (length (run.args) > 3) {
UpdateSSRD <- run.args[4]
if (UpdateSSRD == 'y' || UpdateSSRD == 'Y' || UpdateSSRD == 'TRUE') {
UpdateSSRD <- TRUE
} else {
UpdateSSRD <- FALSE
}
}
if (length (run.args) > 4) {
SimpleOnly <- run.args[5]
if (SimpleOnly == 'y' || SimpleOnly == 'Y' || SimpleOnly == 'TRUE') {
SimpleOnly <- TRUE
} else {
SimpleOnly <- FALSE
}
}
if (length (run.args) > 5) {
NSTEP <- as.numeric (run.args[6])
}
if (length (run.args) > 6) {
outputDirectory <- run.args[7]
} else {
outputDirectory <- paste0(DataDirectory(), Project, '/KF/')
}
if (length (run.args) > 7) {
GeneratePlots <- run.args[8]
if (GeneratePlots == 'y' || GeneratePlots == 'Y' || GeneratePlots == 'TRUE') {
GeneratePlots <- TRUE
} else {
GeneratePlots <- FALSE
}
}
} else { ## This is the interactive section
x <- readline (sprintf ("Project is %s; CR to accept or enter new project name: ", Project))
if (nchar(x) > 1) {
Project <- x
if (grepl ('HIPPO', Project)) {
ProjectDir <- 'HIPPO'
} else {
ProjectDir <- Project
}
}
x <- readline (sprintf ("Flight is %s; CR to accept, number or 'ALL' or 'NEXT' for new flight name: ", Flight))
if (x == 'ALL') {
ALL <- TRUE
flight <- 'ALL'
} else if (x == 'ALLHR') {
ALLHR <- TRUE
flight <- 'ALLHR'
HighRate <- TRUE
} else if (x == 'NEXT') {
flight <- sprintf('rf%02d', getNext(ProjectDir, Project))
} else if (x == 'NEXTHR') {
flight <- sprintf('rf%02d', getNextHR(ProjectDir, Project))
HighRate <- TRUE
} else if (nchar(x) > 0) {
if (!is.na(as.numeric(x))) {
flight <- sprintf('rf%02d', as.numeric(x))
} else {
flight <- x
if (grepl('h', flight)) {
HighRate <- TRUE
}
}
}
x <- readline (sprintf ("newAK is %s; CR to accept, Y or T to enable, N or F to disable: ", UpdateAKRD))
if (nchar(x) > 0)
UpdateAKRD <- ifelse ((grepl('^T', x) || grepl('^Y', x)), TRUE, FALSE)
x <- readline (sprintf ("newSS is %s; CR to accept, Y or T to enable, N or F to disable: ", UpdateSSRD))
if (nchar(x) > 0)
UpdateSSRD <- ifelse ((grepl('^T', x) || grepl('^Y', x)), TRUE, FALSE)
x <- readline (sprintf ("simple is %s; CR to accept, Y or T to enable, N or F to disable: ", SimpleOnly))
if (nchar(x) > 0)
SimpleOnly <- ifelse ((grepl('^T', x) || grepl('^Y', x)), TRUE, FALSE)
x <- readline (sprintf ("NSTEP is %d, CR to accept or number to change: ", NSTEP))
if (nchar(x) > 0 && !is.na(as.numeric(x))) {
NSTEP <- as.numeric(x)
}
outputDirectory <- paste0(DataDirectory(), Project, '/KF/')
x <- readline (sprintf ("Output directory is %s; CR to accept, full path entry to change", outputDirectory))
if (nchar(x) > 0) {
outputDirectory <- x
}
x <- readline (sprintf ("Generate Plots is %s; CR to accept, Y or T to enable, N or F to disable: ", GeneratePlots))
if (nchar(x) > 0)
GeneratePlots <- ifelse ((grepl('^T', x) || grepl('^Y', x)), TRUE, FALSE)
}
print (sprintf ('run controls: Project: %s; Flight: %s; UpdateAKRD: %s; UpdateSSRD: %s; SimpleOnly: %s; Time increment: %d',
Project, flight, UpdateAKRD, UpdateSSRD, SimpleOnly, NSTEP))
if (!SHINY) {
print (sprintf ('The output directory will be %s', outputDirectory))
}
## Here is the "ALL" loop:
if (ALL) {
Fl <- sort (list.files (sprintf ("%s%s/", DataDirectory (), ProjectDir),
sprintf ("%srf...nc", Project)))
FlKF <- sort (list.files (sprintf ("%s%s/", DataDirectory (), ProjectDir),
sprintf ("%srf..KF.nc", Project)))
FlKF <- sort (c(FlKF,
list.files (sprintf ("%s%s/KF/", DataDirectory (), ProjectDir),
sprintf ("%srf..KF.nc", Project)),
list.files ('KFoutput',
sprintf ("%srf..KF.nc", Project))))
if (is.na (Fl[1])) {
print (sprintf ('no files to process for project %s', Project))
exit()
} else {
ip <- pmatch(sub('.nc', '', Fl), sub('KF.nc', '', FlKF))
if (all(!is.na(ip))) {
print (sprintf ('no unprocessed files in project directory for %s', Project))
exit()
} else if (any(!is.na(ip))) {
Fl <- Fl[-ip[!is.na(ip)]]
}
## now process each flight in Fl
Fl <- sub('.nc', '', sub(Project, '', Fl))
print(' ALL loop: These files will be processed:')
print(Fl)
}
} else if(ALLHR) {
Fl <- sort (list.files (sprintf ("%s%s/", DataDirectory (), ProjectDir),
sprintf ("%srf..h.nc", Project)))
FlKF <- sort (list.files (sprintf ("%s%s/", DataDirectory (), ProjectDir),
sprintf ("%srf..hKF.nc", Project)))
FlKF <- sort (c(FlKF,
list.files (sprintf ("%s%s/KF/", DataDirectory (), ProjectDir),
sprintf ("%srf..hKF.nc", Project)),
list.files ('KFoutput',
sprintf ("%srf..hKF.nc", Project))))
if (is.na (Fl[1])) {
print (sprintf ('no files to process for project %s', Project))
exit()
} else {
ip <- pmatch(sub('.nc', '', Fl), sub('KF.nc', '', FlKF))
if (all(!is.na(ip))) {
print (sprintf ('no unprocessed files in project directory for %s', Project))
exit()
} else if (any(!is.na(ip))) {
Fl <- Fl[-ip[!is.na(ip)]]
}
## now process each flight in Fl
Fl <- sub('.nc', '', sub(Project, '', Fl))
print(' ALLHR loop: These files will be processed:')
print(Fl)
HighRate <- TRUE
}
} else {
Fl <- flight
}
for (flight in Fl) {
if(HighRate) {
fname = sprintf("%s%s/%s%s.nc", Directory, ProjectDir, Project, flight)
## Check if the LR KF file exists; if not, branch out of loop
fcheck <- sub('h.nc', 'KF.nc', fname)
fcheck2 <- paste0('KFoutput/', sub('.*/', '', sub('h.nc$', 'KF.nc', fname)))
fcheck3 <- sub(Project, paste0(Project, '/KF'), fcheck)
if (!file.exists(fcheck) && !file.exists(fcheck2) && !file.exists(fcheck3)) {
print (sprintf ('LR processed KF file %s or %s not present; skipping file %s',
fcheck, fcheck2, fname))
next
}
} else {
fname = sprintf("%s%s/%s%s.nc", Directory, ProjectDir, Project, flight)
}
# function to copy attributes from old variable (e.g., PITCH) to new one (e.g., PITCHKF)
# (placed here because it is used for both LR and HR processing)
copy_attributes <- function (atv, v, nfile) {
for (i in 1:length(atv)) {
aname <- names(atv[i])
if (grepl ('name', aname)) {next} # skips long and standard names
if (grepl ('units', aname)) {next}
if (grepl ('Dependencies', aname)) {next}
if (grepl ('actual_range', aname)) {next}
if (is.numeric (atv[[i]])) {
ncatt_put (nfile, v, attname=aname, attval=as.numeric(atv[[i]]))
} else {
ncatt_put (nfile, v, attname=aname, attval=as.character (atv[[i]]))
}
}
}
## There are two main segments, the first for HR and the second for LR.
## That permits both to be merged into one script.
if (HighRate) {
print (sprintf ('reading LR data -- %s', Sys.time()))
VarList <- c('LAT', 'LON', 'VEW', 'VNS', 'PITCH', 'ROLL', 'THDG',
'LATKF', 'LONKF', 'ALTKF', 'VEWKF', 'VNSKF', 'ROCKF', 'ATX',
'AKKF', 'SSKF', 'ADIFR', 'BDIFR', 'QCF', 'TASX', 'GGVSPD', 'VSPD',
'GGVEW', 'GGVNS',
'PITCHKF', 'ROLLKF', 'THDGKF', 'PSXC', 'GGLAT', 'GGALT', 'ACINS')
fname1 <- setFileName(Project, paste0(sub('h$', '', flight), 'KF'))
fname1b <- paste0('KFoutput/', Project, sub('h$', '', flight), 'KF.nc')
fname1c <- sub(sprintf('%s/', Project), sprintf('%s/KF/', Project), fname1)
if(file.exists(fname1b)) {
fname1 <- fname1b
}
if(file.exists(fname1c)) {
fname1 <- fname1c
}
D1 <- getNetCDF(fname1, VarList)
Rate <- 1
D1$Grav <- Gravity (D1$LAT, D1$GGALT)
Cradeg <- pi/180
Data <- D1 # ROC uses data.frame Data
print (sprintf ('Kalman-loop 5%% done'))
source('chunks/ROC.R')
Cor <- rep(0, nrow(D1)*9)
dim(Cor) <- c(nrow(D1), 9)
D1 <- Data
Cor[, 1] = with(D1, LATKF - LAT)
Cor[, 2] = with(D1, LONKF - LON)
Cor[, 3] = with(D1, ALTKF - ZROC)
Cor[, 4] = with(D1, VEWKF - VEW)
Cor[, 5] = with(D1, VNSKF - VNS)
Cor[, 6] = with(D1, ROCKF - ROC)
Cor[, 7] = with(D1, PITCHKF - PITCH)
Cor[, 8] = with(D1, ROLLKF - ROLL)
Cor[, 9] = with(D1, THDGKF - THDG)
rm(Data)
print (sprintf ('reading HR data -- %s', Sys.time()))
print (sprintf ('Kalman-loop 10%% done'))
## Get the needed variables from the 25-Hz file
fname25 <- setFileName(Project, flight)
FI <- DataFileInfo(fname25)
VarList <- VarList[-which(!(VarList %in% FI$Variables))]
D25 <- getNetCDF(fname25, VarList)
D25$Grav <- Gravity (D25$LAT, D25$GGALT)
Data <- D25
Rate <- 25
print (sprintf ('ROC HR -- %s', Sys.time()))
print (sprintf ('Kalman-loop 15%% done'))
source('chunks/ROC.R')
D25 <- Data
rm(Data)
## interpolate the results over the full data.frame:
IntFilter <- function (X, inRate, outRate) {
if (inRate == outRate) {return (X)}
ratio <- as.integer(outRate/inRate) ## expected to be an integer
x <- 0:(length(X)-1)
A <- stats::approx (x, X, n=length(X)*ratio-ratio+1)
T <- A$y
T <- signal::filter(signal::sgolay(4,75),T)
## now shift to match Rate:
n <- as.integer (ratio / 2)
NL = length(T)
T <- c(rep(T[1],n), T, rep(T[NL],ratio-n-1)) ## OK, even or odd ratio
return (T)
}
## find time overlap
SE1 <- getStartEnd(D1)
SE25 <- getStartEnd(D25)
nsec <- function(T) {
as.integer(T %% 100 + (T %/% 100) * 60 + (T %/% 10000) * 3600)
}
if(SE1[1] > SE25[1]) {
gap <- nsec(SE1[1]) - nsec(SE25[1])
for (n in 1:gap) {
Cor <- rbind(Cor[1,], Cor)
}
} else if (SE1[1] < SE25[1]) {
gap <- nsec(SE25[1]) - nsec(SE1[1])
Cor <- Cor[(gap+1):nrow(Cor), ]
}
if(SE1[2] > SE25[2]) {
gap <- nsec(SE1[2]) - nsec(SE25[2])
Cor <- Cor[1:(nrow(Cor) - gap), ]
} else if (SE1[2] < SE25[2]) {
gap <- nsec(SE25[2]) - nsec(SE1[2])
if (gap > 0) {
for (n in 1:gap) {
Cor <- rbind(Cor, Cor[nrow(Cor), ])
}
}
}
print (sprintf ('interpolation -- %s', Sys.time()))
print (sprintf ('Kalman-loop 25%% done'))
C25 <- vector('numeric', nrow(D25) * 9)
dim (C25) <- c(nrow(D25), 9)
for (j in 1:9) {
# print (sprintf ('j=%d, nrow(D25)=%d', j, nrow(D25)))
# print (str(IntFilter (Cor[, j], 1, 25)))
C25[, j] <- IntFilter (Cor[, j], 1, 25)
}
## ----newvars, include = TRUE-------------------------------------------------------
D25$LATKF <- D25$LAT + C25[, 1]
D25$LONKF <- D25$LON + C25[, 2]
D25$ALTKF <- D25$ZROC + C25[, 3]
D25$VEWKF <- D25$VEW + C25[, 4]
D25$VNSKF <- D25$VNS + C25[, 5]
D25$ROCKF <- D25$ROC + C25[, 6]
D25$PITCHKF <- D25$PITCH + C25[, 7]
D25$ROLLKF <- D25$ROLL + C25[, 8]
D25$THDGKF <- D25$THDG + C25[, 9]
## ----radome, include = TRUE--------------------------------------------------------
Platform <- ifelse (grepl('130', attr(D1, 'Platform')), "C130", "GV")
cffn <- 19.70547
cff <- 21.481
cfs <- c(4.525341674, 19.933222011, -0.001960992)
if (grepl('130', attr(D1, 'Platform'))) { # C-130 WECAN values
cff <- 10.3123
cfs <- c(5.6885, 14.0452, -0.00461)
}
print (sprintf ('wind calculation -- %s', Sys.time()))
print (sprintf ('Kalman-loop 50%% done'))
D25$QR <- D25$ADIFR / D25$QCF
D25$QR[D25$QCF < 20] <- NA
D25$QR[is.infinite(D25$QR)] <- NA
CutoffFreq <- 600 * Rate
D25$QRS <- zoo::na.approx (as.vector(D25$QR), maxgap=1000*Rate, na.rm = FALSE, rule=2)
D25$QRS[is.na(D25$QRS)] <- 0
D25$QRS <- signal::filtfilt (signal::butter (3, 2/CutoffFreq), D25$QRS)
D25$QRF <- D25$QR - D25$QRS
D25$QCFS <- zoo::na.approx (as.vector(D25$QCF), maxgap=1000*Rate, na.rm = FALSE)
D25$QCFS[is.na(D25$QCFS)] <- 0
D25$QCFS <- signal::filtfilt (signal::butter (3, 2/CutoffFreq), D25$QCFS)
D25$AKKF <- cff * D25$QRF + cfs[1] + cfs[2] * D25$QRS + cfs[3] * D25$QCFS
D25$AKKF[D25$QCF < 10] <- NA
D25$SSKF <- 0.008 + 22.301 * D25$BDIFR / D25$QCF
if (grepl('130', attr(D25, 'Platform'))) { # C-130 WECAN values; use with heading offset +0.76
D25$SSKF <- 0.85 + 12.6582 * D25$BDIFR / D25$QCF
}
D25$SSKF[D25$QCF < 10] <- NA
## ----getWind, include = TRUE-------------------------------------------------------
## This follows "chunks/wind-calculation.R" from the KalmanFilter directory
DataW <- D25
DataW$ATTACK <- DataW$AKKF ## These are the values used in WindProcessor()
DataW$SSLIP <- DataW$SSKF
DataN <- WindProcessor (DataW, AC = Platform) ## This function is in Ranadu
D25$WICC <- DataN$WIN
D25$WDCC <- DataN$WDN
D25$WSCC <- DataN$WSN
DataW$PITCH <- D25$PITCHKF
DataW$ROLL <- D25$ROLLKF
DataW$THDG <- D25$THDGKF
DataW$VEW <- D25$VEWKF
DataW$VNS <- D25$VNSKF
DataW$GGVSPD <- D25$ROCKF
print (sprintf ('Kalman-loop 75%% done'))
DataN <- WindProcessor(DataW, AC = Platform, CompF=FALSE) ## suppress comp filter and GPS lever arm)
D25$WDKF <- DataN$WDN
D25$WSKF <- DataN$WSN
D25$WIKF <- DataN$WIN
## add longitudinal and lateral components analogous to UXC and VYC:
.hdg <- D25$THDGKF * Cradeg
.wd <- D25$WDKF * Cradeg + pi
D25$UXKF <- D25$WSKF * (sin(.hdg)*sin(.wd) + cos(.hdg)*cos(.wd))
.hdg <- .hdg - pi/2
D25$VYKF <- D25$WSKF * (sin(.hdg)*sin(.wd) + cos(.hdg)*cos(.wd))
DataW$GGVSPD <- D25[, 'GGVSPD'] ## std script has code to substitute for GGVSPD
DataN <- WindProcessor(DataW, AC = Platform, CompF=FALSE) ## suppress comp filter and GPS lever arm)
D25$WIKFG <- DataN$WIN
print (sprintf ('making new netCDF 25%%'))
## ----newCDF, include = TRUE--------------------------------------------------------
## create-new-netcdf.R
print (sprintf ('create new netCDF file -- %s', Sys.time()))
fnew <- sub ('h.nc', 'hKF.nc', fname25)
fnew <- paste0(outputDirectory, sub('.*/', '', fnew))
if(SHINY) { ## for the datavis shiny server, only this output is allowed:
fnew <- paste0('KFoutput/', sub('.*/', '', fnew))
}
print (sprintf ('making new netCDF file named %s', fnew))
## Make the directory KF if needed:
if (grepl ('/KF/', fnew)) {
if (!dir.exists(outputDirectory)) {
dr <- dir.create(outputDirectory)
print (sprintf ('creating the output directory named %s; return is %s',
outputDirectory, dr))
}
}
## beware: overwrites without warning!!
Z <- file.copy (fname25, fnew, overwrite=TRUE) ## BEWARE: overwrites without warning!!
## ----writeCDF, include = FALSE-----------------------------------------------------
## modify-new-netcdf.R
## variables needed for attributes:
VarListRef <- VarList
FI <- DataFileInfo (fname25)
if (!('GGVSPD' %in% FI$Variables)) {
if ('GGVSPDB' %in% FI$Variables) {
VarList [which (VarList == 'GGVSPD')] <- 'GGVSPDB'
} else if ('VSPD_A' %in% FI$Variables) {
VarList [which (VarList == 'GGVSPD')] <- 'VSPD_A'
} else if ('VSPD_G' %in% FI$Variables) {
VarList [which (VarList == 'GGVSPD')] <- 'VSPD_G'
} else {
print ('ERROR: no VSPD variable found')
exit()
}
}
for (Var in VarList) {
if (!(Var %in% FI$Variables)) {
print (sprintf (' required variable %s not found in file %s; skipping...', Var, fname))
exit()
}
}
netCDFfile <- nc_open (fnew, write=TRUE)
Rate <- 25
Dimensions <- attr (D25, "Dimensions")
Dim <- Dimensions[["Time"]]
if ("sps25" %in% names (Dimensions)) {
Rate <- 25
Dim <- list(Dimensions[["sps25"]], Dimensions[["Time"]])
}
if ("sps50" %in% names (Dimensions)) {
Rate <- 50
Dim <- list(Dimensions[["sps50"]], Dimensions[["Time"]])
}
DATT <- D25 ## save to ensure that attributes are preserved
## variables to add to the netCDF file:
VarNew <- c('LATKF', 'LONKF', 'ALTKF', 'VEWKF', 'VNSKF', 'ROCKF', 'PITCHKF', 'ROLLKF', 'THDGKF',
'WDKF', 'WSKF', 'WIKF', 'WICC', 'WDCC', 'WSCC', 'UXKF', 'VYKF',
'AKKF', 'SSKF')
VarOld <- c('LAT', 'LON', 'ALT', 'VEW', 'VNS', 'VSPD', 'PITCH', 'ROLL', 'THDG',
'WD', 'WS', 'WI', 'WIC', 'WDC', 'WSC', 'UXC', 'VYC', 'AKRD', 'SSRD')
VarUnits <- c('degrees', 'degrees', 'm', 'm/s', 'm/s', 'm/s', 'degrees', 'degrees', 'degrees',
'degrees', 'm/s', 'm/s', 'm/s', 'degrees', 'm/s', 'm/s', 'm/s',
'degrees', 'degrees')
VarStdName <- c('latitude, KF', 'longitude, KF', 'altitude MSL, KF',
'eastward groundspeed, KF', 'northward groundspeed, KF', 'rate of climb, KF',
'pitch, KF', 'roll, KF', 'heading, KF',
'wind direction, KF', 'wind speed, KF', 'vertical wind, KF',
'vertical wind, KF and GGVSPD', 'WIC recalc', 'WDC recalc', 'WSC recalc',
'longitudinal wind, KF', 'lateral wind, KF',
'angle of attack, KF', 'sideslip, KF')
VarLongName <- c('INS latitude, Kalman-filter-corrected',
'INS longitude, Kalman-filter-corrected',
'INS altitude, Kalman-filter-corrected',
'INS eastward ground speed, Kalman-filter-corrected',
'INS northward ground speed, Kalman-filter-corrected',
'INS rate of climb, Kalman-filter-corrected',
'INS aircraft pitch angle, Kalman-filter-corrected',
'INS aircraft roll angle, Kalman-filter-corrected',
'INS aircraft true heading angle, Kalman-filter-corrected',
'horizontal wind direction, Kalman-filter-corrected',
'horizontal wind speed, Kalman-filter-corrected',
'vertical wind speed, Kalman-filter-corrected',
'original WIC, recalculated',
'original WDC, recalculated',
'original WSC, recalculated',
'longitudinal component of the horizontal wind, Kalman-filter-corrected',
'lateral component of the horizontal wind, Kalman-filter-corrected',
'angle of attack, from Kalman-filter routine', 'sideslip, from Kalman-filter routine')
## create the new variables
varCDF <- list ()
for (i in 1:length(VarNew)) {
print (sprintf ('new-netcdf %d%% done', as.integer(100*(i-1)/length(VarNew))))
varCDF[[i]] <- ncvar_def (VarNew[i],
units=VarUnits[i],
dim=Dim,
missval=as.single(-32767.), prec='float',
longname=VarLongName[i])
if (i == 1) {
newfile <- ncvar_add (netCDFfile, varCDF[[i]])
} else {
newfile <- ncvar_add (newfile, varCDF[[i]])
}
ATV <- ncatt_get (netCDFfile, VarOld[i])
copy_attributes (ATV, VarNew[i], newfile)
ncatt_put (newfile, VarNew[i], attname="standard_name",
attval=VarStdName[i])
ncvar_put (newfile, varCDF[[i]], D25[, VarNew[i]], count=c(25, nrow(D25)/25))
}
nc_close (newfile)
} else { ## The LR segment begins here:
Cradeg <- pi/180
OmegaE <- StandardConstant ('Omega') ## Earth's rotation rate
OmegaE <- 15*Cradeg/3600 ## better match to INS?
Ree <- 6378137 ## for radii of curvature
Ecc <- 0.08181919
source ('chunks/RotationCorrection.R')
source ('chunks/STMFF.R')
## adjust GPS velocity components for GPS antenna location
FI_KF <- DataFileInfo (fname, LLrange=FALSE)
LG <- ifelse (grepl('130', FI_KF$Platform), -9.88, -4.30)
MaxGap <- 1000
.span <- 25
## These very small adjustments prevent gradual ramping during the flight.
# D1$BROLLR <- D1$BROLLR + 0.00026
# D1$BPITCHR <- D1$BPITCHR + 0.00026
## ----new-data, include=TRUE, cache=FALSE---------------------------------
SaveRData2 <- sprintf("%s2.Rdata", thisFileName)
SaveRData <- sprintf("%s.Rdata", thisFileName)
print ('reading netCDF file')
source ('chunks/AcquireData.R')
## add ROC variable
source ('chunks/ROC.R')
# VV <- c('BLONGA', 'BLATA', 'BNORMA')
# .shift <- c(-50,-50,-50)
# names(.shift) <- VV
# for (V in VV) {
# Data[, V] <- ShiftInTime (Data[, V], Rate, .shift[V])
# }
# # Data[, 'VSPD'] <- ShiftInTime (Data[, 'VSPD'], Rate, 40)
# # Data[, 'VEW'] <- ShiftInTime (Data[, 'VEW'], Rate, 60)
# # Data[, 'VNS'] <- ShiftInTime (Data[, 'VNS'], Rate, 60)
# Data[, 'PITCH'] <- ShiftInTime (Data[, 'PITCH'], Rate, 20)
# Data[, 'ROLL'] <- ShiftInTime (Data[, 'ROLL'], Rate, 20)
.shift = 60
D1 <- Data ## make adjustments to a copy; avoid changing original
## adjustments:
source ('chunks/AdjustCal.R')
## GPS shifted vs INS in AdjustCal.R
## interpolate and protect against missing values
VV <- c('LAT', 'LON', 'ZROC', 'VEW', 'VNS', 'ROC', 'PITCH', 'ROLL', 'THDG',
'BPITCHR', 'BROLLR', 'BYAWR')
DSave <- D1
for (V in VV) {
D1[, V] <- zoo::na.approx (as.vector (D1[, V]), maxgap=1000*Rate, na.rm=FALSE, rule=2)
D1[is.na(D1[, V]), V] <- 0
}
D1 <- transferAttributes(DSave, D1)
## transform to the a-frame for comparison to the IRU:
VL <- matrix(c(D1$VEW, D1$VNS, D1$VSPD), ncol=3)
LA <- matrix (c(D1$vedot, D1$vndot, -D1$vudot - D1$Grav), ncol=3) + RotationCorrection (D1, VL)
AA <- XformLA (D1, LA, .inverse=TRUE)
AA[,3] <- AA[,3] - D1$Grav
fa1 <- lm(D1$BLONGA ~ AA[, 1])
fa2 <- lm(D1$BLATA ~ AA[, 2])
fa3 <- lm(D1$BNORMA ~ AA[, 3])
fm1 <- lm (D1$vedot ~ D1$LACCX)
fm2 <- lm (D1$vndot ~ D1$LACCY)
fm3 <- lm (D1$vudot ~ D1$LACCZ)
cfa1 <- coef(fa1); cfa2 <- coef(fa2); cfa3 <- coef(fa3)
# save(cfa1, cfa2, cfa3, file="./BodyAccCal.Rdata") ## Don't over-write; data from new file.
## ----Kalman-setup, include=TRUE, cache=CACHE-----------------------------
## the chunk is 'sourced' here so the same code can be used in KalmanFilter.R
# source ('chunks/Kalman-setup.R')
## Kalman-setup
## initialize matrices needed by the Kalman filter and load the starting-point
## for the error-state vector.
if (!SimpleOnly) {
source ('chunks/Kalman-setup.R')
}
PC <- CorrectPitch(D1, .span=901)
D1$PC <- PC[, 1]
D1$RC <- PC[, 2]
D1$HC <- CorrectHeading(D1, .plotfile='KFplots/HCPlot.pdf')
## ----Kalman-loop, include=TRUE, cache=CACHE------------------------------
if (!SimpleOnly) {
print (sprintf ('main loop is starting -- %s', Sys.time()))
source ('chunks/Kalman-loop.R')
print (sprintf ('main loop is done -- %s', Sys.time()))
}
## ----plot-Kalman-position, include=TRUE,
## Avoid some bad measurements at the start and end
## (e.g., from two-pass filtering of LAT/LON)
minT <- D1$Time[1]- as.integer (D1$Time[1]) %% 60 + 120
maxT <- D1$Time[nrow(D1)]- as.integer (D1$Time[nrow(D1)]) %% 60 - 120
r1 <- max(which(D1$Time == minT)[1], which(D1$TASX > 90)[1])
r2 <- which(D1$TASX > 90); r2 <- r2[length(r2)]
r2 <- min(which(D1$Time == maxT)[1], r2)
r <- r1:r2
DP <- D1[r, ]
DP <- transferAttributes(D1, DP)
if (GeneratePlots && !SimpleOnly) {
print ("generating plots")
png (file='KFplots/Position.png', width=900, height=600, res=150)
print (ggplotWAC (with (DP,
data.frame(Time, DLONM, CLONM, DLATM, CLATM, DALT, CALT)),
ylab=expression (paste ('difference in position [km]')),
panels=3,
labelL=c('KF-GPS', 'correction'),
labelP=c('east', 'north', 'up'),
legend.position=c(0.2,0.68), theme.version=1,
gtitle=sprintf ('%s Flight %s', Project, flight)))
invisible(dev.off())
if (ShowPlots) {
print (ggplotWAC (with (DP,
data.frame(Time, DLONM, CLONM, DLATM, CLATM, DALT, CALT)),
ylab=expression (paste ('difference in position [km]')),
panels=3,
labelL=c('KF-GPS', 'correction'),
labelP=c('east', 'north', 'up'),
legend.position=c(0.2,0.68), theme.version=1))
}
pct <- as.integer(100/8)
print (sprintf ('figures %d%% done', pct))
sdZ <- sd(DP$DALT, na.rm=TRUE)
sdvew <- sd(DP$GGVEW - DP$VEWKF, na.rm=TRUE)
sdvns <- sd(DP$GGVNS - DP$VNSKF, na.rm=TRUE)
meanvz <- mean(DP$ROCKF - DP[, VROC], na.rm=TRUE)
sdvz <- sd(DP[, VROC] - DP$ROCKF, na.rm=TRUE)
## ----plot-Kalman-velocity
png(file='KFplots/Velocity.png', width=900, height=600, res=150)
print (ggplotWAC (with (DP,
data.frame(Time, DVEW, CVEW, DVNS, CVNS, DROC, CROC)),
ylab=expression (paste ('velocity component [m ',s^-1,']')),
panels=3,
labelL=c('KF-GPS', 'correction'),
labelP=c('east', 'north', 'up'),
legend.position=c(0.2, 0.97), theme.version=1,
gtitle=sprintf ('%s Flight %s', Project, flight)))
invisible(dev.off())
if (ShowPlots) {
print (ggplotWAC (with (DP,
data.frame(Time, DVEW, CVEW, DVNS, CVNS, DROC, CROC)),
ylab=expression (paste ('velocity component [m ',s^-1,']')),
panels=3,
labelL=c('KF-GPS', 'correction'),
labelP=c('east', 'north', 'up'),
legend.position=c(0.2, 0.97), theme.version=1))
}
pct <- as.integer(100*2/8)
print (sprintf ('figures %d%% done', pct))
## ----errors-in-pitch,
## must construct using many of the elements of ggplotWAC but in this order:
attr(DP$CPLF, 'filtered') <- TRUE
attr(DP$CRLF, 'filtered') <- TRUE
attributes(DP$SDCPL) <- NULL
attributes(DP$SDCRL) <- NULL
d1 <- with(DP, data.frame(Time, CPL, CPLF, CRL, CRLF))
lines_per_panel <- 2; panels <- 2
labelL=c('KF value', 'smoothed')
labelP=c('pitch', 'roll')
DLP <- nrow(d1)
VarGroup <- rep (gl (lines_per_panel, DLP, labels=labelL), panels)
PanelGroup <- gl (panels, lines_per_panel*DLP, labels=labelP)
dd <- data.frame(reshape2::melt(d1, 1), VarGroup, PanelGroup)
lvl <- levels(dd$VarGroup)
d2 <- with(DP, data.frame(Time, "plo"=CPLF-2*SDCPL, "rlo"=CRLF-2*SDCRL,
"phi"=CPLF+2*SDCPL, "rhi"=CRLF+2*SDCRL))
attributes(d2$plo) <- NULL ## (Don't know why this makes the ribbon-plot work...)
## note the required order below:
de <- data.frame (reshape2::melt(d2, 1, c(2,4,3,5), factorsAsStrings=TRUE), VarGroup, PanelGroup)
g7 <- ggplot(dd, aes(x=Time)) + facet_grid(PanelGroup ~ .)
g7 <- g7 + geom_ribbon(aes(x=Time, ymin=value, ymax=value), data=de, alpha=0.25) ##alpha=.25
g7 <- g7 + geom_path(aes(x=Time, y=value, colour=VarGroup, linetype=VarGroup, size=VarGroup), data=dd)
g7 <- g7 + ylim(-0.05,0.05)
g7 <- g7 + scale_linetype_manual ('', labels=lvl, breaks=lvl, values = c(1,1))
g7 <- g7 + scale_colour_manual('', labels = lvl, breaks=lvl, values = c('blue', 'red'))
g7 <- g7 + scale_size_manual('', labels=lvl, breaks=lvl, values = c(0.5, 1.5))
g7 <- g7 + theme_WAC(1) + theme (legend.position=c(0.5,0.95))
g7 <- g7 + theme(axis.text.x = element_text (size=11.5, margin=margin(15,0,0,0)))
g7 <- g7 + theme(axis.title.x = element_text (size=12))
g7 <- g7 + labs (x='Time [UTC]', y=expression (paste ('l-frame error [',degree,']')))
g7 <- g7 + ggtitle (sprintf ('%s Flight %s', Project, flight))
## I'm not sure why this is necessary; printing g7 in the usual way did not leave
## the ribbon visible, apparently because the resulting pdf file did not include it.
## Now the plot is generated here, but placed in the document in the LaTeX code
## preceding this chunk. This out-of-order sequence requires run
# png (filename='Fig7.png', width=600, height=480, res=150)
png(file='KFplots/AAlframe.png', width=900, height=600, res=150)
print (g7)
invisible(dev.off())
if (ShowPlots) {print (g7)}
# invisible (dev.off())
pct <- as.integer(100*3/8)
print (sprintf ('figures %d%% done', pct))
## ----a-frame-errors
attr(DP$CRAF, 'filtered') <- TRUE
attr(DP$CPAF, 'filtered') <- TRUE
d1 <- with(DP, data.frame(Time, CPAF, PC, CRAF, RC))
lines_per_panel <- 2; panels <- 2
labelL=c('smoothed KF value', 'PC')
labelP=c('pitch', 'roll')
DLP <- nrow(d1)
VarGroup <- rep (gl (lines_per_panel, DLP, labels=labelL), panels)
PanelGroup <- gl (panels, lines_per_panel*DLP, labels=labelP)
dd <- data.frame(reshape2::melt(d1, 1), VarGroup, PanelGroup)
lvl <- levels(dd$VarGroup)
d2 <- with(DP, data.frame(Time, "plo"=CPAF-2*SDCPA, "rlo"=CRAF-2*SDCRA,
"phi"=CPAF+2*SDCPA, "rhi"=CRAF+2*SDCRA))
attributes(d2$plo) <- NULL
## note the required order below:
de <- data.frame (reshape2::melt(d2, 1, c(2,4,3,5)), VarGroup, PanelGroup)
g9 <- ggplot(dd, aes(x=Time)) + facet_grid(PanelGroup ~ .)
g9 <- g9 + geom_ribbon(aes(x=Time, ymin=value, ymax=value), data=de, alpha=0.25)
g9 <- g9 + geom_line(aes(x=Time, y=value, colour=VarGroup, linetype=VarGroup), data=dd)
g9 <- g9 + ylim(-0.05,0.05)
g9 <- g9 + scale_linetype_manual ('', labels=lvl, breaks=lvl, values = c(1,1))
g9 <- g9 + scale_colour_manual('', labels = lvl, breaks=lvl, values = c('blue', 'red'))
g9 <- g9 + theme_WAC(1) + theme (legend.position=c(0.5,0.95))
g9 <- g9 + theme(axis.text.x = element_text (size=11.5, margin=margin(15,0,0,0)))
g9 <- g9 + theme(axis.title.x = element_text (size=12))
g9 <- g9 + labs (x='Time [UTC]', y=expression (paste ('a-frame error [',degree,']')))
g9 <- g9 + ggtitle (sprintf ('%s Flight %s', Project, flight))
## I'm not sure why this is necessary; printing g9 in the usual way did not leave
## the ribbon visible, apparently because the resulting pdf file did not include it.
## Now the plot is generated here, but placed in the document in the LaTeX code
## preceding this chunk. This out-of-order sequence requires run
# png (filename='Fig9.png', width=600, height=480, res=150)
png(file='KFplots/AAaframe.png', width=900, height=600, res=150)
print (g9)
invisible(dev.off())
if (ShowPlots) {print (g9)}
# invisible (dev.off())
pct <- as.integer(100*4/8)
print (sprintf ('figures %d%% done', pct))
pct <- as.integer(100*5/8)
print (sprintf ('figures %d%% done', pct))
}
if (SimpleOnly) { ## Skip now because these were calculated earlier
## these are the errors, negative of the corrections
# PC <- CorrectPitch(DP, .span=901)
# DP$PC <- PC[, 1]
# DP$RC <- PC[, 2]
# DP$HC <- CorrectHeading (DP, .plotfile='KFplots/HCPlot.pdf')
} else {
DP$dP <- DP$deltaPsi/Cradeg
HA <- with(DP, sqrt(LACCX^2+LACCY^2))
DP$dP[HA < 1] <- NA
sddP <- sd(DP$dP, na.rm=TRUE)
}
## ----plot-Kalman-heading
# plotWAC(subset(DP,,c(Time, dP)))
if (GeneratePlots && !SimpleOnly) {
png(file='KFplots/HDG.png', width=900, height=600, res=150)
grid.newpage()
suppressWarnings (
with(DP,
ggplotWAC (data.frame(Time, dP),
ylim=c(-1.0, 1.0),
ylab=expression(paste ('error in heading [',degree,']')),
legend.position=NA, position=c(2,2), theme.version=1,
gtitle=sprintf ('%s Flight %s', Project, flight))
)
)
DP$dP <- DP$deltaPsi/Cradeg
HA <- with(DP, sqrt(LACCX^2+LACCY^2))
DP$dP[HA < 1] <- NA
DP$SDH <- sqrt(VCor[r,9]) / (Cradeg * 30)
## minus sign to change back to error from correction:
DP$CCTHDG <- -SmoothInterp (DP$CTHDG, .Length=181)
DP$CCCTHDG <- DP$CCTHDG
DP$CCCTHDG[DP$SDH > 0.02] <- NA
suppressWarnings (
with (DP,
ggplotWAC(data.frame(Time, 'Kalman'=CCTHDG, 'Ranadu'=HC, 'spline'=-HCS, 'KF'=CCCTHDG),
ylim=c(-1.0, 1.0), lwd=c(0.7,1,1,1.5), lty=c(3,1,1,1),
ylab=expression (paste ('error in heading [',degree,']')),
col=c('blue', 'forestgreen', 'red', 'blue'), position=c(1,2),
legend.position=c(0.5,0.95), theme.version=1
)
)
)
invisible(dev.off())
DP$dP <- DP$deltaPsi/Cradeg
HA <- with(DP, sqrt(LACCX^2+LACCY^2))
DP$dP[HA < 1] <- NA
sddP <- sd(DP$dP, na.rm=TRUE)
if (ShowPlots) {
grid.newpage()
suppressWarnings (
with(DP,
ggplotWAC (data.frame(Time, dP),
ylim=c(-0.4,0.4),
ylab=expression (paste ('error in heading [',degree,']')),
legend.position=NA, position=c(2,2), theme.version=1,
gtitle=sprintf ('%s Flight %s', Project, flight))
)
)
DP$dP <- DP$deltaPsi/Cradeg
HA <- with(DP, sqrt(LACCX^2+LACCY^2))
DP$dP[HA < 1] <- NA
DP$SDH <- sqrt(VCor[r,9]) / (Cradeg * 30)
DP$CCTHDG <- SmoothInterp (DP$CTHDG, .Length=181)
DP$CCCTHDG <- DP$CCTHDG
DP$CCCTHDG[DP$SDH > 0.02] <- NA
suppressWarnings (
with (DP,
ggplotWAC(data.frame(Time, 'Kalman'=CCTHDG, 'Ranadu'=HC, 'spline'=-HCS, 'KF'=CCCTHDG),
ylim=c(-0.4,0.4), lwd=c(0.7,1,1,1.5), lty=c(3,1,1,1),
ylab=expression (paste ('error in heading [',degree,']')),
col=c('blue', 'forestgreen', 'red', 'blue'), position=c(1,2),
legend.position=c(0.5,0.95), theme.version=1
)
)
)
}
pct <- as.integer(100*6/8)
print (sprintf ('figures %d%% done', pct))
}
## ----new-AKRD
# load (file='~/RStudio/Reprocessing/AKRD-fit-coef.Rdata')
cffn <- 19.70547
cff <- 21.481
cfs <- c(4.525341674, 19.933222011, -0.001960992)
if (grepl('130', FI_KF$Platform)) { # C-130 WECAN values
cff <- 10.3123
cfs <- c(5.6885, 14.0452, -0.00461)
}
if (SimpleOnly) { # Skip because these were calculated previously
# PC <- CorrectPitch(D1, .span=901)
# D1$PC <- PC[, 1]
# D1$RC <- PC[, 2]
# D1$HC <- CorrectHeading (D1)
}
D1$QR <- D1$ADIFR / D1$QCF
D1$QR[D1$QCF < 20] <- NA
D1$QR[is.infinite(D1$QR)] <- NA
CutoffFreq <- 600 * Rate