-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTargetXList.cs
More file actions
415 lines (374 loc) · 16.2 KB
/
TargetXList.cs
File metadata and controls
415 lines (374 loc) · 16.2 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// --------------------------------------------------------------------------------
// VariScan module
//
// Description:
//
// Environment: Windows 10 executable, 32 and 64 bit
//
// Usage: TBD
//
// Author: (REM) Rick McAlister, rrskybox@yahoo.com
//
// 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.Windows.Forms;
using System.Xml.Linq;
using TheSky64Lib;
namespace VariScan
{
public class TargetXList
{
const string TargetListRootX = "VariScan_List";
const string TargetListRecordX = "VariScan_Target";
const string DifferentialRecordX = "VariScan_Differential";
const string NameX = "Name";
const string RAX = "RA";
const string DecX = "Dec";
//const string FilterX = "Filter";
const string ExposureX = "Exposure";
const string LastDateX = "LastImagingDate";
private XElement targetListRecordX;
public XElement CollectionDefinition { get; set; }
public TargetXList()
{
//Upon instantiation...
//Open empty working XML file for AGN list
//Create an XML datastructure for the Observing List and load it with Observing LIst entries
//Get the path to the AGN List
//For each entry in AGN List, convert RA/Dec to Altitude and Azimuth at start time
//Alt/Az acceptable, then add target to XML data list
//Replace the current working target List file with the new XML data list
//Close the file
{
Configuration cfg = new Configuration();
//Check for an existing xml file. If so, then load it.
// Otherwise log an error and return nothing.
if (File.Exists(cfg.TargetListPath))
{
targetListRecordX = XElement.Load(cfg.TargetListPath);
NameSortTargetXList();
}
else
{
targetListRecordX = null;
}
}
}
public List<TargetXDescriptor> GetTargetXList()
{
List<TargetXDescriptor> varTargets = new List<TargetXDescriptor>();
IEnumerable<XElement> tListX = targetListRecordX.Elements(TargetListRecordX);
foreach (XElement agnt in tListX)
{
varTargets.Add(new TargetXDescriptor(agnt));
}
return varTargets;
}
public int TargetXCount
{
//Public Property VariableTargetCount
//
// Returns the current number of targets remaining in the target list
//
get
{
Configuration cfg = new Configuration();
int count = 0;
try
{
XElement acnX = XElement.Load(cfg.TargetListPath);
count = acnX.Elements(TargetListRecordX).Count();
}
catch { count = 0; }
return (count);
}
}
public static void CreateXListFromCSV(string textFilePath, string xmlFilePath)
{
//Generages the XML target file from the filePath .txt file
//The assumed format for the AGN text file is |<name>|<ra>|<dec>|<magnitude>
char[] remChar = { 'h', 'm', 's', 'd' };
string textLine;
string[] lineElements;
XElement tgtXFile;
tgtXFile = new XElement(TargetListRootX);
TextReader tgtTextFile;
try { tgtTextFile = File.OpenText(textFilePath); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
textLine = tgtTextFile.ReadLine(); //Header line -- skip
while (tgtTextFile.Peek() != -1) //skip non entry lines
{
//next entry
textLine = tgtTextFile.ReadLine();
if (textLine.Contains(",") && !textLine.Contains("["))
{
lineElements = textLine.Split(',');
//Calculate RA and Dec -> decimal values
if (lineElements.Length >= 3)
{
string nameString = Utility.ParseNameString(lineElements[0].Replace(":", " "));
double? raDouble = Utility.ParseRADecString(lineElements[1]);
double? decDouble = Utility.ParseRADecString(lineElements[2]);
//Load up xelement
if (raDouble != null && decDouble != null)
{
XElement varTarget = new XElement(TargetListRecordX,
new XElement(NameX, nameString),
new XElement(RAX, raDouble.ToString()),
new XElement(DecX, decDouble.ToString()),
new XElement(LastDateX, DateTime.MinValue));
tgtXFile.Add(varTarget);
}
}
}
}
//Save file
tgtXFile.Save(xmlFilePath);
}
public static void AddToTargetXList(string name, double ra, double dec, DateTime lastDate)
{
//Adds a Differential type of target into the the target list
Configuration cfg = new Configuration();
//If the target list file doesn't exist, then create an empty one
if (!File.Exists(cfg.TargetListPath))
{
XElement tgtXFile;
tgtXFile = new XElement(TargetListRootX);
tgtXFile.Save(cfg.TargetListPath);
}
//Load xml list and update data on target
var newRef = new XElement(TargetListRecordX,
new XElement(NameX, name),
new XElement(RAX, ra.ToString()),
new XElement(DecX, dec.ToString()),
new XElement(LastDateX, lastDate.ToString()));
XElement acnL = XElement.Load(cfg.TargetListPath);
acnL.Add(newRef);
acnL.Save(cfg.TargetListPath);
return;
}
public static void AddToTargetXList(TargetXDescriptor refX)
{
//Adds a Differential type of target into the the target list
Configuration cfg = new Configuration();
//If the target list file doesn't exist, then create an empty one
if (!File.Exists(cfg.TargetListPath))
{
XElement tgtXFile;
tgtXFile = new XElement(TargetListRootX);
tgtXFile.Save(cfg.TargetListPath);
}
var newRef = new XElement(TargetListRecordX,
new XElement(NameX, refX.Name),
new XElement(RAX, refX.RA.ToString()),
new XElement(DecX, refX.Dec.ToString()),
new XElement(LastDateX, refX.LastImagingDate.ToString()));
//Load xml list and update data on target
XElement acnL = XElement.Load(cfg.TargetListPath);
acnL.Add(newRef);
acnL.Save(cfg.TargetListPath);
return;
}
public static void DeleteFromTargetXList(string name)
{
//Adds a Differential type of target into the the target list
//Load xml list and update data on target
Configuration cfg = new Configuration();
//If the target list file doesn't exist, then create an empty one
if (!File.Exists(cfg.TargetListPath))
{
XElement tgtXFile;
tgtXFile = new XElement(TargetListRootX);
tgtXFile.Save(cfg.TargetListPath);
}
XElement acnL = XElement.Load(cfg.TargetListPath);
foreach (XElement tn in acnL.Elements(TargetListRecordX).Where(t => ((string)t.Element(NameX) == name)))
tn.Remove();
acnL.Save(cfg.TargetListPath);
return;
}
public void UpdateTargetXList(string name, double ra, double dec, DateTime lastDate)
{
//Load xml list and update data on target
Configuration cfg = new Configuration();
XElement acnL = XElement.Load(cfg.TargetListPath);
acnL.Elements(TargetListRecordX).FirstOrDefault(t => ((string)t.Element(NameX) == name)).SetElementValue(RAX, ra.ToString());
acnL.Elements(TargetListRecordX).FirstOrDefault(t => ((string)t.Element(NameX) == name)).SetElementValue(DecX, dec.ToString());
acnL.Elements(TargetListRecordX).FirstOrDefault(t => ((string)t.Element(NameX) == name)).SetElementValue(LastDateX, lastDate.ToString());
acnL.Save(cfg.TargetListPath);
return;
}
public void UpdateTargetXDate(TargetXDescriptor varTgt)
{
var newTgt = new XElement(TargetListRecordX,
new XElement(NameX, varTgt.Name,
new XElement(RAX, varTgt.RA.ToString()),
new XElement(DecX, varTgt.Dec.ToString()),
new XElement(LastDateX, varTgt.LastImagingDate.ToString())));
Configuration cfg = new Configuration();
//Load xml list and update data on target
XElement acnL = XElement.Load(cfg.TargetListPath);
acnL.Elements(TargetListRecordX).FirstOrDefault(t => ((string)t.Element(NameX) == varTgt.Name)).SetElementValue(LastDateX, DateTime.Now.ToString());
acnL.Save(cfg.TargetListPath);
return;
}
public TargetXDescriptor NextClosestTargetX(double az, double alt)
{
//Sets the first target for the session at alt/az and gets the closest target
TargetXDescriptor lastTgt = null;
Configuration cfg = new Configuration();
sky6Utils tsxu = new sky6Utils();
tsxu.ConvertAzAltToRADec(az, alt);
double ra = (double)tsxu.dOut0;
double dec = (double)tsxu.dOut1;
double minAlt = Convert.ToDouble(cfg.MinAltitude);
double minRetake = Convert.ToDouble(cfg.MinRetakeInterval);
double minSeparation = double.MaxValue;
if (targetListRecordX == null)
return lastTgt;
foreach (XElement tDesc in targetListRecordX.Elements())
{
TargetXDescriptor xTarget = new TargetXDescriptor(tDesc);
if (((DateTime.Now - xTarget.LastImagingDate) > TimeSpan.FromHours(minRetake)) && (IsUp(xTarget.RA, xTarget.Dec, minAlt)))
{
double targetSeparation = TargetXSeparation(ra, dec, xTarget.RA, xTarget.Dec);
if (targetSeparation < minSeparation)
{
lastTgt = xTarget;
minSeparation = targetSeparation;
}
}
}
////By now, either we have a target name or not. Make sure the calling function checks for a null name.
return lastTgt;
}
public TargetXDescriptor NextClosestTargetX(TargetXDescriptor lastTgt)
{
// Gets the next target for targeting
// Based on finding the closest target to the last target
// that is above the minimum altitude
{
//Set access to configuration information about minimum altitude
Configuration cfg = new Configuration();
double minAlt = Convert.ToDouble(cfg.MinAltitude);
double minRetake = Convert.ToDouble(cfg.MinRetakeInterval);
double minSeparation = double.MaxValue;
bool foundNewTarget = false;
TargetXDescriptor nextTgt = lastTgt;
foreach (XElement tDesc in targetListRecordX.Elements())
{
TargetXDescriptor xTarget = new TargetXDescriptor(tDesc);
if (((DateTime.Now - xTarget.LastImagingDate) > TimeSpan.FromHours(minRetake)) && (IsUp(xTarget.RA, xTarget.Dec, minAlt)))
{
foundNewTarget = true;
double targetSeparation = TargetXSeparation(lastTgt.RA, lastTgt.Dec, xTarget.RA, xTarget.Dec);
if (targetSeparation < minSeparation)
{
nextTgt = xTarget;
minSeparation = targetSeparation;
}
}
}
////By now, either we have a target name or not. Make sure the calling function checks for a null name.
if (foundNewTarget)
return nextTgt;
else return null;
}
}
private double TargetXSeparation(double ra1, double dec1, double ra2, double dec2)
{
//Computes the angular distance between two polar coordinates using TSX utility function
//
sky6Utils tsx_ut = new sky6Utils();
tsx_ut.ComputeAngularSeparation(ra1, dec1, ra2, dec2);
double dist = tsx_ut.dOut0;
return dist;
}
public bool IsUp(double ra, double dec, double minAlt)
{
sky6Utils tsxu = new sky6Utils();
tsxu.ConvertRADecToAzAlt(ra, dec);
double alt = tsxu.dOut1;
if (minAlt > alt) return false;
else return true;
}
public void NameSortTargetXList()
{
//Sort List based on name
IEnumerable<XElement> sortedX =
from el in targetListRecordX.Elements(TargetListRecordX)
let tName = el.Element(RAX).Value
orderby tName
select el;
targetListRecordX = new XElement(TargetListRecordX);
//AGNListX.RemoveAll();
foreach (var el in sortedX)
targetListRecordX.Add(el);
return;
}
public class TargetXDescriptor
{
private XElement xTarget;
public TargetXDescriptor(string name, double ra, double dec)
{
xTarget = new XElement(TargetListRecordX,
new XElement(NameX, name),
new XElement(RAX, ra.ToString()),
new XElement(DecX, dec.ToString()),
new XElement(LastDateX, DateTime.MinValue));
}
public TargetXDescriptor(XElement variTarget)
{
xTarget = variTarget;
}
public string Name
{
get { return xTarget.Element(NameX).Value; }
set { xTarget.Element(NameX).Value = value.ToString(); }
}
public double RA
{
get { return Convert.ToDouble(xTarget.Element(RAX).Value); }
set { xTarget.Element(RAX).Value = value.ToString(); }
}
public double Dec
{
get { return Convert.ToDouble(xTarget.Element(DecX).Value); }
set { xTarget.Element(DecX).Value = value.ToString(); }
}
public double Exposure
{
get { return Convert.ToDouble(xTarget.Element(ExposureX).Value); }
set
{
if (xTarget.Element(ExposureX) == null)
xTarget.Add(new XElement(ExposureX, value.ToString()));
else xTarget.Element(ExposureX).Value = value.ToString();
}
}
public DateTime LastImagingDate
{
get { return Convert.ToDateTime(xTarget.Element("LastImagingDate").Value); }
set
{
if (xTarget.Element(LastDateX) == null)
xTarget.Add(new XElement(LastDateX, value.ToString()));
else xTarget.Element(LastDateX).Value = value.ToString();
}
}
}
}
}