""" Split an RFI spectra into 1000 GHz bands.
This is to limit the amount of data points being plotted interactively to
something easy to handle by a web browser and javascript
Walter Max-Moerbeck, 2014-07-20
"""
import scipy
import matplotlib.pyplot as pyplot
import os
import sys
import imp
import shutil
# load script configuration parameters from PWD
# this will ensure that only the configredrfi.py file on current directory is
# loaded
PWD = os.environ['PWD']
try:
configredrfi = imp.load_source('configredrfi',
os.path.join(PWD, 'configredrfi.py'))
from configredrfi import *
except IOError:
error_message = """No configredrfi.py file was found in %s
use 'redrfi --help' for help\n""" % (PWD)
sys.exit(error_message)
# Internal configuration of the website directory structure
simple_data_folder='simple_data'
rfi_data_folder='rfi_data'
#-------------------------------------------------------------------------------
def write_webpage(ant, pol, band_name, date,
webpage_folder='',
simple_data_folder=simple_data_folder,
rfi_data_folder=rfi_data_folder):
""" Write the website for a given antenna and polarization
wmax, Nov 30, 2014
"""
# Create the code for the website
webpage = """
""" % (stokes[0], stokes[0],
stokes[1], stokes[1])
# Save the file
filename = os.path.join(webpage_folder, 'index.html')
fileObj = open(filename, 'w')
fileObj.writelines(webpage)
fileObj.close()
return
#-------------------------------------------------------------------------------
def load_data_set_for_webpage(ant, pol,
plots_output='',
webpage_folder='',
simple_data_folder=simple_data_folder,
rfi_data_folder=rfi_data_folder):
""" Load data set for plotting from 'plots_output', save a copy to the
website in 'webpage_rfi_data_folder' and save simplified data set for
interactive plots by dygraphs in 'webpage_simple_data_folder'.
This might need to be modified if another package is used for the
interactive plots in the future (e.g. highcharts)
wmax, Jan 9, 2015
"""
# Compose the file names
datafile = os.path.join(plots_output,
'tpower-all-env-ifs-%s-%s.csv' % (ant, pol))
datafile_website_copy = os.path.join(webpage_folder, rfi_data_folder,
'tpower-all-env-ifs-%s-%s.csv' % (ant, pol))
simple_datafile = os.path.join(webpage_folder, simple_data_folder,
'tpower-all-env-ifs-%s-%s-simple.csv' % (ant, pol))
# Load all the IFs
freq, ifn, real_min, real_max, real_ave, real_rms = scipy.loadtxt(datafile,
delimiter=',',
unpack=True)
# Copy to the website folder
copy_command = 'cp %s %s' % (datafile, datafile_website_copy)
os.system(copy_command)
# Select data for website plotting
# Sorted list of unique IF numbers
IFs = scipy.unique(ifn)
# Arrays to save simplified data
freq_simp = scipy.array([])
real_ave_simp = scipy.array([])
real_rms_simp = scipy.array([])
# Add data for non-overlaping IFs
for IF in IFs:
index_if = scipy.where(ifn == IF)[0]
freq_if = freq[index_if]
# If freq_simp is empty save
if len(freq_simp) == 0:
freq_simp = scipy.concatenate((freq_simp,
freq_if))
real_ave_simp = scipy.concatenate((real_ave_simp,
real_ave[index_if]))
real_rms_simp = scipy.concatenate((real_rms_simp,
real_rms[index_if]))
# Save if there is no overlap
elif not (min(freq_simp) <= min(freq_if) <= max(freq_simp) or\
min(freq_simp) <= max(freq_if) <= max(freq_simp)):
freq_simp = scipy.concatenate((freq_simp,
freq_if))
real_ave_simp = scipy.concatenate((real_ave_simp,
real_ave[index_if]))
real_rms_simp = scipy.concatenate((real_rms_simp,
real_rms[index_if]))
# Save data for website plotting
scipy.savetxt(simple_datafile,
scipy.transpose((freq_simp, real_ave_simp, real_rms_simp)),
delimiter=',',
fmt='%f')
return freq, ifn, real_min, real_max, real_ave, real_rms
#-------------------------------------------------------------------------------
def static_plot(freq, ifn, real_ave, real_max,
ant, pol,
webpage_folder='',
simple_data_folder=simple_data_folder):
""" Make two static plots for the website. These are meant to show the whole
dataset along with the interactive plot
wmax, jan 9, 2015
"""
# Static plots for website
fig = pyplot.figure(figsize=(15, 5))
ax = fig.add_subplot(111)
ax.autoscale(tight=True)
pyplot.figtext(0.85, 0.85, '%s - %s' % (ant, pol))
# plot each IF separately and join data points
ifs = scipy.unique(ifn)
for if_i in ifs:
if_index = scipy.where(ifn == if_i)[0]
ax.fill_between(freq[if_index], real_ave[if_index], real_max[if_index],
facecolor='0.75',
edgecolor='0.75')
ax.plot(freq[if_index], real_ave[if_index],
'-')
ax.set_xlabel('Frequency [MHz]')
ax.set_ylabel('Amplitude [arbitrary units]')
pyplot.savefig(os.path.join(webpage_folder, simple_data_folder,
'full-spectrum-%s-%s.png' % (ant, pol)))
# change the scale and save as y-zoomed
ax.set_ylim(0, 2)
pyplot.savefig(os.path.join(webpage_folder, simple_data_folder,
'full-spectrum-zoomed-%s-%s.png' % (ant, pol)))
pyplot.close()
return
#-------------------------------------------------------------------------------
# Run
#-------------------------------------------------------------------------------
# Create internal folder structure. It can be configured at the beginning of the
# module
# Create folders if they don't exist
for folder in [simple_data_folder, rfi_data_folder]:
# complete path to folder, relative to current directory
folder_complete = os.path.join(webpage_folder, folder)
if not os.path.isdir(folder_complete):
print 'Creating folder "%s"\n' % (folder_complete)
os.mkdir(folder_complete)
# Generate webpages for each antenna and polarization
for ant in antennas:
for pol in stokes:
try:
# Load data, save a copy to website and a simplified version
# for plotting
freq, ifn, real_min, real_max, real_ave, real_rms =\
load_data_set_for_webpage(ant, pol,
plots_output=plots_output,
webpage_folder=webpage_folder)
# Make static plots for website
static_plot(freq, ifn, real_ave, real_max,
ant, pol,
webpage_folder=webpage_folder)
# Write the webpage for this antenna and polarization
write_webpage(ant, pol, band_name, date,
webpage_folder=webpage_folder)
except IOError, Argument:
print '--------------------------------------------------'
print Argument
print 'No data for antenna %s in polarization %s' % (ant, pol)
print '--------------------------------------------------'
# Make a copy of style files to webpage_folder
shutil.copy(os.path.join(os.environ['REDRFIPATH'],
'dygraph-combined.js'),
os.path.join(webpage_folder,
'dygraph-combined.js'))
shutil.copy(os.path.join(os.environ['REDRFIPATH'],
'mystyle.css'),
os.path.join(webpage_folder,
'mystyle.css'))
# Make an index.html file by simply copying the html for one antenna
webpage_files = os.listdir(webpage_folder)
webpage_files = [filename for filename in webpage_files if\
filename.split('.')[-1] == 'html']
# Go trhough the list in reverse order, last copy will be first antenna in list
for pol in stokes:
for ant in antennas[-1::-1]:
for filename in webpage_files:
if filename.find(ant) >= 0 and filename.find(pol) >= 0:
# If there is only one polarization, write a single index.html
if len(stokes) == 1:
shutil.copy(os.path.join(webpage_folder, filename),
os.path.join(webpage_folder,
'index.html'))
# One index_pol.html for each polarization
# and one general index
else:
shutil.copy(os.path.join(webpage_folder, filename),
os.path.join(webpage_folder,
'index_%s.html' % (pol)))
write_index_multiple_pols(stokes,
webpage_folder=webpage_folder)