def DecideHowToMove2(ParamDict, RunNo): c.ParamDictBook[RunNo - 1] = copy.deepcopy(ParamDict[RunNo]) ContinueLoop = 0 HitLimitCheck = 0 KpCArc = cal(z, c.H0, c.WM, c.WV, c.pixelscale)[3] print 'LM ', c.LMag CntrDev = n.sqrt((ParamDict[RunNo][1][2][0] - ParamDict[RunNo][2][2][0])**2.0 + (ParamDict[RunNo][1][2][1] - ParamDict[RunNo][2][2][1])**2.0) if CntrDev < 1.0: CntrDev = 0.05 # print ParamDict[RunNo][1][2][0], ParamDict[RunNo][2][2][0], ParamDict[RunNo][1][2][1], ParamDict[RunNo][2][2][1], CntrDev # print ParamDict[RunNo][1][3], c.LMag - 1.0,ParamDict[RunNo][1][3], c.UMag, ParamDict[RunNo][1][4], ParamDict[RunNo][2][4], CntrDev if abs(ParamDict[RunNo][1][3] - (c.LMag - 1.0)) < 0.05 or abs(ParamDict[RunNo][1][3] - c.UMag) < 0.05 or ParamDict[RunNo][1][4] < 0.21 or ParamDict[RunNo][2][4] < 0.21 or CntrDev * KpCArc > 0.5 and z != 9999 or CntrDev > 4: HitLimitCheck = 1 # print 'Hit ', HitLimitCheck if ParamDict[RunNo][1][4] > ParamDict[RunNo][2][4] * 1.0 and ParamDict[RunNo][1][3] > ParamDict[RunNo][2][3] or HitLimitCheck or ParamDict[RunNo][1][4] * KpCArc > 40 and z != 9999 or ParamDict[RunNo][2][4] * KpCArc > 40 and z != 9999 or ParamDict[RunNo][1][5] > 8 or ParamDict[RunNo][1][6] < 0.08: print 'sati ' ParamDict[RunNo][1][2][0] = copy.deepcopy(ParamDict[0][1][2][0]) ParamDict[RunNo][1][2][1] = copy.deepcopy(ParamDict[0][1][2][1]) ParamDict[RunNo][1][3] = copy.deepcopy(ParamDict[0][1][3]) ParamDict[RunNo][1][4] = copy.deepcopy(ParamDict[0][1][4]) ParamDict[RunNo][1][5] = copy.deepcopy(ParamDict[0][1][5]) ParamDict[RunNo][1][6] = copy.deepcopy(ParamDict[0][1][6]) ParamDict[RunNo][1][7] = copy.deepcopy(ParamDict[0][1][7]) ParamDict[RunNo][2][2][0] = copy.deepcopy(ParamDict[0][2][2][0]) ParamDict[RunNo][2][2][1] = copy.deepcopy(ParamDict[0][2][2][1]) ParamDict[RunNo][2][3] = copy.deepcopy(ParamDict[0][2][3]) ParamDict[RunNo][2][4] = copy.deepcopy(ParamDict[0][2][4]) ParamDict[RunNo][2][5] = copy.deepcopy(ParamDict[0][2][5]) ParamDict[RunNo][2][6] = copy.deepcopy(ParamDict[0][2][6]) try: SkyNo = len(ParamDict[0]) ParamDict[RunNo][SkyNo][2] = copy.deepcopy(SkyArray[RunNo-1]) except: pass c.FitArr.append(1) c.RadArr.append(1) c.CntrDevArr.append(CntrDev) ContinueLoop = 1 else: print 'not sati' if cal(z, c.H0, c.WM, c.WV, c.pixelscale)[3] * \ ParamDict[RunNo][1][4] > 20.0 and z != 9999: c.RadArr.append(1) else: c.RadArr.append(0) ParamDict[RunNo][1][2][0] = copy.deepcopy(ParamDict[0][1][2][0]) ParamDict[RunNo][1][2][1] = copy.deepcopy(ParamDict[0][1][2][1]) ParamDict[RunNo][1][3] = copy.deepcopy(ParamDict[0][1][3]) ParamDict[RunNo][1][4] = copy.deepcopy(ParamDict[0][1][4]) ParamDict[RunNo][1][5] = copy.deepcopy(ParamDict[0][1][5]) ParamDict[RunNo][1][6] = copy.deepcopy(ParamDict[0][1][6]) ParamDict[RunNo][1][7] = copy.deepcopy(ParamDict[0][1][7]) ParamDict[RunNo][2][2][0] = copy.deepcopy(ParamDict[0][2][2][0]) ParamDict[RunNo][2][2][1] = copy.deepcopy(ParamDict[0][2][2][1]) ParamDict[RunNo][2][3] = copy.deepcopy(ParamDict[0][2][3]) ParamDict[RunNo][2][4] = copy.deepcopy(ParamDict[0][2][4]) ParamDict[RunNo][2][5] = copy.deepcopy(ParamDict[0][2][5]) ParamDict[RunNo][2][6] = copy.deepcopy(ParamDict[0][2][6]) try: SkyNo = len(ParamDict[0]) ParamDict[RunNo][SkyNo][2] = copy.deepcopy(SkyArray[RunNo-1]) except: pass c.FitArr.append(0) c.CntrDevArr.append(CntrDev) ContinueLoop = 0 return ContinueLoop
def confiter(cutimage, whtimage, xcntr, ycntr, NXPTS, NYPTS, line_s, psffile, z): # Run SExtractor RunSex(c.datadir+cutimage, c.datadir+whtimage, 'TEMP.SEX.cat', 9999, 9999, 0) # define improtant variables in the namespace imagefile = c.imagefile sex_cata = c.sex_cata threshold = c.threshold thresh_area = c.thresh_area mask_reg = c.mask_reg c.sersic_loc = 2 c.sky_loc = 1 try: ComP = c.components except: ComP = ['bulge', 'disk'] if len(ComP) == 0: ComP = ['bulge', 'disk'] values = line_s.split() # create filenames outfile = 'O_' + c.fstring + '.fits' # galfit output mask_file = 'M_' + c.fstring + '.fits' # mask file config_file = 'G_' + c.fstring + '.in' #GALFIT configuration file constrain_file = c.fstring + '.con' #galfit constraint file xcntr_o = xcntr #x center of the object ycntr_o = ycntr #y center of the object mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius mag_zero = c.mag_zero #magnitude zero point sky = float(values[10]) #sky pos_ang = float(values[11]) - 90.0 #position angle axis_rat = 1.0/float(values[12]) #axis ration b/a area_o = float(values[13]) # object's area major_axis = float(values[14]) #major axis of the object # calculate a 40kpc bulge radius to use for maximum bulge size. # This will only work if z is supplied, otherwise, a default maximum # is used in the constraint file if z > 0 and z < 10: try: KpCArc = cal(z, c.H0, c.WM, c.WV, c.pixelscale)[3] max_rad = 40.0/KpCArc except: KpCArc = 9999.0 max_rad = c.URe else: max_rad = c.URe KpCArc = 9999.0 # Modify flags for the detailed fitting process if isset(c.Flag, GetFlag('FIT_BULGE_CNTR')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_BULGE_CNTR')) if isset(c.Flag, GetFlag('FIT_DISK_CNTR')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_DISK_CNTR')) if isset(c.Flag, GetFlag('FIT_SKY')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_SKY')) if isset(c.Flag, GetFlag('FIT_BULGE')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_BULGE')) if isset(c.Flag, GetFlag('FIT_DISK')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_DISK')) if isset(c.Flag, GetFlag('FIT_POINT')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_POINT')) # define initial fitting parameters for single sersic fit ParamDict = {} ErrDict = {} ParamDict[0] = {} ErrDict[0] = {} BlankDict = {} # to be used in cases where the program crashes BlankDict[0] = {} # Add components AdComp = 1 if 'bulge' in ComP: ParamDict[0][AdComp] = {} #Bulge Parameters ParamDict[0][AdComp][1] = 'sersic' ParamDict[0][AdComp][2] = [xcntr_o, ycntr_o] ParamDict[0][AdComp][3] = mag ParamDict[0][AdComp][4] = radius ParamDict[0][AdComp][5] = 4.0 ParamDict[0][AdComp][6] = axis_rat ParamDict[0][AdComp][7] = pos_ang ParamDict[0][AdComp][8] = 0 ParamDict[0][AdComp][9] = 0 ParamDict[0][AdComp][11] = 'Main' BlankDict[0][AdComp] = {} #Bulge Parameters BlankDict[0][AdComp][1] = 'sersic' BlankDict[0][AdComp][2] = [xcntr_o, ycntr_o] BlankDict[0][AdComp][3] = 9999 BlankDict[0][AdComp][4] = 9999 BlankDict[0][AdComp][5] = 9999 BlankDict[0][AdComp][6] = 9999 BlankDict[0][AdComp][7] = 9999 BlankDict[0][AdComp][8] = 9999 BlankDict[0][AdComp][9] = 9999 BlankDict[0][AdComp][11] = 'Main' AdComp += 1 if 'disk' in ComP: #Disk parameters ParamDict[0][AdComp] = {} ParamDict[0][AdComp][1] = 'expdisk' ParamDict[0][AdComp][2] = [xcntr_o, ycntr_o] ParamDict[0][AdComp][3] = mag ParamDict[0][AdComp][4] = radius ParamDict[0][AdComp][5] = axis_rat ParamDict[0][AdComp][6] = pos_ang ParamDict[0][AdComp][7] = 0 ParamDict[0][AdComp][8] = 0 ParamDict[0][AdComp][11] = 'Main' BlankDict[0][AdComp] = {} BlankDict[0][AdComp][1] = 'expdisk' BlankDict[0][AdComp][2] = [xcntr_o, ycntr_o] BlankDict[0][AdComp][3] = 9999#mag BlankDict[0][AdComp][4] = 9999#radius BlankDict[0][AdComp][5] = 9999#axis_rat BlankDict[0][AdComp][6] = 9999#pos_ang BlankDict[0][AdComp][7] = 9999 BlankDict[0][AdComp][8] = 9999 BlankDict[0][AdComp][11] = 'Main' AdComp += 1 isneighbour = 0 f_constrain = open(constrain_file, 'ab') for line_j in open('TEMP.SEX.cat','r'): try: values = line_j.split() xcntr_n = float(values[1]) #x center of the neighbour ycntr_n = float(values[2]) #y center of the neighbour mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius sky = float(values[10]) #sky pos_ang = float(values[11]) - 90.0 #position angle axis_rat = 1.0/float(values[12]) #axis ration b/a area_n = float(values[13]) # neighbour area maj_axis = float(values[14])#major axis of neighbour NotFitNeigh = 0 if abs(xcntr_n - xcntr_o) > NXPTS / 2.0 + c.avoidme or \ abs(ycntr_n - ycntr_o) > NYPTS / 2.0 + c.avoidme or \ abs(xcntr_n - xcntr_o) < 5.0 and abs(ycntr_n - ycntr_o) < 5.0: NotFitNeigh = 1 if(abs(xcntr_n - xcntr_o) <= (major_axis + maj_axis) * \ threshold and \ abs(ycntr_n - ycntr_o) <= (major_axis + maj_axis) * \ threshold and area_n >= thresh_area * area_o and \ xcntr_n != xcntr_o and ycntr_n != ycntr_o and NotFitNeigh == 0): if((xcntr_o - xcntr_n) < 0): xn = xcntr + abs(xcntr_n - xcntr_o) if((ycntr_o - ycntr_n) < 0): yn = ycntr + abs(ycntr_n - ycntr_o) if((xcntr_o - xcntr_n) > 0): xn = xcntr - (xcntr_o - xcntr_n) if((ycntr_o - ycntr_n) > 0): yn = ycntr - (ycntr_o - ycntr_n) ParamDict[0][AdComp] = {} ParamDict[0][AdComp][1] = 'sersic' ParamDict[0][AdComp][2] = [xn, yn] ParamDict[0][AdComp][3] = mag ParamDict[0][AdComp][4] = radius ParamDict[0][AdComp][5] = 4.0 ParamDict[0][AdComp][6] = axis_rat ParamDict[0][AdComp][7] = pos_ang ParamDict[0][AdComp][8] = 0 ParamDict[0][AdComp][9] = 0 ParamDict[0][AdComp][11] = 'Other' BlankDict[0][AdComp] = {} BlankDict[0][AdComp][1] = 'sersic' BlankDict[0][AdComp][2] = [xn, yn] BlankDict[0][AdComp][3] = mag BlankDict[0][AdComp][4] = radius BlankDict[0][AdComp][5] = 4.0 BlankDict[0][AdComp][6] = axis_rat BlankDict[0][AdComp][7] = pos_ang BlankDict[0][AdComp][8] = 0 BlankDict[0][AdComp][9] = 0 BlankDict[0][AdComp][11] = 'Other' isneighbour += 1 AdComp += 1 except: pass if isneighbor > 0: c.Flag = SetFlag(c.Flag,GetFlag('NEIGHBOUR_FIT')) f_constrain.close() #Sky component ParamDict[0][AdComp] = {} ParamDict[0][AdComp][1] = 'sky' ParamDict[0][AdComp][2] = c.SexSky ParamDict[0][AdComp][3] = 0 ParamDict[0][AdComp][4] = 0 ParamDict[0][AdComp][5] = 0 ParamDict[0][AdComp][11] = 'Other' BlankDict[0][AdComp] = {} BlankDict[0][AdComp][1] = 'sky' BlankDict[0][AdComp][2] = c.SexSky BlankDict[0][AdComp][3] = 0 BlankDict[0][AdComp][4] = 0 BlankDict[0][AdComp][5] = 0 BlankDict[0][AdComp][11] = 'Other' #Write Sersic function ErrDict[0][1] = {} ErrDict[0][1][1] = [9999, 9999] ErrDict[0][1][2] = 9999 ErrDict[0][1][3] = 9999 ErrDict[0][1][4] = 9999 ErrDict[0][2] = {} ErrDict[0][2][1] = [9999, 9999] ErrDict[0][2][2] = 9999 ErrDict[0][2][3] = 9999 c.SkyNo = AdComp c.Chi2DOFArr = [] c.FitArr = [] c.RadArr = [] c.CntrDevArr = [] c.ErrArr = [] c.ParamDictBook = copy.deepcopy(ParamDict) bad_fit = 0 failed_ser = 0 bt_fit = -1 failed_sky = 0 bad_sky = 0 #Write configuration file. RunNo is the number of iteration for RunNo, fit_type in zip(range(5), ['sky','ser','dev','devexp','serexp']): # note that the 'sky' fit is currently serexp #force batch fitting using standard operations run_flag = c.Flag if exists('fit.log'): print "removing fit log" os.system('rm fit.log') print "RunNo ", RunNo, "fit type ", fit_type f_constrain = open(constrain_file, 'w') f_constrain.close() f=open(config_file,'w') f.write('# IMAGE PARAMETERS\n') f.writelines(['A) ', c.datadir+str(cutimage), ' # Input data image',\ ' (FITS file)\n']) f.writelines(['B) ', str(outfile), ' # Name for',\ ' the output image\n']) f.writelines(['C) ', c.datadir + str(whtimage), ' # Noise image name', \ ' (made from data if blank or "none")\n']) f.writelines(['D) ', c.datadir+str(psffile), ' # Input PSF', \ ' image for convolution (FITS file)\n']) f.writelines(['E) 1 # PSF oversampling factor '\ 'relative to data\n']) f.writelines(['F) ', str(mask_file), ' # Bad pixel', ' mask(FITS image or ASCII coord list)\n']) f.writelines(['G) ', str(constrain_file), ' # File with'\ ' parameter constraints (ASCII file)\n']) f.writelines(['H) 1 ', str(NXPTS), ' 1 ', str(NYPTS), ' #',\ ' Image region to fit (xmin xmax ymin ymax)\n']) # f.writelines(['I) ', str(NXPTS), ' ', str(NYPTS), ' #',\ # ' Size of convolution box (x y)\n']) f.writelines(['I) ', str(120), ' ', str(120), ' #',\ ' Size of convolution box (x y)\n']) f.writelines(['J) ', str(mag_zero), ' # Magnitude',\ ' photometric zeropoint\n']) f.writelines(['O) regular # Display type',\ ' (regular, curses, both)\n']) f.writelines(['P) 0 # Create output image only?',\ ' (1=yes; 0=optimize)\n']) f.writelines(['S) 0 # Modify/create',\ ' objects interactively?\n\n\n']) f.close() FitDict, run_flag = DecideFitting(ParamDict, RunNo,fit_type, bad_fit, failed_ser, run_flag, failed_sky, bad_sky) for i in range(len(ParamDict[RunNo])): if ParamDict[RunNo][i + 1][1] == 'sersic': SersicFunc(config_file, ParamDict, FitDict, i+1, RunNo) if ParamDict[RunNo][i + 1][11] == 'Main': run_flag = SetFlag(run_flag, GetFlag('FIT_BULGE')) print "ADDING FLAG FIT_BULGE" if RunNo > 1 and not bad_fit and ParamDict[c.sersic_loc][1][4]*2 < max_rad: # later fits SersicMainConstrain(constrain_file, i + 1, 2.0 , ParamDict[c.sersic_loc][1][4]*2) else: SersicMainConstrain(constrain_file, i + 1, c.center_constrain, max_rad) else: SersicConstrain(constrain_file, i + 1) if ParamDict[RunNo][i + 1][1] == 'expdisk': if RunNo in [1 ,2]: pass #dont fit a disk in the cases of ser or dev fits else: run_flag = SetFlag(run_flag, GetFlag('FIT_DISK')) print "ADDING FLAG FIT_DISK" ExpFunc(config_file, ParamDict, FitDict, i + 1, RunNo) if RunNo > 0 and not bad_fit: # later runs ExpdiskConstrain(constrain_file, i + 1, 2.0) else: ExpdiskConstrain(constrain_file, i + 1, c.center_constrain) if ParamDict[RunNo][i + 1][1] == 'sky': if RunNo > 0 and not bad_sky: SkyConstrain(constrain_file, i + 1, ParamDict[c.sky_loc][i+1][2]) else: SkyConstrain(constrain_file, i + 1, c.SexSky) SkyFunc(config_file, ParamDict, FitDict, i+1, RunNo) # only if forcing normal fit if RunNo > 2 or RunNo == 0: #later fits after single ser fit bt_range = add_constrain(constrain_file, bt_fit, fit_type) cmd = str(c.GALFIT_PATH) + ' ' + config_file os.system(cmd) # ReadLog(ParamDict, 2) => this function reads fig.log # depends on whether, for example, the expdisk function # is the only one function for fitting. It can happends, # for example, the first fitting where we can find the best # initial values for the rest of it. ReadLog(ParamDict, 1) # reads the fit.log in the order, ie. sersic, # expdisk, other sersic etc try: print 'readlog' ParamDict, ErrDict, Chi2DOF = ReadLog(ParamDict, ErrDict, 1, RunNo, detail = True) print 'log read' print 'chi2dof ',Chi2DOF c.ParamDictBook[RunNo+1] = copy.deepcopy(ParamDict[RunNo+1]) # Set limit flags Fitflag = 0 mag_b = ParamDict[RunNo + 1][1][3] re = ParamDict[RunNo+1][1][4] SersicIndex = ParamDict[RunNo+1][1][5] SersicEllipticity = ParamDict[RunNo+1][1][6] if abs(mag_b - c.UMag) < 0.2 or abs(mag_b - c.LMag) < 0.2: FitFlag = SetFlag(FitFlag, Get_FitFlag('IE_AT_LIMIT')) if abs(re - c.LRe) < 0.1 or abs(re - max_rad) < 1.0: FitFlag = SetFlag(FitFlag, Get_FitFlag('RE_AT_LIMIT')) if abs(SersicIndex - c.LN) < 0.03 or abs(SersicIndex - c.UN) < 0.5: FitFlag = SetFlag(FitFlag, Get_FitFlag('N_AT_LIMIT')) if abs(SersicEllipticity - 0.0) < 0.05 or abs(SersicEllipticity - 0.0) > 0.95: FitFlag = SetFlag(FitFlag, Get_FitFlag('EB_AT_LIMIT')) if fit_type in ['devexp', 'serexp']: mag_d = ParamDict[RunNo + 1][2][3] rd = ParamDict[RunNo+1][2][4] DiskEllipticity = ParamDict[RunNo+1][2][5] fb = 10**(-0.4*mag_b) fd = 10**(-0.4*mag_d) if abs(mag_d - c.UMag) < 0.2 or abs(mag_d - c.LMag) < 0.2: FitFlag = SetFlag(FitFlag, Get_FitFlag('ID_AT_LIMIT')) if abs(rd - c.LRd) < 0.1 or abs(rd - c.URd) < 1.0: FitFlag = SetFlag(FitFlag, Get_FitFlag('RD_AT_LIMIT')) if abs((re/rd) - 1.0) < 0.02 or abs((re/rd) - 0.1) < 0.02: FitFlag = SetFlag(FitFlag, Get_FitFlag('RERD_AT_LIMIT')) if abs(DiskEllipticity - 0.0) < 0.05 or abs(DiskEllipticity - 0.0) > 0.95: FitFlag = SetFlag(FitFlag, Get_FitFlag('ED_AT_LIMIT')) try: BT = fb / (fb + fd) except: BT = 9999.0 if abs(BT - bt_range[0]) < .02 or abs(BT - bt_range[1]) < .02: FitFlag = SetFlag(FitFlag, Get_FitFlag('BT_AT_LIMIT')) print "ADDING FLAG BT_AT_LIMIT" except: print "failure at readlog!!!" c.ParamDictBook[RunNo+1] = copy.deepcopy(BlankDict[0]) ParamDict[RunNo + 1] = copy.deepcopy(BlankDict[0]) ErrDict[RunNo + 1] = copy.deepcopy(ErrDict[0]) if fit_type == 'ser': failed_ser = 1 #track the failure of the single sersic fit run_flag = SetFlag(run_flag, GetFlag('DETAIL_FAILED')) c.Flag = SetFlag(c.Flag, GetFlag('DETAIL_FAILED')) print "ADDING FLAG DETAIL_FAILED" elif fit_type == 'sky': failed_sky = 1 #track that we did not fix the sky run_flag = SetFlag(run_flag, GetFlag('DETAIL_FAILED')) c.Flag = SetFlag(c.Flag, GetFlag('DETAIL_FAILED')) print "ADDING FLAG DETAIL_FAILED" try: c.ErrArr.append(FractionalError(ParamDict, ErrDict, RunNo + 1)) except: c.ErrArr.append(9999.0) c.Chi2DOFArr.append(Chi2DOF) if fit_type != 'serexp': #set flags for other fit types bad_sky, bad_fit, bt_fit = DecideHowToMove2(ParamDict, RunNo + 1,fit_type,KpCArc, failed_ser, failed_sky ) try: plot_name = 'P_' + str(cutimage)[6:-5] \ +'_'+fit_type+ '.png' if exists(plot_name): os.system('rm ' + plot_name) GoodNess = PlotFunc(cutimage, outfile, mask_file, ParamDict[RunNo + 1][1][2][0], #xctr ParamDict[RunNo + 1][1][2][1], #yctr ParamDict[RunNo + 1][1][c.SkyNo][2], #sky c.skysig, save_name = plot_name) Goodness = GoodNess.plot_profile except: Goodness = 9999 run_flag = SetFlag(run_flag, GetFlag('PLOT_FAIL')) print "ADDING FLAG PLOT_FAIL" if Goodness < c.Goodness: FitFlag = SetFlag(FitFlag, Get_FitFlag('SMALL_GOODNESS')) print "ADDING FLAG SMALL_GOODNESS" if Chi2DOF > c.chi2sq: if chi2nu != 9999: FitFlag = SetFlag(FitFlag, Get_FitFlag('LARGE_CHISQ')) print "ADDING FLAG LARGE_CHISQ" print 'writing db' try: WriteDbDetail(cutimage.split('.')[0], c.ParamDictBook[RunNo+1], ErrDict[RunNo + 1], c.SexSky, c.ParamDictBook[RunNo+1][c.SkyNo][2], RunNo, run_flag, FitFlag, Chi2DOF, model_type = fit_type, goodness = Goodness) except: print 'No database' traceback.print_exc() time.sleep(10) fit_log = 'fit_%s.log' %fit_type print "saving fit log to fit log %s" %(fit_log) f_fit = open(fit_log,'a') if exists('fit.log'): for line in open('fit.log','r'): f_fit.writelines([str(line)]) f_fit.close() for mv_file_nm in [outfile, config_file, constrain_file]: os.system('cp %s %s_%s.%s' %(mv_file_nm, mv_file_nm.split('.')[0], fit_type, mv_file_nm.split('.')[1])) if fit_type == 'serexp': c.Flag = run_flag return
def WriteParams(ParamNamesToWrite, cutimage, xcntr, ycntr, distance, alpha_j, delta_j, z, Goodness, C, C_err, A, A_err, S, S_err, G, M, EXPTIME): # this dictionary will hold any parameters that may be printed all_params = dict((ParamNamesToWrite[key][0], -999) for key in ParamNamesToWrite.keys()) # load some of the passed in parameters all_params['Name'] = c.fstring all_params['ra_gal'] = alpha_j all_params['dec_gal'] = delta_j all_params['z'] = z all_params['Goodness'] = float(str(round(Goodness, 3))[:5]) all_params['C'] = C all_params['C_err'] = C_err all_params['A'] = A all_params['A_err'] = A_err all_params['S'] = S all_params['S_err'] = S_err all_params['G'] = G all_params['M'] = M all_params['distance'] = float(str(round(distance, 3))[:5]) all_params['mag_auto'] = c.SexMagAuto all_params['magerr_auto'] = c.SexMagAutoErr all_params['num_targets'] = c.SexTargets all_params['SexSky'] = c.SexSky all_params['flag'] = c.Flag all_params['SexHalfRad'] = c.SexHalfRad all_params['magzp'] = c.mag_zero # Now continue try: ComP = c.components except: ComP = ['bulge', 'disk'] if len(ComP) == 0: ComP = ['bulge', 'disk'] f_tpl = open(str(c.PYMORPH_PATH) + '/html/default.html', 'r') template = f_tpl.read() f_tpl.close() ra1, ra2, ra3 = RaDegToHMS(alpha_j) dec1, dec2, dec3 = DecDegToDMS(delta_j) # Formatting ra and dec for display purpose if ra1 < 10: alpha1 = '0' + str(ra1) else: alpha1 = str(ra1) if ra2 < 10: alpha2 = '0' + str(ra2) else: alpha2 = str(ra2) if ra3 < 10: alpha3 = '0' + str(ra3)[:3] else: alpha3 = str(ra3)[:4] if abs(ra1) > 360: alpha1, alpha2, alpha3 = 9999, '', '' if abs(dec1) < 10: delta1 = '0' + str(dec1) else: delta1 = str(dec1) if dec1 > 0: delta1 = '+' + str(delta1) else: pass if dec2 < 10: delta2 = '0' + str(dec2) else: delta2 = dec2 if dec3 < 10: delta3 = '0' + str(dec3)[:3] else: delta3 = str(dec3)[:4] if abs(dec1) > 90: delta1, delta2, delta3 = 9999, '', '' # Writing index file if(c.repeat == False or c.repeat): for line_i in fileinput.input("index.html",inplace =1): line_i = line_i.strip() if not '</BODY></HTML>' in line_i: print line_i indexfile = open('index.html', 'a+') NoImage = 1 for indexline in indexfile: if c.fstring in indexline: NoImage = 0 else: pass if NoImage: indexfile.writelines(['<a href="R_',\ c.fstring,'.html',\ '"> ', c.fstring,\ ' </a> <br>\n']) indexfile.writelines(['</BODY></HTML>\n']) indexfile.close() # Reading fit.log if 'bar' in ComP: basic_info, fit_info, isnan = read_fitlog(filename = 'fit.log', yes_bar = 1) else: basic_info, fit_info, isnan = read_fitlog(filename = 'fit.log', yes_bar = 0) if isnan: try: #set the flag all_params['flag'] = SetFlag(all_params['flag'], GetFlag('ERRORS_FAILED')) except badflag: # the flag is already set pass if 'Input' in basic_info: alpha_ned = str(alpha_j)[:10] delta_ned = str(delta_j)[:10] else: alpha_ned = '' delta_ned = '' if c.decompose: initial_conf = basic_info['initial_conf'] restart_conf = basic_info['restart_conf'] print restart_conf # move the restart file to a reasonably named output file new_outname = initial_conf.replace('in','out') try: os.rename(restart_conf, new_outname) except: print "Failed to find restart file!! Galfit may have crashed!!" basic_info['restart_conf'] = new_outname all_params['chi2nu'] = basic_info['chi2nu'] # first check all err components and replace if nan or inf if 'bulge' in fit_info: all_params['bulge_xctr'] = fit_info['bulge']['xctr'][0] all_params['bulge_yctr'] = fit_info['bulge']['yctr'][0] all_params['Ie'] = fit_info['bulge']['mag'][0] all_params['re_pix'] = fit_info['bulge']['rad'][0] all_params['n'] = fit_info['bulge']['n'][0] all_params['eb'] = fit_info['bulge']['ell'][0] all_params['bpa'] = fit_info['bulge']['pa'][0] all_params['bboxy'] = fit_info['bulge']['boxy'][0] all_params['bulge_xctr_err'] = fit_info['bulge']['xctr'][1] all_params['bulge_yctr_err'] = fit_info['bulge']['yctr'][1] all_params['Ie_err'] = fit_info['bulge']['mag'][1] all_params['re_pix_err'] = fit_info['bulge']['rad'][1] all_params['n_err'] = fit_info['bulge']['n'][1] all_params['eb_err'] = fit_info['bulge']['ell'][1] all_params['bpa_err'] = fit_info['bulge']['pa'][1] all_params['bboxy_err'] = fit_info['bulge']['boxy'][1] if 'disk' in fit_info: all_params['disk_xctr'] = fit_info['disk']['xctr'][0] all_params['disk_yctr'] = fit_info['disk']['yctr'][0] all_params['Id'] = fit_info['disk']['mag'][0] all_params['rd_pix'] = fit_info['disk']['rad'][0] all_params['ed'] = fit_info['disk']['ell'][0] all_params['dpa'] = fit_info['disk']['pa'][0] all_params['dboxy'] = fit_info['disk']['boxy'][0] all_params['disk_xctr_err'] = fit_info['disk']['xctr'][1] all_params['disk_yctr_err'] = fit_info['disk']['yctr'][1] all_params['Id_err'] = fit_info['disk']['mag'][1] all_params['rd_pix_err'] = fit_info['disk']['rad'][1] all_params['ed_err'] = fit_info['disk']['ell'][1] all_params['dpa_err'] = fit_info['disk']['pa'][1] all_params['dboxy_err'] = fit_info['disk']['boxy'][1] if 'point' in fit_info: all_params['p_xctr'] = fit_info['point']['xctr'][0] all_params['p_yctr'] = fit_info['point']['yctr'][0] all_params['Ip'] = fit_info['point']['mag'][0] all_params['p_xctr_err'] = fit_info['point']['xctr'][1] all_params['p_yctr_err'] = fit_info['point']['yctr'][1] all_params['Ip_err'] = fit_info['point']['mag'][1] if 'bar' in fit_info: all_params['bar_xctr'] = fit_info['bar']['xctr'][0] all_params['bar_yctr'] = fit_info['bar']['yctr'][0] all_params['Ibar'] = fit_info['bar']['mag'][0] all_params['rbar_pix'] = fit_info['bar']['rad'][0] all_params['n_bar'] = fit_info['bar']['n'][0] all_params['ebar'] = fit_info['bar']['ell'][0] all_params['barpa'] = fit_info['bar']['pa'][0] all_params['barboxy'] = fit_info['bar']['boxy'][0] all_params['bar_xctr_err'] = fit_info['bar']['xctr'][1] all_params['bar_yctr_err'] = fit_info['bar']['yctr'][1] all_params['Ibar_err'] = fit_info['bar']['mag'][1] all_params['rbar_pix_err'] = fit_info['bar']['rad'][1] all_params['nbar_err'] = fit_info['bar']['n'][1] all_params['ebar_err'] = fit_info['bar']['ell'][1] all_params['barpa_err'] = fit_info['bar']['pa'][1] all_params['barboxy_err'] = fit_info['bar']['boxy'][1] if 'sky' in fit_info: all_params['GalSky'] = fit_info['sky']['mag'][0] all_params['GalSky_err'] = fit_info['sky']['mag'][1] # Converting fitted params to physical params if(z != 9999 and z > 0): phy_parms = cal(z, c.H0, c.WM, c.WV, c.pixelscale) all_params['dis_modu'] = phy_parms[2] if 'bulge' in ComP: if all_params['re_pix'] != 9999: all_params['re_kpc'] = phy_parms[3] * all_params['re_pix'] all_params['re_kpc_err'] = phy_parms[3] * all_params['re_pix_err'] else: all_params['re_kpc'] = 9999 all_params['re_kpc_err'] = 9999 if 'disk' in ComP: if all_params['rd_pix'] != 9999: all_params['rd_kpc'] = phy_parms[3] * all_params['rd_pix'] all_params['rd_kpc_err'] = phy_parms[3] * all_params['rd_pix_err'] else: all_params['rd_kpc'] = 9999 all_params['rd_kpc_err'] = 9999 if 'bar' in ComP: if all_params['rbar_pix'] != 9999: all_params['rbar_kpc'] = phy_parms[3] * all_params['rbar_pix'] all_params['rbar_kpc_err'] = phy_parms[3] * all_params['rbar_pix_err'] else: all_params['rbar_kpc'] = 9999 all_params['rbar_kpc_err'] = 9999 if 'point' in ComP: all_params['Pfwhm_kpc'] = 0.5 * phy_parms[3] # Finding derived parameters if 'bulge' in ComP and 'disk' in ComP: fb = 10**(-0.4 * (all_params['Ie'] - c.mag_zero)) fd = 10**(-0.4 * (all_params['Id'] - c.mag_zero)) if 'point' in ComP: fp = 10**(-0.4 * (all_params['Ip'] - c.mag_zero)) else: fp = 0.0 if 'bar' in ComP: fbar = 10**(-0.4 * (all_params['Ibar'] - c.mag_zero)) else: fbar = 0.0 try: all_params['BD'] = fb / fd all_params['BT'] = fb / (fb + fd + fp + fbar) except: all_params['BD'] = 9999 all_params['BT'] = 9999 elif 'bulge' in ComP: all_params['BT'] = 1.0 elif 'disk' in ComP: all_params['BD'] = 0.0 all_params['BT'] = 0.0 # Start writing html file. Now the template keywords will get values pngfile = 'P_' + c.fstring + '.png' Neighbour_Sersic = '' Object_Sersic = '' Object_Sersic_err = '' Object_Exp = '' Object_Exp_err = '' Point_Vals = '' Point_Vals_err = '' try: for key in fit_info.keys(): if 'neighbor' in key: Neighbour_Sersic = str(Neighbour_Sersic) + \ '<TR align="center" bgcolor=' + \ '"#99CCFF"><TD> neighbor sersic' + \ ' </TD> <TD> ' + \ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][0]) + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + \ str(fit_info[key]['n'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][0]) + ' </TD> <TD> ' + \ str(fit_info[key]['boxy'][0]) + ' </TD> </TR>' Neighbour_Sersic = str(Neighbour_Sersic) + \ '<TR align="center" ' + \ 'bgcolor="#CCFFFF"> <TD>' + ' ' + \ ' </TD> <TD> ' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][1]) + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + \ str(fit_info[key]['n'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][1]) + ' </TD> <TD> ' + \ str(fit_info[key]['boxy'][1]) + ' </TD> </TR>' if 'bulge' in key: Object_Sersic = '<TR align="center" ' +\ 'bgcolor="#99CCFF">' +\ '<TD> sersic bulge</TD> <TD> ' +\ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][0]) + ' </TD> <TD> ' + \ str(round(all_params['re_kpc'], 3))[:5] + ' </TD> <TD> ' + \ str(fit_info[key]['n'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][0]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' Object_Sersic_err = '<TR align="center" ' + \ 'bgcolor="#CCFFFF">' + \ '<TD>' + ' ' + '</TD> <TD>' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][1]) + ' </TD> <TD> ' + \ str(round(all_params['re_kpc_err'], 3))[:5] + ' </TD> <TD> ' + \ str(fit_info[key]['n'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][1]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' if 'disk' in key: Object_Exp = '<TR align="center" bgcolor="#99CCFF">' +\ '<TD> disk </TD> <TD> ' + \ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][0]) + ' </TD> <TD> ' + \ str(round(all_params['rd_kpc'], 3))[:5] +\ ' </TD> <TD> </TD> <TD> ' +\ str(fit_info[key]['ell'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][0]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' Object_Exp_err = '<TR align="center" ' + \ 'bgcolor="#CCFFFF">' + \ '<TD>' + ' ' + '</TD> <TD>' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][1]) + ' </TD> <TD> ' + \ str(round(all_params['rd_kpc_err'], 3))[:5] + \ ' </TD> <TD> </TD> <TD> ' +\ str(fit_info[key]['ell'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][1]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' if 'point' in key: Point_Vals = '<TR align="center" bgcolor="#99CCFF">' + \ '<TD> point </TD> <TD> ' + \ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) +\ ' </TD> <TD> ' + str('9999') + ' </TD> <TD> ' + \ str('9999') + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + ' </TD></TR>' Point_Vals_err = '<TR align="center" ' + \ 'bgcolor="#CCFFFF">' + \ '<TD>' + ' ' + '</TD> <TD>' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) +\ ' </TD> <TD> ' + str('9999') + ' </TD> <TD> ' + \ str('9999') + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + ' </TD></TR>' except Exception, inst: print type(inst) # the exception instance print inst.args # arguments stored in\ # .args print inst # __str__ allows args\ # to printed directly print "something bad happened (writefunc writing)!!!!\n\n" print traceback.print_exc() if 'bulge' in ComP: try: pixelscale = c.pixelscale except: pixelscale = 1 try: all_params['AvgIe'] = all_params['Ie'] + 2.5 * n.log10(2 * 3.14 * pixelscale * \ pixelscale * all_params['re_pix'] * all_params['re_pix'] * n.sqrt(1 - all_params['eb']**2.0)) AvgMagInsideReErr2 = (1.085 * n.sqrt((2 * all_params['re_pix'] * all_params['re_pix_err'])**2.0 + \ (( all_params['eb'] * all_params['eb_err']) / \ n.sqrt(1 - all_params['eb']**2.0))**2.0)) / \ (n.sqrt(1 - all_params['eb']**2.0) * 2 * 3.14 * \ all_params['re_pix'] * all_params['re_pix']) all_params['AvgIe_err'] = n.sqrt( all_params['Ie_err']**2.0 + AvgMagInsideReErr2**2.0) except OverflowError: all_params['AvgIe'] = n.inf all_params['AvgIe_err'] = n.inf for key in ['AvgIe', 'AvgIe_err']: if n.isnan(all_params[key]) or n.isinf(all_params[key]): if n.isnan(all_params[key]): all_params[key] = -9999.99 else: all_params[key] = -6666.66 try: #set the AVGIe flag all_params['flag'] = SetFlag(all_params['flag'], GetFlag('AVGIE_FAILED')) except badflag: # the flag is already set pass wC = str(C)[:5] wA = str(A)[:5] wS = str(S)[:5] wG = str(G)[:5] wM = str(M)[:5] wBD = str( all_params['BD'])[:5] wBT = str( all_params['BT'])[:5] wAvgMagInsideRe = str( all_params['AvgIe'])[:5] error_mesg1 = '' error_mesg2 = '' error_mesg3 = '' error_mesg4 = '' error_mesg5 = '' error_mesg6 = '' error_mesg7 = '' if c.starthandle: error_mesg6 = '<a href="R_' + c.fstring + \ '_1.html"> Crashed </a>' # Now test for fitting problems and set flags for analysis all_params['FitFlag'] = 0 HitLimit = 0 if not c.detail: if 'bulge' in ComP: if abs(all_params['Ie'] - c.UMag) < 0.2 or abs(all_params['Ie'] - c.LMag) < 0.2: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('IE_AT_LIMIT')) if abs(all_params['re_pix'] - c.LRe) < 0.1 or abs(all_params['re_pix'] - c.URe) < 1.0: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('RE_AT_LIMIT')) if abs(all_params['n'] - c.LN) < 0.03 or abs(all_params['n'] - c.UN) < 0.5: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('N_AT_LIMIT')) if abs(all_params['eb'] - 0.0) < 0.05 or abs(all_params['eb'] - 0.0) > 0.95: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('EB_AT_LIMIT')) if 'disk' in ComP: if abs(all_params['Id'] - c.UMag) < 0.2 or abs(all_params['Id'] - c.LMag) < 0.2: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('ID_AT_LIMIT')) if abs(all_params['rd_pix'] - c.LRd) < 0.1 or abs(all_params['rd_pix'] - c.URd) < 1.0: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('RD_AT_LIMIT')) if abs(all_params['ed'] - 0.0) < 0.05 or abs(all_params['ed'] - 0.0) > 0.95: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('ED_AT_LIMIT')) if not all_params['FitFlag']: error_mesg4 = str(error_mesg4) + 'One of the parameters' error_mesg5 = str(error_mesg5) + ' hits limit!' if all_params['Goodness'] < c.Goodness: error_mesg2 = str(error_mesg2) + 'Goodness is poor!' all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('SMALL_GOODNESS')) if all_params['chi2nu'] > c.chi2sq: error_mesg1 = str(error_mesg1) + 'Chi2nu is large!' if all_params['chi2nu'] != 9999: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('LARGE_CHISQ')) if abs(all_params['bulge_xctr'] - xcntr) > c.center_deviation or \ abs(all_params['bulge_yctr'] - ycntr) > c.center_deviation or \ abs(all_params['disk_xctr'] - xcntr) > c.center_deviation or \ abs(all_params['disk_yctr'] - ycntr) > c.center_deviation: if all_params['bulge_xctr'] == -999 or all_params['bulge_yctr'] == -999 or \ all_params['disk_xctr'] == -999 or all_params['disk_yctr'] == -999: pass else: all_params['FitFlag'] = SetFlag(all_params['FitFlag'],Get_FitFlag('FAKE_CNTR')) error_mesg3 = str(error_mesg3) + 'Fake Center!' if all_params['FitFlag'] > 0: img_notify = str(c.PYMORPH_PATH) + '/html/goodfit.gif' good_fit = 1 else: img_notify = str(c.PYMORPH_PATH) + '/html/badfit.gif' good_fit = 0 chi2nu = all_params['chi2nu'] Distance = all_params['distance'] # This can be a waste of time if the list is wrong... # Finding number of runs in the csv file all_params['run'] = 1 if exists('result.csv'): for line_res in csv.reader(open('result.csv').readlines()[1:]): if(str(line_res[0]) == c.fstring): all_params['run'] += 1 if c.GalSky != 9999: all_params['GalSky'] = c.GalSky
def WriteParams(ParamNamesToWrite, cutimage, xcntr, ycntr, distance, alpha_j, delta_j, z, Goodness, C, C_err, A, A_err, S, S_err, G, M, EXPTIME): # this dictionary will hold any parameters that may be printed all_params = dict( (ParamNamesToWrite[key][0], -999) for key in ParamNamesToWrite.keys()) # load some of the passed in parameters all_params['Name'] = c.fstring all_params['ra_gal'] = alpha_j all_params['dec_gal'] = delta_j all_params['z'] = z all_params['Goodness'] = float(str(round(Goodness, 3))[:5]) all_params['C'] = C all_params['C_err'] = C_err all_params['A'] = A all_params['A_err'] = A_err all_params['S'] = S all_params['S_err'] = S_err all_params['G'] = G all_params['M'] = M all_params['distance'] = float(str(round(distance, 3))[:5]) all_params['mag_auto'] = c.SexMagAuto all_params['magerr_auto'] = c.SexMagAutoErr all_params['num_targets'] = c.SexTargets all_params['SexSky'] = c.SexSky all_params['flag'] = c.Flag all_params['SexHalfRad'] = c.SexHalfRad all_params['magzp'] = c.mag_zero # Now continue try: ComP = c.components except: ComP = ['bulge', 'disk'] if len(ComP) == 0: ComP = ['bulge', 'disk'] f_tpl = open(str(c.PYMORPH_PATH) + '/html/default.html', 'r') template = f_tpl.read() f_tpl.close() ra1, ra2, ra3 = RaDegToHMS(alpha_j) dec1, dec2, dec3 = DecDegToDMS(delta_j) # Formatting ra and dec for display purpose if ra1 < 10: alpha1 = '0' + str(ra1) else: alpha1 = str(ra1) if ra2 < 10: alpha2 = '0' + str(ra2) else: alpha2 = str(ra2) if ra3 < 10: alpha3 = '0' + str(ra3)[:3] else: alpha3 = str(ra3)[:4] if abs(ra1) > 360: alpha1, alpha2, alpha3 = 9999, '', '' if abs(dec1) < 10: delta1 = '0' + str(dec1) else: delta1 = str(dec1) if dec1 > 0: delta1 = '+' + str(delta1) else: pass if dec2 < 10: delta2 = '0' + str(dec2) else: delta2 = dec2 if dec3 < 10: delta3 = '0' + str(dec3)[:3] else: delta3 = str(dec3)[:4] if abs(dec1) > 90: delta1, delta2, delta3 = 9999, '', '' # Writing index file if (c.repeat == False or c.repeat): for line_i in fileinput.input("index.html", inplace=1): line_i = line_i.strip() if not '</BODY></HTML>' in line_i: print line_i indexfile = open('index.html', 'a+') NoImage = 1 for indexline in indexfile: if c.fstring in indexline: NoImage = 0 else: pass if NoImage: indexfile.writelines(['<a href="R_',\ c.fstring,'.html',\ '"> ', c.fstring,\ ' </a> <br>\n']) indexfile.writelines(['</BODY></HTML>\n']) indexfile.close() # Reading fit.log if 'bar' in ComP: basic_info, fit_info, isnan = read_fitlog(filename='fit.log', yes_bar=1) else: basic_info, fit_info, isnan = read_fitlog(filename='fit.log', yes_bar=0) if isnan: try: #set the flag all_params['flag'] = SetFlag(all_params['flag'], GetFlag('ERRORS_FAILED')) except badflag: # the flag is already set pass if 'Input' in basic_info: alpha_ned = str(alpha_j)[:10] delta_ned = str(delta_j)[:10] else: alpha_ned = '' delta_ned = '' if c.decompose: initial_conf = basic_info['initial_conf'] restart_conf = basic_info['restart_conf'] print restart_conf # move the restart file to a reasonably named output file new_outname = initial_conf.replace('in', 'out') try: os.rename(restart_conf, new_outname) except: print "Failed to find restart file!! Galfit may have crashed!!" basic_info['restart_conf'] = new_outname all_params['chi2nu'] = basic_info['chi2nu'] # first check all err components and replace if nan or inf if 'bulge' in fit_info: all_params['bulge_xctr'] = fit_info['bulge']['xctr'][0] all_params['bulge_yctr'] = fit_info['bulge']['yctr'][0] all_params['Ie'] = fit_info['bulge']['mag'][0] all_params['re_pix'] = fit_info['bulge']['rad'][0] all_params['n'] = fit_info['bulge']['n'][0] all_params['eb'] = fit_info['bulge']['ell'][0] all_params['bpa'] = fit_info['bulge']['pa'][0] all_params['bboxy'] = fit_info['bulge']['boxy'][0] all_params['bulge_xctr_err'] = fit_info['bulge']['xctr'][1] all_params['bulge_yctr_err'] = fit_info['bulge']['yctr'][1] all_params['Ie_err'] = fit_info['bulge']['mag'][1] all_params['re_pix_err'] = fit_info['bulge']['rad'][1] all_params['n_err'] = fit_info['bulge']['n'][1] all_params['eb_err'] = fit_info['bulge']['ell'][1] all_params['bpa_err'] = fit_info['bulge']['pa'][1] all_params['bboxy_err'] = fit_info['bulge']['boxy'][1] if 'disk' in fit_info: all_params['disk_xctr'] = fit_info['disk']['xctr'][0] all_params['disk_yctr'] = fit_info['disk']['yctr'][0] all_params['Id'] = fit_info['disk']['mag'][0] all_params['rd_pix'] = fit_info['disk']['rad'][0] all_params['ed'] = fit_info['disk']['ell'][0] all_params['dpa'] = fit_info['disk']['pa'][0] all_params['dboxy'] = fit_info['disk']['boxy'][0] all_params['disk_xctr_err'] = fit_info['disk']['xctr'][1] all_params['disk_yctr_err'] = fit_info['disk']['yctr'][1] all_params['Id_err'] = fit_info['disk']['mag'][1] all_params['rd_pix_err'] = fit_info['disk']['rad'][1] all_params['ed_err'] = fit_info['disk']['ell'][1] all_params['dpa_err'] = fit_info['disk']['pa'][1] all_params['dboxy_err'] = fit_info['disk']['boxy'][1] if 'point' in fit_info: all_params['p_xctr'] = fit_info['point']['xctr'][0] all_params['p_yctr'] = fit_info['point']['yctr'][0] all_params['Ip'] = fit_info['point']['mag'][0] all_params['p_xctr_err'] = fit_info['point']['xctr'][1] all_params['p_yctr_err'] = fit_info['point']['yctr'][1] all_params['Ip_err'] = fit_info['point']['mag'][1] if 'bar' in fit_info: all_params['bar_xctr'] = fit_info['bar']['xctr'][0] all_params['bar_yctr'] = fit_info['bar']['yctr'][0] all_params['Ibar'] = fit_info['bar']['mag'][0] all_params['rbar_pix'] = fit_info['bar']['rad'][0] all_params['n_bar'] = fit_info['bar']['n'][0] all_params['ebar'] = fit_info['bar']['ell'][0] all_params['barpa'] = fit_info['bar']['pa'][0] all_params['barboxy'] = fit_info['bar']['boxy'][0] all_params['bar_xctr_err'] = fit_info['bar']['xctr'][1] all_params['bar_yctr_err'] = fit_info['bar']['yctr'][1] all_params['Ibar_err'] = fit_info['bar']['mag'][1] all_params['rbar_pix_err'] = fit_info['bar']['rad'][1] all_params['nbar_err'] = fit_info['bar']['n'][1] all_params['ebar_err'] = fit_info['bar']['ell'][1] all_params['barpa_err'] = fit_info['bar']['pa'][1] all_params['barboxy_err'] = fit_info['bar']['boxy'][1] if 'sky' in fit_info: all_params['GalSky'] = fit_info['sky']['mag'][0] all_params['GalSky_err'] = fit_info['sky']['mag'][1] # Converting fitted params to physical params if (z != 9999 and z > 0): phy_parms = cal(z, c.H0, c.WM, c.WV, c.pixelscale) all_params['dis_modu'] = phy_parms[2] if 'bulge' in ComP: if all_params['re_pix'] != 9999: all_params['re_kpc'] = phy_parms[3] * all_params['re_pix'] all_params[ 're_kpc_err'] = phy_parms[3] * all_params['re_pix_err'] else: all_params['re_kpc'] = 9999 all_params['re_kpc_err'] = 9999 if 'disk' in ComP: if all_params['rd_pix'] != 9999: all_params['rd_kpc'] = phy_parms[3] * all_params['rd_pix'] all_params[ 'rd_kpc_err'] = phy_parms[3] * all_params['rd_pix_err'] else: all_params['rd_kpc'] = 9999 all_params['rd_kpc_err'] = 9999 if 'bar' in ComP: if all_params['rbar_pix'] != 9999: all_params[ 'rbar_kpc'] = phy_parms[3] * all_params['rbar_pix'] all_params['rbar_kpc_err'] = phy_parms[3] * all_params[ 'rbar_pix_err'] else: all_params['rbar_kpc'] = 9999 all_params['rbar_kpc_err'] = 9999 if 'point' in ComP: all_params['Pfwhm_kpc'] = 0.5 * phy_parms[3] # Finding derived parameters if 'bulge' in ComP and 'disk' in ComP: fb = 10**(-0.4 * (all_params['Ie'] - c.mag_zero)) fd = 10**(-0.4 * (all_params['Id'] - c.mag_zero)) if 'point' in ComP: fp = 10**(-0.4 * (all_params['Ip'] - c.mag_zero)) else: fp = 0.0 if 'bar' in ComP: fbar = 10**(-0.4 * (all_params['Ibar'] - c.mag_zero)) else: fbar = 0.0 try: all_params['BD'] = fb / fd all_params['BT'] = fb / (fb + fd + fp + fbar) except: all_params['BD'] = 9999 all_params['BT'] = 9999 elif 'bulge' in ComP: all_params['BT'] = 1.0 elif 'disk' in ComP: all_params['BD'] = 0.0 all_params['BT'] = 0.0 # Start writing html file. Now the template keywords will get values pngfile = 'P_' + c.fstring + '.png' Neighbour_Sersic = '' Object_Sersic = '' Object_Sersic_err = '' Object_Exp = '' Object_Exp_err = '' Point_Vals = '' Point_Vals_err = '' try: for key in fit_info.keys(): if 'neighbor' in key: Neighbour_Sersic = str(Neighbour_Sersic) + \ '<TR align="center" bgcolor=' + \ '"#99CCFF"><TD> neighbor sersic' + \ ' </TD> <TD> ' + \ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][0]) + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + \ str(fit_info[key]['n'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][0]) + ' </TD> <TD> ' + \ str(fit_info[key]['boxy'][0]) + ' </TD> </TR>' Neighbour_Sersic = str(Neighbour_Sersic) + \ '<TR align="center" ' + \ 'bgcolor="#CCFFFF"> <TD>' + ' ' + \ ' </TD> <TD> ' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][1]) + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + \ str(fit_info[key]['n'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][1]) + ' </TD> <TD> ' + \ str(fit_info[key]['boxy'][1]) + ' </TD> </TR>' if 'bulge' in key: Object_Sersic = '<TR align="center" ' +\ 'bgcolor="#99CCFF">' +\ '<TD> sersic bulge</TD> <TD> ' +\ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][0]) + ' </TD> <TD> ' + \ str(round(all_params['re_kpc'], 3))[:5] + ' </TD> <TD> ' + \ str(fit_info[key]['n'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][0]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' Object_Sersic_err = '<TR align="center" ' + \ 'bgcolor="#CCFFFF">' + \ '<TD>' + ' ' + '</TD> <TD>' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][1]) + ' </TD> <TD> ' + \ str(round(all_params['re_kpc_err'], 3))[:5] + ' </TD> <TD> ' + \ str(fit_info[key]['n'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['ell'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][1]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' if 'disk' in key: Object_Exp = '<TR align="center" bgcolor="#99CCFF">' +\ '<TD> disk </TD> <TD> ' + \ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][0]) + ' </TD> <TD> ' + \ str(round(all_params['rd_kpc'], 3))[:5] +\ ' </TD> <TD> </TD> <TD> ' +\ str(fit_info[key]['ell'][0]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][0]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' Object_Exp_err = '<TR align="center" ' + \ 'bgcolor="#CCFFFF">' + \ '<TD>' + ' ' + '</TD> <TD>' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) + \ ' </TD> <TD> ' + \ str(fit_info[key]['rad'][1]) + ' </TD> <TD> ' + \ str(round(all_params['rd_kpc_err'], 3))[:5] + \ ' </TD> <TD> </TD> <TD> ' +\ str(fit_info[key]['ell'][1]) + ' </TD> <TD> ' +\ str(fit_info[key]['pa'][1]) + ' </TD> <TD> ' + \ str(0) + ' </TD> </TR>' if 'point' in key: Point_Vals = '<TR align="center" bgcolor="#99CCFF">' + \ '<TD> point </TD> <TD> ' + \ str(fit_info[key]['xctr'][0]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][0]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][0]) +\ ' </TD> <TD> ' + str('9999') + ' </TD> <TD> ' + \ str('9999') + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + ' </TD></TR>' Point_Vals_err = '<TR align="center" ' + \ 'bgcolor="#CCFFFF">' + \ '<TD>' + ' ' + '</TD> <TD>' + \ str(fit_info[key]['xctr'][1]) + '</TD> <TD> '\ + str(fit_info[key]['yctr'][1]) + \ ' </TD> <TD> ' + str(fit_info[key]['mag'][1]) +\ ' </TD> <TD> ' + str('9999') + ' </TD> <TD> ' + \ str('9999') + ' </TD> <TD> ' + \ ' ' + ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + \ ' </TD> <TD> ' + str('9999') + ' </TD></TR>' except Exception, inst: print type(inst) # the exception instance print inst.args # arguments stored in\ # .args print inst # __str__ allows args\ # to printed directly print "something bad happened (writefunc writing)!!!!\n\n" print traceback.print_exc() if 'bulge' in ComP: try: pixelscale = c.pixelscale except: pixelscale = 1 try: all_params['AvgIe'] = all_params['Ie'] + 2.5 * n.log10(2 * 3.14 * pixelscale * \ pixelscale * all_params['re_pix'] * all_params['re_pix'] * n.sqrt(1 - all_params['eb']**2.0)) AvgMagInsideReErr2 = (1.085 * n.sqrt((2 * all_params['re_pix'] * all_params['re_pix_err'])**2.0 + \ (( all_params['eb'] * all_params['eb_err']) / \ n.sqrt(1 - all_params['eb']**2.0))**2.0)) / \ (n.sqrt(1 - all_params['eb']**2.0) * 2 * 3.14 * \ all_params['re_pix'] * all_params['re_pix']) all_params['AvgIe_err'] = n.sqrt(all_params['Ie_err']**2.0 + AvgMagInsideReErr2**2.0) except OverflowError: all_params['AvgIe'] = n.inf all_params['AvgIe_err'] = n.inf for key in ['AvgIe', 'AvgIe_err']: if n.isnan(all_params[key]) or n.isinf(all_params[key]): if n.isnan(all_params[key]): all_params[key] = -9999.99 else: all_params[key] = -6666.66 try: #set the AVGIe flag all_params['flag'] = SetFlag(all_params['flag'], GetFlag('AVGIE_FAILED')) except badflag: # the flag is already set pass wC = str(C)[:5] wA = str(A)[:5] wS = str(S)[:5] wG = str(G)[:5] wM = str(M)[:5] wBD = str(all_params['BD'])[:5] wBT = str(all_params['BT'])[:5] wAvgMagInsideRe = str(all_params['AvgIe'])[:5] error_mesg1 = '' error_mesg2 = '' error_mesg3 = '' error_mesg4 = '' error_mesg5 = '' error_mesg6 = '' error_mesg7 = '' if c.starthandle: error_mesg6 = '<a href="R_' + c.fstring + \ '_1.html"> Crashed </a>' # Now test for fitting problems and set flags for analysis all_params['FitFlag'] = 0 HitLimit = 0 if not c.detail: if 'bulge' in ComP: if abs(all_params['Ie'] - c.UMag) < 0.2 or abs(all_params['Ie'] - c.LMag) < 0.2: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('IE_AT_LIMIT')) if abs(all_params['re_pix'] - c.LRe) < 0.1 or abs(all_params['re_pix'] - c.URe) < 1.0: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('RE_AT_LIMIT')) if abs(all_params['n'] - c.LN) < 0.03 or abs(all_params['n'] - c.UN) < 0.5: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('N_AT_LIMIT')) if abs(all_params['eb'] - 0.0) < 0.05 or abs(all_params['eb'] - 0.0) > 0.95: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('EB_AT_LIMIT')) if 'disk' in ComP: if abs(all_params['Id'] - c.UMag) < 0.2 or abs(all_params['Id'] - c.LMag) < 0.2: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('ID_AT_LIMIT')) if abs(all_params['rd_pix'] - c.LRd) < 0.1 or abs(all_params['rd_pix'] - c.URd) < 1.0: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('RD_AT_LIMIT')) if abs(all_params['ed'] - 0.0) < 0.05 or abs(all_params['ed'] - 0.0) > 0.95: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('ED_AT_LIMIT')) if not all_params['FitFlag']: error_mesg4 = str(error_mesg4) + 'One of the parameters' error_mesg5 = str(error_mesg5) + ' hits limit!' if all_params['Goodness'] < c.Goodness: error_mesg2 = str(error_mesg2) + 'Goodness is poor!' all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('SMALL_GOODNESS')) if all_params['chi2nu'] > c.chi2sq: error_mesg1 = str(error_mesg1) + 'Chi2nu is large!' if all_params['chi2nu'] != 9999: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('LARGE_CHISQ')) if abs(all_params['bulge_xctr'] - xcntr) > c.center_deviation or \ abs(all_params['bulge_yctr'] - ycntr) > c.center_deviation or \ abs(all_params['disk_xctr'] - xcntr) > c.center_deviation or \ abs(all_params['disk_yctr'] - ycntr) > c.center_deviation: if all_params['bulge_xctr'] == -999 or all_params['bulge_yctr'] == -999 or \ all_params['disk_xctr'] == -999 or all_params['disk_yctr'] == -999: pass else: all_params['FitFlag'] = SetFlag(all_params['FitFlag'], Get_FitFlag('FAKE_CNTR')) error_mesg3 = str(error_mesg3) + 'Fake Center!' if all_params['FitFlag'] > 0: img_notify = str(c.PYMORPH_PATH) + '/html/goodfit.gif' good_fit = 1 else: img_notify = str(c.PYMORPH_PATH) + '/html/badfit.gif' good_fit = 0 chi2nu = all_params['chi2nu'] Distance = all_params['distance'] # This can be a waste of time if the list is wrong... # Finding number of runs in the csv file all_params['run'] = 1 if exists('result.csv'): for line_res in csv.reader(open('result.csv').readlines()[1:]): if (str(line_res[0]) == c.fstring): all_params['run'] += 1 if c.GalSky != 9999: all_params['GalSky'] = c.GalSky
def DecideHowToMove2(ParamDict, RunNo,z): sersic_loc = c.sersic_loc # index location of the single sersic fit in the ParamDict used for decision-making in the dev+exp and Ser+exp fits bad_fit = 0 HitLimitCheck = 0 KpCArc = cal(z, c.H0, c.WM, c.WV, c.pixelscale)[3] if abs(c.ParamDictBook[sersic_loc][1][3] - (c.LMag - 1.0)) < 0.05 or abs(c.ParamDictBook[sersic_loc][1][3] - c.UMag) < 0.05 or c.ParamDictBook[sersic_loc][1][4] < 0.21 or c.ParamDictBook[sersic_loc][2][4] < 0.21 or c.ParamDictBook[sersic_loc][1][5] > 8.0 or c.ParamDictBook[sersic_loc][1][4] *KpCArc > 40.0: print "bad sersic fit" bad_fit = 1 bt_fit = .5 ParamDict[RunNo][1][2][0] = copy.deepcopy(c.ParamDictBook[0][1][2][0]) ParamDict[RunNo][1][2][1] = copy.deepcopy(c.ParamDictBook[0][1][2][1]) ParamDict[RunNo][1][3] = copy.deepcopy(c.ParamDictBook[0][1][3]) - 2.5 * n.log10(bt_fit) ParamDict[RunNo][1][4] = copy.deepcopy(c.ParamDictBook[0][1][4]) ParamDict[RunNo][1][5] = 4.0 ParamDict[RunNo][1][6] = copy.deepcopy(c.ParamDictBook[0][1][6]) ParamDict[RunNo][1][7] = copy.deepcopy(c.ParamDictBook[0][1][7]) ParamDict[RunNo][2][2][0] = copy.deepcopy(c.ParamDictBook[0][1][2][0]) ParamDict[RunNo][2][2][1] = copy.deepcopy(c.ParamDictBook[0][1][2][1]) ParamDict[RunNo][2][3] = copy.deepcopy(c.ParamDictBook[0][1][3])-2.5 * n.log10(1.0 - bt_fit) ParamDict[RunNo][2][4] = copy.deepcopy(c.ParamDictBook[0][1][4]) ParamDict[RunNo][2][5] = copy.deepcopy(c.ParamDictBook[0][1][6]) ParamDict[RunNo][2][6] = copy.deepcopy(c.ParamDictBook[0][1][7]) try: ParamDict[RunNo][c.SkyNo][2] = copy.deepcopy(c.ParamDictBook[0][-1][2]) except: pass elif c.ParamDictBook[sersic_loc][1][5] <= 2.0: # disk-like galaxy -> so set disk parameters to bulge print 'disky' bt_fit = .25 ParamDict[RunNo][1][2][0] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][0]) ParamDict[RunNo][1][2][1] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][1]) ParamDict[RunNo][1][3] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][3]) - 2.5 * n.log10(bt_fit) ParamDict[RunNo][1][4] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][4])/4.0 #shrink bulge radius ParamDict[RunNo][1][5] = 4.0 ParamDict[RunNo][1][6] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][6]) ParamDict[RunNo][1][7] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][7]) ParamDict[RunNo][2][2][0] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][0]) ParamDict[RunNo][2][2][1] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][1]) ParamDict[RunNo][2][3] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][3])-2.5 * n.log10(1.0 - bt_fit) ParamDict[RunNo][2][4] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][4]) ParamDict[RunNo][2][5] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][6]) ParamDict[RunNo][2][6] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][7]) try: ParamDict[RunNo][c.SkyNo][2] = copy.deepcopy(c.ParamDictBook[sersic_loc][-1][2]) except: pass elif 3.0 <= c.ParamDictBook[sersic_loc][1][5] and c.ParamDictBook[sersic_loc][1][5] <= 6.0: # bulge-like galaxy -> so set bulge parameters to bulge print 'bulge_like' bt_fit = .75 ParamDict[RunNo][1][2][0] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][0]) ParamDict[RunNo][1][2][1] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][1]) ParamDict[RunNo][1][3] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][3]) - 2.5 * n.log10(bt_fit) ParamDict[RunNo][1][4] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][4]) #shrink bulge radius ParamDict[RunNo][1][5] = 4.0 ParamDict[RunNo][1][6] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][6]) ParamDict[RunNo][1][7] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][7]) ParamDict[RunNo][2][2][0] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][0]) ParamDict[RunNo][2][2][1] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][1]) ParamDict[RunNo][2][3] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][3])-2.5 * n.log10(1.0 - bt_fit) ParamDict[RunNo][2][4] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][4])*2.0 ParamDict[RunNo][2][5] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][6]) ParamDict[RunNo][2][6] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][7]) try: ParamDict[RunNo][c.SkyNo][2] = copy.deepcopy(c.ParamDictBook[sersic_loc][-1][2]) except: pass else: # some intermediate combination of bulge and disk print 'mix' bt_fit = .5 ParamDict[RunNo][1][2][0] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][0]) ParamDict[RunNo][1][2][1] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][1]) ParamDict[RunNo][1][3] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][3]) - 2.5 * n.log10(bt_fit) ParamDict[RunNo][1][4] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][4])*.5 ParamDict[RunNo][1][5] = 4.0 ParamDict[RunNo][1][6] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][6]) ParamDict[RunNo][1][7] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][7]) ParamDict[RunNo][2][2][0] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][0]) ParamDict[RunNo][2][2][1] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][2][1]) ParamDict[RunNo][2][3] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][3])-2.5 * n.log10(1.0 - bt_fit) ParamDict[RunNo][2][4] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][4])*2 ParamDict[RunNo][2][5] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][6]) ParamDict[RunNo][2][6] = copy.deepcopy(c.ParamDictBook[sersic_loc][1][7]) try: ParamDict[RunNo][c.SkyNo][2] = copy.deepcopy(c.ParamDictBook[sersic_loc][-1][2]) except: pass return bad_fit
def confiter(cutimage, whtimage, xcntr, ycntr, NXPTS, NYPTS, line_s, psffile, z): # Run SExtractor RunSex(os.path.join(c.datadir, cutimage), os.path.join(c.datadir, whtimage), 'TEMP.SEX.cat', 9999, 9999, 0) # define improtant variables in the namespace imagefile = c.imagefile sex_cata = c.sex_cata threshold = c.threshold thresh_area = c.thresh_area mask_reg = c.mask_reg c.sersic_loc = 2 c.sky_loc = 1 try: ComP = c.components except: ComP = ['bulge', 'disk'] if len(ComP) == 0: ComP = ['bulge', 'disk'] values = line_s.split() # create filenames outfile = 'O_' + c.fstring + '.fits' # galfit output mask_file = 'M_' + c.fstring + '.fits' # mask file config_file = 'G_' + c.fstring + '.in' #GALFIT configuration file constrain_file = c.fstring + '.con' #galfit constraint file xcntr_o = xcntr #x center of the object ycntr_o = ycntr #y center of the object mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius mag_zero = c.mag_zero #magnitude zero point sky = float(values[10]) #sky pos_ang = float(values[11]) - 90.0 #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a area_o = float(values[13]) # object's area major_axis = float(values[14]) #major axis of the object # calculate a 40kpc bulge radius to use for maximum bulge size. # This will only work if z is supplied, otherwise, a default maximum # is used in the constraint file if z > 0 and z < 10: try: KpCArc = cal(z, c.H0, c.WM, c.WV, c.pixelscale)[3] max_rad = 40.0 / KpCArc except: KpCArc = 9999.0 max_rad = c.URe else: max_rad = c.URe KpCArc = 9999.0 # Modify flags for the detailed fitting process if isset(c.Flag, GetFlag('FIT_BULGE_CNTR')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_BULGE_CNTR')) if isset(c.Flag, GetFlag('FIT_DISK_CNTR')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_DISK_CNTR')) if isset(c.Flag, GetFlag('FIT_SKY')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_SKY')) if isset(c.Flag, GetFlag('FIT_BULGE')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_BULGE')) if isset(c.Flag, GetFlag('FIT_DISK')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_DISK')) if isset(c.Flag, GetFlag('FIT_POINT')): c.Flag = ClrFlag(c.Flag, GetFlag('FIT_POINT')) # define initial fitting parameters for single sersic fit ParamDict = {} ErrDict = {} ParamDict[0] = {} ErrDict[0] = {} BlankDict = {} # to be used in cases where the program crashes BlankDict[0] = {} # Add components AdComp = 1 if 'bulge' in ComP: ParamDict[0][AdComp] = {} #Bulge Parameters ParamDict[0][AdComp][1] = 'sersic' ParamDict[0][AdComp][2] = [xcntr_o, ycntr_o] ParamDict[0][AdComp][3] = mag ParamDict[0][AdComp][4] = radius ParamDict[0][AdComp][5] = 4.0 ParamDict[0][AdComp][6] = axis_rat ParamDict[0][AdComp][7] = pos_ang ParamDict[0][AdComp][8] = 0 ParamDict[0][AdComp][9] = 0 ParamDict[0][AdComp][11] = 'Main' BlankDict[0][AdComp] = {} #Bulge Parameters BlankDict[0][AdComp][1] = 'sersic' BlankDict[0][AdComp][2] = [xcntr_o, ycntr_o] BlankDict[0][AdComp][3] = 9999 BlankDict[0][AdComp][4] = 9999 BlankDict[0][AdComp][5] = 9999 BlankDict[0][AdComp][6] = 9999 BlankDict[0][AdComp][7] = 9999 BlankDict[0][AdComp][8] = 9999 BlankDict[0][AdComp][9] = 9999 BlankDict[0][AdComp][11] = 'Main' AdComp += 1 if 'disk' in ComP: #Disk parameters ParamDict[0][AdComp] = {} ParamDict[0][AdComp][1] = 'expdisk' ParamDict[0][AdComp][2] = [xcntr_o, ycntr_o] ParamDict[0][AdComp][3] = mag ParamDict[0][AdComp][4] = radius ParamDict[0][AdComp][5] = axis_rat ParamDict[0][AdComp][6] = pos_ang ParamDict[0][AdComp][7] = 0 ParamDict[0][AdComp][8] = 0 ParamDict[0][AdComp][11] = 'Main' BlankDict[0][AdComp] = {} BlankDict[0][AdComp][1] = 'expdisk' BlankDict[0][AdComp][2] = [xcntr_o, ycntr_o] BlankDict[0][AdComp][3] = 9999 #mag BlankDict[0][AdComp][4] = 9999 #radius BlankDict[0][AdComp][5] = 9999 #axis_rat BlankDict[0][AdComp][6] = 9999 #pos_ang BlankDict[0][AdComp][7] = 9999 BlankDict[0][AdComp][8] = 9999 BlankDict[0][AdComp][11] = 'Main' AdComp += 1 isneighbour = 0 f_constrain = open(constrain_file, 'ab') for line_j in open('TEMP.SEX.cat', 'r'): try: values = line_j.split() xcntr_n = float(values[1]) #x center of the neighbour ycntr_n = float(values[2]) #y center of the neighbour mag = float(values[7]) #Magnitude radius = float(values[9]) #Half light radius sky = float(values[10]) #sky pos_ang = float(values[11]) - 90.0 #position angle axis_rat = 1.0 / float(values[12]) #axis ration b/a area_n = float(values[13]) # neighbour area maj_axis = float(values[14]) #major axis of neighbour NotFitNeigh = 0 if abs(xcntr_n - xcntr_o) > NXPTS / 2.0 + c.avoidme or \ abs(ycntr_n - ycntr_o) > NYPTS / 2.0 + c.avoidme or \ abs(xcntr_n - xcntr_o) < 5.0 and abs(ycntr_n - ycntr_o) < 5.0: NotFitNeigh = 1 if(abs(xcntr_n - xcntr_o) <= (major_axis + maj_axis) * \ threshold and \ abs(ycntr_n - ycntr_o) <= (major_axis + maj_axis) * \ threshold and area_n >= thresh_area * area_o and \ xcntr_n != xcntr_o and ycntr_n != ycntr_o and NotFitNeigh == 0): if ((xcntr_o - xcntr_n) < 0): xn = xcntr + abs(xcntr_n - xcntr_o) if ((ycntr_o - ycntr_n) < 0): yn = ycntr + abs(ycntr_n - ycntr_o) if ((xcntr_o - xcntr_n) > 0): xn = xcntr - (xcntr_o - xcntr_n) if ((ycntr_o - ycntr_n) > 0): yn = ycntr - (ycntr_o - ycntr_n) ParamDict[0][AdComp] = {} ParamDict[0][AdComp][1] = 'sersic' ParamDict[0][AdComp][2] = [xn, yn] ParamDict[0][AdComp][3] = mag ParamDict[0][AdComp][4] = radius ParamDict[0][AdComp][5] = 4.0 ParamDict[0][AdComp][6] = axis_rat ParamDict[0][AdComp][7] = pos_ang ParamDict[0][AdComp][8] = 0 ParamDict[0][AdComp][9] = 0 ParamDict[0][AdComp][11] = 'Other' BlankDict[0][AdComp] = {} BlankDict[0][AdComp][1] = 'sersic' BlankDict[0][AdComp][2] = [xn, yn] BlankDict[0][AdComp][3] = mag BlankDict[0][AdComp][4] = radius BlankDict[0][AdComp][5] = 4.0 BlankDict[0][AdComp][6] = axis_rat BlankDict[0][AdComp][7] = pos_ang BlankDict[0][AdComp][8] = 0 BlankDict[0][AdComp][9] = 0 BlankDict[0][AdComp][11] = 'Other' isneighbour += 1 AdComp += 1 except: pass if isneighbor > 0: c.Flag = SetFlag(c.Flag, GetFlag('NEIGHBOUR_FIT')) f_constrain.close() #Sky component ParamDict[0][AdComp] = {} ParamDict[0][AdComp][1] = 'sky' ParamDict[0][AdComp][2] = c.SexSky ParamDict[0][AdComp][3] = 0 ParamDict[0][AdComp][4] = 0 ParamDict[0][AdComp][5] = 0 ParamDict[0][AdComp][11] = 'Other' BlankDict[0][AdComp] = {} BlankDict[0][AdComp][1] = 'sky' BlankDict[0][AdComp][2] = c.SexSky BlankDict[0][AdComp][3] = 0 BlankDict[0][AdComp][4] = 0 BlankDict[0][AdComp][5] = 0 BlankDict[0][AdComp][11] = 'Other' #Write Sersic function ErrDict[0][1] = {} ErrDict[0][1][1] = [9999, 9999] ErrDict[0][1][2] = 9999 ErrDict[0][1][3] = 9999 ErrDict[0][1][4] = 9999 ErrDict[0][2] = {} ErrDict[0][2][1] = [9999, 9999] ErrDict[0][2][2] = 9999 ErrDict[0][2][3] = 9999 c.SkyNo = AdComp c.Chi2DOFArr = [] c.FitArr = [] c.RadArr = [] c.CntrDevArr = [] c.ErrArr = [] c.ParamDictBook = copy.deepcopy(ParamDict) bad_fit = 0 failed_ser = 0 bt_fit = -1 failed_sky = 0 bad_sky = 0 #Write configuration file. RunNo is the number of iteration for RunNo, fit_type in zip(range(5), ['sky', 'ser', 'dev', 'devexp', 'serexp']): # note that the 'sky' fit is currently serexp #force batch fitting using standard operations run_flag = c.Flag if exists('fit.log'): print "removing fit log" os.system('rm fit.log') print "RunNo ", RunNo, "fit type ", fit_type f_constrain = open(constrain_file, 'w') f_constrain.close() f = open(config_file, 'w') f.write('# IMAGE PARAMETERS\n') f.writelines(['A) ', os.path.join(c.datadir, cutimage), ' # Input data image',\ ' (FITS file)\n']) f.writelines(['B) ', str(outfile), ' # Name for',\ ' the output image\n']) f.writelines(['C) ', os.path.join(c.datadir, whtimage), ' # Noise image name', \ ' (made from data if blank or "none")\n']) f.writelines(['D) ', os.path.join(c.datadir, psffile), ' # Input PSF', \ ' image for convolution (FITS file)\n']) f.writelines(['E) 1 # PSF oversampling factor '\ 'relative to data\n']) f.writelines([ 'F) ', str(mask_file), ' # Bad pixel', ' mask(FITS image or ASCII coord list)\n' ]) f.writelines(['G) ', str(constrain_file), ' # File with'\ ' parameter constraints (ASCII file)\n']) f.writelines(['H) 1 ', str(NXPTS), ' 1 ', str(NYPTS), ' #',\ ' Image region to fit (xmin xmax ymin ymax)\n']) # f.writelines(['I) ', str(NXPTS), ' ', str(NYPTS), ' #',\ # ' Size of convolution box (x y)\n']) f.writelines(['I) ', str(120), ' ', str(120), ' #',\ ' Size of convolution box (x y)\n']) f.writelines(['J) ', str(mag_zero), ' # Magnitude',\ ' photometric zeropoint\n']) f.writelines(['O) regular # Display type',\ ' (regular, curses, both)\n']) f.writelines(['P) 0 # Create output image only?',\ ' (1=yes; 0=optimize)\n']) f.writelines(['S) 0 # Modify/create',\ ' objects interactively?\n\n\n']) f.close() FitDict, run_flag = DecideFitting(ParamDict, RunNo, fit_type, bad_fit, failed_ser, run_flag, failed_sky, bad_sky) for i in range(len(ParamDict[RunNo])): if ParamDict[RunNo][i + 1][1] == 'sersic': SersicFunc(config_file, ParamDict, FitDict, i + 1, RunNo) if ParamDict[RunNo][i + 1][11] == 'Main': run_flag = SetFlag(run_flag, GetFlag('FIT_BULGE')) print "ADDING FLAG FIT_BULGE" if RunNo > 1 and not bad_fit and ParamDict[ c.sersic_loc][1][4] * 2 < max_rad: # later fits SersicMainConstrain(constrain_file, i + 1, 2.0, ParamDict[c.sersic_loc][1][4] * 2) else: SersicMainConstrain(constrain_file, i + 1, c.center_constrain, max_rad) else: SersicConstrain(constrain_file, i + 1) if ParamDict[RunNo][i + 1][1] == 'expdisk': if RunNo in [1, 2]: pass #dont fit a disk in the cases of ser or dev fits else: run_flag = SetFlag(run_flag, GetFlag('FIT_DISK')) print "ADDING FLAG FIT_DISK" ExpFunc(config_file, ParamDict, FitDict, i + 1, RunNo) if RunNo > 0 and not bad_fit: # later runs ExpdiskConstrain(constrain_file, i + 1, 2.0) else: ExpdiskConstrain(constrain_file, i + 1, c.center_constrain) if ParamDict[RunNo][i + 1][1] == 'sky': if RunNo > 0 and not bad_sky: SkyConstrain(constrain_file, i + 1, ParamDict[c.sky_loc][i + 1][2]) else: SkyConstrain(constrain_file, i + 1, c.SexSky) SkyFunc(config_file, ParamDict, FitDict, i + 1, RunNo) # only if forcing normal fit if RunNo > 2 or RunNo == 0: #later fits after single ser fit bt_range = add_constrain(constrain_file, bt_fit, fit_type) cmd = str(c.GALFIT_PATH) + ' ' + config_file os.system(cmd) # ReadLog(ParamDict, 2) => this function reads fig.log # depends on whether, for example, the expdisk function # is the only one function for fitting. It can happends, # for example, the first fitting where we can find the best # initial values for the rest of it. ReadLog(ParamDict, 1) # reads the fit.log in the order, ie. sersic, # expdisk, other sersic etc try: print 'readlog' ParamDict, ErrDict, Chi2DOF = ReadLog(ParamDict, ErrDict, 1, RunNo, detail=True) print 'log read' print 'chi2dof ', Chi2DOF c.ParamDictBook[RunNo + 1] = copy.deepcopy(ParamDict[RunNo + 1]) # Set limit flags Fitflag = 0 mag_b = ParamDict[RunNo + 1][1][3] re = ParamDict[RunNo + 1][1][4] SersicIndex = ParamDict[RunNo + 1][1][5] SersicEllipticity = ParamDict[RunNo + 1][1][6] if abs(mag_b - c.UMag) < 0.2 or abs(mag_b - c.LMag) < 0.2: FitFlag = SetFlag(FitFlag, Get_FitFlag('IE_AT_LIMIT')) if abs(re - c.LRe) < 0.1 or abs(re - max_rad) < 1.0: FitFlag = SetFlag(FitFlag, Get_FitFlag('RE_AT_LIMIT')) if abs(SersicIndex - c.LN) < 0.03 or abs(SersicIndex - c.UN) < 0.5: FitFlag = SetFlag(FitFlag, Get_FitFlag('N_AT_LIMIT')) if abs(SersicEllipticity - 0.0) < 0.05 or abs(SersicEllipticity - 0.0) > 0.95: FitFlag = SetFlag(FitFlag, Get_FitFlag('EB_AT_LIMIT')) if fit_type in ['devexp', 'serexp']: mag_d = ParamDict[RunNo + 1][2][3] rd = ParamDict[RunNo + 1][2][4] DiskEllipticity = ParamDict[RunNo + 1][2][5] fb = 10**(-0.4 * mag_b) fd = 10**(-0.4 * mag_d) if abs(mag_d - c.UMag) < 0.2 or abs(mag_d - c.LMag) < 0.2: FitFlag = SetFlag(FitFlag, Get_FitFlag('ID_AT_LIMIT')) if abs(rd - c.LRd) < 0.1 or abs(rd - c.URd) < 1.0: FitFlag = SetFlag(FitFlag, Get_FitFlag('RD_AT_LIMIT')) if abs((re / rd) - 1.0) < 0.02 or abs((re / rd) - 0.1) < 0.02: FitFlag = SetFlag(FitFlag, Get_FitFlag('RERD_AT_LIMIT')) if abs(DiskEllipticity - 0.0) < 0.05 or abs(DiskEllipticity - 0.0) > 0.95: FitFlag = SetFlag(FitFlag, Get_FitFlag('ED_AT_LIMIT')) try: BT = fb / (fb + fd) except: BT = 9999.0 if abs(BT - bt_range[0]) < .02 or abs(BT - bt_range[1]) < .02: FitFlag = SetFlag(FitFlag, Get_FitFlag('BT_AT_LIMIT')) print "ADDING FLAG BT_AT_LIMIT" except: print "failure at readlog!!!" c.ParamDictBook[RunNo + 1] = copy.deepcopy(BlankDict[0]) ParamDict[RunNo + 1] = copy.deepcopy(BlankDict[0]) ErrDict[RunNo + 1] = copy.deepcopy(ErrDict[0]) if fit_type == 'ser': failed_ser = 1 #track the failure of the single sersic fit run_flag = SetFlag(run_flag, GetFlag('DETAIL_FAILED')) c.Flag = SetFlag(c.Flag, GetFlag('DETAIL_FAILED')) print "ADDING FLAG DETAIL_FAILED" elif fit_type == 'sky': failed_sky = 1 #track that we did not fix the sky run_flag = SetFlag(run_flag, GetFlag('DETAIL_FAILED')) c.Flag = SetFlag(c.Flag, GetFlag('DETAIL_FAILED')) print "ADDING FLAG DETAIL_FAILED" try: c.ErrArr.append(FractionalError(ParamDict, ErrDict, RunNo + 1)) except: c.ErrArr.append(9999.0) c.Chi2DOFArr.append(Chi2DOF) if fit_type != 'serexp': #set flags for other fit types bad_sky, bad_fit, bt_fit = DecideHowToMove2( ParamDict, RunNo + 1, fit_type, KpCArc, failed_ser, failed_sky) try: plot_name = 'P_' + str(cutimage)[6:-5] \ +'_'+fit_type+ '.png' if exists(plot_name): os.system('rm ' + plot_name) GoodNess = PlotFunc( cutimage, outfile, mask_file, ParamDict[RunNo + 1][1][2][0], #xctr ParamDict[RunNo + 1][1][2][1], #yctr ParamDict[RunNo + 1][1][c.SkyNo][2], #sky c.skysig, save_name=plot_name) Goodness = GoodNess.plot_profile except: Goodness = 9999 run_flag = SetFlag(run_flag, GetFlag('PLOT_FAIL')) print "ADDING FLAG PLOT_FAIL" if Goodness < c.Goodness: FitFlag = SetFlag(FitFlag, Get_FitFlag('SMALL_GOODNESS')) print "ADDING FLAG SMALL_GOODNESS" if Chi2DOF > c.chi2sq: if chi2nu != 9999: FitFlag = SetFlag(FitFlag, Get_FitFlag('LARGE_CHISQ')) print "ADDING FLAG LARGE_CHISQ" print 'writing db' try: WriteDbDetail(cutimage.split('.')[0], c.ParamDictBook[RunNo + 1], ErrDict[RunNo + 1], c.SexSky, c.ParamDictBook[RunNo + 1][c.SkyNo][2], RunNo, run_flag, FitFlag, Chi2DOF, model_type=fit_type, goodness=Goodness) except: print 'No database' traceback.print_exc() time.sleep(10) fit_log = 'fit_%s.log' % fit_type print "saving fit log to fit log %s" % (fit_log) f_fit = open(fit_log, 'a') if exists('fit.log'): for line in open('fit.log', 'r'): f_fit.writelines([str(line)]) f_fit.close() for mv_file_nm in [outfile, config_file, constrain_file]: os.system('cp %s %s_%s.%s' % (mv_file_nm, mv_file_nm.split('.')[0], fit_type, mv_file_nm.split('.')[1])) if fit_type == 'serexp': c.Flag = run_flag return