001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/utils/SunPosition.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstraße 19
030 53177 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043
044 package org.deegree.ogcwebservices.wpvs.utils;
045
046 import java.util.Calendar;
047 import java.util.GregorianCalendar;
048
049 /**
050 *
051 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
052 * @author last edited by: $Author: apoth $
053 * @version $Revision: 9345 $ $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
054 */
055 public class SunPosition {
056
057 private int year;
058 private int month;
059 private int day;
060 private int hour;
061 private int minute;
062 private double daysSinceVernalEquinox;
063
064 /**
065 * Constructs a sunposition at the currenttime
066 */
067 public SunPosition(){
068 GregorianCalendar calendar = new GregorianCalendar();
069 this.year = calendar.get( Calendar.YEAR );
070 this.month = calendar.get( Calendar.MONTH ) + 1;
071 this.day = calendar.get( Calendar.DAY_OF_MONTH );
072 this.hour = calendar.get( Calendar.HOUR_OF_DAY );
073 this.minute = calendar.get( Calendar.MINUTE );
074 daysSinceVernalEquinox = getDaySinceVernalEquinox( );
075 }
076 /**
077 * Constructs a sunposition with the given Calendar
078 * @param calendar a given Calendar
079 */
080 public SunPosition( Calendar calendar ){
081 this( calendar.get( Calendar.YEAR ),
082 calendar.get( Calendar.MONTH ) + 1,
083 calendar.get( Calendar.DAY_OF_MONTH ),
084 calendar.get( Calendar.HOUR_OF_DAY ),
085 calendar.get( Calendar.MINUTE ) );
086 }
087
088 /**
089 * @param year
090 * @param month
091 * @param day
092 * @param hour
093 * @param minute
094 */
095 public SunPosition( int year, int month, int day, int hour, int minute ){
096 this.year = year;
097 this.month = month;
098 if( month <= 0 || month > 12 )
099 this.month = 1;
100 this.day = day;
101 if( day <= 0 || day > 32 )
102 this.day = 1;
103 this.hour = hour;
104 if( hour < 0 || hour >=24 )
105 this.hour = 0;
106 this.minute = minute;
107 if( minute < 0 || minute >= 60 )
108 this.minute = 0;
109
110 daysSinceVernalEquinox = getDaySinceVernalEquinox( );
111 }
112 /**
113 * calculates the solar altitude for given latitude, year, month, date, hour
114 * and minute
115 * @param latitude latitude of the the viewers position
116 * @return the solar altitud fo given latitude
117 */
118 public double getVerticalSunposition( double latitude )
119 {
120 // Hour Angle (H),
121 // Solar Declination (D),
122 // Latitude (L)
123 // solar altitude (A).
124 // sin(A) = sin(D)*sin(L) + cos(D)*cos(L)*cos(H)
125 double rad23_5 = 0.41015237421866745057706;
126 //double days = getDaySinceVernalEquinox( year, month, date );
127 double sinD = Math.sin( rad23_5 ) * Math.sin( Math.toRadians( daysSinceVernalEquinox * 360.0 / 365.0 ) );
128 double cosD = Math.cos( Math.asin( sinD ) );
129 // the sun hour angle is zero when the object is on the meridian
130 double h = getHorizontalSunPosition( ) - Math.toRadians(180);
131 double radL = Math.toRadians( latitude );
132 double sinA = sinD * Math.sin(radL) + cosD * Math.cos(radL) * Math.cos(h);
133
134 return Math.asin( sinA );
135 }
136
137 /**
138 * calculates the horizontal angle of the sun depending only on hour and minute!
139 * @return the horizontal angle
140 */
141 public double getHorizontalSunPosition( )
142 {
143 double d = hour + minute/60.0;
144 d = 180 + (( d - 12 ) * 15.0);
145 return Math.toRadians(d);
146 }
147
148 /**
149 * caluculates for a given date the number of days since the last vernal(spring)
150 * equinox. (leap years are considered)
151 */
152 private double getDaySinceVernalEquinox( )
153 {
154 //0 for january
155 GregorianCalendar calendar = new GregorianCalendar( year, 2, 21 );
156 int vEq = calendar.get( Calendar.DAY_OF_YEAR );
157 calendar = new GregorianCalendar( year, month-1, day-1 );
158 int doy = calendar.get( Calendar.DAY_OF_YEAR );
159 if ( doy < vEq ) {
160 doy = ( (calendar.isLeapYear(year)?366:365) - vEq ) + doy;
161 }
162 return doy-vEq;
163 }
164
165 }