<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 # Astro Example
Cosmic cleanning of an image composed by 12 Amplifiyers... Warning
Step1:  The image of this example is an IFU science exposure taken with GMOS Hamamatsu CCD
Step2:  limpia_cosmicos  calls la_cosmic from P. van Dokkum
<ASSISTANT_TASK:>
Python Code:
# 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
Explanation: # Astro Example
Cosmic cleanning of an image composed by 12 Amplifiyers... Warning: gemnini biased!
End of explanation
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
print "Splittings Amps"
start_time1 = time.time()
separa_Amps(imagen_in)
start_time2 = time.time()
image_data=pyfits.getdata("python_club_gemini_Amp_7.fits")
print type(image_data)
print image_data.shape
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()
Explanation: 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
End of explanation
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)
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"
image_data=pyfits.getdata("python_club_gemini_CC2_7.fits")
print type(image_data)
print image_data.shape
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()
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()
#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"
Explanation: limpia_cosmicos  calls la_cosmic from P. van Dokkum
End of explanation
<END_TASK>