1   /* Copyright 2002-2024 Thales Alenia Space
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 org.orekit.annotation.DefaultDataContext;
20  import org.orekit.data.DataContext;
21  import org.orekit.errors.OrekitException;
22  import org.orekit.errors.OrekitMessages;
23  import org.orekit.frames.Frame;
24  import org.orekit.frames.Frames;
25  import org.orekit.frames.ITRFVersion;
26  import org.orekit.frames.Predefined;
27  import org.orekit.frames.VersionedITRF;
28  import org.orekit.utils.IERSConventions;
29  
30  import java.util.Locale;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  /** Utility for IGS files.
35   * @author Luc Maisonobe
36   * @since 12.1
37   *
38   */
39  public class IGSUtils {
40  
41      /** Pattern for frame names with year.
42       * @since 12.1
43       */
44      private static final Pattern EARTH_FRAME_WITH_YEAR = Pattern.compile("(?:IER|ITR|ITRF|IGS|IGb|SLR)([0-9]{2})");
45  
46      /** Pattern for GCRF inertial frame.
47       * @since 12.1
48       */
49      private static final Pattern GCRF_FRAME = Pattern.compile(" *GCRF *");
50  
51      /** Pattern for EME2000 inertial frame.
52       * @since 12.1
53       */
54      private static final Pattern EME2000_FRAME = Pattern.compile("EME(?:00|2K)");
55  
56      /** Private constructor for a utility class.
57       */
58      private IGSUtils() {
59          // nothing to do
60      }
61  
62      /** Default string to {@link Frame} conversion for {@link org.orekit.files.sp3.SP3Parser}
63       * or {@link org.orekit.files.rinex.clock.RinexClockParser}.
64       *
65       * <p>
66       * This method uses the {@link DataContext#getDefault() default data context}.
67       * If the frame names has a form like IGS##, or ITR##, or ITRF##, or SLR##,
68       * where ## is a two digits number, then this number will be used to build the
69       * appropriate {@link ITRFVersion}. Otherwise (for example if name is
70       * UNDEF or WGS84), then a default {@link
71       * org.orekit.frames.Frames#getITRF(IERSConventions, boolean) ITRF}
72       * will be created.
73       * </p>
74       *
75       * @param name of the frame.
76       * @return ITRF based on 2010 conventions,
77       * with tidal effects considered during EOP interpolation
78       * @since 12.1
79       */
80      @DefaultDataContext
81      public static Frame guessFrame(final String name) {
82          return guessFrame(DataContext.getDefault().getFrames(), name);
83      }
84  
85      /** Default string to {@link Frame} conversion for {@link org.orekit.files.sp3.SP3Parser}
86       * or {@link org.orekit.files.rinex.clock.RinexClockParser}.
87       *
88       * <p>
89       * Various frame names are supported:
90       * </p>
91       * <ul>
92       *     <li>IGS##, or ITR##, or ITRF##, or SLR##,
93       *      where ## is a two digits number, then this number will be used to build the
94       *      appropriate {@link ITRFVersion}</li>
95       *     <li>GCRF (left or right justified) for GCRF inertial frame</li>
96       *     <li>EME00 or EME2K for EME2000 inertial frame</li>
97       *     <li>for all other names (for example if name is UNDEF or WGS84),
98       *     then a default {@link org.orekit.frames.Frames#getITRF(IERSConventions, boolean) ITRF}
99       *     frame will be selected</li>
100      * </ul>
101      * <p>
102      * Note that using inertial frames in classical products like SP3 files is non-standard,
103      * it is supported by Orekit, but may not be supported by other programs, so they should
104      * be used with caution when writing files.
105      * </p>
106      *
107      * @param frames frames factory
108      * @param name of the frame.
109      * @return guessed frame
110      * @since 12.1
111      */
112     public static Frame guessFrame(final Frames frames, final String name) {
113         final Matcher earthMatcher = EARTH_FRAME_WITH_YEAR.matcher(name);
114         if (earthMatcher.matches()) {
115             // this is a frame of the form IGS14, or ITR20, or SLR08, or similar
116             final int yy = Integer.parseInt(earthMatcher.group(1));
117             final ITRFVersion itrfVersion = ITRFVersion.getITRFVersion(yy);
118             final IERSConventions conventions =
119                 itrfVersion.getYear() < 2003 ?
120                 IERSConventions.IERS_1996 :
121                 (itrfVersion.getYear() < 2010 ? IERSConventions.IERS_2003 : IERSConventions.IERS_2010);
122             return frames.getITRF(itrfVersion, conventions, false);
123         } else {
124             final Matcher gcrfMatcher = GCRF_FRAME.matcher(name);
125             if (gcrfMatcher.matches()) {
126                 // inertial GCRF frame
127                 return frames.getGCRF();
128             } else {
129                 final Matcher eme2000Matcher = EME2000_FRAME.matcher(name);
130                 if (eme2000Matcher.matches()) {
131                     // inertial EME2000 frame
132                     return frames.getEME2000();
133                 } else {
134                     // unknown frame 'maybe UNDEF or WGS84
135                     // we use a default ITRF
136                     return frames.getITRF(IERSConventions.IERS_2010, false);
137                 }
138             }
139         }
140     }
141 
142     /** Guess a frame name.
143      * <p>
144      * If the frame is not compatible with {@link #guessFrame(Frames, String)},
145      * an exception will be triggered
146      * </p>
147      * @param frame frame from which we want the name
148      * @return name compatible with {@link #guessFrame(Frames, String)}
149      * @since 12.1
150      */
151     public static String frameName(final Frame frame) {
152         if (frame instanceof VersionedITRF) {
153             final int yy = ((VersionedITRF) frame).getITRFVersion().getYear() % 100;
154             return String.format(Locale.US, "IGS%02d", yy);
155         } else if (Predefined.GCRF.getName().equals(frame.getName())) {
156             return "GCRF";
157         } else if (Predefined.EME2000.getName().equals(frame.getName())) {
158             return "EME2K";
159         } else {
160             throw new OrekitException(OrekitMessages.FRAME_NOT_ALLOWED, frame.getName());
161         }
162     }
163 
164 }