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
|
from __future__ import with_statement
import sys
try:
import numpy
import os
import re
import reflex
from pipeline_product import PipelineProduct
import pipeline_display
import reflex_plot_widgets
import matplotlib.gridspec as gridspec
from matplotlib.text import Text
from pylab import *
import_success = True
except ImportError:
import_success = False
print "Error importing modules pyfits, wx, matplotlib, numpy"
def paragraph(text, width=None):
""" wrap text string into paragraph
text: text to format, removes leading space and newlines
width: if not None, wraps text, not recommended for tooltips as
they are wrapped by wxWidgets by default
"""
import textwrap
if width is None:
return textwrap.dedent(text).replace('\n', ' ').strip()
else:
return textwrap.fill(textwrap.dedent(text), width=width)
class DataPlotterManager(object):
# static members
recipe_name = "kmo_sci_red"
reconstructed_cat = "SCI_RECONSTRUCTED"
oh_spec_cat = "OH_SPEC"
IFU_stat_color = {'Active':'summer', 'Locked':'spring', 'NotInPAF':'winter', 'NotInPAF & Locked':'autumn', 'Coll.':'cool', 'Empty':'copper'}
def setWindowTitle(self):
return self.recipe_name+"_interactive"
def setInteractiveParameters(self):
return [
reflex.RecipeParameter(recipe=self.recipe_name, displayName="imethod",
group="Recons.", description="Interpolation Method (NN, lwNN, swNN, MS, CS)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="xcal_interpolation",
group="Recons.", description="Interpolate xcal between rotator angles"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="b_samples",
group="Recons.", description="The number of samples in wavelength"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="b_start",
group="Recons.", description="The lowest wavelength"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="b_end",
group="Recons.", description="The highest wavelength"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="fmethod",
group="Extr.", description="Fitting Method (gauss, moffat)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="neighborhoodRange",
group="Extr.", description="Range for Neighbors"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="flux",
group="Extr.", description="Apply flux conservation"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="cmethod",
group="Comb.", description="Combination Method (average, median, sum, min_max, ksigma)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="cpos_rej",
group="Comb.", description="The positive rejection threshold"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="cneg_rej",
group="Comb.", description="The negative rejection threshold"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="citer",
group="Comb.", description="The number of iterations"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="cmax",
group="Comb.", description="The number of maximum pixel values"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="cmin",
group="Comb.", description="The number of minimum pixel values"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="smethod",
group="Other", description="Method to use for interpolation during shifting (NN, CS)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="background",
group="Other", description="Apply background removal"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="pix_scale",
group="Other", description="Pixel Scale"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="no_subtract",
group="Other", description="Don't sky subtract object and references"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="ifus",
group="Other", description="The indices of the IFUs to combine"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="method",
group="Other", description="The shifting method"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="filename",
group="Other", description="The path to the file with the shift vectors"),
]
def readFitsData(self, fitsFiles):
# Initialise
self.files = dict()
self.oh_spec = dict()
self.oh_spec["Found"] = False
# Loop on all FITS files
for f in fitsFiles:
# Use OH_SPEC if found
if f.category == self.oh_spec_cat :
oh_spec_file = PipelineProduct(f)
self.oh_spec["Found"] = True
self.oh_spec["CRVAL1"] = oh_spec_file.all_hdu[1].header["CRVAL1"]
self.oh_spec["CDELT1"] = oh_spec_file.all_hdu[1].header["CDELT1"]
self.oh_spec["Spectrum"] = oh_spec_file.all_hdu[1].data
# For each reconstructed image
if f.category == self.reconstructed_cat :
recons_file = PipelineProduct(f)
filename = os.path.basename(f.name)
# Create a Dictionary per file
self.files[filename] = dict()
# Loop on extensions
for recons_ext in recons_file.all_hdu:
# EXTNAME is missing in the primary header - Skip it anyway
try:
extname = recons_ext.header['EXTNAME']
except KeyError:
continue
# Create Entry for the extension
self.files[filename][extname]=dict()
# Get the IFU number from extname to get the IFU status
m = re.search(r"\d+", extname)
ifu_number = m.group()
self.files[filename][extname]["IFU_NUMBER"] = int(ifu_number)
naxis = recons_ext.header['NAXIS']
# Set the IFU STATUS
arm_key = 'ESO OCS ARM'+ifu_number+' NOTUSED'
if (arm_key in recons_file.all_hdu[0].header) :
self.files[filename][extname]["IFU_STATUS"] = recons_file.all_hdu[0].header[arm_key]
else :
if (naxis == 3):
self.files[filename][extname]["IFU_STATUS"] = 'Active'
else :
self.files[filename][extname]["IFU_STATUS"] = 'Empty'
if (naxis == 3):
# Get Keyword infos
self.files[filename][extname]["CRPIX3"] = recons_ext.header['CRPIX3']
self.files[filename][extname]["CRVAL3"] = recons_ext.header['CRVAL3']
self.files[filename][extname]["CDELT3"] = recons_ext.header['CDELT3']
# Fill Spectrum
self.files[filename][extname]["Spectrum"] = []
for cube_plane in recons_ext.data:
cube_plane_nan_free = cube_plane[~numpy.isnan(cube_plane)]
if (len(cube_plane_nan_free) > 0):
mean = cube_plane_nan_free.mean()
else:
mean = numpy.nan
self.files[filename][extname]["Spectrum"].append(mean)
# If proper files are there...
if (len(self.files.keys()) > 0):
# Set the plotting functions
self._add_subplots = self._add_subplots
self._plot = self._data_plot
else:
self._add_subplots = self._add_nodata_subplots
self._plot = self._nodata_plot
def addSubplots(self, figure):
self._add_subplots(figure)
def plotProductsGraphics(self):
self._plot()
def plotWidgets(self) :
widgets = list()
# Files Selector radiobutton
self.radiobutton = reflex_plot_widgets.InteractiveRadioButtons(self.files_selector, self.setFSCallback, self.files.keys(), 0,
title='Files Selection (Mouse left button)')
widgets.append(self.radiobutton)
self.clickable_ifus =reflex_plot_widgets.InteractiveClickableSubplot(self.ifus_selector, self.setIFUSCallback)
widgets.append(self.clickable_ifus)
return widgets
def extension_has_spectrum(self, filename, extname):
if ("Spectrum" in self.files[self.selected_file][extname].keys()):
return True
else:
return False
def setIFUSCallback(self, point) :
if (1 < point.ydata < 3) :
extname = "IFU."+str(int((point.xdata/2)+0.5))+".DATA"
if (self.extension_has_spectrum(self.selected_file, extname)):
# Update selected extension
self.selected_extension = extname
# Redraw IFUs selection
self._plot_ifus_selector(self.selected_file)
# Redraw spectrum
self._plot_spectrum()
def setFSCallback(self, filename) :
# Keep track of the selected file
self.selected_file = filename
# Check that the new file currently selected extensin is valid
if (not self.extension_has_spectrum(self.selected_file, self.selected_extension)):
self.selected_extension = self._get_first_valid_extname(self.selected_file)
self._plot_ifus_selector(self.selected_file)
# Redraw spectrum
self._plot_spectrum()
def _add_subplots(self, figure):
gs = gridspec.GridSpec(3, 1)
self.files_selector = figure.add_subplot(gs[0,0])
self.ifus_selector = figure.add_subplot(gs[1,0])
self.spec_plot = figure.add_subplot(gs[2,0])
def _data_plot_get_tooltip(self):
return self.selected_file+" ["+self.selected_extension+"]"
def _plot_spectrum(self):
extension_dict = self.files[self.selected_file][self.selected_extension]
# Define wave
pix = numpy.arange(len(extension_dict["Spectrum"]))
wave = extension_dict["CRVAL3"] + pix * extension_dict["CDELT3"]
# Plot Spectrum
self.spec_plot.clear()
specdisp = pipeline_display.SpectrumDisplay()
specdisp.setLabels("Wavelength (microns)", "Flux (ADU)")
specdisp.display(self.spec_plot, "Spectrum", self._data_plot_get_tooltip(), wave, extension_dict["Spectrum"])
if (self.oh_spec["Found"]):
# Overplot the OH spectrum
pix = numpy.arange(len(self.oh_spec["Spectrum"]))
wave = self.oh_spec["CRVAL1"] + pix * self.oh_spec["CDELT1"]
specdisp.overplot(self.spec_plot, wave, self.oh_spec["Spectrum"], 'red')
self.spec_plot.legend(('Observed', 'OH'))
def _plot_ifus_selector(self, filename):
self.ifus_selector.clear()
# Loop on the different kind of Status to Print the Legend
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(1, 9, 8, 10), cmap='summer')
self.ifus_selector.text(2, 8.5, 'Active', fontsize=11,color='white')
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(11, 19, 8, 10), cmap='spring')
self.ifus_selector.text(12, 8.5, 'Locked', fontsize=11,color='white')
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(21, 29, 8, 10), cmap='winter')
self.ifus_selector.text(22, 8.5, 'NotInPAF', fontsize=11,color='white')
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(31, 39, 8, 10), cmap='autumn')
self.ifus_selector.text(32, 8.5, 'NotInPAF & Locked', fontsize=11,color='white')
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(41, 49, 8, 10), cmap='cool')
self.ifus_selector.text(42, 8.5, 'Collision', fontsize=11,color='white')
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(1, 9, 4, 6), cmap='copper')
self.ifus_selector.text(2, 4.5, 'Empty', fontsize=11,color='white')
# Display the IFUs selection squares
box_y_start = 1
box_y_stop = 3
box_xwidth = 1.5
for extname in self.files[filename].keys():
# Compute the IFU number
ifu_number = self.files[filename][extname]['IFU_NUMBER']
# Draw the little IFU image
box_xstart = 2 * ifu_number - 1
box_xstop = box_xstart + box_xwidth
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(box_xstart,box_xstop,box_y_start,box_y_stop),
cmap=self.IFU_stat_color[self.files[filename][extname]["IFU_STATUS"]])
# Write the IFU number in the image
self.ifus_selector.text(2 * (ifu_number-1) + 1.2, 1.5, str(ifu_number), fontsize=13,color='white')
# Mark the selected IFU
if (extname == self.selected_extension):
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(box_xstart,box_xstop,0.5,0.7),
cmap=self.IFU_stat_color[self.files[filename][extname]["IFU_STATUS"]])
self.ifus_selector.axis([0,50,0,11])
self.ifus_selector.set_title("IFU Selection (Mouse middle button)")
self.ifus_selector.get_xaxis().set_ticks([])
self.ifus_selector.get_yaxis().set_ticks([])
# Get the first valid extension name (ie containing a spectrum) in a file - "" if None
def _get_first_valid_extname(self, filename):
for extname in sorted(self.files[filename].keys()):
if (self.extension_has_spectrum(filename, extname)) :
return extname
return ""
def _data_plot(self):
# Initial file is the first one
self.selected_file = self.files.keys()[0]
self.selected_extension = self._get_first_valid_extname(self.selected_file)
# Plot the IFUS selection
self._plot_ifus_selector(self.selected_file)
# Draw Spectrum
self._plot_spectrum()
def _add_nodata_subplots(self, figure):
self.img_plot = figure.add_subplot(1,1,1)
def _nodata_plot(self):
# could be moved to reflex library?
self.img_plot.set_axis_off()
text_nodata = "Data not found. Input files should contain this" \
" type:\n%s" % self.det_img_wave_cat
self.img_plot.text(0.1, 0.6, text_nodata, color='#11557c',
fontsize=18, ha='left', va='center', alpha=1.0)
self.img_plot.tooltip = 'No data found'
#This is the 'main' function
if __name__ == '__main__':
from reflex_interactive_app import PipelineInteractiveApp
# Create interactive application
interactive_app = PipelineInteractiveApp()
# get inputs from the command line
interactive_app.parse_args()
#Check if import failed or not
if not import_success:
interactive_app.setEnableGUI(False)
#Open the interactive window if enabled
if interactive_app.isGUIEnabled():
#Get the specific functions for this window
dataPlotManager = DataPlotterManager()
interactive_app.setPlotManager(dataPlotManager)
interactive_app.showGUI()
else:
interactive_app.set_continue_mode()
#Print outputs. This is parsed by the Reflex python actor to
#get the results. Do not remove
interactive_app.print_outputs()
sys.exit()
|