Example #1
0
def wregister(infile, ref, output):
    #inlist=(",".join(inlist))
    #output=(",".join(output))
    iraf.wregister.unlearn()
    iraf.wregister.setParam('input', infile)
    iraf.wregister.setParam('output', output)
    iraf.wregister.setParam('reference', ref)
    #iraf.wregister.setParam('mode','')
    iraf.wregister(infile, ref, output)
Example #2
0
def convolve_images_psf(images_with_headers):
    print("Convolving images (not implemented yet)")

    for i in range(0, len(images_with_headers)):

        original_filename = os.path.basename(images_with_headers[i][2])
        original_directory = os.path.dirname(images_with_headers[i][2])
        new_directory = original_directory + "/convolved/"
        #artificial_filename = new_directory + original_filename + "_pixelgrid.fits"
        #registered_filename = new_directory + original_filename  + "_registered.fits"
        input_directory = original_directory + "/registered/"
        input_filename = input_directory + original_filename  + "_registered.fits"
        print("Artificial filename: " + artificial_filename)
        print("Registered filename: " + registered_filename)
        print("Input filename: " + input_filename)
        if not os.path.exists(new_directory):
            os.makedirs(new_directory)

        #reading the science image:
        science_image = fits.getdata(input_filename)

        # if using a kernel image, then we first regrid the kernel to the same as in the science image, and we re-center the kernel:

        # create a fake image "apixel_kernel.fits"
        # the original kernel has a grid of 3645*3645 pixels and centered at (1822, 1822)
        # ncols = nlines = initial_number_of_rows * initial_pixelsize_of_the_kernel  / science_image_pixelsize
        # in the current case: 3645* 0.25 (arcsecs per pixel) / 2 (arcsecs per pixel) = 455.62
        artdata.mkpattern(input="apixel_kernel.fits", output="apixel_kernel.fits", pattern="constant", option="replace",v1=0., v2=1., size=1, title="", pixtype="real", ndim=2, ncols=455,nlines=455,n3=1, n4=1, n5=1, n6=1, n7=1, header="")

        #Then, tag the desired WCS in this fake image:
        #
        # unlearn some iraf tasks
        iraf.unlearn('ccsetwcs')
        #xref = yref = ncols/2 = nlines/2
        #xmag, ymag = pixel scale of science image
        iraf.ccsetwcs(images="apixel_kernel.fits", database="", solution="", xref=227.5, yref=227.5, xmag=2, ymag=2, xrotati=0.,yrotati=0.,lngref=0, latref=0, lngunit="hours", latunit="degrees", transpo="no", project="tan", coosyst="j2000", update="yes", pixsyst="logical", verbose="yes")

        # Then, register the fits file of interest to the WCS of the fake fits file
        #
        # unlearn some iraf tasks
        iraf.unlearn('wregister')

        iraf.wregister(input="Kernel_HiRes_PACS_70_to_SPIRE_500.fits", reference="apixel_kernel.fits", output="Kernel_P70_2_S500.fits", fluxconserve="yes")

        # then we get the data from the kernel
        kernel_image = pyfits.getdata('Kernel_P70_2_S500.fits')

        #several ways to do the convolution, but is best to use number 3 or 4:

        #3. 
        result3 = astropy.nddata.convolution.convolve.convolve(science_image, kernel_image) # got a segmentation fault - it needs an odd number of columns/rows for the kernel
        pyfits.writeto('science_image_convolved_3.fits',result3)

        #4. 
        result4 = astropy.nddata.convolution.convolve.convolve_fft(science_image,kernel_image) # worked OK - was the fastest thus far
        pyfits.writeto('science_image_convolved_4.fits',result4) 
Example #3
0
def run_wregister(image, reference):
    '''Align image to reference image using the WCS in 
       the header and IRAF's WREGISTER task
    '''
    # rename original image so it is not overwritten
    old = '%s_old.fits' % os.path.splitext(image)[0]
    os.rename(image, old)
    
    params = {'input':'%s[0]'%old, 
              'reference':'%s[0]'%reference,
              'output':image, 
              'xmin':'INDEF', 'xmax':'INDEF', 'ymin':'INDEF', 'ymax':'INDEF', 
              'wcs':'world', 'transpose':'no', 'fitgeom':'general',
              'function':'polynomial', 'calctype':'double', 
              'geometry':'geometric', 'interpolant':'spline3',
              'boundary':'constant', 'constant':0.0, 'fluxconserve':'yes',
              'wcsinherit':'yes'}

    wregister(**params)
def prep(df_image, hi_res_image):
    'run SExtractor to get bright sources that are easily detected in the high resolution data'
    ##### DEB: Can choose to run sextractor more or less aggressively
    subprocess.call('sex %s' % hi_res_image, shell=True)
    'copy the segmentation map to a mask'
    iraf.imcopy('seg.fits', '_mask.fits')
    'replace the values in the segments in the segmentation map (i.e. stars) all to 1, all the background is still 0'
    iraf.imreplace('_mask.fits', 1, lower=0.5)
    'multiply the mask (with 1s at the stars) by the high res image to get the star flux back - now have the flux model'
    iraf.imarith('_mask.fits', '*', '%s' % hi_res_image, '_fluxmod_cfht')
    'smooth the flux model'
    'increase size of mask, so more is subtracted'
    'the "1.5" in the next line should be a user-defined parameter that is given'
    'to the script; it controls how much of the low surface brightness'
    'emission in the outskirts of galaxies is subtracted. This choice'
    'depends on the science application'
    iraf.gauss('_fluxmod_cfht', '_fluxmod_cfht_smoothed', 2.5)  ### nsigma=4
    '''
    imreplace _fluxmod_cfht_rs -1 lower=0 upper=0
    imreplace _fluxmod_cfht_rs 1 lower=-0.5
    imreplace _fluxmod_cfht_rs 0 upper=-0.5
    imarith _cfht_r * _fluxmod_cfht_rs _fluxmod_cfht_r_new
    '''
    '''
    # this is the key new step! we're registering the CFHT image
    #  to a frame that is 4x finer sampled than the Dragonfly image.
    # this avoids all the pixelation effects we had before. 
    #  (4x seems enough; but we could have it as a free parameter - need
    # to be careful as it occurs elsewhere in the scripts too) 
    blkrep _df_g _df_g4 4 4
    '''

    'register the flux model onto the same pixel scale as the dragonfly image'
    iraf.wregister('_fluxmod_cfht_smoothed',
                   '%s' % df_image,
                   '_fluxmod_dragonfly',
                   interpo='linear',
                   fluxcon='yes')

    return None
Example #5
0
	def reMap(self, remapFits, outname="input_remapped.fits", verbose=False, wcsregister=False):
	  
		# Create an object class for the output image for fluency of continuing code
		outFits = imFits()
		outFits._Name = outname
		outFits._logger["ReMapping"] = []
	  
		# remapFits = template image
		# this object = source image
		# outFits = output fits image
		
		if wcsregister:
			iraf.wregister(input=self._Name, output=outFits._Name, wcs="world", reference=remapFits._Name, Stdout=1)

		else:
			wcscmd = ["wcsremap","-template", self._Name, "-source", remapFits._Name, "-outIm", outFits._Name]
			wcsremap = subprocess.Popen(wcscmd, stdout=subprocess.PIPE)
			outFits._logger["ReMapping"].append(wcsremap.communicate()[0])
		
		if verbose:
			for log in outFits._logger["ReMapping"]:
				print log
		
		return outFits
Example #6
0
 def remap_wcs(self, inimage, refimage, outimage, **kwargs):
     from pyraf import iraf
     try:
         sciext = kwargs['sci']
         templext = kwargs['ref']
     except KeyError:
         sciext = 0
         templext = 0
     sci_ext = '{}'.format(sciext)
     ref_ext = '{}'.format(templext)
     return iraf.wregister(input=inimage + sci_ext,
                           output=outimage,
                           wcs="world",
                           reference=refimage + ref_ext,
                           Stdout=1)
Example #7
0
def wregister(input_drz, input_unc, input_cov, ref_image):
   """
   Use iraf.wregister to resample the input images onto the pixel grid defined
   by ref_image.
   """
   hdr0 = pyfits.getheader(input_drz)
   drz0 = pyfits.getdata(input_drz)
   unc = pyfits.getdata(input_unc)
   
   # prepare the input variance image
   var = np.where(unc > 0, unc**2, 0.)
   input_var = input_unc.replace('unc', 'var')
   if os.path.exists(input_var):
      os.remove(input_var)
   pyfits.append(input_var, var, hdr0)

   # now use wregister
   output_drz = input_drz.replace('drz', 'wreg_drz')
   if os.path.exists(output_drz):
      os.remove(output_drz)
   iraf.wregister(input_drz, ref_image, output_drz)
   output_var = input_var.replace('var', 'wreg_var')
   if os.path.exists(output_var):
      os.remove(output_var)
   iraf.wregister(input_var, ref_image, output_var)
   output_cov = input_cov.replace('cov', 'wreg_cov')
   if os.path.exists(output_cov):
      os.remove(output_cov)
   iraf.wregister(input_cov, ref_image, output_cov)

   # now convert the wregister-ed variance image back to uncertainty image
   var1 = pyfits.getdata(output_var)
   hdr1 = pyfits.getheader(output_var)
   unc1 = np.where(var1 > 0, np.sqrt(var1), 1.e9)
   output_unc = input_unc.replace('unc', 'wreg_unc')
   if os.path.exists(output_unc):
      os.remove(output_unc)
   pyfits.append(output_unc, unc1, hdr1)

   print 'Done.'
Example #8
0
    -must run in folder containing files that will be inputted
    -will create a combined output image named object_filter.fits

'''
from pyraf import iraf
import sys
import os

#read in list of files
infile = sys.argv[1]
imfile = open(infile, 'r')
images = []

for line in imfile:
    images.append(line.rstrip('\n'))
imfile.close()

#run wregister
for image in images:
    iraf.wregister(input=image, reference=images[0], output='r' + image)
#write registered files into an output file
outfile = open('imageFile', 'w')
for im in images:
    outfile.write(im + '\n')
outfile.close()

#combine registered images
iraf.imcombine(input='@imageFile', output=infile + '.fits')

#Written by Tiffany Flood and Kelly Whalen
import sys
import os


#read in list of files
infile=sys.argv[1]
imfile=open(infile,'r')
images=[]

for line in imfile:
    images.append(line.rstrip('\n'))
imfile.close()
                  
#run wregister
for image in images:
    iraf.wregister(input=image,reference=images[0],output='r'+image)
#write registered files into an output file
outfile=open('imageFile','w')
for im in images:
    outfile.write('r'+im+'\n')
outfile.close()

#combine registered images
iraf.imcombine(input='@imageFile', output=infile+'.fits', reject='crreject')


#Written by Tiffany Flood and Kelly Whalen



# /usr/local/astrometry/bin/solve-field --scamp ftr7134t0039o00.cat --scamp-ref image39ref.fits --scamp-config ftr7134t0039o00.config ftr7134t0039o00.fits
Example #10
0
def resample_images(images_with_headers):
    """
    Resamples all of the images to a common pixel grid.

    Parameters
    ----------
    images_with_headers: zipped list structure
        A structure containing headers and image data for all FITS input
        images.

    """

    print("Resampling images.")

    # First we create an artificial fits image, 
    # The difference with the registration step is that the artificial image is now created only once, and it is common for all the input_images_convolved (or imput_images_gaussian_convolved)
    # unlearn some iraf tasks
    iraf.unlearn('mkpattern')
    
    # create a fake image "grid_final_resample.fits", to which we will register all fits images
    fwhm_input = get_fwhm_value(images_with_headers)
    print("fwhm: " + `fwhm_input`)
    # parameter1 & parameter2 depend on the "fwhm" of the convolution step, and following the Nyquist sampling rate. 
    parameter1 = phys_size / (fwhm_input / NYQUIST_SAMPLING_RATE) 
    print("ncols, nlines: " + `parameter1`)
    parameter2 = parameter1
    artdata.mkpattern(input="grid_final_resample.fits", output="grid_final_resample.fits", pattern="constant", pixtype="double", ndim=2, ncols=parameter1, nlines=parameter2)

    if (ra_input != ''):
        lngref_input = ra_input
    else:
        lngref_input = get_herschel_mean(images_with_headers, 'CRVAL1')
    if (dec_input != ''):
        latref_input = dec_input
    else:
        latref_input = get_herschel_mean(images_with_headers, 'CRVAL2')
    
    # Then, we tag the desired WCS in this fake image:
    # unlearn some iraf tasks
    iraf.unlearn('ccsetwcs')

    # tag the desired WCS in the fake image "apixel.fits"
    # NOTETOSELF: in the code Sophia gave me, lngunit was given as "hours", but I have
    # changed it to "degrees".
    iraf.ccsetwcs(images="grid_final_resample.fits", database="", solution="", xref=parameter1/2, yref=parameter2/2, xmag=fwhm_input/NYQUIST_SAMPLING_RATE, ymag=fwhm_input/NYQUIST_SAMPLING_RATE, xrotati=0.,yrotati=0.,lngref=lngref_input, latref=latref_input, lngunit="degrees", latunit="degrees", transpo="no", project="tan", coosyst="j2000", update="yes", pixsyst="logical", verbose="yes")

    for i in range(0, len(images_with_headers)):
        original_filename = os.path.basename(images_with_headers[i][2])
        original_directory = os.path.dirname(images_with_headers[i][2])
        new_directory = original_directory + "/resampled/"
        resampled_filename = new_directory + original_filename  + "_resampled.fits"
        input_directory = original_directory + "/convolved/"
        input_filename = input_directory + original_filename  + "_convolved.fits"
        print("Resampled filename: " + resampled_filename)
        print("Input filename: " + input_filename)
        if not os.path.exists(new_directory):
            os.makedirs(new_directory)

        # Then, register the fits file of interest to the WCS of the fake fits file
        # unlearn some iraf tasks
        iraf.unlearn('wregister')

        # register the science fits image
        iraf.wregister(input=input_filename, reference="grid_final_resample.fits", output=resampled_filename, fluxconserve="yes")

    create_data_cube(images_with_headers)
Example #11
0
def register_images(images_with_headers):
    """
    Registers all of the images to a common WCS

    Parameters
    ----------
    images_with_headers: zipped list structure
        A structure containing headers and image data for all FITS input
        images.

    """

    print("Registering images")
    print("phys_size: " + `phys_size`)

    if (ra_input != ''):
        lngref_input = ra_input
    else:
        lngref_input = get_herschel_mean(images_with_headers, 'CRVAL1')

    if (dec_input != ''):
        latref_input = dec_input
    else:
        latref_input = get_herschel_mean(images_with_headers, 'CRVAL2')

    for i in range(0, len(images_with_headers)):

        native_pixelscale = get_native_pixelscale(images_with_headers[i][1], get_instrument(images_with_headers[i][1]))
        print("Native pixel scale: " + `native_pixelscale`)
        print("Instrument: " + `get_instrument(images_with_headers[i][1])`)
        print("BUNIT: " + `images_with_headers[i][1]['BUNIT']`)

        original_filename = os.path.basename(images_with_headers[i][2])
        original_directory = os.path.dirname(images_with_headers[i][2])
        new_directory = original_directory + "/registered/"
        artificial_filename = new_directory + original_filename + "_pixelgrid.fits"
        registered_filename = new_directory + original_filename  + "_registered.fits"
        input_directory = original_directory + "/converted/"
        input_filename = input_directory + original_filename  + "_converted.fits"
        print("Artificial filename: " + artificial_filename)
        print("Registered filename: " + registered_filename)
        print("Input filename: " + input_filename)
        if not os.path.exists(new_directory):
            os.makedirs(new_directory)

        # First we create an artificial fits image
        # unlearn some iraf tasks
        iraf.unlearn('mkpattern')

        # create an artificial image to which we will register the FITS image.
        artdata.mkpattern(input=artificial_filename, output=artificial_filename, pattern="constant", pixtype="double", ndim=2, ncols=phys_size/native_pixelscale, nlines=phys_size/native_pixelscale)
        #note that in the exact above line, the "ncols" and "nlines" should be wisely chosen, depending on the input images - they provide the pixel-grid 
        #for each input fits image, we will create the corresponding artificial one - therefore we can tune these values such that we cover, for instance, XXarcsecs of the target - so the best is that user provides us with such a value

        # Then, we tag the desired WCS in this fake image:
        # unlearn some iraf tasks
        iraf.unlearn('ccsetwcs')

        # tag the desired WCS in the artificial image.
        iraf.ccsetwcs(images=artificial_filename, database="", solution="", xref=(phys_size/native_pixelscale)/2, yref=(phys_size/native_pixelscale)/2, xmag=native_pixelscale, ymag=native_pixelscale, xrotati=0.,yrotati=0.,lngref=lngref_input, latref=latref_input, lngunit="degrees", latunit="degrees", transpo="no", project="tan", coosyst="j2000", update="yes", pixsyst="logical", verbose="yes")
        #note that the "xref" and "yref" are actually half the above "ncols", "nlines", respectively, so that we center each image
        #note also that "xmag" and "ymag" is the pixel-scale, which in the current step ought to be the same as the native pixel-scale of the input image, for each input image - so we check the corresponding header value in each image
        #note that "lngref" and "latref" can be grabbed by the fits header, it is actually the center of the target (e.g. ngc1569)
        #note that we should make sure that the coordinate system is in coosyst="j2000" by checking the header info, otherwise we need to adjust that

        # Then, register the fits file of interest to the WCS of the fake fits file
        # unlearn some iraf tasks
        iraf.unlearn('wregister')

        # register the science fits image
        iraf.wregister(input=input_filename, reference=artificial_filename, output=registered_filename, fluxconserve="no")
def prep(df_image,hi_res_image,width_mask=1.5,unmaskgal=False,galvalues = None):
    print "\n************ Running the preparation steps ************\n"
    iraf.imdel('_mask.fits')
    iraf.imdel('_fluxmod_cfht*.fits')
    iraf.imdel('_df_4*.fits')
    
    'run SExtractor to get bright sources that are easily detected in the high resolution data'
    subprocess.call('sex %s'%hi_res_image,shell=True)    #####  Add in option to change sextractor threshold detect_thresh and analysis_thresh 2/3
    'copy the segmentation map to a mask'
    iraf.imcopy('seg.fits','_mask.fits')

    ##### Add step to get rid of diffraction spikes

    if unmaskgal:
        'Run SExtractor to get values for the central galaxy'
        print '\nDoing a second sextractor run to unmask central galaxy. \n'
        analysis_thresh_lg=2;back_size_lg=128;detect_thresh_lg=2;detect_minarea_lg=60
        segname = run_SExtractor(hi_res_image,detect_thresh=detect_thresh_lg,analysis_thresh=analysis_thresh_lg,back_size=back_size_lg,detect_minarea=detect_minarea_lg)
        print '\nSegmentation map is named: '+segname
        
        'Open up the data'
        seg2data = fits.getdata(segname)
        segdata,segheader = fits.getdata('_mask.fits',header=True)
        
        'Detect sources from the segmentation map'
        from photutils import detect_sources
        segrefdata = detect_sources(seg2data, 3, npixels=5)#, filter_kernel=kernel) 
        segrefdata = segrefdata.data
        segrefname = re.sub('.fits','_ds.fits',segname)
        writeFITS(segrefdata,segheader,segrefname)    
        print '\nSource separated segmentation map is named: '+segrefname
        
        'Use detected source seg map to mask galaxies' #galvalues = [3531,5444,5496]
        print 'Unmask the galaxies in the mask from the original segmap. \n'
        segdatanew = unmaskgalaxy(segdata,'_mask.fits',segrefname=segrefname,segref=segrefdata,galvalues=galvalues)        
        writeFITS(segdatanew,segheader,'_mask.fits')
        
    if verbose:
        print_verbose_string('Carrying on')
        
    'replace the values in the segments in the segmentation map (i.e. stars) all to 1, all the background is still 0'
    iraf.imreplace('_mask.fits',1,lower=0.5)
    'multiply the mask (with 1s at the stars) by the high res image to get the star flux back - now have the flux model'
    iraf.imarith('_mask.fits','*','%s'%hi_res_image,'_fluxmod_cfht')
    
    'smooth the flux model'
    'increase size of mask, so more is subtracted'
    'the "1.5" in the next line should be a user-defined parameter that is given'
    'to the script; it controls how much of the low surface brightness'
    'emission in the outskirts of galaxies is subtracted. This choice'
    'depends on the science application'
    iraf.gauss('_fluxmod_cfht','_fluxmod_cfht_smoothed',width_mask,nsigma=4.)

    iraf.imreplace('_fluxmod_cfht_smoothed', -1, lower=0, upper=0)
    iraf.imreplace('_fluxmod_cfht_smoothed', 1, lower=-0.5)
    iraf.imreplace('_fluxmod_cfht_smoothed', 0, upper=-0.5)
    iraf.imarith('%s'%hi_res_image, '*','_fluxmod_cfht_smoothed', '_fluxmod_cfht_new')
    
    ' this is the key new step! we"re registering the CFHT image'
    ' to a frame that is 4x finer sampled than the Dragonfly image.'
    ' this avoids all the pixelation effects we had before.'
    '  (4x seems enough; but we could have it as a free parameter - need'
    ' to be careful as it occurs elsewhere in the scripts too) '
    iraf.blkrep('%s'%df_image,'_df_4',4,4)
    
    'register the flux model onto the same pixel scale as the dragonfly image'
    iraf.wregister('_fluxmod_cfht_new','_df_4','_fluxmod_dragonfly',interpo='linear',fluxcon='yes')

    return None
Example #13
0
# images
# written by Duho Kim (1/31/18)
######################################################
from pyraf import iraf
from astropy.io import ascii
import os

work_dir = '/Users/dhk/work/data/NGC_IC/'
cat_dir = '/Users/dhk/work/cat/NGC_IC/'

sha_cat = ascii.read(cat_dir + 'sha_quarry_batch_257_without_prefix.txt')
iraf.daophot()

for i in range(0, len(sha_cat)):
    name = sha_cat['id'][i]  # NGC/IC name
    g = work_dir + 'SDSS/g/' + name + '-g.fits'
    gi = work_dir + 'SDSS/g/' + name + '-gi.fits'
    gir = work_dir + 'SDSS/g/' + name + '-gir.fits'
    r = work_dir + 'SDSS/r/' + name + '-r.fits'
    ri = work_dir + 'SDSS/r/' + name + '-ri.fits'
    rir = work_dir + 'SDSS/r/' + name + '-rir.fits'
    i1 = work_dir + 'SHA/SHA_NGC_IC_LONG/' + name + '.fits'
    g_psf = work_dir + 'SDSS/g_psf6/' + name + '-g.psf3.fits'
    r_psf = work_dir + 'SDSS/r_psf6/' + name + '-r.psf3.fits'
    i_psf = work_dir + 'SHA/psf_master/' + name + '.psf.fits'

    #	iraf.psfmatch(g,reference=i_psf,psfdata=g_psf,output=gi,convolution='psf',kernel='')
    iraf.wregister(gi, reference=i1, output=gir)
    #        iraf.psfmatch(r,reference=i_psf,psfdata=r_psf,output=ri,convolution='psf',kernel='')
    iraf.wregister(ri, reference=i1, output=rir)