test_ephem.py

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

Download (3.62 KB)

 
1
#! /usr/bin/env python
2
# ==========================================================================
3
# Plot difference between GSkyDir.sun() 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 Sun position #
28
# ==================== #
29
def compute_difference(time):
30
    """
31
    """
32
    # Compute ephem position
33
    sun_ephem     = ephem.Sun(time.datetime())
34
    sun_ephem_ra  = sun_ephem.a_ra*gammalib.rad2deg
35
    sun_ephem_dec = sun_ephem.a_dec*gammalib.rad2deg
36

    
37
    # Compute ctools position
38
    sun_ctools=gammalib.GSkyDir()
39
    sun_ctools.sun(time)
40
    sun_ctools_ra  = sun_ctools.ra_deg()
41
    sun_ctools_dec = sun_ctools.dec_deg()
42
    if sun_ctools_ra < 0.0:
43
        sun_ctools_ra += 360.0
44

    
45
    # Compute difference
46
    ra_diff  = sun_ephem_ra  - sun_ctools_ra
47
    dec_diff = sun_ephem_dec - sun_ctools_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
    # Print results
54
    #print(sun_ephem_ra, sun_ephem_dec)
55
    #print(sun_ctools_ra, sun_ctools_dec)
56
    #print(ra_diff, dec_diff)
57

    
58
    # Return
59
    return ra_diff, dec_diff
60

    
61

    
62
# ============== #
63
# Get difference #
64
# ============== #
65
def get_difference(dt=50000.0, nt=31560):
66
    """
67
    """
68
    # Initialise lists
69
    list_mjd  = []
70
    list_dra  = []
71
    list_ddec = []
72

    
73
    # Set start time
74
    time = gammalib.GTime()
75
    time.utc('2000-01-01T00:00:00')
76
    print('Start time .: %s' % time.utc())
77

    
78
    # Loop over times
79
    for i in range(nt):
80

    
81
        # Compute difference
82
        ra_diff, dec_diff = compute_difference(time)
83

    
84
        # Print difference
85
        #print('%s (%f): %f %f' % (time.utc(), time.mjd(), ra_diff, dec_diff))
86

    
87
        # Append result to lists
88
        list_mjd.append(time.mjd())
89
        list_dra.append(ra_diff)
90
        list_ddec.append(dec_diff)
91

    
92
        # Increment time
93
        time += dt
94

    
95
    # Print stop time
96
    print('Stop time ..: %s' % time.utc())
97

    
98
    # Return
99
    return list_mjd, list_dra, list_ddec
100

    
101

    
102
# =============== #
103
# Plot difference #
104
# =============== #
105
def plot_difference(list_mjd, list_dra, list_ddec):
106
    """
107
    """
108
    # Create figure
109
    fig = plt.figure('Sun position difference', (12, 4))
110
    fig.subplots_adjust(left=0.12, right=0.98, top=0.94, bottom=0.14)
111
    ax = plt.subplot()
112

    
113
    # Plot the differeces
114
    ax.plot(list_mjd, list_dra,  'b-', label='Right Ascension')
115
    ax.plot(list_mjd, list_ddec, 'r-', label='Declination')
116

    
117
    # Set labels
118
    ax.set_xlabel('MJD (days)', fontsize=14)
119
    ax.set_ylabel('Difference (deg)', fontsize=14)
120

    
121
    # Add legend
122
    ax.legend()
123

    
124
    # Show figure
125
    plt.show()
126

    
127
    # Return
128
    return
129

    
130

    
131
# ======================== #
132
# Main routine entry point #
133
# ======================== #
134
if __name__ == '__main__':
135

    
136
    # Get difference
137
    list_mjd, list_dra, list_ddec = get_difference()
138

    
139
    # Plot difference
140
    plot_difference(list_mjd, list_dra, list_ddec)
141