Astropy

Units calculation

You may also refer to the following tutorial by AstoPy team http://www.astropy.org/astropy-tutorials/Quantities.html


In [1]:
from astropy import units as u
from astropy import constants as const

In [2]:
const.c


Out[2]:
$2.9979246 \times 10^{8} \; \mathrm{\frac{m}{s}}$

In [3]:
const.m_e


Out[3]:
$9.1093836 \times 10^{-31} \; \mathrm{kg}$

In [4]:
const.m_e.cgs


Out[4]:
$9.1093836 \times 10^{-28} \; \mathrm{g}$

In [5]:
10*u.g*const.c.cgs**2


Out[5]:
$8.9875518 \times 10^{21} \; \mathrm{\frac{cm^{2}\,g}{s^{2}}}$

In [6]:
u.erg


Out[6]:
$\mathrm{erg}$

In [7]:
u.erg.decompose()


Out[7]:
$\mathrm{1 \times 10^{-7}\,\frac{m^{2}\,kg}{s^{2}}}$

Load fits file


In [8]:
from astropy.io import fits
from astropy.utils.data import download_file

In [9]:
image_file = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True)

We can open the fits file by fits.open() and check the info of the fits file by .info()


In [10]:
hdu_list = fits.open(image_file)
hdu_list.info()


Filename: /home/yanyan/.astropy/cache/download/py3/2c9202ae878ecfcb60878ceb63837f5f
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     161   (891, 893)   int16   
  1  er.mask       1 TableHDU        25   1600R x 4C   [F6.2, F6.2, F6.2, F6.2]   

We get the data by .data


In [11]:
image_data = hdu_list[0].data
image_data


Out[11]:
array([[ 7201,  6642,  6642, ...,  9498,  9498, 10057],
       [ 6642,  6363,  6642, ..., 10057, 10616, 10616],
       [ 6922,  6642,  6922, ..., 10337, 11175, 10616],
       ...,
       [ 5412,  5132,  5412, ..., 13000, 12580, 12021],
       [ 5796,  5517,  5796, ..., 12546, 12546, 11987],
       [ 5796,  5796,  6076, ..., 11987, 12546, 12546]], dtype=int16)

We get the header by .header


In [12]:
image_header = hdu_list[0].header
image_header.items


Out[12]:
<bound method Header.items of SIMPLE  =                    T /FITS: Compliance                                
BITPIX  =                   16 /FITS: I*2 Data                                  
NAXIS   =                    2 /FITS: 2-D Image Data                            
NAXIS1  =                  891 /FITS: X Dimension                               
NAXIS2  =                  893 /FITS: Y Dimension                               
EXTEND  =                    T /FITS: File can contain extensions               
DATE    = '2014-01-09        '  /FITS: Creation Date                            
ORIGIN  = 'STScI/MAST'         /GSSS: STScI Digitized Sky Survey                
SURVEY  = 'SERC-ER '           /GSSS: Sky Survey                                
REGION  = 'ER768   '           /GSSS: Region Name                               
PLATEID = 'A0JP    '           /GSSS: Plate ID                                  
SCANNUM = '01      '           /GSSS: Scan Number                               
DSCNDNUM= '00      '           /GSSS: Descendant Number                         
TELESCID=                    4 /GSSS: Telescope ID                              
BANDPASS=                   36 /GSSS: Bandpass Code                             
COPYRGHT= 'AAO/ROE '           /GSSS: Copyright Holder                          
SITELAT =              -31.277 /Observatory: Latitude                           
SITELONG=              210.934 /Observatory: Longitude                          
TELESCOP= 'UK Schmidt - Doubl' /Observatory: Telescope                          
INSTRUME= 'Photographic Plate' /Detector: Photographic Plate                    
EMULSION= 'IIIaF   '           /Detector: Emulsion                              
FILTER  = 'OG590   '           /Detector: Filter                                
PLTSCALE=                67.20 /Detector: Plate Scale arcsec per mm             
PLTSIZEX=              355.000 /Detector: Plate X Dimension mm                  
PLTSIZEY=              355.000 /Detector: Plate Y Dimension mm                  
PLATERA =        85.5994550000 /Observation: Field centre RA degrees            
PLATEDEC=       -4.94660910000 /Observation: Field centre Dec degrees           
PLTLABEL= 'OR14052 '           /Observation: Plate Label                        
DATE-OBS= '1990-12-22T13:49:00' /Observation: Date/Time                         
EXPOSURE=                 65.0 /Observation: Exposure Minutes                   
PLTGRADE= 'AD2     '           /Observation: Plate Grade                        
OBSHA   =             0.158333 /Observation: Hour Angle                         
OBSZD   =              26.3715 /Observation: Zenith Distance                    
AIRMASS =              1.11587 /Observation: Airmass                            
REFBETA =        66.3196420000 /Observation: Refraction Coeff                   
REFBETAP=     -0.0820000000000 /Observation: Refraction Coeff                   
REFK1   =        6423.52290000 /Observation: Refraction Coeff                   
REFK2   =       -102122.550000 /Observation: Refraction Coeff                   
CNPIX1  =                12237 /Scan: X Corner                                  
CNPIX2  =                19965 /Scan: Y Corner                                  
XPIXELS =                23040 /Scan: X Dimension                               
YPIXELS =                23040 /Scan: Y Dimension                               
XPIXELSZ=              15.0295 /Scan: Pixel Size microns                        
YPIXELSZ=              15.0000 /Scan: Pixel Size microns                        
PPO1    =       -3069417.00000 /Scan: Orientation Coeff                         
PPO2    =       0.000000000000 /Scan: Orientation Coeff                         
PPO3    =        177500.000000 /Scan: Orientation Coeff                         
PPO4    =       0.000000000000 /Scan: Orientation Coeff                         
PPO5    =        3069417.00000 /Scan: Orientation Coeff                         
PPO6    =        177500.000000 /Scan: Orientation Coeff                         
PLTRAH  =                    5 /Astrometry: Plate Centre H                      
PLTRAM  =                   42 /Astrometry: Plate Centre M                      
PLTRAS  =                23.86 /Astrometry: Plate Centre S                      
PLTDECSN= '-       '           /Astrometry: Plate Centre +/-                    
PLTDECD =                    4 /Astrometry: Plate Centre D                      
PLTDECM =                   56 /Astrometry: Plate Centre M                      
PLTDECS =                 47.9 /Astrometry: Plate Centre S                      
EQUINOX =               2000.0 /Astrometry: Equinox                             
AMDX1   =        67.1550859799 /Astrometry: GSC1 Coeff                          
AMDX2   =      0.0431478884485 /Astrometry: GSC1 Coeff                          
AMDX3   =       -292.435619180 /Astrometry: GSC1 Coeff                          
AMDX4   =  -2.68934864702E-005 /Astrometry: GSC1 Coeff                          
AMDX5   =   1.99133423290E-005 /Astrometry: GSC1 Coeff                          
AMDX6   =  -2.37011931379E-006 /Astrometry: GSC1 Coeff                          
AMDX7   =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX8   =   2.21426387429E-006 /Astrometry: GSC1 Coeff                          
AMDX9   =  -8.12841581455E-008 /Astrometry: GSC1 Coeff                          
AMDX10  =   2.48169090021E-006 /Astrometry: GSC1 Coeff                          
AMDX11  =   2.77618933926E-008 /Astrometry: GSC1 Coeff                          
AMDX12  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX13  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX14  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX15  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX16  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX17  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX18  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX19  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDX20  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY1   =        67.1593591466 /Astrometry: GSC1 Coeff                          
AMDY2   =     -0.0471363749174 /Astrometry: GSC1 Coeff                          
AMDY3   =        316.004963520 /Astrometry: GSC1 Coeff                          
AMDY4   =   2.86798151430E-005 /Astrometry: GSC1 Coeff                          
AMDY5   =  -2.00968236347E-005 /Astrometry: GSC1 Coeff                          
AMDY6   =   2.27840393227E-005 /Astrometry: GSC1 Coeff                          
AMDY7   =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY8   =   2.23885090381E-006 /Astrometry: GSC1 Coeff                          
AMDY9   =  -2.28360163464E-008 /Astrometry: GSC1 Coeff                          
AMDY10  =   2.44828851495E-006 /Astrometry: GSC1 Coeff                          
AMDY11  =  -5.76717487998E-008 /Astrometry: GSC1 Coeff                          
AMDY12  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY13  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY14  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY15  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY16  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY17  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY18  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY19  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDY20  =       0.000000000000 /Astrometry: GSC1 Coeff                          
AMDREX1 =        67.1532034737 /Astrometry: GSC2 Coeff                          
AMDREX2 =      0.0434354199559 /Astrometry: GSC2 Coeff                          
AMDREX3 =       -292.435438892 /Astrometry: GSC2 Coeff                          
AMDREX4 =   4.60919247070E-006 /Astrometry: GSC2 Coeff                          
AMDREX5 =  -3.21138058537E-006 /Astrometry: GSC2 Coeff                          
AMDREX6 =   7.23651736725E-006 /Astrometry: GSC2 Coeff                          
AMDREX7 =       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX8 =       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX9 =       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX10=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX11=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX12=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX13=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX14=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX15=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX16=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX17=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX18=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX19=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREX20=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY1 =        67.1522589487 /Astrometry: GSC2 Coeff                          
AMDREY2 =     -0.0481758265285 /Astrometry: GSC2 Coeff                          
AMDREY3 =        315.995683716 /Astrometry: GSC2 Coeff                          
AMDREY4 =  -7.47397531230E-006 /Astrometry: GSC2 Coeff                          
AMDREY5 =   9.55221105409E-007 /Astrometry: GSC2 Coeff                          
AMDREY6 =   7.60954485251E-006 /Astrometry: GSC2 Coeff                          
AMDREY7 =       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY8 =       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY9 =       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY10=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY11=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY12=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY13=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY14=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY15=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY16=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY17=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY18=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY19=       0.000000000000 /Astrometry: GSC2 Coeff                          
AMDREY20=       0.000000000000 /Astrometry: GSC2 Coeff                          
ASTRMASK= 'er.mask '           /Astrometry: GSC2 Mask                           
WCSAXES =                    2 /GetImage: Number WCS axes                       
WCSNAME = 'DSS               ' /GetImage: Local WCS approximation from full plat
RADESYS = 'ICRS              ' /GetImage: GSC-II calibration using ICRS system  
CTYPE1  = 'RA---TAN          ' /GetImage: RA-Gnomic projection                  
CRPIX1  =           446.000000 /GetImage: X reference pixel                     
CRVAL1  =            85.274970 /GetImage: RA of reference pixel                 
CUNIT1  = 'deg               ' /GetImage: degrees                               
CTYPE2  = 'DEC--TAN          ' /GetImage: Dec-Gnomic projection                 
CRPIX2  =           447.000000 /GetImage: Y reference pixel                     
CRVAL2  =            -2.458265 /GetImage: Dec of reference pixel                
CUNIT2  = 'deg               ' /Getimage: degrees                               
CD1_1   =        -0.0002802651 /GetImage: rotation matrix coefficient           
CD1_2   =         0.0000003159 /GetImage: rotation matrix coefficient           
CD2_1   =         0.0000002767 /GetImage: rotation matrix coefficient           
CD2_2   =         0.0002798187 /GetImage: rotation matrix coefficient           
OBJECT  = 'data              ' /GetImage: Requested Object Name                 
DATAMIN =                 3759 /GetImage: Minimum returned pixel value          
DATAMAX =                22918 /GetImage: Maximum returned pixel value          
OBJCTRA = '05 41 06.000      ' /GetImage: Requested Right Ascension (J2000)     
OBJCTDEC= '-02 27 30.00      ' /GetImage: Requested Declination (J2000)         
OBJCTX  =             12682.48 /GetImage: Requested X on plate (pixels)         
OBJCTY  =             20411.37 /GetImage: Requested Y on plate (pixels)         >

Image header is similar to a dictionary, we can get individual header items by putting correspoding field:


In [13]:
image_header['CRVAL1'], image_header['CRVAL2']


Out[13]:
(85.27497, -2.458265)

Check the image in fits file


In [14]:
import matplotlib.pyplot as plt
%matplotlib inline

In [15]:
plt.imshow(image_data)


Out[15]:
<matplotlib.image.AxesImage at 0x7f6b566a2cf8>

In [16]:
plt.imshow(image_data,cmap='gray')


Out[16]:
<matplotlib.image.AxesImage at 0x7f6b564466a0>

In [17]:
plt.imshow(image_data,cmap='gray_r')


Out[17]:
<matplotlib.image.AxesImage at 0x7f6b56435470>

In [18]:
# Noted the image is upside down
import numpy as np
plt.imshow(image_data,origin="lower")


Out[18]:
<matplotlib.image.AxesImage at 0x7f6b56390240>

AplPy


In [20]:
import aplpy


/home/yanyan/anaconda3/lib/python3.7/site-packages/mpl_toolkits/axes_grid/__init__.py:12: MatplotlibDeprecationWarning: 
The mpl_toolkits.axes_grid module was deprecated in Matplotlib 2.1 and will be removed two minor releases later. Use mpl_toolkits.axes_grid1 and mpl_toolkits.axisartist, which provide the same functionality instead.
  obj_type='module')

In [21]:
f = aplpy.FITSFigure(image_file,figsize=(16,9), dpi=100)
f.show_colorscale(cmap='gray')


WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]
WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]
INFO: Auto-setting vmin to  3.634e+03 [aplpy.core]
INFO: Auto-setting vmax to  1.940e+04 [aplpy.core]

In [22]:
# vim and vmax set the range of values to be shown
f = aplpy.FITSFigure(image_file,figsize=(16,9), dpi=100)
f.show_colorscale(cmap='jet', vmin=3e3, vmax=3e4)


WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]
WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]

In [23]:
f = aplpy.FITSFigure(image_file,figsize=(16,9), dpi=100)
# Background image
f.show_colorscale(cmap='gray')
# Contour
f.show_contour(image_file,levels=10,cmap='jet',smooth=3,linewidths=1.0)
# Title and colourbar
f.set_title('Horsehead Nebula')
f.add_colorbar()


WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]
WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]
INFO: Auto-setting vmin to  3.634e+03 [aplpy.core]
INFO: Auto-setting vmax to  1.940e+04 [aplpy.core]
WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]
WARNING: Cannot determine equinox. Assuming J2000. [aplpy.wcs_util]

In [ ]: