Skip to content

Commit

Permalink
Simplify FITS I/O for "FitsStorage"
Browse files Browse the repository at this point in the history
Simplify FITS I/O for "FitsStorage" by calling new methods
readFitsWithOptions or writeFitsWithOptions on those objects.
instead of using boost persistence.
  • Loading branch information
r-owen committed Aug 30, 2018
1 parent 3806c63 commit f24ce70
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions python/lsst/daf/persistence/posixStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,61 @@ def writeConfigStorage(butlerLocation, obj):


def readFitsStorage(butlerLocation):
"""Read a FITS image from a butlerLocation.
"""Read objects from a FITS file specified by ButlerLocation.
The class to be read must have a class method or static method
``readFitsWithOptions(path, options)`` where options is
the data returned by ``butlerLocation.getAdditionalData()``
Parameters
----------
butlerLocation : ButlerLocation
The location for the object(s) to be read.
Returns
-------
A list of objects as described by the butler location. One item for
each location in butlerLocation.getLocations()
"""
pythonType = butlerLocation.getPythonType()
if pythonType is not None:
if isinstance(pythonType, basestring):
pythonType = doImport(pythonType)
results = []
additionalData = butlerLocation.getAdditionalData()
for locationString in butlerLocation.getLocations():
locStringWithRoot = os.path.join(butlerLocation.getStorage().root, locationString)
logLoc = LogicalLocation(locStringWithRoot, additionalData)
if not os.path.exists(logLoc.locString()):
raise RuntimeError("No such FITS file: " + logLoc.locString())
finalItem = pythonType.readFitsWithOptions(logLoc.locString(), options=additionalData)
results.append(finalItem)
return results


def writeFitsStorage(butlerLocation, obj):
"""Writes an object to a FITS file specified by ButlerLocation.
The object to be written must have a method
``writeFitsWithOptions(path, options)`` where options is
the data returned by ``butlerLocation.getAdditionalData()``
Parameters
----------
butlerLocation : ButlerLocation
The location for the object to be written.
obj : object instance
The object to be written.
"""
additionalData = butlerLocation.getAdditionalData()
locations = butlerLocation.getLocations()
with SafeFilename(os.path.join(butlerLocation.getStorage().root, locations[0])) as locationString:
logLoc = LogicalLocation(locationString, additionalData)
obj.writeFitsWithOptions(logLoc.locString(), options=additionalData)


def boostReadFitsStorage(butlerLocation):
"""Read a FITS image from a butlerLocation using boost peristence
Parameters
----------
Expand All @@ -589,8 +643,9 @@ def readFitsStorage(butlerLocation):
return results


def writeFitsStorage(butlerLocation, obj):
"""Writes an object to a FITS file specified by ButlerLocation.
def boostWriteFitsStorage(butlerLocation, obj):
"""Writes an object to a FITS file specified by ButlerLocation
using boost peristence
Parameters
----------
Expand Down Expand Up @@ -954,7 +1009,7 @@ def writeBoostStorage(butlerLocation, obj):
PosixStorage.registerFormatters("MatplotlibStorage", readMatplotlibStorage, writeMatplotlibStorage)
PosixStorage.registerFormatters("PafStorage", readFormatter=readPafStorage)
PosixStorage.registerFormatters("YamlStorage", readYamlStorage, writeYamlStorage)
PosixStorage.registerFormatters("BoostStorage", readFitsStorage, writeFitsStorage)
PosixStorage.registerFormatters("BoostStorage", boostReadFitsStorage, boostWriteFitsStorage)

Storage.registerStorageClass(scheme='', cls=PosixStorage)
Storage.registerStorageClass(scheme='file', cls=PosixStorage)

0 comments on commit f24ce70

Please sign in to comment.