This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdr_funcs.py
162 lines (127 loc) · 4.27 KB
/
hdr_funcs.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
"""\
FITS header calculation functions (hdrfunc).
Each function is passed a dictionary containing only the fields it
needs (per the @inkws decorator). It then RETURNS a dictionary of
field name/values to be updated.
NB: The program that calls these functions must do some extra
bookkeeping to scrape the input keywords from multiple HDUs and to
update the values into the first HDU that contains the keyword (or
HDU-0 if no HDU contains it)
All functions have the same signature.
"""
# Grouped by OUTPUT Keyword
import logging
from .hdr_decorators import inkws, outkws
#########################
### DTSERNO
###
@inkws(['DETSERNO'])
@outkws(['DTSERNO'])
def DETSERNOtoDTSERNO(orig, **kwargs):
"""Intended for soar-spartan FITS files. Allow DETSERNO to be missing."""
return {'DTSERNO': orig['DETSERNO'].strip()}
#########################
### DATE-OBS
###
@inkws(['UTSHUT'])
@outkws(['DATE-OBS'])
def fixTriplespec(orig, **kwargs):
"""Fix triplespec DATE-OBS using UTSHUT (Universal Time SHUTter)"""
new = {'DATE-OBS': orig['UTSHUT'],
#'INSTRUME': orig['INSTRUM'],
}
logging.debug('fixTriplespec: fields DATE-OBS ({})'
#', INSTRUME ({})'
#.format(new['DATE-OBS'], new['INSTRUME']))
.format(new['DATE-OBS']))
return new
@inkws(['DATE-OBS', 'TIME-OBS'])
@outkws(['DATE-OBS'])
def addTimeToDATEOBS(orig, **kwargs):
"""Use TIME-OBS for time portion of DATEOBS."""
if ('T' in orig['DATE-OBS']):
return {'DATE-OBS': orig['DATE-OBS']}
else:
return {'DATE-OBS': orig['DATE-OBS'] + 'T' + orig['TIME-OBS']}
@inkws(['DATE-OBS', 'DATE'])
@outkws(['ODATEOBS', 'DATE-OBS'])
def DATEOBSfromDATE(orig, **kwargs):
"""hdr function: DATEOBSfromDATE"""
if 'ODATEOBS' in orig:
logging.info('Overwriting existing ODATEOBS!')
return {'ODATEOBS': orig['DATE-OBS'], # save original
'DATE-OBS': orig['DATE']+'.0' }
#########################
### DTCALDAT
###
@inkws(['DATE-OBS'])
@outkws(['DTCALDAT'])
def DTCALDATfromDATEOBStus(orig, **kwargs):
"""hdr function: DTCALDATfromDATEOBStus"""
from dateutil import tz
import datetime as dt
local_zone = tz.gettz('America/Phoenix')
utc = dt.datetime.strptime(orig['DATE-OBS'], '%Y-%m-%dT%H:%M:%S.%f')
utc = utc.replace(tzinfo=tz.tzutc()) # set UTC zone
localdt = utc.astimezone(local_zone)
if localdt.time().hour > 9:
caldate = localdt.date()
else:
caldate = localdt.date() - dt.timedelta(days=1)
#!logging.debug('localdt={}, DATE-OBS={}, caldate={}'
#! .format(localdt, orig['DATE-OBS'], caldate))
new = {'DTCALDAT': caldate.isoformat()}
return new
@inkws(['DATE-OBS'])
@outkws(['DTCALDAT'])
def DTCALDATfromDATEOBSchile(orig, **kwargs):
"""hdr function: DTCALDATfromDATEOBSchile"""
from dateutil import tz
import datetime as dt
local_zone = tz.gettz('Chile/Continental')
utc = dt.datetime.strptime(orig['DATE-OBS'], '%Y-%m-%dT%H:%M:%S.%f')
utc = utc.replace(tzinfo=tz.tzutc()) # set UTC zone
localdt = utc.astimezone(local_zone)
if localdt.time().hour > 12:
caldate = localdt.date()
else:
caldate = localdt.date() - dt.timedelta(days=1)
logging.debug('localdt={}, DATE-OBS={}, caldate={}'
.format(localdt, orig['DATE-OBS'], caldate))
new = {'DTCALDAT': caldate.isoformat()}
return new
#########################
### DTPROPID
###
@inkws(['PROPID'])
@outkws(['DTPROPID'])
def PROPIDplusCentury(orig, **kwargs):
"""Add missing century"""
return {'DTPROPID': '20' + orig.get('PROPID','NA').strip('"') }
#########################
### DTINSTR
###
@inkws(['INSTRUME'])
@outkws(['DTINSTRU'])
def INSTRUMEtoDT(orig, **kwargs):
"""hdr function: INSTRUMEtoDT"""
if 'DTINSTRU' in orig:
return {'DTINSTRU': orig['DTINSTRU'] }
else:
return {'DTINSTRU': orig['INSTRUME'] }
#########################
### OBSTYPE
###
@inkws(['IMAGETYP'])
@outkws(['OBSTYPE'])
def IMAGTYPEtoOBSTYPE(orig, **kwargs):
"""hdr function: IMAGTYPEtoOBSTYPE"""
return {'OBSTYPE': orig['IMAGETYP'] }
#########################
### OBSID
###
@inkws(['DATE-OBS'])
@outkws(['OBSID'])
def bokOBSID(orig, **kwargs):
"""hdr function: bokOBSID"""
return {'OBSID': 'bok23m.'+orig['DATE-OBS'] }