-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfitsidi.py
1528 lines (1275 loc) · 62.3 KB
/
fitsidi.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
# -*- coding: utf-8 -*-
"""
Module for writing correlator output to a FITS IDI file. The classes and
functions defined in this module are based heavily off the lwda_fits library.
.. note::
Some software that deal with FITS IDI files, e.g. AIPS, does not support
FITS IDI files with more than 99 antennas. See the :class:`lsl.writer.fitsidi.aips`
class for a FITS IDI writer that respects this limit.
.. versionchanged:: 1.1.4
Fixed a conjugation problem in the visibilities saved to a FITS-IDI file
Added support for writing user-specified weights to a FITS-IDI file
.. versionchanged:: 1.2.2
Added support for writing multiple IFs to the same FITS-IDI file
"""
import os
import re
import math
import ephem
import numpy as np
from functools import total_ordering
from astropy.time import Time as AstroTime
from astropy.constants import c as speedOfLight
from astropy.coordinates import EarthLocation, AltAz, CartesianRepresentation, ITRS
from astropy.utils import iers
from astropy.io import fits as astrofits
from datetime import datetime
from collections import OrderedDict
from lsl import astro
from lsl.misc import telemetry
telemetry.track_module()
__version__ = '0.9'
__all__ = ['Idi', 'Aips', 'ExtendedIdi', 'STOKES_CODES', 'NUMERIC_STOKES']
IDI_VERSION = (3, 0)
STOKES_CODES = { 'I': 1, 'Q': 2, 'U': 3, 'V': 4,
'RR': -1, 'LL': -2, 'RL': -3, 'LR': -4,
'XX': -5, 'YY': -6, 'XY': -7, 'YX': -8}
NUMERIC_STOKES = { 1: 'I', 2: 'Q', 3: 'U', 4: 'V',
-1: 'RR', -2: 'LL', -3: 'RL', -4: 'LR',
-5: 'XX', -6: 'YY', -7: 'XY', -8: 'YX'}
speedOfLight = speedOfLight.to('m/s').value
def merge_baseline(ant1, ant2, shift=16):
"""
Merge two stand ID numbers into a single baseline using the specified bit
shift size.
"""
return (ant1 << shift) | ant2
def split_baseline(baseline, shift=16):
"""
Given a baseline, split it into it consistent stand ID numbers.
"""
part = 2**shift - 1
return (baseline >> shift) & part, baseline & part
def get_geocentric_from_enz(center, enz):
"""
Given an array center in geocentric coordinates and an antenna offset in
meters east-north-zenith, convert the antennas position into geocentric
coordinates.
"""
center = EarthLocation.from_geocentric(*center, unit='m')
offset = AltAz(CartesianRepresentation(enz[1], enz[0], enz[2], unit='m'),
location=center, obstime='J2000')
xyz = offset.transform_to(ITRS())
return xyz.cartesian.xyz.value
class WriterBase(object):
"""
Base class for the :mod:`lsl.writer` module.
"""
_MAX_ANTS = 255
_PACKING_BIT_SHIFT = 8
_STOKES_CODES = STOKES_CODES
class _Antenna(object):
"""
Holds information describing the location and properties of an antenna.
"""
def __init__(self, id, x, y, z, bits=8):
self.id = id
self.x = x
self.y = y
self.z = z
self.levels = bits
self.polA = {'Type': 'X', 'Angle': 0.0, 'Cal': [0.0, 0.0]}
self.polB = {'Type': 'Y', 'Angle': 90.0, 'Cal': [0.0, 0.0]}
def get_name(self):
try:
return self.config_name
except AttributeError:
return "LWA%03i" % self.id
class _Frequency:
"""
Holds information about the frequency setup used in the file.
"""
def __init__(self, offset, channel_width, bandwidth):
self.id = 1
self.bandFreq = offset
self.chWidth = channel_width
self.totalBW = bandwidth
self.sideBand = 1
self.baseBand = 0
@total_ordering
class _UVData(object):
"""
Represents one UV visibility data set for a given observation time.
"""
def __init__(self, obsTime, intTime, baselines, visibilities, weights=None, pol=STOKES_CODES['XX'], source='z'):
self.obsTime = obsTime
self.intTime = intTime
self.baselines = baselines
self.visibilities = visibilities
self.weights = weights
self.pol = pol
self.source = source
def __cmp__(self, y):
"""
Function to sort the self.data list in order of time and then
polarization code.
"""
sID = (self.obsTime, abs(self.pol))
yID = (y.obsTime, abs(y.pol) )
if sID > yID:
return 1
elif sID < yID:
return -1
else:
return 0
def __eq__(self, other):
return True if self.__cmp__(other) == 0 else False
def __lt__(self, other):
return True if self.__cmp__(other) < 0 else False
def time(self):
return self.obsTime
def get_uvw(self, HA, dec, obs):
Nbase = len(self.baselines)
uvw = np.zeros((Nbase,3), dtype=np.float32)
# Phase center coordinates
# Convert numbers to radians and, for HA, hours to degrees
HA2 = HA * 15.0 * np.pi/180
dec2 = dec * np.pi/180
lat2 = obs.lat
# Coordinate transformation matrices
trans1 = np.matrix([[0, -np.sin(lat2), np.cos(lat2)],
[1, 0, 0 ],
[0, np.cos(lat2), np.sin(lat2)]])
trans2 = np.matrix([[ np.sin(HA2), np.cos(HA2), 0 ],
[-np.sin(dec2)*np.cos(HA2), np.sin(dec2)*np.sin(HA2), np.cos(dec2)],
[ np.cos(dec2)*np.cos(HA2), -np.cos(dec2)*np.sin(HA2), np.sin(dec2)]])
for i,(a1,a2) in enumerate(self.baselines):
# Go from a east, north, up coordinate system to a celestial equation,
# east, north celestial pole system
xyzPrime = a1.stand - a2.stand
xyz = trans1*np.matrix([[xyzPrime[0]],[xyzPrime[1]],[xyzPrime[2]]])
# Go from CE, east, NCP to u, v, w
temp = trans2*xyz
uvw[i,:] = np.squeeze(temp) / speedOfLight
return uvw
def argsort(self, mapper=None, shift=16):
packed = []
for a1,a2 in self.baselines:
if mapper is None:
s1, s2 = a1.stand.id, a2.stand.id
else:
s1, s2 = mapper[a1.stand.id], mapper[a2.stand.id]
packed.append( merge_baseline(s1, s2, shift=shift) )
packed = np.array(packed, dtype=np.int32)
return np.argsort(packed)
def parse_time(self, ref_time):
"""
Given a time as either a integer, float, string, or datetime object,
convert it to a string in the formation 'YYYY-MM-DDTHH:MM:SS'.
"""
# Valid time string (modulo the 'T')
timeRE = re.compile(r'\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(\.\d+)?')
if type(ref_time) in (int, float):
refDateTime = datetime.utcfromtimestamp(ref_time)
ref_time = refDateTime.strftime("%Y-%m-%dT%H:%M:%S")
elif type(ref_time) == datetime:
ref_time = ref_time.strftime("%Y-%m-%dT%H:%M:%S")
elif type(ref_time) == str:
# Make sure that the string times are of the correct format
if re.match(timeRE, ref_time) is None:
raise RuntimeError("Malformed date/time provided: %s" % ref_time)
else:
ref_time = ref_time.replace(' ', 'T', 1)
else:
raise RuntimeError("Unknown time format provided.")
return ref_time
@property
def astro_ref_time(self):
"""
Convert a reference time string to an :class:`lsl.astro.date` object.
"""
dateStr = self.ref_time.replace('T', '-').replace(':', '-').split('-')
return astro.date(int(dateStr[0]), int(dateStr[1]), int(dateStr[2]), int(dateStr[3]), int(dateStr[4]), float(dateStr[5]))
def __init__(self, filename, ref_time=0.0, verbose=False):
# File-specific information
self.filename = filename
self.verbose = verbose
# Observatory-specific information
self.siteName = 'Unknown'
# Observation-specific information
self.observer = 'UNKNOWN'
self.project = 'UNKNOWN'
self.mode = 'ZA'
self.ref_time = self.parse_time(ref_time)
self.nAnt = 0
self.nChan = 0
self.nStokes = 0
self.refVal = 0
self.refPix = 0
self.channelWidth = 0
# Parameters that store the meta-data and data
self.array = []
self.freq = []
self.stokes = []
self.data = []
self.extra_keywords = {}
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.write()
self.close()
def set_stokes(self, polList):
"""
Given a list of Stokes parameters, update the object's parameters.
"""
for pol in polList:
if type(pol) == str:
numericPol = self._STOKES_CODES[pol.upper()]
else:
numericPol = pol
if numericPol not in self.stokes:
self.stokes.append(numericPol)
# Sort into order of 'XX', 'YY', 'XY', and 'YX' or 'I', 'Q', 'U', and 'V'
self.stokes.sort()
if self.stokes[0] < 0:
self.stokes.reverse()
self.nStokes = len(self.stokes)
def set_frequency(self, freq):
"""
Given a numpy array of frequencies, set the relevant common observation
parameters and add an entry to the self.freq list.
"""
if self.nChan == 0:
self.nChan = len(freq)
self.refVal = freq[0]
self.refPix = 1
self.channelWidth = np.abs(freq[1] - freq[0])
offset = 0.0
else:
assert(len(freq) == self.nChan)
offset = freq[0] - self.refVal
totalWidth = np.abs(freq[-1] - freq[0])
freqSetup = self._Frequency(offset, self.channelWidth, totalWidth)
self.freq.append(freqSetup)
def set_geometry(self, *args, **kwds):
"""
Given a station and an array of stands, set the relevant common observation
parameters and add entries to the self.array list.
"""
raise NotImplementedError
def add_data_set(self, obsTime, intTime, baselines, visibilities, weights=None, pol='XX', source='z'):
"""
Create a UVData object to store a collection of visibilities.
.. versionchanged:: 0.4.0
Switched over to passing in Antenna instances generated by the
:mod:`lsl.common.stations` module instead of a list of stand ID
as part of the baselines.
.. versionchanged:: 1.1.0
Added a new 'source' keyword to set the phase center for the data.
This can either by 'z' for zenith or a ephem.Body instances for a
point on the sky.
.. versionchanged:: 1.3.0
Added a new 'weights' keyword to set the visibility weights for the
data.
"""
if type(pol) == str:
numericPol = self._STOKES_CODES[pol.upper()]
else:
numericPol = pol
self.data.append( self._UVData(obsTime, intTime, baselines, visibilities, weights=weights, pol=numericPol, source=source) )
def write(self):
"""
Fill in the file will all of the required supporting metadata.
"""
raise NotImplementedError
def close(self):
"""
Close out the file.
"""
raise NotImplementedError
class Idi(WriterBase):
"""
Class for storing visibility data and writing the data, along with array
geometry, frequency setup, etc., to a FITS IDI file that can be read into
AIPS via the FITLD task.
"""
def __init__(self, filename, ref_time=0.0, verbose=False, memmap=None, clobber=False):
"""
Initialize a new FITS IDI object using a filename and a reference time
given in seconds since the UNIX 1970 ephem, a python datetime object, or a
string in the format of 'YYYY-MM-DDTHH:MM:SS'.
.. versionchanged:: 1.1.2
Added the 'memmap' and 'clobber' keywords to control if the file
is memory mapped and whether or not to overwrite an existing file,
respectively.
"""
# File-specific information
WriterBase.__init__(self, filename, ref_time=ref_time, verbose=verbose)
# Open the file and get going
if os.path.exists(filename):
if clobber:
os.unlink(filename)
else:
raise IOError("File '%s' already exists" % filename)
self.FITS = astrofits.open(filename, mode='append', memmap=memmap)
def set_geometry(self, site, antennas, bits=8):
"""
Given a station and an array of stands, set the relevant common observation
parameters and add entries to the self.array list.
.. versionchanged:: 0.4.0
Switched over to passing in Antenna instances generated by the
:mod:`lsl.common.stations` module instead of a list of stand ID
numbers.
"""
# Make sure that we have been passed 255 or fewer stands
if len(antennas) > self._MAX_ANTS:
raise RuntimeError("FITS IDI supports up to %s antennas only, given %i" % (self._MAX_ANTS, len(antennas)))
# Update the observatory-specific information
self.siteName = site.name
stands = []
for ant in antennas:
stands.append(ant.stand.id)
stands = np.array(stands)
arrayX, arrayY, arrayZ = site.geocentric_location
xyz = np.zeros((len(stands),3))
for i,ant in enumerate(antennas):
ecef = get_geocentric_from_enz(site.geocentric_location, [ant.stand.x, ant.stand.y, ant.stand.z])
xyz[i,:] = ecef
# Create the stand mapper to deal with the fact that stands range from
# 1 to 258, not 1 to 255
# Changed 2024 Feb 19 - Always map so that the antenna numbers are not sparse
mapper = OrderedDict()
enableMapper = True
ants = []
for i in range(len(stands)):
ants.append( self._Antenna(stands[i], xyz[i,0], xyz[i,1], xyz[i,2], bits=bits) )
if enableMapper:
mapper[stands[i]] = i+1
else:
mapper[stands[i]] = stands[i]
try:
ants[-1].config_name = antennas[i].config_name
except AttributeError:
pass
# If the mapper has been enabled, tell the user about it
if enableMapper and self.verbose:
print("FITS IDI: stand ID mapping enabled")
for key in mapper.keys():
value = mapper[key]
print("FITS IDI: stand #%i -> mapped #%i" % (key, value))
self.nAnt = len(ants)
self.array.append( {'center': [arrayX, arrayY, arrayZ], 'ants': ants, 'mapper': mapper, 'enableMapper': enableMapper, 'inputAnts': antennas} )
def set_observer(self, observer, project='UNKNOWN', mode='ZA'):
"""
Set the observer name, project, and observation mode (if given) to the
self.observer, self.project, and self.mode attributes, respectively.
"""
self.observer = observer
self.project = project
self.mode = mode
def add_header_keyword(self, name, value, comment=None):
"""
Add an additional entry to the header of the primary HDU.
"""
name = name.upper()
if name in ('NAXIS', 'EXTEND', 'GROUPS', 'GCOUNT', 'PCOUNT', 'OBJECT',
'TELESCOP', 'OBSERVER', 'PROJECT', 'ORIGIN', 'CORRELAT', 'FXCORVER',
'LWATYPE', 'LWAMAJV', 'LWAMINV', 'DATE-OBS', 'DATE-MAP',
'COMMENT', 'HISTORY'):
raise ValueError("Cannot set a value for '%s'" % name)
if len(name) > 8:
raise ValueError("Keyword name cannot be more than eight characters long")
self.extra_keywords[name] = value if comment is None else (value, comment)
def add_comment(self, comment):
"""
Add a comment to data.
.. versionadded:: 1.2.4
"""
try:
self._comments.append( comment )
except AttributeError:
self._comments = [comment,]
def add_history(self, history):
"""
Add a history entry to the data.
.. versionadded:: 1.2.4
"""
try:
self._history.append( history )
except AttributeError:
self._history = [history,]
def write(self):
"""
Fill in the FITS-IDI file will all of the tables in the
correct order.
"""
# Validate
if self.nStokes == 0:
raise RuntimeError("No polarization setups defined")
if len(self.freq) == 0:
raise RuntimeError("No frequency setups defined")
if self.nAnt == 0:
raise RuntimeError("No array geometry defined")
if len(self.data) == 0:
raise RuntimeError("No visibility data defined")
# Sort the data set
self.data.sort()
self._write_primary_hdu()
self._write_geometry_hdu()
self._write_frequency_hdu()
self._write_antenna_hdu()
self._write_bandpass_hdu()
self._write_source_hdu()
self._write_uvdata_hdu()
def close(self):
"""
Close out the file.
"""
self.FITS.flush()
self.FITS.close()
def _add_common_keywords(self, hdr, name, revision):
"""
Added keywords common to all table headers.
"""
hdr['EXTNAME'] = (name, 'FITS-IDI table name')
hdr['EXTVER'] = (1, 'table instance number')
hdr['TABREV'] = (revision, 'table format revision number')
hdr['NO_STKD'] = (self.nStokes, 'number of Stokes parameters')
hdr['STK_1'] = (self.stokes[0], 'first Stokes parameter')
hdr['NO_BAND'] = (len(self.freq), 'number of frequency bands')
hdr['NO_CHAN'] = (self.nChan, 'number of frequency channels')
hdr['REF_FREQ'] = (self.refVal, 'reference frequency (Hz)')
hdr['CHAN_BW'] = (self.channelWidth, 'channel bandwidth (Hz)')
hdr['REF_PIXL'] = (float(self.refPix), 'reference frequency bin')
date = self.ref_time.split('-')
name = "ZA%s%s%s" % (date[0][2:], date[1], date[2])
hdr['OBSCODE'] = (name, 'zenith all-sky image')
hdr['ARRNAM'] = self.siteName
hdr['RDATE'] = (self.ref_time, 'file data reference date')
def _write_primary_hdu(self):
"""
Write the primary HDU to file.
"""
primary = astrofits.PrimaryHDU()
primary.header['NAXIS'] = (0, 'indicates IDI file')
primary.header['EXTEND'] = (True, 'indicates IDI file')
primary.header['GROUPS'] = (True, 'indicates IDI file')
primary.header['GCOUNT'] = 0
primary.header['PCOUNT'] = 0
primary.header['OBJECT'] = 'BINARYTB'
primary.header['TELESCOP'] = self.siteName
primary.header['INSTRUME'] = self.siteName
primary.header['OBSERVER'] = (self.observer, 'Observer name(s)')
primary.header['PROJECT'] = (self.project, 'Project name')
primary.header['ORIGIN'] = 'LSL'
primary.header['CORRELAT'] = ('LWASWC', 'Correlator used')
primary.header['FXCORVER'] = ('1', 'Correlator version')
primary.header['LWATYPE'] = (self.mode, 'LWA FITS file type')
primary.header['LWAMAJV'] = (IDI_VERSION[0], 'LWA FITS file format major version')
primary.header['LWAMINV'] = (IDI_VERSION[1], 'LWA FITS file format minor version')
primary.header['DATE-OBS'] = (self.ref_time, 'IDI file data collection date')
ts = str(astro.get_date_from_sys())
primary.header['DATE-MAP'] = (ts.split()[0], 'IDI file creation date')
# Write extra header values
for name in self.extra_keywords:
primary.header[name] = self.extra_keywords[name]
# Write the comments and history
try:
for comment in self._comments:
primary.header['COMMENT'] = comment
del self._comments
except AttributeError:
pass
primary.header['COMMENT'] = " FITS (Flexible Image Transport System) format is defined in 'Astronomy and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H"
try:
for hist in self._history:
primary.header['HISTORY'] = hist
del self._history
except AttributeError:
pass
self.FITS.append(primary)
self.FITS.flush()
def _write_geometry_hdu(self):
"""
Define the Array_Geometry table (group 1, table 1).
"""
names = []
xyz = np.zeros((self.nAnt,3), dtype=np.float64)
for i,ant in enumerate(self.array[0]['ants']):
xyz[i,0] = ant.x
xyz[i,1] = ant.y
xyz[i,2] = ant.z
names.append(ant.get_name())
# Antenna name
c1 = astrofits.Column(name='ANNAME', format='A8',
array=np.array([ant.get_name() for ant in self.array[0]['ants']]))
# Station coordinates in meters
c2 = astrofits.Column(name='STABXYZ', unit='METERS', format='3D',
array=xyz)
# First order derivative of station coordinates in m/s
c3 = astrofits.Column(name='DERXYZ', unit='METERS/S', format='3E',
array=np.zeros((self.nAnt,3), dtype=np.float32))
# Orbital elements
c4 = astrofits.Column(name='ORBPARM', format='1D',
array=np.zeros((self.nAnt,), dtype=np.float64))
# Station number
c5 = astrofits.Column(name='NOSTA', format='1J',
array=np.array([self.array[0]['mapper'][ant.id] for ant in self.array[0]['ants']]))
# Mount type (0 == alt-azimuth)
c6 = astrofits.Column(name='MNTSTA', format='1J',
array=np.zeros((self.nAnt,), dtype=np.int32))
# Axis offset in meters
c7 = astrofits.Column(name='STAXOF', unit='METERS', format='3E',
array=np.zeros((self.nAnt,3), dtype=np.float32))
# Define the collection of columns
colDefs = astrofits.ColDefs([c1, c2, c3, c4, c5, c6, c7])
# Create the table and fill in the header
ag = astrofits.BinTableHDU.from_columns(colDefs)
self._add_common_keywords(ag.header, 'ARRAY_GEOMETRY', 1)
ag.header['EXTVER'] = (1, 'array ID')
ag.header['ARRNAM'] = self.siteName
ag.header['FRAME'] = ('GEOCENTRIC', 'coordinate system')
ag.header['NUMORB'] = (0, 'number of orbital parameters')
ag.header['FREQ'] = (self.refVal, 'reference frequency (Hz)')
ag.header['TIMSYS'] = ('UTC', 'time coordinate system')
date = self.astro_ref_time
utc0 = date.to_jd()
gst0 = astro.get_apparent_sidereal_time(utc0)
ag.header['GSTIA0'] = (gst0 * 15, 'GAST (deg) at RDATE 0 hours')
utc1 = utc0 + 1
gst1 = astro.get_apparent_sidereal_time(utc1)
if gst1 < gst0:
gst1 += 24.0
ds = gst1 - gst0
deg = ds * 15.0
ag.header['DEGPDY'] = (360.0 + deg, 'rotation rate of the earth (deg/day)')
refDate = self.astro_ref_time
refMJD = refDate.to_jd() - astro.MJD_OFFSET
eop = iers.IERS_Auto.open()
refAT = AstroTime(refMJD, format='mjd', scale='utc')
try:
# Temporary fix for maia.usno.navy.mil being down
ut1_utc = eop.ut1_utc(refAT)
pm_xy = eop.pm_xy(refAT)
except iers.IERSRangeError:
eop.close()
with iers.Conf().set_temp('iers_auto_url', 'ftp://cddis.gsfc.nasa.gov/pub/products/iers/finals2000A.all'):
eop = iers.IERS_Auto.open()
ut1_utc = eop.ut1_utc(refAT)
pm_xy = eop.pm_xy(refAT)
ag.header['UT1UTC'] = (ut1_utc.to('s').value, 'difference UT1 - UTC for reference date')
ag.header['IATUTC'] = (astro.leap_secs(utc0), 'TAI - UTC for reference date')
ag.header['POLARX'] = pm_xy[0].to('arcsec').value
ag.header['POLARY'] = pm_xy[1].to('arcsec').value
ag.header['ARRAYX'] = (self.array[0]['center'][0], 'array ECEF X coordinate (m)')
ag.header['ARRAYY'] = (self.array[0]['center'][1], 'array ECEF Y coordinate (m)')
ag.header['ARRAYZ'] = (self.array[0]['center'][2], 'array ECEF Z coordinate (m)')
ag.header['NOSTAMAP'] = (int(self.array[0]['enableMapper']), 'Mapping enabled for stand numbers')
ag.name = 'ARRAY_GEOMETRY'
self.FITS.append(ag)
self.FITS.flush()
if self.array[0]['enableMapper']:
self._write_mapper_hdu()
def _write_frequency_hdu(self):
"""
Define the Frequency table (group 1, table 3).
"""
nBand = len(self.freq)
# Frequency setup number
c1 = astrofits.Column(name='FREQID', format='1J',
array=np.array([self.freq[0].id,], dtype=np.int32))
# Frequency offsets in Hz
c2 = astrofits.Column(name='BANDFREQ', format='%iD' % nBand, unit='HZ',
array=np.array([f.bandFreq for f in self.freq], dtype=np.float64).reshape(1,nBand))
# Channel width in Hz
c3 = astrofits.Column(name='CH_WIDTH', format='%iE' % nBand, unit='HZ',
array=np.array([f.chWidth for f in self.freq], dtype=np.float32).reshape(1,nBand))
# Total bandwidths of bands
c4 = astrofits.Column(name='TOTAL_BANDWIDTH', format='%iE' % nBand, unit='HZ',
array=np.array([f.totalBW for f in self.freq], dtype=np.float32).reshape(1,nBand))
# Sideband flag
c5 = astrofits.Column(name='SIDEBAND', format='%iJ' % nBand,
array=np.array([f.sideBand for f in self.freq], dtype=np.int32).reshape(1,nBand))
# Baseband channel
c6 = astrofits.Column(name='BB_CHAN', format='%iJ' % nBand,
array=np.array([f.baseBand for f in self.freq], dtype=np.int32).reshape(1,nBand))
# Define the collection of columns
colDefs = astrofits.ColDefs([c1, c2, c3, c4, c5, c6])
# Create the table and header
fq = astrofits.BinTableHDU.from_columns(colDefs)
self._add_common_keywords(fq.header, 'FREQUENCY', 1)
# Add the table to the file
fq.name = 'FREQUENCY'
self.FITS.append(fq)
self.FITS.flush()
def _write_antenna_hdu(self):
"""
Define the Antenna table (group 2, table 1).
"""
nBand = len(self.freq)
# Central time of period covered by record in days
c1 = astrofits.Column(name='TIME', unit='DAYS', format='1D',
array=np.zeros((self.nAnt,), dtype=np.float64))
# Duration of period covered by record in days
c2 = astrofits.Column(name='TIME_INTERVAL', unit='DAYS', format='1E',
array=(2*np.ones((self.nAnt,), dtype=np.float32)))
# Antenna name
c3 = astrofits.Column(name='ANNAME', format='A8',
array=self.FITS['ARRAY_GEOMETRY'].data.field('ANNAME'))
# Antenna number
c4 = astrofits.Column(name='ANTENNA_NO', format='1J',
array=self.FITS['ARRAY_GEOMETRY'].data.field('NOSTA'))
# Array number
c5 = astrofits.Column(name='ARRAY', format='1J',
array=np.ones((self.nAnt,), dtype=np.int32))
# Frequency setup number
c6 = astrofits.Column(name='FREQID', format='1J',
array=(np.zeros((self.nAnt,), dtype=np.int32) + self.freq[0].id))
# Number of digitizer levels
c7 = astrofits.Column(name='NO_LEVELS', format='1J',
array=np.array([ant.levels for ant in self.array[0]['ants']]))
# Feed A polarization label
c8 = astrofits.Column(name='POLTYA', format='A1',
array=np.array([ant.polA['Type'] for ant in self.array[0]['ants']]))
# Feed A orientation in degrees
c9 = astrofits.Column(name='POLAA', format='%iE' % nBand, unit='DEGREES',
array=np.array([[ant.polA['Angle'],]*nBand for ant in self.array[0]['ants']], dtype=np.float32))
# Feed A polarization parameters
c10 = astrofits.Column(name='POLCALA', format='%iE' % (2*nBand),
array=np.concatenate([[ant.polA['Cal'],]*nBand for ant in self.array[0]['ants']]).astype(np.float32))
# Feed B polarization label
c11 = astrofits.Column(name='POLTYB', format='A1',
array=np.array([ant.polB['Type'] for ant in self.array[0]['ants']]))
# Feed B orientation in degrees
c12 = astrofits.Column(name='POLAB', format='%iE' % nBand, unit='DEGREES',
array=np.array([[ant.polB['Angle'],]*nBand for ant in self.array[0]['ants']], dtype=np.float32))
# Feed B polarization parameters
c13 = astrofits.Column(name='POLCALB', format='%iE' % (2*nBand),
array=np.concatenate([[ant.polB['Cal'],]*nBand for ant in self.array[0]['ants']]).astype(np.float32))
colDefs = astrofits.ColDefs([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10,
c11, c12, c13])
# Create the Antenna table and update it's header
an = astrofits.BinTableHDU.from_columns(colDefs)
self._add_common_keywords(an.header, 'ANTENNA', 1)
an.header['NOPCAL'] = (2, 'number of polarization parameters')
an.header['POLTYPE'] = ('X-Y LIN', 'polarization parameterization')
an.name = 'ANTENNA'
self.FITS.append(an)
self.FITS.flush()
def _write_bandpass_hdu(self):
"""
Define the Bandpass table (group 2, table 3).
"""
nBand = len(self.freq)
# Central time of period covered by record in days
c1 = astrofits.Column(name='TIME', unit='DAYS', format='1D',
array=np.zeros((self.nAnt,), dtype=np.float64))
# Duration of period covered by record in days
c2 = astrofits.Column(name='TIME_INTERVAL', unit='DAYS', format='1E',
array=(2*np.ones((self.nAnt,), dtype=np.float32)))
# Source ID
c3 = astrofits.Column(name='SOURCE_ID', format='1J',
array=np.zeros((self.nAnt,), dtype=np.int32))
# Antenna number
c4 = astrofits.Column(name='ANTENNA_NO', format='1J',
array=self.FITS['ANTENNA'].data.field('ANTENNA_NO'))
# Array number
c5 = astrofits.Column(name='ARRAY', format='1J',
array=np.ones((self.nAnt,), dtype=np.int32))
# Frequency setup number
c6 = astrofits.Column(name='FREQID', format='1J',
array=(np.zeros((self.nAnt,), dtype=np.int32) + self.freq[0].id))
# Bandwidth in Hz
c7 = astrofits.Column(name='BANDWIDTH', unit='HZ', format='1E',
array=(np.zeros((self.nAnt,), dtype=np.float32)+self.freq[0].totalBW))
# Band frequency in Hz
c8 = astrofits.Column(name='BAND_FREQ', unit='HZ', format='%iD' % nBand,
array=(np.zeros((self.nAnt,nBand,), dtype=np.float64)+[f.bandFreq for f in self.freq]))
# Reference antenna number (pol. 1)
c9 = astrofits.Column(name='REFANT_1', format='1J',
array=np.ones((self.nAnt,), dtype=np.int32))
# Real part of the bandpass (pol. 1)
c10 = astrofits.Column(name='BREAL_1', format='%iE' % (self.nChan*nBand),
array=np.ones((self.nAnt,nBand*self.nChan), dtype=np.float32))
# Imaginary part of the bandpass (pol. 1)
c11 = astrofits.Column(name='BIMAG_1', format='%iE' % (self.nChan*nBand),
array=np.zeros((self.nAnt,nBand*self.nChan), dtype=np.float32))
# Reference antenna number (pol. 2)
c12 = astrofits.Column(name='REFANT_2', format='1J',
array=np.ones((self.nAnt,), dtype=np.int32))
# Real part of the bandpass (pol. 2)
c13 = astrofits.Column(name='BREAL_2', format='%iE' % (self.nChan*nBand),
array=np.ones((self.nAnt,nBand*self.nChan), dtype=np.float32))
# Imaginary part of the bandpass (pol. 2)
c14 = astrofits.Column(name='BIMAG_2', format='%iE' % (self.nChan*nBand),
array=np.zeros((self.nAnt,nBand*self.nChan), dtype=np.float32))
colDefs = astrofits.ColDefs([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10,
c11, c12, c13, c14])
# Create the Bandpass table and update its header
bp = astrofits.BinTableHDU.from_columns(colDefs)
self._add_common_keywords(bp.header, 'BANDPASS', 1)
bp.header['NO_ANT'] = self.nAnt
bp.header['NO_POL'] = 2
bp.header['NO_BACH'] = self.nChan
bp.header['STRT_CHN'] = self.refPix
bp.name = 'BANDPASS'
self.FITS.append(bp)
self.FITS.flush()
def _write_source_hdu(self):
"""
Define the Source table (group 1, table 2).
"""
nBand = len(self.freq)
(arrPos, ag) = self.read_array_geometry()
ids = ag.keys()
obs = ephem.Observer()
obs.lat = arrPos.lat * np.pi/180
obs.lon = arrPos.lng * np.pi/180
obs.elev = arrPos.elv * np.pi/180
obs.pressure = 0
nameList = []
codeList = []
raList = []
decList = []
raPoList = []
decPoList = []
sourceID = 0
for dataSet in self.data:
if dataSet.pol == self.stokes[0]:
utc = astro.taimjd_to_utcjd(dataSet.obsTime)
date = astro.get_date(utc)
date.hours = 0
date.minutes = 0
date.seconds = 0
utc0 = date.to_jd()
obs.date = utc - astro.DJD_OFFSET
try:
currSourceName = dataSet.source.name
except AttributeError:
currSourceName = dataSet.source
if currSourceName not in nameList:
sourceID += 1
if dataSet.source == 'z':
## Zenith pointings
equ = astro.equ_posn( obs.sidereal_time()*180/np.pi, obs.lat*180/np.pi )
# format 'source' name based on local sidereal time
raHms = astro.deg_to_hms(equ.ra)
(tsecs, secs) = math.modf(raHms.seconds)
name = "ZA%02d%02d%02d%01d" % (raHms.hours, raHms.minutes, int(secs), int(tsecs * 10.0))
equPo = astro.get_equ_prec2(equ, utc, astro.J2000_UTC_JD)
else:
## Real-live sources (ephem.Body instances)
name = dataSet.source.name
equ = astro.equ_posn(dataSet.source.ra*180/np.pi, dataSet.source.dec*180/np.pi)
equPo = astro.equ_posn(dataSet.source.a_ra*180/np.pi, dataSet.source.a_dec*180/np.pi)
# current apparent zenith equatorial coordinates
raList.append(equ.ra)
decList.append(equ.dec)
# J2000 zenith equatorial coordinates
raPoList.append(equPo.ra)
decPoList.append(equPo.dec)
# name
nameList.append(name)
# calcode
code = ' '
try:
if dataSet.source.intent.lower() == 'fluxcal':
## calibrator for flux density scale and bandpass
code = 'K '
elif dataSet.source.intent.lower() == 'phasecal':
## calibrator useful to determine Complex Gains
code = 'D '
except AttributeError:
pass
codeList.append(code)
nSource = len(nameList)
# Save these for later since we might need them
self._sourceTable = nameList
# Source ID number
c1 = astrofits.Column(name='SOURCE_ID', format='1J',
array=np.arange(1, nSource+1, dtype=np.int32))
# Source name
c2 = astrofits.Column(name='SOURCE', format='A16',
array=np.array(nameList))
# Source qualifier
c3 = astrofits.Column(name='QUAL', format='1J',
array=np.zeros((nSource,), dtype=np.int32))
# Calibrator code
c4 = astrofits.Column(name='CALCODE', format='A4',
array=np.array(codeList))
# Frequency group ID
c5 = astrofits.Column(name='FREQID', format='1J',
array=(np.zeros((nSource,), dtype=np.int32)+self.freq[0].id))
# Stokes I flux density in Jy
c6 = astrofits.Column(name='IFLUX', format='%iE' % nBand, unit='JY',
array=np.zeros((nSource,nBand), dtype=np.float32))
# Stokes I flux density in Jy
c7 = astrofits.Column(name='QFLUX', format='%iE' % nBand, unit='JY',
array=np.zeros((nSource,nBand), dtype=np.float32))
# Stokes I flux density in Jy
c8 = astrofits.Column(name='UFLUX', format='%iE' % nBand, unit='JY',
array=np.zeros((nSource,nBand), dtype=np.float32))
# Stokes I flux density in Jy
c9 = astrofits.Column(name='VFLUX', format='%iE' % nBand, unit='JY',
array=np.zeros((nSource,nBand), dtype=np.float32))
# Spectral index
c10 = astrofits.Column(name='ALPHA', format='%iE' % nBand,
array=np.zeros((nSource,nBand), dtype=np.float32))
# Frequency offset in Hz
c11 = astrofits.Column(name='FREQOFF', format='%iE' % nBand, unit='HZ',
array=np.zeros((nSource,nBand), dtype=np.float32))
# Mean equinox and epoch