def makecuts(image,imagefilter):
    catdat= fits.getdata(args.catalog)
    print 'Cutting out', image    
    
    zFlag = (catdat.Z > Zmin) & (catdat.Z < Zmax)
    
    f = fits.open(image)
    prihdr = f[0].header
    n2,n1 = f[0].data.shape
    
    w= WCS(image)
    px,py = w.wcs_world2pix(catdat.RA,catdat.DEC,1)
    onimageflag=(px < n1) & (px >0) & (py < n2) & (py > 0)
    
    keepflag=zFlag & onimageflag
    RA=catdat.RA[keepflag]
    DEC=catdat.DEC[keepflag]
    radius=catdat.SERSIC_TH50[keepflag]
    IDNUMBER=catdat.NSAID[keepflag]
    print 'number of galaxies to keep = ', sum(keepflag)

#    if args.region_file:
        
    for i in range(len(RA)):

        if (radius[i]<.01):
            size=120.
        else:
            size=float(args.scale)*radius[i]
            
        position = SkyCoord(ra=RA[i],dec=DEC[i],unit='deg')
        size = u.Quantity((size, size), u.arcsec)
        #print image, radius[i], position, size
        #cutout = Cutout2D(fdulist[0].data, position, size, wcs=w, mode='strict') #require entire image to be on parent image
        try:
            cutout = Cutout2D(f[0].data, position, size, wcs=w, mode='trim') #require entire image to be on parent image
        except astropy.nddata.utils.PartialOverlapError:# PartialOverlapError:
            print 'galaxy is only partially covered by mosaic - skipping ',IDNUMBER[i]
            continue
        except astropy.nddata.utils.NoOverlapError:# PartialOverlapError:
            print 'galaxy is not covered by mosaic - skipping ',IDNUMBER[i]
            continue
        if args.plot:
            plt.figure()
            plt.imshow(f[0].data, origin='lower',cmap='gray', norm=LogNorm())
            cutout.plot_on_original(color='white')
            plt.show()
            r = raw_input('type any key to continue (p to skip plotting) \n')
            if r.find('p') > -1:
                args.plot = False
        # figure out how to save the cutout as fits image
        ((ymin,ymax),(xmin,xmax)) = cutout.bbox_original
        outimage = args.prefix+'-'+(str(IDNUMBER[i])+'-'+ args.filter+".fits")
        newfile = fits.PrimaryHDU()
        newfile.data = f[0].data[ymin:ymax,xmin:xmax]
        newfile.header = f[0].header
        newfile.header.update(w[ymin:ymax,xmin:xmax].to_header())
        
        fits.writeto(outimage, newfile.data, header = newfile.header, clobber=True)
    return cutout
def splitkwaj(fn, outPath, outFN, nStart):
    '''Loads a 16-frame FITS file obtained from Kwajalein MIT/LL telescopes - saves a separate FITS files'''
    
    # Basic idea:
    # Kwaj FITS files have one primary header and 15 extensions, each with an
    # associated 1024x1024 image. We load these and save them individually with the
    # associated headers.
    
    hdu1 = pf.open(fn)
    
    # Write the image in the primary HDU to disk
    d = hdu1[0].data
    h = hdu1[0].header
    h.update('EXTEND', 'F')
    
    pf.writeto(outPath + outFN + "_" + str(nStart) + ".fits", d, h)
    
    # Now write the 15 extensions as individual FITS files
    
    fnX = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15"]
    
    for i0 in range(15):
        i = i0 + 1	# skip the first image
        d = hdu1[i].data
        h = hdu1[i].header
        k = h.keys()
        hList = arange(43)+9	# indices of the keywords we want to keep
        hdu0 = pf.PrimaryHDU(d)	# Make a new stub of a header
        for j in hList:
            hdu0.header.update(k[j], h[k[j]])
        pf.writeto(outPath + outFN + "_" + str(nStart+i) + ".fits", d, hdu0.header)
    
    return
 def export_to_fits(self, fitsfile='ModelDetectorCube'):
     header = self.wcs_info()
     slice_index = 0
     for flux_plus_bg in self.flux_plus_bg_list:
         fitsfile_slice = fitsfile + str(slice_index).strip() + '.fits'
         fits.writeto(fitsfile_slice, np.rollaxis(flux_plus_bg, 2), header, clobber=True)
         fits.append(fitsfile_slice, self.wave)
Exemple #4
0
def determine_ratio_baseline_sigma(plot=False):
    data = pyfits.getdata(fits_path+'local_counts_baseline.fits')
    el = data[:,:,0].astype(np.float)
    sp = data[:,:,1].astype(np.float)
    mask_el = el < 1
    mask_sp = sp < 1
    mask_all = np.logical_and(mask_el, mask_sp)
    mask = np.logical_or(mask_el, mask_sp)
    ratio = pyfits.getdata(fits_path+'local_ratio_baseline.fits')
    count_sum = (el + sp).astype(np.float)
    count_product = (el * sp).astype(np.float)
    np.putmask(count_product, mask, 1.0)
    np.putmask(count_sum, mask, 1.0)
    sigma = (np.log10(np.e))**2 * count_sum / count_product
    sigma = np.sqrt(sigma)
    np.putmask(sigma, mask, unknown_ratio)

    sigma_masked = ma.masked_less_equal(sigma, unknown_ratio)

    if plot:
        fig = plt.figure(3)
        fig.clf()
        ax = fig.add_subplot(111)
        im = ax.imshow(sigma_masked, interpolation='nearest', origin='lower')
        cb = plt.colorbar(im)
        ax.set_aspect('auto')
        ax.set_xlabel(r'$M_R [mag]$',fontsize=22)
        ax.set_ylabel(r'$R_{50} [kpc]$',fontsize=22)

    pyfits.writeto(fits_path+'local_ratio_baseline_sigma.fits',
           sigma, clobber=True)    
def insert_fake_star(d,h,mag):
    """
    Currently this function is NOT used
    
    """
    
    xsize = h['NAXIS1']
    ysize = h['NAXIS2']
    #Insert fake star
    size = 5
    x = np.random.random_sample()*(xsize-size+1)+(size+1)
    y = np.random.random_sample()*(ysize-size+1)+(size+1)
    #print(x,y)
    #x = 700
    #y = 690
    #print(x,y)
    magnitude = mag
    zp = 25.
    expfactor = (magnitude - zp)/(-2.5)
    counts = math.pow(10.,expfactor)
    #print(counts)
    g = gauss_kern(size,counts) #5 is rough guess for FWHM
    d[y-size:y+size+1,x-size:x+size+1] += g #Damn backward numpy arrays

    fits.writeto("TestOuput.fits",d,h,clobber=True)
    return(x,y,magnitude)
Exemple #6
0
 def get_usr_mask(self):
     print 'click on the location to add object mask'
     if args.nods9:
         a = self.fig.canvas.mpl_connect('button_press_event', self.onclick)
 
         while self.xcursor == self.xcursor_old: # stay in while loop until mouse is clicked
             plt.pause(1)
     else:
         self.ds9_onclick()
     # mask out a rectangle around click
     # size is given by mask_size
     xmin = int(self.xcursor) - int(0.5*self.mask_size)
     ymin = int(self.ycursor) - int(0.5*self.mask_size)
     xmax = int(self.xcursor) + int(0.5*self.mask_size)
     ymax = int(self.ycursor) + int(0.5*self.mask_size)
     # make sure mask dimensions are not outside of the image
     if xmin < 0:
         xmin = 0
     if ymin < 0:
         ymin = 0
     if xmax > self.xmax:
         xmax = self.xmax
     if ymax > self.ymax:
         ymax = self.ymax
     print 'xcursor, ycursor = ',self.xcursor, self.ycursor
     mask_value = np.max(self.maskdat) + 1
     self.usr_mask[ymin:ymin+int(self.mask_size),xmin:xmin+int(self.mask_size)] = mask_value*np.ones([self.mask_size,self.mask_size])
     self.maskdat = self.maskdat + self.usr_mask
     fits.writeto(self.mask_image, self.maskdat, header = self.imheader, clobber=True)
     print('added mask object '+str(mask_value))
     self.xcursor_old = self.xcursor 
def trim_image(image, cra, cdec, xsize, ysize, outname):
    '''Trim images and maintain WCS.

       Provide the center of the field and desired size
       xsize,ysize given in arcmin
    '''
    im,hdr = fits.getdata(image, header=True)
    hdr_wcs = pywcs.WCS(hdr)

    # convert size to pixels
    xs = xsize * 60. / hdr['SECPIX1']
    ys = ysize * 60. / hdr['SECPIX2']

    # update header to have correct WCS
    # from Adam Ginsburg's cutout.py
    hdr['CRPIX1'] = xs / 2.
    hdr['CRPIX2'] = ys / 2.
    hdr['CRVAL1'] = cra
    hdr['CRVAL2'] = cdec
    hdr['NAXIS1'] = int(xs)
    hdr['NAXIS2'] = int(ys)

    # find center of WISP field in Palomar pixel coordinates
    pix = hdr_wcs.wcs_sky2pix([[cra,cdec]],1)
    x1 = pix[0][0] - xs/2.
    x2 = pix[0][0] + xs/2.
    y1 = pix[0][1] - ys/2.
    y2 = pix[0][1] + ys/2.

    new = im[y1:y2,x1:x2]

    fits.writeto(outname, new, header=hdr, clobber=True)
Exemple #8
0
 def submit(event):
     try:
         pyfits.writeto(self.model_location+'/xmod.fits', xparams,clobber=True)
         print('Data updated on xmod.fits')
         return 1
     except Exception:
         print('Unable to save data onto file.')
Exemple #9
0
def checkSkylines(filename,min_lambda,max_lambda,sumRow,outname):

    file = fits.open(filename)
    data = file[0].data
    hdr = file[0].header
    sizeLambda = data.shape[2]

    min_image_lambda = hdr['CRVAL1']
    delta_lambda = hdr['CDELT1']

    min_pixel = int(round((min_lambda - min_image_lambda) / delta_lambda))
    max_pixel = int(round((max_lambda - min_image_lambda) / delta_lambda))

    if (min_pixel >= max_pixel):
        sys.exit("Max lambda needs to be greater than min lambda")

    if (min_pixel < 0):
        sys.exit("Selected minimum wavelength is off detector")

    if (max_pixel > sizeLambda):
        sys.exit("Selected maximum wavelength is off detector")

    onlyLine = data[:,:,min_pixel:max_pixel]

    sumLine = np.sum(onlyLine,axis=2)

    sumRowOnly = sumLine[:,sumRow]

    totalRMS = np.std(sumLine)
    rowRMS = np.std(sumRowOnly)


    fits.writeto(outname,sumLine,clobber=True)

    return totalRMS, rowRMS
Exemple #10
0
    def PlotTSmap(self) :
        """ Gather the results of the evaluation of 
        each pixel and fill a fits file"""
        folder = self.config['out']

        # Read the cmap produced before to get the grid for the TS map
        FitRunner = Observation(folder, self.config)
        try :
             header = fits.getheader(FitRunner.cmapfile)
        except :
             self.error('Count map not found.')
        data = fits.getdata(FitRunner.cmapfile)*0.
        npix_im = min(header['NAXIS1'],header['NAXIS2'])
        npix = min(self.config['TSMap']['npix'],npix_im)
        Xref = header['CRPIX1']
        Yref = header['CRPIX2']
        binsz = header['CDELT1']

        import string # read the results
        for i in xrange(npix):
            for j in xrange(npix):
                try : 
                    lines = open(self._PixelFile(i,j),"r").readlines()
                    Value = float(string.split(lines[0])[2])
                except :
                    self.warning("Cannot find, open or read "+self._PixelFile(i,j))
                    Value = 0.
                data[Xref+ (i-npix/2.)][Yref+ (j-npix/2.)] = Value

        # save in a fits files
        fits.writeto(folder+"/"+self.TSfits,data,header)
        self.info("TS Map saved in "+folder+"/"+self.TSfits)
def main(fname, output=False, unit=1, clobber=True):
    '''Convert the CRIRES spectrum to a 1D spectrum

    Input:
        fname: Fits file of CRIRES spectrum
        output: Name of output file. Default is: "wmin-wmax.fits"
        unit: Unit of wavelength vector (Angstrom is default.)
        clobber: Overwrite existing files
    '''
    I = fits.getdata(fname)
    w = _get_wavelength(fname, unit=unit)
    if not output:
        output = '%i-%i.fits' % (w.min(), w.max())
    else:
        if not output.lower().endswith('.fits'):
            output += '.fits'

    N = len(w)
    hdr = fits.Header()
    hdr["NAXIS1"] = N
    hdr["CDELT1"] = (w[-1]-w[0])/N
    hdr["CRVAL1"] = w[0]

    fits.writeto(output, I['Extracted_OPT'], header=hdr, clobber=clobber)
    print('File writed to: {}'.format(output))
def poly(n,zi,dz,filename):
    '''
    Integrates the L-E equation and returns and writes to a file the important values.
    n is the index, zi is the interval to use the polynomial solution, dz is the step size, filename is the prefix for the output file (filename+.fits)
    '''
    #set up equation and variables
    LEeq = LEeqn(n)
    zs = np.arange(0.,zi,dz)
    nz = np.size(zs)

    #integrate over initial interval using polynomial
    y = intg.odeint(thPN,np.array([1.,0.]),zs)

    #integrate until th<0
    while y[-1,0]>0.:
        zs2 = np.arange(zs[-1],zs[-1]+200.*dz,dz)
        zs = np.append(zs,zs2)
        y = np.append(y,intg.odeint(LEeq,y[-1],zs2),axis=0)
        #write progress
        sys.stdout.write("\rz={0},th={1}".format(zs[-1],y[-1,0]))
        sys.stdout.flush()
    sys.stdout.write("\n")

    #only keep values where th>0
    ngood= np.size(np.where(y[:,0]>0.))
    zs = zs[:ngood]
    y = y[:ngood]

    #prepare output array
    data = np.array([zs,y[:,0],-(zs**2)*y[:,1],(-3./zs)*y[:,1]])

    #write and return data
    fits.writeto(filename+'.fits',data)
    return data
def fix_header(image, wispfield):
    '''Fix the DATASEC keyword in the image's header to extend
       out to the full size of the image. After IRAF's imcombine,
       the DATASEC keyword should have been updated to include 
       the new size of the image
       
       Add the total exposure time of the combined images to 
       the header
    '''
    im,hdr = fits.getdata(image, header=True)
    nx1 = hdr['NAXIS1']
    nx2 = hdr['NAXIS2']
    hdr['DATASEC'] = '[1:%i,1:%i]' % (nx1-1, nx2-1)

    if 'TEXPTIME' not in hdr:
        ims = hdr['IMCMB*']
        Nim = len(ims)
        texptime = 0.
        # add up all the exposure times
        for i in range(Nim):
            if os.path.splitext(ims[i])[1] != '.fits':
                ims[i] = ims[i] + '.fits'
            thdr = fits.getheader(os.path.join(wispfield,ims[i]))
            texptime += thdr['exptime']
        hdr.insert(19, ('TEXPTIME', texptime,
                               'Total EXPTIME of combined images'))
    fits.writeto(image, im, hdr, clobber=True)
Exemple #14
0
def linearity_correction(image_list):
  if "--shutter" in sys.argv:
    #self_seach_pattern = patt_shutter_correc
    self_seach_pattern = patt_bias_corrected # remove once shutter is implemented and uncomment previous
  else:
    self_seach_pattern = patt_bias_corrected
  length = len(image_list) * len(quadrants)
  for k in range(len(quadrants)):
    e = quadrants[k]
    for i in range(len(image_list)):
      current= i + k*len(image_list)
      print_progress(current,length)
      image	= proc_data_path + self_seach_pattern + image_list[i] + "c%s.fits"%e
      #print image
      data,header= get_data_header(image)
      header["COMMENT"] = ("linearity corrected image \ Added by %s"%my_name)
      c2	= linear_c2[e-1]
      c3	= linear_c3[e-1]
      alpha	= linear_alpha[e-1]
      lc_data = correct.linear(data,c2,c3,alpha)
      # = data
      lc_image = proc_data_path + patt_linearity_corr + image_list[i] + "c%s.fits"%e
      #print lc_image
      fits.writeto(lc_image,data,header,clobber=True)
      #print_progress(i,len(image_list))
  return
def createVoronoiOutput(inputFile=datadir+cuberoot+'.fits',inputVoronoiFile=datadir+'voronoi_2d_binning_output.txt'):
    cubefits = pyfits.open(inputFile)
    
    cube = cubefits[0].data
    hdr = cubefits[0].header
    errors = cubefits[1].data
    quality = cubefits[2].data
    nframes = cubefits[3].data

    cubeShape = (cube.shape[0],cube.shape[1],cube.shape[2])

    yy, xx, binnum = np.loadtxt(inputVoronoiFile,unpack=True)
    xx = xx.astype(int)
    yy = yy.astype(int)
    binnum = binnum.astype(int)

    newCube = np.zeros(cubeShape, dtype=float)
    newErr = np.zeros(cubeShape, dtype=float)

    for nb in range(binnum.max()+1):
        idx = np.where(binnum == nb)
        nx = xx[idx]
        ny = yy[idx]
        nbins = len(idx[0])
        tmpCube = np.sum(cube[nx,ny,:],axis=0)/nbins
        tmpErr = np.sqrt(np.sum(errors[nx,ny,:]**2,axis=0))/nbins
        newCube[nx,ny,:] = tmpCube
        newErr[nx,ny,:] = tmpErr

    #pdb.set_trace()
    outfile = inputFile.replace('.fits','_vorcube.fits')
    pyfits.writeto(outfile,newCube,header=hdr)
    pyfits.append(outfile,newErr)
    pyfits.append(outfile,quality)
    pyfits.append(outfile,nframes)
    def pngsToFits(self, overwrite = False):
        """
        Converts each PNG file to three FITS files, once per color.
        :param overwrite:
        :return:
        """
        for i, ZooID in enumerate(self.catalog['ZooID']):
            if self.test_mode and i > 5:
                break

            #find images
            basic_name =  str(i) + '_' + ZooID
            png_path = os.path.join(self.path_to_data, "cutouts/png", basic_name + ".png")
            img = Image.open(png_path)
            img_by_color = [self.imgToRgbArray(img)[:,:,c] for c in range(3)]
            for c in range(3):
                fits_path = self.make_img_path(i, ZooID, True, c)
                #open, convert, save
                if os.path.exists(fits_path):
                    if overwrite:
                        warnings.warn("Overwriting FITS files.")
                        os.remove(fits_path)
                    else:
                        warnings.warn("Leaving existing FITS files untouched.")
                        continue
                fits.writeto(fits_path, data = img_by_color[c])
        return
 def export_to_fits(self, fitsfile='ModelSceneCube.fits'):
     """
     Write model cube to a FITS file
     """
     header = self.grid.wcs_info()
     fits.writeto(fitsfile, np.rollaxis(self.int, 2), header, clobber=True)
     fits.append(fitsfile, self.wave)
def save_reduced_data(input_file,sp,unc,wave=None):
	print 'Saving extraction as %s'%(os.path.splitext(input_file)[0]+'_reduced.fits')
	#load up file to grab header to append reduction information to
	hdulist=loadfits(input_file)
	head=hdulist[0].header
	head['ORDERS'] = (sp.shape[1], 'Number of orders reduced')
	#append header field 'how many orders',len(ord)
	head['REDUTIME'] = (strftime("%c"), 'When reduced')
	#append header field 'when reduced', time.strftime("%c")
	head['comment'] = 'data saved as (ext,unc,wave) for each order'
	
	if wave==None:
		wave=np.arange(sp.shape[0])
	data=[[]]*(sp.shape[1]+1)
	for i in np.arange(sp.shape[1]):
		data=np.vstack((sp[:,i],unc[:,i],wave[i,:][::-1]))
		if i==0:
			head['ORDER']=((i+1),'Order number')
			pyfits.writeto(os.path.splitext(input_file)[0]+'_reduced.fits', data,head, clobber=True)
		else:
			head['ORDER']=((i+1),'Order number')
			pyfits.append(os.path.splitext(input_file)[0]+'_reduced.fits', data,head)
	#save file as original file + _reduced
	#ie. if aug008811.fits was the file - this becomes
	#aug008811_reduced.npy
	hdulist.close()
def write_tblspec(flux, wave, loglam, objtype):
    data = dict(flux=flux)    
    hdr = dict(FLUXUNIT='erg/s/cm^2/A')
    if wave is None:
        if loglam:
            hdr['LOGLAM'] = loglam
            hdr['CRVAL1'] = np.log10(wave1D[0])
            hdr['CDELT1'] = np.log10(1+dwave/wave1D[0])
        else:
            if loglam is not None:
                hdr['LOGLAM'] = loglam
            hdr['CRVAL1'] = wave1D[0]
            hdr['CDELT1'] = dwave
    else:
        if loglam is None or not loglam:
            data['wavelength'] = wave
        else:
            data['loglam'] = wave

    if type(objtype) == str:
        hdr['OBJTYPE'] = objtype
    else:
        data['objtype'] = objtype['OBJTYPE']
    
    filename = get_next_filename()
    fits.writeto(filename, data, header=hdr, clobber=True)

    return filename
def write_imgspec(flux, wave, loglam, objtype):
    hdr = dict(FLUXUNIT='erg/s/cm^2/A')
    if wave is None:
        if loglam:
            hdr['LOGLAM'] = loglam
            hdr['CRVAL1'] = np.log10(wave1D[0])
            hdr['CDELT1'] = np.log10(1+dwave/wave1D[0])
        else:
            if loglam is not None:
                hdr['LOGLAM'] = loglam
            hdr['CRVAL1'] = wave1D[0]
            hdr['CDELT1'] = dwave

    if type(objtype) == str:
        hdr['OBJTYPE'] = objtype
    
    filename = get_next_filename()
    fits.writeto(filename, flux, header=hdr, clobber=True)
    if wave is not None:
        if loglam:
            fits.append(filename, wave, extname='LOGLAM')
        else:
            fits.append(filename, wave, extname='WAVELENGTH')
            
    if type(objtype) != str:
        fits.append(filename, objtype, extname='TARGETINFO')

    return filename
Exemple #21
0
def immultiply( image, scalefactor, outfile=None, clobber=False, verbose=False):
    """
    Multiply the image by scalefactor.  If a string is provided for outfile,
    then the resulting scaled image file is written to that file name.  Otherwise, 
    the scaled image data array is returned. 
    """
    import os
    from numpy import ndarray
    import exceptions

    # read in the image
    if isinstance( image, str ):
        if not os.path.isfile( image ) :
            raise exceptions.RuntimeError(
                "The image file %s is not valid."%image )
        im1head = pyfits.getheader( image )
        im1data = pyfits.getdata( image )
        im1head["FLXSCALE"] = (scalefactor,"Flux scaling factor")
    elif isinstance( image, ndarray ):
        im1data = image
        im1head = None
    else : 
        raise  exceptions.RuntimeError("Provide a fits file name or numpy array for each image.")
    
    scaledim =  scalefactor * im1data
    
    if outfile :
        outdir = os.path.split( outfile )[0]
        if outdir : 
            if not os.path.isdir(outdir): 
                os.makedirs( outdir )
        pyfits.writeto( outfile, scaledim, header=im1head,clobber=clobber )
        return( outfile )
    else : 
        return( scaledim )
Exemple #22
0
    def main(self, list_of_files):

        self.print_header()
        log.info('Processing data')
        list_of_files = sorted(list_of_files)

        for filename in list_of_files:
            prefix = "cr"

            # Get joined data
            data = pyfits.getdata(filename)

            # Build header
            header = pyfits.getheader(filename)

            # Remove cosmic rays and hot pixels
            data, header, prefix = self.remove_cosmic_rays(
                data, header, prefix)

            # Writing file
            header.add_history('Cosmic Rays removed.')
            path, filename = os.path.split(filename)
            pyfits.writeto(os.path.join(path, prefix + filename), data,
                           header, clobber=True)

            log.info("\n All done!")
Exemple #23
0
def part_5():

	for i in range(len(object_list)):
		
		outfile = open(object_list[i]+'.CMsub.file.list','w')
		outfile.write('# '+'%16s'%'file name\n')

		infile = open('star_coord_'+object_list[i]+'.dat','r')
		for line in infile.readlines():
			if not line.startswith('#'):
				entries = line.split()
				filename,x_cen,y_cen = str(entries[0]),float(entries[1]),float(entries[2])

				f = fits.open(filename)
				scidata = f[0].data
				
				radial_bins = np.linspace(0,1000,1001)
				radial_displ = np.zeros([1024,1024])
				for y in range(1024):
					for x in range(1024):
						radial_displ[y,x] = int(sqrt((y-y_cen)**2+(x-x_cen)**2))

				max_radius = int(np.max(radial_bins))
				concen_radius = np.linspace(0,max_radius,max_radius+1)
				concen_median = np.zeros(max_radius+1)

				concen_values = [[]]
				for j in range(max_radius+1):
					concen_values.append([])
					
				for y in range(1024):
					for x in range(1024):
						concen_values[int(radial_displ[y,x])].append(scidata[y,x])
				for j in range(max_radius+1):
					concen_median[j] = np.median(concen_values[j])
					
				cm_pix_val = np.zeros([1024,1024])
				for y in range(1024):
					for x in range(1024):
						
						cm_pix_val[y,x] = concen_median[int(radial_displ[y,x])]
						
				newfilename = filename[:len(filename)-8]+'concen_median.fits'
				os.system('rm -f '+newfilename)
				fits.writeto(newfilename,cm_pix_val,f[0].header)
				
				CMsubtracted = scidata -cm_pix_val
				newfilename = filename[:len(filename)-8]+'CMsubtracted.fits'
				os.system('rm -f '+newfilename)
				fits.writeto(newfilename,CMsubtracted,f[0].header)
				
				f.close()
				print('wrote '+newfilename)
				outfile.write(newfilename+'\n')

		outfile.close()
		infile.close()

	part_3(5)
	part_4(5)
Exemple #24
0
def write_fits(fitsfilename, array, header=None, precision=np.float32,
               verbose=True):
    """
    Write array and header into FTIS file.

    If there is a previous file with the same filename then it's replaced.

    Parameters
    ----------
    fitsfilename : string
        Full path of the fits file to be written.
    array : numpy ndarray
        Array to be written into a fits file.
    header : numpy ndarray, optional
        Array with header.
    precision : numpy dtype, optional
        Float precision, by default np.float32 or single precision float.
    verbose : bool, optional
        If True prints message.

    """
    array = array.astype(precision, copy=False)
    if not fitsfilename.endswith('.fits'):
        fitsfilename += '.fits'

    if os.path.exists(fitsfilename):
        os.remove(fitsfilename)
        ap_fits.writeto(fitsfilename, array, header)
        if verbose:
            print("Fits file successfully overwritten")
    else:
        ap_fits.writeto(fitsfilename, array, header)
        if verbose:
            print("Fits file successfully saved")
Exemple #25
0
def dark_subtract(imlist, MasterDark, SaveSteps=False):
    '''Dark subtract the images in imlist using the previously
       combined Master Dark frame. Scale the Master Dark up to 
       the exptime of each image before subtraction.

       Set SaveSteps = True to write each dark-subtracted image 
       to a new file named [input_image].ds.fits. This option is 
       good for checking the reduction at each step. 
       Default is to overwrite input image files.
    '''
    # read in Master Dark frame
    Dark = fits.getdata(MasterDark)

    # get the date and time
    now = time.strftime('%c')
    for image in imlist:
        im,hdr = fits.getdata(image, header=True)
        # write a keyword to header 
        darkstr = 'Dark subtracted: %s' % now
        hdr['DARKSUB'] = darkstr

        # output file name
        if SaveSteps:
            output = ''.join([os.path.splitext(image)[0], '.ds.fits'])
        else:            
            output = image

        # scale Dark up to exptime of image
        exptime = hdr['EXPTIME']
        ScaledDark = Dark * exptime
        new = im - ScaledDark
        fits.writeto(output, new, header=hdr, clobber=True)
    
    return
def export_to_fits(cli):
	
	#
	# Read in the model:
	#
	file = filename(cli, "plot")
	file += ".rtout"
	model = ModelOutput(file)
	
	
	#
	# Write fits file:
	#
	if(cli.mode == "images"):
		
		los = [0 for i in range(3)]
		los[0] = 'x'
		los[1] = 'y'
		los[2] = 'z'

		for k in range(0, 3):
			image = model.get_image(distance=1*pc, units='MJy/sr', inclination=0, component='total', group=k)
			Nwavelength=image.val.shape[2]
			for i in range(0, Nwavelength):
				file = filename(cli, "fits")
				file += "_wavelength=" + str(image.wav[i]) + "micron_los=" + los[k] + ".fits"
				fits.writeto(file, image.val[:, :, i], clobber=True)
				if(cli.verbose):
					print("  The fits file was written to", file)

	else:
		print("ERROR: The specified mode", mode, "is not available. Use 'images' only.")
def write_mle_tofits(filename='', vel_widths=None, dgrs=None,
        likelihoods=None, clobber=False):

    from astropy.io import fits

    print('Writing likelihood grid to file:')
    print(filename)

    header = fits.Header()
    header['NAXIS'] = 2
    header['CTYPE1'] = 'WIDTHS'
    header['CTYPE2'] = 'DGR'
    header['CRPIX1'] = 0
    header['CRPIX2'] = 0
    header['CRVAL1'] = vel_widths[0]
    header['CRVAL2'] = dgrs[0]

    try:
        header['CDELT1'] = vel_widths[1] - vel_widths[0]
    except IndexError:
        header['CDELT1'] = 1
    try:
        header['CDELT2'] = dgrs[1] - dgrs[0]
    except IndexError:
        header['CDELT2'] = 1

    fits.writeto(filename,
                 likelihoods,
                 header,
                 clobber=clobber)
def write_data(data, header, filename):

    pf.writeto(filename,
            data,
            header = header,
            clobber = True,
            output_verify = 'fix')
Exemple #29
0
def bfixpix(image_file, mask_file, outsuffix='_f', msksuffix='_s'):
    """
    Inputs
    ---------
    image_file : string
        input image file to fix bad pixels on

    mask_file : string
        mask file (0 == good pixels, >0 == bad pixels

    outsuffix : string
        suffix for fixed image. default = '_f'

    msksuffix : string
        suffix for bad pixels significance mask. default = '_s'
    """
    outf = image_file.replace('.fits', outsuffix + '.fits')
    outm = image_file.replace('.fits', msksuffix + '.fits')
    
    util.rmall([outf, outm])
    print("bfixpix: {0} -> {1}".format(image_file, outf))

    # fetch the image, fetch the mask
    img, hdr = fits.getdata(image_file, header=True)
    msk = fits.getdata(mask_file)

    # median the image
    medimg = ndimage.median_filter(img, 3, mode='nearest')

    # generate the pixel files
    outf_img = np.where(msk == 0, img, medimg)
    outm_img = np.where(msk == 1, (img - medimg), 0)

    fits.writeto(outf, outf_img, hdr)
    fits.writeto(outm, outm_img, hdr)
def significance_image(infile,
                       outfile,
                       theta,
                       overwrite):
    """Make correlated significance image.

    TODO: describe
    """
    from astropy.io import fits
    from gammapy.image import disk_correlate
    from gammapy.stats import significance_on_off

    log.info('Reading {0}'.format(infile))
    hdus = fits.open(infile)
    n_on = hdus['On'].data
    n_off = hdus['Off'].data
    a_on = hdus['OnExposure'].data
    a_off = hdus['OffExposure'].data

    log.info('Correlating n_on and a_on map')
    theta = theta / hdus['On'].header['CDELT2']
    n_on = disk_correlate(n_on, theta)
    a_on = disk_correlate(a_on, theta)

    log.info('Computing significance map')
    alpha = a_on / a_off
    significance = significance_on_off(n_on, n_off, alpha)

    log.info('Writing {0}'.format(outfile))
    fits.writeto(outfile, data=significance, header=hdus['On'].header, clobber=overwrite)
Exemple #31
0
    def create_mask(self, img, seeing_fwhm, step=0):

        #
        remove_border = True

        # sextractor takes care of bad pixels

        # if seeing_fwhm is not None and seeing_fwhm > 0:
        #    sex.config['SEEING_FWHM'] = seeing_fwhm * sex.config['PIXEL_SCALE']

        if remove_border:
            weigthmap = 'weights4rms.fits'

            # Create weight map, remove n pixs from either side
            # using a Hannig filter
            # npix = 90
            # w1 = npix
            # w2 = npix
            # wmap = numpy.ones_like(sf_data[0])

            # cos_win1 = numpy.hanning(2 * w1)
            # cos_win2 = numpy.hanning(2 * w2)

            # wmap[:,:w1] *= cos_win1[:w1]
            # wmap[:,-w1:] *= cos_win1[-w1:]
            # wmap[:w2,:] *= cos_win2[:w2, numpy.newaxis]
            # wmap[-w2:,:] *= cos_win2[-w2:, numpy.newaxis]

            # Take the number of combined images from the combined image
            wm = img[2].data.copy()
            # Dont search objects where nimages < lower
            # FIXME: this is a magic number
            # We ignore objects in regions where we have less
            # than 10% of the images
            lower = wm.max() // 10
            border = (wm < lower)
            fits.writeto(weigthmap, border.astype('uint8'), overwrite=True)

            # sex.config['WEIGHT_TYPE'] = 'MAP_WEIGHT'
            # FIXME: this is a magic number
            # sex.config['WEIGHT_THRESH'] = 50
            # sex.config['WEIGHT_IMAGE'] = weigthmap
        else:
            border = None

        data_res = img[0].data
        bkg = sep.Background(data_res)
        data_sub = data_res - bkg

        self.logger.info('Runing source extraction in previous result')
        objects, objmask = sep.extract(data_sub,
                                       1.5,
                                       err=bkg.globalrms,
                                       mask=border,
                                       segmentation_map=True)
        fits.writeto(name_segmask(step), objmask, overwrite=True)

        # # Plot objects
        # # FIXME, plot sextractor objects on top of image
        # patches = []
        # fwhms = []
        # nfirst = 0
        # catalog_f = sopen(sex.config['CATALOG_NAME'])
        # try:
        #     star = catalog_f.readline()
        #     while star:
        #         flags = star['FLAGS']
        #         # ignoring those objects with corrupted apertures
        #         if flags & sexcatalog.CORRUPTED_APER:
        #             star = catalog_f.readline()
        #             continue
        #         center = (star['X_IMAGE'], star['Y_IMAGE'])
        #         wd = 10 * star['A_IMAGE']
        #         hd = 10 * star['B_IMAGE']
        #         color = 'red'
        #         e = Ellipse(center, wd, hd, star['THETA_IMAGE'], color=color)
        #         patches.append(e)
        #         fwhms.append(star['FWHM_IMAGE'])
        #         nfirst += 1
        #         # FIXME Plot a ellipse
        #         star = catalog_f.readline()
        # finally:
        #     catalog_f.close()
        #
        # p = PatchCollection(patches, alpha=0.4)
        # ax = self._figure.gca()
        # ax.add_collection(p)
        # self._figure.canvas.draw()
        # self._figure.savefig('figure-segmentation-overlay_%01d.png' % step)
        #
        # self.figure_fwhm_histogram(fwhms, step=step)
        #
        # # mode with an histogram
        # hist, edges = numpy.histogram(fwhms, 50)
        # idx = hist.argmax()
        #
        # seeing_fwhm = 0.5 * (edges[idx] + edges[idx + 1])
        # if seeing_fwhm <= 0:
        #     _logger.warning(
        #         'Seeing FHWM %f pixels is negative, reseting', seeing_fwhm)
        #     seeing_fwhm = None
        # else:
        #     _logger.info('Seeing FHWM %f pixels (%f arcseconds)',
        #                  seeing_fwhm, seeing_fwhm * sex.config['PIXEL_SCALE'])
        # objmask = fits.getdata(name_segmask(step))

        return objmask, seeing_fwhm
Exemple #32
0
    plt.xlabel('g magnitude')

    pl.show()
    pl.clf()

    for i, x in enumerate(flux):
        pl.semilogy(wave, x, label=str(meta['r'][i]))

    pl.legend(ncol=4)

    pl.show()

if save:
    meta.write(meta_name, format='ascii', overwrite=True)

    hdr = fits.Header()

    hdr['EXTNAME'] = 'WAVELENGTH'
    hdr['BUNIT'] = 'Angstrom'

    fits.writeto(infile, wave, header=hdr, overwrite=True)

    hdr['EXTNAME'] = 'FLUX'
    hdr['BUNIT'] = '10^-17 erg/(s*cm^2*Angstrom)'  # Satisifes FITS standard AND Astropy-compatible.

    fits.append(infile, flux, header=hdr)

    print('\n\nWritten to %s.' % infile)

print('\n\nDone.\n\n')
Exemple #33
0
    def process_advanced(self,
                         images_info,
                         result,
                         step,
                         target_is_sky=True,
                         maxsep=5.0,
                         nframes=6,
                         extinction=0,
                         method=None,
                         method_kwargs=None):

        seeing_fwhm = None
        baseshape = (EMIR_NAXIS2, EMIR_NAXIS1)
        target_info = [iinfo for iinfo in images_info if iinfo.valid_target]
        sky_info = [iinfo for iinfo in images_info if iinfo.valid_sky]
        self.logger.info('Step %d, generating segmentation image', step)

        objmask, seeing_fwhm = self.create_mask(result, seeing_fwhm, step=step)

        for frame in target_info:
            frame.objmask = name_object_mask(frame.label, step)
            self.logger.info('Step %d, create object mask %s', step,
                             frame.objmask)
            frame.objmask_data = objmask[frame.valid_region]
            fits.writeto(frame.objmask, frame.objmask_data, overwrite=True)

        if not target_is_sky:
            # Empty object mask for sky frames
            bogus_objmask = numpy.zeros(baseshape, dtype='uint8')

            for frame in sky_info:
                frame.objmask_data = bogus_objmask

        self.logger.info("Step %d, SF: compute superflat", step)
        sf_arr = self.compute_superflat(sky_info,
                                        segmask=objmask,
                                        step=step,
                                        method=method,
                                        method_kwargs=method_kwargs)

        # Apply superflat
        self.logger.info("Step %d, SF: apply superflat", step)
        for iinfo in images_info:
            self.correct_superflat(iinfo, sf_arr, step=step, save=True)

        self.logger.info('Step %d, advanced sky correction (SC)', step)
        self.compute_advanced_sky(target_info,
                                  objmask,
                                  skyframes=sky_info,
                                  target_is_sky=target_is_sky,
                                  maxsep=maxsep,
                                  nframes=nframes,
                                  step=step,
                                  method=method,
                                  method_kwargs=method_kwargs)

        # Combining the images
        self.logger.info("Step %d, Combining the images", step)
        # FIXME: only for science
        result = self.combine_frames(target_info,
                                     extinction,
                                     step=step,
                                     method=method,
                                     method_kwargs=method_kwargs)
        return result
Exemple #34
0
    def compute_advanced_sky_for_frame(self,
                                       frame,
                                       skyframes,
                                       step=0,
                                       save=True,
                                       method=None,
                                       method_kwargs=None):
        self.logger.info('Correcting sky in frame %s', frame.lastname)
        self.logger.info('with sky computed from frames')
        for i in skyframes:
            self.logger.info('%s', i.flat_corrected)

        data = []
        scales = []
        masks = []
        # handle the FITS file to close it finally
        desc = []
        try:
            for i in skyframes:
                filename = i.flat_corrected
                hdulist = fits.open(filename, mode='readonly', memmap=True)

                data.append(hdulist['primary'].data[i.valid_region])
                desc.append(hdulist)
                scales.append(numpy.median(data[-1]))
                if i.objmask_data is not None:
                    masks.append(i.objmask_data)
                    self.logger.debug('object mask is shared')
                elif i.objmask is not None:
                    hdulistmask = fits.open(i.objmask,
                                            mode='readonly',
                                            memmap=True)
                    masks.append(hdulistmask['primary'].data)
                    desc.append(hdulistmask)
                    self.logger.debug('object mask is particular')
                else:
                    self.logger.warn('no object mask for %s', filename)

            self.logger.debug("computing background with %d frames using '%s'",
                              len(data), method.__name__)
            sky, _, num = method(data, masks, scales=scales, **method_kwargs)

            with fits.open(frame.lastname) as hdulist:
                data = hdulist['primary'].data
                valid = data[frame.valid_region]

                if frame.objmask_data is not None:
                    self.logger.debug('object mask defined')
                    msk = frame.objmask_data
                    skymedian = numpy.median(valid[msk == 0])
                else:
                    self.logger.debug('object mask empty')
                    skymedian = numpy.median(valid)
                self.logger.debug('scaling with skymedian %s', skymedian)
            sky *= skymedian

        finally:
            # Closing all FITS files
            for hdl in desc:
                hdl.close()

        if numpy.any(num == 0):
            # We have pixels without
            # sky background information
            self.logger.warning(
                'pixels without sky information when correcting %s',
                frame.flat_corrected)
            binmask = num == 0
            # FIXME: during development, this is faster
            # sky[binmask] = sky[num != 0].mean()

            # To continue we interpolate over the patches
            narray.fixpix2(sky, binmask, out=sky, iterations=1)

            name = name_skybackgroundmask(frame.label, step)
            fits.writeto(name, binmask.astype('int16'), overwrite=True)

        name_sky = name_skybackground(frame.label, step)
        fits.writeto(name_sky, sky, overwrite=True)

        dst = name_skysub_proc(frame.label, step)
        prev = frame.lastname
        shutil.copyfile(prev, dst)
        frame.lastname = dst

        with fits.open(frame.lastname, mode='update') as hdulist:
            data = hdulist['primary'].data
            valid = data[frame.valid_region]
            valid -= sky
            # ToDo: ad hoc sky correction
            adhoc_correction = False
            if adhoc_correction:
                print('---')
                print(frame)
                print('*** adhoc_correction ***')
                skycorr = self.sky_adhoc_correction(valid, frame.objmask_data)
                valid -= skycorr
from astropy.io import fits

from hyperion.model import ModelOutput
from hyperion.util.constants import pc

# Retrieve image cube as before
m = ModelOutput('simple_cube.rtout')
image = m.get_image(inclination=0, distance=300 * pc, units='MJy/sr')

# The image extracted above is a 3D array. We can write it out to FITS.
# We need to swap some of the directions around so as to be able to use
# the ds9 slider to change the wavelength of the image.
fits.writeto('simple_cube.fits',
             image.val.swapaxes(0, 2).swapaxes(1, 2),
             clobber=True)

# We can also just output one of the wavelengths
fits.writeto('simple_cube_slice.fits', image.val[:, :, 1], clobber=True)
Exemple #36
0
    def combine_frames(self,
                       frames,
                       extinction,
                       out=None,
                       step=0,
                       method=None,
                       method_kwargs=None):
        self.logger.debug('Step %d, opening sky-subtracted frames', step)

        def fits_open(name):
            """Open FITS with memmap in readonly mode"""
            return fits.open(name, mode='readonly', memmap=True)

        frameslll = [
            fits_open(frame.lastname) for frame in frames if frame.valid_target
        ]
        self.logger.debug('Step %d, opening mask frames', step)
        mskslll = [
            fits_open(frame.resized_mask) for frame in frames
            if frame.valid_target
        ]

        self.logger.debug("Step %d, combining %d frames using '%s'", step,
                          len(frameslll), method.__name__)
        try:
            extinc = [
                pow(10, -0.4 * frame.metadata['airmass'] * extinction)
                for frame in frames if frame.valid_target
            ]
            data = [i['primary'].data for i in frameslll]
            masks = [i['primary'].data for i in mskslll]
            headers = [i['primary'].header for i in frameslll]

            out = method(data,
                         masks,
                         scales=extinc,
                         dtype='float32',
                         out=out,
                         **method_kwargs)

            base_header = headers[0]
            hdu = fits.PrimaryHDU(out[0], header=base_header)
            hdu.header['history'] = "Combined %d images using '%s'" % (
                len(frameslll), method.__name__)
            hdu.header['history'] = 'Combination time {}'.format(
                datetime.datetime.utcnow().isoformat())
            for img in frameslll:
                hdu.header['history'] = "Image {}".format(
                    img[0].header['uuid'])
            prevnum = base_header.get('NUM-NCOM', 1)
            hdu.header['NUM-NCOM'] = prevnum * len(frameslll)
            hdu.header['NUMRNAM'] = 'FullDitheredImagesRecipe'
            hdu.header['UUID'] = str(uuid.uuid1())
            hdu.header['OBSMODE'] = 'FULL_DITHERED_IMAGE'
            # Headers of last image
            hdu.header['TSUTC2'] = headers[-1]['TSUTC2']

            varhdu = fits.ImageHDU(out[1], name='VARIANCE')
            num = fits.ImageHDU(out[2].astype('uint8'), name='MAP')

            result = fits.HDUList([hdu, varhdu, num])
            # saving the three extensions
            fits.writeto('result_i%0d.fits' % step, out[0], overwrite=True)
            fits.writeto('result_i%0d_var.fits' % step, out[1], overwrite=True)
            fits.writeto('result_i%0d_npix.fits' % step,
                         out[2],
                         overwrite=True)

            result.writeto('result_i%0d_full.fits' % step, overwrite=True)
            return result

        finally:
            self.logger.debug('Step %d, closing sky-subtracted frames', step)
            for f in frameslll:
                f.close()
            self.logger.debug('Step %d, closing mask frames', step)
            for f in mskslll:
                f.close()