001/*
002 * Shredzone Commons - suncalc
003 *
004 * Copyright (C) 2020 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
013 */
014package org.shredzone.commons.suncalc;
015
016import java.text.DateFormat;
017import java.util.Calendar;
018import java.util.Date;
019import java.util.TimeZone;
020
021import org.junit.Ignore;
022import org.junit.Test;
023import org.shredzone.commons.suncalc.param.TimeResultParameter;
024
025/**
026 * These are some examples that are meant to be executed manually.
027 *
028 * @see <a href="https://shredzone.org/maven/commons-suncalc/examples.html">Example
029 * chapter of the Documentation</a>
030 */
031@Ignore // No real unit tests, but meant to be run manually
032public class ExamplesTest {
033
034    @Test
035    public void testTimezone() {
036        // Our example takes place in Paris, so set the timezone accordingly.
037        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
038
039        SunTimes paris = SunTimes.compute()
040                .on(2020, 5, 1)             // May 1st, 2020, starting midnight
041                .latitude(48, 51, 24.0)     // Latitude of Paris: 48°51'24" N
042                .longitude(2, 21, 6.0)      // Longitude:          2°21'06" E
043                .execute();
044        System.out.println("Sunrise in Paris: " + paris.getRise());
045        System.out.println("Sunset in Paris:  " + paris.getSet());
046
047        SunTimes newYork = SunTimes.compute()
048                .on(2020, 5, 1)             // May 1st, 2020, starting midnight
049                .at(40.712778, -74.005833)  // Coordinates of New York
050                .execute();
051        System.out.println("Sunrise in New York: " + newYork.getRise());
052        System.out.println("Sunset in New York:  " + newYork.getSet());
053
054        SunTimes newYorkTz = SunTimes.compute()
055                .on(2020, 5, 1)             // May 1st, 2020, starting midnight
056                .timezone("America/New_York") // ...New York timezone
057                .at(40.712778, -74.005833)  // Coordinates of New York
058                .execute();
059        System.out.println("Sunrise in New York: " + newYorkTz.getRise());
060        System.out.println("Sunset in New York:  " + newYorkTz.getSet());
061
062        DateFormat formatter = DateFormat.getDateTimeInstance();
063        formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
064        System.out.println("Sunrise in New York: " + formatter.format(newYorkTz.getRise()));
065        System.out.println("Sunset in New York:  " + formatter.format(newYorkTz.getSet()));
066    }
067
068    @Test
069    public void testTimeWindow() {
070        // Our example takes place in Alert, Canada, so set the timezone accordingly.
071        TimeZone.setDefault(TimeZone.getTimeZone("Canada/Eastern"));
072
073        double[] ALERT_CANADA = new double[] { 82.5, -62.316667 };
074        SunTimes march = SunTimes.compute()
075                .on(2020, 3, 15)            // March 15th, 2020, starting midnight
076                .at(ALERT_CANADA)           // Coordinates are stored in an array
077                .execute();
078        System.out.println("Sunrise: " + march.getRise());
079        System.out.println("Sunset:  " + march.getSet());
080
081        SunTimes june = SunTimes.compute()
082                .on(2020, 6, 15)            // June 15th, 2020, starting midnight
083                .at(ALERT_CANADA)
084                .execute();
085        System.out.println("Sunrise: " + june.getRise());
086        System.out.println("Sunset:  " + june.getSet());
087
088        System.out.println("Sun is up all day:   " + june.isAlwaysUp());
089        System.out.println("Sun is down all day: " + june.isAlwaysDown());
090
091        SunTimes juneFullCycle = SunTimes.compute()
092                .on(2020, 6, 15)            // June 15th, 2020, starting midnight
093                .at(ALERT_CANADA)
094                .fullCycle()                // No 24h limit, we want to get the full cycle
095                .execute();
096        System.out.println("Sunset:  " + juneFullCycle.getSet());
097        System.out.println("Sunrise: " + juneFullCycle.getRise());
098    }
099
100    @Test
101    public void testParameterRecycling() {
102        double[] COLOGNE = new double[] { 50.938056, 6.956944 };
103
104        MoonTimes.Parameters parameters = MoonTimes.compute()
105                .at(COLOGNE)
106                .midnight();
107
108        MoonTimes today = parameters.execute();
109        System.out.println("Today, the moon rises in Cologne at " + today.getRise());
110
111        parameters.tomorrow();
112        MoonTimes tomorrow = parameters.execute();
113        System.out.println("Tomorrow, the moon will rise in Cologne at " + tomorrow.getRise());
114        System.out.println("But today, the moon still rises at " + today.getRise());
115    }
116
117    @Test
118    public void testParameterRecyclingLoop() {
119        MoonIllumination.Parameters parameters = MoonIllumination.compute()
120                .on(2020, 1, 1);
121
122        for (int i = 1; i <= 31; i++) {
123            long percent = Math.round(parameters.execute().getFraction() * 100.0);
124            System.out.println("On January " + i + " the moon was " + percent + "% lit.");
125            parameters.plusDays(1);
126        }
127    }
128
129    @Test
130    public void testGoldenHour() {
131        // Our example takes place in Singapore so set the timezone accordingly.
132        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Singapore"));
133
134        SunTimes.Parameters base = SunTimes.compute()
135                .at(1.283333, 103.833333)            // Singapore
136                .on(2020, 6, 1);
137
138        for (int i = 0; i < 4; i++) {
139            SunTimes blue = base.copy()
140                    .plusDays(i * 7)
141                    .twilight(SunTimes.Twilight.BLUE_HOUR)      // Blue Hour
142                    .execute();
143            SunTimes golden = base.copy()
144                    .plusDays(i * 7)
145                    .twilight(SunTimes.Twilight.GOLDEN_HOUR)    // Golden Hour
146                    .execute();
147
148            System.out.println("Morning golden hour starts at " + blue.getRise());
149            System.out.println("Morning golden hour ends at   " + golden.getRise());
150            System.out.println("Evening golden hour starts at " + golden.getSet());
151            System.out.println("Evening golden hour ends at   " + blue.getSet());
152        }
153    }
154
155    @Test
156    public void testMoonPhase() {
157        Calendar cal = Calendar.getInstance();
158        cal.clear();
159        cal.set(2023, Calendar.JANUARY, 1);
160
161        MoonPhase.Parameters parameters = MoonPhase.compute()
162                .phase(MoonPhase.Phase.FULL_MOON)
163                .truncatedTo(TimeResultParameter.Unit.DAYS);
164
165        while (cal.get(Calendar.MONTH) < Calendar.DECEMBER) {
166            MoonPhase phase = parameters.on(cal).execute();
167            Date fullMoonAt = phase.getTime();
168
169            System.out.print(fullMoonAt);
170            if (phase.isMicroMoon()) {
171                System.out.print(" (micromoon)");
172            }
173            if (phase.isSuperMoon()) {
174                System.out.print(" (supermoon)");
175            }
176            System.out.println();
177
178            cal.setTime(fullMoonAt);
179            cal.add(Calendar.DAY_OF_MONTH, 1);
180        }
181    }
182
183    @Test
184    public void testPositions() {
185        SunPosition.Parameters sunParam = SunPosition.compute()
186                .at(35.689722, 139.692222)      // Tokyo
187                .timezone("Asia/Tokyo")         // local time
188                .on(2018, 11, 13, 10, 3, 24);   // 2018-11-13 10:03:24
189
190        MoonPosition.Parameters moonParam = MoonPosition.compute()
191                .sameLocationAs(sunParam)
192                .sameTimeAs(sunParam);
193
194        SunPosition sun = sunParam.execute();
195        System.out.println(String.format(
196                "The sun can be seen %.1f° clockwise from the North and "
197                + "%.1f° above the horizon.\nIt is about %.0f km away right now.",
198                sun.getAzimuth(),
199                sun.getAltitude(),
200                sun.getDistance()
201        ));
202
203        MoonPosition moon = moonParam.execute();
204        System.out.println(String.format(
205                "The moon can be seen %.1f° clockwise from the North and "
206                + "%.1f° above the horizon.\nIt is about %.0f km away right now.",
207                moon.getAzimuth(),
208                moon.getAltitude(),
209                moon.getDistance()
210        ));
211    }
212
213}