1 from libc
.stdio cimport fopen
, fclose
, FILE
2 from libc
.stdlib cimport malloc
, free
3 from libc
.string cimport strcmp
4 from libc
.time cimport tm
5 from cython
.operator cimport dereference
as deref
6 from time
import struct_time
7 import pkg_resources
, sys
9 __version__
= pkg_resources
.require
("sbl")[0].version
11 cdef extern from "time.h":
23 cdef extern from "slib/astro.h":
24 double slib_rad2deg
(double
);
25 double slib_deg2rad
(double
);
26 double slib_sun_decl_by_date
(int);
27 double slib_sf_csha
(double
, double
, double
, double
*, tm
*);
28 void slib_sf_sunrise
(double
, double
, double
, double
, tm
*, tm
*, tm
*);
29 double slib_julian_day
(double
);
30 double slib_mean_solar_noon
(double
, double
);
31 double slib_solar_mean_anomaly
(double
);
32 double slib_equation_of_the_center_value
(double
);
33 double slib_ecliptic_longitude
(double
, double
);
34 double slib_solar_transit
(double
, double
, double
);
35 double slib_sun_decl
(double
);
36 double slib_sunrise_hour_angle
(double
, double
);
37 double slib_corr_sunrise_hour_angle
(double
, double
, double
);
39 cdef extern from "slib/fileopt.h":
41 void splitpathS
(const
char *, char *, char *, char *, char *)
42 long *slib_count_fl
(FILE
*, long *);
43 long slib_fbsearch
(char *, FILE
*, int (*)(char *, char *));
44 void slib_fqsort
(FILE
*, FILE
*, int (*)(char *, char *));
46 cdef extern from "slib/general.h":
47 cdef int SBLLIB_VERSION
49 cdef int SBLLIB_PATCHLEVEL
51 cdef extern from "slib/math.h":
52 void slib_prtpn
(unsigned
long, unsigned
long)
53 int slib_ispn
(unsigned
long)
54 int slib_isrp
(unsigned
, unsigned
)
55 double slib_average
(unsigned
int, ...)
56 unsigned
long slib_gcf
(unsigned
long, unsigned
long)
57 unsigned
long slib_lcm
(unsigned
long, unsigned
long)
58 int slib_eular
(unsigned
)
59 unsigned
long slib_factorial
(unsigned
long)
60 unsigned
long slib_combi
(unsigned
long, unsigned
long)
61 unsigned
long slib_permu
(unsigned
long, unsigned
long)
63 cdef extern from "slib/timedate.h":
64 void slib_h2hms
(double
, double
*, double
*, double
*);
65 double slib_hms2h
(double
, double
, double
);
66 int slib_d2dn
(int, int, int);
67 double slib_true_time_diff
(double
);
68 double slib_local_time
(double
, double
, double
);
69 double slib_tm2jd
(tm
*);
70 void slib_jd2tm
(double
, tm
*);
72 ctypedef int (*compar
)(const
char *, const
char *)
74 cdef (long *, long) _wrap_count_fl
(char *file
):
77 cdef FILE
*fp
= fopen
(file
, "rb")
78 lst
= slib_count_fl
(fp
, &count
)
81 cdef _unify_o_f
(file
):
82 return file
.encode
() if isinstance(file
, basestring
) else file
.name
.encode
()
84 cdef (object) ctm2pytm
(tm
*ctm
):
96 cdef (void) pytm2ctm
(object pytm
, tm
*ctm
):
97 ctm
.tm_year
= pytm
.tm_year
98 ctm
.tm_mon
= pytm
.tm_mon
- 1
99 ctm
.tm_mday
= pytm
.tm_mday
100 ctm
.tm_hour
= pytm
.tm_hour
101 ctm
.tm_min
= pytm
.tm_min
102 ctm
.tm_sec
= pytm
.tm_sec
103 ctm
.tm_wday
= pytm
.tm_wday
104 ctm
.tm_yday
= pytm
.tm_yday
105 ctm
.tm_isdst
= pytm
.tm_isdst
108 return slib_rad2deg
(rad
)
111 return slib_deg2rad
(deg
)
113 def sun_decl_by_date
(dn
):
114 return slib_sun_decl_by_date
(dn
)
116 def sf_csha
(lat
, lon
, elev
, pytm
):
118 cdef double solartrans
119 cdef double hour_angle
121 hour_angle
= slib_sf_csha
(lat
, lon
, elev
, &solartrans
, &ctm
)
122 return (hour_angle
, solartrans
)
124 def sf_sunrise
(lat
, lon
, elev
, tz
, utcnow
):
125 cdef tm sunrise
, sunset
, now
126 pytm2ctm
(utcnow
, &now
)
127 slib_sf_sunrise
(lat
, lon
, elev
, tz
, &now
, &sunrise
, &sunset
)
128 return (ctm2pytm
(&sunrise
), ctm2pytm
(&sunset
))
130 def julian_day
(jdate
):
131 return slib_julian_day
(jdate
)
133 def mean_solar_noon
(jday
, lon
):
134 return slib_mean_solar_noon
(jday
, lon
)
136 def solar_mean_anomaly
(mean_snoon
):
137 return slib_solar_mean_anomaly
(mean_snoon
)
139 def equation_of_the_center_value
(smean_anomaly
):
140 return slib_equation_of_the_center_value
(smean_anomaly
)
142 def ecliptic_longitude
(smean_anomaly
, eoc_value
):
143 return slib_ecliptic_longitude
(smean_anomaly
, eoc_value
)
145 def solar_transit
(mean_snoon
, smean_anomaly
, ecl_lon
):
146 return slib_solar_transit
(mean_snoon
, smean_anomaly
, ecl_lon
)
148 def sun_decl
(ecl_lon
):
149 return slib_sun_decl
(ecl_lon
)
151 def sunrise_hour_angle
(lat
, sun_declination
):
152 return slib_sunrise_hour_angle
(lat
, sun_declination
)
154 def corr_sunrise_hour_angle
(lat
, sun_declination
, elev
):
155 return slib_corr_sunrise_hour_angle
(lat
, sun_declination
, elev
)
158 return fsize
(fopen
(_unify_o_f
(file
), "rb"))
161 cdef char *drive
= <char *> malloc
(3)
162 cdef char *dirname
= <char *> malloc
(len(path
))
163 cdef char *basename
= <char *> malloc
(len(path
))
164 cdef char *ext
= <char *> malloc
(len(path
))
165 cdef bytes odrv
, odir
, obase
, oext
166 splitpathS
(path
.encode
(), drive
, dirname
, basename
, ext
)
175 return (odrv
.decode
(), odir
.decode
(), obase
.decode
(), oext
.decode
())
179 clst
, count
= _wrap_count_fl
(_unify_o_f
(file
))
180 for i
in range(0, count
):
181 pylst
.append
(clst
[i
])
183 return (pylst
, count
)
185 def fbsearch
(key
, file
):
186 cdef FILE
*fp
= fopen
(_unify_o_f
(file
), "rb")
187 return slib_fbsearch
(key
.encode
(), fp
, <compar
>strcmp
)
189 def fqsort
(fin
, fout
):
190 cdef FILE
*fpin
= fopen
(_unify_o_f
(fin
), "rb")
191 cdef FILE
*fpout
= fopen
(_unify_o_f
(fout
), "rb")
192 return slib_fqsort
(fpin
, fpout
, <compar
>strcmp
)
195 return slib_prtpn
(min, max)
198 return True
if slib_ispn
(n
) == 1 else False
201 return True
if slib_isrp
(n1
, n2
) == 1 else False
204 return slib_gcf
(n1
, n1
)
207 return slib_lcm
(n1
, n1
)
213 return slib_factorial
(n
)
216 return slib_combi
(n
, r
)
219 return slib_permu
(n
, r
)
223 slib_h2hms
(hour
, &h
, &m
, &s
)
227 return slib_hms2h
(h
, m
, s
)
230 return slib_d2dn
(y
, m
, d
)
232 def true_time_diff
(lon
):
233 return slib_true_time_diff
(lon
)
235 def local_time
(lon
, hours
, tz
):
236 return slib_local_time
(lon
, hours
, tz
)
241 return slib_tm2jd
(&ctm
)
246 return ctm2pytm
(&ctm
)
248 c_version
= f
"{SBLLIB_VERSION}.{SBLLIB_MINOR}.{SBLLIB_PATCHLEVEL}"
249 if c_version
!= __version__
:
250 raise ImportError(f
"Version mismatch between C library({c_version}) and Python module({__version__})")