001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.calendar;
008:
009: // AstroTest
010:
011: import java.util.Date;
012: import java.util.Locale;
013:
014: import com.ibm.icu.dev.test.*;
015: import com.ibm.icu.util.*;
016: import com.ibm.icu.impl.CalendarAstronomer;
017: import com.ibm.icu.impl.CalendarAstronomer.*;
018: import com.ibm.icu.text.DateFormat;
019:
020: // TODO: try finding next new moon after 07/28/1984 16:00 GMT
021:
022: public class AstroTest extends TestFmwk {
023: public static void main(String[] args) throws Exception {
024: new AstroTest().run(args);
025: }
026:
027: static final double PI = Math.PI;
028:
029: public void TestSolarLongitude() {
030: GregorianCalendar gc = new GregorianCalendar(
031: new SimpleTimeZone(0, "UTC"));
032: CalendarAstronomer astro = new CalendarAstronomer();
033: final double tests[][] = { { 1980, 7, 27, 00, 00, 124.114347 },
034: { 1988, 7, 27, 00, 00, 124.187732 }, };
035: logln("");
036: for (int i = 0; i < tests.length; i++) {
037: gc.clear();
038: gc.set((int) tests[i][0], (int) tests[i][1] - 1,
039: (int) tests[i][2], (int) tests[i][3],
040: (int) tests[i][4]);
041:
042: astro.setDate(gc.getTime());
043:
044: double longitude = astro.getSunLongitude();
045: longitude = 0;
046: Equatorial result = astro.getSunPosition();
047: result = null;
048: }
049: }
050:
051: public void TestLunarPosition() {
052: GregorianCalendar gc = new GregorianCalendar(
053: new SimpleTimeZone(0, "UTC"));
054: CalendarAstronomer astro = new CalendarAstronomer();
055: final double tests[][] = { { 1979, 2, 26, 16, 00, 0, 0 }, };
056: logln("");
057:
058: for (int i = 0; i < tests.length; i++) {
059: gc.clear();
060: gc.set((int) tests[i][0], (int) tests[i][1] - 1,
061: (int) tests[i][2], (int) tests[i][3],
062: (int) tests[i][4]);
063: astro.setDate(gc.getTime());
064:
065: Equatorial result = astro.getMoonPosition();
066: result = null;
067: }
068:
069: }
070:
071: public void TestCoordinates() {
072: GregorianCalendar gc = new GregorianCalendar(
073: new SimpleTimeZone(0, "UTC"));
074: CalendarAstronomer astro = new CalendarAstronomer();
075: Equatorial result = astro.eclipticToEquatorial(
076: 139.686111 * PI / 180.0, 4.875278 * PI / 180.0);
077: logln("result is " + result + "; " + result.toHmsString());
078: }
079:
080: public void TestCoverage() {
081: GregorianCalendar gc = new GregorianCalendar(
082: new SimpleTimeZone(0, "UTC"));
083: GregorianCalendar cal = new GregorianCalendar(1958,
084: Calendar.AUGUST, 15);
085: Date then = cal.getTime();
086: CalendarAstronomer myastro = new CalendarAstronomer(then);
087:
088: //Latitude: 34 degrees 05' North
089: //Longitude: 118 degrees 22' West
090: double laLat = 34 + 5d / 60, laLong = 360 - (118 + 22d / 60);
091: CalendarAstronomer myastro2 = new CalendarAstronomer(laLong,
092: laLat);
093:
094: double eclLat = laLat * Math.PI / 360;
095: double eclLong = laLong * Math.PI / 360;
096: Ecliptic ecl = new Ecliptic(eclLat, eclLong);
097: logln("ecliptic: " + ecl);
098:
099: CalendarAstronomer myastro3 = new CalendarAstronomer();
100: myastro3.setJulianDay((4713 + 2000) * 365.25);
101:
102: CalendarAstronomer[] astronomers = { myastro, myastro2,
103: myastro3, myastro2 // check cache
104:
105: };
106:
107: for (int i = 0; i < astronomers.length; ++i) {
108: CalendarAstronomer astro = astronomers[i];
109:
110: logln("astro: " + astro);
111: logln(" time: " + astro.getTime());
112: logln(" date: " + astro.getDate());
113: logln(" cent: " + astro.getJulianCentury());
114: logln(" gw sidereal: " + astro.getGreenwichSidereal());
115: logln(" loc sidereal: " + astro.getLocalSidereal());
116: logln(" equ ecl: " + astro.eclipticToEquatorial(ecl));
117: logln(" equ long: " + astro.eclipticToEquatorial(eclLong));
118: logln(" horiz: " + astro.eclipticToHorizon(eclLong));
119: logln(" sunrise: " + new Date(astro.getSunRiseSet(true)));
120: logln(" sunset: " + new Date(astro.getSunRiseSet(false)));
121: logln(" moon phase: " + astro.getMoonPhase());
122: logln(" moonrise: "
123: + new Date(astro.getMoonRiseSet(true)));
124: logln(" moonset: "
125: + new Date(astro.getMoonRiseSet(false)));
126: logln(" prev summer solstice: "
127: + new Date(astro.getSunTime(
128: CalendarAstronomer.SUMMER_SOLSTICE, false)));
129: logln(" next summer solstice: "
130: + new Date(astro.getSunTime(
131: CalendarAstronomer.SUMMER_SOLSTICE, true)));
132: logln(" prev full moon: "
133: + new Date(astro.getMoonTime(
134: CalendarAstronomer.FULL_MOON, false)));
135: logln(" next full moon: "
136: + new Date(astro.getMoonTime(
137: CalendarAstronomer.FULL_MOON, true)));
138: }
139:
140: }
141:
142: static final long DAY_MS = 24 * 60 * 60 * 1000L;
143:
144: public void TestSunriseTimes() {
145:
146: // logln("Sunrise/Sunset times for San Jose, California, USA");
147: // CalendarAstronomer astro = new CalendarAstronomer(-121.55, 37.20);
148: // TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
149:
150: // We'll use a table generated by the UNSO website as our reference
151: // From: http://aa.usno.navy.mil/
152: //-Location: W079 25, N43 40
153: //-Rise and Set for the Sun for 2001
154: //-Zone: 4h West of Greenwich
155: int[] USNO = { 6, 59, 19, 45, 6, 57, 19, 46, 6, 56, 19, 47, 6,
156: 54, 19, 48, 6, 52, 19, 49, 6, 50, 19, 51, 6, 48, 19,
157: 52, 6, 47, 19, 53, 6, 45, 19, 54, 6, 43, 19, 55, 6, 42,
158: 19, 57, 6, 40, 19, 58, 6, 38, 19, 59, 6, 36, 20, 0, 6,
159: 35, 20, 1, 6, 33, 20, 3, 6, 31, 20, 4, 6, 30, 20, 5, 6,
160: 28, 20, 6, 6, 27, 20, 7, 6, 25, 20, 8, 6, 23, 20, 10,
161: 6, 22, 20, 11, 6, 20, 20, 12, 6, 19, 20, 13, 6, 17, 20,
162: 14, 6, 16, 20, 16, 6, 14, 20, 17, 6, 13, 20, 18, 6, 11,
163: 20, 19, };
164:
165: logln("Sunrise/Sunset times for Toronto, Canada");
166: CalendarAstronomer astro = new CalendarAstronomer(
167: -(79 + 25 / 60), 43 + 40 / 60);
168:
169: // As of ICU4J 2.8 the ICU4J time zones implement pass-through
170: // to the underlying JDK. Because of variation in the
171: // underlying JDKs, we have to use a fixed-offset
172: // SimpleTimeZone to get consistent behavior between JDKs.
173: // The offset we want is [-18000000, 3600000] (raw, dst).
174: // [aliu 10/15/03]
175:
176: // TimeZone tz = TimeZone.getTimeZone("America/Montreal");
177: TimeZone tz = new SimpleTimeZone(-18000000 + 3600000,
178: "Montreal(FIXED)");
179:
180: GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
181: GregorianCalendar cal2 = new GregorianCalendar(tz, Locale.US);
182: cal.clear();
183: cal.set(Calendar.YEAR, 2001);
184: cal.set(Calendar.MONTH, Calendar.APRIL);
185: cal.set(Calendar.DAY_OF_MONTH, 1);
186: cal.set(Calendar.HOUR_OF_DAY, 12); // must be near local noon for getSunRiseSet to work
187:
188: DateFormat df = DateFormat.getTimeInstance(cal,
189: DateFormat.MEDIUM, Locale.US);
190: DateFormat df2 = DateFormat.getDateTimeInstance(cal,
191: DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
192: DateFormat day = DateFormat.getDateInstance(cal,
193: DateFormat.MEDIUM, Locale.US);
194:
195: for (int i = 0; i < 30; i++) {
196: astro.setDate(cal.getTime());
197:
198: Date sunrise = new Date(astro.getSunRiseSet(true));
199: Date sunset = new Date(astro.getSunRiseSet(false));
200:
201: cal2.setTime(cal.getTime());
202: cal2.set(Calendar.SECOND, 0);
203: cal2.set(Calendar.MILLISECOND, 0);
204:
205: cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 0]);
206: cal2.set(Calendar.MINUTE, USNO[4 * i + 1]);
207: Date exprise = cal2.getTime();
208: cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 2]);
209: cal2.set(Calendar.MINUTE, USNO[4 * i + 3]);
210: Date expset = cal2.getTime();
211: // Compute delta of what we got to the USNO data, in seconds
212: int deltarise = Math.abs((int) (sunrise.getTime() - exprise
213: .getTime()) / 1000);
214: int deltaset = Math.abs((int) (sunset.getTime() - expset
215: .getTime()) / 1000);
216:
217: // Allow a deviation of 0..MAX_DEV seconds
218: // It would be nice to get down to 60 seconds, but at this
219: // point that appears to be impossible without a redo of the
220: // algorithm using something more advanced than Duffett-Smith.
221: final int MAX_DEV = 180;
222: if (deltarise > MAX_DEV || deltaset > MAX_DEV) {
223: if (deltarise > MAX_DEV) {
224: errln("FAIL: " + day.format(cal.getTime())
225: + ", Sunrise: " + df2.format(sunrise)
226: + " (USNO " + df.format(exprise) + " d="
227: + deltarise + "s)");
228: } else {
229: logln(day.format(cal.getTime()) + ", Sunrise: "
230: + df.format(sunrise) + " (USNO "
231: + df.format(exprise) + ")");
232: }
233: if (deltaset > MAX_DEV) {
234: errln("FAIL: " + day.format(cal.getTime())
235: + ", Sunset: " + df2.format(sunset)
236: + " (USNO " + df.format(expset) + " d="
237: + deltaset + "s)");
238: } else {
239: logln(day.format(cal.getTime()) + ", Sunset: "
240: + df.format(sunset) + " (USNO "
241: + df.format(expset) + ")");
242: }
243: } else {
244: logln(day.format(cal.getTime()) + ", Sunrise: "
245: + df.format(sunrise) + " (USNO "
246: + df.format(exprise) + ")" + ", Sunset: "
247: + df.format(sunset) + " (USNO "
248: + df.format(expset) + ")");
249: }
250: cal.add(Calendar.DATE, 1);
251: }
252:
253: // CalendarAstronomer a = new CalendarAstronomer(-(71+5/60), 42+37/60);
254: // cal.clear();
255: // cal.set(cal.YEAR, 1986);
256: // cal.set(cal.MONTH, cal.MARCH);
257: // cal.set(cal.DATE, 10);
258: // cal.set(cal.YEAR, 1988);
259: // cal.set(cal.MONTH, cal.JULY);
260: // cal.set(cal.DATE, 27);
261: // a.setDate(cal.getTime());
262: // long r = a.getSunRiseSet2(true);
263: }
264:
265: public void TestBasics() {
266: // Check that our JD computation is the same as the book's (p. 88)
267: GregorianCalendar gc = new GregorianCalendar(
268: new SimpleTimeZone(0, "UTC"));
269: CalendarAstronomer astro = new CalendarAstronomer();
270: GregorianCalendar cal3 = new GregorianCalendar(TimeZone
271: .getTimeZone("GMT"), Locale.US);
272: DateFormat d3 = DateFormat.getDateTimeInstance(cal3,
273: DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
274: cal3.clear();
275: cal3.set(Calendar.YEAR, 1980);
276: cal3.set(Calendar.MONTH, Calendar.JULY);
277: cal3.set(Calendar.DATE, 27);
278: astro.setDate(cal3.getTime());
279: double jd = astro.getJulianDay() - 2447891.5;
280: double exp = -3444;
281: if (jd == exp) {
282: logln(d3.format(cal3.getTime()) + " => " + jd);
283: } else {
284: errln("FAIL: " + d3.format(cal3.getTime()) + " => " + jd
285: + ", expected " + exp);
286: }
287:
288: // cal3.clear();
289: // cal3.set(cal3.YEAR, 1990);
290: // cal3.set(cal3.MONTH, Calendar.JANUARY);
291: // cal3.set(cal3.DATE, 1);
292: // cal3.add(cal3.DATE, -1);
293: // astro.setDate(cal3.getTime());
294: // astro.foo();
295: }
296: }
|