Astro Example

Cosmic cleanning of an image composed by 12 Amplifiyers... Warning: gemnini biased!


In [13]:
# Astro-example

####################
#Cosmic cleanning of an image of 12 detectors
#Uses L.A. Cosmic from P. van Dokkum 
#Requires python version of la_cosmic
#http://www.astro.yale.edu/dokkum/lacosmic/
#it also uses Parallel python PP
##################

#
# La_cosmic must be in the same directory
#
#

import sys, os, string

import astropy
from astropy.io import fits
import numpy as np
import pyfits
import scipy
from scipy import ndimage

# IMPORTING La Cosmic
import cosmics
#time 
import time

# Paralel python
import pp

import matplotlib.pyplot as plt
%matplotlib inline

The image of this example is an IFU science exposure taken with GMOS Hamamatsu CCD: I.e. 12-amp mode readou. Also the image has bias and flat field corrected, but has not beeing mosaiced. So it is a .fits[sci] file with 12 extensions


In [14]:
imagen_in = "python_club_gemini.fits"

def separa_Amps(imagen_a_corregir):
    #header = pyfits.getheader(imagen_a_corregir)
    # Creating arrays
    imagen = []
    header_amp = []
    Amp = []

    for i in range(1, 13):
        print i
        # Reading each amp. 
        # and creating 12 images, where each one of them corresponds to an Amplifier 
        header_amp.append(pyfits.getheader(imagen_a_corregir, 'sci', i))
        imagen.append(pyfits.getdata(imagen_a_corregir, 'sci', i))
        Amp.append(str(imagen_a_corregir[:-5] + "_Amp_" + str(i) + ".fits"))

    for k in range(0, 12):
        print "writing", Amp[k]
        pyfits.writeto(Amp[k], imagen[k], header_amp[k])

    return

In [15]:
print "Splittings Amps"

start_time1 = time.time()

separa_Amps(imagen_in)

start_time2 = time.time()


Splittings Amps
1
2
3
4
5
6
7
8
9
10
11
12
writing python_club_gemini_Amp_1.fits
writing python_club_gemini_Amp_2.fits
writing python_club_gemini_Amp_3.fits
writing python_club_gemini_Amp_4.fits
writing python_club_gemini_Amp_5.fits
writing python_club_gemini_Amp_6.fits
writing python_club_gemini_Amp_7.fits
writing python_club_gemini_Amp_8.fits
writing python_club_gemini_Amp_9.fits
writing python_club_gemini_Amp_10.fits
writing python_club_gemini_Amp_11.fits
writing python_club_gemini_Amp_12.fits

In [16]:
image_data=pyfits.getdata("python_club_gemini_Amp_7.fits")
print type(image_data)
print image_data.shape


<type 'numpy.ndarray'>
(4176, 512)

In [17]:
from matplotlib.colors import LogNorm

plt.imshow(image_data,cmap="gray",aspect='auto',norm=LogNorm(vmin=30.08,vmax=1000.1,clip=True))
plt.colorbar()


Out[17]:
<matplotlib.colorbar.Colorbar at 0x112e78d50>

limpia_cosmicos calls la_cosmic from P. van Dokkum


In [18]:
def limpia_cosmicos(imagen_a_corregir, N_Amp):
    # imagen_a_corregir=imagen_in
    #la cosmic routine
    import cosmics

    array, header = cosmics.fromfits(str(imagen_a_corregir[:-5] + "_Amp_" + str(N_Amp) + ".fits"))
    gain_h = header["GAIN"]  # READING GAIN AND NOISE FROM HEADERS
    rnoise = header["RDNOISE"]
    print "READING GAIN AND NOISE ", gain_h, rnoise, str(imagen_a_corregir[:-5] + "_Amp_" + str(N_Amp) + ".fits")

    #Edit this for SCI or STD or Images
    c = cosmics.cosmicsimage(array, gain=gain_h, readnoise=rnoise, sigclip=3, sigfrac=1, objlim=2.0)

    # There are other options, check the manual  la_cosmnic 

    # Run the full artillery 7 iterations is enough:
    c.run(maxiter=7)

    # Write the cleaned image into a new FITS file, conserving the original header :
    cleaned = str(imagen_a_corregir[:-5] + "_CC2_" + str(N_Amp) + ".fits")
    cosmics.tofits(cleaned, c.cleanarray, header)

In [19]:
start_time1 = time.time()
start_time2 = time.time()

#print "iterations for cosmic rejection..."


ppservers = ()

ncpus = 4
# Creates jobserver with ncpus workers
job_server = pp.Server(ncpus, ppservers=ppservers)

print "Starting pp with", job_server.get_ncpus(), "workers aka CPUs"


#Fuerza Bruta....
Amplific= (1,2,3,4,5,6,7,8,9,10,11,12)
jobs=[(input,job_server.submit(limpia_cosmicos,(imagen_in,input,))) for input in Amplific]
for input, job in jobs:
    print "executing job N", input, "is", job()
job_server.print_stats()


print "Total  time elapsed: ", time.time() - start_time2, "s"


Starting pp with 4 workers aka CPUs
executing job N 1 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 4.03 python_club_gemini_Amp_1.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 0 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
cosmics.py:447: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
cosmics.py:500: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
17288 cosmic pixels (17288 new)
cosmics.py:235: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
Iteration 2
1948 cosmic pixels (1936 new)
Iteration 3
360 cosmic pixels (352 new)
Iteration 4
71 cosmic pixels (63 new)
Iteration 5
16 cosmic pixels (8 new)
Iteration 6
9 cosmic pixels (1 new)
Iteration 7
8 cosmic pixels (0 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_1.fits
None
Time elapsed parallel:  91.2477149963 s
executing job N 2 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 4.25 python_club_gemini_Amp_2.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 0 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
cosmics.py:447: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
cosmics.py:500: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
14373 cosmic pixels (14373 new)
cosmics.py:235: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
Iteration 2
2639 cosmic pixels (2634 new)
Iteration 3
600 cosmic pixels (595 new)
Iteration 4
131 cosmic pixels (126 new)
Iteration 5
34 cosmic pixels (29 new)
Iteration 6
18 cosmic pixels (13 new)
Iteration 7
6 cosmic pixels (1 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_2.fits
None
Time elapsed parallel:  91.2484061718 s
executing job N 3 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 4.0 python_club_gemini_Amp_3.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 1 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
cosmics.py:447: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
cosmics.py:500: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
12264 cosmic pixels (12264 new)
cosmics.py:235: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
Iteration 2
1855 cosmic pixels (1849 new)
Iteration 3
395 cosmic pixels (391 new)
Iteration 4
98 cosmic pixels (92 new)
Iteration 5
27 cosmic pixels (21 new)
Iteration 6
12 cosmic pixels (5 new)
Iteration 7
7 cosmic pixels (0 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_3.fits
None
Time elapsed parallel:  91.2486431599 s
executing job N 4 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 4.24 python_club_gemini_Amp_4.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 0 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
cosmics.py:447: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
cosmics.py:500: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
10422 cosmic pixels (10422 new)
cosmics.py:235: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  if self.satstars != None:
Iteration 2
1836 cosmic pixels (1833 new)
Iteration 3
732 cosmic pixels (727 new)
Iteration 4
324 cosmic pixels (304 new)
Iteration 5
130 cosmic pixels (107 new)
Iteration 6
64 cosmic pixels (24 new)
Iteration 7
38 cosmic pixels (2 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_4.fits
None
Time elapsed parallel:  91.2488350868 s
executing job N 5 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.8 python_club_gemini_Amp_5.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 1 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
13716 cosmic pixels (13716 new)
Iteration 2
4993 cosmic pixels (4423 new)
Iteration 3
3542 cosmic pixels (2494 new)
Iteration 4
2427 cosmic pixels (1587 new)
Iteration 5
1981 cosmic pixels (1083 new)
Iteration 6
1686 cosmic pixels (674 new)
Iteration 7
1481 cosmic pixels (417 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_5.fits
None
Time elapsed parallel:  166.568482161 s
executing job N 6 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.98 python_club_gemini_Amp_6.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 1 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
10820 cosmic pixels (10820 new)
Iteration 2
1746 cosmic pixels (1745 new)
Iteration 3
371 cosmic pixels (370 new)
Iteration 4
79 cosmic pixels (78 new)
Iteration 5
23 cosmic pixels (22 new)
Iteration 6
12 cosmic pixels (11 new)
Iteration 7
9 cosmic pixels (8 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_6.fits
None
Time elapsed parallel:  166.568881035 s
executing job N 7 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.83 python_club_gemini_Amp_7.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 1 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
11696 cosmic pixels (11696 new)
Iteration 2
1689 cosmic pixels (1684 new)
Iteration 3
421 cosmic pixels (417 new)
Iteration 4
97 cosmic pixels (95 new)
Iteration 5
30 cosmic pixels (28 new)
Iteration 6
6 cosmic pixels (4 new)
Iteration 7
3 cosmic pixels (1 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_7.fits
None
Time elapsed parallel:  166.734953165 s
executing job N 8 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 4.12 python_club_gemini_Amp_8.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 0 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
12862 cosmic pixels (12862 new)
Iteration 2
2235 cosmic pixels (2232 new)
Iteration 3
540 cosmic pixels (540 new)
Iteration 4
115 cosmic pixels (114 new)
Iteration 5
15 cosmic pixels (14 new)
Iteration 6
2 cosmic pixels (1 new)
Iteration 7
2 cosmic pixels (1 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_8.fits
None
Time elapsed parallel:  169.388837099 s
executing job N 9 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.5 python_club_gemini_Amp_9.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 1 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
15456 cosmic pixels (15456 new)
Iteration 2
2096 cosmic pixels (2085 new)
Iteration 3
459 cosmic pixels (450 new)
Iteration 4
92 cosmic pixels (86 new)
Iteration 5
31 cosmic pixels (24 new)
Iteration 6
8 cosmic pixels (5 new)
Iteration 7
4 cosmic pixels (1 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_9.fits
None
Time elapsed parallel:  246.958389997 s
executing job N 10 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.25 python_club_gemini_Amp_10.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 1 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
20879 cosmic pixels (20879 new)
Iteration 2
5516 cosmic pixels (4893 new)
Iteration 3
3559 cosmic pixels (2341 new)
Iteration 4
2456 cosmic pixels (1652 new)
Iteration 5
1908 cosmic pixels (1218 new)
Iteration 6
1562 cosmic pixels (804 new)
Iteration 7
1287 cosmic pixels (474 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_10.fits
None
Time elapsed parallel:  257.066220045 s
executing job N 11 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.35 python_club_gemini_Amp_11.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 2 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
16019 cosmic pixels (16019 new)
Iteration 2
2008 cosmic pixels (1969 new)
Iteration 3
414 cosmic pixels (373 new)
Iteration 4
122 cosmic pixels (81 new)
Iteration 5
62 cosmic pixels (20 new)
Iteration 6
52 cosmic pixels (10 new)
Iteration 7
49 cosmic pixels (7 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_11.fits
None
Time elapsed parallel:  257.066589117 s
executing job N 12 is FITS import shape : (512, 4176)
FITS file BITPIX : -32
Internal array type : float32
READING GAIN AND NOISE  1.0 3.46 python_club_gemini_Amp_12.fits
Detecting saturated stars ...
Building mask of saturated stars ...
We have 0 saturated stars.
Mask of saturated stars done
Starting 7 L.A.Cosmic iterations ...
Iteration 1
16942 cosmic pixels (16942 new)
Iteration 2
2465 cosmic pixels (2289 new)
Iteration 3
701 cosmic pixels (533 new)
Iteration 4
287 cosmic pixels (120 new)
Iteration 5
199 cosmic pixels (33 new)
Iteration 6
177 cosmic pixels (11 new)
Iteration 7
170 cosmic pixels (4 new)
FITS export shape : (512, 4176)
Wrote python_club_gemini_CC2_12.fits
None
Time elapsed parallel:  257.066709042 s
Job execution statistics:
 job count | % of all jobs | job time sum | time per job | job server
        12 |        100.00 |     1007.6895 |    83.974128 | local
Time elapsed since server creation 256.819142103
0 active tasks, 4 cores

Total  time elapsed:  257.067054033 s

In [20]:
image_data=pyfits.getdata("python_club_gemini_CC2_7.fits")
print type(image_data)
print image_data.shape


<type 'numpy.ndarray'>
(4176, 512)

In [32]:
from matplotlib.colors import LogNorm
image_data1=pyfits.getdata("python_club_gemini_CC2_7.fits")

plt.imshow(image_data1,cmap="gray",aspect='auto',norm=LogNorm(vmin=30.08,vmax=1000.1,clip=True))
#plt.imshow(image_data,cmap="gray",norm=LogNorm(vmin=0.1,vmax=0))
plt.colorbar()


Out[32]:
<matplotlib.colorbar.Colorbar at 0x133f39c10>

In [31]:
image_data2=pyfits.getdata("python_club_gemini_Amp_7.fits")
plt.imshow(image_data2,cmap="gray",aspect='auto',norm=LogNorm(vmin=30.08,vmax=1000.1,clip=True))
plt.colorbar()


Out[31]:
<matplotlib.colorbar.Colorbar at 0x133ccbfd0>

In [34]:
#Updating image...

imagen_in = "python_club_gemini.fits"
imagen_end = "python_club_gemini.fits"

imagen_a_corregir=imagen_in
FILE_FIN=imagen_end+"_CC2.fits"    #imagen final
FILE_TEMP=imagen_end+"_tmp.fits"    #imagen Temporal donde se hacen los cambios
imagen_header_original=imagen_in   # Imagen Original con todos los headers,

import shutil

#   making a copy of the priginal file to a temporal one.
shutil.copyfile(imagen_header_original,FILE_TEMP)
imagen_header2=FILE_TEMP

for i in range (1,13):

    imagen_Corre=str(imagen_a_corregir[:-5]+"_CC2_"+str(i)+".fits")  # Output of correction "CC2", NAME_CC2.fits
    imagen_no_header=pyfits.getdata(imagen_Corre,header=False)
    header=pyfits.getheader(FILE_TEMP)
    header2=pyfits.getheader(FILE_TEMP,i)
    pyfits.update(imagen_header2,imagen_no_header,ext=i) #copia la imagen en si a la extension correspondiente
    pyfits.update(imagen_header2,imagen_no_header,header2,i) #copia el header original a la extension correspondiente


import shutil
shutil.copyfile(FILE_TEMP,FILE_FIN)##

##Final Sanity check

#pyfits.info(FILE_FIN)
#print "Deleting files"

#print "Deleting _CC_ files and _Amp_ files"

#for m in range (1,13):
#    print " rm "+imagen_a_corregir[:-5]+"_CC2_"+str(m)+".fits"
#    os.remove(imagen_a_corregir[:-5]+"_CC2_"+str(m)+".fits")
#    print " rm "+imagen_a_corregir[:-5]+"_Amp_"+str(m)+".fits"
#    os.remove(imagen_a_corregir[:-5]+"_Amp_"+str(m)+".fits")


print "Cosmic cleaning done"

print "\n Marcelo D. Mora, 07.09.2016 mmora@astro.puc.cl v.2.0 \n"


Cosmic cleaning done

 Marcelo D. Mora, 07.09.2016 mmora@astro.puc.cl v.2.0 


In [ ]:


In [ ]:


In [ ]: