def check_map_params(image1, image2, ifhdu): """v1 < v2""" header1 = take_header(image1, ifhdu) header2 = take_header(image2, ifhdu) cell1 = header1[0] cell2 = header2[0] if cell1 == cell2: cells = header1[0] print 'Same cellsize' else: print 'The images do not have the same cellsize' print 'Convolve them using the same cell' raise Exception('Stopping!') map_data1 = read_map(image1, ifhdu) realDAT = map_data1[0] map_data2 = read_map(image2, ifhdu) realDAT2 = map_data2[0] if realDAT.shape == realDAT2.shape: print 'Same mapsize' else: print 'The images do not have the same mapsize' print 'Convolve them using the same mapsize' raise Exception('Stopping!') beam1 = header1[7] beam2 = header2[7] if beam1 == beam2: beam = header1[7] print 'Same beam' else: print 'The images do not have the same beam' print 'Convolve them using the same beam' raise Exception('Stopping!') #obtaining frequencies freq1 = header1[5] freq2 = header2[5] #obtaining map centers in pixels cent_mapx = map_data1[5] cent_mapy = map_data1[6] #obtaining the four corners of the maps in mas x1 = map_data1[1] x2 = map_data1[2] y1 = map_data1[3] y2 = map_data1[4] print 'Proceeding with the next step' return realDAT, realDAT2, freq1, freq2, cells, beam, cent_mapx, cent_mapy, x1, x2, y1, y2
def checking_shift(shifted_files, position, size, position_feature, size_feature, ifhdu): check2 = check_map_params(shifted_files[0], shifted_files[1], ifhdu) map_data1 = read_map(shifted_files[0], ifhdu) realDATshift = map_data1[0] map_data2 = read_map(shifted_files[1], ifhdu) realDAT2shift = map_data2[0] cutout_v1shift = Cutout2D(realDATshift, position, size) cutout_v2shift = Cutout2D(realDAT2shift, position, size) cutout_v1shift_feature = Cutout2D(cutout_v1shift.data, position_feature, size_feature) cutout_v2shift_feature = Cutout2D(cutout_v2shift.data, position_feature, size_feature) image1 = cutout_v1shift_feature.data image2 = cutout_v2shift_feature.data offset_new = cross_correlation_shifts_FITS(image1, image2, sigma_cut=0.004) return offset_new
def order_by_nu(files, models, fits, ifhdu): #initialize arrays cell = np.array([0.] * len(fits)) bmaj = np.array([0.] * len(fits)) bmin = np.array([0.] * len(fits)) bpa = np.array([0.] * len(fits)) freq = np.array([0.] * len(fits)) beam = np.array([0.] * len(fits)) size_map = np.array([0.] * len(fits)) size_map_y = np.array([0.] * len(fits)) #read some parameters of the map for i in xrange(0, len(fits)): header = take_header(fits[i], ifhdu) cell[i] = header[0] bmaj[i] = header[1] bmin[i] = header[2] bpa[i] = header[3] freq[i] = header[5] beam[i] = header[7] map_details = read_map(fits[i], ifhdu) if map_details[7] == map_details[8]: size_map[i] = map_details[7] else: size_map[i] = map_details[7] size_map_y[i] = map_details[8] #store them in ascendent order related with the frequency n = 0 for z in freq: n += 1 for i in range(n): for j in range(1, n - i): if freq[j - 1] > freq[j]: (freq[j - 1], freq[j]) = (freq[j], freq[j - 1]) (cell[j - 1], cell[j]) = (cell[j], cell[j - 1]) (bmaj[j - 1], bmaj[j]) = (bmaj[j], bmaj[j - 1]) (bmin[j - 1], bmin[j]) = (bmin[j], bmin[j - 1]) (bpa[j - 1], bpa[j]) = (bpa[j], bpa[j - 1]) (beam[j - 1], beam[j]) = (beam[j], beam[j - 1]) (size_map[j - 1], size_map[j]) = (size_map[j], size_map[j - 1]) (size_map_y[j - 1], size_map_y[j]) = (size_map_y[j], size_map_y[j - 1]) (files[j - 1], files[j]) = (files[j], files[j - 1]) (models[j - 1], models[j]) = (models[j], models[j - 1]) (fits[j - 1], fits[j]) = (fits[j], fits[j - 1]) return freq, cell, bmaj, bmin, bpa, beam, size_map, size_map_y, files, models, fits
def Bfieldcalculation(self): self.DLvalue = float(self.DL.text()) self.zvalue = float(self.z.text()) self.scalevalue = float(self.scale.text()) self.gammavalue = float(self.gamma.text()) self.deltavalue = float(self.delta.text()) filefreq = 'bo/shifted15.fits' header = take_header(filefreq,False) self.cellsize = header[0] mapdata = read_map(filefreq,False) realDAT = mapdata[0] self.ext = [mapdata[1],mapdata[2],mapdata[3],mapdata[4]] res = open('turnoverdata.p','rb') pick = pickle.load(res) res.close() self.vmall = pick[0] self.small = pick[1]#*1.4213481404770085 self.alpha0all = pick[2] self.alphathickall = pick[3] shapee = np.shape(self.alpha0all) self.Ball = np.zeros(shapee) self.Kall = np.zeros(shapee) self.Ball[:] = np.nan self.Kall[:] = np.nan #cellsize *2 for getting the diameter for i in xrange (0,shapee[0]): for j in xrange(0,shapee[1]): if np.isnan(self.small[i][j]) == False: tempBK = B_field(self.alpha0all[i][j],self.alphathickall[i][j],self.DLvalue,self.zvalue,self.scalevalue,self.cellsize*2*self.gammavalue,self.deltavalue,self.vmall[i][j],self.small[i][j]) self.Ball[i][j] = tempBK[0] self.Kall[i][j] = tempBK[1] plt.figure(1) cset = plt.contour(realDAT,0.008*np.array([2.,4.,16.,64.,256.,1020.,2050.]),inline=1,colors=['grey'],aspect=1.0,extent=self.ext) plt.imshow(self.Ball,origin='bottom',extent=self.ext,vmin=0,vmax=0.1) plt.colorbar() plt.show()
def check_map_params(image1, image2, ifhdu): """v1 < v2""" header1 = take_header(image1, ifhdu) header2 = take_header(image2, ifhdu) cell1 = header1[0] cell2 = header2[0] if cell1 == cell2: cells = header1[0] OK = True print('Same cellsize') else: cells = 0. print('The images do not have the same cellsize') print('Convolve them using the same cell') OK = False map_data1 = read_map(image1, ifhdu) realDAT = map_data1[0] map_data2 = read_map(image2, ifhdu) realDAT2 = map_data2[0] if realDAT.shape == realDAT2.shape: if OK == False: OK = False else: OK = True print('Same mapsize') else: print('The images do not have the same mapsize') print('Convolve them using the same mapsize') OK = False beam1 = header1[7] beam2 = header2[7] if beam1 == beam2: beam = header1[7] if OK == False: OK = False else: OK = True print('Same beam') else: beam = 0. print('The images do not have the same beam') print('Convolve them using the same beam') OK = False #raise IOError('Stopping!') compatible with python 3, same that raise Exception #obtaining frequencies freq1 = header1[5] freq2 = header2[5] #obtaining map centers in pixels cent_mapx = map_data1[5] cent_mapy = map_data1[6] #obtaining the four corners of the maps in mas x1 = map_data1[1] x2 = map_data1[2] y1 = map_data1[3] y2 = map_data1[4] print('Proceeding with the next step') return OK, realDAT, realDAT2, freq1, freq2, cells, beam, cent_mapx, cent_mapy, x1, x2, y1, y2
def Tbcalculation(self): self.DLvalue = float(self.DL.text()) self.zvalue = float(self.z.text()) self.scalevalue = float(self.scale.text()) a = 0 for i in xrange(len(self.checks)): if self.checks[i].isChecked(): #if a == 0: self.fits1 = needed_param.fits[i] self.models1 = needed_param.modelfit[i] self.models1errors = needed_param.modelfiterror[i] tempi = i # a = 1 #if a == 1: # self.fits2 = needed_param.fits[i] #self.reading_rms_param() header1 = take_header(self.fits1,self.ifhdu) #header2 = take_header(self.fits2,self.ifhdu) map_data1 = read_map(self.fits1,self.ifhdu) self.image1 = map_data1[0] #map_data2 = read_map(self.fits2,self.ifhdu) #self.image2 = map_data2[0] #obtaining the beam and cell self.bmaj_files = header1[1] self.bmin_files = header1[2] self.bpa_files = header1[3] self.beam_files = header1[7] self.cells_files = header1[0] self.freq1 = header1[5] #obtaining map centers in pixels self.cent_mapx = map_data1[5] self.cent_mapy = map_data1[6] self.mapsize_files = 2*map_data1[7] #obtaining the four corners of the maps in mas x1 = map_data1[1] x2 = map_data1[2] y1 = map_data1[3] y2 = map_data1[4] self.extmas=[x1,x2,y1,y2] self.sigma_cut = float(self.SigmaCut.text()) if self.freq1 < 0.5: self.freq1name = str('%1.0f' %(self.freq1*1000)) self.freq1unit = 'MHz' else: self.freq1name = str('%1.2f' %(self.freq1)) self.freq1unit = 'GHz' res=open(needed_param.path+'/rms'+self.freq1name+'.p','rb') pick = pickle.load(res) res.close() self.rms1 = pick modelfitparameters = read_modfile([self.models1],self.beam_files,self.zvalue,self.freq1,True,self.models1errors) #r_arr,errr_arr, #2psi_arr,errpsi_arr, #4size_arr,errsize_arr, #6flux_arr,errflux_arr, #8tb1_arr,errtb1_arr, #dlim1_arr, #11tb2_arr,errtb2_arr, #dlim2_arr x_and_y = x_y(modelfitparameters[0],modelfitparameters[1],modelfitparameters[2],modelfitparameters[3],True) x, errx = np.asarray(x_and_y[0]), np.asarray(x_and_y[1]) y, erry = np.asarray(x_and_y[2]), np.asarray(x_and_y[3]) #for plotting the components in the map pts_arr=[] pt_arr=[] x_el_arr=[] x_elH_arr=[] y_el_arr=[] y_elH_arr=[] #r_arr 0,errr_arr 1 ,psi_arr 2,errpsi_arr 3,size_arr 4,errsize_arr 5,tb_arr 6,errtb_arr 7,flux_arr 8,errflux_arr 9,tbNew 10 r = modelfitparameters[0] errr = modelfitparameters[1] psi = modelfitparameters[2] size = modelfitparameters[4] errsize = modelfitparameters[5] Tb = modelfitparameters[8][0] errTb = modelfitparameters[9][0] self.Tbcore[tempi] = Tb[0] self.sizecore[tempi] = modelfitparameters[4][0] self.fluxcore[tempi] = modelfitparameters[6][0] print 'Tb', self.Tbcore print 'size', self.sizecore print 'flux', self.fluxcore ellipse_plot = ellipse_axis_lines(x,y,modelfitparameters[4]) pts_arr,pt_arr = ellipse_plot[0], ellipse_plot[1] x_el_arr,y_el_arr = ellipse_plot[2], ellipse_plot[3] x_elH_arr,y_elH_arr = ellipse_plot[4], ellipse_plot[5] plt.figure(1) plot_components(pts_arr,x_el_arr,x_elH_arr,y_elH_arr,y_el_arr) plot_maps(self.image1,self.extmas,self.rms1*self.sigma_cut) plt.xlim(x1,x2) #plt.ylim(-1.5,1.5) #plt.savefig('1642CBAND.png', bbox_inches='tight') plt.show() """limits = Annotate() plt.show() ext_new = [] [self.limplot_x1,self.limplot_x2,self.limplot_y1,self.limplot_y2] = limits() self.extmasnew = [self.limplot_x1,self.limplot_x2,self.limplot_y2,self.limplot_y1] plt.figure(1) plot_components(pts_arr,x_el_arr,x_elH_arr,y_elH_arr,y_el_arr) plot_maps(self.image1,self.extmas,self.rms1*self.sigma_cut) plt.xlim(self.limplot_x1,self.limplot_x2) plt.ylim(self.limplot_y2,self.limplot_y1) plt.savefig('modelfit'+str('%1.1f' % (self.freq1))+'.png', bbox_inches='tight') plt.show()""" plt.close('all') plt.figure(2) plt.plot(needed_param.freq,self.Tbcore,'r.',markersize=12) plt.xscale('log') plt.yscale('log') plt.ylabel(r'$T_b$ [K]') plt.xlabel(r'$\nu$ [GHz]') plt.savefig('logTb_ground.png') plt.figure(3) plt.plot(needed_param.freq,self.sizecore,'r.',markersize=12) plt.xscale('log') plt.yscale('log') plt.ylabel(r'$\theta$ [mas]') plt.xlabel(r'$\nu$ [GHz]') plt.savefig('logTheta_ground.png') plt.figure(4) plt.plot(needed_param.freq,self.fluxcore,'r.',markersize=12) plt.xscale('log') plt.yscale('log') plt.ylabel(r'$S_y$ [Jy]') plt.xlabel(r'$\nu$ [GHz]') plt.savefig('logS_ground.png') #plt.xlim(x1,x2) #plt.ylim(-1.5,1.5) #plt.savefig('1642CBAND.png', bbox_inches='tight') plt.close('all') plt.figure(2) plt.xscale('log') plt.yscale('log') plt.errorbar(r,Tb,yerr=0.434*errTb,fmt='ro',ecolor='r', capthick=2) plt.ylabel(r'$T_b$ [K]') plt.xlabel('r [mas]') plt.savefig('Tbfreq'+str('%1.1f' % (self.freq1))+'.png') plt.close('all') res=open(needed_param.path+'/pickle'+str('%1.1f' % (self.freq1))+'.p','wb') pickle.dump([r,Tb,0.434*errTb,psi,x,y,size,errsize],res) res.close()
def selectComp(self, fits1, fits2): self.rms = [] header1 = take_header(fits1, False) header2 = take_header(fits2, False) #self.rms.append(header1[9]) #self.rms.append(header2[9]) map_data1 = read_map(fits1, False) realDAT = map_data1[0] map_data2 = read_map(fits2, False) realDAT2 = map_data2[0] #picks the value of the first contour for plotting the maps ShiftComponentsWindow.realDATA = [realDAT, realDAT2] ShiftComponentsWindow.numComp = int(self.numComp.text()) #obtaining the beam and cell self.bmaj_files = header1[1] self.bmin_files = header1[2] self.bpa_files = header1[3] self.beam_files = header1[7] self.cells_files = header1[0] #obtaining frequencies self.freq1 = header1[5] self.freq2 = header2[5] if self.freq1 < 0.5: self.freq1name = str('%1.0f' % (self.freq1 * 1000)) self.freq1unit = 'MHz' else: self.freq1name = str('%1.2f' % (self.freq1)) self.freq1unit = 'GHz' if self.freq2 < 0.5: self.freq2name = str('%1.0f' % (self.freq2 * 1000)) self.freq2unit = 'MHz' else: self.freq2name = str('%1.2f' % (self.freq2)) self.freq2unit = 'GHz' res = open(needed_param.path + '/rms' + self.freq1name + '.p', 'rb') pick = pickle.load(res) res.close() self.rms.append(pick) res = open(needed_param.path + '/rms' + self.freq2name + '.p', 'rb') pick = pickle.load(res) res.close() self.rms.append(pick) ShiftComponentsWindow.freq1 = self.freq1 ShiftComponentsWindow.freq2 = self.freq2 self.first_contour = [] sigma_cut = float(self.SigmaCut.text()) for i in xrange(0, len(self.rms)): self.first_contour.append(sigma_cut * self.rms[i]) ShiftComponentsWindow.firstCont = self.first_contour #obtaining map centers in pixels cent_mapx = map_data1[5] cent_mapy = map_data1[6] self.mapsize_files = 2 * map_data1[7] #obtaining the four corners of the maps in mas x1 = map_data1[1] x2 = map_data1[2] y1 = map_data1[3] y2 = map_data1[4] ShiftComponentsWindow.ext = [x1, x2, y1, y2] #for plotting the components ShiftComponentsWindow.pts_arr,ShiftComponentsWindow.x_el_arr,ShiftComponentsWindow.x_elH_arr = [],[],[] ShiftComponentsWindow.y_elH_arr,ShiftComponentsWindow.y_el_arr = [],[] ShiftComponentsWindow.xSelected,ShiftComponentsWindow.ySelected = [],[] ShiftComponentsWindow.errxSelected,ShiftComponentsWindow.errySelected = [],[] for i in xrange(0, len(self.checks)): if self.checks[i].isChecked(): ShiftComponentsWindow.pts_arr.append(needed_param.pts_arr[i]) ShiftComponentsWindow.x_el_arr.append(needed_param.x_el_arr[i]) ShiftComponentsWindow.x_elH_arr.append( needed_param.x_elH_arr[i]) ShiftComponentsWindow.y_elH_arr.append( needed_param.y_elH_arr[i]) ShiftComponentsWindow.y_el_arr.append(needed_param.y_el_arr[i]) ShiftComponentsWindow.xSelected.append(needed_param.x[i]) ShiftComponentsWindow.ySelected.append(needed_param.y[i]) ShiftComponentsWindow.errxSelected.append(needed_param.errx[i]) ShiftComponentsWindow.errySelected.append(needed_param.erry[i]) indexComp = selectComponent( realDAT, realDAT2, self.first_contour, ShiftComponentsWindow.pts_arr, ShiftComponentsWindow.x_el_arr, ShiftComponentsWindow.x_elH_arr, ShiftComponentsWindow.y_elH_arr, ShiftComponentsWindow.y_el_arr, ShiftComponentsWindow.ext, self.freq1, self.freq2, ShiftComponentsWindow.xSelected, ShiftComponentsWindow.ySelected, ShiftComponentsWindow.numComp, self.orientation) ShiftComponentsWindow.indexes = [indexComp[0], indexComp[1]] plt.close('all') self.wi = popupAutomaticSelection() self.wi.show() self.wi.YesButton.clicked.connect(lambda: self.GoodCompSelection()) self.wi.NoButton.clicked.connect(lambda: self.BadCompSelection())
from functions_conv import order_by_nu, read_conv_params from functions_align import find_same_beam, beam_array, check_map_params, cuttingMAP, cross_correlation_shifts_FITS, checking_shift from functions_turnover import cuttingTURN, synchrotron, synchrotron_v1, guesses_turnover, guesses_turnoverPoint, guesses_PL, powerLaw, powerLawPlot from functions2 import take_header, read_map, saver from functions2 import convolve_difmap, get_ellipse_coords, Annotate from functions_Bfield import searchNEDnoGUI, B_field, N_UeUb_sigma import os, glob import subprocess as sub from astropy.nddata import Cutout2D from correlate2d import * #from fast_ftts import * filefreq = 'bo/shifted15.fits' header = take_header(filefreq, False) cellsize = header[0] mapdata = read_map(filefreq, False) realDAT = mapdata[0] ext = [mapdata[1], mapdata[2], mapdata[3], mapdata[4]] res = open('turnoverdata.p', 'rb') pick = pickle.load(res) res.close() nu = pick[0] s = pick[1] #*1.4213481404770085 alpha0 = pick[2] alphathick = pick[3] plt.figure(1) ax = plt.gca() cset = plt.contour(realDAT, 0.008 * np.array([2., 4., 16., 64., 256., 1020., 2050.]),
def shifting(self,checkBOXes,fits1,fits2,files,models): self.get_thread = updateGUIshiftText() self.get_thread.start() header1 = take_header(fits1) header2 = take_header(fits2) map_data1 = read_map(fits1) realDAT = map_data1[0] map_data2 = read_map(fits2) realDAT2 = map_data2[0] #obtaining the beam and cell self.bmaj_files = header1[1] self.bmin_files = header1[2] self.bpa_files = header1[3] self.beam_files = header1[7] self.cells_files = header1[0] #obtaining frequencies self.freq1 = header1[5] self.freq2 = header2[5] #obtaining map centers in pixels cent_mapx = map_data1[5] cent_mapy = map_data1[6] self.mapsize_files = 2*map_data1[7] #obtaining the four corners of the maps in mas x1 = map_data1[1] x2 = map_data1[2] y1 = map_data1[3] y2 = map_data1[4] ShiftWindow.ext=[x1,x2,y1,y2] #cut maps v1 < v2, map region to study cut = cuttingMAP(realDAT,realDAT2,cent_mapx,cent_mapy,self.cells_files,self.freq1,self.freq2,iteration=0) cutout_v1 = cut[0] cutout_v2 = cut[1] self.position = cut[2] self.size = cut[3] ShiftWindow.ext = cut[4] #cut maps v1 < v2, feature cut = cuttingMAP(cutout_v1.data,cutout_v2.data,cent_mapx,cent_mapy,self.cells_files,self.freq1,self.freq2,iteration=1) cutout_v1feature = cut[0] cutout_v2feature = cut[1] self.position_feature = cut[2] self.size_feature = cut[3] plt.figure(1) plt.subplot(121) plt.imshow(cutout_v2feature.data, origin='bottom') #plt.axis('scaled') plt.xlabel('Right Ascension [pixels]') plt.ylabel('Relative Declination [pixels]') plt.title(str('%1.3f' %(self.freq2))+' GHz') plt.subplot(122) plt.imshow(cutout_v1feature.data, origin='bottom') #plt.axis('scaled') plt.xlabel('Right Ascension [pixels]') #plt.ylabel('Relative Declination [pixels]') plt.title(str('%1.3f' %(self.freq1))+' GHz') plt.show() #2d cross-correlation image1 = cutout_v1feature.data image2 = cutout_v2feature.data ShiftWindow.offset = cross_correlation_shifts_FITS(image1, image2, sigma_cut=0.004) ShiftWindow.xshift=-ShiftWindow.offset[0]*self.cells_files #mas ShiftWindow.yshift=ShiftWindow.offset[1]*self.cells_files #mas self.shifted_files=[] self.shifted_files.append(needed_param.path+'/SHIFT/'+needed_param.source_name+'-'+str(round(self.freq1))+'convolved_with_beam'+str('%1.2f' % (self.beam_files))+'shifted.fits') self.shifted_files.append(needed_param.path+'/SHIFT/'+needed_param.source_name+'-'+str(round(self.freq2))+'convolved_with_beam'+str('%1.2f' % (self.beam_files))+'shifted.fits') self.models_chosen = [] self.files_chosen = [] for i in xrange(len(checkBOXes)): if checkBOXes[i].isChecked(): self.models_chosen.append(models[i]) self.files_chosen.append(files[i]) self.freqsSHIFTING.setText(("Image2 ( %s GHz) - image1 ( %s GHz)" % ('%1.2f' % (self.freq2), '%1.2f' % (self.freq1)))) self.freqsSHIFTING.setStyleSheet('QLabel {color: blue } QLabel {font: Bold }') self.labelSHIFTraMAS.setText(("%s mas" % ('%1.4f' % (ShiftWindow.xshift)))) self.labelSHIFTraMAS.setStyleSheet('QLabel {color: blue } QLabel {font: Bold }') self.labelSHIFTraPIXEL.setText(("%s pix" % ('%1.2f' % (ShiftWindow.offset[0])))) self.labelSHIFTraPIXEL.setStyleSheet('QLabel {color: blue } QLabel {font: Bold }') self.labelSHIFTdecMAS.setText(("%s mas" % ('%1.4f' % (ShiftWindow.yshift)))) self.labelSHIFTdecMAS.setStyleSheet('QLabel {color: blue } QLabel {font: Bold }') self.labelSHIFTdecPIXEL.setText(("%s pix" % ('%1.2f' % (ShiftWindow.offset[1])))) self.labelSHIFTdecPIXEL.setStyleSheet('QLabel {color: blue } QLabel {font: Bold }') #print 'Image2 (', freq2, ') - image1 (' , freq1, ')' #print 'Shift RA: ', offset[0], 'pixels' #print 'Shift DEC: ', offset[1], 'pixels' #print 'Shift RA: ', xshift, 'mas' #print 'Shift DEC: ', yshift, 'mas' os.system('rm difmap.log*\n') convolve_difmap([self.files_chosen[0]],[self.models_chosen[0]],self.bmaj_files,self.bmin_files,self.bpa_files,ShiftWindow.xshift,ShiftWindow.yshift,self.mapsize_files,self.cells_files,2,-1,[self.shifted_files[0]]) #rms1 (lower freq) self.rms1 = search_rms()[0] os.system('rm difmap.log*\n') convolve_difmap([self.files_chosen[1]],[self.models_chosen[1]],self.bmaj_files,self.bmin_files,self.bpa_files,0,0,self.mapsize_files,self.cells_files,2,-1,[self.shifted_files[1]]) self.rms2 = search_rms()[0] os.system('rm difmap.log*\n') #check if the shift was done properly self.checkingShift()