Dim Dc_year(3)
R_d=180/Pi   ! Constant to change radians to degrees
D_r=Pi/180   ! Constant to change degrees to radians
'
'
'
Rez=Xbios(4) ! Determine the resolution 1 = medium, 2 = High
'
'   Since the plotting and such are set up for Med. and High rez,
'   don't allow the program to be run in low resolution
'
If Xbios(4)=0 Then
  Alert 1,"Please switch to|Medium resolution!",1,"Ok!",Dummy%
  End
Endif
'
'
' Start the program !!
'
Repeat
  Hidem  ! Hide the mouse cursor or it'll get in the way
  '
  '  Input the Year of curiosity
  '
  Input " Year? ",Y
  '
  ' The next few lines calculate the Julian date for Jan. 1 of the desired year.
  '
  Y1=Y-1
  M=13
  D=1
  Cls
  A=Int(Y1/100)
  B=2-A+Int(A/4)
  Jd=Int(365.25*Y1)+Int(30.6001*(M+1))+D+1720994.5+B
  '
  ' Set up constants to find the date of opposition
  '
  Stepper=32
  Day=-16
  Flag1=True ! Flag to detect when oppostion occurs
  Old_diff=0
  Gosub Bell
  Cls
  Print At(30,12);"  Working..";
  '
  ' Here we enter the loop to find the date of opposition by
  ' quartering the year first by 32 days, then 16, then 8...
  '
  While Stepper>0.25
    Iter=1
    Flag2=False
    Repeat
      Te=Jd+Day-2415020 ! Number of days since 1/1/1900
      Tc=Te/36525       ! Number of centuries since 1/1/1900
      '                 ! Find position of Sun and Jupiter at above date
      Gosub Position(Tc)
      '                  When Diff = 0 then Jupiter is at opposition
      Diff=Abs(Abs(Ra_sun-Ra_jup)-180)
      '   Check flags to see if near or at opposition
      If Flag1=True And Old_diff<Diff Then
        Flag2=False
      Else
        If Flag1=False And Old_diff<Diff Then
          Flag2=True
        Endif
      Endif
      If Old_diff>Diff Then
        Flag1=False
      Else
        Flag1=True
      Endif
      Old_diff=Diff
      Day=Day+Stepper
      Iter=Iter+1
    Until Flag2
    '
    '  Having detected that we're near opposition cut Stepper in two and
    '  start again and go back sufficient time to find actual time of
    '  opposition!
    '
    Print ".";
    Day=Day-3*Stepper
    Stepper=Stepper/2
    '
    ' when stepper meets condition above leave loop
    '
  Wend
  Print
  Print
  ' since there was a redcuton of the value of Day above increment it
  Day=Day+2*Stepper
  Gosub Bell
  Print
  '
  '  calculate calendar date from Julian date
  '
  Cls
  Gosub Opp_date(Day,Jd)
  Print
  '
  '   Print out results
  '
  If Day=0 Then
    Day=1
  Endif
  If Day<=365 And Day>0 Then
    Print "   The opposition of Jupiter occurred on day ";Day;" of the year ";Y;"."
  Endif
  If Day>365 Then
    Day1=Day-365
    Print "   There was no opposition of Jupiter in the year ";Y
    Y=Y+1
    Print "   but occurred on day ";Day1;" of the following year ";Y
  Endif
  Print
  Ra_sun=Ra_sun/15
  Ra_jup=Ra_jup/15
  Ra_s_min=Trunc(60*Frac(Ra_sun))
  Ra_j_min=Trunc(60*Frac(Ra_jup))
  Ra_sun=Trunc(Ra_sun)
  Ra_jup=Trunc(Ra_jup)
  Dc_s_min=Abs(Trunc(60*Frac(Dc_sun)))
  Dc_j_min=Abs(Trunc(60*Frac(Dc_jup)))
  Dc_sun=Trunc(Dc_sun)
  Dc_jup=Trunc(Dc_jup)
  Print
  Print "   R.A. of the Sun at Opposition = ";Ra_sun;"H ";" ";Ra_s_min;"M"
  Print "                 Dec. of the Sun = ";Dc_sun;Chr$(248);" ";Dc_s_min;"'"
  Print
  Print "   R.A. of Jupiter at Opposition = ";Ra_jup;"H ";" ";Ra_j_min;"M"
  Print "                 Dec. of Jupiter = ";Dc_jup;Chr$(248);" ";Dc_j_min;"'"
  Print
  Input "   Press 'Return' to start plot ";A$
  Gosub Bell
  '
  ' Set up the limits for plotting
  '
  Day_opp=Day
  Dc_year(1)=Dc_jup
  Dc_opp=Dc_jup
  Ra_opp=Ra_jup
  '
  ' Calculate the starting position
  '
  Day_start=Day-160
  Te=Jd+Day_start-2415020
  Tc=Te/36525
  Gosub Position(Tc)
  Ra_start=Ra_jup
  Ra_start1=Ra_start/15
  Ra_start$="R.A. Start = "+Str$(Trunc(Ra_start1))+"H "+Str$(Trunc((Ra_start1-Trunc(Ra_start1))*60))+"M"
  Dc_start=Dc_jup
  Dc_year(2)=Dc_start
  '
  ' Calculate the end position
  '
  Day_end=Day+160
  Te=Jd+Day_end-2415020
  Tc=Te/36525
  Gosub Position(Tc)
  Ra_end=Ra_jup
  Dc_end=Dc_jup
  Ra_end1=Ra_end/15
  Ra_end$="R.A. End = "+Str$(Trunc(Ra_end1))+"H "+Str$(Trunc((Ra_end1-Trunc(Ra_end1))*60))+"M"
  Dc_year(3)=Dc_end
  '
  '  Sort Declinations of start, end and opposition positions to determine
  '  the range of degrees of declination traveled to determine Dc_step.
  '
  Gosub Dc_sort
  '
  '   Since R.A.'s go from 0 to 360 degrees[0 to 24 hours] we have to
  '   determine if the plot crosses from 359 to 1 degrees to keep the plot on
  '   scale.
  '
  Fudge=0  ! Fudge factor if R.A. crosses the 0H boundary
  Ra_flag=0
  If Ra_end<Ra_start Then
    Fudge=360
    Ra_flag=1
  Endif
  Ra_start=Ra_start-Fudge
  Ra_diff=Ra_start-Ra_end
  '
  ' Determine the plotting step value for R.A.
  '
  Ra_step=640/Ra_diff
  Diff=0
  '
  ' clear the screen and start drawing
  '
  Cls
  If Rez=1 Then
    Box 0,7,639,199
  Else
    Box 0,10,639,399
  Endif
  Deftext ,0,2700,6
  Text 630,50*Rez,130,Ra_start$
  Deftext ,,900
  Text 8,150*Rez,130,Ra_end$
  Year$=Str$(Y)
  W_title$="Retrograde Motion of Jupiter for the year "+Year$
  If Rez=2 Then
    Deftext ,0,0,6
  Else
    Deftext ,1,0,4
  Endif
  If Rez=1 Then
    Text 150,5,300,W_title$
  Else
    Text 150,8,300,W_title$
  Endif
  Dc_1=(Dc_start-Dc_bottom)*Dc_step
  '
  '  Draw to the starting place and label  the Dec. limit
  '
  Draw 639,200*Rez-Dc_1
  Draw  To 1,200*Rez-Dc_1
  Draw 639,200*Rez-Dc_1
  Dc_bottom1h=Trunc(Dc_bottom)
  Dc_bottom1m=Abs(Trunc((Dc_bottom-Dc_bottom1h)*60))
  Dc_text$=Str$(Dc_bottom1h)+Chr$(248)+" "+Str$(Dc_bottom1m)+"'"
  Dc_text$="Dec. = "+Dc_text$
  If Dc_1<(100*Rez) Then
    Text 20,(Rez*198-Dc_1),100,Dc_text$
  Else
    Text 20,(Rez*206-Dc_1),100,Dc_text$
  Endif
  '
  '  Here be the plotting routine!!
  '
  For Day_pos=Day_start To Day_end
    Te=Jd+Day_pos-2415020
    Tc=Te/36525
    Gosub Position(Tc)
    If Ra_jup>180 And Ra_flag=1 Then
      Ra_jup=Ra_jup-Fudge
    Endif
    Diff_ra_start=Abs(Ra_start-Ra_jup)
    Ra_place=639-Abs(Diff_ra_start*Ra_step)
    Dc_place=Rez*200-Abs(Dc_jup-Dc_bottom)*Dc_step
    Diff=Ra_jup-O_ra_jup
    Draw  To Ra_place,Dc_place
    '
    '  If at the day of opposition draw a tic mark to show it
    '
    If (Day_opp=Day_pos) Then
      Draw  To Ra_place,Dc_place+5
      Draw  To Ra_place,Dc_place-5
      Draw  To Ra_place,Dc_place
    Endif
    O_ra_jup=Ra_jup
  Next Day_pos
  Dc_jup1=Trunc(Dc_jup*100)/100
  '
  '  Draw the end Dec. limit and label it.
  '
  Draw  To 639,Dc_place
  Dc_jup1h=Trunc(Dc_jup)
  Dc_jup1m=Abs(Trunc((Dc_jup-Dc_jup1h)*60))
  Dc_text$=Str$(Dc_jup1h)+Chr$(248)+" "+Str$(Dc_jup1m)+"'"
  Dc_text$="Dec. = "+Dc_text$
  If Dc_place<100 Then
    Text 500,Dc_place+6,100,Dc_text$
  Else
    Text 500,Dc_place-2,100,Dc_text$
  Endif
  Gosub Bell
  What_now:
  Repeat
  Until Mousek=0
  Showm
  Alert 2,"Retrograde Drawing|    Complete| Press any Key|  to Continue",1,"Look|Print|QUIT ",Butn
  Hidem
  If Butn=2 Then
    Hardcopy
    Goto What_now
  Endif
  If Butn=3
    Goto Drop_out
  Else
    Gosub Any_key
    Goto What_now
  Endif
  '
  '  Asks if you want to do it again
  '
  Drop_out:
  Input "Do you wish another year's plot (Y/N) ";Yes$
Until ((Yes$="n") Or (Yes$="N"))
'      Return mouse cursor
Showm
End
'
'   The following Procedure calculates the positions of the Sun and Jupiter
'   for the given Julian Date.
'
Procedure Position(Tx)
  '  The eccentricity of the Earth's orbit's orbit's orbit's orbit's orbit                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                un-E_sun2
    E_sun2=E_sun
  Wend
  ' Radius vector of the Sun
  R_sun=1.0000002*(1-Ecc_earth*Cos(E_sun*D_r))
  ' the true anomaly of the Sun
  V_sun=2*Atn(Sqr((1+Ecc_earth)/(1-Ecc_earth))*Tan(E_sun*D_r/2))*R_d
  ' the true logitude of the Sun
  Sun_true_lng=L_sun+V_sun-M_sun
  '  Correction for nutation and abberation
  Omega_sun=259.18-1934.142*Tx
  ' Obiliquity of the Ecliptic
  Obilquity=Obilquity+0.00256*Cos(Omega_sun*D_r)
  Temp1=Cos(Obilquity*D_r)*Sin(Sun_true_lng*D_r)
  Temp2=Cos(Sun_true_lng*D_r)
  '
  ' Now calculate the right ascension of the Sun and put it in
  ' the right quadrent
  '
  Ra_sun=Atn(Temp1/Temp2)
  If Temp2>0 And Temp1<0 Then
    Ra_sun=Ra_sun+2*Pi
  Endif
  If Temp2<0 Then
    Ra_sun=Ra_sun+Pi
  Endif
  '
  ' Calculate the declination position of the Sun
  '
  Temp1=Sin(Obilquity*D_r)*Sin(Sun_true_lng*D_r)
  Temp2=Sqr(1-Temp1*Temp1)
  Dc_sun=Atn(Temp1/Temp2)*R_d
  Ra_sun=Ra_sun*R_d
  '
  '  Now repeat the above for Jupiter
  '
  L_jup=238.049257+3036.301986*Tx+0.0003347*Tx*Tx-1.65E-06*Tx*Tx*Tx
  A_jup=5.202561
  Ecc_jup=0.04833475+0.00016418*Tx-4.676E-07*Tx*Tx-1.7E-09*Tx*Tx*Tx
  I_jup=1.308736-0.0056961*Tx+3.9E-06*Tx*Tx
  W_jup=273.277558+0.5994317*Tx+0.00070405*Tx*Tx+5.08E-06*Tx*Tx*Tx
  Omega_jup=99.443414+1.01053*Tx+0.00035222*Tx*Tx-8.51E-06*Tx*Tx*Tx
  M_jup=225.322833+3034.69202*Tx-0.000722*Tx*Tx
  E_diff=1
  E_jup2=M_jup
  While Abs(E_diff)>1.0E-06
    E_jup=M_jup+Ecc_jup*R_d*Sin(E_jup2*D_r)
    E_diff=E_jup-E_jup2
    E_jup2=E_jup
  Wend
  Temp1=Sin(E_jup*D_r/2)
  Temp2=Cos(E_jup*D_r/2)
  Temp3=Sqr((1+Ecc_jup)/(1-Ecc_jup))
  V_jup=Atn(Temp3*Temp1/Temp2)
  If (Temp1*Temp3)<0 And Temp2>0 Then
    V_jup=V_jup+2*Pi
  Endif
  If Temp2<0 Then
    V_jup=V_jup+Pi
  Endif
  V_jup=2*V_jup*R_d
  R_jup=A_jup*(1-Ecc_jup*Cos(E_jup*D_r))
  U_jup=L_jup+V_jup-M_jup-Omega_jup
  Temp1=Cos(I_jup*D_r)*Sin(U_jup*D_r)
  Temp2=Cos(U_jup*D_r)
  Long_jup=Atn(Temp1/Temp2)
  If Temp2>0 And Temp1<0 Then
    Long_jup=Long_jup+2*Pi
  Endif
  If Temp2<0 Then
    Long_jup=Long_jup+Pi
  Endif
  Long_jup=Long_jup*R_d
  Long_jup=Long_jup+Omega_jup
  Temp1=Sin(U_jup*D_r)*Sin(I_jup*D_r)
  Temp2=Sqr(1-Temp1*Temp1)
  B_jup=Atn(Temp1/Temp2)*R_d
  Geo_sun_lng=Long_jup-Sun_true_lng
  Temp1=R_jup*Cos(B_jup*D_r)*Sin((Geo_sun_lng)*D_r)
  Temp2=R_jup*Cos(B_jup*D_r)*Cos((Geo_sun_lng)*D_r)+R_sun
  Long_jup_geo=Atn(Temp1/Temp2)
  If Temp2>0 And Temp1<0 Then
    Long_jup_geo=Long_jup_geo+2*Pi
  Endif
  If Temp2<0 Then
    Long_jup_geo=Long_jup_geo+Pi
  Endif
  Long_jup_geo=Long_jup_geo*R_d
  Temp_geo=Long_jup_geo
  Long_jup_geo=Long_jup_geo+Sun_true_lng
  Delta_jup=Sqr(R_sun*R_sun+R_jup*R_jup+2*R_jup*R_sun*Cos(B_jup*D_r)*Cos((Geo_sun_lng)*D_r))
  Temp1=R_jup*Sin(B_jup*D_r)/Delta_jup
  Temp2=Sqr(1-Temp1*Temp1)
  Beta_jup=Atn(Temp1/Temp2)*R_d
  Temp1=Cos(Beta_jup*D_r)*Cos(Temp_geo*D_r)
  Temp2=Sqr(1-Temp1*Temp1)
  Elong_jup=Atn(Temp2/Temp1)
  Temp1=Sin(Long_jup_geo*D_r)*Cos(Obilquity*D_r)-Tan(Beta_jup*D_r)*Sin(Obilquity*D_r)
  Temp2=Cos(Long_jup_geo*D_r)
  Ra_jup=Atn(Temp1/Temp2)
  If Temp2>0 And Temp1<0 Then
    Ra_jup=Ra_jup+2*Pi
  Endif
  If Temp2<0 Then
    Ra_jup=Ra_jup+Pi
  Endif
  Ra_jup=Ra_jup*R_d
  Temp1=Sin(Beta_jup*D_r)*Cos(Obilquity*D_r)+Cos(Beta_jup*D_r)*Sin(Obilquity*D_r)*Sin(Long_jup_geo*D_r)
  Temp2=Sqr(1-Temp1*Temp1)
  Dc_jup=Atn(Temp1/Temp2)*R_d
Return
'
' Procedure to sort the three important declinations of Jupiter, start,end,
' and opposition
'
Procedure Dc_sort
  Switch=True
  While Switch=True
    For I%=1 To 2
      If Dc_year(I%)<Dc_year(I%+1) Then
        Dc_temp=Dc_year(I%+1)
        Dc_year(I%+1)=Dc_year(I%)
        Dc_year(I%)=Dc_temp
        Switch=True
      Else
        Switch=False
      Endif
    Next I%
  Wend
  '
  ' Having sorted the Dec.'s calculate the Dec. range and declination
  ' plotting step
  '
  Dc_bottom=Dc_year(3)-0.5
  Dc_range=Abs(Dc_year(1)-Dc_year(3))+1
  Dc_step=200*Rez/Dc_range
Return
'
' procedure to calculate the calendar date form the Julian Date
'
Procedure Opp_date(Day1,Jd1)
  Jd1=Jd1+Day1+0.5
  Z=Int(Jd1)
  F=Frac(Jd1)
  If Z<2299161 Then
    A=Z
  Else
    Alpha=Int((Z-1867216.25)/36524.25)
    A=Z+1+Alpha-Int(Alpha/4)
  Endif
  B=A+1524
  C=Int((B-122.1)/365.25)
  D=Int(365.25*C)
  E=Int((B-D)/30.6001)
  Day_of_month=B-D-Int(30.6001*E)+F-1
  If E<13.5 Then
    Month=E-1
  Endif
  If E>13.5
    Month=E-13
  Endif
  If Month>2.5 Then
    Year=C-4716
  Endif
  If Month<2.5 Then
    Year=C-4715
  Endif
  Print " >>   Date of Opposition = ";Month;"/";Day_of_month;"/";Year
Return
'
'  Procedure for a bell sound!!!
'
Procedure Bell
  For Y99=1 To 2
    Sound 1,15,1,7
    Wave 1,1,9,15000,100
  Next Y99
Return
'
Procedure Any_key
  Repeat
    Z$=Inkey$
    If Mousek<>0
      Z$=""
    Endif
  Until Z$<>""
Return
