How to resolve the algorithm Horizontal sundial calculations step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Horizontal sundial calculations step by step in the PowerShell programming language

Table of Contents

Problem Statement

Create a program that calculates the hour, sun hour angle, dial hour line angle from 6am to 6pm for an operator entered location.

For example, the user is prompted for a location and inputs the latitude and longitude 4°57′S 150°30′W (4.95°S 150.5°W of Jules Verne's Lincoln Island, aka Ernest Legouve Reef), with a legal meridian of 150°W. (Note: the "meridian" is approximately the same concept as the "longitude" - the distinction is that the meridian is used to determine when it is "noon" for official purposes. This will typically be slightly different from when the sun appears at its highest location, because of the structure of time zones. For most, but not all, time zones (hour wide zones with hour zero centred on Greenwich), the legal meridian will be an even multiple of 15 degrees.) Wikipedia: A sundial is a device that measures time by the position of the Sun. In common designs such as the horizontal sundial, the sun casts a shadow from its style (also called its Gnomon, a thin rod or a sharp, straight edge) onto a flat surface marked with lines indicating the hours of the day (also called the dial face or dial plate). As the sun moves across the sky, the shadow-edge progressively aligns with different hour-lines on the plate. Such designs rely on the style being aligned with the axis of the Earth's rotation. Hence, if such a sundial is to tell the correct time, the style must point towards true north (not the north or south magnetic pole) and the style's angle with horizontal must equal the sundial's geographical latitude.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Horizontal sundial calculations step by step in the PowerShell programming language

Source code in the powershell programming language

function Get-Sundial
{
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateRange(-90,90)]
        [double]
        $Latitude,


        [Parameter(Mandatory=$true)]
        [ValidateRange(-180,180)]
        [double]
        $Longitude,


        [Parameter(Mandatory=$true)]
        [ValidateRange(-180,180)]
        [double]
        $Meridian
    )

    [double]$sinLat = [Math]::Sin($Latitude*2*[Math]::PI/360)

    $object = [PSCustomObject]@{
        "Sine of Latitude"     = [Math]::Round($sinLat,3)
        "Longitude Difference" = $Longitude - $Meridian
    }
    
    [int[]]$hours = -6..6

    $hoursArray = foreach ($hour in $hours)
    {
        [double]$hra = (15 * $hour) - ($Longitude - $Meridian)
        [double]$hla = [Math]::Atan($sinLat*[Math]::Tan($hra*2*[Math]::PI/360))*360/(2*[Math]::PI)
        [PSCustomObject]@{
            "Hour"                 = "{0,8}" -f ((Get-Date -Hour ($hour + 12) -Minute 0).ToString("t"))
            "Sun Hour Angle"       = [Math]::Round($hra,3)
            "Dial Hour Line Angle" = [Math]::Round($hla,3)
        }
    }

    $object | Add-Member -MemberType NoteProperty -Name Hours -Value $hoursArray -PassThru
}

$sundial = Get-Sundial -Latitude -4.95 -Longitude -150.5 -Meridian -150
$sundial | Select-Object -Property "Sine of Latitude", "Longitude Difference" | Format-List
$sundial.Hours | Format-Table -AutoSize


  

You may also check:How to resolve the algorithm Ackermann function step by step in the Batch File programming language
You may also check:How to resolve the algorithm 24 game step by step in the 11l programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm LZW compression step by step in the C# programming language
You may also check:How to resolve the algorithm Empty directory step by step in the AWK programming language