forked from elnjensen/Tapir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaladin-tess.js
More file actions
1606 lines (1451 loc) · 54.9 KB
/
aladin-tess.js
File metadata and controls
1606 lines (1451 loc) · 54.9 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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Javascript functions to work with the Aladin Lite finder interface
for the TESS transit finder, specifically the code in aladin.html.
Some of these are standalone utility functions, but a number of
them refer specifically to global variables or HTML elements in
that file. Placing them in a separate file here allows browsers to
cache this part of the code, reducing bandwidth needed.
Copyright 2012-2023 Eric Jensen, ejensen1@swarthmore.edu.
This file is part of the Tapir package, a set of (primarily)
web-based tools for planning astronomical observations. For more
information, see the README.txt file or
http://astro.swarthmore.edu/~jensen/tapir.html .
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program, in the file COPYING.txt. If not, see
<http://www.gnu.org/licenses/>.
*/
function sleep(ms) {
/* Simple sleep function - only works within async functions. See
https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
*/
return new Promise(resolve => setTimeout(resolve, ms));
}
// Asynchronously find the nearest Vizier mirror
// based on the user's IP address:
async function setVizier(ip='') {
vizURL = await nearestVizierURL(ip);
if (vizURL != '') {
vizierServer = vizURL;
console.log('Using Vizier server: ', vizURL);
// Save it for next time:
localStorage.setItem('VizierURL', vizURL)
}
}
async function getToiInfo(target) {
// Query our local target list of TOIs to get the coordinates,
// magnitude and transit depth. Returns a JSON object. If status
// = 1, other fields should be populated. If status = 0, the
// target was not found or the name could not be interpreted.
let toidata;
const result = await $.get('tess_transit_depth.cgi?name=' + target,
success=function(data) {
toidata = data;
}
).fail(function(jqXHR, textStatus, errorThrown) {
alert("Failed to call tess_transit_depth.");
console.log("Status, error: ", textStatus, errorThrown);
toidata = { "status": 0 };
});
if (toidata.status) {
console.log("Resolved target " + target + " in getToiInfo.");
} else {
console.log("Did not find target " + target + " in getToiInfo.");
}
console.log(toidata);
return toidata;
}
async function resolveCoords(target) {
// Try to resolve coordinates of the given target. First we try
// locally with our TOI file, and if that fails, we try Sesame.
// As a side effect, if we succeed in resolving it locally, we
// also set target magnitude and depth if they are not already
// set.
info = await getToiInfo(target);
if (info.status) {
// Got the values, set vars:
// Convert RA from hours to degrees:
ra_center = sexagesimalToDecimal(info.RA)*15.;
dec_center = sexagesimalToDecimal(info.Dec);
ra_string = ra_center;
if (! depth) {
depth = parseFloat(info.depth);
depthDeltaMag = -2.5*Math.log10((depth)/1000.);
}
if (! Tmag) {
Tmag = parseFloat(info.Tmag);
}
if (! tic_number) {
tic_number = (info.name).replace(/^TIC */, '');
// Then possibly trailing .01, .02 etc., which ExoFOP doesn't like:
tic_number = tic_number.split('.')[0];
}
have_center_coords=true;
} else {
// Didn't succeed there, try Sesame instead. If the TIC name
// passed had a trailing .01, .02, etc., remove it before
// passing to Sesame since that won't be resolved in the TIC.
let matchname = target.replace(/^(TIC\s*\d+)\.\d+$/,'$1');
Sesame.resolve(matchname,
function(data) {
d = data.Target.Resolver;
ra_center=d.jradeg;
dec_center=d.jdedeg;
ra_string = d.jradeg;
have_center_coords=true;
console.log('Resolved '+matchname+': '+d.jpos);
if (d.pm) {
console.log('Proper motion is pmra='+d.pm.pmRA+', pmdec='+d.pm.pmDE);
};
},
function(){alert('Could not resolve '+matchname+' into coordinates.')}
);
}
}
async function findSurveyCoverage() {
// Query the MOC server to determine which surveys cover this field,
// so we know which options to offer in the menu. This is an
// asynchronous query, so other page loading continues while this
// query executes.
var moc_url = 'https://alasky.unistra.fr/MocServer/query?expr=' +
'(ID%3D*SDSS9*color||ID%3D*PanSTARRS*color*)&get=id&fmt=ascii&RA='+
ra_center+'&DEC='+dec_center+'&SR=0.2&intersect=enclosed';
$.ajax({
url: moc_url,
method: 'GET',
dataType: 'text',
start_time: new Date().getTime(),
success: function(surveys) {
timeMS = new Date().getTime() - this.start_time;
console.log('Request to ' + moc_url + ' took ' +
timeMS + ' ms.');
console.log('Request output: '+surveys);
const disabledColor = '#AAAAAA'; // gray
if (surveys.includes('PanSTARRS')) {
console.log('Pan-STARRS covers this field.');
} else {
document.getElementById('panstarrs-button').checked=false;
document.getElementById('panstarrs-button').disabled='disabled';
document.getElementById('panstarrs-label').style.color=disabledColor;
console.log('No Pan-STARRS coverage.');
}
if (surveys.includes('SDSS')) {
console.log('SDSS covers this field.');
} else {
document.getElementById('sdss-button').disabled='disabled';
document.getElementById('sdss-label').style.color=disabledColor;
console.log('No SDSS coverage.');
}
},
error: function(err) {
console.log('Could not complete request for MOC url '+moc_url);
}
});
}
function setupCentralStar() {
// Mark the central star if one was passed in. We may update
// coords later if they get shifted based on proper motion.
centralStar = A.catalog({name: 'Central star', sourceSize: 18, color: colors.centralStar});
if (starname != '') {
aladin.addCatalog(centralStar);
// Set the popup text with some useful information and links:
var popupCode = '<em>Tmag:</em> ' + Tmag + '<br/><em>Transit depth:</em> ' +
depth + ' ppt<br/><br/>';
// ExoFOP URL:
popupCode += '<a target="_blank" ' +
'href="https://exofop.ipac.caltech.edu/tess/target.php?id=' +
tic_number + '">ExoFOP</a>';
// Simbad URL:
popupCode += ' <a target="_blank" href="http://simbad.u-strasbg.fr/simbad/sim-coo?' +
'output.format=HTML&Radius=15&Radius.unit=arcsec&Coord=' +
field_center + '">Simbad</a>';
centralStar.addSources([A.marker(ra_center, dec_center,
{popupTitle: starname,
popupDesc: popupCode}
)]);
}
}
/* Query the TIC catalog first; we will use this to update the field center
coordinates to account for proper motion, so that all distances can
be calculated relative to that center at the current epoch.
*/
function currentEpoch() {
/* Return the current epoch in units of years, i.e. as a year with
* a fractional part. This allows calculation of number of years
* since a given epoch for purposes of applying proper motion
* corrections.
*/
const currentYear = new Date().getUTCFullYear();
const jan1 = new Date(Date.UTC(currentYear, 0));
// Above times are in ms, so do conversion.
return currentYear + (Date.now() - jan1)*ms2year;
}
/* Hash to hold all of the TIC sources, keyed by *Gaia* DR2 source
ID. This allows us to try to match the right Gaia DR3 source to its
TIC counterpart, using some of the fields in the TIC to do some
sanity checking. */
var TICCrossRef = {};
async function shiftCatalogTIC(sources) {
/* Callback function to process the TIC catalog returned by a
Vizier query. Take a catalog of sources as input, and shift
positions to the current epoch based on proper motions. Also
updates the field center to the new coordinates (based on known
TIC number of the central star, and recalculates the separation
of each source from that center. Sets a global boolean
TIC_is_done to allow other functions to know when this update
has completed.
*/
// If there is no starname defined, then the only input was the
// coordinates. In that case, use the TIC source closest to the
// center as the target star. Also see if we can find it in the
// local target file to get its depth and TOI number.
if ((starname == '') && (sources.length > 0)) {
console.log("No central star name passed in.");
tic_number = sources[0].data.TIC;
starname = 'TIC ' + tic_number;
console.log("Setting from nearest TIC source: " + starname);
Tmag = parseFloat(sources[0].data.Tmag);
// Try to get further info from local target file:
info = await getToiInfo(starname);
if (info.status && (! depth)) {
depth = parseFloat(info.depth);
depthDeltaMag = -2.5*Math.log10((depth)/1000.);
}
setupCentralStar();
} else if ((! depth) || (! Tmag)) {
// Could have a central star name but no depth and/or mag; see
// if we can resolve that from local target file:
info = await getToiInfo(starname);
if (info.status) {
if (! depth) {
depth = parseFloat(info.depth);
depthDeltaMag = -2.5*Math.log10((depth)/1000.);
console.log("No depth provided but found " + depth + " from local TOI file.");
}
if (! Tmag) {
Tmag = parseFloat(info.Tmag);
}
}
}
// If we still don't have a Tmag from the above, pull it from the
// closest TIC source:
if (! Tmag) {
console.log("Setting Tmag from nearest TIC source: " + sources[0].data.TIC);
Tmag = parseFloat(sources[0].data.Tmag);
}
const current_epoch = currentEpoch();
// Will update this if/when we find the TIC entry for the central star:
var centralStarIndex = -1;
for (i=0; i < sources.length; i++) {
sources[i].data.i = i;
// Get delta T mag:
T = parseFloat(sources[i].data.Tmag);
if (isNaN(T) || Tmag == '') {
sources[i].data.DeltaT = '';
} else {
sources[i].data.DeltaT = (T - Tmag).toPrecision(3);
}
if (sources[i].data.r_Pos.includes('gaia2')) {
epoch = 2015.5;
} else if (sources[i].data.r_Pos.match(/tmm?gaia/)) {
// Matches either tmgaia or tmmgaia
epoch = 2015.0;
} else if ((sources[i].data.r_Pos == '2mass') ||
(sources[i].data.r_Pos == '2MASSEXT')){
epoch = 1999.3; // estimated from survey dates
} else {
// We don't know epoch of coords:
console.log('Source TIC ' + sources[i].data.TIC +
' has position source: '
+ sources[i].data.r_Pos);
console.log('Epoch of coords is unknown, not applying proper motion.');
epoch = '';
}
// Use epoch of coords to get time span:
span = current_epoch - epoch;
pmRA = parseFloat(sources[i].data.pmRA);
pmDE = parseFloat(sources[i].data.pmDE);
RAorig = parseFloat(sources[i].data.RAOdeg);
DEorig = parseFloat(sources[i].data.DEOdeg);
if (isNaN(pmRA) || isNaN(RAorig) || epoch == '') {
sources[i].data.rashift = 0.;
} else {
// pmRA has cos(dec) factored in, so account for
// that when shifting RA coordinate:
sources[i].ra = RAorig + pmRA*span*mas2deg/Math.cos(sources[i].dec * deg2rad);
sources[i].data.RaNow = sources[i].ra;
sources[i].data.rashift = (pmRA*span).toPrecision(3) + ' mas';
};
if (isNaN(pmDE) || isNaN(DEorig) || epoch == '') {
sources[i].data.decshift = 0.;
} else {
sources[i].dec = DEorig + pmDE*span*mas2deg;
sources[i].data.DecNow = sources[i].dec;
sources[i].data.decshift = (pmDE*span).toPrecision(3) + ' mas';
};
// If this is the field-center star, update the center coords:
if (sources[i].data.TIC == tic_number) {
console.log("Found central star as source ", i);
centralStarIndex = i;
// Update the field center coordinates:
ra_center = sources[i].ra;
dec_center = sources[i].dec;
center_coo = new Coo(ra_center, dec_center);
// If no T mag was passed in, get it from this catalog entry:
if (Tmag == '') {
Tmag = parseFloat(sources[i].data.Tmag);
// Also update the popup for the central star symbol:
let p = centralStar.sources[0].popupDesc;
p = p.replace(/(<em>Tmag:<\/em> )(<br\/>)/, '$1' + Tmag + '$2');
centralStar.sources[0].popupDesc = p;
}
// Make a note of the Gaia ID of this entry so we can
// exclude it from the list of blending stars later:
centralStar.gaia = sources[i].data.GAIA;
centralStar.Tmag = sources[i].data.Tmag;
// Most of the time this block won't run since i = 0;
if (i > 0) {
alert("TIC entry matching entered name is not closest" +
" to the center coords. Please ask Karen or Eric" +
" to check the coords for TIC " +
sources[i].data.TIC + ".");
// If it happens that the central star is not first in the list,
// go back and update the previous entries:
for (j=0; j < i; j++) {
console.log("Updating _r for source ", j);
dist = center_coo.distance(new Coo(sources[j].ra,
sources[j].dec));
// Convert distance from center to arcsec:
sources[j].data._r = (dist * 3600).toPrecision(3) + '"';
}
}
}
// Update the distance from the center, using new coordinates:
sources[i].data.coo = new Coo(sources[i].ra,
sources[i].dec);
dist = center_coo.distance(sources[i].data.coo);
// Convert distance from center to arcsec:
sources[i].data._r = (dist * 3600).toPrecision(3) + '"';
// Save the deltaT and Tmag for the sources that have Gaia
// IDs:
if (sources[i].data.GAIA != '') {
const g = sources[i].data.GAIA;
TICCrossRef[g] = sources[i].data;
deltaT_vals[g] = sources[i].data.DeltaT;
Tmag_vals[g] = sources[i].data.Tmag;
TIC_vals[g] = sources[i].data.TIC;
}
}
TIC_is_done = true;
// Update the label with number of sources:
document.getElementById('tic-N-sources').innerHTML = sources.length;
// Update the central star coordinates and popup:
if (centralStar.sources.length > 0) {
centralStar.sources[0].ra = ra_center;
centralStar.sources[0].dec = dec_center;
if (centralStarIndex != -1) {
// Add TIC info to popup:
const t = sources[centralStarIndex].data;
var m = '<br/><br/><i>TIC catalog entry:</i>'
m += '<br/><div class="aladin-marker-measurement">';
m += '<table>';
for (var key in t) {
m += '<tr><td>' + key + '</td><td>' + t[key] + '</td></tr>';
}
m += '</table>';
m += '</div>';
centralStar.sources[0].popupDesc += m;
}
centralStar.reportChange();
}
}
function vizierURL(vizCatId, center, radiusDegrees, search_options) {
var url = vizierServer + '/viz-bin/votable?-source=' + vizCatId + '&-c=' +
encodeURIComponent(center) + '&-out.max=100000&-c.rd=' + radiusDegrees;
if (! search_options == '') {
url = url + search_options;
}
return url;
};
function setupTIC() {
var extra_options = "&-sort=_r&-out.all";
var radius_deg = 5/60.;
TIC = A.catalogFromURL(vizierURL('IV/38/tic', field_center, radius_deg, extra_options),
{onClick: TICpopup,
name: 'TIC',
color: colors.tic,
shape: 'cross'},
shiftCatalogTIC,
true);
aladin.addCatalog(TIC);
// Default to not showing initially, unless boolean passed as an argument:
TIC.isShowing = showTIC;
let checkbox = document.getElementById('tic');
checkbox.checked = showTIC;
toggleElements(checkbox);
}
function TICpopup(s) {
/* Function for showing a customized popup for TIC sources; much
is copied out of the Aladin code for showPopup, but this allows
us to construct our own content for the popup.
*/
var view = s.catalog.view;
var d = s.data;
view.popup.setTitle('<b>TIC ' + d.TIC + '</b><br/><br/>');
var m = '<div class="equation">';
m += '<div>T = ' + d.Tmag + '</div>';
m += '<div>ΔT = ' + d.DeltaT + '</div>';
m += '<div>r = ' + d._r + '</div>';
m += '</div>'
m += '<br/><div class="aladin-marker-measurement">';
m += '<table>';
for (var key in s.data) {
m += '<tr><td>' + key + '</td><td>' + s.data[key] + '</td></tr>';
}
m += '</table>';
m += '</div>';
view.popup.setText(m);
view.popup.setSource(s);
view.popup.show();
}
// Mark - done with section for querying the TIC, though at this
// point in the code execution the query may not have completed yet.
/* Now query the Gaia catalog. We handle this is in much the same
way, but it's a little simpler since we know the epoch of all
sources is the same. As we're shifting stars, we also keep track
of a subset that have the right distance and delta magnitude to
possibly contaminate the TESS detection.x */
/* Since new Gaia data releases will keep coming out, make sure
to define some relevant variables together here, so we'll
notice to change epoch if the catalog changes.
*/
// EDR3 is: I/350/gaiaedr3 ; DR2 is: I/345/gaia2
gaiaVizierCatalog = 'I/355/gaiadr3';
gaiaEpoch = 2016.0; // DR2 is 2015.5
// ---------- Gaia section: -------------
/* Hash to hold all of the Gaia sources, keyed by source ID. This
allows us to use the proper-motion-shifted coordinates created here
to update the positions of the variables and EBs as well. */
var gaiaCrossRef = {};
// Same for variable stars and EBs:
var gaiaEBCrossRef = {};
var gaiaVarCrossRef = {};
function setupGaia() {
let extra_options = "&-sort=_r&-out.all";
let radius_deg = 5/60.;
// Start with defining basic properties for this catalog, but no actual sources
// until we do the main Gaia query and add a subset from there.
gaiaBlends = A.catalog({name: 'Possible Gaia blends',
sourceSize: 12,
shape: 'square',
color: colors.gaiaBlends,
onClick: gaiaPopup});
gaiaAll = A.catalogFromURL(vizierURL(gaiaVizierCatalog, field_center,
radius_deg, extra_options),
{onClick: gaiaPopup,
name: 'All Gaia stars',
sourceSize: 14,
color: colors.gaiaAll,
shape: 'circle'},
shiftCatalogGaia,
true);
aladin.addCatalog(gaiaAll);
gaiaAll.isShowing = false;
// Add this last so it's on top:
aladin.addCatalog(gaiaBlends);
gaiaBlends.isShowing = false;
// Define this boundary so it's global, but don't add content until
// we have time to redefine the field center:
Gaia_boundary = A.graphicOverlay(gaia_options);
aladin.addOverlay(Gaia_boundary);
Gaia_boundary.isShowing = false;
// Overlaid layers are by default not shown initially,
// unless the boolean is set in the URL params:
var checkbox = document.getElementById('gaiaAll');
checkbox.checked = showGaia;
toggleElements(checkbox);
if (depth) {
var checkbox = document.getElementById('gaia');
checkbox.checked = showGaiaBlends;
toggleElements(checkbox);
}
}
async function setupGaiaEBs() {
// Query Vizier for Gaia sources that might be EBs - separate
// table in DR3
let extra_options = "&-sort=_r&-out.all";
let radius_deg = 5/60.;
// Since the TIC callback resets the field center, ideally we
// want it to be done before starting here. But don't keep
// waiting indefinitely - after 10 seconds give up and move on.
var waited = 0;
while ((! TIC_is_done) && (waited < 10000)) {
await sleep(100);
waited += 100; // milliseconds
}
gaiaEBs = A.catalogFromURL(vizierURL('I/358/veb', field_center,
radius_deg, extra_options),
{onClick: gaiaEBPopup,
name: 'Gaia EB candidates',
sourceSize: 14,
color: colors.gaiaEBs,
shape: 'triangle'},
gaiaEBdone,
true);
aladin.addCatalog(gaiaEBs);
gaiaEBs.isShowing = false;
// Overlaid layers are by default not shown initially,
// unless the boolean is set in the URL params:
let checkbox = document.getElementById('ebs');
checkbox.checked = showEBs;
toggleElements(checkbox);
}
async function setupGaiaVars() {
// Query Vizier for Gaia sources that are variable - separate
// table in DR3
let extra_options = "&-sort=_r&-out.all";
let radius_deg = 5/60.;
// Since the TIC callback resets the field center, ideally we
// want it to be done before starting here. But don't keep
// waiting indefinitely - after 10 seconds give up and move on.
var waited = 0;
while ((! TIC_is_done) && (waited < 10000)) {
await sleep(100);
waited += 100; // milliseconds
}
gaiaVars = A.catalogFromURL(vizierURL('I/358/vclassre', field_center,
radius_deg, extra_options),
{onClick: gaiaVarPopup,
name: 'Gaia variables',
sourceSize: 14,
color: colors.gaiaVars,
shape: 'rhomb'},
gaiaVarsDone,
true);
aladin.addCatalog(gaiaVars);
gaiaVars.isShowing = false;
// Defer code about toggling the checkbox and deciding whether to
// show catalog to the gaiaVarsDone callback so we can remove EBs
// (shown separately) first.
}
async function gaiaEBdone(sources) {
console.log("Loaded Gaia EBs: " + sources.length + " sources.")
// Update the label with number of sources:
document.getElementById('gaia-ebs-N-sources').innerHTML = sources.length;
/* Wait for the shift of Gaia coordinates due to proper motion, then
apply shifted coordinates to these sources as well: */
var waited = 0;
while ((! gaiaShiftDone) && (waited < 10000)) {
await sleep(100);
waited += 100; // milliseconds
}
for (i=sources.length - 1; i >= 0; i--) {
s = sources[i];
d = s.data;
gaiaID = s.data.Source;
if (gaiaID in gaiaCrossRef) {
// Copy the proper-motion-shifted coords:
s.ra = gaiaCrossRef[gaiaID].ra;
s.dec = gaiaCrossRef[gaiaID].dec;
};
// Construct the info string used in pop-ups, so it can
// also show when clicking on the regular Gaia symbols:
period = 1/d.Freq;
title = '<br/>Gaia EB candidate,<br//>P = ' + period.toFixed(2) + ' days<br/>';
//title += 'Solution percentile: ' + (d.Rank*100).toFixed(0) + '%<br/>';
title += 'Model type: ' + d.ModelType;
// Save it in the hash:
gaiaEBCrossRef[gaiaID] = title;
};
gaiaEBs.reportChange();
// Check if the central star is an EB, and if so, change the popup:
if (centralStar.gaia in gaiaEBCrossRef) {
centralStar.sources[0].popupDesc = gaiaEBCrossRef[centralStar.gaia] +
"<br/><br/>" + centralStar.sources[0].popupDesc;
}
}
async function gaiaVarsDone(sources) {
n_vars = sources.length;
console.log("Loaded Gaia variables: " + n_vars + " sources.");
// Now remove the EBs from this catalog. Work in
// reverse so that index numbers of sources yet
// to be processed don't change as we remove:
removed = 0;
for (i=gaiaVars.sources.length - 1; i >= 0; i--) {
source = gaiaVars.sources[i];
if (source.data.Class == 'ECL') {
gaiaVars.remove(source);
removed++;
}
}
console.log("Removed " + removed + " EB(s) from variable catalog.");
// Now that we've cleaned up EBs, which can show if specified.
// Overlaid layers are by default not shown initially,
// unless the boolean is set in the URL params:
let checkbox = document.getElementById('vars');
checkbox.checked = showVars;
toggleElements(checkbox);
// Update the label with number of sources:
document.getElementById('gaia-vars-N-sources').innerHTML = n_vars - removed;
/* Wait for the shift of Gaia coordinates due to proper motion, then
apply shifted coordinates to these sources as well: */
var waited = 0;
while ((! gaiaShiftDone) && (waited < 10000)) {
await sleep(100);
waited += 100; // milliseconds
}
for (i=gaiaVars.sources.length - 1; i >= 0; i--) {
s = gaiaVars.sources[i];
d = s.data;
gaiaID = s.data.Source;
if (gaiaID in gaiaCrossRef) {
// Copy the proper-motion-shifted coords:
s.ra = gaiaCrossRef[gaiaID].ra;
s.dec = gaiaCrossRef[gaiaID].dec;
}
// Construct the info string used in pop-ups, so it can
// also show when clicking on the regular Gaia symbols:
varType = gaiaVarTypes();
title = '<br/>Gaia variable<br/><i>' + varType[d.Class] + '<br/>';
title += '(Confidence: ' + (d.ClassSc*100).toFixed(0) + '%)</i>';
// Save it in the hash:
gaiaVarCrossRef[gaiaID] = title;
}
gaiaVars.reportChange();
// Check if the central star is a var, and if so, change the popup:
if (centralStar.gaia in gaiaVarCrossRef) {
centralStar.sources[0].popupDesc = gaiaVarCrossRef[centralStar.gaia] +
"<br/><br/>" + centralStar.sources[0].popupDesc;
}
}
function TfromGaia(source) {
/* Estimate TESS T mag from data in Gaia entry
following the prescription from the TIC v8
paper. Falls back on r mag if G is undefined.
May return a NaN value if not enough
photometry is present in the source entry
passed in.
*/
let gmag = parseFloat(source.data.Gmag);
let rmag = parseFloat(source.data.RPmag);
let color = parseFloat(source.data['BP-RP']);
let mag = null; // which mag we will actually use
if (isNaN(gmag)) {
// Fall back on R mag, different offset:
mag = rmag;
} else {
if (isNaN(color)) {
T_gaia = gmag - 0.43;
} else {
// Calculate the approximate Gaia source T mag from the Gaia
// photometry, following Eq. 1 of the TIC paper (Stassun et al.
// 2019).
T_gaia = gmag - 0.00522555*color**3 + 0.0891337*color**2 -
0.633923*color + 0.0324473;
}
mag = T_gaia;
}
return mag;
}
async function shiftCatalogGaia(sources) {
/* Callback function to process the Gaia catalog returned by a
Vizier query. Take a catalog of sources as input, and shift
positions to the current epoch based on proper motions. This
has a decent amount of code overlap with the shiftCatalogTIC,
but it's a little simpler because the Gaia catalog is more
homogeneous.
*/
// Since the TIC callback resets the field center, ideally we
// want it to be done before starting here. But don't keep
// waiting indefinitely - after 10 seconds give up and move on.
var waited = 0;
while ((! TIC_is_done) && (waited < 10000)) {
await sleep(100);
waited += 100; // milliseconds
}
const current_epoch = currentEpoch();
var neighbors = [];
var neighborDistances = [];
let lastDistance = -1;
var centralStarData = null;
for (i=0; i < sources.length; i++) {
sources[i].data.i = i;
// Use epoch of coords to get time span:
span = current_epoch - gaiaEpoch;
pmRA = parseFloat(sources[i].data.pmRA);
pmDE = parseFloat(sources[i].data.pmDE);
RAorig = parseFloat(sources[i].data.RA_ICRS);
DEorig = parseFloat(sources[i].data.DE_ICRS);
if (isNaN(pmRA) || isNaN(RAorig)) {
sources[i].data.rashift = 0.;
} else {
// pmRA has cos(dec) factored in, so account for
// that when shifting RA coordinate:
sources[i].ra = RAorig + pmRA*span*mas2deg/Math.cos(sources[i].dec * deg2rad);
sources[i].data.RaNow = sources[i].ra;
sources[i].data.rashift = (pmRA*span).toPrecision(3) + ' mas';
};
if (isNaN(pmDE) || isNaN(DEorig)) {
sources[i].data.decshift = 0.;
} else {
sources[i].dec = DEorig + pmDE*span*mas2deg;
sources[i].data.DecNow = sources[i].dec;
sources[i].data.decshift = (pmDE*span).toPrecision(3) + ' mas';
};
// Save a coordinate object for separation calcs:
sources[i].data.coo = new Coo(sources[i].ra, sources[i].dec)
// Update the distance from the center, using new coordinates:
dist = center_coo.distance(sources[i].data.coo);
// Convert distance from center to arcsec:
sources[i].data._r = (dist * 3600).toPrecision(3) + '"';
// And save the raw value for possible later use:
sources[i].data.r_deg = dist.toPrecision(6);
// Check to see if this source is a potential blend that could
// be responsible for the transit, and add to another catalog
// if so.
var gmag = parseFloat(sources[i].data.Gmag);
var rmag = parseFloat(sources[i].data.RPmag);
// If we don't have Gaia mags, or don't have a central
// Tmag to compare to, we can't check this.
if (Tmag == '' || (isNaN(gmag) && isNaN(rmag))) {
sources[i].data.shiftedMag = null;
continue;
}
var mag = TfromGaia(sources[i]); // which mag we will actually use
var magOffset = null;
if (isNaN(gmag)) {
// Fall back on R mag, different offset:
magOffset = 1.0;
} else {
magOffset = 0.5;
}
// Save this mag for possible neighbor recalculation later:
sources[i].data.shiftedMag = (mag - magOffset).toFixed(3);
// Also store which mag we actually used for comparison:
sources[i].data.magUsed = mag.toFixed(3);
if ((mag <= (Tmag + depthDeltaMag + magOffset)) &&
(dist <= gaiaRadius)) {
// We have a neighbor star - add it to the list, but
// keep the list in order by angular distance from the
// center. Most of the time this just means pushing it
// onto the end of the list, but occasionally we'll need
// to search from the end of the list backward to find
// the right location to add it in.
// If the Gaia ID matches that from the TIC, *and* the magnitudes
// are relatively close, assume that this is the central star. The
// latter condition is to double-check against a reassignment of the DR2
// Gaia ID (from the TIC) to a different star in DR3. This isn't
// foolproof but should catch some cases (if they exist) of that ID
// getting split off to a much fainter neighbor.
if ((sources[i].data.Source == centralStar.gaia) &&
(Math.abs(mag - centralStar.Tmag) < 0.2)) {
// Save this separately:
sources[i].isCentralStar = true;
centralStarData = sources[i];
} else {
sources[i].isCentralStar = false;
}
j = neighbors.length - 1; // last index of array
if (dist >= lastDistance) {
// Farthest source yet, just add to the end. Since
// the initial value of lastDistance is negative,
// this will always run for the first source.
neighbors.push(sources[i]);
lastDistance = dist;
neighborDistances.push(dist);
} else {
// Find the right place to insert; here we do not
// update lastDistance, so that it stays as distance
// at end of array.
while ((j >= 0) && (neighborDistances[j] > dist)) {
j--;
}
neighbors.splice(j + 1, 0, sources[i]);
neighborDistances.splice(j + 1, 0, dist);
}
if (sources[i].isCentralStar) {
// Save the index of central star in the
// neighbors array:
centralStarIndex = j+1;
neighbors[centralStarIndex].isCentralStar = true;
}
}
// Add this source to the overall hash, indexed by source name:
gaiaCrossRef[sources[i].data.Source] = sources[i];
}
if (!centralStarData) {
// Look at the first handful of stars and see if one of them has a similar
// magnitude to the target star:
console.log("Did not match central star via Gaia ID, will try to match by T mag.");
for (i=0; i < Math.min(5, neighbors.length); i++) {
currentMag = parseFloat(neighbors[i].data.magUsed);
if (Math.abs(currentMag - centralStar.Tmag) < 0.7) {
// Mags match pretty closely, mark this as the
// central star:
console.log("Matched neighbor source " + i + " as central star, " +
"T = " + centralStar.Tmag + ", T(gaia) = " + currentMag);
neighbors[i].isCentralStar = true;
centralStarData = sources[i];
// Save the index of the central star; note that this is
// an index into the *neighbors* array.
centralStarIndex = i;
// Save the Gaia ID so we can ID this star later; note that
// this might override the DR2 Gaia ID saved from the TIC with
// an DR3 ID, but that allows us to match it unambiguously later
// if we are recalculating the neighbor list.
centralStar.gaia = sources[i].data.Source;
break;
}
}
}
// Quick way to make a shallow copy we can modify:
neighborsOnly = neighbors.map((x) => x);
if (centralStarData) {
// Don't want to include central star in neighbor symbols, so pop
// it out if found:
neighborsOnly.splice(centralStarIndex, 1);
}
gaiaBlends.addSources(neighborsOnly);
// Now add the circle with the field for the Gaia blends:
Gaia_boundary.add(A.circle(ra_center, dec_center,
gaiaRadius, gaia_options));
console.log("Done with Gaia catalog.");
// Create the link to make an AIJ apertures file of the blending stars:
// Update the labels with number of sources:
document.getElementById('gaia-N-sources').innerHTML = sources.length;
if (depth) {
// Central star is almost certainly included in the neighbors list, even if we
// didn't find it yet, so reduce label count by one:
if (neighbors.length > 0) {
document.getElementById('gaia-blends-N-sources').innerHTML = neighbors.length - 1;
}
document.getElementById('depth-input').value = parseFloat(depth);
}
// Also update the AIJ apertures list
updateAIJApertures(neighbors);
// Mark as done so other functions know:
gaiaShiftDone = true;
// Debugging function to see which stars don't match well in
// DR2 vs. DR3:
if (showMismatches) {
showGaiaMismatches();
}
}
function updateAIJApertures(neighbors) {
/* Make the list of AIJ apertures, and update the link accordingly. */
blob = createAIJApertures(neighbors);
blobURL = URL.createObjectURL(blob);
let aijLink = document.getElementById('aij-link');
aijLink.href = blobURL;
aijLink.innerHTML = "AIJ apertures";
aijLink.download = blob.name;
}
function changeGaiaNeighbors(depth) {
/* Recalculate membership for the gaiaBlends catalog, based on the
input depth in ppt. Similar to the code in the previous
function for determining Gaia neighbors, but much shorter since
much of the work has already been done above, saving key
fields.
*/
const magThreshold = Tmag - 2.5*Math.log10(depth/1000);
// We will keep two lists; one of the possibly-blending neighbors
// only, and one that also includes the central star. The latter
// is needed for the AIJ apertures list.
var neighbors = [];
var neighbors_plus = [];
let s = gaiaAll.sources;
for (i = 0; i < s.length; i++) {
if ((s[i].data.shiftedMag) &&
(s[i].data.shiftedMag <= magThreshold) &&
(s[i].data.r_deg <= gaiaRadius)) {
neighbors_plus.push(s[i]);
// If it's not the central star, save in neighbors list:
if (s[i].data.Source != centralStar.gaia) {
neighbors.push(s[i]);
}
}
}
gaiaBlends.removeAll();
gaiaBlends.addSources(neighbors);
document.getElementById('gaia-blends-N-sources').innerHTML = neighbors.length;
// Also update the AIJ apertures list, including central star:
updateAIJApertures(neighbors_plus);
}
function toSexagesimal(num, prec, plus) {
/* Convert degrees to sexagesimal. Adapted from Aladin
code; use ":" instead of space, and keep leading zero
for all fields. 'prec' (precision) is number of decimal
places to include on seconds.
*/
var sign = num < 0 ? '-' : (plus ? '+' : '');
var n = Math.abs(num);
var n1 = Math.floor(n); // d
if (n1<10) n1 = '0' + n1;
var n2 = (n-n1)*60; // M.d
var n3 = Math.floor(n2);// M
if (n3<10) n3 = '0' + n3;
var n4 = (n2-n3)*60; // S.ddd
var n5 = Numbers.format(n4, prec);
if (n5<10) n5 = '0' + n5;
return sign+n1+':'+n3+':'+n5;
}
function createAIJApertures(sources) {
/* Take an input array of Gaia sources and create an in-memory