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
|
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 = "kmos_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="oscan",
group="Recons.", description="Apply Overscan Correction"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="xcal_interpolation",
group="Recons.", description="Interpolate xcal between rotator angles"),
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="obj_sky_table",
group="Sky Sub.", description="The path to the file with the modified obj/sky associations."),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="background",
group="Sky Sub.", description="Apply background removal"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="no_subtract",
group="Sky Sub.", description="Don't sky subtract object and references"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="sky_tweak",
group="Sky Sub.", description="Use modified sky cube for sky subtraction (TRUE or FALSE)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="skip_sky_oh_align",
group="Sky Sub.", description="Do not align the sky on OH lines - Only is stretch is on (0-no, 1-yes)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="stretch",
group="Sky Sub.", description="Stretch the sky for sky subtraction (0-no, 1-yes)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="stretch_degree",
group="Sky Sub.", description="Stretching polynomial degree."),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="stretch_resampling",
group="Sky Sub.", description="Stretching interpolation method (linear / spline)"),
reflex.RecipeParameter(recipe=self.recipe_name, displayName="velocity_offset",
group="Sky Sub.", description="Specify velocity offset correction in km/s for lambda scale."),
]
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']
self.files[filename][extname]["UNIT"] = recons_ext.header['ESO QC CUBE_UNIT']
self.files[filename][extname]["NAME"] = recons_ext.header['ESO OCS ARM' + ifu_number +' NAME']
# 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)
# Fill Collapsed Image
collapsed_frame_ext = self._get_collapsed_ext(fitsFiles, f, extname)
if collapsed_frame_ext:
self.files[filename][extname]["Collapsed"] = collapsed_frame_ext.data
# 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
# Inputs : all files, reconstructed file, extension
# Returns the corresponding extension in the collapsed corresponding file
# The corresponding file must have the same file name as ref_file, prefixed with make_image_
# The corresponding extention must have the same EXTNAME keyword
def _get_collapsed_ext(self, fitsFiles, ref_file, extname):
ref_file_name = os.path.basename(ref_file.name)
# Loop on all FITS files
for f in fitsFiles:
filename = os.path.basename(f.name)
if (filename == "make_image_"+ref_file_name):
collapsed_frame = PipelineProduct(f)
for collapsed_frame_ext in collapsed_frame.all_hdu:
# EXTNAME is missing in the primary header - Skip it anyway
try:
coll_extname = collapsed_frame_ext.header['EXTNAME']
except KeyError:
continue
if coll_extname == extname:
return collapsed_frame_ext
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, sorted(self.files.keys()), 0,
title='Files Selection (Left Mouse 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()
# Redisplay image
self._disp_image()
def setFSCallback(self, filename) :
# Keep track of the selected file
self.selected_file = filename
# Check that the new file currently selected extension 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()
# Redisplay image
self._disp_image()
def _add_subplots(self, figure):
gs = gridspec.GridSpec(3, 2)
self.files_selector = figure.add_subplot(gs[0,:])
self.ifus_selector = figure.add_subplot(gs[1,:])
self.spec_plot = figure.add_subplot(gs[2,0])
self.collapsed_img = figure.add_subplot(gs[2,1])
def _data_plot_get_tooltip(self):
return self.selected_file+" ["+self.selected_extension+"];" +" Object NAME = "+str( self.files[self.selected_file][self.selected_extension]["NAME"] )
def _disp_image(self):
extension_dict = self.files[self.selected_file][self.selected_extension]
self.collapsed_img.clear()
imgdisp = pipeline_display.ImageDisplay()
imgdisp.setAspect('equal')
# Only if data
if 'Collapsed' in extension_dict.keys():
imgdisp.display(self.collapsed_img, "Collapsed image", self._data_plot_get_tooltip(), extension_dict["Collapsed"])
self.collapsed_img.set_xlabel("pixels") # item 6f
self.collapsed_img.set_ylabel("pixels") # item 6f
def _plot_spectrum(self):
extension_dict = self.files[self.selected_file][self.selected_extension]
# Plot Spectrum (to be contd.)
self.spec_plot.clear()
specdisp = pipeline_display.SpectrumDisplay()
specdisp.setLabels(r"$\lambda$["+"$\mu$m]" + " (blue: Observed; red: OH[arb. units])",
self._process_label(extension_dict["UNIT"]) )
if (self.oh_spec["Found"]): # Placed before spectrum plotting, so it stays in the background
# Overplot the OH spectrum
pix = numpy.arange(len(self.oh_spec["Spectrum"]))
wave = self.oh_spec["CRVAL1"] + pix * self.oh_spec["CDELT1"]
oh_flux = self.oh_spec["Spectrum"] / numpy.nanmax(self.oh_spec["Spectrum"]) * 200*numpy.nanmax(extension_dict["Spectrum"])
specdisp.overplot(self.spec_plot, wave, oh_flux, '#ED5D5D')
#self.spec_plot.legend(('Observed', 'OH'))
self.spec_plot.fill_between(wave, 0, oh_flux, color='#ED5D5D')
# Define wave
pix = numpy.arange(len(extension_dict["Spectrum"]))
wave = extension_dict["CRVAL3"] + pix * extension_dict["CDELT3"]
# Plot Spectrum (contd.)
specdisp.display(self.spec_plot, "Spectrum", self._data_plot_get_tooltip(), wave, extension_dict["Spectrum"])
def _process_label(self, in_label):
# If known, 'pretty print' the label
if (in_label == "erg.s**(-1).cm**(-2).angstrom**(-1)"):
return "erg sec" + r"$^{-1}$"+"cm" + r"$^{-2}$" + r"$\AA^{-1}$"
else:
return in_label
def _get_ifu_status_from_file(self, filename, extname):
cur_status = self.files[filename][extname]["IFU_STATUS"]
if cur_status == "NotInPAF/Locked" :
return 'NotInPAF & Locked'
return cur_status
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
ifu_status = self._get_ifu_status_from_file(filename, extname)
self.ifus_selector.imshow(numpy.zeros((1,1)), extent=(box_xstart,box_xstop,box_y_start,box_y_stop),
cmap=self.IFU_stat_color[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[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()
# Display image
self._disp_image()
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.reconstructed_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(enable_init_sop=True)
# 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()
|