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

Adding LTM, LTV parameters to trim_image #718

Open
ysBach opened this issue Dec 16, 2019 · 1 comment
Open

Adding LTM, LTV parameters to trim_image #718

ysBach opened this issue Dec 16, 2019 · 1 comment

Comments

@ysBach
Copy link
Contributor

ysBach commented Dec 16, 2019

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.

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

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.

@Russell-Ryan
Copy link

I just wanted to bump this comment. I think having the LTV/LTM keywords is extremely valuable to delineate between the physical/image coordinate systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants