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

Dask reproject #845

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions spectral_cube/dask_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,80 @@ def convfunc(img, **kwargs):
accepts_chunks=True,
**kwargs).with_beam(beam, raise_error_jybm=False)

def reproject(self, header, order='bilinear', use_memmap=False,
filled=True, **kwargs):
"""
Spatially reproject the cube into a new header. Fills the data with
the cube's ``fill_value`` to replace bad values before reprojection.

If you want to reproject a cube both spatially and spectrally, you need
to use `spectral_interpolate` as well.

.. warning::
The current implementation of ``reproject`` requires that the whole
cube be loaded into memory. Issue #506 notes that this is a
problem, and it is on our to-do list to fix.

Parameters
----------
header : `astropy.io.fits.Header`
A header specifying a cube in valid WCS
order : int or str, optional
The order of the interpolation (if ``mode`` is set to
``'interpolation'``). This can be either one of the following
strings:

* 'nearest-neighbor'
* 'bilinear'
* 'biquadratic'
* 'bicubic'

or an integer. A value of ``0`` indicates nearest neighbor
interpolation.
use_memmap : bool
If specified, a memory mapped temporary file on disk will be
written to rather than storing the intermediate spectra in memory.
filled : bool
Fill the masked values with the cube's fill value before
reprojection? Note that setting ``filled=False`` will use the raw
data array, which can be a workaround that prevents loading large
data into memory.
kwargs : dict
Passed to `reproject.reproject_interp`.
"""

try:
from reproject.version import version
except ImportError:
raise ImportError("Requires the reproject package to be"
" installed.")

# Need version > 0.12 to work with dask
from distutils.version import LooseVersion
if LooseVersion(version) < "0.12":
raise Warning("Requires version >=0.12 of reproject. The current "
"version is: {}".format(version))

from reproject import reproject_interp

# TODO: Find the minimal subcube that contains the header and only reproject that
# (see FITS_tools.regrid_cube for a guide on how to do this)

newwcs = wcs.WCS(header)
shape_out = tuple([header['NAXIS{0}'.format(i + 1)] for i in
range(header['NAXIS'])][::-1])

newcube, newcube_valid = reproject_interp((self._get_filled_data() if filled else self._data, self.header),
newwcs, shape_out=shape_out, block_size=(256, 256, 256), return_type='dask',
)

return self._new_cube_with(data=newcube,
wcs=newwcs,
mask=BooleanArrayMask(newcube_valid.astype('bool'),
newwcs),
meta=self.meta,
)


class DaskVaryingResolutionSpectralCube(DaskSpectralCubeMixin, VaryingResolutionSpectralCube):

Expand Down
Loading