ccdproc icon indicating copy to clipboard operation
ccdproc copied to clipboard

Adding LTM, LTV parameters to trim_image

Open ysBach opened this issue 5 years ago • 1 comments

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.

ysBach avatar Dec 16 '19 11:12 ysBach

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.

Russell-Ryan avatar Sep 21 '22 13:09 Russell-Ryan