This repository has been archived by the owner on Sep 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSW_observations.Rmd
291 lines (233 loc) · 10.4 KB
/
SW_observations.Rmd
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
---
title: "Solar Wind observation data clean up"
author: "Dmitry A. Grechka"
date: "June 8, 2016"
output:
html_document:
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#Intro
We have per minute sampling of solar wind density, temperature and velocity recorded by EPAM <http://sd-www.jhuapl.edu/ACE/EPAM/> instrument of Advanced Composition Explorer (ACE) <http://sd-www.jhuapl.edu/ACE/> spacecraft.
(see <https://github.com/dgrechka/SolarWindVelocity/tree/master/SampleData> for data files)
I want to aggregate the data hourly. For each hour I want to get separate "trend"" value and noise level for each of three data variables (density, temperature, velocity). Thus I will get
* **density_mean**
* **density_noise_sd**
* **temperature_mean**
* **temperature_noise_sd**
* **velocity_mean**
* **velocity_noise_sd**
for every hour.
# Data processing
```{r data load}
sw_2015 <- read.csv('SampleData/ace_swepam_2015.dsv',
sep = '\t',
dec = '.',
colClasses=c("dt_record"="character")
)
```
As a time axis we will use hours since 2015-01-01T00:00:00
```{r time_parsing}
base_ts_sec <- as.numeric(strptime("2015-01-01T00:00:00","%Y-%m-%dT%H:%M:%S",tz='UTC'))
dt_record <- strptime(sw_2015$dt_record,"%Y-%m-%dT%H:%M:%S",tz='UTC')
ts_sec <- as.numeric(dt_record)
sw_2015$ts <- (ts_sec - base_ts_sec)/3600;#hours
sw_2015 <- sw_2015[,names(sw_2015) != 'dt_record']
```
```{r summary}
head(sw_2015)
summary(sw_2015)
```
We will form trend by running median over the data.
```{r trend formation}
k <- 21
sw_2015$temperature.trend <- runmed(sw_2015$temperature,k)
sw_2015$velocity.trend <- runmed(sw_2015$velocity,k)
sw_2015$density.trend <- runmed(sw_2015$density,k)
```
Forming noise variables by subtracting trend from original signal.
```{r noise formation}
sw_2015$temperature.noise <- sw_2015$temperature-sw_2015$temperature.trend
sw_2015$velocity.noise <- sw_2015$velocity-sw_2015$velocity.trend
sw_2015$density.noise <- sw_2015$density-sw_2015$density.trend
```
To perform per hour aggregation, I will add synthetic column containing the beginning of the hour period.
```{r group column}
sw_2015$ts_h <- floor(sw_2015$ts)
```
Now we can aggregate values within each group.
```{r aggregation}
#trend is aggregated with mean
temperature.trend.1h <- aggregate(temperature.trend ~ ts_h,data = sw_2015,mean)
velocity.trend.1h <- aggregate(velocity.trend ~ ts_h,data = sw_2015,mean)
density.trend.1h <- aggregate(density.trend ~ ts_h,data = sw_2015,mean)
#noise is aggregated with standard deviation (sd)
sd.safe <- function(x) { return(sd(x,na.rm = T))}
temperature.noise.1h <- aggregate(temperature.noise ~ ts_h,data = sw_2015,sd.safe)
velocity.noise.1h <- aggregate(velocity.noise ~ ts_h,data = sw_2015,sd.safe)
density.noise.1h <- aggregate(density.noise ~ ts_h,data = sw_2015,sd.safe)
#time is aggregared with mean (to get "centre" of aggregation time interval)
ts.1h <- aggregate(ts ~ ts_h,data = sw_2015,mean)
```
Merging (outer joining) aggregated values into single new data frame
```{r merging}
result <- merge(temperature.trend.1h,velocity.trend.1h, by = "ts_h", all = TRUE)
result <- merge(result,density.trend.1h, by = "ts_h", all = TRUE)
result <- merge(result,temperature.noise.1h, by = "ts_h", all = TRUE)
result <- merge(result,velocity.noise.1h, by = "ts_h", all = TRUE)
result <- merge(result,density.noise.1h, by = "ts_h", all = TRUE)
result <- merge(result,ts.1h, by = "ts_h", all = TRUE)
#renaming columns
result <- data.frame(
ts=result$ts,
density_mean=result$density.trend,
density_noise_sd=result$density.noise,
velocity_mean=result$velocity.trend,
velocity_noise_sd=result$velocity.noise,
temperature_mean=result$temperature.trend,
temperature_noise_sd=result$temperature.noise
)
head(result)
summary(result)
```
#Visualizing the results
Visualizing first week of data
```{r visualize week}
library(ggplot2)
result_w <- result[result$ts<=168,]
raw_w <- sw_2015[sw_2015$ts<=168,]
result_m <- result[result$ts<=720,]
raw_m <- sw_2015[sw_2015$ts<=720,]
shape_scale <- c(shape1=1)
shape_scale_labels <- c(shape1='Raw observations')
fill_scale <- c(fill1='pink',fill2='darkturquoise',fill3='darkseagreen1')
fill_scale_labels <- c(fill1='Mean +/- 1 sigma noise',fill2='Mean +/- 1 sigma noise',fill3='Mean +/- 1 sigma noise')
colour_scale <- c(col1='red',col2='blue',col3='green')
colour_scale_labels <- c(col1='Mean',col2='Mean',col3='Mean')
#Velocity
p <- ggplot(aes(x=ts),data=result_w) +
geom_point(aes(y=velocity, shape="shape1"),data=raw_w,col='darkgrey') +
geom_ribbon(aes(ymin=velocity_mean-velocity_noise_sd,ymax=velocity_mean+velocity_noise_sd,fill ='fill1')) +
geom_line(aes(y=velocity_mean,col='col1'),lwd=1) +
theme_bw() +
xlab("Time (hours since 2015-01-01T00:00:00)") +
ylab("Solar Wind Velocity (km s-1)") +
ggtitle("First Week of 2015 year") +
scale_shape_manual(name="",values=shape_scale,labels=shape_scale_labels) +
scale_fill_manual(name="",values=fill_scale,labels=fill_scale_labels) +
scale_colour_manual(name="",values=colour_scale,labels=colour_scale_labels)
print(p)
#Density
p <- ggplot(aes(x=ts),data=result_w) +
geom_point(aes(y=density, shape="shape1"),data=raw_w,col='darkgrey') +
geom_ribbon(aes(ymin=density_mean-density_noise_sd,ymax=density_mean+density_noise_sd,fill ='fill2')) +
geom_line(aes(y=density_mean,col='col2'),lwd=1) +
theme_bw() +
xlab("Time (hours since 2015-01-01T00:00:00)") +
ylab("Solar Wind Density (particles cm-3)") +
ggtitle("First Week of 2015 year") +
scale_shape_manual(name="",values=shape_scale,labels=shape_scale_labels) +
scale_fill_manual(name="",values=fill_scale,labels=fill_scale_labels) +
scale_colour_manual(name="",values=colour_scale,labels=colour_scale_labels)
print(p)
#Temprature
p <- ggplot(aes(x=ts),data=result_w) +
geom_point(aes(y=temperature, shape="shape1"),data=raw_w,col='darkgrey') +
geom_ribbon(aes(ymin=temperature_mean-temperature_noise_sd,ymax=temperature_mean+temperature_noise_sd,fill ='fill3')) +
geom_line(aes(y=temperature_mean,col='col3'),lwd=1) +
theme_bw() +
xlab("Time (hours since 2015-01-01T00:00:00)") +
ylab("Solar Wind Temperature (?)") +
ggtitle("First Week of 2015 year") +
scale_shape_manual(name="",values=shape_scale,labels=shape_scale_labels) +
scale_fill_manual(name="",values=fill_scale,labels=fill_scale_labels) +
scale_colour_manual(name="",values=colour_scale,labels=colour_scale_labels)
print(p)
```
```{r visualize month}
result_m <- result[result$ts<=720,]
#Velocity
p <- ggplot(aes(x=ts),data=result_m) +
geom_ribbon(aes(ymin=velocity_mean-velocity_noise_sd,ymax=velocity_mean+velocity_noise_sd,fill ='fill1')) +
geom_line(aes(y=velocity_mean,col='col1'),lwd=0.5) +
theme_bw() +
xlab("Time (hours since 2015-01-01T00:00:00)") +
ylab("Solar Wind Velocity (km s-1)") +
ggtitle("First 30 days of 2015 year") +
scale_fill_manual(name="",values=fill_scale,labels=fill_scale_labels) +
scale_colour_manual(name="",values=colour_scale,labels=colour_scale_labels)
print(p)
#Density
p <- ggplot(aes(x=ts),data=result_m) +
geom_ribbon(aes(ymin=density_mean-density_noise_sd,ymax=density_mean+density_noise_sd,fill ='fill2')) +
geom_line(aes(y=density_mean,col='col2'),lwd=0.5) +
theme_bw() +
xlab("Time (hours since 2015-01-01T00:00:00)") +
ylab("Solar Wind Density (particles cm-3)") +
ggtitle("First 30 days of 2015 year") +
scale_fill_manual(name="",values=fill_scale,labels=fill_scale_labels) +
scale_colour_manual(name="",values=colour_scale,labels=colour_scale_labels)
print(p)
#Temprature
p <- ggplot(aes(x=ts),data=result_m) +
geom_ribbon(aes(ymin=temperature_mean-temperature_noise_sd,ymax=temperature_mean+temperature_noise_sd,fill ='fill3')) +
geom_line(aes(y=temperature_mean,col='col3'),lwd=0.5) +
theme_bw() +
xlab("Time (hours since 2015-01-01T00:00:00)") +
ylab("Solar Wind Temperature (?)") +
ggtitle("First 30 days of 2015 year") +
scale_fill_manual(name="",values=fill_scale,labels=fill_scale_labels) +
scale_colour_manual(name="",values=colour_scale,labels=colour_scale_labels)
print(p)
```
# Saving results
```{r outputting}
result <- na.omit(result)
write.csv(file='ResultData/ACE_EPAM_SW_2015.csv',row.names = F,result)
```
Processing 2014 year
```{r year 2014}
sw_2014 <- read.csv('SampleData/ace_swepam_2014.dsv',
sep = '\t',
dec = '.',
colClasses=c("dt_record"="character")
)
dt_record <- strptime(sw_2014$dt_record,"%Y-%m-%dT%H:%M:%S",tz='UTC')
ts_sec <- as.numeric(dt_record)
sw_2014$ts <- (ts_sec - base_ts_sec)/3600;#hours
sw_2014 <- sw_2014[,names(sw_2014) != 'dt_record']
sw_2014$temperature.trend <- runmed(sw_2014$temperature,k)
sw_2014$velocity.trend <- runmed(sw_2014$velocity,k)
sw_2014$density.trend <- runmed(sw_2014$density,k)
sw_2014$temperature.noise <- sw_2014$temperature-sw_2014$temperature.trend
sw_2014$velocity.noise <- sw_2014$velocity-sw_2014$velocity.trend
sw_2014$density.noise <- sw_2014$density-sw_2014$density.trend
sw_2014$ts_h <- floor(sw_2014$ts)
temperature.trend.1h <- aggregate(temperature.trend ~ ts_h,data = sw_2014,mean)
velocity.trend.1h <- aggregate(velocity.trend ~ ts_h,data = sw_2014,mean)
density.trend.1h <- aggregate(density.trend ~ ts_h,data = sw_2014,mean)
temperature.noise.1h <- aggregate(temperature.noise ~ ts_h,data = sw_2014,sd.safe)
velocity.noise.1h <- aggregate(velocity.noise ~ ts_h,data = sw_2014,sd.safe)
density.noise.1h <- aggregate(density.noise ~ ts_h,data = sw_2014,sd.safe)
ts.1h <- aggregate(ts ~ ts_h,data = sw_2014,mean)
result <- merge(temperature.trend.1h,velocity.trend.1h, by = "ts_h", all = TRUE)
result <- merge(result,density.trend.1h, by = "ts_h", all = TRUE)
result <- merge(result,temperature.noise.1h, by = "ts_h", all = TRUE)
result <- merge(result,velocity.noise.1h, by = "ts_h", all = TRUE)
result <- merge(result,density.noise.1h, by = "ts_h", all = TRUE)
result <- merge(result,ts.1h, by = "ts_h", all = TRUE)
result <- data.frame(
ts=result$ts,
density_mean=result$density.trend,
density_noise_sd=result$density.noise,
velocity_mean=result$velocity.trend,
velocity_noise_sd=result$velocity.noise,
temperature_mean=result$temperature.trend,
temperature_noise_sd=result$temperature.noise
)
result <- na.omit(result)
write.csv(file='ResultData/ACE_EPAM_SW_2014.csv',row.names = F,result)
```
This is a part of study described at <http://grechka.family/dmitry/blog/2016/06/solar-wind-prediction/>