patch.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /*
  14. * dec varies from -89 to 89, inclusive.
  15. * ra varies depending on dec; each patch is about 1 square degree.
  16. *
  17. * Northern hemisphere (0<=dec<=89):
  18. * from 0<=dec<=59, ra is every 4m, 360 values
  19. * from 60<=dec<=69, ra is every 8m, 180 values
  20. * from 70<=dec<=79, ra is every 12m, 120 values
  21. * from 80<=dec<=84, ra is every 24m, 60 values
  22. * at dec=85 and 86, ra is every 48m, 30 values
  23. * at dec=87, ra is every 60m, 24 values
  24. * at dec=88, ra is every 120m, 12 values
  25. * at dec=89, ra is 12h, 1 value
  26. *
  27. * Total number of patches in northern hemisphere is therefore:
  28. * 360*60+180*10+120*10+60*5+30*2+24*1+12*1+1 = 24997
  29. * Total number of patches is therefore
  30. * 2*24997-360 = 49634 (dec=0 has been counted twice)
  31. * (cf. 41253 square degrees in the sky)
  32. */
  33. void
  34. radec(int p, int *rah, int *ram, int *deg)
  35. {
  36. *deg = (p&255)-90;
  37. p >>= 8;
  38. *rah = p/15;
  39. *ram = (p%15)*4;
  40. if(*deg<0)
  41. (*deg)++;
  42. }
  43. int32_t
  44. patcha(Angle ra, Angle dec)
  45. {
  46. ra = DEG(ra);
  47. dec = DEG(dec);
  48. if(dec >= 0)
  49. return patch(floor(ra/15), ((int)floor(ra*4))%60, floor(dec));
  50. dec = -dec;
  51. return patch(floor(ra/15), ((int)floor(ra*4))%60, -floor(dec));
  52. }
  53. char round[91]={ /* extra 0 is to offset the array */
  54. /* 0 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. /* 10 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. /* 20 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  57. /* 30 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  58. /* 40 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  59. /* 50 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  60. /* 60 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. /* 70 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  62. /* 80 */ 6, 6, 6, 6, 6, 12, 12, 15, 30, -1,
  63. /* 90 */
  64. };
  65. int32_t
  66. patch(int rah, int ram, int deg)
  67. {
  68. int ra, dec;
  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. }