How to verify results of Pyephem - position

I want to see "in my sky" how many satellites I can see.
I'm using this code:
sat_alt, sat_az, sat_name = [], [], []
observer = ephem.Observer()
observer.long = Longitude
observer.lat = Latitude
observer.date = TimeNow
HowMany = 0
print "TIME: ", TimeNow
print
GPS_list = 'http://www.celestrak.com/NORAD/elements/gps-ops.txt'
GPS2_list = 'http://www.tle.info/data/gps-ops.txt'
GLONASS_list = 'http://www.celestrak.com/NORAD/elements/glo-ops.txt'
GLONASS2_list = 'http://www.tle.info/data/glo-ops.txt'
#'http://www.amsat.org/amsat/ftp/keps/current/nasabare.txt').readlines()
tles = urllib2.urlopen(GPS_list).readlines()
tles = [item.strip() for item in tles]
tles = [(tles[i],tles[i+1],tles[i+2]) for i in xrange(0,len(tles)-2,3)]
for tle in tles:
try:
sat = ephem.readtle(tle[0], tle[1], tle[2])
rt, ra, tt, ta, st, sa = observer.next_pass(sat)
if rt is not None and st is not None:
sat.compute(observer)
if TimeNow >= ephem.localtime(st) and TimeNow <= ephem.localtime(rt):
text = tle[0]
sat_alt.append(np.rad2deg(sat.alt))
sat_az.append(np.rad2deg(sat.az))
text2 = text.rsplit(')', 1)[0]
NamePRN = text2.rsplit('(', 1)[1]
sat_name.append(NamePRN)
#I wanna Try just with the PRN 1
if 1 == 1: # NamePRN = 'PRN 01':
print NamePRN
print "Elev:" , np.rad2deg(sat.alt), "Azimuth: ",np.rad2deg(sat.az) #sat.alt / ephem.degree
print "original rise time: ",rt
print 'rise time: ', ephem.localtime(rt)
print 'set time: ', ephem.localtime(st)
print
print 'Time until rise: ', ephem.localtime(rt) - TimeNow
print 'Time until set: ',ephem.localtime(st) - TimeNow
timeuntilrise = ephem.localtime(rt)-TimeNow
HowMany += 1
minutesaway = timeuntilrise.seconds/60.0
if sys.platform == 'darwin':
say = 'say "%s WILL BE MAKING A PASS IN %d MINUTES."' % (NamePRN,minutesaway)
os.system(say)
else:
print "Minutes Away: ",minutesaway
#print ' Rise Azimuth: ', ra
#print ' Transit Time: ', tt
#print ' Transit Altitude: ', ta
#print ' Set Time: ', st
#print ' Set Azimuth: ', sa
#print 'rise time: ', ephem.localtime(rt)
#print 'set time: ', ephem.localtime(st)
#print
except ValueError as e:
print e
print
print "How Many: ", HowMany
The results are:
TIME: 2017-03-23 09:32:30.445831
PRN 14
Elev: 23.5719168541 Azimuth: 186.783257119
original rise time: 2017/3/23 18:15:23
rise time: 2017-03-23 13:15:23.000003
set time: 2017-03-23 05:56:40.000003
Time until rise: 3:42:52.554172
Time until set: -1 day, 20:24:09.554172
Minutes Away: 222.866666667
PRN 22
Elev: 27.8617383982 Azimuth: 135.529777134
original rise time: 2017/3/23 18:59:39
rise time: 2017-03-23 13:59:38.000003
set time: 2017-03-23 05:52:54.000003
Time until rise: 4:27:07.554172
Time until set: -1 day, 20:20:23.554172
Minutes Away: 267.116666667
PRN 19
Elev: 63.2536032619 Azimuth: 23.0164551349
original rise time: 2017/3/23 19:01:01
rise time: 2017-03-23 14:01:00.000003
set time: 2017-03-23 07:01:07.000003
Time until rise: 4:28:29.554172
Time until set: -1 day, 21:28:36.554172
Minutes Away: 268.483333333
PRN 23
Elev: 2.50815303871 Azimuth: 88.104092255
original rise time: 2017/3/23 21:33:06
rise time: 2017-03-23 16:33:06.000003
set time: 2017-03-23 08:35:45.000003
Time until rise: 7:00:35.554172
Time until set: -1 day, 23:03:14.554172
Minutes Away: 420.583333333
PRN 17
Elev: 43.5586196015 Azimuth: 52.03302655
original rise time: 2017/3/23 18:27:16
rise time: 2017-03-23 13:27:15.000003
set time: 2017-03-23 06:12:30.000003
Time until rise: 3:54:44.554172
Time until set: -1 day, 20:39:59.554172
Minutes Away: 234.733333333
PRN 31
Elev: 7.61244305169 Azimuth: 168.667710308
original rise time: 2017/3/23 19:47:23
rise time: 2017-03-23 14:47:23.000003
set time: 2017-03-23 08:03:23.000003
Time until rise: 5:14:52.554172
Time until set: -1 day, 22:30:52.554172
Minutes Away: 314.866666667
PRN 12
Elev: 48.18881167 Azimuth: 258.782917613
original rise time: 2017/3/23 18:42:38
rise time: 2017-03-23 13:42:38.000003
set time: 2017-03-23 07:35:56.000003
Time until rise: 4:10:07.554172
Time until set: -1 day, 22:03:25.554172
Minutes Away: 250.116666667
PRN 25
Elev: 18.8269690697 Azimuth: 222.58247788
original rise time: 2017/3/23 19:39:10
rise time: 2017-03-23 14:39:09.000003
set time: 2017-03-23 08:34:40.000003
Time until rise: 5:06:38.554172
Time until set: -1 day, 23:02:09.554172
Minutes Away: 306.633333333
PRN 01
Elev: 9.20983988345 Azimuth: 117.518172142
original rise time: 2017/3/23 17:53:54
rise time: 2017-03-23 12:53:54.000003
set time: 2017-03-23 04:56:06.000003
Time until rise: 3:21:23.554172
Time until set: -1 day, 19:23:35.554172
Minutes Away: 201.383333333
PRN 24
Elev: 15.4381621753 Azimuth: 284.908746358
original rise time: 2017/3/23 16:23:16
rise time: 2017-03-23 11:23:16.000003
set time: 2017-03-23 05:14:38.000003
Time until rise: 1:50:45.554172
Time until set: -1 day, 19:42:07.554172
Minutes Away: 110.75
PRN 06
Elev: 49.0638613556 Azimuth: 345.92883678
original rise time: 2017/3/23 20:57:05
rise time: 2017-03-23 15:57:04.000003
set time: 2017-03-23 08:25:55.000003
Time until rise: 6:24:33.554172
Time until set: -1 day, 22:53:24.554172
Minutes Away: 384.55
PRN 03
Elev: 41.016662249 Azimuth: 109.881781593
original rise time: 2017/3/23 19:45:53
rise time: 2017-03-23 14:45:53.000003
set time: 2017-03-23 06:43:35.000003
Time until rise: 5:13:22.554172
Time until set: -1 day, 21:11:04.554172
Minutes Away: 313.366666667
PRN 32
Elev: 13.6017761707 Azimuth: 207.711749038
original rise time: 2017/3/23 17:23:22
rise time: 2017-03-23 12:23:22.000003
set time: 2017-03-23 05:14:55.000003
Time until rise: 2:50:51.554172
Time until set: -1 day, 19:42:24.554172
Minutes Away: 170.85
How Many: 13
I'm checking the page "http://www.n2yo.com/" in the menu "Satellites in Orbit"->"GPS satellites" But How can I see if I'm right or not? another web page?
Thanks.

Calsky and n2yo works to see the azimuth and elevation from some location. in Calsky show for default, the satellites above 25º

For on-line tools, I really find in-the-sky.org useful and helpful. It's quite new, modern, and well-maintained by an active space expert and web developer. See this and this.
Another python check can be done with Skyfield. It is maintained by the same person who maintains PyEphem, but again is much newer and modernized. Skyfield is quite different than PyEphem on the inside, and they have different sets of capabilities. See all of these in stackoverflow, and then all of these in Space Exploration SE, and then all of these in Astronomy SE!

Related

Groovy not converting seconds to hours properly

I have an integer that is:
19045800
I tried different code:
def c = Calendar.instance
c.clear()
c.set(Calendar.SECOND, 19045800)
echo c.format('HH:mm:ss').toString()
String timestamp = new GregorianCalendar( 0, 0, 0, 0, 0, 19045800, 0 ).time.format( 'HH:mm:ss' )
echo timestamp
Both return 10:30:00
19045800 seconds is supposed to be over 5000 hours. What am I doing wrong?
I'm not sure what you are looking for. But if your requirement is to calculate the number of hours, minutes, and the remainder of seconds for given seconds following code will work.
def timeInSeconds = 19045800
int hours = timeInSeconds/3600
int minutes = (timeInSeconds%3600)/60
int seconds = ((timeInSeconds%3600)%60)
println("Hours: " + hours)
println("Minutes: " + minutes)
println("Seconds: " + seconds)
You're attempting to use a Calendar, but what you're actually discussing is what Java calls a Duration – a length of time in a particular measurement unit.
import java.time.*
def dur = Duration.ofSeconds(19045800)
def hours = dur.toHours()
def minutes = dur.minusHours(hours).toMinutes()
def seconds = dur.minusHours(hours).minusMinutes(minutes).toSeconds()
println "hours = ${hours}"
println "minutes = ${minutes}"
println "seconds = ${seconds}"
prints:
hours = 5290
minutes = 30
seconds = 0

How to delay orders in pine script by numbers of minutes in 1hr timeframe?

I'm trying to delay entry orders by minutes.
ex: At 12:00 pm the macd line crossover signal line, but I wanna start entry.order at 12:02 pm.
So I tried the following code
//#version=4
strategy("delay2",calc_on_every_tick=true)
i_qtyTimeUnits = input(2, "Quantity", minval = 0)
int _timeFrom_ = na
_timeFrom_ := i_qtyTimeUnits*60*1000
// Entry conditions.
fastLength = input(12)
slowlength = input(26)
signalLength = input(9)
MACD = sma(close, fastLength) - sma(close, slowlength)
signal = sma(MACD, signalLength)
delta = MACD - signal
bool goLong = delta>0
bool goShort = delta<0
float entrytime = na
float delaytime = na
entrytime := if goLong
nz(entrytime[1], time)
delaytime := if entrytime>0
nz(delaytime[1], entrytime+_timeFrom_)
delayElapsed = time>=delaytime
plot(entrytime)
plot(delaytime)
plot(time)
if goLong and delayElapsed
strategy.entry("Long", strategy.long, comment="Long")
strategy.close('short', when = goShort)
I've plotted and checked that delayElapsed is true, but it just doesn't work,please help.
I made a script that buys 2 minutes after a red candle has appeared. The buy is marked with an X.
There is a variable in pine script that is called time. The variable keeps track at the time where every candle is displayed. time is the number of milliseconds since 1 Jan 1970. For example, the current time when I'm writing this is 1625334502399.
To determine the time when you should buy, you can add the number of milliseconds you want to the time and store it in a variable.
entrytime := time + 2 * 60 * 1000
So 2 minutes is equal to 2 * 60 * 1000 milliseconds. There are 1000 milliseconds on a second and 60 seconds on a minute.
You should store this value in a var variable because that value doesn't get erased after every bar.
var float entrytime = na
You can change to any timeframe and it will still buy 2 min after.
Here is the full code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © CanYouCatchMe
//#version=4
study("buying after 2 min", overlay=true)
delay_minutes = input(defval = 2, title = "Delay minutes")
var float entrytime = na //is used to store the time when it should buy
if (open > close and na(entrytime)) //Red candle and there is no current "entrytime"
entrytime := time + delay_minutes * 60 * 1000 //"time" is the time in milliseconds where to candle appered. There is 1000 milliseconds on a second. And 60 seconds on a minute.
buy_condition = false
if (time >= entrytime) //Buys if the current time is greater or the same as the stored time.
entrytime := na //Sets entrytime to na so it stops buying.
buy_condition:=true
plotshape(buy_condition, style=shape.xcross, color=color.green, size=size.small) //Plotting an green "X" where it should buy
//Displaying the values "Data Window" on the right for Debugging.
plotchar(time, "time", "", location = location.top)
plotchar(entrytime, "entrytime", "", location = location.top)

creating a system that 'unlocks' when the timer reaches 0

I'm working on a script where there is this timer and a user enters a duration e.g 2days: 2hoursd : 2minutes: 2seconds and when the timer hits 0 the word "unlocked" appears
I am absolutely lost and have no clue where to begin
ide show some code but I'm honestly lost
the user enters a duration
timer ticks down
timer reaches 0
word "unlocked" appears
The start_timer function starts a blocking timer which sleeps for one second each, until the defined duration has been reached. Additionally, if you run the script as main, it asks for user input to set the timer.
import datetime
import time
def start_timer(days=0, hours=0, minutes=0, seconds=5):
t0 = datetime.datetime.now()
delta = datetime.timedelta(days=days, seconds=seconds, minutes=minutes,
hours=hours)
t1 = datetime.datetime.now()
while (t1 - t0) < delta:
time.sleep(1)
t1 = datetime.datetime.now()
print("Unlocked")
if __name__ == "__main__":
days = float(input("Days? "))
hours = float(input("Hours? "))
minutes = float(input("Minutes? "))
seconds = float(input("Seconds? "))
start_timer(days=days, hours=hours, minutes=minutes, seconds=seconds)

What is the impact of a function on execution speed?

Fairly new to python and working on an application where speed is critical - essentially in a race to book reservations online as close to 7AM as possible. Was trying to understand the impact on execution speed that a function call has, so i developed a very simple test: count to 1,000,000 and time start and end using both in line and a called function. However, the result i get says that the function takes about 56% of the time that the inline code takes. I must be really be missing something. Can anyone explain this please.
Code:
import time
def timeit():
temp = 0
while temp < 10000000:
temp += 1
return
time1 = time.time()
temp = 0
while temp < 10000000:
temp += 1
time2 = time.time()
timeit()
time3 = time.time()
temp = 0
while temp < 10000000:
temp += 1
time4 = time.time()
timeit()
time5 = time.time()
print("direct took " + "{:.16f}".format(time2 - time1) + " seconds")
print("indirect took " + "{:.16f}".format(time3 - time2) + " seconds")
print("2nd direct took " + "{:.16f}".format(time4 - time3) + " seconds")
print("2nd indirect took " + "{:.16f}".format(time5 - time4) + " seconds")
results:
direct took 1.6094279289245605 seconds
indirect took 0.9220039844512939 seconds
2nd direct took 1.5781939029693604 seconds
2nd indirect took 0.9375154972076416 seconds
I apologize for missing something silly that i must have done?

How to get a series of time ranges with an increment in Groovy

I'm trying to get a list of time series with increments of 15 minutes.
for example:
5:00 AM
5:15 AM
5:30 AM
....
...
.....
11:30 PM
Using Groovy's TimeCategory you can do time manipulations (like adding a number of minutes to a date) quite easily. A runnable example:
import groovy.time.TimeCategory
def start = Date.parseToStringDate('Sun Feb 24 05:00:00 GMT 2013')
use (TimeCategory) {
// An array of 10 Dates separated by a 15 minute time interval from 'start'.
def timeSeries = (0..9).collect { start + (it * 15).minutes }
// Formatted output.
println timeSeries.collect { it.format('KK:mm a') }.join('\n')
}
Output (assuming system timezone is GMT):
05:00 AM
05:15 AM
05:30 AM
05:45 AM
06:00 AM
06:15 AM
06:30 AM
06:45 AM
07:00 AM
07:15 AM
Update
To get the time series between a start and end date, a functional kind of approach similar to what we did before would be:
import groovy.time.TimeCategory
import groovy.time.TimeDuration
def createTimeSeries(Date start, Date end, TimeDuration interval) {
def step = interval.toMilliseconds() as int
(start.time..end.time).step(step).collect { new Date(it) }
}
// Usage
use (TimeCategory) {
def start = Date.parseToStringDate('Sun Feb 24 05:00:00 GMT 2013')
def end = Date.parseToStringDate('Sun Feb 24 06:00:00 GMT 2013')
def timeSeries = createTimeSeries(start, end, 15.minutes)
assert timeSeries.size() == 5 // 5:00, 5:15, 5:30, 5:45 and 6:00
}
I really really wish the Range object and its step method would be more polymorphic, so timeSeries = createTimeSeries(start, end, 15.minutes) could be replaced by simply timeSeries = (start..end).step(15.minutes) (after all start and end areDates and therefore are Comparable between each other, and 15.minutes, which is a TimeDuration, can be added to them); there'd be no need for the createTimeSeries function :)
However, do not use this implementation. For some reason the Range#step method is painfully slow on this case; it takes seconds to get the time series between a 1-day date difference on my laptop, even though they are just 97 elements :S
Here's a more imperative, but much faster, solution:
def createTimeSeries(Date start, Date end, TimeDuration interval) {
def timeSeries = []
while (start <= end) {
timeSeries << start
start += interval
}
timeSeries
}
// Usage is the same.
For example:
Date start = ....
int periods = 26
int incrementMinutes = 15
return (1..periods).collect {
new Date(start.time + TimeUnit.MINUTES.toMillis(it * incrementMinutes)
}

Resources