001    package edu.nrao.sss.astronomy;
002    
003    import edu.nrao.sss.measure.ArcUnits;
004    import edu.nrao.sss.measure.Latitude;
005    import edu.nrao.sss.measure.Longitude;
006    import edu.nrao.sss.util.EnumerationUtility;
007    import edu.nrao.sss.util.StringUtil;
008    
009    /**
010     * An enumeration of coordinate systems commonly used in astronomy.
011     * <p>
012     * <b>Version Info:</b>
013     * <table style="margin-left:2em">
014     *   <tr><td>$Revision: 952 $</td></tr>
015     *   <tr><td>$Date: 2007-10-05 10:25:17 -0600 (Fri, 05 Oct 2007) $</td></tr>
016     *   <tr><td>$Author: dharland $</td></tr>
017     * </table></p>
018     *  
019     * @author David M. Harland
020     * @since 2006-07-18
021     */
022    public enum CelestialCoordinateSystem
023    {
024      //NOTE: the fromString method relies on all latitude and longitude
025      //      names and abbreviations being unique.  If you must violate the
026      //      uniqueness, you will have to recode fromString so that it does
027      //      not create enumeration elements from the lat/lon name/abbr.
028      //      Doing so may impact applications that rely on the current behavior.
029      /**
030       * A spherical system based on the projection of the earth's equator onto
031       * the celestial sphere.
032       */
033      EQUATORIAL("Right Ascension", "Declination", "RA", "Dec")
034      {
035        @Override
036        public String latitudeToString(Latitude latitude,
037                                       int minFracDigits, int maxFracDigits)
038        {
039          return latitude == null ?
040                 "NULL" : latitude.toStringDms(minFracDigits, maxFracDigits);
041        }
042        
043        @Override
044        public String longitudeToString(Longitude longitude,
045                                        int minFracDigits, int maxFracDigits)
046        {
047          return longitude == null ?
048                 "NULL" : longitude.toStringHms(minFracDigits, maxFracDigits);
049        }
050      },
051      
052      /**
053       * A spherical system based on the projection of the observer's horizon onto
054       * the celestial sphere.
055       */
056      HORIZONTAL("Azimuth", "Elevation", "Az", "El"),
057      
058      /**
059       * A spherical system based on the center of the sun and the central plane
060       * of our galaxy.
061       */
062      GALACTIC("Galactic Longitude", "Galactic Latitude", "GLon", "GLat"),
063      
064      /**
065       * A spherical system based on the center of the earth and the earth's
066       * orbit around the sun.
067       */
068      ECLIPTIC("Ecliptic Longitude", "Ecliptic Latitude", "ELon", "ELat");
069      
070      //SUPERGALACTIC
071      
072      private String nameOfLongitude;
073      private String nameOfLatitude;
074      private String abbrOfLongitude;
075      private String abbrOfLatitude;
076      
077      private CelestialCoordinateSystem(String longName, String latName,
078                                        String longAbbr, String latAbbr)
079      {
080        nameOfLongitude = longName;
081        nameOfLatitude  = latName;
082        abbrOfLongitude = longAbbr;
083        abbrOfLatitude  = latAbbr;
084      }
085      
086      /**
087       * Returns the name commonly used for the longitude component of this
088       * system.
089       * @return the name of the longitude component of this system.
090       */
091      public String getNameOfLongitude()  { return nameOfLongitude; }
092      
093      /**
094       * Returns the name commonly used for the latitude component of this
095       * system.
096       * @return the name of the latitude component of this system.
097       */
098      public String getNameOfLatitude()  { return nameOfLatitude; }
099      
100      /**
101       * Returns the abbreviation commonly used for the longitude component of this
102       * system.
103       * @return an abbreviation for the longitude component of this system.
104       */
105      public String getAbbreviationForLongitude()  { return abbrOfLongitude; }
106      
107      /**
108       * Returns the abbreviation commonly used for the latitude component of this
109       * system.
110       * @return an abbreviation for the latitude component of this system.
111       */
112      public String getAbbreviationForLatitude()  { return abbrOfLatitude; }
113      
114      /**
115       * Returns a text representation of {@code latitude} that is appropriate
116       * for this coordinate system.  Note that this might <i>not</i> be the
117       * same text returned by {@code latitude.toString()}.
118       * @param latitude the latitude to be represented as text.
119       * @param minFracDigits the minimum number of places after the decimal point.
120       * @param maxFracDigits the maximum number of places after the decimal point.
121       * @return a text representation of {@code latitude}.
122       */
123      public String latitudeToString(Latitude latitude,
124                                     int minFracDigits, int maxFracDigits)
125      {
126        String text = "NULL";
127        
128        if (latitude != null)
129        {
130          text =  latitude.clone().convertTo(ArcUnits.DEGREE)
131                          .toString(minFracDigits, maxFracDigits);
132        }
133        
134        return text;
135      }
136      
137      /**
138       * Returns a text representation of {@code longitude} that is appropriate
139       * for this coordinate system.  Note that this might <i>not</i> be the
140       * same text returned by {@code longitude.toString()}.
141       * @param longitude the longitude to be represented as text.
142       * @param minFracDigits the minimum number of places after the decimal point.
143       * @param maxFracDigits the maximum number of places after the decimal point.
144       * @return a text representation of {@code longitude}.
145       */
146      public String longitudeToString(Longitude longitude,
147                                      int minFracDigits, int maxFracDigits)
148      {
149        String text = "NULL";
150        
151        if (longitude != null)
152        {
153          text =  longitude.clone().convertTo(ArcUnits.DEGREE)
154                           .toString(minFracDigits, maxFracDigits);
155        }
156        
157        return text;
158      }
159      
160      /**
161       * Returns a default coordinate system.
162       * @return a default coordinate system.
163       */
164      public static CelestialCoordinateSystem getDefault()
165      {
166        return EQUATORIAL;
167      }
168      
169      /**
170       * Returns a text representation of this enumeration constant.
171       * @return a text representation of this enumeration constant.
172       */
173      public String toString()
174      {
175        return EnumerationUtility.getSharedInstance().enumToString(this);
176      }
177      
178      /**
179       * Returns the celestial coordinate system represented by {@code text}.
180       * <p>
181       * For details about the transformation, see
182       * {@link EnumerationUtility#enumFromString(Class, String)}.</p>
183       * 
184       * @param text a text representation of a celestial coordinate system.
185       * 
186       * @return the celestial coordinate system represented by {@code text}.
187       */
188      public static CelestialCoordinateSystem fromString(String text)
189      {
190        CelestialCoordinateSystem result =
191          EnumerationUtility.getSharedInstance()
192                            .enumFromString(CelestialCoordinateSystem.class, text);
193        
194        if (result == null)
195        {
196          text = StringUtil.getInstance().normalizeString(text);
197    
198          for (CelestialCoordinateSystem ccs : CelestialCoordinateSystem.values())
199          {
200            if (ccs.nameOfLatitude.equalsIgnoreCase(text) ||
201                ccs.abbrOfLatitude.equalsIgnoreCase(text) ||
202                ccs.nameOfLongitude.equalsIgnoreCase(text) ||
203                ccs.abbrOfLongitude.equalsIgnoreCase(text))
204            {
205              result = ccs;
206              break;
207            }
208          }
209        }
210        
211        return result;
212      }
213      
214      //Quick & dirty testing
215      /*
216      public static void main(String[] args) throws Exception
217      {
218        Latitude lat = new Latitude(87.654);
219        Longitude lon = new Longitude(123.456);
220        
221        System.out.println();
222        System.out.print("Galactic:   ");
223        System.out.print(" Lon = ");
224        System.out.print(CelestialCoordinateSystem.GALACTIC.longitudeToString(lon, 3, 3));
225        System.out.print(", Lat = ");
226        System.out.print(CelestialCoordinateSystem.GALACTIC.latitudeToString(lat, 3, 3));
227        System.out.println();
228        
229        System.out.println();
230        System.out.print("Equatorial: ");
231        System.out.print(" Lon = ");
232        System.out.print(CelestialCoordinateSystem.EQUATORIAL.longitudeToString(lon, 3, 3));
233        System.out.print(", Lat = ");
234        System.out.print(CelestialCoordinateSystem.EQUATORIAL.latitudeToString(lat, 3, 3));
235        System.out.println();
236        
237        lat = new Latitude(-25.0, ArcUnits.PERCENT);
238        lon = new Longitude(3.14159/4.0, ArcUnits.RADIAN);
239        
240        System.out.println();
241        System.out.print("Galactic:   ");
242        System.out.print(" Lon = ");
243        System.out.print(CelestialCoordinateSystem.GALACTIC.longitudeToString(lon, 3, 3));
244        System.out.print(", Lat = ");
245        System.out.print(CelestialCoordinateSystem.GALACTIC.latitudeToString(lat, 3, 3));
246        System.out.println();
247      }
248      */
249    }