1   /* Copyright 2022-2025 Luc Maisonobe
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.files.sinex;
18  
19  import org.orekit.gnss.ObservationType;
20  import org.orekit.gnss.PredefinedObservationType;
21  import org.orekit.gnss.SatelliteSystem;
22  import org.orekit.time.TimeScales;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.function.BiFunction;
28  
29  /** Parser for Solution INdependent EXchange (SINEX) bias files.
30   * @author Luc Maisonobe
31   * @since 13.0
32   */
33  public class SinexBiasParser extends AbstractSinexParser<SinexBias, SinexBiasParseInfo> {
34  
35      /** Mapper from string to observation type. */
36      private final BiFunction<? super SatelliteSystem, ? super String, ? extends ObservationType> typeBuilder;
37  
38      /** Top level parsers. */
39      private final List<LineParser<SinexBiasParseInfo>> topParsers;
40  
41      /** Simple constructor.
42       * @param timeScales time scales
43       * @param typeBuilder mapper from string to observation type
44       *                    (typically {@code SinexBiasParser::defaultTypeBuilder} if the file uses only
45       *                    predefined types)
46       * @see #defaultTypeBuilder(SatelliteSystem, String)
47       */
48      public SinexBiasParser(final TimeScales timeScales,
49                             final BiFunction<? super SatelliteSystem, ? super String, ? extends ObservationType> typeBuilder) {
50  
51          super(timeScales);
52  
53          this.typeBuilder = typeBuilder;
54  
55          // set up parsers for supported blocks
56          final List<BlockParser<SinexBiasParseInfo>> blockParsers = new ArrayList<>();
57          blockParsers.add(new BlockParser<>("BIAS/DESCRIPTION",
58                                             Arrays.asList(BiasDescriptionPredicate.OBSERVATION_SAMPLING,
59                                                           BiasDescriptionPredicate.PARAMETER_SPACING,
60                                                           BiasDescriptionPredicate.DETERMINATION_METHOD,
61                                                           BiasDescriptionPredicate.BIAS_MODE,
62                                                           BiasDescriptionPredicate.TIME_SYSTEM,
63                                                           new IgnoredBlockContentPredicate<>())));
64          blockParsers.add(new BlockParser<>("BIAS/SOLUTION",
65                                             Arrays.asList(BiasSolutionPredicate.DSB,
66                                                           BiasSolutionPredicate.OSB,
67                                                           // we currently ignore ISB lines
68                                                           new IgnoredBlockContentPredicate<>())));
69  
70          // append at the end of the list one catch-all parser ignoring all remaining not supported blocks
71          blockParsers.add(new IgnoredBlockParser<>());
72  
73          // add the parser for the footer
74          topParsers = new ArrayList<>(blockParsers);
75          topParsers.add(new FooterParser<>("%=ENDBIA"));
76  
77          // set up siblings
78          blockParsers.forEach(parser -> parser.setSiblingParsers(topParsers));
79  
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      protected LineParser<SinexBiasParseInfo> firstLineParser() {
85          return new VersionParser<SinexBiasParseInfo>("BIA") {
86              /** {@inheritDoc} */
87              @Override
88              public Iterable<LineParser<SinexBiasParseInfo>> allowedNextParsers(final SinexBiasParseInfo parseInfo) {
89                  return topParsers;
90              }
91          };
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      protected SinexBiasParseInfo buildParseInfo() {
97          final SinexBiasParseInfo parseInfo = new SinexBiasParseInfo(getTimeScales(), typeBuilder);
98          parseInfo.setTimeScale(getTimeScales().getUTC());
99          return parseInfo;
100     }
101 
102     /** Default type builder.
103      * <p>
104      * This default type builder directly calls {@link PredefinedObservationType#valueOf(String)}
105      * </p>
106      * @param ignoredSystem satellite system (ignored here)
107      * @param typeName name of the observation type
108      * @return observation type
109      */
110     public static ObservationType defaultTypeBuilder(final SatelliteSystem ignoredSystem, final String typeName) {
111         return PredefinedObservationType.valueOf(typeName);
112     }
113 
114 }