1
2 """
3 Retrieve Hipparcos epoch/intermediate photometry from the internet.
4
5 Author: Joris De Ridder & Pieter Degroote
6 """
7
8 from __future__ import with_statement
9 import httplib
10 import logging
11 import os
12
13 import numpy as np
14 from ivs.aux import loggers
15 from ivs.catalogs import sesame
16
17 from ivs import config
18
19 logger = logging.getLogger("CAT.HIP")
20 logger.addHandler(loggers.NullHandler)
21
23
24 """
25 Retrieve Hipparcos epoch/intermediate photometry from the ESA website.
26
27 The time series together with some header information is stored in a record
28 array and dictionary, and optionally written in a specified file.
29
30 The time points are given in barycentric Julian Date and are corrected
31 for the offset of 2440000.0 in the original files, but B{only in the output
32 record array}. The output files display the B{original contents}.
33
34 For epoch photometry, set C{dtype='ep'}.
35 For intermediate date, set C{dtype='i'}.
36
37 For more information:
38 C{http://www.rssd.esa.int/SA-general/Projects/Hipparcos/CATALOGUE_VOL1/sect2_05.ps.gz}
39
40 Example:
41
42 >>> data,header = getHipData(1234)
43 >>> data = data[data['q_mag'] <= 2] # keep only the good points
44
45 To write the retrieved data to a file:
46
47 >>> data, header = getHipData(1234 , "myfile.txt")
48
49 To store the different columns in separate arrays:
50
51 >>> data, header = getHipData(1234)
52 >>> time = data['time']
53 >>> magnitude = data['mag']
54 >>> errorbar = data['e_mag']
55 >>> qualityflag = data['q_mag']
56
57 In the case of intermediate data products:
58 - orbit: orbit number
59 - source: source of abscissa (F=FAST, f=rejected FAST, N=NDAC,n=NDAC rejected)
60 - d_acosd: partial derivative wrt alpha cos(delta)
61 - d_d: partial derivative wrt delta
62 - d_pi: partial derivative wrt parallax
63 - d_mua: partial derivative wrt proper motion alpha cos(delta)
64 - d_mud: partial derivative wrt proper motion delta
65
66 @param ID: identification of the star: if you give an integer or string that
67 can be converted to an integer, it is assumed to be the hipparcos number of
68 the star. E.g. 1234 or "1234". If it is not an integer, the star will
69 be resolved via sesame to get the HIP number if possible
70 @type ID: integer or string
71 @param dtype: data type (epoch ('ep') photometry or intermediate ('i') data)
72 @type dtype: string (one of ('ep','i'))
73 @param outputFileName: the name of the file that will be created
74 to save the Hipparcos time series
75 @type outputFileName: string
76 @return: record array with fields time, mag, e_mag (errorbar),
77 q_mag (quality flag), and a dictionary containing the
78 header information. The header dictionary is of style
79 {'HH14': ('A', 'Annex flag (light curves)'), ...}
80 @rtype: rec array, dict
81 """
82
83 server = "www.rssd.esa.int"
84 webpage = "/hipparcos_scripts/HIPcatalogueSearch.pl?hip%sId="%(dtype)
85
86
87
88 try:
89 hipnr = int(ID)
90 except ValueError:
91 info = sesame.search(ID,db='S')
92 IDs = [alias for alias in info['alias'] if 'HIP' in alias]
93 if len(IDs)!=1:
94 logger.error("Data retrieval for %s not possible. Reason: no HIP number resolved" % (ID))
95 return
96 hipnr = IDs[0].split(' ')[1]
97
98
99
100 conn = httplib.HTTPConnection(server)
101 conn.request("GET", webpage + str(hipnr))
102 response = conn.getresponse()
103 if response.reason != "OK":
104 logger.error("Data retrieval for HIP%s not possible. Reason: %s" % (str(hipnr), response.reason))
105 return
106 else:
107 logger.info("Data retrieval for HIP%s: OK" % str(hipnr))
108
109 contents = response.read()
110 conn.close()
111
112
113
114
115
116 data = []
117 header = {}
118
119 if outputFileName:
120 outputFile = open(outputFileName,'w')
121
122 for line in contents.split('\n'):
123 if line == "": continue
124 if not line.startswith("<"):
125 line = line.replace("\r", "")
126
127
128
129 if not line[0].isdigit():
130 sline = line.split(':')
131
132
133
134
135 if len(sline)==2:
136 key,info = sline
137 info = info.split()
138 header[key] = (info[0]," ".join(info[1:]))
139 if outputFileName:
140 line = "# " + line
141
142
143
144 else:
145 data.append(line.split('|'))
146
147 data[-1] = tuple([(entry.replace(' ','')=='' and np.nan or entry) for entry in data[-1]])
148 if outputFileName:
149 outputFile.write(line + "\n")
150 if outputFileName:
151 outputFile.close()
152
153
154
155
156 if dtype=='ep':
157 dtypes = [('time','f8'),('mag','f8'),('e_mag','f8'),('q_mag','i')]
158 elif dtype=='i':
159 dtypes = [('orbit','i'),('source','a1'),
160 ('d_acosd','f8'),('d_d','f8'),('d_pi','f8'),
161 ('d_mua','f8'),('d_mud','f8'),
162 ('abs_res','f8'),('abs_std','f8'),('cor','f8')]
163 data = np.rec.array(data,dtype=dtypes)
164
165
166 if dtype=='ep':
167 data['time'] += 2440000.0
168
169
170 return data,header
171
172
173
174
175
177 """
178 Convenience function to retrieve epoch data.
179 """
180 return getHipData(ID,dtype='ep',outputFileName=outputFileName)
181
182
183
184
185
191