Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port obs_subaru to python3 Tickets/dm 7306 #42

Merged
merged 4 commits into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions bin.src/displayCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lsst.afw.cameraGeom.utils as cameraGeomUtils
from lsst.afw.cameraGeom import Camera


def checkStr(strVal, level):
"""Check if a string is a valid identifier
@param[in] strVal: String containing the identifier
Expand Down Expand Up @@ -54,8 +55,8 @@ def displayCamera(args):
cameraGeomUtils.showCcd(detector, frame=frame)
frame += 1

raftMap = {'0':[],
'1':[]}
raftMap = {'0': [],
'1': []}
for det in camera:
dName = det.getName()
if dName.startswith('1'):
Expand Down Expand Up @@ -87,16 +88,16 @@ def displayCamera(args):

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Display the Subaru cameras')
parser.add_argument('--showAmp', help='Show an amplifier segment in ds9 May have multiple arguments. '\
'Format like ccd_name amp_name e.g. '\
parser.add_argument('--showAmp', help='Show an amplifier segment in ds9 May have multiple arguments. '
'Format like ccd_name amp_name e.g. '
'\"1_53 0,0\"', type=str, nargs='+')
parser.add_argument('--showCcd', help='Show a CCD from the mosaic in ds9. May have multiple arguments. '\
parser.add_argument('--showCcd', help='Show a CCD from the mosaic in ds9. May have multiple arguments. '
'Format like ccd_name e.g. \"0_17\"', type=str,
nargs='+')
parser.add_argument('--showRaft', help='Show a raft from the mosaic in ds9. May have multiple arguments. '\
'May be 0 or 1', type=str)
parser.add_argument('--showRaft', help='Show a raft from the mosaic in ds9. May have multiple arguments. '
'May be 0 or 1', type=str)
parser.add_argument('--showCamera', help='Show the camera mosaic in ds9.', action='store_true')
parser.add_argument('--cameraBinSize', type= int, default=20,
parser.add_argument('--cameraBinSize', type=int, default=20,
help='Size of binning when displaying the full camera mosaic')
parser.add_argument('--plotFocalPlane', action='store_true',
help='Plot the focalplane in an interactive matplotlib window')
Expand Down
12 changes: 7 additions & 5 deletions bin.src/genDefectFits.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import sys
import os.path
import re
Expand All @@ -14,6 +15,7 @@
Defect = collections.namedtuple('Defect', ['x0', 'y0', 'width', 'height'])
mapperMap = {'hsc': HscMapper, 'suprimecam': SuprimecamMapper, 'suprimecam_mit': SuprimecamMapperMit}


def genDefectFits(cameraName, source, targetDir):
mapper = mapperMap[cameraName.lower()](root=".")
camera = mapper.camera
Expand All @@ -32,16 +34,16 @@ def genDefectFits(cameraName, source, targetDir):
continue
ccd, x0, y0, width, height = re.split("\s+", line)
ccd = int(ccd)
if not ccds.has_key(ccd):
if ccd not in ccds:
raise RuntimeError("Unrecognised ccd: %d" % ccd)
if not defects.has_key(ccd):
if ccd not in defects:
defects[ccd] = list()
defects[ccd].append(Defect(x0=int(x0), y0=int(y0), width=int(width), height=int(height)))
f.close()

for ccd in ccds:
# Make empty defect FITS file for CCDs with no defects
if not defects.has_key(ccd):
if ccd not in defects:
defects[ccd] = list()

columns = list()
Expand All @@ -56,12 +58,12 @@ def genDefectFits(cameraName, source, targetDir):
table.header['NAME'] = ccd

name = os.path.join(targetDir, "defects_%d.fits" % ccd)
print "Writing %d defects from CCD %d (%s) to %s" % (table.header['NAXIS2'], ccd, ccds[ccd], name)
print("Writing %d defects from CCD %d (%s) to %s" % (table.header['NAXIS2'], ccd, ccds[ccd], name))
if os.path.exists(name):
if args.force:
os.unlink(name)
else:
print >> sys.stderr, "File %s already exists; use --force to overwrite" % name
print("File %s already exists; use --force to overwrite" % name, file=sys.stderr)
continue

table.writeto(name)
Expand Down
10 changes: 7 additions & 3 deletions bin.src/genDefectRegistry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python

from __future__ import print_function
from builtins import zip
import glob
import os
import re
Expand All @@ -13,7 +15,9 @@

from lsst.pipe.base import Struct


class Row(Struct):

def __init__(self, path, version, validStart, validEnd=None):
super(Row, self).__init__(path=path, version=version, validStart=validStart, validEnd=validEnd)

Expand Down Expand Up @@ -45,7 +49,7 @@ def __init__(self, path, version, validStart, validEnd=None):
for f in glob.glob(os.path.join(args.root, "*", "defects*.fits")):
m = re.search(r'(\S+)/defects_(\d+)\.fits', f)
if not m:
print >>sys.stderr, "Unrecognized file: %s" % (f,)
print("Unrecognized file: %s" % (f,), file=sys.stderr)
continue
#
# Convert f to be relative to the location of the registry
Expand All @@ -62,7 +66,7 @@ def __init__(self, path, version, validStart, validEnd=None):
# Fix up end dates so there are no collisions.
# Defects files for a CCD are valid from the date they are registered until the next date.
# This means that any defects file should carry ALL the defects that are present at that time.
for ccd, rowList in rowsPerCcd.iteritems():
for ccd, rowList in rowsPerCcd.items():
rowList.sort(key=lambda row: row.validStart) # ISO-8601 will sort just fine without conversion from str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be turned into a list comprehension syntax instead of lambda? Would that be faster/more pythonic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, I think this would be a more modern version:

from operator import attrgetter
rowList.sort(key=attrgetter('validStart'))

But since this executable code doesn't have any tests, I'm loathe to change it.

for thisRow, nextRow in zip(rowList[:-1], rowList[1:]):
thisRow.validEnd = (datetime.datetime.strptime(nextRow.validStart, "%Y-%m-%d") -
Expand All @@ -71,7 +75,7 @@ def __init__(self, path, version, validStart, validEnd=None):

for row in rowList:
if args.verbose:
print "Registering %s" % row.path
print("Registering %s" % row.path)
conn.execute(cmd, (row.path, row.version, ccd, row.validStart, row.validEnd))

conn.commit()
Expand Down
42 changes: 25 additions & 17 deletions bin.src/makeGeom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import print_function
from builtins import map
from builtins import range
import pyfits
import sys
import re
Expand All @@ -36,10 +39,11 @@

hack = True


def getSecCenter(sec):
sec = re.sub('\'', '', sec)
sec = re.sub('\[|\:|\,|\]', ' ', sec)
fields = map(int, sec.split())
fields = list(map(int, sec.split()))
# these indices run from 1..N
# to run from 0..N-1 subtract off 2 from each sum
return 0.5 * (fields[0] + fields[1] - 2), 0.5 * (fields[2] + fields[3] - 2)
Expand All @@ -59,6 +63,7 @@ def printCameraGeom(filename, buff):
buff.write(' } \n')
buff.write('} \n')


def printAmpDefaults(buff):
# http://cfht.hawaii.edu/Instruments/Imaging/MegaPrime/rawdata.html
#
Expand All @@ -72,6 +77,7 @@ def printAmpDefaults(buff):
buff.write(' overclockV: 32 \n')
buff.write('} \n')


def printCcdAmpDefaults(buff):
# Depending on how much we need to undo the imsplice operation
# here are the defaults from straight off the camera
Expand Down Expand Up @@ -104,6 +110,7 @@ def printCcdAmpDefaults(buff):
buff.write(' } \n')
buff.write('} \n')


def printCcdDiskLayout(buff):
buff.write('CcdDiskLayout: { \n')
buff.write(' HduPerAmp: true \n')
Expand Down Expand Up @@ -149,6 +156,7 @@ def printRaftCcdGeom(buff, ccdId, ccdname, xidx, yidx, xpos, ypos):
buff.write(' orientation: 0.000000 0.000000 0.000000 \n')
buff.write(' } \n')


def printElectronics(buff, ccdId, ccdname, xidx, yidx, infoA, infoB):
# http://www.cfht.hawaii.edu/Instruments/Imaging/Megacam/specsinformation.html

Expand Down Expand Up @@ -215,11 +223,11 @@ def printElectronics(buff, ccdId, ccdname, xidx, yidx, infoA, infoB):
# everything else. Raft w.r.t. boresight; CCD w.r.t. Raft center; Amp
# w.r.t CCD center.

buffCamera = open('Camera.paf', 'w')
buffCamera = open('Camera.paf', 'w')
buffElectro = open('Electronics.paf', 'w')

infile = sys.argv[1] # full MEF file; e.g. 871034p.fits
ptr = pyfits.open(infile)
ptr = pyfits.open(infile)

printCameraGeom(infile, buffCamera)
printAmpDefaults(buffCamera)
Expand All @@ -228,29 +236,29 @@ def printElectronics(buff, ccdId, ccdname, xidx, yidx, infoA, infoB):

# 0th layer is pure metadata
for ccd in range(1, len(ptr)):
#for ccd in [1, 28]:
ccdId = ptr[ccd].header['EXTVER']
ccdBoundary = ptr[ccd].header['DETSEC']
ccdName = ptr[ccd].header['CCDNAME']
#for ccd in [1, 28]:
ccdId = ptr[ccd].header['EXTVER']
ccdBoundary = ptr[ccd].header['DETSEC']
ccdName = ptr[ccd].header['CCDNAME']
amp1Boundary = ptr[ccd].header['DETSECA']
amp2Boundary = ptr[ccd].header['DETSECB']

ccdxc, ccdyc = getSecCenter(ccdBoundary)
xpos = (ccdxc - detCenterX) * cfhtPixelScale
ypos = (ccdyc - detCenterY) * cfhtPixelScale
xpos = (ccdxc - detCenterX) * cfhtPixelScale
ypos = (ccdyc - detCenterY) * cfhtPixelScale

gain1 = ptr[ccd].header['GAINA']
gain2 = ptr[ccd].header['GAINB']
gain1 = ptr[ccd].header['GAINA']
gain2 = ptr[ccd].header['GAINB']

rdnoise1 = ptr[ccd].header['RDNOISEA']
rdnoise2 = ptr[ccd].header['RDNOISEB']
rdnoise1 = ptr[ccd].header['RDNOISEA']
rdnoise2 = ptr[ccd].header['RDNOISEB']

# assume non-linear = saturation since we have no non-linearity curves
saturate1 = ptr[ccd].header['MAXLINA']
saturate2 = ptr[ccd].header['MAXLINB']
saturate1 = ptr[ccd].header['MAXLINA']
saturate2 = ptr[ccd].header['MAXLINB']

print ccdId, ccdBoundary, amp1Boundary, amp2Boundary, gain1, gain2, rdnoise1, rdnoise2, \
saturate1, saturate2
print(ccdId, ccdBoundary, amp1Boundary, amp2Boundary, gain1, gain2, rdnoise1, rdnoise2, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line continuation \ isn't needed here anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrapped that at a better place.

saturate1, saturate2)

# flip y
xidx = ccdId % 9
Expand Down
28 changes: 14 additions & 14 deletions bin.src/refileFocus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
108 110
114 108
"""

#!/usr/bin/env python
from __future__ import print_function

import glob
import os
Expand All @@ -37,17 +36,19 @@
#
# Desired remapping
#
remap = {112 : 106,
remap = {112: 106,
#104 : 104
107 : 105,
113 : 107,
115 : 109,
107: 105,
113: 107,
115: 109,
#111 : 111,
108 : 110,
114 : 108,
108: 110,
114: 108,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing choices here are confusing me... not sure if these should be treated more like indices, i.e., 100:101 or new assignments, i.e., 100: 101.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're assignments: it's a dict. I cleaned up the commented lines, though.


# Gather list of files


def globber(field="*", filename="*.fits"):
return glob.glob(os.path.join(args.root, field, "*-*-*", "*", "*", filename))

Expand All @@ -61,7 +62,7 @@ def globber(field="*", filename="*.fits"):
if args.fields:
for f in args.fields:
files += globber(field=f)
print 'Processing %d files...' % len(files)
print('Processing %d files...' % len(files))

# Set up regex for parsing directory structure
reField = r'([\w .+-]+)'
Expand All @@ -75,13 +76,13 @@ def globber(field="*", filename="*.fits"):
for fileNo, fits in enumerate(files):
m = re.search(regex, fits)
if not m:
print "Warning: skipping unrecognized filename:", fits
print("Warning: skipping unrecognized filename:", fits)
continue

field, dateObs, pointing, filterId, visit, ccd = m.groups()
visit = int(visit)
ccd = int(ccd)
if not visits.has_key(visit):
if visit not in visits:
visits[visit] = set()
visits[visit].add(ccd)
#print "Processing %s" % fits
Expand All @@ -91,14 +92,14 @@ def globber(field="*", filename="*.fits"):
#
for visit, ccds in visits.items():
if 105 in ccds: # already refiled
print "%d is already refiled" % visit
print("%d is already refiled" % visit)
del visits[visit]
continue

if set(remap.keys()) != set([_ for _ in ccds if remap.get(_)]): # no focus chips are available
continue

print visit
print(visit)
try:
tmpDir = None
for old, new in remap.items():
Expand All @@ -120,4 +121,3 @@ def globber(field="*", filename="*.fits"):
os.rmdir(tmpDir)
#
#