1 /* Copyright 2002-2022 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.gnss;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.nio.charset.StandardCharsets;
24 import java.text.ParseException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.Pair;
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.orekit.Utils;
33 import org.orekit.data.DataLoader;
34 import org.orekit.data.DataProvidersManager;
35 import org.orekit.errors.OrekitException;
36 import org.orekit.errors.OrekitMessages;
37 import org.orekit.propagation.analytical.gnss.data.QZSSAlmanac;
38 import org.orekit.time.GNSSDate;
39
40 public class QZSSAlmanacTest {
41
42 @Test
43 public void testLoadData() throws IOException, ParseException, OrekitException {
44 Utils.setDataRoot("regular-data");
45 // the parser for reading Yuma files with a pattern
46 QZSSYUMAParser reader = new QZSSYUMAParser(".*\\.yum$");
47 // the YUMA file to read
48 final String fileName = "/gnss/q2019034.alm";
49 final InputStream in = getClass().getResourceAsStream(fileName);
50 reader.loadData(in, fileName);
51
52 Assert.assertEquals(".*\\.yum$", reader.getSupportedNames());
53
54 // Checks the whole file read
55 Assert.assertEquals(4, reader.getAlmanacs().size());
56 Assert.assertEquals(4, reader.getPRNNumbers().size());
57
58 // Checks the last almanac read
59 final QZSSAlmanac alm = reader.getAlmanacs().get(reader.getAlmanacs().size() - 1);
60 Assert.assertEquals(199, alm.getPRN());
61 Assert.assertEquals(1015, alm.getWeek());
62 Assert.assertEquals(262144.0, alm.getTime(), 0.);
63 Assert.assertEquals(6493.484863, FastMath.sqrt(alm.getSma()), FastMath.ulp(5.E+03));
64 Assert.assertEquals(1.387596130E-04, alm.getE(), FastMath.ulp(8E-05));
65 Assert.assertEquals(0.0007490141, alm.getI0(), 0.);
66 Assert.assertEquals(0., alm.getIDot(), 0.);
67 Assert.assertEquals(9.194173760E-01, alm.getOmega0(), 0.);
68 Assert.assertEquals(9.714690370E-10, alm.getOmegaDot(), FastMath.ulp(-8E-09));
69 Assert.assertEquals(2.722442515, alm.getPa(), 0.);
70 Assert.assertEquals(-1.158294811, alm.getM0(), 0.);
71 Assert.assertEquals(6.351470947E-04, alm.getAf0(), 0.);
72 Assert.assertEquals(0.0, alm.getAf1(), 0.);
73 Assert.assertEquals(0, alm.getHealth());
74 Assert.assertEquals("YUMA", alm.getSource());
75 Assert.assertTrue(alm.getDate().durationFrom(new GNSSDate(1015, 262144 * 1000., SatelliteSystem.QZSS).getDate()) == 0);
76 Assert.assertEquals(0., alm.getCic(), 0.);
77 Assert.assertEquals(0., alm.getCis(), 0.);
78 Assert.assertEquals(0., alm.getCrc(), 0.);
79 Assert.assertEquals(0., alm.getCrs(), 0.);
80 Assert.assertEquals(0., alm.getCuc(), 0.);
81 Assert.assertEquals(0., alm.getCus(), 0.);
82 }
83
84 /**
85 * This class reads Yuma almanac files and provides {@link QZSSAlmanac QZSS almanacs}.
86 *
87 * <p>This class is a rewrite of {@link YUMAParser} adapted to QZSS yuma files</p>
88 *
89 * @author Pascal Parraud
90 *
91 */
92 private class QZSSYUMAParser implements DataLoader {
93
94 // Constants
95 /** The source of the almanacs. */
96 private static final String SOURCE = "YUMA";
97
98 /** the useful keys in the YUMA file. */
99 private final String[] KEY = {
100 "id", // ID
101 "health", // Health
102 "eccentricity", // Eccentricity
103 "time", // Time of Applicability(s)
104 "orbital", // Orbital Inclination(rad)
105 "rate", // Rate of Right Ascen(r/s)
106 "sqrt", // SQRT(A) (m 1/2)
107 "right", // Right Ascen at Week(rad)
108 "argument", // Argument of Perigee(rad)
109 "mean", // Mean Anom(rad)
110 "af0", // Af0(s)
111 "af1", // Af1(s/s)
112 "week" // week
113 };
114
115 /** Default supported files name pattern. */
116 private static final String DEFAULT_SUPPORTED_NAMES = ".*\\.alm$";
117
118 // Fields
119 /** Regular expression for supported files names. */
120 private final String supportedNames;
121
122 /** the list of all the almanacs read from the file. */
123 private final List<QZSSAlmanac> almanacs;
124
125 /** the list of all the PRN numbers of all the almanacs read from the file. */
126 private final List<Integer> prnList;
127
128 /** Simple constructor.
129 *
130 * <p>This constructor does not load any data by itself. Data must be loaded
131 * later on by calling one of the {@link #loadData() loadData()} method or
132 * the {@link #loadData(InputStream, String) loadData(inputStream, fileName)}
133 * method.</p>
134 *
135 * <p>The supported files names are used when getting data from the
136 * {@link #loadData() loadData()} method that relies on the
137 * {@link DataProvidersManager data providers manager}. They are useless when
138 * getting data from the {@link #loadData(InputStream, String) loadData(input, name)}
139 * method.</p>
140 *
141 * @param supportedNames regular expression for supported files names
142 * (if null, a default pattern matching files with a ".alm" extension will be used)
143 * @see #loadData()
144 */
145 public QZSSYUMAParser(final String supportedNames) {
146 this.supportedNames = (supportedNames == null) ? DEFAULT_SUPPORTED_NAMES : supportedNames;
147 this.almanacs = new ArrayList<QZSSAlmanac>();
148 this.prnList = new ArrayList<Integer>();
149 }
150
151 @Override
152 public void loadData(final InputStream input, final String name)
153 throws IOException, ParseException, OrekitException {
154
155 // Clears the lists
156 almanacs.clear();
157 prnList.clear();
158
159 // Creates the reader
160 final BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
161
162 try {
163 // Gathers data to create one QZSSAlmanac from 13 consecutive lines
164 final List<Pair<String, String>> entries =
165 new ArrayList<Pair<String, String>>(KEY.length);
166
167 // Reads the data one line at a time
168 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
169 // Try to split the line into 2 tokens as key:value
170 final String[] token = line.trim().split(":");
171 // If the line is made of 2 tokens
172 if (token.length == 2) {
173 // Adds these tokens as an entry to the entries
174 entries.add(new Pair<String, String>(token[0].trim(), token[1].trim()));
175 }
176 // If the number of entries equals the expected number
177 if (entries.size() == KEY.length) {
178 // Gets a QZSSAlmanac from the entries
179 final QZSSAlmanac almanac = getAlmanac(entries, name);
180 // Adds the QZSSAlmanac to the list
181 almanacs.add(almanac);
182 // Adds the PRN number of the QZSSAlmanac to the list
183 prnList.add(almanac.getPRN());
184 // Clears the entries
185 entries.clear();
186 }
187 }
188 } catch (IOException ioe) {
189 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_YUMA_ALMANAC_FILE,
190 name);
191 }
192 }
193
194 @Override
195 public boolean stillAcceptsData() {
196 return almanacs.isEmpty();
197 }
198
199 /** Get the supported names for data files.
200 * @return regular expression for the supported names for data files
201 */
202 public String getSupportedNames() {
203 return supportedNames;
204 }
205
206 /**
207 * Gets all the {@link QZSSAlmanac QZSS Almanacs} read from the file.
208 *
209 * @return the list of {@link QZSSAlmanac} from the file
210 */
211 public List<QZSSAlmanac> getAlmanacs() {
212 return almanacs;
213 }
214
215 /**
216 * Gets the PRN numbers of all the {@link QZSSAlmanac QZSS Almanacs} read from the file.
217 *
218 * @return the PRN numbers of all the {@link QZSSAlmanac QZSS Almanacs} read from the file
219 */
220 public List<Integer> getPRNNumbers() {
221 return prnList;
222 }
223
224 /**
225 * Builds a {@link QZSSAlmanac QZSS Almanac} from data read in the file.
226 *
227 * @param entries the data read from the file
228 * @param name name of the file
229 * @return a {@link QZSSAlmanac QZSS Almanac}
230 */
231 private QZSSAlmanac getAlmanac(final List<Pair<String, String>> entries, final String name) {
232 try {
233 // Initializes almanac
234 final QZSSAlmanac almanac = new QZSSAlmanac();
235 almanac.setSource(SOURCE);
236
237 // Initializes checks
238 final boolean[] checks = new boolean[KEY.length];
239 // Loop over entries
240 for (Pair<String, String> entry: entries) {
241 if (entry.getKey().toLowerCase().startsWith(KEY[0])) {
242 // Gets the PRN of the SVN
243 almanac.setPRN(Integer.parseInt(entry.getValue()));
244 checks[0] = true;
245 } else if (entry.getKey().toLowerCase().startsWith(KEY[1])) {
246 // Gets the Health status
247 almanac.setHealth(Integer.parseInt(entry.getValue()));
248 checks[1] = true;
249 } else if (entry.getKey().toLowerCase().startsWith(KEY[2])) {
250 // Gets the eccentricity
251 almanac.setE(Double.parseDouble(entry.getValue()));
252 checks[2] = true;
253 } else if (entry.getKey().toLowerCase().startsWith(KEY[3])) {
254 // Gets the Time of Applicability
255 almanac.setTime(Double.parseDouble(entry.getValue()));
256 checks[3] = true;
257 } else if (entry.getKey().toLowerCase().startsWith(KEY[4])) {
258 // Gets the Inclination
259 almanac.setI0(Double.parseDouble(entry.getValue()));
260 checks[4] = true;
261 } else if (entry.getKey().toLowerCase().startsWith(KEY[5])) {
262 // Gets the Rate of Right Ascension
263 almanac.setOmegaDot(Double.parseDouble(entry.getValue()));
264 checks[5] = true;
265 } else if (entry.getKey().toLowerCase().startsWith(KEY[6])) {
266 // Gets the square root of the semi-major axis
267 almanac.setSqrtA(Double.parseDouble(entry.getValue()));
268 checks[6] = true;
269 } else if (entry.getKey().toLowerCase().startsWith(KEY[7])) {
270 // Gets the Right Ascension of Ascending Node
271 almanac.setOmega0(Double.parseDouble(entry.getValue()));
272 checks[7] = true;
273 } else if (entry.getKey().toLowerCase().startsWith(KEY[8])) {
274 // Gets the Argument of Perigee
275 almanac.setPa(Double.parseDouble(entry.getValue()));
276 checks[8] = true;
277 } else if (entry.getKey().toLowerCase().startsWith(KEY[9])) {
278 // Gets the Mean Anomalie
279 almanac.setM0(Double.parseDouble(entry.getValue()));
280 checks[9] = true;
281 } else if (entry.getKey().toLowerCase().startsWith(KEY[10])) {
282 // Gets the SV clock bias
283 almanac.setAf0(Double.parseDouble(entry.getValue()));
284 checks[10] = true;
285 } else if (entry.getKey().toLowerCase().startsWith(KEY[11])) {
286 // Gets the SV clock Drift
287 almanac.setAf1(Double.parseDouble(entry.getValue()));
288 checks[11] = true;
289 } else if (entry.getKey().toLowerCase().startsWith(KEY[12])) {
290 // Gets the week number
291 almanac.setWeek(Integer.parseInt(entry.getValue()));
292 checks[12] = true;
293 } else {
294 // Unknown entry: the file is not a YUMA file
295 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_YUMA_ALMANAC_FILE,
296 name);
297 }
298 }
299
300 // If all expected fields have been read
301 if (readOK(checks)) {
302 // Returns a QZSSAlmanac built from the entries
303 almanac.setDate(new GNSSDate(almanac.getWeek(), almanac.getTime() * 1000.0, SatelliteSystem.QZSS).getDate());
304 return almanac;
305 } else {
306 // The file is not a YUMA file
307 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_YUMA_ALMANAC_FILE,
308 name);
309 }
310 } catch (NumberFormatException nfe) {
311 throw new OrekitException(OrekitMessages.NOT_A_SUPPORTED_YUMA_ALMANAC_FILE,
312 name);
313 }
314 }
315
316 /** Checks if all expected fields have been read.
317 * @param checks flags for read fields
318 * @return true if all expected fields have been read, false if not
319 */
320 private boolean readOK(final boolean[] checks) {
321 for (boolean check: checks) {
322 if (!check) {
323 return false;
324 }
325 }
326 return true;
327 }
328 }
329
330 }