-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarchive.cs
340 lines (311 loc) · 16.3 KB
/
Starchive.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// --------------------------------------------------------------------------------
// 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.IO;
using System.Linq;
using System.Xml.Linq;
namespace VariScan
{
/*Starchive Class
*
* This class encapsulates methods for storing and retrieving photometry data
* on individual stars
*
*/
class Starchive
{
#region XML fields
const string XMLRoot = "PhotometrySummaries";
const string PhotometryRecordX = "PhotometryRecord";
const string TargetNameX = "TargetName";
const string TargetRAX = "TargetRA";
const string TargetDecX = "TargetDec";
const string IsTransformedX = "IsTransformed";
const string CatalogNameX = "StarCatalog";
const string SourceRAX = "SourceRA";
const string SourceDecX = "SourceDec";
const string SourceInstrumentMagnitudeX = "SourceInstrumentMagnitude";
const string PrimaryFilterX = "PrimaryFilter";
const string DifferentialFilterX = "DifferentialFilter";
const string PrimaryStandardColorX = "PrimaryStandardColor";
const string DifferentialStandardColorX = "DifferentialStandardColor";
const string StandardColorMagnitudeX = "StandardColorMagnitude";
const string StandardMagnitudeErrorX = "StandardMagnitudeError";
const string TargetToSourcePositionErrorX = "T2SPositionError";
const string SourceEllipticityX = "SourceEllipticity";
const string SourceFWHMX = "SourceFWHM";
const string SourceADUX = "SourceADU";
const string ImageDateUTX = "ImageDate";
const string SessionDateX = "SessionDate";
const string SessionSetX = "SessionSet";
const string ColorTransformX = "ColorTransform";
const string MagnitudeTransformX = "MagnitudeTransform";
const string SourceToAPASSErrorX = "SourceToAPASSError";
const string SourceToGaiaErrorX = "SourceToGaiaError";
const string APASSFieldStarCountX = "APASSFieldStarCount";
const string GaiaFieldStarCountX = "GaiaFieldStarCount";
const string AirMassX = "AirMass";
#endregion
public static bool CheckStarchiveFile()
{
Configuration cfg = new Configuration();
if (!File.Exists(cfg.StarchiveFilePath))
{
XElement newStarchiveX = new XElement(XMLRoot);
newStarchiveX.Save(cfg.StarchiveFilePath);
return false;
}
return true;
}
public static void StorePhotometry(TargetData pData)
{
//Create an XElement with the data pData, after removing any old record
//
Configuration cfg = new Configuration();
CheckStarchiveFile();
XElement newStarchiveX = new XElement(XMLRoot);
XElement oldStarchiveX = XElement.Load(cfg.StarchiveFilePath);
List<XElement> targetXList = oldStarchiveX.Elements(PhotometryRecordX).ToList();
foreach (XElement targetX in targetXList)
{
if (!((targetX.Element(TargetNameX).Value == pData.TargetName) &&
(targetX.Element(SessionDateX).Value == pData.SessionDate.ToShortDateString()) &&
(targetX.Element(ImageDateUTX).Value == pData.ImageDateUT.ToShortTimeString()) &&
(targetX.Element(PrimaryFilterX).Value == pData.PrimaryStandardColor.ToString()) &&
(targetX.Element(CatalogNameX).Value == pData.CatalogName)))
{
newStarchiveX.Add(targetX);
}
}
newStarchiveX.Add(FormXrecord(pData));
newStarchiveX.Save(cfg.StarchiveFilePath);
return;
}
public static List<TargetData> RetrievePhotometry(TargetData tgt, string sessionDate)
{
//Open file, convert xml entries to list, if any and
// retrieve target data on elements matching name, color and filter
Configuration cfg = new Configuration();
List<TargetData> starList = new List<TargetData>();
if (!CheckStarchiveFile())
return starList;
XElement starAllXdata = XElement.Load(cfg.StarchiveFilePath);
IEnumerable<XElement> starXlist = starAllXdata.Elements(PhotometryRecordX);
DateTime sessionDT = DateTime.MinValue; ;
if (sessionDate != "All")
sessionDT = Convert.ToDateTime(sessionDate);
foreach (XElement star in starXlist)
{
//Target name is the same as the requested target
// and, if the sessionDate is not "All"
// then the sessionDate must also be the same
//DateTime recordDT = Convert.ToDateTime(star.Element(SessionDateX).Value.ToString());
bool isTarget = star.Element(TargetNameX).Value == tgt.TargetName;
bool isPartialSession = sessionDate != "All";
DateTime recordDT = Convert.ToDateTime(star.Element(SessionDateX).Value.ToString());
bool matchesSession = recordDT == sessionDT;
bool entryIsInSession = (!isPartialSession) || (isPartialSession && matchesSession);
if (isTarget && entryIsInSession)
{
TargetData pd = CreateTarget(star);
starList.Add(pd);
}
}
return starList;
}
public static bool HasMatchingPhotometryRecord(TargetData tgt)
{
List<TargetData> pRecords = RetrievePhotometry(tgt, "All");
foreach (TargetData p in pRecords)
{
if ((DateTime.Compare(p.SessionDate, tgt.SessionDate) == 0) &&
(p.SessionSet == tgt.SessionSet) &&
(p.PrimaryStandardColor == tgt.PrimaryStandardColor) &&
(p.CatalogName == tgt.CatalogName) &&
(p.DifferentialStandardColor == tgt.DifferentialStandardColor) &&
(p.CatalogName == tgt.CatalogName) &&
(p.PrimaryStandardColor == tgt.PrimaryStandardColor) &&
(p.DifferentialStandardColor == tgt.DifferentialStandardColor) &&
(p.PrimaryImageFilter == tgt.PrimaryImageFilter) &&
(p.DifferentialImageFilter == tgt.DifferentialImageFilter))
return true;
}
return false;
}
public static List<TargetData> RetrieveAllPhotometry()
{
Configuration cfg = new Configuration();
List<TargetData> starList = new List<TargetData>();
if (!CheckStarchiveFile()) return starList;
XElement starAllXdata = XElement.Load(cfg.StarchiveFilePath);
IEnumerable<XElement> starXlist = starAllXdata.Elements(PhotometryRecordX);
foreach (XElement star in starXlist)
{
TargetData pd = CreateTarget(star);
starList.Add(pd);
}
return starList;
}
public static List<TargetData> RetrieveAllPhotometrySummarized()
{
Configuration cfg = new Configuration();
List<TargetData> starchiveList = new List<TargetData>();
List<TargetData> starTgtList = new List<TargetData>();
List<TargetData> starCondensedList = new List<TargetData>();
//If no starchive, then return a null list
if (!CheckStarchiveFile()) return starTgtList;
//Load starchive
XElement starchiveAllXdata = XElement.Load(cfg.StarchiveFilePath);
IEnumerable<XElement> starchiveXlist = starchiveAllXdata.Elements(PhotometryRecordX);
//Translate each entry to a target structure
//Create list of all targets
foreach (XElement srec in starchiveXlist)
{
TargetData pd = CreateTarget(srec);
starTgtList.Add(pd);
}
var starGroups = starTgtList.OrderBy(s => s.TargetName).GroupBy(s => s.TargetName);
return starCondensedList;
}
private static XElement FormXrecord(TargetData pData)
{
XElement newStarXdata = new XElement(PhotometryRecordX,
new XElement(IsTransformedX, pData.IsTransformed),
new XElement(TargetNameX, pData.TargetName),
new XElement(SessionDateX, pData.SessionDate.ToString("d")),
new XElement(ImageDateUTX, pData.ImageDateUT.ToString()),
new XElement(SessionSetX, pData.SessionSet),
new XElement(CatalogNameX, pData.CatalogName),
new XElement(TargetRAX, pData.TargetRA.ToString()),
new XElement(TargetDecX, pData.TargetDec.ToString()),
new XElement(PrimaryStandardColorX, pData.PrimaryStandardColor),
new XElement(DifferentialStandardColorX, pData.DifferentialStandardColor),
new XElement(PrimaryFilterX, pData.PrimaryImageFilter),
new XElement(DifferentialFilterX, pData.DifferentialImageFilter),
new XElement(ColorTransformX, pData.ColorTransform.ToString()),
new XElement(MagnitudeTransformX, pData.MagnitudeTransform.ToString()),
new XElement(StandardColorMagnitudeX, pData.StandardColorMagnitude.ToString()),
new XElement(SourceInstrumentMagnitudeX, pData.SourceInstrumentMagnitude.ToString()),
new XElement(StandardMagnitudeErrorX, pData.StandardMagnitudeError.ToString()),
new XElement(SourceToAPASSErrorX, pData.SourceToAPASSCatalogPositionError.ToString()),
new XElement(SourceToGaiaErrorX, pData.SourceToGAIACatalogPositionError.ToString()),
new XElement(APASSFieldStarCountX, pData.ApassStarCount.ToString()),
new XElement(GaiaFieldStarCountX, pData.GaiaStarCount.ToString()),
new XElement(AirMassX, pData.AirMass.ToString()));
return newStarXdata;
}
private static TargetData CreateTarget(XElement xData)
{
TargetData tData = new TargetData
{
TargetName = FetchX(xData, TargetNameX, "None"),
IsTransformed = Convert.ToBoolean(FetchX(xData, IsTransformedX, "False")),
SessionDate = Convert.ToDateTime(FetchX(xData, SessionDateX, DateTime.MinValue.ToString())),
CatalogName = FetchX(xData, CatalogNameX, "APASS"),
ImageDateUT = Convert.ToDateTime(FetchX(xData, ImageDateUTX, DateTime.MinValue.ToString())),
TargetRA = Convert.ToDouble(FetchX(xData, TargetRAX, "0.0")),
TargetDec = Convert.ToDouble(FetchX(xData, TargetDecX, "0.0")),
PrimaryImageFilter = FetchX(xData, PrimaryFilterX, "None"),
DifferentialImageFilter = FetchX(xData, DifferentialFilterX, "None"),
PrimaryStandardColor = FetchX(xData, PrimaryStandardColorX, "None"),
DifferentialStandardColor = FetchX(xData, DifferentialStandardColorX, "None"),
ColorTransform = Convert.ToDouble(FetchX(xData, ColorTransformX, "0.00")),
MagnitudeTransform = Convert.ToDouble(FetchX(xData, MagnitudeTransformX, "0.00")),
StandardColorMagnitude = Convert.ToDouble(FetchX(xData, StandardColorMagnitudeX, "0.00")),
SourceInstrumentMagnitude = Convert.ToDouble(FetchX(xData, SourceInstrumentMagnitudeX, "0.00")),
StandardMagnitudeError = Convert.ToDouble(FetchX(xData, StandardMagnitudeErrorX, "0.0")),
SourceToAPASSCatalogPositionError = Convert.ToDouble(FetchX(xData, SourceToAPASSErrorX, "0.0")),
SourceToGAIACatalogPositionError = Convert.ToDouble(FetchX(xData, SourceToGaiaErrorX, "0.0")),
ApassStarCount = Convert.ToInt32(FetchX(xData, APASSFieldStarCountX, "0")),
GaiaStarCount = Convert.ToInt32(FetchX(xData, GaiaFieldStarCountX, "0")),
AirMass = Convert.ToDouble(FetchX(xData, AirMassX, "0")),
MasterCatalogInfo = new StarField.CatalogData(),
};
return tData;
}
private static string FetchX(XElement x, string name, string defaultValue)
{
if (x.Element(name) == null)
return defaultValue;
else return x.Element(name).Value;
}
private static bool IsSameNight(DateTime a, DateTime b)
{
//Checks if two datatimes are within same night, i.e. within 12 hours of each other.
if ((a - b).TotalHours < 12) return true;
else return false;
}
public static void ClearStarchiveSession(DateTime sessionDate, string catName)
{
//Removes all entries from Starchive for a given session
Configuration cfg = new Configuration();
XElement newStarchive = new XElement(XMLRoot);
if (!CheckStarchiveFile())
return;
XElement starAllXdata = XElement.Load(cfg.StarchiveFilePath);
List<XElement> starXlist = starAllXdata.Elements(PhotometryRecordX).ToList();
var vb = starXlist.Where(x => (Convert.ToDateTime(x.Element(SessionDateX).Value) != sessionDate) || (x.Element(CatalogNameX).Value != catName));
foreach (XElement x in vb)
newStarchive.Add(vb);
newStarchive.Save(cfg.StarchiveFilePath);
return;
}
public static void ClearStarchiveTarget(string targetName, string catName)
{
//Removes all entries from Starchive for a given target and field catalog
Configuration cfg = new Configuration();
XElement newStarchive = new XElement(XMLRoot);
if (!CheckStarchiveFile())
return;
XElement starAllXdata = XElement.Load(cfg.StarchiveFilePath);
List<XElement> starXlist = starAllXdata.Elements(PhotometryRecordX).ToList();
var vb = starXlist.Where(x => (x.Element(TargetNameX).Value != targetName) || (x.Element(CatalogNameX).Value != catName));
foreach (XElement x in vb)
newStarchive.Add(vb);
newStarchive.Save(cfg.StarchiveFilePath);
return;
}
public static void ClearStarchiveCatalog(string catName)
{
//Removes all entries from Starchive for a given target and field catalog
Configuration cfg = new Configuration();
XElement newStarchive = new XElement(XMLRoot);
if (!CheckStarchiveFile())
return;
XElement starAllXdata = XElement.Load(cfg.StarchiveFilePath);
List<XElement> starXlist = starAllXdata.Elements(PhotometryRecordX).ToList();
var vb = starXlist.Where(x => x.Element(CatalogNameX).Value != catName);
foreach (XElement x in vb)
newStarchive.Add(vb);
newStarchive.Save(cfg.StarchiveFilePath);
return;
}
public static void ClearStarchive()
{
//Removes all entries from Starchive
Configuration cfg = new Configuration();
XElement newStarchive = new XElement(XMLRoot);
if (!CheckStarchiveFile())
return;
newStarchive.Save(cfg.StarchiveFilePath);
return;
}
}
}