patch.c 2.5 KB

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