-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoifits.py
1420 lines (1259 loc) · 67.5 KB
/
oifits.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
"""
A module for reading/writing OIFITS (v1) files
To open an existing OIFITS file, use the oifits.open(filename)
function. This will return an oifits object with the following
members (any of which can be empty dictionaries or numpy arrays):
array: a dictionary of interferometric arrays, as defined by the
OI_ARRAY tables. The dictionary key is the name of the array
(ARRNAME).
header: the header from the primary HDU of the file.
target: a numpy array of targets, as defined by the rows of the
OI_TARGET table.
wavelength: a dictionary of wavelength tables (OI_WAVELENGTH). The
dictionary key is the name of the instrument/settings (INSNAME).
vis, vis2 and t3: numpy arrays of objects containing all the
measurement information. Each list member corresponds to a row in
an OI_VIS/OI_VIS2/OI_T3 table.
A summary of the information in the oifits object can be obtained by
using the info() method:
> import oifits
> oifitsobj = oifits.open('foo.fits')
> oifitsobj.info()
This module makes an ad-hoc, backwards-compatible change to the OIFITS
revision 1 standard originally described by Pauls et al., 2005, PASP,
117, 1255. The OI_VIS and OI_VIS2 tables in OIFITS files produced by
this file contain two additional columns for the correlated flux,
CFLUX and CFLUXERR , which are arrays with a length corresponding to
the number of wavelength elements (just as VISAMP/VIS2DATA). Revision
2 of the OIFITS standard (Duvert, Young & Hummel; arXiv:1510.04556v2)
is not yet supported, but will be soon.
The main purpose of this module is to allow easy access to your OIFITS
data within Python, where you can then analyze it in any way you want.
As of version 0.3, the module can now be used to create OIFITS files
from scratch without serious pain. Be warned, creating an array table
from scratch is probably like nailing jelly to a tree. In a future
verison this may become easier.
The module also provides a simple mechanism for combining multiple
oifits objects, achieved by using the '+' operator on two oifits
objects: result = a + b. The result can then be written to a file
using result.save(filename).
Many of the parameters and their meanings are not specifically
documented here. However, the nomenclature mirrors that of the OIFITS
standard, so it is recommended to use this module with the PASP
reference above in hand.
Beginning with version 0.3, the OI_VIS/OI_VIS2/OI_T3 classes now use
masked arrays for convenience, where the mask is defined via the
'flag' member of these classes. Beware of the following subtlety: as
before, the array data are accessed via (for example) OI_VIS.visamp;
however, OI_VIS.visamp is just a method which constructs (on the fly)
a masked array from OI_VIS._visamp, which is where the data are
actually stored. This is done transparently, and the data can be
accessed and modified transparently via the "visamp" hidden attribute.
The same goes for correlated fluxes, differential/closure phases,
triple products, etc. See the notes on the individual classes for a
list of all the "hidden" attributes.
For further information, contact Paul Boley ([email protected]).
"""
import numpy as np
from numpy import double, bool, ma
from astropy.io import fits as pyfits
import datetime
import copy
import warnings
__author__ = "Paul Boley"
__email__ = "[email protected]"
__date__ ='3 August 2019'
__version__ = '0.3.5-dev'
_mjdzero = datetime.datetime(1858, 11, 17)
matchtargetbyname = False
matchstationbyname = False
refdate = datetime.datetime(2000, 1, 1)
def _plurals(count):
if count != 1: return 's'
return ''
def _array_eq(a, b):
"Test whether all the elements of two arrays are equal."
try:
return not (a != b).any()
except:
return not (a != b)
class _angpoint(float):
"Convenience object for representing angles."
def __init__(self, angle):
self.angle = angle
def __repr__(self):
return '_angpoint(%s)'%self.angle.__repr__()
def __str__(self):
return "%g degrees"%(self.angle)
def __eq__(self, other):
return self.angle == other.angle
def __ne__(self, other):
return not self.__eq__(other)
def asdms(self):
"""Return the value as a string in dms format,
e.g. +25:30:22.55. Useful for declination."""
angle = self.angle
if not np.isfinite(angle):
return self.__repr__()
if angle < 0:
negative = True
angle *= -1.0
else:
negative = False
degrees = np.floor(angle)
minutes = np.floor((angle - degrees)*60.0)
seconds = (angle - degrees - minutes/60.0)*3600.0
if negative:
return "-%02d:%02d:%05.2f"%(degrees,minutes,seconds)
else:
return "+%02d:%02d:%05.2f"%(degrees,minutes,seconds)
def ashms(self):
"""Return the value as a string in hms format,
e.g. 5:12:17.21. Useful for right ascension."""
angle = self.angle*24.0/360.0
if not np.isfinite(angle):
return self.__repr__()
hours = np.floor(angle)
minutes = np.floor((angle - hours)*60.0)
seconds = (angle - hours - minutes/60.0)*3600.0
return "%02d:%02d:%05.2f"%(hours,minutes,seconds)
def _isnone(x):
"""Convenience hack for checking if x is none; needed because numpy
arrays will, at some point, return arrays for x == None."""
return type(x) == type(None)
def _notnone(x):
"""Convenience hack for checking if x is not none; needed because numpy
arrays will, at some point, return arrays for x != None."""
return type(x) != type(None)
class OI_TARGET(object):
def __init__(self, target, raep0, decep0, equinox=2000.0, ra_err=0.0, dec_err=0.0,
sysvel=0.0, veltyp='TOPCENT', veldef='OPTICAL', pmra=0.0, pmdec=0.0,
pmra_err=0.0, pmdec_err=0.0, parallax=0.0, para_err=0.0, spectyp='UNKNOWN'):
self.target = target
self.raep0 = _angpoint(raep0)
self.decep0 = _angpoint(decep0)
self.equinox = equinox
self.ra_err = ra_err
self.dec_err = dec_err
self.sysvel = sysvel
self.veltyp = veltyp
self.veldef = veldef
self.pmra = pmra
self.pmdec = pmdec
self.pmra_err = pmra_err
self.pmdec_err = pmdec_err
self.parallax = parallax
self.para_err = para_err
self.spectyp = spectyp
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(self.target != other.target) or
(self.raep0 != other.raep0) or
(self.decep0 != other.decep0) or
(self.equinox != other.equinox) or
(self.ra_err != other.ra_err) or
(self.dec_err != other.dec_err) or
(self.sysvel != other.sysvel) or
(self.veltyp != other.veltyp) or
(self.veldef != other.veldef) or
(self.pmra != other.pmra) or
(self.pmdec != other.pmdec) or
(self.pmra_err != other.pmra_err) or
(self.pmdec_err != other.pmdec_err) or
(self.parallax != other.parallax) or
(self.para_err != other.para_err) or
(self.spectyp != other.spectyp))
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
return "%s: %s %s (%g)"%(self.target, self.raep0.ashms(), self.decep0.asdms(), self.equinox)
def info(self):
print(str(self))
class OI_WAVELENGTH(object):
def __init__(self, eff_wave, eff_band=None):
self.eff_wave = np.array(eff_wave, dtype=double).reshape(-1)
if _isnone(eff_band):
eff_band = np.zeros_like(eff_wave)
self.eff_band = np.array(eff_band, dtype=double).reshape(-1)
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(not _array_eq(self.eff_wave, other.eff_wave)) or
(not _array_eq(self.eff_band, other.eff_band)))
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "%d wavelength%s (%.3g-%.3g um)"%(len(self.eff_wave), _plurals(len(self.eff_wave)), 1e6*np.min(self.eff_wave),1e6*np.max(self.eff_wave))
def info(self):
print(str(self))
class OI_VIS(object):
"""
Class for storing visibility amplitude and differential phase data.
To access the data, use the following hidden attributes:
visamp, visamperr, visphi, visphierr, flag;
and possibly cflux, cfluxerr.
"""
def __init__(self, timeobs, int_time, visamp, visamperr, visphi, visphierr, flag, ucoord,
vcoord, wavelength, target, array=None, station=(None,None), cflux=None, cfluxerr=None):
self.timeobs = timeobs
self.array = array
self.wavelength = wavelength
self.target = target
self.int_time = int_time
self._visamp = np.array(visamp, dtype=double).reshape(-1)
self._visamperr = np.array(visamperr, dtype=double).reshape(-1)
self._visphi = np.array(visphi, dtype=double).reshape(-1)
self._visphierr = np.array(visphierr, dtype=double).reshape(-1)
if _notnone(cflux): self._cflux = np.array(cflux, dtype=double).reshape(-1)
else: self._cflux = None
if _notnone(cfluxerr): self._cfluxerr = np.array(cfluxerr, dtype=double).reshape(-1)
else: self._cfluxerr = None
self.flag = np.array(flag, dtype=bool).reshape(-1)
self.ucoord = ucoord
self.vcoord = vcoord
self.station = station
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(self.timeobs != other.timeobs) or
(self.array != other.array) or
(self.wavelength != other.wavelength) or
(self.target != other.target) or
(self.int_time != other.int_time) or
(self.ucoord != other.ucoord) or
(self.vcoord != other.vcoord) or
(self.array != other.array) or
(self.station != other.station) or
(not _array_eq(self.visamp, other.visamp)) or
(not _array_eq(self.visamperr, other.visamperr)) or
(not _array_eq(self.visphi, other.visphi)) or
(not _array_eq(self.visphierr, other.visphierr)) or
(not _array_eq(self.flag, other.flag)))
def __ne__(self, other):
return not self.__eq__(other)
def __getattr__(self, attrname):
if attrname in ('visamp', 'visamperr', 'visphi', 'visphierr'):
return ma.masked_array(self.__dict__['_' + attrname], mask=self.flag)
elif attrname in ('cflux', 'cfluxerr'):
if _notnone(self.__dict__['_' + attrname]):
return ma.masked_array(self.__dict__['_' + attrname], mask=self.flag)
else:
return None
else:
raise AttributeError(attrname)
def __setattr__(self, attrname, value):
if attrname in ('visamp', 'visamperr', 'visphi', 'visphierr', 'cflux', 'cfluxerr'):
self.__dict__['_' + attrname] = value
else:
self.__dict__[attrname] = value
def __repr__(self):
meanvis = ma.mean(self.visamp)
if self.station[0] and self.station[1]:
baselinename = ' (' + self.station[0].sta_name + self.station[1].sta_name + ')'
else:
baselinename = ''
return '%s %s%s: %d point%s (%d masked), B = %5.1f m, PA = %5.1f deg, <V> = %4.2g'%(self.target.target, self.timeobs.strftime('%F %T'), baselinename, len(self.visamp), _plurals(len(self.visamp)), np.sum(self.flag), np.sqrt(self.ucoord**2 + self.vcoord**2), np.arctan(self.ucoord / self.vcoord) * 180.0 / np.pi % 180.0, meanvis)
def info(self):
print(str(self))
class OI_VIS2(object):
"""
Class for storing squared visibility amplitude data.
To access the data, use the following hidden attributes:
vis2data, vis2err
"""
def __init__(self, timeobs, int_time, vis2data, vis2err, flag, ucoord, vcoord, wavelength,
target, array=None, station=(None, None)):
self.timeobs = timeobs
self.array = array
self.wavelength = wavelength
self.target = target
self.int_time = int_time
self._vis2data = np.array(vis2data, dtype=double).reshape(-1)
self._vis2err = np.array(vis2err, dtype=double).reshape(-1)
self.flag = np.array(flag, dtype=bool).reshape(-1)
self.ucoord = ucoord
self.vcoord = vcoord
self.station = station
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(self.timeobs != other.timeobs) or
(self.array != other.array) or
(self.wavelength != other.wavelength) or
(self.target != other.target) or
(self.int_time != other.int_time) or
(self.ucoord != other.ucoord) or
(self.vcoord != other.vcoord) or
(self.array != other.array) or
(self.station != other.station) or
(not _array_eq(self.vis2data, other.vis2data)) or
(not _array_eq(self.vis2err, other.vis2err)) or
(not _array_eq(self.flag, other.flag)))
def __ne__(self, other):
return not self.__eq__(other)
def __getattr__(self, attrname):
if attrname in ('vis2data', 'vis2err'):
return ma.masked_array(self.__dict__['_' + attrname], mask=self.flag)
else:
raise AttributeError(attrname)
def __setattr__(self, attrname, value):
if attrname in ('vis2data', 'vis2err'):
self.__dict__['_' + attrname] = value
else:
self.__dict__[attrname] = value
def __repr__(self):
meanvis = ma.mean(self.vis2data)
if self.station[0] and self.station[1]:
baselinename = ' (' + self.station[0].sta_name + self.station[1].sta_name + ')'
else:
baselinename = ''
return "%s %s%s: %d point%s (%d masked), B = %5.1f m, PA = %5.1f deg, <V^2> = %4.2g"%(self.target.target, self.timeobs.strftime('%F %T'), baselinename, len(self.vis2data), _plurals(len(self.vis2data)), np.sum(self.flag), np.sqrt(self.ucoord**2 + self.vcoord**2), np.arctan(self.ucoord / self.vcoord) * 180.0 / np.pi % 180.0, meanvis)
def info(self):
print(str(self))
class OI_T3(object):
"""
Class for storing triple product and closure phase data.
To access the data, use the following hidden attributes:
t3amp, t3amperr, t3phi, t3phierr
"""
def __init__(self, timeobs, int_time, t3amp, t3amperr, t3phi, t3phierr, flag, u1coord,
v1coord, u2coord, v2coord, wavelength, target, array=None, station=(None,None,None)):
self.timeobs = timeobs
self.array = array
self.wavelength = wavelength
self.target = target
self.int_time = int_time
self._t3amp = np.array(t3amp, dtype=double).reshape(-1)
self._t3amperr = np.array(t3amperr, dtype=double).reshape(-1)
self._t3phi = np.array(t3phi, dtype=double).reshape(-1)
self._t3phierr = np.array(t3phierr, dtype=double).reshape(-1)
self.flag = np.array(flag, dtype=bool).reshape(-1)
self.u1coord = u1coord
self.v1coord = v1coord
self.u2coord = u2coord
self.v2coord = v2coord
self.station = station
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(self.timeobs != other.timeobs) or
(self.array != other.array) or
(self.wavelength != other.wavelength) or
(self.target != other.target) or
(self.int_time != other.int_time) or
(self.u1coord != other.u1coord) or
(self.v1coord != other.v1coord) or
(self.u2coord != other.u2coord) or
(self.v2coord != other.v2coord) or
(self.array != other.array) or
(self.station != other.station) or
(not _array_eq(self.t3amp, other.t3amp)) or
(not _array_eq(self.t3amperr, other.t3amperr)) or
(not _array_eq(self.t3phi, other.t3phi)) or
(not _array_eq(self.t3phierr, other.t3phierr)) or
(not _array_eq(self.flag, other.flag)))
def __ne__(self, other):
return not self.__eq__(other)
def __getattr__(self, attrname):
if attrname in ('t3amp', 't3amperr', 't3phi', 't3phierr'):
return ma.masked_array(self.__dict__['_' + attrname], mask=self.flag)
else:
raise AttributeError(attrname)
def __setattr__(self, attrname, value):
if attrname in ('vis2data', 'vis2err'):
self.__dict__['_' + attrname] = value
else:
self.__dict__[attrname] = value
def __repr__(self):
meant3 = np.mean(self.t3amp[np.where(self.flag == False)])
if self.station[0] and self.station[1] and self.station[2]:
baselinename = ' (' + self.station[0].sta_name + self.station[1].sta_name + self.station[2].sta_name + ')'
else:
baselinename = ''
return "%s %s%s: %d point%s (%d masked), B = %5.1fm, %5.1fm, <T3> = %4.2g"%(self.target.target, self.timeobs.strftime('%F %T'), baselinename, len(self.t3amp), _plurals(len(self.t3amp)), np.sum(self.flag), np.sqrt(self.u1coord**2 + self.v1coord**2), np.sqrt(self.u2coord**2 + self.v2coord**2), meant3)
def info(self):
print(str(self))
class OI_STATION(object):
""" This class corresponds to a single row (i.e. single
station/telescope) of an OI_ARRAY table."""
def __init__(self, tel_name=None, sta_name=None, diameter=None, staxyz=[None, None, None]):
self.tel_name = tel_name
self.sta_name = sta_name
self.diameter = diameter
self.staxyz = staxyz
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(self.tel_name != other.tel_name) or
(self.sta_name != other.sta_name) or
(self.diameter != other.diameter) or
(not _array_eq(self.staxyz, other.staxyz)))
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return '%s/%s (%g m)'%(self.sta_name, self.tel_name, self.diameter)
class OI_ARRAY(object):
"""Contains all the data for a single OI_ARRAY table. Note the
hidden convenience attributes latitude, longitude, and altitude."""
def __init__(self, frame, arrxyz, stations=()):
self.frame = frame
self.arrxyz = arrxyz
self.station = np.empty(0)
for station in stations:
tel_name, sta_name, sta_index, diameter, staxyz = station['tel_name'], station['sta_name'], station['sta_index'], station['diameter'], station['staxyz']
self.station = np.append(self.station, OI_STATION(tel_name=tel_name, sta_name=sta_name, diameter=diameter, staxyz=staxyz))
def __eq__(self, other):
if type(self) != type(other): return False
equal = not (
(self.frame != other.frame) or
(not _array_eq(self.arrxyz, other.arrxyz)))
if not equal: return False
# If position appears to be the same, check that the stations
# (and ordering) are also the same
if (self.station != other.station).any():
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def __getattr__(self, attrname):
if attrname == 'latitude':
radius = np.sqrt((self.arrxyz**2).sum())
if radius == 0.0:
warnings.warn('Warning: ARRAYX, ARRAYY, ARRAYZ are all zero', UserWarning)
return _angpoint(np.nan)
return _angpoint(np.arcsin(self.arrxyz[2]/radius)*180.0/np.pi)
elif attrname == 'longitude':
radius = np.sqrt((self.arrxyz**2).sum())
if radius == 0.0:
warnings.warn('Warning: ARRAYX, ARRAYY, ARRAYZ are all zero', UserWarning)
return _angpoint(np.nan)
xylen = np.sqrt(self.arrxyz[0]**2+self.arrxyz[1]**2)
return _angpoint(np.arcsin(self.arrxyz[1]/xylen)*180.0/np.pi)
elif attrname == 'altitude':
radius = np.sqrt((self.arrxyz**2).sum())
if radius == 0.0:
warnings.warn('Warning: ARRAYX, ARRAYY, ARRAYZ are all zero', UserWarning)
return _angpoint(np.nan)
return radius - 6378100.0
else:
raise AttributeError(attrname)
def __repr__(self):
return '%s %s %g m, %d station%s'%(self.latitude.asdms(), self.longitude.asdms(), self.altitude, len(self.station), _plurals(len(self.station)))
def info(self, verbose=0):
"""Print the array's center coordinates. If verbosity >= 1,
print information about each station."""
print(str(self))
if verbose >= 1:
for station in self.station:
print(" %s"%str(station))
def get_station_by_name(self, name):
for station in self.station:
if station.sta_name == name:
return station
raise LookupError('No such station %s'%name)
class oifits(object):
def __init__(self):
self.header = None
self.wavelength = {}
self.target = np.empty(0)
self.array = {}
self.vis = np.empty(0)
self.vis2 = np.empty(0)
self.t3 = np.empty(0)
def __add__(self, other):
"""Consistently combine two separate oifits objects. Note
that targets can be matched by name only (e.g. if coordinates
differ) by setting oifits.matchtargetbyname to True. The same
goes for stations of the array (controlled by
oifits.matchstationbyname)"""
# Don't do anything if the two oifits objects are not CONSISTENT!
if self.isconsistent() == False or other.isconsistent() == False:
raise ValueError('oifits objects are not consistent, bailing')
new = copy.deepcopy(self)
if new.header != None:
if other.header != None:
# Older versions of pyfits don't allow combining headers
try:
new.header = new.header + other.header
except TypeError:
warnings.warn('Warning: Keeping FITS header from first oifits object', UserWarning)
elif other.header != None:
new.header = other.header.copy()
if len(other.wavelength):
wavelengthmap = {}
for key in other.wavelength.keys():
if key not in new.wavelength.keys():
new.wavelength[key] = copy.deepcopy(other.wavelength[key])
elif new.wavelength[key] != other.wavelength[key]:
raise ValueError('Wavelength tables have the same key but differing contents.')
wavelengthmap[id(other.wavelength[key])] = new.wavelength[key]
if len(other.target):
targetmap = {}
for otarget in other.target:
for ntarget in new.target:
if matchtargetbyname and ntarget.target == otarget.target:
targetmap[id(otarget)] = ntarget
break
elif ntarget == otarget:
targetmap[id(otarget)] = ntarget
break
elif ntarget.target == otarget.target:
print('Found a target with a matching name, but some differences in the target specification. Creating a new target. Set oifits.matchtargetbyname to True to override this behavior.')
# If 'id(otarget)' is not in targetmap, then this is a new
# target and should be added to the array of targets
if id(otarget) not in targetmap.keys():
try:
newkey = new.target.keys()[-1]+1
except:
newkey = 1
target = copy.deepcopy(otarget)
new.target = np.append(new.target, target)
targetmap[id(otarget)] = target
if len(other.array):
stationmap = {}
arraymap = {}
for key, otharray in other.array.items():
arraymap[id(otharray)] = key
if key not in new.array.keys():
new.array[key] = copy.deepcopy(other.array[key])
# If arrays have the same name but seem to differ, try
# to combine the two (by including the union of both
# sets of stations)
for othsta in other.array[key].station:
for newsta in new.array[key].station:
if newsta == othsta:
stationmap[id(othsta)] = newsta
break
elif matchstationbyname and newsta.sta_name == othsta.sta_name:
stationmap[id(othsta)] = newsta
break
elif newsta.sta_name == othsta.sta_name and matchstationbyname == False:
raise ValueError('Stations have matching names but conflicting data.')
# If 'id(othsta)' is not in the stationmap
# dictionary, then this is a new station and
# should be added to the current array
if id(othsta) not in stationmap.keys():
newsta = copy.deepcopy(othsta)
new.array[key].station = np.append(new.array[key].station, newsta)
stationmap[id(othsta)] = newsta
# Make sure that staxyz of the new station is relative to the new array center
newsta.staxyz = othsta.staxyz - other.array[key].arrxyz + new.array[key].arrxyz
for vis in other.vis:
if vis not in new.vis:
newvis = copy.copy(vis)
# The wavelength, target, array and station objects
# should point to the appropriate objects inside the
# 'new' structure
newvis.wavelength = wavelengthmap[id(vis.wavelength)]
newvis.target = targetmap[id(vis.target)]
if (vis.array):
newvis.array = new.array[arraymap[id(vis.array)]]
newvis.station = [None, None]
newvis.station[0] = stationmap[id(vis.station[0])]
newvis.station[1] = stationmap[id(vis.station[1])]
new.vis = np.append(new.vis, newvis)
for vis2 in other.vis2:
if vis2 not in new.vis2:
newvis2 = copy.copy(vis2)
# The wavelength, target, array and station objects
# should point to the appropriate objects inside the
# 'new' structure
newvis2.wavelength = wavelengthmap[id(vis2.wavelength)]
newvis2.target = targetmap[id(vis2.target)]
if (vis2.array):
newvis2.array = new.array[arraymap[id(vis2.array)]]
newvis2.station = [None, None]
newvis2.station[0] = stationmap[id(vis2.station[0])]
newvis2.station[1] = stationmap[id(vis2.station[1])]
new.vis2 = np.append(new.vis2, newvis2)
for t3 in other.t3:
if t3 not in new.t3:
newt3 = copy.copy(t3)
# The wavelength, target, array and station objects
# should point to the appropriate objects inside the
# 'new' structure
newt3.wavelength = wavelengthmap[id(t3.wavelength)]
newt3.target = targetmap[id(t3.target)]
if (t3.array):
newt3.array = new.array[arraymap[id(t3.array)]]
newt3.station = [None, None, None]
newt3.station[0] = stationmap[id(t3.station[0])]
newt3.station[1] = stationmap[id(t3.station[1])]
newt3.station[2] = stationmap[id(t3.station[2])]
new.t3 = np.append(new.t3, newt3)
return(new)
def __eq__(self, other):
if type(self) != type(other): return False
return not (
(self.wavelength != other.wavelength) or
(self.target != other.target).any() or
(self.array != other.array) or
(self.vis != other.vis).any() or
(self.vis2 != other.vis2).any() or
(self.t3 != other.t3).any())
def __ne__(self, other):
return not self.__eq__(other)
def isvalid(self):
"""Returns True of the oifits object is both consistent (as
determined by isconsistent()) and conforms to the OIFITS
standard (according to Pauls et al., 2005, PASP, 117, 1255)."""
warnings = []
errors = []
if not self.isconsistent():
errors.append('oifits object is not consistent')
if not self.target.size:
errors.append('No OI_TARGET data')
if not self.wavelength:
errors.append('No OI_WAVELENGTH data')
else:
for wavelength in self.wavelength.values():
if len(wavelength.eff_wave) != len(wavelength.eff_band):
errors.append("eff_wave and eff_band are of different lengths for wavelength table '%s'"%key)
if (self.vis.size + self.vis2.size + self.t3.size == 0):
errors.append('Need to have atleast one measurement table (vis, vis2 or t3)')
for vis in self.vis:
nwave = len(vis.wavelength.eff_band)
if (len(vis.visamp) != nwave) or (len(vis.visamperr) != nwave) or (len(vis.visphi) != nwave) or (len(vis.visphierr) != nwave) or (len(vis.flag) != nwave):
errors.append("Data size mismatch for visibility measurement 0x%x (wavelength table has a length of %d)"%(id(vis), nwave))
for vis2 in self.vis2:
nwave = len(vis2.wavelength.eff_band)
if (len(vis2.vis2data) != nwave) or (len(vis2.vis2err) != nwave) or (len(vis2.flag) != nwave):
errors.append("Data size mismatch for visibility^2 measurement 0x%x (wavelength table has a length of %d)"%(id(vis), nwave))
for t3 in self.t3:
nwave = len(t3.wavelength.eff_band)
if (len(t3.t3amp) != nwave) or (len(t3.t3amperr) != nwave) or (len(t3.t3phi) != nwave) or (len(t3.t3phierr) != nwave) or (len(t3.flag) != nwave):
errors.append("Data size mismatch for visibility measurement 0x%x (wavelength table has a length of %d)"%(id(vis), nwave))
if warnings:
print("*** %d warning%s:"%(len(warnings), _plurals(len(warnings))))
for warning in warnings:
print(' ' + warning)
if errors:
print("*** %d ERROR%s:"%(len(errors), _plurals(len(errors)).upper()))
for error in errors:
print(' ' + error)
return not (len(warnings) or len(errors))
def isconsistent(self):
"""Returns True if the object is entirely self-contained,
i.e. all cross-references to wavelength tables, arrays,
stations etc. in the measurements refer to elements which are
stored in the oifits object. Note that an oifits object can
be 'consistent' in this sense without being 'valid' as checked
by isvalid()."""
for vis in self.vis:
if vis.array and (vis.array not in self.array.values()):
print('A visibility measurement (0x%x) refers to an array which is not inside the main oifits object.'%id(vis))
return False
if ((vis.station[0] and (vis.station[0] not in vis.array.station)) or
(vis.station[1] and (vis.station[1] not in vis.array.station))):
print('A visibility measurement (0x%x) refers to a station which is not inside the main oifits object.'%id(vis))
return False
if vis.wavelength not in self.wavelength.values():
print('A visibility measurement (0x%x) refers to a wavelength table which is not inside the main oifits object.'%id(vis))
return False
if vis.target not in self.target:
print('A visibility measurement (0x%x) refers to a target which is not inside the main oifits object.'%id(vis))
return False
for vis2 in self.vis2:
if vis2.array and (vis2.array not in self.array.values()):
print('A visibility^2 measurement (0x%x) refers to an array which is not inside the main oifits object.'%id(vis2))
return False
if ((vis2.station[0] and (vis2.station[0] not in vis2.array.station)) or
(vis2.station[1] and (vis2.station[1] not in vis2.array.station))):
print('A visibility^2 measurement (0x%x) refers to a station which is not inside the main oifits object.'%id(vis))
return False
if vis2.wavelength not in self.wavelength.values():
print('A visibility^2 measurement (0x%x) refers to a wavelength table which is not inside the main oifits object.'%id(vis2))
return False
if vis2.target not in self.target:
print('A visibility^2 measurement (0x%x) refers to a target which is not inside the main oifits object.'%id(vis2))
return False
for t3 in self.t3:
if t3.array and (t3.array not in self.array.values()):
print('A closure phase measurement (0x%x) refers to an array which is not inside the main oifits object.'%id(t3))
return False
if ((t3.station[0] and (t3.station[0] not in t3.array.station)) or
(t3.station[1] and (t3.station[1] not in t3.array.station)) or
(t3.station[2] and (t3.station[2] not in t3.array.station))):
print('A closure phase measurement (0x%x) refers to a station which is not inside the main oifits object.'%id(t3))
return False
if t3.wavelength not in self.wavelength.values():
print('A closure phase measurement (0x%x) refers to a wavelength table which is not inside the main oifits object.'%id(t3))
return False
if t3.target not in self.target:
print('A closure phase measurement (0x%x) refers to a target which is not inside the main oifits object.'%id(t3))
return False
return True
def info(self, recursive=True, verbose=0):
"""Print out a summary of the contents of the oifits object.
Set recursive=True to obtain more specific information about
each of the individual components, and verbose to an integer
to increase the verbosity level."""
if self.wavelength:
wavelengths = 0
if recursive:
print("====================================================================")
print("SUMMARY OF WAVELENGTH TABLES")
print("====================================================================")
for key in self.wavelength.keys():
wavelengths += len(self.wavelength[key].eff_wave)
if recursive: print("'%s': %s"%(key, str(self.wavelength[key])))
print("%d wavelength table%s with %d wavelength%s in total"%(len(self.wavelength), _plurals(len(self.wavelength)), wavelengths, _plurals(wavelengths)))
if self.target.size:
if recursive:
print("====================================================================")
print("SUMMARY OF TARGET TABLES")
print("====================================================================")
for target in self.target:
target.info()
print("%d target%s"%(len(self.target), _plurals(len(self.target))))
if self.array:
stations = 0
if recursive:
print("====================================================================")
print("SUMMARY OF ARRAY TABLES")
print("====================================================================")
for key in self.array.keys():
if recursive:
print(key + ':')
self.array[key].info(verbose=verbose)
stations += len(self.array[key].station)
print("%d array%s with %d station%s"%(len(self.array), _plurals(len(self.array)), stations, _plurals(stations)))
if self.vis.size:
if recursive:
print("====================================================================")
print("SUMMARY OF VISIBILITY MEASUREMENTS")
print("====================================================================")
for vis in self.vis:
vis.info()
print("%d visibility measurement%s"%(len(self.vis), _plurals(len(self.vis))))
if self.vis2.size:
if recursive:
print("====================================================================")
print("SUMMARY OF VISIBILITY^2 MEASUREMENTS")
print("====================================================================")
for vis2 in self.vis2:
vis2.info()
print("%d visibility^2 measurement%s"%(len(self.vis2), _plurals(len(self.vis2))))
if self.t3.size:
if recursive:
print("====================================================================")
print("SUMMARY OF T3 MEASUREMENTS")
print("====================================================================")
for t3 in self.t3:
t3.info()
print("%d closure phase measurement%s"%(len(self.t3), _plurals(len(self.t3))))
def save(self, filename):
"""Write the contents of the oifits object to a file in OIFITS
format."""
if not self.isconsistent():
raise ValueError('oifits object is not consistent; refusing to go further')
hdulist = pyfits.HDUList()
hdu = pyfits.PrimaryHDU(header=self.header)
hdu.header['DATE'] = datetime.datetime.now().strftime(format='%F'), 'Creation date'
# Remove old oifits.py comments if they are present
remcomments = []
try:
for i, comment in enumerate(hdu.header['COMMENT']):
if (('Written by OIFITS Python module' in str(comment)) |
('http://www.mpia-hd.mpg.de/homes/boley/oifits/' in str(comment)) |
('http://astro.ins.urfu.ru/pages/~pboley/oifits/' in str(comment))):
remcomments.append(i)
except KeyError:
# KeyError should be raised if there are no comments
pass
# Cards should be removed from the bottom, otherwise the
# ordering can get messed up and header.ascard.remove can fail
remcomments.reverse()
for i in remcomments:
del hdu.header[('COMMENT', i)]
# Add (new) advertisement
hdu.header.add_comment('Written by OIFITS Python module version %s'%__version__)
hdu.header.add_comment('http://astro.ins.urfu.ru/pages/~pboley/oifits/')
wavelengthmap = {}
hdulist.append(hdu)
for insname, wavelength in self.wavelength.items():
wavelengthmap[id(wavelength)] = insname
hdu = pyfits.BinTableHDU.from_columns(pyfits.ColDefs((
pyfits.Column(name='EFF_WAVE', format='1E', unit='METERS', array=wavelength.eff_wave),
pyfits.Column(name='EFF_BAND', format='1E', unit='METERS', array=wavelength.eff_band)
)))
hdu.header['EXTNAME'] = 'OI_WAVELENGTH'
hdu.header['OI_REVN'] = 1, 'Revision number of the table definition'
hdu.header['INSNAME'] = insname, 'Name of detector, for cross-referencing'
hdulist.append(hdu)
targetmap = {}
if self.target.size:
target_id = []
target = []
raep0 = []
decep0 = []
equinox = []
ra_err = []
dec_err = []
sysvel = []
veltyp = []
veldef = []
pmra = []
pmdec = []
pmra_err = []
pmdec_err = []
parallax = []
para_err = []
spectyp = []
for i, targ in enumerate(self.target):
key = i+1
targetmap[id(targ)] = key
target_id.append(key)
target.append(targ.target)
raep0.append(targ.raep0)
decep0.append(targ.decep0)
equinox.append(targ.equinox)
ra_err.append(targ.ra_err)
dec_err.append(targ.dec_err)
sysvel.append(targ.sysvel)
veltyp.append(targ.veltyp)
veldef.append(targ.veldef)
pmra.append(targ.pmra)
pmdec.append(targ.pmdec)
pmra_err.append(targ.pmra_err)
pmdec_err.append(targ.pmdec_err)
parallax.append(targ.parallax)
para_err.append(targ.para_err)
spectyp.append(targ.spectyp)
hdu = pyfits.BinTableHDU.from_columns(pyfits.ColDefs((
pyfits.Column(name='TARGET_ID', format='1I', array=target_id),
pyfits.Column(name='TARGET', format='16A', array=target),
pyfits.Column(name='RAEP0', format='1D', unit='DEGREES', array=raep0),
pyfits.Column(name='DECEP0', format='1D', unit='DEGREES', array=decep0),
pyfits.Column(name='EQUINOX', format='1E', unit='YEARS', array=equinox),
pyfits.Column(name='RA_ERR', format='1D', unit='DEGREES', array=ra_err),
pyfits.Column(name='DEC_ERR', format='1D', unit='DEGREES', array=dec_err),
pyfits.Column(name='SYSVEL', format='1D', unit='M/S', array=sysvel),
pyfits.Column(name='VELTYP', format='8A', array=veltyp),
pyfits.Column(name='VELDEF', format='8A', array=veldef),
pyfits.Column(name='PMRA', format='1D', unit='DEG/YR', array=pmra),
pyfits.Column(name='PMDEC', format='1D', unit='DEG/YR', array=pmdec),
pyfits.Column(name='PMRA_ERR', format='1D', unit='DEG/YR', array=pmra_err),
pyfits.Column(name='PMDEC_ERR', format='1D', unit='DEG/YR', array=pmdec_err),
pyfits.Column(name='PARALLAX', format='1E', unit='DEGREES', array=parallax),
pyfits.Column(name='PARA_ERR', format='1E', unit='DEGREES', array=para_err),
pyfits.Column(name='SPECTYP', format='16A', array=spectyp)
)))
hdu.header['EXTNAME'] = 'OI_TARGET'
hdu.header['OI_REVN'] = 1, 'Revision number of the table definition'
hdulist.append(hdu)
arraymap = {}
stationmap = {}
for arrname, array in self.array.items():
arraymap[id(array)] = arrname
tel_name = []
sta_name = []
sta_index = []
diameter = []
staxyz = []
if array.station.size:
for i, station in enumerate(array.station, 1):