astropy / astropy/ccdproc

Adding LTM, LTV parameters to trim_image

Open
#718 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
93
Forks
92
Avg merge
14h 44m
Merged PRs (30d)
30

Description

Often I have to crop images, but the ``Physical`` coordinate, which is understood by SAO ds9, is missing when I use ``ccdproc``.

I have my personal codes, which include image trimming and adds the IRAF-like LTV/LTM, and mimics IRAF ``IMCOPY``. I guess it'll be better if such header keywords are included in ccdproc by default.

The following code simply makes a FITS file, trims it, and adds LTM/LTV keys.

```python
import numpy as np
import ccdproc
from ccdproc import trim_image

def fitsxy2py(fits_section):
slicer = ccdproc.utils.slices.slice_from_string
sl = slicer(fits_section, fits_convention=True)
return sl

def trim_ccd(ccd, fits_section=None, add_keyword=True):
trimmed_ccd = trim_image(ccd, fits_section=fits_section,
add_keyword=add_keyword)
ny, nx = ccd.data.shape
if fits_section:
trim_slice = fitsxy2py(fits_section)
ltv1 = -1*trim_slice[1].indices(nx)[0]
ltv2 = -1*trim_slice[0].indices(ny)[0]
else:
ltv1 = 0.
ltv2 = 0.

trimmed_ccd.header["LTV1"] = ltv1
trimmed_ccd.header["LTV2"] = ltv2
trimmed_ccd.header["LTM1_1"] = 1.
trimmed_ccd.header["LTM2_2"] = 1.
return trimmed_ccd

np.random.seed(123)
test = CCDData(np.random.normal(size=(100,100)), unit='adu')
trim_section = "[:10, 90:]"
trim_slice = fitsxy2py(trim_section)
test.data[trim_slice] += 10

test_trim = trim_ccd(test, trim_section)

test.write("test.fits", overwrite=True)
test_trim.write("test_trim.fits", overwrite=True)

```

As can be seen, the ``Physical`` WCS works correctly on SAO ds9 (the cursor was at the lower-left corner of the trimmed image):
![image](https://user-images.githubusercontent.com/17214293/70904891-e0870a00-2045-11ea-900d-27a997b872d7.png)

```python
import astropy
print(astropy.__version__)
import ccdproc
print(ccdproc.__version__)
import numpy
print(numpy.__version__)
# 3.2.3
# 2.0.1
# 1.17.4
```

**EDIT**: XY order was wrong in the original version of this issue... I corrected it.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.