-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaskap-hi-validation.py
1447 lines (1182 loc) · 60.8 KB
/
gaskap-hi-validation.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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python -u
# Validation script for GASKAP HI data
#
# Author James Dempsey
# Date 23 Nov 2019
from __future__ import print_function, division
import argparse
import csv
import datetime
import glob
import math
import os
import re
from string import Template
import shutil
import time
import warnings
import matplotlib
matplotlib.use('agg')
import aplpy
from astropy.constants import k_B
from astropy.coordinates import solar_system_ephemeris, EarthLocation, Angle, SkyCoord, AltAz, FK5, get_body
from astropy.io import ascii, fits
from astropy.io.votable import parse, from_table, writeto
from astropy.io.votable.tree import Info
from astropy.table import Table, Column
from astropy.time import Time, TimezoneInfo
import astropy.units as u
from astropy.utils.exceptions import AstropyWarning
from astropy.wcs import WCS
import matplotlib.pyplot as plt
import numpy as np
from radio_beam import Beam
from spectral_cube import SpectralCube
from statsmodels.tsa import stattools
from statsmodels.graphics.tsaplots import plot_pacf
import seaborn as sns
from validation import Bandpass, Diagnostics, SelfCal, Spectra
from validation_reporter import ValidationReport, ReportSection, ReportItem, ValidationMetric, output_html_report, output_metrics_xml
vel_steps = [-324, -280, -234, -189, -143, -100, -60, -15, 30, 73, 119, 165, 200, 236, 273, 311, 357, 399]
#emission_vel_range=[] # (165,200)*u.km/u.s
emission_vel_range=(119,165)*u.km/u.s
non_emission_val_range=(-100,-60)*u.km/u.s
figures_folder = 'figures'
METRIC_BAD = 3
METRIC_UNCERTAIN = 2
METRIC_GOOD = 1
def parseargs():
"""
Parse the command line arguments
:return: An args map with the parsed arguments
"""
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Produce a validation report for GASKAP HI observations. Either a cube or an image (or both) must be supplied to be validated.")
parser.add_argument("-c", "--cube", required=False, help="The HI spectral line cube to be checked.")
parser.add_argument("-i", "--image", required=False, help="The continuum image to be checked.")
parser.add_argument("-s", "--source_cat", required=False, help="The selavy source catalogue used for source identification.")
parser.add_argument("-b", "--beam_list", required=False, help="The csv file describing the positions of each beam (in radians).")
parser.add_argument("-d", "--duration", required=False, help="The duration of the observation in hours.", type=float, default=12.0)
parser.add_argument("-o", "--output", help="The folder in which to save the validation report and associated figures.", default='report')
parser.add_argument("-e", "--emvel", required=False, help="The low velocity bound of the velocity region where emission is expected.")
parser.add_argument("-n", "--nonemvel", required=False,
help="The low velocity bound of the velocity region where emission is not expected.", default='-100')
parser.add_argument("-N", "--noise", required=False, help="Use this fits image of the local rms. Default is to run BANE", default=None)
parser.add_argument("-r", "--redo", help="Rerun all steps, even if intermediate files are present.", default=False,
action='store_true')
parser.add_argument("--num_spectra", required=False, help="Number of sample spectra to create", type=int, default=15)
args = parser.parse_args()
return args
def get_str(value):
if isinstance(value, bytes):
return value.decode()
return value
def plot_histogram(file_prefix, xlabel, title):
data = fits.getdata(file_prefix+'.fits')
flat = data.flatten()
flat = flat[~np.isnan(flat)]
v =plt.hist(flat, bins=200, bottom=1, log=True, histtype='step')
plt.grid()
plt.xlabel(xlabel)
plt.ylabel('Count')
plt.title(title)
plt.savefig(file_prefix+'_hist.png', bbox_inches='tight')
plt.savefig(file_prefix+'_hist_sml.png', dpi=16, bbox_inches='tight')
plt.close()
def plot_map(file_prefix, title, cmap='magma', stretch='linear', pmax=99.75, colorbar_label=None):
fig = plt.figure(figsize=(5, 4.5))
gc = aplpy.FITSFigure(file_prefix+'.fits', figure=fig)
gc.show_colorscale(cmap=cmap, stretch=stretch, pmax=pmax)
gc.add_colorbar()
if colorbar_label:
gc.colorbar.set_axis_label_text(colorbar_label)
gc.add_grid()
gc.set_title(title)
gc.savefig(filename=file_prefix+'.png', dpi=200)
gc.savefig(filename=file_prefix+'.pdf', dpi=100)
gc.savefig(filename=file_prefix+'_sml.png', dpi=16 )
gc.close()
def plot_difference_map(hdu, file_prefix, title, vmin=None, vmax=None):
# Initiate a figure and axis object with WCS projection information
wcs = WCS(hdu.header)
fig = plt.figure(figsize=(18, 12))
ax = fig.add_subplot(111, projection=wcs)
no_nan_data = np.nan_to_num(hdu.data)
if vmin is None and vmax is None:
vmin=np.percentile(no_nan_data, 0.25)
vmax=np.percentile(no_nan_data, 99.75)
im = ax.imshow(hdu.data, cmap='RdBu_r',vmin=vmin,vmax=vmax, origin='lower')
#ax.invert_yaxis()
ax.set_xlabel("Right Ascension (degrees)", fontsize=16)
ax.set_ylabel("Declination (degrees)", fontsize=16)
ax.set_title(title, fontsize=16)
ax.grid(color = 'gray', ls = 'dotted', lw = 2)
cbar = plt.colorbar(im, pad=.07)
plt.savefig(file_prefix+'.png', bbox_inches='tight')
plt.savefig(file_prefix+'_sml.png', dpi=10, bbox_inches='tight')
plt.close()
def output_plot(mp, title, imagename):
mp.write('\n<h2>{}</h2>\n<br/>'.format(title))
mp.write('\n<a href="{}"><img width="800px" src="{}"></a>'.format(imagename, imagename))
mp.write('\n<br/>\n')
def output_map_page(filename, file_prefix, title):
with open(filename, 'w') as mp:
mp.write('<html>\n<head><title>{}</title>\n</head>'.format(title))
mp.write('\n<body>\n<h1>{}</h1>'.format(title))
output_plot(mp, 'Large Scale Emission Map', file_prefix + '_bkg.png')
output_plot(mp, 'Noise Map', file_prefix + '_rms.png')
output_plot(mp, 'Moment 0 Map', file_prefix + '.png')
mp.write('\n</body>\n</html>\n')
def convert_slab_to_jy(slab, header):
my_beam = Beam.from_fits_header(header)
restfreq = 1.420405752E+09*u.Hz
if 'RESTFREQ' in header.keys():
restfreq = header['RESTFREQ']*u.Hz
elif 'RESTFRQ' in header.keys():
restfreq = header['RESTFRQ']*u.Hz
if slab.unmasked_data[0,0,0].unit != u.Jy and slab.unmasked_data[0,0,0].unit != u.Jy/u.beam:
print ("Converting slab from {} to Jy".format(slab.unmasked_data[0,0,0].unit) )
print (slab)
slab.allow_huge_operations=True
slab = slab.to(u.Jy, equivalencies=u.brightness_temperature(my_beam, restfreq))
print (slab)
return slab
def convert_data_to_jy(data, header, verbose=False):
my_beam = Beam.from_fits_header(header)
restfreq = 1.420405752E+09*u.Hz
if 'RESTFREQ' in header.keys():
restfreq = header['RESTFREQ']*u.Hz
elif 'RESTFRQ' in header.keys():
restfreq = header['RESTFRQ']*u.Hz
if data[0].unit != u.Jy:
if verbose:
print ("Converting data from {} to Jy".format(data[0].unit) )
data = data.to(u.Jy, equivalencies=u.brightness_temperature(my_beam, restfreq))
return data
def get_vel_limit(vel_cube):
velocities = np.sort(vel_cube.spectral_axis)
return velocities[0], velocities[-1]
def extract_slab(filename, vel_start, vel_end):
cube = SpectralCube.read(filename)
vel_cube = cube.with_spectral_unit(u.m/u.s, velocity_convention='radio')
cube_vel_min, cube_vel_max = get_vel_limit(vel_cube)
if vel_start > cube_vel_max or vel_end < cube_vel_min:
return None
slab = vel_cube.spectral_slab(vel_start, vel_end)
header = fits.getheader(filename)
slab = convert_slab_to_jy(slab, header)
return slab
def extract_channel_slab(filename, chan_start, chan_end):
cube = SpectralCube.read(filename)
vel_cube = cube.with_spectral_unit(u.m/u.s, velocity_convention='radio')
slab = vel_cube[chan_start:chan_end,:, :].with_spectral_unit(u.km/u.s)
header = fits.getheader(filename)
return slab
def build_fname(example_name, suffix):
basename = os.path.basename(example_name)
prefix = os.path.splitext(basename)[0]
fname = prefix + suffix
return fname
def get_figures_folder(dest_folder):
return dest_folder + '/' + figures_folder + '/'
def get_bane_background(infile, outfile_prefix, plot_title_suffix, ncores=8, redo=False, plot=True):
background_prefix = outfile_prefix+'_bkg'
background_file = background_prefix + '.fits'
if redo or not os.path.exists(background_file):
cmd = "BANE --cores={0} --out={1} {2}".format(ncores, outfile_prefix, infile)
print (cmd)
os.system(cmd)
if plot:
plot_map(background_prefix, "Large scale emission in " + plot_title_suffix)
plot_histogram(background_prefix, 'Emission (Jy beam^{-1} km s^{-1})', "Emission for " + plot_title_suffix)
plot_map(outfile_prefix+'_rms', "Noise in "+ plot_title_suffix)
return background_file
def assess_metric(metric, threshold1, threshold2, low_good=False):
if metric < threshold1:
return METRIC_GOOD if low_good else METRIC_BAD
elif metric < threshold2:
return METRIC_UNCERTAIN
else:
return METRIC_BAD if low_good else METRIC_GOOD
def get_spectral_units(ctype, cunit, hdr):
spectral_conversion = 1
if not cunit in hdr:
if ctype.startswith('VEL') or ctype.startswith('VRAD'):
spectral_unit = 'm/s'
else:
spectral_unit = 'Hz'
else:
spectral_unit = hdr[cunit]
if spectral_unit == 'Hz':
spectral_conversion = 1e6
spectral_unit = 'MHz'
elif spectral_unit == 'kHz':
spectral_conversion = 1e3
spectral_unit = 'MHz'
elif spectral_unit == 'm/s':
spectral_conversion = 1e3
spectral_unit = 'km/s'
return spectral_unit, spectral_conversion
def calc_velocity_res(hdr):
spec_sys = hdr['SPECSYS']
axis = '3' if hdr['CTYPE3'] != 'STOKES' else '4'
spec_type = hdr['CTYPE'+axis]
spectral_unit, spectral_conversion = get_spectral_units(spec_type, 'CUNIT'+axis, hdr)
if 'CUNIT'+axis in hdr.keys():
spec_unit = hdr['CUNIT'+axis]
#elif spec_type == 'VRAD' or spec_type == 'VEL':
# spec_unit = 'm/s'
else:
spec_unit = None
spec_delt = hdr['CDELT'+axis]
print ('CDELT={}, CUNIT={}, spec_unit={}, conversion={}'.format(spec_delt, spec_unit, spectral_unit, spectral_conversion))
spec_res_km_s = np.abs(spec_delt) / spectral_conversion
if spectral_unit == 'MHz':
spec_res_km_s = spec_res_km_s/5e-4*0.1 # 0.5 kHz = 0.1 km/s
#elif spec_unit == 'Hz':
# spec_res_km_s = spec_res_km_s/500*0.1 # 0.5 kHz = 0.1 km/s
#elif spec_unit == 'kHz':
# spec_res_km_s = spec_res_km_s/0.5*0.1 # 0.5 kHz = 0.1 km/s
return spec_res_km_s
def get_observing_location():
# Coordinates for Inyarrimanha Ilgari Bundara, the CSIRO Murchison Radio-astronomy Observatory
latitude = Angle("-26:41:46.0", unit=u.deg)
longitude = Angle("116:38:13.0", unit=u.deg)
return EarthLocation(lat=latitude, lon=longitude)
def plot_solar_elevation(fig_folder, obs_time_utc, duration, field_pos, sbid):
obs_end_utc = obs_time_utc + duration
tmjd = np.arange(obs_time_utc.mjd, obs_end_utc.mjd, 1./24.0/60.0/6.0)
times = Time(tmjd, format='mjd', scale='utc')
observing_location = get_observing_location()
solar_system_ephemeris.set('builtin')
sun_sc = get_body("sun", times, location=observing_location)
#sun_sc_median = get_body("sun", Time(np.median(tmjd), format='mjd', scale='utc'), location=observing_location)
sun_altaz = sun_sc.transform_to(AltAz(obstime=times, location=observing_location))
sun_altdeg = sun_altaz.alt.deg
field_sun_sep_deg = sun_sc.separation(field_pos).degree
sns.set()
fig = plt.figure()
ax1 = fig.add_subplot(111)
plot, = ax1.plot((tmjd-tmjd[0])*24.0, sun_altdeg, marker='None', color="black")
#print("Solar position = %s" %(sun_sc_median.to_string(style='hmsdms')))
ax1.set_title("Solar elevation for SBID %s" %(sbid))
ax1.set_xlabel("Time since start of observation (h)")
ax1.set_ylabel("Elevation (deg)")
ax1.axhline(0, color='g')
ax1.axhline(-6, color='blue')
ax1.axhline(-12, color='darkblue')
ax1.axhline(-18, color='indigo')
fig_path = fig_folder + 'solar_el.png'
fig.savefig(fig_path)
plt.close()
# Calculate the daylight percent of the observation
sun_up_filter = sun_altdeg >= 0
steps_sun_up = np.sum(sun_up_filter)
pct_daylight = steps_sun_up/len(sun_altdeg)*100
return fig_path, pct_daylight, field_sun_sep_deg
def plot_target_elevation(fig_folder, obs_time_utc, duration, field_pos, sbid):
obs_end_utc = obs_time_utc + duration
tmjd = np.arange(obs_time_utc.mjd, obs_end_utc.mjd, 1./24.0/60.0/6.0)
times = Time(tmjd, format='mjd', scale='utc')
observing_location = get_observing_location()
field_altaz = field_pos.transform_to(AltAz(obstime=times, location=observing_location))
field_altdeg = field_altaz.alt.deg
fig = plt.figure()
ax1 = fig.add_subplot(111)
plot, = ax1.plot((tmjd-tmjd[0])*24.0, field_altdeg, marker='None', color="black")
ax1.set_title("Target elevation for SBID %s" %(sbid))
ax1.set_xlabel("Time since start of observation (h)")
ax1.set_ylabel("Elevation (deg)")
ax1.axhline(15, color='g')
fig_path = fig_folder + 'target_el.png'
fig.savefig(fig_path)
plt.close()
return fig_path
def report_observation(image, dest_folder, reporter, input_duration, sched_info, obs_metadata):
print('\nReporting observation based on ' + image)
hdr = fits.getheader(image)
w = WCS(hdr).celestial
sbid = hdr['SBID'] if 'SBID' in hdr else sched_info.sbid
project = hdr['PROJECT'] if 'PROJECT' in hdr else ''
proj_link = None
if project.startswith('AS'):
proj_link = "https://confluence.csiro.au/display/askapsst/{0}+Data".format(project)
obs_date = hdr['DATE-OBS'] if 'DATE-OBS' in hdr else 'Unknown'
duration = float(hdr['DURATION'])/3600 if 'DURATION' in hdr else input_duration
naxis1 = int(hdr['NAXIS1'])
naxis2 = int(hdr['NAXIS2'])
pixcrd = np.array([[naxis1/2, naxis2/2]])
centre = w.all_pix2world(pixcrd,1)
centre_coord = SkyCoord(ra=centre[0][0], dec=centre[0][1], unit="deg,deg")
centre = centre_coord.to_string(style='hmsdms',sep=':', precision=1)
# spectral axis
spectral_unit = 'None'
spectral_range = ''
spec_title = 'Spectral Range'
for i in range(3,int(hdr['NAXIS'])+1):
ctype = hdr['CTYPE'+str(i)]
if (ctype.startswith('VEL') or ctype.startswith('VRAD') or ctype.startswith('FREQ')):
key = 'CUNIT'+str(i)
spectral_unit, spectral_conversion = get_spectral_units(ctype, key, hdr)
step = float(hdr['CDELT'+str(i)])
#print ('step {} rval {} rpix {} naxis {}'.format(step, hdr['CRVAL'+str(i)], hdr['CRPIX'+str(i)], hdr['NAXIS'+str(i)]))
spec_start = (float(hdr['CRVAL'+str(i)]) - (step*(float(hdr['CRPIX'+str(i)])-1)))/spectral_conversion
if int(hdr['NAXIS'+str(i)]) > 1:
spec_end = spec_start + (step * (int(hdr['NAXIS'+str(i)]-1)))/spectral_conversion
if step > 0:
spectral_range = '{:0.3f} - {:0.3f}'.format(spec_start, spec_end)
else:
spectral_range = '{:0.3f} - {:0.3f}'.format(spec_end, spec_start)
spec_title = 'Spectral Range'
else:
centre_freq = (float(hdr['CRVAL'+str(i)]) - (step*(float(hdr['CRPIX'+str(i)])-1)))/spectral_conversion
spectral_range = '{:0.3f}'.format(centre_freq)
spec_title = 'Centre Freq'
# Field info
if obs_metadata:
field_names = ''
field_centres = ''
for i,field in enumerate(obs_metadata.fields):
if i > 0:
field_names += '<br/>'
field_centres += '<br/>'
field_names += field.name
field_centres += field.ra + ' ' + field.dec
else:
field_names = sched_info.field_name
field_centres = centre
footprint = sched_info.footprint
if footprint and sched_info.pitch:
footprint = "{}_{}".format(footprint, sched_info.pitch)
section = ReportSection('Observation')
section.add_item('SBID', value=sbid)
section.add_item('Project', value=project, link=proj_link)
section.add_item('Date (UTC)', value=obs_date)
section.add_item('Duration<br/>(hours)', value='{:.2f}'.format(duration))
section.add_item('Field(s)', value=field_names)
section.add_item('Field Centre(s)', value=field_centres)
section.add_item('Correlator<br/>Mode', value=sched_info.corr_mode)
section.add_item('Footprint', value=footprint)
section.add_item('{}<br/>({})'.format(spec_title, spectral_unit), value=spectral_range)
if obs_date != 'Unknown':
murchison_zone = TimezoneInfo(utc_offset=8*u.hour)
obs_time_utc = Time(obs_date, format='isot', scale='utc')
local_start = obs_time_utc.to_datetime(timezone=murchison_zone)
local_end = (obs_time_utc + duration*u.hour).to_datetime(timezone=murchison_zone)
local_time = '{:%Y-%m-%d %H:%M:%S%z}<br/>{:%Y-%m-%d %H:%M:%S%z}'.format(local_start, local_end)
fig_folder= get_figures_folder(dest_folder)
field_pos = SkyCoord(obs_metadata.fields[0].ra, obs_metadata.fields[0].dec, frame=FK5, unit=(u.hourangle, u.deg)) if obs_metadata else centre_coord
solr_el_path, pct_daylight, field_sun_sep_deg = plot_solar_elevation(fig_folder, obs_time_utc, duration*u.hour, field_pos, sbid)
tgt_el_path = plot_target_elevation(fig_folder, obs_time_utc, duration*u.hour, field_pos, sbid)
section.start_new_row()
section.add_item('Local Time', value=local_time)
#section.add_item('Solar Elevation', link=solr_el_path_rel, image=solr_el_path_rel) # Second should be a thumbnail
add_opt_image_section('Solar Elevation', solr_el_path, fig_folder, dest_folder, section)
section.add_item('Daytime Proportion (%)', value="{:.1f}".format(pct_daylight))
section.add_item('Solar Separation (deg)', value="{:.3f} to {:.3f}".format(field_sun_sep_deg[0], field_sun_sep_deg[1]))
add_opt_image_section('Target Elevation', tgt_el_path, fig_folder, dest_folder, section)
reporter.add_section(section)
reporter.project = project
return sbid
def report_cube_stats(cube, reporter):
print ('\nReporting cube stats')
hdr = fits.getheader(cube)
w = WCS(hdr).celestial
# Cube information
askapSoftVer = 'N/A'
askapPipelineVer = 'N/A'
history = hdr['history'] if 'history' in hdr else []
askapSoftVerPrefix = 'Produced with ASKAPsoft version '
askapPipelinePrefix = 'Processed with ASKAP pipeline version '
for row in history:
if row.startswith(askapSoftVerPrefix):
askapSoftVer = row[len(askapSoftVerPrefix):]
elif row.startswith(askapPipelinePrefix):
askapPipelineVer = row[len(askapPipelinePrefix):]
beam = 'N/A'
if 'BMAJ' in hdr:
beam_maj = hdr['BMAJ'] * 60 * 60
beam_min = hdr['BMIN'] * 60 * 60
beam = '{:.1f} x {:.1f}'.format(beam_maj, beam_min)
units = 'N/A'
if 'BUNIT' in hdr:
units = hdr['BUNIT']
dims = []
for i in range(1,int(hdr['NAXIS'])+1):
dims.append(str(hdr['NAXIS'+str(i)]))
dimensions = ' x '.join(dims)
# self.area,self.solid_ang = get_pixel_area(fits, nans=True, ra_axis=self.ra_axis, dec_axis=self.dec_axis, w=w)
cube_name = os.path.basename(cube)
section = ReportSection('Image Cube', cube_name)
section.add_item('ASKAPsoft<br/>version', value=askapSoftVer)
section.add_item('Pipeline<br/>version', value=askapPipelineVer)
section.add_item('Synthesised Beam<br/>(arcsec)', value=beam)
section.add_item('Sky Area<br/>(deg2)', value='')
section.add_item('Dimensions', value=dimensions)
section.add_item('Units', value=units)
reporter.add_section(section)
return
def check_for_emission(cube, vel_start, vel_end, reporter, dest_folder, ncores=8, redo=False):
print ('\nChecking for presence of emission in {:.0f} < v < {:.0f}'.format(vel_start, vel_end))
# Extract a moment 0 map
slab = extract_slab(cube, vel_start, vel_end)
if slab is None:
print ("** No data for the emission range - skipping check **")
return
num_channels = slab.shape[0]
hdr = fits.getheader(cube)
spec_res_km_s = calc_velocity_res(hdr)
mom0 = slab.moment0()
prefix = build_fname(cube, '_mom0')
folder = get_figures_folder(dest_folder)
mom0_fname = folder + prefix + '.fits'
mom0.write(mom0_fname, overwrite=True)
hi_data = fits.open(mom0_fname)
plot_title_suffix = "emission region in " + os.path.basename(cube)
plot_difference_map(hi_data[0], folder+prefix, "Moment 0 map of " + plot_title_suffix)
# Produce the background plots
bkg_data = get_bane_background(mom0_fname, folder+prefix, plot_title_suffix, ncores=ncores, redo=redo)
map_page = folder + '/emission.html'
rel_map_page = get_figures_folder('.') + '/emission.html'
output_map_page(map_page, prefix, 'Emission Plots for ' + os.path.basename(cube))
hi_data = fits.open(folder + prefix+'_bkg.fits')
max_em = np.nanmax(hi_data[0].data)
max_em_per_kms = max_em / (spec_res_km_s * num_channels)
# assess
cube_name = os.path.basename(cube)
section = ReportSection('Presence of Emission', cube_name)
section.add_item('Velocity Range<br/>(km/s LSR)', value='{:.0f} to {:.0f}'.format(vel_start.value, vel_end.value))
section.add_item('Channels', value='{}'.format(num_channels))
section.add_item('Large Scale<br/>Emission Map', link=rel_map_page, image='figures/'+prefix+'_bkg_sml.png')
section.add_item('Emission Histogram', link='figures/'+prefix+'_bkg_hist.png', image='figures/'+prefix+'_bkg_hist_sml.png')
section.add_item('Max Emission<br/>(Jy beam<sup>-1</sup>)', value='{:.3f}'.format(max_em_per_kms))
reporter.add_section(section)
metric = ValidationMetric('Presence of Emission',
'Maximum large scale emission intensity in the velocity range where emission is expected.',
int(max_em_per_kms), assess_metric(max_em_per_kms, 12, 20))
reporter.add_metric(metric)
return
def check_for_non_emission(cube, vel_start, vel_end, reporter, dest_folder, ncores=8, redo=False):
print ('\nChecking for absence of emission in {:.0f} < v < {:.0f}'.format(vel_start, vel_end))
# Extract a moment 0 map
slab = extract_slab(cube, vel_start, vel_end)
if slab is None:
print ("** No data for the non-emission range - skipping check **")
return None
num_channels = slab.shape[0]
hdr = fits.getheader(cube)
spec_res_km_s = calc_velocity_res(hdr)
mom0 = slab.moment0()
prefix = build_fname(cube, '_mom0_off')
folder = get_figures_folder(dest_folder)
mom0_fname = folder + prefix + '.fits'
mom0.write(mom0_fname, overwrite=True)
hi_data = fits.open(mom0_fname)
plot_title_suffix = "non-emission region in " + os.path.basename(cube)
plot_difference_map(hi_data[0], folder+prefix, "Moment 0 map of " + plot_title_suffix)
# Produce the background plots
bkg_data = get_bane_background(mom0_fname, folder+prefix, plot_title_suffix, ncores=ncores, redo=redo)
map_page = folder + '/off_emission.html'
rel_map_page = get_figures_folder('.') + '/off_emission.html'
output_map_page(map_page, prefix, 'Off-line Emission Plots for ' + os.path.basename(cube))
hi_data = fits.open(folder+prefix+'_bkg.fits')
max_em = np.nanmax(hi_data[0].data)
max_em_per_kms = max_em / (spec_res_km_s * num_channels)
# assess
cube_name = os.path.basename(cube)
section = ReportSection('Absence of Off-line Emission', cube_name)
section.add_item('Velocity Range<br/>(km/s LSR)', value='{:.0f} to {:.0f}'.format(vel_start.value, vel_end.value))
section.add_item('Channels', value='{}'.format(num_channels))
section.add_item('Large Scale<br/>Emission Map', link=rel_map_page, image='figures/'+prefix+'_bkg_sml.png')
section.add_item('Emission Histogram', link='figures/'+prefix+'_bkg_hist.png', image='figures/'+prefix+'_bkg_hist_sml.png')
section.add_item('Max Emission<br/>(Jy beam<sup>-1</sup>)', value='{:.3f}'.format(max_em_per_kms))
reporter.add_section(section)
metric = ValidationMetric('Absence of Off-line Emission',
'Maximum large scale emission intensity in the velocity range where emission is not expected.',
int(max_em_per_kms), assess_metric(max_em_per_kms, 5, 12, low_good=True))
reporter.add_metric(metric)
return slab
def calc_theoretical_rms(chan_width, t_obs= 12*60*60, n_ant=36):
"""
Calculating the theoretical rms noise for ASKAP. Assuming natural weighting and not taking into account fraction of flagged data.
Based on ASKAP SEFD measurement in SB 9944.
Parameters
----------
chan_width : int
channel width in Hz
t_obs : int
duration of the observation in seconds
n_ant : int
Number of antennae
Returns
-------
rms : int
Theoretical RMS in mJy
"""
#cor_eff = 0.8 # correlator efficiency - WALLABY
cor_eff = 1.0 # correlator efficiency - assumed to be included in the SEFD
n_pol = 2.0 # Number of polarisation, npol = 2 for images in Stokes I, Q, U, or V
#sefd = 1700*u.Jy # As measured in SB 9944
sefd = 1800*u.Jy # Hotan et al 2021
rms_jy = sefd/(cor_eff*math.sqrt(n_pol*n_ant*(n_ant-1)*chan_width*t_obs))
return rms_jy.to(u.mJy).value
def measure_spectral_line_noise(slab, cube, vel_start, vel_end, reporter, dest_folder, duration, redo=False):
print ('\nMeasuring the spectral line noise levels across {:.0f} < v < {:.0f}'.format(vel_start, vel_end))
if slab is None:
print ("** No data for the non-emission range - skipping check **")
return
# Extract header details
hdr = fits.getheader(cube)
spec_sys = hdr['SPECSYS']
axis_num = '3' if hdr['CTYPE3'] != 'STOKES' else '4'
spec_type = hdr['CTYPE'+axis_num]
axis = spec_sys + ' ' + spec_type
spec_res_km_s = calc_velocity_res(hdr)
# Scale the noise to mJy / 5 kHz channel
std_data = np.nanstd(slab.unmasked_data[:], axis=0)
noise_5kHz = std_data / np.sqrt(1 / spec_res_km_s)
noise_5kHz = noise_5kHz*1000 # Jy => mJy
# Extract the spectral line noise map
mom0_prefix = build_fname(cube, '_mom0_off')
folder = get_figures_folder(dest_folder)
mom0_fname = folder + mom0_prefix + '.fits'
prefix = build_fname(cube, '_spectral_noise')
noise_fname = folder + prefix + '.fits'
fits.writeto(noise_fname, noise_5kHz.value, fits.getheader(mom0_fname), overwrite=True)
# Produce the noise plots
cube_name = os.path.basename(cube)
plot_map(folder+prefix, "Spectral axis noise map for " + cube_name, cmap='mako', stretch='arcsinh',
colorbar_label=r'Noise level per 5 kHz channel (mJy beam$^{-1}$)')
plot_histogram(folder+prefix, r'Noise level per 5 kHz channel (mJy beam$^{-1}$)', 'Spectral axis noise for ' + cube_name)
median_noise_5kHz = np.nanmedian(noise_5kHz.value[noise_5kHz.value!=0.0])
theoretical_gaskap_noise = calc_theoretical_rms(5000, t_obs=duration*60*60) # mJy per 5 kHz for the observation duration
print ("Theoretical noise {:.3f} mJy/beam".format(theoretical_gaskap_noise))
median_ratio = median_noise_5kHz / theoretical_gaskap_noise
# assess
cube_name = os.path.basename(cube)
section = ReportSection('Spectral Line Noise', cube_name)
section.add_item('Velocity Range<br/>(km/s LSR)', value='{:.0f} to {:.0f}'.format(vel_start.value, vel_end.value))
section.add_item('Spectral Axis', value=axis)
section.add_item('Spectral Resolution<br/>(kms)', value='{}'.format(round(spec_res_km_s,2)))
section.add_item('Spectral Axis<br/>Noise Map', link='figures/'+prefix+'.png', image='figures/'+prefix+'_sml.png')
section.add_item('Spectral Axis<br/>Noise Histogram', link='figures/'+prefix+'_hist.png', image='figures/'+prefix+'_hist_sml.png')
section.add_item('Spectral Axis Noise<br/>(mJy per 5 kHz)', value='{:.3f}'.format(median_noise_5kHz))
section.add_item('Spectral Axis Noise<br/>(vs theoretical for {:.2f} hr)'.format(duration), value='{:.3f}'.format(median_ratio))
reporter.add_section(section)
metric = ValidationMetric('Spectral Noise',
'1-sigma spectral noise comparison to theoretical per 5 kHz channel for {:.2f} hr observation.'.format(duration),
round(median_ratio,3), assess_metric(median_ratio,
np.sqrt(2), np.sqrt(2)*2, low_good=True))
reporter.add_metric(metric)
return
def get_pixel_area(fits_file,flux=0,nans=False,ra_axis=0,dec_axis=1,w=None):
"""For a given image, get the area and solid angle of all non-nan pixels or all pixels below a certain flux (doesn't count pixels=0).
The RA and DEC axes follow the WCS convention (i.e. starting from 0).
Arguments:
----------
fits : astropy.io.fits
The primary axis of a fits image.
Keyword arguments:
------------------
flux : float
The flux in Jy, below which pixels will be selected.
nans : bool
Derive the area and solid angle of all non-nan pixels.
ra_axis : int
The index of the RA axis (starting from 0).
dec_axis : int
The index of the DEC axis (starting from 0).
w : astropy.wcs.WCS
A wcs object to use for reading the pixel sizes.
Returns:
--------
area : float
The area in square degrees.
solid_ang : float
The solid angle in steradians.
See Also:
---------
astropy.io.fits
astropy.wcs.WCS"""
if w is None:
w = WCS(fits_file.header)
#count the pixels and derive area and solid angle of all these pixels
if nans:
count = fits_file.data[(~np.isnan(fits_file.data)) & (fits_file.data != 0)].shape[0]
else:
count = fits_file.data[(fits_file.data < flux) & (fits_file.data != 0)].shape[0]
area = (count*np.abs(w.wcs.cdelt[ra_axis])*np.abs(w.wcs.cdelt[dec_axis]))
solid_ang = area*(np.pi/180)**2
return area,solid_ang
def report_image_stats(image, noise_file, reporter, dest_folder, diagnostics_dir, ncores=8, redo=False):
print ('\nReporting image stats')
fits_file = fits.open(image)
hdr = fits_file[0].header
w = WCS(hdr).celestial
fig_folder= get_figures_folder(dest_folder)
# Image information
askapSoftVer = 'N/A'
askapPipelineVer = 'N/A'
history = hdr['history']
askapSoftVerPrefix = 'Produced with ASKAPsoft version '
askapPipelinePrefix = 'Processed with ASKAP pipeline version '
for row in history:
if row.startswith(askapSoftVerPrefix):
askapSoftVer = row[len(askapSoftVerPrefix):]
elif row.startswith(askapPipelinePrefix):
askapPipelineVer = row[len(askapPipelinePrefix):]
beam = 'N/A'
if 'BMAJ' in hdr:
beam_maj = hdr['BMAJ'] * 60 * 60
beam_min = hdr['BMIN'] * 60 * 60
beam = '{:.1f} x {:.1f}'.format(beam_maj, beam_min)
# Analyse image data
area,solid_ang = get_pixel_area(fits_file[0], nans=False)
# if not noise_file:
# prefix = build_fname(image, '')
# folder = get_figures_folder(dest_folder)
# noise_file = get_bane_background(image, folder+prefix, redo=redo, plot=False)
# rms_map = fits.open(noise_file)[0]
img_data = fits_file[0].data
img_peak = np.max(img_data[~np.isnan(img_data)])
# rms_bounds = rms_map.data > 0
# img_rms = int(np.median(rms_map.data[rms_bounds])*1e6) #uJy
# img_peak_bounds = np.max(img_data[rms_bounds])
# img_peak_pos = np.where(img_data == img_peak_bounds)
# img_peak_rms = rms_map.data[img_peak_pos][0]
# dynamic_range = img_peak_bounds/img_peak_rms
#img_flux = np.sum(img_data[~np.isnan(img_data)]) / (1.133*((beam_maj * beam_min) / (img.raPS * img.decPS))) #divide by beam area
# Copy pipleine plots
field_src_plot = copy_existing_image(diagnostics_dir+'/image.i.SB*.cont.restored_sources.png', fig_folder) if diagnostics_dir else None
image_name = os.path.basename(image)
section = ReportSection('Image', image_name)
section.add_item('ASKAPsoft<br/>version', value=askapSoftVer)
section.add_item('Pipeline<br/>version', value=askapPipelineVer)
section.add_item('Synthesised Beam<br/>(arcsec)', value=beam)
add_opt_image_section('Source Map', field_src_plot, fig_folder, dest_folder, section)
# section.add_item('Median r.m.s.<br/>(uJy)', value='{:.2f}'.format(img_rms))
# section.add_item('Image peak<br/>(Jy)', value='{:.2f}'.format(img_peak_bounds))
# section.add_item('Dynamic Range', value='{:.2f}'.format(dynamic_range))
section.add_item('Sky Area<br/>(deg2)', value='{:.2f}'.format(area))
reporter.add_section(section)
return
def set_velocity_range(emvelstr, nonemvelstr):
emvel = int(emvelstr)
if not emvel in vel_steps:
raise ValueError('Velocity {} is not one of the supported GASS velocity steps e.g. 165, 200.'.format(emvel))
nonemvel = int(nonemvelstr)
if not nonemvel in vel_steps:
raise ValueError('Velocity {} is not one of the supported GASS velocity steps e.g. 165, 200.'.format(nonemvel))
idx = vel_steps.index(emvel)
if idx +1 >= len(vel_steps):
raise ValueError('Velocity {} is not one of the supported GASS velocity steps e.g. 165, 200.'.format(emvel))
# emission_vel_range=(vel_steps[idx],vel_steps[idx+1])*u.km/u.s
emission_vel_range[0]=vel_steps[idx]*u.km/u.s
emission_vel_range[1]=vel_steps[idx+1]*u.km/u.s
print ('\nSet emission velocity range to {:.0f} < v < {:.0f}'.format(emission_vel_range[0], emission_vel_range[1]))
idx = vel_steps.index(nonemvel)
if idx +1 >= len(vel_steps):
raise ValueError('Velocity {} is not one of the supported GASS velocity steps e.g. 165, 200.'.format(emvel))
# emission_vel_range=(vel_steps[idx],vel_steps[idx+1])*u.km/u.s
non_emission_val_range[0]=vel_steps[idx]*u.km/u.s
non_emission_val_range[1]=vel_steps[idx+1]*u.km/u.s
print ('\nSet non emission velocity range to {:.0f} < v < {:.0f}'.format(non_emission_val_range[0], non_emission_val_range[1]))
def identify_periodicity(spectrum):
"""
Check if there are periodic features in a spectrum. This tests if there are patterns which are
present in the spectrum seperated by a specific number of channels (or lag). i.e. if the same
pattern repeats every so many channels. Only features with at least 3-sigma significance are
returned.
Arguments:
----------
spectrum : array-like
The numerical spectrum.
Returns:
--------
repeats: array
The lag intervals that have 3-sigma or greater periodic features
sigma: array
The significance of each repeat value, in sigma.
"""
# Use a partial auto-correlation function to identify repeated patterns
pacf = stattools.pacf(spectrum, nlags=min(50, len(spectrum)//5))
sd = np.std(pacf[1:])
significance= pacf/sd
indexes = (significance>3).nonzero()[0]
repeats = indexes[indexes>3]
return repeats, significance[repeats]
def plot_all_spectra(spectra, names, velocities, em_unit, vel_unit, figures_folder, prefix):
fig = None
if len(spectra) > 20:
fig = plt.figure(figsize=(18, 72))
else:
fig = plt.figure(figsize=(18, 12))
num_rows = math.ceil(len(spectra)/3)
for idx, spectrum in enumerate(spectra):
label = get_str(names[idx])
ax = fig.add_subplot(num_rows, 3, idx+1)
ax.plot(velocities, spectrum, linewidth=1)
ax.set_title(label)
ax.grid()
if idx > 2*num_rows:
ax.set_xlabel("$v_{LSRK}$ " + '({})'.format(vel_unit))
if idx % 3 == 0:
ax.set_ylabel(em_unit)
fig.tight_layout()
fig.savefig(figures_folder+'/'+prefix+'-spectra-individual.pdf')
def plot_overlaid_spectra(spectra, names, velocities, em_unit, vel_unit, figures_folder, cube_name, prefix):
fig = plt.figure(figsize=(18, 12))
axes = []
if len(spectra) > 36:
for i in range(1,4):
ax = fig.add_subplot(3,1,i)
axes.append(ax)
else:
ax = fig.add_subplot()
axes.append(ax)
for i, spec in enumerate(spectra):
label = get_str(names[i])
idx = 0
if len(axes) > 1:
interleave = label[-4]
idx = ord(interleave) - ord('A')
ax = axes[idx]
ax.plot(velocities, spec, label=label)
for idx, ax in enumerate(axes):
ax.set_xlabel("$v_{LSRK}$ " + '({})'.format(vel_unit))
ax.set_ylabel(em_unit)
ax.legend()
ax.grid()
if len(axes) > 1:
ax.set_title('Spectra for all beams in interleave {}'.format(chr(ord('A')+idx)))
else:
ax.set_title('Spectra for {} brightest sources in {}'.format(len(spectra), cube_name))
plt.savefig(figures_folder+'/'+prefix+'-spectra.png')
plt.savefig(figures_folder+'/'+prefix+'-spectra_sml.png', dpi=16)
def output_spectra_page(filename, prefix, title):
with open(filename, 'w') as mp:
mp.write('<html>\n<head><title>{}</title>\n</head>'.format(title))
mp.write('\n<body>\n<h1>{}</h1>'.format(title))
output_plot(mp, 'All Spectra', prefix + '-spectra.png')
output_plot(mp, 'Individual Spectra', prefix + '-spectra-individual.pdf')
mp.write('\n</body>\n</html>\n')
def plot_periodic_spectrum(spectrum, fig, name):
ax = fig.add_subplot(211)
ax.plot(spectrum)
ax.set_title('Spectrum for ' + name)
ax.grid()
ax = fig.add_subplot(212)
plot_pacf(spectrum, lags=50, ax=ax)
fig.tight_layout()
def output_periodic_spectra_page(filename, prefix, title, periodic, detections):
with open(filename, 'w') as mp:
mp.write('<html>\n<head><title>{}</title>\n</head>'.format(title))
mp.write('\n<body>\n<h1>{}</h1>'.format(title))
for idx, src_name in enumerate(periodic):
output_plot(mp, src_name, prefix + '{}_periodicity.png'.format(src_name))
mp.write('<p>{}</p>'.format(detections[idx]))
mp.write('\n</body>\n</html>\n')
def save_spectum(name, velocities, fluxes, ra, dec, spectra_folder):
spec_table = Table(
[velocities, fluxes],
names=['Velocity', 'Emission'],
meta={'ID': name, 'RA' : ra, 'Dec': dec})
votable = from_table(spec_table)
votable.infos.append(Info('RA', 'RA', ra))
votable.infos.append(Info('Dec', 'Dec', dec))
writeto(votable, '{}/{}.vot'.format(spectra_folder, name))
def extract_spectra(cube, source_cat, dest_folder, reporter, num_spectra, beam_list, slab_size=40):
print('\nExtracting spectra for the {} brightest sources in {} and beams listed in {}'.format(
num_spectra, source_cat, beam_list))
# Prepare the output folders
spectra_folder = dest_folder + '/spectra'
if not os.path.exists(spectra_folder):
os.makedirs(spectra_folder)
figures_folder = dest_folder + '/figures'
# Read the source list and identify the brightest sources
bright_srcs = []
bright_src_pos = []
if source_cat:
votable = parse(source_cat, pedantic=False)
sources = votable.get_first_table()
srcs_tab = sources.to_table()
for key in ('component_name', 'col_component_name'):
if key in srcs_tab.keys():