Determine the transformation of an aperture diameter of 7.5 kpc at the redshift of abell 370 to pixels. First transform the diameter in absolute size (kpc) to apparent size (arcseconds). Then transform apparent size to pixels using WCS.


```python
import numpy as np 

from astropy.io import fits
from astropy import units as u 
from astropy import cosmology
from astropy.wcs import WCS
```

### Load Data


```python
input_data_path = "https://archive.stsci.edu/pub/hlsp/frontier/abell2744/images/hst/v1.0-epoch1/hlsp_frontier_hst_wfc3-60mas_abell2744_f105w_v1.0_drz.fits"

hdul = fits.open(input_data_path)
hdu = hdul[0]

data = hdu.data
header = hdu.header
wcs = WCS(header)
```

### Given


```python
# Given 
aperture_diameter = (7.5 * u.kpc).to(u.Mpc)
```

NED data for abell 2744:

http://ned.ipac.caltech.edu/byname?objname=Abell%202744&hconst=70.0&omegam=0.279&omegav=0.721&wmap=3&corr_z=1


```python
"""
Cosmology-Corrected Quantities [H0 = 70.0 km/sec/Mpc, Ωmatter = 0.279, Ωvacuum = 0.721]
[Redshift 0.307055 as corrected to the Reference Frame defined by the 3K CMB]
Type	Values
Luminosity Distance	1.6e+03 Mpc (m-M) = 41.02 mag
Angular-Size Distance	938 (m-M) = 39.86 mag
Co-Moving Radial Distance	1.23e+03 (m-M) = 40.44 mag
Co-Moving Tangential Distance	1.23e+03 (m-M) = 40.44 mag
Co-Moving Volume	7.72 Gpc^3
Light Travel-Time	3.499 Gyr
Age at Redshift 0.307055	10.245 Gyr
Age of Universe	13.744 Gyr
Scale (Cosmology Corrected)	4548 pc/arcsec = 4.548 kpc/arcsec = 272.85 kpc/arcmin = 16.37 Mpc/degree
Surface Brightness Dimming	Flux Density per Unit Area = 0.34263; Magnitude per Unit Area = 1.163 mag
""";
```


```python
abell2744_z =  0.30800
abell2744_distance = 1230 * u.Mpc # Comoving
```

### Equations

\begin{align}
\theta = \frac{(z+1) * Diameter}{Distance_{Comoving}} =  \frac{Diameter}{Distance_{Angular Diameter} } \\
\end{align}

### Compute angular diameter distance


```python
def compute_angular_diameter_distance(d, z):
    return d/(z+1)
```


```python
angular_diameter_distance = compute_angular_diameter_distance(abell2744_distance, abell2744_z)
angular_diameter_distance
```




$940.36697 \; \mathrm{Mpc}$




```python
# check astropy cosmology
cosmology.WMAP5.angular_diameter_distance(abell2744_z)
```




$937.71585 \; \mathrm{Mpc}$



### Find angular diameter


```python
angular_diameter = np.tan((aperture_diameter / angular_diameter_distance).value) * u.rad 
angular_diameter 
```




$7.9756098 \times 10^{-6} \; \mathrm{rad}$




```python
# check astropy cosmology
(cosmology.WMAP5.arcsec_per_kpc_proper(abell2744_z) * aperture_diameter.to('kpc')).to('rad')
```




$7.9981585 \times 10^{-6} \; \mathrm{rad}$



### Convert to arcsec


```python
angular_diameter = 0.7*u.arcsec#angular_diameter.to("arcsec")
angular_diameter
```




$0.7 \; \mathrm{{}^{\prime\prime}}$



### Find Pixel Size 

In this section we convert the angular size into pixel size. The way we do this is by adding the angular size to the `CRVAL` of the WCS and then converting that world coordinate to pixel values. After getting the new pixel values we subtract the `CRPIX` values to find the difference (angular size). Because RA-DEC is an equatorial coordinate system, I have decided its best to add the angular size to the Dec component of the WCS CRVAL (Dec axis is a great circle). It looks like the image `CRVAL` is sufficiently away from the pols of the celestial sphere, so no need to worry about loops around +/- 90 degrees.  


```python
# Load center pixel from data's WCS 

ra_0 = wcs.wcs.crval[0] * u.Unit(wcs.wcs.cunit[0])
dec_0 = wcs.wcs.crval[1] * u.Unit(wcs.wcs.cunit[1])

print(ra_0, ",",  dec_0)
```

    3.5832679 deg , -30.3880082 deg



```python
# Add angular_diameter to center Dec value 

ra_1  = ra_0
dec_1 = dec_0 + angular_diameter

print(ra_1, ",",  dec_1)
```

    3.5832679 deg , -30.387813755555555 deg



```python
# Convert ra_1, dec_1 into pixels

world = np.array([[ra_1.value, dec_1.value],], dtype=np.float64) * u.deg
pixcrd = wcs.wcs_world2pix(world, 1)

pixcrd
```




    array([[2675.00000008, 3086.66666198]])




```python
# Look at the center pixel values
wcs.wcs.crpix
```




    array([2675., 3075.])




```python
# Find the difference b/w the center pixel and the angular offset pixel:
pixel_diff =  pixcrd - wcs.wcs.crpix 
assert abs(pixel_diff[0,0]) < 1e-6
pixel_diff[0,1]
```




    11.666661982622372




```python
# Find ceiling of pixel diff to conclude pixel size
pixel_size = int(np.ceil(pixel_diff[0,1]))

pixel_size
```




    12




```python
# radius 
pixel_size // 2
```




    6




```python

```

8fcbc6d1b580db1256d8a84555b17894f73f81ed
ipynb
Jupyter Notebook
notebooks/cosmology/Aperture Diameter of 7.5 kpc at Abell 2744.ipynb
robelgeda/lcbg
5fb999c7cab17d1e516387314529ba54a2fd1df7
BSD-3-Clause
2017-09-20T14:29:39.000Z
2017-09-20T14:29:39.000Z
notebooks/cosmology/Aperture Diameter of 7.5 kpc at Abell 2744.ipynb
robelgeda/lcbg
5fb999c7cab17d1e516387314529ba54a2fd1df7
BSD-3-Clause
2020-03-13T16:42:53.000Z
2020-10-09T18:06:47.000Z
notebooks/cosmology/Aperture Diameter of 7.5 kpc at Abell 2744.ipynb
robelgeda/lcbg
5fb999c7cab17d1e516387314529ba54a2fd1df7
BSD-3-Clause
2018-07-13T19:47:08.000Z
2020-03-13T18:26:39.000Z
Qwen/Qwen-72B
1. YES
2. YES