home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3 / hamradioversion3.0examsandprograms1992.iso / math / sun / sun.bas next >
BASIC Source File  |  1986-10-29  |  11KB  |  347 lines

  1. 10 ' Program to calculate sunrise and sunset times by N4TL
  2. 20 ' file name = sun.bas
  3. 30 '
  4. 40 ' Algorithm from June 84 QST page 56
  5. 50 ' Calculation section by WS4G and SM0CCM
  6. 60 ' If any errors are found please send the information to N4TL
  7. 70 '
  8. 80 ' This program will compile using the IBM BASIC compiler ver 1.00
  9. 90 '
  10. 100 '  LATITUDE : N = + , S = -  Degrees
  11. 110 ' LONGITUDE : W = + , E = -  Degrees
  12. 120 '
  13. 130 ' Arrays used
  14. 140 '
  15. 150 ' P$  Prefix and Country name
  16. 160 ' LA$ Latitude
  17. 170 ' LO$ Longitude
  18. 180 ' SR$ Sunrise time
  19. 190 ' SS$ Sunset  time
  20. 200 ' LD  length of day
  21. 210 ' PA  A index for sort routine
  22. 220 ' PB  B index for sort routine
  23. 230 '
  24. 240 DIM P$(500),LA(500),LO(500),SR$(500),SS$(500),LD$(500),PA(500),PB(500)
  25. 250 KEY OFF : COLOR 7,1  ' turn color off for the portable pc
  26. 260 DEF FNACS(XX) = 1.570796 - ATN(XX/SQR(1-XX*XX))
  27. 270 '
  28. 280 N=500  ' Maximum number of countries
  29. 290 '
  30. 300 VERSION  = 1.8
  31. 310 DATEV$ = "    Oct 29, 1986     "
  32. 320 '
  33. 330 CLS   ' restart entry point
  34. 340 '
  35. 350 PRINT
  36. 360 PRINT "  Sunrise and Sunset Program. Useful for radio communications."
  37. 370 PRINT "  Version "VERSION  DATEV$" by N4TL."
  38. 380 GOTO 670 ' first menu
  39. 390 '
  40. 400 ' start of information
  41. 410 '
  42. 420 PRINT
  43. 430 PRINT"  This program will calculate the sunrise, sunset, and length
  44. 440 PRINT"  of the day for all the countries listed in the SUN.DAT
  45. 450 PRINT"  file, or for a single location. the SUN.DAT file has
  46. 460 PRINT"  to be on the same disk as the default disk.
  47. 470 PRINT"  Times are in hours and minutes UTC.
  48. 480 PRINT"
  49. 490 PRINT"  When the file input is used the times are calculated and
  50. 500 PRINT"  displayed on the screen. The times are saved in the program,
  51. 510 PRINT"  and can be sorted and printed.
  52. 520 PRINT"  Compressed print can be used (for the IBM Graphics Printer).
  53. 530 PRINT
  54. 540 PRINT"  If the SUN.DAT file is edited its format has to remain
  55. 550 PRINT"  unchanged. It also has to be saved without compressed
  56. 560 PRINT"  characters. If the Personal editor is used, use the NOTABS
  57. 570 PRINT"  option when saving the file. The maximum number of countries
  58. 580 PRINT"  is 500.
  59. 590 PRINT
  60. 600 PRINT "  more ";:INPUT IN
  61. 610 GOTO 330
  62. 620 ' end of information
  63. 630 '
  64. 640 '
  65. 650 ' what does the user want to do?    first menu
  66. 660 '
  67. 670 PRINT
  68. 680 PRINT "  Enter 1 for a single calculation  "
  69. 690 PRINT "  Enter 2 for the full file         "
  70. 700 PRINT "  Enter 3 for information           "
  71. 710 PRINT "  Enter 4 to exit the program    ";: INPUT IN
  72. 720 '
  73. 730 IF IN=1 GOTO 790
  74. 740 IF IN=2 GOTO 1290
  75. 750 IF IN=3 GOTO 420
  76. 760 IF IN=4 THEN END
  77. 770 GOTO 670
  78. 780 '
  79. 790 GOSUB 2000  ' get month and day
  80. 800 GOSUB 2110  ' sun declination
  81. 810 '
  82. 820 X=1
  83. 830 PRINT
  84. 840 PRINT "  N = +, S = -"
  85. 850 PRINT "  enter latitude -90 to +90 degrees ";: INPUT LA(X)
  86. 860 IF LA(X) > 90 OR LA(X) < -90 THEN GOTO 850
  87. 870 PRINT "  W = +, E = -"
  88. 880 PRINT "  enter longitude -180 to +180 degrees ";: INPUT LO(X)
  89. 890 IF LO(X) >180 OR LO(X) < -180 THEN GOTO 880
  90. 900 PRINT "  Calculate sunrise and sunset time"
  91. 910 '
  92. 920 ' ------------------------------------
  93. 930 ' Calculate sunrise and sunset times
  94. 940 ' ------------------------------------
  95. 950 XX=LO(X)/15
  96. 960 Y1=(TAN(DEC*3.141593/180)*TAN(LA(X)*3.141593/180))
  97. 970 IF Y1>1 OR Y1<-1 THEN  GOTO 1210      ' jump if none
  98. 980 Z=((FNACS (Y1))*180/3.141593)/15
  99. 990 T=XX+Z      ' sunrise
  100. 1000 TT=XX-Z     ' sunset
  101. 1010 LD=TT-T     ' length of day
  102. 1020 ' ------------------------------------
  103. 1030 GOSUB 2190                  ' time check
  104. 1040 GOSUB 2260                  ' convert decimals to hour and minutes
  105. 1050 '
  106. 1060 PRINT
  107. 1070 PRINT "  Sunrise time = " TIM$ " hours and minutes UTC"
  108. 1080 T=TT
  109. 1090 GOSUB 2190                  ' time check
  110. 1100 GOSUB 2260                  ' convert decimals to hour and minutes
  111. 1110 PRINT "  Sunset  time = " TIM$ " hours and minutes UTC"
  112. 1120 '
  113. 1130 ' Length of day
  114. 1140 '
  115. 1150 T=LD
  116. 1160 GOSUB 2190                  ' time check
  117. 1170 GOSUB 2260                  ' convert decimals to hour and minutes
  118. 1180 PRINT "  Length of day= " TIM$ " hours and minutes UTC"
  119. 1190 GOTO 670
  120. 1200 '
  121. 1210 PRINT
  122. 1220 PRINT "  No sunrise or sunset"
  123. 1230 GOTO 670
  124. 1240 '
  125. 1250 ' end of single calculation
  126. 1260 '--------------------------------------------
  127. 1270 ' start of full file calculation section
  128. 1280 '
  129. 1290 PRINT
  130. 1300 GOSUB 2000  ' get month and day
  131. 1310 GOSUB 2110  ' sun declination
  132. 1320 '
  133. 1330 PRINT "  Calculate sunrise and sunset times"
  134. 1340 PRINT
  135. 1350 '
  136. 1360 ' data for each country is in the sun.dat file
  137. 1370 ' prefix,country name, latitude,longitude
  138. 1380 ' (N=+,S=-,W=+,E=-)
  139. 1390 '
  140. 1400 OPEN "sun.dat" FOR INPUT AS #1
  141. 1410 '
  142. 1420 PRINT "Calculations for   Day = "D  " Month = "M
  143. 1430 PRINT "Prefix, Country,     Latitude, Longitude,  Sunrise,Sunset,length of day"
  144. 1440 PRINT "                                              UTC   UTC"
  145. 1450 '
  146. 1460 ' read country file, "sun.dat"
  147. 1470 '
  148. 1480 ZC=0
  149. 1490 FOR X=1 TO N
  150. 1500 IF EOF(1) THEN 1940
  151. 1510 LINE INPUT#1,P$(X)  ' country
  152. 1520 INPUT#1,LA(X),LO(X)
  153. 1530 '
  154. 1540 ' Calculate sunrise and sunset times
  155. 1550 ' ------------------------------------
  156. 1560 XX=LO(X)/15
  157. 1570 Y1=(TAN(DEC*3.141593/180)*TAN(LA(X)*3.141593/180))
  158. 1580 IF Y1>1 OR Y1<-1 THEN  GOTO 1850      ' jump if none
  159. 1590 Z=((FNACS (Y1))*180/3.141593)/15
  160. 1600 T=XX+Z      ' sunrise
  161. 1610 ' ------------------------------------
  162. 1620 GOSUB 2190                  ' time check
  163. 1630 LDSR=T                      ' save for length of day
  164. 1640 GOSUB 2260                  ' convert decimals to hour and minutes
  165. 1650 SR$(X) = TIM$  ' sunrise
  166. 1660 '
  167. 1670 ' sunset
  168. 1680 '
  169. 1690 T=XX-Z     ' sunset
  170. 1700 GOSUB 2190                 ' time check
  171. 1710 LDSS=T                     ' save for length of day
  172. 1720 GOSUB 2260                 ' convert decimals to hour and minutes
  173. 1730 SS$(X) = TIM$  ' sunset
  174. 1740 '
  175. 1750 ' length of day
  176. 1760 '
  177. 1770 T=LDSS-LDSR
  178. 1780 GOSUB 2190                 ' time check
  179. 1790 GOSUB 2260                 ' convert decimals to hour and minutes
  180. 1800 LD$(X)=TIM$                ' length of day
  181. 1810 GOTO 1870
  182. 1820 '
  183. 1830 ' Set sunrise and sunset to none
  184. 1840 '
  185. 1850 SR$(X) ="none" : SS$(X) = "none" : LD$(X) = "    "
  186. 1860 '
  187. 1870 PRINT USING "\                          \####.## ####.##  \    \\    \\    \";P$(X),LA(X),LO(X),SR$(X),SS$(X),LD$(X)
  188. 1880 '
  189. 1890 ZC=ZC+1
  190. 1900 NEXT X
  191. 1910 '
  192. 1920 PRINT "Number of countries = " ZC
  193. 1930 '
  194. 1940 N = X-1 ' set number of countries in data file
  195. 1950 PRINT "end of country data "
  196. 1960 CLOSE #1
  197. 1970 GOTO 2380  ' goto output section
  198. 1980 '
  199. 1990 ' ------subroutine--------------------
  200. 2000 ' Input correct date
  201. 2010 ' ------------------------------------
  202. 2020 PRINT
  203. 2030 INPUT "  Month? (1-12)";M
  204. 2040 IF M<1 OR M>12 THEN GOTO 2030
  205. 2050 INPUT "  Day? (1-31)";D
  206. 2060 IF D<1 OR D>31 THEN GOTO 2050
  207. 2070 GOSUB 2110      '  sun declination
  208. 2080 RETURN
  209. 2090 '
  210. 2100 ' ------subroutine--------------------
  211. 2110 ' Approximation for sun declination
  212. 2120 '
  213. 2130 DEC = D+(30.3 * (M-1))
  214. 2140 DEC = 10+(.988*DEC)
  215. 2150 DEC = -23.5 * COS (DEC*3.141593/180)
  216. 2160 RETURN
  217. 2170 '
  218. 2180 ' ------subroutine--------------------
  219. 2190 ' Check for correct time
  220. 2200 ' ------------------------------------
  221. 2210 IF T<0 THEN LET T=T+24
  222. 2220 IF T>24 THEN T=T-24
  223. 2230 RETURN
  224. 2240 '
  225. 2250 ' ------subroutine--------------------
  226. 2260 ' Convert decimal time to hours and minutes
  227. 2270 ' ------------------------------------
  228. 2280 HR = INT(T)
  229. 2290 MN=INT((T-HR)*60+.5)
  230. 2300 IF MN = 60 THEN MN = 0 : HR=HR+1
  231. 2310 HT=HR*100+MN
  232. 2320 HDR$=""
  233. 2330 IF HR=0 AND MN<10 THEN HDR$="000" ELSE IF HT < 100 THEN HDR$="00" ELSE IF HT < 1000 THEN HDR$="0"
  234. 2340 TIM$=HDR$ + MID$(STR$(HT),2)
  235. 2350 RETURN
  236. 2360 ' ------------------------------------
  237. 2370 '
  238. 2380 ' init pa(x) index array, for printing and sorting
  239. 2390 '
  240. 2400 FOR X=1 TO N   ' init first index array
  241. 2410   PA(X)=X
  242. 2420 NEXT X
  243. 2430 '                  second menu
  244. 2440 PRINT
  245. 2450 PRINT "  Enter 1 to sort  list "
  246. 2460 PRINT "  Enter 2 to print list "
  247. 2470 PRINT "  Enter 3 to return to main menu ";
  248. 2480 ' PRINT "  Enter 4 to search list, not done yet"
  249. 2490 INPUT IN
  250. 2500 IF IN = 1 THEN GOTO 2560
  251. 2510 IF IN = 2 THEN GOTO 3080
  252. 2520 IF IN = 3 THEN GOTO  330
  253. 2530 IF IN = 4 THEN GOTO 2440  ' reserved for search +/- 15 minutes
  254. 2540 GOTO 2440
  255. 2550 '
  256. 2560 PRINT
  257. 2570 PRINT "  Sort output on sunrise, sunset or length of day (SR/SS/LD) ";:INPUT IN$
  258. 2580 SORTFLAG=0
  259. 2590 IF IN$="sr" OR IN$="SR" THEN SORTFLAG=1 : GOTO 2620
  260. 2600 IF IN$="ld" OR IN$="LD" THEN SORTFLAG=2 : GOTO 2620
  261. 2610 IF IN$<>"ss" AND IN$<>"SS" THEN GOTO 2570
  262. 2620 PRINT
  263. 2630 PRINT "Sort data, Pass counter" ;
  264. 2640 '
  265. 2650 ' fast sort
  266. 2660 ' four passes, move indexes not data
  267. 2670 '
  268. 2680 K=0
  269. 2690 FOR P = 4 TO 1 STEP -1 ' POSITION
  270. 2700   B=1
  271. 2710   FOR NUM = 0 TO 9
  272. 2720     FOR X = 1 TO N
  273. 2730       IF SORTFLAG = 0 THEN GOTO 2760 ELSE IF SORTFLAG = 1 THEN GOTO 2750
  274. 2740       TEMP$ = MID$(LD$(PA(X)),P,1) : GOTO 2770
  275. 2750       TEMP$ = MID$(SR$(PA(X)),P,1) : GOTO 2770
  276. 2760       TEMP$ = MID$(SS$(PA(X)),P,1)
  277. 2770       IF (VAL(TEMP$)= NUM) THEN PB(B) = PA(X) : B=B+1
  278. 2780     NEXT X
  279. 2790     LOCATE 24,25 : K = K + 1 : PRINT K;
  280. 2800   NEXT NUM
  281. 2810 '
  282. 2820 ' move pb index to pa index
  283. 2830 '
  284. 2840   FOR X=1 TO N
  285. 2850     PA(X) =PB(X)
  286. 2860   NEXT X
  287. 2870 NEXT P
  288. 2880 '
  289. 2890 PRINT "sort done" : PRINT
  290. 2900 '
  291. 2910 '--------------------------------------------
  292. 2920 '             display results to the screen
  293. 2930 '
  294. 2940 Z=0
  295. 2950 PRINT "Calculations for   Day = "D  " Month = "M
  296. 2960 PRINT "Prefix, Country,     Latitude, Longitude,  Sunrise,Sunset,length of day"
  297. 2970 PRINT "                                              UTC   UTC"
  298. 2980 FOR Y=1 TO N
  299. 2990 X=PA(Y)
  300. 3000 PRINT USING "\                          \####.## ####.##  \    \\    \\    \";P$(X),LA(X),LO(X),SR$(X),SS$(X),LD$(X)
  301. 3010 Z=Z+1
  302. 3020 NEXT Y
  303. 3030 PRINT "Number of countries = " Z
  304. 3040 PRINT
  305. 3050 GOTO 2440  ' go back to second menu
  306. 3060 '-------------------------------------------
  307. 3070 '
  308. 3080 ' Print output to printer
  309. 3090 '
  310. 3100 PRINT
  311. 3110 PRINT "  Use compressed print (Y/N) ";:INPUT IN$
  312. 3120 IF IN$="N" OR IN$="n" THEN GOTO 3220
  313. 3130 IF IN$<>"Y" AND IN$<>"y" THEN GOTO 3110
  314. 3140 ' printer control         compressed on
  315. 3150 ' paper feed to 1/8 inch
  316. 3160 LPRINT CHR$(27);CHR$(48)
  317. 3170 '
  318. 3180 ' compressed characters
  319. 3190 LPRINT CHR$(15)
  320. 3200 GOTO 3280
  321. 3210 '
  322. 3220 ' reset line feed           compressed off
  323. 3230 LPRINT CHR$(27);CHR$(50)
  324. 3240 '
  325. 3250 ' compressed characters off
  326. 3260 LPRINT CHR$(18)
  327. 3270 '
  328. 3280 ' perforation skip, 5 lines
  329. 3290 LPRINT CHR$(27);CHR$(78);CHR$(5)
  330. 3300 '
  331. 3310 Z=0
  332. 3320 LPRINT "Sunrise and Sunset Program. "
  333. 3330 LPRINT "Version "VERSION   DATEV$ " By N4TL."
  334. 3340 LPRINT
  335. 3350 LPRINT "Calculations for   Day = "D  " Month = "M
  336. 3360 LPRINT "Prefix, Country,     Latitude, Longitude,  Sunrise,Sunset,length of day"
  337. 3370 LPRINT "                                              UTC   UTC"
  338. 3380 FOR Y=1 TO N
  339. 3390 X=PA(Y)
  340. 3400 LPRINT USING "\                          \####.## ####.##  \    \\    \\    \";P$(X),LA(X),LO(X),SR$(X),SS$(X),LD$(X)
  341. 3410 Z=Z+1
  342. 3420 NEXT Y
  343. 3430 LPRINT "Number of countries = " Z
  344. 3440 '
  345. 3450 GOTO 2440  ' go back to second menu
  346. 3460 END
  347.