ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/cal/sun2.cal
Revision: 1.2
Committed: Fri Oct 18 23:42:52 2019 UTC (4 years, 7 months ago) by greg
Branch: MAIN
Changes since 1.1: +16 -16 lines
Log Message:
Working version of Michalsky solar position calculation

File Contents

# User Rev Content
1 greg 1.2 { RCSid $Id: sun2.cal,v 1.1 2019/10/17 03:53:10 greg Exp $ }
2 greg 1.1 {
3     Improved solar angle calculations based on "The Astronomical Almanac's
4 greg 1.2 Algorithm for Approximate Solar Position (1950-2050)" by Joseph J.
5     Michalsky, published in 1988 in Solar Energy, Vol. 40, No. 3
6 greg 1.1
7     Translated by G.Ward, October 2019
8    
9     Inputs:
10     LAT = Site latitude (degrees north of Equator)
11     LON = Site longitude (degrees west of Greenwich)
12     SM = Standard meridian (degrees west of Greenwich)
13     TIME = Standard time (fractional hours 0-24)
14     DAY = Day of the month
15     MONTH = Month (January == 1)
16 greg 1.2 YEAR = Calendar year AD (1950-2050)
17 greg 1.1
18     Outputs:
19     SAZI = Solar altitude (degrees above horizon)
20     SALT = Solar azimuth (degrees west of south)
21     }
22 greg 1.2 { Defaults for San Francisco, CA }
23     LAT = 37.8152145;
24     LON = 122.04;
25     SM = 120;
26 greg 1.1 DEG : PI/180;
27 greg 1.2 { Constants and functions }
28 greg 1.1 normcir(a,c) : if(-a, normcir(a+c, c), if(c-a, a, normcir(a-c, c)));
29     and(a,b) : if(a, b, a);
30 greg 1.2 leap(yr) : floor(yr/4 + .125) - yr/4 + .125;
31 greg 1.1 { Julian date }
32     delta = YEAR - 1949;
33     subjd = DAY + select(MONTH,0,31,59,90,120,151,181,212,243,273,304,334) +
34     if(and(MONTH-2.5,leap(YEAR)), 1, 0);
35     UTIME = TIME + SM/15;
36     jd = 2432916.5 + delta*365 + floor(delta/4) + subjd + UTIME/24;
37     { Ecliptic coordinates }
38     n = jd - 2451545;
39     L = normcir(280.460 + 0.9856474*n, 360);
40     g = normcir(357.528 + 0.9856003*n, 360);
41     l = normcir(L + 1.915*sin(g*DEG) + 0.020*sin(2*DEG*g), 360);
42     ep = 23.439 - 4e-7*n;
43     { Reused values }
44     sin_l = sin(l*DEG);
45     sin_LAT = sin(LAT*DEG);
46     { Right ascension and declination angles }
47     ra = normcir(atan2(sin_l*cos(ep*DEG), cos(l*DEG))/DEG, 360);
48 greg 1.2 sin_dec = sin(ep*DEG)*sin_l;
49     cos_dec = sqrt(1 - sin_dec*sin_dec);
50     dec = asin(sin_dec) / DEG;
51 greg 1.1 { Greenwich mean sidereal time }
52     gmst = normcir(6.697375 + 0.0657098242*n + UTIME, 24);
53     lmst = normcir(gmst - LON/15, 24);
54     ha = 15*(normcir(lmst - ra/15 + 12, 24) - 12);
55     { Final solar angles }
56 greg 1.2 sin_SALT = sin_dec*sin_LAT + cos_dec*cos(LAT*DEG)*cos(ha*DEG);
57     SALT = asin(sin_SALT) / DEG;
58 greg 1.1 az = asin(-cos_dec*sin(ha*DEG)/cos(SALT*DEG)) / DEG;
59 greg 1.2 SAZI = normcir(if(sin_dec-sin_SALT*sin_LAT, az, 180-az), 360) - 180;