Debian Codegraph @

casacore-update-tai_utc

raw
1 #!/usr/bin/env python3
2 #
3 # Copyright (c) 2016, Ole Streicher
4 # License: BSD-2-Clause
5 #
6
7 import argparse
8 import datetime
9 import os
10
11 from casacore import tables
12
13 default_input = '/usr/share/zoneinfo/leap-seconds.list'
14 default_database ='/var/lib/casacore/data/geodetic/TAI_UTC'
15
16 # casacore needs a HOME environment variable, so make it happy
17 if not os.environ.get('HOME'):
18 os.environ['HOME'] = '/nonexistent'
19
20 parser = argparse.ArgumentParser(
21 usage = 'casacore-update-tai_utc [options]',
22 description = 'Update casacore TAI_UTC table from tzdata')
23 parser.add_argument('-i', '--input-file',
24 help='leap_second input data file (default: {})'
25 .format(default_input),
26 default=default_input)
27 parser.add_argument('-o', '--output-path',
28 help='output database (default: {})'
29 .format(default_database),
30 default=default_database)
31 parser.add_argument('-f', '--force',
32 help='overwrite database, even if it has another (sub)type'
33 + ' or the same NTP timestamp',
34 action='store_true')
35 args = parser.parse_args()
36
37 # Old data that is not in the zoneinfo database
38 data = [
39 { 'MJD': 37300, 'dUTC': 1.42282, 'Offset': 37300, 'Multiplier': 0.001296 },
40 { 'MJD': 37512, 'dUTC': 1.37282, 'Offset': 37300, 'Multiplier': 0.001296 },
41 { 'MJD': 37665, 'dUTC': 1.84586, 'Offset': 37665, 'Multiplier': 0.0011232 },
42 { 'MJD': 38334, 'dUTC': 1.94586, 'Offset': 37665, 'Multiplier': 0.0011232 },
43 { 'MJD': 38395, 'dUTC': 3.24013, 'Offset': 38761, 'Multiplier': 0.001296 },
44 { 'MJD': 38486, 'dUTC': 3.34013, 'Offset': 38761, 'Multiplier': 0.001296 },
45 { 'MJD': 38639, 'dUTC': 3.44013, 'Offset': 38761, 'Multiplier': 0.001296 },
46 { 'MJD': 38761, 'dUTC': 3.54013, 'Offset': 38761, 'Multiplier': 0.001296 },
47 { 'MJD': 38820, 'dUTC': 3.64013, 'Offset': 38761, 'Multiplier': 0.001296 },
48 { 'MJD': 38942, 'dUTC': 3.74013, 'Offset': 38761, 'Multiplier': 0.001296 },
49 { 'MJD': 39004, 'dUTC': 3.84013, 'Offset': 38761, 'Multiplier': 0.001296 },
50 { 'MJD': 39126, 'dUTC': 4.31317, 'Offset': 39126, 'Multiplier': 0.002592 },
51 { 'MJD': 39887, 'dUTC': 4.21317, 'Offset': 39126, 'Multiplier': 0.002592 },
52 ]
53
54 # Read the zoneinfo leap second file and return it as a list of dicts
55 # MJD dUTC Offset Multiplier.
56 # Also return the NTP timestamp of the last update.
57 offset = None
58 update_timestamp = None
59 with open(args.input_file) as f:
60 for line in f.readlines():
61 line = line.split('#', 1)
62 if not line[0]:
63 if line[1].strip().startswith('$'):
64 try:
65 update_timestamp = int(line[1].split()[1])
66 ntp_epoch = -2208988800 # NTP starts at 1900-01-01
67 utc = datetime.datetime.fromtimestamp(ntp_epoch
68 + update_timestamp)
69 update_timestamp = utc.strftime('%Y/%m/%d/00:00')
70 except ValueError:
71 pass
72 continue
73 t, dUTC = [int(c) for c in line[0].split()]
74 mjd = t/86400. + 15020.0
75 if offset is None:
76 offset = mjd
77 data.append({'MJD': mjd, 'dUTC': dUTC,
78 'Offset': offset, 'Multiplier': 0.0})
79
80 # Check if the table exists and has the correct type and subtype,
81 # and a different NTP timestamp
82 if not args.force:
83 try:
84 with tables.table(args.output_path) as tbl:
85 db_info = tbl.info()
86 if db_info['type'] != 'IERS':
87 print("Existing table type '{type}' is not 'IERS'"
88 .format(**db_info))
89 exit(1)
90 if db_info['subType'] != 'leapSecond':
91 print("Existing table type '{subType}' is not 'leapSecond'"
92 .format(**db_info))
93 exit(1)
94 if tbl.getkeyword('VS_DATE') == update_timestamp:
95 print('NTP timestamp unchanged. Not updating')
96 exit(0)
97 print('Overwrite old table {}'.format(args.output_path))
98 except RuntimeError as e:
99 print('Creating new table {}'.format(args.output_path))
100
101 if not os.path.exists(args.output_path):
102 os.makedirs(args.output_path)
103
104 # Create data table
105 columns = [
106 tables.makescacoldesc('MJD', 0., valuetype='double', keywords={'UNIT':'d'}),
107 tables.makescacoldesc('dUTC', 0., valuetype='double', keywords={'UNIT':'s'}),
108 tables.makescacoldesc('Offset', 0., valuetype='double', keywords={'UNIT':'d'}),
109 tables.makescacoldesc('Multiplier', 0., valuetype='double', keywords={'UNIT':'s'}),
110 ]
111
112 keywords = {
113 'MJD0': 37300.0,
114 'dMJD': 0.0,
115 'TAB_VERSION': '0001.0000',
116 'VS_TYPE': 'TAI_UTC difference obtained from tzone data',
117 'VS_VERSION': '{:04d}.0000'.format(len(data)),
118 'VS_CREATE': update_timestamp,
119 'VS_DATE': update_timestamp,
120 }
121
122 info = {
123 'type': 'IERS',
124 'subType': 'leapSecond',
125 'readme': 'Created from zoneinfo leap second information.',
126 }
127 with tables.table(args.output_path,
128 tables.tablecreatedesc(columns),
129 len(data)) as tbl:
130 tbl.putinfo(info)
131 tbl.putkeywords(keywords)
132 tablerows = tbl.row()
133 for i, row in enumerate(data):
134 tablerows.put(i, row)