1   /* Contributed in the public domain.
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.data;
18  
19  import org.orekit.bodies.LazyLoadedCelestialBodies;
20  import org.orekit.forces.gravity.potential.LazyLoadedGravityFields;
21  import org.orekit.frames.Frame;
22  import org.orekit.frames.LazyLoadedEop;
23  import org.orekit.frames.LazyLoadedFrames;
24  import org.orekit.models.earth.LazyLoadedGeoMagneticFields;
25  import org.orekit.time.LazyLoadedTimeScales;
26  
27  /**
28   * A data context that aims to match the behavior of Orekit 10.0 regarding auxiliary data.
29   * This data context only loads auxiliary data when it is first accessed. It allows data
30   * loaders to be added before the data is loaded.
31   *
32   * @author Evan Ward
33   * @since 10.1
34   */
35  public class LazyLoadedDataContext implements DataContext {
36  
37      /** The data provider manager. */
38      private final DataProvidersManager dataProvidersManager;
39      /** EOP loader. */
40      private final LazyLoadedEop eop;
41      /** The time scales. */
42      private final LazyLoadedTimeScales timeScales;
43      /** The reference frames. */
44      private LazyLoadedFrames frames;
45      /** The celestial bodies. */
46      private LazyLoadedCelestialBodies bodies;
47      /** The gravity fields. */
48      private final LazyLoadedGravityFields gravityFields;
49      /** The magnetic fields. */
50      private final LazyLoadedGeoMagneticFields geoMagneticFields;
51  
52      /**
53       * Create a new data context that only loads auxiliary data when it is first accessed
54       * and allows configuration of the auxiliary data sources until then.
55       */
56      public LazyLoadedDataContext() {
57          this.dataProvidersManager = new DataProvidersManager();
58          this.eop = new LazyLoadedEop(dataProvidersManager);
59          this.timeScales = new LazyLoadedTimeScales(eop);
60          this.gravityFields =
61                  new LazyLoadedGravityFields(dataProvidersManager, timeScales.getTT());
62          this.geoMagneticFields = new LazyLoadedGeoMagneticFields(dataProvidersManager);
63          // creating Frames and CelestialBodies here creates an initialization problem for
64          // DataContext.getDefault(). Delay creating them until they are used for the first
65          // time.
66      }
67  
68      /**
69       * Get the provider of auxiliary data for this data context.
70       *
71       * @return the provider that supplies auxiliary data to all of the other methods of
72       * this data context.
73       */
74      public DataProvidersManager getDataProvidersManager() {
75          return dataProvidersManager;
76      }
77  
78      @Override
79      public LazyLoadedTimeScales getTimeScales() {
80          return timeScales;
81      }
82  
83      @Override
84      public LazyLoadedFrames getFrames() {
85          synchronized (this) {
86              if (this.frames == null) {
87                  this.frames = new LazyLoadedFrames(
88                                                     eop, getTimeScales(), getCelestialBodies());
89              }
90              return this.frames;
91          }
92      }
93  
94      @Override
95      public LazyLoadedCelestialBodies getCelestialBodies() {
96          synchronized (this) {
97              if (this.bodies == null) {
98                  final Frame gcrf = Frame.getRoot();
99                  this.bodies = new LazyLoadedCelestialBodies(
100                                                             getDataProvidersManager(), getTimeScales(), gcrf);
101             }
102             return this.bodies;
103         }
104     }
105 
106     @Override
107     public LazyLoadedGravityFields getGravityFields() {
108         return gravityFields;
109     }
110 
111     @Override
112     public LazyLoadedGeoMagneticFields getGeoMagneticFields() {
113         return geoMagneticFields;
114     }
115 
116 }