def getCenter(imgs): ''' getCenter: Uses the Iraf imcntr task to find the centers of a list of objects Inputs: a list of image filenames <imgs> (see getIter()) to find the centers of. Outputs: a numpy array with shape (len(imgs), 3), of the form ... ... ... <img_name> <center_x> <center_y> ... ... ... where all three fields are strings. ''' out = np.full((len(imgs),3),"-1",dtype=object) shape_re = re.compile(r"\[(?P<rows>\d+),(?P<cols>\d+)\]") center_re = re.compile(r"x:\s+(?P<center_x>\d+\.\d*)\s+y:\s+(?P<center_y>\d+\.\d*)") for i in range(0,len(imgs)): header_string = iraf.imheader(imgs[i],Stdout=1)[0] shape = shape_re.search(header_string) rows,cols = -1,-1 if shape: rows = int(shape.group("rows")) cols = int(shape.group("cols")) else: sys.exit(1) #end if center_string = iraf.imcntr(imgs[i],rows/2,cols/2,cboxsize=31, Stdout=1)[0] center = center_re.search(center_string) center_x,center_y = "-1","-1" if center: center_x = str(int(float(center.group("center_x")))) center_y = str(int(float(center.group("center_y")))) else: sys.exit(1) #end if out[i,0] = imgs[i] out[i,1] = center_x out[i,2] = center_y #end loop return out
sys.exit() # Get (x,y) values for last two measurements (x1, y1, _) = lines[-1].split(None, 2) # find other 3 stars and compute focus offset x1 = float(x1) y1 = float(y1) x2 = x1 y2 = y1 - refdistance x3 = x1 - refdistance / 2 x4 = x1 + refdistance / 2 y3 = y1 - refdistance / 2 y4 = y3 s1 = iraf.imcntr(filename + "[1]", x1, y1, cboxsize=boxsize, Stdout=1) s2 = iraf.imcntr(filename + "[1]", x2, y2, cboxsize=boxsize, Stdout=1) s3 = iraf.imcntr(filename + "[1]", x3, y3, cboxsize=boxsize, Stdout=1) s4 = iraf.imcntr(filename + "[1]", x4, y4, cboxsize=boxsize, Stdout=1) x1 = float(s1[0].split()[2]) y1 = float(s1[0].split()[4]) x2 = float(s2[0].split()[2]) y2 = float(s2[0].split()[4]) x3 = float(s3[0].split()[2]) y3 = float(s3[0].split()[4]) x4 = float(s4[0].split()[2]) y4 = float(s4[0].split()[4]) xdist = sqrt((x4 - x3) * (x4 - x3) + (y4 - y3) * (y4 - y3)) ydist = sqrt((y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2))
def findImprovedAlignment(self): """ Tries to find stars to be used for improved alignment. Generates coordinate lists for each POL file and for every _flt file. Maps all the positions to uncorrected/distorted frame using pixtopix transformation. Finally, runs the tweakreg to find improved alignment and updates the WCS in the headers. """ #find some good stars source = sourceFinding(pf.open(self.settings['sourceImage'])[1].data) results = source.runAll() #find improved locations for each star acc = [] for x, y in zip(results['xcms'], results['ycms']): acc.append(iraf.imcntr('POL*_drz.fits[1]', x_init=x, y_init=y, cboxsize=45, Stdout=1)) o = open('tmp.txt', 'w') o.write('#File written on {0:>s}\n'.format(datetime.datetime.isoformat(datetime.datetime.now()))) for line in acc: for l in line: o.write(l.replace('[', '').replace(']', '') + '\n') o.close() data = open('tmp.txt').readlines() pol0 = open('POL0coords.txt', 'w') pol60 = open('POL60coords.txt', 'w') pol120 = open('POL120coords.txt', 'w') pol0r = open('POL0coords.reg', 'w') pol60r = open('POL60coords.reg', 'w') pol120r = open('POL120coords.reg', 'w') for line in data: tmp = line.split(':') x = tmp[1].replace('y', '').strip() y = tmp[2].strip() out = '%s %s\n' % (x, y) reg = 'image;circle(%s,%s,5)\n' % ( x, y) if 'POL0' in line: pol0.write(out) pol0r.write(reg) elif 'POL60' in line: pol60.write(out) pol60r.write(reg) elif 'POL120' in line: pol120.write(out) pol120r.write(reg) else: print 'Skipping line:', line pol0.close() pol60.close() pol120.close() pol0r.close() pol60r.close() pol120r.close() data = open('../rawFiles.txt').readlines() pol0 = [line.split()[0].split('_raw')[0] + '_flt.fits' for line in data if 'POL0' in line.split()[2]] pol60 = [line.split()[0].split('_raw')[0] + '_flt.fits' for line in data if 'POL60' in line.split()[2]] pol120 = [line.split()[0].split('_raw')[0] + '_flt.fits' for line in data if 'POL120' in line.split()[2]] for file in pol0: x, y = pixtopix.tran(file + "[1]", 'POL0V_drz.fits[1]', 'backward', coords='POL0coords.txt', output=file.replace('.fits', '') + '.coords', verbose=False) for file in pol60: x, y = pixtopix.tran(file + "[1]", 'POL60V_drz.fits[1]', 'backward', coords='POL60coords.txt', output=file.replace('.fits', '') + '.coords', verbose=False) for file in pol120: x, y = pixtopix.tran(file + "[1]", 'POL120V_drz.fits[1]', 'backward', coords='POL120coords.txt', output=file.replace('.fits', '') + '.coords', verbose=False) del x del y coords = glob.glob('*_flt.coords') #remove comment lines from each coords file and produce a DS9 region file for f in coords: data = open(f).readlines() out = open(f, 'w') reg = open(f.replace('.coords', '.reg'), 'w') reg.write('#File written on {0:>s}\n'.format(datetime.datetime.isoformat(datetime.datetime.now()))) for line in data: if not line.startswith('#'): out.write(line) tmp = line.split() reg.write('image;circle({0:>s},{1:>s},5)\n'.format(tmp[0], tmp[1])) out.close() reg.close() #create a mapping file out = open('regcatalog.txt', 'w') for f in coords: out.write('%s %s\n' % (f.replace('.coords', '.fits'), f)) out.close() params = {'catfile': 'regcatalog.txt', 'shiftfile': True, 'outshifts': 'flt1_shifts.txt', 'updatehdr': True, 'verbose': False, 'minobj': 15, 'use2dhist': False, 'see2dplot': False, 'searchrad': 50, 'searchunits': 'pixels', 'tolerance': 50.0, 'separation': 30.0, 'nclip': 3} tweakreg.TweakReg('*_flt.fits', editpars=False, **params) params.update({'outshifts': 'flt_shifts.txt', 'searchrad': 15, 'tolerance': 3}) tweakreg.TweakReg('*_flt.fits', editpars=False, **params)
def findImprovedAlignment(self): """ Tries to find stars to be used for improved alignment. Generates coordinate lists for each POL file and for every _flt file. Maps all the positions to uncorrected/distorted frame using pixtopix transformation. Finally, runs the tweakreg to find improved alignment and updates the WCS in the headers. """ #find some good stars source = sourceFinding(pf.open(self.settings['sourceImage'])[1].data) results = source.runAll() #find improved locations for each star acc = [] for x, y in zip(results['xcms'], results['ycms']): acc.append( iraf.imcntr('POL*_drz.fits[1]', x_init=x, y_init=y, cboxsize=45, Stdout=1)) o = open('tmp.txt', 'w') o.write('#File written on {0:>s}\n'.format( datetime.datetime.isoformat(datetime.datetime.now()))) for line in acc: for l in line: o.write(l.replace('[', '').replace(']', '') + '\n') o.close() data = open('tmp.txt').readlines() pol0 = open('POL0coords.txt', 'w') pol60 = open('POL60coords.txt', 'w') pol120 = open('POL120coords.txt', 'w') pol0r = open('POL0coords.reg', 'w') pol60r = open('POL60coords.reg', 'w') pol120r = open('POL120coords.reg', 'w') for line in data: tmp = line.split(':') x = tmp[1].replace('y', '').strip() y = tmp[2].strip() out = '%s %s\n' % (x, y) reg = 'image;circle(%s,%s,5)\n' % (x, y) if 'POL0' in line: pol0.write(out) pol0r.write(reg) elif 'POL60' in line: pol60.write(out) pol60r.write(reg) elif 'POL120' in line: pol120.write(out) pol120r.write(reg) else: print 'Skipping line:', line pol0.close() pol60.close() pol120.close() pol0r.close() pol60r.close() pol120r.close() data = open('../rawFiles.txt').readlines() pol0 = [ line.split()[0].split('_raw')[0] + '_flt.fits' for line in data if 'POL0' in line.split()[2] ] pol60 = [ line.split()[0].split('_raw')[0] + '_flt.fits' for line in data if 'POL60' in line.split()[2] ] pol120 = [ line.split()[0].split('_raw')[0] + '_flt.fits' for line in data if 'POL120' in line.split()[2] ] for file in pol0: x, y = pixtopix.tran(file + "[1]", 'POL0V_drz.fits[1]', 'backward', coords='POL0coords.txt', output=file.replace('.fits', '') + '.coords', verbose=False) for file in pol60: x, y = pixtopix.tran(file + "[1]", 'POL60V_drz.fits[1]', 'backward', coords='POL60coords.txt', output=file.replace('.fits', '') + '.coords', verbose=False) for file in pol120: x, y = pixtopix.tran(file + "[1]", 'POL120V_drz.fits[1]', 'backward', coords='POL120coords.txt', output=file.replace('.fits', '') + '.coords', verbose=False) del x del y coords = glob.glob('*_flt.coords') #remove comment lines from each coords file and produce a DS9 region file for f in coords: data = open(f).readlines() out = open(f, 'w') reg = open(f.replace('.coords', '.reg'), 'w') reg.write('#File written on {0:>s}\n'.format( datetime.datetime.isoformat(datetime.datetime.now()))) for line in data: if not line.startswith('#'): out.write(line) tmp = line.split() reg.write('image;circle({0:>s},{1:>s},5)\n'.format( tmp[0], tmp[1])) out.close() reg.close() #create a mapping file out = open('regcatalog.txt', 'w') for f in coords: out.write('%s %s\n' % (f.replace('.coords', '.fits'), f)) out.close() params = { 'catfile': 'regcatalog.txt', 'shiftfile': True, 'outshifts': 'flt1_shifts.txt', 'updatehdr': True, 'verbose': False, 'minobj': 15, 'use2dhist': False, 'see2dplot': False, 'searchrad': 50, 'searchunits': 'pixels', 'tolerance': 50.0, 'separation': 30.0, 'nclip': 3 } tweakreg.TweakReg('*_flt.fits', editpars=False, **params) params.update({ 'outshifts': 'flt_shifts.txt', 'searchrad': 15, 'tolerance': 3 }) tweakreg.TweakReg('*_flt.fits', editpars=False, **params)
#printfile1.close() # Open up the final data file. printfile = open("all_centers.txt","w") # Iterate through each post script file name. # Note that Stdout=1 must be chosen in order to redirect the output to a # variable. If choose Stdout="some_file.txt", only the last FITS file's # output will be saved in the output file. # Example of center_string for one image: # [0.699Gyr.fit] x: 300.934 y: 300.964 # Example of final coords data for one image: # 0.699Gyr 301 301 for i in range(0,len(fits_list_final)): center_string = iraf.imcntr(str(fits_list_final[i])+"Gyr.fit",300,300,cboxsize=31, Stdout=1) print(str(center_string)) # This can be commented out. this_file_name = str(center_string[0][1:9]) printfile.write(this_file_name) printfile.write('\t') x_coord = int(round(float(center_string[0][19:26]))) printfile.write(str(x_coord)) printfile.write('\t') y_coord = int(round(float(center_string[0][33:40]))) printfile.write(str(y_coord)) printfile.write('\n') # End of loop. printfile.close() # ----------------- End of code ----------------------
# Open up the final data file. printfile = open("all_centers.txt", "w") # Iterate through each post script file name. # Note that Stdout=1 must be chosen in order to redirect the output to a # variable. If choose Stdout="some_file.txt", only the last FITS file's # output will be saved in the output file. # Example of center_string for one image: # [0.699Gyr.fit] x: 300.934 y: 300.964 # Example of final coords data for one image: # 0.699Gyr 301 301 for i in range(0, len(fits_list_final)): center_string = iraf.imcntr(str(fits_list_final[i]) + "Gyr.fit", 300, 300, cboxsize=31, Stdout=1) print(str(center_string)) # This can be commented out. this_file_name = str(center_string[0][1:9]) printfile.write(this_file_name) printfile.write('\t') x_coord = int(round(float(center_string[0][19:26]))) printfile.write(str(x_coord)) printfile.write('\t') y_coord = int(round(float(center_string[0][33:40]))) printfile.write(str(y_coord)) printfile.write('\n') # End of loop. printfile.close()