-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruwetools.py
134 lines (102 loc) · 4.33 KB
/
ruwetools.py
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
"""
Functions for handling the tables listing the normalization factor u0 needed to calculate the
renormalized unit weight error (RUWE). The tables are provided on the Gaia DR2 Known Issues pages
(https://www.cosmos.esa.int/web/gaia/dr2-known-issues).
Anthony Brown Oct 2018 - Oct 2018
"""
import numpy as np
import os
from scipy.interpolate import interp1d, RectBivariateSpline
_ROOT = os.path.abspath(os.path.dirname(__file__))
def get_data(path):
return os.path.join(_ROOT, 'data', path)
class U0Interpolator():
"""
Class which holds functions that can used to calculate u0 values given the G-band magnitude only, or
the G-band magnitude and the BP-RP colour of a source. The class initialization takes care of reading
the necessary data and setting up the interpolators.
"""
def __init__(self):
"""
Initialize the class.
"""
ngmagbins = 1741
ncolorbins = 111
self.ming = 3.6
self.maxg = 21.0
self.minbprp = -1.0
self.maxbprp = 10.0
u0data = np.genfromtxt(get_data('table_u0_g_col.txt'), names=['g_mag', 'bp_rp', 'u0'], skip_header=1, delimiter=',')
gmagmesh = np.reshape(u0data['g_mag'], (ngmagbins, ncolorbins))
bprpmesh = np.reshape(u0data['bp_rp'], (ngmagbins, ncolorbins))
u0mesh = np.reshape(u0data['u0'], (ngmagbins, ncolorbins))
gmag = gmagmesh[:,0]
bprp = bprpmesh[0,:]
self.gbprpinterpolator = RectBivariateSpline(gmag, bprp, u0mesh, kx=1, ky=1, s=0)
u0data = np.genfromtxt(get_data('table_u0_g.txt'), names=['g_mag', 'u0'], skip_header=1, delimiter=',')
self.ginterpolator = interp1d(u0data['g_mag'], u0data['u0'], bounds_error=True)
def get_u0_g_col(self, gmag, bprp, asgrid=False):
"""
Calculate the u0 value for the input G-band magnitude(s) and BP-RP colour(s). Input values
outside the interpolation range will lead to a ValueError being raised.
Parameters
----------
gmag : float array
Values of G (size n)
bprp : float array
Values of BP-RP (size m)
Keywords
--------
asgrid : boolean
If True treat the input gmag and bprp arrays as the coordinates for a 2D grid. If set to True
the sizes of the input arrays n and m may be different. If set to False n=m is required.
Returns
-------
The values of u0 evaluated at the input (G, BP-RP) combinations. The returned list consists
of n values for grid=True and n*m otherwise.
"""
return self.gbprpinterpolator(gmag, bprp, grid=asgrid)
def get_u0_g(self, gmag):
"""
Calculate the normalization factor u0 for the RUWE for the input G-band magnitude(s). Input
values outside the interpolation range will lead to a ValueError being raised.
Parameters
----------
gmag : float array
Values of G
Returns
-------
Array of u0 values evaluated at the input G-band magnitudes.
"""
return self.ginterpolator(gmag)
def get_u0(self, gmag, bprp):
"""
Calculate the RUWE normalization factor u0 for the input G-band magnitude(s) and BP-RP colour(s).
Input values outside the interpolation range will be clamped to the corresponding edge of that
range.
USE THIS FUNCTION IF YOU HAVE A LARGE LIST OF MAGNITUDES AND COLOURS OR IF YOU HAVE A LIST WHICH
MAY INCLUDE ENTRIES WITH NO BP_RP COLOUR.
Parameters
----------
gmag : float array
Values of G (size n)
bprp : float array
Values of BP-RP (size m)
Returns
-------
The values of u0 evaluated at the input (G, BP-RP) combinations.
"""
if np.isscalar(gmag):
if np.isnan(bprp):
return self.get_u0_g(gmag)
else:
return self.get_u0_g_col(gmag, bprp)
else:
u0 = np.zeros(gmag.size)
g = np.clip(gmag, self.ming, self.maxg)
col = np.clip(bprp, self.minbprp, self.maxbprp)
indnocol = np.isnan(col)
indcol = np.logical_not(indnocol)
u0[indcol] = self.get_u0_g_col(g[indcol], col[indcol], asgrid=False)
u0[indnocol] = self.get_u0_g(g[indnocol])
return u0