'This program is for dials with standard time hourlines.
'Copyright Steve Lelievre 2000. EoT and Declination calculations 
'are based on sample code provided by Fer De Vries. You may not
'claim copyright or remove this copyright statement. You may extract
'fragments of the source for re-use, provided they do not represent
'the substantive body of the application. 

'Please do not publish altered versions of this application
'using the name SCADD. Notify bugs, suggestions for additional
'facilities and requests for latest version to steve_lelievre@hotmail.com

'Intermediate version: this version has code for a dial type that
'I am temporarily calling TANGENT, but it isn't activated yet.
'The TANGENT dial is a columnar azimuthal dial, where the column itself acts at the style.
'
'Please note that the code is in a bit of a muddle. I cranked it out
'quickly and haven't finished cleaning it yet.


Const pi = 3.1415926535897932384626433832795028841971
Const minuspi = -3.1415926535897932384626433832795028841971
Const yearlen = 365

'Enumerate dial types
Const AXIAL   = 0
Const AZIMUTH = 1
Const TANGENT = 2

'label types
Const STANDARD = 0
Const ROMAN = 1

'Draw positions
Const UNDEFINED = 0
Const XPOSITIVE = 1
Const XNEGATIVE = 2

Const SPINLABELS = 1

Const JANOUTSIDE = 1

'Hemisphere marker
Const NORTHERN = 0
Const SOUTHERN = 1

'Allocate space for global data - accessible throughout the whole program
Dim eot(yearlen) As Double, decl(yearlen) As double, curve(yearlen*2) As double,latitude As Double, longitude As Double
Dim dialtype, pointnum, drawposition, hemisphere, tempsingle

'Allocate space for other data used in the main body of the code
'Some other items are autoallocated, typically as single length numerics
Dim bearing As double, ha As double, location As String

'Turn off updates so that the process executes faster
dcSetDrawingWindowMode dcMaximizeWin
dcUpdateDisplay False

'Clear existing drawing, needs to have user prompt added later.
dcCloseWithoutSaving
dcNew ""

drawposition = UNDEFINED
pointnum = 0

'Because EoT and Declination values are used many times over,
'the application calculates them once and stores the values in arrays.
'This is done purely for performance reasons.
Call initialisesolardata()

'Get parameters from the user
'First, define a dialog class
Begin Dialog POPUP 129,61, 224, 199, "SCADD - Parameter input dialogue"
  TextBox 56,8,17,12, .latitude
  TextBox 80,8,25,12, .latmins
  TextBox 56,26,17,12, .longitude
  TextBox 80,26,25,12, .longmins
  TextBox 56,44,17,12, .tzmeridian
  TextBox 80,44,25,12, .tzmins
  TextBox 172,8,37,12, .outerradius
  TextBox 172,26,37,12, .innerradius
  TextBox 172,44,37,12, .gauge
  OptionGroup .CONFIGURATION
    OptionButton 12,76,49,12, "Polar axis"
    OptionButton 12,88,37,12, "Upright"
  OptionGroup .LABELS
    OptionButton 120,76,37,12, "Roman"
    OptionButton 120,88,45,12, "Standard"
  CheckBox 120,100,41,12, "Inverted", .orientation
  CheckBox 8,110,69,12, "January outside", .sequence
  OKButton 56,134,37,12
  CancelButton 116,134,37,12
  GroupBox 8,68,97,36, "Gnomon"
  GroupBox 116,68,93,48, "Labels"
  Text 8,8,37,12, "Latitude"
  Text 8,26,37,12, "Longitude"
  Text 8,44,45,12, "Timezone"
  Text 116,8,45,12, "Outer radius"
  Text 116,26,45,12, "Inner radius"
  'Text 116,44,49,12, "Gnomon width"
  Text 116,44,49,12, "Unused field"
  Text 20,170,174,12, "Copyright Steve Lelievre 2000. Version 2000-04-27"
  Text 32,182,143,12, "Enquiries to steve_lelievre@hotmail.com"
  Text 36,154,145,12, "Use negative degrees for East and South"
End Dialog
'Second, allocate one of them
Dim prompt As popup
'Third, initalise it
prompt.innerradius = 2
prompt.outerradius = 4
prompt.gauge = 0 
'Fourth, activate it repeatedly - until the user gets the parameters right
Do

   prompt.gauge = 0

   flag = FALSE
   action = Dialog(prompt)
   If action = CancelButton Then Stop
   If IsNumeric(prompt.latitude) And IsNumeric(prompt.longitude) _ 
      And IsNumeric(prompt.tzmeridian) And IsNumeric(prompt.outerradius) _ 
      And IsNumeric(prompt.innerradius) And IsNumeric(prompt.gauge) _ 
      And IsNumeric(prompt.latmins) And IsNumeric (prompt.longmins) _ 
      And IsNumeric(prompt.tzmins) Then
      If Abs(prompt.latitude) < 25 Or Abs(prompt.latitude) > 65 Then 
         message="Latitude must be in the range 25° to 65°, or -25° to -65°"
         flag  = TRUE
         prompt.latitude=""
      End If
      If prompt.longitude < -180 Or prompt.longitude > 180 Then
         message="Longitude must be in range -180 to +180"
         flag  = TRUE
         prompt.longitude=""   
      End If
      If prompt.latmins < 0 Or prompt.latmins >=60 Then
         message="Minutes of latitude must be greater than zero and less than 60'" 
         flag  = TRUE
         prompt.latmins=""   
      End If
      If prompt.longmins < 0 Or prompt.longmins >=60 Then
         message="Minutes of longitude must be greater than zero and less than 60'" 
         flag  = TRUE
         prompt.longmins=""   
      End If
      If prompt.tzmeridian < -180 Or prompt.tzmeridian > 180 Then
         message="Timezone meridian must be in the range -180° to +180°"
         flag  = TRUE
         prompt.tzmeridian=""   
      End If
      If prompt.tzmins < 0 Or prompt.tzmins >=60 Then
         message="Timezone minutes must be greater than zero and less than 60'"
         flag  = TRUE
         prompt.tzmins=""   
      End If
      If prompt.outerradius < 0 Then
         message="Outer radius must be greater than zero"
         flag  = TRUE
         prompt.outerradius=""
      End If
      If prompt.innerradius < 0 Then
         message="Inner radius must be greater than zero"
         flag  = TRUE
         prompt.innerradius=""
      End If
      If prompt.gauge < 0 Then
         message="Style width must be greater than or equal to zero"
         flag=1
         prompt.gauge=""
      End If
      If CSng(prompt.outerradius) <= CSng(prompt.innerradius) Then
         message="Outer radius must be bigger than inner radius"
         flag  = TRUE
      End If 
      If CSng(prompt.innerradius) < CSng(prompt.gauge) Then
         message="Inner radius must be greater than or equal to style width"
         flag  = TRUE
      End If
      If flag = FALSE Then 
         If prompt.latitude < 0 Then tempsingle = prompt.latmins * -1 Else tempsingle = prompt.latmins
         latitude = (tempsingle/60 + prompt.latitude)*pi/180
         If prompt.longitude < 0 Then tempsingle = prompt.longmins * -1 Else tempsingle = prompt.longmins
         longitude = (tempsingle/60 + prompt.longitude)*pi/180
         If prompt.tzmeridian < 0 Then tempsingle = prompt.tzmins * -1 Else tempsingle = prompt.tzmins
         tzoff = (tempsingle/60 + prompt.tzmeridian)*pi/180
         tzoff = tzoff - longitude
      End If
   Else
      message="Only numeric values are allowed"
         flag  = TRUE
   End If
   If flag = TRUE Then
      MsgBox(message)
   Else
      Exit Do
   End If
Loop

'Extract application settings from user data
inner=prompt.innerradius
outer=prompt.outerradius
thickness=prompt.gauge
Select Case prompt.configuration
   Case 0
      dialtype = AXIAL
      thickness=thickness/2
   Case 1
      dialtype = AZIMUTH
   Case Else
      'Shouldn't happen
      MsgBox("Debug 0")
      Stop
End Select

' dialtype = TANGENT

Select Case prompt.labels
   Case 0
      labelling = ROMAN
   Case 1
      labelling = STANDARD
   Case Else
      'shouldn't happen
      MsgBox("Debug 1")
      Stop
End Select

If latitude < 0 Then
   hemisphere = SOUTHERN 
Else
   hemisphere = NORTHERN
End If

'Initalize variables which control the ratios of the drawing
quantum=(outer-inner)/yearlen
delta=15*pi/180

'Add labels
layer("Labels")
For hournum = -11 To 11 Step 1
   For pass = 1 To 2 Step 1
      If pass = 1 Then
         'textsize = prompt.innerradius*4
         textsize = 10
         If prompt.orientation = SPINLABELS Then
            radius = -15
         Else
            radius = -15 - textsize
         End If
         If prompt.sequence = JANOUTSIDE Then
            radius = yearlen - radius
            bearing = labelangle2(hournum)
         Else
            bearing=labelangle1(hournum)
         End If
      Else
         'textsize = prompt.outerradius*4
         textsize = 10
         If prompt.orientation = SPINLABELS Then
            radius = yearlen + 20 + textsize
         Else
            radius = yearlen + 10
         End If
         If prompt.sequence = JANOUTSIDE Then
            radius = yearlen - radius
            bearing = labelangle1(hournum)
         Else
            bearing =labelangle2(hournum)
         End If
      End If
      If bearing <> pi Then
         If prompt.orientation = SPINLABELS Then
            dcSetTextParms dcBLACK, "Times New Roman", "Regular", (pi-bearing)*180/pi, textsize, 25, 0, 0
         Else 
            dcSetTextParms dcBLACK, "Times New Roman", "Regular", -bearing*180/pi, textsize, 25, 0, 0
         End If
         If dialtype = TANGENT Then dcSetTextParms dcBLACK, "Times New Roman", "Regular", 0, textsize, 25, 0, 0
         dcCreateText decodex(bearing,radius),decodey(bearing,radius),0,lookuplabel(hournum,labelling)
      End If
   Next pass
Next hournum

'Now put image up on screen and turn on dispay to allow user to watch as rest of dial is drawn
dcViewAll
dcUpdateDisplay True

'Draw the other bits and pieces
layer("Calibration")

'Place a cross at centre of diagram for checking aspect ratio of printouts
If dialtype <> TANGENT Then
   dcCreateLine 0, prompt.innerradius * .5, 0, prompt.innerradius * -.5
   dcCreateLine prompt.innerradius * .5, 0, prompt.innerradius * -.5,0
Else
   dcCreateLine decodex(minuspi,1),decodey(minuspi,1),decodex(minuspi,yearlen),decodey(minuspi,yearlen)
   dcCreateLine decodex(pi,1),decodey(pi,1),decodex(pi,yearlen),decodey(pi,yearlen)
End If

'Output location
If prompt.orientation = SPINLABELS Then
   dcSetTextParms dcBLACK, "Times New Roman", "Regular", 180, prompt.outerradius*4, 17, 0, 0
Else
   dcSetTextParms dcBLACK, "Times New Roman", "Regular", 0, prompt.outerradius*4, 17, 0, 0
End If
item1 = Abs(Int(prompt.latitude))
'Round to nearest Minute of arc
item2 = Int(Abs(prompt.latmins)+.49999999)
If item2 = 60 Then
   item1 = item1 + 1
   item2 = 0
End If
location = Str(item1) & "°" & Str(item2) & "'"
If latitude >= 0 Then
   location = location & "N  "
Else
   location = location & "S  "
End If
item1 = Abs(Int(prompt.longitude))
'Round to nearest Minute of arc
item2 = Int(Abs(prompt.longmins)+.49999999)
If item2 = 60 Then
   item1 = item1 + 1
   item2 = 0
End If
location = location & Str(item1) & "°" & Str(item2) & "'"
If longitude >= 0 Then
   location = location & "W"
Else
   location = location & "E"
End If
dcCreateText 0, prompt.innerradius * -0.6, 0, location

'Deal with gnomon footprint
If dialtype = AXIAL And thickness > 0 Then
   'Draw in the edges of the gnomon
   dcCreateLine 0-thickness, inner, 0-thickness, outer
   dcCreateLine thickness, inner, thickness, outer
End If

If dialtype = AZIMUTH Then
   'Draw a circle to show the footprint of the gnomon
   dcCreateCircle 0,0,thickness
End If

'Draw limiting curves for sunrise and sunset
layer("twilight")
'Do two passes, to give sunrise and sunset curves. Symetrical, so just
'use the inverse of the angle on the second pass.
For pass = 1 To 2 Step 1
   For daynum = 1 To yearlen Step 2
      ha = zeroaltitude(daynum,latitude)
      If (pass = 1) Then ha = -ha
      bearing = shadowbearing(dialtype, daynum, ha, latitude)
      If (daynum = 1) Then 
         Call moveto(bearing,daynum)
      Else
         Call drawto(bearing,daynum)
      End If
   Next daynum
Next pass


'Draw arcs for first of each month
layer("months")
dayarc(1)
dayarc(1+31)
dayarc(1+31+28)
dayarc(1+31+28+31)
dayarc(1+31+28+31+30)
dayarc(1+31+28+31+30+31)
dayarc(1+31+28+31+30+31+30)
dayarc(1+31+28+31+30+31+30+31)
dayarc(1+31+28+31+30+31+30+31+31)
dayarc(1+31+28+31+30+31+30+31+31+30)
dayarc(1+31+28+31+30+31+30+31+31+30+31)
dayarc(1+31+28+31+30+31+30+31+31+30+31+30)
dayarc(1+31+28+31+30+31+30+31+31+30+31+30+31)

'Force drawing of previous line at current thickness
Call flush()

'Start creating the diagram
layer("Hours")

'Use thick(er) lines
dcSetLineParms dcBLACK, dcSOLID, dcNORMAL

'Draw the hour lines
'Value 1 means use a 1 hour interval
timelines(1)

'Force drawing of last line at current thickness
Call flush()

'Draw the rest of the diagram using thin lines
dcSetLineParms dcBLACK, dcSOLID, dcTHIN

'Draw the quarters
layer("Quarters")
timelines(.25)

'Dispay diagram, beep and terminate
'Force final line to appear
Call flush()
Beep


'Various subroutines and functions are defined between here and end of file 

Function shadowbearing(ByVal dialtype, ByVal daynum, ByVal ha As double, ByVal latitude As double) As double
'Calculate the  horizontal bearing of the sun relative to base of style
   Dim result As double 
   Select Case dialtype
      Case AXIAL
         result=Atn(Tan(ha)*Sin(latitude))
      Case AZIMUTH 
         result=Atn(Sin(ha)/(Sin(latitude)*Cos(ha)-Tan(decl(daynum))*Cos(latitude)))
      Case TANGENT
         result=Atn(Sin(ha)/(Sin(latitude)*Cos(ha)-Tan(decl(daynum))*Cos(latitude)))
      Case Else
        'shouldn't happen
         MsgBox("Debug 2a")
         Stop
   End Select
   'Fiddle it to get the angle into the right quadrant
   'The value pi/4 is not thought out precisely. It has to be big enough to allow
   'the noon Line To wriggle either side of North-South without being displaced.
   'Only early morning and late evening values should get changed.
   If hemisphere = NORTHERN Then
      If (ha < minuspi/4 And result > 0) Then result = result - pi
      If (ha > pi/4 And result < 0) Then result = result + pi
   Else
      'Dont know if this is always right, but it seems to work....
      If (ha < minuspi/4 And result < 0) Then result = result + pi
      If (ha > pi/4 And result > 0) Then result = result - pi
   End If
   shadowbearing = result
End Function

Function zeroaltitude(ByVal daynum,ByVal latitude As double) As double
' Calculates the local hour angle when the sun is on the horizon
' No refraction handling
    zeroaltitude = acos(-Tan(decl(daynum))*Tan(latitude))
End Function

Sub timelines(interval)
'Draw hour lines. Each hour line is drawn as 365 line segments.
'The hourline may need to be in two sections due to the way it crosses
'the curved shape of twilight lines. split is a toggle which detects
'the hourline going outside the twilight lines, and flags that a new
'line segment will be needed once it comes back inside.
    Dim split, hournum, daynum, ha As Double, bearing As double
    For hournum = -11 To 11 Step interval
      'Do not make a quarter hour line where an hour line already sits
      If (hournum <> Int(hournum)) Or (interval = 1) Then
         split= 0
         For daynum = 1 To yearlen Step 2
            twilight = zeroaltitude(daynum,latitude)
            ha = hournum*delta + eot(daynum)+tzoff
            If (Abs(ha) < Abs(twilight)) Then
               'Sun is above horizon, so plot a point
               bearing = shadowbearing(dialtype, daynum, ha, latitude)
               If (split = 0) Then
                  'We need to start a new curve segment
                  Call moveto(bearing,daynum)
                  split = 1
               Else
                  Call drawto(bearing,daynum)
               End If
            Else
               'Set the toggle so that we start a new segment when sun comes above horizon 
               split = 0
            End If
         Next daynum
      End If
   Next hournum
End Sub

Function labelangle1(ByVal hournum) As Double
'Work out the angle at which the inner label should appear. It should be by the end of the hour line
   Dim daynum, ha As Double, twilight As Double
   daynum = 0
   Do 
      daynum = daynum + 1
      twilight = zeroaltitude(daynum,latitude)
      ha = hournum*delta + eot(daynum)+tzoff
   Loop Until (daynum = yearlen) Or (Abs(ha) <= Abs(twilight))
   If daynum = yearlen Then 
      labelangle1 = pi
   Else
      labelangle1 = shadowbearing(dialtype, daynum, ha, latitude)
   End If
End Function

Function labelangle2(ByVal hournum) As Double
'Work out the angle at which the outer label should appear. It should be by the end of the hour line
   Dim daynum, ha As double, twilight As Double
   daynum = yearlen + 1
   Do 
      daynum = daynum - 1
      twilight = zeroaltitude(daynum,latitude)
      ha = hournum*delta + eot(daynum)+tzoff
   Loop Until (daynum = 1) Or (Abs(ha) <= Abs(twilight))
   If daynum = 1 Then
      labelangle2 = pi
   Else
      labelangle2 = shadowbearing(dialtype, daynum, ha, latitude)
   End If
End Function

Sub dayarc(ByVal daynum)
'Draw arc for chosen day, typically first of each month
'The arc goes from sunrise To sunset positions
Dim twilight As double, dayindex
   If (daynum = yearlen + 1) Then
      'This is the first of January of the following year
      dayindex=1
   Else
      dayindex=daynum
   End If
   twilight = zeroaltitude(dayindex,latitude)
   bearing = shadowbearing(dialtype,dayindex,twilight,latitude) 
   drawarcs(bearing,daynum)
End Sub

Function decodex (ByVal angle As double,ByVal daynum) As double
'Converts a polar coordinate into a Cartesian one, and adjusts for gnomon width at the same time
   Dim radius As double
   If prompt.sequence = JANOUTSIDE Then
      radius = outer - daynum*quantum
   Else
      radius = daynum*quantum + inner
   End If

   Select Case dialtype
      Case AXIAL
         If angle >= 0 Then
            If angle > pi/2 Then
               'Recalculate angle to reflect shadow coming from other side of gnomon
               angle = pi/2 + Atn(radius*Sin(angle - pi/2)/(radius*Cos(angle - pi/2) - thickness))

            End If
            decodex = radius*Sin(angle) + thickness
         Else
            If angle < minuspi/2 Then
               'Recalculate angle to reflect shadow coming from other side of gnomon
               angle = minuspi/2 - Atn(radius*Sin(pi/2 - angle)/(radius*Cos(pi/2 - angle) - thickness))
            End If
            decodex = radius*Sin(angle) - thickness
         End If
      Case AZIMUTH
         decodex = radius*Sin(angle + pi/2 - acos(thickness/radius))
      Case TANGENT
         decodex = outer*angle/(2*pi)
      Case Else
        'shouldn't happen
         MsgBox("Debug 3")
         Stop
   End Select
End Function

Function decodey (ByVal angle As double,ByVal daynum) As double
'Converts a polar coordinate into a Cartesian one, and adjusts for gnomon width at the same time
   Dim radius As double
   If prompt.sequence = JANOUTSIDE Then
      radius = outer - daynum*quantum
   Else
      radius = daynum*quantum + inner
   End If
   Select Case dialtype
      Case AXIAL
         decodey = radius*Cos(angle)
      Case AZIMUTH
         decodey = radius*Cos(angle + pi/2 - acos(thickness/radius))
      Case TANGENT
         decodey = inner * daynum / yearlen 
      Case Else
        'shouldn't happen
         MsgBox("Debug 3A")
         Stop
   End Select
End Function


Sub drawto (ByVal angle As double,ByVal daynum)
'Draw a line sement from current point to a new point
'This is polar.
'The data is not drawn to screen until a new line is started.
'Instead, we are building up an array of points to be plotted
   If angle >= 0 Then
      If drawposition = XNEGATIVE Then Call flush()
      drawposition = XPOSITIVE
   Else
      If drawposition = XPOSITIVE Then Call flush()
      drawposition = XNEGATIVE
   End If
   curve(pointnum*2 + 1) = decodex(angle,daynum)
   curve(pointnum*2 + 2) = decodey(angle,daynum)
   pointnum = pointnum + 1
End Sub

Sub moveto (ByVal angle As double,ByVal daynum)
'Move current point to a new place without drawing a line
'This is polar
   If pointnum > 0 Then
      Call flush()
   End If
   pointnum = 0
   Call drawto (angle, daynum)
End Sub

Sub flush()
'Output previous curve
   'dcCreateSpline curve(1),pointnum,False
   Dim counter
   For counter = 1 To (pointnum-1)*2 Step 2
      dcCreateLine curve(counter), curve(counter+1), curve(counter+2), curve(Counter+3)
   Next counter
   pointnum=0
End Sub


Sub drawarcs(ByVal startangle As double,ByVal daynum)
'Draw symetrical arcs by rotation through given angle, but adjust for gnomon width 
   Dim radius As double, localx1 As double, localy1 As Double, localx2 As double, localy2 As double
   If prompt.sequence = JANOUTSIDE Then
      radius = outer - daynum*quantum
   Else
      radius = daynum*quantum + inner
   End If
   If startangle > pi/2 Then
     'Recalculate angle to reflect shadow coming from other side of gnomon
     startangle = pi/2 + Atn(radius*Sin(startangle - pi/2)/(radius*Cos(startangle - pi/2) - thickness))
   End If
   Select Case dialtype
      Case AXIAL
         'Draw 2 curves. Their centres are displaced from the NS line to allow for gnomon thickness
            dcCreateCircleEx  thickness,0, decodex(Abs(startangle),daynum), decodey(Abs(startangle),daynum), _ 
                                        thickness,radius,radius,radius,0,0
            dcCreateCircleEx -thickness,0, -thickness,radius,decodex(-Abs(startangle),daynum), _ 
                                        decodey(-Abs(startangle),daynum),radius,radius, 0, 0
      Case AZIMUTH  
         'Draw one arc from dawn to dusk.
            dcCreateCircleEx 0,0,decodex(Abs(startangle),daynum),decodey(Abs(startangle),daynum), _ 
                                        decodex(-Abs(startangle),daynum),decodey(-Abs(startangle),daynum),radius,radius,0,0
      Case TANGENT
         ' Need a straight line around the column
         dcCreateLine decodex(Abs(startangle),daynum),decodey(Abs(startangle),daynum),decodex(-Abs(startangle),daynum), _ 
                                        decodey(-Abs(startangle),daynum)
      Case Else
        'shouldn't happen
         MsgBox("Debug 4")
         Stop
   End Select
End Sub

Sub layer(ByVal layername)
'Create and activate a layer
   Dim result
   dcAddLayer layername
   dcSetCurrentLayer layername
End Sub

Function asin(ByVal value) As double
'Arcsin function is not provided, so must write my own
   Dim divisor As double
   divisor = Abs(1 - value^2)
   If divisor = 0 Then
      asin = pi/2
   Else
      asin=Atn(Sqr(value^2/divisor))
      If value < 0 Then asin = -asin
   End If   
End Function

Function acos(ByVal value) As double
'Arccos function is not provided, so must write my own
   If value = 0 Then
      acos = pi/2
   Else 
      acos=Atn(Sqr(1/value^2 - 1))
   End If
   If value < 0 Then acos = pi - acos
End Function

Sub initialisesolardata()
'Because EoT and Declination values are used many times over,
'the application calculates them one and stores the values in arrays.
'This is done for performance reasons.
   Dim L As double, epsilon As double, zz As double, lambda As double, eotv As double, daynum
   ' (day + 0.5) means at 12:00 UT
   zz = pi/180
   For daynum = 1 To yearlen
      L =((daynum + 0.5)*360/365.2422 - 80.535132)*zz
      eotv = - 107.0605*Sin(L) - 428.6697*Cos(L) +  596.1009*Sin(2*L) - 2.0898*Cos(2*L) + 4.4173*Sin(3*L) + 19.2776*Cos(3*L) - 12.7338*Sin(4*L)
      eot(daynum) = eotv / 3600 * 15 * zz
      epsilon  =   23.43954*zz
      lambda   = L/zz + 0.4277*Sin(L) + 1.8664*Cos(L) - 0.0180*Sin(2*L) + 0.0087*Cos(2*L)
      decl(daynum) = asin(Sin(lambda*zz) * Sin(epsilon))
      'In radians - doesn't work yet!!!!!
      'L=daynum*0.0172028 - 1.3195893
      'eot(daynum) = -0.0077857*Sin(L) - 0.0311737*Cos(L) + 0.0433497*Sin(2*L) - 0.000152*Cos(2*L) + 0.0003212*Sin(3*L) +0.0014019*Cos(3*L) - 0.000926*Sin(4*L)
      'lambda   = L + 0.0074648*Sin(L) + 0.0325748*Cos(L) - 0.0003142*Sin(2*L) + 0.0001518*Cos(2*L)
      'decl(daynum)= asin(Sin(lambda) * 0.0071400)
   Next daynum
End Sub

Function lookuplabel(ByVal Hournum, ByVal labeltype) As String
'This routine returns a printable string for the hour label label, depending on hour and notation chosen
   Dim labels As String
   If hournum < -11 Or hournum > 11 Or labeltype < 0 Or labeltype > 1 Then
        'shouldn't happen
         MsgBox("Debug 5")
         Stop
   End If
   labels="1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11" & " " & _ 
          "I II III IIII V VI VII VIII IX X XI XII I II III IIII V VI VII VIII IX X XI" & " "   'Must have trailing space
   Dim index, counter1, counter2
   index = labeltype*23+hournum+11+1
   counter2 = 1
   Do 
      'Skip a block of spaces
      counter1 = counter2
      While Mid(labels,counter1,1) = " "
         counter1 = counter1 + 1
      Wend
      counter2 = counter1
      'Skip a block of non-spaces
      While Mid(labels,counter2,1) <> " "
         counter2 = counter2 + 1
      Wend
      index = index - 1
   Loop Until index = 0
   lookuplabel=Mid(labels,counter1,counter2-counter1)
End Function