Update action steps
[slib.git] / sbl.pyx
blob1b9950df05c5f1cd391761f2ef560c0bd22ffb8c
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":
12 cdef struct tm:
13 int tm_sec
14 int tm_min
15 int tm_hour
16 int tm_mday
17 int tm_mon
18 int tm_year
19 int tm_wday
20 int tm_yday
21 int tm_isdst
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":
40 long fsize(FILE *)
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
48 cdef int SBLLIB_MINOR
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):
75 cdef long count
76 cdef long *lst
77 cdef FILE *fp = fopen(file, "rb")
78 lst = slib_count_fl(fp, &count)
79 return (lst, 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):
85 return struct_time((
86 ctm.tm_year,
87 ctm.tm_mon + 1,
88 ctm.tm_mday,
89 ctm.tm_hour,
90 ctm.tm_min,
91 ctm.tm_sec,
92 ctm.tm_wday,
93 ctm.tm_yday,
94 ctm.tm_isdst))
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
107 def rad2deg(rad):
108 return slib_rad2deg(rad)
110 def deg2rad(deg):
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):
117 cdef tm ctm
118 cdef double solartrans
119 cdef double hour_angle
120 pytm2ctm(pytm, &ctm)
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)
157 def filesize(file):
158 return fsize(fopen(_unify_o_f(file), "rb"))
160 def splitpath(path):
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)
167 odrv = drive
168 odir = dirname
169 obase = basename
170 oext = ext
171 free(drive)
172 free(dirname)
173 free(basename)
174 free(ext)
175 return (odrv.decode(), odir.decode(), obase.decode(), oext.decode())
177 def count_fl(file):
178 pylst = []
179 clst, count = _wrap_count_fl(_unify_o_f(file))
180 for i in range(0, count):
181 pylst.append(clst[i])
182 free(clst)
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)
194 def prtpn(min, max):
195 return slib_prtpn(min, max)
197 def ispn(n):
198 return True if slib_ispn(n) == 1 else False
200 def isrp(n1, n2):
201 return True if slib_isrp(n1, n2) == 1 else False
203 def gcf(n1, n2):
204 return slib_gcf(n1, n1)
206 def lcm(n1, n2):
207 return slib_lcm(n1, n1)
209 def eular(n):
210 return slib_eular(n)
212 def factorial(n):
213 return slib_factorial(n)
215 def combi(n, r):
216 return slib_combi(n, r)
218 def permu(n, r):
219 return slib_permu(n, r)
221 def h2hms(hour):
222 cdef double h, m, s
223 slib_h2hms(hour, &h, &m, &s)
224 return (h, m, s)
226 def hms2h(h, m, s):
227 return slib_hms2h(h, m, s)
229 def d2dn(y, m, d):
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)
238 def tm2jd(pytm):
239 cdef tm ctm
240 pytm2ctm(pytm, &ctm)
241 return slib_tm2jd(&ctm)
243 def jd2tm(jd):
244 cdef tm ctm
245 slib_jd2tm(jd, &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__})")