1 package org.wcb.autohome.util;
2 /***
3 * Copyright (C) 1999 Walter Bogaardt
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 *
19 * Project: Home Automation Interface
20 * Filename: $Id: SunriseCalculator.java,v 1.0 1999/09/14 23:42:04 wbogaardt
21 */
22 import java.util.Calendar;
23 import java.util.GregorianCalendar;
24
25 public class SunriseCalculator {
26
27 double tpi=2 * Math.PI;
28 double degs = 180.0/Math.PI;
29 double rads = Math.PI/180.0;
30 double l,g,daylen;
31 double sunDia = 0.53;
32 double airRefact=34.0/60.0;
33 public double riseTime;
34 public double setTime;
35
36 public SunriseCalculator(double latitude, double longitude, double timezone ) {
37
38
39
40
41
42 GregorianCalendar curday = new GregorianCalendar();
43 int day=curday.get(Calendar.DATE);
44 int month=curday.get(Calendar.MONTH)+1;
45 int year=curday.get(Calendar.YEAR);
46 timeDate(year,day,month,latitude,longitude,timezone);
47 }
48
49 private double declination(int month, int day) {
50 double dec;
51 dec = day + 30.3*(month - 1);
52 dec = 10 + .988*dec;
53 dec = -23.5*Math.cos(dec* 3.14/180);
54 return dec;
55 }
56
57 private double fnacs(double x) {
58 return 3.14/2 - Math.atan(x/Math.sqrt(1-x*x));
59 }
60
61 private void sunrise(int month, int day, double latitude, double longitude) {
62 double sunrise, sunset, lengthDay;
63 double dec, y, x, z=0;
64 dec = declination(month, day);
65 x = longitude/15;
66 y = Math.tan(dec*3.14/180)*Math.tan(latitude*3.14/180);
67 if (y > 1 || y < -1)
68 System.out.println("No sunrise or sunset");
69 else
70 z = ((fnacs(y)*180)/3.14)/15;
71 sunrise = x + z;
72 sunset = x - z;
73 lengthDay = sunset - sunrise;
74 System.out.println("Sunrise"+sunrise);
75 System.out.println("Sunset"+sunset);
76 System.out.println("Length of day "+lengthDay);
77 }
78
79 public static void main (String args[]){
80 SunriseCalculator calc=new SunriseCalculator(34.03,118.15,16.78);
81 }
82
83 private void sunset(int month, int day, double latitude, double longitude){
84 double n,lo, c,c2,sd,cd,sc,c3,dawn,dusk;
85 int timeZone = 6;
86 n=(275*month/9)-2*((month+9)/12)+day - 30;
87 lo = 4.8771+0.0172*(n+0.5 - longitude/360);
88 c = 0.03342*Math.sin(lo+1.345);
89 c2 = (1/0.017453293)*(Math.atan(Math.tan(lo+c))-Math.atan(0.9175*Math.tan(lo+c))-c);
90 sd = 0.3978*Math.sin(lo+c);
91 cd= Math.sqrt(1-sd*sd);
92 sc=(sd*Math.sin(latitude*0.017453293)+0.014539)/(Math.cos(latitude*0.017453293)*cd);
93 c3=(1/0.017453293)*Math.atan(sc/Math.sqrt(1-sc*sc));
94 dawn = (6-timeZone-(longitude+c2+c3)/15)/24;
95 dusk = (18-timeZone - (longitude+c2-c3)/15)/24;
96 System.out.println("Dawn= "+dawn);
97 System.out.println("Dusk= "+dusk);
98 }
99
100 public void timeDate(int y, int day, int m, double latit, double longit,double tzone){
101 float h =12;
102 double d = fnDay(y,m,day,h);
103 double lambda = fnSun(d);
104 double obliq = 23.439*rads -.0000004 *rads *d;
105 double alpha = Math.atan2(Math.cos(obliq)*Math.sin(lambda), Math.cos(lambda));
106 double delta = Math.asin(Math.sin(obliq)*Math.sin(lambda));
107 double equation = 1440 - (l -alpha) *degs*4;
108 double ha = fO(latit, delta);
109 double riset = 12.0 - 12.0 * ha/Math.PI+tzone - longit/15.0+equation/60.0;
110 double settm = 12.0 + 12.0 * ha/Math.PI+tzone - longit/15.0+equation/60.0;
111 if (riset > 24.0) riset-=24.0;
112 if (settm > 24.0) settm-=24.0;
113 riseTime = riset;
114 setTime = settm;
115 }
116
117 public String getSunrise(){
118 return showhrmn(riseTime);
119 }
120
121 public String getSunset(){
122 return showhrmn(setTime);
123 }
124 public double fnDay(int y, int m, int d, float h){
125 long luku=-7*(y+(m+9)/12)/4+275*m/9+d;
126 luku+=y*367;
127 return luku - 730531.5+h/24.0;
128 }
129
130 public double fnRange(double x){
131 double b=x/tpi;
132 double a = tpi *(b -(long)(b));
133 if(a<0) a = tpi+a;
134 return a;
135 }
136
137 public double fnSun(double d){
138
139 double l = fnRange(280.461*rads+.9856474*rads*d);
140
141 double g = fnRange(357.528*rads+.9856003*rads*d);
142
143 return fnRange(l+1.915*rads*Math.sin(g)+.02*rads*Math.sin(2*g));
144 }
145
146 public double fO(double lat, double declin){
147 double fo;
148 fo = Math.tan(declin + rads*(0.5*sunDia+airRefact))*Math.tan(lat*rads);
149 if (fo>0.99999)
150 fo=1.0;
151 fo = Math.asin(fo)+Math.PI/2.0;
152 return fo;
153 }
154
155 public String showhrmn(double dhr){
156 int hr, mn;
157 hr=(int)dhr;
158 double min = (dhr-((double)hr))*60;
159 mn=(int)min;
160
161
162
163
164
165
166
167
168
169
170
171 return new String(hr+":"+mn);
172 }
173
174
175
176 }
177
178
179
180
181
182
183
184
185