1   /* Copyright 2002-2020 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.time;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.orekit.Utils;
29  import org.orekit.errors.OrekitException;
30  import org.orekit.errors.OrekitMessages;
31  import org.orekit.gnss.SatelliteSystem;
32  
33  public class GNSSDateTest {
34  
35      @Test
36      public void testFromWeekAndMilliGPS() {
37          final DateComponents date = new DateComponents(2006, 8, 9);
38          final TimeComponents time = new TimeComponents(16, 31, 3.0);
39          doTestFromWeekAndMilli(SatelliteSystem.GPS, date, time, 1387, 318677000.0);
40      }
41  
42      @Test
43      public void testFromWeekAndMilliGalileo() {
44          final DateComponents date = new DateComponents(2006, 8, 9);
45          final TimeComponents time = new TimeComponents(16, 31, 3.0);
46          doTestFromWeekAndMilli(SatelliteSystem.GALILEO, date, time, 363, 318677000.0);
47      }
48  
49      @Test
50      public void testFromWeekAndMilliQZSS() {
51          final DateComponents date = new DateComponents(2006, 8, 9);
52          final TimeComponents time = new TimeComponents(16, 31, 3.0);
53          doTestFromWeekAndMilli(SatelliteSystem.QZSS, date, time, 1387, 318677000.0);
54      }
55  
56      @Test
57      public void testFromWeekAndMilliBeidou() {
58          final DateComponents date = new DateComponents(2010, 2, 26);
59          final TimeComponents time = new TimeComponents(23, 15, 12);
60          doTestFromWeekAndMilli(SatelliteSystem.BEIDOU, date, time, 216, 515713000.0);
61      }
62  
63      @Test
64      public void testFromWeekAndMilliIRNSS() {
65          final DateComponents date = new DateComponents(2006, 8, 9);
66          final TimeComponents time = new TimeComponents(16, 31, 3.0);
67          doTestFromWeekAndMilli(SatelliteSystem.IRNSS, date, time, 363, 318677000.0);
68      }
69  
70      @Test
71      public void testFromWeekAndMilliSBAS() {
72          final DateComponents date = new DateComponents(2006, 8, 9);
73          final TimeComponents time = new TimeComponents(16, 31, 3.0);
74          doTestFromWeekAndMilli(SatelliteSystem.SBAS, date, time, 1387, 318677000.0);
75      }
76  
77      private void doTestFromWeekAndMilli(final SatelliteSystem system,
78                                          final DateComponents date, final TimeComponents time,
79                                          final int refWeek, final double refMilliSeconds) {
80          GNSSDate GNSSDate  = new GNSSDate(refWeek, refMilliSeconds, system);
81          AbsoluteDate ref  = new AbsoluteDate(date, time, utc);
82          Assert.assertEquals(refWeek, GNSSDate.getWeekNumber());
83          Assert.assertEquals(refMilliSeconds, GNSSDate.getMilliInWeek(), 1.0e-15);
84          Assert.assertEquals(0, GNSSDate.getDate().durationFrom(ref), 1.0e-15);
85      }
86  
87      @Test
88      public void testFromAbsoluteDateGPS() {
89          final DateComponents date = new DateComponents(2006, 8, 9);
90          final TimeComponents time = new TimeComponents(16, 31, 3.0);
91          doTestFromAbsoluteDate(SatelliteSystem.GPS, date, time, 1387, 318677000.0);
92      }
93  
94      @Test
95      public void testFromAbsoluteDateGalileo() {
96          final DateComponents date = new DateComponents(2006, 8, 9);
97          final TimeComponents time = new TimeComponents(16, 31, 3.0);
98          doTestFromAbsoluteDate(SatelliteSystem.GALILEO, date, time, 363, 318677000.0);
99      }
100 
101     @Test
102     public void testFromAbsoluteDateQZSS() {
103         final DateComponents date = new DateComponents(2006, 8, 9);
104         final TimeComponents time = new TimeComponents(16, 31, 3.0);
105         doTestFromAbsoluteDate(SatelliteSystem.QZSS, date, time, 1387, 318677000.0);
106     }
107 
108     @Test
109     public void testFromAbsoluteDateBeidou() {
110         final DateComponents date = new DateComponents(2010, 2, 26);
111         final TimeComponents time = new TimeComponents(23, 15, 12.0);
112         doTestFromAbsoluteDate(SatelliteSystem.BEIDOU, date, time, 216, 515713000.0);
113     }
114 
115     @Test
116     public void testFromAbsoluteDateIRNSS() {
117         final DateComponents date = new DateComponents(2006, 8, 9);
118         final TimeComponents time = new TimeComponents(16, 31, 3.0);
119         doTestFromAbsoluteDate(SatelliteSystem.IRNSS, date, time, 363, 318677000.0);
120     }
121 
122     @Test
123     public void testFromAbsoluteDateSBAS() {
124         final DateComponents date = new DateComponents(2006, 8, 9);
125         final TimeComponents time = new TimeComponents(16, 31, 3.0);
126         doTestFromAbsoluteDate(SatelliteSystem.SBAS, date, time, 1387, 318677000.0);
127     }
128 
129     private void doTestFromAbsoluteDate(final SatelliteSystem system,
130                                         final DateComponents date, final TimeComponents time,
131                                         final int refWeek, final double refMilliSeconds) {
132         GNSSDate GNSSDate = new GNSSDate(new AbsoluteDate(date, time, utc), system);
133         Assert.assertEquals(refWeek, GNSSDate.getWeekNumber());
134         Assert.assertEquals(refMilliSeconds, GNSSDate.getMilliInWeek(), 1.0e-15);
135     }
136 
137     @Test
138     public void testZeroGPS() {
139         doTestZero(SatelliteSystem.GPS);
140     }
141 
142     @Test
143     public void testZeroGalileo() {
144         doTestZero(SatelliteSystem.GALILEO);
145     }
146 
147     @Test
148     public void testZeroQZSS() {
149         doTestZero(SatelliteSystem.QZSS);
150     }
151 
152     @Test
153     public void testZeroBeidou() {
154         doTestZero(SatelliteSystem.BEIDOU);
155     }
156 
157     @Test
158     public void testZeroIRNSS() {
159         doTestZero(SatelliteSystem.IRNSS);
160     }
161 
162     @Test
163     public void testZeroSBAS() {
164         doTestZero(SatelliteSystem.SBAS);
165     }
166 
167     private void doTestZero(final SatelliteSystem system) {
168         AbsoluteDate epoch = null;
169         switch (system) {
170             case GPS:
171             case SBAS:
172                 epoch = AbsoluteDate.GPS_EPOCH;
173                 break;
174             case GALILEO:
175                 epoch = AbsoluteDate.GALILEO_EPOCH;
176                 break;
177             case QZSS:
178                 epoch = AbsoluteDate.QZSS_EPOCH;
179                 break;
180             case BEIDOU:
181                 epoch = AbsoluteDate.BEIDOU_EPOCH;
182                 break;
183             case IRNSS:
184                 epoch = AbsoluteDate.IRNSS_EPOCH;
185                 break;
186             default:
187                 break;
188         }
189         GNSSDate date = new GNSSDate(epoch, system);
190         Assert.assertEquals(0, date.getWeekNumber());
191         Assert.assertEquals(0.0, date.getMilliInWeek(), 1.0e-15);
192     }
193 
194     @Test
195     public void testZeroZeroGPS() {
196         GNSSDate.setRolloverReference(new DateComponents(DateComponents.GPS_EPOCH, 7 * 512));
197         GNSSDate date1 = new GNSSDate(0, 0.0, SatelliteSystem.GPS);
198         Assert.assertEquals(0.0, date1.getDate().durationFrom(AbsoluteDate.GPS_EPOCH), 1.0e-15);
199         GNSSDate.setRolloverReference(new DateComponents(GNSSDate.getRolloverReference(), 1));
200         GNSSDate date2 = new GNSSDate(0, 0.0, SatelliteSystem.GPS);
201         Assert.assertEquals(1024, date2.getWeekNumber());
202     }
203 
204     @Test
205     public void testZeroZeroGalileo() {
206         GNSSDate.setRolloverReference(new DateComponents(DateComponents.GALILEO_EPOCH, 7 * 2048));
207         GNSSDate date1 = new GNSSDate(0, 0.0, SatelliteSystem.GALILEO);
208         Assert.assertEquals(0.0, date1.getDate().durationFrom(AbsoluteDate.GALILEO_EPOCH), 1.0e-15);
209         GNSSDate.setRolloverReference(new DateComponents(GNSSDate.getRolloverReference(), 1));
210         GNSSDate date2 = new GNSSDate(0, 0.0, SatelliteSystem.GALILEO);
211         Assert.assertEquals(4096, date2.getWeekNumber());
212     }
213 
214     @Test
215     public void testZeroZeroQZSS() {
216         GNSSDate.setRolloverReference(new DateComponents(DateComponents.QZSS_EPOCH, 7 * 512));
217         GNSSDate date1 = new GNSSDate(0, 0.0, SatelliteSystem.QZSS);
218         Assert.assertEquals(0.0, date1.getDate().durationFrom(AbsoluteDate.QZSS_EPOCH), 1.0e-15);
219         GNSSDate.setRolloverReference(new DateComponents(GNSSDate.getRolloverReference(), 1));
220         GNSSDate date2 = new GNSSDate(0, 0.0, SatelliteSystem.QZSS);
221         Assert.assertEquals(1024, date2.getWeekNumber());
222     }
223 
224     @Test
225     public void testZeroZeroBeidou() {
226         GNSSDate.setRolloverReference(new DateComponents(DateComponents.BEIDOU_EPOCH, 7 * 4096));
227         GNSSDate date1 = new GNSSDate(0, 0.0, SatelliteSystem.BEIDOU);
228         Assert.assertEquals(0.0, date1.getDate().durationFrom(AbsoluteDate.BEIDOU_EPOCH), 1.0e-15);
229         GNSSDate.setRolloverReference(new DateComponents(GNSSDate.getRolloverReference(), 1));
230         GNSSDate date2 = new GNSSDate(0, 0.0, SatelliteSystem.BEIDOU);
231         Assert.assertEquals(8192, date2.getWeekNumber());
232     }
233 
234     @Test
235     public void testZeroZeroIRNSS() {
236         GNSSDate.setRolloverReference(new DateComponents(DateComponents.IRNSS_EPOCH, 7 * 512));
237         GNSSDate date1 = new GNSSDate(0, 0.0, SatelliteSystem.IRNSS);
238         Assert.assertEquals(0.0, date1.getDate().durationFrom(AbsoluteDate.IRNSS_EPOCH), 1.0e-15);
239         GNSSDate.setRolloverReference(new DateComponents(GNSSDate.getRolloverReference(), 1));
240         GNSSDate date2 = new GNSSDate(0, 0.0, SatelliteSystem.IRNSS);
241         Assert.assertEquals(1024, date2.getWeekNumber());
242     }
243 
244     @Test
245     public void testZeroZeroSBAS() {
246         GNSSDate.setRolloverReference(new DateComponents(DateComponents.GPS_EPOCH, 7 * 512));
247         GNSSDate date1 = new GNSSDate(0, 0.0, SatelliteSystem.SBAS);
248         Assert.assertEquals(0.0, date1.getDate().durationFrom(AbsoluteDate.GPS_EPOCH), 1.0e-15);
249         GNSSDate.setRolloverReference(new DateComponents(GNSSDate.getRolloverReference(), 1));
250         GNSSDate date2 = new GNSSDate(0, 0.0, SatelliteSystem.SBAS);
251         Assert.assertEquals(1024, date2.getWeekNumber());
252     }
253 
254     @Test
255     public void testSerializationGPS() throws ClassNotFoundException, IOException {
256         final DateComponents date = new DateComponents(2006, 8, 9);
257         final TimeComponents time = new TimeComponents(16, 31, 3.0);
258         doTestSerialization(SatelliteSystem.GPS, date, time, 1387, 318677000.0);
259     }
260 
261     @Test
262     public void testSerializationGalileo() throws ClassNotFoundException, IOException {
263         final DateComponents date = new DateComponents(2006, 8, 9);
264         final TimeComponents time = new TimeComponents(16, 31, 3.0);
265         doTestSerialization(SatelliteSystem.GALILEO, date, time, 363, 318677000.0);
266     }
267 
268     @Test
269     public void testSerializationQZSS() throws ClassNotFoundException, IOException {
270         final DateComponents date = new DateComponents(2006, 8, 9);
271         final TimeComponents time = new TimeComponents(16, 31, 3.0);
272         doTestSerialization(SatelliteSystem.QZSS, date, time, 1387, 318677000.0);
273     }
274 
275     @Test
276     public void testSerializationBeidou() throws ClassNotFoundException, IOException {
277         final DateComponents date = new DateComponents(2010, 2, 26);
278         final TimeComponents time = new TimeComponents(23, 15, 12.0);
279         doTestSerialization(SatelliteSystem.BEIDOU, date, time, 216, 515713000.0);
280     }
281 
282     @Test
283     public void testSerializationIRNSS() throws ClassNotFoundException, IOException {
284         final DateComponents date = new DateComponents(2006, 8, 9);
285         final TimeComponents time = new TimeComponents(16, 31, 3.0);
286         doTestSerialization(SatelliteSystem.IRNSS, date, time, 363, 318677000.0);
287     }
288 
289     @Test
290     public void testSerializationSBAS() throws ClassNotFoundException, IOException {
291         final DateComponents date = new DateComponents(2006, 8, 9);
292         final TimeComponents time = new TimeComponents(16, 31, 3.0);
293         doTestSerialization(SatelliteSystem.SBAS, date, time, 1387, 318677000.0);
294     }
295 
296     @Test
297     public void testDefaultRolloverReference() {
298         Assert.assertNull(GNSSDate.getRolloverReference());
299         GNSSDate date = new GNSSDate(305, 1.5, SatelliteSystem.GPS);
300         // the default reference is extracted from last EOP entry
301         // which in this test comes from bulletin B 218, in the final values section
302         Assert.assertEquals("2006-03-05", GNSSDate.getRolloverReference().toString());
303         Assert.assertEquals(305 + 1024, date.getWeekNumber());
304     }
305 
306     @Test
307     public void testUserRolloverReference() {
308         GNSSDate.setRolloverReference(new DateComponents(DateComponents.GPS_EPOCH, 7 * (3 * 1024 + 512)));
309         GNSSDate date = new GNSSDate(305, 1.5, SatelliteSystem.GPS);
310         Assert.assertEquals("2048-09-13", GNSSDate.getRolloverReference().toString());
311         Assert.assertEquals(305 + 3 * 1024, date.getWeekNumber());
312     }
313 
314     private void doTestSerialization(final SatelliteSystem system,
315                                      final DateComponents date, final TimeComponents time,
316                                      final int refWeek, final double refMilliSeconds)
317         throws IOException, ClassNotFoundException {
318         GNSSDate GNSSDate = new GNSSDate(refWeek, refMilliSeconds, system);
319 
320         ByteArrayOutputStream bos = new ByteArrayOutputStream();
321         ObjectOutputStream    oos = new ObjectOutputStream(bos);
322         oos.writeObject(GNSSDate);
323 
324         Assert.assertTrue(bos.size() > 95);
325         Assert.assertTrue(bos.size() < 236);
326 
327         ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
328         ObjectInputStream     ois = new ObjectInputStream(bis);
329         GNSSDate deserialized  = (GNSSDate) ois.readObject();
330         AbsoluteDate ref  = new AbsoluteDate(date, time, utc);
331         Assert.assertEquals(refWeek, deserialized.getWeekNumber());
332         Assert.assertEquals(refMilliSeconds, deserialized.getMilliInWeek(), 1.0e-15);
333         Assert.assertEquals(0, deserialized.getDate().durationFrom(ref), 1.0e-15);
334 
335     }
336 
337     @Test
338     public void testBadSatelliteSystem() {
339         try {
340             @SuppressWarnings("unused")
341             GNSSDate date = new GNSSDate(new AbsoluteDate(), SatelliteSystem.GLONASS);
342             Assert.fail("an exception should have been thrown");
343         } catch (OrekitException oe) {
344             Assert.assertEquals(OrekitMessages.INVALID_SATELLITE_SYSTEM, oe.getSpecifier());
345         }
346     }
347 
348     @Before
349     public void setUp() {
350         Utils.setDataRoot("regular-data");
351         utc = TimeScalesFactory.getUTC();
352     }
353 
354     private TimeScale utc;
355 
356 }