-
Notifications
You must be signed in to change notification settings - Fork 1
/
vis.py
286 lines (239 loc) · 9.47 KB
/
vis.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
"""
A collection of functions for plotting and visualization.
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import datetime
import ephem
import glob
import simbad_reader
import ipdb
import math
import os
def singletarget(sim,target):
pass
def get_sun(simpath):
dt_fmt = '%Y%m%dT%H:%M:%S'
simname = simpath.split('/')[2]
summ = np.genfromtxt(simpath+simname+'.txt',dtype=None,delimiter=': ')
start = datetime.datetime.strptime(summ[1,1],dt_fmt)
strrises = np.genfromtxt(simpath+'sunrise.txt',dtype=None)
srises = [datetime.datetime.strptime(dstr,dt_fmt) for dstr in strrises]
sr_days = []
sr_times = []
for srise in srises:
sr_days.append((srise-start).days)
ttime = srise.time()
sr_times.append(ttime.hour+ttime.minute/60.+ttime.second/3600.)
sr_days=np.array(sr_days)
sr_times=np.array(sr_times)
strsets = np.genfromtxt(simpath+'sunset.txt',dtype=None)
ssets = [datetime.datetime.strptime(dstr,dt_fmt) for dstr in strsets]
ss_days = []
ss_times = []
for sset in ssets:
ss_days.append((sset-start).days)
ttime = sset.time()
ss_times.append(ttime.hour+ttime.minute/60.+ttime.second/3600.)
ss_days=np.array(ss_days)
ss_times=np.array(ss_times)
return sr_days,sr_times,ss_days,ss_times
def get_targ_rise_set(simpath,targetname):
dt_fmt = '%Y%m%dT%H:%M:%S'
simname = simpath.split('/')[2]
summ = np.genfromtxt(simpath+simname+'.txt',dtype=None,delimiter=': ')
start = datetime.datetime.strptime(summ[1,1],dt_fmt)
strrises = np.genfromtxt(simpath+targetname+'rise.txt',dtype=None)
srises = [datetime.datetime.strptime(dstr,dt_fmt) for dstr in strrises]
sr_days = []
sr_times = []
for srise in srises:
sr_days.append((srise-start).days)
ttime = srise.time()
sr_times.append(ttime.hour+ttime.minute/60.+ttime.second/3600.)
sr_days=np.array(sr_days).astype(float)
sr_times=np.array(sr_times)
strsets = np.genfromtxt(simpath+targetname+'set.txt',dtype=None)
ssets = [datetime.datetime.strptime(dstr,dt_fmt) for dstr in strsets]
ss_days = []
ss_times = []
for sset in ssets:
ss_days.append((sset-start).days)
ttime = sset.time()
ss_times.append(ttime.hour+ttime.minute/60.+ttime.second/3600.)
ss_days=np.array(ss_days).astype(float)
ss_times=np.array(ss_times)
return sr_days,sr_times,ss_days,ss_times
def get_target(simpath,targetname):
dt_fmt = '%Y%m%dT%H:%M:%S'
simname = simpath.split('/')[2]
summ = np.genfromtxt(simpath+simname+'.txt',dtype=None,delimiter=': ')
start = datetime.datetime.strptime(summ[1,1],dt_fmt)
temp = np.genfromtxt(simpath+targetname+'.txt',names=True,dtype=None)
start = datetime.datetime.strptime(summ[1,1],dt_fmt)
obs = [datetime.datetime.strptime(dstr,dt_fmt) \
for dstr in temp['obs_start']]
# ipdb.set_trace()
alts = temp['altitude']
days = []
times = []
for obser in obs:
days.append((obser-start).days)
ttime = obser.time()
times.append(ttime.hour+ttime.minute/60.+ttime.second/3600.)
days = np.array(days)
times = np.array(times)
return days,times,alts
def window_function(freqs,times):
result = []
for freq in freqs:
result.append(np.sum(np.exp(-2j*np.pi*(1./freq)*times))/len(times))
return result
def plot_target(simpath,target):
sr_days,sr_times,ss_days,ss_times = get_sun(simpath)
try:
days,times,alts = get_target(simpath,target['name'])
dec_times = days+times
except:
print('Bad pick, try again')
return
figt = plt.figure()#figsize=(11,6))
mpl.rcParams.update({'font.size': 18})
ax2 = figt.add_subplot(2,1,2)
ax2.yaxis.grid(True)
ax2.set_title('Window Function')
ax2.set_xlabel('Period [days]')
ax2.set_ylabel('Amplitude')
ax2.axis([-.05*len(sr_days),len(sr_days)+.05*len(sr_days),0,1])
freqs = np.linspace(0.001,365*3,100000)
win_func = window_function(freqs,dec_times)
plt.plot(freqs,np.absolute(win_func))
ax1 = figt.add_subplot(2,1,1)
ax1.plot(sr_days,sr_times,label='sun rises')
ax1.plot(ss_days,ss_times,label='sun sets')
"""
pltdays = []
maxplt = []
for ind in range(len(sr_times)):
if sr_times[ind]>tr_times[ind] and tr_times[ind]>ts_times[ind]:
pltdays.append(ind)
maxplt.append(sr_times[ind])
elif sr_times[ind]>tr_times[ind] and tr_times[ind]>ss_times[ind]:
pltdays.append(ind)
maxplt.append(sr_times[ind])
elif ts_times[ind]<sr_times[ind] and ts_times[ind]>ss_times[ind]:
pltdays.append(ind)
maxplt.append(ts_times[ind])
# maxplt = np.minimum(sr_times,ts_times)
# minplt = np.maximum(ss_times,tr_times)
# minplt = np.minimum(minplt,sr_times)
ax1.plot(pltdays,maxplt,'.',label='minplt')
# ax1.plot(sr_days,maxplt,label='maxplt')
# ax1.fill_between(sr_days,minplt,maxplt,alpha=.5,where=minplt<maxplt,facecolor='yellow')
# ax1.fill_between(sr_days,sr_times,ss_times,where=(tr_times[1:]<=sr_times),alpha=.5,facecolor='yellow')
"""
try:
tr_days,tr_times,ts_days,ts_times=\
get_targ_rise_set(simpath,target['name'])
tr_discont = np.where(np.abs(np.diff(tr_times)) >= 0.5)[0]+1
tr_days = np.insert(tr_days, tr_discont, np.nan)
tr_times = np.insert(tr_times, tr_discont, np.nan)
ts_discont = np.where(np.abs(np.diff(ts_times)) >= 0.5)[0]+1
ts_days = np.insert(ts_days, ts_discont, np.nan)
ts_times = np.insert(ts_times, ts_discont, np.nan)
ax1.plot(tr_days,tr_times,label='target rises',ms=2)
ax1.plot(ts_days,ts_times,label='target sets',ms=2)
except:
print('No rise/set data for target?')
# save plotting the obs for last for cleanliness in legend
ax1.plot(days,times,'.',label='obs')
ax1.axis([-.05*len(sr_days),len(sr_days)+.25*len(sr_days),\
np.min(ss_times)-.2*np.min(ss_times),\
np.max(sr_times)+.2*np.max(sr_times)])
box = ax.get_position()
# ax1.set_position([box.x0, box.y0, box.width * 1.2, box.height])
# Put a legend to the right of the current axis
ax1.legend()#loc='center left', bbox_to_anchor=(1, 0.5),borderaxespad=0.)
title_str =('%s: (%0.2f,%0.2f), Number of obs: %.1f')%(target['name'],target['ra'],target['dec'],target['num_obs'])
dt_fmt = '%Y%m%dT%H:%M:%S'
simname = simpath.split('/')[2]
summ = np.genfromtxt(simpath+simname+'.txt',dtype=None,delimiter=': ')
ax1.set_title(title_str)
ax1.set_xlabel('Days from '+summ[1,1][:8])
ax1.set_ylabel('Hours from UTC 00:00:00')
# ax2 = figt.add_subplot(2,1,2)
# ax2.plot(days,alts,'.')
plt.show()
def onpick(event,simpath,target_list):
if event.artist!=bline: return True
N=len(event.ind)
if not N: return True
thisstar = event.artist
xdata,ydata = thisstar.get_data()
ind = event.ind
print xdata[ind][0]
print ydata[ind][0]
for target in target_list:
if xdata[ind][0] == target['ra'] and ydata[ind][0] == target['dec']:
print target['name']
plot_target(simpath,target)
plt.show()
break
return True
if __name__ == '__main__':
# get the full target list
target_list = simbad_reader.read_simbad('./secret/eta_list.txt')
simnumber = raw_input('Enter sim number: ')
simpath = glob.glob('./results/*.'+simnumber+'/')[0]
os.stat(simpath)
all_ras = []
all_decs = []
max_num_obs = 0
min_num_obs = 3000
num_stars_obs=0
for target in target_list:
all_ras.append(target['ra'])
all_decs.append(target['dec'])
try:
tdays,ttimes,alts = get_target(simpath,target['name'])
target['num_obs'] = len(ttimes)
num_stars_obs+=1
if target['num_obs']>max_num_obs:
max_num_obs = target['num_obs']
if target['num_obs']<min_num_obs:
min_num_obs = target['num_obs']
except:
target['num_obs'] = 0
print(num_stars_obs)
# for mollweide
# all_ras=np.radians(np.array(all_ras)-180.)
# all_decs=np.radians(np.array(all_decs))
fig = plt.figure()
ax = fig.add_subplot(111)#,projection='mollweide')
bline, = ax.plot(all_ras,all_decs,'.',zorder=1,picker=5)
for target in target_list:
# subtract 180 to center
ra = target['ra']
dec = target['dec']
# for mollweide
# ra = math.radians(target['ra']-180.)
# dec = math.radians(target['dec'])
# ax.scatter(ra,dec,color='green')
if target['num_obs'] == 0:
ax.scatter(ra,dec,color='grey',s=30,zorder=2)
ax.text(ra,dec,target['name'],size=10)
else:
ax.scatter(ra,dec,c=target['num_obs'],s=30,zorder=2,
vmin=min_num_obs,vmax=max_num_obs,cmap=plt.cm.autumn)
ax.text(ra,dec,target['name'],size=10)
ax.grid()
ax.axis([-.5,24.9,-40,95])
# mpl.rcParams.update({'font.size': 18})
ax.set_title('Eta Earth list '+simnumber,fontsize=16)
ax.set_xlabel('Right Ascension [hours]',fontsize=16)
ax.set_ylabel('Declination [$^\circ$]',fontsize=16)
sm = plt.cm.ScalarMappable(cmap=plt.cm.autumn, norm=plt.Normalize(vmin=min_num_obs, vmax=max_num_obs))
sm._A = []
fig.canvas.mpl_connect('pick_event',lambda event: onpick(event,simpath,target_list))
plt.show()