-
Notifications
You must be signed in to change notification settings - Fork 1
/
modsView.py
1873 lines (1616 loc) · 64.9 KB
/
modsView.py
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
#!/usr/bin/env python
#
# Typical System: /usr/bin/env python
# LBT MODS runtime: /lbt/mods_runtime/anaconda/bin/python
#
# modsView - View a MODS target acquisition (.acq) or imaging (.img) script
#
# Reads the contents of a MODS target acquisition (.acq) or the target
# acquisition blocks of a MODS imaging (.img) script, and displays the
# results on a ds9 window, overlaying the MODS focal plane on a
# digitized sky survey image of the target field. It also finds and
# overlays catalog stars and their R magnitudes.
#
# It tests to see if the guide star is inside the guide patrol field
# given the instrument rotator angle. If there are any telescope
# offsets as part of the target acquisition, it will test the location
# of the guide star after the offset to verify that it is still inside
# the MODS guide patrol field.
#
# Uses the Python ds9 module to interact with a named DS9 window. The
# window is launched as needed, turning off all of the IRAF imtool
# pipes so a person running IRAF will not step on this display (and
# vis-versa).
#
# Creates a temporary modsView.reg file containing the DS9 region file
# used to draw the MODS focal plane on the screen.
#
# It can also be used to create a PNG-format finder chart by using the
# --finder option.
#
# Dependencies:
# Requires your system have SAOImage DS9 and the XPA utilities
# installed and in your default path (hea-www.harvard.edu/saord/ds9/)
#
# Requires the ds9 Python module for DS9 interaction
# github.com/ericmandel/pyds9
#
# Distribution:
# The primary distribution is now on GitHub
# github.com/rwpogge/modsView
#
# Author:
# R. Pogge, OSU Astronomy Department
# 2012 May 4
#
# Modification History:
# 2012 May 04 - first beta release, does only guide-star checking
# and *very* minimal syntax checks. [rwp/osu]
# 2012 May 06 - first experiments with interactive mode, added a
# number of functions [rwp/osu]
# 2012 May 20 - Added probe shadow option (--shadow) and plot OBJNAME
# label or suppress with --nolabel [rwp/osu]
# 2012 Oct 01 - Experimental interactive version, call it 1.0.0...
# 2012 Nov 08 - Added ability to use B magnitudes in catalogs instead
# of just R, and introduced the --find option to give an
# R_mag-sorted list of candidate guide stars to pick from
# interactively (type number at prompt) [rwp/osu]
# 2012 Nov 28 - added NOMAD1 catalog support as default [rwp/osu]
# 2012 Dec 03 - restored --rotate function [rwp/osu]
# 2012 Dec 18 - fixed bug in display of the instrument rotator sweep
# and long-slit positions after offsets [rwp/osu]
# 2012 Dec 19 - minor patch, exception catching for python 2.7
# (raises exception of you try to close a catalog pane that
# is already closed - python 2.6 doesn't care) [rwp/osu]
# 2013 Jan 7 - minor bug if nonsensical min/max magnitude ranges set
# [rwp/osu]
# 2013 Mar 19 - bugs in the offseting logic [rwp/osu]
# 2013 Apr 11 - Allow for no + sign on Decs in MMS files (not strictly
# permitted, but apparent can happen), and fixed a
# previously undetected bug in guide probe shaddow
# rendering under RA/Dec offsets [rwp/osu]
# 2014 Feb 23 - adjusted guide patrol field size for changes in
# maximum Y stage travel limits with the WFS hotspot
# offset. Found conflicting limits in different parts of
# the program resulting in the patrol field being drawn
# too large, and tests failing to alert a star just outside
# the effective guide patrol field. [rwp/osu]
# ================================================================
# 2014 Apr 28 - Start of binocular MODS hooks, including support for the
# different MODS1 and MODS2 AGw parameters [rwp/osu]
# 2015 Nov 20 - First v2.0 release for MODS1 or MODS2 use [rwp/osu]
# 2016 Oct 11 - MODS1 and 2 have the same AGw WFS configuration as of
# October 2016, so old MODS1 offset hotspot has been removed,
# and --mods1/mods2 is no longer required [rwp/osu]
# 2016 Oct 15 - Minor mods for pyds9 vs ds9 back compatibility [rwp/osu]
# 2018 May 22 - Support for experimental SNS masks [rwp/osu]
# 2018 Jul 22 - Patches for Python 3 & MacOS operation, first release
# using GitHub [rwp/osu]
# 2018 Sep 05 - fixed input/raw_input problem P2/3 issue [rwp/osu]
# 2019 Nov 24 - Updated AGw patrol field coordinates [rwp/osu]
# 2022 Nov 11 - Updated for changes in XPA with ds9 version 8.x [rwp/osu]
#
#---------------------------------------------------------------------------
import sys
import os
import readline
import math
import getopt
import subprocess
import shlex
from time import sleep
from operator import itemgetter
# pyds9 has moved from SAO to github and the module name changed
try:
import pyds9 as ds9
except:
import ds9
# input vs raw_input for Python 3/2 compatibility
try:
input = raw_input
except NameError:
pass
# Version number and date, update as needed
versNum = '2.2.1'
versDate = '2022-11-11'
# Some useful global defaults (mostly so we can report them in usage)
lbtScale = 0.600 # LBT focal plane scale in mm/arcsec
minRMag = 16.5 # Typical guide star R magnitude limits for the MODS AGw unit
maxRMag = 12.0
minBMag = 16.5 # Typical guide star B magnitude limits for the MODS AGw unit
maxBMag = 11.0
fsFoV = 5.5 # Radius of the focal station field-of-view in arcminutes
defCatalog = 'nomad' # default is the NOMAD1 Catalog, other option is ub1=USNO-B1
defAGwFilt = 'R' # default AGw guide camera filter is 'R'
defServer = 'stsci' # default is the STScI image server
defSurvey = 'all' # default is the STScI composite survey image catalog
#---------------------------------------------------------------------------
#
# sex2dec - Sexagesimal to decimal conversion
#
# Inputs:
# inStr = string with the sexagesimal number in +/-dd:mm:ss.ss format
#
# Returns:
# Decimal representation of the sexagesmial string
#
# Description:
# Converts a signed or unsigned sexagesimal string in dd:mm:ss.sss
# notation into a decimal equivalent. It is agnostic about units
# (e.g., degrees or hours). Does not test validity on any range.
# It correctly handles the conversion for the -00:mm:ss.ss case.
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 2
#
def sex2dec(inStr):
bits = inStr.split(':')
dsign = 1.0
major = float(bits[0])
minor = float(bits[1])
sec = float(bits[2])
if major==0.0 and inStr.startswith('-'):
dsign = -1.0
if major < 0.0 :
decimal = major - minor/60.0 - sec/3600.0
else :
decimal = major + minor/60.0 + sec/3600.0
decimal *= dsign
return decimal
#---------------------------------------------------------------------------
#
# dec2sex - Decimal to Sexagesimal to conversion
#
# Inputs:
# angle = decimal angle to convert
#
# Returns:
# string with the sexagesimal number in +/-dd:mm:ss.ss format
#
# Description:
# Converts a floating-point decimal 'angle' into a sexagesimal
# string in dd:mm:ss.sss notation. It is agnostic about units
# (e.g., degrees or hours). Does not test validity on any range.
# The final 'seconds' part is rounded to the nearest 0.01 seconds.
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 7
#
def dec2sex(angle):
arg = math.fabs(angle)
dd = int(arg)
temp = 60.0*(arg - float(dd))
mm = int(temp)
ss = 3600.0*(arg - (float(dd)+(float(mm)/60.0)))
if angle < 0:
sexStr = '-%02d:%02d:%05.2f' % (dd,mm,ss)
else:
sexStr = '%02d:%02d:%05.2f' % (dd,mm,ss)
return sexStr
#---------------------------------------------------------------------------
#
# rdToStd - convert celestial coordinates (ra,dec) to standard coordinates
# (xi,eta) on the tanget plane.
#
# Inputs:
# ra,dec = target RA and Dec in decimal hours and degrees, respectively
# ra0,dec0 = reference (tangent) point RA and Dec in decimal hrs/deg
#
# Returns:
# xi,eta = standard coordinates in arcseconds
#
# Description:
# Given the celestial coordinates (ra,dec) of an object, and the
# celestial coordinates of the field center (ra0,dec0), compute
# standard coordinates (xi,eta) of the object in the tangent plane
# to the celestial sphere. This geometric problem is described in
# W.W. Smart, Textbook on Spherical Astronomy, Chapter XII, sections
# 160 and 161.
#
# See also stdToRD()
#
# R. Pogge, OSU Astronomy Dept
# 2012 May 3
#
def rdToStd(ra,dec,ra0,dec0):
dra = math.radians(15.0*(ra-ra0))
cosd0 = math.cos(math.radians(dec0))
sind0 = math.sin(math.radians(dec0))
tand = math.tan(math.radians(dec))
cosdra = math.cos(dra)
denom = sind0*tand + cosd0*cosdra
xi = 3600.0 * math.degrees(math.sin(dra)/denom)
eta = 3600.0 * math.degrees((cosd0*tand - sind0*cosdra)/denom)
return xi, eta
#---------------------------------------------------------------------------
#
# stdToRD - convert standard coordinates (xi,eta) on the tanget plane to
# celestial coordinates (ra,dec)
#
# Inputs:
# xi,eta = object standard coordinates in arcseconds
# ra0,dec0 = reference (tangent) point RA and Dec in decimal hrs/deg
#
# Returns:
# ra,dec = celestial coordinates in decimal hours and degrees, respetively
#
# Description:
# Given standard coordinates (xi,eta) of an object and the celestial
# coordinates of the field center (ra0,dec0), compute the celestial
# coordinates (ra,dec) of the object on the celestial sphere. This
# geometric problem is described in W.W. Smart, Textbook on Spherical
# Astronomy, Chapter XII, sections 160 and 161.
#
# See also rdToStd()
#
# R. Pogge, OSU Astronomy Dept
# 2012 May 3
#
def stdToRD(xi,eta,ra0,dec0):
xi_r = math.radians(xi/3600.0)
eta_r = math.radians(eta/3600.0)
ra0_r = math.radians(15.0*ra0)
cosd0 = math.cos(math.radians(dec0))
tand0 = math.tan(math.radians(dec0))
denom = 1.0 - eta_r*tand0
dra = math.atan(xi_r/(cosd0*denom))
ra = math.degrees(ra0_r+dra)/15.0
dec = math.degrees(math.atan((math.cos(dra)*(eta_r+tand0))/denom))
return ra, dec
#---------------------------------------------------------------------------
#
# inTriangle() - Test to see if (x,y) is inside a triangle
#
# Inputs:
# x1,y1,x2,y2,x3,y3 = Cartesian coordinates of the triangle vertices
# x,y = Cartesian coordinates of the test point.
#
# Returns:
# True if (x,y) is inside the triangle, False if outside.
#
# Description:
# Solves this classic geometry problem by transforming the
# coordinates of the triangle vertices and the test point into the
# barycentric coordinate system of the triangle.
#
# A very lucid description of the problem is in the Wikipedia
# article on the barycentric coordinate system:
#
# en.wikipedia.org/wiki/Barycentric_coordinate_system_(mathematics)
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 2
#
def inTriangle(x1,y1,x2,y2,x3,y3,x,y):
n1 = (y2-y3)*(x -x3) + (x3-x2)*(y -y3)
d1 = (y2-y3)*(x1-x3) + (x3-x2)*(y1-y3)
n2 = (y3-y1)*(x -x3) + (x1-x3)*(y -y3)
d2 = (y2-y3)*(x1-x3) + (x3-x2)*(y1-y3)
if d1 != 0.0:
lam1 = n1/d1
else:
lam1 = 0.0
if d2 !=0.0:
lam2 = n2/d2
else:
lam2 = 0.0
lam3 = 1 - lam1 - lam2
if (lam1<0.0 or lam2<0.0 or lam3<0.0):
return False
else:
return True
#---------------------------------------------------------------------------
#
# inBox - is (x,y) inside a given rectangle at a given field rotation?
#
# Inputs:
# x,y = coordinates of the test point
# rect = rectangle (unrotated frame)
# rotAng = rotation angle about (0,0)
#
# Breaks the box into two triangles and then tests the point against
# these triangles using inTriangle()
#
def inBox(x,y,rect,rotAng):
(xc,yc,dx,dy) = rect;
# Coordinates of the center and rectangle vertices after rotation
(xr0,yr0)=rotXY(xc,yc,-posAng)
(xr1,yr1)=rotXY(xc-dx/2,yc-dy/2,-rotAng)
(xr2,yr2)=rotXY(xc+dx/2,yc-dy/2,-rotAng)
(xr3,yr3)=rotXY(xc+dx/2,yc+dy/2,-rotAng)
(xr4,yr4)=rotXY(xc-dx/2,yc+dy/2,-rotAng)
# Slice the guide patrol field diagonally into two triangles. The
# guide star must be inside one or other triangle
if inTriangle(xr1,yr1,xr2,yr2,xr3,yr3,x,y) or \
inTriangle(xr1,yr1,xr3,yr3,xr4,yr4,x,y):
return True
else:
return False
#---------------------------------------------------------------------------
#
# rotXY - rotate XY coordinates
#
# Inputs:
# x,y = float positions relative to origin
# rotAng = rotation angle in degrees
#
# Returns:
# xr,yr = (x,y) in the rotated frame
#
# Description:
# Convenience function to evaluate the standard Cartesian 2D
# coordinate system rotation. Note that for applying this to
# astronomical standard coordinates (xi,eta), the helicity of rotAng
# has the opposite sign (e.g., compute xi,eta when rotating by
# celestial position angle posAng, use rotAng=-posAng).
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 2
#
def rotXY(x,y,rotAng):
if rotAng==0.0 or rotAng==-0.0:
return x, y
else:
sinPA = math.sin(math.radians(rotAng))
cosPA = math.cos(math.radians(rotAng))
xr = x*cosPA - y*sinPA
yr = x*sinPA + y*cosPA
return xr, yr
#----------------------------------------------------------------
#
# parseMMS - parse the contents of a MODS Mask design (MMS) file
#
# Input:
# file [str] = name of the MMS file to open and parse
#
# Returns:
# ra,dec = RA, Dec in decimal hours/degrees
# wid,len,rot = slit width and length in decimal arcseconds
#
# Description:
# Opens and reads in the contents of a MODS mask design (MMS) file,
# and extracts the coordinates and dimensions of the slits,
# returning them as useful floating-point representations. The
# trick lies in reading the MMS file's coding for slit RA/Dec
# coordinates:
#
# RA format: INS.TARGnnn.ALPHA 203448.232
# Dec format: INS.TARGnnn.DELTA -001415.123
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 21
#
# 2012 Dec 16 - Added slitlet rotation angles [rwp/osu]
# 2013 Apr 11 - Allow for (technically incorrect) lack of + sign
# declination entries. [rwp/osu]
#
def parseMMS(file):
ra = []
dec = []
swid = []
slen = []
srot = []
F = open(file, 'r')
M = F.readlines()[::]
F.close()
numRef = 0
for i in range(len(M)):
inStr = M[i].strip()
if not inStr.startswith('#') and len(inStr)>0: # ignore comments and blank lines
(param,datum) = inStr.split()
(slitType,targID,field) = param.split('.')
if slitType == 'INS':
if targID=='RSLIT' and field=='NUMBER':
numRef = int(datum)
firstTarget = 100+numRef+1 # first non-reference slit ID number
elif targID[0:4] == 'TARG':
targNum = int(targID[4:])
if targNum >= firstTarget:
if field == 'ALPHA':
raStr = datum
sexStr = '%s:%s:%s' % (raStr[0:2],raStr[2:4],raStr[4:])
ra.append(sex2dec(sexStr))
elif field == 'DELTA':
decStr = datum
if decStr[0] == '-' or decStr[0]=='+':
sexStr = '%s:%s:%s' % (decStr[0:3],decStr[3:5],decStr[5:])
else:
sexStr = '%s:%s:%s' % (decStr[0:2],decStr[2:4],decStr[4:])
dec.append(sex2dec(sexStr))
elif field == 'WID':
swid.append(float(datum))
elif field == 'LEN':
slen.append(float(datum))
elif field == 'ROT':
srot.append(float(datum))
numSlits = len(ra)
return ra, dec, swid, slen, srot
#---------------------------------------------------------------------------
#
# findStar - find a star in a star catalog given RA, Dec and a search radius
#
# inputs:
# ra, dec = RA/Dec in decimal degrees of the test point
# catRAd,catDec = arrays with the catalog star RA/Dec in decimal degrees
# (how it comes from the DS9 catalog servers)
# radius = search radius in arcseconds. Star must be this close to
# the cursor to be 'found'
#
# returns 0..N-1, the index of the star found, or -1 if no star found
#
def findStar(ra,dec,catRAd,catDec,radius):
if len(catRAd)==0:
return -1
for i in range(len(catRAd)):
dist = math.sqrt(math.pow(ra-catRAd[i],2) + math.pow(dec-catDec[i],2))
if i==0:
dmin = dist
iFound = 0
else:
if dist<dmin:
iFound = i
dmin = dist
darcs = 3600.0*dmin
if darcs < radius:
return iFound
else:
return -1
#---------------------------------------------------------------------------
#
# isds9up - see if a named DS9 window is up
#
# Inputs:
# ds9ID = ID ('title') of a DS9 display window
#
# Returns:
# True if ds9ID is running, False otherwise.
#
# Description:
# Uses the shell's xpaaccess method to see if the named ds9 window
# is up and running.
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 3
#
def isds9up(ds9ID):
test = subprocess.Popen(['xpaaccess','-n',ds9ID],
stdout=subprocess.PIPE).communicate()[0]
if int(test):
return True
else:
return False
#---------------------------------------------------------------------------
#
# startDS9 - launch a named ds9 window
#
# Inputs:
# ds9ID = ID ('title') of a DS9 display window to open
#
# Description:
# Launches a named DS9 instance, making sure all of the IRAF
# imtool pipes are suppresed so that IRAF won't interfere
# with it (and vis-vers). It sleeps for 2 seconds to allow
# the tool to open. This may have to be increased on slower
# or more loaded systems.
#
# Author:
# R. Pogge, OSU Astronomy Dept
# 2012 May 3
#
def startDS9(ds9ID):
cmdStr = 'ds9 -fifo none -port none -unix none -title %s' % (ds9ID)
args = shlex.split(cmdStr)
subprocess.Popen(args)
sleep(2)
#---------------------------------------------------------------------------
#
# loadCat - load in a star catalog into working arrays
#
# inputs:
# catFile - name of the catalog file
#
# returns:
# numStars, catID, catRAd, catDec, catBmag, catRmag, catName
# where:
# numStars = [scalar] number of stars in the catalog
# catID = [string] catalog ID (e.g., USNO-B1.0)
# catRAd = [vector] RA in decimal degrees (float)
# catDec = [vector] Dec in decimal degrees (float)
# catBmag = [vector] B magnitude (float)
# catRmag = [vector] R magnitude (float)
# catName = [vector] star ID (string)
#
def loadCat(catFile):
SC=open(catFile,'r')
catLines = SC.readlines()[::]
SC.close()
catRAd = []
catDec = []
catRmag = []
catBmag = []
catName = []
catID = 'None'
numStars = len(catLines)-1
for i in range(len(catLines)):
catBits = catLines[i].split('\t') # split on tabs
if i==0: # first line is the header, get the element count
numItems = len(catBits)
catID = catBits[2]
else:
catRAd.append(float(catBits[0]))
catDec.append(float(catBits[1]))
catName.append(catBits[2])
if catID.upper() == 'NOMAD1':
try:
catRmag.append(float(catBits[15]))
except ValueError:
catRmag.append(99.99)
try:
catBmag.append(float(catBits[11]))
except ValueError:
catBmag.append(99.99)
else:
catRmag.append(float(catBits[numItems-2]))
try:
catBmag.append(float(catBits[numItems-3]))
except ValueError:
catBmag.append(99.99) # placeholder if B is blank in catalog
return numStars,catID,catRAd,catDec,catBmag,catRmag,catName
#---------------------------------------------------------------------------
#
# drawMODS - Create the MODS instrument overlay as a DS9 region file
#
# Inputs:
# objName - [string] Object Name
# target - [tuple] target (RA,Dec) in decimal hours and degrees
# posAng - [float] mask celestial position angle in decimal degrees
# gstar - [tuple] guide star (RA,Dec) in decimal h/deg or None
# slitMask - [string] slit mask ID
# offRD - [tuple] RADEC offset (dRA,dDec) in decimal arcseconds
# offXY - [tuple] DETXY offset (dX,dY) in decimal arcseconds
# mmsFile - [string] name of a MODS Mask Specification (mms) file
# gprobe - [bool] show the guide probe shadow
# boxSize - [int] size of the image display box in arcminutes
#
def drawMODS(objName,target,posAng,gstar,slitMask,offRD,offXY,mmsFile,gprobe,boxSize):
# MODS science field at PA=0 (center and width/height in arcsec).
(sciX,sciY) = (0,0)
(sciW,sciH) = (360,360)
# MODS guide patrol field at PA=0 (center and width/height in arcsec)
(gpfX,gpfY) = (0,-150.0)
(gpfW,gpfH) = (290,300)
(xr0,yr0) = rotXY(gpfX,gpfY,-posAng)
gsBox = (gpfX,gpfY,gpfW,gpfH)
# Nominal guide probe shadow region (center & dimensions) relative to
# the guide star position in arcsec in (xi,eta) coordinates.
(gpsX,gpsY) = (35,0)
(gpsW,gpsH) = (150,85)
# Guide probe carrier arm shadow
(armX,armY) = (76,-105)
(armW,armH) = (80,125)
# Guide Field of View position & dimensions relative to the guide star.
# Both MODS have a 40x40-arcsec FoV
(gcX,gcY) = (0.042,0.404) # really a slight offset (0.042,0.404)
(gcW,gcH) = (40,40)
# Centers of the facility long-slit mask segments in arcsec
slitCen = [-126,-63,0,63,126]
# Process Arguments
# Target RA/Dec
(targRA,targDec) = target
targRAd = 15.0*targRA
# Guide Star? If not, turn off guide star plotting features
if gstar==None:
hasGS = False
gprobe = False
else:
hasGS = True
(gsRA,gsDec) = gstar
gsRAd = 15.0*gsRA
(gsXi,gsEta) = rdToStd(gsRA,gsDec,targRA,targDec)
if inBox(gsXi,gsEta,gsBox,posAng):
gsValid = True
else:
gsValid = False
print('\n ** WARNING: The guide star is OUTSIDE the guide patrol field.')
# RA/Dec Offset
(offsetRA,offsetDec)=offRD
dRAd = (offsetRA/math.cos(math.radians(targDec)))/3600.0
dRA = dRAd/15.0
dDec = offsetDec/3600.0
# DETXY Offset
(offsetX,offsetY)=offXY
(dX,dY) = rotXY(offsetX,offsetY,-posAng)
# Mask ID for slit overlay?
if slitMask==None:
showSlit = False
else:
if slitMask.upper().startswith('LS'):
showSlit = True
else:
showSlit = False
# MMS File for MOS mask overlay?
if mmsFile==None:
showMMS = False
else:
showMMS = True
# Where are we working? (required for getting paths right for DS9 later)
myDir = os.getcwd()
#
# Build the regions file
#
# We have to create an external regions file to draw our boxes on
# the image. Why? Because if you send individual regions
# commands directly (e.g., regions command {box ...}) it reverts
# to physical coordinates not WCS, and screws up, even if you
# explicitly include 'fk5;' in the command string (proper syntax
# ala the manual). A blind-spot of ds9 and XPA...
#
regFile = os.path.join(myDir,'modsView.reg')
if os.path.isfile(regFile):
os.remove(regFile)
RF = open(regFile,'w')
RF.write('#\n# modsView regions file\n#\nfk5\n')
# Draw the 'aim point', the original preset coordinates before any
# offsets are applied, as a yellow circle
regCmd = 'circle %fd %fd 3.0\" # color=yellow width=2\n' % (targRAd,targDec)
RF.write(regCmd)
# RA/Dec of the instrument aim point after all offsets are applied
(instRA,instDec) = stdToRD(sciX+dX,sciY+dY,targRA+dRA,targDec+dDec)
instRAd = 15.0*instRA
# The circle shows the full sweep of the MODS science and guide patrol fields
regCmd = 'circle %fd %fd %f\' # color=red\n' % (instRAd,instDec,fsFoV-0.0333)
RF.write(regCmd)
regCmd = 'circle %fd %fd %f\' # color=cyan\n' % (instRAd,instDec,fsFoV)
RF.write(regCmd)
regCmd = 'circle %fd %fd %f\' # color=red\n' % (instRAd,instDec,fsFoV+0.033)
RF.write(regCmd)
# RA/Dec of the science field center after all offsets are applied
(sciRA,sciDec) = stdToRD(sciX+dX,sciY+dY,targRA+dRA,targDec+dDec)
sciRAd = 15.0*sciRA
# Draw the MODS science field
regCmd = 'box %fd %fd 6\' 6\' %f # width=2 color=green\n' % (sciRAd,sciDec,posAng)
RF.write(regCmd)
# RA/Dec of the guide patrol field after all offsets are applied
(gpfRA,gpfDec) = stdToRD(xr0+dX,yr0+dY,targRA+dRA,targDec+dDec)
gpfRAd = 15.0*gpfRA
# Draw the MODS AGw Guide Patrol Field
regCmd = 'box %fd %fd %f\' %f\' %f # color=cyan width=2\n' % (gpfRAd,gpfDec,(gpfW/60.0),(gpfH/60.0),posAng)
RF.write(regCmd)
# Show the long-slit locations if using a facility mask
if showSlit:
if slitMask.upper() == 'LS60X5':
slitWid=5.0
slitLen=60.0
regCmd = 'box %fd %fd %f\' %f\' %f\n' % \
(sciRAd,sciDec,(slitWid/60.0),(slitLen/60.0),posAng)
RF.write(regCmd)
elif slitMask.upper() == 'LS10X0.8SNS':
slitWid = 0.8
slitLen = 10.0
ys = -120.0
(xsr,ysr) = rotXY(0,ys,-posAng)
(rs,ds) = stdToRD(xsr,ysr,sciRA,sciDec)
rsd = 15.0*rs
regCmd = 'box %fd %fd %f\' %f\' %f\n' % \
(rsd,ds,(slitWid/60.0),(slitLen/60.0),posAng)
RF.write(regCmd)
else:
bits = slitMask.upper().split('X')
slitWid = float(bits[len(bits)-1])
slitLen=60.0
for ys in slitCen:
(xsr,ysr) = rotXY(0,ys,-posAng)
(rs,ds) = stdToRD(xsr,ysr,sciRA,sciDec)
rsd = 15.0*rs
regCmd = 'box %fd %fd %f\' %f\' %f\n' % \
(rsd,ds,(slitWid/60.0),(slitLen/60.0),posAng)
RF.write(regCmd)
# If using a MOS mask and showMMS, display the MOS mask slitlets
if showMMS:
(mmsRA,mmsDec,mmsWid,mmsLen,mmsRot) = parseMMS(mmsFile)
for i in range(len(mmsRA)):
if mmsWid[i]==mmsLen[i]: # assume square = alignment box, color magenta
regCmd = 'box %fd %fd %.1f\" %.1f\" %.2f # color=magenta width=2\n' % \
(15.0*mmsRA[i],mmsDec[i],mmsWid[i],mmsLen[i],posAng+mmsRot[i])
else:
regCmd = 'box %fd %fd %.1f\" %.1f\" %.2f # color=green\n' % \
(15.0*mmsRA[i],mmsDec[i],mmsWid[i],mmsLen[i],posAng+mmsRot[i])
RF.write(regCmd)
# Put in an orientation compass. Arms are 40-arcsec long
xCompass = -4.5*boxSize/10.0
yCompass = 4.0*boxSize/10.0
(cRA,cDec) = stdToRD(60.0*xCompass,60.0*yCompass,targRA,targDec)
cRAd=15.0*cRA
regCmd = 'compass %fd %fd %f\" # compass=fk5 {N} {E} 1 1 color=yellow\n' % (cRAd,cDec,40)
RF.write(regCmd)
# Put in the target name from the OBJNAME parameter in the ACQ file.
if showLabel and len(objName) > 0:
xName = 0.0
yName = 4.75*boxSize/10.0
(cRA,cDec) = stdToRD(60.0*xName,60.0*yName,targRA,targDec)
cRAd=15.0*cRA
regCmd = 'text %fd %fd # text={%s} color=yellow font=\'helvetica 14 normal roman\'\n' \
% (cRAd,cDec,objName)
RF.write(regCmd)
# If requested, show the nominal guide probe shadow. Note that
# the probe stays fixed on the guide star, so we don't follow any
# offsets. We only do this if the guide star is actually in the
# patrol field
if hasGS and (gprobe and gsValid):
# Pickoff shadow, including sensor cable
(dXgps,dYgps) = rotXY(gpsX,gpsY,-posAng)
(rsh,dsh) = stdToRD(gsXi+dXgps,gsEta+dYgps,targRA,targDec)
rshd = 15.0*rsh
regCmd = 'box %fd %fd %f\' %f\' %f # color=yellow width=2\n' % \
(rshd,dsh,(gpsW/60.0),(gpsH/60.0),posAng)
RF.write(regCmd)
# Pickoff actuator arm
(dXarm,dYarm) = rotXY(armX,armY,-posAng)
(rsh,dsh) = stdToRD(gsXi+dXarm,gsEta+dYarm,targRA,targDec)
rshd = 15.0*rsh
regCmd = 'box %fd %fd %f\' %f\' %f # color=yellow width=2\n' % \
(rshd,dsh,(armW/60.0),(armH/60.0),posAng)
RF.write(regCmd)
# Guide camera FoV
(dXgc,dYgc) = rotXY(gcX,gcY,-posAng)
(rsh,dsh) = stdToRD(gsXi+dXgc,gsEta+dYgc,targRA,targDec)
rshd = 15.0*rsh
regCmd = 'box %fd %fd %f\' %f\' %f # color=yellow\n' % \
(rshd,dsh,(gcW/60.0),(gcH/60.0),posAng)
RF.write(regCmd)
# WFS pickoff FoV ('hot spot')
regCmd = 'box %fd %fd 8\" 8\" %f # color=yellow\n' % (gsRAd,gsDec,posAng)
RF.write(regCmd)
# Draw a heavy cyan circle around the guide star if valid, red if
# it is invalid
if hasGS:
if gsValid:
regCmd = 'circle %fd %fd 10\" # width=3 color=cyan\n' % (gsRAd,gsDec)
else:
regCmd = 'circle %fd %fd 10\" # width=3 color=red\n' % (gsRAd,gsDec)
RF.write(regCmd)
# Close the regions file as our work here is done. The calling
# routine is responsible for loading the regions file onto the ds9
# display
RF.close()
#---------------------------------------------------------------------------
#
# printUsage() - print the usage message
#
def printUsage():
print('\nUsage: modsView [options] modsScript [fitsFile]')
print('\nWhere:')
print(' modsScript is a MODS .acq or .img script')
print(' fitsFile optional: use this FITS image with a WCS instead of DSS')
print('\nOptions:')
print(' --mms mmsFile overlay slits from an MMS multi-object mask file')
print(' --shadow overlay the guide probe pickoff shadow region (default: no shadow)')
print(' --finder create a PNG finder chart')
print(' --grid overlay celestial coordinate grid (default: no grid)')
print(' --rotate rotate to fixed-MODS orientation (default: N=up/E=left)')
print(' --noalign do not align the DSS image to N=up/E=left, default: align')
print(' --size s change the size of the image to s arcmin (default: 12 arcmin)')
print(' --cat catID use catalog catID, options: nomad, ub1 or ua2 (default: %s)' % (defCatalog))
print(' --nocat do not overlay catalog stars')
print(' --keepcat do not delete star catalog working files (default: delete catalogs)')
print(' --find Print a list of candidate guide stars to select from')
print(' --minmag x specify the catalog faint magnitude limit. default: %.1f' % (minRMag))
print(' --maxmag x specify the catalog bright magnitude limit, default: %.1f' % (maxRMag))
print(' --server x image server to use, must be one of stsci or eso, default: %s' % (defServer))
print(' --survey x sky survey to use, must be valid for server')
print(' defaults: stsci=all, eso=DSS2-Red')
print(' --nolabel do not label the image with OBJNAME')
print(' --nodisp only print the analysis and quit without displaying in ds9')
print(' --kill kill any delinquent/hidden modsView ds9 window and exit')
print(' -V print version info and exit')
print('\nSee the ds9 manual for server/survey options')
print('')
#===========================================================================
#
# main program
#
# Other runtime defaults
useDSS = True # Use the Digitized Sky Survey as the image source
showField = True # Display the target field in DS9 by default
makeFinder = False # Do not make a finder unless asked to
alignWCS = True # Align the image N=up/E=left by default
alignMODS = False # Align MODS view with the sky (False to align sky to MODS)
showCat = True # Overlay catalog stars by default
keepCat = False # Delete the catalog file when done (True to keep)
dssServer = defServer # Default image server
skySurvey = defSurvey # Default sky survey to use (must be valid)
catFile = 'modsView.cat' # Placeholder star catalog file
starCat = defCatalog # Default optical star catalog
catFilt = defAGwFilt # Default AGw Guide Camera Filter (options: 'R' or 'B')
boxSize = 12 # Size of the sky box in arcminutes (square)
showSlit = False # Don't show a slit unless there is one to show
showGrid = False # Do not overlay a coordinate grid
killDS9 = False # If True, kill the DS9 window if up and exit
fitsFile = 'none'
interact = False # Non-interactive by default
catRadius = 10.0 # catalog star to cursor search radius in arcsec
showShadow = False # Do not draw the nominal pickoff shadow
objName = None # Blank object name by default
showLabel = True # Show the objName label by default
showMMS = False # Show MOS mask slitlets
mmsFile = None # Name of the MMS file, if showMMS=True
findGStars = False # Search for candidate guide stars if True, default: False
# Typical limiting magnitudes for guide stars. The manual suggests
# 12.0-16.5, this offers some margin as the USNO photometry is not
# always that great
minMag = minRMag
maxMag = maxRMag
# Parse the command-line arguments using GNU-style getopt
try:
opts, files = getopt.gnu_getopt(sys.argv[1:],'igflrs:Vc:m:',
['interact','grid','finder','version','catalog=',
'longslit','noalign','cat=','size=','rotate',
'nocat','minmag=','min=','maxmag=','max=',
'server=','survey=','kill','nodisp','shadow',
'nolabel','mask=','mms=','keepcat','agwfilt=','find',
'mods1','mods2','mods='])
except getopt.GetoptError as err:
print('\n** ERROR: %s' % (err))
printUsage()
sys.exit(2)
if len(opts)==0 and len(files)==0:
printUsage()
sys.exit(1)
for opt, arg in opts:
if opt in ('-g','--grid'):
showGrid = True