def skysub(name,maskA,maskB,outdir=None): f = pyfits.open(name) data = f[0].data hdr = f[0].header # Skysub two halves separately datA = np.ma.masked_array(data,maskA) valA = np.ma.median(datA) hdr['SKYVALA'] = (valA,'Median sky of A') hdr['SKYVARA'] = (np.ma.std(datA)**2,'Sky variance of A') hdr['SKYRMSA'] = (np.sqrt(1.0/datA.count()*np.ma.sum(datA**2)),'RMS sky of A') datB = np.ma.masked_array(data,maskB) valB = np.ma.median(datB) hdr['SKYVALB'] = (valB,'Median sky of B') hdr['SKYVARB'] = (np.ma.std(datB)**2,'Sky variance of B') hdr['SKYRMSB'] = (np.sqrt(1.0/datB.count()*np.ma.sum(datB**2)),'RMS sky of B') ydim = np.shape(data)[1]/2 data[ydim:,:] = data[ydim:,:] - valA data[0:ydim,:] = data[0:ydim,:] - valB if outdir is None: outname = name else: outname = os.path.join(outdir,name) print 'Applying skymasks to %s' % outname pyfits.writeto(outname,data,header=hdr,clobber=True) return outname
def make_voronoi_intens(targetSN, w1, w2): """ Make image""" image = "collapsed_w{0}_{1}.fits".format(w1, w2) intens = pf.getdata(image) extent = calc_extent(image) vordata = pf.getdata("voronoi_sn{0}_w{1}_{2}.fits".format(targetSN, w1, w2)) vordata = np.ma.array(vordata, mask=np.isnan(vordata)) bins = np.unique(vordata)[:-1] combined = np.zeros_like(intens) combined[:] = np.nan for j, bin in enumerate(bins): idx, idy = np.where(vordata == bin) flux = intens[idx,idy] combined[idx,idy] = np.nanmean(flux) vmax = np.nanmedian(intens) + 4 * np.nanstd(intens) fig = plt.figure(1) plt.minorticks_on() make_contours() plt.imshow(combined, cmap="cubehelix_r", origin="bottom", vmax=vmax, extent=extent, vmin=0) plt.xlabel("X [kpc]") plt.ylabel("Y [kpc]") cbar = plt.colorbar() cbar.set_label("Flux [$10^{-20}$ erg s$^{-1}$ cm$^{-2}$]") plt.savefig("figs/intens_sn{0}.png".format(targetSN), dpi=300) pf.writeto("figs/intens_sn{0}.fits".format(targetSN), combined, clobber=True) return
def write_fits(self, path, data, fits_file): if os.path.isfile(fits_file): os.remove(fits_file) pyfits.writeto(fits_file, data) if self.do_extract: self.extract(path, fits_file)
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 imcopy(infile, outfile, dim = None): print >> sys.stdout, 'Copying ', infile, ' ----> ', outfile if len(outfile.split('[')) == 1: subprocess.call('cp ' + infile + ' ' + outfile, shell = True) else: if not dim: print >> sys.stderr, 'Error : for image section copying, dim parameter cannot be None. Exiting.' sys.exit(-1) header = pyfits.getheader(infile) output = numpy.zeros((dim, dim), dtype = numpy.float32) try: f1 = pyfits.open(infile) except: print >> sys.stderr, 'Error : Not able to open ', infile, '. Exiting.' sys.exit(-1) x1, x2 = int(outfile.split('[')[1].replace(']', '').split(',')[0].split(':')[0] ), int(outfile.split('[')[1].replace(']', '').split(',')[0].split(':')[1]) y1, y2 = int(outfile.split('[')[1].replace(']', '').split(',')[1].split(':')[0] ), int(outfile.split('[')[1].replace(']', '').split(',')[1].split(':')[1]) output[x1:x2, y1:y2] = f1[0].data outfile = outfile.split('[')[0] subprocess.call('rm -f ' + outfile, shell = True) pyfits.writeto(outfile, output, header = header) return outfile
def write_spectrum(data, header, orig_fn): new_fn = orig_fn.replace('.fits', '_smooth.fits') pyfits.writeto(new_fn, data, header=header, clobber=True) return new_fn
def combine_seg_map(self, filt, out_dir): """Combines bright and faint segmentation maps. Regions belonging to bright objects are expanded by 5 pixels""" cat_name = out_dir + '/' + filt + '_clean.cat' bright_name = out_dir + '/' + filt + '_bright_seg_map.fits' faint_name = out_dir + '/' + filt + '_faint_seg_map.fits' hdu1 = pyfits.open(bright_name) hdu2 = pyfits.open(faint_name) br = hdu1[0].data ft = hdu2[0].data hdu2.close() hdu1.close() cat = Table.read(cat_name, format='ascii.basic') new_seg = br # Expand bright regions by 5 pixels q, = np.where(cat['IS_BRIGHT'] == 1) for i in q: new_seg = fn.seg_expand(new_seg, buff=5, val=int(i) + 1, set_to=int(i) + 1) # +1 to account for renumbering q, = np.where(cat['IS_BRIGHT'] == 0) s = ft.shape for i in q: for j in range(s[0]): pix, = np.where((ft[j, :] == cat['OLD_NUMBER'][i]) & (new_seg[j, :] == 0)) new_seg[j][pix] = cat['NUMBER'][i] + 1 new_seg_name = out_dir + '/' + filt + '_comb_seg_map.fits' print "Bright faint combined seg map created at", new_seg_name pyfits.writeto(new_seg_name, new_seg, clobber=True) os.remove(bright_name) os.remove(faint_name)
def _deripple (infile, outfile, start, end, period): """ Script to run deripple under pyraf. # Start pyraf pyraf # Set deripplepath to where the deripple.py module is # located. set deripplepath=/home/nzarate/deripple/ # Define the pyraf task task deripple = deripplepath$deripple.cl # Load the task into the pyraf environment deripple # Run the task deripple(infile='data/xobj_comb.fits',outfile='out.fits',start=550 ,end=610 ,period=2) """ infits = pf.open(infile) spectrum = infits['SCI'].data print 'SPEin',spectrum.shape outspectrum = deripple (spectrum,start,end,period) # Create a new file with the input PHU and the SCI header phu = infits[0] pf.writeto(outfile, None, header=infits[0].header, clobber=True) pf.append(outfile, outspectrum, header=infits['SCI'].header) infits.close() print 'SPEout:::',outspectrum.shape
def test_exampleimage(): """Test application of model compared to an independent implementation that was run on the example image. """ shiftcoeff = 1.e-7 #n, r0, t0, rx, tx, r, t, alpha cd = galsim.cdmodel.PowerLawCD( 5, 2. * shiftcoeff, shiftcoeff, 1.25 * shiftcoeff, 1.25 * shiftcoeff, 0.75 * shiftcoeff, 0.5 * shiftcoeff, 0.3) # model used externally to bring cdtest1 to cdtest2 image_orig = galsim.fits.read("fits_files/cdtest1.fits") # unprocessed image image_proc = galsim.fits.read("fits_files/cdtest2.fits") # image with cd model applied with # other library # Calculate the test image image_plcd = cd.applyForward(image_orig) # For debugging: make if True in block below to output difference image. # Compare to fits_files/cdtest[1-2].fits above if False: import pyfits pyfits.writeto( "junk_test_cdmodel_exampleimage_difference.fits", (image_proc - image_plcd).array, clobber=True) # These images have a large flux per pixel, so make the typical flux per pixel in each image # closer to O(1) for a more transparently meaningful decimal order in the test norm = 2.64 / np.std(image_orig.array) image_proc *= norm image_plcd *= norm # Compare np.testing.assert_array_almost_equal( image_proc.array, image_plcd.array, 4, "Externally and internally processed image unequal")
def save_fits_image(file_name, image, cell_size_deg, ra0, dec0, freq_hz): # FIXME-BM get ra0, dec0 from the oskar vis file?! data = numpy.reshape(image, (1, 1, image.shape[0], image.shape[1])) header = pyfits.header.Header() header.append(("BUNIT", "Jy/beam")) header.append(("CTYPE1", "RA--SIN")) header.append(("CRVAL1", ra0)) header.append(("CDELT1", -cell_size_deg)) header.append(("CUNIT1", "deg")) # Note: Assumes even image dims and that image pixels start at 1 (not 0) header.append(("CRPIX1", image.shape[1] / 2 + 1)) header.append(("CTYPE2", "DEC-SIN")) header.append(("CRVAL2", dec0)) header.append(("CDELT2", cell_size_deg)) header.append(("CRPIX2", image.shape[0] / 2 + 1)) header.append(("CUNIT2", "deg")) header.append(("CTYPE3", "FREQ")) header.append(("CRVAL3", freq_hz)) header.append(("CDELT3", 1.0)) header.append(("CRPIX3", 1.0)) header.append(("CUNIT3", "Hz")) header.append(("CTYPE4", "STOKES")) header.append(("CRVAL4", 1.0)) header.append(("CDELT4", 1.0)) header.append(("CRPIX4", 1.0)) header.append(("CUNIT4", "")) if os.path.exists(file_name): print("- WARNING: Overwriting FITS file: %s" % file_name) os.remove(file_name) print("- Saving FITS image:", file_name) pyfits.writeto(file_name, data, header)
def imshift(filename,shifts,center,refFile,name_ext='.al',clobber=False): f = pyfits.open(filename) header = f[0].header header['REF_FILE'] = (os.path.basename(refFile),'Reference file') header['PRE_FILE'] = (os.path.basename(filename),'Filename before shift') header['XSHIFT'] = (shifts[0],'X shift from ref_file') header['YSHIFT'] = (shifts[1],'Y shift from ref_file') header['XCEN'] = (center[0],'X shift from ref_file') header['YCEN'] = (center[1],'Y shift from ref_file') header['PALIGN'] = (True,'Aligned') newName = os.path.splitext(filename) newName = ''.join([newName[0],name_ext,newName[1]]) if shifts[0] != 0 and shifts[1] != 0: newDat = shift(f[0].data,(shifts[0],shifts[1])) else: newDat = f[0].data print filename print '\tShifting (%.2f,%.2f) pixels' % (shifts[0],shifts[1]) print '\tWriting to %s' % newName pyfits.writeto(newName,newDat,header=header,clobber=clobber) return newName
def make_images(model='A', brighten=0, bandsel=['u', 'g', 'r', 'i', 'z', 'Y', 'J', 'H', 'K']): if noisetype == 'realistic': zp = zp_realistic sky = sky_realistic exp = exp_realistic else: zp = zp_flat sky = sky_flat exp = exp_flat print 'Using zeropoints:', zp print 'Using sky values:', sky gals = glob('model%s.galfit'%model) for g in gals: print g os.system('nice galfit %s > %s.out'%(g,g)) imgname = g.replace('.galfit', '') img = pyfits.open(imgname+'.fits') for j, b in enumerate(bands): if b in bandsel: ext = img['MODEL_'+b] print b, j, ext.name zp_factor = 10**(0.4*(zp[j]-29-fade)) ext.data *= zp_factor brighten_factor = 10**(0.4*brighten) if noisetype == 'simple': sigma = numpy.sqrt(sky[j]/exp[j]*brighten_factor)/brighten_factor else: sigma = numpy.sqrt((ext.data+sky[j])/exp[j]*brighten_factor)/brighten_factor ext.data += numpy.random.normal(0.0, 1.0, sigma.shape) * sigma pyfits.writeto(imgname+'_%i%s_%s%i_sigma.fits'%(j+1, b, noisetype[0], brighten), sigma, clobber=True) pyfits.writeto(imgname+'_%i%s_%s%i.fits'%(j+1, b, noisetype[0], brighten), ext.data, clobber=True)
def main(): parser = argparse.ArgumentParser(description='Cross correlate images and return shift necessary to align image2 to image1') parser.add_argument('image1',type=str, help='FITS file of image1') parser.add_argument('image2',type=str, help='FITS file of image2') parser.add_argument('-s',metavar='size',type=int, default=None, help='Specify box size for correlation. Default is the full image, which can be very slow') parser.add_argument('-c',metavar=('x_cen', 'y_cen'),type=int,nargs=2, default=None,help="If '-size' specified, optionally include a center for the box region. Default is the center of image1.") parser.add_argument('-o',type=str,nargs='?',metavar='outfile',const='-1',default=None,help="If '-o' specified, shift image2 and write to [image2].shft.fits. If '-o [filename]', shift image2 and write to [filename].") args = parser.parse_args() print 'Cross-correlating\n\t%s\n\t%s' % (args.image1,args.image2) xcorr_im = xcorr(args.image1,args.image2,size=args.s,center=args.c) print 'Calculating shift' shiftx, shifty = find_shift(xcorr_im) print '\t(%i, %i)' % (shiftx, shifty) # if outfile specified, perform shift of second image if args.o: outfile = args.o if args.o != '-1' else \ os.path.splitext(args.image2)[0] + '.shft.fits' image2, header = pyfits.getdata(args.image2, header=True) image2 = shift(image2, (shifty,shiftx), cval=np.nan) header['SHFT_REF'] = (args.image1, 'Reference image of shift') header['SHFT_X'] = (shiftx, 'X shift pix') header['SHFT_Y'] = (shifty, 'Y shift pix') print 'Performing shift on %s' % args.image2 print '\tWriting to %s' % outfile pyfits.writeto(outfile,image2,header=header,clobber=True) return 0
def invert_image(image, data, header, prefix=None): ext = fits_ext(image) output = prefix + "_negatives.fits" or image.replace(ext,"_negative.fits") newdata = -data pyfits.writeto(output, newdata, header, clobber=True) return output
def copySub(self, other_mapper, dataset, data_id, use_cols, new_template=None): """Copy a subset of a particular file in the directory structure defined by this mapper to the same location in a directory structure defined by some other mapper. @param[in] other_mapper The mapper defining the directory structure to which the file should be copied. @param[in] dataset Type of dataset to get; must be one of the keys in self.mappings. @param[in] data_id A dict of values with which to expand the path template (the first value in self.mappings). @param[in] use_cols A list of columns to copy over (i.e., neglect the others). @param[in] new_template Naming template to use for output catalog, if different from previous. @param[out] outfile The new file name. """ import numpy import pyfits # Read in the catalog. template, reader, writer = self.mappings[dataset] infile = os.path.join(self.full_dir, template % data_id) incat = readCatalog(infile) # Choose the subset of data to save. outcat = numpy.zeros(len(incat), dtype=numpy.dtype(use_cols)) for col in use_cols: outcat[col[0]] = incat[col[0]] # Write to output file. if new_template is None: new_template = template outfile = os.path.join(other_mapper.full_dir, new_template % data_id) pyfits.writeto(outfile + ".fits", outcat, clobber = True) return outfile+'.fits'
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 = pyfits.getheader(FitRunner.cmapfile) except : logging.error('Count map not found.') sys.exit(1) data = pyfits.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 : print "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 pyfits.writeto(folder+"/"+self.TSfits,data,header) print "TS Map saved in "+folder+"/"+self.TSfits
def make_wifes_p08_template(ddir, fn, out_dir, star,rv=0.0): """From a p08 file, create a template spectrum for future cross-correlation. The template is interpolated onto a 0.1 Angstrom grid (to match higher resolution templates. Parameters ---------- ddir: string Data directory for the p08 file fn: string p08 fits filename out_dir: string Output directory """ flux_stamp,wave = read_and_find_star_p08(ddir + '/' + fn) heliocentric_correction = pyfits.getheader(ddir + '/' + fn)['RADVEL'] spectrum,sig = weighted_extract_spectrum(flux_stamp) dell_template = 0.1 wave_template=np.arange(90000)*dell_template + 3000 spectrum_interp = np.interp(wave_template,wave*(1 - (rv - heliocentric_correction)/2.998e5),spectrum) outfn = out_dir + '/' + star + ':' + fn pyfits.writeto(outfn,spectrum_interp,clobber=True)
def save_fits_image(filename, data, header, img1=None, img2=None): """Save a FITS image.""" # Reshape to add the frequency axis data = np.reshape(data, (1, 1, data.shape[0], data.shape[1])) new_hdr = pyfits.header.Header() for i, item in enumerate(header.items()): if item[0] != 'HISTORY': new_hdr.append(item) if img1 and img2: new_hdr.append(('HISTORY', '' * 60)) new_hdr.append(('HISTORY', '-' * 60)) new_hdr.append(('HISTORY', 'Diff created from image1 - image2:')) new_hdr.append(('HISTORY', '- image1 : %s' % img1)) new_hdr.append(('HISTORY', '- image2 : %s' % img2)) new_hdr.append(('HISTORY', '-' * 60)) new_hdr.append(('HISTORY', ' - Max : % .3e' % np.max(data))) new_hdr.append(('HISTORY', ' - Min : % .3e' % np.min(data))) new_hdr.append(('HISTORY', ' - Mean : % .3e' % np.mean(data))) new_hdr.append(('HISTORY', ' - STD : % .3e' % np.std(data))) new_hdr.append(('HISTORY', ' - RMS : % .3e' % np.sqrt(np.mean(data**2)))) new_hdr.append(('HISTORY', '-' * 60)) new_hdr.append(('HISTORY', '' * 60)) if (os.path.exists(filename)): print '+ WARNING, output FITS file already exsits, overwriting.' os.remove(filename) pyfits.writeto(filename, data, new_hdr)
def save(idstr, tractor, zr): mod = tractor.getModelImages()[0] synthfn = 'synth-%s.fits' % idstr print 'Writing synthetic image to', synthfn pyfits.writeto(synthfn, mod, clobber=True) pfn = 'tractor-%s.pickle' % idstr print 'Saving state to', pfn pickle_to_file(tractor, pfn) timg = tractor.getImage(0) data = timg.getImage() ima = dict(interpolation='nearest', origin='lower', vmin=zr[0], vmax=zr[1]) def savepng(pre, img, title=None, **kwargs): fn = '%s-%s.png' % (pre, idstr) print 'Saving', fn plt.clf() plt.imshow(img, **kwargs) if title is not None: plt.title(title) plt.colorbar() plt.gray() plt.savefig(fn) sky = np.median(mod) savepng('data', data - sky, title='Data '+timg.name, **ima) savepng('model', mod - sky, title='Model', **ima) savepng('diff', data - mod, title='Data - Model', **ima)
def split(filename, outdir, prefix): basename, ext = os.path.splitext(filename) basename = os.path.basename(basename) basename = os.path.join(outdir,basename) f = pyfits.open(filename) ydim = f[0].header['NAXIS2']/2 Adat = f[0].data[ydim:,:] #top Bdat = f[0].data[0:ydim,:] #bot Afile = ''.join([basename,'_A',ext]) Bfile = ''.join([basename,'_B',ext]) hdr = f[0].header hdr['WOLLY'] = ('A','Image half') hdr['FILENUM'] = (get_filenum(filename,prefix),'Observation number') pyfits.writeto(Afile,Adat,header=hdr,clobber=True) hdr['WOLLY'] = ('B','Image half') pyfits.writeto(Bfile,Bdat,header=hdr,clobber=True) return (Afile, Bfile)
def FixTHINGS(imageIn, imageOut): print sys.stdout.write('Fixing file %s ... ' % imageIn) sys.stdout.flush() if imageOut != imageIn: hdu = pf.open(imageIn) else: hdu = pf.open(imageIn, mode='update') dataNew = hdu[0].data[0,0,:,:] del hdu[0].header['CTYPE3']; del hdu[0].header['CDELT3']; del hdu[0].header['CRVAL3'] del hdu[0].header['CRPIX3']; del hdu[0].header['CROTA3'] del hdu[0].header['CTYPE4']; del hdu[0].header['CDELT4']; del hdu[0].header['CRVAL4'] del hdu[0].header['CRPIX4']; del hdu[0].header['CROTA4'] if imageOut != imageIn: if os.path.exists(imageOut): os.remove(imageOut) pf.writeto(imageOut, dataNew, hdu[0].header) else: hdu[0].data = dataNew hdu.flush() print 'Done' print return
def calculate_noise_cube(cube=None, velocity_axis=None, velocity_noise_range=[-110,-90,90,110], header=None, Tsys=30., filename=None): """ Calcuates noise envelopes for each pixel in a cube """ import numpy as np import pyfits as pf from mycoords import make_velocity_axis if velocity_axis is None: velocity_axis = make_velocity_axis(header) noise_cube = np.zeros(cube.shape) for i in range(cube.shape[1]): for j in range(cube.shape[2]): profile = cube[:,i,j] noise = calculate_noise(profile, velocity_axis, velocity_noise_range) #noise = 0.1 # measured in line free region noise_cube[:,i,j] = calculate_noise_scale(Tsys, profile, noise=noise) if filename is not None: pf.writeto(filename, noise_cube, header=header) return noise_cube
def ccdgap(name): fimg = pft.open(name) prihdr = fimg[0].header scidata = fimg[0].data n1 = prihdr['NAXIS1'] n2 = prihdr['NAXIS2'] #below are the 4 coordinates of edge of the ccd gap a = ccd_locate(scidata)[0]-4;b = ccd_locate(scidata)[1]+2;c = ccd_locate(scidata)[2]-2;d = ccd_locate(scidata)[3]+3 e = n2 #ccd height gap1_part1 = (scidata[:,a-6:a-1].sum(axis=1))/5.0 gap1_part2 = (scidata[:,b+1:b+6].sum(axis=1))/5.0 gap2_part1 = (scidata[:,c-6:c-1].sum(axis=1))/5.0 gap2_part2 = (scidata[:,d+1:d+6].sum(axis=1))/5.0 grad1 = (gap1_part2-gap1_part1)/((b-a)+5.0) grad2 = (gap2_part2-gap2_part1)/((d-c)+5.0) for i in range(a,b): scidata[:,i] = grad1*((i-a)+2)+gap1_part1 for i in range(c,d): scidata[:,i] = grad2*((i-c)+2)+gap2_part1 namec = "c"+name pft.writeto(namec,data=scidata,header=prihdr,clobber=True) fimg.close() os.system('mv %s history/' % (name)) return
def fits_write(sim_dir, filename, image): pathname = join(sim_dir, filename) header = pyfits.header.Header([('SIMPLE', True), ('NAXIS', 2), ('NAXIS1', image.shape[0]), ('NAXIS2', image.shape[1])]) if (os.path.exists(pathname)): os.remove(pathname) pyfits.writeto(pathname, image, header)
def cutout(fpC, ra, dec, size, cutout, fpMfn=None, psFieldfn=None, invvarfn=None, band=None): wcs = Tan(fpC, 0) x,y = wcs.radec2pixelxy(ra, dec) x,y = int(x),int(y) print 'x,y', x,y dl = size / 2 dh = size - dl # ?? xlo,xhi = max(0, x-dl), min(2048-1, x+dh-1) ylo,yhi = max(0, y-dl), min(1489-1, y+dh-1) os.system('imcopy %s"[%i:%i,%i:%i]" !%s' % (fpC, xlo, xhi, ylo, yhi, cutout)) if invvarfn is None: return bandnum = 'ugriz'.index(band) fpc = pyfits.open(fpC)[0].data.astype(float) fpM = pyfits.open(fpMfn) (gain, darkvar, sky, skyerr) = sdss_psfield_noise(psFieldfn, band=bandnum) invvar = sdss_noise_invvar(fpc, fpM, xlo, xhi, ylo, yhi, gain, darkvar, sky, skyerr) print invvar.shape #print 'x', xlo, xhi #print 'y', ylo, yhi #invvar = invvar[ylo:yhi, xlo:xhi] #print invvar.shape pyfits.writeto(invvarfn, invvar, clobber=True)
def modify_binning(field): if field in ["fieldA", "fieldB"]: return seg = pf.getdata("sources.fits") white = [x for x in os.listdir(".") if "(white)" in x][0] ra = wavelength_array(white, axis=1) dec = wavelength_array(white, axis=2) # Ofset to the center of NGC 3311 ra -= ra0 dec -= dec0 # Convert to radians X = D * 1000 * np.deg2rad(ra) Y = D * 1000 * np.deg2rad(dec) xx, yy = np.meshgrid(X,Y) R = np.sqrt(xx**2 + yy**2) base = 10 Rbins = 10 + 35 * np.logspace(0.3,1,4, base=base) / base Rbins = np.hstack((10, Rbins)) for i,rbin in enumerate(Rbins[:-1]): deltar = Rbins[i+1] - Rbins[i] newbin = seg.max() + 1 idxbin = np.where((R > rbin) & (R <= rbin + deltar) & (seg==0)) if i == 3: newbin = 0 seg[idxbin] = newbin pf.writeto("sources.fits", seg, clobber=True)
def make_dead(fact=5.): """ To do in the sorted_by_pos directory in the fringe directory Make a boolean mask for data. To work on single_masterflat_image.fits. """ frames = gl.glob("*/*/") frames.sort() length_f = len(frames) i_f = 1 for f in frames: print "Computing frame : " + str(i_f) + "/" + str(length_f) image = gl.glob(f + "single_masterflat_image.fits") if len(image) == 1: temp_flat = pf.open(image[0]) d = temp_flat[0].data x_step = 143 y_step = 128 x_div = np.linspace(0,4004,29).astype(int) y_div = np.linspace(0,4096,33).astype(int) mask = np.zeros((4004,4096)) for x in x_div[:-1]: for y in y_div[:-1]: median = np.median(d[x:x+x_step,y:y+y_step]) std = np.std(d[x:x+x_step,y:y+y_step]) mask[x:x+x_step,y:y+y_step][np.fabs(d[x:x+x_step,y:y+y_step] - median) > fact*std] = 1 pf.writeto("mask.fits", mask, clobber = True) os.system("mv mask.fits " + f) temp_flat.close() i_f +=1
def main(): parser = argparse.ArgumentParser(description='Normalize image by exptime. If image already normalized, it is skipped.') parser.add_argument('filelist', nargs='+', help='Files to normalize') parser.add_argument('-o',metavar='outfile', type=str, help='Specify optional output file, otherwise rewrite file') parser.add_argument('-key', type=str, default='EXPTIME', help='Specify exposure time keyword (default=EXPTIME)') parser.add_argument('-normkey',type=str, default='NORM', help='Specify normalized keyword (default=NORM)') parser.add_argument('--c',action='store_true',help='If specified, force clobber on write') args = parser.parse_args() for filename in args.filelist: data,header = pyfits.getdata(filename, header=True) # If already normalized, skip this file if is_normalized(header, args.normkey): print 'Skipping %s. Already normalized.' % filename continue # Else, normalize data, header = normalize(data, header, args.key, args.normkey) if args.o: outname = args.o else: outname = filename print 'Writing to %s' % outname pyfits.writeto(outname, data, header=header, clobber=args.c)
def extract_spec2(): infile = workdir + '/maps/standards/HD221246_K3III.fits' specInfo, hdr = pyfits.getdata(infile, header=True) wave = specInfo[0,:] * 1e3 # in nm spec = specInfo[1,:] print wave[0:10] print wave[-10:] print spec crpix1 = 1 crval1 = wave[0] cdelt1 = wave[1] - wave[0] cunit1 = 'nm' tmp = np.arange(len(spec), dtype=float) tmp = tmp*cdelt1 + crval1 print tmp[0:10] print tmp[-10:] hdr.update('CRPIX1', crpix1) hdr.update('CRVAL1', crval1) hdr.update('CDELT1', cdelt1) hdr.update('CUNIT1', cunit1) fitsFile = workdir + 'maps/test_spec_standard.fits' ir.imdelete(fitsFile) pyfits.writeto(fitsFile, spec, header=hdr)
def makeband(band='V'): files = glob.glob('Mantis*[0-9]'+band+'_cal.fit*') zsz = len(files) reffile = files[zsz/2] image0,header0 = readimage(reffile) ysz,xsz = np.shape(image0) refim = h.pyfits.open(reffile) refh = h.pyfits.getheader(reffile) stack = np.zeros((xsz,ysz,zsz)) for i in range(zsz): im = h.pyfits.open(files[i]) newim = h.hcongrid(im[0].data,im[0].header,refh) stack[:,:,i] = newim final = np.median(stack,axis=2) if band == 'V': tag = 'Blue' if band == 'R': tag = 'Green' if band == 'ip': tag = 'Red' test = glob.glob(tag+'.fit') if test: os.remove(tag+'.fit') pf.writeto(tag+'.fit',final,header0)
def main(): parser = argparse.ArgumentParser( description= 'Aligns all spectra, combines sky spectra, and writes a fits file for the new sky spectrum and each object spectrum.' ) parser.add_argument( 'specimgs', type=str, help='File location and handle for the spectra (e.g. NGC253_spec).') parser.add_argument('name', type=str, help='Name of data set (e.g. NGC253).') parser.add_argument('specmin', type=int, help='Number of first aperture (usually 0).') parser.add_argument('specmax', type=int, help='Number of last aperture.') parser.add_argument('-obj', nargs='+', type=int, help='Object aperture numbers.') args = parser.parse_args() ydata = [] for num in range(args.specmin, args.specmax + 1): ydata.append(pyfits.getdata(args.specimgs + '%i.fits' % num)) ydata = np.array(ydata) heads = [] for num in range(args.specmin, args.specmax + 1): heads.append(pyfits.getheader(args.specimgs + '%i.fits' % num)) xdata = [] for i, num in enumerate(range(args.specmin, args.specmax + 1)): xdata.append( np.arange(0, len(ydata[i])) * heads[i]['CDELT1'] + heads[i]['CRVAL1']) xdata = np.array(xdata) min_wave = np.min([np.min(x) for x in xdata]) max_wave = np.max([np.max(x) for x in xdata]) newspecs = [ rescale(xdata[i], ydata[i], min_wave, max_wave) for i, spec in enumerate(xdata) ] xnew = [] ynew = [] for spec in newspecs: xnew.append(spec[0]) ynew.append(spec[1]) xnew = np.array(xnew) ynew = np.array(ynew) #Following steps are updated to accommodate for cases in which specmin is non-zero. for obj in args.obj: pyfits.writeto('%s%i_r.fits' % (args.specimgs, obj), ynew[obj - args.specmin], Header(heads[obj - args.specmin], False, xnew[obj - args.specmin], args.name), clobber=True) yskyspecs = np.array( [y for i, y in enumerate(ynew) if i + args.specmin not in args.obj]) ysky = np.nanmean(yskyspecs, axis=0) pyfits.writeto('%s%s' % (args.specimgs, 'sky.fits'), ysky, Header(heads[0], False, xnew[0], args.name), clobber=True) for obj in args.obj: skysub = ynew[obj - args.specmin] - ysky pyfits.writeto('%s%i_skysub.fits' % (args.specimgs, obj), skysub, Header(heads[obj - args.specmin], True, xnew[obj - args.specmin], args.name), clobber=True)
def _setup_img(image, name): if not type(image) == type(''): import pyfits pyfits.writeto(name, image)
curr_dir = fnmatch.filter( listdir(path), '*.fits' ) # read fits files (list) for the first folder in the list of images (one radiograph) # In[ ]: for j in range(0, len(curr_dir)): img = np.zeros([512, 512], dtype='>f4') avg_img = np.zeros([512, 512], dtype='>f4') for i in range(0, (len(coll_dir) - 1)): path = rootpath + coll_dir[i] + '/' # print(path) curr_dir = fnmatch.filter(listdir(path), '*.fits') name = path + curr_dir[j] # print(name) name_dir = name.split(".") name_s = name_dir[0].split("/") name_sa = name_s[-1].split("_") name_save = name_sa[0] + '_' + name_sa[1] # print(name_save) with fits.open(name) as im: data = im[0].data img = img + data avg_img = np.true_divide(img, (i + 1)) pyfits.writeto( savepath + 'S3_avg_img_' + name_save + '_' + str(f"{j:03}") + '.fits', avg_img) # pyfits.writeto(savedir+'S2_avg_img_'+ name_save+'_'+str(f"{j:03}")+'.fits', img) print(savepath + 'S3_avg_img_' + name_save + '_' + str(f"{j:03}") + '.fits')
def parsetext(self,text): args = None try: # catch -h, or error exit args = self.subparser.parse_args(text.split()) except SystemExit: return if not args: return if args.c: self.clobber = True print 'Force clobber on write' if args.s: self.step = args.s print 'Step size changed to %.2f' % self.step if args.stretch: print 'Changed stretch to %s' % args.stretch self.norm.stretch = args.stretch self.display(self.active_data) if args.clip_lo: print 'Clip lo changed to %.2f' % args.clip_lo self.norm.clip_lo = args.clip_lo self.display(self.active_data) if args.clip_hi: print 'Clip hi changed to %.2f' % args.clip_hi self.norm.clip_hi = args.clip_hi self.display(self.active_data) if args.r: self.active_data = self.orig_data.copy() self.offsets[self.current][0] = 0.0 self.offsets[self.current][1] = 0.0 self.norm = DS9Normalize(stretch='linear') self.display(self.active_data) print 'Restored image from %s' % self.filelist[self.current] if args.w: h = pyfits.getheader(self.filelist[self.current]) h['N_ORIG_F'] = (self.filelist[self.current],'Original file before nudge') h['N_XS'] = (self.offsets[self.current][0],'Xshift of nudge') h['N_YS'] = (self.offsets[self.current][1],'Yshift of nudge') outfile = os.path.basename(self.filelist[self.current]) outfile = os.path.splitext(outfile) outfile = ''.join([outfile[0],self.ext,outfile[1]]) outfile = os.path.join(self.outdir,outfile) try: pyfits.writeto(outfile,data=self.active_data,header=h,clobber=self.clobber) except IOError as e: print e, "'--c' to force overwrite" else: print '%s: written to disk, s = [%.2f, %.2f]' % (outfile,self.offsets[self.current][0],self.offsets[self.current][1]) if args.wq: args.wa = True args.q = True if args.wa: for idx in range(0,len(self.filelist)): h = pyfits.getheader(self.filelist[idx]) h['N_ORIG_F'] = (self.filelist[idx],'Original file before nudge') h['N_XS'] = (self.offsets[idx][0],'Xshift of nudge') h['N_YS'] = (self.offsets[idx][1],'Yshift of nudge') outfile = os.path.basename(self.filelist[idx]) outfile = os.path.splitext(outfile) outfile = ''.join([outfile[0],self.ext,outfile[1]]) outfile = os.path.join(self.outdir,outfile) try: if idx == self.current: pyfits.writeto(outfile,data=self.active_data,header=h,clobber=self.clobber) else: pyfits.writeto(outfile,data=self.datalist[idx],header=h,clobber=self.clobber) except IOError as e: print e, "'--c' to force overwrite" args.q = False #don't quit if file fails to write else: print '%s: written to disk, s = [%.2f, %.2f]' % (outfile,self.offsets[idx][0],self.offsets[idx][1]) if args.q: plt.close() exit()
def calculate_nhi(cube = None, velocity_axis = None, velocity_range = [], return_nhi_error = True, noise_cube = None, velocity_noise_range=[90,100], Tsys = 30., header = None, fits_filename = None, fits_error_filename = None, verbose = True): ''' Calculates an N(HI) image given a velocity range within which to include a SpectralGrid's components. Parameters ---------- cube : array-like, optional Three dimensional array with velocity axis as 0th axis. Must specify a velocity_axis if cube is used. velocity_axis : array-like, optional One dimensional array containing velocities corresponding to fits_filename : str If specified, and a header is provided, the nhi image will be written. header : pyfits.Header Header from cube. ''' import numpy as np # Calculate NHI from cube if set if cube is not None and velocity_axis is not None: image = np.empty((cube.shape[1], cube.shape[2])) image[:,:] = np.NaN indices = np.where((velocity_axis > velocity_range[0]) & \ (velocity_axis < velocity_range[1]))[0] image[:,:] = cube[indices,:,:].sum(axis=0) # Calculate image error if return_nhi_error: image_error = np.empty((cube.shape[1], cube.shape[2])) image_error[:,:] = np.NaN image_error[:,:] = (noise_cube[indices,:,:]**2).sum(axis=0)**0.5 # NHI in units of 1e20 cm^-2 nhi_image = np.ma.array(image,mask=np.isnan(image)) * 1.823e-2 if fits_filename is not None and header is not None: if verbose: print('Writing N(HI) image to FITS file %s' % fits_filename) header['BUNIT'] = '1e20 cm^-2' header.remove('CDELT3') header.remove('CRVAL3') header.remove('CRPIX3') header.remove('CTYPE3') header.remove('NAXIS3') header['NAXIS'] = 2 pf.writeto(fits_filename, image*1.823e-2, header = header, clobber = True, output_verify = 'fix') if fits_error_filename is not None and header is not None: if verbose: print('Writing N(HI) error image to FITS file %s' % fits_filename) pf.writeto(fits_error_filename, image_error * 1.823e-2, header = header, clobber = True, output_verify = 'fix') if return_nhi_error: nhi_image_error = np.ma.array(image_error, mask=np.isnan(image_error)) * 1.823e-2 return nhi_image, nhi_image_error else: return nhi_image
Cube_freq = pf.open(dirwr + folder_CDF + cube_name)[0].data i = 0 while i < len(Cube_freq): # we register each "FDM_val" images an image with the DM flat if (i % FDM_val) == 0: # We apply to the DM the initial flatmap status = p3k.load_new_flatmap( fmap.convert_hodm_telem(initial_flatmap)) # We read an image header, im_flat = pharo.take_src_return_imagedata_header(exptime) # We save this image pf.writeto(dirwr + folder_FDM_Im + "FDM_Im_Com_" + str(int(i)) + ".fits", im_flat, header=header, clobber=True, output_verify='ignore') # Construction de la commmande pour le DM DM_commande = Cube_freq[i, :, :] + initial_flatmap # On applique la commande au DM status = p3k.load_new_flatmap(fmap.convert_hodm_telem(DM_commande)) # On enregistre la commande envoyée au DM pf.writeto(dirwr + folder_DM_Com + "DM_Com_" + str(int(i)) + ".fits", DM_commande, clobber=True) # We are taking one image with pharo. The exposture time is exptime in UT (unit of time)
def create_matched_fits(name, data, mean_delta_x, mean_delta_y): imAh=pyfits.getheader(name) rotation_matrix_inverse = np.array([[1,0,mean_delta_x],[0,1,mean_delta_y],[0,0,1]], dtype = float) regrided_data = regrid_data(data, rotation_matrix_inverse) pyfits.writeto(name[0:-5]+'_m.fits',regrided_data,imAh, clobber = True) if VERBOSE>0 : print name[0:-5]+"_m.fits OK"
def align(file): #dir = '/media/data/20170313/saphira/processed/' #filename = 'pbimage_12:10:16.115625254_p.fits' img = pyfits.getdata(file)[:-1] n_brightest = 17 #number of pixels to include in centroid calculation brightness_threshold = 1000 #when do you stop looking for an astrometric speckle? #1600 for 12:16:42 x_shift_arr = np.zeros(len(img)) y_shift_arr = np.zeros(len(img)) #### FIND AVERAGE LOCATION OF EACH ASTROMETRIC SPECKLE #Calculate approximate center of PSF im_coadd = (np.sum(img, 0) / len(img)).astype('float') x0 = np.sum(np.sum(im_coadd, 0)*range(np.shape(im_coadd)[1])) / \ np.sum(im_coadd) y0 = np.sum(np.sum(im_coadd, 1)*range(np.shape(im_coadd)[0])) / \ np.sum(im_coadd) #Get ALL PSFs sorta close to this if 0: print "coarse aligning" x_mask_shift = np.zeros(len(img)) y_mask_shift = np.zeros(len(img)) for z in range(len(img)): x_mask_shift[z] = np.sum(np.sum(img[z], 0)*range(np.shape(img[z])[1])) / \ np.sum(img[z]) - x0 y_mask_shift[z] = np.sum(np.sum(img[z], 1)*range(np.shape(img[z])[0])) / \ np.sum(img[z]) - y0 #img[z] = np.roll(img[z], int(round(x0-x1)), axis=1) #img[z] = np.roll(img[z], int(round(y0-y1)), axis=0) if 0: plt.figure(1, figsize=(10, 5), dpi=100) plt.subplot(121) plt.imshow(im_coadd, interpolation='none', vmin=1, vmax=np.max(im_coadd) * 1.1) plt.title('before') plt.subplot(122) plt.imshow((np.sum(img, 0) / len(img)).astype('float'), interpolation='none', vmin=1, vmax=np.max(im_coadd) * 1.1) plt.title('after') plt.show() #Q2 Q1 #Q3 Q4 #Q1 im = np.copy(im_coadd) im[y0 - 25:, :] = 0 im[:, :x0 + 25] = 0 q1_x0, q1_y0 = centroid(im, n_brightest + 10) #Q2 im = np.copy(im_coadd) im[y0 - 25:, :] = 0 im[:, x0 - 25:] = 0 q2_x0, q2_y0 = centroid(im, n_brightest + 10) #Q3 im = np.copy(im_coadd) im[:y0 + 25, :] = 0 im[:, x0 - 25:] = 0 q3_x0, q3_y0 = centroid(im, n_brightest + 10) #Q4 im = np.copy(im_coadd) im[:y0 + 25, :] = 0 im[:, :x0 + 25] = 0 q4_x0, q4_y0 = centroid(im, n_brightest + 10) print "x avg", np.mean((q1_x0, q2_x0, q3_x0, q4_x0)) print "y avg", np.mean((q1_y0, q2_y0, q3_y0, q4_y0)) if 1: plt.imshow(im_coadd, interpolation='none', vmin=0, vmax=5000) plt.plot([x0], [y0], 'wx') plt.plot([q1_x0], [q1_y0], 'kx') plt.plot([q2_x0], [q2_y0], 'kx') plt.plot([q3_x0], [q3_y0], 'kx') plt.plot([q4_x0], [q4_y0], 'kx') plt.show() ### ALIGN IMAGES #make masks around each astrometric speckle mask_q1_avg = np.zeros(np.shape(im)) mask_q2_avg = np.zeros(np.shape(im)) mask_q3_avg = np.zeros(np.shape(im)) mask_q4_avg = np.zeros(np.shape(im)) for x in range(np.shape(im)[1]): for y in range(np.shape(im)[0]): if (x - q1_x0)**2 + (y - q1_y0)**2 < 11**2: mask_q1_avg[y, x] = 1 if (x - q2_x0)**2 + (y - q2_y0)**2 < 11**2: mask_q2_avg[y, x] = 1 if (x - q3_x0)**2 + (y - q3_y0)**2 < 11**2: mask_q3_avg[y, x] = 1 if (x - q4_x0)**2 + (y - q4_y0)**2 < 11**2: mask_q4_avg[y, x] = 1 #Recenter PSFs to reduce tip/tilt for i in range(0, np.shape(img)[0]): if i % 100 == 0: print int(np.round(np.float(i) / np.shape(img)[0] * 100)), "% done." if 0: plt.imshow(img[i] * (mask_q1 + mask_q2 + mask_q3 + mask_q4), interpolation='none') plt.show() if i == 0: diff = img[0] - img[1] elif (i > 0) & (i < len(img) - 1): diff = img[i + 1] - img[i - 1] else: #if i==len(img)-1 diff = img[i] - img[i - 1] #n_good_quads = 0 #shift masks to compensate for crazy tip/tilt, but only if they exist if 'x_mask_shift' in vars(): mask_q1 = np.roll(mask_q1_avg, int(round(x_mask_shift[i])), axis=1) mask_q1 = np.roll(mask_q1, int(round(y_mask_shift[i])), axis=0) mask_q2 = np.roll(mask_q2_avg, int(round(x_mask_shift[i])), axis=1) mask_q2 = np.roll(mask_q2, int(round(y_mask_shift[i])), axis=0) mask_q3 = np.roll(mask_q3_avg, int(round(x_mask_shift[i])), axis=1) mask_q3 = np.roll(mask_q3, int(round(y_mask_shift[i])), axis=0) mask_q4 = np.roll(mask_q4_avg, int(round(x_mask_shift[i])), axis=1) mask_q4 = np.roll(mask_q4, int(round(y_mask_shift[i])), axis=0) else: mask_q1 = mask_q1_avg mask_q2 = mask_q2_avg mask_q3 = mask_q3_avg mask_q4 = mask_q4_avg if 0: #i%100==0: plt.imshow(img[i] + 1000 * (mask_q1 + mask_q2 + mask_q3 + mask_q4), interpolation='none', vmin=0, vmax=1e4) plt.plot([y0], [x0], 'wx') plt.plot([y0 + y_mask_shift[i]], [x0 + x_mask_shift[i]], 'kx') plt.plot([q1_x0], [q1_y0], 'wx') plt.plot([q2_x0], [q2_y0], 'wx') plt.plot([q3_x0], [q3_y0], 'wx') plt.plot([q4_x0], [q4_y0], 'wx') plt.colorbar() plt.show() x_arr, y_arr, weight = get_centroids(diff, mask_q1, mask_q2, mask_q3, mask_q4, \ brightness_threshold, n_brightest, q1_x0, \ q1_y0, q2_x0, q2_y0, q3_x0, q3_y0, q4_x0, q4_y0) #print "1. len x_arr, y_arr", len(x_arr), len(y_arr) #IF NOT ENOUGH POINTS FOUND #if 0: #(len(x_arr) < 3) & (i>0) & (i<len(img)-1): if (i > 0) & (i < len(img) - 1): diff = img[i] - img[i - 1] x_arr2, y_arr2, weight2 = get_centroids(diff, mask_q1, mask_q2, mask_q3, mask_q4, \ brightness_threshold, n_brightest, q1_x0, \ q1_y0, q2_x0, q2_y0, q3_x0, q3_y0, q4_x0, q4_y0) x_arr = np.append(x_arr, x_arr2) y_arr = np.append(y_arr, y_arr2) weight = np.append(weight, weight2) #print "2. len x_arr, y_arr", len(x_arr), len(y_arr) diff = img[i] - img[i + 1] x_arr2, y_arr2, weight2 = get_centroids(diff, mask_q1, mask_q2, mask_q3, mask_q4, \ brightness_threshold, n_brightest, q1_x0, \ q1_y0, q2_x0, q2_y0, q3_x0, q3_y0, q4_x0, q4_y0) x_arr = np.append(x_arr, x_arr2) y_arr = np.append(y_arr, y_arr2) weight = np.append(weight, weight2) #print "3. len x_arr, y_arr", len(x_arr), len(y_arr) #if i==110: pdb.set_trace() #compute weighted average if len(x_arr) == 0: print np.max(diff * (mask_q1 + mask_q2 + mask_q3 + mask_q4)) print np.min(diff * (mask_q1 + mask_q2 + mask_q3 + mask_q4)) diff = img[i + 1] - img[i - 1] print np.max(diff * (mask_q1 + mask_q2 + mask_q3 + mask_q4)) print np.min(diff * (mask_q1 + mask_q2 + mask_q3 + mask_q4)) pdb.set_trace() x_shift = np.sum(x_arr * weight) / np.sum(weight) y_shift = np.sum(y_arr * weight) / np.sum(weight) x_shift_arr[i] = x_shift y_shift_arr[i] = y_shift #check shifting if 0: plt.figure(1, figsize=(15, 5), dpi=100) plt.subplot(131) plt.imshow(img[i] * (mask_q1 + mask_q2 + mask_q3 + mask_q4), interpolation='none') plt.plot([q1_x0, q2_x0, q3_x0, q4_x0], [q1_y0, q2_y0, q3_y0, q4_y0], 'wx') plt.title('Reference Positions') plt.subplot(132) plt.imshow(img[i] * (mask_q1 + mask_q2 + mask_q3 + mask_q4), interpolation='none') plt.plot(np.median(x_arr) + np.array([q1_x0, q2_x0, q3_x0, q4_x0]), \ np.median(y_arr) + np.array([q1_y0, q2_y0, q3_y0, q4_y0]), 'kx') plt.title('Detected Positions') crop = scipy.ndimage.interpolation.shift( im, [-1 * x_shift, -1 * y_shift], mode='wrap') plt.subplot(133) plt.imshow(crop, interpolation='none', vmin=0, vmax=4000) plt.plot([q1_x0, q2_x0, q3_x0, q4_x0], [q1_y0, q2_y0, q3_y0, q4_y0], 'wx') plt.title('Shifted to Reference Positions') plt.show() #print "xarr", x_arr #print "yarr", y_arr #print "weight", weight #print #im = np.copy(img[i]) #img[i,:,:] = scipy.ndimage.interpolation.shift(im, # [-1*y_shift, -1*x_shift], mode='wrap') x_shift_arr_filtered = gaussian_filter(x_shift_arr, 1) y_shift_arr_filtered = gaussian_filter(y_shift_arr, 1) for i in range(len(img)): img[i] = scipy.ndimage.interpolation.shift(img[i], [-1*y_shift_arr_filtered[i],\ -1*x_shift_arr_filtered[i]],\ mode='wrap') plt.figure(1, figsize=(10, 10), dpi=100) plt.subplot(211) plt.plot(x_shift_arr) plt.plot(x_shift_arr_filtered) plt.title('X displacement') plt.subplot(212) plt.plot(y_shift_arr) plt.plot(y_shift_arr_filtered) plt.title('Y displacement') plt.show() #pdb.set_trace() plt.figure(1, figsize=(10, 5), dpi=100) plt.subplot(121) plt.imshow(im_coadd, interpolation='none', vmin=1) #, vmax=np.max(im_coadd)*1.1) plt.colorbar() plt.title('before') plt.subplot(122) plt.imshow((np.sum(img, 0) / len(img)).astype('float'), interpolation='none', vmin=1) #, #vmax=np.max(im_coadd)*1.1) plt.colorbar() plt.title('after') plt.show() if 1: #Save image? newfilename = file[:file.find('.fits')] + '_aligned_tt3.fits' print "Saving image as " + newfilename pyfits.writeto(newfilename, img, clobber='true') #save file print "Saved."
#import median as mdn import numpy as np import statistics as st print "Collecting all the bias files..." with open("bias.in") as bias_list: BL = [line for line in bias_list] n = len(BL) BL = [BL[i].split("\n")[0] for i in range(n)] DL = [pf.getdata(s) for s in BL] print "The bias files collected are:" for i in BL: print i print "Combining all bias files as median..." mbias = st.median_arrays(DL) print "Saving median bias..." pf.writeto("mbias.fits", mbias, clobber=True) print "Median bias is saved as mbias.fits." t2 = time.clock() T = abs(t2 - t1) print "The taken for execution of the program is", T, "seconds."
mpl.rcParams['legend.numpoints'] = 1 mpl.rcParams['axes.labelweight']='semibold' mpl.rcParams['axes.titlesize']=9 mpl.rcParams['axes.titleweight']='semibold' mpl.rcParams['font.weight'] = 'semibold' list = (glob.glob("../Post_ML/*.npy")) #list = (glob.glob("../Post_ML/*feh2*.npy")) for i in list: file = i.replace('.npy','.fits') if not os.path.isfile(file): print file tmp = np.load(i) pyfits.writeto(file, tmp, clobber=True) ## this part makes plots #list = (glob.glob("../Post_ML/Mk-M_10_feh2_er2*flat.npy")) list = (glob.glob("../Post_ML/*8*feh*flat.npy")) #list = (glob.glob("../Post_ML/*_7_er1_flat.npy")) #list = [list1,list2] print list ## how do we burn-in this shit? for i in list: flat = np.load(i) print i pdfname = 'fail' adder = '?'
import pyfits templatehdulist = pyfits.open( 'mom0_regrid_Stutz_convol18_mask_imfit_12co_pix_2_Tmb.fits') templatedata = templatehdulist[0].data[0, :, :] templatehdulist[0].header['NAXIS'] = 2 del templatehdulist[0].header['CRPIX3'] del templatehdulist[0].header['CDELT3'] del templatehdulist[0].header['CRVAL3'] del templatehdulist[0].header['CTYPE3'] del templatehdulist[0].header['NAXIS3'] pyfits.writeto( 'novelocity_mom0_regrid_Stutz_convol18_mask_imfit_12co_pix_2_Tmb.fits', templatedata, templatehdulist[0].header, output_verify='exception', clobber=True, checksum=False)
#Author(s): Bikram Keshari Sahu (15051) and Parth Nayak (15118) #Code under the project of the course PHY312 Numerical Methods and Programming import numpy as np import pyfits as pf def crop(A, xl, xr, yu, yd): B = [] n = len(A) m = len(A[0]) for i in range(yu, n - yd): t = [] for j in range(xl, m - xr): t.append(A[i][j]) B.append(np.array(t)) B = np.array(B) return B C = pf.getdata("J0901p3846_R.4577.0_clean.fits") G = crop(C, 100, 100, 0, 0) pf.writeto("J0901p3846_R.4577.0_clean_crop.fits", G, clobber=True)
import pyfits templatehdulist = pyfits.open( 'pixel6_convol18_han1_mask_imfit_13co_pix_2_Tmb.fits') print templatehdulist[0].data.shape templatedata = templatehdulist[0].data[0, :, :] templatehdulist[0].header['NAXIS'] = 2 del templatehdulist[0].header['CRPIX3'] del templatehdulist[0].header['CUNIT3'] del templatehdulist[0].header['CDELT3'] del templatehdulist[0].header['CRVAL3'] del templatehdulist[0].header['CTYPE3'] del templatehdulist[0].header['NAXIS3'] del templatehdulist[0].header['PC1_1'] del templatehdulist[0].header['PC2_1'] del templatehdulist[0].header['PC3_1'] del templatehdulist[0].header['PC1_2'] del templatehdulist[0].header['PC2_2'] del templatehdulist[0].header['PC3_2'] del templatehdulist[0].header['PC1_3'] del templatehdulist[0].header['PC2_3'] del templatehdulist[0].header['PC3_3'] pyfits.writeto('chan1_pixel6_convol18_han1_mask_imfit_13co_pix_2_Tmb.fits', templatedata, templatehdulist[0].header, output_verify='exception', clobber=True, checksum=False)
def stack_fits(fitslist, outname, axis=0, ctype=None, keep_old=False, fits=False): """ Stack a list of fits files along a given axiis. fitslist: list of fits file to combine outname: output file name axis: axis along which to combine the files fits: If True will axis FITS ordering axes ctype: Axis label in the fits header (if given, axis will be ignored) keep_old: Keep component files after combining? """ import numpy try: import pyfits except ImportError: warnings.warn( "Could not find pyfits on this system. FITS files will not be stacked" ) sys.exit(0) hdu = pyfits.open(fitslist[0])[0] hdr = hdu.header naxis = hdr['NAXIS'] # find axis via CTYPE key if ctype is not None: for i in range(1, naxis + 1): if hdr['CTYPE%d' % i].lower().startswith(ctype.lower()): axis = naxis - i # fits to numpy convention elif fits: axis = naxis - axis fits_ind = abs(axis - naxis) crval = hdr['CRVAL%d' % fits_ind] imslice = [slice(None)] * naxis _sorted = sorted([pyfits.open(fits) for fits in fitslist], key=lambda a: a[0].header['CRVAL%d' % (naxis - axis)]) # define structure of new FITS file nn = [hd[0].header['NAXIS%d' % (naxis - axis)] for hd in _sorted] shape = list(hdu.data.shape) shape[axis] = sum(nn) data = numpy.zeros(shape, dtype=float) for i, hdu0 in enumerate(_sorted): h = hdu0[0].header d = hdu0[0].data imslice[axis] = range(sum(nn[:i]), sum(nn[:i + 1])) data[imslice] = d if crval > h['CRVAL%d' % fits_ind]: crval = h['CRVAL%d' % fits_ind] # update header hdr['CRVAL%d' % fits_ind] = crval hdr['CRPIX%d' % fits_ind] = 1 pyfits.writeto(outname, data, hdr, clobber=True) print("Successfully stacked images. Output image is %s" % outname) # remove old files if not keep_old: for fits in fitslist: os.system('rm -f %s' % fits)
def choose_science(instrument, workdir='.', targetdir='.', cams=[0,1,2,3], auto=False, save_select=True, figsize=(10,10), window_zoom=4, calibrate=False, noplot=False): """ PURPOSE: Display science images for verification by user INPUT: instrument - instrument name defined in instrument_dict (ex. 'ratir') workdir - directory where function is to be executed targetdir - directory where selected frames and lists are output cams - camera numbers to process data, all by default auto - select all science frames save_select - save dictionary of selected frames to python pickle file figsize - dimensions of figure used to display frames for selection window_zoom - zoom level for closer look EXAMPLE: file_dict = choose_science('ratir', workdir = 'path/to/data/', targetdir = 'path/to/processeddata/',cams = [#,#,...], calibrate=True) """ instrum = instrument_dict[instrument] # check for non-list camera argument if type(cams) is not list: cams = [cams] # convert to list # check for non-integer camera designators if type(cams[0]) is not int: af.print_err("Error: cameras must be specified by an integer. Exiting...") return pl.ion() # pylab in interactive mode # move to working directory start_dir = os.getcwd() os.chdir(workdir) d = os.getcwd().split('/')[-1] # name of current directory if not auto: af.print_head("\nDisplaying science frames in {} for selection:".format(d)) else: af.print_head("\nSelecting all science frames in {}:".format(d)) # dictionary to store selected fits files by camera or filter fits_list_dict = {} # remove tailing / from target directory name if present if targetdir[-1] == '/': targetdir = targetdir[:-1] # make target directory if it does not exist if not os.path.exists(targetdir): af.print_blue("Creating target directory: {}".format(targetdir)) os.makedirs(targetdir) # warn user if previous files may be overwritten else: af.print_warn("Warning: Target directory exists. Existing files will be overwritten.") resp = raw_input("Proceed? (y/n): ") if resp.lower() != 'y': af.print_bold("Exiting...") os.chdir(start_dir) # move back to starting directory return else: shutil.rmtree(targetdir) os.makedirs(targetdir) fits_check = glob('????????T??????C??.fits') if len(fits_check) == 0: files = instrum.change_file_names(glob(instrum.original_file_format())) fits_check_2 = glob('????????T??????C??.fits') if len(fits_check_2) == 0: af.print_err("Error: no files with correct format after file name changes") return # open figure for images if not auto if not auto and not noplot: fig = pl.figure(figsize=figsize) # work on FITs files for specified cameras for cam_i in cams: # print current camera number af.print_under("\n{:^50}".format('CAMERA {}'.format(cam_i))) if calibrate: # get master dark and bias frames for current camera if required if instrum.has_cam_bias(cam_i) == True: # if instrum.has_cam_bias(cam_i) == True: mbias_fn = '{}_C{}.fits'.format(instrum.biasname, cam_i) if not os.path.exists(mbias_fn): af.print_err('Error: {} not found. Move master bias file to working directory to proceed.'.format(mbias_fn)) continue else: mbias_data = pf.getdata(mbias_fn) if instrum.has_cam_dark(cam_i) == True: mdark_fn = '{}_C{}.fits'.format(instrum.darkname, cam_i) if not os.path.exists(mdark_fn): af.print_err('Error: {} not found. Move master dark file to working directory to proceed.'.format(mdark_fn)) continue else: mdark_data = pf.getdata(mdark_fn) # find raw files of selected type for this camera fits_list = glob('????????T??????C{}{}.fits'.format(cam_i, instrum.ftype_post[instrum.objname])) if len(fits_list) == 0: af.print_warn("Warning: no fits files found. Skipping camera {}.".format(cam_i)) continue # look at FITs data sequentially for fits_fn in fits_list: fits_id = fits_fn.split('.')[0] # fits file name with extension removed print '{}'.format(fits_fn) # open data hdulist = pf.open(fits_fn) im = hdulist[0].data h = hdulist[0].header if calibrate: # get master flat frame for current filter if instrum.is_cam_split(cam_i) == True: mflat_fn1 = '{}_{}.fits'.format(instrum.flatname, instrum.get_filter(h,'C{}a'.format(cam_i))) if not os.path.exists(mflat_fn1): af.print_err('Error: {} not found. Move master flat file to working directory to proceed.'.format(mflat_fn1)) continue else: mflat_data1 = pf.getdata(mflat_fn1) mflat_fn2 = '{}_{}.fits'.format(instrum.flatname, instrum.get_filter(h,'C{}b'.format(cam_i))) if not os.path.exists(mflat_fn2): af.print_err('Error: {} not found. Move master flat file to working directory to proceed.'.format(mflat_fn2)) continue else: mflat_data2 = pf.getdata(mflat_fn2) else: mflat_fn = '{}_{}.fits'.format(instrum.flatname, instrum.get_filter(h,'C{}'.format(cam_i))) if not os.path.exists(mflat_fn): af.print_err('Error: {} not found. Move master flat file to working directory to proceed.'.format(mflat_fn)) continue else: mflat_data = pf.getdata(mflat_fn) # get image statistics if instrum.is_cam_split(cam_i) == True: im1 = im[instrum.slice('C{}a'.format(cam_i))] im2 = im[instrum.slice('C{}b'.format(cam_i))] else: im1 = im[instrum.slice('C{}'.format(cam_i))] # display image and prompt user if not auto: if instrum.is_cam_split(cam_i) == True: disp_im1 = np.copy(im1) disp_im2 = np.copy(im2) if calibrate: if instrum.has_cam_bias(cam_i): disp_im1 -= mbias_data disp_im2 -= mbias_data if instrum.has_cam_dark(cam_i): disp_im1 -= mbias_data*instrum.get_exptime(h) disp_im2 -= mbias_data*instrum.get_exptime(h) disp_im1 = np.divide(disp_im1, mflat_data1) disp_im2 = np.divide(disp_im2, mflat_data2) if not noplot: # display top ax1 = fig.add_subplot(221) plot_params_science(ax1, disp_im1, instrum.get_filter(h,'C{}a'.format(cam_i)), h, central=False) # and central subregion ax1s = fig.add_subplot(222) plot_params_science(ax1s, disp_im1, instrum.get_filter(h,'C{}a'.format(cam_i)), h, central=True) # display bottom ax2 = fig.add_subplot(223) plot_params_science(ax2, disp_im2, instrum.get_filter(h,'C{}b'.format(cam_i)), h, central=False) # and central subregion ax2s = fig.add_subplot(224) plot_params_science(ax2s, disp_im2, instrum.get_filter(h, 'C{}b'.format(cam_i)), h, central=True) else: disp_im1 = np.copy(im1) if calibrate: if instrum.has_cam_bias(cam_i): disp_im1 -= mbias_data if instrum.has_cam_dark(cam_i): disp_im1 -= mdark_data*instrum.get_exptime(h) disp_im1 = np.divide(disp_im1, mflat_data) if not noplot: ax = fig.add_subplot(121) plot_params_science(ax, disp_im1, instrum.get_filter(h, 'C{}'.format(cam_i)), h, central=False) # and central subregion axs = fig.add_subplot(122) plot_params_science(axs, disp_im1, instrum.get_filter(h, 'C{}'.format(cam_i)), h, central=True) if not noplot: fig.set_tight_layout(True) fig.canvas.draw() if instrum.is_cam_split(cam_i) == True: if instrum.get_centered_filter(h, cam_i).count(instrum.get_filter(h, 'C{}a'.format(cam_i))) != 0: print "\t* The target is focused on the {} filter.".format(instrum.get_filter(h, 'C{}a'.format(cam_i))) elif instrum.get_centered_filter(h, cam_i).count(instrum.get_filter(h, 'C{}b'.format(cam_i))) != 0: print "\t* The target is focused on the {} filter.".format(instrum.get_filter(h, 'C{}b'.format(cam_i))) else: af.print_warn("\t* Warning: The target is NOT focused on an split filter. The target is focused on the {} filter.".format(instrum.get_centered_filter(h, cam_i))) else: # print filter name print '\t* Filter used: {}'.format(instrum.get_filter(h, 'C{}'.format(cam_i))) # query user until valid response is provided valid_entry = False while not valid_entry: # either select all if auto, or have user select if auto: user = '******' else: user = raw_input("\nType Y for YES, N for NO, Q for QUIT: ") if user.lower() == 'y' and instrum.is_cam_split(cam_i): filterA = instrum.get_filter(h, 'C{}a'.format(cam_i)).lower() filterB = instrum.get_filter(h, 'C{}b'.format(cam_i)).lower() if instrum.get_centered_filter(h, cam_i).count(filterB) != 0: direction = 't' elif instrum.get_centered_filter(h, cam_i).count(filterA) != 0: direction = 'b' elif instrum.get_centered_filter(h, cam_i).lower() == 'r' and (filterB == 'h' or filterA == 'z'): # keeping frames 'r' centered with split filter 'Z/Y' or 'J/H' direction = 'b' else: af.print_warn("\t* Warning: Skipping frame not centered on split filter.") user = '******' direction = '' if user.lower() == 'y': h_c = h.copy() if instrum.is_cam_split(cam_i) == True: if direction.lower() == 'b': f_img_post = 'a' f_sky_post = 'b' else: f_img_post = 'b' f_sky_post = 'a' imfits = '{}/{}_{}_{}.fits'.format(targetdir, fits_id, instrum.objname, instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_img_post))) im_img = im[instrum.slice('C{}{}'.format(cam_i, f_img_post))] hnew = instrum.change_header_keywords(h_c, 'C{}{}'.format(cam_i, f_img_post)) pf.writeto(imfits, im_img, header=hnew, clobber=True) # save object frame if fits_list_dict.has_key(instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_img_post))): fits_list_dict[instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_img_post))].append(imfits) else: fits_list_dict[instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_img_post))] = [imfits] # filter side with sky, now saved as object, but different list to keep track skyfits = '{}/{}_{}_{}.fits'.format(targetdir, fits_id, instrum.objname, instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_sky_post))) im_sky = im[instrum.slice('C{}{}'.format(cam_i, f_sky_post))] hnew = instrum.change_header_keywords(h_c, 'C{}{}'.format(cam_i, f_sky_post)) pf.writeto(skyfits, im_sky, header=hnew, clobber=True) # save sky frame if fits_list_dict.has_key(instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_sky_post))): fits_list_dict[instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_sky_post))].append(skyfits) else: fits_list_dict[instrum.get_filter(h_c,'C{}{}'.format(cam_i, f_sky_post))] = [skyfits] valid_entry = True else: imfits = '{}/{}_{}_{}.fits'.format(targetdir, fits_id, instrum.objname, cam_i) im_img = im[instrum.slice('C{}'.format(cam_i))] hnew = instrum.change_header_keywords(h_c, 'C{}'.format(cam_i)) pf.writeto(imfits, im_img, header=hnew, clobber=True) if fits_list_dict.has_key(instrum.get_filter(h_c,'C{}'.format(cam_i))): fits_list_dict[instrum.get_filter(h_c,'C{}'.format(cam_i))].append(imfits) else: fits_list_dict[instrum.get_filter(h_c,'C{}'.format(cam_i))] = [imfits] valid_entry = True elif user.lower() == 'q': # exit function af.print_bold("Exiting...") os.chdir(start_dir) # move back to starting directory pl.close('all') # close image to free memory return elif user.lower() == 'n': # 'N' selected, skip valid_entry = True else: # invalid case af.print_warn("'{}' is not a valid entry.".format(user)) if not auto and not noplot: fig.clear() # clear image hdulist.close() # close FITs file if auto: if not noplot: af.print_head("\nDisplaying automatically selected science frames:") af.show_list(fits_list_dict) else: if not noplot: pl.close('all') # close image to free memory if save_select: dt = datetime.datetime.now() fnout = 'object_'+dt.isoformat().split('.')[0].replace('-','').replace(':','')+'.p' # python pickle extension af.print_head("\nSaving selection dictionary to {}".format(fnout)) pickle.dump( fits_list_dict, open( fnout, 'wb' ) ) # save dictionary to pickle os.chdir(start_dir) # move back to starting directory return fits_list_dict
for kk in templatehdulist[0].header.keys(): if kk not in [ 'SIMPLE', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2', 'OBJECT', 'BUNIT', 'DATE', 'TELESCOP', 'DATE-OBS', 'CRPIX1', 'CRPIX2', 'CRVAL1', 'CRVAL2', 'CTYPE1', 'CTYPE2', 'CDELT1', 'CDELT2', 'MJD-OBS', 'RADESYS', 'EQUINOX', 'BACKEND' ]: del templatehdulist[0].header[kk] del templatehdulist[0].header['LBOUND*'] #templatehdulist[0].header['BMAJ'] = 14.6/3600. #templatehdulist[0].header['BMIN'] = 14.6/3600. #templatehdulist[0].header['BPA'] = 0 #templatehdulist[0].header['BUNIT'] = 'JY/BEAM' pyfits.writeto('nofreq_OrionA_850_auto_mos_clip.fits', templatedata, templatehdulist[0].header, output_verify='exception', clobber=True, checksum=False) #pyfits.writeto('nofreq_OrionA_850_auto_mos_clip_smooth60.fits',templatedata,templatehdulist[0].header,output_verify='exception',clobber=True,checksum=False) templatehdulist = pyfits.open('OrionA_450_auto_mos_clip.fits') templatedata = templatehdulist[0].data[0, :, :] templatehdulist[0].header['NAXIS'] = 2 del templatehdulist[0].header['COMMENT'] del templatehdulist[0].header[''] del templatehdulist[0].header['HISTORY'] templatehdulist[0].header['OBJECT'] = 'OrionA' for kk in templatehdulist[0].header.keys(): if kk not in [ 'SIMPLE', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2', 'OBJECT', 'BUNIT', 'DATE', 'TELESCOP', 'DATE-OBS', 'CRPIX1', 'CRPIX2', 'CRVAL1',
def save(idstr, tractor, nlscale=1., debug=False, plotAll=False, imgi=0, chilo=-10., chihi=10.): print("Index: ", imgi) mod = tractor.getModelImage(imgi) chi = tractor.getChiImage(imgi=imgi) synthfn = 'synth-%s.fits' % idstr print('Writing synthetic image to', synthfn) pyfits.writeto(synthfn, mod, clobber=True) #pfn = 'tractor-%s.pickle' % idstr #print 'Saving state to', pfn #pickle_to_file(tractor, pfn) plt.clf() plt.hist(chi.ravel(), range=(-10, 10), bins=100) plt.savefig('chi2.png') timg = tractor.getImage(imgi) data = timg.getImage() print('Mod type:', mod.dtype) print('Chi type:', chi.dtype) print('Data type:', data.dtype) zr = timg.zr print('zr', zr) # Set up nonlinear mapping based on the statistics of the data image. #sigma = np.median(timg.getInvError()) #print 'sigma', sigma ima = dict(interpolation='nearest', origin='lower') if nlscale == 0.: ima.update(vmin=zr[0], vmax=zr[1]) else: q1, q2, q3 = np.percentile(data.ravel(), [25, 50, 75]) print('Data quartiles:', q1, q2, q3) ima.update(norm=ArcsinhNormalize(mean=q2, std=(1. / nlscale) * (q3 - q1) / 2., vmin=zr[0], vmax=zr[1])) imchi = ima.copy() if nlscale == 0. or True: imchi.update(vmin=chilo, vmax=chihi, norm=None) else: imchi.update(norm=ArcsinhNormalize( mean=0., std=1. / nlscale, vmin=chilo, vmax=chihi)) imdiff = ima.copy() dzr = (zr[1] - zr[0]) / 2. if nlscale == 0.: imdiff.update(vmin=-dzr, vmax=+dzr, norm=None) else: imdiff.update(norm=ArcsinhNormalize( mean=0., std=1. / nlscale, vmin=-dzr, vmax=dzr)) if debug: sources = tractor.getCatalog() wcs = timg.getWcs() allobjx = [] allobjy = [] allobjc = [] pointx = [] pointy = [] xplotx = [] xploty = [] for obj in sources: if (isinstance(obj, PointSource)): xt, yt = wcs.positionToPixel(obj.getPosition(), obj) pointx.append(xt) pointy.append(yt) continue shapes = [] attrType = [] if (isinstance(obj, st.CompositeGalaxy)): for attr in 'shapeExp', 'shapeDev': shapes.append(getattr(obj, attr)) attrType.append(attr) else: shapes.append(getattr(obj, 'shape')) attrType.append(' ') x0, y0 = wcs.positionToPixel(obj.getPosition(), obj) cd = timg.getWcs().cdAtPixel(x0, y0) for i, shape in enumerate(shapes): xplotx.append(x0) xploty.append(y0) T = np.linalg.inv(shape.getTensor(cd)) x, y = [], [] for theta in np.linspace(0, 2 * np.pi, 100): ux = np.cos(theta) uy = np.sin(theta) dx, dy = np.dot(T, np.array([ux, uy])) x.append(x0 + dx) y.append(y0 + dy) allobjx.append(x) allobjy.append(y) if (attrType[i] == 'shapeExp'): allobjc.append('b') elif attrType[i] == 'shapeDev': allobjc.append('g') else: allobjc.append('r') def savepng(pre, img, title=None, **kwargs): fn = '%s-%s.png' % (pre, idstr) print('Saving', fn) plt.clf() plt.imshow(img, **kwargs) ax = plt.axis() if debug: print(len(xplotx), len(allobjx)) for i, (objx, objy, objc) in enumerate(zip(allobjx, allobjy, allobjc)): plt.plot(objx, objy, '-', c=objc) tempx = [] tempx.append(xplotx[i]) tempx.append(objx[0]) tempy = [] tempy.append(xploty[i]) tempy.append(objy[0]) plt.plot(tempx, tempy, '-', c='purple') plt.plot(pointx, pointy, 'y.') plt.plot(xplotx, xploty, 'xg') plt.axis(ax) if title is not None: plt.title(title) plt.colorbar() plt.gray() plt.savefig(fn) savepng('data', data, title='Data ' + timg.name, **ima) savepng('model', mod, title='Model ' + timg.name, **ima) savepng('diff', data - mod, title='Data - Model, ' + timg.name, **imdiff) savepng('chi', chi, title='Chi ' + timg.name, **imchi) print("Chi mean: ", np.mean(chi)) print("Chi median: ", np.median(chi)) if plotAll: debug = False for i, src in enumerate(tractor.getCatalog()): savepng('data-s%i' % (i + 1), data - sky, title='Data ' + timg.name, **ima) modelimg = tractor.getModelImage(timg, srcs=[src]) savepng('model-s%i' % (i + 1), modelimg - sky, title='Model-s%i' % (i + 1), **ima) savepng('diff-s%i' % (i + 1), data - modelimg, title='Model-s%i' % (i + 1), **ima) savepng('chi-s%i' % (i + 1), tractor.getChiImage(imgi, srcs=[src]), title='Chi', **imchi)
ts = np.mean(ts[:,180+4*i:180+4*(i+1)],axis=1)-np.mean(ts[:,180+4*i:180+4*(i+1)]) tts += ts/5. burst_loc = np.where(tts==np.max(tts))[0][0] burst_loc=0 print 'Imaging at location',burst_loc # SNR in TS S2 = (np.max(tts)-np.mean(tts))/np.std(tts) # Calculate the u,v,w and delta_delay for image centre rotation u,v,w,wcorr = calc_image_uvw(vis_fname=vis_fname, image_centre=image_centre, savez=True) # Read visibilities and apply calibration solutions to them vis = applycal(cal_fname_x=cal_fname_x,cal_fname_y=cal_fname_y,vis_fname=vis_fname, flag_ants=flag_ants, flag_chans=flag_chans, burst_loc=burst_loc,burst_nsamp=burst_nsamp) # Phase rotate calibrated visibilities to the image centre w_term = np.exp(-1j*2.0*np.pi*wcorr).astype(np.complex64) # Multiplying by w_term shifts the phase centre to image_centre vis*=w_term u_g,im,psf = dirty_image(vis=vis,u=u,v=v,w=w,fov=fov,res=res,min_base=min_base,max_base=max_base) # Figure out some header keywords n=len(u_g) toa = pf.open(vis_fname)[1].header['MJD'] dtheta = 1.0/( (u_g[1]-u_g[0]) * float(n)) * 180.0/np.pi # Write results into FITS files hdr = pf.Header([('SIMPLE',True),('BITPIX',-32),('ORIGIN','DSA-10 CLIP'), ('AUTHOR','H.K.VEDANTHAM'), ('BTYPE','FLUX-DENSITY'), ('BUNIT','JY/BEAM'), ('OBJECT',object_name), ('POL','I'), ('NAXIS',2), ('NAXIS1',n), ('NAXIS2',n), ('IMTYPE','DIRTY'), ('EPOCH',2000.0), ('EQUINOX',2000.0), ('MJD',toa), ('CTYPE1','RA---SIN'), ('CTYPE2','DEC--SIN'), ('CUNIT1','DEGREE'), ('CUNIT2','DEGREE'), ('CRVAL1',image_centre[0]*180.0/np.pi), ('CRVAL2',image_centre[1]*180.0/np.pi), ('CRPIX1',n/2), ('CRPIX2',n/2), ('CDELT1',-dtheta), ('CDELT2',dtheta)]) pf.writeto(odir+'/'+oname+'.fits',np.fliplr(im.real),hdr,clobber=True)
p3k = hardware.P3K_COM('P3K_COM', configfile = hardwareconfigfile) # Load the flatmap of the DM # initial_flatmap = p3k.grab_current_flatmap() initial_flatmap = pf.open(FDMC_folder + FDMC_name)[0].data # We apply to the DM the initial flatmap status = p3k.load_new_flatmap(fmap.convert_hodm_telem(initial_flatmap)) ipdb.set_trace() phdm = initial_flatmap # On enregistre la commande DM DM_commande_save = phdm pf.writeto(dirwr + folder_Correction + folder_DM_Com + "Commande_" + str(int(0)) + ".fits", DM_commande_save,clobber=True) i = 0 while i <nb_ite: if i > 0: phdm -= pf.open(dirwr + folder_Correction + folder_DM_Com + "Commande_" + str(int(i)) + ".fits")[0].data # We apply the command to the DM status = p3k.load_new_flatmap(fmap.convert_hodm_telem(phdm)) # We read the image with pharo header, Im_SCC_tmp= pharo.take_src_return_imagedata_header(exptime) # On enregistre l'image
def main(exp_time, filename): exp_time = exp_time closeCam() openCam() temperature = queryTemperature() while temperature > 5.0: time.sleep(1) temperature = queryTemperature() print(temperature) #time.sleep(60) #openCam() gcip = GetCCDInfoParams() gcip.request = CCD_INFO_IMAGING gcir0 = GetCCDInfoResults0() err = SBIGUnivDrvCommand(CC_GET_CCD_INFO, gcip, gcir0) #help(gcir0) print('name: ', gcir0.name) print('readoutmodes: ', gcir0.readoutModes) print('cameratype: ', gcir0.cameraType) print('Height: ', gcir0.readoutInfo.height) print('Width: ', gcir0.readoutInfo.width) print('Mode: ', gcir0.readoutInfo.mode) print('Pixelwidth:', gcir0.readoutInfo.pixelWidth) print('Gain: ', gcir0.readoutInfo.gain) #print gcir0.mode pylab.ion() #pylab.gray() for j in numpy.arange(1): #print j img = takeImg(exp_time) darkimg = takeDark(exp_time, SC_CLOSE_SHUTTER) #pyfits.writeto("img_10_"+str(j)+".fits",img,clobber=True) #img=takeImg(exp_time/10.) #pyfits.writeto("img_1_"+str(j)+".fits",img,clobber=True) #img=takeImg(exp_time/100.) #pyfits.writeto("img_0p1_"+str(j)+".fits",img,clobber=True) #img=takeImg(exp_time/1000.) #pyfits.writeto("img_0p01_"+str(j)+".fits",img,clobber=True) #img=takeImg(exp_time/10000.) #pyfits.writeto("img_0p001_"+str(j)+".fits",img,clobber=True) #img=takeImg(exp_time*3.) #pyfits.writeto("img_30_"+str(j)+".fits",img,clobber=True) #pylab.imshow(img,interpolation="nearest") #pylab.draw() #time.sleep(1) #img=takeBias() #pyfits.writeto("bias_"+str(j)+".fits",img,clobber=True) #time.sleep(1) #print numpy.std(img) #closeCam() print('Saving fits') hdu = pyfits.PrimaryHDU(img) pyfits.writeto(filename + ".fit", img, clobber=True) hdu_dark = pyfits.PrimaryHDU(darkimg) pyfits.writeto(filename + '_DarkCurrent.fit', darkimg, clobber=True) print('Done')
dark_diff = dark_midpt2 - dark_midpt1 data -= dark_midpt1 #pyfits.writeto(dark_file.replace('.fits', '.median.fits'), data, hdr) print('Dark Stats') print(dark_diff) science_file = '/data/20160505/007/tbxj*.fits' files = glob(science_file) for f in files: d = pyfits.getdata(f) h = pyfits.getheader(f) midpt1 = np.median(d[539:589, 999:1009]) midpt2 = np.median(d[449:506, 975:1019]) diff = midpt2 - midpt1 print(diff) k = diff / dark_diff temp_dark = data * k d -= midpt1 d -= temp_dark print("k = ", k) h.add_history('Lateral glow subtracted') pyfits.writeto(f.replace('/tbxj', '/ctbxj'), d, h, clobber=True)
def writeFits(self, filename, overWrite=False): """ @brief Write a liteMap as a Fits file""" pyfits.writeto(filename, self.data, self.header, clobber=overWrite)
Cube_freq = hdulist[0].data hdulist.close() i = 45 while i < len(Cube_freq): #print, 'We are working with the frequency number ' + str(int(i)) + '/' + str(int(len(Cube_freq))) if (i % FDM_val) == 0: # We apply to the DM the initial flatmap status = p3k.load_new_flatmap( fmap.convert_hodm_telem(initial_flatmap)) #READ Image before any correction im_flat = pharo.take_src_return_imagedata(exptime) #works, tested im_flat = im_flat[np.newaxis, :, :] pf.writeto(dirwr + folder_DM_Flat_image + "DM_flat_Im_" + str(int((i % FDM_val) / FDM_val)) + '_afterfreq_' + str(int(i)) + ".fits", im_flat, clobber=True) # Construction de la commmande pour le DM DM_commande = Cube_freq[i, :, :] * ampl + initial_flatmap # On applique la commande au DM status = p3k.load_new_flatmap(fmap.convert_hodm_telem(DM_commande)) # On enregistre la commande au DM DM_commande_save = DM_commande[np.newaxis, :, :] pf.writeto(dirwr + folder_DM_vector + "DM_vector_mat_number_" + str(int(i)) + ".fits", DM_commande, clobber=True) # We are taking "nb_im" images. Cube contain all these images
# Let us setup the emcee Ensemble Sampler # It is very simple: just one, self-explanatory line sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(observed_TD, icov)) ##this is the burn-in process import time time0 = time.time() # burnin phase pos, prob, state = sampler.run_mcmc(p0, int(get_number(input_file, "burn_in"))) time1 = time.time() print("The first %s steps time:" % (int(get_number(input_file, "burn_in"))), time1 - time0) print("the acceptance fraction:", sampler.acceptance_fraction) lnlike(pos[-1], observed_TD, icov) sampler.reset() ##really working on the long run using the last step from the burn-in. time1 = time.time() pos, prob, state = sampler.run_mcmc(pos, nsteps) time2 = time.time() print(time2 - time1) samples = sampler.flatchain print(samples.shape[0]) print(sampler.acceptance_fraction) pyfits.writeto(input_file + '.fits', samples, clobber=True) ##output will be the same file with .fits. ##ploting your results using ChainConsumer is a good choice.
import pyfits templatehdulist = pyfits.open('planck_herschel.fits') neg = templatehdulist[0].data < 0 templatehdulist[0].data[neg] = 0 #templatedata = templatehdulist[0].data[0,:,:] #templatedata = templatehdulist[0].data[2,:,:] templatedata = templatehdulist[0].data[3, :, :] templatehdulist[0].header['NAXIS'] = 2 del templatehdulist[0].header['NAXIS3'] del templatehdulist[0].header['PLANE1'] del templatehdulist[0].header['PLANE2'] del templatehdulist[0].header['PLANE3'] #del templatehdulist[0].header['PLANE4'] del templatehdulist[0].header['PLANE5'] del templatehdulist[0].header['PLANE6'] del templatehdulist[0].header['PLANE7'] #pyfits.writeto('planck_herschel_plane1.fits',templatedata,templatehdulist[0].header,output_verify='exception',clobber=True,checksum=False) #pyfits.writeto('lombardi_planck_herschel_plane3_colorT.fits',templatedata,templatehdulist[0].header,output_verify='exception',clobber=True,checksum=False) pyfits.writeto('lombardi_planck_herschel_plane4_colorTerror.fits', templatedata, templatehdulist[0].header, output_verify='exception', clobber=True, checksum=False)
x_range = [grid_x[i], grid_x[i + 1]] y_range = [grid_y[j], grid_y[j + 1]] work_queue.put((x_range, y_range)) # create a queue to pass to workers to store the results result_queue = mp.Queue() procs = [] # spawn workers for i in range(n_process): worker = Worker_convolve(work_queue, result_queue) procs.append(worker) worker.start() # collect the results off the queue for i in range(n_process): result_queue.get() for p in procs: p.join() print("Convolution took %.2f seconds." % (time.time() - tic)) if os.path.isfile(im_file_out): print('Deleting existing image: ', im_file_out) os.remove(im_file_out) print('Writing image: ', im_file_out) pyfits.writeto(im_file_out, shared_nim, header=im_h)
else: outdic = readout_objs( owd+fits_image, params, config['default'], header=use_header, increase=float(increase), preset=instrument ); if ( outdic == False ): print >> sys.stderr,"Finished."; sys.exit(1); if ( outdic == None ): print >> sys.stdout,"Finished."; sys.exit(0); objIDs = outdic['IDs']; objs_list = outdic['images']; hdrs_list = outdic['headers']; # And for each object array in list, write down their pixels ;-) # for i in xrange( len(objIDs) ): print >> sys.stdout, "Writing object (id:) %s image..." % (objIDs[i]); outname = out_name+"%04d.fits" % (objIDs[i]); os.system( 'rm %s &> /dev/null' % (outname) ); pyfits.writeto( outname, objs_list[i], hdrs_list[i] ); print >> sys.stdout, "Done."; os.chdir( owd ); sys.exit(0); # \endcond
# make 2373 to 2370 images bin_number = int(number) for i in range(185, len(coll_dir_root)): savepath = binpath + str(coll_dir_root[i]) + '_bin_python/' if not os.path.exists(savepath): os.makedirs(savepath) datapath = rootpath + str(coll_dir_root[i]) + '/Corrected/' print(datapath) print(savepath) current_dir = fnmatch.filter(listdir(datapath), '*.fits') #to read originial fits sorted_dir = sorted(current_dir) summed = np.zeros([512, 512], dtype='>f4') print(type(len(sorted_dir))) for k in range(0, (len(sorted_dir) - 3)): unbin = datapath + sorted_dir[k] im = fits.open(unbin) im_data = im[0].data summed = summed + im_data for n in range(0, int(len(sorted_dir) / bin_number)): if (k == ((n * bin_number) - 1)): avg = summed / bin_number fitpath = savepath + sorted_dir[k] pyfits.writeto(fitpath, avg, clobber=True) summed = np.zeros([512, 512], dtype='>f4')
import pyfits import sys templatehdulist = pyfits.open(sys.argv[1]) print templatehdulist.info() neg = templatehdulist[0].data < 0 templatehdulist[0].data[neg] = 0 templatedata = templatehdulist[0].data[int(sys.argv[2]),:,:] templatehdulist[0].header['NAXIS'] = 2 #del templatehdulist[0].header['CRPIX3'] #del templatehdulist[0].header['CDELT3'] #del templatehdulist[0].header['CRVAL3'] #del templatehdulist[0].header['CTYPE3'] del templatehdulist[0].header['NAXIS3'] del templatehdulist[0].header['PLANE2'] del templatehdulist[0].header['PLANE3'] del templatehdulist[0].header['PLANE4'] del templatehdulist[0].header['PLANE5'] del templatehdulist[0].header['PLANE6'] del templatehdulist[0].header['PLANE7'] pyfits.writeto(sys.argv[3],templatedata,templatehdulist[0].header,output_verify='exception',clobber=True,checksum=False)
starpositions.append([y_center, x_center]) #starnames.append('comp'+str(counter)) counter += 1 makemasterdark = False makemasterflat = False if makemasterdark: darkpaths = glob('/local/tmp/ARCSAT/20141106/Dark_B2_20141107_0*.fits') testhdr = pyfits.getheader(darkpaths[0]) fullx, fully = 512, 512 #testhdr['FULLX'], testhdr['FULLY'] alldarks = np.zeros((fullx, fully, len(darkpaths))) for i, darkpath in enumerate(darkpaths): if pyfits.getheader(darkpath)['EXPTIME'] == 45: alldarks[:, :, i] = pyfits.getdata(darkpath)[:fully, :fullx] masterdark = np.median(alldarks, axis=2) pyfits.writeto('masterdark.fits', masterdark, clobber=True) testhdr = pyfits.getheader(darkpaths[0]) fullx, fully = 512, 512 #testhdr['FULLX'], testhdr['FULLY'] alldarks = np.zeros((fullx, fully, len(darkpaths))) for i, darkpath in enumerate(darkpaths): if pyfits.getheader(darkpath)['EXPTIME'] == 1: alldarks[:, :, i] = pyfits.getdata(darkpath)[:fully, :fullx] flatdark = np.median(alldarks, axis=2) pyfits.writeto('flatdark.fits', flatdark, clobber=True) else: masterdark = pyfits.getdata('masterdark.fits') if makemasterflat: flatpaths = glob('/local/tmp/ARCSAT/20141106/domeflat_sdss_g_*.fits')