-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarField.cs
514 lines (473 loc) · 23.2 KB
/
StarField.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// --------------------------------------------------------------------------------
// 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 TheSky64Lib;
namespace VariScan
{
public partial class StarField
{
private ccdsoftImage TSX_Image;
public bool IsImageLinked = false;
//public List<TargetData> LightSourceStars;
//public List<TargetData> validAPASSCatalogedStars;
//public List<TargetData> validGAIACatalogedStars;
private object[] lsInvMagArr;
private object[] lsInvFWHMArr;
private object[] lsInvElipsArr;
private object[] lsInvXPosArr;
private object[] lsInvYPosArr;
//private static double fieldOfView;
//private static double rightAscension;
//private static double declination;
public DateTime ImageDateTime;
public StarField(ccdsoftImage tsx_Image, SampleManager.SessionSample sample)
{
TSX_Image = tsx_Image;
//close the current tsx image if one is open
if (TSX_Image != null)
VariScanFileManager.CloseImageFile(TSX_Image);
bool FitsIsOpen = VariScanFileManager.DirectOpenFitsFilePath(TSX_Image, sample.ImagePath);
}
public StarField(ccdsoftImage tsx_Image, string fitsFilePath)
{
TSX_Image = tsx_Image;
//close the current tsx image if one is open
if (TSX_Image != null)
VariScanFileManager.CloseImageFile(TSX_Image);
bool FitsIsOpen = VariScanFileManager.DirectOpenFitsFilePath(TSX_Image, fitsFilePath);
}
public double AirMass()
{
return Convert.ToDouble(TSX_Image.FITSKeyword("AIRMASS"));
}
public DateTime FitsImageDateTime()
{
//REturns the image date from the fits file (format '2021-10-30T09:52:13.336')
string fitsUDT = TSX_Image.FITSKeyword("DATE-OBS").Split('.')[0];
string[] dsts = fitsUDT.Split('T');
string[] ds = dsts[0].Split('-');
string[] dt = dsts[1].Split(':');
int year = Convert.ToInt16(ds[0]);
int month = Convert.ToInt16(ds[1]);
int day = Convert.ToInt16(ds[2]);
int hour = Convert.ToInt16(dt[0]);
int minute = Convert.ToInt16(dt[1]) % 60;
int second = Convert.ToInt16(dt[2]);
//
DateTime utcDT = new DateTime(year, month, day, hour, minute, second);
return utcDT;
}
public List<LightSourceInventory> AssembleLightSources()
{
List<FieldLightSource> fieldStars = new List<FieldLightSource>();
//ImageLink for light sources (Insert WCS)
//Set Image Scale
double scale = TSX_Resources.GetFOVImageScale();
TSX_Image.ScaleInArcsecondsPerPixel = scale;
int ferr;
try
{
ferr = TSX_Image.InsertWCS(true);
IsImageLinked = true;
}
catch
{
IsImageLinked = false;
return null;
}
//Collect astrometric light source data from the image linking into single index arrays:
// magnitude, fmhm, ellipsicity, x and y position
lsInvMagArr = TSX_Image.InventoryArray((int)TSX_Enums.ccdsoftInventoryIndex.cdInventoryMagnitude); //Instrument magnitude
lsInvFWHMArr = TSX_Image.InventoryArray((int)TSX_Enums.ccdsoftInventoryIndex.cdInventoryFWHM); //FMHW, we think
lsInvElipsArr = TSX_Image.InventoryArray((int)TSX_Enums.ccdsoftInventoryIndex.cdInventoryEllipticity);//Ellipticity, we think
lsInvXPosArr = TSX_Image.InventoryArray((int)TSX_Enums.ccdsoftInventoryIndex.cdInventoryX);//X position, we think
lsInvYPosArr = TSX_Image.InventoryArray((int)TSX_Enums.ccdsoftInventoryIndex.cdInventoryY); //Y position, we think
List<LightSourceInventory> fieldInventory = new List<LightSourceInventory>();
for (int i = 0; i < lsInvMagArr.Length; i++)
fieldInventory.Add(new LightSourceInventory
{
Magnitude = (double)lsInvMagArr[i],
FWHM = (double)lsInvFWHMArr[i],
XPosition = (double)lsInvXPosArr[i],
YPosition = (double)lsInvYPosArr[i],
Ellipticity = (double)lsInvElipsArr[i]
});
return fieldInventory;
}
public List<FieldLightSource> PositionLightSources(List<LightSourceInventory> fieldInventory)
{
Configuration cfg = new Configuration();
//Set the maximum ADU to be the maximum imaging ADU + 10% just to make sure that brighter
// stars don't get discarded (like the target star) because of a roundoff error
double nonLinearADU = Convert.ToDouble(cfg.ADUMax) * 1.1;
//Center the skychart on the ra/dec coordinates
//Set the star chart size to 1.5 times the image width (fits the whole thing on, persumably
//ImageLinkResults tsxilr = new ImageLinkResults();
//rightAscension = tsxilr.imageCenterRAJ2000;
//declination = tsxilr.imageCenterDecJ2000;
//fieldOfView = (((double)TSX_Image.ScaleInArcsecondsPerPixel * (double)TSX_Image.WidthInPixels) * 1.5) / 3600; //in degrees
//CenterSkyChart();
List<FieldLightSource> lsStars = new List<FieldLightSource>();
if (fieldInventory == null || fieldInventory.Count == 0)
return lsStars;
foreach (LightSourceInventory ls in fieldInventory)
{
//if the source is "active" (meaning used for imagelinking)
// get it's RA/Dec and pull up its Inventory data
// Search the catalogs and create a target data object,
// and save to the culled list of targets (validStarPoints)
//Convert X,Y to RA, Dec for the light source
TSX_Image.XYToRADec(ls.XPosition, ls.YPosition);
double raStar = (double)TSX_Image.XYToRADecResultRA();
double decStar = (double)TSX_Image.XYToRADecResultDec();
FieldLightSource starData = new FieldLightSource()
{
LightSourceRA = raStar,
LightSourceDec = decStar,
LightSourceInstMag = ls.Magnitude,
LightSourceFWHM = ls.FWHM,
LightSourceEllipticity = ls.Ellipticity
};
//Get ADU at image X,Y position
// Note: the Zero Y line is the top line of the image
// Note: ran across an instance where x position exceeded max bound
// This info comes from SEXtractor, so it's unfixable
// must apply exception handler -- catch but drop starData
// as if it were saturated
int xPos = (int)ls.XPosition;
int yPos = (int)ls.YPosition;
object[] xDataLine = TSX_Image.scanLine(yPos);
int xyADU = int.MaxValue;
try { xyADU = (int)xDataLine[xPos]; }
catch { }
starData.LightSourceADU = xyADU;
starData.SourceInventory = ls;
//Remove saturated and antibloomed stars and crop image by 1/2
if (xyADU < nonLinearADU && InsideCrop(starData))
lsStars.Add(starData);
};
return lsStars;
}
public static CatalogData ClickFindCatalogData(double ra, double dec)
{
//Adds cataloged data for stars very near the input catalog data
//It is assumed that the star chart is centered and framed before calling this method
//The method first converts the ra/dec location to xy coordinates on the starchart.
// then a clickfind is executed which returns a set of cataloged stars near that location
// the cataloged location and magnitudes are acquired for the first APASS entry
// and the first Gaia entry.
// In the case of multiple APASS (improbable) or Gaia (probable), the method assumes
// that the first entry for each is the closest entry for that catalog
// Upon completion a CatalogData object for that light source is returned.
sky6ObjectInformation tsxod = new sky6ObjectInformation();
sky6StarChart tsxsc = new sky6StarChart();
tsxsc.EquatorialToStarChartXY(ra, dec);
int xC = (int)tsxsc.dOut0;
int yC = (int)tsxsc.dOut1;
//Turn on star display -- otherwise clickfind doesn't work
tsxsc.SetDisplayProperty(Sk6DisplayPropertyObjectType.OT6_STAR,
Sk6DisplayPropertySkyMode.sk6DisplayPropertySkyModeChartMode,
Sk6DisplayProperty.sk6DisplayPropertyVisible,
Sk6DisplayPropertyItem.sk6DisplayPropertyItemVisibleValue,
1);
//Retrieve list of cataloged stars near the target position
tsxsc.ClickFind(xC, yC);
int objcnt = tsxod.Count;
CatalogData fc = new CatalogData()
{
IsAPASSCataloged = false,
IsGAIACataloged = false,
};
for (int i = 0; i < objcnt; i++)
{
tsxod.Index = i;
if (!fc.IsAPASSCataloged || !fc.IsGAIACataloged)
{
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_NAME1);
string catalogedName = tsxod.ObjInfoPropOut;
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_MAG);
var catalogMag = tsxod.ObjInfoPropOut;
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_RA_2000);
var catalogRA = tsxod.ObjInfoPropOut;
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DEC_2000);
var catalogDec = tsxod.ObjInfoPropOut;
if (catalogedName.Contains("Gaia"))
{
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DB_FIELD_1); //FIlter G
var catalogMagG = Convert.ToDouble(tsxod.ObjInfoPropOut);
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DB_FIELD_2); //Filter Gbp
var catalogMagGbp = Convert.ToDouble(tsxod.ObjInfoPropOut);
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DB_FIELD_3); //Filter Gbr
var catalogMagGrp = Convert.ToDouble(tsxod.ObjInfoPropOut);
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DB_FIELD_4); //Filter Gbr
var catalogStellarTemp = Convert.ToDouble(tsxod.ObjInfoPropOut);
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DB_FIELD_5); //Filter Gbr
var catalogPhotoVar = Convert.ToString(tsxod.ObjInfoPropOut);
fc.IsGAIACataloged = true;
fc.GAIACatalogName = catalogedName;
fc.GAIACatalogRA = catalogRA;
fc.GAIACatalogDec = catalogDec;
fc.GAIACatalogMagnitudeG = catalogMagG;
fc.GAIACatalogMagnitudeGbp = catalogMagGbp;
fc.GAIACatalogMagnitudeGrp = catalogMagGrp;
fc.GAIACatalogStellarTemp = catalogStellarTemp;
fc.GAIACatalogPhotoVar = catalogPhotoVar;
}
else if (catalogedName.Contains("APASS"))
{
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_STAR_MAGB);
var catalogMagB = tsxod.ObjInfoPropOut;
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_STAR_MAGV);
var catalogMagV = tsxod.ObjInfoPropOut;
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_STAR_MAGR);
var catalogMagR = tsxod.ObjInfoPropOut;
tsxod.Property(TheSky64Lib.Sk6ObjectInformationProperty.sk6ObjInfoProp_DB_FIELD_4);
char[] s = new char[] { ' ' };
var catalogMagG = Convert.ToDouble(tsxod.ObjInfoPropOut.Split(s, StringSplitOptions.RemoveEmptyEntries)[0]);
fc.IsAPASSCataloged = true;
fc.APASSCatalogName = catalogedName;
fc.APASSCatalogRA = catalogRA;
fc.APASSCatalogDec = catalogDec;
fc.APASSCatalogMagnitudeB = catalogMagB;
fc.APASSCatalogMagnitudeV = catalogMagV;
fc.APASSCatalogMagnitudeR = catalogMagR;
//fc.APASSCatalogMagnitudeI = catalogMagI;
//fc.APASSCatalogMagnitudeU = catalogMagU;
}
}
}
return fc;
}
public static CatalogData ReadCatalogData(FieldLightSource lightSource)
{
//Adds cataloged data for stars from the lightSource registration
//fill the fields
if (lightSource.CatalogInfo != null)
return (CatalogData)lightSource.CatalogInfo;
else
return new CatalogData()
{
IsAPASSCataloged = false,
IsGAIACataloged = false,
};
}
public static (double, double) CalculateSeparations(CatalogData tgtCatEntry, double ra, double dec)
{
//Determine separation between source and APASS, and source and GAIA in arcsec?
sky6Utils tsxu = new sky6Utils();
tsxu.ComputeAngularSeparation(tgtCatEntry.APASSCatalogRA, tgtCatEntry.APASSCatalogDec, ra, dec);
double currentAPASSseparation = tsxu.dOut0;
tsxu.ComputeAngularSeparation(tgtCatEntry.GAIACatalogRA, tgtCatEntry.GAIACatalogDec, ra, dec);
double currentGAIAseparation = tsxu.dOut0;
return (currentAPASSseparation, currentGAIAseparation);
}
public bool InsideCrop(FieldLightSource ls)
{
//Returns true if the light source is inside the cropped region
// as defined by 1/2 of the field of view
int pWidth = TSX_Image.WidthInPixels;
int pHeight = TSX_Image.HeightInPixels;
double xMin = pWidth * .25;
double xMax = pWidth * .75;
double yMin = pHeight * .25;
double yMax = pHeight * .75;
double x = ls.SourceInventory.XPosition;
double y = ls.SourceInventory.YPosition;
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax)
return true;
else
return false;
}
//private static void CenterSkyChart()
//{
// //Center the skychart on the ra/dec coordinates
// //Set the star chart size to 1.5 times the image width (fits the whole thing on, persumably
// sky6StarChart tsxc = new sky6StarChart
// {
// RightAscension = rightAscension,
// Declination = declination,
// FieldOfView = fieldOfView
// };
//}
public void Close()
{
if (TSX_Image != null) VariScanFileManager.CloseImageFile(TSX_Image);
}
public TargetData FlushOutTargetDataFields(TargetData tgtData)
{
//Gets location and magnitude data on light source closest to light source RA/Dec
sky6Utils tsxu = new sky6Utils();
double ra = tgtData.TargetRA;
double dec = tgtData.TargetDec;
double currentSeparation = double.MaxValue;
double minimumSeparation = double.MaxValue;
int sIndex = -1;
for (int iSource = 0; iSource < lsInvXPosArr.Length; iSource++)
{
TSX_Image.XYToRADec((double)lsInvXPosArr[iSource], (double)lsInvYPosArr[iSource]);
double raSrc = TSX_Image.XYToRADecResultRA();
double decSrc = TSX_Image.XYToRADecResultDec();
tsxu.ComputeAngularSeparation(ra, dec, raSrc, decSrc);
currentSeparation = tsxu.dOut0;
if (currentSeparation <= minimumSeparation)
{
minimumSeparation = currentSeparation;
sIndex = iSource;
//populate lightSource targetData in anticipation...
tgtData.InventoryArrayIndex = sIndex;
tgtData.TargetToSourcePositionError = minimumSeparation * 3600.0;
tgtData.SourceRA = raSrc;
tgtData.SourceDec = decSrc;
tgtData.SourceX = (double)lsInvXPosArr[sIndex];
tgtData.SourceY = (double)lsInvYPosArr[sIndex];
tgtData.SourceInstrumentMagnitude = (double)lsInvMagArr[sIndex];
}
}
return tgtData;
}
public double? FWHMAvg_Pixels
{
get
{
if (lsInvFWHMArr != null)
return MathNet.Numerics.Statistics.ArrayStatistics.Mean(Array.ConvertAll<object, double>(lsInvFWHMArr, x => (double)x));
else
return null;
}
}
public double? EllipticityAvg
{
get
{
if (lsInvElipsArr != null)
return MathNet.Numerics.Statistics.ArrayStatistics.Mean(Array.ConvertAll<object, double>(lsInvElipsArr, x => (double)x));
else
return null;
}
}
public LightSourceInventory GetLightSourceInventory(int inventoryIndex)
{
LightSourceInventory lsi = new LightSourceInventory
{
Magnitude = (double)lsInvMagArr[inventoryIndex],
XPosition = (double)lsInvXPosArr[inventoryIndex],
YPosition = (double)lsInvYPosArr[inventoryIndex],
FWHM = (double)lsInvFWHMArr[inventoryIndex],
Ellipticity = (double)lsInvElipsArr[inventoryIndex]
};
return lsi;
}
public struct LightSourceInventory
{
public double Magnitude { get; set; }
public double XPosition { get; set; }
public double YPosition { get; set; }
public double FWHM { get; set; }
public double Ellipticity { get; set; }
}
public struct FieldLightSource
{
public double ColorStandard(ColorIndexing.ColorDataSource cds)
{
double mag = 0;
switch (cds)
{
case ColorIndexing.ColorDataSource.Instrument:
{
mag = this.LightSourceInstMag;
break;
}
case ColorIndexing.ColorDataSource.Bj:
{
mag = this.CatalogInfo.Value.APASSCatalogMagnitudeB;
break;
}
case ColorIndexing.ColorDataSource.Vj:
{
mag = this.CatalogInfo.Value.APASSCatalogMagnitudeV;
break;
}
case ColorIndexing.ColorDataSource.Rc:
{
mag = this.CatalogInfo.Value.APASSCatalogMagnitudeR;
break;
}
case ColorIndexing.ColorDataSource.Ic:
{
mag = this.CatalogInfo.Value.APASSCatalogMagnitudeI;
break;
}
case ColorIndexing.ColorDataSource.Uc:
{
mag = this.CatalogInfo.Value.APASSCatalogMagnitudeU;
break;
}
case ColorIndexing.ColorDataSource.Gp:
{
mag = this.CatalogInfo.Value.GAIACatalogMagnitudeG;
break;
}
case ColorIndexing.ColorDataSource.GBp:
{
mag = this.CatalogInfo.Value.GAIACatalogMagnitudeGbp;
break;
}
case ColorIndexing.ColorDataSource.GRp:
{
mag = this.CatalogInfo.Value.GAIACatalogMagnitudeGrp;
break;
}
}
return (mag);
}
public int? RegistrationIndex { get; set; }
public double LightSourceRA { get; set; }
public double LightSourceDec { get; set; }
public double LightSourceInstMag { get; set; }
public double LightSourceADU { get; set; }
public double LightSourceEllipticity { get; set; }
public double LightSourceFWHM { get; set; }
public CatalogData? CatalogInfo { get; set; }
public LightSourceInventory SourceInventory { get; set; }
}
public struct CatalogData
{
//Structure for encapsulating star catalog data after look up
public bool IsAPASSCataloged { get; set; }
public string APASSCatalogName { get; set; }
public double APASSCatalogRA { get; set; }
public double APASSCatalogDec { get; set; }
public double APASSCatalogMagnitudeB { get; set; }
public double APASSCatalogMagnitudeV { get; set; }
public double APASSCatalogMagnitudeR { get; set; }
public double APASSCatalogMagnitudeI { get; set; }
public double APASSCatalogMagnitudeU { get; set; }
public bool IsGAIACataloged { get; set; }
public string GAIACatalogName { get; set; }
public double GAIACatalogRA { get; set; }
public double GAIACatalogDec { get; set; }
public double GAIACatalogMagnitudeG { get; set; }
public double GAIACatalogMagnitudeGbp { get; set; }
public double GAIACatalogMagnitudeGrp { get; set; }
public double GAIACatalogStellarTemp { get; set; }
public string GAIACatalogPhotoVar { get; set; }
}
}
}