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

DM-7244: Port to Python 3 #15

Merged
merged 14 commits into from
Aug 20, 2016
32 changes: 17 additions & 15 deletions bin.src/genInputRegistry.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/usr/bin/env python2
from __future__ import absolute_import, division, print_function
#
#
# LSST Data Management System
# Copyright 2008, 2009, 2010, 2011, 2012, 2013 LSST Corporation.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
import argparse
Expand All @@ -35,6 +35,7 @@

DefaultOutputRegistry = "registry.sqlite3"


def process(dirList, inputRegistry=None, outputRegistry="registry.sqlite3"):
print("process(dirList=%s)" % (dirList,))
if os.path.exists(outputRegistry):
Expand Down Expand Up @@ -86,6 +87,7 @@ def process(dirList, inputRegistry=None, outputRegistry="registry.sqlite3"):
conn.close()
print("wrote registry file %r" % (outputRegistry,))


def processRawDir(rawDir, conn, done, qsp):
print(rawDir, "... started")
nProcessed = 0
Expand All @@ -100,44 +102,44 @@ def processRawDir(rawDir, conn, done, qsp):

visit, filterName = m.groups()
key = "%s_f%s" % (visit, filterName)
if done.has_key(key):
if key in done:
nSkipped += 1
continue

md = afwImage.readMetadata(fitsPath)
expTime = md.get("EXPTIME")
mjdObs = md.get("MJD-OBS")
taiObs = dafBase.DateTime(mjdObs, dafBase.DateTime.MJD,
dafBase.DateTime.TAI).toString()[:-1]
dafBase.DateTime.TAI).toString()[:-1]
conn.execute("""INSERT INTO raw VALUES
(NULL, ?, ?, ?, ?)""",
(visit, filterName, taiObs, expTime))
(visit, filterName, taiObs, expTime))

for row in conn.execute("SELECT last_insert_rowid()"):
id = row[0]
break

wcs = afwImage.makeWcs(md)
poly = skypix.imageToPolygon(wcs,
md.get("NAXIS1"), md.get("NAXIS2"),
padRad=0.000075) # about 15 arcsec
md.get("NAXIS1"), md.get("NAXIS2"),
padRad=0.000075) # about 15 arcsec
pix = qsp.intersect(poly)
for skyTileId in pix:
conn.execute("INSERT INTO raw_skyTile VALUES(?, ?)",
(id, skyTileId))
(id, skyTileId))

conn.commit()

nProcessed += 1

print("%s... %d processed, %d skipped, %d unrecognized" %
(rawDir, nProcessed, nSkipped, nUnrecognized))
(rawDir, nProcessed, nSkipped, nUnrecognized))

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Make a registry file for a data repository")
parser.add_argument("dir", nargs="+", help="one or more directories of data, e.g. data/input")
parser.add_argument("-i", "--input", help="input registry")
parser.add_argument("-o", "--output", default=DefaultOutputRegistry,
help="output registry (default=%s)" % (DefaultOutputRegistry,))
help="output registry (default=%s)" % (DefaultOutputRegistry,))
args = parser.parse_args()
process(args.dir, args.input, args.output)
10 changes: 6 additions & 4 deletions bin.src/mkpickle.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from future import standard_library
standard_library.install_aliases()
#!/usr/bin/env python

import cPickle
import pickle
import os

for raft in ["0,1", "0,2", "0,3"]:
for sensor in ["0,1", "1,0", "1,1"]:
for snap in [0, 1]:
for channel in ["0,0", "0,1", "1,0", "1,1"]:
dataId = dict(visit=85470982,
raft=raft, sensor=sensor, snap=snap, channel=channel)
raft=raft, sensor=sensor, snap=snap, channel=channel)
loc = "raw-v%(visit)d-E%(snap)03d-r%(raft)s-s%(sensor)s-c%(channel)s.pickle" % dataId
loc = os.path.join("tests", "data", "input", loc)
num = int(raft[0] + raft[2] + sensor[0] + sensor[2] +
channel[0] + channel[2]) * 10 + snap
channel[0] + channel[2]) * 10 + snap
if raft != "0,3" or (snap != 1 and sensor != "0,1"):
with open(loc, "w") as f:
Copy link
Member Author

Choose a reason for hiding this comment

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

I worry that this pickle file should be opened in "wb" mode. I'm guessing the test never tries to read from the dumped pickle file. Do we have any check to see if we can read this dumped file back again?

Copy link
Contributor

@r-owen r-owen Aug 19, 2016

Choose a reason for hiding this comment

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

Good catch. This is an obsolete file. I deleted it.

cPickle.dump(num, f)
pickle.dump(num, f)
4 changes: 2 additions & 2 deletions config/calibrate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# we don't have astrometry_net data (yet) so astrom and photo cal are impossible
config.doAstrometry=False
config.doPhotoCal=False
config.doAstrometry = False
config.doPhotoCal = False
4 changes: 2 additions & 2 deletions config/isr.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
config.doDark=False
config.doFringe=False
config.doDark = False
config.doFringe = False
24 changes: 13 additions & 11 deletions data/utils/assembleLsstChannels.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/usr/bin/env python2
from __future__ import absolute_import, division, print_function
#
#
# LSST Data Management System
# Copyright 2014 LSST Corporation.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
"""Assemble a set of LSSTSim channel images into one obs_test image
Expand All @@ -34,7 +34,7 @@
import lsst.afw.image as afwImage

OutFileName = "image.fits"
SizeY = 1000 # number of pixels per amplifier in X direction (Y uses all pixels)
SizeY = 1000 # number of pixels per amplifier in X direction (Y uses all pixels)

KeysToCopy = (
"EPOCH",
Expand All @@ -46,10 +46,11 @@
"AIRMASS",
)


def openChannelImage(dirPath, x, y):
"""Open an LSSTSim channel image
"""
globStr = os.path.join(dirPath, "imsim_*_R22_S00_C%d%d*" % (y, x))
globStr = os.path.join(dirPath, "imsim_*_R22_S00_C%d%d*" % (y, x))
inImagePathList = glob.glob(globStr)
if len(inImagePathList) != 1:
raise RuntimeError("Found %s instead of 1 file" % (inImagePathList,))
Expand All @@ -65,6 +66,7 @@ def openChannelImage(dirPath, x, y):
exposureClass = afwImage.ExposureF
return exposureClass(inImagePath)


def assembleImage(dirPath):
"""Make one image by combining half of amplifiers C00, C01, C10, C11 of lsstSim data
"""
Expand All @@ -73,7 +75,7 @@ def assembleImage(dirPath):
yStart = fullInDim[1] - SizeY
if yStart < 0:
raise RuntimeError("channel image unexpectedly small")
subDim = afwGeom.Extent2I(fullInDim[0], SizeY) # dimensions of the portion of a channel that we use
subDim = afwGeom.Extent2I(fullInDim[0], SizeY) # dimensions of the portion of a channel that we use
inSubBBox = afwGeom.Box2I(afwGeom.Point2I(0, yStart), subDim)
outBBox = afwGeom.Box2I(afwGeom.Point2I(0, 0), subDim * 2)
outExposure = inExposure.Factory(outBBox)
Expand Down Expand Up @@ -101,7 +103,7 @@ def assembleImage(dirPath):
inArrList = inMIView.getArrays()
for arr in inArrList:
if numpy.any(arr != 0):
arr[:,:] = numpy.flipud(arr)
arr[:, :] = numpy.flipud(arr)

xStart = x * subDim[0]
yStart = y * subDim[1]
Expand All @@ -120,7 +122,7 @@ def assembleImage(dirPath):
""" % (OutFileName,),
)
parser.add_argument("dir", default=".", nargs="?", help="directory containing LSSTSim channel images " +
"(at least for channels 0,0, 0,1, 1,0 and 1,1); defaults to the current working directory.")
"(at least for channels 0,0, 0,1, 1,0 and 1,1); defaults to the current working directory.")
args = parser.parse_args()

assembleImage(args.dir)
27 changes: 14 additions & 13 deletions data/utils/defectsFromBias.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
#!/usr/bin/env python2
from __future__ import absolute_import, division, print_function
#
#
# LSST Data Management System
# Copyright 2014 LSST Corporation.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
import argparse
import itertools
import time

import numpy
Expand All @@ -36,6 +35,7 @@
detectorName = "0"
detectorSerial = "0000011"


def getBBoxList(path, detectorName):
"""Read a defects file and return the defects as a list of bounding boxes
"""
Expand All @@ -55,11 +55,12 @@ def getBBoxList(path, detectorName):
return bboxList
raise RuntimeError("Data not found for detector %r" % (detectorName,))


def writeDefectsFile(bboxList, path, detectorSerial, detectorName):
head = pyfits.Header()
head.update('SERIAL', detectorSerial,'Serial of the detector')
head.update('NAME', detectorName,'Name of detector for this defect map')
head.update('CDATE',time.asctime(time.gmtime()),'UTC of creation')
head.update('SERIAL', detectorSerial, 'Serial of the detector')
head.update('NAME', detectorName, 'Name of detector for this defect map')
head.update('CDATE', time.asctime(time.gmtime()), 'UTC of creation')

x0 = numpy.array([d.getMinX() for d in bboxList])
y0 = numpy.array([d.getMinY() for d in bboxList])
Expand All @@ -71,7 +72,7 @@ def writeDefectsFile(bboxList, path, detectorSerial, detectorName):
col3 = pyfits.Column(name='height', format='I', array=numpy.array(height))
col4 = pyfits.Column(name='width', format='I', array=numpy.array(width))
cols = pyfits.ColDefs([col1, col2, col3, col4])
tbhdu = pyfits.new_table(cols, header = head)
tbhdu = pyfits.new_table(cols, header=head)
hdu = pyfits.PrimaryHDU()
thdulist = pyfits.HDUList([hdu, tbhdu])
thdulist.writeto(DefectsPath)
Expand All @@ -94,6 +95,6 @@ def writeDefectsFile(bboxList, path, detectorSerial, detectorName):

test2BBoxList = getBBoxList(DefectsPath, detectorName)
assert len(bboxList) == len(test2BBoxList)
for boxA, boxB in itertools.izip(bboxList, test2BBoxList):
for boxA, boxB in zip(bboxList, test2BBoxList):
Copy link
Member Author

Choose a reason for hiding this comment

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

might be worth adding from builtins import zip at the top to ensure we get the iterator version on python2.

Copy link
Contributor

@r-owen r-owen Aug 19, 2016

Choose a reason for hiding this comment

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

I wonder why futurize didn't do that automatically. I think it's a tiny list, so it may not matter, but I'll add it

assert boxA == boxB
print("verified that defects file %r round trips correctly" % (DefectsPath,))
print("verified that defects file %r round trips correctly" % (DefectsPath,))
16 changes: 9 additions & 7 deletions data/utils/maskFromDefects.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/usr/bin/env python2
from __future__ import absolute_import, division, print_function
#
#
# LSST Data Management System
# Copyright 2014 LSST Corporation.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
import argparse
Expand All @@ -32,6 +32,7 @@

MaskFileName = "defectsMask.fits"


def getBBoxList(path, detectorName):
"""Get a list of defects as a list of bounding boxes
"""
Expand All @@ -51,8 +52,9 @@ def getBBoxList(path, detectorName):
return bboxList
raise RuntimeError("Data not found for detector %r" % (detectorName,))


def writeDefectsFile(bboxList, path):
maskBBox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(1,1))
maskBBox = afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(1, 1))
for box in bboxList:
maskBBox.include(box)

Expand Down
3 changes: 2 additions & 1 deletion python/lsst/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import pkgutil, lsstimport
import pkgutil
import lsstimport
__path__ = pkgutil.extend_path(__path__, __name__)
3 changes: 2 additions & 1 deletion python/lsst/obs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import pkgutil, lsstimport
import pkgutil
import lsstimport
__path__ = pkgutil.extend_path(__path__, __name__)
12 changes: 6 additions & 6 deletions python/lsst/obs/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#
#
# LSST Data Management System
# Copyright 2008, 2009, 2010 LSST Corporation.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
from .version import *
Expand Down