1 #include <u.h>
2 #include <libc.h>
3 #include	<bio.h>
4 #include "sky.h"
5 
6 /*
7  * dec varies from -89 to 89, inclusive.
8  * ra varies depending on dec; each patch is about 1 square degree.
9  *
10  * Northern hemisphere (0<=dec<=89):
11  *	from  0<=dec<=59, ra is every 4m,  360 values
12  *	from 60<=dec<=69, ra is every 8m,  180 values
13  *	from 70<=dec<=79, ra is every 12m, 120 values
14  *	from 80<=dec<=84, ra is every 24m,  60 values
15  *	at dec=85 and 86, ra is every 48m,  30 values
16  *	at dec=87,        ra is every 60m,  24 values
17  *	at dec=88,        ra is every 120m, 12 values
18  *	at dec=89,        ra is 12h,	     1 value
19  *
20  * Total number of patches in northern hemisphere is therefore:
21  * 	360*60+180*10+120*10+60*5+30*2+24*1+12*1+1 = 24997
22  * Total number of patches is therefore
23  *	2*24997-360 = 49634	(dec=0 has been counted twice)
24  * (cf. 41253 square degrees in the sky)
25  */
26 
27 void
radec(int p,int * rah,int * ram,int * deg)28 radec(int p, int *rah, int *ram, int *deg)
29 {
30 	*deg = (p&255)-90;
31 	p >>= 8;
32 	*rah = p/15;
33 	*ram = (p%15)*4;
34 	if(*deg<0)
35 		(*deg)++;
36 }
37 
38 int32
patcha(Angle ra,Angle dec)39 patcha(Angle ra, Angle dec)
40 {
41 	ra = DEG(ra);
42 	dec = DEG(dec);
43 	if(dec >= 0)
44 		return patch(floor(ra/15), ((int)floor(ra*4))%60, floor(dec));
45 	dec = -dec;
46 	return patch(floor(ra/15), ((int)floor(ra*4))%60, -floor(dec));
47 }
48 
49 #define round scatround
50 
51 char round[91]={	/* extra 0 is to offset the array */
52 	/*  0 */    0,	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
53 	/* 10 */	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
54 	/* 20 */	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
55 	/* 30 */	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
56 	/* 40 */	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
57 	/* 50 */	 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
58 	/* 60 */	 2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
59 	/* 70 */	 3,  3,  3,  3,  3,  3,  3,  3,  3,  3,
60 	/* 80 */	 6,  6,  6,  6,  6, 12, 12, 15, 30, -1,
61 	/* 90 */
62 };
63 
64 int32
patch(int rah,int ram,int deg)65 patch(int rah, int ram, int deg)
66 {
67 	int ra, dec;
68 
69 	/*
70 	 * patches go from lower limit <= position < upper limit.
71 	 * therefore dec ' " can be ignored; always inc. dec degrees.
72 	 * the computed angle is then the upper limit (ignoring sign).
73 	 * when done, +ve values are shifted down so 90 (0 degrees) is a value;
74 	 */
75 	if(rah<0 || rah>=24 || ram<0 || abs(deg)>=90){
76 		fprint(2, "scat: patch: bad ra or dec %dh%dm %d\n", rah, ram, deg);
77 		abort();
78 	}
79 	if(deg < 0)
80 		deg--;
81 	else if(deg < 90)
82 		deg++;
83 	dec = deg+90;
84 	deg = abs(deg);
85 	if(deg<1 || deg>90){
86 		fprint(2, "scat: patch: panic %dh%dm %d\n", rah, ram, deg);
87 		abort();
88 	}
89 	if(deg == 90)
90 		ra = 180;
91 	else{
92 		ra = 15*rah+ram/4;
93 		ra -= ra%round[deg];
94 	}
95 	/* close the hole at 0 */
96 	if(dec > 90)
97 		--dec;
98 	if(ra >= 360)
99 		ra -= 360;
100 	return (ra<<8)|dec;
101 }
102