Exemple #1
0
def reduceToFits(obj):
    """Pickle to FITS

    Intended to be used by the ``__reduce__`` method of a class.

    Parameters
    ----------
    obj
        any object with a ``writeFits`` method taking a
        `~lsst.afw.fits.MemFileManager` and possibly an
        `~lsst.afw.fits.ImageWriteOptions`.

    Returns
    -------
    reduced : `tuple` [callable, `tuple`]
        a tuple in the format returned by `~object.__reduce__`
    """
    manager = MemFileManager()
    options = ImageWriteOptions(ImageCompressionOptions(ImageCompressionOptions.NONE))
    table = getattr(obj, 'table', None)
    if isinstance(table, lsst.afw.table.BaseTable):
        # table objects don't take `options`
        obj.writeFits(manager)
    else:
        # MaskedImage and Exposure both require options for each plane (image, mask, variance)
        if isinstance(obj, (lsst.afw.image.MaskedImage, lsst.afw.image.Exposure)):
            obj.writeFits(manager, options, options, options)
        else:
            obj.writeFits(manager, options)
    size = manager.getLength()
    data = manager.getData()
    return (unreduceFromFits, (obj.__class__, data, size))
Exemple #2
0
def unreduceFromFits(cls, data, size):
    """Unpickle from FITS

    Unpack data produced by `reduceToFits`. This method is used by the
    pickling framework and should not need to be called from user code.

    Parameters
    ----------
    cls : `type`
        the class of object to unpickle. Must have a class-level ``readFits``
        method taking a `~lsst.afw.fits.MemFileManager`.
    data : `bytes`
        an in-memory representation of the object, compatible with
        `~lsst.afw.fits.MemFileManager`
    size : `int`
        the length of `data`

    Returns
    -------
    unpickled : ``cls``
        the object represented by ``data``
    """
    manager = MemFileManager(size)
    manager.setData(data, size)
    return cls.readFits(manager)
Exemple #3
0
def reduceToFits(obj):
    """Pickle to FITS

    Intended to be used by the ``__reduce__`` method of a class.

    Parameters
    ----------
    obj
        any object with a ``writeFits`` method taking a
        `~lsst.afw.fits.MemFileManager` and possibly an
        `~lsst.afw.fits.ImageWriteOptions`.

    Returns
    -------
    reduced : `tuple` [callable, `tuple`]
        a tuple in the format returned by `~object.__reduce__`
    """
    manager = MemFileManager()
    options = ImageWriteOptions(
        ImageCompressionOptions(ImageCompressionOptions.NONE))
    table = getattr(obj, 'table', None)
    if isinstance(table, lsst.afw.table.BaseTable):
        # table objects don't take `options`
        obj.writeFits(manager)
    else:
        # MaskedImage and Exposure both require options for each plane (image, mask, variance)
        if isinstance(obj,
                      (lsst.afw.image.MaskedImage, lsst.afw.image.Exposure)):
            obj.writeFits(manager, options, options, options)
        else:
            obj.writeFits(manager, options)
    size = manager.getLength()
    data = manager.getData()
    return (unreduceFromFits, (obj.__class__, data, size))
Exemple #4
0
def unreduceFromFits(cls, data, size):
    """Unpickle from FITS

    Unpack data produced by `reduceToFits`. This method is used by the
    pickling framework and should not need to be called from user code.

    Parameters
    ----------
    cls : `type`
        the class of object to unpickle. Must have a class-level ``readFits``
        method taking a `~lsst.afw.fits.MemFileManager`.
    data : `bytes`
        an in-memory representation of the object, compatible with
        `~lsst.afw.fits.MemFileManager`
    size : `int`
        the length of `data`

    Returns
    -------
    unpickled : ``cls``
        the object represented by ``data``
    """
    manager = MemFileManager(size)
    manager.setData(data, size)
    return cls.readFits(manager)
Exemple #5
0
def getCosmosCutout(ra=150.23983, dec=+2.56283, sizeX=3):
    """Return an ACS COSMOS cutout from IRSA

    \param ra   Right ascension in decimal degrees (J2000)
    \param dec  Declination in decimal degrees (J2000)
    \param size Size of cutout (arcsec)

    See http://irsa.ipac.caltech.edu/applications/Cutouts/docs/CutoutsProgramInterface.html
    but beware that (as of 2014-03-31) the locstr in the example isn't quite right (needs
    a %20 between the ra and dec)
    """
    url = "http://irsa.ipac.caltech.edu/cgi-bin/Cutouts/nph-cutouts?" + \
        "mission=COSMOS&max_size=180&locstr=%(ra).5f%%20%(dec).5f&sizeX=%(sizeX)d&ntable_cutouts=1&cutouttbl1=acs_mosaic_2.0&mode=PI" % \
        dict(ra=ra, dec=dec, sizeX=sizeX)

    response = urllib2.urlopen(url)
    html = response.read()

    root = ET.fromstring(html)

    status = root.get("status")
    if status != "ok":
        raise RuntimeError("Failed to open cutout URL: %s %s" % (status, root.get("message")))
    #
    # Find the cutouts
    #
    cutouts = root.find("images").find("cutouts")
    if not len(cutouts):
        html2 = urllib2.urlopen(root.find("summary").find("resultHtml").text.strip()).read()
        # fix things for ET
        html2 = re.sub(r"&(nbsp;|micro)", "", html2)

        root2 = ET.fromstring(html2)

        body = root2[1]
        try:
            msg = body[-1][0][3][0][0][-1][0][0].text # aaarghhhh
            msg = re.sub(r":\s*$", "", msg)
        except:
            msg = "Unable to retrieve cutout" + ":"
        print >> sys.stderr, "Failed to retrieve cutout %s" % re.sub(r"\s+", " ", msg)
        return afwImage.ExposureF(30,30)
    #
    # Read the fits data
    #
    try:
        fitsFD = urllib2.urlopen(cutouts[0].text.strip()) # 0: fits, 1: jpg
        fitsData = fitsFD.read()
    except Exception as e:
        fitsFD = None
    finally:
        del fitsFD
    #
    # and create an afwExposure
    #
    manager = MemFileManager(len(fitsData))
    memmove(manager.getData(), fitsData)

    return afwImage.ExposureF(manager)
Exemple #6
0
def getCosmosCutout(ra=150.23983, dec=+2.56283, sizeX=3):
    """Return an ACS COSMOS cutout from IRSA

    \param ra   Right ascension in decimal degrees (J2000)
    \param dec  Declination in decimal degrees (J2000)
    \param size Size of cutout (arcsec)

See http://irsa.ipac.caltech.edu/applications/Cutouts/docs/CutoutsProgramInterface.html but beware that (as of
2014-03-31) the locstr in the example isn't quite right (needs a %20 between the ra and dec)
    """

    url = "http://irsa.ipac.caltech.edu/cgi-bin/Cutouts/nph-cutouts?" + \
        "mission=COSMOS&max_size=180&locstr=%(ra).5f%%20%(dec).5f&sizeX=%(sizeX)d&ntable_cutouts=1&cutouttbl1=acs_mosaic_2.0&mode=PI" % \
        dict(ra=ra, dec=dec, sizeX=sizeX)

    response = urllib2.urlopen(url)
    html = response.read()

    root = ET.fromstring(html)

    status = root.get("status")
    if status != "ok":
        raise RuntimeError("Failed to open cutout URL: %s %s" % (status, root.get("message")))
    #
    # Find the cutouts
    #
    cutouts = root.find("images").find("cutouts")
    if not len(cutouts):
        html2 = urllib2.urlopen(root.find("summary").find("resultHtml").text.strip()).read()
        # fix things for ET
        html2 = re.sub(r"&(nbsp;|micro)", "", html2)

        root2 = ET.fromstring(html2)

        body = root2[1]
        try:
            msg = body[-1][0][3][0][0][-1][0][0].text # aaarghhhh
            msg = re.sub(r":\s*$", "", msg)
        except:
            msg = "Unable to retrieve cutout" + ":"
        print >> sys.stderr, "Failed to retrieve cutout %s" % re.sub(r"\s+", " ", msg)
        return afwImage.ExposureF(30,30)
    #
    # Read the fits data
    #
    try:
        fitsFD = urllib2.urlopen(cutouts[0].text.strip()) # 0: fits, 1: jpg
        fitsData = fitsFD.read()
    except Exception as e:
        fitsFD = None
    finally:
        del fitsFD
    #
    # and create an afwExposure
    #
    manager = MemFileManager(len(fitsData))
    memmove(manager.getData(), fitsData)

    return afwImage.ExposureF(manager)
Exemple #7
0
def unreduceFromFits(cls, data, size):
    """Unpickle from FITS

    Unpacks data produced by reduceToFits.

    Assumes the existence of a "readFits" method on the object.
    """
    manager = MemFileManager(size + 1) # Allow an extra char for nul
    memmove(manager.getData(), data)
    return cls.readFits(manager)
Exemple #8
0
def unreduceFromFits(cls, data, size):
    """Unpickle from FITS

    Unpacks data produced by reduceToFits.

    Assumes the existence of a "readFits" method on the object.
    """
    manager = MemFileManager(size)
    manager.setData(data, size)
    return cls.readFits(manager)
Exemple #9
0
def reduceToFits(obj):
    """Pickle to FITS

    Intended to be used by the __reduce__ method of a class.

    Assumes the existence of a "writeFits" method on the object.
    """
    manager = MemFileManager()
    obj.writeFits(manager)
    size = manager.getLength()
    data = cdata(manager.getData(), size);
    return (unreduceFromFits, (obj.__class__, data, size))
Exemple #10
0
def reduceToFits(obj):
    """Pickle to FITS

    Intended to be used by the __reduce__ method of a class.

    Assumes the existence of a "writeFits" method on the object.
    """
    manager = MemFileManager()
    options = ImageWriteOptions(
        ImageCompressionOptions(ImageCompressionOptions.NONE))
    try:
        obj.writeFits(manager, options)
    except Exception:
        obj.writeFits(manager)
    size = manager.getLength()
    data = manager.getData()
    return (unreduceFromFits, (obj.__class__, data, size))