util.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "sky.h"
  13. double PI_180 = 0.0174532925199432957692369;
  14. double TWOPI = 6.2831853071795864769252867665590057683943387987502;
  15. double LN2 = 0.69314718055994530941723212145817656807550013436025;
  16. static double angledangle=(180./PI)*MILLIARCSEC;
  17. int
  18. rint(char *p, int n)
  19. {
  20. int i=0;
  21. while(*p==' ' && n)
  22. p++, --n;
  23. while(n--)
  24. i=i*10+*p++-'0';
  25. return i;
  26. }
  27. DAngle
  28. dangle(Angle angle)
  29. {
  30. return angle*angledangle;
  31. }
  32. Angle
  33. angle(DAngle dangle)
  34. {
  35. return dangle/angledangle;
  36. }
  37. double
  38. rfloat(char *p, int n)
  39. {
  40. double i, d=0;
  41. while(*p==' ' && n)
  42. p++, --n;
  43. if(*p == '+')
  44. return rfloat(p+1, n-1);
  45. if(*p == '-')
  46. return -rfloat(p+1, n-1);
  47. while(*p == ' ' && n)
  48. p++, --n;
  49. if(n == 0)
  50. return 0.0;
  51. while(n-- && *p!='.')
  52. d = d*10+*p++-'0';
  53. if(n <= 0)
  54. return d;
  55. p++;
  56. i = 1;
  57. while(n--)
  58. d+=(*p++-'0')/(i*=10.);
  59. return d;
  60. }
  61. int
  62. sign(int c)
  63. {
  64. if(c=='-')
  65. return -1;
  66. return 1;
  67. }
  68. char*
  69. hms(Angle a)
  70. {
  71. static char buf[20];
  72. double x;
  73. int h, m, s, ts;
  74. x=DEG(a)/15;
  75. x += 0.5/36000.; /* round up half of 0.1 sec */
  76. h = floor(x);
  77. x -= h;
  78. x *= 60;
  79. m = floor(x);
  80. x -= m;
  81. x *= 60;
  82. s = floor(x);
  83. x -= s;
  84. ts = 10*x;
  85. sprint(buf, "%dh%.2dm%.2d.%ds", h, m, s, ts);
  86. return buf;
  87. }
  88. char*
  89. dms(Angle a)
  90. {
  91. static char buf[20];
  92. double x;
  93. int sign, d, m, s, ts;
  94. x = DEG(a);
  95. sign='+';
  96. if(a<0){
  97. sign='-';
  98. x=-x;
  99. }
  100. x += 0.5/36000.; /* round up half of 0.1 arcsecond */
  101. d = floor(x);
  102. x -= d;
  103. x *= 60;
  104. m = floor(x);
  105. x -= m;
  106. x *= 60;
  107. s = floor(x);
  108. x -= s;
  109. ts = floor(10*x);
  110. sprint(buf, "%c%d°%.2d'%.2d.%d\"", sign, d, m, s, ts);
  111. return buf;
  112. }
  113. char*
  114. ms(Angle a)
  115. {
  116. static char buf[20];
  117. double x;
  118. int d, m, s, ts;
  119. x = DEG(a);
  120. x += 0.5/36000.; /* round up half of 0.1 arcsecond */
  121. d = floor(x);
  122. x -= d;
  123. x *= 60;
  124. m = floor(x);
  125. x -= m;
  126. x *= 60;
  127. s = floor(x);
  128. x -= s;
  129. ts = floor(10*x);
  130. if(d != 0)
  131. sprint(buf, "%d°%.2d'%.2d.%d\"", d, m, s, ts);
  132. else
  133. sprint(buf, "%.2d'%.2d.%d\"", m, s, ts);
  134. return buf;
  135. }
  136. char*
  137. hm(Angle a)
  138. {
  139. static char buf[20];
  140. double x;
  141. int h, m, n;
  142. x = DEG(a)/15;
  143. x += 0.5/600.; /* round up half of tenth of minute */
  144. h = floor(x);
  145. x -= h;
  146. x *= 60;
  147. m = floor(x);
  148. x -= m;
  149. x *= 10;
  150. n = floor(x);
  151. sprint(buf, "%dh%.2d.%1dm", h, m, n);
  152. return buf;
  153. }
  154. char*
  155. hm5(Angle a)
  156. {
  157. static char buf[20];
  158. double x;
  159. int h, m;
  160. x = DEG(a)/15;
  161. x += 2.5/60.; /* round up 2.5m */
  162. h = floor(x);
  163. x -= h;
  164. x *= 60;
  165. m = floor(x);
  166. m -= m % 5;
  167. sprint(buf, "%dh%.2dm", h, m);
  168. return buf;
  169. }
  170. char*
  171. dm(Angle a)
  172. {
  173. static char buf[20];
  174. double x;
  175. int sign, d, m, n;
  176. x = DEG(a);
  177. sign='+';
  178. if(a<0){
  179. sign='-';
  180. x=-x;
  181. }
  182. x += 0.5/600.; /* round up half of tenth of arcminute */
  183. d = floor(x);
  184. x -= d;
  185. x *= 60;
  186. m = floor(x);
  187. x -= m;
  188. x *= 10;
  189. n = floor(x);
  190. sprint(buf, "%c%d°%.2d.%.1d'", sign, d, m, n);
  191. return buf;
  192. }
  193. char*
  194. deg(Angle a)
  195. {
  196. static char buf[20];
  197. double x;
  198. int sign, d;
  199. x = DEG(a);
  200. sign='+';
  201. if(a<0){
  202. sign='-';
  203. x=-x;
  204. }
  205. x += 0.5; /* round up half degree */
  206. d = floor(x);
  207. sprint(buf, "%c%d°", sign, d);
  208. return buf;
  209. }
  210. char*
  211. getword(char *ou, char *in)
  212. {
  213. int c;
  214. for(;;) {
  215. c = *in++;
  216. if(c == ' ' || c == '\t')
  217. continue;
  218. if(c == 0)
  219. return 0;
  220. break;
  221. }
  222. if(c == '\'')
  223. for(;;) {
  224. if(c >= 'A' && c <= 'Z')
  225. c += 'a' - 'A';
  226. *ou++ = c;
  227. c = *in++;
  228. if(c == 0)
  229. return 0;
  230. if(c == '\'') {
  231. *ou = 0;
  232. return in-1;
  233. }
  234. }
  235. for(;;) {
  236. if(c >= 'A' && c <= 'Z')
  237. c += 'a' - 'A';
  238. *ou++ = c;
  239. c = *in++;
  240. if(c == ' ' || c == '\t' || c == 0) {
  241. *ou = 0;
  242. return in-1;
  243. }
  244. }
  245. }
  246. /*
  247. * Read formatted angle. Must contain no embedded blanks
  248. */
  249. Angle
  250. getra(char *p)
  251. {
  252. Rune r;
  253. char *q;
  254. Angle f, d;
  255. int neg;
  256. neg = 0;
  257. d = 0;
  258. while(*p == ' ')
  259. p++;
  260. for(;;) {
  261. if(*p == ' ' || *p=='\0')
  262. goto Return;
  263. if(*p == '-') {
  264. neg = 1;
  265. p++;
  266. }
  267. if(*p == '+') {
  268. neg = 0;
  269. p++;
  270. }
  271. q = p;
  272. f = strtod(p, &q);
  273. if(q > p) {
  274. p = q;
  275. }
  276. p += chartorune(&r, p);
  277. switch(r) {
  278. default:
  279. Return:
  280. if(neg)
  281. d = -d;
  282. return RAD(d);
  283. case 'h':
  284. d += f*15;
  285. break;
  286. case 'm':
  287. d += f/4;
  288. break;
  289. case 's':
  290. d += f/240;
  291. break;
  292. case L'°':
  293. d += f;
  294. break;
  295. case '\'':
  296. d += f/60;
  297. break;
  298. case '\"':
  299. d += f/3600;
  300. break;
  301. }
  302. }
  303. }
  304. double
  305. xsqrt(double a)
  306. {
  307. if(a < 0)
  308. return 0;
  309. return sqrt(a);
  310. }
  311. Angle
  312. dist(Angle ra1, Angle dec1, Angle ra2, Angle dec2)
  313. {
  314. double a;
  315. a = sin(dec1) * sin(dec2) +
  316. cos(dec1) * cos(dec2) *
  317. cos(ra1 - ra2);
  318. a = atan2(xsqrt(1 - a*a), a);
  319. if(a < 0)
  320. a = -a;
  321. return a;
  322. }
  323. int
  324. dogamma(Pix c)
  325. {
  326. float f;
  327. f = c - gam.min;
  328. if(f < 1)
  329. f = 1;
  330. if(gam.absgamma == 1)
  331. c = f * gam.mult2;
  332. else
  333. c = exp(log(f*gam.mult1) * gam.absgamma) * 255;
  334. if(c > 255)
  335. c = 255;
  336. if(gam.neg)
  337. c = 255-c;
  338. return c;
  339. }