swe4r 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/swe4r/swe4r.c ADDED
@@ -0,0 +1,129 @@
1
+ #include "ruby.h"
2
+ #include "swephexp.h"
3
+
4
+ // Module
5
+ VALUE rb_mSwe4r = Qnil;
6
+
7
+ // Set directory path of ephemeris files
8
+ // int swe_set_ephe_path(char *path);
9
+ static VALUE t_swe_set_ephe_path(VALUE self, VALUE path)
10
+ {
11
+ swe_set_ephe_path(StringValuePtr(path));
12
+ return Qnil;
13
+ }
14
+
15
+ // Get the Julian day number from year, month, day, hour
16
+ // double swe_julday(
17
+ // int year,
18
+ // int month,
19
+ // int day,
20
+ // double hour,
21
+ // int gregflag /* Gregorian calendar: 1, Julian calendar: 0 */
22
+ // );
23
+ static VALUE t_swe_julday(VALUE self, VALUE year, VALUE month, VALUE day, VALUE hour)
24
+ {
25
+ double julday = swe_julday( NUM2INT(year), NUM2INT(month), NUM2INT(day), NUM2DBL(hour), SE_GREG_CAL );
26
+ return rb_float_new(julday);
27
+ }
28
+
29
+ // Set the geographic location for topocentric planet computation
30
+ // The longitude and latitude must be in degrees, the altitude in meters.
31
+ // void swe_set_topo (
32
+ // double geolon, /* geographic longitude: eastern longitude is positive, western longitude is negative */
33
+ // double geolat, /* geographic latitude: northern latitude is positive, southern latitude is negative */
34
+ // double altitude /* altitude above sea*/
35
+ // );
36
+ static VALUE t_swe_set_topo(VALUE self, VALUE lon, VALUE lat, VALUE alt) {
37
+ swe_set_topo(NUM2DBL(lon),NUM2DBL(lat),NUM2DBL(alt));
38
+ return Qnil;
39
+ }
40
+
41
+ // Calculation of planets, moon, asteroids, lunar nodes, apogees, fictitious bodies
42
+ // long swe_calc_ut(
43
+ // double tjd_ut, /* Julian day number, Universal Time */
44
+ // int ipl, /* planet number */
45
+ // long iflag, /* flag bits */
46
+ // double *xx, /* target address for 6 position values: longitude, latitude, distance, long.speed, lat.speed, dist. peed */
47
+ // char *serr /* 256 bytes for error string */
48
+ // );
49
+ static VALUE t_swe_calc_ut(VALUE self, VALUE julian_ut, VALUE body, VALUE iflag) {
50
+ double results[6];
51
+ char serr[AS_MAXCH];
52
+ VALUE arr = rb_ary_new();
53
+ int id_push = rb_intern("push");
54
+ int i=0;
55
+
56
+ if ( swe_calc_ut(NUM2DBL(julian_ut), NUM2INT(body), NUM2LONG(iflag), results, serr) < 0 )
57
+ rb_raise (rb_eRuntimeError, serr);
58
+
59
+ for ( i = 0; i < 6; i++)
60
+ rb_funcall(arr, id_push, 1, rb_float_new(results[i]));
61
+
62
+ return arr;
63
+ }
64
+
65
+ // This function can be used to specify the mode for sidereal computations.
66
+ // void swe_set_sid_mode (
67
+ // int32 sid_mode, /* Mode */
68
+ // double t0, /* Reference date */
69
+ // double ayan_t0 /* Initial value of the ayanamsha */
70
+ // );
71
+ static VALUE t_swe_set_sid_mode(VALUE self, VALUE mode, VALUE t0, VALUE ayan_t0) {
72
+ swe_set_sid_mode(NUM2INT(mode), NUM2DBL(t0), NUM2DBL(ayan_t0));
73
+ return Qnil;
74
+ }
75
+
76
+ // This function computes the ayanamsha, the distance of the tropical vernal point from the sidereal zero point of the zodiac.
77
+ // The ayanamsha is used to compute sidereal planetary positions from tropical ones:
78
+ // pos_sid = pos_trop – ayanamsha
79
+ // Before calling swe_get_ayanamsha(), you have to set the sidereal mode with swe_set_sid_mode, unless you want the default sidereal mode, which is the Fagan/Bradley ayanamsha.
80
+ // double swe_get_ayanamsa_ut(double tjd_ut);
81
+ static VALUE t_swe_get_ayanamsa_ut(VALUE self, VALUE julian_ut) {
82
+ double ayanamsa = swe_get_ayanamsa_ut(NUM2DBL(julian_ut));
83
+ return rb_float_new(ayanamsa);
84
+ }
85
+
86
+ // This function computes house cusps, ascendant, midheaven, etc
87
+ // int swe_houses(
88
+ // double tjd_ut, /* Julian day number, UT */
89
+ // double geolat, /* geographic latitude, in degrees */
90
+ // double geolon, /* geographic longitude, in degrees
91
+ // * eastern longitude is positive,
92
+ // * western longitude is negative,
93
+ // * northern latitude is positive,
94
+ // * southern latitude is negative */
95
+ // int hsys, /* house method, ascii code of one of the letters PKORCAEVXHTBG */
96
+ // double *cusps, /* array for 13 doubles */
97
+ // double *ascmc); /* array for 10 doubles */
98
+ static VALUE t_swe_houses(VALUE self, VALUE julian_day, VALUE latitude, VALUE longitude, VALUE house_system)
99
+ {
100
+ double cusps[13];
101
+ double ascmc[10];
102
+ char serr[AS_MAXCH];
103
+ VALUE arr = rb_ary_new();
104
+ int id_push = rb_intern("push");
105
+ int i =0;
106
+
107
+ if ( swe_houses(NUM2DBL(julian_day), NUM2DBL(latitude), NUM2DBL(longitude), NUM2CHR(house_system), cusps, ascmc) < 0 )
108
+ rb_raise (rb_eRuntimeError, serr);
109
+
110
+ for ( i = 0; i < 13; i++)
111
+ rb_funcall(arr, id_push, 1, rb_float_new(cusps[i]));
112
+
113
+ for ( i = 0; i < 10; i++)
114
+ rb_funcall(arr, id_push, 1, rb_float_new(ascmc[i]));
115
+
116
+ return arr;
117
+ }
118
+
119
+ void Init_swe4r()
120
+ {
121
+ rb_mSwe4r = rb_define_module ("Swe4r");
122
+ rb_define_method(rb_mSwe4r, "swe_set_ephe_path", t_swe_set_ephe_path, 1);
123
+ rb_define_method(rb_mSwe4r, "swe_julday", t_swe_julday, 4);
124
+ rb_define_method(rb_mSwe4r, "swe_set_topo", t_swe_set_topo, 3);
125
+ rb_define_method(rb_mSwe4r, "swe_calc_ut", t_swe_calc_ut, 3);
126
+ rb_define_method(rb_mSwe4r, "swe_set_sid_mode", t_swe_set_sid_mode, 3);
127
+ rb_define_method(rb_mSwe4r, "swe_get_ayanamsa_ut", t_swe_get_ayanamsa_ut, 1);
128
+ rb_define_method(rb_mSwe4r, "swe_houses", t_swe_houses, 4);
129
+ }