-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake_plot.py
More file actions
656 lines (489 loc) · 19.3 KB
/
make_plot.py
File metadata and controls
656 lines (489 loc) · 19.3 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# Matt Bonnyman 19 July 2018
import numpy as np
import astropy.units as u
from astropy.time import Time
import matplotlib.pyplot as plt
import weights as weights
from hms_to_hr import hms_to_hr
from intervals import intervals
def airmass(date, plan, obs_id, am, local, moonam=None, obslabels=False, description='', istart=0, iend=-1,
savefig=False):
"""
Create air mass plot of current plan.
Parameters
----------
date : string
date of current plan
plan : numpy integer array or None
indices of targets from 'targets' along tot_time grid.
obs_id : np.array of string
observation identifiers of targets.
am : np.arrays of float
target airmasses along tot_time grid.
local : np.array of string
local times along tot_time grid in format accepted by 'astropy.tot_time.Time'.
moonam : array of floats, optional
moon air mass values. Default = None
obslabels : boolean, optional
annotate plot with observation identifiers. Default = False
description : string, optional
descriptor appended to plot title (eg. '2018-08-12 schedule"description"'). Default = ''.
savefig : boolean, optional
save figure as png. Default = False.
istart : integer, optional
starting index of plan window to include in plot. Default = 0
iend : integer, optional
ending index of plan window to include in plot. Default = -1
"""
verbose = False
thk = 4
thn = 1
if iend == -1:
iend = len(plan) - 1
pplan = plan[istart:iend + 1]
hours = _hour_from_midnight(local)
if moonam is not None:
plt.plot(hours.value, moonam, linestyle=':', label='Moon', linewidth=thn, color='grey', markersize=0)
index = np.unique(pplan) # indices of obs in plan
for ind in index:
if ind >= 0:
plt.plot(hours.value, am[ind], linestyle='-', linewidth=thn, color='black', markersize=0)
for ind in index:
if ind >= 0:
ii = np.where(pplan == ind)[0][:]
intvl = intervals(ii)
iint = np.unique(intvl)
if verbose:
print('ii', ii)
print('intvl', intvl)
print('iint', iint)
for i in iint:
jj = np.where(intvl == i)[0][:]
if verbose:
print('jj', jj)
plt.plot(hours[ii[jj[0]]:ii[jj[-1]]+1].value, am[ind][ii[jj[0]]:ii[jj[-1]]+1], linestyle='-',
linewidth=thk, markersize=0) # , label=targets['id'][ind])
if obslabels:
arrowprops = dict(arrowstyle="<-", connectionstyle="arc3")
plt.annotate(obs_id[ind][-11:], xy=(hours[ii[jj[0]]].value, am[ind][ii[jj[0]]]),
xytext=(hours[ii[jj[0]]].value, am[ind][ii[jj[0]]] + 0.2), arrowprops=arrowprops)
plt.title(date+' schedule'+description)
plt.ylim(2.1, 0.9)
plt.xlim(hours[0].value, hours[-1].value)
plt.ylabel('Airmass')
plt.xlabel(r'$\Delta t$ from local midnight (hrs)')
plt.legend(loc=8, ncol=4, fontsize=8, markerscale=0.5)
plt.tight_layout()
if savefig:
plt.savefig('amplot'+date+'.png')
plt.show(block=True)
# input(' Press enter to close plot window...')
plt.close()
plt.clf()
return
def altaz(date, plan, obs_id, az, zd, moonaz=None, moonzd=None, obslabels=False, description='', savefig=False,
istart=0, iend=-1):
"""
Create air mass plot of current plan.
Parameters
----------
date : string
date of current schedule
plan : numpy integer array or None
indices of targets from 'targets' along tot_time grid.
obs_id : np.array of string
observation identifiers of targets.
az : arrays of 'astropy.units' degrees
target azimuth angles along tot_time grid
zd : arrays of 'astropy.units' degrees
target zenith distance angles along tot_time grid
moonaz : array of 'astropy.units' degrees, optional
moon azimuth angles along tot_time grid. Default = None
moonzd : arrays of 'astropy.units' degrees, optional
moon zenith distance angles along tot_time grid. Default = None
obslabels : boolean, optional
annotate plot with observation identifiers. Default = False
description : string, optional
descriptor appended to plot title (eg. '2018-08-12 schedule"description"'). Default = ''.
savefig : boolean, optional
save figure as png. Default = False.
istart : integer, optional
starting index of plan window to include in plot. Default = 0
iend : integer, optional
ending index of plan window to include in plot. Default = -1
"""
verbose = False
# Line thicknesses
thk = 4
thn = 1
if iend == -1:
iend = len(plan) - 1
pplan = plan[istart:iend]
ax = plt.subplot(111, polar=True)
if moonaz is not None and moonzd is not None:
kk = np.where(moonzd <= 90*u.deg)
ax.plot(moonaz[kk].to(u.rad), moonzd[kk], linestyle=':', label='Moon', linewidth=thn,
color='grey', markersize=0)
index = np.unique(pplan) # indices of obs in plan
for ind in index:
if ind >= 0:
ii = np.where(zd[ind] <= 90*u.deg)
ax.plot(az[ind][ii].to(u.rad), zd[ind][ii], linestyle='-', linewidth=thn,
color='grey', markersize=0)
for ind in index:
if ind >= 0:
ii = np.where(pplan == ind)[0][:]
intvl = intervals(ii)
iint = np.unique(intvl)
if verbose:
print('ii', ii)
print('intvl', intvl)
print('iint', iint)
for i in iint:
jj = np.where(intvl == i)[0][:]
if verbose:
print('jj', jj)
ax.plot(az[ind][ii[jj[0]]:ii[jj[-1]]+1].to(u.rad),
zd[ind][ii[jj[0]]:ii[jj[-1]]+1], linestyle='-', linewidth=thk)
if obslabels:
arrowprops = dict(arrowstyle="<-", connectionstyle="arc3")
ax.annotate(obs_id[ind][-11:], (az[ind][ii[jj[0]]], zd[ind][ii[jj[0]]]), arrowprops=arrowprops)
if verbose:
print(zd[ind][ii[jj[0]]:ii[jj[-1]]+1])
ax.set_title(str(date)+' schedule'+description)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rlim(0, 90)
plt.legend(loc=8, ncol=4, fontsize=8, markerscale=0.5)
if savefig:
plt.savefig('altazplot'+date+'.png')
plt.tight_layout()
plt.show(block=True)
# input(' Press enter to close plot window...')
plt.close()
plt.clf()
return
def vsb(vsb, local_time, date, obs_id, savefig=False):
"""
Plot V sky brightness
:param vsb:
:param local_time:
:return:
"""
plt.title(obs_id + ' on ' + date + ': sky brightness')
hours = _hour_from_midnight(local_time)
plt.plot(hours, vsb, label='sky brightness', alpha=0.7, linestyle='--')
# plt.legend(loc='upper right', fontsize=8, markerscale=0.5)
plt.ylabel('V sky brightness')
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.gca().invert_yaxis()
if savefig:
plt.savefig('vsbplot' + date + '.png')
plt.tight_layout()
plt.show(block=True)
# input(' Press enter to close plot window...')
plt.close()
plt.clf()
return
def skyconditions(skycond, local_time, date, bg=None, savefig=False, verbose = False):
"""
Plot sky conditions percentiles
Parameters
----------
skycond : 'astropy.table.Table'
Sky conditions table with columns 'iq', 'cc', 'wv'.
local_time : array of strings or 'astropy.tot_time.core.Time' array
Time grid of local times in format accepted by 'astropy.tot_time.Time'
date : string
Plot date
bg : array of floats (optional)
Sky background conditions for target
"""
plt.title(date + ' sky conditions')
hours = _hour_from_midnight(local_time)
if verbose:
print(date)
print(Time(local_time[-1].iso[0:10]))
print(hours)
print(skycond['iq'].quantity)
print(skycond['cc'].quantity)
print(skycond['wv'].quantity)
print(bg)
plt.plot(hours, skycond['iq'].quantity, label='image quality', alpha=0.7, linestyle='--')
plt.plot(hours, skycond['cc'].quantity, label='cloud condition', alpha=0.7, linestyle='--')
plt.plot(hours, skycond['wv'].quantity, label='water vapor', alpha=0.7, linestyle='--')
if bg is not None:
plt.plot(hours, bg, label='sky background', alpha=0.7, linestyle='--')
plt.legend(loc='upper right', fontsize=8, markerscale=0.5)
plt.ylabel('Decimal percentile')
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
if savefig:
plt.savefig('condplot' + date + '.png')
plt.tight_layout()
plt.show(block=True)
# input(' Press enter to close plot window...')
plt.close()
plt.clf()
return
def windconditions(wind, local_time, date, savefig=False):
"""
Plot sky conditions percentiles
Parameters
----------
wind : 'astropy.table.Table'
Wind conditions table with columns dir, vel.
local_time : array of strings
tot_time grid of local times in format accepted by 'astropy.tot_time.Time'
date : string
Date of tot_time window
"""
verbose = False
plt.title(date + ' wind conditions')
hours = _hour_from_midnight(local_time)
if verbose:
print(date)
print(Time(local_time[-1][0:10]))
print(wind['vel'].quantity.value)
print(wind['dir'].quantity.value)
# Add 90 degrees so that 0 degrees points North.
# Invert x-component so that degrees increase in clockwise direction.
r = 0.5
xcomp = r * np.cos(wind['dir'].quantity + 90*u.deg) * -1
ycomp = r * np.sin(wind['dir'].quantity + 90*u.deg)
plt.quiver(hours.value, wind['vel'].quantity.value, xcomp, ycomp)
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel(r'$velocity$ (m/s)')
plt.xlim(hours[0].value, hours[-1].value)
plt.ylim(-0.5, np.max(wind['vel'].quantity.value) + 2)
if savefig:
plt.savefig('windplot'+date+'.png')
plt.tight_layout()
plt.show(block=True)
# input(' Press enter to close plot window...')
plt.close()
plt.clf()
return
def weightfunction(obs_id, local_time, date, weight):
"""
Show plot of target weighting function with respect to tot_time.
Parameters
----------
obs_id : string
Unique observation identifier
local_time : array of strings or 'astropy.tot_time.core.Time' array
Time grid of local times in format accepted by 'astropy.tot_time.Time'
date : string
Plot date
weight : np.array of floats
weighting function values. Must have same length as columns in timetable.
"""
hours = _hour_from_midnight(Time(local_time))
# Get non-zero parts of weight function
ii = np.where(weight > 0)[0][:]
if len(ii) != 0:
i_intervals = intervals(ii)
wmin = np.min(weight[ii])
wmax = np.max(weight[ii])
else:
print(' No non-zero weights.')
return
# Plot non-zero portions of weight function
n_int = np.unique(i_intervals)
for n in n_int:
iint = ii[np.where(i_intervals == n)[0][:]]
plt.plot(hours[iint].value, weight[iint], color='black')
plt.title(obs_id + ' on ' + date)
plt.xlim(hours[0].value, hours[-1].value)
plt.ylim(wmin, wmax)
plt.ylabel('Weight')
plt.xlabel(r'$\Delta t$ from local midnight (hrs)')
plt.tight_layout()
plt.show(block=True)
# input('\n Press enter to close window...')
plt.close()
plt.clf()
return
def weightcomponents(obs_id, ra, dec, iq, cc, bg, wv, elev_const, i_wins, band, user_prior, AM, HA, AZ, latitude,
prog_comp, obs_comp, skyiq, skycc, skybg, skywv, winddir, windvel, wra, localtimes):
"""
Calculate observation weights.
Parameters
----------
obs_id : string
observation identifier (only needed if printing output)
ra : 'astropy.units' degrees
observation right ascension
dec : 'astropy.units' degrees
observation declination
iq : float
observation image quality constraint percentile
cc : float
observation cloud condition constraint percentile
bg : float
observation sky background constraint percentile
wv : float
observation water vapour constraint percentile
elev_const : dictionary
observation elevation constraint (type, min, max).
Example
-------
elev_const = {type='Hour Angle', min='-2.00', max='2.00'}
i_wins : list of integer pair(s)
indices of observation tot_time window(s) along tot_time grid.
Example
-------
an observation with two tot_time windows would look something like...
i_wins = [
[0,80],
[110, 130],
]
band : int
observation ranking band (1, 2, 3, 4)
user_prior : string
observation user priority ('Low', 'Medium', 'High', 'Target of Opportunity')
obs_comp : float
fraction of observation completed
AM : np.array of floats
target airmasses along tot_time grid
HA : np.array of 'astropy.units' hourangles
target hour angles along tot_time grid
AZ : np.array of 'astropy.units' radians
target azimuth angles along tot_time grid
skyiq : np.array of float
sky image quality percentile along tot_time grid
skycc : np.array of float
sky cloud condition percentile along tot_time grid
skywv : np.array of float
sky water vapour percentile along tot_time grid
skybg : array of floats
target sky background percentiles along tot_time grid
latitude : '~astropy.coordinates.angles.Latitude' or '~astropy.unit.Quantity'
observatory latitude
prog_comp : float
Completion fraction of program
winddir : np.array of 'astropy.units' degrees
wind direction along tot_time grid
windvel : np.array of 'astropy.units' kilometers/hour
wind velocity along tot_time grid
wra : np.ndarray of floats
RA tot_time distribution weighting factor
"""
print('\n\t{} weights\n\t-------------------------'.format(obs_id))
aprint = '\t{0:<25s}{1}' # print two strings
f = '{} ({})'
func = 'iq={}, cc={}, bg={}, wv={}'.format(iq, cc, bg, wv)
twcond = weights.total_cond(iq=iq, cc=cc, bg=bg, wv=wv)
print(aprint.format('Total cond: ', f.format(str(twcond), func)))
print(aprint.format('RA: ', wra))
func = 'Band {}'.format(band)
wband = weights.rankingband(band=band)
print(aprint.format('Band: ', f.format(str(wband), func)))
func = '{} priority'.format(user_prior)
wprior = weights.userpriority(user_prior=user_prior)
print(aprint.format('User priority: ', f.format(str(wprior), func)))
func = 'Partially complete: prog={}, obs={}'.format(prog_comp > 0, obs_comp > 0)
wstatus = weights.status(prog_comp=prog_comp, obs_comp=obs_comp)
print(aprint.format('Status: ', f.format(str(wstatus), func)))
# Not yet added
# if 'wbal' in function:
# func = '{} partner balance'.format(bal)
# f = '{} ({})'
# wprior = calc_weight.weight_userpriority(user_prior=user_prior)
# print(aprint.format('wstatus: ', f.format(str(wprior), func)))
wha = weights.hourangle(latitude=latitude, dec=dec, ha=HA)
cmatch = weights.cond_match(iq=iq, cc=cc, bg=bg, wv=wv,
skyiq=skyiq, skycc=skycc, skywv=skywv, skybg=skybg, negha=min(HA) < 0. * u.hourangle,
user_prior=user_prior)
wam = weights.airmass(am=AM, ha=HA, elev=elev_const)
wwins = weights.time_wins(grid_size=len(skyiq), i_wins=i_wins)
wwind = weights.windconditions(dir=winddir, vel=windvel, az=AZ)
hours = _hour_from_midnight(local_time=localtimes)
plt.subplot(221)
plt.plot(hours, wha, color='black', label='wha')
plt.title('Hour angle')
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel('Weight')
plt.subplot(222)
plt.plot(hours, cmatch, color='black', label='cmatch')
plt.title('Conditions constraints')
plt.ylim(-0.1, 1.1)
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel('Weight')
plt.subplot(223)
plt.plot(hours, wam, color='black', label='wam')
plt.title('Air mass')
plt.ylim(-0.1, 1.1)
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel('Weight')
plt.subplot(224)
plt.plot(hours, wwind, color='black', label='wwind')
plt.title('Wind')
plt.ylim(-0.1, 1.1)
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel('Weight')
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35)
plt.tight_layout()
plt.show(block=True)
# input('\n Press enter to close window...')
plt.close()
plt.clf()
plt.subplot(221)
plt.plot(hours, wwins, color='black', label='wwins')
plt.title('Time windows')
plt.ylim(-0.1, 1.1)
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel('Weight')
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35)
plt.tight_layout()
plt.show(block=True)
# input('\n Press enter to close window...')
plt.close()
plt.clf()
# All in one
plt.plot(hours, wha, label='Hour Angle')
plt.plot(hours, cmatch, label='Conditions')
plt.plot(hours, wam, label='Airmass')
plt.plot(hours, wwind, label='Wind')
plt.plot(hours, wwins, label='Timing windows')
plt.xlabel(r'$\Delta t_{mid}$ (hrs)')
plt.ylabel('Weight')
plt.legend()
plt.show(block=True)
# input('\n Press enter to close window...')
plt.close()
plt.clf()
return
def _hour_from_midnight(local_time):
"""
Get array of the tot_time difference from local midnight for each tot_time in array 'localtimes'.
Parameters
----------
local_time : array of strings
local tot_time in iso format (i.e. 'YYYY-MM-DD hh:mm:ss.sss')
Returns
-------
array of floats
"""
hr = hms_to_hr(local_time[0].iso[11:]) * u.h
if hr < 12. * u.h:
midnight = Time(local_time[0].iso[0:10])
else:
midnight = Time(local_time[0].iso[0:10]) + 1. * u.d
return (Time(local_time) - midnight).to(u.h)
def test_hour_from_midnight():
print(' Test _hour_from_midnight()...')
print('\n Case that tot_time array begins before midnight:')
print(' Input times = [\'2018-07-25 19:00:40.529\', \'2018-07-26 06:36:40.529\']')
print(' Difference from midnight =',
_hour_from_midnight(Time(['2018-07-25 19:00:40.529', '2018-07-26 06:36:40.529'])))
print('\n Case that tot_time array begins after midnight:')
print(' Input times = [\'2018-07-26 2:00:40.529\', \'2018-07-26 06:36:40.529\']')
print(' Difference from midnight =',
_hour_from_midnight(Time(['2018-07-26 2:00:40.529', '2018-07-26 06:36:40.529'])))
assert _hour_from_midnight(Time(['2018-07-25 19:00:40.529', '2018-07-26 06:36:40.529'])).value.all() \
== (np.array([-4.98874194, 6.61125806])*u.h).value.all()
assert _hour_from_midnight(Time(['2018-07-26 2:00:40.529', '2018-07-26 06:36:40.529'])).value.all() \
== (np.array([ 2.01125806, 6.61125806])*u.h).value.all()
print(' Test successful!')
if __name__ == '__main__':
test_hour_from_midnight()