-
Notifications
You must be signed in to change notification settings - Fork 1
/
targs.py
322 lines (274 loc) · 8.43 KB
/
targs.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
import pandas as pd
import numpy as np
import utils
import datetime
import json
import traceback
import sys
import io
import math
import os
import dss2Header
from inOutChecker import InOutChecker
from maskLayouts import MaskLayouts
def readRaw(fh,params):
def toFloat(x):
try:
return float(x)
except:
return 0
out = []
cols = (
"objectId",
"raHour",
"decDeg",
"eqx",
"mag",
"pBand",
"pcode",
"sampleNr",
"selected",
"slitLPA",
"length1",
"length2",
"slitWidth",
"orgIndex",
"inMask",
"raRad",
"decRad",
)
cnt = 0
slitLength = float(params["MinSlitLengthfd"][0]) #### correct paramn name?
halfLen = slitLength / 2.
slitWidth = params["SlitWidthfd"][0] ####
slitpa = params["SlitPAfd"][0] ####
for nr, line in enumerate(fh):
if not line:
continue
line = line.strip()
p1, p2, p3 = line.partition("#")
parts = p1.split()
if len(parts) == 0:
# line empty
continue
objectId = parts[0]
parts = parts[1:]
if len(parts) < 3:
continue
if 'PA=' in line:
#line has dsim output first line
continue
# print (nr, "len", parts)
template = ["", "", "2000", "99", "I", "0", "-1", "0", slitpa, halfLen, halfLen, slitWidth, "0", "0"]
minLength = min(len(parts), len(template))
template[:minLength] = parts[:minLength]
sampleNr, selected, slitLPA, length1, length2, slitWidth = 1, 1, 0, 4, 4, 1.5
mag, pBand, pcode = 99, "I", 99
try:
try:
raHour=float(template[0])
except:
raHour = utils.sexg2Float(template[0])
if raHour < 0 or raHour > 24:
raise Exception("Bad RA value " + raHour)
try:
decDeg=float(template[1])
except:
decDeg = utils.sexg2Float(template[1])
if decDeg < -90 or decDeg > 90:
raise Exception("Bad DEC value " + decDeg)
eqx = float(template[2])
if eqx > 3000:
eqx = float(template[2][:4])
tmp = template[2][4:]
template[3 : minLength + 1] = parts[2:minLength]
template[3] = tmp
mag = toFloat(template[3])
pBand = template[4].upper()
pcode = int(template[5])
sampleNr = int(template[6])
selected = int(template[7])
slitLPA = toFloat(template[8])
length1 = toFloat(template[9])
length2 = toFloat(template[10])
slitWidth = toFloat(template[11])
inMask = int(template[12])
except Exception as e:
# traceback.print_exc()
# break
pass
raRad = math.radians(raHour * 15)
decRad = math.radians(decDeg)
target = (
objectId,
raHour,
decDeg,
eqx,
mag,
pBand,
pcode,
sampleNr,
selected,
slitLPA,
length1,
length2,
slitWidth,
cnt,
inMask,
raRad,
decRad,
)
out.append(target)
cnt += 1
df = pd.DataFrame(out, columns=cols)
# df["inMask"] = np.zeros_like(df.name)
fieldcenterRADeg = df.raHour.mean() * 15
fieldcenterDEC = df.decDeg.mean()
return df
def toJsonWithInfo(params,tgs,xgaps=[]):
"""
Returns the targets and ROI info in JSON format
"""
data = [list(tgs[i]) for i in tgs]
data1 = {}
for i, colName in enumerate(tgs.columns):
data1[colName] = data[i]
data2 = {"info": getROIInfo(params), "targets": data1, "xgaps": xgaps}
return json.dumps(data2)
def getROIInfo(params):
"""
Returns a dict with keywords that look like fits headers
Used to show the footprint of the DSS image
"""
centerRADeg,centerDEC,positionAngle=15*utils.sexg2Float(params['InputRAfd'][0]),utils.sexg2Float(params['InputDECfd'][0]),params['MaskPAfd'][0]
hdr = dss2Header.DssWCSHeader(centerRADeg, centerDEC, 60, 60)
north, east = hdr.skyPA()
nlist = "platescl", "xpsize", "ypsize" # , 'raDeg', 'decDeg'
out = {n: hdr.__dict__[n] for n in nlist}
out["centerRADeg"] = "%.7f" % centerRADeg
out["centerDEC"] = "%.7f" % centerDEC
out["NAXIS1"] = hdr.naxis1
out["NAXIS2"] = hdr.naxis2
out["northAngle"] = north
out["eastAngle"] = east
out["xpsize"] = hdr.xpsize # pixel size in micron
out["ypsize"] = hdr.ypsize # pixel size in micron
out["platescl"] = hdr.platescl # arcsec / mm
out["positionAngle"] = positionAngle
return out
def setColum(targets, colName, value):
"""
Updates the dataframe by column name
"""
targets[colName] = value
return targets
def findTarget(targets, targetName):
"""
Finds entry with the given targName.
Returns idx, or -1 if not found
"""
for i, stg in targets.iterrows():
if stg.objectId == targetName:
return stg.orgIndex
return -1
def updateColumn(targets,col,value):
"""
Used by GUI to change values to an entire column of a target.
"""
# options=['SlitPAfd','MaskPAfd','length1','length2','slitWidth']
# if col=='slitLPA':value=float(jvalues["slitLPA"])
# if col=='length1':value=float(jvalues["length1"])
# if col=='length2':value=float(jvalues["length2"])
# if col=='slitWidth':value=float(jvalues["slitWidth"])
if col=='length1' or col=='length2':
targets['length1']=float(value)
targets['length2']=float(value)
else:
targets[col]=float(value)
print(targets[col])
return targets
def updateTarget(targets, jvalues):
"""
Used by GUI to change values in a target.
"""
print('Running updateTarget\n\n\n\n\n\n\n\n\n')
values=jvalues
tgs = targets
pcode = int(values["prior"])
selected = int(values["selected"])
slitLPA = float(values["slitLPA"])
slitWidth = float(values["slitWidth"])
len1 = float(values["len1"])
len2 = float(values["len2"])
targetName = values["targetName"]
mag = float(values["mag"])
raSexa = values["raSexa"]
decSexa = values["decSexa"]
raHour = utils.sexg2Float(raSexa)
decDeg = utils.sexg2Float(decSexa)
raRad = math.radians(raHour * 15)
decRad = math.radians(decDeg)
idx = findTarget(tgs,targetName)
if idx >= 0:
# Existing entry
tgs.at[idx, "pcode"] = pcode
tgs.at[idx, "selected"] = selected
tgs.at[idx, "slitLPA"] = slitLPA
tgs.at[idx, "slitWidth"] = slitWidth
tgs.at[idx, "length1"] = len1
tgs.at[idx, "length2"] = len2
tgs.at[idx, "mag"] = mag
tgs.at[idx, "raHour"] = raHour
tgs.at[idx, "decDeg"] = decDeg
tgs.at[idx, "raRad"] = raRad
tgs.at[idx, "decRad"] = decRad
#Missing targets=tgs????????
else:
# Add a new entry
idx = len(targets.index)
print('idx= ',idx)
newItem = {
"objectId": targetName,
"raHour": raHour,
"decDeg": decDeg,
"eqx": 2000,
"mag": values["mag"],
"pBand": values["pBand"],
"pcode": int(values["prior"]),
"sampleNr": 1,
"selected": selected,
"slitLPA": slitLPA,
"inMask": 0,
"length1": len1,
"length2": len2,
"slitWidth": slitWidth,
"orgIndex": idx,
"raRad": raRad,
"decRad": decRad,
}
targets = tgs.append(newItem, ignore_index=True)
return targets,idx
def deleteTarget(targets, idx):
"""
Remove a row idx from the data frame
"""
tgs = targets
print('Index to delete',idx)
if idx < 0:
return
targets = tgs.drop(tgs.index[idx])
# SMDTLogger.info("Delete target idx")
return targets
def markInside(targets):
"""
Sets the inMask flag to 1 (inside) or 0 (outside)
"""
layout=MaskLayouts['deimos']
inOutChecker = InOutChecker(layout)
tgs = targets
inMask = []
for i, stg in tgs.iterrows():
isIn = 1 if inOutChecker.checkPoint(stg.xarcs, stg.yarcs) else 0
inMask.append(isIn)
targets["inMask"] = inMask
return targets