-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformation.cs
317 lines (299 loc) · 14 KB
/
Transformation.cs
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
// --------------------------------------------------------------------------------
// VariScan module
//
// Description:
//
// Environment: Windows 10 executable, 32 and 64 bit
//
// Usage: TBD
//
// Author: (REM) Rick McAlister, [email protected]
//
// Edit Log: Rev 1.0 Initial Version
//
// Date Who Vers Description
// ----------- --- ----- -------------------------------------------------------
//
// ---------------------------------------------------------------------------------
//
using System;
using System.Collections.Generic;
using System.Linq;
namespace VariScan
{
public class Transformation
{
public static (double primaryMag, double differentialMag) FormColorIndex(int regIndex, ColorIndexing.ColorDataSource diffColor,
StarField.FieldLightSource[] differentialFLS,
ColorIndexing.ColorDataSource priColor,
StarField.FieldLightSource[] primaryFLS,
string catalog)
{
StarField.FieldLightSource pFLS = (from StarField.FieldLightSource p in primaryFLS
where (p.RegistrationIndex == regIndex)
select p).FirstOrDefault();
StarField.FieldLightSource dFLS = (from StarField.FieldLightSource d in differentialFLS
where (d.RegistrationIndex == regIndex)
select d).FirstOrDefault();
if (pFLS.CatalogInfo == null || dFLS.CatalogInfo == null)
return (0, 0);
else
{
double pri = GetCatalogedMagnitude(priColor, pFLS, catalog);
double dif = GetCatalogedMagnitude(diffColor, dFLS, catalog);
return (pri, dif);
}
}
public static double GetCatalogedMagnitude(ColorIndexing.ColorDataSource cds, StarField.FieldLightSource fls, string catalog)
{
double mag = 0;
switch (cds)
{
case ColorIndexing.ColorDataSource.Instrument:
{
mag = fls.LightSourceInstMag;
break;
}
case ColorIndexing.ColorDataSource.Bj:
{
if (catalog == "Gaia")
mag = ColorIndexing.GaiaToJohnson(ColorIndexing.StandardColors.Bj,
fls.CatalogInfo.Value.GAIACatalogMagnitudeG,
fls.CatalogInfo.Value.GAIACatalogMagnitudeGbp,
fls.CatalogInfo.Value.GAIACatalogMagnitudeGrp);
else //APASS
mag = fls.CatalogInfo.Value.APASSCatalogMagnitudeB;
break;
}
case ColorIndexing.ColorDataSource.Vj:
{
if (catalog == "Gaia")
mag = ColorIndexing.GaiaToJohnson(ColorIndexing.StandardColors.Vj,
fls.CatalogInfo.Value.GAIACatalogMagnitudeG,
fls.CatalogInfo.Value.GAIACatalogMagnitudeGbp,
fls.CatalogInfo.Value.GAIACatalogMagnitudeGrp);
else //APASS
mag = fls.CatalogInfo.Value.APASSCatalogMagnitudeV;
break;
}
case ColorIndexing.ColorDataSource.Rc:
{
if (catalog == "Gaia")
mag = ColorIndexing.GaiaToJohnson(ColorIndexing.StandardColors.Rc,
fls.CatalogInfo.Value.GAIACatalogMagnitudeG,
fls.CatalogInfo.Value.GAIACatalogMagnitudeGbp,
fls.CatalogInfo.Value.GAIACatalogMagnitudeGrp);
else //APASS
mag = fls.CatalogInfo.Value.APASSCatalogMagnitudeR;
break;
}
case ColorIndexing.ColorDataSource.Ic:
{
mag = fls.CatalogInfo.Value.APASSCatalogMagnitudeI;
break;
}
case ColorIndexing.ColorDataSource.Uc:
{
mag = fls.CatalogInfo.Value.APASSCatalogMagnitudeU;
break;
}
case ColorIndexing.ColorDataSource.Gp:
{
mag = fls.CatalogInfo.Value.GAIACatalogMagnitudeG;
break;
}
case ColorIndexing.ColorDataSource.GBp:
{
mag = fls.CatalogInfo.Value.GAIACatalogMagnitudeGbp;
break;
}
case ColorIndexing.ColorDataSource.GRp:
{
mag = fls.CatalogInfo.Value.GAIACatalogMagnitudeGrp;
break;
}
}
return (mag);
}
public static double GetCatalogedMagnitude(ColorIndexing.ColorDataSource cds, StarField.CatalogData fls, string catalog)
{
double mag = 0;
switch (cds)
{
case ColorIndexing.ColorDataSource.Bj:
{
if (catalog == "Gaia")
mag = ColorIndexing.GaiaToJohnson(ColorIndexing.StandardColors.Bj,
fls.GAIACatalogMagnitudeG,
fls.GAIACatalogMagnitudeGbp,
fls.GAIACatalogMagnitudeGrp);
else //APASS
mag = fls.APASSCatalogMagnitudeB;
break;
}
case ColorIndexing.ColorDataSource.Vj:
{
if (catalog == "Gaia")
mag = ColorIndexing.GaiaToJohnson(ColorIndexing.StandardColors.Vj,
fls.GAIACatalogMagnitudeG,
fls.GAIACatalogMagnitudeGbp,
fls.GAIACatalogMagnitudeGrp);
else //APASS
mag = fls.APASSCatalogMagnitudeV;
break;
}
case ColorIndexing.ColorDataSource.Rc:
{
if (catalog == "Gaia")
mag = ColorIndexing.GaiaToJohnson(ColorIndexing.StandardColors.Rc,
fls.GAIACatalogMagnitudeG,
fls.GAIACatalogMagnitudeGbp,
fls.GAIACatalogMagnitudeGrp);
else //APASS
mag = fls.APASSCatalogMagnitudeR;
break;
}
case ColorIndexing.ColorDataSource.Ic:
{
mag = fls.APASSCatalogMagnitudeI;
break;
}
case ColorIndexing.ColorDataSource.Uc:
{
mag = fls.APASSCatalogMagnitudeU;
break;
}
case ColorIndexing.ColorDataSource.Gp:
{
mag = fls.GAIACatalogMagnitudeG;
break;
}
case ColorIndexing.ColorDataSource.GBp:
{
mag = fls.GAIACatalogMagnitudeGbp;
break;
}
case ColorIndexing.ColorDataSource.GRp:
{
mag = fls.GAIACatalogMagnitudeGrp;
break;
}
}
return (mag);
}
public static (double intercept, double slope) ColorTransform(List<double> xp, List<double> yp)
{
const int thetaCount = 180;
const int rangeCount = 1000;
(double intercept, double slope) = HoughTransform(xp, yp, thetaCount, rangeCount);
return (intercept, slope);
}
public static (double intercept, double slope) MagnitudeTransform(List<double> xp, List<double> yp)
{
//Trp => Magnitude Transform is the regressed slope of mean (R-p)/(R-P) over images
const int thetaCount = 180;
const int rangeCount = 1000;
(double intercept, double slope) = HoughTransform(xp, yp, thetaCount, rangeCount);
return (intercept, slope);
}
public static (double intercept, double slope) HoughTransform(List<double> xP, List<double> yP, int thetaCount, int rangeCount)
{
//Runs Hough transform on list of diagram points
//
//Theta will increment from zero to PI (180 degrees)
double thetaIncrement = Math.PI / thetaCount;
//The maximum range (r) can be no greater than largest abs(x) + abs(y) value, e.g. r = x cos theta + y sin theta
double rangeMax = 0;
for (int i = 0; i < xP.Count; i++)
{
double r = Math.Abs(xP[i]) + Math.Abs(yP[i]);
if (r > rangeMax)
rangeMax = r;
}
double rangeMin = -rangeMax; //may chnage this later
//Calculate the range value for each incremental index
double rangeIncrement = (rangeMax - rangeMin) / rangeCount;
//Set accumlator range to +/- maxRange
//Create accumulator array size
int[,] accumulator = new int[thetaCount, rangeCount];
//Convert diagramXY to NormalPoint array
for (int p = 0; p < xP.Count; p++)
{
for (int t = 0; t < thetaCount; t++)
{
double theta = t * thetaIncrement;
double rangePoint = xP[p] * Math.Cos(theta) + yP[p] * Math.Sin(theta);
//the range runs from - rangeMax to + rangeMax
// the index will be 2 * range/max
int rangeBucket = (int)Math.Floor((rangePoint - rangeMin) / rangeIncrement);
if (rangeBucket >= rangeCount)
rangeBucket = rangeCount - 1;
//Add vote to range/theta
accumulator[t, rangeBucket]++;
}
}
//Find max voted in normal space
int maxVote = 0;
int currentVote;
int votedRangeIndex = 0;
int votedThetaIndex = 0;
for (int r = 0; r < rangeCount; r++)
{
for (int t = 1; t < thetaCount; t++) //Ignor omega = 0 to avoid infinite slope
{
currentVote = accumulator[t, r];
if (currentVote >= maxVote)
{
maxVote = currentVote;
votedRangeIndex = r;
votedThetaIndex = t;
}
}
}
//y = (-cotθ)*x + (r*cosecθ)
// m = (-cotθ)
// b = (r * cosecθ)
if (maxVote == 0)
return (1, 1);
double votedTheta = (votedThetaIndex * thetaIncrement); //in radians
double slope = -1.0 / Math.Tan(votedTheta);
double votedRange = votedRangeIndex * rangeIncrement + rangeMin;
double intercept = votedRange * (1.0 / Math.Sin(votedTheta));
return (intercept, slope);
}
public static List<StarField.FieldLightSource> SortByMagnitude(List<StarField.FieldLightSource> lsList, int brightCount)
{
//Find the ten brightest stars
if (lsList.Count < brightCount)
brightCount = lsList.Count;
List<StarField.FieldLightSource> listOut = lsList.OrderByDescending(a => a.LightSourceInstMag, new SpecialComparer()).ToList();
return listOut.GetRange(0, brightCount);
}
public static (List<double>, List<double>) CullOutliers(List<double> xP, List<double> yP, double intercept, double slope)
{
//Compute standard deviation of x,y datapoints
//Build new list of all points that are within the standard deviation of line y = slope X + intercept
//Compute the mean deviation for all points w/r/t line defined by slope/intercept
List<double> distanceList = new List<double>();
List<double> xPout = new List<double>();
List<double> yPout = new List<double>();
double mean = 0;
for (int i = 0; i < xP.Count; i++)
{
double distance = Math.Abs(-slope * xP[i] + yP[i] + (-intercept)) / (Math.Sqrt((-slope * -slope) + 1));
distanceList.Add(distance);
mean += distance;
}
mean /= xP.Count;
for (int i = 0; i < xP.Count; i++)
{
if (distanceList[i] <= mean)
{
xPout.Add(xP[i]);
yPout.Add(yP[i]);
}
}
return (xPout, yPout);
}
}
}