1
+ # This code computes ISO 8601 time stamps using the data in a fits file
2
+ # https://archive.stsci.edu/pub/kepler/lightcurves/0007/000757076/kplr000757076-2009131105131_llc.fits
3
+ # Computing the ISO 6801 times requires several calculations that are
4
+ # covered in
5
+ # https://archive.stsci.edu/kepler/manuals/Data_Characteristics_Handbook_20110201.pdf
6
+ #
7
+ # The next step is to create HAPI metadata and dump the data
8
+ # as HAPI CSV or binary. To tbl_np, we should add the ISO timestamps.
9
+
10
+ import numpy as np
11
+ from astropy .table import Table
12
+ filename = '/tmp/kplr000757076-2009131105131_llc.fits'
13
+ tbl = Table .read (filename )
14
+ tbl_np = np .array (tbl )
15
+
16
+ from astropy .io import fits
17
+ hdulist = fits .open (filename )
18
+ print (hdulist [1 ].header )
19
+
20
+ LC_START = tbl .meta ['LC_START' ] # mid point of first cadence in MJD
21
+ LC_START_JD = LC_START + 2400000.5
22
+
23
+ TSTART = tbl .meta ['TSTART' ] # observation start time in BJD-BJDREF
24
+ TSTART_BJD = TSTART + tbl .meta ['BJDREFI' ] + tbl .meta ['BJDREFF' ]
25
+ TSTART_CAL = tbl .meta ['DATE-OBS' ] # TSTART as UTC calendar date
26
+
27
+ TIMSLICE = tbl .meta ['TIMSLICE' ] # time-slice readout sequence section
28
+
29
+ BJDREFI = tbl .meta ['BJDREFI' ]
30
+ BJDREFF = tbl .meta ['BJDREFF' ]
31
+
32
+ TIME0 = tbl ['TIME' ][0 ] + tbl .meta ['BJDREFI' ] + tbl .meta ['BJDREFF' ]
33
+ TIMECORR0 = tbl ['TIMECORR' ][0 ]
34
+ TIME0CORR = TIME0 - TIMECORR0
35
+
36
+ # Equation TTS is t_ts on pg 38 of
37
+ # https://archive.stsci.edu/kepler/manuals/Data_Characteristics_Handbook_20110201.pdf
38
+ TTS = (0.25 + 0.62 * (5 - TIMSLICE ))/ 86400.0
39
+ TIME0CORR_TS = TIME0 - TIMECORR0 + TTS
40
+
41
+ #t0 = t['TIME'][0] - time_correction[0] + 2454833
42
+
43
+ jd = tbl ['TIME' ].data + tbl .meta ['BJDREFI' ] + tbl .meta ['BJDREFF' ] - tbl ['TIMECORR' ].data
44
+ jd_tts = jd + TTS
45
+
46
+ print ("\n -----" )
47
+ print (f"LC_START = { LC_START } (mid point of first cadence in MJD)" )
48
+ print (f"LC_START_JD = { LC_START_JD } (mid point of first cadence in JD)" )
49
+ print ("" )
50
+ print (f"TSTART = { TSTART } (observation start time in BJD-BJDREF)" )
51
+ print (f"TSTART_BJD = { TSTART_BJD } (observation start time in BJD)" )
52
+ print (f"TSTART_CAL = { TSTART_CAL } (observation start time as calander date)" )
53
+ print ("" )
54
+ print (f"TIME0 = { TIME0 } (first time value in BJD)" )
55
+ print (f"TIMECORR0 = { TIMECORR0 } (first time correction value" )
56
+ print (f"TIME0CORR = { TIME0CORR } (first time - first time correction)" )
57
+ print (f"TIME0CORR_TS = { TIME0CORR_TS } (first time - first time correction + t_slice correction)" )
58
+
59
+ print (f"TIMSLICE = { TIMSLICE } (time-slice readout sequence section)" )
60
+ print (f"TTS = { TTS } (computed time slice offset)" )
61
+
62
+ # Given that this is zero, LC_START_JD does not have time slice correction added
63
+ print ("\n LC_START_JD - TIME0CORR = {}" .format (LC_START_JD - TIME0CORR ))
64
+
65
+ print ("-----\n " )
66
+
67
+ # https://gist.github.com/jiffyclub/1294443#file-jdutil-py-L119
68
+ def jd_to_date (jd ):
69
+ """
70
+ Convert Julian Day to date.
71
+
72
+ Algorithm from 'Practical Astronomy with your Calculator or Spreadsheet',
73
+ 4th ed., Duffet-Smith and Zwart, 2011.
74
+
75
+ Parameters
76
+ ----------
77
+ jd : float
78
+ Julian Day
79
+
80
+ Returns
81
+ -------
82
+ year : int
83
+ Year as integer. Years preceding 1 A.D. should be 0 or negative.
84
+ The year before 1 A.D. is 0, 10 B.C. is year -9.
85
+
86
+ month : int
87
+ Month as integer, Jan = 1, Feb. = 2, etc.
88
+
89
+ day : float
90
+ Day, may contain fractional part.
91
+
92
+ Examples
93
+ --------
94
+ Convert Julian Day 2446113.75 to year, month, and day.
95
+
96
+ >>> jd_to_date(2446113.75)
97
+ (1985, 2, 17.25)
98
+
99
+ """
100
+ import math
101
+ jd = jd + 0.5
102
+
103
+ F , I = math .modf (jd )
104
+ I = int (I )
105
+
106
+ A = math .trunc ((I - 1867216.25 )/ 36524.25 )
107
+
108
+ if I > 2299160 :
109
+ B = I + 1 + A - math .trunc (A / 4. )
110
+ else :
111
+ B = I
112
+
113
+ C = B + 1524
114
+
115
+ D = math .trunc ((C - 122.1 ) / 365.25 )
116
+
117
+ E = math .trunc (365.25 * D )
118
+
119
+ G = math .trunc ((C - E ) / 30.6001 )
120
+
121
+ day = C - E + F - math .trunc (30.6001 * G )
122
+
123
+ if G < 13.5 :
124
+ month = G - 1
125
+ else :
126
+ month = G - 13
127
+
128
+ if month > 2.5 :
129
+ year = D - 4716
130
+ else :
131
+ year = D - 4715
132
+
133
+ return year , month , day
134
+
135
+
136
+ def jd_to_iso (jd ):
137
+ import math
138
+
139
+ ymd = jd_to_date (jd )
140
+ #ymd = jd_to_date(LC_START_JD)
141
+ y = ymd [0 ]
142
+ m = ymd [1 ]
143
+ d = math .floor (ymd [2 ])
144
+
145
+ fd = ymd [2 ] - math .floor (ymd [2 ])
146
+
147
+ fh = 24 * fd
148
+ H = math .floor (fh )
149
+
150
+ fm = 60 * (fh - H )
151
+ M = math .floor (fm )
152
+
153
+ fs = 60 * (fm - M )
154
+ S = math .floor (fs )
155
+
156
+ fn = 1e9 * (fs - S )
157
+ N = math .floor (fn )
158
+
159
+ #print(y, m, d, H, M, S, N)
160
+ return "{0:4d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}.{6:09d}Z" .format (y , m , d , H , M , S , N )
161
+
162
+ TIME0CORR_CAL = jd_to_iso (TIME0CORR )
163
+ TIME0CORR_TS_CAL = jd_to_iso (TIME0CORR_TS )
164
+ LC_START_CAL = jd_to_iso (LC_START_JD )
165
+
166
+ print (f"DATE-OBS = { tbl .meta ['DATE-OBS' ]} (observation start time)" )
167
+ print (f"LC_START_CAL = { LC_START_CAL } (mid point of first cadence)" )
168
+ print (f"TIME0CORR_CAL = { TIME0CORR_CAL } (first time value - TIMECORR)" )
169
+ print (f"TIME0CORR_TS_CAL = { TIME0CORR_TS_CAL } (first time value - TIMECORR + TTS)" )
170
+ #print(tbl.meta['DATE-END'])
171
+
172
+ #for key, value in t.meta.items():
173
+ # print(f'{key} = {value}')
174
+
175
+ #from astropy.time import Time
176
+ #t = Time('130.0', format='tdb')
177
+ #print(tm)
178
+
179
+ #t.write('/tmp/a.csv', format='csv')
0 commit comments