-
Notifications
You must be signed in to change notification settings - Fork 0
/
sun.m
31 lines (26 loc) · 1.21 KB
/
sun.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [right_ascension, declination, distance, azimuth, altitude] = sun(day_number, latitude, longitude, UT)
% rotate equitorial coordinates
[x1, y1, z1, oblecl, L, lon, r] = sun_rectangular(day_number);
xequat = x1;
yequat = y1 * cosd(oblecl) - z1 * sind(oblecl);
zequat = y1 * sind(oblecl) - z1 * cosd(oblecl);
right_ascension = atan2d(yequat, xequat);
right_ascension = revolve_degree(right_ascension);
% convert right_ascension to hours
right_ascension = right_ascension / 15;
declination = atan2d(zequat, sqrt(xequat^2 + yequat^2));
distance = sqrt(xequat^2 + yequat^2 + zequat^2);
% calculate hour angle
hour_angle = (sidtime(day_number, longitude, UT) - right_ascension) * 15;
% convert hour_angle and declination to rectangular system
x2 = cosd(hour_angle) * cosd(declination);
y2 = sind(hour_angle) * cosd(declination);
z2 = sind(declination);
% rotate this along the y2 axis
xhor = x2 * sind(latitude) - z2 * cosd(latitude);
yhor = y2;
zhor = x2 * cosd(latitude) + z2 * sind(latitude);
% finally calculate azimuth and altitude
azimuth = atan2d(yhor, xhor) + 180;
altitude = atan2d(zhor, sqrt(xhor^2 + yhor^2));
end