1 """
2 Retrieve CoRoT data from a local data repository.
3
4 check out http://nsted.ipac.caltech.edu/
5 """
6
7 import logging
8 import os
9 import urllib
10 from astropy.io import fits as pyfits
11 import glob
12 import numpy as np
13 from cc.ivs.aux import loggers
14 from cc.ivs.catalogs import sesame
15 from cc.ivs.catalogs import vizier
16 from cc.ivs.io import fits
17 import cc.path
18
19
20 logger = logging.getLogger("CAT.COROT")
21 logger.addHandler(loggers.NullHandler())
22
24 """
25 Retrieve CoRoT timeseries from a local data repository.
26
27 The output record array has fields 'HJD', 'flux', 'e_flux', 'flag'.
28
29 @param ID: ID of the target: either an integer (CoRoT ID), an SIMBAD-recognised
30 target name, or a valid CoRoT FITS file
31 @type ID: int or str
32 @return: data, header
33 @rtype: numpy recarray, dict
34 """
35
36
37 data = []
38
39 if isinstance(ID,str) and os.path.isfile(ID):
40 header = pyfits.getheader(ID)
41 times,flux,error,flags = fits.read_corot(ID)
42 data.append([times,flux,error,flags])
43 else:
44
45 try:
46 ID = int(ID)
47 except ValueError:
48 info = sesame.search(ID,db='S')
49 IDs = [alias for alias in info['alias'] if 'HD' in alias]
50 if len(IDs)!=1:
51 logger.error("Data retrieval for %s not possible. Reason: no HD number resolved" % (ID))
52 return
53 ID = IDs[0]
54
55
56 fn_sismo = os.path.join(cc.path.ivsdata,'catalogs','corot','sismo','*.fits')
57 catfiles = glob.glob(fn_sismo)
58 catfiles.sort()
59 for catfile in catfiles:
60 try:
61 header = pyfits.getheader(catfile)
62 except IOError:
63 continue
64 if header['starname']==ID or header['corotid'].replace(' ','')=='%s'%(ID):
65 times,flux,error,flags = fits.read_corot(catfile)
66 data.append([times,flux,error,flags])
67
68 if not data:
69 raise ValueError('target {0} not in offline CoRoT data repository'.format(ID))
70 data = np.hstack(data)
71 data = np.rec.fromarrays(data,dtype=[('HJD','>f8'),('flux','>f8'),('e_flux','>f8'),('flag','i')])
72 sa = np.argsort(data['HJD'])
73 return data[sa],header
74
76 """
77 Retrieve CoRoT timeseries from a remote data repository.
78
79 The output record array has fields 'HJD', 'flux', 'e_flux', 'flag'.
80
81 @param ID: ID of the target: either an integer (CoRoT ID), an SIMBAD-recognised
82 target name, or a valid CoRoT FITS file
83 @type ID: int or str
84 @param type_data: 'white' or 'colors' (if available!)
85 @type type_data: str
86 @return: data, header
87 @rtype: numpy recarray, dict
88 """
89 cat,units,comms = get_exo_catalog()
90 header = None
91 data = []
92 if isinstance(ID,str) and os.path.isfile(ID):
93 header = pyfits.getheader(ID)
94 times,flux,error,flags = fits.read_corot(ID)
95 data.append([times,flux,error,flags])
96 else:
97
98 try:
99 ID = int(ID)
100 except ValueError:
101 logger.error("Data retrieval for %s not possible. Reason: ID not resolved" % (ID))
102 return None
103
104 for filename in cat['FileName'][cat['CoRoT']==ID]:
105 url = urllib.URLopener()
106 filen,msg = url.retrieve(filename)
107 try:
108 header = pyfits.getheader(filen)
109 except IOError:
110 continue
111 times,flux,error,flags = fits.read_corot(filen,type_data=type_data)
112 url.close()
113 data.append([times,flux,error,flags])
114
115
116 if not data:
117 raise ValueError('target {0} not in online CoRoT data repository'.format(ID))
118 data = np.hstack(data)
119 data = np.rec.fromarrays(data,dtype=[('HJD','>f8'),('flux','>f8'),('e_flux','>f8'),('flag','i')])
120 sa = np.argsort(data['HJD'])
121 return data[sa],header
122
123
132
134 """
135 Convert a CoRoT ID to ra,dec.
136
137 @param corot_id: CoRoT exoplanet identification number
138 @type corot_id: int
139 @return: RA, DEC (degrees)
140 @rtype: float,float
141 """
142 exofile= os.path.join(cc.path.ivsdata,'catalogs','corot','exo','exo.tsv')
143
144 data,units,comms = vizier.tsv2recarray(exofile)
145 index = np.argmin(np.abs(data['CoRoT']-corot_id))
146 if data['CoRoT'][index]-corot_id==0:
147 logger.info('CoRoT %s resolved to RA=%s, DEC=%s'%(corot_id,data[index]['_RAJ2000'],data[index]['_DEJ2000']))
148 return data[index]['_RAJ2000'],data[index]['_DEJ2000']
149 else:
150 logger.info('CoRoT %s not resolved by CoRoT catalog')
151