calc_sun 0.1.1 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.autotest +15 -0
- data/.gitignore +12 -0
- data/.source_index +0 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +19 -28
- data/Guardfile +41 -0
- data/LICENSE.txt +21 -21
- data/Manifest.txt +20 -4
- data/README.rdoc +45 -37
- data/Rakefile +76 -11
- data/calc_sun.gemspec +10 -15
- data/dev.md +49 -0
- data/example/Almanac.html +1456 -0
- data/example/Makefile +10 -0
- data/example/adt_helper.rb +91 -0
- data/example/askgeo_query.rb +32 -0
- data/example/eq_center.md +15 -0
- data/example/sunriset.c +670 -0
- data/example/sunriset.exe +0 -0
- data/example/sunriset.rb +16 -28
- data/example/test_my_times.rb +134 -0
- data/ext/calc_sun/calc_sun.c +491 -224
- data/ext/side_time/extconf.rb +5 -0
- data/ext/side_time/side_time.c +117 -0
- data/lib/calc_sun.rb +1 -0
- data/lib/calc_sun/version.rb +3 -2
- data/lib/side_time/side_time.so +0 -0
- data/lib/side_time/version.rb +7 -0
- data/lib/sidereal_time.rb +4 -0
- metadata +37 -62
- metadata.gz.sig +0 -0
- data/test/calc_sun_test.rb +0 -230
data/example/Makefile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'calc_sun'
|
4
|
+
require 'date'
|
5
|
+
require 'eot'
|
6
|
+
# This is for equationoftime gem but conversion is in process.
|
7
|
+
#
|
8
|
+
class AnalemmaDataTable
|
9
|
+
attr_accessor :year,
|
10
|
+
:eot,
|
11
|
+
:finish,
|
12
|
+
:start,
|
13
|
+
:span,
|
14
|
+
:page,
|
15
|
+
:table
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@year = Time.now.utc.year
|
19
|
+
@eot = Eot.new
|
20
|
+
@cs = CalcSun.new
|
21
|
+
@start = Date.parse("#{@year}-1-1")
|
22
|
+
@finish = Date.parse("#{@year}-12-31")
|
23
|
+
@span = 0..(@finish - @start).to_i
|
24
|
+
@page = '<head>'
|
25
|
+
@page << '<link rel="stylesheet", type="text/css",
|
26
|
+
href="/stylesheets/app.css">'
|
27
|
+
@page << '</link></head><body>'
|
28
|
+
build
|
29
|
+
end
|
30
|
+
|
31
|
+
def page_head
|
32
|
+
@page << "<h2 align=center>Analemma Data for #{@year}</h2>"
|
33
|
+
@page << '<div align="center">'
|
34
|
+
@page << '<table>'
|
35
|
+
@page << '<tbody>'
|
36
|
+
@page << '<tr><th></th><th></th>'
|
37
|
+
@page << '<th> yday </th>'
|
38
|
+
@page << '<th> date </th>'
|
39
|
+
end
|
40
|
+
|
41
|
+
def table_head
|
42
|
+
@page << '<th> Julian No. </th>'
|
43
|
+
@page << '<th> Oblique ΔT </th>'
|
44
|
+
@page << '<th> Orbital ΔT </th>'
|
45
|
+
@page << '<th> Total ΔT </th>'
|
46
|
+
@page << '<th> Right Ascension </th>'
|
47
|
+
@page << '<th> Declination </th>'
|
48
|
+
@page << '<th></th><th></th></tr>'
|
49
|
+
end
|
50
|
+
|
51
|
+
def setup_each_iteration(inc)
|
52
|
+
@cs.jd(@start + inc)
|
53
|
+
@eot.jd = (@start + inc).jd + 0.5
|
54
|
+
@eot.ajd = (@start + inc).ajd + 0.5
|
55
|
+
@eot.ma_ta_set
|
56
|
+
end
|
57
|
+
|
58
|
+
def table_data(inc)
|
59
|
+
@page << "<td><b>#{@eot.show_minutes(@eot.time_delta_oblique)}<b/></td>"
|
60
|
+
@page << "<td><b>#{@eot.show_minutes(@eot.time_delta_orbit)}<b/></td>"
|
61
|
+
@page << "<td><b>#{@eot.show_minutes(@eot.time_eot)}<b/></td>"
|
62
|
+
@page << "<td><b>#{@eot.string_time((@eot.ra_sun * Eot::R2D) / 15.0)}<b/></td>"
|
63
|
+
@page << "<td><b>#{@cs.declination(inc)}<b/></td>"
|
64
|
+
end
|
65
|
+
|
66
|
+
def table_body
|
67
|
+
@span.each do |i|
|
68
|
+
setup_each_iteration(i)
|
69
|
+
@page << '<tr><td><b><b/></td><td><b><b/></td>'
|
70
|
+
@page << "<td><b>#{(@start + i).yday}<b/></td>"
|
71
|
+
@page << "<td><b>#{(@start + i).month}/#{(@start + i).day}</b></td>"
|
72
|
+
@page << "<td><b>#{(@start + i).jd}.0<b/></td>"
|
73
|
+
table_data(i)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def table_foot
|
78
|
+
@page << '<td><b><b/></td><td><b><b/></td></tr>'
|
79
|
+
@page << '</tbody></table></div>'
|
80
|
+
end
|
81
|
+
|
82
|
+
def build
|
83
|
+
page_head
|
84
|
+
table_head
|
85
|
+
table_body
|
86
|
+
table_foot
|
87
|
+
@page << '</body>'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
p AnalemmaDataTable.new
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
# askgeo_query.rb
|
3
|
+
|
4
|
+
require 'date'
|
5
|
+
require 'addressable/uri'
|
6
|
+
require 'json'
|
7
|
+
require 'rest-client'
|
8
|
+
|
9
|
+
lat = 51.4770228
|
10
|
+
lon = -0.0001147
|
11
|
+
base_url = 'http://api.askgeo.com/v1'
|
12
|
+
account_id = '1738' # my account for now but you can get one it's free
|
13
|
+
# my api key too
|
14
|
+
api_key = '7f33e4f2a928bc460cbb997fe3b5cb0b30fc7abebe16422e6b267989f80297d0'
|
15
|
+
url = "#{base_url}/#{account_id}/#{api_key}/query.json?points="
|
16
|
+
query = "#{lat},#{lon};databases=Astronomy"
|
17
|
+
addr = Addressable::URI.escape(url + query)
|
18
|
+
rest_resource = JSON.parse(RestClient.get(addr))
|
19
|
+
puts status = rest_resource['message']
|
20
|
+
results = rest_resource['data']
|
21
|
+
if status == 'ok'
|
22
|
+
astronomy = {}
|
23
|
+
results[0].fetch('Astronomy').each do |key, value|
|
24
|
+
astronomy.store(key, value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
puts astronomy.fetch('TodaySunriseIso8601')
|
28
|
+
puts astronomy.fetch('TodaySolarNoonIso8601')
|
29
|
+
puts astronomy.fetch('TodaySunsetIso8601')
|
30
|
+
puts astronomy
|
31
|
+
|
32
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
# Equation of Center
|
3
|
+
|
4
|
+
Ccen =
|
5
|
+
(2e - e3 / 4 + (5 / 96)e5) * sinM +
|
6
|
+
((5 / 4)e2 - (11 / 24)e4) * sin(2M) +
|
7
|
+
((13 / 12)e3 - (43 / 64)e5) * sin(3M) +
|
8
|
+
(103 / 96)e4) * sin(4M) +
|
9
|
+
(1097 / 960)e5 * sin(5M)
|
10
|
+
|
11
|
+
* [Equation of Center](https://en.wikipedia.org/wiki/Equation_of_the_center)
|
12
|
+
|
13
|
+
* [Determinates](https://en.wikipedia.org/wiki/Determinant)
|
14
|
+
|
15
|
+
* [Matrix](https://www.youtube.com/watch?v=95zVTo2nu3Q)
|
data/example/sunriset.c
ADDED
@@ -0,0 +1,670 @@
|
|
1
|
+
/* if you have the SOFA C lib installed
|
2
|
+
/* gcc -o sunriset sunriset.c -lm -lsofa_c
|
3
|
+
|
4
|
+
SUNRISET.C - computes Sun rise/set times, start/end of twilight, and
|
5
|
+
the length of the day at any date and latitude
|
6
|
+
|
7
|
+
Written as DAYLEN.C, 1989-08-16
|
8
|
+
|
9
|
+
Modified to SUNRISET.C, 1992-12-01
|
10
|
+
|
11
|
+
(c) Paul Schlyter, 1989, 1992
|
12
|
+
|
13
|
+
Released to the public domain by Paul Schlyter, December 1992
|
14
|
+
|
15
|
+
http://stjarnhimlen.se/comp/sunriset.c
|
16
|
+
|
17
|
+
*/
|
18
|
+
|
19
|
+
// #include <sofa.h>
|
20
|
+
#include <stdio.h>
|
21
|
+
#include <math.h>
|
22
|
+
#include <time.h>
|
23
|
+
|
24
|
+
|
25
|
+
/* A macro to compute the number of days elapsed since 2000 Jan 0.0 */
|
26
|
+
/* (which is equal to 1999 Dec 31, 0h UT) */
|
27
|
+
|
28
|
+
#define days_since_2000_Jan_0(y,m,d) \
|
29
|
+
(367L * (y) - ((7 * ((y) + (((m) + 9) / 12))) / 4) + ((275 * (m)) /9) + (d) - 730531.5L)
|
30
|
+
|
31
|
+
/* Some conversion factors between radians and degrees */
|
32
|
+
|
33
|
+
#ifndef PI
|
34
|
+
#define PI 3.1415926535897932384
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#define RADEG ( 180.0 / PI )
|
38
|
+
#define DEGRAD ( PI / 180.0 )
|
39
|
+
|
40
|
+
/* The trigonometric functions in degrees */
|
41
|
+
|
42
|
+
#define sind(x) sin((x) * DEGRAD)
|
43
|
+
#define cosd(x) cos((x) * DEGRAD)
|
44
|
+
#define tand(x) tan((x) * DEGRAD)
|
45
|
+
|
46
|
+
#define atand(x) (RADEG * atan(x))
|
47
|
+
#define asind(x) (RADEG * asin(x))
|
48
|
+
#define acosd(x) (RADEG * acos(x))
|
49
|
+
#define atan2d(y,x) (RADEG * atan2(y,x));
|
50
|
+
|
51
|
+
|
52
|
+
/* Following are some macros around the "workhorse" function __daylen__ */
|
53
|
+
/* They mainly fill in the desired values for the reference altitude */
|
54
|
+
/* below the horizon, and also selects whether this altitude should */
|
55
|
+
/* refer to the Sun's center or its upper limb. */
|
56
|
+
/* This macro computes the length of the day, from sunrise to sunset. */
|
57
|
+
/* Sunrise/set is considered to occur when the Sun's upper limb is */
|
58
|
+
/* 35 arc minutes below the horizon (this accounts for the refraction */
|
59
|
+
/* of the Earth's atmosphere). */
|
60
|
+
#define day_length(jd, lon, lat) \
|
61
|
+
__daylen__( jd, lon, lat, -35.0/60.0, 1 );
|
62
|
+
// #define day_length(year,month,day,lon,lat) \
|
63
|
+
//~ __daylen__( year, month, day, lon, lat, -35.0/60.0, 1 )
|
64
|
+
|
65
|
+
/* This macro computes the length of the day, including civil twilight. */
|
66
|
+
/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
|
67
|
+
/* the horizon. */
|
68
|
+
#define day_civil_twilight_length(jd,lon,lat) \
|
69
|
+
__daylen__( jd, lon, lat, -6.0, 0 );
|
70
|
+
// #define day_civil_twilight_length(year,month,day,lon,lat) \
|
71
|
+
// __daylen__( year, month, day, lon, lat, -6.0, 0 )
|
72
|
+
|
73
|
+
/* This macro computes the length of the day, incl. nautical twilight. */
|
74
|
+
/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
|
75
|
+
/* below the horizon. */
|
76
|
+
#define day_nautical_twilight_length(jd, lon, lat) \
|
77
|
+
__daylen__( jd, lon, lat, -12.0, 0 );
|
78
|
+
// #define day_nautical_twilight_length(year,month,day,lon,lat) \
|
79
|
+
// __daylen__( year, month, day, lon, lat, -12.0, 0 )
|
80
|
+
|
81
|
+
/* This macro computes the length of the day, incl. astronomical twilight. */
|
82
|
+
/* Astronomical twilight starts/ends when the Sun's center is 18 degrees */
|
83
|
+
/* below the horizon. */
|
84
|
+
#define day_astronomical_twilight_length(jd, lon, lat) \
|
85
|
+
__daylen__( jd, lon, lat, -18.0, 0 );
|
86
|
+
// #define day_astronomical_twilight_length(year,month,day,lon,lat) \
|
87
|
+
// __daylen__( year, month, day, lon, lat, -18.0, 0 )
|
88
|
+
|
89
|
+
/* This macro computes times for sunrise/sunset. */
|
90
|
+
/* Sunrise/set is considered to occur when the Sun's upper limb is */
|
91
|
+
/* 35 arc minutes below the horizon (this accounts for the refraction */
|
92
|
+
/* of the Earth's atmosphere). */
|
93
|
+
#define sun_rise_set(jd, lon, lat, rise, set) \
|
94
|
+
__sunriset__( jd, lon, lat, -35.0/60.0, 0, rise, set );
|
95
|
+
// #define sun_rise_set(year,month,day,lon,lat,rise,set) \
|
96
|
+
// __sunriset__( year, month, day, lon, lat, -35.0/60.0, 0, rise, set )
|
97
|
+
|
98
|
+
/* This macro computes the start and end times of civil twilight. */
|
99
|
+
/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
|
100
|
+
/* the horizon. */
|
101
|
+
#define civil_twilight(jd, lon, lat, start, end) \
|
102
|
+
__sunriset__( jd, lon, lat, -6.0, 0, start, end );
|
103
|
+
// #define civil_twilight(year,month,day,lon,lat,start,end) \
|
104
|
+
// __sunriset__( year, month, day, lon, lat, -6.0, 0, start, end )
|
105
|
+
|
106
|
+
/* This macro computes the start and end times of nautical twilight. */
|
107
|
+
/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
|
108
|
+
/* below the horizon. */
|
109
|
+
#define nautical_twilight(jd, lon, lat, start, end) \
|
110
|
+
__sunriset__( jd, lon, lat, -12.0, 0, start, end );
|
111
|
+
// #define nautical_twilight(year,month,day,lon,lat,start,end) \
|
112
|
+
// __sunriset__( year, month, day, lon, lat, -12.0, 0, start, end )
|
113
|
+
|
114
|
+
/* This macro computes the start and end times of astronomical twilight. */
|
115
|
+
/* Astronomical twilight starts/ends when the Sun's center is 18 degrees */
|
116
|
+
/* below the horizon. */
|
117
|
+
#define astronomical_twilight(jd, lon, lat, start, end) \
|
118
|
+
__sunriset__( jd, lon, lat, -18.0, 0, start, end );
|
119
|
+
// #define astronomical_twilight(year,month,day,lon,lat,start,end) \
|
120
|
+
// __sunriset__( year, month, day, lon, lat, -18.0, 0, start, end )
|
121
|
+
|
122
|
+
/* Function prototypes */
|
123
|
+
|
124
|
+
double __daylen__( double jd, double lon, double lat,
|
125
|
+
double altit, int upper_limb );
|
126
|
+
// double __daylen__( int year, int month, int day, double lon, double lat,
|
127
|
+
//~ double altit, int upper_limb );
|
128
|
+
|
129
|
+
int __sunriset__( double jd, double lon, double lat,
|
130
|
+
double altit, int upper_limb, double *rise, double *set );
|
131
|
+
// int __sunriset__( int year, int month, int day, double lon, double lat,
|
132
|
+
//~ double altit, int upper_limb, double *rise, double *set );
|
133
|
+
|
134
|
+
void sunpos( double d, double *lon, double *r );
|
135
|
+
|
136
|
+
void sun_RA_dec( double d, double *RA, double *dec, double *r );
|
137
|
+
|
138
|
+
double revolution( double x );
|
139
|
+
|
140
|
+
double rev180( double x );
|
141
|
+
|
142
|
+
double GMST0( double d );
|
143
|
+
|
144
|
+
/* midnight 1.1.1970 = JD 2440587.5 */
|
145
|
+
#define EJD (double) 2440587.0
|
146
|
+
/* midnight 1.1.2000 = JD 2451544.5 */
|
147
|
+
#define J2000 (double) 2451545.0
|
148
|
+
|
149
|
+
/* Compute the Julian Day for the given date */
|
150
|
+
/* Julian Date is the number of days since noon of Jan 1 4713 B.C. */
|
151
|
+
|
152
|
+
double CalcJD(int ny, int nm, int nd, double ut)
|
153
|
+
{
|
154
|
+
double A, B, C, D, jd, day;
|
155
|
+
|
156
|
+
day = nd + ut / 24.0;
|
157
|
+
if ((nm == 1) || (nm == 2)) {
|
158
|
+
ny = ny - 1;
|
159
|
+
nm = nm + 12;
|
160
|
+
}
|
161
|
+
|
162
|
+
if (((double) ny + nm / 12.0 + day / 365.25) >=
|
163
|
+
(1582.0 + 10.0 / 12.0 + 15.0 / 365.25))
|
164
|
+
{
|
165
|
+
A = ((int) (ny / 100.0));
|
166
|
+
B = 2.0 - A + (int) (A / 4.0);
|
167
|
+
}
|
168
|
+
else
|
169
|
+
{
|
170
|
+
B = 0.0;
|
171
|
+
}
|
172
|
+
|
173
|
+
if (ny < 0.0)
|
174
|
+
{
|
175
|
+
C = (int) ((365.25 * (double) ny) - 0.75);
|
176
|
+
}
|
177
|
+
else
|
178
|
+
{
|
179
|
+
C = (int) (365.25 * (double) ny);
|
180
|
+
}
|
181
|
+
|
182
|
+
D = (int) (30.6001 * (double) (nm + 1));
|
183
|
+
jd = B + C + D + day + 1720994.5;
|
184
|
+
return (jd);
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
/* Calculate the Julian date from the system clock */
|
189
|
+
|
190
|
+
double JDNow(void)
|
191
|
+
{
|
192
|
+
int year,month,day;
|
193
|
+
int hours,minutes,seconds;
|
194
|
+
double ut,jd;
|
195
|
+
time_t observatory;
|
196
|
+
struct tm * greenwich;
|
197
|
+
|
198
|
+
time(&observatory);
|
199
|
+
greenwich=gmtime(&observatory);
|
200
|
+
|
201
|
+
year = greenwich->tm_year;
|
202
|
+
year = year + 1900;
|
203
|
+
month = greenwich->tm_mon;
|
204
|
+
month = month + 1;
|
205
|
+
day = greenwich->tm_mday;
|
206
|
+
hours = greenwich->tm_hour;
|
207
|
+
minutes = greenwich->tm_min;
|
208
|
+
seconds = greenwich->tm_sec;
|
209
|
+
|
210
|
+
ut = ( (double) seconds )/3600. +
|
211
|
+
( (double) minutes )/60. +
|
212
|
+
( (double) hours );
|
213
|
+
|
214
|
+
jd = CalcJD(year, month, day, ut);
|
215
|
+
|
216
|
+
/* To test for specific jd change this value and uncomment */
|
217
|
+
|
218
|
+
/* jd = 2462088.69; */
|
219
|
+
|
220
|
+
return (jd) ;
|
221
|
+
}
|
222
|
+
|
223
|
+
/* A small test program */
|
224
|
+
|
225
|
+
void main(void)
|
226
|
+
{
|
227
|
+
int year,month,day;
|
228
|
+
double d; /* Days since 2000 Jan 0.0 (negative before) */
|
229
|
+
|
230
|
+
double majd;
|
231
|
+
|
232
|
+
double lon, lat;
|
233
|
+
double daylen, civlen, nautlen, astrlen;
|
234
|
+
double rise, set, civ_start, civ_end, naut_start, naut_end,
|
235
|
+
astr_start, astr_end;
|
236
|
+
int rs, civ, naut, astr;
|
237
|
+
|
238
|
+
lat = 41.9475360;
|
239
|
+
lon = -88.7430640;
|
240
|
+
int epoc_days = (int) time(0) / 86400.0;
|
241
|
+
double jd_today = epoc_days + EJD;
|
242
|
+
double jd_days = epoc_days + EJD - J2000;
|
243
|
+
double jd = epoc_days + EJD - J2000 - lon / 360.0;
|
244
|
+
int iy, im, id;
|
245
|
+
double fd;
|
246
|
+
// iauJd2cal(jd + J2000, 0, &iy, &im, &id, &fd);
|
247
|
+
printf("\n");
|
248
|
+
printf("\tjd now \t\t\t : %7.9f\n", JDNow());
|
249
|
+
|
250
|
+
majd = days_since_2000_Jan_0(year,month,day);
|
251
|
+
/* printf( "MAJD %6.6f \n", majd );
|
252
|
+
printf( "AJD %6.6f \n", majd + 2451545.0 );
|
253
|
+
printf( "JD %6.6f \n", majd + 2451545.0 + 0.5 );*/
|
254
|
+
// printf ("\tDate \t\t\t : %4d/%2.2d/%2.2d\n", iy, im, id );
|
255
|
+
// printf ("\tFD \t\t\t : %f\n", fd );
|
256
|
+
printf("\tEpoch \t\t\t : %llu seconds past midnight 1.1.1970\n", time(0));
|
257
|
+
printf("\tDays \t\t\t : %d days past midnight 1.1.1970\n", epoc_days);
|
258
|
+
printf("\tJD \t\t\t : %f \n", jd_today);
|
259
|
+
printf("\tDays \t\t\t : %f Number of days since 12:00 UTC Jan 1st 2000\n", jd_days);
|
260
|
+
printf("\tDays \t\t\t : %f My Number of days since 12:00 UTC Jan 1st 2000\n", jd);
|
261
|
+
// "is %ld \n", seconds / 86400);
|
262
|
+
|
263
|
+
// printf( "Longitude (+ is east) and latitude (+ is north) : " );
|
264
|
+
// scanf( "%lf %lf", &lon, &lat );
|
265
|
+
|
266
|
+
// for(;;)
|
267
|
+
// {
|
268
|
+
// printf( "Input date ( yyyy mm dd ) (ctrl-C exits): " );
|
269
|
+
// scanf( "%d %d %d", &year, &month, &day );
|
270
|
+
|
271
|
+
// year = 2015;
|
272
|
+
// month = 06;
|
273
|
+
// day = 6;
|
274
|
+
|
275
|
+
// d = days_since_2000_Jan_0(year,month,day) - lon/360.0;
|
276
|
+
// printf( "JD %6.6fh \n", d + 2451545.0 );
|
277
|
+
|
278
|
+
daylen = day_length(jd, lon, lat);
|
279
|
+
civlen = day_civil_twilight_length(jd, lon, lat);
|
280
|
+
nautlen = day_nautical_twilight_length(jd, lon, lat);
|
281
|
+
astrlen = day_astronomical_twilight_length(jd, lon, lat);
|
282
|
+
|
283
|
+
printf( "\tDay length \t\t : %5.2f hours\n", daylen );
|
284
|
+
// printf( "With civil twilight %5.2f hours\n", civlen );
|
285
|
+
// printf( "With nautical twilight %5.2f hours\n", nautlen );
|
286
|
+
// printf( "With astronomical twilight %5.2f hours\n", astrlen );
|
287
|
+
// printf( "Length of twilight: civil %5.2f hours\n",
|
288
|
+
// (civlen-daylen)/2.0);
|
289
|
+
// printf( " nautical %5.2f hours\n",
|
290
|
+
// (nautlen-daylen)/2.0);
|
291
|
+
// printf( " astronomical %5.2f hours\n",
|
292
|
+
// (astrlen-daylen)/2.0);
|
293
|
+
rs = sun_rise_set( jd, lon, lat, &rise, &set );
|
294
|
+
// rs = sun_rise_set( year, month, day, lon, lat, &rise, &set );
|
295
|
+
civ = civil_twilight( jd, lon, lat, &civ_start, &civ_end );
|
296
|
+
// civ = civil_twilight( year, month, day, lon, lat,
|
297
|
+
// &civ_start, &civ_end );
|
298
|
+
naut = nautical_twilight( jd, lon, lat, &naut_start, &naut_end );
|
299
|
+
// naut = nautical_twilight( year, month, day, lon, lat,
|
300
|
+
// &naut_start, &naut_end );
|
301
|
+
astr = astronomical_twilight( jd, lon, lat, &astr_start, &astr_end );
|
302
|
+
// astr = astronomical_twilight( year, month, day, lon, lat,
|
303
|
+
// &astr_start, &astr_end );
|
304
|
+
|
305
|
+
printf( "\tSun at south \t\t : %2.0f:%02.0f UTC\n",
|
306
|
+
floor((rise+set)/2.0),
|
307
|
+
floor(fmod((rise+set)/2.0, 1.0) * 60));
|
308
|
+
|
309
|
+
switch( rs )
|
310
|
+
{
|
311
|
+
case 0:
|
312
|
+
printf( "\tSun rises \t\t : %2.0f:%02.0f UTC\n",
|
313
|
+
floor(rise),
|
314
|
+
floor(fmod(rise, 1.0 ) * 60));
|
315
|
+
printf( "\tSun sets \t\t : %2.0f:%02.0f UTC\n",
|
316
|
+
floor(set),
|
317
|
+
floor(fmod(set, 1.0 ) * 60));
|
318
|
+
break;
|
319
|
+
case +1:
|
320
|
+
printf( "Sun above horizon\n" );
|
321
|
+
break;
|
322
|
+
case -1:
|
323
|
+
printf( "Sun below horizon\n" );
|
324
|
+
break;
|
325
|
+
}
|
326
|
+
|
327
|
+
switch( civ )
|
328
|
+
{
|
329
|
+
case 0:
|
330
|
+
printf( "\tCivil twilight \t\t : starts %2.0f:%02.0f UTC\n"
|
331
|
+
"\t\t\t\t : ends %2.0f:%02.0f UTC\n", civ_start, civ_end );
|
332
|
+
break;
|
333
|
+
case +1:
|
334
|
+
printf( "Never darker than civil twilight\n" );
|
335
|
+
break;
|
336
|
+
case -1:
|
337
|
+
printf( "Never as bright as civil twilight\n" );
|
338
|
+
break;
|
339
|
+
}
|
340
|
+
|
341
|
+
switch( naut )
|
342
|
+
{
|
343
|
+
case 0:
|
344
|
+
printf( "\tNautical twilight \t : starts %2.0f:%02.0f UTC\n"
|
345
|
+
"\t\t\t\t : ends %2.0f:%02.0f UTC\n", naut_start, naut_end );
|
346
|
+
break;
|
347
|
+
case +1:
|
348
|
+
printf( "Never darker than nautical twilight\n" );
|
349
|
+
break;
|
350
|
+
case -1:
|
351
|
+
printf( "Never as bright as nautical twilight\n" );
|
352
|
+
break;
|
353
|
+
}
|
354
|
+
|
355
|
+
switch( astr )
|
356
|
+
{
|
357
|
+
case 0:
|
358
|
+
printf( "\tAstronomical twilight \t : starts %2.0f:%02.0f UTC\n"
|
359
|
+
"\t\t\t\t : ends %2.0f:%02.0f UTC\n", astr_start, astr_end );
|
360
|
+
break;
|
361
|
+
case +1:
|
362
|
+
printf( "Never darker than astronomical twilight\n" );
|
363
|
+
break;
|
364
|
+
case -1:
|
365
|
+
printf( "Never as bright as astronomical twilight\n" );
|
366
|
+
break;
|
367
|
+
}
|
368
|
+
printf("\n");
|
369
|
+
}
|
370
|
+
|
371
|
+
/* The "workhorse" function for sun rise/set times */
|
372
|
+
int __sunriset__( double jd, double lon, double lat,
|
373
|
+
double altit, int upper_limb, double *trise, double *tset )
|
374
|
+
// int __sunriset__( int year, int month, int day, double lon, double lat,
|
375
|
+
// double altit, int upper_limb, double *trise, double *tset )
|
376
|
+
/***************************************************************************/
|
377
|
+
/* Note: year,month,date = calendar date, 1801-2099 only. */
|
378
|
+
/* Eastern longitude positive, Western longitude negative */
|
379
|
+
/* Northern latitude positive, Southern latitude negative */
|
380
|
+
/* The longitude value IS critical in this function! */
|
381
|
+
/* altit = the altitude which the Sun should cross */
|
382
|
+
/* Set to -35/60 degrees for rise/set, -6 degrees */
|
383
|
+
/* for civil, -12 degrees for nautical and -18 */
|
384
|
+
/* degrees for astronomical twilight. */
|
385
|
+
/* upper_limb: non-zero -> upper limb, zero -> center */
|
386
|
+
/* Set to non-zero (e.g. 1) when computing rise/set */
|
387
|
+
/* times, and to zero when computing start/end of */
|
388
|
+
/* twilight. */
|
389
|
+
/* *rise = where to store the rise time */
|
390
|
+
/* *set = where to store the set time */
|
391
|
+
/* Both times are relative to the specified altitude, */
|
392
|
+
/* and thus this function can be used to comupte */
|
393
|
+
/* various twilight times, as well as rise/set times */
|
394
|
+
/* Return value: 0 = sun rises/sets this day, times stored at */
|
395
|
+
/* *trise and *tset. */
|
396
|
+
/* +1 = sun above the specified "horizon" 24 hours. */
|
397
|
+
/* *trise set to time when the sun is at south, */
|
398
|
+
/* minus 12 hours while *tset is set to the south */
|
399
|
+
/* time plus 12 hours. "Day" length = 24 hours */
|
400
|
+
/* -1 = sun is below the specified "horizon" 24 hours */
|
401
|
+
/* "Day" length = 0 hours, *trise and *tset are */
|
402
|
+
/* both set to the time when the sun is at south. */
|
403
|
+
/* */
|
404
|
+
/***************************************************************************/
|
405
|
+
{
|
406
|
+
double d, /* Days since 2000 Jan 0.0 (negative before) */
|
407
|
+
gmsad, /* */
|
408
|
+
majd, /* */
|
409
|
+
sRA, /* Sun's Right Ascension */
|
410
|
+
sdec, /* Sun's declination */
|
411
|
+
sr, /* Solar distance, astronomical units */
|
412
|
+
sradius, /* Sun's apparent radius */
|
413
|
+
t, /* Diurnal arc */
|
414
|
+
tsouth, /* Time when Sun is at south */
|
415
|
+
sidtime; /* Local sidereal time */
|
416
|
+
|
417
|
+
int rc = 0; /* Return cde from function - usually 0 */
|
418
|
+
|
419
|
+
/* Compute d of 12h local mean solar time */
|
420
|
+
d = jd; // + 0.5 - lon / 360.0;
|
421
|
+
// printf( "mean solar transit JD %6.9f \n", d + 2451545.0 );
|
422
|
+
|
423
|
+
/* Compute local sideral time of this moment */
|
424
|
+
gmsad = GMST0(d);
|
425
|
+
// printf( "GMSAD %6.6fh \n", gmsad );
|
426
|
+
sidtime = revolution( gmsad + 180.0 + lon );
|
427
|
+
// printf( "LMSAD %6.6fh \n", sidtime );
|
428
|
+
// printf( "LMST %6.6fh \n", sidtime / 15.0 );
|
429
|
+
|
430
|
+
/* Compute Sun's RA + Decl at this moment */
|
431
|
+
sun_RA_dec( d, &sRA, &sdec, &sr );
|
432
|
+
|
433
|
+
/* Compute time when Sun is at south - in hours UT */
|
434
|
+
tsouth = 12.0 - rev180(sidtime - sRA) / 15.0;
|
435
|
+
|
436
|
+
/* Compute the Sun's apparent radius, degrees */
|
437
|
+
sradius = 0.2666 / sr;
|
438
|
+
|
439
|
+
/* Do correction to upper limb, if necessary */
|
440
|
+
if ( upper_limb )
|
441
|
+
altit -= sradius;
|
442
|
+
|
443
|
+
/* Compute the diurnal arc that the Sun traverses to reach */
|
444
|
+
/* the specified altitide altit: */
|
445
|
+
{
|
446
|
+
double cost;
|
447
|
+
cost = ( sind(altit) - sind(lat) * sind(sdec) ) /
|
448
|
+
( cosd(lat) * cosd(sdec) );
|
449
|
+
if ( cost >= 1.0 )
|
450
|
+
rc = -1, t = 0.0; /* Sun always below altit */
|
451
|
+
else if ( cost <= -1.0 )
|
452
|
+
rc = +1, t = 12.0; /* Sun always above altit */
|
453
|
+
else
|
454
|
+
t = acosd(cost) / 15.0; /* The diurnal arc, hours */
|
455
|
+
}
|
456
|
+
|
457
|
+
/* Store rise and set times - in hours UT */
|
458
|
+
*trise = tsouth - t;
|
459
|
+
*tset = tsouth + t;
|
460
|
+
|
461
|
+
return rc;
|
462
|
+
} /* __sunriset__ */
|
463
|
+
|
464
|
+
|
465
|
+
/* The "workhorse" function */
|
466
|
+
|
467
|
+
// double __daylen__( int year, int month, int day, double lon, double lat,
|
468
|
+
// double altit, int upper_limb )
|
469
|
+
double __daylen__( double jd, double lon, double lat,
|
470
|
+
double altit, int upper_limb )
|
471
|
+
/**********************************************************************/
|
472
|
+
/* Note: year,month,date = calendar date, 1801-2099 only. */
|
473
|
+
/* Eastern longitude positive, Western longitude negative */
|
474
|
+
/* Northern latitude positive, Southern latitude negative */
|
475
|
+
/* The longitude value is not critical. Set it to the correct */
|
476
|
+
/* longitude if you're picky, otherwise set to to, say, 0.0 */
|
477
|
+
/* The latitude however IS critical - be sure to get it correct */
|
478
|
+
/* altit = the altitude which the Sun should cross */
|
479
|
+
/* Set to -35/60 degrees for rise/set, -6 degrees */
|
480
|
+
/* for civil, -12 degrees for nautical and -18 */
|
481
|
+
/* degrees for astronomical twilight. */
|
482
|
+
/* upper_limb: non-zero -> upper limb, zero -> center */
|
483
|
+
/* Set to non-zero (e.g. 1) when computing day length */
|
484
|
+
/* and to zero when computing day+twilight length. */
|
485
|
+
/**********************************************************************/
|
486
|
+
{
|
487
|
+
double d, /* Days since 2000 Jan 0.0 (negative before) */
|
488
|
+
obl_ecl, /* Obliquity (inclination) of Earth's axis */
|
489
|
+
sr, /* Solar distance, astronomical units */
|
490
|
+
slon, /* True solar longitude */
|
491
|
+
sin_sdecl, /* Sine of Sun's declination */
|
492
|
+
cos_sdecl, /* Cosine of Sun's declination */
|
493
|
+
sradius, /* Sun's apparent radius */
|
494
|
+
t; /* Diurnal arc */
|
495
|
+
|
496
|
+
/* Compute d of 12h local mean solar time */
|
497
|
+
// d = days_since_2000_Jan_0(year,month,day) + 0.5 - lon/360.0;
|
498
|
+
/* Compute d of 12h local mean solar time */
|
499
|
+
d = jd; // + 0.5 - lon / 360.0;
|
500
|
+
|
501
|
+
/* Compute obliquity of ecliptic (inclination of Earth's axis) */
|
502
|
+
obl_ecl = 23.4393 - 3.563E-7 * d;
|
503
|
+
|
504
|
+
/* Compute Sun's position */
|
505
|
+
sunpos( d, &slon, &sr );
|
506
|
+
|
507
|
+
/* Compute sine and cosine of Sun's declination */
|
508
|
+
sin_sdecl = sind(obl_ecl) * sind(slon);
|
509
|
+
cos_sdecl = sqrt( 1.0 - sin_sdecl * sin_sdecl );
|
510
|
+
|
511
|
+
/* Compute the Sun's apparent radius, degrees */
|
512
|
+
sradius = 0.2666 / sr;
|
513
|
+
|
514
|
+
/* Do correction to upper limb, if necessary */
|
515
|
+
if ( upper_limb )
|
516
|
+
altit -= sradius;
|
517
|
+
|
518
|
+
/* Compute the diurnal arc that the Sun traverses to reach */
|
519
|
+
/* the specified altitide altit: */
|
520
|
+
{
|
521
|
+
double cost;
|
522
|
+
cost = ( sind(altit) - sind(lat) * sin_sdecl ) /
|
523
|
+
( cosd(lat) * cos_sdecl );
|
524
|
+
if ( cost >= 1.0 )
|
525
|
+
t = 0.0; /* Sun always below altit */
|
526
|
+
else if ( cost <= -1.0 )
|
527
|
+
t = 24.0; /* Sun always above altit */
|
528
|
+
else
|
529
|
+
t = (2.0/15.0) * acosd(cost); /* The diurnal arc, hours */
|
530
|
+
}
|
531
|
+
return t;
|
532
|
+
} /* __daylen__ */
|
533
|
+
|
534
|
+
|
535
|
+
/* This function computes the Sun's position at any instant */
|
536
|
+
|
537
|
+
void sunpos( double d, double *lon, double *r )
|
538
|
+
/******************************************************/
|
539
|
+
/* Computes the Sun's ecliptic longitude and distance */
|
540
|
+
/* at an instant given in d, number of days since */
|
541
|
+
/* 2000 Jan 0.0. The Sun's ecliptic latitude is not */
|
542
|
+
/* computed, since it's always very near 0. */
|
543
|
+
/******************************************************/
|
544
|
+
{
|
545
|
+
double M, /* Mean anomaly of the Sun */
|
546
|
+
w, /* Mean longitude of perihelion */
|
547
|
+
/* Note: Sun's mean longitude = M + w */
|
548
|
+
e, /* Eccentricity of Earth's orbit */
|
549
|
+
E, /* Eccentric anomaly */
|
550
|
+
x, y, /* x, y coordinates in orbit */
|
551
|
+
v; /* True anomaly */
|
552
|
+
|
553
|
+
/* Compute mean elements */
|
554
|
+
// M = fmod( 356.0470 + 0.9856002585 * d, 360.0 );
|
555
|
+
M = fmod( 357.52911 + 0.985600281725 * d +
|
556
|
+
-4.20718412047e-09 * d * d +
|
557
|
+
1.03430001369e-12 * d * d * d, 360.0 );
|
558
|
+
w = 282.9404 + 4.70935E-5 * d;
|
559
|
+
e = 0.016709 - 1.151E-9 * d;
|
560
|
+
|
561
|
+
/* Compute true longitude and radius vector */
|
562
|
+
E = M + e * RADEG * sind(M) * ( 1.0 + e * cosd(M) );
|
563
|
+
x = cosd(E) - e;
|
564
|
+
y = sqrt( 1.0 - e * e ) * sind(E);
|
565
|
+
*r = sqrt( x * x + y * y ); /* Solar distance */
|
566
|
+
v = atan2d( y, x ); /* True anomaly */
|
567
|
+
*lon = fmod(v + w, 360.0); /* True solar longitude */
|
568
|
+
|
569
|
+
// if ( *lon >= 360.0 )
|
570
|
+
// *lon -= 360.0; /* Make it 0..360 degrees */
|
571
|
+
}
|
572
|
+
|
573
|
+
void sun_RA_dec( double d, double *RA, double *dec, double *r )
|
574
|
+
{
|
575
|
+
double lon, obl_ecl, x, y, z;
|
576
|
+
|
577
|
+
/* Compute Sun's ecliptical coordinates */
|
578
|
+
sunpos( d, &lon, r );
|
579
|
+
|
580
|
+
/* Compute ecliptic rectangular coordinates (z=0) */
|
581
|
+
x = *r * cosd(lon);
|
582
|
+
y = *r * sind(lon);
|
583
|
+
|
584
|
+
/* Compute obliquity of ecliptic (inclination of Earth's axis) */
|
585
|
+
obl_ecl = 23.439291 - 3.563E-7 * d;
|
586
|
+
|
587
|
+
/* Convert to equatorial rectangular coordinates - x is uchanged */
|
588
|
+
z = y * sind(obl_ecl);
|
589
|
+
y = y * cosd(obl_ecl);
|
590
|
+
|
591
|
+
/* Convert to spherical coordinates */
|
592
|
+
*RA = atan2d( y, x );
|
593
|
+
*dec = atan2d( z, sqrt(x * x + y * y) );
|
594
|
+
|
595
|
+
} /* sun_RA_dec */
|
596
|
+
|
597
|
+
|
598
|
+
/******************************************************************/
|
599
|
+
/* This function reduces any angle to within the first revolution */
|
600
|
+
/* by subtracting or adding even multiples of 360.0 until the */
|
601
|
+
/* result is >= 0.0 and < 360.0 */
|
602
|
+
/******************************************************************/
|
603
|
+
|
604
|
+
#define INV360 ( 1.0 / 360.0 )
|
605
|
+
|
606
|
+
double revolution( double x )
|
607
|
+
/*****************************************/
|
608
|
+
/* Reduce angle to within 0..360 degrees */
|
609
|
+
/*****************************************/
|
610
|
+
{
|
611
|
+
return( x - 360.0 * floor( x * INV360 ) );
|
612
|
+
} /* revolution */
|
613
|
+
|
614
|
+
double rev180( double x )
|
615
|
+
/*********************************************/
|
616
|
+
/* Reduce angle to within +180..+180 degrees */
|
617
|
+
/*********************************************/
|
618
|
+
{
|
619
|
+
return( x - 360.0 * floor( x * INV360 + 0.5 ) );
|
620
|
+
} /* revolution */
|
621
|
+
|
622
|
+
|
623
|
+
/*********************************************************************/
|
624
|
+
/* This function computes GMST0, the Greenwhich Mean Sidereal Time */
|
625
|
+
/* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at */
|
626
|
+
/* 0h UT). GMST is then the sidereal time at Greenwich at any */
|
627
|
+
/* time of the day. I've generelized GMST0 as well, and define it */
|
628
|
+
/* as: GMST0 = GMST - UT -- this allows GMST0 to be computed at */
|
629
|
+
/* other times than 0h UT as well. While this sounds somewhat */
|
630
|
+
/* contradictory, it is very practical: instead of computing */
|
631
|
+
/* GMST like: */
|
632
|
+
/* */
|
633
|
+
/* GMST = (GMST0) + UT * (366.2422/365.2422) */
|
634
|
+
/* */
|
635
|
+
/* where (GMST0) is the GMST last time UT was 0 hours, one simply */
|
636
|
+
/* computes: */
|
637
|
+
/* */
|
638
|
+
/* GMST = GMST0 + UT */
|
639
|
+
/* */
|
640
|
+
/* where GMST0 is not the GMST "at 0h UT" but at the current moment! */
|
641
|
+
/* Defined in this way, GMST0 will increase with about 4 min a */
|
642
|
+
/* day. It also happens that GMST0 (in degrees, 1 hr = 15 degr) */
|
643
|
+
/* is equal to the Sun's mean longitude plus/minus 180 degrees! */
|
644
|
+
/* (if we neglect aberration, which amounts to 20 seconds of arc */
|
645
|
+
/* or 1.33 seconds of time) */
|
646
|
+
/* */
|
647
|
+
/*********************************************************************/
|
648
|
+
|
649
|
+
double GMST0( double d )
|
650
|
+
{
|
651
|
+
double sidtim0;
|
652
|
+
/* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr */
|
653
|
+
/* L = M + w, as defined in sunpos(). Since I'm too lazy to */
|
654
|
+
/* add these numbers, I'll let the C compiler do it for me. */
|
655
|
+
/* Any decent C compiler will add the constants at compile */
|
656
|
+
/* time, imposing no runtime or code overhead. */
|
657
|
+
sidtim0 = revolution( ( 180.0 + 356.0470 + 282.9404) +
|
658
|
+
( 0.9856002585 + 4.70935E-5 ) * d );
|
659
|
+
sidtim0 = fmod( ( 180.0 + 356.0470 + 282.9404) +
|
660
|
+
( 0.9856002585 + 4.70935E-5 ) * d, 360.0 );
|
661
|
+
sidtim0 = fmod( ( 180 + 357.52911 + 282.9404 ) +
|
662
|
+
( 0.985600281725 + 4.70935E-5 ) * d, 360.0);
|
663
|
+
// sidtim0 = fmod( 280.4664567 +
|
664
|
+
// d * ( 0.9856473601 +
|
665
|
+
// d * ( 8.30124024641e-09 +
|
666
|
+
// d * ( -5.48326848477e-11 +
|
667
|
+
// d * ( -1.78956192374e-10 +
|
668
|
+
// d * ( -11.37718852471e-12 ) ) ) ) ), 360.0 );
|
669
|
+
return sidtim0;
|
670
|
+
} /* GMST0 */
|