Code Examples
A repository of 155 code examples for BeepBeep
RadialAcceleration.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package voyager;
19 
20 import static ca.uqac.lif.cep.Connector.INPUT;
21 import static ca.uqac.lif.cep.Connector.OUTPUT;
22 
23 import ca.uqac.lif.cep.Connector;
24 import ca.uqac.lif.cep.functions.ApplyFunction;
25 import ca.uqac.lif.cep.functions.FunctionTree;
26 import ca.uqac.lif.cep.functions.StreamVariable;
27 import ca.uqac.lif.cep.io.ReadLines;
28 import ca.uqac.lif.cep.mtnp.DrawPlot;
29 import ca.uqac.lif.cep.mtnp.UpdateTableStream;
30 import ca.uqac.lif.cep.signal.Smooth;
31 import ca.uqac.lif.cep.tmf.CountDecimate;
32 import ca.uqac.lif.cep.tmf.Fork;
33 import ca.uqac.lif.cep.tmf.Pump;
34 import ca.uqac.lif.cep.tmf.Splice;
35 import ca.uqac.lif.cep.tmf.Trim;
36 import ca.uqac.lif.cep.util.NthElement;
37 import ca.uqac.lif.cep.util.Numbers;
38 import ca.uqac.lif.cep.util.Strings;
39 import ca.uqac.lif.mtnp.plot.TwoDimensionalPlot.Axis;
40 import ca.uqac.lif.mtnp.plot.gral.Scatterplot;
41 import plots.BitmapJFrame;
42 
43 /**
44  * Detect planetary encounters of Voyager 2 by analyzing its distance from
45  * the Earth.
46  * <h4>About the data files</h4>
47  * <p>This example requires you to first download some data files from the
48  * Voyager 2 space probe; these files are publicly available on a NASA FTP
49  * site:
50  * <p>
51  * <a href="ftp://spdf.gsfc.nasa.gov/pub/data/voyager/voyager2/merged/">ftp://spdf.gsfc.nasa.gov/pub/data/voyager/voyager2/merged/</a>
52  * <p>
53  * @author Sylvain HallĂ©
54  */
55 public class RadialAcceleration
56 {
57  public static void main(String[] args)
58  {
59  int start_year = 1985, end_year = 1992;
60 
61  /* The Voyager data is split into multiple CSV files, one per civil
62  * year. Let us create one ReadLines processor for each file. */
63  ReadLines[] readers = new ReadLines[end_year - start_year + 1];
64  for (int y = start_year; y <= end_year; y++)
65  {
66  readers[y - start_year] = new ReadLines(RadialAcceleration.class.getResourceAsStream("data/vy2_" + y + ".asc"));
67  }
68 
69  /* Pass this array of readers to the Splice processors, so that their
70  * contents be used as an uninterrupted stream of events. */
71  Splice spl = new Splice(readers);
72 
73  /* The input files have hourly readings; keep only one reading per week. */
74  CountDecimate cd = new CountDecimate(24 * 1);
75  Connector.connect(spl, cd);
76 
77  /* The 1977 file has no data before week 31 or so (the launch date);
78  * let's ignore these first lines. */
79  //Trim ignore_beginning = new Trim(39*7);
80  Trim ignore_beginning = new Trim(39*7);
81  Connector.connect(cd, ignore_beginning);
82 
83  /* Split the surviving lines into arrays with the TAB character. */
84  ApplyFunction to_array = new ApplyFunction(new Strings.SplitString("\\s+"));
85  Connector.connect(ignore_beginning, to_array);
86 
87  /* Fork this stream of arrays in three. */
88  Fork fork = new Fork(2);
89  Connector.connect(to_array, fork);
90 
91  /* From first stream, format date */
92  ApplyFunction format_date = new ApplyFunction(new FunctionTree(FormatDate.instance, new FunctionTree(new NthElement(0), StreamVariable.X), new FunctionTree(new NthElement(1), StreamVariable.X)));
93  Connector.connect(fork, 0, format_date, INPUT);
94 
95  /* From second stream, extract distance */
96  ApplyFunction get_au1 = new ApplyFunction(new FunctionTree(Numbers.numberCast, new FunctionTree(new NthElement(3), StreamVariable.X)));
97  Connector.connect(fork, 1, get_au1, INPUT);
98 
99  /* Compute rate of change of distance */
100  Slope speed = new Slope();
101  Connector.connect(get_au1, speed);
102 
103  /* Compute rate of change of speed (2nd derivative) */
104  Slope acceleration = new Slope();
105  Connector.connect(speed, acceleration);
106  Smooth smooth = new Smooth(2);
107  Connector.connect(acceleration, smooth);
108 
109  UpdateTableStream table = new UpdateTableStream("Date", "Acceleration");
110 
111  Connector.connect(format_date, OUTPUT, table, 0);
112  Connector.connect(smooth, OUTPUT, table, 1);
113 
114  CountDecimate reduce_resolution = new CountDecimate(15);
115  Connector.connect(table, reduce_resolution);
116  Scatterplot plot = new Scatterplot();
117  plot.withPoints(false);
118  plot.setTitle("Planetary encounters of Voyager 2");
119  plot.setCaption(Axis.X, "Days after launch");
120  plot.setCaption(Axis.Y, "AU");
121  DrawPlot draw = new DrawPlot(plot);
122  Connector.connect(reduce_resolution, draw);
123  Pump pump = new Pump();
124  Connector.connect(draw, pump);
125  BitmapJFrame window = new BitmapJFrame();
126  Connector.connect(pump, window);
127  window.start();
128  pump.start();
129  }
130 
131 
132 }
Detect planetary encounters of Voyager 2 by analyzing its distance from the Earth.
static final FormatDate instance
The single publicly accessible instance of this function.
Definition: FormatDate.java:32
Computes the slope between two successive points in a data stream (which is simply the difference bet...
Definition: Slope.java:32
Receives two arguments: one of a year number, and another with a number of days in the year...
Definition: FormatDate.java:27
Generate tables, charts and plots from event streams.
Receives a byte array as an input, and shows it in a Swing window as a picture.