Welcome to my lessons


Bo Zhang (NAOC, mailto:bozhang@nao.cas.cn) will have a few lessons on python.

  • These are very useful knowledge, skills and code styles when you use python to process astronomical data.
  • All materials can be found on my github page.
  • jupyter notebook (formerly named ipython notebook) is recommeded to use

These lectures are organized as below:

  1. install python
  2. basic syntax
  3. numerical computing
  4. scientific computing
  5. plotting
  6. astronomical data processing
  7. high performance computing
  8. version control

APLpy basics

aplpyreaddocs

- Introduction

APLpy : Astronomical Plotting Library in Python.

For convenient fits image or cubes plotting with astronomical coordinates

- Dependencies

APLpy is based on matplotlib, so any matplotlib originated functions/classes can be easily adopted.

For image reprojection or mosaicing purpose, APLpy relies on Montage, which is developed by IPAC team. To enable functions like making RGB cube, one should first install Montage package.

Montage package docs

- Tips

For plotting plenty of similar images, one could define a function to set up a set of standard properties (i.e. def standard_setup( )), such as axis and tick labels and use this user-defined function instead of the calling the APLpy functions time to time.

Of course, a different setting by directly calling APLpy functions will overide the settings after calling the user-defined standard setup function.


In [1]:
# import matplotlib.pyplot as plt
# import numpy as np
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import aplpy
from astropy.io import fits
import numpy as np
import matplotlib.pyplot as plt

def standard_setup(fig):
    fig.show_colorscale(cmap='gist_gray')
    fig.tick_labels.set_font(size='large')
    fig.frame.set_linewidth(1)
    fig.frame.set_color('black')
    fig.add_colorbar()
    fig.colorbar.show()
    fig.colorbar.set_location('top')
    fig.colorbar.set_font(size='medium', weight='medium', \
                          stretch='normal', family='sans-serif', \
                          style='normal', variant='normal')
    fig.list_layers()
    #fig.axis_labels.hide_y()
    #fig.tick_labels.hide_y()
    #fig.axis_labels.hide_x()
    #fig.tick_labels.hide_x()
  


dirpath = r'./data/wise_image/'
outpath = r'./data/wise_image/'

filepath1= dirpath+r'w4_cut.fits'
filepath2= dirpath+r'w3_cut.fits'
filepath3= dirpath+r'w2_cut.fits'

aplpy.make_rgb_cube([filepath3,filepath2,filepath1], outpath+r'wise234_RGB.fits')
aplpy.make_rgb_image(outpath+r'wise234_RGB.fits',
                     outpath+r'rgb_image_wise234.png',
                     stretch_r='linear', 
                     vmid_r=None,
                     pmin_r=10, 
                     pmax_r=98,
                     exponent_r=2,
                     vmin_g=None,
                     vmax_g=None,
                     pmin_g=10,
                     pmax_g=98,
                     stretch_g='linear',
                     vmid_g=None,
                     exponent_g=2,
                     vmin_b=None,
                     vmax_b=None,
                     pmin_b=10,
                     pmax_b=98,
                     stretch_b='linear',
                     vmid_b=None)
fits.setval(outpath+r'wise234_RGB.fits','CTYPE3',value='RGB')


# Launch APLpy figure of 2D cube
img = aplpy.FITSFigure(outpath+r'wise234_RGB.fits',dimensions=[0,1],slices=[2],figsize=(10,9))
standard_setup(img)
img.recenter(084.78475,+30.09747,width=0.12,height=0.12)
img.show_rgb(outpath+r'rgb_image_wise234.png')
img.colorbar.hide()
img.add_label(0.9,0.9, 'WISE 2/3/4\nRGB', color='black', relative=True, size='large',layer='source') 
img.add_scalebar(0.05,"1 pc",color='white', corner='bottom left')
#add 850 um continuum contour
img.show_contour(r'./data/G178_final.850.fits',levels=np.linspace(0.08,0.23,5),colors='white')
# img.save(outpath+r'rgb_W3_contour.pdf')
plt.show()


INFO:astropy:Deleting work directory /tmp/tmp4VisvR
INFO: Deleting work directory /tmp/tmp4VisvR [montage_wrapper.wrappers]
INFO
INFO:astropy:Deleting work directory /tmp/tmpt09YEd
: Deleting work directory /tmp/tmpt09YEd [montage_wrapper.wrappers]
INFO
INFO:astropy:Deleting work directory /tmp/tmpzSLAIG
: Deleting work directory /tmp/tmpzSLAIG [montage_wrapper.wrappers]
INFO
INFO:astropy:Red:
: Red: [aplpy.rgb]
INFO
INFO:astropy:vmin =  1.175e+01 (auto)
INFO:astropy:vmax =  2.671e+01 (auto)
INFO:astropy:Green:
: vmin =  1.175e+01 (auto) [aplpy.rgb]
INFO: vmax =  2.671e+01 (auto) [aplpy.rgb]
INFO: Green: [aplpy.rgb]
INFO
INFO:astropy:vmin =  7.930e+02 (auto)
INFO:astropy:vmax =  8.299e+02 (auto)
INFO:astropy:Blue:
: vmin =  7.930e+02 (auto) [aplpy.rgb]
INFO: vmax =  8.299e+02 (auto) [aplpy.rgb]
INFO: Blue: [aplpy.rgb]
INFO
INFO:astropy:vmin =  2.530e+02 (auto)
INFO:astropy:vmax =  2.548e+02 (auto)
: vmin =  2.530e+02 (auto) [aplpy.rgb]
INFO: vmax =  2.548e+02 (auto) [aplpy.rgb]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-f94e21cb7114> in <module>()
     48                      pmax_b=98,
     49                      stretch_b='linear',
---> 50                      vmid_b=None)
     51 fits.setval(outpath+r'wise234_RGB.fits','CTYPE3',value='RGB')
     52 

/usr/local/lib/python2.7/dist-packages/aplpy/rgb.pyc in make_rgb_image(data, output, indices, vmin_r, vmax_r, pmin_r, pmax_r, stretch_r, vmid_r, exponent_r, vmin_g, vmax_g, pmin_g, pmax_g, stretch_g, vmid_g, exponent_g, vmin_b, vmax_b, pmin_b, pmax_b, stretch_b, vmid_b, exponent_b, make_nans_transparent, embed_avm_tags)
    226 
    227         if output.lower().endswith(('.jpg', '.jpeg', '.png')):
--> 228             avm = AVM.from_header(header)
    229             avm.embed(output, output)
    230         else:

/usr/local/lib/python2.7/dist-packages/pyavm/avm.pyc in from_header(cls, header, include_full_header)
    518 
    519         wcs = WCS(header)
--> 520         self = cls.from_wcs(wcs)
    521 
    522         if include_full_header:

/usr/local/lib/python2.7/dist-packages/pyavm/avm.pyc in from_wcs(cls, wcs)
    549             raise Exception("Projections do not agree: %s / %s" % (proj1, proj2))
    550 
--> 551         self.Spatial.ReferenceDimension = [wcs.naxis1, wcs.naxis2]
    552         self.Spatial.ReferenceValue = wcs.wcs.crval.tolist()
    553         self.Spatial.ReferencePixel = wcs.wcs.crpix.tolist()

AttributeError: 'WCS' object has no attribute 'naxis1'

Healpy

aplpyreaddocs

- Introduction

To take care of fits files of Healpix pixelization schemes (http://healpix.sourceforge.net/downloads.php), including fitsfile io and transformation to standard coordinate systems.

This is particularly useful for all-sky survey such as Planck data, or SCUBA2 data release 1.

-


In [3]:
import os
import healpy as hp
from astropy.io import fits
import montage_wrapper as mt

#read the fitsfile
plankCO = hp.read_map('HFI_CompMap_CO-Type3_2048_R1.10.fits')#standard planck survey data from online archive
pixNum = 8192

#view the fitsfile in Cartesian projection
imageData = hp.cartview(plankCO, title='Histogram equalized Ecliptic', 
     unit='K km/s', min=1,max=500, cmap = 'gist_heat_r', 
     norm = 'log', cbar= False, return_projected_map = True, 
     xsize = pixNum)

#fits header editing
hdu = fits.PrimaryHDU(imageData.data)
hdu.header['CTYPE1']  = 'GLON-CAR'                                      
hdu.header['CRVAL1']  =         0.0                           
hdu.header['CDELT1']  =         -360.0/pixNum                        
hdu.header['CRPIX1']  =               pixNum/2.0+0.5                           
hdu.header['CTYPE2']  = 'GLAT-CAR'                                      
hdu.header['CRVAL2']  =         0.0                          
hdu.header['CDELT2']  =           360.0/pixNum                    
hdu.header['CRPIX2']  =              pixNum/4.0+0.5
hdu.header['BUNIT']   = 'K*km/s'
hdu.header['TELESCOP'] = 'Planck'
hdu.writeto('PlanckCO_Type3_car.fits', clobber= True)

#target fitsheader editing
hdrTAN = hdu.header.copy()
hdrTAN['CTYPE1']  = 'GLON-TAN'
hdrTAN['CTYPE2']  = 'GLAT-TAN'

#fitsheader and file output
#use mongtage as the reprojection engine, one could also implement other reprojection packages
if os.path.isfile('TAN.hdr'):
    os.remove('TAN.hdr')

hdrTAN.totextfile('TAN.hdr')

if os.path.isfile('PlanckCO_Type3_tan.fits'):
    os.remove('PlanckCO_Type3_tan.fits')

mt.reproject('PlanckCO_Type3_car.fits', 
    'PlanckCO_Type3_tan.fits', 
    header = 'TAN.hdr')


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-3-9de64c481a11> in <module>()
      5 
      6 #read the fitsfile
----> 7 plankCO = hp.read_map('HFI_CompMap_CO-Type3_2048_R1.10.fits')#standard planck survey data from online archive
      8 pixNum = 8192
      9 

/usr/local/lib/python2.7/dist-packages/healpy/fitsfunc.pyc in read_map(filename, field, dtype, nest, hdu, h, verbose, memmap)
    252       The map(s) read from the file, and the header if *h* is True.
    253     """
--> 254     hdulist=pf.open(filename, memmap=memmap)
    255     #print hdulist[1].header
    256     nside = hdulist[hdu].header.get('NSIDE')

/usr/local/lib/python2.7/dist-packages/astropy/io/fits/hdu/hdulist.pyc in fitsopen(name, mode, memmap, save_backup, cache, **kwargs)
    136         raise ValueError('Empty filename: %s' % repr(name))
    137 
--> 138     return HDUList.fromfile(name, mode, memmap, save_backup, cache, **kwargs)
    139 
    140 

/usr/local/lib/python2.7/dist-packages/astropy/io/fits/hdu/hdulist.pyc in fromfile(cls, fileobj, mode, memmap, save_backup, cache, **kwargs)
    278 
    279         return cls._readfrom(fileobj=fileobj, mode=mode, memmap=memmap,
--> 280                              save_backup=save_backup, cache=cache, **kwargs)
    281 
    282     @classmethod

/usr/local/lib/python2.7/dist-packages/astropy/io/fits/hdu/hdulist.pyc in _readfrom(cls, fileobj, data, mode, memmap, save_backup, cache, **kwargs)
    799             if not isinstance(fileobj, _File):
    800                 # instantiate a FITS file object (ffo)
--> 801                 ffo = _File(fileobj, mode=mode, memmap=memmap, cache=cache)
    802             else:
    803                 ffo = fileobj

/usr/local/lib/python2.7/dist-packages/astropy/io/fits/file.pyc in __init__(self, fileobj, mode, memmap, clobber, cache)
    139             self._open_fileobj(fileobj, mode, clobber)
    140         elif isinstance(fileobj, string_types):
--> 141             self._open_filename(fileobj, mode, clobber)
    142         else:
    143             self._open_filelike(fileobj, mode, clobber)

/usr/local/lib/python2.7/dist-packages/astropy/io/fits/file.pyc in _open_filename(self, filename, mode, clobber)
    491             self._file = bz2.BZ2File(self.name, bzip2_mode)
    492         else:
--> 493             self._file = fileobj_open(self.name, PYFITS_MODES[mode])
    494 
    495         # Make certain we're back at the beginning of the file

/usr/local/lib/python2.7/dist-packages/astropy/io/fits/util.pyc in fileobj_open(filename, mode)
    413         """
    414 
--> 415         return open(filename, mode)
    416 
    417 

IOError: [Errno 2] No such file or directory: 'HFI_CompMap_CO-Type3_2048_R1.10.fits'

In [ ]:


In [ ]: