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

Hips2fits new service #1734

Merged
merged 11 commits into from
Dec 25, 2020
44 changes: 44 additions & 0 deletions astroquery/hips2fits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
CDS hips2fits Query Tool
------------------------

:Author: Matthieu Baumann (matthieu.baumann@astro.unistra.fr)

This package is for querying the CDS hips2fits service, primarily hosted at:

* http://alasky.u-strasbg.fr/hips-image-services/hips2fits
* http://alaskybis.u-strasbg.fr/hips-image-services/hips2fits (mirror)

Note: If the access to hips2fits was helpful for your research
work, the following acknowledgment would be appreciated::

This research has made use of the hips2fits, a tool developed at CDS, Strasbourg, France aiming at extracting
FITS images from HiPS sky maps with respect to a WCS.
"""

from astropy import config as _config


class Conf(_config.ConfigNamespace):
"""
Configuration parameters for ``astroquery.template_module``.
"""
server = _config.ConfigItem(
["http://alasky.u-strasbg.fr/hips-image-services/hips2fits",
"http://alaskybis.u-strasbg.fr/hips-image-services/hips2fits"],
'Name of the template_module server to use.')

timeout = _config.ConfigItem(
30,
'Time limit for connecting to template_module server.')


conf = Conf()

from .core import hips2fits, hips2fitsClass

__all__ = [
'hips2fits', 'hips2fitsClass',
'Conf', 'conf',
]
404 changes: 404 additions & 0 deletions astroquery/hips2fits/core.py

Large diffs are not rendered by default.

Empty file.
68 changes: 68 additions & 0 deletions astroquery/hips2fits/tests/test_hips2fits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
import pytest

from ..core import hips2fits

from astropy import wcs as astropy_wcs
from astropy.io import fits
import numpy as np

from astropy.utils.exceptions import AstropyUserWarning
from matplotlib.colors import Colormap

from astropy.coordinates import Angle, Longitude, Latitude
import astropy.units as u


class TestHips2fitsRemote(object):

# Create a new WCS astropy object
w = astropy_wcs.WCS(header={
'NAXIS1': 2000, # Width of the output fits/image
'NAXIS2': 1000, # Height of the output fits/image
'WCSAXES': 2, # Number of coordinate axes
'CRPIX1': 1000.0, # Pixel coordinate of reference point
'CRPIX2': 500.0, # Pixel coordinate of reference point
'CDELT1': -0.18, # [deg] Coordinate increment at reference point
'CDELT2': 0.18, # [deg] Coordinate increment at reference point
'CUNIT1': 'deg', # Units of coordinate increment and value
'CUNIT2': 'deg', # Units of coordinate increment and value
'CTYPE1': 'GLON-MOL', # galactic longitude, Mollweide's projection
'CTYPE2': 'GLAT-MOL', # galactic latitude, Mollweide's projection
'CRVAL1': 0.0, # [deg] Coordinate value at reference point
'CRVAL2': 0.0, # [deg] Coordinate value at reference point
})
hips = 'CDS/P/DSS2/red'

def test_query_jpg(self):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
get_query_payload=True,
format='jpg',
min_cut=0.5,
max_cut=99.5,
cmap=Colormap('viridis'),
)

# We must get a numpy array with 3 dimensions, and the last one should be of size 3 (RGB)
assert result["format"] == 'jpg' and result["hips"] == "CDS/P/DSS2/red"

def test_query_no_wcs_fits(self):
result = hips2fits.query(
hips=self.hips,
get_query_payload=True,
width=1000,
height=500,
fov=Angle(20 * u.deg),
ra=Longitude(0 * u.deg),
dec=Latitude(20 * u.deg),
projection="TAN",
min_cut=0.5,
max_cut=99.5,
cmap=Colormap('viridis'),
)

# We must get a numpy array with 3 dimensions, and the last one should be of size 3 (RGB)
assert result["format"] == 'fits' and result["hips"] == "CDS/P/DSS2/red"
165 changes: 165 additions & 0 deletions astroquery/hips2fits/tests/test_hips2fits_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# -*- coding: utf-8 -*

# Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
import pytest

from ..core import hips2fits

from astropy import wcs as astropy_wcs
from astropy.io import fits
import numpy as np

from astropy.utils.exceptions import AstropyUserWarning
from matplotlib.colors import Colormap


@pytest.mark.remote_data
class TestHips2fitsRemote(object):

# Create a new WCS astropy object
w = astropy_wcs.WCS(header={
'NAXIS1': 2000, # Width of the output fits/image
'NAXIS2': 1000, # Height of the output fits/image
'WCSAXES': 2, # Number of coordinate axes
'CRPIX1': 1000.0, # Pixel coordinate of reference point
'CRPIX2': 500.0, # Pixel coordinate of reference point
'CDELT1': -0.18, # [deg] Coordinate increment at reference point
'CDELT2': 0.18, # [deg] Coordinate increment at reference point
'CUNIT1': 'deg', # Units of coordinate increment and value
'CUNIT2': 'deg', # Units of coordinate increment and value
'CTYPE1': 'GLON-MOL', # galactic longitude, Mollweide's projection
'CTYPE2': 'GLAT-MOL', # galactic latitude, Mollweide's projection
'CRVAL1': 0.0, # [deg] Coordinate value at reference point
'CRVAL2': 0.0, # [deg] Coordinate value at reference point
})
hips = 'CDS/P/DSS2/red'

def test_query_jpg(self):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
get_query_payload=False,
format='jpg',
min_cut=0.5,
max_cut=99.5,
cmap=Colormap('viridis'),
)

# import matplotlib.cm as cm
# import matplotlib.pyplot as plt

# im = plt.imshow(result)
# plt.show(im)

# We must get a numpy array with 3 dimensions, and the last one should be of size 3 (RGB)
assert isinstance(result, np.ndarray) and result.shape[2] == 3

def test_query_jpg_no_wcs(self):
from astropy.coordinates import Longitude
from astropy.coordinates import Latitude
from astropy.coordinates import Angle
import astropy.units as u

result = hips2fits.query(
hips=self.hips,
width=1000,
height=500,
fov=Angle(20 * u.deg),
ra=Longitude(0 * u.deg),
dec=Latitude(20 * u.deg),
projection="TAN",
get_query_payload=False,
format='jpg',
min_cut=0.5,
max_cut=99.5,
cmap=Colormap('viridis'),
)

import matplotlib.cm as cm
import matplotlib.pyplot as plt

im = plt.imshow(result)
plt.show(im)

# We must get a numpy array with 3 dimensions, and the last one should be of size 3 (RGB)
assert isinstance(result, np.ndarray) and result.shape[2] == 3

def test_bad_strech(self):
with pytest.raises(AttributeError):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
get_query_payload=False,
format='jpg',
stretch="azs",
min_cut=0.5,
max_cut=99.5,
cmap=Colormap('viridis'),
)

def test_query_fits(self):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
get_query_payload=False,
)

assert isinstance(result, fits.HDUList)

def test_query_fits(self):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
# Here we send additional keywords incompatible with
# the fits output format
min_cut=1.5,
max_cut=96,
stretch='asinh',
cmap='twilight',
)

def test_query_png(self):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
get_query_payload=False,
format='png'
)
# We must get a numpy array with 3 dimensions, and the last one should be of size 4 (RGBA)
assert isinstance(result, np.ndarray) and result.shape[2] == 4

def test_bad_format_asked(self):
with pytest.raises(AttributeError):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=self.w,
get_query_payload=False,
format='sdfsg'
)

def test_wcs_having_no_naxis(self):
w2 = astropy_wcs.WCS(header={
'WCSAXES': 2, # Number of coordinate axes
'CRPIX1': 1000.0, # Pixel coordinate of reference point
'CRPIX2': 500.0, # Pixel coordinate of reference point
'CDELT1': -0.18, # [deg] Coordinate increment at reference point
'CDELT2': 0.18, # [deg] Coordinate increment at reference point
'CUNIT1': 'deg', # Units of coordinate increment and value
'CUNIT2': 'deg', # Units of coordinate increment and value
'CTYPE1': 'GLON-MOL', # galactic longitude, Mollweide's projection
'CTYPE2': 'GLAT-MOL', # galactic latitude, Mollweide's projection
'CRVAL1': 0.0, # [deg] Coordinate value at reference point
'CRVAL2': 0.0, # [deg] Coordinate value at reference point
})

with pytest.raises(AttributeError):
result = hips2fits.query_with_wcs(
hips=self.hips,
wcs=w2,
get_query_payload=False,
format='jpg',
min_cut=0.5,
max_cut=99.5,
cmap=Colormap('viridis'),
)
113 changes: 113 additions & 0 deletions docs/hips2fits/hips2fits.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
.. doctest-skip-all

.. _astroquery.hips2fits:

******************************************
HiPS2fits Service (`astroquery.hips2fits`)
******************************************

Getting started
===============

Query the `CDS hips2fits service <http://alasky.u-strasbg.fr/hips-image-services/hips2fits>`_

The `CDS hips2fits service <http://alasky.u-strasbg.fr/hips-image-services/hips2fits>`_ offers a way
to extract FITS images from HiPS sky maps. HiPS is an IVOA standard that combines individual images in
order to produce a progressive hierarchical sky map describing the whole survey. Please refer to the
`IVOA paper <http://www.ivoa.net/documents/HiPS/20170519/REC-HIPS-1.0-20170519.pdf>`_ for more info.

Given an astropy user-defined WCS with a HiPS name,
hips2fits will return you the corresponding FITS image (JPG/PNG output formats are also implemented).

See the list of valid HiPS names hosted in CDS `here <http://aladin.unistra.fr/hips/list>`_. It is also
possible to use :meth:`astroquery.cds.CdsClass.find_datasets` to retrieve the list of HiPSes matching an expression
(e.g. \*HST\* will return the names of the HST surveys HiPSes).

This package implements two methods:

* :meth:`~astroquery.hips2fits.hips2fitsClass.query_with_wcs` extracting a FITS image from a HiPS and an astropy ``wcs.WCS``.
See `here <http://aladin.unistra.fr/hips/list>`_ all the valid HiPS names hosted in CDS.
* :meth:`~astroquery.hips2fits.hips2fitsClass.query` extracting a FITS image from a HiPS given the output image pixel size, the center of projection, the type of projection and the field of view.
See `here <http://aladin.unistra.fr/hips/list>`_ all the valid HiPS names hosted in CDS.


Examples
========

With a user defined astropy WCS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python

>>> from astroquery.hips2fits import hips2fits
>>> import matplotlib.pyplot as plt
>>> from matplotlib.colors import Colormap
>>> from astropy import wcs as astropy_wcs
>>> # Create a new WCS astropy object
>>> w = astropy_wcs.WCS(header={
... 'NAXIS1': 2000, # Width of the output fits/image
... 'NAXIS2': 1000, # Height of the output fits/image
... 'WCSAXES': 2, # Number of coordinate axes
... 'CRPIX1': 1000.0, # Pixel coordinate of reference point
... 'CRPIX2': 500.0, # Pixel coordinate of reference point
... 'CDELT1': -0.18, # [deg] Coordinate increment at reference point
... 'CDELT2': 0.18, # [deg] Coordinate increment at reference point
... 'CUNIT1': 'deg', # Units of coordinate increment and value
... 'CUNIT2': 'deg', # Units of coordinate increment and value
... 'CTYPE1': 'GLON-MOL', # galactic longitude, Mollweide's projection
... 'CTYPE2': 'GLAT-MOL', # galactic latitude, Mollweide's projection
... 'CRVAL1': 0.0, # [deg] Coordinate value at reference point
... 'CRVAL2': 0.0, # [deg] Coordinate value at reference point
... })
>>> hips = 'CDS/P/DSS2/red'
>>> result = hips2fits.query_with_wcs(
... hips=hips,
... wcs=w,
... get_query_payload=False,
... format='jpg',
... min_cut=0.5,
... max_cut=99.5,
... cmap=Colormap('viridis'),
... )
>>> im = plt.imshow(result)
>>> plt.show(im)

.. image:: ./query_wcs.png

Without WCS
~~~~~~~~~~~

>>> from astroquery.hips2fits import hips2fits
>>> import matplotlib.pyplot as plt
>>> from matplotlib.colors import Colormap
>>> import astropy.units as u
>>> from astropy.coordinates import Longitude, Latitude, Angle
>>> hips = 'CDS/P/DSS2/red'
>>> result = hips2fits.query(
... hips=hips,
... width=1000,
... height=500,
... ra=Longitude(0 * u.deg),
... dec=Latitude(20 * u.deg),
... fov=Angle(80 * u.deg),
... projection="AIT",
... get_query_payload=False,
... format='jpg',
... min_cut=0.5,
... max_cut=99.5,
... cmap=Colormap('viridis'),
... )
>>> im = plt.imshow(result)
>>> plt.show(im)

.. image:: ./query_no_wcs.png

Reference/API
=============

.. automodapi:: astroquery.hips2fits
:no-inheritance-diagram:


.. _hips2fits: http://alasky.u-strasbg.fr/hips-image-services/hips2fits

Binary file added docs/hips2fits/query_no_wcs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/hips2fits/query_wcs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading