test_moon_ephem.py

Knödlseder Jürgen, 11/14/2019 12:05 AM

Download (3.55 KB)

 
1
#! /usr/bin/env python
2
# ==========================================================================
3
# Plot difference between GSkyDir.moon() results and PyEphem results
4
#
5
# Copyright (C) 2019 Juergen Knoedlseder
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
#
20
# ==========================================================================
21
import gammalib
22
import ephem
23
import matplotlib.pyplot as plt
24

    
25

    
26
# ===================== #
27
# Compute Moon position #
28
# ===================== #
29
def compute_difference(time):
30
    """
31
    """
32
    # Compute ephem position
33
    moon_ephem     = ephem.Moon(time.datetime())
34
    moon_ephem_ra  = moon_ephem.a_ra  * gammalib.rad2deg
35
    moon_ephem_dec = moon_ephem.a_dec * gammalib.rad2deg
36

    
37
    # Compute gammalibs position
38
    moon_gammalibs = gammalib.GSkyDir()
39
    moon_gammalibs.moon(time)
40
    moon_gammalibs_ra  = moon_gammalibs.ra_deg()
41
    moon_gammalibs_dec = moon_gammalibs.dec_deg()
42
    if moon_gammalibs_ra < 0.0:
43
        moon_gammalibs_ra += 360.0
44

    
45
    # Compute difference
46
    ra_diff  = moon_ephem_ra  - moon_gammalibs_ra
47
    dec_diff = moon_ephem_dec - moon_gammalibs_dec
48
    if ra_diff > 180.0:
49
        ra_diff -= 360.0
50
    if ra_diff < -180.0:
51
        ra_diff += 360.0
52

    
53
    # Return
54
    return ra_diff, dec_diff
55

    
56

    
57
# ============== #
58
# Get difference #
59
# ============== #
60
def get_difference(dt=50000.0, nt=31560):
61
    """
62
    """
63
    # Initialise lists
64
    list_mjd  = []
65
    list_dra  = []
66
    list_ddec = []
67

    
68
    # Set start time
69
    time = gammalib.GTime()
70
    time.utc('2000-01-01T00:00:00')
71
    print('Start time .: %s' % time.utc())
72

    
73
    # Loop over times
74
    for i in range(nt):
75

    
76
        # Compute difference
77
        ra_diff, dec_diff = compute_difference(time)
78

    
79
        # Print difference
80
        #print('%s (%f): %f %f' % (time.utc(), time.mjd(), ra_diff, dec_diff))
81

    
82
        # Append result to lists
83
        list_mjd.append(time.mjd())
84
        list_dra.append(ra_diff)
85
        list_ddec.append(dec_diff)
86

    
87
        # Increment time
88
        time += dt
89

    
90
    # Print stop time
91
    print('Stop time ..: %s' % time.utc())
92

    
93
    # Return
94
    return list_mjd, list_dra, list_ddec
95

    
96

    
97
# =============== #
98
# Plot difference #
99
# =============== #
100
def plot_difference(list_mjd, list_dra, list_ddec):
101
    """
102
    """
103
    # Create figure
104
    fig = plt.figure('Moon position difference', (12, 4))
105
    fig.subplots_adjust(left=0.12, right=0.98, top=0.94, bottom=0.14)
106
    ax = plt.subplot()
107

    
108
    # Plot the differeces
109
    ax.plot(list_mjd, list_dra,  'b-', label='Right Ascension')
110
    ax.plot(list_mjd, list_ddec, 'r-', label='Declination')
111

    
112
    # Set labels
113
    ax.set_xlabel('MJD (days)', fontsize=14)
114
    ax.set_ylabel('Difference (deg)', fontsize=14)
115

    
116
    # Add legend
117
    ax.legend()
118

    
119
    # Show figure
120
    plt.show()
121

    
122
    # Return
123
    return
124

    
125

    
126
# ======================== #
127
# Main routine entry point #
128
# ======================== #
129
if __name__ == '__main__':
130

    
131
    # Get difference
132
    list_mjd, list_dra, list_ddec = get_difference()
133

    
134
    # Plot difference
135
    plot_difference(list_mjd, list_dra, list_ddec)
136