Its been a while since my last post. In that time I have been busy with life stuff.
Here is a script I wrote to convert the current time in to sidereal time (GM sidereal time and also local sidereal time)
I have modified it in to functions so its a little more user friendly to non scripters.
Its has 4 functions
1 convert the time and location data in to sidereal time
2 ask the user to enter a location or use the current location of there geolocated IP
3 ask the user to enter a date time or use the current date/time
4 a nice function a human can run
Running this script will load all the functions in to memory and run the very last command to call them.
I have put in some comments with different uses for this command. Just uncomment (remove the “#”) out the like you want to run.
### start of functions ###
# this function will get current location and time or ask for them, then convert the sidereal time (both local and Greenwhich Mean)
# it is a wraper for the following 3 functions in this script
# usage
# Get-SiderealTime -- will get the current location and time and convert to sidereal time
# Get-SiderealTime -AskLocation "no" -AskTime "no" -- will get the current location and time and convert to sidereal time
# Get-SiderealTime -AskLocation "no" -AskTime "yes" -- will get the current location and prompt for time. then convert to sidereal time
# Get-SiderealTime -AskLocation "yes" -AskTime "yes" -- will promt for the current location time. then convert to sidereal time
# the function will write to the screen, it currenlty has no output
function Get-SiderealTime
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)] $AskLocation = "no",
[Parameter(Mandatory = $false)] $AskTime = "no"
)
$LocationObject = Get-UserLocation -AskLocation $AskLocation
$DateTimeObject = Get-UserTime -AskTime $AskTime
Get-SiderealDateTime -LocationObj $LocationObject -DateTimeObj $DateTimeObject
}
# will use the current external IP address found by requesting it form ifconfig.me and send it to "http://ip-api.com/json/" to get giolocated
# will not work if the user is using a VPN
# usage
# Get-UserLocation -asklocation "yes" -- will prompt for Latitude and Longitude
# Get-UserLocation -asklocation "No" -- (default) will giolocate external IP
function Get-UserLocation
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)] $AskLocation = "no"
)
if ($AskLocation -eq "no")
{
$LocationData = Invoke-RestMethod -Method Get -Uri ("http://ip-api.com/json/" + (curl ifconfig.me).content )
}else{
$LocationData = '' | select lat,lon,status,city,regionName
$LocationData.city = "NA"
$LocationData.regionName = "NA"
write-host "Enter Latitude in degrees (eg Latitude of Brighton is 50.8229)"
[Decimal]$LocationData.lat = read-host "Latitude"
write-host "Enter Longitude in degrees (eg Longitude of Brighton is -0.1363)"
[Decimal]$LocationData.lon = read-host "Longitude"
if ($LocationData.lat -and $LocationData.lon){$LocationData.status = "success"}
}
if ($LocationData.status -eq "success")
{
write-host (" " + $LocationData.city + " , " + $LocationData.regionName) -ForegroundColor Green
write-host (" Long " + $LocationData.lon + " , Lat " + $LocationData.lat) -ForegroundColor Green
write-host " =============================== " -ForegroundColor Green
}
return $LocationData
}
# will use get the current date and time unless instrusted to promp for a date and time
# usage
# Get-UserTime -AskTime "yes" -- will prompt for date time in the following format 17/07/2017 09:00:00
# Get-UserTime -AskTime "No" -- (default) will get current date and time
# Get-UserTime -AskTime "any" -DateTimeObj (get-date) -- will check and return the datetime object, or the current date time if its invalid
function Get-UserTime
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)] $AskTime = "no",
[Parameter(Mandatory = $false)] $DateTimeObj
)
if($DateTimeObj)
{
if ($DateTimeObj.gettype() = "DataTime")
{
return $DateTimeObj
}else{
# not vailid, get getting current date time
return get-date
}
}
if ($AskTime -eq "no")
{
return get-date
}else{
write-host "Enter the date and time in the following format"
$StartDate = Get-Date (Read-Host -Prompt 'Enter the date and time: 17/07/2017 or 17/07/2017 09:00:00')
if ($StartDate.gettype().name -eq "datetime"){return $StartDate}
}
return get-date
}
# will take the location, datetime objects and output the Greenwhich and local sidereal time
# formula from https://aa.usno.navy.mil/faq/GAST
# some code ideas from https://github.com/jhaupt/Sidereal-Time-Calculator/blob/master/SiderealTimeCalculator.py
function Get-SiderealDateTime
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)] $LocationObj,
[Parameter(Mandatory = $true)] $DateTimeObj
)
$Datenow = $DateTimeObj
if($Datenow.IsDaylightSavingTime()){$DateNow = $DateNow.addhours(-1)}
$LocationData = $LocationObj
# split TD into individual variables for month, day, etc. and convert to floatingPoint
[float]$Month = $DateNow.month
[float]$Day = $DateNow.day
[float]$Year = $DateNow.year
[float]$Hour = $DateNow.hour
[float]$Min = $DateNow.Minute
# convert mm to fractional time:
[float]$Min = $Min/60
# reformat UTC time as fractional hours:
[float]$UT = $Hour + $Min
# get the Julian date:
$JD = (367 * $Year) - [Math]::Truncate(( 7 * ($Year + [Math]::Truncate(($Month + 9) / 12) )) / 4) + [Math]::Truncate((275 * $Month) / 9) + $Day + 1721013.5 + ( $UT / 24)
write-host ("Julian date " + $JD) -ForegroundColor Green
write-host ("Date / time " + $DateNow) -ForegroundColor Green
# get the Greenwhich mean sidereal time:
$GMST = 18.697374558 + 24.06570982441908 * ($JD - 2451545)
$GMST = $GMST % 24 #use modulo operator to convert to 24 hours
$GMSTmm = ($GMST - [Math]::Truncate($GMST)) * 60 #convert fraction hours to minutes
$GMSTss = ($GMSTmm - [Math]::Truncate($GMSTmm)) * 60 #convert fractional minutes to seconds
$GMSThh = [Math]::Truncate($GMST)
$GMSTmm = [Math]::Truncate($GMSTmm)
$GMSTss = [Math]::Truncate($GMSTss)
write-host ("Greenwhich Mean Sidereal Time: " + $GMSThh + ":" + $GMSTmm + ":" + $GMSTss) -ForegroundColor Green
# Convert to the local sidereal time by adding the longitude (in hours) from the GMST.
# (Hours = Degrees/15, Degrees = Hours*15)
$Long = $LocationData.lon / 15 #Convert longitude to hours
$LST = $GMST + $Long #Fraction LST. If negative add 24
if ($LST -lt 0)
{$LST = $LST +24}
$LSTmm = ($LST - [Math]::Truncate($LST)) * 60 #convert fraction hours to minutes
$LSTss = ($LSTmm - [Math]::Truncate($LSTmm )) * 60 #convert fractional minutes to seconds
$LSThh = [Math]::Truncate($LST)
$LSTmm = [Math]::Truncate($LSTmm)
$LSTss = [Math]::Truncate($LSTss)
write-host (" Local Sidereal Time: " + $LSThh + ":" + $LSTmm + ":" + $LSTss) -ForegroundColor Green
}
### end of functions ###
#Get-SiderealTime # -- will get the current location and time and convert to sidereal time
#Get-SiderealTime -AskLocation "no" -AskTime "no" # -- will get the current location and time and convert to sidereal time
#Get-SiderealTime -AskLocation "no" -AskTime "yes" # -- will get the current location and prompt for time. then convert to sidereal time
#Get-SiderealTime -AskLocation "yes" -AskTime "yes" # -- will prompt for the current location time. then convert to sidereal time
Get-SiderealTime