Blame view

src/core/celme/targets.py 14.2 KB
1cffbf1c   Etienne Pallier   moved pyros_djang...
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#from math import pi, sin, cos, fmod, tan, atan, fabs, atan2, asin, acos, sqrt
import doctest
from .angles import Angle
from .dates import Date
from .coords import Coords
from .mechanics import Mechanics
#from celme.site import Site

# ========================================================
# ========================================================
# === TARGETS
# ========================================================
# ========================================================

class Target(Mechanics):
    """ Class to compute planets for astronomy
    """
# ========================================================
# === attributs
# ========================================================

    # analytical fit methods
    _TYPE_UNKNOWN = 0
    _TYPE_PLANET = 1
    _TYPE_STAR = 2
    _TYPE_COORD_RADEC = 3
    _TYPE_COORD_HADEC = 4
    _TYPE_COORD_ALTAZ = 5
    
    _target_input = dict()
    _target_type = _TYPE_UNKNOWN

# ========================================================
# === internal methods : Generals
# ========================================================

    def _init_target(self,*args,**kwargs):
        """ Object initialization
        
        :param planet_name: A string (cf. help(PLanet))
        :type planet_name: string
        
        skyobj= {'planet':'Sun'}
        target.define(skyobj)
        or
        name = 'Sun'
        target.define(planet=name)
        
        """
        #print("1.*args={}".format(args))
        #print("1.**kwargs={}".format(kwargs))
        if (len(kwargs)==0):
            if (len(args)==0):
                dics = {'planet':'Sun'}
                self._define_resolver(*args,**dics)
        else:
            self._define_resolver(*args,**kwargs)
        return
        
    def _define_resolver(self,*args,**kwargs):
        """ Resolve the definition of the target
        
        :param **kwargs: keys:values of parameter
        :type **kwargs: dict
        """
        #print("2.*args={}".format(args))
        #print("2.**kwargs={}".format(kwargs))
        # ========= ?
        if (len(kwargs)==0):
            if (len(args)==0):
                return self._target_type, self._target_input
            elif (args[0]=="?"):
                res = "Dictionary keys: "
                res += "planet, ra, dec, equinox, epoch, mura, mudec, plx"
                return res
            else:
                kwargs = args[0]
        #print("3.*args={}".format(args))
        #print("3.**kwargs={}".format(kwargs))
        # ========= change keys into upper strings
        self._target_type = self._TYPE_UNKNOWN
        self._target_input = dict()
        dics = dict()
        for k,v in kwargs.items():
            ku = str(k).upper()
            if (type(v) is str):
                v = v.strip()
            dic = { ku : v }
            dics.update(dic)
        #print("dics={}".format(dics))
        # ========= name
        xxx_names = ['NAME'] ; # string of the name
        for xxx_name in xxx_names:
            if dics.get(xxx_name,'None')!='None':
                dic = { 'NAME' : dics[xxx_name] }
                self._target_input.update(dic)
                break
        # ========= search for a planet
        if dics.get('STAR','None')!='None':
            star_name = (dics['STAR']).strip()
            sn = (star_name).upper()
            if (sn == "SUN"):
                self._target_type = self._TYPE_PLANET
                dic = { 'PLANET' : sn }                    
                self._target_input.update(dic)
            else:
                self._target_type = self._TYPE_STAR
                dic = { 'STAR' : sn }                    
                self._target_input.update(dic)
        if (self._target_type != self._TYPE_UNKNOWN):
            return self._target_type
        # ========= search for a planet
        planet_names = {'SUN':'Sun', 'MER':'Mercury', 'VEN':'Venus', 'MAR':'Mars', 'JUP':'Jupiter', 'SAT':'Saturn', 'URA':'Uranus', 'NEP':'Neptune', 'PLU':'Pluto', 'MOO':'Moon', 'ELP':'ELP-Moon'}
        if dics.get('PLANET','None')!='None':
            planet_name = (dics['PLANET']).strip()
            if len(planet_name)>=3:
                p = (planet_name[0:3]).upper()
                if (p in planet_names):
                    self._target_type = self._TYPE_PLANET
                    dic = { 'PLANET' : planet_names[p] }                    
                    self._target_input.update(dic)
        if (self._target_type != self._TYPE_UNKNOWN):
            return self._target_type
        # ========= search for coords ra,dec
        dic0s = dict()
        ra_names = ['RA','R.A','R.A.','RIGHT ASCENSION']
        for ra_name in ra_names:            
            if dics.get(ra_name,'None')!='None':
                dic = { 'RA' : dics[ra_name] }
                dic0s.update(dic)
                break
        dec_names = ['DEC','DEC.','DECL.','DECLINATION']
        for dec_name in dec_names:            
            if dics.get(dec_name,'None')!='None':
                dic = { 'DEC' : dics[dec_name] }
                dic0s.update(dic)
                break
        equ_names = ['EQUINOX','EQU']
        for equ_name in equ_names:            
            if dics.get(equ_name,'None')!='None':
                dic = { 'EQUINOX' : dics[equ_name] }
                dic0s.update(dic)
                break
        epo_names = ['EPOCH','EP','EPO']
        for epo_name in epo_names:            
            if dics.get(epo_name,'None')!='None':
                dic = { 'EPOCH' : dics[epo_name] }
                dic0s.update(dic)
                break
        xxx_names = ['MURA','MU RA','MU R.A','MU R.A.'] ; # mas/yr
        for xxx_name in xxx_names: 
            if dics.get(xxx_name,'None')!='None':
                dic = { 'MURA' : dics[xxx_name] }
                dic0s.update(dic)
                break
        xxx_names = ['MUDEC','MU DEC','MU DEC.','MU DECL.'] ; # mas/yr
        for xxx_name in xxx_names: 
            if dics.get(xxx_name,'None')!='None':
                dic = { 'MUDEC' : dics[xxx_name] }
                dic0s.update(dic)
                break
        xxx_names = ['PLX','PARALLAX','PI'] ; # mas
        for xxx_name in xxx_names:
            if dics.get(xxx_name,'None')!='None':
                dic = { 'PLX' : dics[xxx_name] }
                dic0s.update(dic)
                break
        if (dic0s.get('RA','None')!='None') and (dic0s.get('DEC','None')!='None'):
            self._target_type = self._TYPE_COORD_RADEC
            if (dic0s.get('EQUINOX','None')=='None'):
                dic = { 'EQUINOX' : 'J2000' }
                dic0s.update(dic)
            if (dic0s.get('EPOCH','None')=='None'):
                dic = { 'EPOCH' : 'J2000' }
                dic0s.update(dic)
            if (dic0s.get('MURA','None')=='None'):
                dic = { 'MURA' : 0 }
                dic0s.update(dic)
            if (dic0s.get('MUDEC','None')=='None'):
                dic = { 'MUDEC' : 0 }
                dic0s.update(dic)
            if (dic0s.get('PLX','None')=='None'):
                dic = { 'PLX' : 0 }
                dic0s.update(dic)
            self._target_input.update(dic0s)
        if (self._target_type != self._TYPE_UNKNOWN):
            return self._target_type
        # ========= coords ha,dec
        dic0s = dict()
        xxx_names = ['HA','H.A','H.A.','HOUR ANGLE']
        for xxx_name in xxx_names:
            if dics.get(xxx_name,'None')!='None':
                dic = { 'HA' : dics[xxx_name] }
                dic0s.update(dic)
                break
        dec_names = ['DEC','DEC.','DECL.','DECLINATION']
        for dec_name in dec_names:            
            if dics.get(dec_name,'None')!='None':
                dic = { 'DEC' : dics[dec_name] }
                dic0s.update(dic)
                break
        if (dic0s.get('HA','None')!='None') and (dic0s.get('DEC','None')!='None'):
            self._target_type = self._TYPE_COORD_HADEC
            self._target_input.update(dic0s)
        if (self._target_type != self._TYPE_UNKNOWN):
            return self._target_type
        # ========= coords alt, az
        dic0s = dict()
        xxx_names = ['AZ','AZIMUT','AZIMUTH','AZIMUTAL']
        for xxx_name in xxx_names:
            if dics.get(xxx_name,'None')!='None':
                dic = { 'AZ' : dics[xxx_name] }
                dic0s.update(dic)
                break
        xxx_names = ['ELEV','ELEVATION','ELEV.','ALTITUDE','ALT','ALT.']
        for xxx_name in xxx_names:            
            if dics.get(xxx_name,'None')!='None':
                dic = { 'ELEV' : dics[xxx_name] }
                dic0s.update(dic)
                break
        if (dic0s.get('AZ','None')!='None') and (dic0s.get('ELEV','None')!='None'):
            self._target_type = self._TYPE_COORD_ALTAZ
            self._target_input.update(dic0s)
        if (self._target_type != self._TYPE_UNKNOWN):
            return self._target_type
        # ================================================
        #print("self._target_type={}".format(self._target_type))
        #print("self._target_input={}".format(self._target_input))
        # ================================================
        return self._target_type, self._target_input
    
# ========================================================
# === internal methods : 
# ========================================================
 
        
# ========================================================
# === target methods
# ========================================================

    def get(self):
        return self._target_type, self._target_input

    def ephem(self, date_utc, site, outputs="", options=""):
        
        if (self._target_type == self._TYPE_UNKNOWN):
            raise Exception
            return ""

        # === PLANETS
        if (self._target_type == self._TYPE_PLANET):
            planet_name = self._target_input['PLANET']
            # --- get outputs
            if (outputs==""):
                outputs = ['ra','dec','equinox']
            # --- get options
            astrometric = 1
            equinox = Date("J2000").jd()
            if isinstance(options, dict):
                # --- change keys into upper strings
                self._target_type = self._TYPE_UNKNOWN
                self._target_input = dict()
                dics = dict()
                for k,v in options.items():
                    ku = str(k).upper()
                    if (type(v) is str):
                        v = v.strip()
                    dic = { ku : v }
                    dics.update(dic)
                # --- possible options are astrometric and equinox
                if dics.get('ASTROMETRIC','None')!='None':
                    astrometric = int(options['ASTROMETRIC'])
                if dics.get('EQUINOX','None')!='None':
                    equinox = Date(options['EQUINOX']).jd()
            # --- ephemeris for the given equinox
            longmpc, rhocosphip, rhosinphip = site.lrr
            results = self._mc_ephem_planet(planet_name, date_utc, longmpc, rhocosphip, rhosinphip, astrometric, equinox, outputs)
        # === COORD_RADEC
        if (self._target_type == self._TYPE_COORD_RADEC):
            longmpc, rhocosphip, rhosinphip = site.lrr
            tk = site.temperature
            ppa = site.pressure
            hump = site.humidity
            lnm = 550 ; # lambda (nm)
            ra=0; dec=0; equinox=Date("J2000").jd(); epoch=Date("J2000").jd(); mura=0; mudec=0; plx=0
            ra = self._target_input['RA']
            ra = Angle(ra).deg()
            dec = self._target_input['DEC']
            dec = Angle(dec).deg()
            #print("-10. ra={} dec={}".format(ra,dec))
            equinox= Date(self._target_input['EQUINOX']).jd()
            epoch= Date(self._target_input['EPOCH']).jd()
            mura = self._target_input['MURA']; # mas/yr
            mudec = self._target_input['MUDEC']; # mas/yr
            plx = self._target_input['PLX']; # mas
            results = self._mc_radec2app(date_utc, longmpc, rhocosphip, rhosinphip, ra, dec, equinox, epoch, mura, mudec, plx,  tk,ppa,hump,lnm, outputs)
        return results
    
    def define(self, *args, **kwargs):
        return self._define_resolver(*args, **kwargs)
    
# ========================================================
# === get/set methods
# ========================================================

    
# ========================================================
# === debug methods
# ========================================================
    
    def infos(self, action) -> None:
        """ To get informations about this class
        
        :param action: A command to run a debug action (see examples).
        :type action: string
        
        :Example:
            
        Target().infos("doctest")
        Target().infos("doc_methods")
        Target().infos("internal_attributes")
        Target().infos("public_methods")        
        """
        if (action == "doc_methods"):
            publics = [x for x in dir(self) if x[0]!="_"]
            for public in publics:
                varname = "{}".format(public)
                if (callable(getattr(self,varname))==True):
                    print("\n{:=^40}".format(" method "+varname+" "))
                    t = "Target()."+varname+".__doc__"
                    tt =eval(t)
                    print(tt)
        if (action == "doctest"):
            if __name__ == "__main__":
                print("\n{:~^40}".format("doctest"))
                doctest.testmod(verbose=False)
        if (action == "internal_attributes"):
            internals = [x for x in dir(self) if x[0]=="_" and x[1]!="_"]
            for internal in internals:
                varname = "{}".format(internal)
                #if (hasattr(self,varname)==True):
                if (callable(getattr(self,varname))==False):
                    print(varname + "=" + str(getattr(self,varname)))
        if (action == "public_methods"):
            publics = [x for x in dir(self) if x[0]!="_"]
            for public in publics:
                varname = "{}".format(public)
                if (callable(getattr(self,varname))==True):
                    print(varname)

# ========================================================
# === special methods
# ========================================================
        
    def __init__(self,*args,**kwargs):
        """ Object initialization
        """
        self._init_target(*args,**kwargs)
		  # super().__init__()