eff_azel2radec.py
10.1 KB
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
title = "AzEl<->RaDec"
tip = "transforms horizontal map to given coordinate system"
onein = True
import numpy as np
import nodastro
from nodmath import FixNaNs
#from scipy.ndimage import map_coordinates
from nodmath import map_interpolate
try:
from scipy.interpolate import griddata
except:
from nodmath import griddata
from guidata.qt.QtGui import QMessageBox
from guidata.dataset.datatypes import DataSet
from guidata.dataset.dataitems import (IntItem, StringItem, ChoiceItem, FloatItem, BoolItem)
from guiqwt.config import _
RAD = np.pi/180.0
def nint(x):
if x > 0: return int(x+0.5)
else: return int(x-0.5)
class NOD3_App:
def __init__(self, parent):
self.parent = parent
self.parent.activateWindow()
def Error(self, msg):
QMessageBox.critical(self.parent.parent(), title,
_(u"Error:")+"\n%s" % str(msg))
def compute_app(self, **args):
class FuncParam(DataSet):
descript = BoolItem("Descriptive", default=True)
name = title.replace(" ", "")
if args == {}:
param = FuncParam(_(title), "Transform in descriptive coordinate system:")
else:
param = self.parent.ScriptParameter(name, args)
# if no parameter needed set param to None. activate next line
param = None
self.parent.compute_11(name, lambda m, p: self.function(m, p), param, onein)
def matrix(self, ra, dec, epoch, desc):
if epoch == 2000:
pmat = self.na.adnow2000
elif epoch == 1950:
pmat = self.na.adnow1950
else:
txt = str("Epoch of %d not yet defined" % epoch)
self.Error(txt)
pmat = np.array(self.na.unit)
return None
rmat = self.na.rotmat(ra, dec, 0.0)
if desc:
return np.dot(pmat.T, rmat)
else:
return pmat.T
def radec2azel(self, m, p_descript):
na = self.na
epoch = int(m.header['EPOCH']+0.5)
if m.header['CTYPE1'][:2] == "GL":
txt = str("Map in galactic coordinates, cannot transform to Az/El")
self.Error(txt)
return []
phi = m.header['SITELAT']
RA = m.header['CRVAL1']
DEC = m.header['CRVAL2']
dAz = m.header['PATLONG']
dEl = m.header['PATLAT']
xpix = m.header['CRPIX1']-1
ypix = m.header['CRPIX2']-1
dxpix = abs(m.header['CDELT1'])
dypix = m.header['CDELT2']
sid = m.sidmap.ravel()
par = m.parmap.ravel()
# calculate new sidereal time and eqatorial coordinates from horizontal
pmat = self.matrix(RA, DEC, epoch, False)
mat = self.matrix(RA, DEC, epoch, p_descript)
ra_apcent, dec_apcent = self.na.getCoord(RA, DEC, pmat)
if p_descript:
Mat = na.rotmat(ra_apcent, dec_apcent, 0.0)
else:
Mat = np.array(na.unit)
py, px = np.indices(m.data.shape)
ddec, dra = (py.ravel()-ypix)*dypix, -(px.ravel()-xpix)*dxpix
# apparent position absolut
r_ap, d_ap = self.na.getCoord(dra, ddec, mat)
az, el = self.na.azel(phi, sid-r_ap, d_ap)
az = np.where(az < 0.0, az+360, az)
az_center, el_center = self.na.azel(phi, sid-ra_apcent, 0*sid+dec_apcent)
az_center = np.where(az_center < 0.0, az_center+360, az_center)
Az, El = (az-az_center)*np.cos(RAD*el), el-el_center
Az = np.where(Az < -180.0, Az+360, Az)
y, x = (El-np.nanmin(El))/dypix, (Az-np.nanmin(Az))/dxpix
newcols = nint(abs(np.nanmax(Az)-np.nanmin(Az))/dxpix)+1
newrows = nint(abs(np.nanmax(El)-np.nanmin(El))/dypix)+1
grid_y, grid_x = np.indices((newrows, newcols))
newsid = griddata((y, x), sid, (grid_y, grid_x), method='nearest')
sid = newsid.ravel()
newpar = griddata((y, x), par, (grid_y, grid_x), method='nearest')
par = newpar.ravel()
rows, cols = np.indices((newrows, newcols))
newypix, newxpix = -np.nanmin(El)/dypix, -np.nanmin(Az)/dxpix
El, Az = (rows.ravel()-newypix)*dypix, (cols.ravel()-newxpix)*dxpix
az_center, el_center = self.na.azel(phi, sid-ra_apcent, 0*sid+dec_apcent)
el = el_center + El
az = az_center + Az/np.cos(RAD*el)
t, d = self.na.azel(phi, az, el)
ra, dec = self.na.getCoord(sid-t, d, mat.T)
px = xpix - ra/dxpix
py = ypix + dec/dypix
# interpolation
#m.data = FixNaNs(m.data)
#m.data = map_coordinates(m.data, (py, px), order=4, mode='constant',
# cval=np.nan, prefilter=True, output=np.float32)
m.data = map_interpolate(m.data, px, py)
m.data = m.data.reshape((newrows, newcols))
m.sidmap = sid.reshape((newrows, newcols))*240.0
m.parmap = par.reshape((newrows, newcols))*240.0
m.header['PATLONG'] = 0.0
m.header['PATLAT'] = 0.0
m.header['CDELT1'] = abs(m.header['CDELT1'])
m.header['CRPIX1'] = newxpix+1 + dAz/m.header['CDELT1']
m.header['CRPIX2'] = newypix+1 + dEl/m.header['CDELT2']
#m.header['CTYPE1'] = "RA---SFL"
#m.header['CTYPE2'] = "DEC--SFL"
m.header['NAXIS1'] = newcols
m.header['NAXIS2'] = newrows
m.header['SCANDIR'] = 'ALON'
return m
def azel2radec(self, m, p_descript):
na = self.na
epoch = int(m.header['EPOCH']+0.5)
phi = m.header['SITELAT']
RA = m.header['CRVAL1']
DEC = m.header['CRVAL2']
dAz = m.header['PATLONG']
dEl = m.header['PATLAT']
xpix = m.header['CRPIX1']-1 + dAz/m.header['CDELT1']
ypix = m.header['CRPIX2']-1 + dEl/m.header['CDELT2']
dxpix = m.header['CDELT1']
dypix = m.header['CDELT2']
sid = m.sidmap.ravel()
par = m.parmap.ravel()
# calculate new sidereal time and eqatorial coordinates from horizontal
pmat = self.matrix(RA, DEC, epoch, False)
mat = self.matrix(RA, DEC, epoch, p_descript)
ra_apcent, dec_apcent = self.na.getCoord(RA, DEC, pmat)
if p_descript:
Mat = na.rotmat(ra_apcent, dec_apcent, 0.0)
else:
Mat = np.array(na.unit)
az_center, el_center = self.na.azel(phi, sid-ra_apcent, 0*sid+dec_apcent)
py, px = np.indices(m.data.shape)
el = (py.ravel()-ypix)*dypix + el_center - dEl
az = (px.ravel()-xpix)*dxpix/np.cos(RAD*el) + az_center #- dAz
t, d = self.na.azel(phi, az, el)
# apparent position descriptive or absolut
ra, dec = self.na.getCoord(sid-t, d, Mat.T)
y, x = (dec-np.nanmin(dec))/dypix, (np.nanmax(ra)-ra)/dxpix
# catalog position descriptive or absolut
ra, dec = self.na.getCoord(sid-t, d, mat.T)
newcols = nint(abs(np.nanmax(ra)-np.nanmin(ra))/dxpix)+1
newrows = nint(abs(np.nanmax(dec)-np.nanmin(dec))/dypix)+1
grid_y, grid_x = np.indices((newrows, newcols))
newsid = griddata((y, x), sid, (grid_y, grid_x), method='nearest')
sid = newsid.ravel()
newpar = griddata((y, x), par, (grid_y, grid_x), method='nearest')
par = newpar.ravel()
rows, cols = np.indices((newrows, newcols))
newypix, newxpix = -np.nanmin(dec)/dypix, np.nanmax(ra)/dxpix
ddec = (rows.ravel()-newypix)*dypix
dra = -(cols.ravel()-newxpix)*dxpix
if p_descript:
r, d = self.na.getCoord(dra, ddec, mat)
else:
r, d = self.na.getCoord(ra+dra, dec+ddec, mat)
az, el = self.na.azel(phi, sid-r, d)
ac, ec = self.na.azel(phi, sid-ra_apcent, 0*d+dec_apcent)
#px = xpix + (az-ac+dAz)/dxpix*np.cos(RAD*el)
px = xpix + (az-ac)/dxpix*np.cos(RAD*el)
py = ypix + (el-ec+dEl)/dypix
# interpolation
#m.data = FixNaNs(m.data)
#m.data = map_coordinates(m.data, (py, px), order=4, mode='constant',
# cval=np.nan, prefilter=True, output=np.float32)
m.data = map_interpolate(m.data, px, py)
m.data = m.data.reshape((newrows, newcols))
m.sidmap = sid.reshape((newrows, newcols))*240.0
m.parmap = par.reshape((newrows, newcols))*240.0
m.header['PATLONG'] = 0.0
m.header['PATLAT'] = 0.0
m.header['CDELT1'] = -abs(m.header['CDELT1'])
m.header['CRPIX1'] = newxpix+1
m.header['CRPIX2'] = newypix+1
m.header['NAXIS1'] = newcols
m.header['NAXIS2'] = newrows
if p_descript:
m.header['CTYPE1'] = "RA---DES"
m.header['CTYPE2'] = "DEC--DES"
else:
m.header['CTYPE1'] = "RA---CAR"
m.header['CTYPE2'] = "DEC--CAR"
#m.header.__delitem__('SCANDIR')
#m.header['SCANDIR'] = "None"
del m.header['SCANDIR']
return m
def function(self, m, p):
if p == None:
p_descript = True
else:
p_descript = p.descript
na = nodastro.nodtrafo(m.header['DATE_OBS'])
self.na = na
if m.header['CTYPE1'][:4] == "GLON":
m.header['CRVAL1'], m.header['CRVAL2'] = \
na.trafo(m.header['CRVAL1'], m.header['CRVAL2'], na.lbad2000)
if int(m.header['EPOCH']+0.5) != 1950 and int(m.header['EPOCH']+0.5) != 2000:
self.Error("Map not in J2000 or B1950")
return [], None
# rescale sidmap, parmap
if 'SIDPIX' in m.header:
dx = m.header['SIDPIX']
ix = nint(dx+0.5)
else:
ix = 0
if not hasattr(m, 'sidmap'):
self.Error("sorry, no sidereal time data available")
return [], None
m.sidmap = m.sidmap[:,:m.sidmap.shape[1]-ix+1]/240.0
m.parmap = m.parmap[:,:m.parmap.shape[1]-ix+1]/240.0
if 'SCANDIR' in m.header and m.header['SCANDIR'] in ('ALON', 'ALAT'):
m = self.azel2radec(m, p_descript)
else:
m = self.radec2azel(m, p_descript)
if m != []:
m = self.parent.removeNANedges(m)
amap = m.parmap.ravel()
m.header["PARANG"] = (amap[len(amap)/2], 'Mean parallactic angle')
self.parent.SidOut = True
self.parent.ParOut = True
return m, p