Skip to content

Commit

Permalink
Merge pull request #2 from lsst-camera-dh/py3_compatibility_and_eT_re…
Browse files Browse the repository at this point in the history
…factoring

Py3 compatibility and e t refactoring
  • Loading branch information
cjuramy authored Jan 31, 2019
2 parents 37efb51 + 20a07f3 commit ed1d892
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 84 deletions.
2 changes: 1 addition & 1 deletion python/bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, channels, names):
# works only from python 2.7
# self.reverse = {v: k for k, v in self.dictionary.iteritems()}
self.reverse = {}
for k,v in self.dictionary.iteritems():
for k,v in self.dictionary.items():
self.reverse[v] = k
#
self.dictionary.update(self.reverse)
Expand Down
18 changes: 9 additions & 9 deletions python/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# (sequencer with variables)
#
# Author: Laurent Le Guillou

from __future__ import print_function
import re

class SeqParser(object):
Expand Down Expand Up @@ -1031,7 +1031,7 @@ def m_func_clocks_names_line(self, pos):

pnext = self.m_zmsp(pnext)
if self.verbose:
print pnext
print(pnext)

comment = ''
r = self.m_comment(pnext)
Expand Down Expand Up @@ -2034,7 +2034,7 @@ def m_seq(self, pos):
pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[includes]"
print("[includes]")
r = self.m_include_section(pnext)
if r == None:
result['includes'] = [] # keep it optional
Expand All @@ -2044,7 +2044,7 @@ def m_seq(self, pos):
pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[constants]"
print("[constants]")
r = self.m_constant_section(pnext)
if r == None:
return None
Expand All @@ -2053,7 +2053,7 @@ def m_seq(self, pos):
pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[clocks]"
print("[clocks]")
r = self.m_clock_section(pnext)
if r == None:
return None
Expand All @@ -2062,15 +2062,15 @@ def m_seq(self, pos):
pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[pointers]"
print("[pointers]")
r = self.m_ptr_section(pnext)
if r != None:
pnext, result['pointers'] = r

pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[functions]"
print("[functions]")
r = self.m_func_section(pnext)
if r == None:
return None
Expand All @@ -2079,15 +2079,15 @@ def m_seq(self, pos):
pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[subroutines]"
print("[subroutines]")
r = self.m_subr_section(pnext)
if r != None:
pnext, result['subroutines'] = r

pnext = self.m_empty_lines(pnext)

if self.verbose:
print "[mains]"
print("[mains]")
r = self.m_subr_section(pnext, main=True)
if r == None:
return None
Expand Down
77 changes: 48 additions & 29 deletions python/multiscope.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# !/usr/bin/env python
#!/usr/bin/env python

# Displays a scope-like view of the CCD output waveforms for the whole raft.
#
Expand All @@ -12,15 +12,15 @@
#
# Syntax in a script:
# import multiscope
# multiscope.raft_display("tm-scan.fits")

# multiscope.raft_display_allchans("tm-scan.fits")
from __future__ import print_function
import sys
import os.path
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import colors as mplcol
import scope
import pyfits
from astropy.io import fits


def get_scandata_raft(inputfile, datadir=''):
Expand All @@ -33,7 +33,7 @@ def get_scandata_raft(inputfile, datadir=''):
raftarrays = []
if os.path.splitext(inputfile)[1] in [".fits", ".fz"]:
# starts with 00 through 22
seglist = ["%d%d" % (i, j) for i in range(3) for j in range(3)]
seglist = slot_ids()
# when REB2 data is missing
#seglist = ["%d%d" % (i, j) for i in range(2) for j in range(3)]

Expand All @@ -58,7 +58,7 @@ def get_scandata_raft(inputfile, datadir=''):
#print fullreb.shape
raftarrays.extend([a for a in np.split(fullreb, 3, axis=0)]) # splits REB data into 3 CCDs
elif inputfile == '':
seglist = ["%d%d" % (i, j) for i in range(3) for j in range(3)]
seglist = slot_ids()
for segstr in seglist:
d = os.path.join(datadir, 'S%s' % segstr)
for f in os.listdir(d):
Expand All @@ -72,6 +72,7 @@ def get_scandata_raft(inputfile, datadir=''):

return raftarrays, seglist


def raft_display_allchans(inputfile, datadir='', suptitle=''):
"""
Builds up data from all raft files and display scans.
Expand All @@ -81,7 +82,36 @@ def raft_display_allchans(inputfile, datadir='', suptitle=''):
:return:
"""
raftarrays, seglist = get_scandata_raft(inputfile, datadir)
if inputfile:
dataname = scope.get_rootfile(inputfile)
else:
dataname = os.path.split(datadir)[-1]
if not suptitle:
suptitle = dataname
outfile = os.path.join(datadir, "multiscope-%s.png" % dataname)
plot_raft_allchans(raftarrays, seglist, suptitle)
plt.savefig(outfile)
plt.show()


def slot_ids():
return ["%d%d" % (i, j) for i in range(3) for j in range(3)]


def plot_raft_allchans(raftarrays, seglist, suptitle=''):
"""
Builds up data from all raft files and display scans.
Parameters
----------
raftarrays: list
list of channel data for a single CCD.
seglist: list
list of CCD slot ids, e.g.,
['00', '01', '02', '10', '11', '12', '20', '21', '22']
suptitle: str ['']
Title of the plot.
"""
fig, axes = plt.subplots(nrows = 3, ncols = 3, figsize=(15, 9))
# when REB2 data is missing
# fig, axes = plt.subplots(nrows = 2, ncols = 3, figsize=(15, 9))
Expand All @@ -91,7 +121,7 @@ def raft_display_allchans(inputfile, datadir='', suptitle=''):
maxplot = 0
minplot = 25000
for num,tmscope in enumerate(raftarrays):
ax = axes[num / 3, num % 3 ]
ax = axes[num // 3, num % 3 ]

# single CCD plot
for c in range(16):
Expand All @@ -109,30 +139,19 @@ def raft_display_allchans(inputfile, datadir='', suptitle=''):

if num%3 == 0:
ax.set_ylabel('Scan (ADU)')
if num/3 == 2:
if num//3 == 2:
ax.set_xlabel('Time increment (10 ns)')
ax.grid(True)

for num in range(len(raftarrays)):
ax = axes[num / 3, num % 3]
ax = axes[num // 3, num % 3]
# common scale for all subplots
ax.set_ylim(0, maxplot * 1.02)
#ax.set_ylim(10000, 40000)
#ax.set_ylim(minplot * 0.95, maxplot * 1.02)

ax.legend(bbox_to_anchor=(1.05, 0), loc='lower left', borderaxespad=0.)

if inputfile:
dataname = scope.get_rootfile(inputfile)
else:
dataname = os.path.split(datadir)[-1]

if suptitle:
plt.suptitle(suptitle, fontsize='x-large')
else:
plt.suptitle(dataname)
plt.savefig(os.path.join(datadir, "multiscope-%s.png" % dataname))
plt.show()
plt.suptitle(suptitle, fontsize='x-large')


def raft_display_longscan(inputfile, niter, datadir='', suptitle=''):
Expand All @@ -153,7 +172,7 @@ def raft_display_longscan(inputfile, niter, datadir='', suptitle=''):

# plot all channels, with one subplot per CCD
for num in range(len(raftfits)):
ax = axes[num / 3, num % 3 ]
ax = axes[num // 3, num % 3 ]
tmscope = scope.stitch_long_scan(raftfits[num], niter, datadir)

# single CCD plot
Expand Down Expand Up @@ -228,19 +247,19 @@ def reframe_scope_frames(listfits):
:return:
"""
for f in listfits:
h = pyfits.open(f)
h = fits.open(f)
imgcols = h[1].header['naxis1']
imglines = h[1].header['naxis2']
detstring = '[1:%d,1:%d]' % (imgcols * 8, 2 * imglines)
# Create HDU list
primaryhdu = pyfits.PrimaryHDU()
primaryhdu = fits.PrimaryHDU()
primaryhdu.header = h[0].header.copy()
primaryhdu.header['DETSIZE'] = (detstring, 'NOAO MOSAIC keywords')
hcopy = pyfits.HDUList([primaryhdu])
hcopy = fits.HDUList([primaryhdu])

#print h.info()
for extnum in range(1,17,1):
exthdu = pyfits.ImageHDU(data=h[extnum].data)
exthdu = fits.ImageHDU(data=h[extnum].data)
get_extension_header(imgcols, imgcols, extnum-1, exthdu, detstring)
hcopy.append(exthdu)

Expand All @@ -260,13 +279,13 @@ def corrcoef_raftscope(raftsfits, ROIrows, ROIcols, norm=True):
stackh = []

for fl in raftsfits:
h = pyfits.open(fl)
h = fits.open(fl)
for i in range(1, 17):
stackh.append(h[i].data[ROIrows, ROIcols])
h.close()
del h
stackh = np.stack(stackh)
print stackh.shape
print(stackh.shape)

a = []

Expand All @@ -276,7 +295,7 @@ def corrcoef_raftscope(raftsfits, ROIrows, ROIcols, norm=True):
else:
a.append(np.cov(stackh[:, :, numcol]))
a = np.stack(a)
print a.shape
print(a.shape)

return a.mean(axis=0)

Expand Down
14 changes: 7 additions & 7 deletions python/rebtxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Python minimal interface for the REB FPGA
# TXT assembler-style IO
#

from __future__ import print_function
from sequencer import *
import grammar

Expand Down Expand Up @@ -148,7 +148,7 @@ def parse_functions(self, functions_node):
self.functions_desc[name] = dict(func_dict)

if self.verbose:
print name, fullname
print(name, fullname)

function = Function(name=name,
fullname=fullname,
Expand All @@ -173,7 +173,7 @@ def parse_functions(self, functions_node):
cpos += 1

if self.verbose:
print channel_position
print(channel_position)
self.functions_desc[name]['clocks'] = func['clocks'] # list of active channels

# self.timelengths = {0: 12, 1: 14}
Expand Down Expand Up @@ -208,7 +208,7 @@ def parse_functions(self, functions_node):
cval = values[cpos]
output |= (cval << crank)
if self.verbose:
print bin(output)
print(bin(output))

outputs[islice] = output

Expand Down Expand Up @@ -331,7 +331,7 @@ def parse_subroutine(self, sub_node):
subname = sub_node['name']
fullname = sub_node['comment']
if self.verbose:
print " name = ", subname
print(" name = ", subname)
# print " fullname = ", fullname

sub = Subroutine()
Expand Down Expand Up @@ -385,12 +385,12 @@ def parse_result(self, result):
# Parse all subroutines
self.parse_subroutines(result['subroutines'])
if self.verbose:
print "SUBS", self.subroutines_names
print("SUBS", self.subroutines_names)

# Parse all 'mains'
self.parse_mains(result['mains'])
if self.verbose:
print "MAINS", self.mains_names
print("MAINS", self.mains_names)
# we will use 0x340000 (now in pointers) to point to the right one
# initializing Main pointer if does not exist in file
if 'Main' not in self.pointers:
Expand Down
Loading

0 comments on commit ed1d892

Please sign in to comment.