File: xearth.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (134 lines) | stat: -rw-r--r-- 4,404 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import math, time

def getLatLong():
    "Returns URL for day/night picture"
    # Define some 'constants'
    ClientRecieveTime=time.time() * 1000
    # QueryTimeZone = 10
    QueryTimeZone = -time.timezone/3600
    QueryTimeZoneOffsetMin = QueryTimeZone * 60
    NISTSendTimeGMTms = ClientRecieveTime - ( QueryTimeZoneOffsetMin * 60 * 1000 )

    # NIST start time in the query time zone
    QueryTimeZoneOffset = ( QueryTimeZoneOffsetMin * 60 * 1000 )
    # Replace this with "QueryTimeZoneOffset = ( -time.timezone * 60 * 1000 )"

    # Client start time in some time zone
    ClientRecieveTimems = ClientRecieveTime

    # what timezone does your computer think it is? - in minutes
    ClientTimeZone = QueryTimeZoneOffsetMin;
    ClientNISTDelta = math.floor(NISTSendTimeGMTms - ClientRecieveTimems)

    currTime = ClientRecieveTime + ClientNISTDelta # ThisMilliseconds
    gmtTime = time.gmtime()
    ssue = currTime / 1000
    TwoPi = 2 * math.pi
    EpochStart = 631065600
    DaysSinceEpoch = (ssue - EpochStart)/ (24*3600)
    RadsPerDay = TwoPi / 365.242191
    Epsilon_g = 279.403303 * (TwoPi / 360)
    OmegaBar_g = 282.768422 * (TwoPi / 360)
    Eccentricity = 0.016713
    MeanObliquity = 23.440592 * (TwoPi / 360);
    # Calculate sun_ecliptic_longitude
    N = RadsPerDay * DaysSinceEpoch
    N = N % TwoPi
    if N < 0:
        N += TwoPi # This should never be executed, but never mind
    M_Sun = N + Epsilon_g - OmegaBar_g
    if M_Sun < 0:
        M_Sun += TwoPi # This should never be executed either
    # Now we solve keplers equation. For those who are interested keplers 
    # equation is all about plotting the orbit of an object on the 
    # elliptic plane.
    E = M_Sun
    while 1:
        delta = E - (Eccentricity*math.sin(E)) - M_Sun
        if (abs(delta) <= 1E-10):
            break
        E -= delta / (1 - (Eccentricity*math.cos(E)))
    # End of the keplers equation solution
    myLambda = OmegaBar_g + (2 * math.atan(math.sqrt((1+Eccentricity) / (1-Eccentricity)) * math.tan(E/2)))
    # There, finished calculating the sun ecliptic longitude
    # Now we calculate the ecliptic to equatorial (something or other)
    sin_e = math.sin(MeanObliquity)
    cos_e = math.cos(MeanObliquity)
    alpha = math.atan2(math.sin(myLambda)*cos_e, math.cos(myLambda))
    delta = math.asin(sin_e*math.sin(myLambda));
    # End of ecliptic to equatorial
    # We calculate the Julian date here, Python could probably do this better
    # I leave it to the casual observer to replace the following few lines
    y = gmtTime[0] # Year
    m = gmtTime[1] # Month number
    z = gmtTime[2] # Day number
    A = y / 100
    B = 2 - A + (A/4)
    C = 365.25 * y
    D = 30.6001 * (m+1)
    JD = B + C + D + z + 1720994.5
    T = (JD - 2451545) / 36525
    T0 = ((((T + 2.5862E-5) * T) + 2400.051336) * T) + 6.697374558
    T0 = T0 % 24
    if T0 < 0:
        T0 += 24
    UT = (float(gmtTime[3])) + ((float(gmtTime[4]) + (float(gmtTime[5]) / 60)) / 60)
    T0 += UT * 1.002737909
    T0 = T0 % 24
    if T0 < 0:
        T0 += 24

    tmp = alpha - ((TwoPi/24)*T0);
    while tmp < -math.pi:
        tmp += TwoPi
    while tmp > math.pi:
        tmp -= TwoPi

    # Now calculate our longitude and latitude
    lon = tmp * (360/TwoPi)
    lat = delta * (360/TwoPi)

    # Generate the path of the appropriate xearth image
    lon = round(lon)
    
    if (lon % 2 != 0):
      if (lon > 0):
        lon -= 1
      else:
        lon += 1

    lon = round(lon/2) * 2
    if lon <= -181:
        lon = -180
    if lon >= 181:
        lon = 180
    # lat is odd
    lat = round(lat)
    if (lat % 2 == 0):
        if lat > 0:
            lat -= 1
        else:
            lat += 1
    # Need to do different calculations for negative and positive values of lat
    # to emulate the way javascript handles rounding
    if lat < 0:
        lat = (round(int(lat/2) - 1) * 2) + 1
    else:
        lat = (round(lat/2 - 1) * 2) + 1
  
    if lat <= -24:
        lat = -23
    if lat >= 24:
        lat = 23

    if lat < 0:
        latStr = str(int(-lat)) + "S"
    else:
        latStr = str(int(lat)) + "N"
    if lon < 0:
        lonStr = str(int(-lon)) + "S"
    else:
        lonStr = str(int(lon)) + "N"
    # url = "http://www.time.gov/" + getLatLong()
    # return 'http://www.time.gov/images/xearths/11N/154N.jpg'
    return "images/xearths/" + latStr + "/" + lonStr + ".jpg"