def cut_xy(self, x, y, band, width, name, norm=False): # width is in arcsec imgname = '%s_%s.fits' % (name, band) if os.path.exists(imgname): os.remove(imgname) width_pix = width / self.pixscale[band] xmin = int((x - width_pix / 2.)) xmax = int((x + width_pix / 2.)) ymin = int((y - width_pix / 2.)) ymax = int((y + width_pix / 2.)) # Enforce that the image have equal number of pixels in both dimensions npix = np.min([xmax - xmin, ymax - ymin]) if xmax - xmin > npix: xmin += 1 elif ymax - ymin > npix: ymin += 1 self.xmin = xmin self.ymin = ymin iraf.imcopy('%s[%d:%d,%d:%d]' % (self.images[band], xmin, xmax, ymin, ymax), imgname, verbose=False) if norm: imgsum = pyfits.getdata(imgname).ravel().sum() imgsum = np.abs(imgsum) iraf.imarith(imgname, '/', imgsum, 'temp.fits') os.remove(imgname) os.system('mv temp.fits %s' % imgname) return imgname
def fit_elli(clus_id, line_s): #try: size = c.size values = line_s.split() mask_file = 'ell_mask_' + str(c.rootname) + '_' + str(clus_id) + '.fits' image_file = 'image_' + str(c.rootname) + '_' + str(clus_id) + '.fits' print image_file xcntr_o = float(values[1]) #x center of the object ycntr_o = float(values[2]) #y center of the object xcntr = (size / 2.0) + 1.0 + xcntr_o - int(xcntr_o) ycntr = (size / 2.0) + 1.0 + ycntr_o - int(ycntr_o) mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius mag_zero = 25.256 #magnitude zero point sky = float(values[10]) #sky if (float(values[11]) >= 0 and float(values[11]) <= 180.0): pos_ang = float(values[11]) - 90.0 #position angle if (float(values[11]) < 0 and float(values[11]) >= -180.0): pos_ang = 90.0 - abs(float(values[11])) #position angle if (float(values[11]) > 180 and float(values[11]) <= 360.0): pos_ang = float(values[11]) - 360.0 + 90.0 #position angle if (float(values[11]) >= -360 and float(values[11]) < -180.0): pos_ang = float(values[11]) + 360.0 - 90.0 #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a eg = 1 - axis_rat if (eg <= 0.05): eg = 0.07 major_axis = float(values[14]) #major axis of the object iraf.imcopy(c.outdir + mask_file, c.outdir + 'image' + str(mask_file)[8:] + '.pl') run_elli(image_file, xcntr, ycntr, eg, pos_ang, major_axis)
def fit_elli(clus_id, line_s): #try: size = c.size values = line_s.split() mask_file = 'ell_mask_' + str(imagefile)[:6] + '_' + str(clus_id) + '.fits' image_file = 'image_' + str(imagefile)[:6] + '_' + str(clus_id) + '.fits' print image_file xcntr_o = float(values[1]) #x center of the object ycntr_o = float(values[2]) #y center of the object xcntr = (size/2.0) + 1.0 + xcntr_o - int(xcntr_o) ycntr = (size/2.0) + 1.0 + ycntr_o - int(ycntr_o) mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius mag_zero = 25.256 #magnitude zero point sky = float(values[10]) #sky if(float(values[11])>=0 and float(values[11])<=180.0): pos_ang = float(values[11]) - 90.0 #position angle if(float(values[11])<0 and float(values[11])>=-180.0): pos_ang = 90.0 - abs(float(values[11])) #position angle if(float(values[11])>180 and float(values[11])<=360.0): pos_ang = float(values[11]) - 360.0 + 90.0 #position angle if(float(values[11])>=-360 and float(values[11])<-180.0): pos_ang = float(values[11]) + 360.0 - 90.0 #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a eg = 1 - axis_rat if(eg<=0.05): eg = 0.07 major_axis = float(values[14])#major axis of the object iraf.imcopy(mask_file, 'image'+str(mask_file)[8:]+'.pl') run_elli(image_file, xcntr, ycntr, eg, pos_ang, major_axis)
def trim_img(img, x1, x2, y1, y2): """ Trim a stacked image based on the coordinates given. The image is trimmed using ``imcopy`` through pyraf, so the x and y pixel ranges should be given in the correct ``imcopy`` format. ``[x1:x2,y1:y2]`` Parameters --------- img : str String containing name of the image currently in use x1 : int Pixel coordinate of x1 x2 : int Pixel coordinate of x2 y1 : int Pixel coordinate of y1 y2 : int Pixel coordinate of y2 Returns ------- img : str The new image is given the extension ``.trim.fits``. """ x1, x2 = x1, x2 y1, y2 = y1, y2 input = img.nofits() + '[' + repr(x1) + ':' + repr(x2) + ',' + repr( y1) + ':' + repr(y2) + ']' output = img.nofits() + '.trim.fits' if not os.path.isfile(output): print 'Trimming image: ', img iraf.unlearn(iraf.imcopy) iraf.imcopy(input=input, output=output, verbose='no', mode='h')
def cutout(image, ra, dec, xwidth, ywidth, scale): """ Make a cutout around position (ra, dec) with the dimensions of the cutout being (xwidth, ywidth). image: file name of the image ra, dec: sky coordinates of the center of cutout, in degrees xwidth, ywidth: dimensions of the cutout, **in arcsec** scale: pixel scale of the input image, **in arcsec** """ hdr = pyfits.getheader(image) # First calculate the image coordinates, based on 1-indexing wcs = pywcs.WCS(hdr) xypos = wcs.wcs_sky2pix([[ra, dec]], 1)[0] xc = int(round(xypos[0])) yc = int(round(xypos[1])) xw_pix = int(round(xwidth / (2. * scale))) yw_pix = int(round(ywidth / (2. * scale))) xmin = xc - xw_pix xmax = xc + xw_pix ymin = yc - yw_pix ymax = yc + yw_pix outname = raw_input('Please give the file name of the output image: ') if not len(outname): outname = 'cutout.fits' print "No file name is given; use cutout.fits" if os.path.exists(outname): os.remove(outname) iraf.imcopy(image + '[%d:%d,%d:%d]' % (xmin, xmax, ymin, ymax), outname)
def lris_rpreproc(image): '''Take an LRIS red image, combine into a single extension, subtract overscan, trim, and rotate.''' iraf.keck() iraf.lris() # Need grating name to start grating = get_head("../rawdata/%s" % image, "GRANAME", extn=0) # Convert to single extension fits image iraf.multi2simple("../rawdata/%s" % image, "r%s.fits" % image[8:12], overscan=yes, header=yes, trim=yes, verbose=no, debug=no) # Trim and rotate iraf.imcopy("r%s[%s]" % (image[8:12], LGRATINGS[grating]["trimreg"]), "tr%s" % image[8:12]) iraf.imtranspose("tr%s" % image[8:12], "rtr%s" % image[8:12]) return
def ds9(image, jpg, extra='', save=False): import pyraf, os from pyraf import iraf os.system('rm /tmp/image.fits') iraf.imcopy(image + '[4000:6000,4000:6000]', '/tmp/image.fits') com = [ 'file /tmp/image.fits', 'zoom to fit', 'view colorbar no', 'minmax', extra, 'scale histequ', ] # -quit >& /dev/null &") for c in com: z = 'xpaset -p ds9 ' + c print z os.system(z) for rad in range(10): command = 'echo "circle 5000 5000 ' + str( rad * 60 * 5) + '" | xpaset ds9 regions ' print command os.system(command) if save: command = 'xpaset -p ds9 saveimage jpeg ' + jpg os.system(command)
def trim_img(img,x1,x2,y1,y2): """ Trim a stacked image based on the coordinates given. The image is trimmed using ``imcopy`` through pyraf, so the x and y pixel ranges should be given in the correct ``imcopy`` format. ``[x1:x2,y1:y2]`` Parameters --------- img : str String containing name of the image currently in use x1 : int Pixel coordinate of x1 x2 : int Pixel coordinate of x2 y1 : int Pixel coordinate of y1 y2 : int Pixel coordinate of y2 Returns ------- img : str The new image is given the extension ``.trim.fits``. """ x1,x2 = x1,x2 y1,y2 = y1,y2 input = img.nofits()+'['+repr(x1)+':'+repr(x2)+','+repr(y1)+':'+repr(y2)+']' output = img.nofits()+'.trim.fits' if not os.path.isfile(output): print 'Trimming image: ' ,img iraf.unlearn(iraf.imcopy) iraf.imcopy(input = input,output = output,verbose='no',mode='h')
def imcopy(im, size=size, positions=positions): ''' size=10 position=(ra,dec), default (False,False) ''' outname = os.path.splitext(im)[0] + '_' + str(size) + 'min_cut.fits' if os.path.isfile(outname): os.system('rm ' + outname) hdr = fits.getheader(im) w = WCS(hdr) xpscale, ypscale = wcsutils.proj_plane_pixel_scales(w) * 3600 pixscale = (xpscale + ypscale) / 2. if positions == (False, False): print('RA or DEC input, False, position will be center of', im) px, py = hdr['NAXIS1'] / 2., hdr['NAXIS2'] / 2. ax, bx = px - size / 2 / pixscale * 60, px + size / 2 / pixscale * 60 ay, by = py - size / 2 / pixscale * 60, py + size / 2 / pixscale * 60 else: ra, dec = positions[0], positions[1] px, py = w.wcs_world2pix(ra, dec, 1) print('center pixel coordinates', int(px), int(py)) ax, bx = px - size / 2 / pixscale * 60, px + size / 2 / pixscale * 60 ay, by = py - size / 2 / pixscale * 60, py + size / 2 / pixscale * 60 print('pixel scale =', '%.3f' % (pixscale), size, 'arcmin rectangular cut =', int(bx - ax), 'pixels') region = '[' + str(int(ax)) + ':' + str(int(bx)) + ',' + str( int(ay)) + ':' + str(int(by)) + ']' print(outname, 'will be created') #region='[200:2048,60:2048]' chinim = im + region iraf.imcopy(chinim, output=outname) return 'Done'
def GeoTran(): filters = ['J', '1113', '1184'] for filt in filters: s = 'mfm*' + filt + '*.fits' files = glob.glob(s) flist = 'flist' + str(filt) glist = 'glist' + str(filt) fout = open(flist, 'w') gout = open(glist, 'w') for file in files: ffile = str(file) #+'\n' gfile = 'g' + str(file) #+'\n' fout.write(ffile + '\n') gout.write(gfile + '\n') iraf.geotran(input=ffile, output=gfile, database='PiscesBok', transforms=filt, fluxconserve='no') iraf.rotate(input=gfile, output=gfile, rotation=90) gfilein = 'g' + str(file) + '[-*,*]' gfileout = 'g' + str(file) iraf.imcopy(gfilein, gfileout) infiles = '@' + str(flist) outfiles = '@' + str(glist) #infiles=str(flist) #outfiles=str(glist) fout.close() gout.close()
def test_imcopy(self): iraf.imcopy('dev$pix', 'image.short', verbose=False) with fits.open('image.short.fits') as f: assert len(f) == 1 assert f[0].header['BITPIX'] == 16 assert (f[0].header['ORIGIN'] == 'NOAO-IRAF FITS Image Kernel July 2003') assert f[0].data.shape == (512, 512)
def prelimstuff(): #pull out ext 1 as science image images = glob.glob('*preimage.fits') for im in images: input = im + '[1]' prefix = im.split('.') pre = prefix[0] newname = pre + 'sci.fits' iraf.imcopy(input, newname)
def trim_img(img): x1,x2 = 2508,15798 y1,y2 = 2216,15506 input = img[:-5]+'['+repr(x1)+':'+repr(x2)+','+repr(y1)+':'+repr(y2)+']' output = img[:-5]+'.trim.fits' if not os.path.isfile(output): print 'Trimming image: ' ,img iraf.unlearn(iraf.imcopy) iraf.imcopy(input = input,output = output,verbose='no',mode='h')
def custom1(filename): # for NACO timing mode cubes - removes horizontal banding #iraf.imarith(filename,'-','dark','temp') iraf.imarith(filename,'/','flatK','temp') im = pyfits.getdata('temp.fits') med = median(im.transpose()) out = ((im).transpose()-med).transpose() (pyfits.ImageHDU(out)).writeto("temp2.fits",clobber=True) iraf.imdel('temp') iraf.imcopy('temp2[1]','temp')
def makecutouts24(delta): cutouts24 = [] images24 = rimages24 for i in range(len(images24)): #for i in range(1): #print i,prefix,prefix[i] outcoords = str(prefix[i]) + '.xy24' #iraf.imgets(image=images24[i],param='CDELT1')#get x plate scale iraf.imgets(image=images24[i], param='CD2_1') #get x plate scale on rotated image print iraf.imgets.value xplate = abs(float(iraf.imgets.value)) #deg/pixel dpix = delta / 3600. / xplate / 2. iraf.imgets(image=images24[i], param='naxis1') #get x value corresponding to RA xpixmax = (int(iraf.imgets.value)) #deg/pixel iraf.imgets(image=images24[i], param='naxis2') #get x value corresponding to RA ypixmax = (int(iraf.imgets.value)) #deg/pixel infile = open(outcoords, 'r') #print outcoords for line in infile: #print images24[i],line,outcoords if line.find('#') > -1: continue if len(line) < 2: continue x, y, id = line.split() x = float(x) y = float(y) #delta=100. xmin = int(round(x - dpix)) xmax = int(round(x + dpix)) ymin = int(round(y - dpix)) ymax = int(round(y + dpix)) if xmin < 1: xmin = 1 if ymin < 1: ymin = 1 if xmax > xpixmax: xmax = xpixmax if ymax > ypixmax: ymax = ypixmax s = images24[i] + '[%i:%i,%i:%i]' % (xmin, xmax, ymin, ymax) print s #print ra[i],dec[i] outim = id + 'cutout24.fits' print outim iraf.imcopy(s, outim) cutouts24.append(outim) infile.close() return cutouts24
def shiftTrim(files, xi, xf, yi, yf, dx=None, dy=None, offset_x=0, offset_y=0, fnew='', newdir=TRIMDIR, test=False): """ Given initial and final x and y values of shifted stars, will compute shift in x and y and trim files to compensate for shifting image. Warning: - Assumes that movement across CCD is uniform and predictable in x and y (can be zero) - Discard bad images after trim is complete (requires complete series to trim accurately) - Only use reduced science images - Pixels are discretely counted Parameters: ---------------------- parameter: (dtype) [default (if optional)], information xi: (int), initial x value(s) for star(s) xf: (int), final x values(s) for star(s) yi: (int), initial y value(s) for star(s) yx: (int), final y value(s) for star(s) dx: (int) [None], overwrite x shift value (will ignore xi,xf) dy: (int) [None], overwrite y shift value (will ignore yi,yf) fnew: (string) [None], add string to new file name newdir: (string) [TRIMDIR], trimmed image directory test: (boolean) [False], if True will only print pixel output and not trim files ---------------------- """ xi = np.array(xi) xf = np.array(xf) yi = np.array(yi) yf = np.array(yf) num = len(files) - 1 if dx == None: dx = _dCalc(xi,xf,num,'x') if dy == None: dy = _dCalc(yi,yf,num,'y') print 'dx: %s, dy: %s' % (dx,dy) for i,f in enumerate(files): x = _pixelFinder(dx, i, abs(dx*num), 1, CCDx, offset_x) y = _pixelFinder(dy, i, abs(dy*num), 1, CCDy, offset_y) print "x: %s, y: %s, x*y: %s" % (x, y, ((x[1]-x[0])*(y[1]-y[0]))) f_trim = f + '[%s:%s,%s:%s]' % (x[0],x[1],y[0],y[1]) if fnew != None: f = f + '.' + fnew if test == False: iraf.imcopy(f_trim, newdir + f)
def imcopy(imlist_name, region): """ SAO KL400 SN2019ein [893:1083,337:1695] """ import glob from pyraf import iraf imlist = glob.glob(imlist_name) imlist.sort() for i in range(len(imlist)): inim = imlist[i] iraf.imcopy(input=inim + region, output='t' + inim, verbose='yes')
def sourceExtract(galaxyname): sew = sewpy.SEW(params=["NUMBER"], config={ "DETECT_MINAREA": 50, "DETECT_THRESH": 3.0, "CHECKIMAGE_NAME": galaxyname + "_seg.fits", "CHECKIMAGE_TYPE": "SEGMENTATION", "WEIGHT_TYPE": "MAP_RMS", "WEIGHT_IMAGE": galaxyname + "_sig.fits" }) sew(galaxyname + "_modsub.fits") iraf.imcopy(galaxyname + "_seg.fits", galaxyname + ".fits.pl")
def psfconv(df_image,psf): print "\n************ Running the psf convolution steps ************\n" iraf.imdel('_model.fits') iraf.imdel('_model_4.fits') iraf.imdel('_res*.fits') iraf.imdel('_psf*.fits') iraf.imdel('_df_sub') 'subtract the sky value from the dragonfly image header' try: df_backval = fits.getheader(df_image)['BACKVAL'] iraf.imarith('%s'%df_image,'-','%s'%df_backval,'_df_sub') except: print "WARNING: No BACKVAL to subtract! Skipping the background subtraction..." iraf.imcopy('%s'%df_image,'_df_sub.fits') ##### subtract the background from the cfht image? 'convolve the model with the Dragonfly PSF' if usemodelpsf: makeallisonspsf() psf = './psf/psf_static_fullframe.fits' if verbose: print 'VERBOSE: Using %s for the psf convolution.'%psf 'resample the PSF by a factor of 4' iraf.magnify('%s'%psf,'_psf_4',4,4,interp="spline3") 'this is just to retain the same total flux in the psf' iraf.imarith('_psf_4','*',16.,'_psf_4') 'Convolve with the psf' # from scipy import signal # fluxmoddata,fluxmodheader = fits.getdata('_fluxmod_dragonfly.fits',header=True) # psfdata = fits.getdata('_psf_4.fits') # fluxmodheader['COMMENT']='convolved with '+'_psf_4.fits' # modeldata = signal.fftconvolve(fluxmoddata, psfdata) # print "" # print fluxmoddata.shape # print modeldata.shape # print "" # writeFITS(modeldata,fluxmodheader,'_model_4.fits') iraf.stsdas.analysis.fourier.fconvolve('_fluxmod_dragonfly','_psf_4','_model_4') 'now after the convolution we can go back to the Dragonfly resolution' iraf.blkavg('_model_4','_model',4,4,option="average") return None
def call_lacos(args, science, Nslits=0, longslit=False): outfile = '{0}_lacos.fits'.format(science) #if os.path.isfile(outfile): #os.remove(outfile) if utils.skip(args, 'lacos', outfile): return outfile[:-5] print() print('-' * 30) print() print('Removing cosmic rays with LACos ...') to = time() head = pyfits.getheader(science + '.fits') gain = head['GAIN'] rdnoise = head['RDNOISE'] #utils.delete(outfile) if os.path.isfile(outfile): iraf.imdelete(outfile) os.system('cp -p ' + science + '.fits ' + outfile) utils.removedir('slits') utils.makedir('slits') iraf.imcopy.unlearn() if longslit: slit = '{0}[sci,1]'.format(science) outslit = os.path.join('slits' '{0}_long'.format(science)) outmask = os.path.join('slits' '{0}_longmask'.format(science)) iraf.lacos_spec(slit, outslit, outmask, gain=gain, readn=rdnoise) iraf.imcopy(outslit, '{0}[SCI,1,overwrite]'.format(outfile[:-5]), verbose='no') else: for i in xrange(1, Nslits+1): slit = '{0}[sci,{1}]'.format(science, i) print('slit =', slit) outslit = os.path.join('slits', '{0}_{1}'.format(science, i)) outmask = os.path.join('slits', '{0}_mask{1}'.format(science, i)) if os.path.isfile(outslit): iraf.imdelete(outslit) if os.path.isfile(outmask): iraf.imdelete(outmask) iraf.lacos_spec(slit, outslit, outmask, gain=gain, readn=rdnoise) iraf.imcopy( outslit, '{0}[SCI,{1},overwrite]'.format(outfile[:-5], i), verbose='yes') utils.delete('lacos*') utils.removedir('slits') print(outfile[:-5]) print('Done in {0:.2f}'.format((time()-to)/60)) print() print('-' * 30) print() return outfile[:-5]
def deimos_preproc(image): '''Take a MEF DEIMOS image, extract the relevant extensions, trim the LVM slit masks appropriately, and rename.''' # Needing grating graname = get_head(image, "GRATENAM", extn=0) # "Blue" chip iraf.imcopy("%s[%i]" % (image, BEXT), "d%s_B.fits" % image[6:10]) iraf.ccdproc("d%s_B.fits" % image[6:10], overscan=yes, trim=yes, fixpix=yes, biassec=BBIASSEC, trimsec=BTRIMSEC, fixfile="%s_%i.fits" % (MASK, BEXT)) iraf.imcopy("d%s_B.fits%s" % (image[6:10], DGRATINGS[graname]["blvmreg"]), "td%s_B.fits" % image[6:10]) iraf.imtranspose("td%s_B.fits" % image[6:10], "rtd%s_B.fits" % image[6:10]) # "Red" chip iraf.imcopy("%s[%i]" % (image, REXT), "d%s_R.fits" % image[6:10]) iraf.ccdproc("d%s_R.fits" % image[6:10], overscan=yes, trim=yes, fixpix=yes, biassec=RBIASSEC, trimsec=RTRIMSEC, fixfile="%s_%i.fits" % (MASK, BEXT)) iraf.imcopy("d%s_R.fits%s" % (image[6:10], DGRATINGS[graname]["rlvmreg"]), "td%s_R.fits" % image[6:10]) iraf.imtranspose("td%s_R.fits" % image[6:10], "rtd%s_R.fits" % image[6:10]) iraf.rotate("rtd%s_R.fits" % image[6:10], "rtd%s_R.fits" % image[6:10], 180.0) return
def hLineCorrection(combined_extracted_1d_spectra, grating, path, hlineinter, tempInter, hline_method, log, over, airmass_std=1.0): """ Remove hydrogen lines from the spectrum of a telluric standard, using a model of vega's atmosphere. """ # File for recording shift/scale from calls to "telluric" telluric_shift_scale_record = open('telluric_hlines.txt', 'w') # Remove H lines from standard star correction spectrum no_hline = False if os.path.exists("final_tel_no_hlines_no_norm.fits"): if over: iraf.delete("final_tel_no_hlines_no_norm.fits") else: no_hline = True logging.info("Output file exists and -over- not set - skipping H line removal") if hline_method == "vega" and not no_hline: vega(combined_extracted_1d_spectra, grating, path, hlineinter, telluric_shift_scale_record, log, over) #if hline_method == "linefitAuto" and not no_hline: # linefitAuto(combined_extracted_1d_spectra, grating) # Disabled and untested because interactive scripted iraf tasks are broken... #if hline_method == "linefitManual" and not no_hline: # linefitManual(combined_extracted_1d_spectra+'[sci,1]', grating) #if hline_method == "vega_tweak" and not no_hline: #run vega removal automatically first, then give user chance to interact with spectrum as well # vega(combined_extracted_1d_spectra,grating, path, hlineinter, telluric_shift_scale_record, log, over) # linefitManual("final_tel_no_hlines_no_norm", grating) #if hline_method == "linefit_tweak" and not no_hline: #run Lorentz removal automatically first, then give user chance to interact with spectrum as well # linefitAuto(combined_extracted_1d_spectra,grating) # linefitManual("final_tel_no_hlines_no_norm", grating) if hline_method == "none" and not no_hline: #need to copy files so have right names for later use iraf.imcopy(input=combined_extracted_1d_spectra+'[sci,'+str(1)+']', output="final_tel_no_hlines_no_norm", verbose='no') # Plot the non-hline corrected spectrum and the h-line corrected spectrum. uncorrected = astropy.io.fits.open(combined_extracted_1d_spectra+'.fits')[1].data corrected = astropy.io.fits.open("final_tel_no_hlines_no_norm.fits")[0].data if hlineinter or tempInter: plt.title('Before and After HLine Correction') plt.plot(uncorrected) plt.plot(corrected) plt.show()
def _iraf_dqbits_init(_data): """Initialize common IRAF tasks for dqbits tests """ if not HAS_IRAF: return # imports & package loading iraf.stsdas(_doprint=0) iraf.imgtools(_doprint=0) iraf.artdata(_doprint=0) iraf.mstools(_doprint=0) # create two data files as input (dont care if appropriate to mscombine) iraf.imcopy('dev$pix', _data['dqbits']['input1']) iraf.imcopy('dev$pix', _data['dqbits']['input2'])
def HandleEllipseTask(cutimage, xcntr, ycntr, SizeX, SizeY, sky, out): """Running the ellipse task. SizeX, SizeY are the total size""" manual_profile = 0 try: raise ImportError() #Temporarily kill this loop as the new flagging does not work with pyraf-functions, yet from pyraf import iraf from fitellifunc import run_elli use_pyraf = 1 except ImportError: use_pyraf = 0 print 'No pyraf installed!' WriteError('Cannot find pyraf installation! Trying manual 1d ' + \ 'profile finder\n') if use_pyraf: if out: ell_mask_file = 'OEM_' + c.fstring + '.fits' ell_out = 'OE_' + c.fstring + '.txt' else: ell_mask_file = 'EM_' + c.fstring + '.fits' ell_out = 'E_' + c.fstring + '.txt' plfile = 'GalEllFit.fits.pl' CleanEllipse(ell_out, 0) try: iraf.imcopy(ell_mask_file, plfile, verbose='no') iraf.flpr() except: pass try: run_elli(cutimage, ell_out, xcntr, ycntr, c.eg, \ c.pos_ang, c.major_axis, sky) CleanEllipse(ell_out, 1) try: iraf.flpr() except: pass if exists(ell_out): pass else: manual_profile = 1 except: manual_profile = 1 WriteError('Error in ellipse task. Trying manual profile finder\n') try: c.Flag = SetFlag(c.Flag, GetFlag('ELLIPSE_FAIL')) except badflag: pass if use_pyraf == 0 or manual_profile: FitEllipseManual(cutimage, xcntr, ycntr, SizeX, SizeY, sky, out)
def zeropadfits(smfits, bigfits, padfits): """Pads smfits with zeros to match size of bigfits. Result is padfits, centered as was smfits. Assumes smfits & bigfits are squares w/ odd # of pixels across. """ NY, NX = fits.getheader(bigfits)["NAXIS2"], fits.getheader(bigfits)["NAXIS1"] ny, nx = fits.getheader(smfits)["NAXIS2"], fits.getheader(smfits)["NAXIS1"] print "\nPadding 'smfits' at %ix%i to match 'bigfits' at %ix%i\n" % (nx, ny, NX, NY) center = (NY + 1) / 2 border = ny / 2 lo = center - border hi = center + border croprange = "[%d:%d,%d:%d]" % (lo, hi, lo, hi) imarith(bigfits, "*", 0, padfits) imcopy(smfits, padfits + croprange)
def trim_remove_bias(WhatToTrim): imstat = WhatToTrim + "[2098:2147,*]" mean_overscan = iraf.imstat(imstat, Stdout=1, fields="mean", format="no") single_file_new = WhatToTrim.replace(".fits", ".-overscan_will_be_removed.fits") iraf.imarith(WhatToTrim, "-", mean_overscan[0], single_file_new) old = single_file_new + "[51:2097,3:2063]" new = WhatToTrim.replace(".fits", ".trim_will_be_removed.fits") iraf.imcopy(old, new) os.remove(single_file_new) return new
def trimMyself(self, outname="remcut.fits", region="[400:500,400:500]", verbose=False): self._logger["trimMyself"] = [] if self._Name == "None": self._logger["trimMyself"].append("No filename set, please check.") imCopy = imFits() imCopy._Name = outname iraf.imcopy("%s%s" % (self._Name,region), imCopy._Name) if verbose: self._logger["trimMyself"].append(iraf.imstat(self._Name)) for log in self._logger["trimMyself"]: print log return imCopy
def copy_frame(inlist, inpref='', outpref='r'): # open input list and check if it exists inimg_arr = check_input(inlist, inpref) if isinstance(inimg_arr, int): return 1 # check output images outimg_arr = check_outpref(outpref, inimg_arr) if isinstance(outimg_arr, int): return 1 # check if input images exist and fix bad pixel if requested iraf.unlearn('imcopy') for i in range(len(inimg_arr)): iraf.imcopy(inimg_arr[i], outimg_arr[i]) return 0
def arma_soar(imagen_proc,out_name): """creates a single image for SOAR SAMI It add to the file the exposure time and the filter. It also copies the header of the PRIMARY extenssion.""" output_tmp=imagen_proc[:-5]+'_tmp.fits' iraf.blkrep(input=imagen_proc+'[1]',output=output_tmp,b1=2,b2=2) iraf.imcopy(input=imagen_proc+'[1]',output=output_tmp+'[1:1024,1:1028]',verbose='yes') iraf.imcopy(input=imagen_proc+'[2]',output=output_tmp+'[1025:2048,1:1028]',verbose='yes') iraf.imcopy(input=imagen_proc+'[3]',output=output_tmp+'[1:1024,1029:2056]',verbose='yes') iraf.imcopy(input=imagen_proc+'[4]',output=output_tmp+'[1025:2048,1029:2056]',verbose='yes') iraf.imcopy(input=output_tmp,output=out_name,verbose='yes') iraf.imutil.imdelete(output_tmp) return
def sn_primo(): from pyraf import iraf import unicorn.go_3dhst as go import threedhst.process_grism as proc import unicorn.analysis os.chdir(unicorn.GRISM_HOME + 'SN-PRIMO') #### Copy necessary files from PREP_FLT to DATA os.chdir('PREP_FLT') grism_asn = glob.glob('PRIMO-1???-G141_asn.fits') files = glob.glob('PRIMO-1???-G141_shifts.txt') files.extend(grism_asn) files.append('PRIMO-1026-F160W_tweak.fits') for file in files: status = os.system('cp ' + file + ' ../DATA') #shutil.copy(file, '../DATA') try: iraf.imcopy( '/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F125W_drz.fits[1]', '../DATA/f125w.fits') except: os.remove('../DATA/f125w.fits') iraf.imcopy( '/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F125W_drz.fits[1]', '../DATA/f125w.fits') os.chdir('../') #### Initialize parameters go.set_parameters(direct='F160W', LIMITING_MAGNITUDE=26) #### Main loop for reduction for i, asn in enumerate(grism_asn): threedhst.options[ 'PREFAB_DIRECT_IMAGE'] = '/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F160W_drz.fits' threedhst.options['OTHER_BANDS'] = [[ 'f125w.fits', 'F125W', 1248.6, 26.25 ]] proc.reduction_script(asn_grism_file=asn) unicorn.analysis.make_SED_plots(grism_root=asn.split('_asn.fits')[0]) go.clean_up()
def imagecut(imgname,ra,dec,path,exptime,hsize): hdu=pyfits.open(imgname) xc,yc=get_center_coords(imgname,ra,dec) nmgy=hdu[0].header['nmgy'] xmax=hdu[0].header["naxis1"] ymax=hdu[0].header["naxis2"] if xc < 0 or xc > xmax: raise ValueError("Ra or Dec out of image range") if yc < 0 or yc > ymax: raise ValueError("Ra or Dec out of image range") if xc-hsize<=0: xc=hsize+1 if yc-hsize<=0: yc=hsize+1 if xc+hsize>xmax: xc=xmax-hsize-1 if yc+hsize>ymax: yc=ymax-hsize-1 xl=int(xc-hsize) xr=int(xc+hsize) yd=int(yc-hsize) yu=int(yc+hsize) if os.path.isfile(path+'galaxy.fits'): print "galaxy.fits exists, removing old file\n" os.system('rm '+path+'galaxy.fits') if os.path.isfile(path+'intermed.fits'): os.system('rm '+path+'intermed.fits') iraf.imcopy("%s[0]"%imgname,path+"intermed.fits") iraf.imarith("%sintermed.fits[%i:%i,%i:%i]"%(path,xl,xr,yd,yu),'*',(exptime/nmgy),path+"galaxy.fits") os.system('rm %sintermed.fits'%path) return xc,yc
def sn_marshall(): """ """ from pyraf import iraf import unicorn.go_3dhst as go import threedhst.process_grism as proc import unicorn.analysis os.chdir(unicorn.GRISM_HOME + 'SN-MARSHALL') #### Copy necessary files from PREP_FLT to DATA os.chdir('PREP_FLT') grism_asn = glob.glob('MARSHALL-2??-G141_asn.fits') files = glob.glob('MARSHALL-2*-G141_shifts.txt') files.extend(grism_asn) for file in files: status = os.system('cp ' + file + ' ../DATA') #shutil.copy(file, '../DATA') try: iraf.imcopy('MARSHALL-F125W_drz.fits[1]', '../DATA/f125w_sci.fits') except: os.remove('../DATA/f125w_sci.fits') iraf.imcopy('MARSHALL-F125W_drz.fits[1]', '../DATA/f125w_sci.fits') os.chdir('../') #### Initialize parameters go.set_parameters(direct='F160W', LIMITING_MAGNITUDE=26) threedhst.options[ 'PREFAB_DIRECT_IMAGE'] = '../PREP_FLT/MARSHALL-F160W_drz.fits' threedhst.options['OTHER_BANDS'] = [[ 'f125w_sci.fits', 'F125W', 1248.6, 26.25 ]] #### Main loop for reduction for i in range(len(grism_asn)): asn = grism_asn[i] proc.reduction_script(asn_grism_file=asn) unicorn.analysis.make_SED_plots(grism_root=asn.split('_asn.fits')[0]) go.clean_up()
def mask(res_org,upperlim=0.04): print "\n**** Masking the residual, using upperlim of %s ****\n" % upperlim upperlim = float(upperlim) iraf.imdel('_res_final.fits') iraf.imdel('_model_mask') iraf.imdel('_model_maskb') iraf.imcopy('_model_sc.fits','_model_mask.fits') iraf.imreplace('_model_mask.fits',0,upper=upperlim) iraf.imreplace('_model_mask.fits',1,lower=upperlim/2.) iraf.boxcar('_model_mask','_model_maskb',5,5) iraf.imreplace('_model_maskb.fits',1,lower=0.1) iraf.imreplace('_model_maskb.fits',0,upper=0.9) iraf.imarith(1,'-','_model_maskb','_model_maskb') iraf.imarith('_model_maskb','*',res_org,'_res_final') return None
def prepare_extraction(self): """ Prepares the aXe extraction The module does some preparatory stuff before the extraction can start. This includes copying the simulated dispersed image to AXE_IMAGE_PATH and subtracting the background on this copy. """ import shutil from pyraf import iraf # give brief feedback print('Dummy extraction on the dispersed image:') sys.stdout.flush() # get a random filenames tmpfile1 = get_random_filename('t', '.fits') tmpfile2 = get_random_filename('t', '.fits') # copy the grism image to AXE_IMAGE_PATH shutil.copy(getOUTSIM(self.simul_grisim), getIMAGE(tmpfile1)) # subtract the background from # the grism image expression = "(a - b)" iraf.imexpr(expr=expression, output=tmpfile2, a=getIMAGE(tmpfile1) + '[SCI]', b=self.bck_flux, Stdout=1) # copy the background subracted image to the grism image, sci-extension iraf.imcopy(input=tmpfile2, output=getIMAGE(tmpfile1) + '[SCI,overwrite]', Stdout=1) # delete the background subtracted # tmp image os.unlink(tmpfile2) # store the name of the background # subtracted grism image self.dispersed_image = tmpfile1
def crop(img, center_x, center_y, radius, append="_crop"): ''' crop: Crops a single FITS image using the IRAF imcopy task. Inputs: <img>, a filename specifying the image to be cropped, <center_x> and <center_y>, the coordinates of the object center, and <radius>, the radius to crop to. Optionally, a tag <append> to be appended to the filename can be specified. The default appendant is "_crop". Output: cropped image, written to disk ''' base = splitext(img)[0] extension = splitext(img)[1] upper_x = center_x + radius lower_x = center_x - radius upper_y = center_y + radius lower_y = center_y - radius try: # These indices look backwards, but that's how the IRAF convention works. iraf.imcopy(img +"["+str(lower_x)+":"+str(upper_x)+","+str(lower_y)+":"+str(upper_y)+"]",base + append + extension,Stdout="/dev/null") except iraf.IrafError as e: print("IRAF Error: " + str(e))
def do_ref_apall(WORK_DIR): print '\n + Doing apall for a reference to the masterflat\n' #do quick apall #This will serve as the reference where the apertures are in the process of creating a flat. 30 apertures are found automatically, including the red-most aperture that goes off the image. Change the position of apertures if needed. It is assumed that the object is positioned at roughly 1/3 from the left edge of the slit (1/3 from the right edge of the aperture). # d - briši aperturo # m - dodaj aperturo # o - posortiraj aperture po vrsti na novo # z - odstrani interval fitanja # s - levo in desno, dodaj interval fitanja iraf.unlearn('apall') try: os.remove(observations[WORK_DIR]['objects'][0]+'.fits') except: pass iraf.imcopy(input=observations[WORK_DIR]['ORIG_DIR']+observations[WORK_DIR]['objects'][0], output=observations[WORK_DIR]['objects'][0]) iraf.apall(input=observations[WORK_DIR]['objects'][0], referen='', format='echelle', interac='yes', find='yes', recente='yes', resize='no', edit='yes', trace='yes', fittrac='yes',extract='no', extras='no', review='no', line=550, nsum=10, lower=-6, upper=6, width=11, radius=11, thresho=0.1, nfind=32, minsep=20, maxsep=155, t_func='chebyshev', t_order=4, t_sampl='51:2098', t_niter=5, backgro='none', ylevel='INDEF', llimit=AP_LIM[observations[WORK_DIR]['ap_position']][0], ulimit=AP_LIM[observations[WORK_DIR]['ap_position']][1]) iraf.apresize(input=observations[WORK_DIR]['objects'][0], interac='no', find='no', recente='no', resize='yes', edit='yes', ylevel='INDEF', llimit=AP_LIM[observations[WORK_DIR]['ap_position']][0], ulimit=AP_LIM[observations[WORK_DIR]['ap_position']][1])
def rename(filelist_new): for i in range(len(filelist_new)): hdulist = fits.open(filelist_new[i]) objname = hdulist[0].header['OBJECT'] hdulist.close() old = filelist_new[i] path = os.path.dirname(filelist_new[i]) new = filelist_new[i].replace(os.path.join(path, ""), os.path.join(path, objname) + "_") new2 = new.replace(".fits", ".raw.fits") iraf.imcopy(old, new2) path = os.path.dirname(filelist_new[i]) FileName = os.path.join(path, objname) + ".lis" filelist = open(FileName, "a") filelist.write(new2 + "\n") filelist.close()
def lris_bpreproc(image): '''Take an LRIS blue image, combine into a single extension, subtract overscan, trim, and rotate.''' iraf.keck() iraf.lris() # Need grating name to start grism = get_head("../rawdata/%s" % image, "GRISNAME", extn=0) # Convert to single extension fits image iraf.multi2simple("../rawdata/%s" % image, "b%s.fits" % image[8:12], overscan=yes, header=yes, trim=yes, verbose=no, debug=no) # Trim and rotate iraf.imcopy("b%s[%s]" % (image[8:12], LGRISMS[grism]["trimreg"]), "tb%s" % image[8:12]) iraf.imtranspose("tb%s" % image[8:12], "rtb%s" % image[8:12]) return
def sn_marshall(): """ """ from pyraf import iraf import unicorn.go_3dhst as go import threedhst.process_grism as proc import unicorn.analysis os.chdir(unicorn.GRISM_HOME+'SN-MARSHALL') #### Copy necessary files from PREP_FLT to DATA os.chdir('PREP_FLT') grism_asn = glob.glob('MARSHALL-2??-G141_asn.fits') files=glob.glob('MARSHALL-2*-G141_shifts.txt') files.extend(grism_asn) for file in files: status = os.system('cp '+file+' ../DATA') #shutil.copy(file, '../DATA') try: iraf.imcopy('MARSHALL-F125W_drz.fits[1]', '../DATA/f125w_sci.fits') except: os.remove('../DATA/f125w_sci.fits') iraf.imcopy('MARSHALL-F125W_drz.fits[1]', '../DATA/f125w_sci.fits') os.chdir('../') #### Initialize parameters go.set_parameters(direct='F160W', LIMITING_MAGNITUDE=26) threedhst.options['PREFAB_DIRECT_IMAGE'] = '../PREP_FLT/MARSHALL-F160W_drz.fits' threedhst.options['OTHER_BANDS'] = [['f125w_sci.fits', 'F125W' , 1248.6, 26.25]] #### Main loop for reduction for i in range(len(grism_asn)): asn = grism_asn[i] proc.reduction_script(asn_grism_file=asn) unicorn.analysis.make_SED_plots(grism_root=asn.split('_asn.fits')[0]) go.clean_up()
def _compose_multiext_image(self, sci_ext, err_ext, dq_ext, del_input=1): """ Compose the multi-extension image from individual layers @param sci_ext: the science extension image @type sci_ext: string @param err_ext: the error extension image @type err_ext: string @param dq_ext: the dq extension image @type dq_ext: string """ # set the various keywords self._set_keywords() # put the noisy stuff on the input image, replacing the original first extension iraf.imcopy(input=sci_ext, output=self.image_name + '[' + str(self.extname) + ',overwrite]', Stdout=1) iraf.imcopy(input=err_ext, output=self.image_name + '[ERR, append]', Stdout=1) iraf.imcopy(input=dq_ext, output=self.image_name + '[DQ, append]', Stdout=1) # delete the input images, # if reuqested if del_input: os.unlink(sci_ext) os.unlink(err_ext) os.unlink(dq_ext)
def split_frames(cubefile, range_pairs, target_dir): dirname, filename = os.path.split(cubefile) filebase = filename.rsplit('.', 1)[0] if len(filebase) > 8: warn("IRAF doesn't like long filenames. " "Consider shortening the cube filename ({0})".format(filebase)) outfiles = [] for fromidx, toidx in range_pairs: for i in range(fromidx, toidx+1): infile = cubefile + "[*,*,{0}]".format(i) outfile = '{0}/frame_{1:04}.fit'.format(target_dir, i) debug("imcopy", infile, outfile) iraf.imcopy( # easier to use imcopy and preserve headers than to use pyfits I think input=infile, output=outfile ) outfiles.append(outfile) # f = open(inlst, 'w') # f.writelines(infiles) # f.write('\n') # f.close() # outfile = '{0}/{1}_{2}-{3}.fit'.format(target_dir, filebase, fromidx, toidx) # debug("imcombine input={input} output={output} combine=sum reject=none".format( # input="@{0}".format(inlst), #','.join(infiles), # output=outfile, # )) # outfiles.append(outfile) # iraf.imcombine( # input=','.join(infiles), # output=outfile, # combine="sum", # reject="none", # # project='no', # IRAF wants bools specified / default is nonboolean? # # mclip='no', # ) return outfiles
def GetSkyMedian(gain,trim): # for this and subtracted images there is a new way to get the sky level. # this involves measuring the stddev in the subtracted images and then squaring it # and multiplying by the gain to get the flux per pix which can then be used in the # sky calculations, see below to do so. # first imcopy the subtracted images into a new file # this is to stopp the truncation problem which is common. t=cmd.getoutput('ls /Volumes/DATA/NITES/data/2011-08-12/reduced/CheckSkyInSubtracted/s_M71*.fits').split('\n') if trim == 1: for i in range(0,len(t)): image=str(t[i]+"[1:500,1:500]") image2=str(t[i]) # clobber the old image iraf.imcopy(input=image,output=image2) # then define a box in each image (same box for bright and dark time) and # get the standard deviation in it for each frame that night. Then get the average # nightly stddev which is used to get the sky level. std_list=np.empty(len(t)) for i in range(0,len(t)): h=pf.open(t[i]) d=h[0].data[243:313,274:344] std_list[i]=np.std(d) #print "%s std: %.2f" % (t[i],std_list[i]) av=pow(np.average(std_list),2)*gain print "Sky_bg_pp: %.2f e-" % (av) return av
def apply_rectangle_mask(image, mask, threshold, min_width, min_height): #finds rectangular middle part with good exposure and trims images to that part. hdulist = pyfits.open(image) imdata = hdulist[0].data #print len(imdata) hdulist.close() hdulist = pyfits.open(mask) immask = hdulist[0].data #print len(immask) hdulist.close() good_row = np.zeros(immask.shape[1]) good_col = np.zeros(immask.shape[0]) for i in np.arange(0, immask.shape[1]): good_col[i] = len(np.where(immask[i,:] > threshold*immask.max())[0]) #print good_row for i in np.arange(0, immask.shape[0]): good_row[i] = len(np.where(immask[:,i] > threshold*immask.max())[0]) # if sufficient exposure exists (i.e. not a bad frame), trim the image and the mask accordingly: if (len(np.where(good_row > min_width)[0]) > min_height) & (len(np.where(good_col > min_height)[0]) > min_width): good_width = 0.8 * max(good_row) good_height = 0.8 * max(good_col) ind_row = [np.where(good_row >= good_width)[0].min(), np.where(good_row >= good_width)[0].max()] ind_col = [np.where(good_col >= good_height)[0].min(), np.where(good_col >= good_height)[0].max()] print ind_row print ind_col outdata = deepcopy(imdata[ind_row[0]:ind_row[1], ind_col[0]:ind_col[1]]) outmask = deepcopy(immask[ind_row[0]:ind_row[1], ind_col[0]:ind_col[1]]) outimage = image.replace('.fits', '_trimmed.fits') outmask = mask.replace('.fits', '_trimmed.fits') iraf.imcopy(image + '[' + str(ind_row[0]+1) + ':' + str(ind_row[1]+1) + ',' + str(ind_col[0]+1) + ':' + str(ind_col[1]+1) + ']', outimage) iraf.imcopy(mask + '[' + str(ind_row[0]+1) + ':' + str(ind_row[1]+1) + ',' + str(ind_col[0]+1) + ':' + str(ind_col[1]+1) + ']', outmask)
def slice_rc(img): ''' Slices the Rainbow Camera into 4 different images and adds the 'filter' keyword in the fits file. ''' fname = os.path.basename(img) fdir = os.path.dirname(img) # Running IRAF iraf.noao(_doprint=0) '''f = pf.open(img) h = f[0].header i = f[0].data[1000:-1,0:920] g = f[0].data[0:910,0:900] r = f[0].data[1035:2046, 1050:2046] u = f[0].data[0:910,1050:2046]''' corners = { "i" : [1, 910, 1, 900], "g" : [1, 910, 1060, 2045], "r" : [1040, 2045, 1015, 2045], "u" : [1030, 2045, 1, 900] } filenames = [] for i, b in enumerate(corners.keys()): name = fname.replace(".fits", "_%s.fits"%b) iraf.imcopy("%s[%d:%d,%d:%d]"%(img, corners[b][0], corners[b][1], corners[b][2], corners[b][3]), name) fitsutils.update_par(name, 'filter', b) filenames.append(name) return filenames
def sn_primo(): from pyraf import iraf import unicorn.go_3dhst as go import threedhst.process_grism as proc import unicorn.analysis os.chdir(unicorn.GRISM_HOME+'SN-PRIMO') #### Copy necessary files from PREP_FLT to DATA os.chdir('PREP_FLT') grism_asn = glob.glob('PRIMO-1???-G141_asn.fits') files=glob.glob('PRIMO-1???-G141_shifts.txt') files.extend(grism_asn) files.append('PRIMO-1026-F160W_tweak.fits') for file in files: status = os.system('cp '+file+' ../DATA') #shutil.copy(file, '../DATA') try: iraf.imcopy('/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F125W_drz.fits[1]', '../DATA/f125w.fits') except: os.remove('../DATA/f125w.fits') iraf.imcopy('/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F125W_drz.fits[1]', '../DATA/f125w.fits') os.chdir('../') #### Initialize parameters go.set_parameters(direct='F160W', LIMITING_MAGNITUDE=26) #### Main loop for reduction for i, asn in enumerate(grism_asn): threedhst.options['PREFAB_DIRECT_IMAGE'] = '/Users/gbrammer/CANDELS/GOODS-S/PREP_FLT/PRIMO-F160W_drz.fits' threedhst.options['OTHER_BANDS'] = [['f125w.fits', 'F125W' , 1248.6, 26.25]] proc.reduction_script(asn_grism_file=asn) unicorn.analysis.make_SED_plots(grism_root=asn.split('_asn.fits')[0]) go.clean_up()
def shift_master(ref, master): """ Find the shift between two images. returns the shift in y direction (along columns) """ #make a narow copy of the reference flat and masterflat iraf.imcopy(input="../"+ref+"[1990:2010,*]", output="tmp/masterflat_ref.fitscut",Stdout="/dev/null") iraf.imcopy(input="tmp/masterflat.fits[1990:2010,*]", output="tmp/masterflat.fitscut",Stdout="/dev/null") #find the shift with correlation iraf.images(_doprint=0,Stdout="/dev/null") iraf.immatch(_doprint=0,Stdout="/dev/null") shift=iraf.xregister(input="tmp/masterflat_ref.fitscut, tmp/masterflat.fitscut", reference="tmp/masterflat_ref.fitscut", regions='[*,*]', shifts="tmp/mastershift", output="", databasefmt="no", correlation="fourier", xwindow=3, ywindow=51, xcbox=21, ycbox=21, Stdout=1) shift=float(shift[-2].split()[-2]) #Shift the list of apertures f=open(master, "r") o=open("database/aptmp_masterflat_s", "w") for line in f: l=line.split() if len(l)>0 and l[0]=='begin': l[-1]=str(float(l[-1])-shift) o.write(l[0]+"\t"+l[1]+"\t"+"tmp/masterflat_s"+"\t"+l[3]+"\t"+l[4]+"\t"+l[5]+"\n") elif len(l)>0 and l[0]=='center': l[-1]=str(float(l[-1])-shift) o.write("\t"+l[0]+"\t"+l[1]+"\t"+l[2]+"\n") elif len(l)>0 and l[0]=='image': o.write("\t"+"image"+"\t"+"tmp/masterflat_s"+"\n") else: o.write(line) f.close() o.close()
def slice_rc(img): ''' Slices the Rainbow Camera into 4 different images and adds the 'filter' keyword in the fits file. ''' fname = os.path.basename(img) fdir = os.path.dirname(img) # Running IRAF iraf.noao(_doprint=0) corners = { "g" : [1, 910, 1, 900], "i" : [1, 910, 1060, 2045], "r" : [1040, 2045, 1015, 2045], "u" : [1030, 2045, 1, 900] } filenames = [] for i, b in enumerate(corners.keys()): logger.info( "Slicing for filter %s"% b) name = fname.replace(".fits", "_%s.fits"%b) #Clean first if (os.path.isfile(name)): os.remove(name) iraf.imcopy("%s[%d:%d,%d:%d]"%(img, corners[b][0], corners[b][1], corners[b][2], corners[b][3]), name) fitsutils.update_par(name, 'filter', b) is_on_target(name) filenames.append(name) return filenames
iraf.images() iraf.images.imfit() iraf.images.imutil() #iraf.crutil() if mode == 'MMIRS': flipme = True headers = iraf.imhead(fitsfile, long='yes', Stdout=1) for line in headers: if re.match('WAT', line): flipme = False if flipme: print "not flipped" iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2, upper=2, lower=2, ngrow=15) iraf.imarith(fitsfile, '-', 'back.fits', fitsfile) iraf.imcopy(fitsfile + '[*,*]', fitsfile) # iraf.cosmicrays(fitsfile, fitsfile, interactive='no') else: print "flipped" else: iraf.imsurfit(fitsfile, 'back.fits', xorder=2, yorder=2) iraf.imarith(fitsfile, '-', 'back.fits', fitsfile) hdu = rfits(fitsfile) image = hdu.data hdr = hdu.header try: rot = hdr['ROT'] except KeyError:
slit_piece3 = '%s[%d][4170:6218, *]' % (slitted_fits[i], ext_num) #for some reason, if I did specify the y-dimensions, then I get some corruption error, not sure why; code wrong? #slit_piece1 = '%s[%d][1:2048,%d:%d]' % (slitted_fits[i], ext_num, secy1[j], secy2[j]) #slit_piece2 = '%s[%d][2067:4114,%d:%d]' % (slitted_fits[i], ext_num, secy1[j], secy2[j]) #slit_piece3 = '%s[%d][4133:6180,%d:%d]' % (slitted_fits[i], ext_num, secy1[j], secy2[j]) #Now when I copy it into the 3 CCD extension image the coordinates aren't the same as the slit dimensions. That's because each extension starts over at 1 for the x-dimension; therefore each CCD extension goes from 1:2048 and not the x-dimensions of the slitted-image ext1 = '%s[1][1:2048,%d:%d]' % (new_mosaics[i], secy1[j], secy2[j]) ext2 = '%s[2][1:2048,%d:%d]' % (new_mosaics[i], secy1[j], secy2[j]) ext3 = '%s[3][1:2048,%d:%d]' % (new_mosaics[i], secy1[j], secy2[j]) #now running the actual copying of the image iraf.imcopy(slit_piece1, ext1) iraf.imcopy(slit_piece2, ext2) iraf.imcopy(slit_piece3, ext3) os.system('rm frost_files.list') os.system('rm retilted_optimum_corrected.list') end = time.strftime("%H:%M:%S") print "Start time: ", start print "End time: ", end ''' http://www.stsci.edu/hst/HST_overview/documents/datahandbook/intro_ch23.html Task Parameters:
z11 = float(_z11) z22 = float(_z22) else: z11, z22 = '', '' answ = 'n' while answ == 'n': coordlist = str(_xpos) + ' ' + str(_ypos) + ' ' + str(_mag) os.system('echo ' + coordlist + ' > ddd') iraf.imarith(img0, '-', img0, '_tmp.fits', verbose='no') if float(_mag) != 0.0: iraf.daophot.addstar("_tmp.fits", 'ddd', psfimg, "_tmp2.fits", nstar=1, veri='no', simple='yes', verb='no') iraf.imarith(img0, '-', '_tmp2.fits', imgout, verbose='yes') else: print '\#### copy file ' iraf.imcopy(img0, imgout, verbose='yes') if _show: _z11, _z22, goon = agnkey.util.display_image(imgout, 2, z11, z22, False) answ = raw_input('ok ? [[y]/n]') if not answ: answ = 'y' else: answ = 'y' agnkey.util.delete('_tmp.fits,_tmp2.fits,_tmp2.fits.art,ddd') if answ == 'n': agnkey.util.delete(imgout) _mag0 = raw_input('which magnitude ' + str(_mag) + ' ?') if _mag0: _mag = _mag0 print 'insert in the archive' _targid = agnkey.agnsqldef.targimg(imgout) hd = agnkey.util.readhdr(imgout)
yxor=num, yyor=num, yxterms="half", calctype="real", inter='No',verbose='no') imgtemp = temp_file0+'_temp.fits' imgtarg = temp_file0+'_targ.fits' imgout = temp_file0+'_out.fits' tempmask = temp_file0+'_tempmask.fits' targmask = temp_file0+'_targmask.fits' agnkey.util.delete(imgtemp) agnkey.util.delete(imgtarg) agnkey.util.delete(imgout) agnkey.util.delete(tempmask) agnkey.util.delete(targmask) agnkey.util.delete(temp_file0+'_tempmask3.fits') if os.path.isfile(re.sub('.fits','.clean.fits',_dir+imgtarg0)): print 'use clean' iraf.imcopy(re.sub('.fits','.clean.fits', _dir+imgtarg0)+'[0]', imgtarg, verbose='yes') else: iraf.imcopy(_dir + imgtarg0+'[0]', imgtarg, verbose='no') iraf.imcopy(_dir + targmask0, targmask, verbose='no') # try: iraf.immatch.gregister(_dirtemp + imgtemp0, imgtemp, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric", interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no') try: iraf.immatch.gregister(_dirtemp + tempmask0, tempmask, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric", interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no') except: # this is strange, sometime the registration of the msk fail the first time, but not the second time iraf.immatch.gregister(_dirtemp + tempmask0, tempmask, "tmp$db"+temp_file0, temp_file0+"_tmpcoo", geometr="geometric", interpo=_interpolation, boundar='constant', constan=0, flux='yes', verbose='no')
wavefilter.append(float(p[0])) transmission.append(float(p[1])) wavefilterv = array(wavefilter) transmissionv = array(transmission) fil_obs_min= min(wavefilterv) fil_obs_max= int(max(wavefilterv)) #integer is needed for a sharper cut-off ############################################################# ############################################################# ##### Mananging the spectra ############################################################# spec = sn + "[*,1,1]" # generally multidimension iraf.imcopy(sn+'[*,1,1]','sn.fits',verbose='no') # to create a onedimension fit to use during the script spectrum=iraf.wspec("sn.fits","sn_xbbody.txt", header='no') lcf = open('sn_xbbody.txt','r') riga = lcf.readlines() lcf.close() wave,flux= [],[] for line in riga: p = line.split() wave.append(float(p[0])) flux.append(float(p[1])) wavev = array(wave) fluxv = array(flux) waveobs_min= min(wavev)
image_slices = functions.read_table(image_slices) ### Loop through and cut out each slice ### save in individual files print "Chopping image into its image slices" os.chdir(file_path_temp) slices_file_list = "" for i in range(len(image_slices)): start_column = int(image_slices[i][0]) end_column = int(image_slices[i][1]) region = '[1:4093,' + str(start_column) + ':'+str(end_column)+']' print region os.system("rm " + str(i) +"_"+ file_name) iraf.imcopy( input = biassubtracted_file_name + region,\ output = str(i) +'_'+ file_name,\ verbose =1) ### Save the names of the output files into a list slices_file_list = slices_file_list + str(i)+"_" + file_name + "\n" ### Save this list of output files into an ascii file ### This is called on by future programs image_slices_file_list = open("slice_"+file_name + ".txt","w") image_slices_file_list.write(slices_file_list) image_slices_file_list.close()
def wirc_flatscale(infiles,inflat,outflat,clipfrac=0.03, clobber=globclob): # "infiles" is a list of filenames inlist=','.join(infiles) bpmfile=get_head(infiles[0],'BPM') # Make a median combination of input files imcmb1=iraf.mktemp("iqwircc")+".fits" iraf.imcombine(inlist,imcmb1,combine="median",reject="none", project=no,outtype="real",outlimits="",offsets="", masktype="none",blank=0.0,scale="!SKYBKG",zero="none", weight="none",statsec="",lthreshold="INDEF", hthreshold="INDEF",nkeep=1) [naxis1,naxis2]=get_head(imcmb1,['NAXIS1','NAXIS2']) npixall=float(naxis1)*float(naxis2) # Calculate sky background & divide, subtract 1 skybkg=wirc_sky(imcmb1,bpmfile) imcmb2=iraf.mktemp("iqwircc")+".fits" iraf.imarith(imcmb1,'/',skybkg,imcmb2,verbose=no,noact=no) iraf.imdel(imcmb1,verify=no,go_ahead=yes) iraf.imarith(imcmb2,'-',1.0,imcmb1,verbose=no,noact=no) # Surface fit to median image imsurf1=iraf.mktemp("iqwircs")+".fits" iraf.imsurfit(imcmb1,imsurf1,xorder=6,yorder=6,type_output="fit", function="chebyshev",cross_terms=yes, xmedian=21,ymedian=21,median_percent=50.0, lower=0.0,upper=0.0,ngrow=0,niter=0, regions="all",rows="*",columns="*",border=50, sections="",circle="",div_min="INDEF") # Corresponding bad pixel mask imbpm1=iraf.mktemp("iqwircb")+".pl" iraf.imexpr("abs(x)>%.3f ? 0 : 1" % clipfrac,imbpm1,x=imsurf1) # Subtract 1.0 from flatfield imflat1=iraf.mktemp("iqwircf")+".fits" iraf.imarith(inflat,'-',1.0,imflat1,verbose=no,noact=no) # Surface fit to the flatfield imsurf2=iraf.mktemp("iqwircs")+".fits" iraf.imsurfit(imflat1,imsurf2,xorder=6,yorder=6,type_output="fit", function="chebyshev",cross_terms=yes, xmedian=21,ymedian=21,median_percent=50.0, lower=0.0,upper=0.0,ngrow=0,niter=0, regions="all",rows="*",columns="*",border=50, sections="",circle="",div_min="INDEF") # Corresponding bad pixel mask imbpm2=iraf.mktemp("iqwircb")+".pl" iraf.imexpr("abs(x)>%.3f ? 0 : 1" % clipfrac,imbpm2,x=imsurf2) # Combine bad pixel masks for median + flat imbpm3=iraf.mktemp("iqwircb")+".pl" iraf.imexpr("(x>0 || y>0) ? 1 : 0",imbpm3,x=imbpm1,y=imbpm2) # Calculate the ratio image imratio=iraf.mktemp("iqwircr")+".fits" iraf.imexpr("z>0 ? 0 : x/y",imratio,x=imsurf1,y=imsurf2,z=imbpm3) # Mimstat on the ratio image mimstat=iraf.mimstatistics(imratio,imasks=imbpm3,omasks="", fields='image,npix,mean,stddev,min,max,mode', lower='INDEF',upper='INDEF',nclip=4, lsigma=5.0,usigma=5.0,binwidth=0.1, format=no,Stdout=1,Stderr=1) mimels=mimstat[0].split() npix=float(mimels[1]) xmult=float(mimels[2]) # Check that a reasonable number of pixels have made the grade check_exist(outflat,'w',clobber=clobber) if npix<0.05*npixall: print "Less than 5% of pixels passed the cut... preserving flatfield" iraf.imcopy(inflat,outflat,verbose=no) xmult=1.0 else: # Create the final flatfield image iraf.imexpr("x*%.3f + 1" % xmult,outflat,x=imflat1) # Update header keywords update_head(outflat,'RESCALE',1,"Flatfield has been rescaled") update_head(outflat,'ORIGFLAT',inflat, "Input flatfield name (before rescaling)") update_head(outflat,'XMULT',xmult, "Multiplied ORIGFLAT by this factor to rescale") # Clean up iraf.imdel(imcmb1,verify=no,go_ahead=yes) iraf.imdel(imcmb2,verify=no,go_ahead=yes) iraf.imdel(imsurf1,verify=no,go_ahead=yes) iraf.imdel(imsurf2,verify=no,go_ahead=yes) iraf.imdel(imbpm1,verify=no,go_ahead=yes) iraf.imdel(imbpm2,verify=no,go_ahead=yes) iraf.imdel(imbpm3,verify=no,go_ahead=yes) iraf.imdel(imflat1,verify=no,go_ahead=yes) iraf.imdel(imratio,verify=no,go_ahead=yes)
def defringe(self,qombos): if type(qombos) is StringType: qombo=qombos qombos=[qombo] # Loop over input list of qombos for qombo in qombos: # Corresponding files offiles2=self.science_images(qombo,pfx=self.flatpfx) if len(offiles2)==0: continue oflist2=','.join(offiles2) # Check for existence of fringe image offrg=self.fringepre+qombo+'.fits' if not os.path.exists(offrg): self.mkfringe(qombo) # Defringe images iraf.iqdefringe(oflist2,offrg,outpfx=self.defrgpfx, skykey="SKYBKG",minlim=no, clobber=self.clobber,verbose=no) # List of defringed images offiles3=pfx_list(offiles2,self.defrgpfx) oflist3=','.join(offiles3) # Clipping of defringed images clipreg=self.clipreg if len(clipreg)>0: re1=re.search('(\d+):(\d+),(\d+):(\d+)',clipreg) if re1: szx=int(re1.group(2))-int(re1.group(1))+1 szy=int(re1.group(4))-int(re1.group(3))+1 for image in offiles3: # Check that it's not already clipped naxis1,naxis2=get_head(image,['NAXIS1','NAXIS2']) if naxis1<=szx and naxis2<=szy: continue # Clip the defringed images iraf.imcopy(image+clipreg,image,verbose=no) # Clip the corresponding BPM oldbpm=get_head(image,'BPM') newbpm=oldbpm.replace('.pl',self.clpsfx+'.pl') if os.path.exists(oldbpm) and \ not os.path.exists(newbpm): iraf.imcopy(oldbpm+clipreg,newbpm,verbose=yes) # Update the image headers update_head(image,'BPM',newbpm) else: print "Failed to parse clipping region... not clipping" #################### # Attempt WCS refinement for image in offiles3: # Skip images with good WCS #if check_head(image,'IQWCS'): #if int(get_head(image,'IQWCS')): #continue # Object-detection iraf.iqobjs(image,self.sigma,self.satval,skyval="!SKYBKG", masksfx=self.masksfx,wtimage="",minlim=no, clobber=yes,verbose=no) # WCS refinement wirc_anet(image) #iraf.iqwcs(image,objkey=self.objkey,rakey='RA', #deckey='DEC',pixscl=self.pix,pixtol=0.05, #starfile='!STARFILE',catalog='ir', #nstar=60,nstarmax=60, #diffuse=yes,clobber=yes,verbose=self.verbose) # Figure out which have good WCS wcsdict=keysplit(offiles3,'IQWCS') # Interpolate WCS onto files without good WCS so far if wcsdict.has_key(""): wcsbadfiles=wcsdict[""] if len(wcsbadfiles)==len(offiles3): print "No images with good WCS in this set" else: wcsgoodfiles=wcsdict[1] wcsinterp(wcsbadfiles,wcsgoodfiles) # Done with Step #5 update_head(offiles3,'IQWRCSTP',5, "Stage of IQWIRC processing")
def flatten(self,qombos,flat=None,flatscale=no): if type(qombos) is StringType: qombo=qombos qombos=[qombo] # Loop over input list of qombos for qombo in qombos: # Check for presence of calibration files obj,filt=qombo_split(qombo) if not self.check_calib(filt): self.calib() # Corresponding files offiles=self.science_images(qombo) if len(offiles)==0: continue # Make a text ".lis" file to record this putlines(qombo+'.lis',offiles,clobber=yes) # Create the renormalized flatfield inflat=self.flatpre+filt+'.fits' outflat=self.flatpre+qombo+'.fits' check_exist(outflat,"w",clobber=self.clobber) # Rescale flatfield if requested if flatscale: wirc_flatscale(offiles,inflat,outflat,clipfrac=0.02, clobber=yes) else: iraf.imcopy(inflat,outflat,verbose=no) check_exist(outflat,"r") # Further preprocessing for image in offiles: # Flatfielding update_head(image,self.filtset,filt) iraf.iqflatten(image,outflat,outpfx=self.flatpfx, normflat=yes,statsec=self.statsec, subsky=yes,vignflat=no, clobber=yes,verbose=no) # Set bad pixels to zero image2=self.flatpfx+image iraf.iqmask(image2,mask='!BPM',method='constant',value=0.0, clobber=yes,verbose=no) # Reject cosmic rays / bad pixels #iraf.lacos_im(image2, "tempcr.fits", "tempcrmask.fits", #gain=5.5, readn=12.0, statsec="*,*", #skyval=get_head(image2,"SKYBKG"), sigclip=6.0, #sigfrac=0.5, objlim=1.0, niter=1, verbose=no) #shutil.move("tempcr.fits", image2) #os.remove("tempcrmask.fits") # Write Useful Keywords update_head(image2,'PROCESSD',1, "Image has been processed by iqwirc") update_head(image2,'PROCVER',version, "Version number of iqwirc used") update_head(image2,'ZEROPT','INDEF', "Zero-point relative to 2MASS or INDEF") update_head(image2,'PROCPROB','None', "Problems encountered in iqwirc processing") # List of flatfielded images offiles2=pfx_list(offiles,self.flatpfx) oflist2=','.join(offiles2) # Object-detection iraf.iqobjs(oflist2,self.sigma,self.satval, skyval="!SKYBKG",masksfx=self.masksfx, wtimage="",minlim=no, clobber=yes,verbose=no) # Attempt WCS refinement iraf.iqwcs(oflist2,objkey=self.objkey,rakey='RA', deckey='DEC',pixscl=self.pix,pixtol=0.05, starfile='!STARFILE',catalog='ir', nstar=60,nstarmax=60, diffuse=yes,clobber=yes,verbose=self.verbose) # Done with Step #3 update_head(offiles2,'IQWRCSTP',3, "Stage of IQWIRC processing")
def calib(self): dolamps=no lampfilts=[] lampfiles=[] lamponfiles={} lampofffiles={} allfiles=glob.glob(self.inpat) # Look for flatfield images for image in allfiles: # Criteria for flatfield images lampval=get_head(image,self.lampkey) if re.search(self.lampon,lampval,re.I) or \ re.search(self.lampoff,lampval,re.I): dolamps=yes filter=wirc_filter(image) if filter not in lampfilts: lampfilts.append(filter) if re.search(self.lampon,lampval,re.I): update_head(image,self.lampfkey,'On-'+filter, "Lamp status + Filter setting") lampfiles.append(image) if lamponfiles.has_key(filter): lamponfiles[filter].append(image) else: lamponfiles[filter]=[image] elif re.search(self.lampoff,lampval,re.I): update_head(image,self.lampfkey,'Off-'+filter, "Lamp status + Filter setting") lampfiles.append(image) if lampofffiles.has_key(filter): lampofffiles[filter].append(image) else: lampofffiles[filter]=[image] lamplist=','.join(lampfiles) # Use flatfield images to make our calibration files if dolamps: # Make lamp-on and lamp-off flats & bad-pixel mask iraf.iqcals(lamplist,dobias=no,biasproc=no, doflats=yes,flatproc=no,flatkey=self.flatkey, flatre=self.flatre,filtkey=self.lampfkey, flatpre=self.flatpre,flatscale="none", statsec=self.statsec,normflat=no, dobpm=yes,bpmmethod='flatratio', bpmroot=self.bpmroot, bpmffilt=self.bpmffilt,classkey="", mosaic=no,clobber=self.clobber, verbose=self.verbose) # Subtract lamp-off flat from lamp-on flat, if necessary for filter in lamponfiles.keys(): onflat=self.flatpre+'On-'+filter+'.fits' offflat=self.flatpre+'Off-'+filter+'.fits' outflat=self.flatpre+filter+'.fits' check_exist(outflat,'w',self.clobber) if filter in self.onofflist: if not lampofffiles.has_key(filter): print "Need lamp-off flats for filter '%s'" % filter continue iraf.imarith(onflat,'-',offflat,outflat, verbose=self.verbose,noact=no) else: # For some filters "lampsoff" counts are negligible iraf.imcopy(onflat,outflat,verbose=yes) update_head(outflat,self.filtset,filter) medflat=wirc_sky(outflat,"") #update_head(outflat,"NORMFLAT",1, update_head(outflat,"NORMFLAT",0, "Has flatfield been normalized?") update_head(outflat,"MEDFLAT",medflat, "Median value of original flatfield")